wasm_loader.c 597 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_platform.h"
  7. #include "wasm.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_runtime.h"
  10. #include "wasm_loader_common.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. #if WASM_ENABLE_MEMORY64 != 0
  36. static bool
  37. has_module_memory64(WASMModule *module)
  38. {
  39. /* TODO: multi-memories for now assuming the memory idx type is consistent
  40. * across multi-memories */
  41. if (module->import_memory_count > 0)
  42. return !!(module->import_memories[0].u.memory.mem_type.flags
  43. & MEMORY64_FLAG);
  44. else if (module->memory_count > 0)
  45. return !!(module->memories[0].flags & MEMORY64_FLAG);
  46. return false;
  47. }
  48. static bool
  49. is_table_64bit(WASMModule *module, uint32 table_idx)
  50. {
  51. if (table_idx < module->import_table_count)
  52. return !!(module->import_tables[table_idx].u.table.table_type.flags
  53. & TABLE64_FLAG);
  54. else
  55. return !!(module->tables[table_idx - module->import_table_count]
  56. .table_type.flags
  57. & TABLE64_FLAG);
  58. return false;
  59. }
  60. #endif
  61. static void
  62. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  63. {
  64. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  65. }
  66. #if WASM_ENABLE_MEMORY64 != 0
  67. static void
  68. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  69. {
  70. if (error_buf != NULL) {
  71. snprintf(error_buf, error_buf_size, "offset out of range");
  72. }
  73. }
  74. #endif
  75. static void
  76. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  77. {
  78. va_list args;
  79. char buf[128];
  80. if (error_buf != NULL) {
  81. va_start(args, format);
  82. vsnprintf(buf, sizeof(buf), format, args);
  83. va_end(args);
  84. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  85. }
  86. }
  87. static bool
  88. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  89. char *error_buf, uint32 error_buf_size)
  90. {
  91. if ((uintptr_t)buf + length < (uintptr_t)buf
  92. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  93. set_error_buf(error_buf, error_buf_size,
  94. "unexpected end of section or function");
  95. return false;
  96. }
  97. return true;
  98. }
  99. static bool
  100. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  101. char *error_buf, uint32 error_buf_size)
  102. {
  103. if ((uintptr_t)buf + length < (uintptr_t)buf
  104. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  105. set_error_buf(error_buf, error_buf_size, "unexpected end");
  106. return false;
  107. }
  108. return true;
  109. }
  110. #define CHECK_BUF(buf, buf_end, length) \
  111. do { \
  112. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  113. goto fail; \
  114. } \
  115. } while (0)
  116. #define CHECK_BUF1(buf, buf_end, length) \
  117. do { \
  118. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  119. goto fail; \
  120. } \
  121. } while (0)
  122. #define skip_leb(p) while (*p++ & 0x80)
  123. #define skip_leb_int64(p, p_end) skip_leb(p)
  124. #define skip_leb_uint32(p, p_end) skip_leb(p)
  125. #define skip_leb_int32(p, p_end) skip_leb(p)
  126. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  127. #define skip_leb_memidx(p, p_end) skip_leb(p)
  128. #if WASM_ENABLE_MULTI_MEMORY == 0
  129. #define skip_leb_align(p, p_end) skip_leb(p)
  130. #else
  131. /* Skip the following memidx if applicable */
  132. #define skip_leb_align(p, p_end) \
  133. do { \
  134. if (*p++ & OPT_MEMIDX_FLAG) \
  135. skip_leb_uint32(p, p_end); \
  136. } while (0)
  137. #endif
  138. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  139. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  140. #define read_leb_int64(p, p_end, res) \
  141. do { \
  142. uint64 res64; \
  143. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  144. error_buf_size)) \
  145. goto fail; \
  146. res = (int64)res64; \
  147. } while (0)
  148. #if WASM_ENABLE_MEMORY64 != 0
  149. #define read_leb_mem_offset(p, p_end, res) \
  150. do { \
  151. uint64 res64; \
  152. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  153. &res64, error_buf, error_buf_size)) { \
  154. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  155. goto fail; \
  156. } \
  157. res = (mem_offset_t)res64; \
  158. } while (0)
  159. #else
  160. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  161. #endif
  162. #define read_leb_uint32(p, p_end, res) \
  163. do { \
  164. uint64 res64; \
  165. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  166. error_buf_size)) \
  167. goto fail; \
  168. res = (uint32)res64; \
  169. } while (0)
  170. #define read_leb_int32(p, p_end, res) \
  171. do { \
  172. uint64 res64; \
  173. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  174. error_buf_size)) \
  175. goto fail; \
  176. res = (int32)res64; \
  177. } while (0)
  178. #if WASM_ENABLE_MULTI_MEMORY != 0
  179. #define check_memidx(module, memidx) \
  180. do { \
  181. if (memidx >= module->import_memory_count + module->memory_count) { \
  182. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  183. memidx); \
  184. goto fail; \
  185. } \
  186. } while (0)
  187. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  188. * alignment check */
  189. #define read_leb_memarg(p, p_end, res) \
  190. do { \
  191. read_leb_uint32(p, p_end, res); \
  192. if (res & OPT_MEMIDX_FLAG) { \
  193. res &= ~OPT_MEMIDX_FLAG; \
  194. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  195. check_memidx(module, memidx); \
  196. } \
  197. } while (0)
  198. #else
  199. /* reserved byte 0x00 */
  200. #define check_memidx(module, memidx) \
  201. do { \
  202. (void)module; \
  203. if (memidx != 0) { \
  204. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  205. goto fail; \
  206. } \
  207. } while (0)
  208. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  209. #endif
  210. static char *
  211. type2str(uint8 type)
  212. {
  213. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  214. #if WASM_ENABLE_GC != 0
  215. char *type_str_ref[] = { "stringview_iter",
  216. "stringview_wtf16",
  217. "(ref null ht)",
  218. "(ref ht)",
  219. "", /* reserved */
  220. "stringview_wtf8",
  221. "stringref",
  222. "", /* reserved */
  223. "", /* reserved */
  224. "arrayref",
  225. "structref",
  226. "i31ref",
  227. "eqref",
  228. "anyref",
  229. "externref",
  230. "funcref",
  231. "nullref",
  232. "nullexternref",
  233. "nullfuncref" };
  234. #endif
  235. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  236. return type_str[type - VALUE_TYPE_V128];
  237. #if WASM_ENABLE_GC != 0
  238. else if (wasm_is_type_reftype(type))
  239. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  240. #endif
  241. else if (type == VALUE_TYPE_FUNCREF)
  242. return "funcref";
  243. else if (type == VALUE_TYPE_EXTERNREF)
  244. return "externref";
  245. else
  246. return "unknown type";
  247. }
  248. static bool
  249. is_32bit_type(uint8 type)
  250. {
  251. if (type == VALUE_TYPE_I32
  252. || type == VALUE_TYPE_F32
  253. /* the operand stack is in polymorphic state */
  254. || type == VALUE_TYPE_ANY
  255. #if WASM_ENABLE_GC != 0
  256. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  257. #elif WASM_ENABLE_REF_TYPES != 0
  258. /* For reference types, we use uint32 index to represent
  259. the funcref and externref */
  260. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  261. #endif
  262. )
  263. return true;
  264. return false;
  265. }
  266. static bool
  267. is_64bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  270. #if WASM_ENABLE_GC != 0
  271. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  272. #endif
  273. )
  274. return true;
  275. return false;
  276. }
  277. #if WASM_ENABLE_GC != 0
  278. static bool
  279. is_packed_type(uint8 type)
  280. {
  281. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  282. }
  283. #endif
  284. static bool
  285. is_byte_a_type(uint8 type)
  286. {
  287. return (is_valid_value_type_for_interpreter(type)
  288. || (type == VALUE_TYPE_VOID))
  289. ? true
  290. : false;
  291. }
  292. #if WASM_ENABLE_SIMD != 0
  293. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  294. static V128
  295. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  296. {
  297. V128 result;
  298. uint8 i;
  299. for (i = 0; i != 16; ++i) {
  300. result.i8x16[i] = read_uint8(p_buf);
  301. }
  302. return result;
  303. }
  304. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  305. #endif /* end of WASM_ENABLE_SIMD */
  306. static void *
  307. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  308. {
  309. void *mem;
  310. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  311. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  312. return NULL;
  313. }
  314. memset(mem, 0, (uint32)size);
  315. return mem;
  316. }
  317. static void *
  318. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  319. uint32 error_buf_size)
  320. {
  321. uint8 *mem_new;
  322. bh_assert(size_new > size_old);
  323. if ((mem_new = wasm_runtime_realloc(mem_old, size_new))) {
  324. memset(mem_new + size_old, 0, size_new - size_old);
  325. return mem_new;
  326. }
  327. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  328. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  329. wasm_runtime_free(mem_old);
  330. }
  331. return mem_new;
  332. }
  333. #define MEM_REALLOC(mem, size_old, size_new) \
  334. do { \
  335. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  336. error_buf_size); \
  337. if (!mem_new) \
  338. goto fail; \
  339. mem = mem_new; \
  340. } while (0)
  341. #if WASM_ENABLE_GC != 0
  342. static bool
  343. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  344. char *error_buf, uint32 error_buf_size)
  345. {
  346. if (type_index >= type_count) {
  347. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  348. type_index);
  349. return false;
  350. }
  351. return true;
  352. }
  353. static bool
  354. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  355. uint32 error_buf_size)
  356. {
  357. if (!check_type_index(module, module->type_count, type_index, error_buf,
  358. error_buf_size)) {
  359. return false;
  360. }
  361. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  362. set_error_buf(error_buf, error_buf_size, "unkown array type");
  363. return false;
  364. }
  365. return true;
  366. }
  367. #endif
  368. static bool
  369. check_function_index(const WASMModule *module, uint32 function_index,
  370. char *error_buf, uint32 error_buf_size)
  371. {
  372. if (function_index
  373. >= module->import_function_count + module->function_count) {
  374. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  375. function_index);
  376. return false;
  377. }
  378. return true;
  379. }
  380. typedef struct InitValue {
  381. uint8 type;
  382. uint8 flag;
  383. #if WASM_ENABLE_GC != 0
  384. uint8 gc_opcode;
  385. WASMRefType ref_type;
  386. #endif
  387. WASMValue value;
  388. } InitValue;
  389. typedef struct ConstExprContext {
  390. uint32 sp;
  391. uint32 size;
  392. WASMModule *module;
  393. InitValue *stack;
  394. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  395. } ConstExprContext;
  396. static void
  397. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  398. {
  399. ctx->sp = 0;
  400. ctx->module = module;
  401. ctx->stack = ctx->data;
  402. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  403. }
  404. static bool
  405. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  406. #if WASM_ENABLE_GC != 0
  407. WASMRefType *ref_type, uint8 gc_opcode,
  408. #endif
  409. WASMValue *value, char *error_buf, uint32 error_buf_size)
  410. {
  411. InitValue *cur_value;
  412. if (ctx->sp >= ctx->size) {
  413. if (ctx->stack != ctx->data) {
  414. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  415. (ctx->size + 4) * sizeof(InitValue));
  416. }
  417. else {
  418. if (!(ctx->stack =
  419. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  420. error_buf, error_buf_size))) {
  421. goto fail;
  422. }
  423. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  424. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  425. }
  426. ctx->size += 4;
  427. }
  428. cur_value = &ctx->stack[ctx->sp++];
  429. cur_value->type = type;
  430. cur_value->flag = flag;
  431. cur_value->value = *value;
  432. #if WASM_ENABLE_GC != 0
  433. cur_value->gc_opcode = gc_opcode;
  434. if (wasm_is_type_multi_byte_type(type)) {
  435. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  436. ref_type, wasm_reftype_struct_size(ref_type));
  437. }
  438. #endif
  439. return true;
  440. fail:
  441. return false;
  442. }
  443. #if WASM_ENABLE_GC != 0
  444. static void
  445. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  446. {
  447. WASMStructNewInitValues *struct_init_values =
  448. (WASMStructNewInitValues *)data;
  449. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  450. WASMType *wasm_type;
  451. uint32 i;
  452. if (!data)
  453. return;
  454. wasm_type = module->types[struct_init_values->type_idx];
  455. /* The data can only be type of `WASMStructNewInitValues *`
  456. or `WASMArrayNewInitValues *` */
  457. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  458. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  459. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  460. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  461. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  462. WASMRefType *ref_type;
  463. uint8 field_type;
  464. for (i = 0; i < struct_init_values->count; i++) {
  465. field_type = struct_type->fields[i].field_type;
  466. if (wasm_is_type_multi_byte_type(field_type))
  467. ref_type = ref_type_map->ref_type;
  468. else
  469. ref_type = NULL;
  470. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  471. REF_TYPE_STRUCTREF, NULL,
  472. module->types, module->type_count)
  473. || wasm_reftype_is_subtype_of(
  474. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  475. module->types, module->type_count)) {
  476. destroy_init_expr_data_recursive(
  477. module, struct_init_values->fields[i].data);
  478. }
  479. }
  480. }
  481. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  482. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  483. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  484. uint8 elem_type = array_type->elem_type;
  485. for (i = 0; i < array_init_values->length; i++) {
  486. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  487. REF_TYPE_STRUCTREF, NULL,
  488. module->types, module->type_count)
  489. || wasm_reftype_is_subtype_of(
  490. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  491. module->types, module->type_count)) {
  492. destroy_init_expr_data_recursive(
  493. module, array_init_values->elem_data[i].data);
  494. }
  495. }
  496. }
  497. wasm_runtime_free(data);
  498. }
  499. #endif
  500. static bool
  501. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  502. #if WASM_ENABLE_GC != 0
  503. WASMRefType *ref_type, uint8 *p_gc_opcode,
  504. #endif
  505. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  506. {
  507. InitValue *cur_value;
  508. if (ctx->sp == 0) {
  509. set_error_buf(error_buf, error_buf_size,
  510. "type mismatch: const expr stack underflow");
  511. return false;
  512. }
  513. cur_value = &ctx->stack[--ctx->sp];
  514. #if WASM_ENABLE_GC == 0
  515. if (cur_value->type != type) {
  516. set_error_buf(error_buf, error_buf_size, "type mismatch");
  517. return false;
  518. }
  519. #else
  520. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  521. ref_type, ctx->module->types,
  522. ctx->module->type_count)) {
  523. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  524. "type mismatch: expect ", type2str(type),
  525. " but got other");
  526. goto fail;
  527. }
  528. #endif
  529. if (p_flag)
  530. *p_flag = cur_value->flag;
  531. if (p_value)
  532. *p_value = cur_value->value;
  533. #if WASM_ENABLE_GC != 0
  534. if (p_gc_opcode)
  535. *p_gc_opcode = cur_value->gc_opcode;
  536. #endif
  537. return true;
  538. #if WASM_ENABLE_GC != 0
  539. fail:
  540. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  541. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  542. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  543. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  544. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  545. }
  546. return false;
  547. #endif
  548. }
  549. static void
  550. destroy_const_expr_stack(ConstExprContext *ctx)
  551. {
  552. #if WASM_ENABLE_GC != 0
  553. uint32 i;
  554. for (i = 0; i < ctx->sp; i++) {
  555. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  556. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  557. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  558. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  559. destroy_init_expr_data_recursive(ctx->module,
  560. ctx->stack[i].value.data);
  561. }
  562. }
  563. #endif
  564. if (ctx->stack != ctx->data) {
  565. wasm_runtime_free(ctx->stack);
  566. }
  567. }
  568. #if WASM_ENABLE_GC != 0
  569. static void
  570. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  571. {
  572. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  573. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  574. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  575. destroy_init_expr_data_recursive(module, expr->u.data);
  576. }
  577. }
  578. #endif /* end of WASM_ENABLE_GC != 0 */
  579. static bool
  580. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  581. InitializerExpression *init_expr, uint8 type, void *ref_type,
  582. char *error_buf, uint32 error_buf_size)
  583. {
  584. const uint8 *p = *p_buf, *p_end = buf_end;
  585. uint8 flag, *p_float;
  586. uint32 i;
  587. ConstExprContext const_expr_ctx = { 0 };
  588. WASMValue cur_value;
  589. #if WASM_ENABLE_GC != 0
  590. uint32 opcode1, type_idx;
  591. uint8 opcode;
  592. WASMRefType cur_ref_type = { 0 };
  593. #endif
  594. init_const_expr_stack(&const_expr_ctx, module);
  595. CHECK_BUF(p, p_end, 1);
  596. flag = read_uint8(p);
  597. while (flag != WASM_OP_END) {
  598. switch (flag) {
  599. /* i32.const */
  600. case INIT_EXPR_TYPE_I32_CONST:
  601. read_leb_int32(p, p_end, cur_value.i32);
  602. if (!push_const_expr_stack(
  603. &const_expr_ctx, flag, VALUE_TYPE_I32,
  604. #if WASM_ENABLE_GC != 0
  605. NULL, 0,
  606. #endif
  607. &cur_value, error_buf, error_buf_size))
  608. goto fail;
  609. break;
  610. /* i64.const */
  611. case INIT_EXPR_TYPE_I64_CONST:
  612. read_leb_int64(p, p_end, cur_value.i64);
  613. if (!push_const_expr_stack(
  614. &const_expr_ctx, flag, VALUE_TYPE_I64,
  615. #if WASM_ENABLE_GC != 0
  616. NULL, 0,
  617. #endif
  618. &cur_value, error_buf, error_buf_size))
  619. goto fail;
  620. break;
  621. /* f32.const */
  622. case INIT_EXPR_TYPE_F32_CONST:
  623. CHECK_BUF(p, p_end, 4);
  624. p_float = (uint8 *)&cur_value.f32;
  625. for (i = 0; i < sizeof(float32); i++)
  626. *p_float++ = *p++;
  627. if (!push_const_expr_stack(
  628. &const_expr_ctx, flag, VALUE_TYPE_F32,
  629. #if WASM_ENABLE_GC != 0
  630. NULL, 0,
  631. #endif
  632. &cur_value, error_buf, error_buf_size))
  633. goto fail;
  634. break;
  635. /* f64.const */
  636. case INIT_EXPR_TYPE_F64_CONST:
  637. CHECK_BUF(p, p_end, 8);
  638. p_float = (uint8 *)&cur_value.f64;
  639. for (i = 0; i < sizeof(float64); i++)
  640. *p_float++ = *p++;
  641. if (!push_const_expr_stack(
  642. &const_expr_ctx, flag, VALUE_TYPE_F64,
  643. #if WASM_ENABLE_GC != 0
  644. NULL, 0,
  645. #endif
  646. &cur_value, error_buf, error_buf_size))
  647. goto fail;
  648. break;
  649. #if WASM_ENABLE_SIMD != 0
  650. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  651. /* v128.const */
  652. case INIT_EXPR_TYPE_V128_CONST:
  653. {
  654. uint64 high, low;
  655. CHECK_BUF(p, p_end, 1);
  656. (void)read_uint8(p);
  657. CHECK_BUF(p, p_end, 16);
  658. wasm_runtime_read_v128(p, &high, &low);
  659. p += 16;
  660. cur_value.v128.i64x2[0] = high;
  661. cur_value.v128.i64x2[1] = low;
  662. if (!push_const_expr_stack(
  663. &const_expr_ctx, flag, VALUE_TYPE_V128,
  664. #if WASM_ENABLE_GC != 0
  665. NULL, 0,
  666. #endif
  667. &cur_value, error_buf, error_buf_size))
  668. goto fail;
  669. #if WASM_ENABLE_WAMR_COMPILER != 0
  670. /* If any init_expr is v128.const, mark SIMD used */
  671. module->is_simd_used = true;
  672. #endif
  673. break;
  674. }
  675. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  676. #endif /* end of WASM_ENABLE_SIMD */
  677. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  678. /* ref.func */
  679. case INIT_EXPR_TYPE_FUNCREF_CONST:
  680. {
  681. uint32 func_idx;
  682. read_leb_uint32(p, p_end, func_idx);
  683. cur_value.ref_index = func_idx;
  684. if (!check_function_index(module, func_idx, error_buf,
  685. error_buf_size)) {
  686. goto fail;
  687. }
  688. #if WASM_ENABLE_GC == 0
  689. if (!push_const_expr_stack(&const_expr_ctx, flag,
  690. VALUE_TYPE_FUNCREF, &cur_value,
  691. error_buf, error_buf_size))
  692. goto fail;
  693. #else
  694. if (func_idx < module->import_function_count) {
  695. type_idx =
  696. module->import_functions[func_idx].u.function.type_idx;
  697. }
  698. else {
  699. type_idx = module
  700. ->functions[func_idx
  701. - module->import_function_count]
  702. ->type_idx;
  703. }
  704. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  705. false, type_idx);
  706. if (!push_const_expr_stack(&const_expr_ctx, flag,
  707. cur_ref_type.ref_type, &cur_ref_type,
  708. 0, &cur_value, error_buf,
  709. error_buf_size))
  710. goto fail;
  711. #endif
  712. #if WASM_ENABLE_WAMR_COMPILER != 0
  713. module->is_ref_types_used = true;
  714. #endif
  715. break;
  716. }
  717. /* ref.null */
  718. case INIT_EXPR_TYPE_REFNULL_CONST:
  719. {
  720. uint8 type1;
  721. CHECK_BUF(p, p_end, 1);
  722. type1 = read_uint8(p);
  723. #if WASM_ENABLE_GC == 0
  724. cur_value.ref_index = NULL_REF;
  725. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  726. &cur_value, error_buf,
  727. error_buf_size))
  728. goto fail;
  729. #else
  730. cur_value.gc_obj = NULL_REF;
  731. if (!is_byte_a_type(type1)) {
  732. p--;
  733. read_leb_uint32(p, p_end, type_idx);
  734. if (!check_type_index(module, module->type_count, type_idx,
  735. error_buf, error_buf_size))
  736. goto fail;
  737. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  738. true, type_idx);
  739. if (!push_const_expr_stack(&const_expr_ctx, flag,
  740. cur_ref_type.ref_type,
  741. &cur_ref_type, 0, &cur_value,
  742. error_buf, error_buf_size))
  743. goto fail;
  744. }
  745. else {
  746. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  747. NULL, 0, &cur_value, error_buf,
  748. error_buf_size))
  749. goto fail;
  750. }
  751. #endif
  752. #if WASM_ENABLE_WAMR_COMPILER != 0
  753. module->is_ref_types_used = true;
  754. #endif
  755. break;
  756. }
  757. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  758. /* get_global */
  759. case INIT_EXPR_TYPE_GET_GLOBAL:
  760. {
  761. uint32 global_idx;
  762. uint8 global_type;
  763. read_leb_uint32(p, p_end, cur_value.global_index);
  764. global_idx = cur_value.global_index;
  765. /*
  766. * Currently, constant expressions occurring as initializers
  767. * of globals are further constrained in that contained
  768. * global.get instructions are
  769. * only allowed to refer to imported globals.
  770. *
  771. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  772. */
  773. if (global_idx >= module->import_global_count
  774. /* make spec test happy */
  775. #if WASM_ENABLE_GC != 0
  776. + module->global_count
  777. #endif
  778. ) {
  779. set_error_buf_v(error_buf, error_buf_size,
  780. "unknown global %u", global_idx);
  781. goto fail;
  782. }
  783. if (
  784. /* make spec test happy */
  785. #if WASM_ENABLE_GC != 0
  786. global_idx < module->import_global_count &&
  787. #endif
  788. module->import_globals[global_idx]
  789. .u.global.type.is_mutable) {
  790. set_error_buf_v(error_buf, error_buf_size,
  791. "constant expression required");
  792. goto fail;
  793. }
  794. if (global_idx < module->import_global_count) {
  795. global_type = module->import_globals[global_idx]
  796. .u.global.type.val_type;
  797. #if WASM_ENABLE_GC != 0
  798. if (wasm_is_type_multi_byte_type(global_type)) {
  799. WASMRefType *global_ref_type =
  800. module->import_globals[global_idx]
  801. .u.global.ref_type;
  802. bh_memcpy_s(&cur_ref_type,
  803. wasm_reftype_struct_size(global_ref_type),
  804. global_ref_type,
  805. wasm_reftype_struct_size(global_ref_type));
  806. }
  807. #endif
  808. }
  809. else {
  810. global_type =
  811. module
  812. ->globals[global_idx - module->import_global_count]
  813. .type.val_type;
  814. #if WASM_ENABLE_GC != 0
  815. if (wasm_is_type_multi_byte_type(global_type)) {
  816. WASMRefType *global_ref_type =
  817. module
  818. ->globals[global_idx
  819. - module->import_global_count]
  820. .ref_type;
  821. bh_memcpy_s(&cur_ref_type,
  822. wasm_reftype_struct_size(global_ref_type),
  823. global_ref_type,
  824. wasm_reftype_struct_size(global_ref_type));
  825. }
  826. #endif
  827. }
  828. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  829. #if WASM_ENABLE_GC != 0
  830. &cur_ref_type, 0,
  831. #endif
  832. &cur_value, error_buf,
  833. error_buf_size))
  834. goto fail;
  835. break;
  836. }
  837. #if WASM_ENABLE_GC != 0
  838. /* struct.new and array.new */
  839. case WASM_OP_GC_PREFIX:
  840. {
  841. read_leb_uint32(p, p_end, opcode1);
  842. switch (opcode1) {
  843. case WASM_OP_STRUCT_NEW:
  844. {
  845. WASMStructType *struct_type;
  846. WASMStructNewInitValues *struct_init_values = NULL;
  847. uint32 field_count;
  848. read_leb_uint32(p, p_end, type_idx);
  849. if (!check_type_index(module, module->type_count,
  850. type_idx, error_buf,
  851. error_buf_size)) {
  852. goto fail;
  853. }
  854. struct_type = (WASMStructType *)module->types[type_idx];
  855. if (struct_type->base_type.type_flag
  856. != WASM_TYPE_STRUCT) {
  857. set_error_buf(error_buf, error_buf_size,
  858. "unkown struct type");
  859. goto fail;
  860. }
  861. field_count = struct_type->field_count;
  862. if (!(struct_init_values = loader_malloc(
  863. offsetof(WASMStructNewInitValues, fields)
  864. + (uint64)field_count * sizeof(WASMValue),
  865. error_buf, error_buf_size))) {
  866. goto fail;
  867. }
  868. struct_init_values->type_idx = type_idx;
  869. struct_init_values->count = field_count;
  870. for (i = field_count; i > 0; i--) {
  871. WASMRefType *field_ref_type = NULL;
  872. uint32 field_idx = i - 1;
  873. uint8 field_type =
  874. struct_type->fields[field_idx].field_type;
  875. if (wasm_is_type_multi_byte_type(field_type)) {
  876. field_ref_type = wasm_reftype_map_find(
  877. struct_type->ref_type_maps,
  878. struct_type->ref_type_map_count, field_idx);
  879. }
  880. if (is_packed_type(field_type)) {
  881. field_type = VALUE_TYPE_I32;
  882. }
  883. if (!pop_const_expr_stack(
  884. &const_expr_ctx, NULL, field_type,
  885. field_ref_type, NULL,
  886. &struct_init_values->fields[field_idx],
  887. error_buf, error_buf_size)) {
  888. destroy_init_expr_data_recursive(
  889. module, struct_init_values);
  890. goto fail;
  891. }
  892. }
  893. cur_value.data = struct_init_values;
  894. wasm_set_refheaptype_typeidx(
  895. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  896. if (!push_const_expr_stack(
  897. &const_expr_ctx, flag, cur_ref_type.ref_type,
  898. &cur_ref_type, (uint8)opcode1, &cur_value,
  899. error_buf, error_buf_size)) {
  900. destroy_init_expr_data_recursive(
  901. module, struct_init_values);
  902. goto fail;
  903. }
  904. break;
  905. }
  906. case WASM_OP_STRUCT_NEW_DEFAULT:
  907. {
  908. read_leb_uint32(p, p_end, cur_value.type_index);
  909. type_idx = cur_value.type_index;
  910. if (!check_type_index(module, module->type_count,
  911. type_idx, error_buf,
  912. error_buf_size)) {
  913. goto fail;
  914. }
  915. if (module->types[type_idx]->type_flag
  916. != WASM_TYPE_STRUCT) {
  917. set_error_buf(error_buf, error_buf_size,
  918. "unkown struct type");
  919. goto fail;
  920. }
  921. cur_value.type_index = type_idx;
  922. cur_value.data = NULL;
  923. wasm_set_refheaptype_typeidx(
  924. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  925. if (!push_const_expr_stack(
  926. &const_expr_ctx, flag, cur_ref_type.ref_type,
  927. &cur_ref_type, (uint8)opcode1, &cur_value,
  928. error_buf, error_buf_size)) {
  929. goto fail;
  930. }
  931. break;
  932. }
  933. case WASM_OP_ARRAY_NEW:
  934. case WASM_OP_ARRAY_NEW_DEFAULT:
  935. case WASM_OP_ARRAY_NEW_FIXED:
  936. {
  937. WASMArrayNewInitValues *array_init_values = NULL;
  938. WASMArrayType *array_type = NULL;
  939. WASMRefType *elem_ref_type = NULL;
  940. uint64 total_size;
  941. uint8 elem_type;
  942. read_leb_uint32(p, p_end, cur_value.type_index);
  943. type_idx = cur_value.type_index;
  944. if (!check_type_index(module, module->type_count,
  945. type_idx, error_buf,
  946. error_buf_size)) {
  947. goto fail;
  948. }
  949. array_type = (WASMArrayType *)module->types[type_idx];
  950. if (array_type->base_type.type_flag
  951. != WASM_TYPE_ARRAY) {
  952. set_error_buf(error_buf, error_buf_size,
  953. "unkown array type");
  954. goto fail;
  955. }
  956. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  957. elem_type = array_type->elem_type;
  958. if (wasm_is_type_multi_byte_type(elem_type)) {
  959. elem_ref_type = array_type->elem_ref_type;
  960. }
  961. if (is_packed_type(elem_type)) {
  962. elem_type = VALUE_TYPE_I32;
  963. }
  964. if (opcode1 == WASM_OP_ARRAY_NEW) {
  965. WASMValue len_val;
  966. if (!(array_init_values = loader_malloc(
  967. sizeof(WASMArrayNewInitValues),
  968. error_buf, error_buf_size))) {
  969. goto fail;
  970. }
  971. array_init_values->type_idx = type_idx;
  972. if (!pop_const_expr_stack(
  973. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  974. NULL, NULL, &len_val, error_buf,
  975. error_buf_size)) {
  976. destroy_init_expr_data_recursive(
  977. module, array_init_values);
  978. goto fail;
  979. }
  980. array_init_values->length = len_val.i32;
  981. if (!pop_const_expr_stack(
  982. &const_expr_ctx, NULL, elem_type,
  983. elem_ref_type, NULL,
  984. &array_init_values->elem_data[0],
  985. error_buf, error_buf_size)) {
  986. destroy_init_expr_data_recursive(
  987. module, array_init_values);
  988. goto fail;
  989. }
  990. cur_value.data = array_init_values;
  991. }
  992. else {
  993. /* WASM_OP_ARRAY_NEW_FIXED */
  994. uint32 len;
  995. read_leb_uint32(p, p_end, len);
  996. total_size =
  997. (uint64)offsetof(WASMArrayNewInitValues,
  998. elem_data)
  999. + (uint64)sizeof(WASMValue) * len;
  1000. if (!(array_init_values =
  1001. loader_malloc(total_size, error_buf,
  1002. error_buf_size))) {
  1003. goto fail;
  1004. }
  1005. array_init_values->type_idx = type_idx;
  1006. array_init_values->length = len;
  1007. for (i = len; i > 0; i--) {
  1008. if (!pop_const_expr_stack(
  1009. &const_expr_ctx, NULL, elem_type,
  1010. elem_ref_type, NULL,
  1011. &array_init_values
  1012. ->elem_data[i - 1],
  1013. error_buf, error_buf_size)) {
  1014. destroy_init_expr_data_recursive(
  1015. module, array_init_values);
  1016. goto fail;
  1017. }
  1018. }
  1019. cur_value.data = array_init_values;
  1020. }
  1021. }
  1022. else {
  1023. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1024. WASMValue len_val;
  1025. uint32 len;
  1026. /* POP(i32) */
  1027. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1028. VALUE_TYPE_I32, NULL,
  1029. NULL, &len_val, error_buf,
  1030. error_buf_size)) {
  1031. goto fail;
  1032. }
  1033. len = len_val.i32;
  1034. cur_value.array_new_default.type_index = type_idx;
  1035. cur_value.array_new_default.length = len;
  1036. }
  1037. wasm_set_refheaptype_typeidx(
  1038. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1039. if (!push_const_expr_stack(
  1040. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1041. &cur_ref_type, (uint8)opcode1, &cur_value,
  1042. error_buf, error_buf_size)) {
  1043. if (array_init_values) {
  1044. destroy_init_expr_data_recursive(
  1045. module, array_init_values);
  1046. }
  1047. goto fail;
  1048. }
  1049. break;
  1050. }
  1051. case WASM_OP_ANY_CONVERT_EXTERN:
  1052. {
  1053. set_error_buf(error_buf, error_buf_size,
  1054. "unsupported constant expression of "
  1055. "extern.internalize");
  1056. goto fail;
  1057. }
  1058. case WASM_OP_EXTERN_CONVERT_ANY:
  1059. {
  1060. set_error_buf(error_buf, error_buf_size,
  1061. "unsupported constant expression of "
  1062. "extern.externalize");
  1063. goto fail;
  1064. }
  1065. case WASM_OP_REF_I31:
  1066. {
  1067. /* POP(i32) */
  1068. if (!pop_const_expr_stack(
  1069. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1070. NULL, &cur_value, error_buf, error_buf_size)) {
  1071. goto fail;
  1072. }
  1073. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1074. false, HEAP_TYPE_I31);
  1075. if (!push_const_expr_stack(
  1076. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1077. &cur_ref_type, (uint8)opcode1, &cur_value,
  1078. error_buf, error_buf_size)) {
  1079. goto fail;
  1080. }
  1081. break;
  1082. }
  1083. default:
  1084. set_error_buf(
  1085. error_buf, error_buf_size,
  1086. "type mismatch or constant expression required");
  1087. goto fail;
  1088. }
  1089. break;
  1090. }
  1091. #endif /* end of WASM_ENABLE_GC != 0 */
  1092. default:
  1093. {
  1094. set_error_buf(error_buf, error_buf_size,
  1095. "illegal opcode "
  1096. "or constant expression required "
  1097. "or type mismatch");
  1098. goto fail;
  1099. }
  1100. }
  1101. CHECK_BUF(p, p_end, 1);
  1102. flag = read_uint8(p);
  1103. }
  1104. /* There should be only one value left on the init value stack */
  1105. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1106. #if WASM_ENABLE_GC != 0
  1107. ref_type, &opcode,
  1108. #endif
  1109. &cur_value, error_buf, error_buf_size)) {
  1110. goto fail;
  1111. }
  1112. if (const_expr_ctx.sp != 0) {
  1113. set_error_buf(error_buf, error_buf_size,
  1114. "type mismatch: illegal constant opcode sequence");
  1115. goto fail;
  1116. }
  1117. init_expr->init_expr_type = flag;
  1118. init_expr->u = cur_value;
  1119. #if WASM_ENABLE_GC != 0
  1120. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1121. switch (opcode) {
  1122. case WASM_OP_STRUCT_NEW:
  1123. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1124. break;
  1125. case WASM_OP_STRUCT_NEW_DEFAULT:
  1126. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1127. break;
  1128. case WASM_OP_ARRAY_NEW:
  1129. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1130. break;
  1131. case WASM_OP_ARRAY_NEW_DEFAULT:
  1132. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1133. break;
  1134. case WASM_OP_ARRAY_NEW_FIXED:
  1135. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1136. break;
  1137. case WASM_OP_REF_I31:
  1138. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1139. break;
  1140. default:
  1141. bh_assert(0);
  1142. break;
  1143. }
  1144. }
  1145. #endif /* end of WASM_ENABLE_GC != 0 */
  1146. *p_buf = p;
  1147. destroy_const_expr_stack(&const_expr_ctx);
  1148. return true;
  1149. fail:
  1150. destroy_const_expr_stack(&const_expr_ctx);
  1151. return false;
  1152. }
  1153. static bool
  1154. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1155. {
  1156. if (mutable >= 2) {
  1157. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1158. return false;
  1159. }
  1160. return true;
  1161. }
  1162. #if WASM_ENABLE_GC != 0
  1163. static void
  1164. destroy_func_type(WASMFuncType *type)
  1165. {
  1166. /* Destroy the reference type hash set */
  1167. if (type->ref_type_maps)
  1168. wasm_runtime_free(type->ref_type_maps);
  1169. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1170. && WASM_ENABLE_LAZY_JIT != 0
  1171. if (type->call_to_llvm_jit_from_fast_jit)
  1172. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1173. #endif
  1174. /* Free the type */
  1175. wasm_runtime_free(type);
  1176. }
  1177. static void
  1178. destroy_struct_type(WASMStructType *type)
  1179. {
  1180. if (type->ref_type_maps)
  1181. wasm_runtime_free(type->ref_type_maps);
  1182. wasm_runtime_free(type);
  1183. }
  1184. static void
  1185. destroy_array_type(WASMArrayType *type)
  1186. {
  1187. wasm_runtime_free(type);
  1188. }
  1189. static void
  1190. destroy_wasm_type(WASMType *type)
  1191. {
  1192. if (type->ref_count > 1) {
  1193. /* The type is referenced by other types
  1194. of current wasm module */
  1195. type->ref_count--;
  1196. return;
  1197. }
  1198. if (type->type_flag == WASM_TYPE_FUNC)
  1199. destroy_func_type((WASMFuncType *)type);
  1200. else if (type->type_flag == WASM_TYPE_STRUCT)
  1201. destroy_struct_type((WASMStructType *)type);
  1202. else if (type->type_flag == WASM_TYPE_ARRAY)
  1203. destroy_array_type((WASMArrayType *)type);
  1204. else {
  1205. bh_assert(0);
  1206. }
  1207. }
  1208. /* Resolve (ref null ht) or (ref ht) */
  1209. static bool
  1210. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1211. WASMModule *module, uint32 type_count, bool nullable,
  1212. WASMRefType *ref_type, char *error_buf,
  1213. uint32 error_buf_size)
  1214. {
  1215. const uint8 *p = *p_buf, *p_end = buf_end;
  1216. ref_type->ref_type =
  1217. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1218. ref_type->ref_ht_common.nullable = nullable;
  1219. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1220. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1221. /* heap type is (type i), i : typeidx, >= 0 */
  1222. if (!check_type_index(module, type_count,
  1223. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1224. error_buf_size)) {
  1225. return false;
  1226. }
  1227. }
  1228. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1229. /* heap type is func, extern, any, eq, i31 or data */
  1230. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1231. return false;
  1232. }
  1233. *p_buf = p;
  1234. return true;
  1235. fail:
  1236. return false;
  1237. }
  1238. static bool
  1239. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1240. WASMModule *module, uint32 type_count,
  1241. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1242. bool allow_packed_type, char *error_buf,
  1243. uint32 error_buf_size)
  1244. {
  1245. const uint8 *p = *p_buf, *p_end = buf_end;
  1246. uint8 type;
  1247. memset(ref_type, 0, sizeof(WASMRefType));
  1248. CHECK_BUF(p, p_end, 1);
  1249. type = read_uint8(p);
  1250. if (wasm_is_reftype_htref_nullable(type)) {
  1251. /* (ref null ht) */
  1252. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1253. ref_type, error_buf, error_buf_size))
  1254. return false;
  1255. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1256. *p_need_ref_type_map = true;
  1257. else {
  1258. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1259. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1260. multi-byte type to one-byte type to reduce the footprint and
  1261. the complexity of type equal/subtype checking */
  1262. ref_type->ref_type =
  1263. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1264. *p_need_ref_type_map = false;
  1265. }
  1266. }
  1267. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1268. /* (ref ht) */
  1269. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1270. ref_type, error_buf, error_buf_size))
  1271. return false;
  1272. *p_need_ref_type_map = true;
  1273. #if WASM_ENABLE_STRINGREF != 0
  1274. /* covert (ref string) to stringref */
  1275. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1276. ref_type->ref_type =
  1277. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1278. *p_need_ref_type_map = false;
  1279. }
  1280. #endif
  1281. }
  1282. else {
  1283. /* type which can be represented by one byte */
  1284. if (!is_valid_value_type_for_interpreter(type)
  1285. && !(allow_packed_type && is_packed_type(type))) {
  1286. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1287. return false;
  1288. }
  1289. ref_type->ref_type = type;
  1290. *p_need_ref_type_map = false;
  1291. #if WASM_ENABLE_WAMR_COMPILER != 0
  1292. /* If any value's type is v128, mark the module as SIMD used */
  1293. if (type == VALUE_TYPE_V128)
  1294. module->is_simd_used = true;
  1295. #endif
  1296. }
  1297. *p_buf = p;
  1298. return true;
  1299. fail:
  1300. return false;
  1301. }
  1302. static WASMRefType *
  1303. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1304. char *error_buf, uint32 error_buf_size)
  1305. {
  1306. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1307. if (!ret) {
  1308. set_error_buf(error_buf, error_buf_size,
  1309. "insert ref type to hash set failed");
  1310. }
  1311. return ret;
  1312. }
  1313. static bool
  1314. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1315. uint32 type_count, uint32 type_idx, char *error_buf,
  1316. uint32 error_buf_size)
  1317. {
  1318. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1319. uint32 param_count, result_count, i, j = 0;
  1320. uint32 param_cell_num, ret_cell_num;
  1321. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1322. uint64 total_size;
  1323. bool need_ref_type_map;
  1324. WASMRefType ref_type;
  1325. WASMFuncType *type = NULL;
  1326. /* Parse first time to resolve param count, result count and
  1327. ref type map count */
  1328. read_leb_uint32(p, p_end, param_count);
  1329. p_org = p;
  1330. for (i = 0; i < param_count; i++) {
  1331. if (!resolve_value_type(&p, p_end, module, type_count,
  1332. &need_ref_type_map, &ref_type, false, error_buf,
  1333. error_buf_size)) {
  1334. return false;
  1335. }
  1336. if (need_ref_type_map)
  1337. ref_type_map_count++;
  1338. }
  1339. read_leb_uint32(p, p_end, result_count);
  1340. for (i = 0; i < result_count; i++) {
  1341. if (!resolve_value_type(&p, p_end, module, type_count,
  1342. &need_ref_type_map, &ref_type, false, error_buf,
  1343. error_buf_size)) {
  1344. return false;
  1345. }
  1346. if (need_ref_type_map) {
  1347. ref_type_map_count++;
  1348. result_ref_type_map_count++;
  1349. }
  1350. }
  1351. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1352. "ref type map count: %d",
  1353. type_idx, param_count, result_count, ref_type_map_count);
  1354. /* Parse second time to resolve param types, result types and
  1355. ref type map info */
  1356. p = p_org;
  1357. total_size = offsetof(WASMFuncType, types)
  1358. + sizeof(uint8) * (uint64)(param_count + result_count);
  1359. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1360. return false;
  1361. }
  1362. if (ref_type_map_count > 0) {
  1363. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1364. if (!(type->ref_type_maps =
  1365. loader_malloc(total_size, error_buf, error_buf_size))) {
  1366. goto fail;
  1367. }
  1368. }
  1369. type->base_type.type_flag = WASM_TYPE_FUNC;
  1370. type->param_count = param_count;
  1371. type->result_count = result_count;
  1372. type->ref_type_map_count = ref_type_map_count;
  1373. if (ref_type_map_count > 0) {
  1374. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1375. - result_ref_type_map_count;
  1376. }
  1377. for (i = 0; i < param_count; i++) {
  1378. if (!resolve_value_type(&p, p_end, module, type_count,
  1379. &need_ref_type_map, &ref_type, false, error_buf,
  1380. error_buf_size)) {
  1381. goto fail;
  1382. }
  1383. type->types[i] = ref_type.ref_type;
  1384. if (need_ref_type_map) {
  1385. type->ref_type_maps[j].index = i;
  1386. if (!(type->ref_type_maps[j++].ref_type =
  1387. reftype_set_insert(module->ref_type_set, &ref_type,
  1388. error_buf, error_buf_size))) {
  1389. goto fail;
  1390. }
  1391. }
  1392. }
  1393. read_leb_uint32(p, p_end, result_count);
  1394. for (i = 0; i < result_count; i++) {
  1395. if (!resolve_value_type(&p, p_end, module, type_count,
  1396. &need_ref_type_map, &ref_type, false, error_buf,
  1397. error_buf_size)) {
  1398. goto fail;
  1399. }
  1400. type->types[param_count + i] = ref_type.ref_type;
  1401. if (need_ref_type_map) {
  1402. type->ref_type_maps[j].index = param_count + i;
  1403. if (!(type->ref_type_maps[j++].ref_type =
  1404. reftype_set_insert(module->ref_type_set, &ref_type,
  1405. error_buf, error_buf_size))) {
  1406. goto fail;
  1407. }
  1408. }
  1409. }
  1410. bh_assert(j == type->ref_type_map_count);
  1411. #if TRACE_WASM_LOADER != 0
  1412. os_printf("type %d = ", type_idx);
  1413. wasm_dump_func_type(type);
  1414. #endif
  1415. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1416. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1417. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1418. set_error_buf(error_buf, error_buf_size,
  1419. "param count or result count too large");
  1420. goto fail;
  1421. }
  1422. type->param_cell_num = (uint16)param_cell_num;
  1423. type->ret_cell_num = (uint16)ret_cell_num;
  1424. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1425. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1426. #endif
  1427. #if WASM_ENABLE_WAMR_COMPILER != 0
  1428. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1429. if (type->types[i] == VALUE_TYPE_V128)
  1430. module->is_simd_used = true;
  1431. }
  1432. #endif
  1433. *p_buf = p;
  1434. module->types[type_idx] = (WASMType *)type;
  1435. return true;
  1436. fail:
  1437. if (type)
  1438. destroy_func_type(type);
  1439. return false;
  1440. }
  1441. static bool
  1442. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1443. WASMModule *module, uint32 type_count, uint32 type_idx,
  1444. char *error_buf, uint32 error_buf_size)
  1445. {
  1446. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1447. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1448. uint32 i, j = 0, offset;
  1449. uint16 *reference_table;
  1450. uint64 total_size;
  1451. uint8 mutable;
  1452. bool need_ref_type_map;
  1453. WASMRefType ref_type;
  1454. WASMStructType *type = NULL;
  1455. /* Parse first time to resolve field count and ref type map count */
  1456. read_leb_uint32(p, p_end, field_count);
  1457. p_org = p;
  1458. for (i = 0; i < field_count; i++) {
  1459. if (!resolve_value_type(&p, p_end, module, type_count,
  1460. &need_ref_type_map, &ref_type, true, error_buf,
  1461. error_buf_size)) {
  1462. return false;
  1463. }
  1464. if (need_ref_type_map)
  1465. ref_type_map_count++;
  1466. if (wasm_is_type_reftype(ref_type.ref_type))
  1467. ref_field_count++;
  1468. CHECK_BUF(p, p_end, 1);
  1469. mutable = read_uint8(p);
  1470. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1471. return false;
  1472. }
  1473. }
  1474. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1475. type_idx, field_count, ref_type_map_count);
  1476. /* Parse second time to resolve field types and ref type map info */
  1477. p = p_org;
  1478. total_size = offsetof(WASMStructType, fields)
  1479. + sizeof(WASMStructFieldType) * (uint64)field_count
  1480. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1481. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1482. return false;
  1483. }
  1484. if (ref_type_map_count > 0) {
  1485. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1486. if (!(type->ref_type_maps =
  1487. loader_malloc(total_size, error_buf, error_buf_size))) {
  1488. goto fail;
  1489. }
  1490. }
  1491. type->reference_table = reference_table =
  1492. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1493. + sizeof(WASMStructFieldType) * field_count);
  1494. *reference_table++ = ref_field_count;
  1495. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1496. type->field_count = field_count;
  1497. type->ref_type_map_count = ref_type_map_count;
  1498. offset = (uint32)offsetof(WASMStructObject, field_data);
  1499. for (i = 0; i < field_count; i++) {
  1500. if (!resolve_value_type(&p, p_end, module, type_count,
  1501. &need_ref_type_map, &ref_type, true, error_buf,
  1502. error_buf_size)) {
  1503. goto fail;
  1504. }
  1505. type->fields[i].field_type = ref_type.ref_type;
  1506. if (need_ref_type_map) {
  1507. type->ref_type_maps[j].index = i;
  1508. if (!(type->ref_type_maps[j++].ref_type =
  1509. reftype_set_insert(module->ref_type_set, &ref_type,
  1510. error_buf, error_buf_size))) {
  1511. goto fail;
  1512. }
  1513. }
  1514. CHECK_BUF(p, p_end, 1);
  1515. type->fields[i].field_flags = read_uint8(p);
  1516. type->fields[i].field_size =
  1517. (uint8)wasm_reftype_size(ref_type.ref_type);
  1518. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1519. || defined(BUILD_TARGET_X86_32))
  1520. if (type->fields[i].field_size == 2)
  1521. offset = align_uint(offset, 2);
  1522. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1523. offset = align_uint(offset, 4);
  1524. #endif
  1525. type->fields[i].field_offset = offset;
  1526. if (wasm_is_type_reftype(ref_type.ref_type))
  1527. *reference_table++ = offset;
  1528. offset += type->fields[i].field_size;
  1529. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1530. type->fields[i].field_flags, type->fields[i].field_type);
  1531. }
  1532. type->total_size = offset;
  1533. bh_assert(j == type->ref_type_map_count);
  1534. #if TRACE_WASM_LOADER != 0
  1535. os_printf("type %d = ", type_idx);
  1536. wasm_dump_struct_type(type);
  1537. #endif
  1538. *p_buf = p;
  1539. module->types[type_idx] = (WASMType *)type;
  1540. return true;
  1541. fail:
  1542. if (type)
  1543. destroy_struct_type(type);
  1544. return false;
  1545. }
  1546. static bool
  1547. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1548. WASMModule *module, uint32 type_count, uint32 type_idx,
  1549. char *error_buf, uint32 error_buf_size)
  1550. {
  1551. const uint8 *p = *p_buf, *p_end = buf_end;
  1552. uint8 mutable;
  1553. bool need_ref_type_map;
  1554. WASMRefType ref_type;
  1555. WASMArrayType *type = NULL;
  1556. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1557. &ref_type, true, error_buf, error_buf_size)) {
  1558. return false;
  1559. }
  1560. CHECK_BUF(p, p_end, 1);
  1561. mutable = read_uint8(p);
  1562. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1563. return false;
  1564. }
  1565. LOG_VERBOSE("type %u: array", type_idx);
  1566. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1567. error_buf_size))) {
  1568. return false;
  1569. }
  1570. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1571. type->elem_flags = mutable;
  1572. type->elem_type = ref_type.ref_type;
  1573. if (need_ref_type_map) {
  1574. if (!(type->elem_ref_type =
  1575. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1576. error_buf_size))) {
  1577. goto fail;
  1578. }
  1579. }
  1580. #if TRACE_WASM_LOADER != 0
  1581. os_printf("type %d = ", type_idx);
  1582. wasm_dump_array_type(type);
  1583. #endif
  1584. *p_buf = p;
  1585. module->types[type_idx] = (WASMType *)type;
  1586. return true;
  1587. fail:
  1588. if (type)
  1589. destroy_array_type(type);
  1590. return false;
  1591. }
  1592. static bool
  1593. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1594. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1595. {
  1596. if (heap_type >= 0) {
  1597. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1598. error_buf_size)) {
  1599. return false;
  1600. }
  1601. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1602. heap_type);
  1603. }
  1604. else {
  1605. if (!wasm_is_valid_heap_type(heap_type)) {
  1606. set_error_buf(error_buf, error_buf_size, "unknown type");
  1607. return false;
  1608. }
  1609. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1610. heap_type);
  1611. if (nullable) {
  1612. /* For (ref null func/extern/any/eq/i31/data),
  1613. they are same as
  1614. funcref/externref/anyref/eqref/i31ref/dataref,
  1615. we convert the multi-byte type to one-byte
  1616. type to reduce the footprint and the
  1617. complexity of type equal/subtype checking */
  1618. ref_type->ref_type =
  1619. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1620. }
  1621. }
  1622. return true;
  1623. }
  1624. static void
  1625. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1626. WASMRefType *ref_type2)
  1627. {
  1628. /**
  1629. * The difference rt1 ∖ rt2 between two reference types is defined as
  1630. * follows:
  1631. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1632. * (ref ht2) = (ref null?1 ht1)
  1633. */
  1634. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1635. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1636. ref_type1, wasm_reftype_struct_size(ref_type1));
  1637. }
  1638. else {
  1639. ref_type_diff->ref_type = ref_type1->ref_type;
  1640. }
  1641. if (ref_type2->ref_ht_common.nullable) {
  1642. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1643. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1644. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1645. (int32)ref_type_diff->ref_type - 0x80);
  1646. }
  1647. else {
  1648. ref_type_diff->ref_ht_typeidx.nullable = false;
  1649. }
  1650. }
  1651. }
  1652. #else /* else of WASM_ENABLE_GC != 0 */
  1653. static void
  1654. destroy_wasm_type(WASMType *type)
  1655. {
  1656. if (type->ref_count > 1) {
  1657. /* The type is referenced by other types
  1658. of current wasm module */
  1659. type->ref_count--;
  1660. return;
  1661. }
  1662. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1663. && WASM_ENABLE_LAZY_JIT != 0
  1664. if (type->call_to_llvm_jit_from_fast_jit)
  1665. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1666. #endif
  1667. wasm_runtime_free(type);
  1668. }
  1669. #endif /* end of WASM_ENABLE_GC != 0 */
  1670. static bool
  1671. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1672. char *error_buf, uint32 error_buf_size)
  1673. {
  1674. const uint8 *p = buf, *p_end = buf_end;
  1675. uint32 type_count, i;
  1676. uint64 total_size;
  1677. uint8 flag;
  1678. #if WASM_ENABLE_GC != 0
  1679. uint32 processed_type_count = 0;
  1680. #endif
  1681. read_leb_uint32(p, p_end, type_count);
  1682. if (type_count) {
  1683. module->type_count = type_count;
  1684. total_size = sizeof(WASMType *) * (uint64)type_count;
  1685. if (!(module->types =
  1686. loader_malloc(total_size, error_buf, error_buf_size))) {
  1687. return false;
  1688. }
  1689. #if WASM_ENABLE_GC == 0
  1690. for (i = 0; i < type_count; i++) {
  1691. WASMFuncType *type;
  1692. const uint8 *p_org;
  1693. uint32 param_count, result_count, j;
  1694. uint32 param_cell_num, ret_cell_num;
  1695. CHECK_BUF(p, p_end, 1);
  1696. flag = read_uint8(p);
  1697. if (flag != 0x60) {
  1698. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1699. return false;
  1700. }
  1701. read_leb_uint32(p, p_end, param_count);
  1702. /* Resolve param count and result count firstly */
  1703. p_org = p;
  1704. CHECK_BUF(p, p_end, param_count);
  1705. p += param_count;
  1706. read_leb_uint32(p, p_end, result_count);
  1707. CHECK_BUF(p, p_end, result_count);
  1708. p = p_org;
  1709. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1710. set_error_buf(error_buf, error_buf_size,
  1711. "param count or result count too large");
  1712. return false;
  1713. }
  1714. total_size = offsetof(WASMFuncType, types)
  1715. + sizeof(uint8) * (uint64)(param_count + result_count);
  1716. if (!(type = module->types[i] =
  1717. loader_malloc(total_size, error_buf, error_buf_size))) {
  1718. return false;
  1719. }
  1720. /* Resolve param types and result types */
  1721. type->ref_count = 1;
  1722. type->param_count = (uint16)param_count;
  1723. type->result_count = (uint16)result_count;
  1724. for (j = 0; j < param_count; j++) {
  1725. CHECK_BUF(p, p_end, 1);
  1726. type->types[j] = read_uint8(p);
  1727. }
  1728. read_leb_uint32(p, p_end, result_count);
  1729. for (j = 0; j < result_count; j++) {
  1730. CHECK_BUF(p, p_end, 1);
  1731. type->types[param_count + j] = read_uint8(p);
  1732. }
  1733. for (j = 0; j < param_count + result_count; j++) {
  1734. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1735. set_error_buf(error_buf, error_buf_size,
  1736. "unknown value type");
  1737. return false;
  1738. }
  1739. }
  1740. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1741. ret_cell_num =
  1742. wasm_get_cell_num(type->types + param_count, result_count);
  1743. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1744. set_error_buf(error_buf, error_buf_size,
  1745. "param count or result count too large");
  1746. return false;
  1747. }
  1748. type->param_cell_num = (uint16)param_cell_num;
  1749. type->ret_cell_num = (uint16)ret_cell_num;
  1750. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1751. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1752. #endif
  1753. #if WASM_ENABLE_WAMR_COMPILER != 0
  1754. for (j = 0; j < type->param_count + type->result_count; j++) {
  1755. if (type->types[j] == VALUE_TYPE_V128)
  1756. module->is_simd_used = true;
  1757. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1758. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1759. module->is_ref_types_used = true;
  1760. }
  1761. #endif
  1762. /* If there is already a same type created, use it instead */
  1763. for (j = 0; j < i; j++) {
  1764. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1765. if (module->types[j]->ref_count == UINT16_MAX) {
  1766. set_error_buf(error_buf, error_buf_size,
  1767. "wasm type's ref count too large");
  1768. return false;
  1769. }
  1770. destroy_wasm_type(type);
  1771. module->types[i] = module->types[j];
  1772. module->types[j]->ref_count++;
  1773. break;
  1774. }
  1775. }
  1776. }
  1777. #else /* else of WASM_ENABLE_GC == 0 */
  1778. for (i = 0; i < type_count; i++) {
  1779. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1780. uint32 rec_count = 1, j;
  1781. bool is_sub_final = true;
  1782. CHECK_BUF(p, p_end, 1);
  1783. flag = read_uint8(p);
  1784. if (flag == DEFINED_TYPE_REC) {
  1785. read_leb_uint32(p, p_end, rec_count);
  1786. if (rec_count > 1) {
  1787. uint64 new_total_size;
  1788. /* integer overflow */
  1789. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1790. set_error_buf(error_buf, error_buf_size,
  1791. "recursive type count too large");
  1792. return false;
  1793. }
  1794. module->type_count += rec_count - 1;
  1795. new_total_size =
  1796. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1797. if (new_total_size > UINT32_MAX) {
  1798. set_error_buf(error_buf, error_buf_size,
  1799. "allocate memory failed");
  1800. return false;
  1801. }
  1802. MEM_REALLOC(module->types, (uint32)total_size,
  1803. (uint32)new_total_size);
  1804. total_size = new_total_size;
  1805. }
  1806. LOG_VERBOSE("Processing rec group [%d-%d]",
  1807. processed_type_count,
  1808. processed_type_count + rec_count - 1);
  1809. }
  1810. else {
  1811. p--;
  1812. }
  1813. for (j = 0; j < rec_count; j++) {
  1814. WASMType *cur_type = NULL;
  1815. CHECK_BUF(p, p_end, 1);
  1816. flag = read_uint8(p);
  1817. parent_type_idx = -1;
  1818. if (flag == DEFINED_TYPE_SUB
  1819. || flag == DEFINED_TYPE_SUB_FINAL) {
  1820. read_leb_uint32(p, p_end, super_type_count);
  1821. if (super_type_count > 1) {
  1822. set_error_buf(error_buf, error_buf_size,
  1823. "super type count too large");
  1824. return false;
  1825. }
  1826. if (super_type_count > 0) {
  1827. read_leb_uint32(p, p_end, parent_type_idx);
  1828. if (parent_type_idx >= processed_type_count + j) {
  1829. set_error_buf_v(error_buf, error_buf_size,
  1830. "unknown type %d", parent_type_idx);
  1831. return false;
  1832. }
  1833. if (module->types[parent_type_idx]->is_sub_final) {
  1834. set_error_buf(error_buf, error_buf_size,
  1835. "sub type can not inherit from "
  1836. "a final super type");
  1837. return false;
  1838. }
  1839. }
  1840. if (flag == DEFINED_TYPE_SUB)
  1841. is_sub_final = false;
  1842. CHECK_BUF(p, p_end, 1);
  1843. flag = read_uint8(p);
  1844. }
  1845. if (flag == DEFINED_TYPE_FUNC) {
  1846. if (!resolve_func_type(&p, buf_end, module,
  1847. processed_type_count + rec_count,
  1848. processed_type_count + j, error_buf,
  1849. error_buf_size)) {
  1850. return false;
  1851. }
  1852. }
  1853. else if (flag == DEFINED_TYPE_STRUCT) {
  1854. if (!resolve_struct_type(&p, buf_end, module,
  1855. processed_type_count + rec_count,
  1856. processed_type_count + j,
  1857. error_buf, error_buf_size)) {
  1858. return false;
  1859. }
  1860. }
  1861. else if (flag == DEFINED_TYPE_ARRAY) {
  1862. if (!resolve_array_type(&p, buf_end, module,
  1863. processed_type_count + rec_count,
  1864. processed_type_count + j, error_buf,
  1865. error_buf_size)) {
  1866. return false;
  1867. }
  1868. }
  1869. else {
  1870. set_error_buf(error_buf, error_buf_size,
  1871. "invalid type flag");
  1872. return false;
  1873. }
  1874. cur_type = module->types[processed_type_count + j];
  1875. cur_type->ref_count = 1;
  1876. cur_type->parent_type_idx = parent_type_idx;
  1877. cur_type->is_sub_final = is_sub_final;
  1878. cur_type->rec_count = rec_count;
  1879. cur_type->rec_idx = j;
  1880. cur_type->rec_begin_type_idx = processed_type_count;
  1881. }
  1882. /* resolve subtyping relationship in current rec group */
  1883. for (j = 0; j < rec_count; j++) {
  1884. WASMType *cur_type = module->types[processed_type_count + j];
  1885. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1886. WASMType *parent_type =
  1887. module->types[cur_type->parent_type_idx];
  1888. cur_type->parent_type = parent_type;
  1889. cur_type->root_type = parent_type->root_type;
  1890. if (parent_type->inherit_depth == UINT16_MAX) {
  1891. set_error_buf(error_buf, error_buf_size,
  1892. "parent type's inherit depth too large");
  1893. return false;
  1894. }
  1895. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1896. }
  1897. else {
  1898. cur_type->parent_type = NULL;
  1899. cur_type->root_type = cur_type;
  1900. cur_type->inherit_depth = 0;
  1901. }
  1902. }
  1903. for (j = 0; j < rec_count; j++) {
  1904. WASMType *cur_type = module->types[processed_type_count + j];
  1905. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1906. WASMType *parent_type =
  1907. module->types[cur_type->parent_type_idx];
  1908. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1909. module->types,
  1910. module->type_count)) {
  1911. set_error_buf_v(error_buf, error_buf_size,
  1912. "sub type %u does not match super type",
  1913. processed_type_count + j);
  1914. return false;
  1915. }
  1916. }
  1917. }
  1918. /* If there is already an equivalence type or a group of equivalence
  1919. recursive types created, use it or them instead */
  1920. for (j = 0; j < processed_type_count;) {
  1921. WASMType *src_type = module->types[j];
  1922. WASMType *cur_type = module->types[processed_type_count];
  1923. uint32 k, src_rec_count;
  1924. src_rec_count = src_type->rec_count;
  1925. if (src_rec_count != rec_count) {
  1926. /* no type equivalence */
  1927. j += src_rec_count;
  1928. continue;
  1929. }
  1930. for (k = 0; k < rec_count; k++) {
  1931. src_type = module->types[j + k];
  1932. cur_type = module->types[processed_type_count + k];
  1933. if (!wasm_type_equal(src_type, cur_type, module->types,
  1934. module->type_count)) {
  1935. break;
  1936. }
  1937. }
  1938. if (k < rec_count) {
  1939. /* no type equivalence */
  1940. j += src_rec_count;
  1941. continue;
  1942. }
  1943. /* type equivalence */
  1944. for (k = 0; k < rec_count; k++) {
  1945. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1946. set_error_buf(error_buf, error_buf_size,
  1947. "wasm type's ref count too large");
  1948. return false;
  1949. }
  1950. destroy_wasm_type(module->types[processed_type_count + k]);
  1951. module->types[processed_type_count + k] =
  1952. module->types[j + k];
  1953. module->types[j + k]->ref_count++;
  1954. }
  1955. break;
  1956. }
  1957. if (rec_count > 1) {
  1958. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1959. processed_type_count,
  1960. processed_type_count + rec_count - 1);
  1961. }
  1962. processed_type_count += rec_count;
  1963. }
  1964. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1965. * module->type_count,
  1966. error_buf, error_buf_size))) {
  1967. return false;
  1968. }
  1969. #endif /* end of WASM_ENABLE_GC == 0 */
  1970. }
  1971. if (p != p_end) {
  1972. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1973. return false;
  1974. }
  1975. LOG_VERBOSE("Load type section success.\n");
  1976. return true;
  1977. fail:
  1978. return false;
  1979. }
  1980. static void
  1981. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  1982. uint32 *max_size)
  1983. {
  1984. uint32 default_max_size;
  1985. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  1986. * ABI unchanged */
  1987. (void)is_table64;
  1988. if (UINT32_MAX / 2 > init_size)
  1989. default_max_size = init_size * 2;
  1990. else
  1991. default_max_size = UINT32_MAX;
  1992. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1993. default_max_size = WASM_TABLE_MAX_SIZE;
  1994. if (max_size_flag) {
  1995. /* module defines the table limitation */
  1996. bh_assert(init_size <= *max_size);
  1997. if (init_size < *max_size) {
  1998. *max_size =
  1999. *max_size < default_max_size ? *max_size : default_max_size;
  2000. }
  2001. }
  2002. else {
  2003. /* partial defined table limitation, gives a default value */
  2004. *max_size = default_max_size;
  2005. }
  2006. }
  2007. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2008. /**
  2009. * Find export item of a module with export info:
  2010. * module name, field name and export kind
  2011. */
  2012. static WASMExport *
  2013. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2014. const char *field_name, uint8 export_kind,
  2015. char *error_buf, uint32 error_buf_size)
  2016. {
  2017. WASMExport *export =
  2018. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2019. export_kind, error_buf, error_buf_size);
  2020. return export;
  2021. }
  2022. #endif
  2023. #if WASM_ENABLE_MULTI_MODULE != 0
  2024. static WASMTable *
  2025. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2026. uint32 init_size, uint32 max_size, char *error_buf,
  2027. uint32 error_buf_size)
  2028. {
  2029. WASMModuleCommon *module_reg;
  2030. WASMTable *table = NULL;
  2031. WASMExport *export = NULL;
  2032. WASMModule *module = NULL;
  2033. module_reg = wasm_runtime_find_module_registered(module_name);
  2034. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2035. LOG_DEBUG("can not find a module named %s for table", module_name);
  2036. set_error_buf(error_buf, error_buf_size, "unknown import");
  2037. return NULL;
  2038. }
  2039. module = (WASMModule *)module_reg;
  2040. export =
  2041. wasm_loader_find_export(module, module_name, table_name,
  2042. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2043. if (!export) {
  2044. return NULL;
  2045. }
  2046. /* resolve table and check the init/max size */
  2047. if (export->index < module->import_table_count) {
  2048. table =
  2049. module->import_tables[export->index].u.table.import_table_linked;
  2050. }
  2051. else {
  2052. table = &(module->tables[export->index - module->import_table_count]);
  2053. }
  2054. if (table->table_type.init_size < init_size
  2055. || table->table_type.max_size > max_size) {
  2056. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2057. module_name, table_name, table->table_type.init_size,
  2058. table->table_type.max_size, init_size, max_size);
  2059. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2060. return NULL;
  2061. }
  2062. return table;
  2063. }
  2064. static WASMMemory *
  2065. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2066. uint32 init_page_count, uint32 max_page_count,
  2067. char *error_buf, uint32 error_buf_size)
  2068. {
  2069. WASMModuleCommon *module_reg;
  2070. WASMMemory *memory = NULL;
  2071. WASMExport *export = NULL;
  2072. WASMModule *module = NULL;
  2073. module_reg = wasm_runtime_find_module_registered(module_name);
  2074. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2075. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2076. set_error_buf(error_buf, error_buf_size, "unknown import");
  2077. return NULL;
  2078. }
  2079. module = (WASMModule *)module_reg;
  2080. export =
  2081. wasm_loader_find_export(module, module_name, memory_name,
  2082. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2083. if (!export) {
  2084. return NULL;
  2085. }
  2086. /* resolve memory and check the init/max page count */
  2087. if (export->index < module->import_memory_count) {
  2088. memory = module->import_memories[export->index]
  2089. .u.memory.import_memory_linked;
  2090. }
  2091. else {
  2092. memory =
  2093. &(module->memories[export->index - module->import_memory_count]);
  2094. }
  2095. if (memory->init_page_count < init_page_count
  2096. || memory->max_page_count > max_page_count) {
  2097. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2098. module_name, memory_name, memory->init_page_count,
  2099. memory->max_page_count, init_page_count, max_page_count);
  2100. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2101. return NULL;
  2102. }
  2103. return memory;
  2104. }
  2105. static WASMGlobal *
  2106. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2107. uint8 type, bool is_mutable, char *error_buf,
  2108. uint32 error_buf_size)
  2109. {
  2110. WASMModuleCommon *module_reg;
  2111. WASMGlobal *global = NULL;
  2112. WASMExport *export = NULL;
  2113. WASMModule *module = NULL;
  2114. module_reg = wasm_runtime_find_module_registered(module_name);
  2115. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2116. LOG_DEBUG("can not find a module named %s for global", module_name);
  2117. set_error_buf(error_buf, error_buf_size, "unknown import");
  2118. return NULL;
  2119. }
  2120. module = (WASMModule *)module_reg;
  2121. export =
  2122. wasm_loader_find_export(module, module_name, global_name,
  2123. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2124. if (!export) {
  2125. return NULL;
  2126. }
  2127. /* resolve and check the global */
  2128. if (export->index < module->import_global_count) {
  2129. global =
  2130. module->import_globals[export->index].u.global.import_global_linked;
  2131. }
  2132. else {
  2133. global =
  2134. &(module->globals[export->index - module->import_global_count]);
  2135. }
  2136. if (global->type.val_type != type
  2137. || global->type.is_mutable != is_mutable) {
  2138. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2139. module_name, global_name, global->type.val_type,
  2140. global->type.is_mutable, type, is_mutable);
  2141. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2142. return NULL;
  2143. }
  2144. return global;
  2145. }
  2146. #if WASM_ENABLE_TAGS != 0
  2147. static WASMTag *
  2148. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2149. const WASMType *expected_tag_type,
  2150. uint32 *linked_tag_index, char *error_buf,
  2151. uint32 error_buf_size)
  2152. {
  2153. WASMModuleCommon *module_reg;
  2154. WASMTag *tag = NULL;
  2155. WASMExport *export = NULL;
  2156. WASMModule *module = NULL;
  2157. module_reg = wasm_runtime_find_module_registered(module_name);
  2158. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2159. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2160. tag_name);
  2161. set_error_buf(error_buf, error_buf_size, "unknown import");
  2162. return NULL;
  2163. }
  2164. module = (WASMModule *)module_reg;
  2165. export =
  2166. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2167. error_buf, error_buf_size);
  2168. if (!export) {
  2169. return NULL;
  2170. }
  2171. /* resolve tag type and tag */
  2172. if (export->index < module->import_tag_count) {
  2173. /* importing an imported tag from the submodule */
  2174. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2175. }
  2176. else {
  2177. /* importing an section tag from the submodule */
  2178. tag = module->tags[export->index - module->import_tag_count];
  2179. }
  2180. /* check function type */
  2181. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2182. module->type_count)) {
  2183. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2184. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2185. return NULL;
  2186. }
  2187. if (linked_tag_index != NULL) {
  2188. *linked_tag_index = export->index;
  2189. }
  2190. return tag;
  2191. }
  2192. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2193. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2194. static bool
  2195. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2196. const WASMModule *parent_module,
  2197. const char *sub_module_name, const char *function_name,
  2198. WASMFunctionImport *function, bool no_resolve,
  2199. char *error_buf, uint32 error_buf_size)
  2200. {
  2201. const uint8 *p = *p_buf, *p_end = buf_end;
  2202. uint32 declare_type_index = 0;
  2203. read_leb_uint32(p, p_end, declare_type_index);
  2204. *p_buf = p;
  2205. if (declare_type_index >= parent_module->type_count) {
  2206. set_error_buf(error_buf, error_buf_size, "unknown type");
  2207. return false;
  2208. }
  2209. #if WASM_ENABLE_GC != 0
  2210. function->type_idx = declare_type_index;
  2211. #endif
  2212. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2213. declare_type_index = wasm_get_smallest_type_idx(
  2214. parent_module->types, parent_module->type_count, declare_type_index);
  2215. #endif
  2216. function->func_type =
  2217. (WASMFuncType *)parent_module->types[declare_type_index];
  2218. function->module_name = (char *)sub_module_name;
  2219. function->field_name = (char *)function_name;
  2220. function->attachment = NULL;
  2221. function->signature = NULL;
  2222. function->call_conv_raw = false;
  2223. /* lookup registered native symbols first */
  2224. if (!no_resolve) {
  2225. wasm_resolve_import_func(parent_module, function);
  2226. }
  2227. return true;
  2228. fail:
  2229. return false;
  2230. }
  2231. static bool
  2232. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2233. uint32 error_buf_size)
  2234. {
  2235. if (max_size < init_size) {
  2236. set_error_buf(error_buf, error_buf_size,
  2237. "size minimum must not be greater than maximum");
  2238. return false;
  2239. }
  2240. return true;
  2241. }
  2242. static bool
  2243. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2244. WASMModule *parent_module, const char *sub_module_name,
  2245. const char *table_name, WASMTableImport *table,
  2246. char *error_buf, uint32 error_buf_size)
  2247. {
  2248. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2249. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2250. declare_max_size = 0;
  2251. #if WASM_ENABLE_MULTI_MODULE != 0
  2252. WASMModule *sub_module = NULL;
  2253. WASMTable *linked_table = NULL;
  2254. #endif
  2255. #if WASM_ENABLE_GC != 0
  2256. WASMRefType ref_type;
  2257. bool need_ref_type_map;
  2258. #endif
  2259. bool is_table64 = false;
  2260. #if WASM_ENABLE_GC == 0
  2261. CHECK_BUF(p, p_end, 1);
  2262. /* 0x70 or 0x6F */
  2263. declare_elem_type = read_uint8(p);
  2264. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2265. #if WASM_ENABLE_REF_TYPES != 0
  2266. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2267. #endif
  2268. ) {
  2269. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2270. return false;
  2271. }
  2272. #else /* else of WASM_ENABLE_GC == 0 */
  2273. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2274. &need_ref_type_map, &ref_type, false, error_buf,
  2275. error_buf_size)) {
  2276. return false;
  2277. }
  2278. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2279. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2280. return false;
  2281. }
  2282. declare_elem_type = ref_type.ref_type;
  2283. if (need_ref_type_map) {
  2284. if (!(table->table_type.elem_ref_type =
  2285. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2286. error_buf, error_buf_size))) {
  2287. return false;
  2288. }
  2289. }
  2290. #if TRACE_WASM_LOADER != 0
  2291. os_printf("import table type: ");
  2292. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2293. os_printf("\n");
  2294. #endif
  2295. #endif /* end of WASM_ENABLE_GC == 0 */
  2296. p_org = p;
  2297. read_leb_uint32(p, p_end, table_flag);
  2298. is_table64 = table_flag & TABLE64_FLAG;
  2299. if (p - p_org > 1) {
  2300. LOG_VERBOSE("integer representation too long(import table)");
  2301. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2302. return false;
  2303. }
  2304. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2305. return false;
  2306. }
  2307. read_leb_uint32(p, p_end, declare_init_size);
  2308. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2309. read_leb_uint32(p, p_end, declare_max_size);
  2310. if (!check_table_max_size(declare_init_size, declare_max_size,
  2311. error_buf, error_buf_size))
  2312. return false;
  2313. }
  2314. adjust_table_max_size(is_table64, declare_init_size,
  2315. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2316. *p_buf = p;
  2317. #if WASM_ENABLE_MULTI_MODULE != 0
  2318. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2319. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2320. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2321. error_buf_size);
  2322. if (sub_module) {
  2323. linked_table = wasm_loader_resolve_table(
  2324. sub_module_name, table_name, declare_init_size,
  2325. declare_max_size, error_buf, error_buf_size);
  2326. if (linked_table) {
  2327. /* reset with linked table limit */
  2328. declare_elem_type = linked_table->table_type.elem_type;
  2329. declare_init_size = linked_table->table_type.init_size;
  2330. declare_max_size = linked_table->table_type.max_size;
  2331. table_flag = linked_table->table_type.flags;
  2332. table->import_table_linked = linked_table;
  2333. table->import_module = sub_module;
  2334. }
  2335. }
  2336. }
  2337. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2338. /* (table (export "table") 10 20 funcref) */
  2339. /* (table (export "table64") 10 20 funcref) */
  2340. /* we need this section working in wamrc */
  2341. if (!strcmp("spectest", sub_module_name)) {
  2342. const uint32 spectest_table_init_size = 10;
  2343. const uint32 spectest_table_max_size = 20;
  2344. if (strcmp("table", table_name)
  2345. #if WASM_ENABLE_MEMORY64 != 0
  2346. && strcmp("table64", table_name)
  2347. #endif
  2348. ) {
  2349. set_error_buf(error_buf, error_buf_size,
  2350. "incompatible import type or unknown import");
  2351. return false;
  2352. }
  2353. if (declare_init_size > spectest_table_init_size
  2354. || declare_max_size < spectest_table_max_size) {
  2355. set_error_buf(error_buf, error_buf_size,
  2356. "incompatible import type");
  2357. return false;
  2358. }
  2359. declare_init_size = spectest_table_init_size;
  2360. declare_max_size = spectest_table_max_size;
  2361. }
  2362. /* now we believe all declaration are ok */
  2363. table->table_type.elem_type = declare_elem_type;
  2364. table->table_type.init_size = declare_init_size;
  2365. table->table_type.flags = table_flag;
  2366. table->table_type.max_size = declare_max_size;
  2367. #if WASM_ENABLE_WAMR_COMPILER != 0
  2368. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2369. parent_module->is_ref_types_used = true;
  2370. #endif
  2371. (void)parent_module;
  2372. return true;
  2373. fail:
  2374. return false;
  2375. }
  2376. static bool
  2377. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2378. uint32 error_buf_size)
  2379. {
  2380. uint32 default_max_size =
  2381. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2382. if (!is_memory64 && init_size > default_max_size) {
  2383. set_error_buf(error_buf, error_buf_size,
  2384. "memory size must be at most 65536 pages (4GiB)");
  2385. return false;
  2386. }
  2387. #if WASM_ENABLE_MEMORY64 != 0
  2388. else if (is_memory64 && init_size > default_max_size) {
  2389. set_error_buf(
  2390. error_buf, error_buf_size,
  2391. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2392. return false;
  2393. }
  2394. #endif
  2395. return true;
  2396. }
  2397. static bool
  2398. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2399. char *error_buf, uint32 error_buf_size)
  2400. {
  2401. uint32 default_max_size =
  2402. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2403. if (max_size < init_size) {
  2404. set_error_buf(error_buf, error_buf_size,
  2405. "size minimum must not be greater than maximum");
  2406. return false;
  2407. }
  2408. if (!is_memory64 && max_size > default_max_size) {
  2409. set_error_buf(error_buf, error_buf_size,
  2410. "memory size must be at most 65536 pages (4GiB)");
  2411. return false;
  2412. }
  2413. #if WASM_ENABLE_MEMORY64 != 0
  2414. else if (is_memory64 && max_size > default_max_size) {
  2415. set_error_buf(
  2416. error_buf, error_buf_size,
  2417. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2418. return false;
  2419. }
  2420. #endif
  2421. return true;
  2422. }
  2423. static bool
  2424. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2425. WASMModule *parent_module, const char *sub_module_name,
  2426. const char *memory_name, WASMMemoryImport *memory,
  2427. char *error_buf, uint32 error_buf_size)
  2428. {
  2429. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2430. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2431. uint32 pool_size = wasm_runtime_memory_pool_size();
  2432. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2433. / DEFAULT_NUM_BYTES_PER_PAGE;
  2434. #else
  2435. uint32 max_page_count;
  2436. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2437. uint32 mem_flag = 0;
  2438. bool is_memory64 = false;
  2439. uint32 declare_init_page_count = 0;
  2440. uint32 declare_max_page_count = 0;
  2441. #if WASM_ENABLE_MULTI_MODULE != 0
  2442. WASMModule *sub_module = NULL;
  2443. WASMMemory *linked_memory = NULL;
  2444. #endif
  2445. p_org = p;
  2446. read_leb_uint32(p, p_end, mem_flag);
  2447. is_memory64 = mem_flag & MEMORY64_FLAG;
  2448. if (p - p_org > 1) {
  2449. LOG_VERBOSE("integer representation too long(import memory)");
  2450. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2451. return false;
  2452. }
  2453. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2454. return false;
  2455. }
  2456. read_leb_uint32(p, p_end, declare_init_page_count);
  2457. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2458. error_buf_size)) {
  2459. return false;
  2460. }
  2461. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2462. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2463. #endif
  2464. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2465. read_leb_uint32(p, p_end, declare_max_page_count);
  2466. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2467. declare_max_page_count, error_buf,
  2468. error_buf_size)) {
  2469. return false;
  2470. }
  2471. if (declare_max_page_count > max_page_count) {
  2472. declare_max_page_count = max_page_count;
  2473. }
  2474. }
  2475. else {
  2476. /* Limit the maximum memory size to max_page_count */
  2477. declare_max_page_count = max_page_count;
  2478. }
  2479. #if WASM_ENABLE_MULTI_MODULE != 0
  2480. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2481. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2482. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2483. error_buf_size);
  2484. if (sub_module) {
  2485. linked_memory = wasm_loader_resolve_memory(
  2486. sub_module_name, memory_name, declare_init_page_count,
  2487. declare_max_page_count, error_buf, error_buf_size);
  2488. if (linked_memory) {
  2489. /**
  2490. * reset with linked memory limit
  2491. */
  2492. memory->import_module = sub_module;
  2493. memory->import_memory_linked = linked_memory;
  2494. declare_init_page_count = linked_memory->init_page_count;
  2495. declare_max_page_count = linked_memory->max_page_count;
  2496. }
  2497. }
  2498. }
  2499. #endif
  2500. /* (memory (export "memory") 1 2) */
  2501. if (!strcmp("spectest", sub_module_name)) {
  2502. uint32 spectest_memory_init_page = 1;
  2503. uint32 spectest_memory_max_page = 2;
  2504. if (strcmp("memory", memory_name)) {
  2505. set_error_buf(error_buf, error_buf_size,
  2506. "incompatible import type or unknown import");
  2507. return false;
  2508. }
  2509. if (declare_init_page_count > spectest_memory_init_page
  2510. || declare_max_page_count < spectest_memory_max_page) {
  2511. set_error_buf(error_buf, error_buf_size,
  2512. "incompatible import type");
  2513. return false;
  2514. }
  2515. declare_init_page_count = spectest_memory_init_page;
  2516. declare_max_page_count = spectest_memory_max_page;
  2517. }
  2518. #if WASM_ENABLE_WASI_TEST != 0
  2519. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2520. else if (!strcmp("foo", sub_module_name)) {
  2521. uint32 spectest_memory_init_page = 1;
  2522. uint32 spectest_memory_max_page = 1;
  2523. if (strcmp("bar", memory_name)) {
  2524. set_error_buf(error_buf, error_buf_size,
  2525. "incompatible import type or unknown import");
  2526. return false;
  2527. }
  2528. if (declare_init_page_count > spectest_memory_init_page
  2529. || declare_max_page_count < spectest_memory_max_page) {
  2530. set_error_buf(error_buf, error_buf_size,
  2531. "incompatible import type");
  2532. return false;
  2533. }
  2534. declare_init_page_count = spectest_memory_init_page;
  2535. declare_max_page_count = spectest_memory_max_page;
  2536. }
  2537. #endif
  2538. /* now we believe all declaration are ok */
  2539. memory->mem_type.flags = mem_flag;
  2540. memory->mem_type.init_page_count = declare_init_page_count;
  2541. memory->mem_type.max_page_count = declare_max_page_count;
  2542. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2543. *p_buf = p;
  2544. (void)parent_module;
  2545. return true;
  2546. fail:
  2547. return false;
  2548. }
  2549. #if WASM_ENABLE_TAGS != 0
  2550. static bool
  2551. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2552. const WASMModule *parent_module, /* this module ! */
  2553. const char *sub_module_name, const char *tag_name,
  2554. WASMTagImport *tag, /* structure to fill */
  2555. char *error_buf, uint32 error_buf_size)
  2556. {
  2557. /* attribute and type of the import statement */
  2558. uint8 declare_tag_attribute;
  2559. uint32 declare_type_index;
  2560. const uint8 *p = *p_buf, *p_end = buf_end;
  2561. #if WASM_ENABLE_MULTI_MODULE != 0
  2562. WASMModule *sub_module = NULL;
  2563. #endif
  2564. /* get the one byte attribute */
  2565. CHECK_BUF(p, p_end, 1);
  2566. declare_tag_attribute = read_uint8(p);
  2567. if (declare_tag_attribute != 0) {
  2568. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2569. goto fail;
  2570. }
  2571. /* get type */
  2572. read_leb_uint32(p, p_end, declare_type_index);
  2573. /* compare against module->types */
  2574. if (declare_type_index >= parent_module->type_count) {
  2575. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2576. goto fail;
  2577. }
  2578. WASMFuncType *declare_tag_type =
  2579. (WASMFuncType *)parent_module->types[declare_type_index];
  2580. /* check, that the type of the declared tag returns void */
  2581. if (declare_tag_type->result_count != 0) {
  2582. set_error_buf(error_buf, error_buf_size,
  2583. "tag type signature does not return void");
  2584. goto fail;
  2585. }
  2586. #if WASM_ENABLE_MULTI_MODULE != 0
  2587. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2588. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2589. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2590. error_buf_size);
  2591. if (sub_module) {
  2592. /* wasm_loader_resolve_tag checks, that the imported tag
  2593. * and the declared tag have the same type
  2594. */
  2595. uint32 linked_tag_index = 0;
  2596. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2597. sub_module_name, tag_name, declare_tag_type,
  2598. &linked_tag_index /* out */, error_buf, error_buf_size);
  2599. if (linked_tag) {
  2600. tag->import_module = sub_module;
  2601. tag->import_tag_linked = linked_tag;
  2602. tag->import_tag_index_linked = linked_tag_index;
  2603. }
  2604. }
  2605. }
  2606. #endif
  2607. /* store to module tag declarations */
  2608. tag->attribute = declare_tag_attribute;
  2609. tag->type = declare_type_index;
  2610. tag->module_name = (char *)sub_module_name;
  2611. tag->field_name = (char *)tag_name;
  2612. tag->tag_type = declare_tag_type;
  2613. *p_buf = p;
  2614. (void)parent_module;
  2615. LOG_VERBOSE("Load tag import success\n");
  2616. return true;
  2617. fail:
  2618. return false;
  2619. }
  2620. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2621. static bool
  2622. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2623. WASMModule *parent_module, char *sub_module_name,
  2624. char *global_name, WASMGlobalImport *global, char *error_buf,
  2625. uint32 error_buf_size)
  2626. {
  2627. const uint8 *p = *p_buf, *p_end = buf_end;
  2628. uint8 declare_type = 0;
  2629. uint8 declare_mutable = 0;
  2630. #if WASM_ENABLE_MULTI_MODULE != 0
  2631. WASMModule *sub_module = NULL;
  2632. WASMGlobal *linked_global = NULL;
  2633. #endif
  2634. #if WASM_ENABLE_GC != 0
  2635. WASMRefType ref_type;
  2636. bool need_ref_type_map;
  2637. #endif
  2638. bool ret = false;
  2639. #if WASM_ENABLE_GC == 0
  2640. CHECK_BUF(p, p_end, 2);
  2641. /* global type */
  2642. declare_type = read_uint8(p);
  2643. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2644. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2645. return false;
  2646. }
  2647. declare_mutable = read_uint8(p);
  2648. #else
  2649. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2650. &need_ref_type_map, &ref_type, false, error_buf,
  2651. error_buf_size)) {
  2652. return false;
  2653. }
  2654. declare_type = ref_type.ref_type;
  2655. if (need_ref_type_map) {
  2656. if (!(global->ref_type =
  2657. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2658. error_buf, error_buf_size))) {
  2659. return false;
  2660. }
  2661. }
  2662. #if TRACE_WASM_LOADER != 0
  2663. os_printf("import global type: ");
  2664. wasm_dump_value_type(declare_type, global->ref_type);
  2665. os_printf("\n");
  2666. #endif
  2667. CHECK_BUF(p, p_end, 1);
  2668. declare_mutable = read_uint8(p);
  2669. #endif /* end of WASM_ENABLE_GC == 0 */
  2670. *p_buf = p;
  2671. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2672. return false;
  2673. }
  2674. #if WASM_ENABLE_MULTI_MODULE != 0
  2675. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2676. /* link with libc-builtin provided */
  2677. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2678. global);
  2679. if (ret) {
  2680. if (global->type.val_type != declare_type
  2681. || global->type.is_mutable != declare_mutable) {
  2682. set_error_buf(error_buf, error_buf_size,
  2683. "incompatible import type");
  2684. return false;
  2685. }
  2686. global->is_linked = true;
  2687. }
  2688. #endif
  2689. /* link with other modules' */
  2690. if (!global->is_linked
  2691. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2692. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2693. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2694. error_buf_size);
  2695. if (sub_module) {
  2696. /* check sub modules */
  2697. linked_global = wasm_loader_resolve_global(
  2698. sub_module_name, global_name, declare_type, declare_mutable,
  2699. error_buf, error_buf_size);
  2700. if (linked_global) {
  2701. global->import_module = sub_module;
  2702. global->import_global_linked = linked_global;
  2703. global->is_linked = true;
  2704. }
  2705. }
  2706. }
  2707. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2708. global->module_name = sub_module_name;
  2709. global->field_name = global_name;
  2710. global->type.val_type = declare_type;
  2711. global->type.is_mutable = (declare_mutable == 1);
  2712. #if WASM_ENABLE_WAMR_COMPILER != 0
  2713. if (global->type.val_type == VALUE_TYPE_V128)
  2714. parent_module->is_simd_used = true;
  2715. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2716. parent_module->is_ref_types_used = true;
  2717. #endif
  2718. (void)parent_module;
  2719. (void)ret;
  2720. return true;
  2721. fail:
  2722. return false;
  2723. }
  2724. static bool
  2725. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2726. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2727. {
  2728. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2729. #if WASM_ENABLE_GC != 0
  2730. WASMRefType ref_type;
  2731. bool need_ref_type_map;
  2732. #endif
  2733. bool is_table64 = false;
  2734. #if WASM_ENABLE_GC == 0
  2735. CHECK_BUF(p, p_end, 1);
  2736. /* 0x70 or 0x6F */
  2737. table->table_type.elem_type = read_uint8(p);
  2738. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2739. #if WASM_ENABLE_REF_TYPES != 0
  2740. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2741. #endif
  2742. ) {
  2743. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2744. return false;
  2745. }
  2746. #else /* else of WASM_ENABLE_GC == 0 */
  2747. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2748. &need_ref_type_map, &ref_type, false, error_buf,
  2749. error_buf_size)) {
  2750. return false;
  2751. }
  2752. table->table_type.elem_type = ref_type.ref_type;
  2753. if (need_ref_type_map) {
  2754. if (!(table->table_type.elem_ref_type =
  2755. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2756. error_buf_size))) {
  2757. return false;
  2758. }
  2759. }
  2760. #if TRACE_WASM_LOADER != 0
  2761. os_printf("table type: ");
  2762. wasm_dump_value_type(table->table_type.elem_type,
  2763. table->table_type.elem_ref_type);
  2764. os_printf("\n");
  2765. #endif
  2766. #endif /* end of WASM_ENABLE_GC == 0 */
  2767. p_org = p;
  2768. read_leb_uint32(p, p_end, table->table_type.flags);
  2769. is_table64 = table->table_type.flags & TABLE64_FLAG;
  2770. if (p - p_org > 1) {
  2771. LOG_VERBOSE("integer representation too long(table)");
  2772. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2773. return false;
  2774. }
  2775. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  2776. error_buf_size, false)) {
  2777. return false;
  2778. }
  2779. read_leb_uint32(p, p_end, table->table_type.init_size);
  2780. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  2781. read_leb_uint32(p, p_end, table->table_type.max_size);
  2782. if (!check_table_max_size(table->table_type.init_size,
  2783. table->table_type.max_size, error_buf,
  2784. error_buf_size))
  2785. return false;
  2786. }
  2787. adjust_table_max_size(is_table64, table->table_type.init_size,
  2788. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  2789. &table->table_type.max_size);
  2790. #if WASM_ENABLE_WAMR_COMPILER != 0
  2791. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2792. module->is_ref_types_used = true;
  2793. #endif
  2794. *p_buf = p;
  2795. return true;
  2796. fail:
  2797. return false;
  2798. }
  2799. static bool
  2800. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2801. char *error_buf, uint32 error_buf_size)
  2802. {
  2803. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2804. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2805. uint32 pool_size = wasm_runtime_memory_pool_size();
  2806. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2807. / DEFAULT_NUM_BYTES_PER_PAGE;
  2808. #else
  2809. uint32 max_page_count;
  2810. #endif
  2811. bool is_memory64 = false;
  2812. p_org = p;
  2813. read_leb_uint32(p, p_end, memory->flags);
  2814. is_memory64 = memory->flags & MEMORY64_FLAG;
  2815. if (p - p_org > 1) {
  2816. LOG_VERBOSE("integer representation too long(memory)");
  2817. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2818. return false;
  2819. }
  2820. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2821. false)) {
  2822. return false;
  2823. }
  2824. read_leb_uint32(p, p_end, memory->init_page_count);
  2825. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2826. error_buf_size))
  2827. return false;
  2828. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2829. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2830. #endif
  2831. if (memory->flags & 1) {
  2832. read_leb_uint32(p, p_end, memory->max_page_count);
  2833. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2834. memory->max_page_count, error_buf,
  2835. error_buf_size))
  2836. return false;
  2837. if (memory->max_page_count > max_page_count)
  2838. memory->max_page_count = max_page_count;
  2839. }
  2840. else {
  2841. /* Limit the maximum memory size to max_page_count */
  2842. memory->max_page_count = max_page_count;
  2843. }
  2844. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2845. *p_buf = p;
  2846. return true;
  2847. fail:
  2848. return false;
  2849. }
  2850. static int
  2851. cmp_export_name(const void *a, const void *b)
  2852. {
  2853. return strcmp(*(char **)a, *(char **)b);
  2854. }
  2855. static bool
  2856. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2857. bool is_load_from_file_buf, bool no_resolve,
  2858. char *error_buf, uint32 error_buf_size)
  2859. {
  2860. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2861. uint32 import_count, name_len, type_index, i, u32, flags;
  2862. uint64 total_size;
  2863. WASMImport *import;
  2864. WASMImport *import_functions = NULL, *import_tables = NULL;
  2865. WASMImport *import_memories = NULL, *import_globals = NULL;
  2866. #if WASM_ENABLE_TAGS != 0
  2867. WASMImport *import_tags = NULL;
  2868. #endif
  2869. char *sub_module_name, *field_name;
  2870. uint8 u8, kind, global_type;
  2871. read_leb_uint32(p, p_end, import_count);
  2872. if (import_count) {
  2873. module->import_count = import_count;
  2874. total_size = sizeof(WASMImport) * (uint64)import_count;
  2875. if (!(module->imports =
  2876. loader_malloc(total_size, error_buf, error_buf_size))) {
  2877. return false;
  2878. }
  2879. p_old = p;
  2880. /* Scan firstly to get import count of each type */
  2881. for (i = 0; i < import_count; i++) {
  2882. /* module name */
  2883. read_leb_uint32(p, p_end, name_len);
  2884. CHECK_BUF(p, p_end, name_len);
  2885. p += name_len;
  2886. /* field name */
  2887. read_leb_uint32(p, p_end, name_len);
  2888. CHECK_BUF(p, p_end, name_len);
  2889. p += name_len;
  2890. CHECK_BUF(p, p_end, 1);
  2891. /* 0x00/0x01/0x02/0x03/0x04 */
  2892. kind = read_uint8(p);
  2893. switch (kind) {
  2894. case IMPORT_KIND_FUNC: /* import function */
  2895. read_leb_uint32(p, p_end, type_index);
  2896. module->import_function_count++;
  2897. break;
  2898. case IMPORT_KIND_TABLE: /* import table */
  2899. CHECK_BUF(p, p_end, 1);
  2900. /* 0x70 */
  2901. u8 = read_uint8(p);
  2902. read_leb_uint32(p, p_end, flags);
  2903. read_leb_uint32(p, p_end, u32);
  2904. if (flags & 1)
  2905. read_leb_uint32(p, p_end, u32);
  2906. module->import_table_count++;
  2907. if (module->import_table_count > 1) {
  2908. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2909. set_error_buf(error_buf, error_buf_size,
  2910. "multiple tables");
  2911. return false;
  2912. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2913. module->is_ref_types_used = true;
  2914. #endif
  2915. }
  2916. break;
  2917. case IMPORT_KIND_MEMORY: /* import memory */
  2918. read_leb_uint32(p, p_end, flags);
  2919. read_leb_uint32(p, p_end, u32);
  2920. if (flags & 1)
  2921. read_leb_uint32(p, p_end, u32);
  2922. module->import_memory_count++;
  2923. #if WASM_ENABLE_MULTI_MEMORY == 0
  2924. if (module->import_memory_count > 1) {
  2925. set_error_buf(error_buf, error_buf_size,
  2926. "multiple memories");
  2927. return false;
  2928. }
  2929. #endif
  2930. break;
  2931. #if WASM_ENABLE_TAGS != 0
  2932. case IMPORT_KIND_TAG: /* import tags */
  2933. /* it only counts the number of tags to import */
  2934. module->import_tag_count++;
  2935. CHECK_BUF(p, p_end, 1);
  2936. u8 = read_uint8(p);
  2937. read_leb_uint32(p, p_end, type_index);
  2938. break;
  2939. #endif
  2940. case IMPORT_KIND_GLOBAL: /* import global */
  2941. #if WASM_ENABLE_GC != 0
  2942. /* valtype */
  2943. CHECK_BUF(p, p_end, 1);
  2944. global_type = read_uint8(p);
  2945. if (wasm_is_type_multi_byte_type(global_type)) {
  2946. int32 heap_type;
  2947. read_leb_int32(p, p_end, heap_type);
  2948. (void)heap_type;
  2949. }
  2950. /* mutability */
  2951. CHECK_BUF(p, p_end, 1);
  2952. p += 1;
  2953. #else
  2954. CHECK_BUF(p, p_end, 2);
  2955. p += 2;
  2956. #endif
  2957. (void)global_type;
  2958. module->import_global_count++;
  2959. break;
  2960. default:
  2961. set_error_buf(error_buf, error_buf_size,
  2962. "invalid import kind");
  2963. return false;
  2964. }
  2965. }
  2966. if (module->import_function_count)
  2967. import_functions = module->import_functions = module->imports;
  2968. if (module->import_table_count)
  2969. import_tables = module->import_tables =
  2970. module->imports + module->import_function_count;
  2971. if (module->import_memory_count)
  2972. import_memories = module->import_memories =
  2973. module->imports + module->import_function_count
  2974. + module->import_table_count;
  2975. #if WASM_ENABLE_TAGS != 0
  2976. if (module->import_tag_count)
  2977. import_tags = module->import_tags =
  2978. module->imports + module->import_function_count
  2979. + module->import_table_count + module->import_memory_count;
  2980. if (module->import_global_count)
  2981. import_globals = module->import_globals =
  2982. module->imports + module->import_function_count
  2983. + module->import_table_count + module->import_memory_count
  2984. + module->import_tag_count;
  2985. #else
  2986. if (module->import_global_count)
  2987. import_globals = module->import_globals =
  2988. module->imports + module->import_function_count
  2989. + module->import_table_count + module->import_memory_count;
  2990. #endif
  2991. p = p_old;
  2992. /* Scan again to resolve the data */
  2993. for (i = 0; i < import_count; i++) {
  2994. /* load module name */
  2995. read_leb_uint32(p, p_end, name_len);
  2996. CHECK_BUF(p, p_end, name_len);
  2997. if (!(sub_module_name = wasm_const_str_list_insert(
  2998. p, name_len, module, is_load_from_file_buf, error_buf,
  2999. error_buf_size))) {
  3000. return false;
  3001. }
  3002. p += name_len;
  3003. /* load field name */
  3004. read_leb_uint32(p, p_end, name_len);
  3005. CHECK_BUF(p, p_end, name_len);
  3006. if (!(field_name = wasm_const_str_list_insert(
  3007. p, name_len, module, is_load_from_file_buf, error_buf,
  3008. error_buf_size))) {
  3009. return false;
  3010. }
  3011. p += name_len;
  3012. CHECK_BUF(p, p_end, 1);
  3013. /* 0x00/0x01/0x02/0x03/0x4 */
  3014. kind = read_uint8(p);
  3015. switch (kind) {
  3016. case IMPORT_KIND_FUNC: /* import function */
  3017. bh_assert(import_functions);
  3018. import = import_functions++;
  3019. if (!load_function_import(&p, p_end, module,
  3020. sub_module_name, field_name,
  3021. &import->u.function, no_resolve,
  3022. error_buf, error_buf_size)) {
  3023. return false;
  3024. }
  3025. break;
  3026. case IMPORT_KIND_TABLE: /* import table */
  3027. bh_assert(import_tables);
  3028. import = import_tables++;
  3029. if (!load_table_import(&p, p_end, module, sub_module_name,
  3030. field_name, &import->u.table,
  3031. error_buf, error_buf_size)) {
  3032. LOG_DEBUG("can not import such a table (%s,%s)",
  3033. sub_module_name, field_name);
  3034. return false;
  3035. }
  3036. break;
  3037. case IMPORT_KIND_MEMORY: /* import memory */
  3038. bh_assert(import_memories);
  3039. import = import_memories++;
  3040. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3041. field_name, &import->u.memory,
  3042. error_buf, error_buf_size)) {
  3043. return false;
  3044. }
  3045. break;
  3046. #if WASM_ENABLE_TAGS != 0
  3047. case IMPORT_KIND_TAG:
  3048. bh_assert(import_tags);
  3049. import = import_tags++;
  3050. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3051. field_name, &import->u.tag, error_buf,
  3052. error_buf_size)) {
  3053. return false;
  3054. }
  3055. break;
  3056. #endif
  3057. case IMPORT_KIND_GLOBAL: /* import global */
  3058. bh_assert(import_globals);
  3059. import = import_globals++;
  3060. if (!load_global_import(&p, p_end, module, sub_module_name,
  3061. field_name, &import->u.global,
  3062. error_buf, error_buf_size)) {
  3063. return false;
  3064. }
  3065. break;
  3066. default:
  3067. set_error_buf(error_buf, error_buf_size,
  3068. "invalid import kind");
  3069. return false;
  3070. }
  3071. import->kind = kind;
  3072. import->u.names.module_name = sub_module_name;
  3073. import->u.names.field_name = field_name;
  3074. }
  3075. #if WASM_ENABLE_LIBC_WASI != 0
  3076. import = module->import_functions;
  3077. for (i = 0; i < module->import_function_count; i++, import++) {
  3078. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3079. || !strcmp(import->u.names.module_name,
  3080. "wasi_snapshot_preview1")) {
  3081. module->import_wasi_api = true;
  3082. break;
  3083. }
  3084. }
  3085. #endif
  3086. }
  3087. if (p != p_end) {
  3088. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3089. return false;
  3090. }
  3091. LOG_VERBOSE("Load import section success.\n");
  3092. (void)u8;
  3093. (void)u32;
  3094. (void)type_index;
  3095. return true;
  3096. fail:
  3097. return false;
  3098. }
  3099. static bool
  3100. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3101. uint32 error_buf_size)
  3102. {
  3103. WASMFuncType *param_type = func->func_type;
  3104. uint32 param_count = param_type->param_count;
  3105. uint8 *param_types = param_type->types;
  3106. uint32 local_count = func->local_count;
  3107. uint8 *local_types = func->local_types;
  3108. uint32 i, local_offset = 0;
  3109. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3110. /*
  3111. * Only allocate memory when total_size is not 0,
  3112. * or the return value of malloc(0) might be NULL on some platforms,
  3113. * which causes wasm loader return false.
  3114. */
  3115. if (total_size > 0
  3116. && !(func->local_offsets =
  3117. loader_malloc(total_size, error_buf, error_buf_size))) {
  3118. return false;
  3119. }
  3120. for (i = 0; i < param_count; i++) {
  3121. func->local_offsets[i] = (uint16)local_offset;
  3122. local_offset += wasm_value_type_cell_num(param_types[i]);
  3123. }
  3124. for (i = 0; i < local_count; i++) {
  3125. func->local_offsets[param_count + i] = (uint16)local_offset;
  3126. local_offset += wasm_value_type_cell_num(local_types[i]);
  3127. }
  3128. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3129. return true;
  3130. }
  3131. static bool
  3132. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3133. const uint8 *buf_code, const uint8 *buf_code_end,
  3134. WASMModule *module, char *error_buf,
  3135. uint32 error_buf_size)
  3136. {
  3137. const uint8 *p = buf, *p_end = buf_end;
  3138. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3139. uint32 func_count;
  3140. uint64 total_size;
  3141. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3142. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3143. uint8 type;
  3144. WASMFunction *func;
  3145. #if WASM_ENABLE_GC != 0
  3146. bool need_ref_type_map;
  3147. WASMRefType ref_type;
  3148. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3149. #endif
  3150. read_leb_uint32(p, p_end, func_count);
  3151. if (buf_code)
  3152. read_leb_uint32(p_code, buf_code_end, code_count);
  3153. if (func_count != code_count) {
  3154. set_error_buf(error_buf, error_buf_size,
  3155. "function and code section have inconsistent lengths or "
  3156. "unexpected end");
  3157. return false;
  3158. }
  3159. if (is_indices_overflow(module->import_function_count, func_count,
  3160. error_buf, error_buf_size))
  3161. return false;
  3162. if (func_count) {
  3163. module->function_count = func_count;
  3164. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3165. if (!(module->functions =
  3166. loader_malloc(total_size, error_buf, error_buf_size))) {
  3167. return false;
  3168. }
  3169. for (i = 0; i < func_count; i++) {
  3170. /* Resolve function type */
  3171. read_leb_uint32(p, p_end, type_index);
  3172. if (type_index >= module->type_count) {
  3173. set_error_buf(error_buf, error_buf_size, "unknown type");
  3174. return false;
  3175. }
  3176. #if WASM_ENABLE_GC != 0
  3177. type_index_org = type_index;
  3178. #endif
  3179. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3180. && WASM_ENABLE_GC == 0
  3181. type_index = wasm_get_smallest_type_idx(
  3182. module->types, module->type_count, type_index);
  3183. #endif
  3184. read_leb_uint32(p_code, buf_code_end, code_size);
  3185. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3186. set_error_buf(error_buf, error_buf_size,
  3187. "invalid function code size");
  3188. return false;
  3189. }
  3190. /* Resolve local set count */
  3191. p_code_end = p_code + code_size;
  3192. local_count = 0;
  3193. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3194. p_code_save = p_code;
  3195. #if WASM_ENABLE_GC != 0
  3196. ref_type_map_count = 0;
  3197. #endif
  3198. /* Calculate total local count */
  3199. for (j = 0; j < local_set_count; j++) {
  3200. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3201. if (sub_local_count > UINT32_MAX - local_count) {
  3202. set_error_buf(error_buf, error_buf_size, "too many locals");
  3203. return false;
  3204. }
  3205. #if WASM_ENABLE_GC == 0
  3206. CHECK_BUF(p_code, buf_code_end, 1);
  3207. /* 0x7F/0x7E/0x7D/0x7C */
  3208. type = read_uint8(p_code);
  3209. local_count += sub_local_count;
  3210. #if WASM_ENABLE_WAMR_COMPILER != 0
  3211. /* If any value's type is v128, mark the module as SIMD used */
  3212. if (type == VALUE_TYPE_V128)
  3213. module->is_simd_used = true;
  3214. #endif
  3215. #else
  3216. if (!resolve_value_type(&p_code, buf_code_end, module,
  3217. module->type_count, &need_ref_type_map,
  3218. &ref_type, false, error_buf,
  3219. error_buf_size)) {
  3220. return false;
  3221. }
  3222. local_count += sub_local_count;
  3223. if (need_ref_type_map)
  3224. ref_type_map_count += sub_local_count;
  3225. #endif
  3226. }
  3227. /* Code size in code entry can't be smaller than size of vec(locals)
  3228. * + expr(at least 1 for opcode end). And expressions are encoded by
  3229. * their instruction sequence terminated with an explicit 0x0B
  3230. * opcode for end. */
  3231. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3232. set_error_buf(
  3233. error_buf, error_buf_size,
  3234. "section size mismatch: function body END opcode expected");
  3235. return false;
  3236. }
  3237. /* Alloc memory, layout: function structure + local types */
  3238. code_size = (uint32)(p_code_end - p_code);
  3239. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3240. if (!(func = module->functions[i] =
  3241. loader_malloc(total_size, error_buf, error_buf_size))) {
  3242. return false;
  3243. }
  3244. #if WASM_ENABLE_GC != 0
  3245. if (ref_type_map_count > 0) {
  3246. total_size =
  3247. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3248. if (!(func->local_ref_type_maps = loader_malloc(
  3249. total_size, error_buf, error_buf_size))) {
  3250. return false;
  3251. }
  3252. func->local_ref_type_map_count = ref_type_map_count;
  3253. }
  3254. #endif
  3255. /* Set function type, local count, code size and code body */
  3256. func->func_type = (WASMFuncType *)module->types[type_index];
  3257. func->local_count = local_count;
  3258. if (local_count > 0)
  3259. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3260. func->code_size = code_size;
  3261. /*
  3262. * we shall make a copy of code body [p_code, p_code + code_size]
  3263. * when we are worrying about inappropriate releasing behaviour.
  3264. * all code bodies are actually in a buffer which user allocates in
  3265. * his embedding environment and we don't have power on them.
  3266. * it will be like:
  3267. * code_body_cp = malloc(code_size);
  3268. * memcpy(code_body_cp, p_code, code_size);
  3269. * func->code = code_body_cp;
  3270. */
  3271. func->code = (uint8 *)p_code;
  3272. #if WASM_ENABLE_GC != 0
  3273. func->type_idx = type_index_org;
  3274. #endif
  3275. #if WASM_ENABLE_GC != 0
  3276. t = 0;
  3277. #endif
  3278. /* Load each local type */
  3279. p_code = p_code_save;
  3280. local_type_index = 0;
  3281. for (j = 0; j < local_set_count; j++) {
  3282. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3283. /* Note: sub_local_count is allowed to be 0 */
  3284. if (local_type_index > UINT32_MAX - sub_local_count
  3285. || local_type_index + sub_local_count > local_count) {
  3286. set_error_buf(error_buf, error_buf_size,
  3287. "invalid local count");
  3288. return false;
  3289. }
  3290. #if WASM_ENABLE_GC == 0
  3291. CHECK_BUF(p_code, buf_code_end, 1);
  3292. /* 0x7F/0x7E/0x7D/0x7C */
  3293. type = read_uint8(p_code);
  3294. if (!is_valid_value_type_for_interpreter(type)) {
  3295. if (type == VALUE_TYPE_V128)
  3296. set_error_buf(error_buf, error_buf_size,
  3297. "v128 value type requires simd feature");
  3298. else if (type == VALUE_TYPE_FUNCREF
  3299. || type == VALUE_TYPE_EXTERNREF)
  3300. set_error_buf(error_buf, error_buf_size,
  3301. "ref value type requires "
  3302. "reference types feature");
  3303. else
  3304. set_error_buf_v(error_buf, error_buf_size,
  3305. "invalid local type 0x%02X", type);
  3306. return false;
  3307. }
  3308. #else
  3309. if (!resolve_value_type(&p_code, buf_code_end, module,
  3310. module->type_count, &need_ref_type_map,
  3311. &ref_type, false, error_buf,
  3312. error_buf_size)) {
  3313. return false;
  3314. }
  3315. if (need_ref_type_map) {
  3316. WASMRefType *ref_type_tmp;
  3317. if (!(ref_type_tmp = reftype_set_insert(
  3318. module->ref_type_set, &ref_type, error_buf,
  3319. error_buf_size))) {
  3320. return false;
  3321. }
  3322. for (k = 0; k < sub_local_count; k++) {
  3323. func->local_ref_type_maps[t + k].ref_type =
  3324. ref_type_tmp;
  3325. func->local_ref_type_maps[t + k].index =
  3326. local_type_index + k;
  3327. }
  3328. t += sub_local_count;
  3329. }
  3330. type = ref_type.ref_type;
  3331. #endif
  3332. for (k = 0; k < sub_local_count; k++) {
  3333. func->local_types[local_type_index++] = type;
  3334. }
  3335. #if WASM_ENABLE_WAMR_COMPILER != 0
  3336. if (type == VALUE_TYPE_V128)
  3337. module->is_simd_used = true;
  3338. else if (type == VALUE_TYPE_FUNCREF
  3339. || type == VALUE_TYPE_EXTERNREF)
  3340. module->is_ref_types_used = true;
  3341. #endif
  3342. }
  3343. bh_assert(local_type_index == func->local_count);
  3344. #if WASM_ENABLE_GC != 0
  3345. bh_assert(t == func->local_ref_type_map_count);
  3346. #if TRACE_WASM_LOADER != 0
  3347. os_printf("func %u, local types: [", i);
  3348. k = 0;
  3349. for (j = 0; j < func->local_count; j++) {
  3350. WASMRefType *ref_type_tmp = NULL;
  3351. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3352. bh_assert(j == func->local_ref_type_maps[k].index);
  3353. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3354. }
  3355. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3356. if (j < func->local_count - 1)
  3357. os_printf(" ");
  3358. }
  3359. os_printf("]\n");
  3360. #endif
  3361. #endif
  3362. func->param_cell_num = func->func_type->param_cell_num;
  3363. func->ret_cell_num = func->func_type->ret_cell_num;
  3364. local_cell_num =
  3365. wasm_get_cell_num(func->local_types, func->local_count);
  3366. if (local_cell_num > UINT16_MAX) {
  3367. set_error_buf(error_buf, error_buf_size,
  3368. "local count too large");
  3369. return false;
  3370. }
  3371. func->local_cell_num = (uint16)local_cell_num;
  3372. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3373. return false;
  3374. p_code = p_code_end;
  3375. }
  3376. }
  3377. if (p != p_end) {
  3378. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3379. return false;
  3380. }
  3381. LOG_VERBOSE("Load function section success.\n");
  3382. return true;
  3383. fail:
  3384. return false;
  3385. }
  3386. static bool
  3387. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3388. char *error_buf, uint32 error_buf_size)
  3389. {
  3390. const uint8 *p = buf, *p_end = buf_end;
  3391. uint32 table_count, i;
  3392. uint64 total_size;
  3393. WASMTable *table;
  3394. read_leb_uint32(p, p_end, table_count);
  3395. if (module->import_table_count + table_count > 1) {
  3396. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3397. /* a total of one table is allowed */
  3398. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3399. return false;
  3400. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3401. module->is_ref_types_used = true;
  3402. #endif
  3403. }
  3404. if (table_count) {
  3405. module->table_count = table_count;
  3406. total_size = sizeof(WASMTable) * (uint64)table_count;
  3407. if (!(module->tables =
  3408. loader_malloc(total_size, error_buf, error_buf_size))) {
  3409. return false;
  3410. }
  3411. /* load each table */
  3412. table = module->tables;
  3413. for (i = 0; i < table_count; i++, table++) {
  3414. #if WASM_ENABLE_GC != 0
  3415. uint8 flag;
  3416. bool has_init = false;
  3417. CHECK_BUF(buf, buf_end, 1);
  3418. flag = read_uint8(p);
  3419. if (flag == TABLE_INIT_EXPR_FLAG) {
  3420. CHECK_BUF(buf, buf_end, 1);
  3421. flag = read_uint8(p);
  3422. if (flag != 0x00) {
  3423. set_error_buf(error_buf, error_buf_size,
  3424. "invalid leading bytes for table");
  3425. return false;
  3426. }
  3427. has_init = true;
  3428. }
  3429. else {
  3430. p--;
  3431. }
  3432. #endif /* end of WASM_ENABLE_GC != 0 */
  3433. if (!load_table(&p, p_end, module, table, error_buf,
  3434. error_buf_size))
  3435. return false;
  3436. #if WASM_ENABLE_GC != 0
  3437. if (has_init) {
  3438. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3439. table->table_type.elem_type,
  3440. table->table_type.elem_ref_type, error_buf,
  3441. error_buf_size))
  3442. return false;
  3443. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3444. && table->init_expr.init_expr_type
  3445. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3446. set_error_buf(
  3447. error_buf, error_buf_size,
  3448. "unsupported initializer expression for table");
  3449. return false;
  3450. }
  3451. }
  3452. else {
  3453. if (wasm_is_reftype_htref_non_nullable(
  3454. table->table_type.elem_type)) {
  3455. set_error_buf(
  3456. error_buf, error_buf_size,
  3457. "type mismatch: non-nullable table without init expr");
  3458. return false;
  3459. }
  3460. }
  3461. #endif /* end of WASM_ENABLE_GC != 0 */
  3462. #if WASM_ENABLE_WAMR_COMPILER != 0
  3463. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3464. module->is_ref_types_used = true;
  3465. #endif
  3466. }
  3467. }
  3468. if (p != p_end) {
  3469. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3470. return false;
  3471. }
  3472. LOG_VERBOSE("Load table section success.\n");
  3473. return true;
  3474. fail:
  3475. return false;
  3476. }
  3477. static bool
  3478. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3479. char *error_buf, uint32 error_buf_size)
  3480. {
  3481. const uint8 *p = buf, *p_end = buf_end;
  3482. uint32 memory_count, i;
  3483. uint64 total_size;
  3484. WASMMemory *memory;
  3485. read_leb_uint32(p, p_end, memory_count);
  3486. #if WASM_ENABLE_MULTI_MEMORY == 0
  3487. /* a total of one memory is allowed */
  3488. if (module->import_memory_count + memory_count > 1) {
  3489. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3490. return false;
  3491. }
  3492. #endif
  3493. if (memory_count) {
  3494. module->memory_count = memory_count;
  3495. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3496. if (!(module->memories =
  3497. loader_malloc(total_size, error_buf, error_buf_size))) {
  3498. return false;
  3499. }
  3500. /* load each memory */
  3501. memory = module->memories;
  3502. for (i = 0; i < memory_count; i++, memory++)
  3503. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3504. return false;
  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 memory section success.\n");
  3511. return true;
  3512. fail:
  3513. return false;
  3514. }
  3515. static bool
  3516. load_global_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 global_count, i;
  3521. uint64 total_size;
  3522. WASMGlobal *global;
  3523. uint8 mutable;
  3524. #if WASM_ENABLE_GC != 0
  3525. bool need_ref_type_map;
  3526. WASMRefType ref_type;
  3527. #endif
  3528. read_leb_uint32(p, p_end, global_count);
  3529. if (is_indices_overflow(module->import_global_count, global_count,
  3530. error_buf, error_buf_size))
  3531. return false;
  3532. module->global_count = 0;
  3533. if (global_count) {
  3534. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3535. if (!(module->globals =
  3536. loader_malloc(total_size, error_buf, error_buf_size))) {
  3537. return false;
  3538. }
  3539. global = module->globals;
  3540. for (i = 0; i < global_count; i++, global++) {
  3541. #if WASM_ENABLE_GC == 0
  3542. CHECK_BUF(p, p_end, 2);
  3543. /* global type */
  3544. global->type.val_type = read_uint8(p);
  3545. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3546. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3547. return false;
  3548. }
  3549. mutable = read_uint8(p);
  3550. #else
  3551. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3552. &need_ref_type_map, &ref_type, false,
  3553. error_buf, error_buf_size)) {
  3554. return false;
  3555. }
  3556. global->type.val_type = ref_type.ref_type;
  3557. CHECK_BUF(p, p_end, 1);
  3558. mutable = read_uint8(p);
  3559. #endif /* end of WASM_ENABLE_GC */
  3560. #if WASM_ENABLE_WAMR_COMPILER != 0
  3561. if (global->type.val_type == VALUE_TYPE_V128)
  3562. module->is_simd_used = true;
  3563. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3564. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3565. module->is_ref_types_used = true;
  3566. #endif
  3567. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3568. return false;
  3569. }
  3570. global->type.is_mutable = mutable ? true : false;
  3571. /* initialize expression */
  3572. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3573. global->type.val_type,
  3574. #if WASM_ENABLE_GC == 0
  3575. NULL,
  3576. #else
  3577. &ref_type,
  3578. #endif
  3579. error_buf, error_buf_size))
  3580. return false;
  3581. #if WASM_ENABLE_GC != 0
  3582. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3583. uint8 global_type;
  3584. WASMRefType *global_ref_type;
  3585. uint32 global_idx = global->init_expr.u.global_index;
  3586. if (global->init_expr.u.global_index
  3587. >= module->import_global_count + i) {
  3588. set_error_buf(error_buf, error_buf_size, "unknown global");
  3589. return false;
  3590. }
  3591. if (global_idx < module->import_global_count) {
  3592. global_type = module->import_globals[global_idx]
  3593. .u.global.type.val_type;
  3594. global_ref_type =
  3595. module->import_globals[global_idx].u.global.ref_type;
  3596. }
  3597. else {
  3598. global_type =
  3599. module
  3600. ->globals[global_idx - module->import_global_count]
  3601. .type.val_type;
  3602. global_ref_type =
  3603. module
  3604. ->globals[global_idx - module->import_global_count]
  3605. .ref_type;
  3606. }
  3607. if (!wasm_reftype_is_subtype_of(
  3608. global_type, global_ref_type, global->type.val_type,
  3609. global->ref_type, module->types, module->type_count)) {
  3610. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3611. return false;
  3612. }
  3613. }
  3614. if (need_ref_type_map) {
  3615. if (!(global->ref_type =
  3616. reftype_set_insert(module->ref_type_set, &ref_type,
  3617. error_buf, error_buf_size))) {
  3618. return false;
  3619. }
  3620. }
  3621. #if TRACE_WASM_LOADER != 0
  3622. os_printf("global type: ");
  3623. wasm_dump_value_type(global->type, global->ref_type);
  3624. os_printf("\n");
  3625. #endif
  3626. #endif
  3627. module->global_count++;
  3628. }
  3629. bh_assert(module->global_count == global_count);
  3630. }
  3631. if (p != p_end) {
  3632. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3633. return false;
  3634. }
  3635. LOG_VERBOSE("Load global section success.\n");
  3636. return true;
  3637. fail:
  3638. return false;
  3639. }
  3640. static bool
  3641. check_duplicate_exports(WASMModule *module, char *error_buf,
  3642. uint32 error_buf_size)
  3643. {
  3644. uint32 i;
  3645. bool result = false;
  3646. char *names_buf[32], **names = names_buf;
  3647. if (module->export_count > 32) {
  3648. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3649. error_buf_size);
  3650. if (!names) {
  3651. return result;
  3652. }
  3653. }
  3654. for (i = 0; i < module->export_count; i++) {
  3655. names[i] = module->exports[i].name;
  3656. }
  3657. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3658. for (i = 1; i < module->export_count; i++) {
  3659. if (!strcmp(names[i], names[i - 1])) {
  3660. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3661. goto cleanup;
  3662. }
  3663. }
  3664. result = true;
  3665. cleanup:
  3666. if (module->export_count > 32) {
  3667. wasm_runtime_free(names);
  3668. }
  3669. return result;
  3670. }
  3671. static bool
  3672. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3673. bool is_load_from_file_buf, char *error_buf,
  3674. uint32 error_buf_size)
  3675. {
  3676. const uint8 *p = buf, *p_end = buf_end;
  3677. uint32 export_count, i, index;
  3678. uint64 total_size;
  3679. uint32 str_len;
  3680. WASMExport *export;
  3681. read_leb_uint32(p, p_end, export_count);
  3682. if (export_count) {
  3683. module->export_count = export_count;
  3684. total_size = sizeof(WASMExport) * (uint64)export_count;
  3685. if (!(module->exports =
  3686. loader_malloc(total_size, error_buf, error_buf_size))) {
  3687. return false;
  3688. }
  3689. export = module->exports;
  3690. for (i = 0; i < export_count; i++, export ++) {
  3691. #if WASM_ENABLE_THREAD_MGR == 0
  3692. if (p == p_end) {
  3693. /* export section with inconsistent count:
  3694. n export declared, but less than n given */
  3695. set_error_buf(error_buf, error_buf_size,
  3696. "length out of bounds");
  3697. return false;
  3698. }
  3699. #endif
  3700. read_leb_uint32(p, p_end, str_len);
  3701. CHECK_BUF(p, p_end, str_len);
  3702. if (!(export->name = wasm_const_str_list_insert(
  3703. p, str_len, module, is_load_from_file_buf, error_buf,
  3704. error_buf_size))) {
  3705. return false;
  3706. }
  3707. p += str_len;
  3708. CHECK_BUF(p, p_end, 1);
  3709. export->kind = read_uint8(p);
  3710. read_leb_uint32(p, p_end, index);
  3711. export->index = index;
  3712. switch (export->kind) {
  3713. /* function index */
  3714. case EXPORT_KIND_FUNC:
  3715. if (index >= module->function_count
  3716. + module->import_function_count) {
  3717. set_error_buf(error_buf, error_buf_size,
  3718. "unknown function");
  3719. return false;
  3720. }
  3721. #if WASM_ENABLE_SIMD != 0
  3722. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3723. /* TODO: check func type, if it has v128 param or result,
  3724. report error */
  3725. #endif
  3726. #endif
  3727. break;
  3728. /* table index */
  3729. case EXPORT_KIND_TABLE:
  3730. if (index
  3731. >= module->table_count + module->import_table_count) {
  3732. set_error_buf(error_buf, error_buf_size,
  3733. "unknown table");
  3734. return false;
  3735. }
  3736. break;
  3737. /* memory index */
  3738. case EXPORT_KIND_MEMORY:
  3739. if (index
  3740. >= module->memory_count + module->import_memory_count) {
  3741. set_error_buf(error_buf, error_buf_size,
  3742. "unknown memory");
  3743. return false;
  3744. }
  3745. break;
  3746. #if WASM_ENABLE_TAGS != 0
  3747. /* export tag */
  3748. case EXPORT_KIND_TAG:
  3749. if (index >= module->tag_count + module->import_tag_count) {
  3750. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3751. return false;
  3752. }
  3753. break;
  3754. #endif
  3755. /* global index */
  3756. case EXPORT_KIND_GLOBAL:
  3757. if (index
  3758. >= module->global_count + module->import_global_count) {
  3759. set_error_buf(error_buf, error_buf_size,
  3760. "unknown global");
  3761. return false;
  3762. }
  3763. break;
  3764. default:
  3765. set_error_buf(error_buf, error_buf_size,
  3766. "invalid export kind");
  3767. return false;
  3768. }
  3769. }
  3770. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  3771. return false;
  3772. }
  3773. }
  3774. if (p != p_end) {
  3775. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3776. return false;
  3777. }
  3778. LOG_VERBOSE("Load export section success.\n");
  3779. return true;
  3780. fail:
  3781. return false;
  3782. }
  3783. static bool
  3784. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3785. uint32 error_buf_size)
  3786. {
  3787. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3788. if (table_index != 0) {
  3789. set_error_buf(
  3790. error_buf, error_buf_size,
  3791. "zero byte expected. The module uses reference types feature "
  3792. "which is disabled in the runtime.");
  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. /* Element segments must match element type of table */
  3820. static bool
  3821. check_table_elem_type(WASMModule *module, uint32 table_index,
  3822. uint32 type_from_elem_seg, char *error_buf,
  3823. uint32 error_buf_size)
  3824. {
  3825. uint32 table_declared_elem_type;
  3826. if (table_index < module->import_table_count)
  3827. table_declared_elem_type =
  3828. module->import_tables[table_index].u.table.table_type.elem_type;
  3829. else
  3830. table_declared_elem_type =
  3831. module->tables[table_index - module->import_table_count]
  3832. .table_type.elem_type;
  3833. if (table_declared_elem_type == type_from_elem_seg)
  3834. return true;
  3835. #if WASM_ENABLE_GC != 0
  3836. /*
  3837. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3838. */
  3839. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3840. return true;
  3841. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3842. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3843. return true;
  3844. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3845. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3846. return true;
  3847. #endif
  3848. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3849. return false;
  3850. }
  3851. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3852. static bool
  3853. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3854. uint32 *p_elem_type,
  3855. #if WASM_ENABLE_GC != 0
  3856. WASMRefType **p_elem_ref_type,
  3857. #endif
  3858. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3859. {
  3860. const uint8 *p = *p_buf, *p_end = buf_end;
  3861. uint8 elem_type;
  3862. #if WASM_ENABLE_GC != 0
  3863. WASMRefType elem_ref_type;
  3864. bool need_ref_type_map;
  3865. #endif
  3866. CHECK_BUF(p, p_end, 1);
  3867. elem_type = read_uint8(p);
  3868. if (elemkind_zero) {
  3869. if (elem_type != 0) {
  3870. set_error_buf(error_buf, error_buf_size,
  3871. "invalid reference type or unknown type");
  3872. return false;
  3873. }
  3874. else {
  3875. *p_elem_type = VALUE_TYPE_FUNCREF;
  3876. *p_buf = p;
  3877. return true;
  3878. }
  3879. }
  3880. #if WASM_ENABLE_GC == 0
  3881. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3882. set_error_buf(error_buf, error_buf_size,
  3883. "invalid reference type or unknown type");
  3884. return false;
  3885. }
  3886. *p_elem_type = elem_type;
  3887. #else
  3888. p--;
  3889. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3890. module->type_count, &need_ref_type_map,
  3891. &elem_ref_type, false, error_buf, error_buf_size)) {
  3892. return false;
  3893. }
  3894. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3895. set_error_buf(error_buf, error_buf_size,
  3896. "invalid reference type or unknown type");
  3897. return false;
  3898. }
  3899. *p_elem_type = elem_ref_type.ref_type;
  3900. if (need_ref_type_map) {
  3901. if (!(*p_elem_ref_type =
  3902. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3903. error_buf, error_buf_size))) {
  3904. return false;
  3905. }
  3906. }
  3907. #endif
  3908. *p_buf = p;
  3909. return true;
  3910. fail:
  3911. return false;
  3912. }
  3913. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3914. static bool
  3915. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3916. WASMModule *module, WASMTableSeg *table_segment,
  3917. char *error_buf, uint32 error_buf_size)
  3918. {
  3919. const uint8 *p = *p_buf, *p_end = buf_end;
  3920. uint32 function_count, function_index = 0, i;
  3921. uint64 total_size;
  3922. read_leb_uint32(p, p_end, function_count);
  3923. table_segment->value_count = function_count;
  3924. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3925. if (total_size > 0
  3926. && !(table_segment->init_values =
  3927. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3928. error_buf_size))) {
  3929. return false;
  3930. }
  3931. for (i = 0; i < function_count; i++) {
  3932. InitializerExpression *init_expr = &table_segment->init_values[i];
  3933. read_leb_uint32(p, p_end, function_index);
  3934. if (!check_function_index(module, function_index, error_buf,
  3935. error_buf_size)) {
  3936. return false;
  3937. }
  3938. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3939. init_expr->u.ref_index = function_index;
  3940. }
  3941. *p_buf = p;
  3942. return true;
  3943. fail:
  3944. return false;
  3945. }
  3946. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3947. static bool
  3948. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3949. WASMModule *module, WASMTableSeg *table_segment,
  3950. char *error_buf, uint32 error_buf_size)
  3951. {
  3952. const uint8 *p = *p_buf, *p_end = buf_end;
  3953. uint32 ref_count, i;
  3954. uint64 total_size;
  3955. read_leb_uint32(p, p_end, ref_count);
  3956. table_segment->value_count = ref_count;
  3957. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3958. if (total_size > 0
  3959. && !(table_segment->init_values =
  3960. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3961. error_buf_size))) {
  3962. return false;
  3963. }
  3964. for (i = 0; i < ref_count; i++) {
  3965. InitializerExpression *init_expr = &table_segment->init_values[i];
  3966. if (!load_init_expr(module, &p, p_end, init_expr,
  3967. table_segment->elem_type,
  3968. #if WASM_ENABLE_GC == 0
  3969. NULL,
  3970. #else
  3971. table_segment->elem_ref_type,
  3972. #endif
  3973. error_buf, error_buf_size))
  3974. return false;
  3975. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3976. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3977. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3978. && init_expr->init_expr_type
  3979. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3980. }
  3981. *p_buf = p;
  3982. return true;
  3983. fail:
  3984. return false;
  3985. }
  3986. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3987. static bool
  3988. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3989. WASMModule *module, char *error_buf,
  3990. uint32 error_buf_size)
  3991. {
  3992. const uint8 *p = buf, *p_end = buf_end;
  3993. uint8 table_elem_idx_type;
  3994. uint32 table_segment_count, i;
  3995. uint64 total_size;
  3996. WASMTableSeg *table_segment;
  3997. read_leb_uint32(p, p_end, table_segment_count);
  3998. if (table_segment_count) {
  3999. module->table_seg_count = table_segment_count;
  4000. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4001. if (!(module->table_segments =
  4002. loader_malloc(total_size, error_buf, error_buf_size))) {
  4003. return false;
  4004. }
  4005. table_segment = module->table_segments;
  4006. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4007. if (p >= p_end) {
  4008. set_error_buf(error_buf, error_buf_size,
  4009. "invalid value type or "
  4010. "invalid elements segment kind");
  4011. return false;
  4012. }
  4013. table_elem_idx_type = VALUE_TYPE_I32;
  4014. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4015. read_leb_uint32(p, p_end, table_segment->mode);
  4016. /* last three bits */
  4017. table_segment->mode = table_segment->mode & 0x07;
  4018. switch (table_segment->mode) {
  4019. /* elemkind/elemtype + active */
  4020. case 0:
  4021. case 4:
  4022. {
  4023. #if WASM_ENABLE_GC != 0
  4024. if (table_segment->mode == 0) {
  4025. /* vec(funcidx), set elem type to (ref func) */
  4026. WASMRefType elem_ref_type = { 0 };
  4027. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4028. wasm_set_refheaptype_common(
  4029. &elem_ref_type.ref_ht_common, false,
  4030. HEAP_TYPE_FUNC);
  4031. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4032. module->ref_type_set, &elem_ref_type,
  4033. error_buf, error_buf_size)))
  4034. return false;
  4035. }
  4036. else {
  4037. /* vec(expr), set elem type to funcref */
  4038. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4039. }
  4040. #else
  4041. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4042. #endif
  4043. table_segment->table_index = 0;
  4044. if (!check_table_index(module, table_segment->table_index,
  4045. error_buf, error_buf_size))
  4046. return false;
  4047. #if WASM_ENABLE_MEMORY64 != 0
  4048. table_elem_idx_type =
  4049. is_table_64bit(module, table_segment->table_index)
  4050. ? VALUE_TYPE_I64
  4051. : VALUE_TYPE_I32;
  4052. #endif
  4053. if (!load_init_expr(module, &p, p_end,
  4054. &table_segment->base_offset,
  4055. table_elem_idx_type, NULL, error_buf,
  4056. error_buf_size))
  4057. return false;
  4058. if (table_segment->mode == 0) {
  4059. /* vec(funcidx) */
  4060. if (!load_func_index_vec(&p, p_end, module,
  4061. table_segment, error_buf,
  4062. error_buf_size))
  4063. return false;
  4064. }
  4065. else {
  4066. /* vec(expr) */
  4067. if (!load_init_expr_vec(&p, p_end, module,
  4068. table_segment, error_buf,
  4069. error_buf_size))
  4070. return false;
  4071. }
  4072. if (!check_table_elem_type(module,
  4073. table_segment->table_index,
  4074. table_segment->elem_type,
  4075. error_buf, error_buf_size))
  4076. return false;
  4077. break;
  4078. }
  4079. /* elemkind + passive/declarative */
  4080. case 1:
  4081. case 3:
  4082. if (!load_elem_type(module, &p, p_end,
  4083. &table_segment->elem_type,
  4084. #if WASM_ENABLE_GC != 0
  4085. &table_segment->elem_ref_type,
  4086. #endif
  4087. true, error_buf, error_buf_size))
  4088. return false;
  4089. /* vec(funcidx) */
  4090. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4091. error_buf, error_buf_size))
  4092. return false;
  4093. break;
  4094. /* elemkind/elemtype + table_idx + active */
  4095. case 2:
  4096. case 6:
  4097. if (!load_table_index(&p, p_end, module,
  4098. &table_segment->table_index,
  4099. error_buf, error_buf_size))
  4100. return false;
  4101. #if WASM_ENABLE_MEMORY64 != 0
  4102. table_elem_idx_type =
  4103. is_table_64bit(module, table_segment->table_index)
  4104. ? VALUE_TYPE_I64
  4105. : VALUE_TYPE_I32;
  4106. #endif
  4107. if (!load_init_expr(module, &p, p_end,
  4108. &table_segment->base_offset,
  4109. table_elem_idx_type, NULL, error_buf,
  4110. error_buf_size))
  4111. return false;
  4112. if (!load_elem_type(module, &p, p_end,
  4113. &table_segment->elem_type,
  4114. #if WASM_ENABLE_GC != 0
  4115. &table_segment->elem_ref_type,
  4116. #endif
  4117. table_segment->mode == 2 ? true : false,
  4118. error_buf, error_buf_size))
  4119. return false;
  4120. if (table_segment->mode == 2) {
  4121. /* vec(funcidx) */
  4122. if (!load_func_index_vec(&p, p_end, module,
  4123. table_segment, error_buf,
  4124. error_buf_size))
  4125. return false;
  4126. }
  4127. else {
  4128. /* vec(expr) */
  4129. if (!load_init_expr_vec(&p, p_end, module,
  4130. table_segment, error_buf,
  4131. error_buf_size))
  4132. return false;
  4133. }
  4134. if (!check_table_elem_type(module,
  4135. table_segment->table_index,
  4136. table_segment->elem_type,
  4137. error_buf, error_buf_size))
  4138. return false;
  4139. break;
  4140. case 5:
  4141. case 7:
  4142. if (!load_elem_type(module, &p, p_end,
  4143. &table_segment->elem_type,
  4144. #if WASM_ENABLE_GC != 0
  4145. &table_segment->elem_ref_type,
  4146. #endif
  4147. false, error_buf, error_buf_size))
  4148. return false;
  4149. /* vec(expr) */
  4150. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4151. error_buf, error_buf_size))
  4152. return false;
  4153. break;
  4154. default:
  4155. set_error_buf(error_buf, error_buf_size,
  4156. "unknown element segment kind");
  4157. return false;
  4158. }
  4159. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4160. /*
  4161. * like: 00 41 05 0b 04 00 01 00 01
  4162. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4163. */
  4164. if (!load_table_index(&p, p_end, module,
  4165. &table_segment->table_index, error_buf,
  4166. error_buf_size))
  4167. return false;
  4168. #if WASM_ENABLE_MEMORY64 != 0
  4169. table_elem_idx_type =
  4170. is_table_64bit(module, table_segment->table_index)
  4171. ? VALUE_TYPE_I64
  4172. : VALUE_TYPE_I32;
  4173. #endif
  4174. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4175. table_elem_idx_type, NULL, error_buf,
  4176. error_buf_size))
  4177. return false;
  4178. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4179. error_buf, error_buf_size))
  4180. return false;
  4181. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4182. if (!check_table_elem_type(module, table_segment->table_index,
  4183. table_segment->elem_type, error_buf,
  4184. error_buf_size))
  4185. return false;
  4186. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4187. #if WASM_ENABLE_MEMORY64 != 0
  4188. if (table_elem_idx_type == VALUE_TYPE_I64
  4189. && table_segment->base_offset.u.u64 > UINT32_MAX) {
  4190. set_error_buf(error_buf, error_buf_size,
  4191. "In table64, table base offset can't be "
  4192. "larger than UINT32_MAX");
  4193. return false;
  4194. }
  4195. #endif
  4196. #if WASM_ENABLE_WAMR_COMPILER != 0
  4197. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4198. module->is_ref_types_used = true;
  4199. #endif
  4200. }
  4201. }
  4202. if (p != p_end) {
  4203. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4204. return false;
  4205. }
  4206. LOG_VERBOSE("Load table segment section success.\n");
  4207. return true;
  4208. fail:
  4209. return false;
  4210. }
  4211. #if WASM_ENABLE_BULK_MEMORY != 0
  4212. static bool
  4213. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4214. int data_seg_len, char *error_buf,
  4215. uint32 error_buf_size)
  4216. {
  4217. if (has_datacount_section && datacount_len != data_seg_len) {
  4218. set_error_buf(error_buf, error_buf_size,
  4219. "data count and data section have inconsistent lengths");
  4220. return false;
  4221. }
  4222. return true;
  4223. }
  4224. #endif
  4225. static bool
  4226. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4227. WASMModule *module,
  4228. #if WASM_ENABLE_BULK_MEMORY != 0
  4229. bool has_datacount_section,
  4230. #endif
  4231. bool clone_data_seg, char *error_buf,
  4232. uint32 error_buf_size)
  4233. {
  4234. const uint8 *p = buf, *p_end = buf_end;
  4235. uint32 data_seg_count, i, mem_index, data_seg_len;
  4236. uint64 total_size;
  4237. WASMDataSeg *dataseg;
  4238. InitializerExpression init_expr;
  4239. #if WASM_ENABLE_BULK_MEMORY != 0
  4240. bool is_passive = false;
  4241. uint32 mem_flag;
  4242. #endif
  4243. uint8 mem_offset_type = VALUE_TYPE_I32;
  4244. read_leb_uint32(p, p_end, data_seg_count);
  4245. #if WASM_ENABLE_BULK_MEMORY != 0
  4246. if (!check_data_count_consistency(has_datacount_section,
  4247. module->data_seg_count1, data_seg_count,
  4248. error_buf, error_buf_size)) {
  4249. return false;
  4250. }
  4251. #endif
  4252. if (data_seg_count) {
  4253. module->data_seg_count = data_seg_count;
  4254. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4255. if (!(module->data_segments =
  4256. loader_malloc(total_size, error_buf, error_buf_size))) {
  4257. return false;
  4258. }
  4259. for (i = 0; i < data_seg_count; i++) {
  4260. read_leb_uint32(p, p_end, mem_index);
  4261. #if WASM_ENABLE_BULK_MEMORY != 0
  4262. is_passive = false;
  4263. mem_flag = mem_index & 0x03;
  4264. switch (mem_flag) {
  4265. case 0x01:
  4266. is_passive = true;
  4267. #if WASM_ENABLE_WAMR_COMPILER != 0
  4268. module->is_bulk_memory_used = true;
  4269. #endif
  4270. break;
  4271. case 0x00:
  4272. /* no memory index, treat index as 0 */
  4273. mem_index = 0;
  4274. goto check_mem_index;
  4275. case 0x02:
  4276. /* read following memory index */
  4277. read_leb_uint32(p, p_end, mem_index);
  4278. #if WASM_ENABLE_WAMR_COMPILER != 0
  4279. module->is_bulk_memory_used = true;
  4280. #endif
  4281. check_mem_index:
  4282. if (mem_index
  4283. >= module->import_memory_count + module->memory_count) {
  4284. set_error_buf_v(error_buf, error_buf_size,
  4285. "unknown memory %d", mem_index);
  4286. return false;
  4287. }
  4288. break;
  4289. case 0x03:
  4290. default:
  4291. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4292. return false;
  4293. break;
  4294. }
  4295. #else
  4296. if (mem_index
  4297. >= module->import_memory_count + module->memory_count) {
  4298. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4299. mem_index);
  4300. return false;
  4301. }
  4302. #endif /* WASM_ENABLE_BULK_MEMORY */
  4303. #if WASM_ENABLE_BULK_MEMORY != 0
  4304. if (!is_passive)
  4305. #endif
  4306. {
  4307. #if WASM_ENABLE_MEMORY64 != 0
  4308. /* This memory_flag is from memory instead of data segment */
  4309. uint8 memory_flag;
  4310. if (module->import_memory_count > 0) {
  4311. memory_flag = module->import_memories[mem_index]
  4312. .u.memory.mem_type.flags;
  4313. }
  4314. else {
  4315. memory_flag =
  4316. module
  4317. ->memories[mem_index - module->import_memory_count]
  4318. .flags;
  4319. }
  4320. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4321. : VALUE_TYPE_I32;
  4322. #else
  4323. mem_offset_type = VALUE_TYPE_I32;
  4324. #endif
  4325. }
  4326. #if WASM_ENABLE_BULK_MEMORY != 0
  4327. if (!is_passive)
  4328. #endif
  4329. if (!load_init_expr(module, &p, p_end, &init_expr,
  4330. mem_offset_type, NULL, error_buf,
  4331. error_buf_size))
  4332. return false;
  4333. read_leb_uint32(p, p_end, data_seg_len);
  4334. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4335. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4336. return false;
  4337. }
  4338. #if WASM_ENABLE_BULK_MEMORY != 0
  4339. dataseg->is_passive = is_passive;
  4340. if (!is_passive)
  4341. #endif
  4342. {
  4343. bh_memcpy_s(&dataseg->base_offset,
  4344. sizeof(InitializerExpression), &init_expr,
  4345. sizeof(InitializerExpression));
  4346. dataseg->memory_index = mem_index;
  4347. }
  4348. dataseg->data_length = data_seg_len;
  4349. CHECK_BUF(p, p_end, data_seg_len);
  4350. if (clone_data_seg) {
  4351. if (!(dataseg->data = loader_malloc(
  4352. dataseg->data_length, error_buf, error_buf_size))) {
  4353. return false;
  4354. }
  4355. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4356. data_seg_len);
  4357. }
  4358. else {
  4359. dataseg->data = (uint8 *)p;
  4360. }
  4361. dataseg->is_data_cloned = clone_data_seg;
  4362. p += data_seg_len;
  4363. }
  4364. }
  4365. if (p != p_end) {
  4366. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4367. return false;
  4368. }
  4369. LOG_VERBOSE("Load data segment section success.\n");
  4370. return true;
  4371. fail:
  4372. return false;
  4373. }
  4374. #if WASM_ENABLE_BULK_MEMORY != 0
  4375. static bool
  4376. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4377. WASMModule *module, char *error_buf,
  4378. uint32 error_buf_size)
  4379. {
  4380. const uint8 *p = buf, *p_end = buf_end;
  4381. uint32 data_seg_count1 = 0;
  4382. read_leb_uint32(p, p_end, data_seg_count1);
  4383. module->data_seg_count1 = data_seg_count1;
  4384. if (p != p_end) {
  4385. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4386. return false;
  4387. }
  4388. #if WASM_ENABLE_WAMR_COMPILER != 0
  4389. module->is_bulk_memory_used = true;
  4390. #endif
  4391. LOG_VERBOSE("Load datacount section success.\n");
  4392. return true;
  4393. fail:
  4394. return false;
  4395. }
  4396. #endif
  4397. #if WASM_ENABLE_TAGS != 0
  4398. static bool
  4399. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4400. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4401. uint32 error_buf_size)
  4402. {
  4403. (void)buf_code;
  4404. (void)buf_code_end;
  4405. const uint8 *p = buf, *p_end = buf_end;
  4406. size_t total_size = 0;
  4407. /* number of tags defined in the section */
  4408. uint32 section_tag_count = 0;
  4409. uint8 tag_attribute;
  4410. uint32 tag_type;
  4411. WASMTag *tag = NULL;
  4412. /* get tag count */
  4413. read_leb_uint32(p, p_end, section_tag_count);
  4414. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4415. error_buf, error_buf_size))
  4416. return false;
  4417. module->tag_count = section_tag_count;
  4418. if (section_tag_count) {
  4419. total_size = sizeof(WASMTag *) * module->tag_count;
  4420. if (!(module->tags =
  4421. loader_malloc(total_size, error_buf, error_buf_size))) {
  4422. return false;
  4423. }
  4424. /* load each tag, imported tags precede the tags */
  4425. uint32 tag_index;
  4426. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4427. /* get the one byte attribute */
  4428. CHECK_BUF(p, p_end, 1);
  4429. tag_attribute = read_uint8(p);
  4430. /* get type */
  4431. read_leb_uint32(p, p_end, tag_type);
  4432. /* compare against module->types */
  4433. if (tag_type >= module->type_count) {
  4434. set_error_buf(error_buf, error_buf_size, "unknown type");
  4435. return false;
  4436. }
  4437. /* get return type (must be 0) */
  4438. /* check, that the type of the referred tag returns void */
  4439. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4440. if (func_type->result_count != 0) {
  4441. set_error_buf(error_buf, error_buf_size,
  4442. "non-empty tag result type");
  4443. goto fail;
  4444. }
  4445. if (!(tag = module->tags[tag_index] = loader_malloc(
  4446. sizeof(WASMTag), error_buf, error_buf_size))) {
  4447. return false;
  4448. }
  4449. /* store to module tag declarations */
  4450. tag->attribute = tag_attribute;
  4451. tag->type = tag_type;
  4452. tag->tag_type = func_type;
  4453. }
  4454. }
  4455. if (p != p_end) {
  4456. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4457. return false;
  4458. }
  4459. LOG_VERBOSE("Load tag section success.\n");
  4460. return true;
  4461. fail:
  4462. return false;
  4463. }
  4464. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4465. static bool
  4466. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4467. const uint8 *buf_func_end, WASMModule *module,
  4468. char *error_buf, uint32 error_buf_size)
  4469. {
  4470. const uint8 *p = buf, *p_end = buf_end;
  4471. const uint8 *p_func = buf_func;
  4472. uint32 func_count = 0, code_count;
  4473. /* code has been loaded in function section, so pass it here, just check
  4474. * whether function and code section have inconsistent lengths */
  4475. read_leb_uint32(p, p_end, code_count);
  4476. if (buf_func)
  4477. read_leb_uint32(p_func, buf_func_end, func_count);
  4478. if (func_count != code_count) {
  4479. set_error_buf(error_buf, error_buf_size,
  4480. "function and code section have inconsistent lengths");
  4481. return false;
  4482. }
  4483. LOG_VERBOSE("Load code segment section success.\n");
  4484. (void)module;
  4485. return true;
  4486. fail:
  4487. return false;
  4488. }
  4489. static bool
  4490. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4491. char *error_buf, uint32 error_buf_size)
  4492. {
  4493. const uint8 *p = buf, *p_end = buf_end;
  4494. WASMFuncType *type;
  4495. uint32 start_function;
  4496. read_leb_uint32(p, p_end, start_function);
  4497. if (start_function
  4498. >= module->function_count + module->import_function_count) {
  4499. set_error_buf(error_buf, error_buf_size, "unknown function");
  4500. return false;
  4501. }
  4502. if (start_function < module->import_function_count)
  4503. type = module->import_functions[start_function].u.function.func_type;
  4504. else
  4505. type = module->functions[start_function - module->import_function_count]
  4506. ->func_type;
  4507. if (type->param_count != 0 || type->result_count != 0) {
  4508. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4509. return false;
  4510. }
  4511. module->start_function = start_function;
  4512. if (p != p_end) {
  4513. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4514. return false;
  4515. }
  4516. LOG_VERBOSE("Load start section success.\n");
  4517. return true;
  4518. fail:
  4519. return false;
  4520. }
  4521. #if WASM_ENABLE_STRINGREF != 0
  4522. static bool
  4523. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4524. WASMModule *module, bool is_load_from_file_buf,
  4525. char *error_buf, uint32 error_buf_size)
  4526. {
  4527. const uint8 *p = buf, *p_end = buf_end;
  4528. int32 deferred_count, immediate_count, string_length, i;
  4529. uint64 total_size;
  4530. read_leb_uint32(p, p_end, deferred_count);
  4531. read_leb_uint32(p, p_end, immediate_count);
  4532. /* proposal set deferred_count for future extension */
  4533. if (deferred_count != 0) {
  4534. goto fail;
  4535. }
  4536. if (immediate_count > 0) {
  4537. total_size = sizeof(char *) * (uint64)immediate_count;
  4538. if (!(module->string_literal_ptrs =
  4539. loader_malloc(total_size, error_buf, error_buf_size))) {
  4540. goto fail;
  4541. }
  4542. module->string_literal_count = immediate_count;
  4543. total_size = sizeof(uint32) * (uint64)immediate_count;
  4544. if (!(module->string_literal_lengths =
  4545. loader_malloc(total_size, error_buf, error_buf_size))) {
  4546. goto fail;
  4547. }
  4548. for (i = 0; i < immediate_count; i++) {
  4549. read_leb_uint32(p, p_end, string_length);
  4550. CHECK_BUF(p, p_end, string_length);
  4551. module->string_literal_ptrs[i] = p;
  4552. module->string_literal_lengths[i] = string_length;
  4553. p += string_length;
  4554. }
  4555. }
  4556. if (p != p_end) {
  4557. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4558. goto fail;
  4559. }
  4560. LOG_VERBOSE("Load stringref section success.\n");
  4561. return true;
  4562. fail:
  4563. return false;
  4564. }
  4565. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4566. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4567. static bool
  4568. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4569. bool is_load_from_file_buf, char *error_buf,
  4570. uint32 error_buf_size)
  4571. {
  4572. const uint8 *p = buf, *p_end = buf_end;
  4573. uint32 name_type, subsection_size;
  4574. uint32 previous_name_type = 0;
  4575. uint32 num_func_name;
  4576. uint32 func_index;
  4577. uint32 previous_func_index = ~0U;
  4578. uint32 func_name_len;
  4579. uint32 name_index;
  4580. int i = 0;
  4581. if (p >= p_end) {
  4582. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4583. return false;
  4584. }
  4585. while (p < p_end) {
  4586. read_leb_uint32(p, p_end, name_type);
  4587. if (i != 0) {
  4588. if (name_type == previous_name_type) {
  4589. set_error_buf(error_buf, error_buf_size,
  4590. "duplicate sub-section");
  4591. return false;
  4592. }
  4593. if (name_type < previous_name_type) {
  4594. set_error_buf(error_buf, error_buf_size,
  4595. "out-of-order sub-section");
  4596. return false;
  4597. }
  4598. }
  4599. previous_name_type = name_type;
  4600. read_leb_uint32(p, p_end, subsection_size);
  4601. CHECK_BUF(p, p_end, subsection_size);
  4602. switch (name_type) {
  4603. case SUB_SECTION_TYPE_FUNC:
  4604. if (subsection_size) {
  4605. read_leb_uint32(p, p_end, num_func_name);
  4606. for (name_index = 0; name_index < num_func_name;
  4607. name_index++) {
  4608. read_leb_uint32(p, p_end, func_index);
  4609. if (func_index == previous_func_index) {
  4610. set_error_buf(error_buf, error_buf_size,
  4611. "duplicate function name");
  4612. return false;
  4613. }
  4614. if (func_index < previous_func_index
  4615. && previous_func_index != ~0U) {
  4616. set_error_buf(error_buf, error_buf_size,
  4617. "out-of-order function index ");
  4618. return false;
  4619. }
  4620. previous_func_index = func_index;
  4621. read_leb_uint32(p, p_end, func_name_len);
  4622. CHECK_BUF(p, p_end, func_name_len);
  4623. /* Skip the import functions */
  4624. if (func_index >= module->import_function_count) {
  4625. func_index -= module->import_function_count;
  4626. if (func_index >= module->function_count) {
  4627. set_error_buf(error_buf, error_buf_size,
  4628. "out-of-range function index");
  4629. return false;
  4630. }
  4631. if (!(module->functions[func_index]->field_name =
  4632. wasm_const_str_list_insert(
  4633. p, func_name_len, module,
  4634. #if WASM_ENABLE_WAMR_COMPILER != 0
  4635. false,
  4636. #else
  4637. is_load_from_file_buf,
  4638. #endif
  4639. error_buf, error_buf_size))) {
  4640. return false;
  4641. }
  4642. }
  4643. p += func_name_len;
  4644. }
  4645. }
  4646. break;
  4647. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4648. */
  4649. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4650. default:
  4651. p = p + subsection_size;
  4652. break;
  4653. }
  4654. i++;
  4655. }
  4656. return true;
  4657. fail:
  4658. return false;
  4659. }
  4660. #endif
  4661. static bool
  4662. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4663. bool is_load_from_file_buf, char *error_buf,
  4664. uint32 error_buf_size)
  4665. {
  4666. const uint8 *p = buf, *p_end = buf_end;
  4667. char section_name[32];
  4668. uint32 name_len, buffer_len;
  4669. if (p >= p_end) {
  4670. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4671. return false;
  4672. }
  4673. read_leb_uint32(p, p_end, name_len);
  4674. if (p + name_len > p_end) {
  4675. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4676. return false;
  4677. }
  4678. if (!wasm_check_utf8_str(p, name_len)) {
  4679. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4680. return false;
  4681. }
  4682. buffer_len = sizeof(section_name);
  4683. memset(section_name, 0, buffer_len);
  4684. if (name_len < buffer_len) {
  4685. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4686. }
  4687. else {
  4688. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4689. memset(section_name + buffer_len - 4, '.', 3);
  4690. }
  4691. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4692. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4693. module->name_section_buf = buf;
  4694. module->name_section_buf_end = buf_end;
  4695. p += name_len;
  4696. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4697. error_buf, error_buf_size)) {
  4698. return false;
  4699. }
  4700. LOG_VERBOSE("Load custom name section success.");
  4701. }
  4702. #endif
  4703. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4704. {
  4705. WASMCustomSection *section =
  4706. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4707. if (!section) {
  4708. return false;
  4709. }
  4710. section->name_addr = (char *)p;
  4711. section->name_len = name_len;
  4712. section->content_addr = (uint8 *)(p + name_len);
  4713. section->content_len = (uint32)(p_end - p - name_len);
  4714. section->next = module->custom_section_list;
  4715. module->custom_section_list = section;
  4716. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4717. return true;
  4718. }
  4719. #endif
  4720. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4721. (void)is_load_from_file_buf;
  4722. (void)module;
  4723. return true;
  4724. fail:
  4725. return false;
  4726. }
  4727. static void
  4728. calculate_global_data_offset(WASMModule *module)
  4729. {
  4730. uint32 i, data_offset;
  4731. data_offset = 0;
  4732. for (i = 0; i < module->import_global_count; i++) {
  4733. WASMGlobalImport *import_global =
  4734. &((module->import_globals + i)->u.global);
  4735. #if WASM_ENABLE_FAST_JIT != 0
  4736. import_global->data_offset = data_offset;
  4737. #endif
  4738. data_offset += wasm_value_type_size(import_global->type.val_type);
  4739. }
  4740. for (i = 0; i < module->global_count; i++) {
  4741. WASMGlobal *global = module->globals + i;
  4742. #if WASM_ENABLE_FAST_JIT != 0
  4743. global->data_offset = data_offset;
  4744. #endif
  4745. data_offset += wasm_value_type_size(global->type.val_type);
  4746. }
  4747. module->global_data_size = data_offset;
  4748. }
  4749. #if WASM_ENABLE_FAST_JIT != 0
  4750. static bool
  4751. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4752. uint32 error_buf_size)
  4753. {
  4754. #if WASM_ENABLE_LAZY_JIT != 0
  4755. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4756. #endif
  4757. uint32 i;
  4758. if (!module->function_count)
  4759. return true;
  4760. if (!(module->fast_jit_func_ptrs =
  4761. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4762. error_buf_size))) {
  4763. return false;
  4764. }
  4765. #if WASM_ENABLE_LAZY_JIT != 0
  4766. for (i = 0; i < module->function_count; i++) {
  4767. module->fast_jit_func_ptrs[i] =
  4768. jit_globals->compile_fast_jit_and_then_call;
  4769. }
  4770. #endif
  4771. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4772. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4773. set_error_buf(error_buf, error_buf_size,
  4774. "init fast jit thread lock failed");
  4775. return false;
  4776. }
  4777. module->fast_jit_thread_locks_inited[i] = true;
  4778. }
  4779. return true;
  4780. }
  4781. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4782. #if WASM_ENABLE_JIT != 0
  4783. static bool
  4784. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4785. uint32 error_buf_size)
  4786. {
  4787. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4788. AOTCompOption option = { 0 };
  4789. char *aot_last_error;
  4790. uint64 size;
  4791. #if WASM_ENABLE_GC != 0
  4792. bool gc_enabled = true;
  4793. #else
  4794. bool gc_enabled = false;
  4795. #endif
  4796. if (module->function_count == 0)
  4797. return true;
  4798. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4799. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4800. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4801. return false;
  4802. }
  4803. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4804. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4805. os_mutex_destroy(&module->tierup_wait_lock);
  4806. return false;
  4807. }
  4808. module->tierup_wait_lock_inited = true;
  4809. #endif
  4810. size = sizeof(void *) * (uint64)module->function_count
  4811. + sizeof(bool) * (uint64)module->function_count;
  4812. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4813. return false;
  4814. }
  4815. module->func_ptrs_compiled =
  4816. (bool *)((uint8 *)module->func_ptrs
  4817. + sizeof(void *) * module->function_count);
  4818. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4819. if (!module->comp_data) {
  4820. aot_last_error = aot_get_last_error();
  4821. bh_assert(aot_last_error != NULL);
  4822. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4823. return false;
  4824. }
  4825. option.is_jit_mode = true;
  4826. option.opt_level = llvm_jit_options->opt_level;
  4827. option.size_level = llvm_jit_options->size_level;
  4828. option.segue_flags = llvm_jit_options->segue_flags;
  4829. option.quick_invoke_c_api_import =
  4830. llvm_jit_options->quick_invoke_c_api_import;
  4831. #if WASM_ENABLE_BULK_MEMORY != 0
  4832. option.enable_bulk_memory = true;
  4833. #endif
  4834. #if WASM_ENABLE_THREAD_MGR != 0
  4835. option.enable_thread_mgr = true;
  4836. #endif
  4837. #if WASM_ENABLE_TAIL_CALL != 0
  4838. option.enable_tail_call = true;
  4839. #endif
  4840. #if WASM_ENABLE_SIMD != 0
  4841. option.enable_simd = true;
  4842. #endif
  4843. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4844. option.enable_ref_types = true;
  4845. #elif WASM_ENABLE_GC != 0
  4846. option.enable_gc = true;
  4847. #endif
  4848. option.enable_aux_stack_check = true;
  4849. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4850. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4851. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4852. aot_call_stack_features_init_default(&option.call_stack_features);
  4853. #endif
  4854. #if WASM_ENABLE_PERF_PROFILING != 0
  4855. option.enable_perf_profiling = true;
  4856. #endif
  4857. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4858. option.enable_memory_profiling = true;
  4859. option.enable_stack_estimation = true;
  4860. #endif
  4861. #if WASM_ENABLE_SHARED_HEAP != 0
  4862. option.enable_shared_heap = true;
  4863. #endif
  4864. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4865. if (!module->comp_ctx) {
  4866. aot_last_error = aot_get_last_error();
  4867. bh_assert(aot_last_error != NULL);
  4868. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4869. return false;
  4870. }
  4871. return true;
  4872. }
  4873. static bool
  4874. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4875. uint32 error_buf_size)
  4876. {
  4877. char *aot_last_error;
  4878. uint32 i;
  4879. if (module->function_count == 0)
  4880. return true;
  4881. if (!aot_compile_wasm(module->comp_ctx)) {
  4882. aot_last_error = aot_get_last_error();
  4883. bh_assert(aot_last_error != NULL);
  4884. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4885. return false;
  4886. }
  4887. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4888. if (module->orcjit_stop_compiling)
  4889. return false;
  4890. #endif
  4891. bh_print_time("Begin to lookup llvm jit functions");
  4892. for (i = 0; i < module->function_count; i++) {
  4893. LLVMOrcJITTargetAddress func_addr = 0;
  4894. LLVMErrorRef error;
  4895. char func_name[48];
  4896. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4897. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4898. func_name);
  4899. if (error != LLVMErrorSuccess) {
  4900. char *err_msg = LLVMGetErrorMessage(error);
  4901. set_error_buf_v(error_buf, error_buf_size,
  4902. "failed to compile llvm jit function: %s", err_msg);
  4903. LLVMDisposeErrorMessage(err_msg);
  4904. return false;
  4905. }
  4906. /**
  4907. * No need to lock the func_ptr[func_idx] here as it is basic
  4908. * data type, the load/store for it can be finished by one cpu
  4909. * instruction, and there can be only one cpu instruction
  4910. * loading/storing at the same time.
  4911. */
  4912. module->func_ptrs[i] = (void *)func_addr;
  4913. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4914. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4915. if (module->orcjit_stop_compiling)
  4916. return false;
  4917. #endif
  4918. }
  4919. bh_print_time("End lookup llvm jit functions");
  4920. return true;
  4921. }
  4922. #endif /* end of WASM_ENABLE_JIT != 0 */
  4923. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4924. && WASM_ENABLE_LAZY_JIT != 0
  4925. static void *
  4926. init_llvm_jit_functions_stage2_callback(void *arg)
  4927. {
  4928. WASMModule *module = (WASMModule *)arg;
  4929. char error_buf[128];
  4930. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4931. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4932. module->orcjit_stop_compiling = true;
  4933. return NULL;
  4934. }
  4935. os_mutex_lock(&module->tierup_wait_lock);
  4936. module->llvm_jit_inited = true;
  4937. os_cond_broadcast(&module->tierup_wait_cond);
  4938. os_mutex_unlock(&module->tierup_wait_lock);
  4939. return NULL;
  4940. }
  4941. #endif
  4942. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4943. /* The callback function to compile jit functions */
  4944. static void *
  4945. orcjit_thread_callback(void *arg)
  4946. {
  4947. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4948. #if WASM_ENABLE_JIT != 0
  4949. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4950. #endif
  4951. WASMModule *module = thread_arg->module;
  4952. uint32 group_idx = thread_arg->group_idx;
  4953. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4954. uint32 func_count = module->function_count;
  4955. uint32 i;
  4956. #if WASM_ENABLE_FAST_JIT != 0
  4957. /* Compile fast jit functions of this group */
  4958. for (i = group_idx; i < func_count; i += group_stride) {
  4959. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4960. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4961. break;
  4962. }
  4963. if (module->orcjit_stop_compiling) {
  4964. return NULL;
  4965. }
  4966. }
  4967. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4968. os_mutex_lock(&module->tierup_wait_lock);
  4969. module->fast_jit_ready_groups++;
  4970. os_mutex_unlock(&module->tierup_wait_lock);
  4971. #endif
  4972. #endif
  4973. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4974. && WASM_ENABLE_LAZY_JIT != 0
  4975. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4976. for (i = group_idx; i < func_count;
  4977. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4978. uint32 j;
  4979. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4980. if (i + j * group_stride < func_count) {
  4981. if (!jit_compiler_set_call_to_fast_jit(
  4982. module,
  4983. i + j * group_stride + module->import_function_count)) {
  4984. LOG_ERROR(
  4985. "failed to compile call_to_fast_jit for func %u\n",
  4986. i + j * group_stride + module->import_function_count);
  4987. module->orcjit_stop_compiling = true;
  4988. return NULL;
  4989. }
  4990. }
  4991. if (module->orcjit_stop_compiling) {
  4992. return NULL;
  4993. }
  4994. }
  4995. }
  4996. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4997. fast jit functions are compiled */
  4998. os_mutex_lock(&module->tierup_wait_lock);
  4999. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5000. && module->fast_jit_ready_groups >= group_stride)) {
  5001. os_cond_reltimedwait(&module->tierup_wait_cond,
  5002. &module->tierup_wait_lock, 10000);
  5003. if (module->orcjit_stop_compiling) {
  5004. /* init_llvm_jit_functions_stage2 failed */
  5005. os_mutex_unlock(&module->tierup_wait_lock);
  5006. return NULL;
  5007. }
  5008. }
  5009. os_mutex_unlock(&module->tierup_wait_lock);
  5010. #endif
  5011. #if WASM_ENABLE_JIT != 0
  5012. /* Compile llvm jit functions of this group */
  5013. for (i = group_idx; i < func_count;
  5014. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5015. LLVMOrcJITTargetAddress func_addr = 0;
  5016. LLVMErrorRef error;
  5017. char func_name[48];
  5018. typedef void (*F)(void);
  5019. union {
  5020. F f;
  5021. void *v;
  5022. } u;
  5023. uint32 j;
  5024. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5025. "_wrapper");
  5026. LOG_DEBUG("compile llvm jit func %s", func_name);
  5027. error =
  5028. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5029. if (error != LLVMErrorSuccess) {
  5030. char *err_msg = LLVMGetErrorMessage(error);
  5031. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5032. LLVMDisposeErrorMessage(err_msg);
  5033. break;
  5034. }
  5035. /* Call the jit wrapper function to trigger its compilation, so as
  5036. to compile the actual jit functions, since we add the latter to
  5037. function list in the PartitionFunction callback */
  5038. u.v = (void *)func_addr;
  5039. u.f();
  5040. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5041. if (i + j * group_stride < func_count) {
  5042. module->func_ptrs_compiled[i + j * group_stride] = true;
  5043. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5044. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5045. i + j * group_stride);
  5046. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5047. func_name);
  5048. if (error != LLVMErrorSuccess) {
  5049. char *err_msg = LLVMGetErrorMessage(error);
  5050. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5051. err_msg);
  5052. LLVMDisposeErrorMessage(err_msg);
  5053. /* Ignore current llvm jit func, as its func ptr is
  5054. previous set to call_to_fast_jit, which also works */
  5055. continue;
  5056. }
  5057. jit_compiler_set_llvm_jit_func_ptr(
  5058. module,
  5059. i + j * group_stride + module->import_function_count,
  5060. (void *)func_addr);
  5061. /* Try to switch to call this llvm jit function instead of
  5062. fast jit function from fast jit jitted code */
  5063. jit_compiler_set_call_to_llvm_jit(
  5064. module,
  5065. i + j * group_stride + module->import_function_count);
  5066. #endif
  5067. }
  5068. }
  5069. if (module->orcjit_stop_compiling) {
  5070. break;
  5071. }
  5072. }
  5073. #endif
  5074. return NULL;
  5075. }
  5076. static void
  5077. orcjit_stop_compile_threads(WASMModule *module)
  5078. {
  5079. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5080. / sizeof(OrcJitThreadArg));
  5081. module->orcjit_stop_compiling = true;
  5082. for (i = 0; i < thread_num; i++) {
  5083. if (module->orcjit_threads[i])
  5084. os_thread_join(module->orcjit_threads[i], NULL);
  5085. }
  5086. }
  5087. static bool
  5088. compile_jit_functions(WASMModule *module, char *error_buf,
  5089. uint32 error_buf_size)
  5090. {
  5091. uint32 thread_num =
  5092. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5093. uint32 i, j;
  5094. bh_print_time("Begin to compile jit functions");
  5095. /* Create threads to compile the jit functions */
  5096. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5097. #if WASM_ENABLE_JIT != 0
  5098. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5099. #endif
  5100. module->orcjit_thread_args[i].module = module;
  5101. module->orcjit_thread_args[i].group_idx = i;
  5102. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5103. (void *)&module->orcjit_thread_args[i],
  5104. APP_THREAD_STACK_SIZE_DEFAULT)
  5105. != 0) {
  5106. set_error_buf(error_buf, error_buf_size,
  5107. "create orcjit compile thread failed");
  5108. /* Terminate the threads created */
  5109. module->orcjit_stop_compiling = true;
  5110. for (j = 0; j < i; j++) {
  5111. os_thread_join(module->orcjit_threads[j], NULL);
  5112. }
  5113. return false;
  5114. }
  5115. }
  5116. #if WASM_ENABLE_LAZY_JIT == 0
  5117. /* Wait until all jit functions are compiled for eager mode */
  5118. for (i = 0; i < thread_num; i++) {
  5119. if (module->orcjit_threads[i])
  5120. os_thread_join(module->orcjit_threads[i], NULL);
  5121. }
  5122. #if WASM_ENABLE_FAST_JIT != 0
  5123. /* Ensure all the fast-jit functions are compiled */
  5124. for (i = 0; i < module->function_count; i++) {
  5125. if (!jit_compiler_is_compiled(module,
  5126. i + module->import_function_count)) {
  5127. set_error_buf(error_buf, error_buf_size,
  5128. "failed to compile fast jit function");
  5129. return false;
  5130. }
  5131. }
  5132. #endif
  5133. #if WASM_ENABLE_JIT != 0
  5134. /* Ensure all the llvm-jit functions are compiled */
  5135. for (i = 0; i < module->function_count; i++) {
  5136. if (!module->func_ptrs_compiled[i]) {
  5137. set_error_buf(error_buf, error_buf_size,
  5138. "failed to compile llvm jit function");
  5139. return false;
  5140. }
  5141. }
  5142. #endif
  5143. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5144. bh_print_time("End compile jit functions");
  5145. return true;
  5146. }
  5147. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5148. static bool
  5149. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5150. uint32 cur_func_idx, char *error_buf,
  5151. uint32 error_buf_size);
  5152. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5153. void **
  5154. wasm_interp_get_handle_table(void);
  5155. static void **handle_table;
  5156. #endif
  5157. static bool
  5158. load_from_sections(WASMModule *module, WASMSection *sections,
  5159. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5160. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5161. {
  5162. WASMExport *export;
  5163. WASMSection *section = sections;
  5164. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5165. *buf_func = NULL, *buf_func_end = NULL;
  5166. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5167. WASMGlobal *aux_stack_top_global = NULL, *global;
  5168. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5169. aux_stack_top = (uint64)-1LL;
  5170. uint32 global_index, func_index, i;
  5171. uint32 aux_data_end_global_index = (uint32)-1;
  5172. uint32 aux_heap_base_global_index = (uint32)-1;
  5173. WASMFuncType *func_type;
  5174. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5175. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5176. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5177. #if WASM_ENABLE_BULK_MEMORY != 0
  5178. bool has_datacount_section = false;
  5179. #endif
  5180. /* Find code and function sections if have */
  5181. while (section) {
  5182. if (section->section_type == SECTION_TYPE_CODE) {
  5183. buf_code = section->section_body;
  5184. buf_code_end = buf_code + section->section_body_size;
  5185. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5186. module->buf_code = (uint8 *)buf_code;
  5187. module->buf_code_size = section->section_body_size;
  5188. #endif
  5189. }
  5190. else if (section->section_type == SECTION_TYPE_FUNC) {
  5191. buf_func = section->section_body;
  5192. buf_func_end = buf_func + section->section_body_size;
  5193. }
  5194. section = section->next;
  5195. }
  5196. section = sections;
  5197. while (section) {
  5198. buf = section->section_body;
  5199. buf_end = buf + section->section_body_size;
  5200. switch (section->section_type) {
  5201. case SECTION_TYPE_USER:
  5202. /* unsupported user section, ignore it. */
  5203. if (!load_user_section(buf, buf_end, module,
  5204. reuse_const_strings, error_buf,
  5205. error_buf_size))
  5206. return false;
  5207. break;
  5208. case SECTION_TYPE_TYPE:
  5209. if (!load_type_section(buf, buf_end, module, error_buf,
  5210. error_buf_size))
  5211. return false;
  5212. break;
  5213. case SECTION_TYPE_IMPORT:
  5214. if (!load_import_section(buf, buf_end, module,
  5215. reuse_const_strings, no_resolve,
  5216. error_buf, error_buf_size))
  5217. return false;
  5218. break;
  5219. case SECTION_TYPE_FUNC:
  5220. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5221. module, error_buf, error_buf_size))
  5222. return false;
  5223. break;
  5224. case SECTION_TYPE_TABLE:
  5225. if (!load_table_section(buf, buf_end, module, error_buf,
  5226. error_buf_size))
  5227. return false;
  5228. break;
  5229. case SECTION_TYPE_MEMORY:
  5230. if (!load_memory_section(buf, buf_end, module, error_buf,
  5231. error_buf_size))
  5232. return false;
  5233. break;
  5234. #if WASM_ENABLE_TAGS != 0
  5235. case SECTION_TYPE_TAG:
  5236. /* load tag declaration section */
  5237. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5238. module, error_buf, error_buf_size))
  5239. return false;
  5240. break;
  5241. #endif
  5242. case SECTION_TYPE_GLOBAL:
  5243. if (!load_global_section(buf, buf_end, module, error_buf,
  5244. error_buf_size))
  5245. return false;
  5246. break;
  5247. case SECTION_TYPE_EXPORT:
  5248. if (!load_export_section(buf, buf_end, module,
  5249. reuse_const_strings, error_buf,
  5250. error_buf_size))
  5251. return false;
  5252. break;
  5253. case SECTION_TYPE_START:
  5254. if (!load_start_section(buf, buf_end, module, error_buf,
  5255. error_buf_size))
  5256. return false;
  5257. break;
  5258. case SECTION_TYPE_ELEM:
  5259. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5260. error_buf_size))
  5261. return false;
  5262. break;
  5263. case SECTION_TYPE_CODE:
  5264. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5265. module, error_buf, error_buf_size))
  5266. return false;
  5267. break;
  5268. case SECTION_TYPE_DATA:
  5269. if (!load_data_segment_section(buf, buf_end, module,
  5270. #if WASM_ENABLE_BULK_MEMORY != 0
  5271. has_datacount_section,
  5272. #endif
  5273. clone_data_seg, error_buf,
  5274. error_buf_size))
  5275. return false;
  5276. break;
  5277. #if WASM_ENABLE_BULK_MEMORY != 0
  5278. case SECTION_TYPE_DATACOUNT:
  5279. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5280. error_buf_size))
  5281. return false;
  5282. has_datacount_section = true;
  5283. break;
  5284. #endif
  5285. #if WASM_ENABLE_STRINGREF != 0
  5286. case SECTION_TYPE_STRINGREF:
  5287. if (!load_stringref_section(buf, buf_end, module,
  5288. reuse_const_strings, error_buf,
  5289. error_buf_size))
  5290. return false;
  5291. break;
  5292. #endif
  5293. default:
  5294. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5295. return false;
  5296. }
  5297. section = section->next;
  5298. }
  5299. #if WASM_ENABLE_BULK_MEMORY != 0
  5300. if (!check_data_count_consistency(
  5301. has_datacount_section, module->data_seg_count1,
  5302. module->data_seg_count, error_buf, error_buf_size)) {
  5303. return false;
  5304. }
  5305. #endif
  5306. module->aux_data_end_global_index = (uint32)-1;
  5307. module->aux_heap_base_global_index = (uint32)-1;
  5308. module->aux_stack_top_global_index = (uint32)-1;
  5309. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5310. export = module->exports;
  5311. for (i = 0; i < module->export_count; i++, export ++) {
  5312. if (export->kind == EXPORT_KIND_GLOBAL) {
  5313. if (!strcmp(export->name, "__heap_base")) {
  5314. if (export->index < module->import_global_count) {
  5315. LOG_DEBUG("Skip the process if __heap_base is imported "
  5316. "instead of being a local global");
  5317. continue;
  5318. }
  5319. /* only process linker-generated symbols */
  5320. global_index = export->index - module->import_global_count;
  5321. global = module->globals + global_index;
  5322. if (global->type.val_type == VALUE_TYPE_I32
  5323. && !global->type.is_mutable
  5324. && global->init_expr.init_expr_type
  5325. == INIT_EXPR_TYPE_I32_CONST) {
  5326. aux_heap_base_global = global;
  5327. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5328. aux_heap_base_global_index = export->index;
  5329. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5330. aux_heap_base);
  5331. }
  5332. }
  5333. else if (!strcmp(export->name, "__data_end")) {
  5334. if (export->index < module->import_global_count) {
  5335. LOG_DEBUG("Skip the process if __data_end is imported "
  5336. "instead of being a local global");
  5337. continue;
  5338. }
  5339. /* only process linker-generated symbols */
  5340. global_index = export->index - module->import_global_count;
  5341. global = module->globals + global_index;
  5342. if (global->type.val_type == VALUE_TYPE_I32
  5343. && !global->type.is_mutable
  5344. && global->init_expr.init_expr_type
  5345. == INIT_EXPR_TYPE_I32_CONST) {
  5346. aux_data_end_global = global;
  5347. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5348. aux_data_end_global_index = export->index;
  5349. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5350. aux_data_end);
  5351. aux_data_end = align_uint64(aux_data_end, 16);
  5352. }
  5353. }
  5354. /* For module compiled with -pthread option, the global is:
  5355. [0] stack_top <-- 0
  5356. [1] tls_pointer
  5357. [2] tls_size
  5358. [3] data_end <-- 3
  5359. [4] global_base
  5360. [5] heap_base <-- 5
  5361. [6] dso_handle
  5362. For module compiled without -pthread option:
  5363. [0] stack_top <-- 0
  5364. [1] data_end <-- 1
  5365. [2] global_base
  5366. [3] heap_base <-- 3
  5367. [4] dso_handle
  5368. */
  5369. if (aux_data_end_global && aux_heap_base_global
  5370. && aux_data_end <= aux_heap_base) {
  5371. module->aux_data_end_global_index = aux_data_end_global_index;
  5372. module->aux_data_end = aux_data_end;
  5373. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5374. module->aux_heap_base = aux_heap_base;
  5375. /* Resolve aux stack top global */
  5376. for (global_index = 0; global_index < module->global_count;
  5377. global_index++) {
  5378. global = module->globals + global_index;
  5379. if (global->type.is_mutable /* heap_base and data_end is
  5380. not mutable */
  5381. && global->type.val_type == VALUE_TYPE_I32
  5382. && global->init_expr.init_expr_type
  5383. == INIT_EXPR_TYPE_I32_CONST
  5384. && (uint64)(uint32)global->init_expr.u.i32
  5385. <= aux_heap_base) {
  5386. aux_stack_top_global = global;
  5387. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5388. module->aux_stack_top_global_index =
  5389. module->import_global_count + global_index;
  5390. module->aux_stack_bottom = aux_stack_top;
  5391. module->aux_stack_size =
  5392. aux_stack_top > aux_data_end
  5393. ? (uint32)(aux_stack_top - aux_data_end)
  5394. : (uint32)aux_stack_top;
  5395. LOG_VERBOSE(
  5396. "Found aux stack top global, value: %" PRIu64 ", "
  5397. "global index: %d, stack size: %d",
  5398. aux_stack_top, global_index,
  5399. module->aux_stack_size);
  5400. break;
  5401. }
  5402. }
  5403. if (!aux_stack_top_global) {
  5404. /* Auxiliary stack global isn't found, it must be unused
  5405. in the wasm app, as if it is used, the global must be
  5406. defined. Here we set it to __heap_base global and set
  5407. its size to 0. */
  5408. aux_stack_top_global = aux_heap_base_global;
  5409. aux_stack_top = aux_heap_base;
  5410. module->aux_stack_top_global_index =
  5411. module->aux_heap_base_global_index;
  5412. module->aux_stack_bottom = aux_stack_top;
  5413. module->aux_stack_size = 0;
  5414. }
  5415. break;
  5416. }
  5417. }
  5418. }
  5419. module->malloc_function = (uint32)-1;
  5420. module->free_function = (uint32)-1;
  5421. module->retain_function = (uint32)-1;
  5422. /* Resolve malloc/free function exported by wasm module */
  5423. #if WASM_ENABLE_MEMORY64 != 0
  5424. if (has_module_memory64(module))
  5425. malloc_free_io_type = VALUE_TYPE_I64;
  5426. #endif
  5427. export = module->exports;
  5428. for (i = 0; i < module->export_count; i++, export ++) {
  5429. if (export->kind == EXPORT_KIND_FUNC) {
  5430. if (!strcmp(export->name, "malloc")
  5431. && export->index >= module->import_function_count) {
  5432. func_index = export->index - module->import_function_count;
  5433. func_type = module->functions[func_index]->func_type;
  5434. if (func_type->param_count == 1 && func_type->result_count == 1
  5435. && func_type->types[0] == malloc_free_io_type
  5436. && func_type->types[1] == malloc_free_io_type) {
  5437. bh_assert(module->malloc_function == (uint32)-1);
  5438. module->malloc_function = export->index;
  5439. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5440. export->name, export->index);
  5441. }
  5442. }
  5443. else if (!strcmp(export->name, "__new")
  5444. && export->index >= module->import_function_count) {
  5445. /* __new && __pin for AssemblyScript */
  5446. func_index = export->index - module->import_function_count;
  5447. func_type = module->functions[func_index]->func_type;
  5448. if (func_type->param_count == 2 && func_type->result_count == 1
  5449. && func_type->types[0] == malloc_free_io_type
  5450. && func_type->types[1] == VALUE_TYPE_I32
  5451. && func_type->types[2] == malloc_free_io_type) {
  5452. uint32 j;
  5453. WASMExport *export_tmp;
  5454. bh_assert(module->malloc_function == (uint32)-1);
  5455. module->malloc_function = export->index;
  5456. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5457. export->name, export->index);
  5458. /* resolve retain function.
  5459. If not found, reset malloc function index */
  5460. export_tmp = module->exports;
  5461. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5462. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5463. && (!strcmp(export_tmp->name, "__retain")
  5464. || (!strcmp(export_tmp->name, "__pin")))
  5465. && (export_tmp->index
  5466. >= module->import_function_count)) {
  5467. func_index = export_tmp->index
  5468. - module->import_function_count;
  5469. func_type =
  5470. module->functions[func_index]->func_type;
  5471. if (func_type->param_count == 1
  5472. && func_type->result_count == 1
  5473. && func_type->types[0] == malloc_free_io_type
  5474. && func_type->types[1] == malloc_free_io_type) {
  5475. bh_assert(module->retain_function
  5476. == (uint32)-1);
  5477. module->retain_function = export_tmp->index;
  5478. LOG_VERBOSE("Found retain function, name: %s, "
  5479. "index: %u",
  5480. export_tmp->name,
  5481. export_tmp->index);
  5482. break;
  5483. }
  5484. }
  5485. }
  5486. if (j == module->export_count) {
  5487. module->malloc_function = (uint32)-1;
  5488. LOG_VERBOSE("Can't find retain function,"
  5489. "reset malloc function index to -1");
  5490. }
  5491. }
  5492. }
  5493. else if (((!strcmp(export->name, "free"))
  5494. || (!strcmp(export->name, "__release"))
  5495. || (!strcmp(export->name, "__unpin")))
  5496. && export->index >= module->import_function_count) {
  5497. func_index = export->index - module->import_function_count;
  5498. func_type = module->functions[func_index]->func_type;
  5499. if (func_type->param_count == 1 && func_type->result_count == 0
  5500. && func_type->types[0] == malloc_free_io_type) {
  5501. bh_assert(module->free_function == (uint32)-1);
  5502. module->free_function = export->index;
  5503. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5504. export->name, export->index);
  5505. }
  5506. }
  5507. }
  5508. }
  5509. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5510. handle_table = wasm_interp_get_handle_table();
  5511. #endif
  5512. for (i = 0; i < module->function_count; i++) {
  5513. WASMFunction *func = module->functions[i];
  5514. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5515. error_buf_size)) {
  5516. return false;
  5517. }
  5518. if (i == module->function_count - 1
  5519. && func->code + func->code_size != buf_code_end) {
  5520. set_error_buf(error_buf, error_buf_size,
  5521. "code section size mismatch");
  5522. return false;
  5523. }
  5524. }
  5525. if (!module->possible_memory_grow) {
  5526. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5527. if (aux_data_end_global && aux_heap_base_global
  5528. && aux_stack_top_global) {
  5529. uint64 init_memory_size;
  5530. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5531. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5532. * valid range of uint32 */
  5533. if (shrunk_memory_size <= UINT32_MAX) {
  5534. if (module->import_memory_count) {
  5535. WASMMemoryImport *memory_import =
  5536. &module->import_memories[0].u.memory;
  5537. init_memory_size =
  5538. (uint64)memory_import->mem_type.num_bytes_per_page
  5539. * memory_import->mem_type.init_page_count;
  5540. if (shrunk_memory_size <= init_memory_size) {
  5541. /* Reset memory info to decrease memory usage */
  5542. memory_import->mem_type.num_bytes_per_page =
  5543. (uint32)shrunk_memory_size;
  5544. memory_import->mem_type.init_page_count = 1;
  5545. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5546. shrunk_memory_size);
  5547. }
  5548. }
  5549. if (module->memory_count) {
  5550. WASMMemory *memory = &module->memories[0];
  5551. init_memory_size = (uint64)memory->num_bytes_per_page
  5552. * memory->init_page_count;
  5553. if (shrunk_memory_size <= init_memory_size) {
  5554. /* Reset memory info to decrease memory usage */
  5555. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5556. memory->init_page_count = 1;
  5557. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5558. shrunk_memory_size);
  5559. }
  5560. }
  5561. }
  5562. }
  5563. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  5564. #if WASM_ENABLE_MULTI_MODULE == 0
  5565. if (module->import_memory_count) {
  5566. WASMMemoryImport *memory_import =
  5567. &module->import_memories[0].u.memory;
  5568. /* Only resize the memory to one big page if num_bytes_per_page is
  5569. * in valid range of uint32 */
  5570. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5571. memory_import->mem_type.num_bytes_per_page *=
  5572. memory_import->mem_type.init_page_count;
  5573. if (memory_import->mem_type.init_page_count > 0)
  5574. memory_import->mem_type.init_page_count =
  5575. memory_import->mem_type.max_page_count = 1;
  5576. else
  5577. memory_import->mem_type.init_page_count =
  5578. memory_import->mem_type.max_page_count = 0;
  5579. }
  5580. }
  5581. if (module->memory_count) {
  5582. WASMMemory *memory = &module->memories[0];
  5583. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5584. * valid range of uint32 */
  5585. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5586. memory->num_bytes_per_page *= memory->init_page_count;
  5587. if (memory->init_page_count > 0)
  5588. memory->init_page_count = memory->max_page_count = 1;
  5589. else
  5590. memory->init_page_count = memory->max_page_count = 0;
  5591. }
  5592. }
  5593. #endif
  5594. }
  5595. #if WASM_ENABLE_MEMORY64 != 0
  5596. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5597. false))
  5598. return false;
  5599. #endif
  5600. calculate_global_data_offset(module);
  5601. #if WASM_ENABLE_FAST_JIT != 0
  5602. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5603. return false;
  5604. }
  5605. #endif
  5606. #if WASM_ENABLE_JIT != 0
  5607. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5608. return false;
  5609. }
  5610. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5611. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5612. return false;
  5613. }
  5614. #else
  5615. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5616. thread fast jit execution, since applying llvm optimizations in
  5617. aot_compile_wasm may cost a lot of time.
  5618. Create thread with enough native stack to apply llvm optimizations */
  5619. if (os_thread_create(&module->llvm_jit_init_thread,
  5620. init_llvm_jit_functions_stage2_callback,
  5621. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5622. != 0) {
  5623. set_error_buf(error_buf, error_buf_size,
  5624. "create orcjit compile thread failed");
  5625. return false;
  5626. }
  5627. #endif
  5628. #endif
  5629. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5630. /* Create threads to compile the jit functions */
  5631. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5632. return false;
  5633. }
  5634. #endif
  5635. #if WASM_ENABLE_MEMORY_TRACING != 0
  5636. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5637. #endif
  5638. return true;
  5639. }
  5640. static WASMModule *
  5641. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5642. {
  5643. WASMModule *module =
  5644. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5645. bh_list_status ret;
  5646. if (!module) {
  5647. return NULL;
  5648. }
  5649. module->module_type = Wasm_Module_Bytecode;
  5650. /* Set start_function to -1, means no start function */
  5651. module->start_function = (uint32)-1;
  5652. module->name = name;
  5653. module->is_binary_freeable = false;
  5654. #if WASM_ENABLE_FAST_INTERP == 0
  5655. module->br_table_cache_list = &module->br_table_cache_list_head;
  5656. ret = bh_list_init(module->br_table_cache_list);
  5657. bh_assert(ret == BH_LIST_SUCCESS);
  5658. #endif
  5659. #if WASM_ENABLE_MULTI_MODULE != 0
  5660. module->import_module_list = &module->import_module_list_head;
  5661. ret = bh_list_init(module->import_module_list);
  5662. bh_assert(ret == BH_LIST_SUCCESS);
  5663. #endif
  5664. #if WASM_ENABLE_DEBUG_INTERP != 0
  5665. ret = bh_list_init(&module->fast_opcode_list);
  5666. bh_assert(ret == BH_LIST_SUCCESS);
  5667. #endif
  5668. #if WASM_ENABLE_GC != 0
  5669. if (!(module->ref_type_set =
  5670. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5671. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5672. goto fail1;
  5673. }
  5674. if (os_mutex_init(&module->rtt_type_lock)) {
  5675. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5676. goto fail2;
  5677. }
  5678. #endif
  5679. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5680. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5681. && WASM_ENABLE_LAZY_JIT != 0)
  5682. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5683. set_error_buf(error_buf, error_buf_size,
  5684. "init instance list lock failed");
  5685. goto fail3;
  5686. }
  5687. #endif
  5688. (void)ret;
  5689. return module;
  5690. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5691. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5692. && WASM_ENABLE_LAZY_JIT != 0)
  5693. fail3:
  5694. #endif
  5695. #if WASM_ENABLE_GC != 0
  5696. os_mutex_destroy(&module->rtt_type_lock);
  5697. fail2:
  5698. bh_hash_map_destroy(module->ref_type_set);
  5699. fail1:
  5700. #endif
  5701. wasm_runtime_free(module);
  5702. return NULL;
  5703. }
  5704. #if WASM_ENABLE_DEBUG_INTERP != 0
  5705. static bool
  5706. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5707. uint32 error_buf_size)
  5708. {
  5709. WASMFastOPCodeNode *fast_op =
  5710. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5711. if (fast_op) {
  5712. fast_op->offset = pos - module->load_addr;
  5713. fast_op->orig_op = orig_op;
  5714. bh_list_insert(&module->fast_opcode_list, fast_op);
  5715. }
  5716. return fast_op ? true : false;
  5717. }
  5718. #endif
  5719. WASMModule *
  5720. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5721. uint32 error_buf_size)
  5722. {
  5723. WASMModule *module = create_module("", error_buf, error_buf_size);
  5724. if (!module)
  5725. return NULL;
  5726. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  5727. error_buf_size)) {
  5728. wasm_loader_unload(module);
  5729. return NULL;
  5730. }
  5731. LOG_VERBOSE("Load module from sections success.\n");
  5732. return module;
  5733. }
  5734. static void
  5735. destroy_sections(WASMSection *section_list)
  5736. {
  5737. WASMSection *section = section_list, *next;
  5738. while (section) {
  5739. next = section->next;
  5740. wasm_runtime_free(section);
  5741. section = next;
  5742. }
  5743. }
  5744. /* clang-format off */
  5745. static uint8 section_ids[] = {
  5746. SECTION_TYPE_USER,
  5747. SECTION_TYPE_TYPE,
  5748. SECTION_TYPE_IMPORT,
  5749. SECTION_TYPE_FUNC,
  5750. SECTION_TYPE_TABLE,
  5751. SECTION_TYPE_MEMORY,
  5752. #if WASM_ENABLE_TAGS != 0
  5753. SECTION_TYPE_TAG,
  5754. #endif
  5755. #if WASM_ENABLE_STRINGREF != 0
  5756. /* must immediately precede the global section,
  5757. or where the global section would be */
  5758. SECTION_TYPE_STRINGREF,
  5759. #endif
  5760. SECTION_TYPE_GLOBAL,
  5761. SECTION_TYPE_EXPORT,
  5762. SECTION_TYPE_START,
  5763. SECTION_TYPE_ELEM,
  5764. #if WASM_ENABLE_BULK_MEMORY != 0
  5765. SECTION_TYPE_DATACOUNT,
  5766. #endif
  5767. SECTION_TYPE_CODE,
  5768. SECTION_TYPE_DATA
  5769. };
  5770. /* clang-format on */
  5771. static uint8
  5772. get_section_index(uint8 section_type)
  5773. {
  5774. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5775. for (uint8 i = 0; i < max_id; i++) {
  5776. if (section_type == section_ids[i])
  5777. return i;
  5778. }
  5779. return (uint8)-1;
  5780. }
  5781. static bool
  5782. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5783. char *error_buf, uint32 error_buf_size)
  5784. {
  5785. WASMSection *section_list_end = NULL, *section;
  5786. const uint8 *p = buf, *p_end = buf + size;
  5787. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5788. uint32 section_size;
  5789. bh_assert(!*p_section_list);
  5790. p += 8;
  5791. while (p < p_end) {
  5792. CHECK_BUF(p, p_end, 1);
  5793. section_type = read_uint8(p);
  5794. section_index = get_section_index(section_type);
  5795. if (section_index != (uint8)-1) {
  5796. if (section_type != SECTION_TYPE_USER) {
  5797. /* Custom sections may be inserted at any place,
  5798. while other sections must occur at most once
  5799. and in prescribed order. */
  5800. if (last_section_index != (uint8)-1
  5801. && (section_index <= last_section_index)) {
  5802. set_error_buf(error_buf, error_buf_size,
  5803. "unexpected content after last section or "
  5804. "junk after last section");
  5805. return false;
  5806. }
  5807. last_section_index = section_index;
  5808. }
  5809. read_leb_uint32(p, p_end, section_size);
  5810. CHECK_BUF1(p, p_end, section_size);
  5811. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5812. error_buf_size))) {
  5813. return false;
  5814. }
  5815. section->section_type = section_type;
  5816. section->section_body = (uint8 *)p;
  5817. section->section_body_size = section_size;
  5818. if (!section_list_end)
  5819. *p_section_list = section_list_end = section;
  5820. else {
  5821. section_list_end->next = section;
  5822. section_list_end = section;
  5823. }
  5824. p += section_size;
  5825. }
  5826. else {
  5827. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5828. return false;
  5829. }
  5830. }
  5831. return true;
  5832. fail:
  5833. return false;
  5834. }
  5835. static void
  5836. exchange32(uint8 *p_data)
  5837. {
  5838. uint8 value = *p_data;
  5839. *p_data = *(p_data + 3);
  5840. *(p_data + 3) = value;
  5841. value = *(p_data + 1);
  5842. *(p_data + 1) = *(p_data + 2);
  5843. *(p_data + 2) = value;
  5844. }
  5845. static union {
  5846. int a;
  5847. char b;
  5848. } __ue = { .a = 1 };
  5849. #define is_little_endian() (__ue.b == 1)
  5850. static bool
  5851. load(const uint8 *buf, uint32 size, WASMModule *module,
  5852. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  5853. uint32 error_buf_size)
  5854. {
  5855. const uint8 *buf_end = buf + size;
  5856. const uint8 *p = buf, *p_end = buf_end;
  5857. uint32 magic_number, version;
  5858. WASMSection *section_list = NULL;
  5859. CHECK_BUF1(p, p_end, sizeof(uint32));
  5860. magic_number = read_uint32(p);
  5861. if (!is_little_endian())
  5862. exchange32((uint8 *)&magic_number);
  5863. if (magic_number != WASM_MAGIC_NUMBER) {
  5864. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5865. return false;
  5866. }
  5867. CHECK_BUF1(p, p_end, sizeof(uint32));
  5868. version = read_uint32(p);
  5869. if (!is_little_endian())
  5870. exchange32((uint8 *)&version);
  5871. if (version != WASM_CURRENT_VERSION) {
  5872. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5873. return false;
  5874. }
  5875. module->package_version = version;
  5876. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5877. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5878. no_resolve, error_buf, error_buf_size)) {
  5879. destroy_sections(section_list);
  5880. return false;
  5881. }
  5882. destroy_sections(section_list);
  5883. return true;
  5884. fail:
  5885. return false;
  5886. }
  5887. #if WASM_ENABLE_LIBC_WASI != 0
  5888. /**
  5889. * refer to
  5890. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5891. */
  5892. static bool
  5893. check_wasi_abi_compatibility(const WASMModule *module,
  5894. #if WASM_ENABLE_MULTI_MODULE != 0
  5895. bool main_module,
  5896. #endif
  5897. char *error_buf, uint32 error_buf_size)
  5898. {
  5899. /**
  5900. * be careful with:
  5901. * wasi compatible modules(command/reactor) which don't import any wasi
  5902. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5903. * reactor can depend on nothing. At the same time, each has its own entry
  5904. * point.
  5905. *
  5906. * observations:
  5907. * - clang always injects `_start` into a command
  5908. * - clang always injects `_initialize` into a reactor
  5909. * - `iwasm -f` allows to run a function in the reactor
  5910. *
  5911. * strong assumptions:
  5912. * - no one will define either `_start` or `_initialize` on purpose
  5913. * - `_start` should always be `void _start(void)`
  5914. * - `_initialize` should always be `void _initialize(void)`
  5915. *
  5916. */
  5917. /* clang-format off */
  5918. /**
  5919. *
  5920. * | | import_wasi_api True | | import_wasi_api False | |
  5921. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5922. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5923. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5924. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5925. */
  5926. /* clang-format on */
  5927. WASMExport *initialize = NULL, *start = NULL;
  5928. uint32 import_function_count = module->import_function_count;
  5929. WASMFuncType *func_type;
  5930. /* (func (export "_start") (...) */
  5931. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5932. error_buf, error_buf_size);
  5933. if (start) {
  5934. if (start->index < import_function_count) {
  5935. set_error_buf(
  5936. error_buf, error_buf_size,
  5937. "the builtin _start function can not be an import function");
  5938. return false;
  5939. }
  5940. func_type =
  5941. module->functions[start->index - import_function_count]->func_type;
  5942. if (func_type->param_count || func_type->result_count) {
  5943. set_error_buf(error_buf, error_buf_size,
  5944. "the signature of builtin _start function is wrong");
  5945. return false;
  5946. }
  5947. }
  5948. else {
  5949. /* (func (export "_initialize") (...) */
  5950. initialize =
  5951. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5952. error_buf, error_buf_size);
  5953. if (initialize) {
  5954. if (initialize->index < import_function_count) {
  5955. set_error_buf(error_buf, error_buf_size,
  5956. "the builtin _initialize function can not be an "
  5957. "import function");
  5958. return false;
  5959. }
  5960. func_type =
  5961. module->functions[initialize->index - import_function_count]
  5962. ->func_type;
  5963. if (func_type->param_count || func_type->result_count) {
  5964. set_error_buf(
  5965. error_buf, error_buf_size,
  5966. "the signature of builtin _initialize function is wrong");
  5967. return false;
  5968. }
  5969. }
  5970. }
  5971. /* filter out non-wasi compatible modules */
  5972. if (!module->import_wasi_api && !start && !initialize) {
  5973. return true;
  5974. }
  5975. /* should have one at least */
  5976. if (module->import_wasi_api && !start && !initialize) {
  5977. LOG_WARNING("warning: a module with WASI apis should be either "
  5978. "a command or a reactor");
  5979. }
  5980. /*
  5981. * there is at least one of `_start` and `_initialize` in below cases.
  5982. * according to the assumption, they should be all wasi compatible
  5983. */
  5984. #if WASM_ENABLE_MULTI_MODULE != 0
  5985. /* filter out commands (with `_start`) cases */
  5986. if (start && !main_module) {
  5987. set_error_buf(
  5988. error_buf, error_buf_size,
  5989. "a command (with _start function) can not be a sub-module");
  5990. return false;
  5991. }
  5992. #endif
  5993. return true;
  5994. }
  5995. #endif
  5996. WASMModule *
  5997. wasm_loader_load(uint8 *buf, uint32 size,
  5998. #if WASM_ENABLE_MULTI_MODULE != 0
  5999. bool main_module,
  6000. #endif
  6001. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6002. {
  6003. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6004. if (!module) {
  6005. return NULL;
  6006. }
  6007. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6008. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6009. module->load_addr = (uint8 *)buf;
  6010. module->load_size = size;
  6011. #endif
  6012. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6013. error_buf, error_buf_size)) {
  6014. goto fail;
  6015. }
  6016. #if WASM_ENABLE_LIBC_WASI != 0
  6017. /* Check the WASI application ABI */
  6018. if (!check_wasi_abi_compatibility(module,
  6019. #if WASM_ENABLE_MULTI_MODULE != 0
  6020. main_module,
  6021. #endif
  6022. error_buf, error_buf_size)) {
  6023. goto fail;
  6024. }
  6025. #endif
  6026. LOG_VERBOSE("Load module success.\n");
  6027. return module;
  6028. fail:
  6029. wasm_loader_unload(module);
  6030. return NULL;
  6031. }
  6032. void
  6033. wasm_loader_unload(WASMModule *module)
  6034. {
  6035. uint32 i;
  6036. if (!module)
  6037. return;
  6038. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6039. && WASM_ENABLE_LAZY_JIT != 0
  6040. module->orcjit_stop_compiling = true;
  6041. if (module->llvm_jit_init_thread)
  6042. os_thread_join(module->llvm_jit_init_thread, NULL);
  6043. #endif
  6044. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6045. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6046. module internal data after they were freed */
  6047. orcjit_stop_compile_threads(module);
  6048. #endif
  6049. #if WASM_ENABLE_JIT != 0
  6050. if (module->func_ptrs)
  6051. wasm_runtime_free(module->func_ptrs);
  6052. if (module->comp_ctx)
  6053. aot_destroy_comp_context(module->comp_ctx);
  6054. if (module->comp_data)
  6055. aot_destroy_comp_data(module->comp_data);
  6056. #endif
  6057. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6058. && WASM_ENABLE_LAZY_JIT != 0
  6059. if (module->tierup_wait_lock_inited) {
  6060. os_mutex_destroy(&module->tierup_wait_lock);
  6061. os_cond_destroy(&module->tierup_wait_cond);
  6062. }
  6063. #endif
  6064. if (module->imports)
  6065. wasm_runtime_free(module->imports);
  6066. if (module->functions) {
  6067. for (i = 0; i < module->function_count; i++) {
  6068. if (module->functions[i]) {
  6069. if (module->functions[i]->local_offsets)
  6070. wasm_runtime_free(module->functions[i]->local_offsets);
  6071. #if WASM_ENABLE_FAST_INTERP != 0
  6072. if (module->functions[i]->code_compiled)
  6073. wasm_runtime_free(module->functions[i]->code_compiled);
  6074. if (module->functions[i]->consts)
  6075. wasm_runtime_free(module->functions[i]->consts);
  6076. #endif
  6077. #if WASM_ENABLE_FAST_JIT != 0
  6078. if (module->functions[i]->fast_jit_jitted_code) {
  6079. jit_code_cache_free(
  6080. module->functions[i]->fast_jit_jitted_code);
  6081. }
  6082. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6083. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6084. jit_code_cache_free(
  6085. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6086. }
  6087. #endif
  6088. #endif
  6089. #if WASM_ENABLE_GC != 0
  6090. if (module->functions[i]->local_ref_type_maps) {
  6091. wasm_runtime_free(
  6092. module->functions[i]->local_ref_type_maps);
  6093. }
  6094. #endif
  6095. wasm_runtime_free(module->functions[i]);
  6096. }
  6097. }
  6098. wasm_runtime_free(module->functions);
  6099. }
  6100. if (module->tables) {
  6101. #if WASM_ENABLE_GC != 0
  6102. for (i = 0; i < module->table_count; i++) {
  6103. destroy_init_expr(module, &module->tables[i].init_expr);
  6104. }
  6105. #endif
  6106. wasm_runtime_free(module->tables);
  6107. }
  6108. if (module->memories)
  6109. wasm_runtime_free(module->memories);
  6110. if (module->globals) {
  6111. #if WASM_ENABLE_GC != 0
  6112. for (i = 0; i < module->global_count; i++) {
  6113. destroy_init_expr(module, &module->globals[i].init_expr);
  6114. }
  6115. #endif
  6116. wasm_runtime_free(module->globals);
  6117. }
  6118. #if WASM_ENABLE_TAGS != 0
  6119. if (module->tags) {
  6120. for (i = 0; i < module->tag_count; i++) {
  6121. if (module->tags[i])
  6122. wasm_runtime_free(module->tags[i]);
  6123. }
  6124. wasm_runtime_free(module->tags);
  6125. }
  6126. #endif
  6127. if (module->exports)
  6128. wasm_runtime_free(module->exports);
  6129. if (module->table_segments) {
  6130. for (i = 0; i < module->table_seg_count; i++) {
  6131. if (module->table_segments[i].init_values) {
  6132. #if WASM_ENABLE_GC != 0
  6133. uint32 j;
  6134. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6135. destroy_init_expr(
  6136. module, &module->table_segments[i].init_values[j]);
  6137. }
  6138. #endif
  6139. wasm_runtime_free(module->table_segments[i].init_values);
  6140. }
  6141. }
  6142. wasm_runtime_free(module->table_segments);
  6143. }
  6144. if (module->data_segments) {
  6145. for (i = 0; i < module->data_seg_count; i++) {
  6146. if (module->data_segments[i]) {
  6147. if (module->data_segments[i]->is_data_cloned)
  6148. wasm_runtime_free(module->data_segments[i]->data);
  6149. wasm_runtime_free(module->data_segments[i]);
  6150. }
  6151. }
  6152. wasm_runtime_free(module->data_segments);
  6153. }
  6154. if (module->types) {
  6155. for (i = 0; i < module->type_count; i++) {
  6156. if (module->types[i])
  6157. destroy_wasm_type(module->types[i]);
  6158. }
  6159. wasm_runtime_free(module->types);
  6160. }
  6161. if (module->const_str_list) {
  6162. StringNode *node = module->const_str_list, *node_next;
  6163. while (node) {
  6164. node_next = node->next;
  6165. wasm_runtime_free(node);
  6166. node = node_next;
  6167. }
  6168. }
  6169. #if WASM_ENABLE_STRINGREF != 0
  6170. if (module->string_literal_ptrs) {
  6171. wasm_runtime_free((void *)module->string_literal_ptrs);
  6172. }
  6173. if (module->string_literal_lengths) {
  6174. wasm_runtime_free(module->string_literal_lengths);
  6175. }
  6176. #endif
  6177. #if WASM_ENABLE_FAST_INTERP == 0
  6178. if (module->br_table_cache_list) {
  6179. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6180. BrTableCache *node_next;
  6181. while (node) {
  6182. node_next = bh_list_elem_next(node);
  6183. wasm_runtime_free(node);
  6184. node = node_next;
  6185. }
  6186. }
  6187. #endif
  6188. #if WASM_ENABLE_MULTI_MODULE != 0
  6189. /* just release the sub module list */
  6190. if (module->import_module_list) {
  6191. WASMRegisteredModule *node =
  6192. bh_list_first_elem(module->import_module_list);
  6193. while (node) {
  6194. WASMRegisteredModule *next = bh_list_elem_next(node);
  6195. bh_list_remove(module->import_module_list, node);
  6196. /*
  6197. * unload(sub_module) will be triggered during runtime_destroy().
  6198. * every module in the global module list will be unloaded one by
  6199. * one. so don't worry.
  6200. */
  6201. wasm_runtime_free(node);
  6202. /*
  6203. * the module file reading buffer will be released
  6204. * in runtime_destroy()
  6205. */
  6206. node = next;
  6207. }
  6208. }
  6209. #endif
  6210. #if WASM_ENABLE_DEBUG_INTERP != 0
  6211. WASMFastOPCodeNode *fast_opcode =
  6212. bh_list_first_elem(&module->fast_opcode_list);
  6213. while (fast_opcode) {
  6214. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6215. wasm_runtime_free(fast_opcode);
  6216. fast_opcode = next;
  6217. }
  6218. #endif
  6219. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6220. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6221. && WASM_ENABLE_LAZY_JIT != 0)
  6222. os_mutex_destroy(&module->instance_list_lock);
  6223. #endif
  6224. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6225. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6226. #endif
  6227. #if WASM_ENABLE_FAST_JIT != 0
  6228. if (module->fast_jit_func_ptrs) {
  6229. wasm_runtime_free(module->fast_jit_func_ptrs);
  6230. }
  6231. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6232. if (module->fast_jit_thread_locks_inited[i]) {
  6233. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6234. }
  6235. }
  6236. #endif
  6237. #if WASM_ENABLE_GC != 0
  6238. os_mutex_destroy(&module->rtt_type_lock);
  6239. bh_hash_map_destroy(module->ref_type_set);
  6240. if (module->rtt_types) {
  6241. for (i = 0; i < module->type_count; i++) {
  6242. if (module->rtt_types[i])
  6243. wasm_runtime_free(module->rtt_types[i]);
  6244. }
  6245. wasm_runtime_free(module->rtt_types);
  6246. }
  6247. #if WASM_ENABLE_STRINGREF != 0
  6248. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6249. if (module->stringref_rtts[i])
  6250. wasm_runtime_free(module->stringref_rtts[i]);
  6251. }
  6252. #endif
  6253. #endif
  6254. wasm_runtime_free(module);
  6255. }
  6256. bool
  6257. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6258. const uint8 *start_addr, const uint8 *code_end_addr,
  6259. uint8 label_type, uint8 **p_else_addr,
  6260. uint8 **p_end_addr)
  6261. {
  6262. const uint8 *p = start_addr, *p_end = code_end_addr;
  6263. uint8 *else_addr = NULL;
  6264. char error_buf[128];
  6265. uint32 block_nested_depth = 1, count, i, j, t;
  6266. uint32 error_buf_size = sizeof(error_buf);
  6267. uint8 opcode, u8;
  6268. BlockAddr block_stack[16] = { { 0 } }, *block;
  6269. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6270. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6271. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6272. if (block[j].start_addr == start_addr) {
  6273. /* Cache hit */
  6274. *p_else_addr = block[j].else_addr;
  6275. *p_end_addr = block[j].end_addr;
  6276. return true;
  6277. }
  6278. }
  6279. /* Cache unhit */
  6280. block_stack[0].start_addr = start_addr;
  6281. while (p < code_end_addr) {
  6282. opcode = *p++;
  6283. #if WASM_ENABLE_DEBUG_INTERP != 0
  6284. op_break_retry:
  6285. #endif
  6286. switch (opcode) {
  6287. case WASM_OP_UNREACHABLE:
  6288. case WASM_OP_NOP:
  6289. break;
  6290. #if WASM_ENABLE_EXCE_HANDLING != 0
  6291. case WASM_OP_TRY:
  6292. u8 = read_uint8(p);
  6293. if (block_nested_depth
  6294. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6295. block_stack[block_nested_depth].start_addr = p;
  6296. block_stack[block_nested_depth].else_addr = NULL;
  6297. }
  6298. block_nested_depth++;
  6299. break;
  6300. case EXT_OP_TRY:
  6301. skip_leb_uint32(p, p_end);
  6302. if (block_nested_depth
  6303. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6304. block_stack[block_nested_depth].start_addr = p;
  6305. block_stack[block_nested_depth].else_addr = NULL;
  6306. }
  6307. block_nested_depth++;
  6308. break;
  6309. case WASM_OP_CATCH:
  6310. if (block_nested_depth == 1) {
  6311. *p_end_addr = (uint8 *)(p - 1);
  6312. /* stop search and return the address of the catch block */
  6313. return true;
  6314. }
  6315. break;
  6316. case WASM_OP_CATCH_ALL:
  6317. if (block_nested_depth == 1) {
  6318. *p_end_addr = (uint8 *)(p - 1);
  6319. /* stop search and return the address of the catch_all block
  6320. */
  6321. return true;
  6322. }
  6323. break;
  6324. case WASM_OP_THROW:
  6325. /* skip tag_index */
  6326. skip_leb(p);
  6327. break;
  6328. case WASM_OP_RETHROW:
  6329. /* skip depth */
  6330. skip_leb(p);
  6331. break;
  6332. case WASM_OP_DELEGATE:
  6333. if (block_nested_depth == 1) {
  6334. *p_end_addr = (uint8 *)(p - 1);
  6335. return true;
  6336. }
  6337. else {
  6338. skip_leb(p);
  6339. /* the DELEGATE opcode ends the tryblock, */
  6340. block_nested_depth--;
  6341. if (block_nested_depth
  6342. < sizeof(block_stack) / sizeof(BlockAddr))
  6343. block_stack[block_nested_depth].end_addr =
  6344. (uint8 *)(p - 1);
  6345. }
  6346. break;
  6347. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6348. case WASM_OP_BLOCK:
  6349. case WASM_OP_LOOP:
  6350. case WASM_OP_IF:
  6351. {
  6352. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6353. u8 = read_uint8(p);
  6354. if (is_byte_a_type(u8)) {
  6355. #if WASM_ENABLE_GC != 0
  6356. if (wasm_is_type_multi_byte_type(u8)) {
  6357. /* the possible extra bytes of GC ref type have been
  6358. modified to OP_NOP, no need to resolve them again */
  6359. }
  6360. #endif
  6361. }
  6362. else {
  6363. p--;
  6364. /* block type */
  6365. skip_leb_int32(p, p_end);
  6366. }
  6367. if (block_nested_depth
  6368. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6369. block_stack[block_nested_depth].start_addr = p;
  6370. block_stack[block_nested_depth].else_addr = NULL;
  6371. }
  6372. block_nested_depth++;
  6373. break;
  6374. }
  6375. case EXT_OP_BLOCK:
  6376. case EXT_OP_LOOP:
  6377. case EXT_OP_IF:
  6378. /* block type */
  6379. skip_leb_int32(p, p_end);
  6380. if (block_nested_depth
  6381. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6382. block_stack[block_nested_depth].start_addr = p;
  6383. block_stack[block_nested_depth].else_addr = NULL;
  6384. }
  6385. block_nested_depth++;
  6386. break;
  6387. case WASM_OP_ELSE:
  6388. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6389. else_addr = (uint8 *)(p - 1);
  6390. if (block_nested_depth - 1
  6391. < sizeof(block_stack) / sizeof(BlockAddr))
  6392. block_stack[block_nested_depth - 1].else_addr =
  6393. (uint8 *)(p - 1);
  6394. break;
  6395. case WASM_OP_END:
  6396. if (block_nested_depth == 1) {
  6397. if (label_type == LABEL_TYPE_IF)
  6398. *p_else_addr = else_addr;
  6399. *p_end_addr = (uint8 *)(p - 1);
  6400. block_stack[0].end_addr = (uint8 *)(p - 1);
  6401. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6402. t++) {
  6403. start_addr = block_stack[t].start_addr;
  6404. if (start_addr) {
  6405. i = ((uintptr_t)start_addr)
  6406. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6407. block =
  6408. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6409. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6410. if (!block[j].start_addr)
  6411. break;
  6412. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6413. memmove(block + 1, block,
  6414. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6415. * sizeof(BlockAddr));
  6416. j = 0;
  6417. }
  6418. block[j].start_addr = block_stack[t].start_addr;
  6419. block[j].else_addr = block_stack[t].else_addr;
  6420. block[j].end_addr = block_stack[t].end_addr;
  6421. }
  6422. else
  6423. break;
  6424. }
  6425. return true;
  6426. }
  6427. else {
  6428. block_nested_depth--;
  6429. if (block_nested_depth
  6430. < sizeof(block_stack) / sizeof(BlockAddr))
  6431. block_stack[block_nested_depth].end_addr =
  6432. (uint8 *)(p - 1);
  6433. }
  6434. break;
  6435. case WASM_OP_BR:
  6436. case WASM_OP_BR_IF:
  6437. skip_leb_uint32(p, p_end); /* labelidx */
  6438. break;
  6439. case WASM_OP_BR_TABLE:
  6440. read_leb_uint32(p, p_end, count); /* label num */
  6441. #if WASM_ENABLE_FAST_INTERP != 0
  6442. for (i = 0; i <= count; i++) /* labelidxs */
  6443. skip_leb_uint32(p, p_end);
  6444. #else
  6445. p += count + 1;
  6446. while (*p == WASM_OP_NOP)
  6447. p++;
  6448. #endif
  6449. break;
  6450. #if WASM_ENABLE_FAST_INTERP == 0
  6451. case EXT_OP_BR_TABLE_CACHE:
  6452. read_leb_uint32(p, p_end, count); /* label num */
  6453. while (*p == WASM_OP_NOP)
  6454. p++;
  6455. break;
  6456. #endif
  6457. case WASM_OP_RETURN:
  6458. break;
  6459. case WASM_OP_CALL:
  6460. #if WASM_ENABLE_TAIL_CALL != 0
  6461. case WASM_OP_RETURN_CALL:
  6462. #endif
  6463. skip_leb_uint32(p, p_end); /* funcidx */
  6464. break;
  6465. case WASM_OP_CALL_INDIRECT:
  6466. #if WASM_ENABLE_TAIL_CALL != 0
  6467. case WASM_OP_RETURN_CALL_INDIRECT:
  6468. #endif
  6469. skip_leb_uint32(p, p_end); /* typeidx */
  6470. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6471. skip_leb_uint32(p, p_end); /* tableidx */
  6472. #else
  6473. u8 = read_uint8(p); /* 0x00 */
  6474. #endif
  6475. break;
  6476. #if WASM_ENABLE_GC != 0
  6477. case WASM_OP_CALL_REF:
  6478. case WASM_OP_RETURN_CALL_REF:
  6479. skip_leb_uint32(p, p_end); /* typeidx */
  6480. break;
  6481. #endif
  6482. case WASM_OP_DROP:
  6483. case WASM_OP_SELECT:
  6484. case WASM_OP_DROP_64:
  6485. case WASM_OP_SELECT_64:
  6486. break;
  6487. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6488. case WASM_OP_SELECT_T:
  6489. {
  6490. skip_leb_uint32(p, p_end); /* vec length */
  6491. u8 = read_uint8(p); /* typeidx */
  6492. /* the possible extra bytes of GC ref type have been
  6493. modified to OP_NOP, no need to resolve them again */
  6494. break;
  6495. }
  6496. case WASM_OP_TABLE_GET:
  6497. case WASM_OP_TABLE_SET:
  6498. skip_leb_uint32(p, p_end); /* table index */
  6499. break;
  6500. case WASM_OP_REF_NULL:
  6501. {
  6502. u8 = read_uint8(p); /* type */
  6503. if (is_byte_a_type(u8)) {
  6504. #if WASM_ENABLE_GC != 0
  6505. if (wasm_is_type_multi_byte_type(u8)) {
  6506. /* the possible extra bytes of GC ref type have been
  6507. modified to OP_NOP, no need to resolve them again */
  6508. }
  6509. #endif
  6510. }
  6511. else {
  6512. p--;
  6513. skip_leb_uint32(p, p_end);
  6514. }
  6515. break;
  6516. }
  6517. case WASM_OP_REF_IS_NULL:
  6518. break;
  6519. case WASM_OP_REF_FUNC:
  6520. skip_leb_uint32(p, p_end); /* func index */
  6521. break;
  6522. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6523. #if WASM_ENABLE_GC != 0
  6524. case WASM_OP_REF_AS_NON_NULL:
  6525. case WASM_OP_REF_EQ:
  6526. break;
  6527. case WASM_OP_BR_ON_NULL:
  6528. case WASM_OP_BR_ON_NON_NULL:
  6529. skip_leb_uint32(p, p_end); /* label index */
  6530. break;
  6531. #endif /* end of WASM_ENABLE_GC != 0 */
  6532. case WASM_OP_GET_LOCAL:
  6533. case WASM_OP_SET_LOCAL:
  6534. case WASM_OP_TEE_LOCAL:
  6535. case WASM_OP_GET_GLOBAL:
  6536. case WASM_OP_SET_GLOBAL:
  6537. case WASM_OP_GET_GLOBAL_64:
  6538. case WASM_OP_SET_GLOBAL_64:
  6539. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6540. skip_leb_uint32(p, p_end); /* local index */
  6541. break;
  6542. case EXT_OP_GET_LOCAL_FAST:
  6543. case EXT_OP_SET_LOCAL_FAST:
  6544. case EXT_OP_TEE_LOCAL_FAST:
  6545. CHECK_BUF(p, p_end, 1);
  6546. p++;
  6547. break;
  6548. case WASM_OP_I32_LOAD:
  6549. case WASM_OP_I64_LOAD:
  6550. case WASM_OP_F32_LOAD:
  6551. case WASM_OP_F64_LOAD:
  6552. case WASM_OP_I32_LOAD8_S:
  6553. case WASM_OP_I32_LOAD8_U:
  6554. case WASM_OP_I32_LOAD16_S:
  6555. case WASM_OP_I32_LOAD16_U:
  6556. case WASM_OP_I64_LOAD8_S:
  6557. case WASM_OP_I64_LOAD8_U:
  6558. case WASM_OP_I64_LOAD16_S:
  6559. case WASM_OP_I64_LOAD16_U:
  6560. case WASM_OP_I64_LOAD32_S:
  6561. case WASM_OP_I64_LOAD32_U:
  6562. case WASM_OP_I32_STORE:
  6563. case WASM_OP_I64_STORE:
  6564. case WASM_OP_F32_STORE:
  6565. case WASM_OP_F64_STORE:
  6566. case WASM_OP_I32_STORE8:
  6567. case WASM_OP_I32_STORE16:
  6568. case WASM_OP_I64_STORE8:
  6569. case WASM_OP_I64_STORE16:
  6570. case WASM_OP_I64_STORE32:
  6571. skip_leb_align(p, p_end); /* align */
  6572. skip_leb_mem_offset(p, p_end); /* offset */
  6573. break;
  6574. case WASM_OP_MEMORY_SIZE:
  6575. case WASM_OP_MEMORY_GROW:
  6576. skip_leb_memidx(p, p_end); /* memidx */
  6577. break;
  6578. case WASM_OP_I32_CONST:
  6579. skip_leb_int32(p, p_end);
  6580. break;
  6581. case WASM_OP_I64_CONST:
  6582. skip_leb_int64(p, p_end);
  6583. break;
  6584. case WASM_OP_F32_CONST:
  6585. p += sizeof(float32);
  6586. break;
  6587. case WASM_OP_F64_CONST:
  6588. p += sizeof(float64);
  6589. break;
  6590. case WASM_OP_I32_EQZ:
  6591. case WASM_OP_I32_EQ:
  6592. case WASM_OP_I32_NE:
  6593. case WASM_OP_I32_LT_S:
  6594. case WASM_OP_I32_LT_U:
  6595. case WASM_OP_I32_GT_S:
  6596. case WASM_OP_I32_GT_U:
  6597. case WASM_OP_I32_LE_S:
  6598. case WASM_OP_I32_LE_U:
  6599. case WASM_OP_I32_GE_S:
  6600. case WASM_OP_I32_GE_U:
  6601. case WASM_OP_I64_EQZ:
  6602. case WASM_OP_I64_EQ:
  6603. case WASM_OP_I64_NE:
  6604. case WASM_OP_I64_LT_S:
  6605. case WASM_OP_I64_LT_U:
  6606. case WASM_OP_I64_GT_S:
  6607. case WASM_OP_I64_GT_U:
  6608. case WASM_OP_I64_LE_S:
  6609. case WASM_OP_I64_LE_U:
  6610. case WASM_OP_I64_GE_S:
  6611. case WASM_OP_I64_GE_U:
  6612. case WASM_OP_F32_EQ:
  6613. case WASM_OP_F32_NE:
  6614. case WASM_OP_F32_LT:
  6615. case WASM_OP_F32_GT:
  6616. case WASM_OP_F32_LE:
  6617. case WASM_OP_F32_GE:
  6618. case WASM_OP_F64_EQ:
  6619. case WASM_OP_F64_NE:
  6620. case WASM_OP_F64_LT:
  6621. case WASM_OP_F64_GT:
  6622. case WASM_OP_F64_LE:
  6623. case WASM_OP_F64_GE:
  6624. case WASM_OP_I32_CLZ:
  6625. case WASM_OP_I32_CTZ:
  6626. case WASM_OP_I32_POPCNT:
  6627. case WASM_OP_I32_ADD:
  6628. case WASM_OP_I32_SUB:
  6629. case WASM_OP_I32_MUL:
  6630. case WASM_OP_I32_DIV_S:
  6631. case WASM_OP_I32_DIV_U:
  6632. case WASM_OP_I32_REM_S:
  6633. case WASM_OP_I32_REM_U:
  6634. case WASM_OP_I32_AND:
  6635. case WASM_OP_I32_OR:
  6636. case WASM_OP_I32_XOR:
  6637. case WASM_OP_I32_SHL:
  6638. case WASM_OP_I32_SHR_S:
  6639. case WASM_OP_I32_SHR_U:
  6640. case WASM_OP_I32_ROTL:
  6641. case WASM_OP_I32_ROTR:
  6642. case WASM_OP_I64_CLZ:
  6643. case WASM_OP_I64_CTZ:
  6644. case WASM_OP_I64_POPCNT:
  6645. case WASM_OP_I64_ADD:
  6646. case WASM_OP_I64_SUB:
  6647. case WASM_OP_I64_MUL:
  6648. case WASM_OP_I64_DIV_S:
  6649. case WASM_OP_I64_DIV_U:
  6650. case WASM_OP_I64_REM_S:
  6651. case WASM_OP_I64_REM_U:
  6652. case WASM_OP_I64_AND:
  6653. case WASM_OP_I64_OR:
  6654. case WASM_OP_I64_XOR:
  6655. case WASM_OP_I64_SHL:
  6656. case WASM_OP_I64_SHR_S:
  6657. case WASM_OP_I64_SHR_U:
  6658. case WASM_OP_I64_ROTL:
  6659. case WASM_OP_I64_ROTR:
  6660. case WASM_OP_F32_ABS:
  6661. case WASM_OP_F32_NEG:
  6662. case WASM_OP_F32_CEIL:
  6663. case WASM_OP_F32_FLOOR:
  6664. case WASM_OP_F32_TRUNC:
  6665. case WASM_OP_F32_NEAREST:
  6666. case WASM_OP_F32_SQRT:
  6667. case WASM_OP_F32_ADD:
  6668. case WASM_OP_F32_SUB:
  6669. case WASM_OP_F32_MUL:
  6670. case WASM_OP_F32_DIV:
  6671. case WASM_OP_F32_MIN:
  6672. case WASM_OP_F32_MAX:
  6673. case WASM_OP_F32_COPYSIGN:
  6674. case WASM_OP_F64_ABS:
  6675. case WASM_OP_F64_NEG:
  6676. case WASM_OP_F64_CEIL:
  6677. case WASM_OP_F64_FLOOR:
  6678. case WASM_OP_F64_TRUNC:
  6679. case WASM_OP_F64_NEAREST:
  6680. case WASM_OP_F64_SQRT:
  6681. case WASM_OP_F64_ADD:
  6682. case WASM_OP_F64_SUB:
  6683. case WASM_OP_F64_MUL:
  6684. case WASM_OP_F64_DIV:
  6685. case WASM_OP_F64_MIN:
  6686. case WASM_OP_F64_MAX:
  6687. case WASM_OP_F64_COPYSIGN:
  6688. case WASM_OP_I32_WRAP_I64:
  6689. case WASM_OP_I32_TRUNC_S_F32:
  6690. case WASM_OP_I32_TRUNC_U_F32:
  6691. case WASM_OP_I32_TRUNC_S_F64:
  6692. case WASM_OP_I32_TRUNC_U_F64:
  6693. case WASM_OP_I64_EXTEND_S_I32:
  6694. case WASM_OP_I64_EXTEND_U_I32:
  6695. case WASM_OP_I64_TRUNC_S_F32:
  6696. case WASM_OP_I64_TRUNC_U_F32:
  6697. case WASM_OP_I64_TRUNC_S_F64:
  6698. case WASM_OP_I64_TRUNC_U_F64:
  6699. case WASM_OP_F32_CONVERT_S_I32:
  6700. case WASM_OP_F32_CONVERT_U_I32:
  6701. case WASM_OP_F32_CONVERT_S_I64:
  6702. case WASM_OP_F32_CONVERT_U_I64:
  6703. case WASM_OP_F32_DEMOTE_F64:
  6704. case WASM_OP_F64_CONVERT_S_I32:
  6705. case WASM_OP_F64_CONVERT_U_I32:
  6706. case WASM_OP_F64_CONVERT_S_I64:
  6707. case WASM_OP_F64_CONVERT_U_I64:
  6708. case WASM_OP_F64_PROMOTE_F32:
  6709. case WASM_OP_I32_REINTERPRET_F32:
  6710. case WASM_OP_I64_REINTERPRET_F64:
  6711. case WASM_OP_F32_REINTERPRET_I32:
  6712. case WASM_OP_F64_REINTERPRET_I64:
  6713. case WASM_OP_I32_EXTEND8_S:
  6714. case WASM_OP_I32_EXTEND16_S:
  6715. case WASM_OP_I64_EXTEND8_S:
  6716. case WASM_OP_I64_EXTEND16_S:
  6717. case WASM_OP_I64_EXTEND32_S:
  6718. break;
  6719. #if WASM_ENABLE_GC != 0
  6720. case WASM_OP_GC_PREFIX:
  6721. {
  6722. uint32 opcode1;
  6723. read_leb_uint32(p, p_end, opcode1);
  6724. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6725. is no larger than UINT8_MAX */
  6726. opcode = (uint8)opcode1;
  6727. switch (opcode) {
  6728. case WASM_OP_STRUCT_NEW:
  6729. case WASM_OP_STRUCT_NEW_DEFAULT:
  6730. skip_leb_uint32(p, p_end); /* typeidx */
  6731. break;
  6732. case WASM_OP_STRUCT_GET:
  6733. case WASM_OP_STRUCT_GET_S:
  6734. case WASM_OP_STRUCT_GET_U:
  6735. case WASM_OP_STRUCT_SET:
  6736. skip_leb_uint32(p, p_end); /* typeidx */
  6737. skip_leb_uint32(p, p_end); /* fieldidx */
  6738. break;
  6739. case WASM_OP_ARRAY_NEW:
  6740. case WASM_OP_ARRAY_NEW_DEFAULT:
  6741. case WASM_OP_ARRAY_GET:
  6742. case WASM_OP_ARRAY_GET_S:
  6743. case WASM_OP_ARRAY_GET_U:
  6744. case WASM_OP_ARRAY_SET:
  6745. case WASM_OP_ARRAY_FILL:
  6746. skip_leb_uint32(p, p_end); /* typeidx */
  6747. break;
  6748. case WASM_OP_ARRAY_COPY:
  6749. skip_leb_uint32(p, p_end); /* typeidx1 */
  6750. skip_leb_uint32(p, p_end); /* typeidx2 */
  6751. break;
  6752. case WASM_OP_ARRAY_LEN:
  6753. break;
  6754. case WASM_OP_ARRAY_NEW_FIXED:
  6755. case WASM_OP_ARRAY_NEW_DATA:
  6756. case WASM_OP_ARRAY_NEW_ELEM:
  6757. skip_leb_uint32(p, p_end); /* typeidx */
  6758. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6759. break;
  6760. case WASM_OP_REF_I31:
  6761. case WASM_OP_I31_GET_S:
  6762. case WASM_OP_I31_GET_U:
  6763. break;
  6764. case WASM_OP_REF_TEST:
  6765. case WASM_OP_REF_CAST:
  6766. case WASM_OP_REF_TEST_NULLABLE:
  6767. case WASM_OP_REF_CAST_NULLABLE:
  6768. skip_leb_int32(p, p_end); /* heaptype */
  6769. break;
  6770. case WASM_OP_BR_ON_CAST:
  6771. case WASM_OP_BR_ON_CAST_FAIL:
  6772. p += sizeof(uint8); /* castflag */
  6773. skip_leb_uint32(p, p_end); /* labelidx */
  6774. skip_leb_int32(p, p_end); /* heaptype */
  6775. skip_leb_int32(p, p_end); /* heaptype2 */
  6776. break;
  6777. case WASM_OP_ANY_CONVERT_EXTERN:
  6778. case WASM_OP_EXTERN_CONVERT_ANY:
  6779. break;
  6780. #if WASM_ENABLE_STRINGREF != 0
  6781. case WASM_OP_STRING_NEW_UTF8:
  6782. case WASM_OP_STRING_NEW_WTF16:
  6783. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6784. case WASM_OP_STRING_NEW_WTF8:
  6785. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6786. break;
  6787. case WASM_OP_STRING_CONST:
  6788. skip_leb_int32(p, p_end); /* contents */
  6789. break;
  6790. case WASM_OP_STRING_MEASURE_UTF8:
  6791. case WASM_OP_STRING_MEASURE_WTF8:
  6792. case WASM_OP_STRING_MEASURE_WTF16:
  6793. break;
  6794. case WASM_OP_STRING_ENCODE_UTF8:
  6795. case WASM_OP_STRING_ENCODE_WTF16:
  6796. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6797. case WASM_OP_STRING_ENCODE_WTF8:
  6798. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6799. break;
  6800. case WASM_OP_STRING_CONCAT:
  6801. case WASM_OP_STRING_EQ:
  6802. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6803. case WASM_OP_STRING_AS_WTF8:
  6804. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6805. break;
  6806. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6807. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6808. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6809. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6810. break;
  6811. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6812. case WASM_OP_STRING_AS_WTF16:
  6813. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6814. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6815. break;
  6816. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6817. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6818. break;
  6819. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6820. case WASM_OP_STRING_AS_ITER:
  6821. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6822. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6823. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6824. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6825. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6826. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6827. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6828. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6829. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6830. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6831. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6832. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6833. break;
  6834. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6835. default:
  6836. return false;
  6837. }
  6838. break;
  6839. }
  6840. #endif /* end of WASM_ENABLE_GC != 0 */
  6841. case WASM_OP_MISC_PREFIX:
  6842. {
  6843. uint32 opcode1;
  6844. read_leb_uint32(p, p_end, opcode1);
  6845. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6846. is no larger than UINT8_MAX */
  6847. opcode = (uint8)opcode1;
  6848. switch (opcode) {
  6849. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6850. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6851. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6852. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6853. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6854. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6855. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6856. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6857. break;
  6858. #if WASM_ENABLE_BULK_MEMORY != 0
  6859. case WASM_OP_MEMORY_INIT:
  6860. skip_leb_uint32(p, p_end);
  6861. skip_leb_memidx(p, p_end);
  6862. break;
  6863. case WASM_OP_DATA_DROP:
  6864. skip_leb_uint32(p, p_end);
  6865. break;
  6866. case WASM_OP_MEMORY_COPY:
  6867. skip_leb_memidx(p, p_end);
  6868. skip_leb_memidx(p, p_end);
  6869. break;
  6870. case WASM_OP_MEMORY_FILL:
  6871. skip_leb_memidx(p, p_end);
  6872. break;
  6873. #endif /* WASM_ENABLE_BULK_MEMORY */
  6874. #if WASM_ENABLE_REF_TYPES != 0
  6875. case WASM_OP_TABLE_INIT:
  6876. case WASM_OP_TABLE_COPY:
  6877. /* tableidx */
  6878. skip_leb_uint32(p, p_end);
  6879. /* elemidx */
  6880. skip_leb_uint32(p, p_end);
  6881. break;
  6882. case WASM_OP_ELEM_DROP:
  6883. /* elemidx */
  6884. skip_leb_uint32(p, p_end);
  6885. break;
  6886. case WASM_OP_TABLE_SIZE:
  6887. case WASM_OP_TABLE_GROW:
  6888. case WASM_OP_TABLE_FILL:
  6889. skip_leb_uint32(p, p_end); /* table idx */
  6890. break;
  6891. #endif /* WASM_ENABLE_REF_TYPES */
  6892. default:
  6893. return false;
  6894. }
  6895. break;
  6896. }
  6897. #if WASM_ENABLE_SIMD != 0
  6898. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6899. case WASM_OP_SIMD_PREFIX:
  6900. {
  6901. uint32 opcode1;
  6902. read_leb_uint32(p, p_end, opcode1);
  6903. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6904. is no larger than UINT8_MAX */
  6905. opcode = (uint8)opcode1;
  6906. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6907. */
  6908. switch (opcode) {
  6909. case SIMD_v128_load:
  6910. case SIMD_v128_load8x8_s:
  6911. case SIMD_v128_load8x8_u:
  6912. case SIMD_v128_load16x4_s:
  6913. case SIMD_v128_load16x4_u:
  6914. case SIMD_v128_load32x2_s:
  6915. case SIMD_v128_load32x2_u:
  6916. case SIMD_v128_load8_splat:
  6917. case SIMD_v128_load16_splat:
  6918. case SIMD_v128_load32_splat:
  6919. case SIMD_v128_load64_splat:
  6920. case SIMD_v128_store:
  6921. /* memarg align */
  6922. skip_leb_uint32(p, p_end);
  6923. /* memarg offset */
  6924. skip_leb_mem_offset(p, p_end);
  6925. break;
  6926. case SIMD_v128_const:
  6927. case SIMD_v8x16_shuffle:
  6928. /* immByte[16] immLaneId[16] */
  6929. CHECK_BUF1(p, p_end, 16);
  6930. p += 16;
  6931. break;
  6932. case SIMD_i8x16_extract_lane_s:
  6933. case SIMD_i8x16_extract_lane_u:
  6934. case SIMD_i8x16_replace_lane:
  6935. case SIMD_i16x8_extract_lane_s:
  6936. case SIMD_i16x8_extract_lane_u:
  6937. case SIMD_i16x8_replace_lane:
  6938. case SIMD_i32x4_extract_lane:
  6939. case SIMD_i32x4_replace_lane:
  6940. case SIMD_i64x2_extract_lane:
  6941. case SIMD_i64x2_replace_lane:
  6942. case SIMD_f32x4_extract_lane:
  6943. case SIMD_f32x4_replace_lane:
  6944. case SIMD_f64x2_extract_lane:
  6945. case SIMD_f64x2_replace_lane:
  6946. /* ImmLaneId */
  6947. CHECK_BUF(p, p_end, 1);
  6948. p++;
  6949. break;
  6950. case SIMD_v128_load8_lane:
  6951. case SIMD_v128_load16_lane:
  6952. case SIMD_v128_load32_lane:
  6953. case SIMD_v128_load64_lane:
  6954. case SIMD_v128_store8_lane:
  6955. case SIMD_v128_store16_lane:
  6956. case SIMD_v128_store32_lane:
  6957. case SIMD_v128_store64_lane:
  6958. /* memarg align */
  6959. skip_leb_uint32(p, p_end);
  6960. /* memarg offset */
  6961. skip_leb_mem_offset(p, p_end);
  6962. /* ImmLaneId */
  6963. CHECK_BUF(p, p_end, 1);
  6964. p++;
  6965. break;
  6966. case SIMD_v128_load32_zero:
  6967. case SIMD_v128_load64_zero:
  6968. /* memarg align */
  6969. skip_leb_uint32(p, p_end);
  6970. /* memarg offset */
  6971. skip_leb_mem_offset(p, p_end);
  6972. break;
  6973. default:
  6974. /*
  6975. * since latest SIMD specific used almost every value
  6976. * from 0x00 to 0xff, the default branch will present
  6977. * all opcodes without imm
  6978. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6979. */
  6980. break;
  6981. }
  6982. break;
  6983. }
  6984. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6985. #endif /* end of WASM_ENABLE_SIMD */
  6986. #if WASM_ENABLE_SHARED_MEMORY != 0
  6987. case WASM_OP_ATOMIC_PREFIX:
  6988. {
  6989. uint32 opcode1;
  6990. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6991. read_leb_uint32(p, p_end, opcode1);
  6992. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6993. is no larger than UINT8_MAX */
  6994. opcode = (uint8)opcode1;
  6995. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6996. skip_leb_uint32(p, p_end); /* align */
  6997. skip_leb_mem_offset(p, p_end); /* offset */
  6998. }
  6999. else {
  7000. /* atomic.fence doesn't have memarg */
  7001. p++;
  7002. }
  7003. break;
  7004. }
  7005. #endif
  7006. #if WASM_ENABLE_DEBUG_INTERP != 0
  7007. case DEBUG_OP_BREAK:
  7008. {
  7009. WASMDebugInstance *debug_instance =
  7010. wasm_exec_env_get_instance(exec_env);
  7011. char original_opcode[1];
  7012. uint64 size = 1;
  7013. WASMModuleInstance *module_inst =
  7014. (WASMModuleInstance *)exec_env->module_inst;
  7015. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7016. ? (p - 1) - module_inst->module->load_addr
  7017. : ~0;
  7018. if (debug_instance) {
  7019. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7020. original_opcode, &size)
  7021. && size == 1) {
  7022. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7023. "with %02x: ",
  7024. original_opcode[0]);
  7025. opcode = original_opcode[0];
  7026. goto op_break_retry;
  7027. }
  7028. }
  7029. break;
  7030. }
  7031. #endif
  7032. default:
  7033. return false;
  7034. }
  7035. }
  7036. (void)u8;
  7037. (void)exec_env;
  7038. return false;
  7039. fail:
  7040. return false;
  7041. }
  7042. #if WASM_ENABLE_FAST_INTERP != 0
  7043. #if WASM_DEBUG_PREPROCESSOR != 0
  7044. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7045. #else
  7046. #define LOG_OP(...) (void)0
  7047. #endif
  7048. #define PATCH_ELSE 0
  7049. #define PATCH_END 1
  7050. typedef struct BranchBlockPatch {
  7051. struct BranchBlockPatch *next;
  7052. uint8 patch_type;
  7053. uint8 *code_compiled;
  7054. } BranchBlockPatch;
  7055. #endif
  7056. typedef struct BranchBlock {
  7057. uint8 label_type;
  7058. BlockType block_type;
  7059. uint8 *start_addr;
  7060. uint8 *else_addr;
  7061. uint8 *end_addr;
  7062. uint32 stack_cell_num;
  7063. #if WASM_ENABLE_GC != 0
  7064. uint32 reftype_map_num;
  7065. /* Indicate which local is used inside current block, used to validate
  7066. * local.get with non-nullable ref types */
  7067. uint8 *local_use_mask;
  7068. uint32 local_use_mask_size;
  7069. #endif
  7070. #if WASM_ENABLE_FAST_INTERP != 0
  7071. uint16 dynamic_offset;
  7072. uint8 *code_compiled;
  7073. BranchBlockPatch *patch_list;
  7074. /* This is used to save params frame_offset of of if block */
  7075. int16 *param_frame_offsets;
  7076. /* This is used to recover the dynamic offset for else branch,
  7077. * and also to remember the start offset of dynamic space which
  7078. * stores the block arguments for loop block, so we can use it
  7079. * to copy the stack operands to the loop block's arguments in
  7080. * wasm_loader_emit_br_info for opcode br. */
  7081. uint16 start_dynamic_offset;
  7082. #endif
  7083. /* Indicate the operand stack is in polymorphic state.
  7084. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7085. * to polymorphic state until the block's 'end' opcode is processed.
  7086. * If stack is in polymorphic state and stack is empty, instruction can
  7087. * pop any type of value directly without decreasing stack top pointer
  7088. * and stack cell num. */
  7089. bool is_stack_polymorphic;
  7090. } BranchBlock;
  7091. typedef struct WASMLoaderContext {
  7092. /* frame ref stack */
  7093. uint8 *frame_ref;
  7094. uint8 *frame_ref_bottom;
  7095. uint8 *frame_ref_boundary;
  7096. uint32 frame_ref_size;
  7097. uint32 stack_cell_num;
  7098. uint32 max_stack_cell_num;
  7099. #if WASM_ENABLE_GC != 0
  7100. /* frame reftype map stack */
  7101. WASMRefTypeMap *frame_reftype_map;
  7102. WASMRefTypeMap *frame_reftype_map_bottom;
  7103. WASMRefTypeMap *frame_reftype_map_boundary;
  7104. uint32 frame_reftype_map_size;
  7105. uint32 reftype_map_num;
  7106. uint32 max_reftype_map_num;
  7107. /* Current module */
  7108. WASMModule *module;
  7109. /* Current module's ref_type_set */
  7110. HashMap *ref_type_set;
  7111. /* Always point to local variable ref_type of
  7112. wasm_loader_prepare_bytecode */
  7113. WASMRefType *ref_type_tmp;
  7114. #endif
  7115. /* frame csp stack */
  7116. BranchBlock *frame_csp;
  7117. BranchBlock *frame_csp_bottom;
  7118. BranchBlock *frame_csp_boundary;
  7119. uint32 frame_csp_size;
  7120. uint32 csp_num;
  7121. uint32 max_csp_num;
  7122. #if WASM_ENABLE_FAST_INTERP != 0
  7123. /* frame offset stack */
  7124. int16 *frame_offset;
  7125. int16 *frame_offset_bottom;
  7126. int16 *frame_offset_boundary;
  7127. uint32 frame_offset_size;
  7128. int16 dynamic_offset;
  7129. int16 start_dynamic_offset;
  7130. int16 max_dynamic_offset;
  7131. /* preserved local offset */
  7132. int16 preserved_local_offset;
  7133. /* const buffer for i64 and f64 consts, note that the raw bytes
  7134. * of i64 and f64 are the same, so we read an i64 value from an
  7135. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7136. int64 *i64_consts;
  7137. uint32 i64_const_max_num;
  7138. uint32 i64_const_num;
  7139. /* const buffer for i32 and f32 consts */
  7140. int32 *i32_consts;
  7141. uint32 i32_const_max_num;
  7142. uint32 i32_const_num;
  7143. /* processed code */
  7144. uint8 *p_code_compiled;
  7145. uint8 *p_code_compiled_end;
  7146. uint32 code_compiled_size;
  7147. /* If the last opcode will be dropped, the peak memory usage will be larger
  7148. * than the final code_compiled_size, we record the peak size to ensure
  7149. * there will not be invalid memory access during second traverse */
  7150. uint32 code_compiled_peak_size;
  7151. #endif
  7152. } WASMLoaderContext;
  7153. #define CHECK_CSP_PUSH() \
  7154. do { \
  7155. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7156. MEM_REALLOC( \
  7157. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7158. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7159. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7160. ctx->frame_csp_boundary = \
  7161. ctx->frame_csp_bottom \
  7162. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7163. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7164. } \
  7165. } while (0)
  7166. #define CHECK_CSP_POP() \
  7167. do { \
  7168. if (ctx->csp_num < 1) { \
  7169. set_error_buf(error_buf, error_buf_size, \
  7170. "type mismatch: " \
  7171. "expect data but block stack was empty"); \
  7172. goto fail; \
  7173. } \
  7174. } while (0)
  7175. #if WASM_ENABLE_FAST_INTERP != 0
  7176. static bool
  7177. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7178. uint32 error_buf_size)
  7179. {
  7180. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7181. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7182. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7183. ctx->frame_offset_size + 16);
  7184. ctx->frame_offset_size += 16;
  7185. ctx->frame_offset_boundary =
  7186. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7187. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7188. }
  7189. return true;
  7190. fail:
  7191. return false;
  7192. }
  7193. static bool
  7194. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7195. {
  7196. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7197. return false;
  7198. return true;
  7199. }
  7200. static void
  7201. free_label_patch_list(BranchBlock *frame_csp)
  7202. {
  7203. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7204. BranchBlockPatch *next;
  7205. while (label_patch != NULL) {
  7206. next = label_patch->next;
  7207. wasm_runtime_free(label_patch);
  7208. label_patch = next;
  7209. }
  7210. frame_csp->patch_list = NULL;
  7211. }
  7212. static void
  7213. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7214. {
  7215. BranchBlock *tmp_csp = frame_csp;
  7216. uint32 i;
  7217. for (i = 0; i < csp_num; i++) {
  7218. free_label_patch_list(tmp_csp);
  7219. tmp_csp++;
  7220. }
  7221. }
  7222. static void
  7223. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7224. {
  7225. BranchBlock *tmp_csp = frame_csp;
  7226. uint32 i;
  7227. for (i = 0; i < csp_num; i++) {
  7228. if (tmp_csp->param_frame_offsets)
  7229. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7230. tmp_csp++;
  7231. }
  7232. }
  7233. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7234. #if WASM_ENABLE_GC != 0
  7235. static bool
  7236. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7237. char *error_buf, uint32 error_buf_size)
  7238. {
  7239. BranchBlock *current_csp = ctx->frame_csp - 1;
  7240. uint32 local_mask_size;
  7241. if (local_count == 0) {
  7242. current_csp->local_use_mask_size = 0;
  7243. return true;
  7244. }
  7245. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7246. * else branch, we don't need to allocate memory again */
  7247. if (!current_csp->local_use_mask) {
  7248. local_mask_size = (local_count + 7) / sizeof(uint8);
  7249. if (!(current_csp->local_use_mask =
  7250. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7251. return false;
  7252. }
  7253. current_csp->local_use_mask_size = local_mask_size;
  7254. }
  7255. else {
  7256. local_mask_size = current_csp->local_use_mask_size;
  7257. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7258. }
  7259. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7260. /* For non-function blocks, inherit the use status from parent block */
  7261. BranchBlock *parent_csp = current_csp - 1;
  7262. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7263. bh_assert(parent_csp->local_use_mask);
  7264. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7265. parent_csp->local_use_mask, local_mask_size);
  7266. }
  7267. return true;
  7268. }
  7269. static void
  7270. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7271. {
  7272. BranchBlock *current_csp = ctx->frame_csp - 1;
  7273. bh_assert(current_csp->local_use_mask
  7274. || current_csp->local_use_mask_size == 0);
  7275. if (current_csp->local_use_mask) {
  7276. wasm_runtime_free(current_csp->local_use_mask);
  7277. }
  7278. current_csp->local_use_mask = NULL;
  7279. current_csp->local_use_mask_size = 0;
  7280. }
  7281. static void
  7282. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7283. {
  7284. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7285. uint32 i;
  7286. for (i = 0; i < ctx->csp_num; i++) {
  7287. if (tmp_csp->local_use_mask) {
  7288. wasm_runtime_free(tmp_csp->local_use_mask);
  7289. tmp_csp->local_use_mask = NULL;
  7290. tmp_csp->local_use_mask_size = 0;
  7291. }
  7292. tmp_csp++;
  7293. }
  7294. }
  7295. static void
  7296. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7297. {
  7298. BranchBlock *current_csp = ctx->frame_csp - 1;
  7299. uint32 byte_offset = index / sizeof(uint8);
  7300. uint32 bit_offset = index % sizeof(uint8);
  7301. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7302. bh_assert(current_csp->local_use_mask);
  7303. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7304. }
  7305. static bool
  7306. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7307. {
  7308. BranchBlock *current_csp = ctx->frame_csp - 1;
  7309. uint32 byte_offset = index / sizeof(uint8);
  7310. uint32 bit_offset = index % sizeof(uint8);
  7311. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7312. bh_assert(current_csp->local_use_mask);
  7313. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7314. ? true
  7315. : false;
  7316. }
  7317. #endif /* end of WASM_ENABLE_GC != 0 */
  7318. static void
  7319. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7320. {
  7321. if (ctx) {
  7322. if (ctx->frame_ref_bottom)
  7323. wasm_runtime_free(ctx->frame_ref_bottom);
  7324. #if WASM_ENABLE_GC != 0
  7325. if (ctx->frame_reftype_map_bottom)
  7326. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7327. #endif
  7328. if (ctx->frame_csp_bottom) {
  7329. #if WASM_ENABLE_FAST_INTERP != 0
  7330. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7331. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7332. ctx->csp_num);
  7333. #endif
  7334. #if WASM_ENABLE_GC != 0
  7335. wasm_loader_clean_all_local_use_masks(ctx);
  7336. #endif
  7337. wasm_runtime_free(ctx->frame_csp_bottom);
  7338. }
  7339. #if WASM_ENABLE_FAST_INTERP != 0
  7340. if (ctx->frame_offset_bottom)
  7341. wasm_runtime_free(ctx->frame_offset_bottom);
  7342. if (ctx->i64_consts)
  7343. wasm_runtime_free(ctx->i64_consts);
  7344. if (ctx->i32_consts)
  7345. wasm_runtime_free(ctx->i32_consts);
  7346. #endif
  7347. wasm_runtime_free(ctx);
  7348. }
  7349. }
  7350. static WASMLoaderContext *
  7351. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7352. {
  7353. WASMLoaderContext *loader_ctx =
  7354. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7355. if (!loader_ctx)
  7356. return NULL;
  7357. loader_ctx->frame_ref_size = 32;
  7358. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7359. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7360. goto fail;
  7361. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7362. #if WASM_ENABLE_GC != 0
  7363. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7364. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7365. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7366. error_buf_size)))
  7367. goto fail;
  7368. loader_ctx->frame_reftype_map_boundary =
  7369. loader_ctx->frame_reftype_map_bottom + 16;
  7370. #endif
  7371. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7372. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7373. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7374. goto fail;
  7375. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7376. #if WASM_ENABLE_EXCE_HANDLING != 0
  7377. func->exception_handler_count = 0;
  7378. #endif
  7379. #if WASM_ENABLE_FAST_INTERP != 0
  7380. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7381. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7382. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7383. error_buf_size)))
  7384. goto fail;
  7385. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7386. loader_ctx->i64_const_max_num = 8;
  7387. if (!(loader_ctx->i64_consts =
  7388. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7389. error_buf, error_buf_size)))
  7390. goto fail;
  7391. loader_ctx->i32_const_max_num = 8;
  7392. if (!(loader_ctx->i32_consts =
  7393. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7394. error_buf, error_buf_size)))
  7395. goto fail;
  7396. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7397. set_error_buf(error_buf, error_buf_size,
  7398. "fast interpreter offset overflow");
  7399. goto fail;
  7400. }
  7401. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7402. loader_ctx->max_dynamic_offset =
  7403. func->param_cell_num + func->local_cell_num;
  7404. #endif
  7405. return loader_ctx;
  7406. fail:
  7407. wasm_loader_ctx_destroy(loader_ctx);
  7408. return NULL;
  7409. }
  7410. static bool
  7411. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7412. uint32 error_buf_size)
  7413. {
  7414. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7415. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7416. /* Increase the frame ref stack */
  7417. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7418. ctx->frame_ref_size + 16);
  7419. ctx->frame_ref_size += 16;
  7420. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7421. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7422. }
  7423. #if WASM_ENABLE_GC != 0
  7424. if (wasm_is_type_multi_byte_type(type)
  7425. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7426. /* Increase the frame reftype map stack */
  7427. bh_assert(
  7428. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7429. * sizeof(WASMRefTypeMap))
  7430. == ctx->frame_reftype_map_size);
  7431. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7432. ctx->frame_reftype_map_size
  7433. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7434. ctx->frame_reftype_map =
  7435. ctx->frame_reftype_map_bottom
  7436. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7437. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7438. ctx->frame_reftype_map_boundary =
  7439. ctx->frame_reftype_map_bottom
  7440. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7441. }
  7442. #endif
  7443. return true;
  7444. fail:
  7445. return false;
  7446. }
  7447. static bool
  7448. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7449. uint32 error_buf_size)
  7450. {
  7451. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7452. uint32 i;
  7453. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7454. return false;
  7455. #if WASM_ENABLE_GC != 0
  7456. if (wasm_is_type_multi_byte_type(type)) {
  7457. WASMRefType *ref_type;
  7458. if (!(ref_type =
  7459. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7460. error_buf, error_buf_size))) {
  7461. return false;
  7462. }
  7463. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7464. /* Increase the frame reftype map stack */
  7465. bh_assert((uint32)((ctx->frame_reftype_map
  7466. - ctx->frame_reftype_map_bottom)
  7467. * sizeof(WASMRefTypeMap))
  7468. == ctx->frame_reftype_map_size);
  7469. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7470. ctx->frame_reftype_map_size,
  7471. ctx->frame_reftype_map_size
  7472. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7473. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7474. + ctx->frame_reftype_map_size
  7475. / ((uint32)sizeof(WASMRefTypeMap));
  7476. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7477. ctx->frame_reftype_map_boundary =
  7478. ctx->frame_reftype_map_bottom
  7479. + ctx->frame_reftype_map_size
  7480. / ((uint32)sizeof(WASMRefTypeMap));
  7481. }
  7482. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7483. ctx->frame_reftype_map->ref_type = ref_type;
  7484. ctx->frame_reftype_map++;
  7485. ctx->reftype_map_num++;
  7486. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7487. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7488. }
  7489. #endif
  7490. for (i = 0; i < type_cell_num; i++)
  7491. *ctx->frame_ref++ = type;
  7492. ctx->stack_cell_num += type_cell_num;
  7493. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7494. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7495. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7496. set_error_buf(error_buf, error_buf_size,
  7497. "operand stack depth limit exceeded");
  7498. return false;
  7499. }
  7500. }
  7501. return true;
  7502. #if WASM_ENABLE_GC != 0
  7503. fail:
  7504. return false;
  7505. #endif
  7506. }
  7507. static bool
  7508. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7509. int32 stack_cell_num,
  7510. #if WASM_ENABLE_GC != 0
  7511. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7512. #endif
  7513. uint8 type,
  7514. #if WASM_ENABLE_GC != 0
  7515. WASMRefType *ref_type,
  7516. #endif
  7517. char *error_buf, uint32 error_buf_size)
  7518. {
  7519. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7520. #if WASM_ENABLE_GC != 0
  7521. WASMRefType *frame_reftype = NULL;
  7522. #endif
  7523. if (stack_cell_num < type_cell_num) {
  7524. set_error_buf(error_buf, error_buf_size,
  7525. "type mismatch: expect data but stack was empty");
  7526. return false;
  7527. }
  7528. #if WASM_ENABLE_GC == 0
  7529. for (i = 0; i < type_cell_num; i++) {
  7530. if (*(frame_ref - 1 - i) != type) {
  7531. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7532. "type mismatch: expect ", type2str(type),
  7533. " but got other");
  7534. return false;
  7535. }
  7536. }
  7537. #else
  7538. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7539. bh_assert(reftype_map_num > 0);
  7540. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7541. }
  7542. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7543. ref_type, ctx->module->types,
  7544. ctx->module->type_count)) {
  7545. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7546. "type mismatch: expect ", type2str(type),
  7547. " but got other");
  7548. return false;
  7549. }
  7550. for (i = 0; i < type_cell_num - 1; i++) {
  7551. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7552. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7553. "type mismatch: expect ", type2str(type),
  7554. " but got other");
  7555. return false;
  7556. }
  7557. }
  7558. #endif
  7559. return true;
  7560. }
  7561. static bool
  7562. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7563. uint32 error_buf_size)
  7564. {
  7565. int32 block_stack_cell_num =
  7566. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7567. #if WASM_ENABLE_GC != 0
  7568. int32 reftype_map_num =
  7569. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7570. #endif
  7571. if (block_stack_cell_num > 0) {
  7572. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7573. /* the stack top is a value of any type, return success */
  7574. return true;
  7575. }
  7576. #if WASM_ENABLE_GC != 0
  7577. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7578. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7579. WASMRefType *stack_top_ref_type = NULL;
  7580. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7581. bh_assert(reftype_map_num > 0);
  7582. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7583. }
  7584. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7585. ctx->ref_type_tmp, ctx->module->types,
  7586. ctx->module->type_count)) {
  7587. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7588. uint32 ref_type_struct_size =
  7589. wasm_reftype_struct_size(stack_top_ref_type);
  7590. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7591. stack_top_ref_type, ref_type_struct_size);
  7592. }
  7593. return true;
  7594. }
  7595. }
  7596. #endif
  7597. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7598. #if WASM_ENABLE_GC != 0
  7599. ctx->frame_reftype_map, reftype_map_num,
  7600. #endif
  7601. type,
  7602. #if WASM_ENABLE_GC != 0
  7603. ctx->ref_type_tmp,
  7604. #endif
  7605. error_buf, error_buf_size)) {
  7606. return false;
  7607. }
  7608. return true;
  7609. }
  7610. static bool
  7611. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7612. uint32 error_buf_size)
  7613. {
  7614. BranchBlock *cur_block = ctx->frame_csp - 1;
  7615. int32 available_stack_cell =
  7616. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7617. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7618. /* Directly return success if current block is in stack
  7619. polymorphic state while stack is empty. */
  7620. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7621. return true;
  7622. if (type == VALUE_TYPE_VOID)
  7623. return true;
  7624. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7625. return false;
  7626. bh_assert(available_stack_cell > 0);
  7627. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7628. type = VALUE_TYPE_ANY;
  7629. cell_num_to_pop = 1;
  7630. }
  7631. ctx->frame_ref -= cell_num_to_pop;
  7632. ctx->stack_cell_num -= cell_num_to_pop;
  7633. #if WASM_ENABLE_GC != 0
  7634. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7635. ctx->frame_reftype_map--;
  7636. ctx->reftype_map_num--;
  7637. }
  7638. #endif
  7639. return true;
  7640. }
  7641. #if WASM_ENABLE_GC != 0
  7642. /* Get the stack top element of current block */
  7643. static bool
  7644. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7645. WASMRefType **p_ref_type, char *error_buf,
  7646. uint32 error_buf_size)
  7647. {
  7648. BranchBlock *cur_block = ctx->frame_csp - 1;
  7649. int32 available_stack_cell =
  7650. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7651. if (available_stack_cell <= 0) {
  7652. /* Directly return success if current block is in stack
  7653. polymorphic state while stack is empty. */
  7654. if (cur_block->is_stack_polymorphic) {
  7655. *p_type = VALUE_TYPE_ANY;
  7656. return true;
  7657. }
  7658. else {
  7659. set_error_buf(
  7660. error_buf, error_buf_size,
  7661. "type mismatch: expect data but block stack was empty");
  7662. return false;
  7663. }
  7664. }
  7665. *p_type = *(ctx->frame_ref - 1);
  7666. if (wasm_is_type_multi_byte_type(*p_type)) {
  7667. int32 available_reftype_map =
  7668. (int32)(ctx->reftype_map_num
  7669. - (ctx->frame_csp - 1)->reftype_map_num);
  7670. bh_assert(available_reftype_map > 0);
  7671. (void)available_reftype_map;
  7672. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7673. }
  7674. return true;
  7675. }
  7676. #if WASM_ENABLE_FAST_INTERP != 0
  7677. static bool
  7678. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7679. char *error_buf, uint32 error_buf_size);
  7680. #endif
  7681. /* Check whether the stack top elem is a heap object, and if yes,
  7682. pop and return it */
  7683. static bool
  7684. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7685. WASMRefType *ref_ht_ret, char *error_buf,
  7686. uint32 error_buf_size)
  7687. {
  7688. uint8 type = 0;
  7689. WASMRefType *ref_type = NULL;
  7690. /* Get stack top element */
  7691. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7692. error_buf_size)) {
  7693. return false;
  7694. }
  7695. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7696. /* stack top isn't a ref type */
  7697. && !wasm_is_type_reftype(type)) {
  7698. set_error_buf(error_buf, error_buf_size,
  7699. "type mismatch: expect heap object but got others");
  7700. return false;
  7701. }
  7702. /* POP stack top */
  7703. if (wasm_is_type_multi_byte_type(type)) {
  7704. bh_assert(ref_type);
  7705. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7706. wasm_reftype_struct_size(ref_type));
  7707. }
  7708. #if WASM_ENABLE_FAST_INTERP != 0
  7709. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7710. error_buf_size)) {
  7711. return false;
  7712. }
  7713. #else
  7714. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7715. return false;
  7716. }
  7717. #endif
  7718. if (p_type)
  7719. *p_type = type;
  7720. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7721. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7722. wasm_reftype_struct_size(ref_type));
  7723. }
  7724. return true;
  7725. }
  7726. /* Check whether the stack top elem is subtype of (ref null ht),
  7727. and if yes, pop it and return the converted (ref ht) */
  7728. static bool
  7729. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7730. WASMRefType *ref_ht_ret, char *error_buf,
  7731. uint32 error_buf_size)
  7732. {
  7733. uint8 type = 0;
  7734. WASMRefType ref_type = { 0 };
  7735. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7736. error_buf_size)) {
  7737. return false;
  7738. }
  7739. /* Convert to related (ref ht) and return */
  7740. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7741. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7742. */
  7743. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7744. HEAP_TYPE_ARRAY
  7745. + (type - REF_TYPE_ARRAYREF));
  7746. type = ref_ht_ret->ref_type;
  7747. }
  7748. else if (wasm_is_reftype_htref_nullable(type)
  7749. || wasm_is_reftype_htref_non_nullable(type)) {
  7750. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7751. wasm_reftype_struct_size(&ref_type));
  7752. /* Convert to (ref ht) */
  7753. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7754. ref_ht_ret->ref_ht_common.nullable = false;
  7755. type = ref_ht_ret->ref_type;
  7756. }
  7757. *p_type = type;
  7758. return true;
  7759. }
  7760. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7761. and if yes, pop it and return the type_idx */
  7762. static bool
  7763. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7764. uint32 *p_type_idx, char *error_buf,
  7765. uint32 error_buf_size)
  7766. {
  7767. uint8 type = 0;
  7768. int32 type_idx = -1;
  7769. WASMRefType *ref_type = NULL;
  7770. /* Get stack top element */
  7771. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7772. error_buf_size)) {
  7773. return false;
  7774. }
  7775. if (type != VALUE_TYPE_ANY) {
  7776. /* stack top isn't (ref null $t) */
  7777. if (!((wasm_is_reftype_htref_nullable(type)
  7778. || wasm_is_reftype_htref_non_nullable(type))
  7779. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7780. set_error_buf(error_buf, error_buf_size,
  7781. "type mismatch: expect (ref null $t) but got others");
  7782. return false;
  7783. }
  7784. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7785. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7786. wasm_reftype_struct_size(ref_type));
  7787. }
  7788. /* POP stack top */
  7789. #if WASM_ENABLE_FAST_INTERP != 0
  7790. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7791. error_buf_size)) {
  7792. return false;
  7793. }
  7794. #else
  7795. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7796. return false;
  7797. }
  7798. #endif
  7799. /* Convert to type_idx and return */
  7800. *p_type = type;
  7801. if (type != VALUE_TYPE_ANY)
  7802. *p_type_idx = (uint32)type_idx;
  7803. return true;
  7804. }
  7805. #endif /* WASM_ENABLE_GC != 0 */
  7806. #if WASM_ENABLE_FAST_INTERP == 0
  7807. static bool
  7808. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7809. uint8 type_push, uint8 type_pop, char *error_buf,
  7810. uint32 error_buf_size)
  7811. {
  7812. for (int i = 0; i < pop_cnt; i++) {
  7813. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7814. error_buf_size))
  7815. return false;
  7816. }
  7817. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7818. return false;
  7819. return true;
  7820. }
  7821. #endif
  7822. static bool
  7823. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7824. BlockType block_type, uint8 *start_addr,
  7825. char *error_buf, uint32 error_buf_size)
  7826. {
  7827. CHECK_CSP_PUSH();
  7828. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7829. ctx->frame_csp->label_type = label_type;
  7830. ctx->frame_csp->block_type = block_type;
  7831. ctx->frame_csp->start_addr = start_addr;
  7832. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7833. #if WASM_ENABLE_GC != 0
  7834. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7835. #endif
  7836. #if WASM_ENABLE_FAST_INTERP != 0
  7837. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7838. ctx->frame_csp->patch_list = NULL;
  7839. #endif
  7840. ctx->frame_csp++;
  7841. ctx->csp_num++;
  7842. if (ctx->csp_num > ctx->max_csp_num) {
  7843. ctx->max_csp_num = ctx->csp_num;
  7844. if (ctx->max_csp_num > UINT16_MAX) {
  7845. set_error_buf(error_buf, error_buf_size,
  7846. "label stack depth limit exceeded");
  7847. return false;
  7848. }
  7849. }
  7850. return true;
  7851. fail:
  7852. return false;
  7853. }
  7854. static bool
  7855. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7856. uint32 error_buf_size)
  7857. {
  7858. CHECK_CSP_POP();
  7859. #if WASM_ENABLE_FAST_INTERP != 0
  7860. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7861. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7862. #endif
  7863. ctx->frame_csp--;
  7864. ctx->csp_num--;
  7865. return true;
  7866. fail:
  7867. return false;
  7868. }
  7869. #if WASM_ENABLE_FAST_INTERP != 0
  7870. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7871. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7872. #define emit_label(opcode) \
  7873. do { \
  7874. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7875. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7876. } while (0)
  7877. #define skip_label() \
  7878. do { \
  7879. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7880. LOG_OP("\ndelete last op\n"); \
  7881. } while (0)
  7882. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7883. #if UINTPTR_MAX == UINT64_MAX
  7884. #define emit_label(opcode) \
  7885. do { \
  7886. int32 offset = \
  7887. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7888. /* emit int32 relative offset in 64-bit target */ \
  7889. wasm_loader_emit_uint32(loader_ctx, offset); \
  7890. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7891. } while (0)
  7892. #else
  7893. #define emit_label(opcode) \
  7894. do { \
  7895. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7896. /* emit uint32 label address in 32-bit target */ \
  7897. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7898. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7899. } while (0)
  7900. #endif
  7901. #define skip_label() \
  7902. do { \
  7903. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7904. LOG_OP("\ndelete last op\n"); \
  7905. } while (0)
  7906. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7907. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7908. #define emit_label(opcode) \
  7909. do { \
  7910. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7911. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7912. } while (0)
  7913. #define skip_label() \
  7914. do { \
  7915. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7916. LOG_OP("\ndelete last op\n"); \
  7917. } while (0)
  7918. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7919. #define emit_empty_label_addr_and_frame_ip(type) \
  7920. do { \
  7921. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7922. loader_ctx->p_code_compiled, error_buf, \
  7923. error_buf_size)) \
  7924. goto fail; \
  7925. /* label address, to be patched */ \
  7926. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7927. } while (0)
  7928. #define emit_br_info(frame_csp, is_br) \
  7929. do { \
  7930. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7931. error_buf_size)) \
  7932. goto fail; \
  7933. } while (0)
  7934. #define LAST_OP_OUTPUT_I32() \
  7935. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7936. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7937. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7938. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7939. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7940. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7941. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7942. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7943. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7944. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7945. || (last_op == EXT_OP_COPY_STACK_TOP)
  7946. #define LAST_OP_OUTPUT_I64() \
  7947. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7948. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7949. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7950. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7951. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7952. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7953. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7954. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7955. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7956. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7957. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7958. #define GET_CONST_OFFSET(type, val) \
  7959. do { \
  7960. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7961. &operand_offset, error_buf, \
  7962. error_buf_size))) \
  7963. goto fail; \
  7964. } while (0)
  7965. #define GET_CONST_F32_OFFSET(type, fval) \
  7966. do { \
  7967. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7968. &operand_offset, error_buf, \
  7969. error_buf_size))) \
  7970. goto fail; \
  7971. } while (0)
  7972. #define GET_CONST_F64_OFFSET(type, fval) \
  7973. do { \
  7974. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7975. &operand_offset, error_buf, \
  7976. error_buf_size))) \
  7977. goto fail; \
  7978. } while (0)
  7979. #define emit_operand(ctx, offset) \
  7980. do { \
  7981. wasm_loader_emit_int16(ctx, offset); \
  7982. LOG_OP("%d\t", offset); \
  7983. } while (0)
  7984. #define emit_byte(ctx, byte) \
  7985. do { \
  7986. wasm_loader_emit_uint8(ctx, byte); \
  7987. LOG_OP("%d\t", byte); \
  7988. } while (0)
  7989. #define emit_uint32(ctx, value) \
  7990. do { \
  7991. wasm_loader_emit_uint32(ctx, value); \
  7992. LOG_OP("%d\t", value); \
  7993. } while (0)
  7994. #define emit_uint64(ctx, value) \
  7995. do { \
  7996. wasm_loader_emit_const(ctx, &value, false); \
  7997. LOG_OP("%lld\t", value); \
  7998. } while (0)
  7999. #define emit_float32(ctx, value) \
  8000. do { \
  8001. wasm_loader_emit_const(ctx, &value, true); \
  8002. LOG_OP("%f\t", value); \
  8003. } while (0)
  8004. #define emit_float64(ctx, value) \
  8005. do { \
  8006. wasm_loader_emit_const(ctx, &value, false); \
  8007. LOG_OP("%f\t", value); \
  8008. } while (0)
  8009. static bool
  8010. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8011. {
  8012. if (!(ctx->p_code_compiled =
  8013. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8014. return false;
  8015. ctx->p_code_compiled_end =
  8016. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8017. /* clean up frame ref */
  8018. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8019. ctx->frame_ref = ctx->frame_ref_bottom;
  8020. ctx->stack_cell_num = 0;
  8021. #if WASM_ENABLE_GC != 0
  8022. /* clean up reftype map */
  8023. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8024. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8025. ctx->reftype_map_num = 0;
  8026. #endif
  8027. /* clean up frame csp */
  8028. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8029. ctx->frame_csp = ctx->frame_csp_bottom;
  8030. ctx->csp_num = 0;
  8031. ctx->max_csp_num = 0;
  8032. /* clean up frame offset */
  8033. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8034. ctx->frame_offset = ctx->frame_offset_bottom;
  8035. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8036. /* init preserved local offsets */
  8037. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8038. /* const buf is reserved */
  8039. return true;
  8040. }
  8041. static void
  8042. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8043. {
  8044. ctx->code_compiled_size += size;
  8045. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8046. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8047. }
  8048. }
  8049. static void
  8050. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8051. {
  8052. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8053. if (ctx->p_code_compiled) {
  8054. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8055. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8056. #endif
  8057. bh_memcpy_s(ctx->p_code_compiled,
  8058. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8059. value, size);
  8060. ctx->p_code_compiled += size;
  8061. }
  8062. else {
  8063. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8064. bh_assert((ctx->code_compiled_size & 1) == 0);
  8065. #endif
  8066. increase_compiled_code_space(ctx, size);
  8067. }
  8068. }
  8069. static void
  8070. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8071. {
  8072. if (ctx->p_code_compiled) {
  8073. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8074. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8075. #endif
  8076. STORE_U32(ctx->p_code_compiled, value);
  8077. ctx->p_code_compiled += sizeof(uint32);
  8078. }
  8079. else {
  8080. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8081. bh_assert((ctx->code_compiled_size & 1) == 0);
  8082. #endif
  8083. increase_compiled_code_space(ctx, sizeof(uint32));
  8084. }
  8085. }
  8086. static void
  8087. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8088. {
  8089. if (ctx->p_code_compiled) {
  8090. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8091. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8092. #endif
  8093. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8094. ctx->p_code_compiled += sizeof(int16);
  8095. }
  8096. else {
  8097. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8098. bh_assert((ctx->code_compiled_size & 1) == 0);
  8099. #endif
  8100. increase_compiled_code_space(ctx, sizeof(uint16));
  8101. }
  8102. }
  8103. static void
  8104. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8105. {
  8106. if (ctx->p_code_compiled) {
  8107. *(ctx->p_code_compiled) = value;
  8108. ctx->p_code_compiled += sizeof(uint8);
  8109. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8110. ctx->p_code_compiled++;
  8111. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8112. #endif
  8113. }
  8114. else {
  8115. increase_compiled_code_space(ctx, sizeof(uint8));
  8116. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8117. increase_compiled_code_space(ctx, sizeof(uint8));
  8118. bh_assert((ctx->code_compiled_size & 1) == 0);
  8119. #endif
  8120. }
  8121. }
  8122. static void
  8123. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8124. {
  8125. if (ctx->p_code_compiled) {
  8126. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8127. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8128. #endif
  8129. STORE_PTR(ctx->p_code_compiled, value);
  8130. ctx->p_code_compiled += sizeof(void *);
  8131. }
  8132. else {
  8133. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8134. bh_assert((ctx->code_compiled_size & 1) == 0);
  8135. #endif
  8136. increase_compiled_code_space(ctx, sizeof(void *));
  8137. }
  8138. }
  8139. static void
  8140. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8141. {
  8142. if (ctx->p_code_compiled) {
  8143. ctx->p_code_compiled -= size;
  8144. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8145. if (size == sizeof(uint8)) {
  8146. ctx->p_code_compiled--;
  8147. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8148. }
  8149. #endif
  8150. }
  8151. else {
  8152. ctx->code_compiled_size -= size;
  8153. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8154. if (size == sizeof(uint8)) {
  8155. ctx->code_compiled_size--;
  8156. bh_assert((ctx->code_compiled_size & 1) == 0);
  8157. }
  8158. #endif
  8159. }
  8160. }
  8161. static bool
  8162. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8163. uint32 local_index, uint32 local_type,
  8164. bool *preserved, char *error_buf,
  8165. uint32 error_buf_size)
  8166. {
  8167. uint32 i = 0;
  8168. int16 preserved_offset = (int16)local_index;
  8169. *preserved = false;
  8170. while (i < loader_ctx->stack_cell_num) {
  8171. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8172. /* move previous local into dynamic space before a set/tee_local opcode
  8173. */
  8174. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8175. if (!(*preserved)) {
  8176. *preserved = true;
  8177. skip_label();
  8178. preserved_offset = loader_ctx->preserved_local_offset;
  8179. if (loader_ctx->p_code_compiled) {
  8180. bh_assert(preserved_offset != (int16)local_index);
  8181. }
  8182. if (is_32bit_type(local_type)) {
  8183. /* Only increase preserve offset in the second traversal */
  8184. if (loader_ctx->p_code_compiled)
  8185. loader_ctx->preserved_local_offset++;
  8186. emit_label(EXT_OP_COPY_STACK_TOP);
  8187. }
  8188. else {
  8189. if (loader_ctx->p_code_compiled)
  8190. loader_ctx->preserved_local_offset += 2;
  8191. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8192. }
  8193. emit_operand(loader_ctx, local_index);
  8194. emit_operand(loader_ctx, preserved_offset);
  8195. emit_label(opcode);
  8196. }
  8197. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8198. }
  8199. if (is_32bit_type(cur_type))
  8200. i++;
  8201. else
  8202. i += 2;
  8203. }
  8204. (void)error_buf;
  8205. (void)error_buf_size;
  8206. return true;
  8207. }
  8208. static bool
  8209. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8210. char *error_buf, uint32 error_buf_size)
  8211. {
  8212. uint32 i = 0;
  8213. bool preserve_local;
  8214. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8215. blocks will not influence the value of these locals */
  8216. while (i < loader_ctx->stack_cell_num) {
  8217. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8218. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8219. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8220. && (cur_offset >= 0)) {
  8221. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8222. cur_type, &preserve_local,
  8223. error_buf, error_buf_size)))
  8224. return false;
  8225. }
  8226. if (is_32bit_type(cur_type)) {
  8227. i++;
  8228. }
  8229. else {
  8230. i += 2;
  8231. }
  8232. }
  8233. return true;
  8234. }
  8235. static bool
  8236. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8237. uint8 *p_code_compiled, char *error_buf,
  8238. uint32 error_buf_size)
  8239. {
  8240. BranchBlockPatch *patch =
  8241. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8242. if (!patch) {
  8243. return false;
  8244. }
  8245. patch->patch_type = patch_type;
  8246. patch->code_compiled = p_code_compiled;
  8247. if (!frame_csp->patch_list) {
  8248. frame_csp->patch_list = patch;
  8249. patch->next = NULL;
  8250. }
  8251. else {
  8252. patch->next = frame_csp->patch_list;
  8253. frame_csp->patch_list = patch;
  8254. }
  8255. return true;
  8256. }
  8257. static void
  8258. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8259. {
  8260. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8261. BranchBlockPatch *node = frame_csp->patch_list;
  8262. BranchBlockPatch *node_prev = NULL, *node_next;
  8263. if (!ctx->p_code_compiled)
  8264. return;
  8265. while (node) {
  8266. node_next = node->next;
  8267. if (node->patch_type == patch_type) {
  8268. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8269. if (node_prev == NULL) {
  8270. frame_csp->patch_list = node_next;
  8271. }
  8272. else {
  8273. node_prev->next = node_next;
  8274. }
  8275. wasm_runtime_free(node);
  8276. }
  8277. else {
  8278. node_prev = node;
  8279. }
  8280. node = node_next;
  8281. }
  8282. }
  8283. static bool
  8284. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8285. bool is_br, char *error_buf, uint32 error_buf_size)
  8286. {
  8287. /* br info layout:
  8288. * a) arity of target block
  8289. * b) total cell num of arity values
  8290. * c) each arity value's cell num
  8291. * d) each arity value's src frame offset
  8292. * e) each arity values's dst dynamic offset
  8293. * f) branch target address
  8294. *
  8295. * Note: b-e are omitted when arity is 0 so that
  8296. * interpreter can recover the br info quickly.
  8297. */
  8298. BlockType *block_type = &frame_csp->block_type;
  8299. uint8 *types = NULL, cell;
  8300. #if WASM_ENABLE_GC != 0
  8301. WASMRefTypeMap *reftype_maps;
  8302. uint32 reftype_map_count;
  8303. #endif
  8304. uint32 arity = 0;
  8305. int32 i;
  8306. int16 *frame_offset = ctx->frame_offset;
  8307. uint16 dynamic_offset;
  8308. /* Note: loop's arity is different from if and block. loop's arity is
  8309. * its parameter count while if and block arity is result count.
  8310. */
  8311. #if WASM_ENABLE_GC == 0
  8312. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8313. arity = block_type_get_param_types(block_type, &types);
  8314. else
  8315. arity = block_type_get_result_types(block_type, &types);
  8316. #else
  8317. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8318. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8319. &reftype_map_count);
  8320. else
  8321. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8322. &reftype_map_count);
  8323. #endif
  8324. /* Part a */
  8325. emit_uint32(ctx, arity);
  8326. if (arity) {
  8327. /* Part b */
  8328. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8329. /* Part c */
  8330. for (i = (int32)arity - 1; i >= 0; i--) {
  8331. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8332. emit_byte(ctx, cell);
  8333. }
  8334. /* Part d */
  8335. for (i = (int32)arity - 1; i >= 0; i--) {
  8336. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8337. frame_offset -= cell;
  8338. emit_operand(ctx, *(int16 *)(frame_offset));
  8339. }
  8340. /* Part e */
  8341. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8342. /* Use start_dynamic_offset which was set in
  8343. copy_params_to_dynamic_space */
  8344. dynamic_offset = frame_csp->start_dynamic_offset
  8345. + wasm_get_cell_num(types, arity);
  8346. else
  8347. dynamic_offset =
  8348. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8349. if (is_br)
  8350. ctx->dynamic_offset = dynamic_offset;
  8351. for (i = (int32)arity - 1; i >= 0; i--) {
  8352. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8353. dynamic_offset -= cell;
  8354. emit_operand(ctx, dynamic_offset);
  8355. }
  8356. }
  8357. /* Part f */
  8358. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8359. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8360. }
  8361. else {
  8362. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8363. error_buf, error_buf_size))
  8364. return false;
  8365. /* label address, to be patched */
  8366. wasm_loader_emit_ptr(ctx, NULL);
  8367. }
  8368. return true;
  8369. }
  8370. static bool
  8371. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8372. bool disable_emit, int16 operand_offset,
  8373. char *error_buf, uint32 error_buf_size)
  8374. {
  8375. uint32 cell_num_to_push, i;
  8376. if (type == VALUE_TYPE_VOID)
  8377. return true;
  8378. /* only check memory overflow in first traverse */
  8379. if (ctx->p_code_compiled == NULL) {
  8380. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8381. return false;
  8382. }
  8383. if (disable_emit)
  8384. *(ctx->frame_offset)++ = operand_offset;
  8385. else {
  8386. emit_operand(ctx, ctx->dynamic_offset);
  8387. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8388. ctx->dynamic_offset++;
  8389. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8390. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8391. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8392. goto fail;
  8393. }
  8394. }
  8395. }
  8396. if (is_32bit_type(type))
  8397. return true;
  8398. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8399. for (i = 0; i < cell_num_to_push; i++) {
  8400. if (ctx->p_code_compiled == NULL) {
  8401. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8402. return false;
  8403. }
  8404. ctx->frame_offset++;
  8405. if (!disable_emit) {
  8406. ctx->dynamic_offset++;
  8407. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8408. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8409. if (ctx->max_dynamic_offset >= INT16_MAX)
  8410. goto fail;
  8411. }
  8412. }
  8413. }
  8414. return true;
  8415. fail:
  8416. set_error_buf(error_buf, error_buf_size,
  8417. "fast interpreter offset overflow");
  8418. return false;
  8419. }
  8420. /* This function should be in front of wasm_loader_pop_frame_ref
  8421. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8422. will be modified by wasm_loader_pop_frame_ref */
  8423. static bool
  8424. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8425. char *error_buf, uint32 error_buf_size)
  8426. {
  8427. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8428. then current block is the function block */
  8429. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8430. BranchBlock *cur_block = ctx->frame_csp - depth;
  8431. int32 available_stack_cell =
  8432. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8433. uint32 cell_num_to_pop;
  8434. /* Directly return success if current block is in stack
  8435. polymorphic state while stack is empty. */
  8436. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8437. return true;
  8438. if (type == VALUE_TYPE_VOID)
  8439. return true;
  8440. /* Change type to ANY when the stack top is ANY, so as to avoid
  8441. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8442. two offsets */
  8443. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8444. type = VALUE_TYPE_ANY;
  8445. cell_num_to_pop = wasm_value_type_cell_num(type);
  8446. /* Check the offset stack bottom to ensure the frame offset
  8447. stack will not go underflow. But we don't thrown error
  8448. and return true here, because the error msg should be
  8449. given in wasm_loader_pop_frame_ref */
  8450. if (!check_offset_pop(ctx, cell_num_to_pop))
  8451. return true;
  8452. ctx->frame_offset -= cell_num_to_pop;
  8453. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8454. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8455. ctx->dynamic_offset -= cell_num_to_pop;
  8456. emit_operand(ctx, *(ctx->frame_offset));
  8457. (void)error_buf;
  8458. (void)error_buf_size;
  8459. return true;
  8460. }
  8461. static bool
  8462. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8463. bool disable_emit, int16 operand_offset,
  8464. char *error_buf, uint32 error_buf_size)
  8465. {
  8466. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8467. error_buf, error_buf_size)))
  8468. return false;
  8469. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8470. return false;
  8471. return true;
  8472. }
  8473. static bool
  8474. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8475. char *error_buf, uint32 error_buf_size)
  8476. {
  8477. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8478. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8479. return false;
  8480. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8481. return false;
  8482. return true;
  8483. }
  8484. static bool
  8485. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8486. uint8 type_push, uint8 type_pop,
  8487. bool disable_emit, int16 operand_offset,
  8488. char *error_buf, uint32 error_buf_size)
  8489. {
  8490. uint8 i;
  8491. for (i = 0; i < pop_cnt; i++) {
  8492. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8493. error_buf_size))
  8494. return false;
  8495. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8496. error_buf_size))
  8497. return false;
  8498. }
  8499. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8500. operand_offset, error_buf,
  8501. error_buf_size))
  8502. return false;
  8503. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8504. return false;
  8505. return true;
  8506. }
  8507. static int
  8508. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  8509. {
  8510. int64 i64_const1 = *(int64 *)p_i64_const1;
  8511. int64 i64_const2 = *(int64 *)p_i64_const2;
  8512. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  8513. }
  8514. static int
  8515. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  8516. {
  8517. int32 i32_const1 = *(int32 *)p_i32_const1;
  8518. int32 i32_const2 = *(int32 *)p_i32_const2;
  8519. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  8520. }
  8521. static bool
  8522. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8523. int16 *offset, char *error_buf,
  8524. uint32 error_buf_size)
  8525. {
  8526. if (!ctx->p_code_compiled) {
  8527. /* Treat i64 and f64 as the same by reading i64 value from
  8528. the raw bytes */
  8529. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8530. /* No slot left, emit const instead */
  8531. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  8532. *offset = 0;
  8533. return true;
  8534. }
  8535. /* Traverse the list if the const num is small */
  8536. if (ctx->i64_const_num < 10) {
  8537. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  8538. if (ctx->i64_consts[i] == *(int64 *)value) {
  8539. *offset = -1;
  8540. return true;
  8541. }
  8542. }
  8543. }
  8544. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  8545. MEM_REALLOC(ctx->i64_consts,
  8546. sizeof(int64) * ctx->i64_const_max_num,
  8547. sizeof(int64) * (ctx->i64_const_max_num * 2));
  8548. ctx->i64_const_max_num *= 2;
  8549. }
  8550. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  8551. }
  8552. else {
  8553. /* Treat i32 and f32 as the same by reading i32 value from
  8554. the raw bytes */
  8555. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  8556. /* No slot left, emit const instead */
  8557. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  8558. *offset = 0;
  8559. return true;
  8560. }
  8561. /* Traverse the list if the const num is small */
  8562. if (ctx->i32_const_num < 10) {
  8563. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  8564. if (ctx->i32_consts[i] == *(int32 *)value) {
  8565. *offset = -1;
  8566. return true;
  8567. }
  8568. }
  8569. }
  8570. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  8571. MEM_REALLOC(ctx->i32_consts,
  8572. sizeof(int32) * ctx->i32_const_max_num,
  8573. sizeof(int32) * (ctx->i32_const_max_num * 2));
  8574. ctx->i32_const_max_num *= 2;
  8575. }
  8576. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  8577. }
  8578. *offset = -1;
  8579. return true;
  8580. }
  8581. else {
  8582. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8583. int64 key = *(int64 *)value, *i64_const;
  8584. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  8585. sizeof(int64), cmp_i64_const);
  8586. if (!i64_const) { /* not found, emit const instead */
  8587. *offset = 0;
  8588. return true;
  8589. }
  8590. *offset = -(uint32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  8591. + (uint32)(i64_const - ctx->i64_consts) * 2;
  8592. }
  8593. else {
  8594. int32 key = *(int32 *)value, *i32_const;
  8595. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  8596. sizeof(int32), cmp_i32_const);
  8597. if (!i32_const) { /* not found, emit const instead */
  8598. *offset = 0;
  8599. return true;
  8600. }
  8601. *offset = -(uint32)(ctx->i32_const_num)
  8602. + (uint32)(i32_const - ctx->i32_consts);
  8603. }
  8604. return true;
  8605. }
  8606. fail:
  8607. return false;
  8608. }
  8609. /*
  8610. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8611. -- Mostly used for the binary / compare operation
  8612. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8613. -- Mostly used in block / control instructions
  8614. The POP will always emit the offset on the top of the frame_offset stack
  8615. PUSH can be used in two ways:
  8616. 1. directly PUSH:
  8617. PUSH_XXX();
  8618. will allocate a dynamic space and emit
  8619. 2. silent PUSH:
  8620. operand_offset = xxx; disable_emit = true;
  8621. PUSH_XXX();
  8622. only push the frame_offset stack, no emit
  8623. */
  8624. #define TEMPLATE_PUSH(Type) \
  8625. do { \
  8626. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8627. disable_emit, operand_offset, \
  8628. error_buf, error_buf_size)) \
  8629. goto fail; \
  8630. } while (0)
  8631. #define TEMPLATE_PUSH_REF(Type) \
  8632. do { \
  8633. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8634. operand_offset, error_buf, \
  8635. error_buf_size)) \
  8636. goto fail; \
  8637. } while (0)
  8638. #define TEMPLATE_POP(Type) \
  8639. do { \
  8640. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8641. error_buf, error_buf_size)) \
  8642. goto fail; \
  8643. } while (0)
  8644. #define TEMPLATE_POP_REF(Type) \
  8645. do { \
  8646. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8647. error_buf_size)) \
  8648. goto fail; \
  8649. } while (0)
  8650. #define PUSH_OFFSET_TYPE(type) \
  8651. do { \
  8652. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8653. operand_offset, error_buf, \
  8654. error_buf_size))) \
  8655. goto fail; \
  8656. } while (0)
  8657. #define POP_OFFSET_TYPE(type) \
  8658. do { \
  8659. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8660. error_buf_size))) \
  8661. goto fail; \
  8662. } while (0)
  8663. #define POP_AND_PUSH(type_pop, type_push) \
  8664. do { \
  8665. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8666. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8667. operand_offset, error_buf, error_buf_size))) \
  8668. goto fail; \
  8669. } while (0)
  8670. /* type of POPs should be the same */
  8671. #define POP2_AND_PUSH(type_pop, type_push) \
  8672. do { \
  8673. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8674. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8675. operand_offset, error_buf, error_buf_size))) \
  8676. goto fail; \
  8677. } while (0)
  8678. #else /* WASM_ENABLE_FAST_INTERP */
  8679. #define TEMPLATE_PUSH(Type) \
  8680. do { \
  8681. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8682. error_buf, error_buf_size))) \
  8683. goto fail; \
  8684. } while (0)
  8685. #define TEMPLATE_PUSH_REF(Type) \
  8686. do { \
  8687. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8688. error_buf_size))) \
  8689. goto fail; \
  8690. } while (0)
  8691. #define TEMPLATE_POP(Type) \
  8692. do { \
  8693. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8694. error_buf, error_buf_size))) \
  8695. goto fail; \
  8696. } while (0)
  8697. #define TEMPLATE_POP_REF(Type) \
  8698. do { \
  8699. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8700. error_buf_size))) \
  8701. goto fail; \
  8702. } while (0)
  8703. #define POP_AND_PUSH(type_pop, type_push) \
  8704. do { \
  8705. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8706. type_pop, error_buf, \
  8707. error_buf_size))) \
  8708. goto fail; \
  8709. } while (0)
  8710. /* type of POPs should be the same */
  8711. #define POP2_AND_PUSH(type_pop, type_push) \
  8712. do { \
  8713. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8714. type_pop, error_buf, \
  8715. error_buf_size))) \
  8716. goto fail; \
  8717. } while (0)
  8718. #endif /* WASM_ENABLE_FAST_INTERP */
  8719. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8720. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8721. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8722. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8723. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8724. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8725. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8726. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8727. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8728. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8729. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8730. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  8731. #define POP_I32() TEMPLATE_POP(I32)
  8732. #define POP_F32() TEMPLATE_POP(F32)
  8733. #define POP_I64() TEMPLATE_POP(I64)
  8734. #define POP_F64() TEMPLATE_POP(F64)
  8735. #define POP_V128() TEMPLATE_POP(V128)
  8736. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8737. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8738. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8739. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8740. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  8741. #if WASM_ENABLE_FAST_INTERP != 0
  8742. static bool
  8743. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8744. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8745. {
  8746. int16 operand_offset = 0;
  8747. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8748. : loader_ctx->frame_csp;
  8749. BlockType *block_type = &block->block_type;
  8750. uint8 *return_types = NULL;
  8751. #if WASM_ENABLE_GC != 0
  8752. WASMRefTypeMap *reftype_maps = NULL;
  8753. uint32 reftype_map_count;
  8754. #endif
  8755. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8756. int32 i = 0;
  8757. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8758. *frame_offset_org = NULL;
  8759. #if WASM_ENABLE_GC == 0
  8760. return_count = block_type_get_result_types(block_type, &return_types);
  8761. #else
  8762. return_count = block_type_get_result_types(
  8763. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8764. #endif
  8765. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8766. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8767. if (return_count == 1) {
  8768. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8769. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8770. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8771. /* insert op_copy before else opcode */
  8772. if (opcode == WASM_OP_ELSE)
  8773. skip_label();
  8774. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8775. : EXT_OP_COPY_STACK_TOP_I64);
  8776. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8777. emit_operand(loader_ctx, block->dynamic_offset);
  8778. if (opcode == WASM_OP_ELSE) {
  8779. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8780. }
  8781. else {
  8782. loader_ctx->frame_offset -= cell;
  8783. loader_ctx->dynamic_offset = block->dynamic_offset;
  8784. PUSH_OFFSET_TYPE(return_types[0]);
  8785. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8786. }
  8787. if (opcode == WASM_OP_ELSE)
  8788. emit_label(opcode);
  8789. }
  8790. return true;
  8791. }
  8792. /* Copy stack top values to block's results which are in dynamic space.
  8793. * The instruction format:
  8794. * Part a: values count
  8795. * Part b: all values total cell num
  8796. * Part c: each value's cell_num, src offset and dst offset
  8797. * Part d: each value's src offset and dst offset
  8798. * Part e: each value's dst offset
  8799. */
  8800. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8801. dynamic_offset = dynamic_offset_org =
  8802. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8803. /* First traversal to get the count of values needed to be copied. */
  8804. for (i = (int32)return_count - 1; i >= 0; i--) {
  8805. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8806. frame_offset -= cells;
  8807. dynamic_offset -= cells;
  8808. if (dynamic_offset != *frame_offset) {
  8809. value_count++;
  8810. total_cel_num += cells;
  8811. }
  8812. }
  8813. if (value_count) {
  8814. uint32 j = 0;
  8815. uint8 *emit_data = NULL, *cells = NULL;
  8816. int16 *src_offsets = NULL;
  8817. uint16 *dst_offsets = NULL;
  8818. uint64 size =
  8819. (uint64)value_count
  8820. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8821. /* Allocate memory for the emit data */
  8822. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8823. return false;
  8824. cells = emit_data;
  8825. src_offsets = (int16 *)(cells + value_count);
  8826. dst_offsets = (uint16 *)(src_offsets + value_count);
  8827. /* insert op_copy before else opcode */
  8828. if (opcode == WASM_OP_ELSE)
  8829. skip_label();
  8830. emit_label(EXT_OP_COPY_STACK_VALUES);
  8831. /* Part a) */
  8832. emit_uint32(loader_ctx, value_count);
  8833. /* Part b) */
  8834. emit_uint32(loader_ctx, total_cel_num);
  8835. /* Second traversal to get each value's cell num, src offset and dst
  8836. * offset. */
  8837. frame_offset = frame_offset_org;
  8838. dynamic_offset = dynamic_offset_org;
  8839. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8840. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8841. frame_offset -= cell;
  8842. dynamic_offset -= cell;
  8843. if (dynamic_offset != *frame_offset) {
  8844. /* cell num */
  8845. cells[j] = cell;
  8846. /* src offset */
  8847. src_offsets[j] = *frame_offset;
  8848. /* dst offset */
  8849. dst_offsets[j] = dynamic_offset;
  8850. j++;
  8851. }
  8852. if (opcode == WASM_OP_ELSE) {
  8853. *frame_offset = dynamic_offset;
  8854. }
  8855. else {
  8856. loader_ctx->frame_offset = frame_offset;
  8857. loader_ctx->dynamic_offset = dynamic_offset;
  8858. if (!(wasm_loader_push_frame_offset(
  8859. loader_ctx, return_types[i], disable_emit,
  8860. operand_offset, error_buf, error_buf_size))) {
  8861. wasm_runtime_free(emit_data);
  8862. goto fail;
  8863. }
  8864. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8865. loader_ctx->frame_offset = frame_offset_org;
  8866. loader_ctx->dynamic_offset = dynamic_offset_org;
  8867. }
  8868. }
  8869. bh_assert(j == value_count);
  8870. /* Emit the cells, src_offsets and dst_offsets */
  8871. for (j = 0; j < value_count; j++)
  8872. emit_byte(loader_ctx, cells[j]);
  8873. for (j = 0; j < value_count; j++)
  8874. emit_operand(loader_ctx, src_offsets[j]);
  8875. for (j = 0; j < value_count; j++)
  8876. emit_operand(loader_ctx, dst_offsets[j]);
  8877. if (opcode == WASM_OP_ELSE)
  8878. emit_label(opcode);
  8879. wasm_runtime_free(emit_data);
  8880. }
  8881. return true;
  8882. fail:
  8883. return false;
  8884. }
  8885. #endif /* WASM_ENABLE_FAST_INTERP */
  8886. #define PUSH_TYPE(type) \
  8887. do { \
  8888. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8889. error_buf_size))) \
  8890. goto fail; \
  8891. } while (0)
  8892. #define POP_TYPE(type) \
  8893. do { \
  8894. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8895. error_buf_size))) \
  8896. goto fail; \
  8897. } while (0)
  8898. #if WASM_ENABLE_GC == 0
  8899. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8900. do { \
  8901. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8902. _start_addr, error_buf, \
  8903. error_buf_size)) \
  8904. goto fail; \
  8905. } while (0)
  8906. #define POP_CSP() \
  8907. do { \
  8908. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8909. goto fail; \
  8910. } while (0)
  8911. #else
  8912. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8913. do { \
  8914. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8915. _start_addr, error_buf, \
  8916. error_buf_size)) \
  8917. goto fail; \
  8918. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8919. error_buf, error_buf_size)) { \
  8920. goto fail; \
  8921. } \
  8922. } while (0)
  8923. #define POP_CSP() \
  8924. do { \
  8925. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8926. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8927. goto fail; \
  8928. } while (0)
  8929. #endif /* end of WASM_ENABLE_GC == 0 */
  8930. #if WASM_ENABLE_GC == 0
  8931. #define GET_LOCAL_REFTYPE() (void)0
  8932. #else
  8933. #define GET_LOCAL_REFTYPE() \
  8934. do { \
  8935. if (wasm_is_type_multi_byte_type(local_type)) { \
  8936. WASMRefType *_ref_type; \
  8937. if (local_idx < param_count) \
  8938. _ref_type = wasm_reftype_map_find( \
  8939. param_reftype_maps, param_reftype_map_count, local_idx); \
  8940. else \
  8941. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8942. local_reftype_map_count, \
  8943. local_idx - param_count); \
  8944. bh_assert(_ref_type); \
  8945. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8946. wasm_reftype_struct_size(_ref_type)); \
  8947. } \
  8948. } while (0)
  8949. #endif /* end of WASM_ENABLE_GC == 0 */
  8950. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8951. do { \
  8952. read_leb_uint32(p, p_end, local_idx); \
  8953. if (local_idx >= param_count + local_count) { \
  8954. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8955. goto fail; \
  8956. } \
  8957. local_type = local_idx < param_count \
  8958. ? param_types[local_idx] \
  8959. : local_types[local_idx - param_count]; \
  8960. local_offset = local_offsets[local_idx]; \
  8961. GET_LOCAL_REFTYPE(); \
  8962. } while (0)
  8963. static bool
  8964. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8965. {
  8966. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8967. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8968. return false;
  8969. }
  8970. return true;
  8971. }
  8972. #define CHECK_MEMORY() \
  8973. do { \
  8974. if (!check_memory(module, error_buf, error_buf_size)) \
  8975. goto fail; \
  8976. } while (0)
  8977. static bool
  8978. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8979. uint32 error_buf_size)
  8980. {
  8981. uint8 mem_access_aligns[] = {
  8982. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8983. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8984. };
  8985. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8986. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8987. set_error_buf(error_buf, error_buf_size,
  8988. "invalid memop flags: alignment must not be larger "
  8989. "than natural");
  8990. return false;
  8991. }
  8992. return true;
  8993. }
  8994. #if WASM_ENABLE_SIMD != 0
  8995. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8996. static bool
  8997. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8998. uint32 error_buf_size)
  8999. {
  9000. uint8 mem_access_aligns[] = {
  9001. 4, /* load */
  9002. 3, 3, 3, 3, 3, 3, /* load and extend */
  9003. 0, 1, 2, 3, /* load and splat */
  9004. 4, /* store */
  9005. };
  9006. uint8 mem_access_aligns_load_lane[] = {
  9007. 0, 1, 2, 3, /* load lane */
  9008. 0, 1, 2, 3, /* store lane */
  9009. 2, 3 /* store zero */
  9010. };
  9011. if (!((opcode <= SIMD_v128_store)
  9012. || (SIMD_v128_load8_lane <= opcode
  9013. && opcode <= SIMD_v128_load64_zero))) {
  9014. set_error_buf(error_buf, error_buf_size,
  9015. "the opcode doesn't include memarg");
  9016. return false;
  9017. }
  9018. if ((opcode <= SIMD_v128_store
  9019. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9020. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9021. && align > mem_access_aligns_load_lane[opcode
  9022. - SIMD_v128_load8_lane])) {
  9023. set_error_buf(error_buf, error_buf_size,
  9024. "invalid memop flags: alignment must not be larger "
  9025. "than natural");
  9026. return false;
  9027. }
  9028. return true;
  9029. }
  9030. static bool
  9031. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9032. uint32 error_buf_size)
  9033. {
  9034. switch (opcode) {
  9035. case SIMD_i8x16_extract_lane_s:
  9036. case SIMD_i8x16_extract_lane_u:
  9037. case SIMD_i8x16_replace_lane:
  9038. if (lane >= 16) {
  9039. goto fail;
  9040. }
  9041. break;
  9042. case SIMD_i16x8_extract_lane_s:
  9043. case SIMD_i16x8_extract_lane_u:
  9044. case SIMD_i16x8_replace_lane:
  9045. if (lane >= 8) {
  9046. goto fail;
  9047. }
  9048. break;
  9049. case SIMD_i32x4_extract_lane:
  9050. case SIMD_i32x4_replace_lane:
  9051. case SIMD_f32x4_extract_lane:
  9052. case SIMD_f32x4_replace_lane:
  9053. if (lane >= 4) {
  9054. goto fail;
  9055. }
  9056. break;
  9057. case SIMD_i64x2_extract_lane:
  9058. case SIMD_i64x2_replace_lane:
  9059. case SIMD_f64x2_extract_lane:
  9060. case SIMD_f64x2_replace_lane:
  9061. if (lane >= 2) {
  9062. goto fail;
  9063. }
  9064. break;
  9065. case SIMD_v128_load8_lane:
  9066. case SIMD_v128_load16_lane:
  9067. case SIMD_v128_load32_lane:
  9068. case SIMD_v128_load64_lane:
  9069. case SIMD_v128_store8_lane:
  9070. case SIMD_v128_store16_lane:
  9071. case SIMD_v128_store32_lane:
  9072. case SIMD_v128_store64_lane:
  9073. case SIMD_v128_load32_zero:
  9074. case SIMD_v128_load64_zero:
  9075. {
  9076. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9077. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9078. goto fail;
  9079. }
  9080. break;
  9081. }
  9082. default:
  9083. goto fail;
  9084. }
  9085. return true;
  9086. fail:
  9087. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9088. return false;
  9089. }
  9090. static bool
  9091. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9092. {
  9093. uint8 i;
  9094. for (i = 0; i != 16; ++i) {
  9095. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9096. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9097. return false;
  9098. }
  9099. }
  9100. return true;
  9101. }
  9102. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9103. #endif /* end of WASM_ENABLE_SIMD */
  9104. #if WASM_ENABLE_SHARED_MEMORY != 0
  9105. static bool
  9106. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9107. uint32 error_buf_size)
  9108. {
  9109. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9110. uint8 mem_access_aligns[] = {
  9111. 2, 3, 0, 1, 0, 1, 2,
  9112. };
  9113. uint8 expect;
  9114. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9115. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9116. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9117. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9118. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9119. }
  9120. else {
  9121. /* 7 opcodes in every group */
  9122. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9123. }
  9124. if (align != expect) {
  9125. set_error_buf(error_buf, error_buf_size,
  9126. "alignment isn't equal to natural");
  9127. return false;
  9128. }
  9129. return true;
  9130. }
  9131. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9132. static bool
  9133. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9134. char *error_buf, uint32 error_buf_size)
  9135. {
  9136. BranchBlock *target_block, *cur_block;
  9137. BlockType *target_block_type;
  9138. uint8 type, *types = NULL, *frame_ref;
  9139. uint32 arity = 0;
  9140. int32 i, available_stack_cell;
  9141. uint16 cell_num;
  9142. #if WASM_ENABLE_GC != 0
  9143. WASMRefTypeMap *frame_reftype_map;
  9144. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9145. WASMRefType *ref_type;
  9146. uint32 reftype_map_count = 0;
  9147. int32 available_reftype_map;
  9148. bool is_type_multi_byte;
  9149. #endif
  9150. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9151. uint8 *frame_ref_after_popped = NULL;
  9152. uint8 frame_ref_tmp[4] = { 0 };
  9153. uint8 *frame_ref_buf = frame_ref_tmp;
  9154. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9155. #if WASM_ENABLE_GC != 0
  9156. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9157. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9158. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9159. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9160. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9161. #endif
  9162. #if WASM_ENABLE_FAST_INTERP != 0
  9163. int16 *frame_offset_old = loader_ctx->frame_offset;
  9164. int16 *frame_offset_after_popped = NULL;
  9165. int16 frame_offset_tmp[4] = { 0 };
  9166. int16 *frame_offset_buf = frame_offset_tmp;
  9167. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9168. #endif
  9169. bool ret = false;
  9170. bh_assert(loader_ctx->csp_num > 0);
  9171. if (loader_ctx->csp_num - 1 < depth) {
  9172. set_error_buf(error_buf, error_buf_size,
  9173. "unknown label, "
  9174. "unexpected end of section or function");
  9175. return false;
  9176. }
  9177. cur_block = loader_ctx->frame_csp - 1;
  9178. target_block = loader_ctx->frame_csp - (depth + 1);
  9179. target_block_type = &target_block->block_type;
  9180. frame_ref = loader_ctx->frame_ref;
  9181. #if WASM_ENABLE_GC != 0
  9182. frame_reftype_map = loader_ctx->frame_reftype_map;
  9183. #endif
  9184. /* Note: loop's arity is different from if and block. loop's arity is
  9185. * its parameter count while if and block arity is result count.
  9186. */
  9187. #if WASM_ENABLE_GC == 0
  9188. if (target_block->label_type == LABEL_TYPE_LOOP)
  9189. arity = block_type_get_param_types(target_block_type, &types);
  9190. else
  9191. arity = block_type_get_result_types(target_block_type, &types);
  9192. #else
  9193. if (target_block->label_type == LABEL_TYPE_LOOP)
  9194. arity = block_type_get_param_types(target_block_type, &types,
  9195. &reftype_maps, &reftype_map_count);
  9196. else
  9197. arity = block_type_get_result_types(target_block_type, &types,
  9198. &reftype_maps, &reftype_map_count);
  9199. #endif
  9200. /* If the stack is in polymorphic state, just clear the stack
  9201. * and then re-push the values to make the stack top values
  9202. * match block type. */
  9203. if (cur_block->is_stack_polymorphic) {
  9204. #if WASM_ENABLE_GC != 0
  9205. int32 j = reftype_map_count - 1;
  9206. #endif
  9207. for (i = (int32)arity - 1; i >= 0; i--) {
  9208. #if WASM_ENABLE_GC != 0
  9209. if (wasm_is_type_multi_byte_type(types[i])) {
  9210. bh_assert(reftype_maps[j].index == i);
  9211. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9212. reftype_maps[j].ref_type,
  9213. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9214. j--;
  9215. }
  9216. #endif
  9217. #if WASM_ENABLE_FAST_INTERP != 0
  9218. POP_OFFSET_TYPE(types[i]);
  9219. #endif
  9220. POP_TYPE(types[i]);
  9221. }
  9222. /* Backup stack data since it may be changed in the below
  9223. push operations, and the stack data may be used when
  9224. checking other target blocks of opcode br_table */
  9225. if (opcode == WASM_OP_BR_TABLE) {
  9226. uint64 total_size;
  9227. frame_ref_after_popped = loader_ctx->frame_ref;
  9228. total_size = (uint64)sizeof(uint8)
  9229. * (frame_ref_old - frame_ref_after_popped);
  9230. if (total_size > sizeof(frame_ref_tmp)
  9231. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9232. error_buf_size))) {
  9233. goto fail;
  9234. }
  9235. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9236. frame_ref_after_popped, (uint32)total_size);
  9237. #if WASM_ENABLE_GC != 0
  9238. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9239. total_size =
  9240. (uint64)sizeof(WASMRefTypeMap)
  9241. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9242. if (total_size > sizeof(frame_reftype_map_tmp)
  9243. && !(frame_reftype_map_buf = loader_malloc(
  9244. total_size, error_buf, error_buf_size))) {
  9245. goto fail;
  9246. }
  9247. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9248. frame_reftype_map_after_popped, (uint32)total_size);
  9249. #endif
  9250. #if WASM_ENABLE_FAST_INTERP != 0
  9251. frame_offset_after_popped = loader_ctx->frame_offset;
  9252. total_size = (uint64)sizeof(int16)
  9253. * (frame_offset_old - frame_offset_after_popped);
  9254. if (total_size > sizeof(frame_offset_tmp)
  9255. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9256. error_buf_size))) {
  9257. goto fail;
  9258. }
  9259. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9260. frame_offset_after_popped, (uint32)total_size);
  9261. #endif
  9262. }
  9263. #if WASM_ENABLE_GC != 0
  9264. j = 0;
  9265. #endif
  9266. for (i = 0; i < (int32)arity; i++) {
  9267. #if WASM_ENABLE_GC != 0
  9268. if (wasm_is_type_multi_byte_type(types[i])) {
  9269. bh_assert(reftype_maps[j].index == i);
  9270. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9271. reftype_maps[j].ref_type,
  9272. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9273. j++;
  9274. }
  9275. #endif
  9276. #if WASM_ENABLE_FAST_INTERP != 0
  9277. bool disable_emit = true;
  9278. int16 operand_offset = 0;
  9279. PUSH_OFFSET_TYPE(types[i]);
  9280. #endif
  9281. PUSH_TYPE(types[i]);
  9282. }
  9283. #if WASM_ENABLE_FAST_INTERP != 0
  9284. emit_br_info(target_block, opcode == WASM_OP_BR);
  9285. #endif
  9286. /* Restore the stack data, note that frame_ref_bottom,
  9287. frame_reftype_map_bottom, frame_offset_bottom may be
  9288. re-allocated in the above push operations */
  9289. if (opcode == WASM_OP_BR_TABLE) {
  9290. uint32 total_size;
  9291. /* The stack operand num should not be smaller than before
  9292. after pop and push operations */
  9293. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9294. loader_ctx->stack_cell_num = stack_cell_num_old;
  9295. loader_ctx->frame_ref =
  9296. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9297. total_size = (uint32)(sizeof(uint8)
  9298. * (frame_ref_old - frame_ref_after_popped));
  9299. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9300. frame_ref_buf, total_size);
  9301. #if WASM_ENABLE_GC != 0
  9302. /* The stack operand num should not be smaller than before
  9303. after pop and push operations */
  9304. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9305. loader_ctx->reftype_map_num = reftype_map_num_old;
  9306. loader_ctx->frame_reftype_map =
  9307. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9308. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9309. * (frame_reftype_map_old
  9310. - frame_reftype_map_after_popped));
  9311. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9312. total_size, frame_reftype_map_buf, total_size);
  9313. #endif
  9314. #if WASM_ENABLE_FAST_INTERP != 0
  9315. loader_ctx->frame_offset =
  9316. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9317. total_size =
  9318. (uint32)(sizeof(int16)
  9319. * (frame_offset_old - frame_offset_after_popped));
  9320. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9321. total_size, frame_offset_buf, total_size);
  9322. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9323. #endif
  9324. }
  9325. ret = true;
  9326. goto cleanup_and_return;
  9327. }
  9328. available_stack_cell =
  9329. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9330. #if WASM_ENABLE_GC != 0
  9331. available_reftype_map =
  9332. (int32)(loader_ctx->reftype_map_num
  9333. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9334. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9335. #endif
  9336. /* Check stack top values match target block type */
  9337. for (i = (int32)arity - 1; i >= 0; i--) {
  9338. type = types[i];
  9339. #if WASM_ENABLE_GC != 0
  9340. ref_type = NULL;
  9341. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9342. if (is_type_multi_byte) {
  9343. bh_assert(reftype_map);
  9344. ref_type = reftype_map->ref_type;
  9345. }
  9346. #endif
  9347. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9348. break;
  9349. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9350. #if WASM_ENABLE_GC != 0
  9351. frame_reftype_map, available_reftype_map,
  9352. #endif
  9353. type,
  9354. #if WASM_ENABLE_GC != 0
  9355. ref_type,
  9356. #endif
  9357. error_buf, error_buf_size)) {
  9358. goto fail;
  9359. }
  9360. cell_num = wasm_value_type_cell_num(types[i]);
  9361. frame_ref -= cell_num;
  9362. available_stack_cell -= cell_num;
  9363. #if WASM_ENABLE_GC != 0
  9364. if (is_type_multi_byte) {
  9365. frame_reftype_map--;
  9366. available_reftype_map--;
  9367. reftype_map--;
  9368. }
  9369. #endif
  9370. }
  9371. #if WASM_ENABLE_FAST_INTERP != 0
  9372. emit_br_info(target_block, opcode == WASM_OP_BR);
  9373. #endif
  9374. ret = true;
  9375. cleanup_and_return:
  9376. fail:
  9377. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9378. wasm_runtime_free(frame_ref_buf);
  9379. #if WASM_ENABLE_GC != 0
  9380. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9381. wasm_runtime_free(frame_reftype_map_buf);
  9382. #endif
  9383. #if WASM_ENABLE_FAST_INTERP != 0
  9384. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9385. wasm_runtime_free(frame_offset_buf);
  9386. #endif
  9387. return ret;
  9388. }
  9389. static BranchBlock *
  9390. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9391. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9392. {
  9393. uint8 *p = *p_buf, *p_end = buf_end;
  9394. BranchBlock *frame_csp_tmp;
  9395. uint32 depth;
  9396. read_leb_uint32(p, p_end, depth);
  9397. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9398. error_buf_size)) {
  9399. goto fail;
  9400. }
  9401. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9402. *p_buf = p;
  9403. return frame_csp_tmp;
  9404. fail:
  9405. return NULL;
  9406. }
  9407. #if WASM_ENABLE_EXCE_HANDLING != 0
  9408. static BranchBlock *
  9409. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9410. uint8 *buf_end, char *error_buf,
  9411. uint32 error_buf_size)
  9412. {
  9413. uint8 *p = *p_buf, *p_end = buf_end;
  9414. BranchBlock *frame_csp_tmp;
  9415. uint32 depth;
  9416. read_leb_uint32(p, p_end, depth);
  9417. /*
  9418. * Note: "delegate 0" means the surrounding block, not the
  9419. * try-delegate block itself.
  9420. *
  9421. * Note: the caller hasn't popped the try-delegate frame yet.
  9422. */
  9423. bh_assert(loader_ctx->csp_num > 0);
  9424. if (loader_ctx->csp_num - 1 <= depth) {
  9425. #if WASM_ENABLE_SPEC_TEST == 0
  9426. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9427. #else
  9428. set_error_buf(error_buf, error_buf_size, "unknown label");
  9429. #endif
  9430. goto fail;
  9431. }
  9432. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9433. #if WASM_ENABLE_FAST_INTERP != 0
  9434. emit_br_info(frame_csp_tmp, false);
  9435. #endif
  9436. *p_buf = p;
  9437. return frame_csp_tmp;
  9438. fail:
  9439. return NULL;
  9440. }
  9441. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9442. static bool
  9443. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9444. char *error_buf, uint32 error_buf_size)
  9445. {
  9446. BlockType *block_type = &block->block_type;
  9447. uint8 *return_types = NULL;
  9448. uint32 return_count = 0;
  9449. int32 available_stack_cell, return_cell_num, i;
  9450. uint8 *frame_ref = NULL;
  9451. #if WASM_ENABLE_GC != 0
  9452. WASMRefTypeMap *frame_reftype_map;
  9453. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9454. WASMRefType *ref_type;
  9455. uint32 param_count, return_reftype_map_count = 0;
  9456. int32 available_reftype_map =
  9457. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9458. #endif
  9459. available_stack_cell =
  9460. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9461. #if WASM_ENABLE_GC == 0
  9462. return_count = block_type_get_result_types(block_type, &return_types);
  9463. #else
  9464. return_count = block_type_get_result_types(block_type, &return_types,
  9465. &return_reftype_maps,
  9466. &return_reftype_map_count);
  9467. param_count =
  9468. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9469. (void)param_count;
  9470. #endif
  9471. return_cell_num =
  9472. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9473. /* If the stack is in polymorphic state, just clear the stack
  9474. * and then re-push the values to make the stack top values
  9475. * match block type. */
  9476. if (block->is_stack_polymorphic) {
  9477. #if WASM_ENABLE_GC != 0
  9478. int32 j = return_reftype_map_count - 1;
  9479. #endif
  9480. for (i = (int32)return_count - 1; i >= 0; i--) {
  9481. #if WASM_ENABLE_GC != 0
  9482. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9483. bh_assert(return_reftype_maps[j].index == i + param_count);
  9484. bh_memcpy_s(
  9485. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9486. return_reftype_maps[j].ref_type,
  9487. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9488. j--;
  9489. }
  9490. #endif
  9491. #if WASM_ENABLE_FAST_INTERP != 0
  9492. POP_OFFSET_TYPE(return_types[i]);
  9493. #endif
  9494. POP_TYPE(return_types[i]);
  9495. }
  9496. /* Check stack is empty */
  9497. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9498. set_error_buf(
  9499. error_buf, error_buf_size,
  9500. "type mismatch: stack size does not match block type");
  9501. goto fail;
  9502. }
  9503. #if WASM_ENABLE_GC != 0
  9504. j = 0;
  9505. #endif
  9506. for (i = 0; i < (int32)return_count; i++) {
  9507. #if WASM_ENABLE_GC != 0
  9508. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9509. bh_assert(return_reftype_maps[j].index == i + param_count);
  9510. bh_memcpy_s(
  9511. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9512. return_reftype_maps[j].ref_type,
  9513. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9514. j++;
  9515. }
  9516. #endif
  9517. #if WASM_ENABLE_FAST_INTERP != 0
  9518. bool disable_emit = true;
  9519. int16 operand_offset = 0;
  9520. PUSH_OFFSET_TYPE(return_types[i]);
  9521. #endif
  9522. PUSH_TYPE(return_types[i]);
  9523. }
  9524. return true;
  9525. }
  9526. if (available_stack_cell != return_cell_num) {
  9527. #if WASM_ENABLE_EXCE_HANDLING != 0
  9528. /* testspec: this error message format is expected by try_catch.wast */
  9529. snprintf(
  9530. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9531. block->label_type == LABEL_TYPE_TRY
  9532. || (block->label_type == LABEL_TYPE_CATCH
  9533. && return_cell_num > 0)
  9534. ? "instruction"
  9535. : "block",
  9536. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9537. " but stack has ",
  9538. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9539. : "");
  9540. goto fail;
  9541. #else
  9542. set_error_buf(error_buf, error_buf_size,
  9543. "type mismatch: stack size does not match block type");
  9544. goto fail;
  9545. #endif
  9546. }
  9547. /* Check stack values match return types */
  9548. frame_ref = loader_ctx->frame_ref;
  9549. #if WASM_ENABLE_GC != 0
  9550. frame_reftype_map = loader_ctx->frame_reftype_map;
  9551. return_reftype_map =
  9552. return_reftype_map_count
  9553. ? return_reftype_maps + return_reftype_map_count - 1
  9554. : NULL;
  9555. #endif
  9556. for (i = (int32)return_count - 1; i >= 0; i--) {
  9557. uint8 type = return_types[i];
  9558. #if WASM_ENABLE_GC != 0
  9559. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9560. ref_type = NULL;
  9561. if (is_type_multi_byte) {
  9562. bh_assert(return_reftype_map);
  9563. ref_type = return_reftype_map->ref_type;
  9564. }
  9565. #endif
  9566. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9567. #if WASM_ENABLE_GC != 0
  9568. frame_reftype_map, available_reftype_map,
  9569. #endif
  9570. type,
  9571. #if WASM_ENABLE_GC != 0
  9572. ref_type,
  9573. #endif
  9574. error_buf, error_buf_size))
  9575. return false;
  9576. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9577. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9578. #if WASM_ENABLE_GC != 0
  9579. if (is_type_multi_byte) {
  9580. frame_reftype_map--;
  9581. available_reftype_map--;
  9582. return_reftype_map--;
  9583. }
  9584. #endif
  9585. }
  9586. return true;
  9587. fail:
  9588. return false;
  9589. }
  9590. #if WASM_ENABLE_FAST_INTERP != 0
  9591. /* Copy parameters to dynamic space.
  9592. * 1) POP original parameter out;
  9593. * 2) Push and copy original values to dynamic space.
  9594. * The copy instruction format:
  9595. * Part a: param count
  9596. * Part b: all param total cell num
  9597. * Part c: each param's cell_num, src offset and dst offset
  9598. * Part d: each param's src offset
  9599. * Part e: each param's dst offset
  9600. */
  9601. static bool
  9602. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9603. uint32 error_buf_size)
  9604. {
  9605. bool ret = false;
  9606. int16 *frame_offset = NULL;
  9607. uint8 *cells = NULL, cell;
  9608. int16 *src_offsets = NULL;
  9609. uint8 *emit_data = NULL;
  9610. uint32 i;
  9611. BranchBlock *block = loader_ctx->frame_csp - 1;
  9612. BlockType *block_type = &block->block_type;
  9613. WASMFuncType *wasm_type = block_type->u.type;
  9614. uint32 param_count = block_type->u.type->param_count;
  9615. int16 condition_offset = 0;
  9616. bool disable_emit = false;
  9617. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9618. int16 operand_offset = 0;
  9619. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9620. bh_assert(size > 0);
  9621. /* For if block, we also need copy the condition operand offset. */
  9622. if (is_if_block)
  9623. size += sizeof(*cells) + sizeof(*src_offsets);
  9624. /* Allocate memory for the emit data */
  9625. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9626. return false;
  9627. cells = emit_data;
  9628. src_offsets = (int16 *)(cells + param_count);
  9629. if (is_if_block)
  9630. condition_offset = *loader_ctx->frame_offset;
  9631. /* POP original parameter out */
  9632. for (i = 0; i < param_count; i++) {
  9633. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9634. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9635. }
  9636. frame_offset = loader_ctx->frame_offset;
  9637. /* Get each param's cell num and src offset */
  9638. for (i = 0; i < param_count; i++) {
  9639. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9640. cells[i] = cell;
  9641. src_offsets[i] = *frame_offset;
  9642. frame_offset += cell;
  9643. }
  9644. /* emit copy instruction */
  9645. emit_label(EXT_OP_COPY_STACK_VALUES);
  9646. /* Part a) */
  9647. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9648. /* Part b) */
  9649. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9650. : wasm_type->param_cell_num);
  9651. /* Part c) */
  9652. for (i = 0; i < param_count; i++)
  9653. emit_byte(loader_ctx, cells[i]);
  9654. if (is_if_block)
  9655. emit_byte(loader_ctx, 1);
  9656. /* Part d) */
  9657. for (i = 0; i < param_count; i++)
  9658. emit_operand(loader_ctx, src_offsets[i]);
  9659. if (is_if_block)
  9660. emit_operand(loader_ctx, condition_offset);
  9661. /* Since the start offset to save the block's params and
  9662. * the start offset to save the block's results may be
  9663. * different, we remember the dynamic offset for loop block
  9664. * so that we can use it to copy the stack operands to the
  9665. * loop block's params in wasm_loader_emit_br_info. */
  9666. if (block->label_type == LABEL_TYPE_LOOP)
  9667. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9668. /* Part e) */
  9669. /* Push to dynamic space. The push will emit the dst offset. */
  9670. for (i = 0; i < param_count; i++)
  9671. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9672. if (is_if_block)
  9673. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9674. ret = true;
  9675. fail:
  9676. /* Free the emit data */
  9677. wasm_runtime_free(emit_data);
  9678. return ret;
  9679. }
  9680. #endif
  9681. #if WASM_ENABLE_GC == 0
  9682. #define RESET_REFTYPE_MAP_STACK() (void)0
  9683. #else
  9684. #define RESET_REFTYPE_MAP_STACK() \
  9685. do { \
  9686. loader_ctx->reftype_map_num = \
  9687. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9688. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9689. + loader_ctx->reftype_map_num; \
  9690. } while (0)
  9691. #endif
  9692. /* reset the stack to the state of before entering the last block */
  9693. #if WASM_ENABLE_FAST_INTERP != 0
  9694. #define RESET_STACK() \
  9695. do { \
  9696. loader_ctx->stack_cell_num = \
  9697. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9698. loader_ctx->frame_ref = \
  9699. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9700. loader_ctx->frame_offset = \
  9701. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9702. RESET_REFTYPE_MAP_STACK(); \
  9703. } while (0)
  9704. #else
  9705. #define RESET_STACK() \
  9706. do { \
  9707. loader_ctx->stack_cell_num = \
  9708. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9709. loader_ctx->frame_ref = \
  9710. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9711. RESET_REFTYPE_MAP_STACK(); \
  9712. } while (0)
  9713. #endif
  9714. /* set current block's stack polymorphic state */
  9715. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9716. do { \
  9717. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9718. _cur_block->is_stack_polymorphic = flag; \
  9719. } while (0)
  9720. #define BLOCK_HAS_PARAM(block_type) \
  9721. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9722. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9723. do { \
  9724. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9725. error_buf_size))) { \
  9726. goto fail; \
  9727. } \
  9728. } while (0)
  9729. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9730. static bool
  9731. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9732. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9733. uint32 error_buf_size)
  9734. {
  9735. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9736. return false;
  9737. }
  9738. if (table_idx < module->import_table_count) {
  9739. if (p_elem_type)
  9740. *p_elem_type =
  9741. module->import_tables[table_idx].u.table.table_type.elem_type;
  9742. #if WASM_ENABLE_GC != 0
  9743. if (p_ref_type)
  9744. *((WASMRefType **)p_ref_type) =
  9745. module->import_tables[table_idx]
  9746. .u.table.table_type.elem_ref_type;
  9747. #endif
  9748. }
  9749. else {
  9750. if (p_elem_type)
  9751. *p_elem_type =
  9752. module->tables[table_idx - module->import_table_count]
  9753. .table_type.elem_type;
  9754. #if WASM_ENABLE_GC != 0
  9755. if (p_ref_type)
  9756. *((WASMRefType **)p_ref_type) =
  9757. module->tables[table_idx - module->import_table_count]
  9758. .table_type.elem_ref_type;
  9759. #endif
  9760. }
  9761. return true;
  9762. }
  9763. static bool
  9764. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9765. uint8 *p_elem_type, void **p_elem_ref_type,
  9766. char *error_buf, uint32 error_buf_size)
  9767. {
  9768. if (table_seg_idx >= module->table_seg_count) {
  9769. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9770. table_seg_idx);
  9771. return false;
  9772. }
  9773. if (p_elem_type) {
  9774. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9775. }
  9776. #if WASM_ENABLE_GC != 0
  9777. if (p_elem_ref_type)
  9778. *((WASMRefType **)p_elem_ref_type) =
  9779. module->table_segments[table_seg_idx].elem_ref_type;
  9780. #endif
  9781. return true;
  9782. }
  9783. #endif
  9784. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9785. const uint8 *
  9786. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9787. uint32 *len)
  9788. {
  9789. WASMCustomSection *section = module->custom_section_list;
  9790. while (section) {
  9791. if ((section->name_len == strlen(name))
  9792. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9793. if (len) {
  9794. *len = section->content_len;
  9795. }
  9796. return section->content_addr;
  9797. }
  9798. section = section->next;
  9799. }
  9800. return NULL;
  9801. }
  9802. #endif
  9803. #if 0
  9804. #define HANDLE_OPCODE(opcode) #opcode
  9805. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9806. #undef HANDLE_OPCODE
  9807. #endif
  9808. #if WASM_ENABLE_FAST_INTERP == 0
  9809. #define pb_read_leb_uint32 read_leb_uint32
  9810. #define pb_read_leb_int32 read_leb_int32
  9811. #define pb_read_leb_int64 read_leb_int64
  9812. #define pb_read_leb_memarg read_leb_memarg
  9813. #define pb_read_leb_mem_offset read_leb_mem_offset
  9814. #else
  9815. /* Read leb without malformed format check */
  9816. static uint64
  9817. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  9818. {
  9819. uint8 *buf = *p_buf;
  9820. uint64 result = 0, byte = 0;
  9821. uint32 shift = 0;
  9822. do {
  9823. byte = *buf++;
  9824. result |= ((byte & 0x7f) << shift);
  9825. shift += 7;
  9826. } while (byte & 0x80);
  9827. if (sign && (shift < maxbits) && (byte & 0x40)) {
  9828. /* Sign extend */
  9829. result |= (~((uint64)0)) << shift;
  9830. }
  9831. *p_buf = buf;
  9832. return result;
  9833. }
  9834. #define pb_read_leb_uint32(p, p_end, res) \
  9835. do { \
  9836. if (!loader_ctx->p_code_compiled) \
  9837. /* Enable format check in the first scan */ \
  9838. read_leb_uint32(p, p_end, res); \
  9839. else \
  9840. /* Disable format check in the second scan */ \
  9841. res = (uint32)read_leb_quick(&p, 32, false); \
  9842. } while (0)
  9843. #define pb_read_leb_int32(p, p_end, res) \
  9844. do { \
  9845. if (!loader_ctx->p_code_compiled) \
  9846. /* Enable format check in the first scan */ \
  9847. read_leb_int32(p, p_end, res); \
  9848. else \
  9849. /* Disable format check in the second scan */ \
  9850. res = (int32)read_leb_quick(&p, 32, true); \
  9851. } while (0)
  9852. #define pb_read_leb_int64(p, p_end, res) \
  9853. do { \
  9854. if (!loader_ctx->p_code_compiled) \
  9855. /* Enable format check in the first scan */ \
  9856. read_leb_int64(p, p_end, res); \
  9857. else \
  9858. /* Disable format check in the second scan */ \
  9859. res = (int64)read_leb_quick(&p, 64, true); \
  9860. } while (0)
  9861. #if WASM_ENABLE_MULTI_MEMORY != 0
  9862. #define pb_read_leb_memarg read_leb_memarg
  9863. #else
  9864. #define pb_read_leb_memarg pb_read_leb_uint32
  9865. #endif
  9866. #if WASM_ENABLE_MEMORY64 != 0
  9867. #define pb_read_leb_mem_offset read_leb_mem_offset
  9868. #else
  9869. #define pb_read_leb_mem_offset pb_read_leb_uint32
  9870. #endif
  9871. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9872. static bool
  9873. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9874. uint32 cur_func_idx, char *error_buf,
  9875. uint32 error_buf_size)
  9876. {
  9877. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9878. uint32 param_count, local_count, global_count;
  9879. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  9880. table_elem_idx_type;
  9881. BlockType func_block_type;
  9882. uint16 *local_offsets, local_offset;
  9883. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9884. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9885. mem_offset_t mem_offset;
  9886. int32 i32_const = 0;
  9887. int64 i64_const;
  9888. uint8 opcode;
  9889. bool return_value = false;
  9890. WASMLoaderContext *loader_ctx;
  9891. BranchBlock *frame_csp_tmp;
  9892. #if WASM_ENABLE_GC != 0
  9893. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9894. uint32 param_reftype_map_count, local_reftype_map_count;
  9895. int32 heap_type;
  9896. WASMRefType wasm_ref_type = { 0 };
  9897. bool need_ref_type_map;
  9898. #endif
  9899. #if WASM_ENABLE_FAST_INTERP != 0
  9900. int16 operand_offset = 0;
  9901. uint8 last_op = 0;
  9902. bool disable_emit, preserve_local = false, if_condition_available = true;
  9903. float32 f32_const;
  9904. float64 f64_const;
  9905. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9906. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9907. #endif
  9908. #if WASM_ENABLE_MEMORY64 != 0
  9909. bool is_memory64 = has_module_memory64(module);
  9910. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9911. #else
  9912. mem_offset_type = VALUE_TYPE_I32;
  9913. table_elem_idx_type = VALUE_TYPE_I32;
  9914. #endif
  9915. uint32 memidx;
  9916. global_count = module->import_global_count + module->global_count;
  9917. param_count = func->func_type->param_count;
  9918. param_types = func->func_type->types;
  9919. func_block_type.is_value_type = false;
  9920. func_block_type.u.type = func->func_type;
  9921. local_count = func->local_count;
  9922. local_types = func->local_types;
  9923. local_offsets = func->local_offsets;
  9924. #if WASM_ENABLE_GC != 0
  9925. param_reftype_maps = func->func_type->ref_type_maps;
  9926. param_reftype_map_count = func->func_type->ref_type_map_count;
  9927. local_reftype_maps = func->local_ref_type_maps;
  9928. local_reftype_map_count = func->local_ref_type_map_count;
  9929. #endif
  9930. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9931. goto fail;
  9932. }
  9933. #if WASM_ENABLE_GC != 0
  9934. loader_ctx->module = module;
  9935. loader_ctx->ref_type_set = module->ref_type_set;
  9936. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9937. #endif
  9938. #if WASM_ENABLE_FAST_INTERP != 0
  9939. /* For the first traverse, the initial value of preserved_local_offset has
  9940. * not been determined, we use the INT16_MAX to represent that a slot has
  9941. * been copied to preserve space. For second traverse, this field will be
  9942. * set to the appropriate value in wasm_loader_ctx_reinit.
  9943. * This is for Issue #1230,
  9944. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9945. * drop opcodes need to know which slots are preserved, so those slots will
  9946. * not be treated as dynamically allocated slots */
  9947. loader_ctx->preserved_local_offset = INT16_MAX;
  9948. re_scan:
  9949. if (loader_ctx->code_compiled_size > 0) {
  9950. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9951. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9952. goto fail;
  9953. }
  9954. p = func->code;
  9955. func->code_compiled = loader_ctx->p_code_compiled;
  9956. func->code_compiled_size = loader_ctx->code_compiled_size;
  9957. if (loader_ctx->i64_const_num > 0) {
  9958. int64 *i64_consts_old = loader_ctx->i64_consts;
  9959. /* Sort the i64 consts */
  9960. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  9961. cmp_i64_const);
  9962. /* Remove the duplicated i64 consts */
  9963. uint32 k = 1;
  9964. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  9965. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  9966. i64_consts_old[k++] = i64_consts_old[i];
  9967. }
  9968. }
  9969. if (k < loader_ctx->i64_const_num) {
  9970. int64 *i64_consts_new;
  9971. /* Try to reallocate memory with a smaller size */
  9972. if ((i64_consts_new =
  9973. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  9974. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  9975. i64_consts_old, (uint32)sizeof(int64) * k);
  9976. /* Free the old memory */
  9977. wasm_runtime_free(i64_consts_old);
  9978. loader_ctx->i64_consts = i64_consts_new;
  9979. loader_ctx->i64_const_max_num = k;
  9980. }
  9981. loader_ctx->i64_const_num = k;
  9982. }
  9983. }
  9984. if (loader_ctx->i32_const_num > 0) {
  9985. int32 *i32_consts_old = loader_ctx->i32_consts;
  9986. /* Sort the i32 consts */
  9987. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  9988. cmp_i32_const);
  9989. /* Remove the duplicated i32 consts */
  9990. uint32 k = 1;
  9991. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  9992. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  9993. i32_consts_old[k++] = i32_consts_old[i];
  9994. }
  9995. }
  9996. if (k < loader_ctx->i32_const_num) {
  9997. int32 *i32_consts_new;
  9998. /* Try to reallocate memory with a smaller size */
  9999. if ((i32_consts_new =
  10000. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10001. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10002. i32_consts_old, (uint32)sizeof(int32) * k);
  10003. /* Free the old memory */
  10004. wasm_runtime_free(i32_consts_old);
  10005. loader_ctx->i32_consts = i32_consts_new;
  10006. loader_ctx->i32_const_max_num = k;
  10007. }
  10008. loader_ctx->i32_const_num = k;
  10009. }
  10010. }
  10011. }
  10012. #endif
  10013. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10014. while (p < p_end) {
  10015. opcode = *p++;
  10016. #if WASM_ENABLE_FAST_INTERP != 0
  10017. p_org = p;
  10018. disable_emit = false;
  10019. emit_label(opcode);
  10020. #endif
  10021. switch (opcode) {
  10022. case WASM_OP_UNREACHABLE:
  10023. RESET_STACK();
  10024. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10025. break;
  10026. case WASM_OP_NOP:
  10027. #if WASM_ENABLE_FAST_INTERP != 0
  10028. skip_label();
  10029. #endif
  10030. break;
  10031. case WASM_OP_IF:
  10032. {
  10033. #if WASM_ENABLE_FAST_INTERP != 0
  10034. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10035. int32 available_stack_cell =
  10036. (int32)(loader_ctx->stack_cell_num
  10037. - parent_block->stack_cell_num);
  10038. if (available_stack_cell <= 0
  10039. && parent_block->is_stack_polymorphic)
  10040. if_condition_available = false;
  10041. else
  10042. if_condition_available = true;
  10043. PRESERVE_LOCAL_FOR_BLOCK();
  10044. #endif
  10045. #if WASM_ENABLE_GC == 0
  10046. POP_I32();
  10047. #endif
  10048. goto handle_op_block_and_loop;
  10049. }
  10050. case WASM_OP_BLOCK:
  10051. case WASM_OP_LOOP:
  10052. #if WASM_ENABLE_EXCE_HANDLING != 0
  10053. case WASM_OP_TRY:
  10054. if (opcode == WASM_OP_TRY) {
  10055. /*
  10056. * keep track of exception handlers to account for
  10057. * memory allocation
  10058. */
  10059. func->exception_handler_count++;
  10060. /*
  10061. * try is a block
  10062. * do nothing special, but execution continues to
  10063. * to handle_op_block_and_loop,
  10064. * and that be pushes the csp
  10065. */
  10066. }
  10067. #endif
  10068. #if WASM_ENABLE_FAST_INTERP != 0
  10069. PRESERVE_LOCAL_FOR_BLOCK();
  10070. #endif
  10071. handle_op_block_and_loop:
  10072. {
  10073. uint8 value_type;
  10074. BlockType block_type;
  10075. #if WASM_ENABLE_FAST_INTERP != 0
  10076. uint32 available_params = 0;
  10077. #endif
  10078. CHECK_BUF(p, p_end, 1);
  10079. value_type = read_uint8(p);
  10080. if (is_byte_a_type(value_type)) {
  10081. /* If the first byte is one of these special values:
  10082. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10083. * the single return value. */
  10084. block_type.is_value_type = true;
  10085. block_type.u.value_type.type = value_type;
  10086. #if WASM_ENABLE_WAMR_COMPILER != 0
  10087. if (value_type == VALUE_TYPE_V128)
  10088. module->is_simd_used = true;
  10089. else if (value_type == VALUE_TYPE_FUNCREF
  10090. || value_type == VALUE_TYPE_EXTERNREF)
  10091. module->is_ref_types_used = true;
  10092. #endif
  10093. #if WASM_ENABLE_GC != 0
  10094. if (value_type != VALUE_TYPE_VOID) {
  10095. p_org = p;
  10096. p--;
  10097. if (!resolve_value_type((const uint8 **)&p, p_end,
  10098. module, module->type_count,
  10099. &need_ref_type_map,
  10100. &wasm_ref_type, false,
  10101. error_buf, error_buf_size)) {
  10102. goto fail;
  10103. }
  10104. if (need_ref_type_map) {
  10105. block_type.u.value_type.ref_type_map.index = 0;
  10106. if (!(block_type.u.value_type.ref_type_map
  10107. .ref_type = reftype_set_insert(
  10108. module->ref_type_set, &wasm_ref_type,
  10109. error_buf, error_buf_size))) {
  10110. goto fail;
  10111. }
  10112. }
  10113. /* Set again as the type might be changed, e.g.
  10114. (ref null any) to anyref */
  10115. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10116. #if WASM_ENABLE_FAST_INTERP == 0
  10117. while (p_org < p) {
  10118. #if WASM_ENABLE_DEBUG_INTERP != 0
  10119. if (!record_fast_op(module, p_org, *p_org,
  10120. error_buf, error_buf_size)) {
  10121. goto fail;
  10122. }
  10123. #endif
  10124. /* Ignore extra bytes for interpreter */
  10125. *p_org++ = WASM_OP_NOP;
  10126. }
  10127. #endif
  10128. }
  10129. #endif /* end of WASM_ENABLE_GC != 0 */
  10130. }
  10131. else {
  10132. int32 type_index;
  10133. /* Resolve the leb128 encoded type index as block type */
  10134. p--;
  10135. p_org = p - 1;
  10136. pb_read_leb_int32(p, p_end, type_index);
  10137. if ((uint32)type_index >= module->type_count) {
  10138. set_error_buf(error_buf, error_buf_size,
  10139. "unknown type");
  10140. goto fail;
  10141. }
  10142. block_type.is_value_type = false;
  10143. block_type.u.type =
  10144. (WASMFuncType *)module->types[type_index];
  10145. #if WASM_ENABLE_FAST_INTERP == 0
  10146. /* If block use type index as block type, change the opcode
  10147. * to new extended opcode so that interpreter can resolve
  10148. * the block quickly.
  10149. */
  10150. #if WASM_ENABLE_DEBUG_INTERP != 0
  10151. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10152. error_buf_size)) {
  10153. goto fail;
  10154. }
  10155. #endif
  10156. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10157. #endif
  10158. }
  10159. #if WASM_ENABLE_GC != 0
  10160. if (opcode == WASM_OP_IF) {
  10161. POP_I32();
  10162. }
  10163. #endif
  10164. /* Pop block parameters from stack */
  10165. if (BLOCK_HAS_PARAM(block_type)) {
  10166. WASMFuncType *wasm_type = block_type.u.type;
  10167. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10168. #if WASM_ENABLE_FAST_INTERP != 0
  10169. uint32 cell_num;
  10170. available_params = block_type.u.type->param_count;
  10171. #endif
  10172. for (i = 0; i < block_type.u.type->param_count; i++) {
  10173. int32 available_stack_cell =
  10174. (int32)(loader_ctx->stack_cell_num
  10175. - cur_block->stack_cell_num);
  10176. if (available_stack_cell <= 0
  10177. && cur_block->is_stack_polymorphic) {
  10178. #if WASM_ENABLE_FAST_INTERP != 0
  10179. available_params = i;
  10180. #endif
  10181. break;
  10182. }
  10183. POP_TYPE(
  10184. wasm_type->types[wasm_type->param_count - i - 1]);
  10185. #if WASM_ENABLE_FAST_INTERP != 0
  10186. /* decrease the frame_offset pointer accordingly to keep
  10187. * consistent with frame_ref stack */
  10188. cell_num = wasm_value_type_cell_num(
  10189. wasm_type->types[wasm_type->param_count - i - 1]);
  10190. loader_ctx->frame_offset -= cell_num;
  10191. #endif
  10192. }
  10193. }
  10194. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10195. block_type, p);
  10196. /* Pass parameters to block */
  10197. if (BLOCK_HAS_PARAM(block_type)) {
  10198. WASMFuncType *func_type = block_type.u.type;
  10199. #if WASM_ENABLE_GC != 0
  10200. WASMRefType *ref_type;
  10201. uint32 j = 0;
  10202. #endif
  10203. for (i = 0; i < func_type->param_count; i++) {
  10204. #if WASM_ENABLE_FAST_INTERP != 0
  10205. uint32 cell_num =
  10206. wasm_value_type_cell_num(func_type->types[i]);
  10207. if (i >= available_params) {
  10208. /* make sure enough space */
  10209. if (loader_ctx->p_code_compiled == NULL) {
  10210. loader_ctx->frame_offset += cell_num;
  10211. if (!check_offset_push(loader_ctx, error_buf,
  10212. error_buf_size))
  10213. goto fail;
  10214. /* for following dummy value assignemnt */
  10215. loader_ctx->frame_offset -= cell_num;
  10216. }
  10217. /* If there isn't enough data on stack, push a dummy
  10218. * offset to keep the stack consistent with
  10219. * frame_ref.
  10220. * Since the stack is already in polymorphic state,
  10221. * the opcode will not be executed, so the dummy
  10222. * offset won't cause any error */
  10223. for (uint32 n = 0; n < cell_num; n++) {
  10224. *loader_ctx->frame_offset++ = 0;
  10225. }
  10226. }
  10227. else {
  10228. loader_ctx->frame_offset += cell_num;
  10229. }
  10230. #endif
  10231. #if WASM_ENABLE_GC != 0
  10232. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10233. bh_assert(func_type->ref_type_maps[j].index == i);
  10234. ref_type = func_type->ref_type_maps[j].ref_type;
  10235. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10236. ref_type,
  10237. wasm_reftype_struct_size(ref_type));
  10238. j++;
  10239. }
  10240. #endif
  10241. PUSH_TYPE(func_type->types[i]);
  10242. }
  10243. }
  10244. #if WASM_ENABLE_FAST_INTERP != 0
  10245. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10246. skip_label();
  10247. if (BLOCK_HAS_PARAM(block_type)) {
  10248. /* Make sure params are in dynamic space */
  10249. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10250. error_buf_size))
  10251. goto fail;
  10252. }
  10253. if (opcode == WASM_OP_LOOP) {
  10254. (loader_ctx->frame_csp - 1)->code_compiled =
  10255. loader_ctx->p_code_compiled;
  10256. }
  10257. }
  10258. #if WASM_ENABLE_EXCE_HANDLING != 0
  10259. else if (opcode == WASM_OP_TRY) {
  10260. skip_label();
  10261. }
  10262. #endif
  10263. else if (opcode == WASM_OP_IF) {
  10264. BranchBlock *block = loader_ctx->frame_csp - 1;
  10265. /* If block has parameters, we should make sure they are in
  10266. * dynamic space. Otherwise, when else branch is missing,
  10267. * the later opcode may consume incorrect operand offset.
  10268. * Spec case:
  10269. * (func (export "params-id") (param i32) (result i32)
  10270. * (i32.const 1)
  10271. * (i32.const 2)
  10272. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10273. * (then)) (i32.add)
  10274. * )
  10275. *
  10276. * So we should emit a copy instruction before the if.
  10277. *
  10278. * And we also need to save the parameter offsets and
  10279. * recover them before entering else branch.
  10280. *
  10281. */
  10282. if (BLOCK_HAS_PARAM(block_type)) {
  10283. uint64 size;
  10284. /* In polymorphic state, there may be no if condition on
  10285. * the stack, so the offset may not emitted */
  10286. if (if_condition_available) {
  10287. /* skip the if condition operand offset */
  10288. wasm_loader_emit_backspace(loader_ctx,
  10289. sizeof(int16));
  10290. }
  10291. /* skip the if label */
  10292. skip_label();
  10293. /* Emit a copy instruction */
  10294. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10295. error_buf_size))
  10296. goto fail;
  10297. /* Emit the if instruction */
  10298. emit_label(opcode);
  10299. /* Emit the new condition operand offset */
  10300. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10301. /* Save top param_count values of frame_offset stack, so
  10302. * that we can recover it before executing else branch
  10303. */
  10304. size = sizeof(int16)
  10305. * (uint64)block_type.u.type->param_cell_num;
  10306. if (!(block->param_frame_offsets = loader_malloc(
  10307. size, error_buf, error_buf_size)))
  10308. goto fail;
  10309. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10310. loader_ctx->frame_offset
  10311. - size / sizeof(int16),
  10312. (uint32)size);
  10313. }
  10314. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10315. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10316. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10317. }
  10318. #endif
  10319. break;
  10320. }
  10321. #if WASM_ENABLE_EXCE_HANDLING != 0
  10322. case WASM_OP_THROW:
  10323. {
  10324. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10325. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10326. uint8 label_type = cur_block->label_type;
  10327. uint32 tag_index = 0;
  10328. pb_read_leb_int32(p, p_end, tag_index);
  10329. /* check validity of tag_index against module->tag_count */
  10330. /* check tag index is within the tag index space */
  10331. if (tag_index >= module->import_tag_count + module->tag_count) {
  10332. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10333. tag_index);
  10334. goto fail;
  10335. }
  10336. /* the tag_type is stored in either the WASMTag (section tags)
  10337. * or WASMTagImport (import tag) */
  10338. WASMFuncType *tag_type = NULL;
  10339. if (tag_index < module->import_tag_count) {
  10340. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10341. }
  10342. else {
  10343. tag_type =
  10344. module->tags[tag_index - module->import_tag_count]
  10345. ->tag_type;
  10346. }
  10347. if (tag_type->result_count != 0) {
  10348. set_error_buf(error_buf, error_buf_size,
  10349. "tag type signature does not return void");
  10350. goto fail;
  10351. }
  10352. int32 available_stack_cell =
  10353. (int32)(loader_ctx->stack_cell_num
  10354. - cur_block->stack_cell_num);
  10355. int32 tti;
  10356. /* Check stack values match return types by comparing tag param
  10357. * types with stack cells */
  10358. uint8 *frame_ref = loader_ctx->frame_ref;
  10359. #if WASM_ENABLE_GC != 0
  10360. WASMRefTypeMap *frame_reftype_map =
  10361. loader_ctx->frame_reftype_map;
  10362. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10363. /* Temporarily set these values since they may be used in
  10364. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10365. param_reftype_maps = tag_type->ref_type_maps;
  10366. /* For tag_type function, it shouldn't have result_count = 0 */
  10367. param_reftype_map_count = tag_type->ref_type_map_count;
  10368. param_count = tag_type->param_count;
  10369. #endif
  10370. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10371. #if WASM_ENABLE_GC != 0
  10372. local_type = tag_type->types[tti];
  10373. local_idx = tti;
  10374. /* Get the wasm_ref_type if the local_type is multibyte
  10375. type */
  10376. GET_LOCAL_REFTYPE();
  10377. #endif
  10378. if (!check_stack_top_values(
  10379. loader_ctx, frame_ref, available_stack_cell,
  10380. #if WASM_ENABLE_GC != 0
  10381. frame_reftype_map, frame_reftype_map_num,
  10382. #endif
  10383. tag_type->types[tti],
  10384. #if WASM_ENABLE_GC != 0
  10385. &wasm_ref_type,
  10386. #endif
  10387. error_buf, error_buf_size)) {
  10388. snprintf(error_buf, error_buf_size,
  10389. "type mismatch: instruction requires [%s] but "
  10390. "stack has [%s]",
  10391. tag_type->param_count > 0
  10392. ? type2str(tag_type->types[tti])
  10393. : "",
  10394. available_stack_cell > 0
  10395. ? type2str(*(loader_ctx->frame_ref - 1))
  10396. : "");
  10397. goto fail;
  10398. }
  10399. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10400. available_stack_cell -=
  10401. wasm_value_type_cell_num(tag_type->types[tti]);
  10402. }
  10403. #if WASM_ENABLE_GC != 0
  10404. /* Restore the values */
  10405. param_reftype_maps = func->func_type->ref_type_maps;
  10406. param_reftype_map_count = func->func_type->ref_type_map_count;
  10407. param_count = func->func_type->param_count;
  10408. #endif
  10409. /* throw is stack polymorphic */
  10410. (void)label_type;
  10411. RESET_STACK();
  10412. break;
  10413. }
  10414. case WASM_OP_RETHROW:
  10415. {
  10416. /* must be done before checking branch block */
  10417. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10418. /* check the target catching block: LABEL_TYPE_CATCH */
  10419. if (!(frame_csp_tmp =
  10420. check_branch_block(loader_ctx, &p, p_end, opcode,
  10421. error_buf, error_buf_size)))
  10422. goto fail;
  10423. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10424. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10425. /* trap according to spectest (rethrow.wast) */
  10426. set_error_buf(error_buf, error_buf_size,
  10427. "invalid rethrow label");
  10428. goto fail;
  10429. }
  10430. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10431. uint8 label_type = cur_block->label_type;
  10432. (void)label_type;
  10433. /* rethrow is stack polymorphic */
  10434. RESET_STACK();
  10435. break;
  10436. }
  10437. case WASM_OP_DELEGATE:
  10438. {
  10439. /* check target block is valid */
  10440. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10441. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10442. goto fail;
  10443. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10444. uint8 label_type = cur_block->label_type;
  10445. (void)label_type;
  10446. /* DELEGATE ends the block */
  10447. POP_CSP();
  10448. break;
  10449. }
  10450. case WASM_OP_CATCH:
  10451. {
  10452. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10453. uint8 label_type = cur_block->label_type;
  10454. uint32 tag_index = 0;
  10455. pb_read_leb_int32(p, p_end, tag_index);
  10456. /* check validity of tag_index against module->tag_count */
  10457. /* check tag index is within the tag index space */
  10458. if (tag_index >= module->import_tag_count + module->tag_count) {
  10459. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10460. __FUNCTION__);
  10461. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10462. goto fail;
  10463. }
  10464. /* the tag_type is stored in either the WASMTag (section tags)
  10465. * or WASMTagImport (import tag) */
  10466. WASMFuncType *func_type = NULL;
  10467. if (tag_index < module->import_tag_count) {
  10468. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10469. }
  10470. else {
  10471. func_type =
  10472. module->tags[tag_index - module->import_tag_count]
  10473. ->tag_type;
  10474. }
  10475. if (func_type->result_count != 0) {
  10476. set_error_buf(error_buf, error_buf_size,
  10477. "tag type signature does not return void");
  10478. goto fail;
  10479. }
  10480. /* check validity of current label (expect LABEL_TYPE_TRY or
  10481. * LABEL_TYPE_CATCH) */
  10482. if ((LABEL_TYPE_CATCH != label_type)
  10483. && (LABEL_TYPE_TRY != label_type)) {
  10484. set_error_buf(error_buf, error_buf_size,
  10485. "Unexpected block sequence encountered.");
  10486. goto fail;
  10487. }
  10488. /*
  10489. * replace frame_csp by LABEL_TYPE_CATCH
  10490. */
  10491. cur_block->label_type = LABEL_TYPE_CATCH;
  10492. /* RESET_STACK removes the values pushed in TRY or pervious
  10493. * CATCH Blocks */
  10494. RESET_STACK();
  10495. #if WASM_ENABLE_GC != 0
  10496. WASMRefType *ref_type;
  10497. uint32 j = 0;
  10498. #endif
  10499. /* push types on the stack according to caught type */
  10500. for (i = 0; i < func_type->param_count; i++) {
  10501. #if WASM_ENABLE_GC != 0
  10502. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10503. bh_assert(func_type->ref_type_maps[j].index == i);
  10504. ref_type = func_type->ref_type_maps[j].ref_type;
  10505. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10506. ref_type,
  10507. wasm_reftype_struct_size(ref_type));
  10508. j++;
  10509. }
  10510. #endif
  10511. PUSH_TYPE(func_type->types[i]);
  10512. }
  10513. break;
  10514. }
  10515. case WASM_OP_CATCH_ALL:
  10516. {
  10517. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10518. /* expecting a TRY or CATCH, anything else will be considered an
  10519. * error */
  10520. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10521. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10522. set_error_buf(error_buf, error_buf_size,
  10523. "Unexpected block sequence encountered.");
  10524. goto fail;
  10525. }
  10526. /* no immediates */
  10527. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10528. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10529. /* RESET_STACK removes the values pushed in TRY or pervious
  10530. * CATCH Blocks */
  10531. RESET_STACK();
  10532. /* catch_all has no tagtype and therefore no parameters */
  10533. break;
  10534. }
  10535. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10536. case WASM_OP_ELSE:
  10537. handle_op_else:
  10538. {
  10539. BranchBlock *block = NULL;
  10540. BlockType block_type;
  10541. if (loader_ctx->csp_num < 2
  10542. /* the matched if isn't found */
  10543. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10544. /* duplicated else is found */
  10545. || (loader_ctx->frame_csp - 1)->else_addr) {
  10546. set_error_buf(
  10547. error_buf, error_buf_size,
  10548. "opcode else found without matched opcode if");
  10549. goto fail;
  10550. }
  10551. block = loader_ctx->frame_csp - 1;
  10552. /* check whether if branch's stack matches its result type */
  10553. if (!check_block_stack(loader_ctx, block, error_buf,
  10554. error_buf_size))
  10555. goto fail;
  10556. block->else_addr = p - 1;
  10557. block_type = block->block_type;
  10558. #if WASM_ENABLE_GC != 0
  10559. if (!wasm_loader_init_local_use_masks(
  10560. loader_ctx, local_count, error_buf, error_buf_size)) {
  10561. goto fail;
  10562. }
  10563. #endif
  10564. #if WASM_ENABLE_FAST_INTERP != 0
  10565. /* if the result of if branch is in local or const area, add a
  10566. * copy op */
  10567. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10568. error_buf, error_buf_size)) {
  10569. goto fail;
  10570. }
  10571. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10572. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10573. #endif
  10574. RESET_STACK();
  10575. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10576. /* Pass parameters to if-false branch */
  10577. if (BLOCK_HAS_PARAM(block_type)) {
  10578. for (i = 0; i < block_type.u.type->param_count; i++)
  10579. PUSH_TYPE(block_type.u.type->types[i]);
  10580. }
  10581. #if WASM_ENABLE_FAST_INTERP != 0
  10582. /* Recover top param_count values of frame_offset stack */
  10583. if (BLOCK_HAS_PARAM((block_type))) {
  10584. uint32 size;
  10585. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10586. bh_memcpy_s(loader_ctx->frame_offset, size,
  10587. block->param_frame_offsets, size);
  10588. loader_ctx->frame_offset += (size / sizeof(int16));
  10589. }
  10590. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10591. #endif
  10592. break;
  10593. }
  10594. case WASM_OP_END:
  10595. {
  10596. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10597. /* check whether block stack matches its result type */
  10598. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10599. error_buf_size))
  10600. goto fail;
  10601. /* if there is no else branch, make a virtual else opcode for
  10602. easier integrity check and to copy the correct results to
  10603. the block return address for fast-interp mode:
  10604. change if block from `if ... end` to `if ... else end` */
  10605. if (cur_block->label_type == LABEL_TYPE_IF
  10606. && !cur_block->else_addr) {
  10607. opcode = WASM_OP_ELSE;
  10608. p--;
  10609. #if WASM_ENABLE_FAST_INTERP != 0
  10610. p_org = p;
  10611. skip_label();
  10612. disable_emit = false;
  10613. emit_label(opcode);
  10614. #endif
  10615. goto handle_op_else;
  10616. }
  10617. POP_CSP();
  10618. #if WASM_ENABLE_FAST_INTERP != 0
  10619. skip_label();
  10620. /* copy the result to the block return address */
  10621. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10622. error_buf, error_buf_size)) {
  10623. /* it could be tmp frame_csp allocated from opcode like
  10624. * OP_BR and not counted in loader_ctx->csp_num, it won't
  10625. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  10626. * to free the loader_ctx->frame_csp if fails */
  10627. free_label_patch_list(loader_ctx->frame_csp);
  10628. goto fail;
  10629. }
  10630. apply_label_patch(loader_ctx, 0, PATCH_END);
  10631. free_label_patch_list(loader_ctx->frame_csp);
  10632. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10633. int32 idx;
  10634. uint8 ret_type;
  10635. emit_label(WASM_OP_RETURN);
  10636. for (idx = (int32)func->func_type->result_count - 1;
  10637. idx >= 0; idx--) {
  10638. ret_type = *(func->func_type->types
  10639. + func->func_type->param_count + idx);
  10640. POP_OFFSET_TYPE(ret_type);
  10641. }
  10642. }
  10643. #endif
  10644. if (loader_ctx->csp_num > 0) {
  10645. loader_ctx->frame_csp->end_addr = p - 1;
  10646. }
  10647. else {
  10648. /* end of function block, function will return */
  10649. if (p < p_end) {
  10650. set_error_buf(error_buf, error_buf_size,
  10651. "section size mismatch");
  10652. goto fail;
  10653. }
  10654. }
  10655. break;
  10656. }
  10657. case WASM_OP_BR:
  10658. {
  10659. if (!(frame_csp_tmp =
  10660. check_branch_block(loader_ctx, &p, p_end, opcode,
  10661. error_buf, error_buf_size)))
  10662. goto fail;
  10663. RESET_STACK();
  10664. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10665. break;
  10666. }
  10667. case WASM_OP_BR_IF:
  10668. {
  10669. POP_I32();
  10670. if (!(frame_csp_tmp =
  10671. check_branch_block(loader_ctx, &p, p_end, opcode,
  10672. error_buf, error_buf_size)))
  10673. goto fail;
  10674. break;
  10675. }
  10676. case WASM_OP_BR_TABLE:
  10677. {
  10678. uint32 depth = 0, default_arity, arity = 0;
  10679. BranchBlock *target_block;
  10680. BlockType *target_block_type;
  10681. #if WASM_ENABLE_FAST_INTERP == 0
  10682. BrTableCache *br_table_cache = NULL;
  10683. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10684. uint32 j;
  10685. #endif
  10686. pb_read_leb_uint32(p, p_end, count);
  10687. #if WASM_ENABLE_FAST_INTERP != 0
  10688. emit_uint32(loader_ctx, count);
  10689. #endif
  10690. POP_I32();
  10691. /* Get each depth and check it */
  10692. p_org = p;
  10693. for (i = 0; i <= count; i++) {
  10694. pb_read_leb_uint32(p, p_end, depth);
  10695. bh_assert(loader_ctx->csp_num > 0);
  10696. if (loader_ctx->csp_num - 1 < depth) {
  10697. set_error_buf(error_buf, error_buf_size,
  10698. "unknown label, "
  10699. "unexpected end of section or function");
  10700. goto fail;
  10701. }
  10702. }
  10703. p = p_org;
  10704. /* Get the default block's arity */
  10705. target_block = loader_ctx->frame_csp - (depth + 1);
  10706. target_block_type = &target_block->block_type;
  10707. default_arity = block_type_get_arity(target_block_type,
  10708. target_block->label_type);
  10709. #if WASM_ENABLE_FAST_INTERP == 0
  10710. p_depth_begin = p_depth = p;
  10711. #endif
  10712. for (i = 0; i <= count; i++) {
  10713. p_org = p;
  10714. pb_read_leb_uint32(p, p_end, depth);
  10715. p = p_org;
  10716. /* Get the target block's arity and check it */
  10717. target_block = loader_ctx->frame_csp - (depth + 1);
  10718. target_block_type = &target_block->block_type;
  10719. arity = block_type_get_arity(target_block_type,
  10720. target_block->label_type);
  10721. if (arity != default_arity) {
  10722. set_error_buf(error_buf, error_buf_size,
  10723. "type mismatch: br_table targets must "
  10724. "all use same result type");
  10725. goto fail;
  10726. }
  10727. if (!(frame_csp_tmp =
  10728. check_branch_block(loader_ctx, &p, p_end, opcode,
  10729. error_buf, error_buf_size))) {
  10730. goto fail;
  10731. }
  10732. #if WASM_ENABLE_FAST_INTERP == 0
  10733. if (br_table_cache) {
  10734. br_table_cache->br_depths[i] = depth;
  10735. }
  10736. else {
  10737. if (depth > 255) {
  10738. /* The depth cannot be stored in one byte,
  10739. create br_table cache to store each depth */
  10740. #if WASM_ENABLE_DEBUG_INTERP != 0
  10741. if (!record_fast_op(module, p_opcode, *p_opcode,
  10742. error_buf, error_buf_size)) {
  10743. goto fail;
  10744. }
  10745. #endif
  10746. if (!(br_table_cache = loader_malloc(
  10747. offsetof(BrTableCache, br_depths)
  10748. + sizeof(uint32)
  10749. * (uint64)(count + 1),
  10750. error_buf, error_buf_size))) {
  10751. goto fail;
  10752. }
  10753. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10754. br_table_cache->br_table_op_addr = p_opcode;
  10755. br_table_cache->br_count = count;
  10756. /* Copy previous depths which are one byte */
  10757. for (j = 0; j < i; j++) {
  10758. br_table_cache->br_depths[j] = p_depth_begin[j];
  10759. }
  10760. br_table_cache->br_depths[i] = depth;
  10761. bh_list_insert(module->br_table_cache_list,
  10762. br_table_cache);
  10763. }
  10764. else {
  10765. /* The depth can be stored in one byte, use the
  10766. byte of the leb to store it */
  10767. *p_depth++ = (uint8)depth;
  10768. }
  10769. }
  10770. #endif
  10771. }
  10772. #if WASM_ENABLE_FAST_INTERP == 0
  10773. /* Set the tailing bytes to nop */
  10774. if (br_table_cache)
  10775. p_depth = p_depth_begin;
  10776. while (p_depth < p)
  10777. *p_depth++ = WASM_OP_NOP;
  10778. #endif
  10779. RESET_STACK();
  10780. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10781. break;
  10782. }
  10783. case WASM_OP_RETURN:
  10784. {
  10785. WASMFuncType *func_type = func->func_type;
  10786. int32 idx;
  10787. uint8 ret_type;
  10788. #if WASM_ENABLE_GC != 0
  10789. uint32 j = func_type->ref_type_map_count - 1;
  10790. #endif
  10791. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10792. idx--) {
  10793. ret_type =
  10794. *(func_type->types + func_type->param_count + idx);
  10795. #if WASM_ENABLE_GC != 0
  10796. if (wasm_is_type_multi_byte_type(ret_type)) {
  10797. WASMRefType *ref_type =
  10798. func_type->ref_type_maps[j].ref_type;
  10799. bh_assert(func_type->ref_type_maps[j].index
  10800. == func_type->param_count + idx);
  10801. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10802. ref_type,
  10803. wasm_reftype_struct_size(ref_type));
  10804. j--;
  10805. }
  10806. #endif
  10807. #if WASM_ENABLE_FAST_INTERP != 0
  10808. /* emit the offset after return opcode */
  10809. POP_OFFSET_TYPE(ret_type);
  10810. #endif
  10811. POP_TYPE(ret_type);
  10812. }
  10813. RESET_STACK();
  10814. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10815. break;
  10816. }
  10817. case WASM_OP_CALL:
  10818. #if WASM_ENABLE_TAIL_CALL != 0
  10819. case WASM_OP_RETURN_CALL:
  10820. #endif
  10821. #if WASM_ENABLE_GC != 0
  10822. case WASM_OP_CALL_REF:
  10823. case WASM_OP_RETURN_CALL_REF:
  10824. #endif
  10825. {
  10826. WASMFuncType *func_type;
  10827. uint8 type;
  10828. int32 idx;
  10829. #if WASM_ENABLE_GC != 0
  10830. WASMRefType *ref_type;
  10831. uint32 type_idx1;
  10832. int32 j;
  10833. #endif
  10834. #if WASM_ENABLE_GC != 0
  10835. if (opcode == WASM_OP_CALL_REF
  10836. || opcode == WASM_OP_RETURN_CALL_REF) {
  10837. pb_read_leb_uint32(p, p_end, type_idx1);
  10838. if (!check_type_index(module, module->type_count, type_idx1,
  10839. error_buf, error_buf_size)) {
  10840. goto fail;
  10841. }
  10842. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10843. set_error_buf(error_buf, error_buf_size,
  10844. "unkown function type");
  10845. goto fail;
  10846. }
  10847. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10848. &type_idx, error_buf,
  10849. error_buf_size)) {
  10850. goto fail;
  10851. }
  10852. if (type == VALUE_TYPE_ANY) {
  10853. type_idx = type_idx1;
  10854. }
  10855. if (!check_type_index(module, module->type_count, type_idx,
  10856. error_buf, error_buf_size)) {
  10857. goto fail;
  10858. }
  10859. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10860. set_error_buf(error_buf, error_buf_size,
  10861. "unkown function type");
  10862. goto fail;
  10863. }
  10864. if (!wasm_func_type_is_super_of(
  10865. (WASMFuncType *)module->types[type_idx1],
  10866. (WASMFuncType *)module->types[type_idx])) {
  10867. set_error_buf(error_buf, error_buf_size,
  10868. "function type mismatch");
  10869. goto fail;
  10870. }
  10871. func_type = (WASMFuncType *)module->types[type_idx];
  10872. }
  10873. else
  10874. #endif
  10875. {
  10876. pb_read_leb_uint32(p, p_end, func_idx);
  10877. #if WASM_ENABLE_FAST_INTERP != 0
  10878. /* we need to emit func_idx before arguments */
  10879. emit_uint32(loader_ctx, func_idx);
  10880. #endif
  10881. if (!check_function_index(module, func_idx, error_buf,
  10882. error_buf_size)) {
  10883. goto fail;
  10884. }
  10885. if (func_idx < module->import_function_count)
  10886. func_type = module->import_functions[func_idx]
  10887. .u.function.func_type;
  10888. else
  10889. func_type =
  10890. module
  10891. ->functions[func_idx
  10892. - module->import_function_count]
  10893. ->func_type;
  10894. }
  10895. if (func_type->param_count > 0) {
  10896. #if WASM_ENABLE_GC != 0
  10897. j = (int32)(func_type->result_ref_type_maps
  10898. - func_type->ref_type_maps - 1);
  10899. #endif
  10900. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10901. idx--) {
  10902. #if WASM_ENABLE_GC != 0
  10903. if (wasm_is_type_multi_byte_type(
  10904. func_type->types[idx])) {
  10905. ref_type = func_type->ref_type_maps[j].ref_type;
  10906. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10907. ref_type,
  10908. wasm_reftype_struct_size(ref_type));
  10909. j--;
  10910. }
  10911. #endif
  10912. #if WASM_ENABLE_FAST_INTERP != 0
  10913. POP_OFFSET_TYPE(func_type->types[idx]);
  10914. #endif
  10915. POP_TYPE(func_type->types[idx]);
  10916. }
  10917. }
  10918. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10919. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10920. #endif
  10921. #if WASM_ENABLE_GC != 0
  10922. j = (int32)(func_type->result_ref_type_maps
  10923. - func_type->ref_type_maps);
  10924. #endif
  10925. for (i = 0; i < func_type->result_count; i++) {
  10926. #if WASM_ENABLE_GC != 0
  10927. if (wasm_is_type_multi_byte_type(
  10928. func_type->types[func_type->param_count + i])) {
  10929. ref_type = func_type->ref_type_maps[j].ref_type;
  10930. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10931. ref_type,
  10932. wasm_reftype_struct_size(ref_type));
  10933. j++;
  10934. }
  10935. #endif
  10936. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10937. #if WASM_ENABLE_FAST_INTERP != 0
  10938. /* Here we emit each return value's dynamic_offset. But
  10939. * in fact these offsets are continuous, so interpreter
  10940. * only need to get the first return value's offset.
  10941. */
  10942. PUSH_OFFSET_TYPE(
  10943. func_type->types[func_type->param_count + i]);
  10944. #endif
  10945. }
  10946. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10947. }
  10948. else {
  10949. #if WASM_ENABLE_GC == 0
  10950. if (func_type->result_count
  10951. != func->func_type->result_count) {
  10952. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10953. "type mismatch: expect ",
  10954. func->func_type->result_count,
  10955. " return values but got other");
  10956. goto fail;
  10957. }
  10958. for (i = 0; i < func_type->result_count; i++) {
  10959. type = func->func_type
  10960. ->types[func->func_type->param_count + i];
  10961. if (func_type->types[func_type->param_count + i]
  10962. != type) {
  10963. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10964. "type mismatch: expect ",
  10965. type2str(type), " but got other");
  10966. goto fail;
  10967. }
  10968. }
  10969. #else
  10970. if (!wasm_func_type_result_is_subtype_of(
  10971. func_type, func->func_type, module->types,
  10972. module->type_count)) {
  10973. set_error_buf(
  10974. error_buf, error_buf_size,
  10975. "type mismatch: invalid func result types");
  10976. goto fail;
  10977. }
  10978. #endif
  10979. RESET_STACK();
  10980. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10981. }
  10982. #endif
  10983. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10984. || WASM_ENABLE_WAMR_COMPILER != 0
  10985. func->has_op_func_call = true;
  10986. #endif
  10987. (void)type;
  10988. break;
  10989. }
  10990. /*
  10991. * if disable reference type: call_indirect typeidx, 0x00
  10992. * if enable reference type: call_indirect typeidx, tableidx
  10993. */
  10994. case WASM_OP_CALL_INDIRECT:
  10995. #if WASM_ENABLE_TAIL_CALL != 0
  10996. case WASM_OP_RETURN_CALL_INDIRECT:
  10997. #endif
  10998. {
  10999. int32 idx;
  11000. WASMFuncType *func_type;
  11001. uint32 tbl_elem_type;
  11002. #if WASM_ENABLE_GC != 0
  11003. WASMRefType *elem_ref_type = NULL;
  11004. #endif
  11005. pb_read_leb_uint32(p, p_end, type_idx);
  11006. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11007. #if WASM_ENABLE_WAMR_COMPILER != 0
  11008. if (p + 1 < p_end && *p != 0x00) {
  11009. /*
  11010. * Any non-0x00 byte requires the ref types proposal.
  11011. * This is different from checking the table_idx value
  11012. * since `0x80 0x00` etc. are all valid encodings of zero.
  11013. */
  11014. module->is_ref_types_used = true;
  11015. }
  11016. #endif
  11017. pb_read_leb_uint32(p, p_end, table_idx);
  11018. #else
  11019. CHECK_BUF(p, p_end, 1);
  11020. table_idx = read_uint8(p);
  11021. #endif
  11022. if (!check_table_index(module, table_idx, error_buf,
  11023. error_buf_size)) {
  11024. goto fail;
  11025. }
  11026. tbl_elem_type =
  11027. table_idx < module->import_table_count
  11028. ? module->import_tables[table_idx]
  11029. .u.table.table_type.elem_type
  11030. : module->tables[table_idx - module->import_table_count]
  11031. .table_type.elem_type;
  11032. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11033. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11034. set_error_buf_v(error_buf, error_buf_size,
  11035. "type mismatch: instruction requires table "
  11036. "of functions but table %u has externref",
  11037. table_idx);
  11038. goto fail;
  11039. }
  11040. #elif WASM_ENABLE_GC != 0
  11041. /* Table element must match type ref null func */
  11042. elem_ref_type =
  11043. table_idx < module->import_table_count
  11044. ? module->import_tables[table_idx]
  11045. .u.table.table_type.elem_ref_type
  11046. : module->tables[table_idx - module->import_table_count]
  11047. .table_type.elem_ref_type;
  11048. if (!wasm_reftype_is_subtype_of(
  11049. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11050. module->types, module->type_count)) {
  11051. set_error_buf_v(error_buf, error_buf_size,
  11052. "type mismatch: instruction requires "
  11053. "reference type t match type ref null func"
  11054. "in table %u",
  11055. table_idx);
  11056. goto fail;
  11057. }
  11058. #else
  11059. (void)tbl_elem_type;
  11060. #endif
  11061. #if WASM_ENABLE_FAST_INTERP != 0
  11062. /* we need to emit before arguments */
  11063. #if WASM_ENABLE_TAIL_CALL != 0
  11064. emit_byte(loader_ctx, opcode);
  11065. #endif
  11066. emit_uint32(loader_ctx, type_idx);
  11067. emit_uint32(loader_ctx, table_idx);
  11068. #endif
  11069. #if WASM_ENABLE_MEMORY64 != 0
  11070. table_elem_idx_type = is_table_64bit(module, table_idx)
  11071. ? VALUE_TYPE_I64
  11072. : VALUE_TYPE_I32;
  11073. #endif
  11074. /* skip elem idx */
  11075. POP_TBL_ELEM_IDX();
  11076. if (type_idx >= module->type_count) {
  11077. set_error_buf(error_buf, error_buf_size, "unknown type");
  11078. goto fail;
  11079. }
  11080. func_type = (WASMFuncType *)module->types[type_idx];
  11081. if (func_type->param_count > 0) {
  11082. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11083. idx--) {
  11084. #if WASM_ENABLE_FAST_INTERP != 0
  11085. POP_OFFSET_TYPE(func_type->types[idx]);
  11086. #endif
  11087. POP_TYPE(func_type->types[idx]);
  11088. }
  11089. }
  11090. #if WASM_ENABLE_TAIL_CALL != 0
  11091. if (opcode == WASM_OP_CALL_INDIRECT) {
  11092. #endif
  11093. for (i = 0; i < func_type->result_count; i++) {
  11094. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11095. #if WASM_ENABLE_FAST_INTERP != 0
  11096. PUSH_OFFSET_TYPE(
  11097. func_type->types[func_type->param_count + i]);
  11098. #endif
  11099. }
  11100. #if WASM_ENABLE_TAIL_CALL != 0
  11101. }
  11102. else {
  11103. uint8 type;
  11104. if (func_type->result_count
  11105. != func->func_type->result_count) {
  11106. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11107. "type mismatch: expect ",
  11108. func->func_type->result_count,
  11109. " return values but got other");
  11110. goto fail;
  11111. }
  11112. for (i = 0; i < func_type->result_count; i++) {
  11113. type = func->func_type
  11114. ->types[func->func_type->param_count + i];
  11115. if (func_type->types[func_type->param_count + i]
  11116. != type) {
  11117. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11118. "type mismatch: expect ",
  11119. type2str(type), " but got other");
  11120. goto fail;
  11121. }
  11122. }
  11123. RESET_STACK();
  11124. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11125. }
  11126. #endif
  11127. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11128. || WASM_ENABLE_WAMR_COMPILER != 0
  11129. func->has_op_func_call = true;
  11130. #endif
  11131. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11132. func->has_op_call_indirect = true;
  11133. #endif
  11134. break;
  11135. }
  11136. case WASM_OP_DROP:
  11137. {
  11138. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11139. int32 available_stack_cell =
  11140. (int32)(loader_ctx->stack_cell_num
  11141. - cur_block->stack_cell_num);
  11142. if (available_stack_cell <= 0
  11143. && !cur_block->is_stack_polymorphic) {
  11144. set_error_buf(error_buf, error_buf_size,
  11145. "type mismatch, opcode drop was found "
  11146. "but stack was empty");
  11147. goto fail;
  11148. }
  11149. if (available_stack_cell > 0) {
  11150. #if WASM_ENABLE_GC != 0
  11151. if (wasm_is_type_multi_byte_type(
  11152. *(loader_ctx->frame_ref - 1))) {
  11153. bh_assert((int32)(loader_ctx->reftype_map_num
  11154. - cur_block->reftype_map_num)
  11155. > 0);
  11156. loader_ctx->frame_reftype_map--;
  11157. loader_ctx->reftype_map_num--;
  11158. }
  11159. #endif
  11160. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11161. loader_ctx->frame_ref--;
  11162. loader_ctx->stack_cell_num--;
  11163. #if WASM_ENABLE_FAST_INTERP != 0
  11164. skip_label();
  11165. loader_ctx->frame_offset--;
  11166. if ((*(loader_ctx->frame_offset)
  11167. > loader_ctx->start_dynamic_offset)
  11168. && (*(loader_ctx->frame_offset)
  11169. < loader_ctx->max_dynamic_offset))
  11170. loader_ctx->dynamic_offset--;
  11171. #endif
  11172. }
  11173. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11174. loader_ctx->frame_ref -= 2;
  11175. loader_ctx->stack_cell_num -= 2;
  11176. #if WASM_ENABLE_FAST_INTERP == 0
  11177. *(p - 1) = WASM_OP_DROP_64;
  11178. #endif
  11179. #if WASM_ENABLE_FAST_INTERP != 0
  11180. skip_label();
  11181. loader_ctx->frame_offset -= 2;
  11182. if ((*(loader_ctx->frame_offset)
  11183. > loader_ctx->start_dynamic_offset)
  11184. && (*(loader_ctx->frame_offset)
  11185. < loader_ctx->max_dynamic_offset))
  11186. loader_ctx->dynamic_offset -= 2;
  11187. #endif
  11188. }
  11189. #if WASM_ENABLE_SIMD != 0
  11190. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11191. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11192. loader_ctx->frame_ref -= 4;
  11193. loader_ctx->stack_cell_num -= 4;
  11194. }
  11195. #endif
  11196. #endif
  11197. else {
  11198. set_error_buf(error_buf, error_buf_size,
  11199. "type mismatch");
  11200. goto fail;
  11201. }
  11202. }
  11203. else {
  11204. #if WASM_ENABLE_FAST_INTERP != 0
  11205. skip_label();
  11206. #endif
  11207. }
  11208. break;
  11209. }
  11210. case WASM_OP_SELECT:
  11211. {
  11212. uint8 ref_type;
  11213. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11214. int32 available_stack_cell;
  11215. #if WASM_ENABLE_FAST_INTERP != 0
  11216. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11217. #endif
  11218. POP_I32();
  11219. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11220. - cur_block->stack_cell_num);
  11221. if (available_stack_cell <= 0
  11222. && !cur_block->is_stack_polymorphic) {
  11223. set_error_buf(error_buf, error_buf_size,
  11224. "type mismatch or invalid result arity, "
  11225. "opcode select was found "
  11226. "but stack was empty");
  11227. goto fail;
  11228. }
  11229. if (available_stack_cell > 0) {
  11230. switch (*(loader_ctx->frame_ref - 1)) {
  11231. case VALUE_TYPE_I32:
  11232. case VALUE_TYPE_F32:
  11233. case VALUE_TYPE_ANY:
  11234. break;
  11235. case VALUE_TYPE_I64:
  11236. case VALUE_TYPE_F64:
  11237. #if WASM_ENABLE_FAST_INTERP == 0
  11238. *(p - 1) = WASM_OP_SELECT_64;
  11239. #endif
  11240. #if WASM_ENABLE_FAST_INTERP != 0
  11241. if (loader_ctx->p_code_compiled) {
  11242. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11243. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11244. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11245. *(void **)(p_code_compiled_tmp
  11246. - sizeof(void *)) =
  11247. handle_table[opcode_tmp];
  11248. #else
  11249. #if UINTPTR_MAX == UINT64_MAX
  11250. /* emit int32 relative offset in 64-bit target
  11251. */
  11252. int32 offset =
  11253. (int32)((uint8 *)handle_table[opcode_tmp]
  11254. - (uint8 *)handle_table[0]);
  11255. *(int32 *)(p_code_compiled_tmp
  11256. - sizeof(int32)) = offset;
  11257. #else
  11258. /* emit uint32 label address in 32-bit target */
  11259. *(uint32 *)(p_code_compiled_tmp
  11260. - sizeof(uint32)) =
  11261. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11262. #endif
  11263. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11264. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11265. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11266. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11267. #else
  11268. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11269. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11270. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11271. }
  11272. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11273. break;
  11274. #if WASM_ENABLE_SIMD != 0
  11275. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11276. case VALUE_TYPE_V128:
  11277. break;
  11278. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11279. #endif /* WASM_ENABLE_SIMD != 0 */
  11280. default:
  11281. {
  11282. set_error_buf(error_buf, error_buf_size,
  11283. "type mismatch");
  11284. goto fail;
  11285. }
  11286. }
  11287. ref_type = *(loader_ctx->frame_ref - 1);
  11288. #if WASM_ENABLE_FAST_INTERP != 0
  11289. POP_OFFSET_TYPE(ref_type);
  11290. POP_TYPE(ref_type);
  11291. POP_OFFSET_TYPE(ref_type);
  11292. POP_TYPE(ref_type);
  11293. PUSH_OFFSET_TYPE(ref_type);
  11294. PUSH_TYPE(ref_type);
  11295. #else
  11296. POP2_AND_PUSH(ref_type, ref_type);
  11297. #endif
  11298. }
  11299. else {
  11300. #if WASM_ENABLE_FAST_INTERP != 0
  11301. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11302. #endif
  11303. PUSH_TYPE(VALUE_TYPE_ANY);
  11304. }
  11305. break;
  11306. }
  11307. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11308. case WASM_OP_SELECT_T:
  11309. {
  11310. uint8 vec_len, type;
  11311. #if WASM_ENABLE_GC != 0
  11312. WASMRefType *ref_type = NULL;
  11313. #endif
  11314. #if WASM_ENABLE_FAST_INTERP != 0
  11315. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11316. #endif
  11317. pb_read_leb_uint32(p, p_end, vec_len);
  11318. if (vec_len != 1) {
  11319. /* typed select must have exactly one result */
  11320. set_error_buf(error_buf, error_buf_size,
  11321. "invalid result arity");
  11322. goto fail;
  11323. }
  11324. #if WASM_ENABLE_GC == 0
  11325. CHECK_BUF(p, p_end, 1);
  11326. type = read_uint8(p);
  11327. if (!is_valid_value_type_for_interpreter(type)) {
  11328. set_error_buf(error_buf, error_buf_size,
  11329. "unknown value type");
  11330. goto fail;
  11331. }
  11332. #else
  11333. p_org = p + 1;
  11334. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11335. module->type_count, &need_ref_type_map,
  11336. &wasm_ref_type, false, error_buf,
  11337. error_buf_size)) {
  11338. goto fail;
  11339. }
  11340. type = wasm_ref_type.ref_type;
  11341. if (need_ref_type_map) {
  11342. if (!(ref_type = reftype_set_insert(
  11343. module->ref_type_set, &wasm_ref_type, error_buf,
  11344. error_buf_size))) {
  11345. goto fail;
  11346. }
  11347. }
  11348. #if WASM_ENABLE_FAST_INTERP == 0
  11349. while (p_org < p) {
  11350. #if WASM_ENABLE_DEBUG_INTERP != 0
  11351. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11352. error_buf_size)) {
  11353. goto fail;
  11354. }
  11355. #endif
  11356. /* Ignore extra bytes for interpreter */
  11357. *p_org++ = WASM_OP_NOP;
  11358. }
  11359. #endif
  11360. #endif /* end of WASM_ENABLE_GC == 0 */
  11361. POP_I32();
  11362. #if WASM_ENABLE_FAST_INTERP != 0
  11363. if (loader_ctx->p_code_compiled) {
  11364. uint8 opcode_tmp = WASM_OP_SELECT;
  11365. if (type == VALUE_TYPE_V128) {
  11366. #if (WASM_ENABLE_SIMD == 0) \
  11367. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11368. set_error_buf(error_buf, error_buf_size,
  11369. "SIMD v128 type isn't supported");
  11370. goto fail;
  11371. #endif
  11372. }
  11373. else {
  11374. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11375. opcode_tmp = WASM_OP_SELECT_64;
  11376. #if WASM_ENABLE_GC != 0
  11377. if (wasm_is_type_reftype(type))
  11378. opcode_tmp = WASM_OP_SELECT_T;
  11379. #endif
  11380. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11381. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11382. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11383. handle_table[opcode_tmp];
  11384. #else
  11385. #if UINTPTR_MAX == UINT64_MAX
  11386. /* emit int32 relative offset in 64-bit target */
  11387. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11388. - (uint8 *)handle_table[0]);
  11389. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11390. offset;
  11391. #else
  11392. /* emit uint32 label address in 32-bit target */
  11393. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11394. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11395. #endif
  11396. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11397. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11398. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11399. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11400. #else
  11401. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11402. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11403. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11404. }
  11405. }
  11406. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11407. POP_REF(type);
  11408. #if WASM_ENABLE_GC != 0
  11409. if (need_ref_type_map) {
  11410. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11411. wasm_reftype_struct_size(ref_type));
  11412. }
  11413. #endif
  11414. POP_REF(type);
  11415. #if WASM_ENABLE_GC != 0
  11416. if (need_ref_type_map) {
  11417. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11418. wasm_reftype_struct_size(ref_type));
  11419. }
  11420. #endif
  11421. PUSH_REF(type);
  11422. #if WASM_ENABLE_WAMR_COMPILER != 0
  11423. module->is_ref_types_used = true;
  11424. #endif
  11425. (void)vec_len;
  11426. break;
  11427. }
  11428. /* table.get x. tables[x]. [it] -> [t] */
  11429. /* table.set x. tables[x]. [it t] -> [] */
  11430. case WASM_OP_TABLE_GET:
  11431. case WASM_OP_TABLE_SET:
  11432. {
  11433. uint8 decl_ref_type;
  11434. #if WASM_ENABLE_GC != 0
  11435. WASMRefType *ref_type;
  11436. #endif
  11437. pb_read_leb_uint32(p, p_end, table_idx);
  11438. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11439. #if WASM_ENABLE_GC != 0
  11440. (void **)&ref_type,
  11441. #else
  11442. NULL,
  11443. #endif
  11444. error_buf, error_buf_size))
  11445. goto fail;
  11446. #if WASM_ENABLE_GC != 0
  11447. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11448. bh_assert(ref_type);
  11449. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11450. ref_type, wasm_reftype_struct_size(ref_type));
  11451. }
  11452. #endif
  11453. #if WASM_ENABLE_FAST_INTERP != 0
  11454. emit_uint32(loader_ctx, table_idx);
  11455. #endif
  11456. #if WASM_ENABLE_MEMORY64 != 0
  11457. table_elem_idx_type = is_table_64bit(module, table_idx)
  11458. ? VALUE_TYPE_I64
  11459. : VALUE_TYPE_I32;
  11460. #endif
  11461. if (opcode == WASM_OP_TABLE_GET) {
  11462. POP_TBL_ELEM_IDX();
  11463. #if WASM_ENABLE_FAST_INTERP != 0
  11464. PUSH_OFFSET_TYPE(decl_ref_type);
  11465. #endif
  11466. PUSH_TYPE(decl_ref_type);
  11467. }
  11468. else {
  11469. #if WASM_ENABLE_FAST_INTERP != 0
  11470. POP_OFFSET_TYPE(decl_ref_type);
  11471. #endif
  11472. POP_TYPE(decl_ref_type);
  11473. POP_TBL_ELEM_IDX();
  11474. }
  11475. #if WASM_ENABLE_WAMR_COMPILER != 0
  11476. module->is_ref_types_used = true;
  11477. #endif
  11478. break;
  11479. }
  11480. case WASM_OP_REF_NULL:
  11481. {
  11482. uint8 ref_type;
  11483. #if WASM_ENABLE_GC == 0
  11484. CHECK_BUF(p, p_end, 1);
  11485. ref_type = read_uint8(p);
  11486. if (ref_type != VALUE_TYPE_FUNCREF
  11487. && ref_type != VALUE_TYPE_EXTERNREF) {
  11488. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11489. goto fail;
  11490. }
  11491. #else
  11492. pb_read_leb_int32(p, p_end, heap_type);
  11493. if (heap_type >= 0) {
  11494. if (!check_type_index(module, module->type_count, heap_type,
  11495. error_buf, error_buf_size)) {
  11496. goto fail;
  11497. }
  11498. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11499. true, heap_type);
  11500. ref_type = wasm_ref_type.ref_type;
  11501. }
  11502. else {
  11503. if (!wasm_is_valid_heap_type(heap_type)) {
  11504. set_error_buf(error_buf, error_buf_size,
  11505. "unknown type");
  11506. goto fail;
  11507. }
  11508. ref_type = (uint8)((int32)0x80 + heap_type);
  11509. }
  11510. #endif /* end of WASM_ENABLE_GC == 0 */
  11511. #if WASM_ENABLE_FAST_INTERP != 0
  11512. PUSH_OFFSET_TYPE(ref_type);
  11513. #endif
  11514. PUSH_TYPE(ref_type);
  11515. #if WASM_ENABLE_WAMR_COMPILER != 0
  11516. module->is_ref_types_used = true;
  11517. #endif
  11518. break;
  11519. }
  11520. case WASM_OP_REF_IS_NULL:
  11521. {
  11522. #if WASM_ENABLE_GC == 0
  11523. #if WASM_ENABLE_FAST_INTERP != 0
  11524. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11525. int32 block_stack_cell_num =
  11526. (int32)(loader_ctx->stack_cell_num
  11527. - cur_block->stack_cell_num);
  11528. if (block_stack_cell_num <= 0) {
  11529. if (!cur_block->is_stack_polymorphic) {
  11530. set_error_buf(
  11531. error_buf, error_buf_size,
  11532. "type mismatch: expect data but stack was empty");
  11533. goto fail;
  11534. }
  11535. }
  11536. else {
  11537. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11538. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11539. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11540. if (!wasm_loader_pop_frame_ref_offset(
  11541. loader_ctx, *(loader_ctx->frame_ref - 1),
  11542. error_buf, error_buf_size)) {
  11543. goto fail;
  11544. }
  11545. }
  11546. else {
  11547. set_error_buf(error_buf, error_buf_size,
  11548. "type mismatch");
  11549. goto fail;
  11550. }
  11551. }
  11552. #else
  11553. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11554. error_buf, error_buf_size)
  11555. && !wasm_loader_pop_frame_ref(loader_ctx,
  11556. VALUE_TYPE_EXTERNREF,
  11557. error_buf, error_buf_size)) {
  11558. goto fail;
  11559. }
  11560. #endif
  11561. #else /* else of WASM_ENABLE_GC == 0 */
  11562. uint8 type;
  11563. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11564. error_buf, error_buf_size)) {
  11565. goto fail;
  11566. }
  11567. #endif
  11568. PUSH_I32();
  11569. #if WASM_ENABLE_WAMR_COMPILER != 0
  11570. module->is_ref_types_used = true;
  11571. #endif
  11572. break;
  11573. }
  11574. case WASM_OP_REF_FUNC:
  11575. {
  11576. pb_read_leb_uint32(p, p_end, func_idx);
  11577. if (!check_function_index(module, func_idx, error_buf,
  11578. error_buf_size)) {
  11579. goto fail;
  11580. }
  11581. /* Refer to a forward-declared function:
  11582. the function must be an import, exported, or present in
  11583. a table elem segment or global initializer to be used as
  11584. the operand to ref.func */
  11585. if (func_idx >= module->import_function_count) {
  11586. WASMTableSeg *table_seg = module->table_segments;
  11587. bool func_declared = false;
  11588. uint32 j;
  11589. for (i = 0; i < module->global_count; i++) {
  11590. if (module->globals[i].type.val_type
  11591. == VALUE_TYPE_FUNCREF
  11592. && module->globals[i].init_expr.init_expr_type
  11593. == INIT_EXPR_TYPE_FUNCREF_CONST
  11594. && module->globals[i].init_expr.u.u32 == func_idx) {
  11595. func_declared = true;
  11596. break;
  11597. }
  11598. }
  11599. if (!func_declared) {
  11600. /* Check whether the function is declared in table segs,
  11601. note that it doesn't matter whether the table seg's
  11602. mode is passive, active or declarative. */
  11603. for (i = 0; i < module->table_seg_count;
  11604. i++, table_seg++) {
  11605. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11606. #if WASM_ENABLE_GC != 0
  11607. /* elem type is (ref null? func) or
  11608. (ref null? $t) */
  11609. || ((table_seg->elem_type
  11610. == REF_TYPE_HT_NON_NULLABLE
  11611. || table_seg->elem_type
  11612. == REF_TYPE_HT_NULLABLE)
  11613. && (table_seg->elem_ref_type->ref_ht_common
  11614. .heap_type
  11615. == HEAP_TYPE_FUNC
  11616. || table_seg->elem_ref_type
  11617. ->ref_ht_common.heap_type
  11618. > 0))
  11619. #endif
  11620. ) {
  11621. for (j = 0; j < table_seg->value_count; j++) {
  11622. if (table_seg->init_values[j].u.ref_index
  11623. == func_idx) {
  11624. func_declared = true;
  11625. break;
  11626. }
  11627. }
  11628. }
  11629. }
  11630. }
  11631. if (!func_declared) {
  11632. /* Check whether the function is exported */
  11633. for (i = 0; i < module->export_count; i++) {
  11634. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11635. && module->exports[i].index == func_idx) {
  11636. func_declared = true;
  11637. break;
  11638. }
  11639. }
  11640. }
  11641. if (!func_declared) {
  11642. set_error_buf(error_buf, error_buf_size,
  11643. "undeclared function reference");
  11644. goto fail;
  11645. }
  11646. }
  11647. #if WASM_ENABLE_FAST_INTERP != 0
  11648. emit_uint32(loader_ctx, func_idx);
  11649. #endif
  11650. #if WASM_ENABLE_GC == 0
  11651. PUSH_FUNCREF();
  11652. #else
  11653. if (func_idx < module->import_function_count)
  11654. type_idx =
  11655. module->import_functions[func_idx].u.function.type_idx;
  11656. else
  11657. type_idx = module
  11658. ->functions[func_idx
  11659. - module->import_function_count]
  11660. ->type_idx;
  11661. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11662. false, type_idx);
  11663. PUSH_REF(wasm_ref_type.ref_type);
  11664. #endif
  11665. #if WASM_ENABLE_WAMR_COMPILER != 0
  11666. module->is_ref_types_used = true;
  11667. #endif
  11668. break;
  11669. }
  11670. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11671. #if WASM_ENABLE_GC != 0
  11672. case WASM_OP_REF_AS_NON_NULL:
  11673. case WASM_OP_BR_ON_NULL:
  11674. {
  11675. uint8 type;
  11676. WASMRefType ref_type;
  11677. /* POP (ref null ht) and get the converted (ref ht) */
  11678. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11679. error_buf, error_buf_size)) {
  11680. goto fail;
  11681. }
  11682. if (opcode == WASM_OP_BR_ON_NULL) {
  11683. if (!(frame_csp_tmp =
  11684. check_branch_block(loader_ctx, &p, p_end, opcode,
  11685. error_buf, error_buf_size))) {
  11686. goto fail;
  11687. }
  11688. #if WASM_ENABLE_FAST_INTERP != 0
  11689. disable_emit = true;
  11690. #endif
  11691. }
  11692. /* PUSH the converted (ref ht) */
  11693. if (type != VALUE_TYPE_ANY) {
  11694. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11695. sizeof(WASMRefType));
  11696. }
  11697. PUSH_REF(type);
  11698. break;
  11699. }
  11700. case WASM_OP_BR_ON_NON_NULL:
  11701. {
  11702. uint8 type;
  11703. WASMRefType ref_type;
  11704. uint32 available_stack_cell =
  11705. loader_ctx->stack_cell_num
  11706. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11707. /* POP (ref null ht) and get the converted (ref ht) */
  11708. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11709. error_buf, error_buf_size)) {
  11710. goto fail;
  11711. }
  11712. #if WASM_ENABLE_FAST_INTERP != 0
  11713. disable_emit = true;
  11714. #endif
  11715. /* Temporarily PUSH back (ref ht), check brach block and
  11716. then POP it */
  11717. if (available_stack_cell
  11718. > 0) { /* stack isn't in polymorphic state */
  11719. if (type != VALUE_TYPE_ANY) {
  11720. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11721. &ref_type, sizeof(WASMRefType));
  11722. }
  11723. PUSH_REF(type);
  11724. }
  11725. if (!(frame_csp_tmp =
  11726. check_branch_block(loader_ctx, &p, p_end, opcode,
  11727. error_buf, error_buf_size))) {
  11728. goto fail;
  11729. }
  11730. if (available_stack_cell
  11731. > 0) { /* stack isn't in polymorphic state */
  11732. POP_REF(type);
  11733. #if WASM_ENABLE_FAST_INTERP != 0
  11734. /* Erase the opnd offset emitted by POP_REF() */
  11735. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11736. #endif
  11737. }
  11738. break;
  11739. }
  11740. case WASM_OP_REF_EQ:
  11741. POP_REF(REF_TYPE_EQREF);
  11742. POP_REF(REF_TYPE_EQREF);
  11743. PUSH_I32();
  11744. break;
  11745. #endif /* end of WASM_ENABLE_GC != 0 */
  11746. case WASM_OP_GET_LOCAL:
  11747. {
  11748. p_org = p - 1;
  11749. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11750. PUSH_TYPE(local_type);
  11751. #if WASM_ENABLE_GC != 0
  11752. /* Cannot get a non-nullable and unset local */
  11753. if (local_idx >= param_count
  11754. && wasm_is_reftype_htref_non_nullable(local_type)
  11755. && !wasm_loader_get_local_status(loader_ctx,
  11756. local_idx - param_count)) {
  11757. set_error_buf(error_buf, error_buf_size,
  11758. "uninitialized local");
  11759. goto fail;
  11760. }
  11761. #endif
  11762. #if WASM_ENABLE_FAST_INTERP != 0
  11763. /* Get Local is optimized out */
  11764. skip_label();
  11765. disable_emit = true;
  11766. operand_offset = local_offset;
  11767. PUSH_OFFSET_TYPE(local_type);
  11768. #else
  11769. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11770. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11771. if (local_offset < 0x80
  11772. #if WASM_ENABLE_GC != 0
  11773. && !wasm_is_type_reftype(local_type)
  11774. #endif
  11775. ) {
  11776. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11777. if (is_32bit_type(local_type)) {
  11778. *p_org++ = (uint8)local_offset;
  11779. }
  11780. else {
  11781. *p_org++ = (uint8)(local_offset | 0x80);
  11782. }
  11783. while (p_org < p) {
  11784. *p_org++ = WASM_OP_NOP;
  11785. }
  11786. }
  11787. #endif
  11788. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11789. break;
  11790. }
  11791. case WASM_OP_SET_LOCAL:
  11792. {
  11793. p_org = p - 1;
  11794. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11795. #if WASM_ENABLE_FAST_INTERP != 0
  11796. if (!(preserve_referenced_local(
  11797. loader_ctx, opcode, local_offset, local_type,
  11798. &preserve_local, error_buf, error_buf_size)))
  11799. goto fail;
  11800. if (local_offset < 256
  11801. #if WASM_ENABLE_GC != 0
  11802. && !wasm_is_type_reftype(local_type)
  11803. #endif
  11804. ) {
  11805. skip_label();
  11806. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11807. if (loader_ctx->p_code_compiled)
  11808. STORE_U16(loader_ctx->p_code_compiled - 2,
  11809. local_offset);
  11810. loader_ctx->frame_offset--;
  11811. loader_ctx->dynamic_offset--;
  11812. }
  11813. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11814. if (loader_ctx->p_code_compiled)
  11815. STORE_U16(loader_ctx->p_code_compiled - 2,
  11816. local_offset);
  11817. loader_ctx->frame_offset -= 2;
  11818. loader_ctx->dynamic_offset -= 2;
  11819. }
  11820. else {
  11821. if (is_32bit_type(local_type)) {
  11822. emit_label(EXT_OP_SET_LOCAL_FAST);
  11823. emit_byte(loader_ctx, (uint8)local_offset);
  11824. }
  11825. else {
  11826. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11827. emit_byte(loader_ctx, (uint8)local_offset);
  11828. }
  11829. POP_OFFSET_TYPE(local_type);
  11830. }
  11831. }
  11832. else { /* local index larger than 255, reserve leb */
  11833. emit_uint32(loader_ctx, local_idx);
  11834. POP_OFFSET_TYPE(local_type);
  11835. }
  11836. #else
  11837. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11838. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11839. if (local_offset < 0x80
  11840. #if WASM_ENABLE_GC != 0
  11841. && !wasm_is_type_reftype(local_type)
  11842. #endif
  11843. ) {
  11844. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11845. if (is_32bit_type(local_type)) {
  11846. *p_org++ = (uint8)local_offset;
  11847. }
  11848. else {
  11849. *p_org++ = (uint8)(local_offset | 0x80);
  11850. }
  11851. while (p_org < p) {
  11852. *p_org++ = WASM_OP_NOP;
  11853. }
  11854. }
  11855. #endif
  11856. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11857. #if WASM_ENABLE_GC != 0
  11858. if (local_idx >= param_count) {
  11859. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11860. }
  11861. #endif
  11862. POP_TYPE(local_type);
  11863. break;
  11864. }
  11865. case WASM_OP_TEE_LOCAL:
  11866. {
  11867. p_org = p - 1;
  11868. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11869. #if WASM_ENABLE_FAST_INTERP != 0
  11870. /* If the stack is in polymorphic state, do fake pop and push on
  11871. offset stack to keep the depth of offset stack to be the
  11872. same with ref stack */
  11873. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11874. if (cur_block->is_stack_polymorphic) {
  11875. POP_OFFSET_TYPE(local_type);
  11876. PUSH_OFFSET_TYPE(local_type);
  11877. }
  11878. #endif
  11879. POP_TYPE(local_type);
  11880. PUSH_TYPE(local_type);
  11881. #if WASM_ENABLE_FAST_INTERP != 0
  11882. if (!(preserve_referenced_local(
  11883. loader_ctx, opcode, local_offset, local_type,
  11884. &preserve_local, error_buf, error_buf_size)))
  11885. goto fail;
  11886. if (local_offset < 256
  11887. #if WASM_ENABLE_GC != 0
  11888. && !wasm_is_type_reftype(local_type)
  11889. #endif
  11890. ) {
  11891. skip_label();
  11892. if (is_32bit_type(local_type)) {
  11893. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11894. emit_byte(loader_ctx, (uint8)local_offset);
  11895. }
  11896. else {
  11897. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11898. emit_byte(loader_ctx, (uint8)local_offset);
  11899. }
  11900. }
  11901. else { /* local index larger than 255, reserve leb */
  11902. emit_uint32(loader_ctx, local_idx);
  11903. }
  11904. emit_operand(loader_ctx,
  11905. *(loader_ctx->frame_offset
  11906. - wasm_value_type_cell_num(local_type)));
  11907. #else
  11908. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11909. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11910. if (local_offset < 0x80
  11911. #if WASM_ENABLE_GC != 0
  11912. && !wasm_is_type_reftype(local_type)
  11913. #endif
  11914. ) {
  11915. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11916. if (is_32bit_type(local_type)) {
  11917. *p_org++ = (uint8)local_offset;
  11918. }
  11919. else {
  11920. *p_org++ = (uint8)(local_offset | 0x80);
  11921. }
  11922. while (p_org < p) {
  11923. *p_org++ = WASM_OP_NOP;
  11924. }
  11925. }
  11926. #endif
  11927. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11928. #if WASM_ENABLE_GC != 0
  11929. if (local_idx >= param_count) {
  11930. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11931. }
  11932. #endif
  11933. break;
  11934. }
  11935. case WASM_OP_GET_GLOBAL:
  11936. {
  11937. #if WASM_ENABLE_GC != 0
  11938. WASMRefType *ref_type;
  11939. #endif
  11940. p_org = p - 1;
  11941. pb_read_leb_uint32(p, p_end, global_idx);
  11942. if (global_idx >= global_count) {
  11943. set_error_buf(error_buf, error_buf_size, "unknown global");
  11944. goto fail;
  11945. }
  11946. global_type = global_idx < module->import_global_count
  11947. ? module->import_globals[global_idx]
  11948. .u.global.type.val_type
  11949. : module
  11950. ->globals[global_idx
  11951. - module->import_global_count]
  11952. .type.val_type;
  11953. #if WASM_ENABLE_GC != 0
  11954. ref_type =
  11955. global_idx < module->import_global_count
  11956. ? module->import_globals[global_idx].u.global.ref_type
  11957. : module
  11958. ->globals[global_idx
  11959. - module->import_global_count]
  11960. .ref_type;
  11961. if (wasm_is_type_multi_byte_type(global_type)) {
  11962. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11963. wasm_reftype_struct_size(ref_type));
  11964. }
  11965. #endif
  11966. PUSH_TYPE(global_type);
  11967. #if WASM_ENABLE_FAST_INTERP == 0
  11968. if (global_type == VALUE_TYPE_I64
  11969. || global_type == VALUE_TYPE_F64) {
  11970. #if WASM_ENABLE_DEBUG_INTERP != 0
  11971. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11972. error_buf_size)) {
  11973. goto fail;
  11974. }
  11975. #endif
  11976. *p_org = WASM_OP_GET_GLOBAL_64;
  11977. }
  11978. #else /* else of WASM_ENABLE_FAST_INTERP */
  11979. if (global_type == VALUE_TYPE_I64
  11980. || global_type == VALUE_TYPE_F64) {
  11981. skip_label();
  11982. emit_label(WASM_OP_GET_GLOBAL_64);
  11983. }
  11984. emit_uint32(loader_ctx, global_idx);
  11985. PUSH_OFFSET_TYPE(global_type);
  11986. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11987. break;
  11988. }
  11989. case WASM_OP_SET_GLOBAL:
  11990. {
  11991. bool is_mutable = false;
  11992. #if WASM_ENABLE_GC != 0
  11993. WASMRefType *ref_type;
  11994. #endif
  11995. p_org = p - 1;
  11996. pb_read_leb_uint32(p, p_end, global_idx);
  11997. if (global_idx >= global_count) {
  11998. set_error_buf(error_buf, error_buf_size, "unknown global");
  11999. goto fail;
  12000. }
  12001. is_mutable = global_idx < module->import_global_count
  12002. ? module->import_globals[global_idx]
  12003. .u.global.type.is_mutable
  12004. : module
  12005. ->globals[global_idx
  12006. - module->import_global_count]
  12007. .type.is_mutable;
  12008. if (!is_mutable) {
  12009. #if WASM_ENABLE_GC == 0
  12010. set_error_buf(error_buf, error_buf_size,
  12011. "global is immutable");
  12012. #else
  12013. set_error_buf(error_buf, error_buf_size,
  12014. "immutable global");
  12015. #endif
  12016. goto fail;
  12017. }
  12018. global_type = global_idx < module->import_global_count
  12019. ? module->import_globals[global_idx]
  12020. .u.global.type.val_type
  12021. : module
  12022. ->globals[global_idx
  12023. - module->import_global_count]
  12024. .type.val_type;
  12025. #if WASM_ENABLE_GC != 0
  12026. ref_type =
  12027. global_idx < module->import_global_count
  12028. ? module->import_globals[global_idx].u.global.ref_type
  12029. : module
  12030. ->globals[global_idx
  12031. - module->import_global_count]
  12032. .ref_type;
  12033. if (wasm_is_type_multi_byte_type(global_type)) {
  12034. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12035. wasm_reftype_struct_size(ref_type));
  12036. }
  12037. #endif
  12038. #if WASM_ENABLE_FAST_INTERP == 0
  12039. if (global_type == VALUE_TYPE_I64
  12040. || global_type == VALUE_TYPE_F64) {
  12041. #if WASM_ENABLE_DEBUG_INTERP != 0
  12042. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12043. error_buf_size)) {
  12044. goto fail;
  12045. }
  12046. #endif
  12047. *p_org = WASM_OP_SET_GLOBAL_64;
  12048. }
  12049. else if (module->aux_stack_size > 0
  12050. && global_idx == module->aux_stack_top_global_index) {
  12051. #if WASM_ENABLE_DEBUG_INTERP != 0
  12052. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12053. error_buf_size)) {
  12054. goto fail;
  12055. }
  12056. #endif
  12057. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12058. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12059. func->has_op_set_global_aux_stack = true;
  12060. #endif
  12061. }
  12062. #else /* else of WASM_ENABLE_FAST_INTERP */
  12063. if (global_type == VALUE_TYPE_I64
  12064. || global_type == VALUE_TYPE_F64) {
  12065. skip_label();
  12066. emit_label(WASM_OP_SET_GLOBAL_64);
  12067. }
  12068. else if (module->aux_stack_size > 0
  12069. && global_idx == module->aux_stack_top_global_index) {
  12070. skip_label();
  12071. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12072. }
  12073. emit_uint32(loader_ctx, global_idx);
  12074. POP_OFFSET_TYPE(global_type);
  12075. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12076. POP_TYPE(global_type);
  12077. break;
  12078. }
  12079. /* load */
  12080. case WASM_OP_I32_LOAD:
  12081. case WASM_OP_I32_LOAD8_S:
  12082. case WASM_OP_I32_LOAD8_U:
  12083. case WASM_OP_I32_LOAD16_S:
  12084. case WASM_OP_I32_LOAD16_U:
  12085. case WASM_OP_I64_LOAD:
  12086. case WASM_OP_I64_LOAD8_S:
  12087. case WASM_OP_I64_LOAD8_U:
  12088. case WASM_OP_I64_LOAD16_S:
  12089. case WASM_OP_I64_LOAD16_U:
  12090. case WASM_OP_I64_LOAD32_S:
  12091. case WASM_OP_I64_LOAD32_U:
  12092. case WASM_OP_F32_LOAD:
  12093. case WASM_OP_F64_LOAD:
  12094. /* store */
  12095. case WASM_OP_I32_STORE:
  12096. case WASM_OP_I32_STORE8:
  12097. case WASM_OP_I32_STORE16:
  12098. case WASM_OP_I64_STORE:
  12099. case WASM_OP_I64_STORE8:
  12100. case WASM_OP_I64_STORE16:
  12101. case WASM_OP_I64_STORE32:
  12102. case WASM_OP_F32_STORE:
  12103. case WASM_OP_F64_STORE:
  12104. {
  12105. #if WASM_ENABLE_FAST_INTERP != 0
  12106. /* change F32/F64 into I32/I64 */
  12107. if (opcode == WASM_OP_F32_LOAD) {
  12108. skip_label();
  12109. emit_label(WASM_OP_I32_LOAD);
  12110. }
  12111. else if (opcode == WASM_OP_F64_LOAD) {
  12112. skip_label();
  12113. emit_label(WASM_OP_I64_LOAD);
  12114. }
  12115. else if (opcode == WASM_OP_F32_STORE) {
  12116. skip_label();
  12117. emit_label(WASM_OP_I32_STORE);
  12118. }
  12119. else if (opcode == WASM_OP_F64_STORE) {
  12120. skip_label();
  12121. emit_label(WASM_OP_I64_STORE);
  12122. }
  12123. #endif
  12124. CHECK_MEMORY();
  12125. pb_read_leb_memarg(p, p_end, align); /* align */
  12126. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12127. if (!check_memory_access_align(opcode, align, error_buf,
  12128. error_buf_size)) {
  12129. goto fail;
  12130. }
  12131. #if WASM_ENABLE_FAST_INTERP != 0
  12132. emit_uint32(loader_ctx, mem_offset);
  12133. #endif
  12134. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12135. func->has_memory_operations = true;
  12136. #endif
  12137. switch (opcode) {
  12138. /* load */
  12139. case WASM_OP_I32_LOAD:
  12140. case WASM_OP_I32_LOAD8_S:
  12141. case WASM_OP_I32_LOAD8_U:
  12142. case WASM_OP_I32_LOAD16_S:
  12143. case WASM_OP_I32_LOAD16_U:
  12144. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12145. break;
  12146. case WASM_OP_I64_LOAD:
  12147. case WASM_OP_I64_LOAD8_S:
  12148. case WASM_OP_I64_LOAD8_U:
  12149. case WASM_OP_I64_LOAD16_S:
  12150. case WASM_OP_I64_LOAD16_U:
  12151. case WASM_OP_I64_LOAD32_S:
  12152. case WASM_OP_I64_LOAD32_U:
  12153. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12154. break;
  12155. case WASM_OP_F32_LOAD:
  12156. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12157. break;
  12158. case WASM_OP_F64_LOAD:
  12159. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12160. break;
  12161. /* store */
  12162. case WASM_OP_I32_STORE:
  12163. case WASM_OP_I32_STORE8:
  12164. case WASM_OP_I32_STORE16:
  12165. POP_I32();
  12166. POP_MEM_OFFSET();
  12167. break;
  12168. case WASM_OP_I64_STORE:
  12169. case WASM_OP_I64_STORE8:
  12170. case WASM_OP_I64_STORE16:
  12171. case WASM_OP_I64_STORE32:
  12172. POP_I64();
  12173. POP_MEM_OFFSET();
  12174. break;
  12175. case WASM_OP_F32_STORE:
  12176. POP_F32();
  12177. POP_MEM_OFFSET();
  12178. break;
  12179. case WASM_OP_F64_STORE:
  12180. POP_F64();
  12181. POP_MEM_OFFSET();
  12182. break;
  12183. default:
  12184. break;
  12185. }
  12186. break;
  12187. }
  12188. case WASM_OP_MEMORY_SIZE:
  12189. CHECK_MEMORY();
  12190. pb_read_leb_uint32(p, p_end, memidx);
  12191. check_memidx(module, memidx);
  12192. PUSH_PAGE_COUNT();
  12193. module->possible_memory_grow = true;
  12194. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12195. func->has_memory_operations = true;
  12196. #endif
  12197. break;
  12198. case WASM_OP_MEMORY_GROW:
  12199. CHECK_MEMORY();
  12200. pb_read_leb_uint32(p, p_end, memidx);
  12201. check_memidx(module, memidx);
  12202. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12203. module->possible_memory_grow = true;
  12204. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12205. || WASM_ENABLE_WAMR_COMPILER != 0
  12206. func->has_op_memory_grow = true;
  12207. #endif
  12208. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12209. func->has_memory_operations = true;
  12210. #endif
  12211. break;
  12212. case WASM_OP_I32_CONST:
  12213. pb_read_leb_int32(p, p_end, i32_const);
  12214. #if WASM_ENABLE_FAST_INTERP != 0
  12215. skip_label();
  12216. disable_emit = true;
  12217. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12218. if (operand_offset == 0) {
  12219. disable_emit = false;
  12220. emit_label(WASM_OP_I32_CONST);
  12221. emit_uint32(loader_ctx, i32_const);
  12222. }
  12223. #else
  12224. (void)i32_const;
  12225. #endif
  12226. PUSH_I32();
  12227. break;
  12228. case WASM_OP_I64_CONST:
  12229. pb_read_leb_int64(p, p_end, i64_const);
  12230. #if WASM_ENABLE_FAST_INTERP != 0
  12231. skip_label();
  12232. disable_emit = true;
  12233. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12234. if (operand_offset == 0) {
  12235. disable_emit = false;
  12236. emit_label(WASM_OP_I64_CONST);
  12237. emit_uint64(loader_ctx, i64_const);
  12238. }
  12239. #endif
  12240. PUSH_I64();
  12241. break;
  12242. case WASM_OP_F32_CONST:
  12243. CHECK_BUF(p, p_end, sizeof(float32));
  12244. p += sizeof(float32);
  12245. #if WASM_ENABLE_FAST_INTERP != 0
  12246. skip_label();
  12247. disable_emit = true;
  12248. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12249. sizeof(float32));
  12250. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12251. if (operand_offset == 0) {
  12252. disable_emit = false;
  12253. emit_label(WASM_OP_F32_CONST);
  12254. emit_float32(loader_ctx, f32_const);
  12255. }
  12256. #endif
  12257. PUSH_F32();
  12258. break;
  12259. case WASM_OP_F64_CONST:
  12260. CHECK_BUF(p, p_end, sizeof(float64));
  12261. p += sizeof(float64);
  12262. #if WASM_ENABLE_FAST_INTERP != 0
  12263. skip_label();
  12264. disable_emit = true;
  12265. /* Some MCU may require 8-byte align */
  12266. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12267. sizeof(float64));
  12268. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12269. if (operand_offset == 0) {
  12270. disable_emit = false;
  12271. emit_label(WASM_OP_F64_CONST);
  12272. emit_float64(loader_ctx, f64_const);
  12273. }
  12274. #endif
  12275. PUSH_F64();
  12276. break;
  12277. case WASM_OP_I32_EQZ:
  12278. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12279. break;
  12280. case WASM_OP_I32_EQ:
  12281. case WASM_OP_I32_NE:
  12282. case WASM_OP_I32_LT_S:
  12283. case WASM_OP_I32_LT_U:
  12284. case WASM_OP_I32_GT_S:
  12285. case WASM_OP_I32_GT_U:
  12286. case WASM_OP_I32_LE_S:
  12287. case WASM_OP_I32_LE_U:
  12288. case WASM_OP_I32_GE_S:
  12289. case WASM_OP_I32_GE_U:
  12290. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12291. break;
  12292. case WASM_OP_I64_EQZ:
  12293. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12294. break;
  12295. case WASM_OP_I64_EQ:
  12296. case WASM_OP_I64_NE:
  12297. case WASM_OP_I64_LT_S:
  12298. case WASM_OP_I64_LT_U:
  12299. case WASM_OP_I64_GT_S:
  12300. case WASM_OP_I64_GT_U:
  12301. case WASM_OP_I64_LE_S:
  12302. case WASM_OP_I64_LE_U:
  12303. case WASM_OP_I64_GE_S:
  12304. case WASM_OP_I64_GE_U:
  12305. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12306. break;
  12307. case WASM_OP_F32_EQ:
  12308. case WASM_OP_F32_NE:
  12309. case WASM_OP_F32_LT:
  12310. case WASM_OP_F32_GT:
  12311. case WASM_OP_F32_LE:
  12312. case WASM_OP_F32_GE:
  12313. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12314. break;
  12315. case WASM_OP_F64_EQ:
  12316. case WASM_OP_F64_NE:
  12317. case WASM_OP_F64_LT:
  12318. case WASM_OP_F64_GT:
  12319. case WASM_OP_F64_LE:
  12320. case WASM_OP_F64_GE:
  12321. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12322. break;
  12323. case WASM_OP_I32_CLZ:
  12324. case WASM_OP_I32_CTZ:
  12325. case WASM_OP_I32_POPCNT:
  12326. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12327. break;
  12328. case WASM_OP_I32_ADD:
  12329. case WASM_OP_I32_SUB:
  12330. case WASM_OP_I32_MUL:
  12331. case WASM_OP_I32_DIV_S:
  12332. case WASM_OP_I32_DIV_U:
  12333. case WASM_OP_I32_REM_S:
  12334. case WASM_OP_I32_REM_U:
  12335. case WASM_OP_I32_AND:
  12336. case WASM_OP_I32_OR:
  12337. case WASM_OP_I32_XOR:
  12338. case WASM_OP_I32_SHL:
  12339. case WASM_OP_I32_SHR_S:
  12340. case WASM_OP_I32_SHR_U:
  12341. case WASM_OP_I32_ROTL:
  12342. case WASM_OP_I32_ROTR:
  12343. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12344. break;
  12345. case WASM_OP_I64_CLZ:
  12346. case WASM_OP_I64_CTZ:
  12347. case WASM_OP_I64_POPCNT:
  12348. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12349. break;
  12350. case WASM_OP_I64_ADD:
  12351. case WASM_OP_I64_SUB:
  12352. case WASM_OP_I64_MUL:
  12353. case WASM_OP_I64_DIV_S:
  12354. case WASM_OP_I64_DIV_U:
  12355. case WASM_OP_I64_REM_S:
  12356. case WASM_OP_I64_REM_U:
  12357. case WASM_OP_I64_AND:
  12358. case WASM_OP_I64_OR:
  12359. case WASM_OP_I64_XOR:
  12360. case WASM_OP_I64_SHL:
  12361. case WASM_OP_I64_SHR_S:
  12362. case WASM_OP_I64_SHR_U:
  12363. case WASM_OP_I64_ROTL:
  12364. case WASM_OP_I64_ROTR:
  12365. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12366. break;
  12367. case WASM_OP_F32_ABS:
  12368. case WASM_OP_F32_NEG:
  12369. case WASM_OP_F32_CEIL:
  12370. case WASM_OP_F32_FLOOR:
  12371. case WASM_OP_F32_TRUNC:
  12372. case WASM_OP_F32_NEAREST:
  12373. case WASM_OP_F32_SQRT:
  12374. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12375. break;
  12376. case WASM_OP_F32_ADD:
  12377. case WASM_OP_F32_SUB:
  12378. case WASM_OP_F32_MUL:
  12379. case WASM_OP_F32_DIV:
  12380. case WASM_OP_F32_MIN:
  12381. case WASM_OP_F32_MAX:
  12382. case WASM_OP_F32_COPYSIGN:
  12383. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12384. break;
  12385. case WASM_OP_F64_ABS:
  12386. case WASM_OP_F64_NEG:
  12387. case WASM_OP_F64_CEIL:
  12388. case WASM_OP_F64_FLOOR:
  12389. case WASM_OP_F64_TRUNC:
  12390. case WASM_OP_F64_NEAREST:
  12391. case WASM_OP_F64_SQRT:
  12392. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12393. break;
  12394. case WASM_OP_F64_ADD:
  12395. case WASM_OP_F64_SUB:
  12396. case WASM_OP_F64_MUL:
  12397. case WASM_OP_F64_DIV:
  12398. case WASM_OP_F64_MIN:
  12399. case WASM_OP_F64_MAX:
  12400. case WASM_OP_F64_COPYSIGN:
  12401. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12402. break;
  12403. case WASM_OP_I32_WRAP_I64:
  12404. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12405. break;
  12406. case WASM_OP_I32_TRUNC_S_F32:
  12407. case WASM_OP_I32_TRUNC_U_F32:
  12408. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12409. break;
  12410. case WASM_OP_I32_TRUNC_S_F64:
  12411. case WASM_OP_I32_TRUNC_U_F64:
  12412. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12413. break;
  12414. case WASM_OP_I64_EXTEND_S_I32:
  12415. case WASM_OP_I64_EXTEND_U_I32:
  12416. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12417. break;
  12418. case WASM_OP_I64_TRUNC_S_F32:
  12419. case WASM_OP_I64_TRUNC_U_F32:
  12420. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12421. break;
  12422. case WASM_OP_I64_TRUNC_S_F64:
  12423. case WASM_OP_I64_TRUNC_U_F64:
  12424. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12425. break;
  12426. case WASM_OP_F32_CONVERT_S_I32:
  12427. case WASM_OP_F32_CONVERT_U_I32:
  12428. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12429. break;
  12430. case WASM_OP_F32_CONVERT_S_I64:
  12431. case WASM_OP_F32_CONVERT_U_I64:
  12432. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12433. break;
  12434. case WASM_OP_F32_DEMOTE_F64:
  12435. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12436. break;
  12437. case WASM_OP_F64_CONVERT_S_I32:
  12438. case WASM_OP_F64_CONVERT_U_I32:
  12439. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12440. break;
  12441. case WASM_OP_F64_CONVERT_S_I64:
  12442. case WASM_OP_F64_CONVERT_U_I64:
  12443. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12444. break;
  12445. case WASM_OP_F64_PROMOTE_F32:
  12446. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12447. break;
  12448. case WASM_OP_I32_REINTERPRET_F32:
  12449. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12450. break;
  12451. case WASM_OP_I64_REINTERPRET_F64:
  12452. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12453. break;
  12454. case WASM_OP_F32_REINTERPRET_I32:
  12455. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12456. break;
  12457. case WASM_OP_F64_REINTERPRET_I64:
  12458. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12459. break;
  12460. case WASM_OP_I32_EXTEND8_S:
  12461. case WASM_OP_I32_EXTEND16_S:
  12462. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12463. break;
  12464. case WASM_OP_I64_EXTEND8_S:
  12465. case WASM_OP_I64_EXTEND16_S:
  12466. case WASM_OP_I64_EXTEND32_S:
  12467. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12468. break;
  12469. #if WASM_ENABLE_GC != 0
  12470. case WASM_OP_GC_PREFIX:
  12471. {
  12472. uint32 opcode1;
  12473. pb_read_leb_uint32(p, p_end, opcode1);
  12474. #if WASM_ENABLE_FAST_INTERP != 0
  12475. emit_byte(loader_ctx, ((uint8)opcode1));
  12476. #endif
  12477. switch (opcode1) {
  12478. case WASM_OP_STRUCT_NEW:
  12479. case WASM_OP_STRUCT_NEW_DEFAULT:
  12480. {
  12481. pb_read_leb_uint32(p, p_end, type_idx);
  12482. #if WASM_ENABLE_FAST_INTERP != 0
  12483. emit_uint32(loader_ctx, type_idx);
  12484. #endif
  12485. if (!check_type_index(module, module->type_count,
  12486. type_idx, error_buf,
  12487. error_buf_size)) {
  12488. goto fail;
  12489. }
  12490. if (module->types[type_idx]->type_flag
  12491. != WASM_TYPE_STRUCT) {
  12492. set_error_buf(error_buf, error_buf_size,
  12493. "unkown struct type");
  12494. goto fail;
  12495. }
  12496. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12497. int32 j, k;
  12498. uint8 value_type;
  12499. uint32 ref_type_struct_size;
  12500. WASMStructType *struct_type =
  12501. (WASMStructType *)module->types[type_idx];
  12502. k = struct_type->ref_type_map_count - 1;
  12503. for (j = struct_type->field_count - 1; j >= 0;
  12504. j--) {
  12505. value_type = struct_type->fields[j].field_type;
  12506. if (wasm_is_type_reftype(value_type)) {
  12507. if (wasm_is_type_multi_byte_type(
  12508. value_type)) {
  12509. ref_type_struct_size =
  12510. wasm_reftype_struct_size(
  12511. struct_type->ref_type_maps[k]
  12512. .ref_type);
  12513. bh_memcpy_s(
  12514. &wasm_ref_type,
  12515. (uint32)sizeof(WASMRefType),
  12516. struct_type->ref_type_maps[k]
  12517. .ref_type,
  12518. ref_type_struct_size);
  12519. k--;
  12520. }
  12521. POP_REF(value_type);
  12522. }
  12523. else {
  12524. switch (value_type) {
  12525. case VALUE_TYPE_I32:
  12526. case PACKED_TYPE_I8:
  12527. case PACKED_TYPE_I16:
  12528. POP_I32();
  12529. break;
  12530. case VALUE_TYPE_I64:
  12531. POP_I64();
  12532. break;
  12533. case VALUE_TYPE_F32:
  12534. POP_F32();
  12535. break;
  12536. case VALUE_TYPE_F64:
  12537. POP_F64();
  12538. break;
  12539. default:
  12540. set_error_buf(error_buf,
  12541. error_buf_size,
  12542. "unknown type");
  12543. goto fail;
  12544. }
  12545. }
  12546. }
  12547. }
  12548. /* PUSH struct obj, (ref $t) */
  12549. wasm_set_refheaptype_typeidx(
  12550. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12551. PUSH_REF(wasm_ref_type.ref_type);
  12552. break;
  12553. }
  12554. case WASM_OP_STRUCT_GET:
  12555. case WASM_OP_STRUCT_GET_S:
  12556. case WASM_OP_STRUCT_GET_U:
  12557. case WASM_OP_STRUCT_SET:
  12558. {
  12559. WASMStructType *struct_type;
  12560. WASMRefType *ref_type = NULL;
  12561. uint32 field_idx;
  12562. uint8 field_type;
  12563. pb_read_leb_uint32(p, p_end, type_idx);
  12564. #if WASM_ENABLE_FAST_INTERP != 0
  12565. emit_uint32(loader_ctx, type_idx);
  12566. #endif
  12567. if (!check_type_index(module, module->type_count,
  12568. type_idx, error_buf,
  12569. error_buf_size)) {
  12570. goto fail;
  12571. }
  12572. if (module->types[type_idx]->type_flag
  12573. != WASM_TYPE_STRUCT) {
  12574. set_error_buf(error_buf, error_buf_size,
  12575. "unknown struct type");
  12576. goto fail;
  12577. }
  12578. struct_type = (WASMStructType *)module->types[type_idx];
  12579. pb_read_leb_uint32(p, p_end, field_idx);
  12580. #if WASM_ENABLE_FAST_INTERP != 0
  12581. emit_uint32(loader_ctx, field_idx);
  12582. #endif
  12583. if (field_idx >= struct_type->field_count) {
  12584. set_error_buf(error_buf, error_buf_size,
  12585. "unknown struct field");
  12586. goto fail;
  12587. }
  12588. if (opcode1 == WASM_OP_STRUCT_SET
  12589. && !(struct_type->fields[field_idx].field_flags
  12590. & 1)) {
  12591. set_error_buf(error_buf, error_buf_size,
  12592. "field is immutable");
  12593. goto fail;
  12594. }
  12595. field_type = struct_type->fields[field_idx].field_type;
  12596. if (is_packed_type(field_type)) {
  12597. if (opcode1 == WASM_OP_STRUCT_GET) {
  12598. set_error_buf(error_buf, error_buf_size,
  12599. "type mismatch");
  12600. goto fail;
  12601. }
  12602. else {
  12603. field_type = VALUE_TYPE_I32;
  12604. }
  12605. }
  12606. if (wasm_is_type_multi_byte_type(field_type)) {
  12607. ref_type = wasm_reftype_map_find(
  12608. struct_type->ref_type_maps,
  12609. struct_type->ref_type_map_count, field_idx);
  12610. bh_assert(ref_type);
  12611. }
  12612. if (opcode1 == WASM_OP_STRUCT_SET) {
  12613. /* POP field */
  12614. if (wasm_is_type_multi_byte_type(field_type)) {
  12615. bh_memcpy_s(&wasm_ref_type,
  12616. (uint32)sizeof(WASMRefType),
  12617. ref_type,
  12618. wasm_reftype_struct_size(ref_type));
  12619. }
  12620. POP_REF(field_type);
  12621. /* POP struct obj, (ref null $t) */
  12622. wasm_set_refheaptype_typeidx(
  12623. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12624. POP_REF(wasm_ref_type.ref_type);
  12625. }
  12626. else {
  12627. /* POP struct obj, (ref null $t) */
  12628. wasm_set_refheaptype_typeidx(
  12629. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12630. POP_REF(wasm_ref_type.ref_type);
  12631. /* PUSH field */
  12632. if (wasm_is_type_multi_byte_type(field_type)) {
  12633. bh_memcpy_s(&wasm_ref_type,
  12634. (uint32)sizeof(WASMRefType),
  12635. ref_type,
  12636. wasm_reftype_struct_size(ref_type));
  12637. }
  12638. PUSH_REF(field_type);
  12639. }
  12640. break;
  12641. }
  12642. case WASM_OP_ARRAY_NEW:
  12643. case WASM_OP_ARRAY_NEW_DEFAULT:
  12644. case WASM_OP_ARRAY_NEW_FIXED:
  12645. case WASM_OP_ARRAY_NEW_DATA:
  12646. case WASM_OP_ARRAY_NEW_ELEM:
  12647. {
  12648. WASMArrayType *array_type;
  12649. uint8 elem_type;
  12650. uint32 u32 = 0;
  12651. pb_read_leb_uint32(p, p_end, type_idx);
  12652. #if WASM_ENABLE_FAST_INTERP != 0
  12653. emit_uint32(loader_ctx, type_idx);
  12654. #endif
  12655. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12656. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12657. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12658. pb_read_leb_uint32(p, p_end, u32);
  12659. #if WASM_ENABLE_FAST_INTERP != 0
  12660. emit_uint32(loader_ctx, u32);
  12661. #endif
  12662. }
  12663. if (!check_array_type(module, type_idx, error_buf,
  12664. error_buf_size)) {
  12665. goto fail;
  12666. }
  12667. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12668. /* length */
  12669. POP_I32();
  12670. }
  12671. array_type = (WASMArrayType *)module->types[type_idx];
  12672. elem_type = array_type->elem_type;
  12673. if (opcode1 == WASM_OP_ARRAY_NEW
  12674. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12675. if (wasm_is_type_multi_byte_type(elem_type)) {
  12676. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12677. array_type->elem_ref_type,
  12678. wasm_reftype_struct_size(
  12679. array_type->elem_ref_type));
  12680. }
  12681. if (is_packed_type(elem_type)) {
  12682. elem_type = VALUE_TYPE_I32;
  12683. }
  12684. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12685. uint32 N = u32;
  12686. for (i = 0; i < N; i++) {
  12687. if (wasm_is_type_multi_byte_type(
  12688. elem_type)) {
  12689. bh_memcpy_s(
  12690. &wasm_ref_type, sizeof(WASMRefType),
  12691. array_type->elem_ref_type,
  12692. wasm_reftype_struct_size(
  12693. array_type->elem_ref_type));
  12694. }
  12695. POP_REF(elem_type);
  12696. }
  12697. }
  12698. else
  12699. POP_REF(elem_type);
  12700. }
  12701. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12702. /* offset of data segment */
  12703. POP_I32();
  12704. if (u32 >= module->data_seg_count) {
  12705. set_error_buf(error_buf, error_buf_size,
  12706. "unknown data segment");
  12707. goto fail;
  12708. }
  12709. if (wasm_is_type_reftype(elem_type)) {
  12710. set_error_buf(error_buf, error_buf_size,
  12711. "array elem type mismatch");
  12712. goto fail;
  12713. }
  12714. }
  12715. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12716. WASMTableSeg *table_seg =
  12717. module->table_segments + u32;
  12718. /* offset of element segment */
  12719. POP_I32();
  12720. if (u32 >= module->table_seg_count) {
  12721. set_error_buf(error_buf, error_buf_size,
  12722. "unknown element segment");
  12723. goto fail;
  12724. }
  12725. if (!wasm_reftype_is_subtype_of(
  12726. table_seg->elem_type,
  12727. table_seg->elem_ref_type, elem_type,
  12728. array_type->elem_ref_type, module->types,
  12729. module->type_count)) {
  12730. set_error_buf(error_buf, error_buf_size,
  12731. "array elem type mismatch");
  12732. goto fail;
  12733. }
  12734. }
  12735. /* PUSH array obj, (ref $t) */
  12736. wasm_set_refheaptype_typeidx(
  12737. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12738. PUSH_REF(wasm_ref_type.ref_type);
  12739. break;
  12740. }
  12741. case WASM_OP_ARRAY_GET:
  12742. case WASM_OP_ARRAY_GET_S:
  12743. case WASM_OP_ARRAY_GET_U:
  12744. case WASM_OP_ARRAY_SET:
  12745. {
  12746. uint8 elem_type;
  12747. WASMArrayType *array_type;
  12748. WASMRefType *ref_type = NULL;
  12749. pb_read_leb_uint32(p, p_end, type_idx);
  12750. #if WASM_ENABLE_FAST_INTERP != 0
  12751. emit_uint32(loader_ctx, type_idx);
  12752. #endif
  12753. if (!check_array_type(module, type_idx, error_buf,
  12754. error_buf_size)) {
  12755. goto fail;
  12756. }
  12757. array_type = (WASMArrayType *)module->types[type_idx];
  12758. if (opcode1 == WASM_OP_ARRAY_SET
  12759. && !(array_type->elem_flags & 1)) {
  12760. set_error_buf(error_buf, error_buf_size,
  12761. "array is immutable");
  12762. goto fail;
  12763. }
  12764. elem_type = array_type->elem_type;
  12765. if (is_packed_type(elem_type)) {
  12766. if (opcode1 != WASM_OP_ARRAY_GET_S
  12767. && opcode1 != WASM_OP_ARRAY_GET_U
  12768. && opcode1 != WASM_OP_ARRAY_SET) {
  12769. set_error_buf(error_buf, error_buf_size,
  12770. "type mismatch");
  12771. goto fail;
  12772. }
  12773. else {
  12774. elem_type = VALUE_TYPE_I32;
  12775. }
  12776. }
  12777. ref_type = array_type->elem_ref_type;
  12778. if (opcode1 == WASM_OP_ARRAY_SET) {
  12779. /* POP elem to set */
  12780. if (wasm_is_type_multi_byte_type(elem_type)) {
  12781. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12782. ref_type,
  12783. wasm_reftype_struct_size(ref_type));
  12784. }
  12785. POP_REF(elem_type);
  12786. }
  12787. /* elem idx */
  12788. POP_I32();
  12789. /* POP array obj, (ref null $t) */
  12790. wasm_set_refheaptype_typeidx(
  12791. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12792. POP_REF(wasm_ref_type.ref_type);
  12793. if (opcode1 != WASM_OP_ARRAY_SET) {
  12794. /* PUSH elem */
  12795. if (wasm_is_type_multi_byte_type(elem_type)) {
  12796. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12797. ref_type,
  12798. wasm_reftype_struct_size(ref_type));
  12799. }
  12800. PUSH_REF(elem_type);
  12801. }
  12802. break;
  12803. }
  12804. case WASM_OP_ARRAY_LEN:
  12805. {
  12806. POP_REF(REF_TYPE_ARRAYREF);
  12807. /* length */
  12808. PUSH_I32();
  12809. break;
  12810. }
  12811. case WASM_OP_ARRAY_FILL:
  12812. {
  12813. WASMArrayType *array_type;
  12814. uint8 elem_type;
  12815. /* typeidx */
  12816. pb_read_leb_uint32(p, p_end, type_idx);
  12817. #if WASM_ENABLE_FAST_INTERP != 0
  12818. emit_uint32(loader_ctx, type_idx);
  12819. #endif
  12820. if (!check_array_type(module, type_idx, error_buf,
  12821. error_buf_size)) {
  12822. goto fail;
  12823. }
  12824. array_type = (WASMArrayType *)module->types[type_idx];
  12825. if (!(array_type->elem_flags & 1)) {
  12826. set_error_buf(error_buf, error_buf_size,
  12827. "array is immutable");
  12828. goto fail;
  12829. }
  12830. elem_type = array_type->elem_type;
  12831. if (is_packed_type(elem_type)) {
  12832. elem_type = VALUE_TYPE_I32;
  12833. }
  12834. POP_I32(); /* length */
  12835. #if WASM_ENABLE_FAST_INTERP != 0
  12836. POP_OFFSET_TYPE(elem_type);
  12837. #endif
  12838. POP_TYPE(elem_type);
  12839. POP_I32(); /* start */
  12840. /* POP array obj, (ref null $t) */
  12841. wasm_set_refheaptype_typeidx(
  12842. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12843. POP_REF(wasm_ref_type.ref_type);
  12844. break;
  12845. }
  12846. case WASM_OP_ARRAY_COPY:
  12847. {
  12848. uint32 src_type_idx;
  12849. uint8 src_elem_type, dst_elem_type;
  12850. WASMRefType src_ref_type = { 0 },
  12851. *src_elem_ref_type = NULL;
  12852. WASMRefType dst_ref_type = { 0 },
  12853. *dst_elem_ref_type = NULL;
  12854. WASMArrayType *array_type;
  12855. /* typeidx1 */
  12856. pb_read_leb_uint32(p, p_end, type_idx);
  12857. #if WASM_ENABLE_FAST_INTERP != 0
  12858. emit_uint32(loader_ctx, type_idx);
  12859. #endif
  12860. /* typeidx2 */
  12861. pb_read_leb_uint32(p, p_end, src_type_idx);
  12862. #if WASM_ENABLE_FAST_INTERP != 0
  12863. emit_uint32(loader_ctx, src_type_idx);
  12864. #endif
  12865. if (!check_array_type(module, type_idx, error_buf,
  12866. error_buf_size)) {
  12867. goto fail;
  12868. }
  12869. if (!check_array_type(module, src_type_idx, error_buf,
  12870. error_buf_size)) {
  12871. goto fail;
  12872. }
  12873. POP_I32();
  12874. POP_I32();
  12875. /* POP array obj, (ref null $t) */
  12876. wasm_set_refheaptype_typeidx(
  12877. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12878. POP_REF(wasm_ref_type.ref_type);
  12879. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12880. &wasm_ref_type,
  12881. wasm_reftype_struct_size(&wasm_ref_type));
  12882. POP_I32();
  12883. /* POP array obj, (ref null $t) */
  12884. wasm_set_refheaptype_typeidx(
  12885. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12886. POP_REF(wasm_ref_type.ref_type);
  12887. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12888. &wasm_ref_type,
  12889. wasm_reftype_struct_size(&wasm_ref_type));
  12890. array_type = (WASMArrayType *)module->types[type_idx];
  12891. if (!(array_type->elem_flags & 1)) {
  12892. set_error_buf(error_buf, error_buf_size,
  12893. "destination array is immutable");
  12894. goto fail;
  12895. }
  12896. dst_elem_type = array_type->elem_type;
  12897. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12898. dst_elem_ref_type = array_type->elem_ref_type;
  12899. }
  12900. array_type =
  12901. (WASMArrayType *)module->types[src_type_idx];
  12902. src_elem_type = array_type->elem_type;
  12903. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12904. src_elem_ref_type = array_type->elem_ref_type;
  12905. }
  12906. if (!wasm_reftype_is_subtype_of(
  12907. src_elem_type, src_elem_ref_type, dst_elem_type,
  12908. dst_elem_ref_type, module->types,
  12909. module->type_count)) {
  12910. set_error_buf(error_buf, error_buf_size,
  12911. "array types do not match");
  12912. goto fail;
  12913. }
  12914. break;
  12915. }
  12916. case WASM_OP_REF_I31:
  12917. {
  12918. POP_I32();
  12919. wasm_set_refheaptype_common(
  12920. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12921. PUSH_REF(wasm_ref_type.ref_type);
  12922. break;
  12923. }
  12924. case WASM_OP_I31_GET_S:
  12925. case WASM_OP_I31_GET_U:
  12926. {
  12927. POP_REF(REF_TYPE_I31REF);
  12928. PUSH_I32();
  12929. break;
  12930. }
  12931. case WASM_OP_REF_TEST:
  12932. case WASM_OP_REF_CAST:
  12933. case WASM_OP_REF_TEST_NULLABLE:
  12934. case WASM_OP_REF_CAST_NULLABLE:
  12935. {
  12936. uint8 type;
  12937. pb_read_leb_int32(p, p_end, heap_type);
  12938. #if WASM_ENABLE_FAST_INTERP != 0
  12939. emit_uint32(loader_ctx, (uint32)heap_type);
  12940. #endif
  12941. if (heap_type >= 0) {
  12942. if (!check_type_index(module, module->type_count,
  12943. heap_type, error_buf,
  12944. error_buf_size)) {
  12945. goto fail;
  12946. }
  12947. }
  12948. else {
  12949. if (!wasm_is_valid_heap_type(heap_type)) {
  12950. set_error_buf(error_buf, error_buf_size,
  12951. "unknown type");
  12952. goto fail;
  12953. }
  12954. }
  12955. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12956. &wasm_ref_type, error_buf,
  12957. error_buf_size)) {
  12958. goto fail;
  12959. }
  12960. if (opcode1 == WASM_OP_REF_TEST
  12961. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12962. PUSH_I32();
  12963. else {
  12964. bool nullable =
  12965. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12966. : false;
  12967. if (heap_type >= 0 || !nullable) {
  12968. wasm_set_refheaptype_typeidx(
  12969. &wasm_ref_type.ref_ht_typeidx, nullable,
  12970. heap_type);
  12971. PUSH_REF(wasm_ref_type.ref_type);
  12972. }
  12973. else {
  12974. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12975. }
  12976. }
  12977. break;
  12978. }
  12979. case WASM_OP_BR_ON_CAST:
  12980. case WASM_OP_BR_ON_CAST_FAIL:
  12981. {
  12982. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12983. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12984. uint8 type_tmp, castflags;
  12985. uint32 depth;
  12986. int32 heap_type_dst;
  12987. bool src_nullable, dst_nullable;
  12988. CHECK_BUF(p, p_end, 1);
  12989. castflags = read_uint8(p);
  12990. #if WASM_ENABLE_FAST_INTERP != 0
  12991. /* Emit heap_type firstly */
  12992. emit_byte(loader_ctx, castflags);
  12993. #endif
  12994. p_org = p;
  12995. pb_read_leb_uint32(p, p_end, depth);
  12996. pb_read_leb_int32(p, p_end, heap_type);
  12997. #if WASM_ENABLE_FAST_INTERP != 0
  12998. /* Emit heap_type firstly */
  12999. emit_uint32(loader_ctx, (uint32)heap_type);
  13000. #endif
  13001. pb_read_leb_int32(p, p_end, heap_type_dst);
  13002. #if WASM_ENABLE_FAST_INTERP != 0
  13003. /* Emit heap_type firstly */
  13004. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13005. #endif
  13006. (void)depth;
  13007. /*
  13008. * castflags should be 0~3:
  13009. * 0: (non-null, non-null)
  13010. * 1: (null, non-null)
  13011. * 2: (non-null, null)
  13012. * 3: (null, null)
  13013. */
  13014. if (castflags > 3) {
  13015. set_error_buf(error_buf, error_buf_size,
  13016. "invalid castflags");
  13017. break;
  13018. }
  13019. src_nullable =
  13020. (castflags == 1) || (castflags == 3) ? true : false;
  13021. dst_nullable =
  13022. (castflags == 2) || (castflags == 3) ? true : false;
  13023. /* Pop and backup the stack top's ref type */
  13024. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13025. &ref_type_tmp, error_buf,
  13026. error_buf_size)) {
  13027. goto fail;
  13028. }
  13029. /* The reference type rt1 must be valid */
  13030. if (!init_ref_type(module, &ref_type1, src_nullable,
  13031. heap_type, error_buf,
  13032. error_buf_size)) {
  13033. goto fail;
  13034. }
  13035. /* The reference type rt2 must be valid. */
  13036. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13037. heap_type_dst, error_buf,
  13038. error_buf_size)) {
  13039. goto fail;
  13040. }
  13041. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13042. &ref_type2);
  13043. /* The reference type rt2 must match rt1. */
  13044. if (!wasm_reftype_is_subtype_of(
  13045. ref_type2.ref_type, &ref_type2,
  13046. ref_type1.ref_type, &ref_type1, module->types,
  13047. module->type_count)) {
  13048. set_error_buf(error_buf, error_buf_size,
  13049. "type mismatch");
  13050. goto fail;
  13051. }
  13052. p = p_org;
  13053. /* Push ref type casted for branch block check */
  13054. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13055. /* The reference type rt2 must match rt′. */
  13056. type_tmp = ref_type2.ref_type;
  13057. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13058. bh_memcpy_s(
  13059. &wasm_ref_type,
  13060. wasm_reftype_struct_size(&ref_type2),
  13061. &ref_type2,
  13062. wasm_reftype_struct_size(&ref_type2));
  13063. }
  13064. }
  13065. else {
  13066. /* The reference type rt′1 must match rt′. */
  13067. type_tmp = ref_type_diff.ref_type;
  13068. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13069. bh_memcpy_s(
  13070. &wasm_ref_type,
  13071. wasm_reftype_struct_size(&ref_type_diff),
  13072. &ref_type_diff,
  13073. wasm_reftype_struct_size(&ref_type_diff));
  13074. }
  13075. }
  13076. PUSH_REF(type_tmp);
  13077. if (!(frame_csp_tmp = check_branch_block(
  13078. loader_ctx, &p, p_end, opcode, error_buf,
  13079. error_buf_size))) {
  13080. goto fail;
  13081. }
  13082. /* Ignore heap_types */
  13083. skip_leb_uint32(p, p_end);
  13084. skip_leb_uint32(p, p_end);
  13085. /* Restore the original stack top's ref type */
  13086. POP_REF(type_tmp);
  13087. #if WASM_ENABLE_FAST_INTERP != 0
  13088. /* Erase the opnd offset emitted by POP_REF() */
  13089. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13090. #endif
  13091. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13092. type_tmp = ref_type_diff.ref_type;
  13093. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13094. bh_memcpy_s(
  13095. &wasm_ref_type,
  13096. wasm_reftype_struct_size(&ref_type_diff),
  13097. &ref_type_diff,
  13098. wasm_reftype_struct_size(&ref_type_diff));
  13099. }
  13100. }
  13101. else {
  13102. type_tmp = ref_type2.ref_type;
  13103. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13104. bh_memcpy_s(
  13105. &wasm_ref_type,
  13106. wasm_reftype_struct_size(&ref_type2),
  13107. &ref_type2,
  13108. wasm_reftype_struct_size(&ref_type2));
  13109. }
  13110. }
  13111. PUSH_REF(type_tmp);
  13112. #if WASM_ENABLE_FAST_INTERP != 0
  13113. /* Erase the opnd offset emitted by PUSH_REF() */
  13114. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13115. #endif
  13116. break;
  13117. }
  13118. case WASM_OP_ANY_CONVERT_EXTERN:
  13119. {
  13120. uint8 type;
  13121. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13122. &wasm_ref_type, error_buf,
  13123. error_buf_size)) {
  13124. goto fail;
  13125. }
  13126. if (!(type == REF_TYPE_EXTERNREF
  13127. || (type == REF_TYPE_HT_NON_NULLABLE
  13128. && wasm_ref_type.ref_ht_common.heap_type
  13129. == HEAP_TYPE_EXTERN)
  13130. || type == VALUE_TYPE_ANY)) {
  13131. set_error_buf(error_buf, error_buf_size,
  13132. "type mismatch");
  13133. goto fail;
  13134. }
  13135. if (type == REF_TYPE_EXTERNREF)
  13136. type = REF_TYPE_ANYREF;
  13137. else {
  13138. wasm_ref_type.ref_ht_common.heap_type =
  13139. HEAP_TYPE_ANY;
  13140. }
  13141. PUSH_REF(type);
  13142. break;
  13143. }
  13144. case WASM_OP_EXTERN_CONVERT_ANY:
  13145. {
  13146. uint8 type;
  13147. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13148. &wasm_ref_type, error_buf,
  13149. error_buf_size)) {
  13150. goto fail;
  13151. }
  13152. if (type == REF_TYPE_EXTERNREF
  13153. || ((type == REF_TYPE_HT_NULLABLE
  13154. || type == REF_TYPE_HT_NON_NULLABLE)
  13155. && wasm_ref_type.ref_ht_common.heap_type
  13156. == HEAP_TYPE_EXTERN)) {
  13157. set_error_buf(error_buf, error_buf_size,
  13158. "type mismatch");
  13159. goto fail;
  13160. }
  13161. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13162. /* push (ref null extern) */
  13163. type = REF_TYPE_EXTERNREF;
  13164. }
  13165. else {
  13166. /* push (ref extern) */
  13167. type = REF_TYPE_HT_NON_NULLABLE;
  13168. wasm_set_refheaptype_common(
  13169. &wasm_ref_type.ref_ht_common, false,
  13170. HEAP_TYPE_EXTERN);
  13171. }
  13172. PUSH_REF(type);
  13173. break;
  13174. }
  13175. #if WASM_ENABLE_STRINGREF != 0
  13176. case WASM_OP_STRING_NEW_UTF8:
  13177. case WASM_OP_STRING_NEW_WTF16:
  13178. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13179. case WASM_OP_STRING_NEW_WTF8:
  13180. {
  13181. uint32 memidx;
  13182. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13183. func->has_memory_operations = true;
  13184. #endif
  13185. pb_read_leb_uint32(p, p_end, memidx);
  13186. #if WASM_ENABLE_FAST_INTERP != 0
  13187. emit_uint32(loader_ctx, (uint32)memidx);
  13188. #endif
  13189. POP_I32();
  13190. POP_I32();
  13191. PUSH_REF(REF_TYPE_STRINGREF);
  13192. (void)memidx;
  13193. break;
  13194. }
  13195. case WASM_OP_STRING_CONST:
  13196. {
  13197. uint32 contents;
  13198. pb_read_leb_uint32(p, p_end, contents);
  13199. #if WASM_ENABLE_FAST_INTERP != 0
  13200. emit_uint32(loader_ctx, (uint32)contents);
  13201. #endif
  13202. PUSH_REF(REF_TYPE_STRINGREF);
  13203. (void)contents;
  13204. break;
  13205. }
  13206. case WASM_OP_STRING_MEASURE_UTF8:
  13207. case WASM_OP_STRING_MEASURE_WTF8:
  13208. case WASM_OP_STRING_MEASURE_WTF16:
  13209. {
  13210. POP_STRINGREF();
  13211. PUSH_I32();
  13212. break;
  13213. }
  13214. case WASM_OP_STRING_ENCODE_UTF8:
  13215. case WASM_OP_STRING_ENCODE_WTF16:
  13216. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13217. case WASM_OP_STRING_ENCODE_WTF8:
  13218. {
  13219. uint32 memidx;
  13220. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13221. func->has_memory_operations = true;
  13222. #endif
  13223. pb_read_leb_uint32(p, p_end, memidx);
  13224. #if WASM_ENABLE_FAST_INTERP != 0
  13225. emit_uint32(loader_ctx, (uint32)memidx);
  13226. #endif
  13227. POP_I32();
  13228. POP_STRINGREF();
  13229. PUSH_I32();
  13230. (void)memidx;
  13231. break;
  13232. }
  13233. case WASM_OP_STRING_CONCAT:
  13234. {
  13235. POP_STRINGREF();
  13236. POP_STRINGREF();
  13237. PUSH_REF(REF_TYPE_STRINGREF);
  13238. break;
  13239. }
  13240. case WASM_OP_STRING_EQ:
  13241. {
  13242. POP_STRINGREF();
  13243. POP_STRINGREF();
  13244. PUSH_I32();
  13245. break;
  13246. }
  13247. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13248. {
  13249. POP_STRINGREF();
  13250. PUSH_I32();
  13251. break;
  13252. }
  13253. case WASM_OP_STRING_AS_WTF8:
  13254. {
  13255. POP_STRINGREF();
  13256. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13257. break;
  13258. }
  13259. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13260. {
  13261. POP_I32();
  13262. POP_I32();
  13263. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13264. PUSH_I32();
  13265. break;
  13266. }
  13267. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13268. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13269. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13270. {
  13271. uint32 memidx;
  13272. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13273. func->has_memory_operations = true;
  13274. #endif
  13275. pb_read_leb_uint32(p, p_end, memidx);
  13276. #if WASM_ENABLE_FAST_INTERP != 0
  13277. emit_uint32(loader_ctx, (uint32)memidx);
  13278. #endif
  13279. POP_I32();
  13280. POP_I32();
  13281. POP_I32();
  13282. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13283. PUSH_I32();
  13284. PUSH_I32();
  13285. (void)memidx;
  13286. break;
  13287. }
  13288. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13289. {
  13290. POP_I32();
  13291. POP_I32();
  13292. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13293. PUSH_REF(REF_TYPE_STRINGREF);
  13294. break;
  13295. }
  13296. case WASM_OP_STRING_AS_WTF16:
  13297. {
  13298. POP_STRINGREF();
  13299. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13300. break;
  13301. }
  13302. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13303. {
  13304. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13305. PUSH_I32();
  13306. break;
  13307. }
  13308. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13309. {
  13310. POP_I32();
  13311. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13312. PUSH_I32();
  13313. break;
  13314. }
  13315. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13316. {
  13317. uint32 memidx;
  13318. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13319. func->has_memory_operations = true;
  13320. #endif
  13321. pb_read_leb_uint32(p, p_end, memidx);
  13322. #if WASM_ENABLE_FAST_INTERP != 0
  13323. emit_uint32(loader_ctx, (uint32)memidx);
  13324. #endif
  13325. POP_I32();
  13326. POP_I32();
  13327. POP_I32();
  13328. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13329. PUSH_I32();
  13330. (void)memidx;
  13331. break;
  13332. }
  13333. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13334. {
  13335. POP_I32();
  13336. POP_I32();
  13337. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13338. PUSH_REF(REF_TYPE_STRINGREF);
  13339. break;
  13340. }
  13341. case WASM_OP_STRING_AS_ITER:
  13342. {
  13343. POP_STRINGREF();
  13344. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13345. break;
  13346. }
  13347. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13348. {
  13349. POP_REF(REF_TYPE_STRINGVIEWITER);
  13350. PUSH_I32();
  13351. break;
  13352. }
  13353. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13354. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13355. {
  13356. POP_I32();
  13357. POP_REF(REF_TYPE_STRINGVIEWITER);
  13358. PUSH_I32();
  13359. break;
  13360. }
  13361. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13362. {
  13363. POP_I32();
  13364. POP_REF(REF_TYPE_STRINGVIEWITER);
  13365. PUSH_REF(REF_TYPE_STRINGREF);
  13366. break;
  13367. }
  13368. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13369. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13370. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13371. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13372. {
  13373. POP_I32();
  13374. POP_I32();
  13375. POP_REF(REF_TYPE_ARRAYREF);
  13376. PUSH_REF(REF_TYPE_STRINGREF);
  13377. break;
  13378. }
  13379. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13380. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13381. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13382. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13383. {
  13384. POP_I32();
  13385. POP_REF(REF_TYPE_ARRAYREF);
  13386. POP_STRINGREF();
  13387. PUSH_I32();
  13388. break;
  13389. }
  13390. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13391. default:
  13392. set_error_buf_v(error_buf, error_buf_size,
  13393. "%s %02x %02x", "unsupported opcode",
  13394. 0xfb, opcode1);
  13395. goto fail;
  13396. }
  13397. break;
  13398. }
  13399. #endif /* end of WASM_ENABLE_GC != 0 */
  13400. case WASM_OP_MISC_PREFIX:
  13401. {
  13402. uint32 opcode1;
  13403. pb_read_leb_uint32(p, p_end, opcode1);
  13404. #if WASM_ENABLE_FAST_INTERP != 0
  13405. emit_byte(loader_ctx, ((uint8)opcode1));
  13406. #endif
  13407. switch (opcode1) {
  13408. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13409. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13410. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13411. break;
  13412. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13413. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13414. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13415. break;
  13416. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13417. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13418. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13419. break;
  13420. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13421. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13422. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13423. break;
  13424. #if WASM_ENABLE_BULK_MEMORY != 0
  13425. case WASM_OP_MEMORY_INIT:
  13426. {
  13427. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13428. #if WASM_ENABLE_FAST_INTERP != 0
  13429. emit_uint32(loader_ctx, data_seg_idx);
  13430. #endif
  13431. if (module->import_memory_count == 0
  13432. && module->memory_count == 0)
  13433. goto fail_unknown_memory;
  13434. pb_read_leb_uint32(p, p_end, memidx);
  13435. check_memidx(module, memidx);
  13436. if (data_seg_idx >= module->data_seg_count) {
  13437. set_error_buf_v(error_buf, error_buf_size,
  13438. "unknown data segment %d",
  13439. data_seg_idx);
  13440. goto fail;
  13441. }
  13442. if (module->data_seg_count1 == 0)
  13443. goto fail_data_cnt_sec_require;
  13444. POP_I32();
  13445. POP_I32();
  13446. POP_MEM_OFFSET();
  13447. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13448. func->has_memory_operations = true;
  13449. #endif
  13450. #if WASM_ENABLE_WAMR_COMPILER != 0
  13451. module->is_bulk_memory_used = true;
  13452. #endif
  13453. break;
  13454. }
  13455. case WASM_OP_DATA_DROP:
  13456. {
  13457. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13458. #if WASM_ENABLE_FAST_INTERP != 0
  13459. emit_uint32(loader_ctx, data_seg_idx);
  13460. #endif
  13461. if (data_seg_idx >= module->data_seg_count) {
  13462. set_error_buf(error_buf, error_buf_size,
  13463. "unknown data segment");
  13464. goto fail;
  13465. }
  13466. if (module->data_seg_count1 == 0)
  13467. goto fail_data_cnt_sec_require;
  13468. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13469. func->has_memory_operations = true;
  13470. #endif
  13471. #if WASM_ENABLE_WAMR_COMPILER != 0
  13472. module->is_bulk_memory_used = true;
  13473. #endif
  13474. break;
  13475. }
  13476. case WASM_OP_MEMORY_COPY:
  13477. {
  13478. CHECK_BUF(p, p_end, sizeof(int16));
  13479. /* check both src and dst memory index */
  13480. pb_read_leb_uint32(p, p_end, memidx);
  13481. check_memidx(module, memidx);
  13482. pb_read_leb_uint32(p, p_end, memidx);
  13483. check_memidx(module, memidx);
  13484. if (module->import_memory_count == 0
  13485. && module->memory_count == 0)
  13486. goto fail_unknown_memory;
  13487. POP_MEM_OFFSET();
  13488. POP_MEM_OFFSET();
  13489. POP_MEM_OFFSET();
  13490. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13491. func->has_memory_operations = true;
  13492. #endif
  13493. #if WASM_ENABLE_WAMR_COMPILER != 0
  13494. module->is_bulk_memory_used = true;
  13495. #endif
  13496. break;
  13497. }
  13498. case WASM_OP_MEMORY_FILL:
  13499. {
  13500. pb_read_leb_uint32(p, p_end, memidx);
  13501. check_memidx(module, memidx);
  13502. if (module->import_memory_count == 0
  13503. && module->memory_count == 0) {
  13504. goto fail_unknown_memory;
  13505. }
  13506. POP_MEM_OFFSET();
  13507. POP_I32();
  13508. POP_MEM_OFFSET();
  13509. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13510. func->has_memory_operations = true;
  13511. #endif
  13512. #if WASM_ENABLE_WAMR_COMPILER != 0
  13513. module->is_bulk_memory_used = true;
  13514. #endif
  13515. break;
  13516. }
  13517. fail_unknown_memory:
  13518. set_error_buf(error_buf, error_buf_size,
  13519. "unknown memory 0");
  13520. goto fail;
  13521. fail_data_cnt_sec_require:
  13522. set_error_buf(error_buf, error_buf_size,
  13523. "data count section required");
  13524. goto fail;
  13525. #endif /* WASM_ENABLE_BULK_MEMORY */
  13526. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13527. case WASM_OP_TABLE_INIT:
  13528. {
  13529. uint8 seg_type = 0, tbl_type = 0;
  13530. #if WASM_ENABLE_GC != 0
  13531. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13532. #endif
  13533. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13534. pb_read_leb_uint32(p, p_end, table_idx);
  13535. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13536. #if WASM_ENABLE_GC != 0
  13537. (void **)&tbl_ref_type,
  13538. #else
  13539. NULL,
  13540. #endif
  13541. error_buf, error_buf_size))
  13542. goto fail;
  13543. if (!get_table_seg_elem_type(module, table_seg_idx,
  13544. &seg_type,
  13545. #if WASM_ENABLE_GC != 0
  13546. (void **)&seg_ref_type,
  13547. #else
  13548. NULL,
  13549. #endif
  13550. error_buf, error_buf_size))
  13551. goto fail;
  13552. #if WASM_ENABLE_GC == 0
  13553. if (seg_type != tbl_type) {
  13554. set_error_buf(error_buf, error_buf_size,
  13555. "type mismatch");
  13556. goto fail;
  13557. }
  13558. #else
  13559. if (!wasm_reftype_is_subtype_of(
  13560. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13561. module->types, module->type_count)) {
  13562. set_error_buf(error_buf, error_buf_size,
  13563. "type mismatch");
  13564. goto fail;
  13565. }
  13566. #endif
  13567. #if WASM_ENABLE_FAST_INTERP != 0
  13568. emit_uint32(loader_ctx, table_seg_idx);
  13569. emit_uint32(loader_ctx, table_idx);
  13570. #endif
  13571. POP_I32();
  13572. POP_I32();
  13573. #if WASM_ENABLE_MEMORY64 != 0
  13574. table_elem_idx_type = is_table_64bit(module, table_idx)
  13575. ? VALUE_TYPE_I64
  13576. : VALUE_TYPE_I32;
  13577. #endif
  13578. POP_TBL_ELEM_IDX();
  13579. #if WASM_ENABLE_WAMR_COMPILER != 0
  13580. module->is_ref_types_used = true;
  13581. #endif
  13582. break;
  13583. }
  13584. case WASM_OP_ELEM_DROP:
  13585. {
  13586. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13587. if (!get_table_seg_elem_type(module, table_seg_idx,
  13588. NULL, NULL, error_buf,
  13589. error_buf_size))
  13590. goto fail;
  13591. #if WASM_ENABLE_FAST_INTERP != 0
  13592. emit_uint32(loader_ctx, table_seg_idx);
  13593. #endif
  13594. #if WASM_ENABLE_WAMR_COMPILER != 0
  13595. module->is_ref_types_used = true;
  13596. #endif
  13597. break;
  13598. }
  13599. case WASM_OP_TABLE_COPY:
  13600. {
  13601. uint8 src_type, dst_type, src_tbl_idx_type,
  13602. dst_tbl_idx_type, min_tbl_idx_type;
  13603. #if WASM_ENABLE_GC != 0
  13604. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13605. #endif
  13606. uint32 src_tbl_idx, dst_tbl_idx;
  13607. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  13608. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13609. #if WASM_ENABLE_GC != 0
  13610. (void **)&dst_ref_type,
  13611. #else
  13612. NULL,
  13613. #endif
  13614. error_buf, error_buf_size))
  13615. goto fail;
  13616. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  13617. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13618. #if WASM_ENABLE_GC != 0
  13619. (void **)&src_ref_type,
  13620. #else
  13621. NULL,
  13622. #endif
  13623. error_buf, error_buf_size))
  13624. goto fail;
  13625. #if WASM_ENABLE_GC == 0
  13626. if (src_type != dst_type) {
  13627. set_error_buf(error_buf, error_buf_size,
  13628. "type mismatch");
  13629. goto fail;
  13630. }
  13631. #else
  13632. if (!wasm_reftype_is_subtype_of(
  13633. src_type, src_ref_type, dst_type, dst_ref_type,
  13634. module->types, module->type_count)) {
  13635. set_error_buf(error_buf, error_buf_size,
  13636. "type mismatch");
  13637. goto fail;
  13638. }
  13639. #endif
  13640. #if WASM_ENABLE_FAST_INTERP != 0
  13641. emit_uint32(loader_ctx, dst_tbl_idx);
  13642. emit_uint32(loader_ctx, src_tbl_idx);
  13643. #endif
  13644. #if WASM_ENABLE_MEMORY64 != 0
  13645. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  13646. ? VALUE_TYPE_I64
  13647. : VALUE_TYPE_I32;
  13648. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  13649. ? VALUE_TYPE_I64
  13650. : VALUE_TYPE_I32;
  13651. min_tbl_idx_type =
  13652. (src_tbl_idx_type == VALUE_TYPE_I32
  13653. || dst_tbl_idx_type == VALUE_TYPE_I32)
  13654. ? VALUE_TYPE_I32
  13655. : VALUE_TYPE_I64;
  13656. #else
  13657. src_tbl_idx_type = VALUE_TYPE_I32;
  13658. dst_tbl_idx_type = VALUE_TYPE_I32;
  13659. min_tbl_idx_type = VALUE_TYPE_I32;
  13660. #endif
  13661. table_elem_idx_type = min_tbl_idx_type;
  13662. POP_TBL_ELEM_IDX();
  13663. table_elem_idx_type = src_tbl_idx_type;
  13664. POP_TBL_ELEM_IDX();
  13665. table_elem_idx_type = dst_tbl_idx_type;
  13666. POP_TBL_ELEM_IDX();
  13667. #if WASM_ENABLE_WAMR_COMPILER != 0
  13668. module->is_ref_types_used = true;
  13669. #endif
  13670. break;
  13671. }
  13672. case WASM_OP_TABLE_SIZE:
  13673. {
  13674. pb_read_leb_uint32(p, p_end, table_idx);
  13675. /* TODO: shall we create a new function to check
  13676. table idx instead of using below function? */
  13677. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13678. error_buf, error_buf_size))
  13679. goto fail;
  13680. #if WASM_ENABLE_FAST_INTERP != 0
  13681. emit_uint32(loader_ctx, table_idx);
  13682. #endif
  13683. #if WASM_ENABLE_MEMORY64 != 0
  13684. table_elem_idx_type = is_table_64bit(module, table_idx)
  13685. ? VALUE_TYPE_I64
  13686. : VALUE_TYPE_I32;
  13687. #endif
  13688. PUSH_TBL_ELEM_IDX();
  13689. #if WASM_ENABLE_WAMR_COMPILER != 0
  13690. module->is_ref_types_used = true;
  13691. #endif
  13692. break;
  13693. }
  13694. case WASM_OP_TABLE_GROW:
  13695. case WASM_OP_TABLE_FILL:
  13696. {
  13697. uint8 decl_type;
  13698. #if WASM_ENABLE_GC != 0
  13699. WASMRefType *ref_type = NULL;
  13700. #endif
  13701. pb_read_leb_uint32(p, p_end, table_idx);
  13702. if (!get_table_elem_type(module, table_idx, &decl_type,
  13703. #if WASM_ENABLE_GC != 0
  13704. (void **)&ref_type,
  13705. #else
  13706. NULL,
  13707. #endif
  13708. error_buf, error_buf_size))
  13709. goto fail;
  13710. #if WASM_ENABLE_GC != 0
  13711. if (wasm_is_type_multi_byte_type(decl_type)) {
  13712. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13713. ref_type,
  13714. wasm_reftype_struct_size(ref_type));
  13715. }
  13716. #endif
  13717. if (opcode1 == WASM_OP_TABLE_GROW) {
  13718. if (table_idx < module->import_table_count) {
  13719. module->import_tables[table_idx]
  13720. .u.table.table_type.possible_grow = true;
  13721. }
  13722. else {
  13723. module
  13724. ->tables[table_idx
  13725. - module->import_table_count]
  13726. .table_type.possible_grow = true;
  13727. }
  13728. }
  13729. #if WASM_ENABLE_FAST_INTERP != 0
  13730. emit_uint32(loader_ctx, table_idx);
  13731. #endif
  13732. #if WASM_ENABLE_MEMORY64 != 0
  13733. table_elem_idx_type = is_table_64bit(module, table_idx)
  13734. ? VALUE_TYPE_I64
  13735. : VALUE_TYPE_I32;
  13736. #endif
  13737. POP_TBL_ELEM_IDX();
  13738. #if WASM_ENABLE_FAST_INTERP != 0
  13739. POP_OFFSET_TYPE(decl_type);
  13740. #endif
  13741. POP_TYPE(decl_type);
  13742. if (opcode1 == WASM_OP_TABLE_GROW)
  13743. PUSH_TBL_ELEM_IDX();
  13744. else
  13745. POP_TBL_ELEM_IDX();
  13746. #if WASM_ENABLE_WAMR_COMPILER != 0
  13747. module->is_ref_types_used = true;
  13748. #endif
  13749. break;
  13750. }
  13751. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13752. default:
  13753. set_error_buf_v(error_buf, error_buf_size,
  13754. "%s %02x %02x", "unsupported opcode",
  13755. 0xfc, opcode1);
  13756. goto fail;
  13757. }
  13758. break;
  13759. }
  13760. #if WASM_ENABLE_SIMD != 0
  13761. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13762. case WASM_OP_SIMD_PREFIX:
  13763. {
  13764. uint32 opcode1;
  13765. #if WASM_ENABLE_WAMR_COMPILER != 0
  13766. /* Mark the SIMD instruction is used in this module */
  13767. module->is_simd_used = true;
  13768. #endif
  13769. pb_read_leb_uint32(p, p_end, opcode1);
  13770. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13771. */
  13772. switch (opcode1) {
  13773. /* memory instruction */
  13774. case SIMD_v128_load:
  13775. case SIMD_v128_load8x8_s:
  13776. case SIMD_v128_load8x8_u:
  13777. case SIMD_v128_load16x4_s:
  13778. case SIMD_v128_load16x4_u:
  13779. case SIMD_v128_load32x2_s:
  13780. case SIMD_v128_load32x2_u:
  13781. case SIMD_v128_load8_splat:
  13782. case SIMD_v128_load16_splat:
  13783. case SIMD_v128_load32_splat:
  13784. case SIMD_v128_load64_splat:
  13785. {
  13786. CHECK_MEMORY();
  13787. pb_read_leb_uint32(p, p_end, align); /* align */
  13788. if (!check_simd_memory_access_align(
  13789. opcode1, align, error_buf, error_buf_size)) {
  13790. goto fail;
  13791. }
  13792. pb_read_leb_mem_offset(p, p_end,
  13793. mem_offset); /* offset */
  13794. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13795. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13796. func->has_memory_operations = true;
  13797. #endif
  13798. break;
  13799. }
  13800. case SIMD_v128_store:
  13801. {
  13802. CHECK_MEMORY();
  13803. pb_read_leb_uint32(p, p_end, align); /* align */
  13804. if (!check_simd_memory_access_align(
  13805. opcode1, align, error_buf, error_buf_size)) {
  13806. goto fail;
  13807. }
  13808. pb_read_leb_mem_offset(p, p_end,
  13809. mem_offset); /* offset */
  13810. POP_V128();
  13811. POP_MEM_OFFSET();
  13812. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13813. func->has_memory_operations = true;
  13814. #endif
  13815. break;
  13816. }
  13817. /* basic operation */
  13818. case SIMD_v128_const:
  13819. {
  13820. CHECK_BUF1(p, p_end, 16);
  13821. p += 16;
  13822. PUSH_V128();
  13823. break;
  13824. }
  13825. case SIMD_v8x16_shuffle:
  13826. {
  13827. V128 mask;
  13828. CHECK_BUF1(p, p_end, 16);
  13829. mask = read_i8x16(p, error_buf, error_buf_size);
  13830. p += 16;
  13831. if (!check_simd_shuffle_mask(mask, error_buf,
  13832. error_buf_size)) {
  13833. goto fail;
  13834. }
  13835. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13836. break;
  13837. }
  13838. case SIMD_v8x16_swizzle:
  13839. {
  13840. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13841. break;
  13842. }
  13843. /* splat operation */
  13844. case SIMD_i8x16_splat:
  13845. case SIMD_i16x8_splat:
  13846. case SIMD_i32x4_splat:
  13847. case SIMD_i64x2_splat:
  13848. case SIMD_f32x4_splat:
  13849. case SIMD_f64x2_splat:
  13850. {
  13851. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13852. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13853. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13854. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13855. VALUE_TYPE_V128);
  13856. break;
  13857. }
  13858. /* lane operation */
  13859. case SIMD_i8x16_extract_lane_s:
  13860. case SIMD_i8x16_extract_lane_u:
  13861. case SIMD_i8x16_replace_lane:
  13862. case SIMD_i16x8_extract_lane_s:
  13863. case SIMD_i16x8_extract_lane_u:
  13864. case SIMD_i16x8_replace_lane:
  13865. case SIMD_i32x4_extract_lane:
  13866. case SIMD_i32x4_replace_lane:
  13867. case SIMD_i64x2_extract_lane:
  13868. case SIMD_i64x2_replace_lane:
  13869. case SIMD_f32x4_extract_lane:
  13870. case SIMD_f32x4_replace_lane:
  13871. case SIMD_f64x2_extract_lane:
  13872. case SIMD_f64x2_replace_lane:
  13873. {
  13874. uint8 lane;
  13875. /* clang-format off */
  13876. uint8 replace[] = {
  13877. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13878. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13879. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13880. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13881. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13882. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13883. };
  13884. uint8 push_type[] = {
  13885. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13886. VALUE_TYPE_V128,
  13887. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13888. VALUE_TYPE_V128,
  13889. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13890. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13891. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13892. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13893. };
  13894. /* clang-format on */
  13895. CHECK_BUF(p, p_end, 1);
  13896. lane = read_uint8(p);
  13897. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13898. error_buf_size)) {
  13899. goto fail;
  13900. }
  13901. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13902. if (!(wasm_loader_pop_frame_ref(
  13903. loader_ctx,
  13904. replace[opcode1
  13905. - SIMD_i8x16_extract_lane_s],
  13906. error_buf, error_buf_size)))
  13907. goto fail;
  13908. }
  13909. POP_AND_PUSH(
  13910. VALUE_TYPE_V128,
  13911. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13912. break;
  13913. }
  13914. /* i8x16 compare operation */
  13915. case SIMD_i8x16_eq:
  13916. case SIMD_i8x16_ne:
  13917. case SIMD_i8x16_lt_s:
  13918. case SIMD_i8x16_lt_u:
  13919. case SIMD_i8x16_gt_s:
  13920. case SIMD_i8x16_gt_u:
  13921. case SIMD_i8x16_le_s:
  13922. case SIMD_i8x16_le_u:
  13923. case SIMD_i8x16_ge_s:
  13924. case SIMD_i8x16_ge_u:
  13925. /* i16x8 compare operation */
  13926. case SIMD_i16x8_eq:
  13927. case SIMD_i16x8_ne:
  13928. case SIMD_i16x8_lt_s:
  13929. case SIMD_i16x8_lt_u:
  13930. case SIMD_i16x8_gt_s:
  13931. case SIMD_i16x8_gt_u:
  13932. case SIMD_i16x8_le_s:
  13933. case SIMD_i16x8_le_u:
  13934. case SIMD_i16x8_ge_s:
  13935. case SIMD_i16x8_ge_u:
  13936. /* i32x4 compare operation */
  13937. case SIMD_i32x4_eq:
  13938. case SIMD_i32x4_ne:
  13939. case SIMD_i32x4_lt_s:
  13940. case SIMD_i32x4_lt_u:
  13941. case SIMD_i32x4_gt_s:
  13942. case SIMD_i32x4_gt_u:
  13943. case SIMD_i32x4_le_s:
  13944. case SIMD_i32x4_le_u:
  13945. case SIMD_i32x4_ge_s:
  13946. case SIMD_i32x4_ge_u:
  13947. /* f32x4 compare operation */
  13948. case SIMD_f32x4_eq:
  13949. case SIMD_f32x4_ne:
  13950. case SIMD_f32x4_lt:
  13951. case SIMD_f32x4_gt:
  13952. case SIMD_f32x4_le:
  13953. case SIMD_f32x4_ge:
  13954. /* f64x2 compare operation */
  13955. case SIMD_f64x2_eq:
  13956. case SIMD_f64x2_ne:
  13957. case SIMD_f64x2_lt:
  13958. case SIMD_f64x2_gt:
  13959. case SIMD_f64x2_le:
  13960. case SIMD_f64x2_ge:
  13961. {
  13962. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13963. break;
  13964. }
  13965. /* v128 operation */
  13966. case SIMD_v128_not:
  13967. {
  13968. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13969. break;
  13970. }
  13971. case SIMD_v128_and:
  13972. case SIMD_v128_andnot:
  13973. case SIMD_v128_or:
  13974. case SIMD_v128_xor:
  13975. {
  13976. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13977. break;
  13978. }
  13979. case SIMD_v128_bitselect:
  13980. {
  13981. POP_V128();
  13982. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13983. break;
  13984. }
  13985. case SIMD_v128_any_true:
  13986. {
  13987. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13988. break;
  13989. }
  13990. /* Load Lane Operation */
  13991. case SIMD_v128_load8_lane:
  13992. case SIMD_v128_load16_lane:
  13993. case SIMD_v128_load32_lane:
  13994. case SIMD_v128_load64_lane:
  13995. case SIMD_v128_store8_lane:
  13996. case SIMD_v128_store16_lane:
  13997. case SIMD_v128_store32_lane:
  13998. case SIMD_v128_store64_lane:
  13999. {
  14000. uint8 lane;
  14001. CHECK_MEMORY();
  14002. pb_read_leb_uint32(p, p_end, align); /* align */
  14003. if (!check_simd_memory_access_align(
  14004. opcode1, align, error_buf, error_buf_size)) {
  14005. goto fail;
  14006. }
  14007. pb_read_leb_mem_offset(p, p_end,
  14008. mem_offset); /* offset */
  14009. CHECK_BUF(p, p_end, 1);
  14010. lane = read_uint8(p);
  14011. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14012. error_buf_size)) {
  14013. goto fail;
  14014. }
  14015. POP_V128();
  14016. POP_MEM_OFFSET();
  14017. if (opcode1 < SIMD_v128_store8_lane) {
  14018. PUSH_V128();
  14019. }
  14020. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14021. func->has_memory_operations = true;
  14022. #endif
  14023. break;
  14024. }
  14025. case SIMD_v128_load32_zero:
  14026. case SIMD_v128_load64_zero:
  14027. {
  14028. CHECK_MEMORY();
  14029. pb_read_leb_uint32(p, p_end, align); /* align */
  14030. if (!check_simd_memory_access_align(
  14031. opcode1, align, error_buf, error_buf_size)) {
  14032. goto fail;
  14033. }
  14034. pb_read_leb_mem_offset(p, p_end,
  14035. mem_offset); /* offset */
  14036. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14037. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14038. func->has_memory_operations = true;
  14039. #endif
  14040. break;
  14041. }
  14042. /* Float conversion */
  14043. case SIMD_f32x4_demote_f64x2_zero:
  14044. case SIMD_f64x2_promote_low_f32x4_zero:
  14045. {
  14046. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14047. break;
  14048. }
  14049. /* i8x16 Operation */
  14050. case SIMD_i8x16_abs:
  14051. case SIMD_i8x16_neg:
  14052. case SIMD_i8x16_popcnt:
  14053. {
  14054. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14055. break;
  14056. }
  14057. case SIMD_i8x16_all_true:
  14058. case SIMD_i8x16_bitmask:
  14059. {
  14060. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14061. break;
  14062. }
  14063. case SIMD_i8x16_narrow_i16x8_s:
  14064. case SIMD_i8x16_narrow_i16x8_u:
  14065. {
  14066. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14067. break;
  14068. }
  14069. case SIMD_f32x4_ceil:
  14070. case SIMD_f32x4_floor:
  14071. case SIMD_f32x4_trunc:
  14072. case SIMD_f32x4_nearest:
  14073. {
  14074. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14075. break;
  14076. }
  14077. case SIMD_i8x16_shl:
  14078. case SIMD_i8x16_shr_s:
  14079. case SIMD_i8x16_shr_u:
  14080. {
  14081. POP_I32();
  14082. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14083. break;
  14084. }
  14085. case SIMD_i8x16_add:
  14086. case SIMD_i8x16_add_sat_s:
  14087. case SIMD_i8x16_add_sat_u:
  14088. case SIMD_i8x16_sub:
  14089. case SIMD_i8x16_sub_sat_s:
  14090. case SIMD_i8x16_sub_sat_u:
  14091. {
  14092. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14093. break;
  14094. }
  14095. case SIMD_f64x2_ceil:
  14096. case SIMD_f64x2_floor:
  14097. {
  14098. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14099. break;
  14100. }
  14101. case SIMD_i8x16_min_s:
  14102. case SIMD_i8x16_min_u:
  14103. case SIMD_i8x16_max_s:
  14104. case SIMD_i8x16_max_u:
  14105. {
  14106. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14107. break;
  14108. }
  14109. case SIMD_f64x2_trunc:
  14110. {
  14111. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14112. break;
  14113. }
  14114. case SIMD_i8x16_avgr_u:
  14115. {
  14116. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14117. break;
  14118. }
  14119. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14120. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14121. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14122. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14123. /* i16x8 operation */
  14124. case SIMD_i16x8_abs:
  14125. case SIMD_i16x8_neg:
  14126. {
  14127. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14128. break;
  14129. }
  14130. case SIMD_i16x8_q15mulr_sat_s:
  14131. {
  14132. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14133. break;
  14134. }
  14135. case SIMD_i16x8_all_true:
  14136. case SIMD_i16x8_bitmask:
  14137. {
  14138. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14139. break;
  14140. }
  14141. case SIMD_i16x8_narrow_i32x4_s:
  14142. case SIMD_i16x8_narrow_i32x4_u:
  14143. {
  14144. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14145. break;
  14146. }
  14147. case SIMD_i16x8_extend_low_i8x16_s:
  14148. case SIMD_i16x8_extend_high_i8x16_s:
  14149. case SIMD_i16x8_extend_low_i8x16_u:
  14150. case SIMD_i16x8_extend_high_i8x16_u:
  14151. {
  14152. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14153. break;
  14154. }
  14155. case SIMD_i16x8_shl:
  14156. case SIMD_i16x8_shr_s:
  14157. case SIMD_i16x8_shr_u:
  14158. {
  14159. POP_I32();
  14160. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14161. break;
  14162. }
  14163. case SIMD_i16x8_add:
  14164. case SIMD_i16x8_add_sat_s:
  14165. case SIMD_i16x8_add_sat_u:
  14166. case SIMD_i16x8_sub:
  14167. case SIMD_i16x8_sub_sat_s:
  14168. case SIMD_i16x8_sub_sat_u:
  14169. {
  14170. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14171. break;
  14172. }
  14173. case SIMD_f64x2_nearest:
  14174. {
  14175. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14176. break;
  14177. }
  14178. case SIMD_i16x8_mul:
  14179. case SIMD_i16x8_min_s:
  14180. case SIMD_i16x8_min_u:
  14181. case SIMD_i16x8_max_s:
  14182. case SIMD_i16x8_max_u:
  14183. case SIMD_i16x8_avgr_u:
  14184. case SIMD_i16x8_extmul_low_i8x16_s:
  14185. case SIMD_i16x8_extmul_high_i8x16_s:
  14186. case SIMD_i16x8_extmul_low_i8x16_u:
  14187. case SIMD_i16x8_extmul_high_i8x16_u:
  14188. {
  14189. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14190. break;
  14191. }
  14192. /* i32x4 operation */
  14193. case SIMD_i32x4_abs:
  14194. case SIMD_i32x4_neg:
  14195. {
  14196. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14197. break;
  14198. }
  14199. case SIMD_i32x4_all_true:
  14200. case SIMD_i32x4_bitmask:
  14201. {
  14202. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14203. break;
  14204. }
  14205. case SIMD_i32x4_extend_low_i16x8_s:
  14206. case SIMD_i32x4_extend_high_i16x8_s:
  14207. case SIMD_i32x4_extend_low_i16x8_u:
  14208. case SIMD_i32x4_extend_high_i16x8_u:
  14209. {
  14210. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14211. break;
  14212. }
  14213. case SIMD_i32x4_shl:
  14214. case SIMD_i32x4_shr_s:
  14215. case SIMD_i32x4_shr_u:
  14216. {
  14217. POP_I32();
  14218. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14219. break;
  14220. }
  14221. case SIMD_i32x4_add:
  14222. case SIMD_i32x4_sub:
  14223. case SIMD_i32x4_mul:
  14224. case SIMD_i32x4_min_s:
  14225. case SIMD_i32x4_min_u:
  14226. case SIMD_i32x4_max_s:
  14227. case SIMD_i32x4_max_u:
  14228. case SIMD_i32x4_dot_i16x8_s:
  14229. case SIMD_i32x4_extmul_low_i16x8_s:
  14230. case SIMD_i32x4_extmul_high_i16x8_s:
  14231. case SIMD_i32x4_extmul_low_i16x8_u:
  14232. case SIMD_i32x4_extmul_high_i16x8_u:
  14233. {
  14234. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14235. break;
  14236. }
  14237. /* i64x2 operation */
  14238. case SIMD_i64x2_abs:
  14239. case SIMD_i64x2_neg:
  14240. {
  14241. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14242. break;
  14243. }
  14244. case SIMD_i64x2_all_true:
  14245. case SIMD_i64x2_bitmask:
  14246. {
  14247. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14248. break;
  14249. }
  14250. case SIMD_i64x2_extend_low_i32x4_s:
  14251. case SIMD_i64x2_extend_high_i32x4_s:
  14252. case SIMD_i64x2_extend_low_i32x4_u:
  14253. case SIMD_i64x2_extend_high_i32x4_u:
  14254. {
  14255. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14256. break;
  14257. }
  14258. case SIMD_i64x2_shl:
  14259. case SIMD_i64x2_shr_s:
  14260. case SIMD_i64x2_shr_u:
  14261. {
  14262. POP_I32();
  14263. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14264. break;
  14265. }
  14266. case SIMD_i64x2_add:
  14267. case SIMD_i64x2_sub:
  14268. case SIMD_i64x2_mul:
  14269. case SIMD_i64x2_eq:
  14270. case SIMD_i64x2_ne:
  14271. case SIMD_i64x2_lt_s:
  14272. case SIMD_i64x2_gt_s:
  14273. case SIMD_i64x2_le_s:
  14274. case SIMD_i64x2_ge_s:
  14275. case SIMD_i64x2_extmul_low_i32x4_s:
  14276. case SIMD_i64x2_extmul_high_i32x4_s:
  14277. case SIMD_i64x2_extmul_low_i32x4_u:
  14278. case SIMD_i64x2_extmul_high_i32x4_u:
  14279. {
  14280. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14281. break;
  14282. }
  14283. /* f32x4 operation */
  14284. case SIMD_f32x4_abs:
  14285. case SIMD_f32x4_neg:
  14286. case SIMD_f32x4_sqrt:
  14287. {
  14288. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14289. break;
  14290. }
  14291. case SIMD_f32x4_add:
  14292. case SIMD_f32x4_sub:
  14293. case SIMD_f32x4_mul:
  14294. case SIMD_f32x4_div:
  14295. case SIMD_f32x4_min:
  14296. case SIMD_f32x4_max:
  14297. case SIMD_f32x4_pmin:
  14298. case SIMD_f32x4_pmax:
  14299. {
  14300. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14301. break;
  14302. }
  14303. /* f64x2 operation */
  14304. case SIMD_f64x2_abs:
  14305. case SIMD_f64x2_neg:
  14306. case SIMD_f64x2_sqrt:
  14307. {
  14308. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14309. break;
  14310. }
  14311. case SIMD_f64x2_add:
  14312. case SIMD_f64x2_sub:
  14313. case SIMD_f64x2_mul:
  14314. case SIMD_f64x2_div:
  14315. case SIMD_f64x2_min:
  14316. case SIMD_f64x2_max:
  14317. case SIMD_f64x2_pmin:
  14318. case SIMD_f64x2_pmax:
  14319. {
  14320. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14321. break;
  14322. }
  14323. case SIMD_i32x4_trunc_sat_f32x4_s:
  14324. case SIMD_i32x4_trunc_sat_f32x4_u:
  14325. case SIMD_f32x4_convert_i32x4_s:
  14326. case SIMD_f32x4_convert_i32x4_u:
  14327. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14328. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14329. case SIMD_f64x2_convert_low_i32x4_s:
  14330. case SIMD_f64x2_convert_low_i32x4_u:
  14331. {
  14332. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14333. break;
  14334. }
  14335. default:
  14336. {
  14337. if (error_buf != NULL) {
  14338. snprintf(error_buf, error_buf_size,
  14339. "WASM module load failed: "
  14340. "invalid opcode 0xfd %02x.",
  14341. opcode1);
  14342. }
  14343. goto fail;
  14344. }
  14345. }
  14346. break;
  14347. }
  14348. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14349. #endif /* end of WASM_ENABLE_SIMD */
  14350. #if WASM_ENABLE_SHARED_MEMORY != 0
  14351. case WASM_OP_ATOMIC_PREFIX:
  14352. {
  14353. uint32 opcode1;
  14354. pb_read_leb_uint32(p, p_end, opcode1);
  14355. #if WASM_ENABLE_FAST_INTERP != 0
  14356. emit_byte(loader_ctx, opcode1);
  14357. #endif
  14358. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14359. CHECK_MEMORY();
  14360. pb_read_leb_uint32(p, p_end, align); /* align */
  14361. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14362. if (!check_memory_align_equal(opcode1, align, error_buf,
  14363. error_buf_size)) {
  14364. goto fail;
  14365. }
  14366. #if WASM_ENABLE_FAST_INTERP != 0
  14367. emit_uint32(loader_ctx, mem_offset);
  14368. #endif
  14369. }
  14370. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14371. func->has_memory_operations = true;
  14372. #endif
  14373. switch (opcode1) {
  14374. case WASM_OP_ATOMIC_NOTIFY:
  14375. POP_I32();
  14376. POP_MEM_OFFSET();
  14377. PUSH_I32();
  14378. break;
  14379. case WASM_OP_ATOMIC_WAIT32:
  14380. POP_I64();
  14381. POP_I32();
  14382. POP_MEM_OFFSET();
  14383. PUSH_I32();
  14384. break;
  14385. case WASM_OP_ATOMIC_WAIT64:
  14386. POP_I64();
  14387. POP_I64();
  14388. POP_MEM_OFFSET();
  14389. PUSH_I32();
  14390. break;
  14391. case WASM_OP_ATOMIC_FENCE:
  14392. /* reserved byte 0x00 */
  14393. if (*p++ != 0x00) {
  14394. set_error_buf(error_buf, error_buf_size,
  14395. "zero byte expected");
  14396. goto fail;
  14397. }
  14398. break;
  14399. case WASM_OP_ATOMIC_I32_LOAD:
  14400. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14401. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14402. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14403. break;
  14404. case WASM_OP_ATOMIC_I32_STORE:
  14405. case WASM_OP_ATOMIC_I32_STORE8:
  14406. case WASM_OP_ATOMIC_I32_STORE16:
  14407. POP_I32();
  14408. POP_MEM_OFFSET();
  14409. break;
  14410. case WASM_OP_ATOMIC_I64_LOAD:
  14411. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14412. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14413. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14414. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14415. break;
  14416. case WASM_OP_ATOMIC_I64_STORE:
  14417. case WASM_OP_ATOMIC_I64_STORE8:
  14418. case WASM_OP_ATOMIC_I64_STORE16:
  14419. case WASM_OP_ATOMIC_I64_STORE32:
  14420. POP_I64();
  14421. POP_MEM_OFFSET();
  14422. break;
  14423. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14424. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14425. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14426. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14427. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14428. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14429. case WASM_OP_ATOMIC_RMW_I32_AND:
  14430. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14431. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14432. case WASM_OP_ATOMIC_RMW_I32_OR:
  14433. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14434. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14435. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14436. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14437. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14438. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14439. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14440. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14441. POP_I32();
  14442. POP_MEM_OFFSET();
  14443. PUSH_I32();
  14444. break;
  14445. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14446. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14447. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14448. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14449. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14450. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14451. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14452. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14453. case WASM_OP_ATOMIC_RMW_I64_AND:
  14454. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14455. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14456. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14457. case WASM_OP_ATOMIC_RMW_I64_OR:
  14458. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14459. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14460. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14461. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14462. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14463. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14464. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14465. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14466. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14467. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14468. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14469. POP_I64();
  14470. POP_MEM_OFFSET();
  14471. PUSH_I64();
  14472. break;
  14473. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14474. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14475. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14476. POP_I32();
  14477. POP_I32();
  14478. POP_MEM_OFFSET();
  14479. PUSH_I32();
  14480. break;
  14481. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14482. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14483. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14484. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14485. POP_I64();
  14486. POP_I64();
  14487. POP_MEM_OFFSET();
  14488. PUSH_I64();
  14489. break;
  14490. default:
  14491. set_error_buf_v(error_buf, error_buf_size,
  14492. "%s %02x %02x", "unsupported opcode",
  14493. 0xfe, opcode1);
  14494. goto fail;
  14495. }
  14496. break;
  14497. }
  14498. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14499. default:
  14500. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14501. "unsupported opcode", opcode);
  14502. goto fail;
  14503. }
  14504. #if WASM_ENABLE_FAST_INTERP != 0
  14505. last_op = opcode;
  14506. #endif
  14507. }
  14508. if (loader_ctx->csp_num > 0) {
  14509. /* unmatched end opcodes result from unbalanced control flow structures,
  14510. * for example, br_table with inconsistent target count (1 declared, 2
  14511. * given), or simply superfluous end opcodes */
  14512. set_error_buf(
  14513. error_buf, error_buf_size,
  14514. "unexpected end opcodes from unbalanced control flow structures");
  14515. goto fail;
  14516. }
  14517. #if WASM_ENABLE_FAST_INTERP != 0
  14518. if (loader_ctx->p_code_compiled == NULL)
  14519. goto re_scan;
  14520. func->const_cell_num =
  14521. loader_ctx->i64_const_num * 2 + loader_ctx->i32_const_num;
  14522. if (func->const_cell_num > 0) {
  14523. if (!(func->consts =
  14524. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  14525. error_buf, error_buf_size)))
  14526. goto fail;
  14527. if (loader_ctx->i64_const_num > 0) {
  14528. bh_memcpy_s(func->consts,
  14529. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  14530. loader_ctx->i64_consts,
  14531. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  14532. }
  14533. if (loader_ctx->i32_const_num > 0) {
  14534. bh_memcpy_s(func->consts
  14535. + sizeof(int64) * loader_ctx->i64_const_num,
  14536. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  14537. loader_ctx->i32_consts,
  14538. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  14539. }
  14540. }
  14541. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14542. - loader_ctx->start_dynamic_offset + 1;
  14543. #else
  14544. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14545. #endif
  14546. func->max_block_num = loader_ctx->max_csp_num;
  14547. return_value = true;
  14548. fail:
  14549. wasm_loader_ctx_destroy(loader_ctx);
  14550. (void)table_idx;
  14551. (void)table_seg_idx;
  14552. (void)data_seg_idx;
  14553. (void)i64_const;
  14554. (void)local_offset;
  14555. (void)p_org;
  14556. (void)mem_offset;
  14557. (void)align;
  14558. return return_value;
  14559. }