wasm_loader.c 613 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_platform.h"
  7. #include "wasm.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_runtime.h"
  10. #include "wasm_loader_common.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. #if WASM_ENABLE_MEMORY64 != 0
  36. static bool
  37. has_module_memory64(WASMModule *module)
  38. {
  39. /* TODO: multi-memories for now assuming the memory idx type is consistent
  40. * across multi-memories */
  41. if (module->import_memory_count > 0)
  42. return !!(module->import_memories[0].u.memory.mem_type.flags
  43. & MEMORY64_FLAG);
  44. else if (module->memory_count > 0)
  45. return !!(module->memories[0].flags & MEMORY64_FLAG);
  46. return false;
  47. }
  48. static bool
  49. is_table_64bit(WASMModule *module, uint32 table_idx)
  50. {
  51. if (table_idx < module->import_table_count)
  52. return !!(module->import_tables[table_idx].u.table.table_type.flags
  53. & TABLE64_FLAG);
  54. else
  55. return !!(module->tables[table_idx - module->import_table_count]
  56. .table_type.flags
  57. & TABLE64_FLAG);
  58. return false;
  59. }
  60. #endif
  61. static void
  62. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  63. {
  64. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  65. }
  66. #if WASM_ENABLE_MEMORY64 != 0
  67. static void
  68. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  69. {
  70. if (error_buf != NULL) {
  71. snprintf(error_buf, error_buf_size, "offset out of range");
  72. }
  73. }
  74. #endif
  75. static void
  76. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  77. {
  78. va_list args;
  79. char buf[128];
  80. if (error_buf != NULL) {
  81. va_start(args, format);
  82. vsnprintf(buf, sizeof(buf), format, args);
  83. va_end(args);
  84. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  85. }
  86. }
  87. static bool
  88. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  89. char *error_buf, uint32 error_buf_size)
  90. {
  91. if ((uintptr_t)buf + length < (uintptr_t)buf
  92. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  93. set_error_buf(error_buf, error_buf_size,
  94. "unexpected end of section or function");
  95. return false;
  96. }
  97. return true;
  98. }
  99. static bool
  100. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  101. char *error_buf, uint32 error_buf_size)
  102. {
  103. if ((uintptr_t)buf + length < (uintptr_t)buf
  104. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  105. set_error_buf(error_buf, error_buf_size, "unexpected end");
  106. return false;
  107. }
  108. return true;
  109. }
  110. #define CHECK_BUF(buf, buf_end, length) \
  111. do { \
  112. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  113. goto fail; \
  114. } \
  115. } while (0)
  116. #define CHECK_BUF1(buf, buf_end, length) \
  117. do { \
  118. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  119. goto fail; \
  120. } \
  121. } while (0)
  122. #define skip_leb(p) while (*p++ & 0x80)
  123. #define skip_leb_int64(p, p_end) skip_leb(p)
  124. #define skip_leb_uint32(p, p_end) skip_leb(p)
  125. #define skip_leb_int32(p, p_end) skip_leb(p)
  126. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  127. #define skip_leb_memidx(p, p_end) skip_leb(p)
  128. #if WASM_ENABLE_MULTI_MEMORY == 0
  129. #define skip_leb_align(p, p_end) skip_leb(p)
  130. #else
  131. /* Skip the following memidx if applicable */
  132. #define skip_leb_align(p, p_end) \
  133. do { \
  134. if (*p++ & OPT_MEMIDX_FLAG) \
  135. skip_leb_uint32(p, p_end); \
  136. } while (0)
  137. #endif
  138. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  139. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  140. #define read_leb_int64(p, p_end, res) \
  141. do { \
  142. uint64 res64; \
  143. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  144. error_buf_size)) \
  145. goto fail; \
  146. res = (int64)res64; \
  147. } while (0)
  148. #if WASM_ENABLE_MEMORY64 != 0
  149. #define read_leb_mem_offset(p, p_end, res) \
  150. do { \
  151. uint64 res64; \
  152. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  153. &res64, error_buf, error_buf_size)) { \
  154. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  155. goto fail; \
  156. } \
  157. res = (mem_offset_t)res64; \
  158. } while (0)
  159. #else
  160. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  161. #endif
  162. #define read_leb_uint32(p, p_end, res) \
  163. do { \
  164. uint64 res64; \
  165. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  166. error_buf_size)) \
  167. goto fail; \
  168. res = (uint32)res64; \
  169. } while (0)
  170. #define read_leb_int32(p, p_end, res) \
  171. do { \
  172. uint64 res64; \
  173. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  174. error_buf_size)) \
  175. goto fail; \
  176. res = (int32)res64; \
  177. } while (0)
  178. #if WASM_ENABLE_MULTI_MEMORY != 0
  179. #define check_memidx(module, memidx) \
  180. do { \
  181. if (memidx >= module->import_memory_count + module->memory_count) { \
  182. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  183. memidx); \
  184. goto fail; \
  185. } \
  186. } while (0)
  187. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  188. * alignment check */
  189. #define read_leb_memarg(p, p_end, res) \
  190. do { \
  191. read_leb_uint32(p, p_end, res); \
  192. if (res & OPT_MEMIDX_FLAG) { \
  193. res &= ~OPT_MEMIDX_FLAG; \
  194. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  195. check_memidx(module, memidx); \
  196. } \
  197. } while (0)
  198. #else
  199. /* reserved byte 0x00 */
  200. #define check_memidx(module, memidx) \
  201. do { \
  202. (void)module; \
  203. if (memidx != 0) { \
  204. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  205. goto fail; \
  206. } \
  207. } while (0)
  208. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  209. #endif
  210. static char *
  211. type2str(uint8 type)
  212. {
  213. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  214. #if WASM_ENABLE_GC != 0
  215. char *type_str_ref[] = { "stringview_iter",
  216. "stringview_wtf16",
  217. "(ref null ht)",
  218. "(ref ht)",
  219. "", /* reserved */
  220. "stringview_wtf8",
  221. "stringref",
  222. "", /* reserved */
  223. "", /* reserved */
  224. "arrayref",
  225. "structref",
  226. "i31ref",
  227. "eqref",
  228. "anyref",
  229. "externref",
  230. "funcref",
  231. "nullref",
  232. "nullexternref",
  233. "nullfuncref" };
  234. #endif
  235. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  236. return type_str[type - VALUE_TYPE_V128];
  237. #if WASM_ENABLE_GC != 0
  238. else if (wasm_is_type_reftype(type))
  239. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  240. #endif
  241. else if (type == VALUE_TYPE_FUNCREF)
  242. return "funcref";
  243. else if (type == VALUE_TYPE_EXTERNREF)
  244. return "externref";
  245. else
  246. return "unknown type";
  247. }
  248. static bool
  249. is_32bit_type(uint8 type)
  250. {
  251. if (type == VALUE_TYPE_I32
  252. || type == VALUE_TYPE_F32
  253. /* the operand stack is in polymorphic state */
  254. || type == VALUE_TYPE_ANY
  255. #if WASM_ENABLE_GC != 0
  256. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  257. #elif WASM_ENABLE_REF_TYPES != 0
  258. /* For reference types, we use uint32 index to represent
  259. the funcref and externref */
  260. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  261. #endif
  262. )
  263. return true;
  264. return false;
  265. }
  266. static bool
  267. is_64bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  270. #if WASM_ENABLE_GC != 0
  271. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  272. #endif
  273. )
  274. return true;
  275. return false;
  276. }
  277. #if WASM_ENABLE_GC != 0
  278. static bool
  279. is_packed_type(uint8 type)
  280. {
  281. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  282. }
  283. #endif
  284. static bool
  285. is_byte_a_type(uint8 type)
  286. {
  287. return (is_valid_value_type_for_interpreter(type)
  288. || (type == VALUE_TYPE_VOID))
  289. ? true
  290. : false;
  291. }
  292. #if WASM_ENABLE_SIMD != 0
  293. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  294. || (WASM_ENABLE_FAST_INTERP != 0)
  295. static V128
  296. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  297. {
  298. V128 result;
  299. uint8 i;
  300. for (i = 0; i != 16; ++i) {
  301. result.i8x16[i] = read_uint8(p_buf);
  302. }
  303. return result;
  304. }
  305. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  306. (WASM_ENABLE_FAST_INTERP != 0) */
  307. #endif /* end of WASM_ENABLE_SIMD */
  308. static void *
  309. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  310. {
  311. void *mem;
  312. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  313. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  314. return NULL;
  315. }
  316. memset(mem, 0, (uint32)size);
  317. return mem;
  318. }
  319. static void *
  320. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  321. uint32 error_buf_size)
  322. {
  323. uint8 *mem_new;
  324. bh_assert(size_new > size_old);
  325. if ((mem_new = wasm_runtime_realloc(mem_old, size_new))) {
  326. memset(mem_new + size_old, 0, size_new - size_old);
  327. return mem_new;
  328. }
  329. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  330. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  331. wasm_runtime_free(mem_old);
  332. }
  333. return mem_new;
  334. }
  335. #define MEM_REALLOC(mem, size_old, size_new) \
  336. do { \
  337. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  338. error_buf_size); \
  339. if (!mem_new) \
  340. goto fail; \
  341. mem = mem_new; \
  342. } while (0)
  343. static bool
  344. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  345. char *error_buf, uint32 error_buf_size)
  346. {
  347. if (type_index >= type_count) {
  348. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  349. type_index);
  350. return false;
  351. }
  352. return true;
  353. }
  354. #if WASM_ENABLE_GC != 0
  355. static bool
  356. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  357. uint32 error_buf_size)
  358. {
  359. if (!check_type_index(module, module->type_count, type_index, error_buf,
  360. error_buf_size)) {
  361. return false;
  362. }
  363. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  364. set_error_buf(error_buf, error_buf_size, "unknown array type");
  365. return false;
  366. }
  367. return true;
  368. }
  369. #endif
  370. /*
  371. * if no GC is enabled, an valid type is always a function type.
  372. * but if GC is enabled, we need to check the type flag
  373. */
  374. static bool
  375. check_function_type(const WASMModule *module, uint32 type_index,
  376. char *error_buf, uint32 error_buf_size)
  377. {
  378. if (!check_type_index(module, module->type_count, type_index, error_buf,
  379. error_buf_size)) {
  380. return false;
  381. }
  382. #if WASM_ENABLE_GC != 0
  383. if (module->types[type_index]->type_flag != WASM_TYPE_FUNC) {
  384. set_error_buf(error_buf, error_buf_size, "unknown function type");
  385. return false;
  386. }
  387. #endif
  388. return true;
  389. }
  390. static bool
  391. check_function_index(const WASMModule *module, uint32 function_index,
  392. char *error_buf, uint32 error_buf_size)
  393. {
  394. if (function_index
  395. >= module->import_function_count + module->function_count) {
  396. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  397. function_index);
  398. return false;
  399. }
  400. return true;
  401. }
  402. typedef struct InitValue {
  403. uint8 type;
  404. uint8 flag;
  405. #if WASM_ENABLE_GC != 0
  406. uint8 gc_opcode;
  407. WASMRefType ref_type;
  408. #endif
  409. WASMValue value;
  410. } InitValue;
  411. typedef struct ConstExprContext {
  412. uint32 sp;
  413. uint32 size;
  414. WASMModule *module;
  415. InitValue *stack;
  416. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  417. } ConstExprContext;
  418. static void
  419. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  420. {
  421. ctx->sp = 0;
  422. ctx->module = module;
  423. ctx->stack = ctx->data;
  424. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  425. }
  426. static bool
  427. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  428. #if WASM_ENABLE_GC != 0
  429. WASMRefType *ref_type, uint8 gc_opcode,
  430. #endif
  431. WASMValue *value, char *error_buf, uint32 error_buf_size)
  432. {
  433. InitValue *cur_value;
  434. if (ctx->sp >= ctx->size) {
  435. if (ctx->stack != ctx->data) {
  436. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  437. (ctx->size + 4) * sizeof(InitValue));
  438. }
  439. else {
  440. if (!(ctx->stack =
  441. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  442. error_buf, error_buf_size))) {
  443. goto fail;
  444. }
  445. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  446. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  447. }
  448. ctx->size += 4;
  449. }
  450. cur_value = &ctx->stack[ctx->sp++];
  451. cur_value->type = type;
  452. cur_value->flag = flag;
  453. cur_value->value = *value;
  454. #if WASM_ENABLE_GC != 0
  455. cur_value->gc_opcode = gc_opcode;
  456. if (wasm_is_type_multi_byte_type(type)) {
  457. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  458. ref_type, wasm_reftype_struct_size(ref_type));
  459. }
  460. #endif
  461. return true;
  462. fail:
  463. return false;
  464. }
  465. #if WASM_ENABLE_GC != 0
  466. static void
  467. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  468. {
  469. WASMStructNewInitValues *struct_init_values =
  470. (WASMStructNewInitValues *)data;
  471. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  472. WASMType *wasm_type;
  473. uint32 i;
  474. if (!data)
  475. return;
  476. wasm_type = module->types[struct_init_values->type_idx];
  477. /* The data can only be type of `WASMStructNewInitValues *`
  478. or `WASMArrayNewInitValues *` */
  479. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  480. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  481. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  482. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  483. WASMRefType *ref_type;
  484. uint8 field_type;
  485. uint16 ref_type_map_index = 0;
  486. for (i = 0; i < struct_init_values->count; i++) {
  487. field_type = struct_type->fields[i].field_type;
  488. if (wasm_is_type_multi_byte_type(field_type))
  489. ref_type =
  490. struct_type->ref_type_maps[ref_type_map_index++].ref_type;
  491. else
  492. ref_type = NULL;
  493. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  494. REF_TYPE_STRUCTREF, NULL,
  495. module->types, module->type_count)
  496. || wasm_reftype_is_subtype_of(
  497. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  498. module->types, module->type_count)) {
  499. destroy_init_expr_data_recursive(
  500. module, struct_init_values->fields[i].data);
  501. }
  502. }
  503. }
  504. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  505. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  506. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  507. uint8 elem_type = array_type->elem_type;
  508. for (i = 0; i < array_init_values->length; i++) {
  509. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  510. REF_TYPE_STRUCTREF, NULL,
  511. module->types, module->type_count)
  512. || wasm_reftype_is_subtype_of(
  513. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  514. module->types, module->type_count)) {
  515. destroy_init_expr_data_recursive(
  516. module, array_init_values->elem_data[i].data);
  517. }
  518. }
  519. }
  520. wasm_runtime_free(data);
  521. }
  522. #endif
  523. static bool
  524. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  525. #if WASM_ENABLE_GC != 0
  526. WASMRefType *ref_type, uint8 *p_gc_opcode,
  527. #endif
  528. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  529. {
  530. InitValue *cur_value;
  531. if (ctx->sp == 0) {
  532. set_error_buf(error_buf, error_buf_size,
  533. "type mismatch: const expr stack underflow");
  534. return false;
  535. }
  536. cur_value = &ctx->stack[--ctx->sp];
  537. #if WASM_ENABLE_GC == 0
  538. if (cur_value->type != type) {
  539. set_error_buf(error_buf, error_buf_size, "type mismatch");
  540. return false;
  541. }
  542. #else
  543. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  544. ref_type, ctx->module->types,
  545. ctx->module->type_count)) {
  546. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  547. "type mismatch: expect ", type2str(type),
  548. " but got other");
  549. goto fail;
  550. }
  551. #endif
  552. if (p_flag)
  553. *p_flag = cur_value->flag;
  554. if (p_value)
  555. *p_value = cur_value->value;
  556. #if WASM_ENABLE_GC != 0
  557. if (p_gc_opcode)
  558. *p_gc_opcode = cur_value->gc_opcode;
  559. #endif
  560. return true;
  561. #if WASM_ENABLE_GC != 0
  562. fail:
  563. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  564. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  565. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  566. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  567. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  568. }
  569. return false;
  570. #endif
  571. }
  572. static void
  573. destroy_const_expr_stack(ConstExprContext *ctx)
  574. {
  575. #if WASM_ENABLE_GC != 0
  576. uint32 i;
  577. for (i = 0; i < ctx->sp; i++) {
  578. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  579. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  580. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  581. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  582. destroy_init_expr_data_recursive(ctx->module,
  583. ctx->stack[i].value.data);
  584. }
  585. }
  586. #endif
  587. if (ctx->stack != ctx->data) {
  588. wasm_runtime_free(ctx->stack);
  589. }
  590. }
  591. #if WASM_ENABLE_GC != 0
  592. static void
  593. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  594. {
  595. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  596. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  597. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  598. destroy_init_expr_data_recursive(module, expr->u.data);
  599. }
  600. }
  601. #endif /* end of WASM_ENABLE_GC != 0 */
  602. static bool
  603. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  604. InitializerExpression *init_expr, uint8 type, void *ref_type,
  605. char *error_buf, uint32 error_buf_size)
  606. {
  607. const uint8 *p = *p_buf, *p_end = buf_end;
  608. uint8 flag, *p_float;
  609. uint32 i;
  610. ConstExprContext const_expr_ctx = { 0 };
  611. WASMValue cur_value;
  612. #if WASM_ENABLE_GC != 0
  613. uint32 opcode1, type_idx;
  614. uint8 opcode;
  615. WASMRefType cur_ref_type = { 0 };
  616. #endif
  617. init_const_expr_stack(&const_expr_ctx, module);
  618. CHECK_BUF(p, p_end, 1);
  619. flag = read_uint8(p);
  620. while (flag != WASM_OP_END) {
  621. switch (flag) {
  622. /* i32.const */
  623. case INIT_EXPR_TYPE_I32_CONST:
  624. read_leb_int32(p, p_end, cur_value.i32);
  625. if (!push_const_expr_stack(
  626. &const_expr_ctx, flag, VALUE_TYPE_I32,
  627. #if WASM_ENABLE_GC != 0
  628. NULL, 0,
  629. #endif
  630. &cur_value, error_buf, error_buf_size))
  631. goto fail;
  632. break;
  633. /* i64.const */
  634. case INIT_EXPR_TYPE_I64_CONST:
  635. read_leb_int64(p, p_end, cur_value.i64);
  636. if (!push_const_expr_stack(
  637. &const_expr_ctx, flag, VALUE_TYPE_I64,
  638. #if WASM_ENABLE_GC != 0
  639. NULL, 0,
  640. #endif
  641. &cur_value, error_buf, error_buf_size))
  642. goto fail;
  643. break;
  644. /* f32.const */
  645. case INIT_EXPR_TYPE_F32_CONST:
  646. CHECK_BUF(p, p_end, 4);
  647. p_float = (uint8 *)&cur_value.f32;
  648. for (i = 0; i < sizeof(float32); i++)
  649. *p_float++ = *p++;
  650. if (!push_const_expr_stack(
  651. &const_expr_ctx, flag, VALUE_TYPE_F32,
  652. #if WASM_ENABLE_GC != 0
  653. NULL, 0,
  654. #endif
  655. &cur_value, error_buf, error_buf_size))
  656. goto fail;
  657. break;
  658. /* f64.const */
  659. case INIT_EXPR_TYPE_F64_CONST:
  660. CHECK_BUF(p, p_end, 8);
  661. p_float = (uint8 *)&cur_value.f64;
  662. for (i = 0; i < sizeof(float64); i++)
  663. *p_float++ = *p++;
  664. if (!push_const_expr_stack(
  665. &const_expr_ctx, flag, VALUE_TYPE_F64,
  666. #if WASM_ENABLE_GC != 0
  667. NULL, 0,
  668. #endif
  669. &cur_value, error_buf, error_buf_size))
  670. goto fail;
  671. break;
  672. #if WASM_ENABLE_SIMD != 0
  673. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  674. || (WASM_ENABLE_FAST_INTERP != 0)
  675. /* v128.const */
  676. case INIT_EXPR_TYPE_V128_CONST:
  677. {
  678. uint64 high, low;
  679. CHECK_BUF(p, p_end, 1);
  680. (void)read_uint8(p);
  681. CHECK_BUF(p, p_end, 16);
  682. wasm_runtime_read_v128(p, &high, &low);
  683. p += 16;
  684. cur_value.v128.i64x2[0] = high;
  685. cur_value.v128.i64x2[1] = low;
  686. if (!push_const_expr_stack(
  687. &const_expr_ctx, flag, VALUE_TYPE_V128,
  688. #if WASM_ENABLE_GC != 0
  689. NULL, 0,
  690. #endif
  691. &cur_value, error_buf, error_buf_size))
  692. goto fail;
  693. #if WASM_ENABLE_WAMR_COMPILER != 0
  694. /* If any init_expr is v128.const, mark SIMD used */
  695. module->is_simd_used = true;
  696. #endif
  697. break;
  698. }
  699. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  700. (WASM_ENABLE_FAST_INTERP != 0) */
  701. #endif /* end of WASM_ENABLE_SIMD */
  702. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  703. /* ref.func */
  704. case INIT_EXPR_TYPE_FUNCREF_CONST:
  705. {
  706. uint32 func_idx;
  707. read_leb_uint32(p, p_end, func_idx);
  708. cur_value.ref_index = func_idx;
  709. if (!check_function_index(module, func_idx, error_buf,
  710. error_buf_size)) {
  711. goto fail;
  712. }
  713. #if WASM_ENABLE_GC == 0
  714. if (!push_const_expr_stack(&const_expr_ctx, flag,
  715. VALUE_TYPE_FUNCREF, &cur_value,
  716. error_buf, error_buf_size))
  717. goto fail;
  718. #else
  719. if (func_idx < module->import_function_count) {
  720. type_idx =
  721. module->import_functions[func_idx].u.function.type_idx;
  722. }
  723. else {
  724. type_idx = module
  725. ->functions[func_idx
  726. - module->import_function_count]
  727. ->type_idx;
  728. }
  729. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  730. false, type_idx);
  731. if (!push_const_expr_stack(&const_expr_ctx, flag,
  732. cur_ref_type.ref_type, &cur_ref_type,
  733. 0, &cur_value, error_buf,
  734. error_buf_size))
  735. goto fail;
  736. #endif
  737. #if WASM_ENABLE_WAMR_COMPILER != 0
  738. module->is_ref_types_used = true;
  739. #endif
  740. break;
  741. }
  742. /* ref.null */
  743. case INIT_EXPR_TYPE_REFNULL_CONST:
  744. {
  745. uint8 type1;
  746. CHECK_BUF(p, p_end, 1);
  747. type1 = read_uint8(p);
  748. #if WASM_ENABLE_GC == 0
  749. cur_value.ref_index = NULL_REF;
  750. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  751. &cur_value, error_buf,
  752. error_buf_size))
  753. goto fail;
  754. #else
  755. cur_value.gc_obj = NULL_REF;
  756. if (!is_byte_a_type(type1)
  757. || wasm_is_type_multi_byte_type(type1)) {
  758. p--;
  759. read_leb_uint32(p, p_end, type_idx);
  760. if (!check_type_index(module, module->type_count, type_idx,
  761. error_buf, error_buf_size))
  762. goto fail;
  763. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  764. true, type_idx);
  765. if (!push_const_expr_stack(&const_expr_ctx, flag,
  766. cur_ref_type.ref_type,
  767. &cur_ref_type, 0, &cur_value,
  768. error_buf, error_buf_size))
  769. goto fail;
  770. }
  771. else {
  772. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  773. NULL, 0, &cur_value, error_buf,
  774. error_buf_size))
  775. goto fail;
  776. }
  777. #endif
  778. #if WASM_ENABLE_WAMR_COMPILER != 0
  779. module->is_ref_types_used = true;
  780. #endif
  781. break;
  782. }
  783. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  784. /* get_global */
  785. case INIT_EXPR_TYPE_GET_GLOBAL:
  786. {
  787. uint32 global_idx;
  788. uint8 global_type;
  789. read_leb_uint32(p, p_end, cur_value.global_index);
  790. global_idx = cur_value.global_index;
  791. /*
  792. * Currently, constant expressions occurring as initializers
  793. * of globals are further constrained in that contained
  794. * global.get instructions are
  795. * only allowed to refer to imported globals.
  796. *
  797. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  798. */
  799. if (global_idx >= module->import_global_count
  800. /* make spec test happy */
  801. #if WASM_ENABLE_GC != 0
  802. + module->global_count
  803. #endif
  804. ) {
  805. set_error_buf_v(error_buf, error_buf_size,
  806. "unknown global %u", global_idx);
  807. goto fail;
  808. }
  809. if (
  810. /* make spec test happy */
  811. #if WASM_ENABLE_GC != 0
  812. global_idx < module->import_global_count &&
  813. #endif
  814. module->import_globals[global_idx]
  815. .u.global.type.is_mutable) {
  816. set_error_buf_v(error_buf, error_buf_size,
  817. "constant expression required");
  818. goto fail;
  819. }
  820. if (global_idx < module->import_global_count) {
  821. global_type = module->import_globals[global_idx]
  822. .u.global.type.val_type;
  823. #if WASM_ENABLE_GC != 0
  824. if (wasm_is_type_multi_byte_type(global_type)) {
  825. WASMRefType *global_ref_type =
  826. module->import_globals[global_idx]
  827. .u.global.ref_type;
  828. bh_memcpy_s(&cur_ref_type,
  829. wasm_reftype_struct_size(global_ref_type),
  830. global_ref_type,
  831. wasm_reftype_struct_size(global_ref_type));
  832. }
  833. #endif
  834. }
  835. else {
  836. global_type =
  837. module
  838. ->globals[global_idx - module->import_global_count]
  839. .type.val_type;
  840. #if WASM_ENABLE_GC != 0
  841. if (wasm_is_type_multi_byte_type(global_type)) {
  842. WASMRefType *global_ref_type =
  843. module
  844. ->globals[global_idx
  845. - module->import_global_count]
  846. .ref_type;
  847. bh_memcpy_s(&cur_ref_type,
  848. wasm_reftype_struct_size(global_ref_type),
  849. global_ref_type,
  850. wasm_reftype_struct_size(global_ref_type));
  851. }
  852. #endif
  853. }
  854. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  855. #if WASM_ENABLE_GC != 0
  856. &cur_ref_type, 0,
  857. #endif
  858. &cur_value, error_buf,
  859. error_buf_size))
  860. goto fail;
  861. break;
  862. }
  863. #if WASM_ENABLE_GC != 0
  864. /* struct.new and array.new */
  865. case WASM_OP_GC_PREFIX:
  866. {
  867. read_leb_uint32(p, p_end, opcode1);
  868. switch (opcode1) {
  869. case WASM_OP_STRUCT_NEW:
  870. {
  871. WASMStructType *struct_type;
  872. WASMStructNewInitValues *struct_init_values = NULL;
  873. uint32 field_count;
  874. read_leb_uint32(p, p_end, type_idx);
  875. if (!check_type_index(module, module->type_count,
  876. type_idx, error_buf,
  877. error_buf_size)) {
  878. goto fail;
  879. }
  880. struct_type = (WASMStructType *)module->types[type_idx];
  881. if (struct_type->base_type.type_flag
  882. != WASM_TYPE_STRUCT) {
  883. set_error_buf(error_buf, error_buf_size,
  884. "unknown struct type");
  885. goto fail;
  886. }
  887. field_count = struct_type->field_count;
  888. if (!(struct_init_values = loader_malloc(
  889. offsetof(WASMStructNewInitValues, fields)
  890. + (uint64)field_count * sizeof(WASMValue),
  891. error_buf, error_buf_size))) {
  892. goto fail;
  893. }
  894. struct_init_values->type_idx = type_idx;
  895. struct_init_values->count = field_count;
  896. for (i = field_count; i > 0; i--) {
  897. WASMRefType *field_ref_type = NULL;
  898. uint32 field_idx = i - 1;
  899. uint8 field_type =
  900. struct_type->fields[field_idx].field_type;
  901. if (wasm_is_type_multi_byte_type(field_type)) {
  902. field_ref_type = wasm_reftype_map_find(
  903. struct_type->ref_type_maps,
  904. struct_type->ref_type_map_count, field_idx);
  905. }
  906. if (is_packed_type(field_type)) {
  907. field_type = VALUE_TYPE_I32;
  908. }
  909. if (!pop_const_expr_stack(
  910. &const_expr_ctx, NULL, field_type,
  911. field_ref_type, NULL,
  912. &struct_init_values->fields[field_idx],
  913. error_buf, error_buf_size)) {
  914. destroy_init_expr_data_recursive(
  915. module, struct_init_values);
  916. goto fail;
  917. }
  918. }
  919. cur_value.data = struct_init_values;
  920. wasm_set_refheaptype_typeidx(
  921. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  922. if (!push_const_expr_stack(
  923. &const_expr_ctx, flag, cur_ref_type.ref_type,
  924. &cur_ref_type, (uint8)opcode1, &cur_value,
  925. error_buf, error_buf_size)) {
  926. destroy_init_expr_data_recursive(
  927. module, struct_init_values);
  928. goto fail;
  929. }
  930. break;
  931. }
  932. case WASM_OP_STRUCT_NEW_DEFAULT:
  933. {
  934. read_leb_uint32(p, p_end, cur_value.type_index);
  935. type_idx = cur_value.type_index;
  936. if (!check_type_index(module, module->type_count,
  937. type_idx, error_buf,
  938. error_buf_size)) {
  939. goto fail;
  940. }
  941. if (module->types[type_idx]->type_flag
  942. != WASM_TYPE_STRUCT) {
  943. set_error_buf(error_buf, error_buf_size,
  944. "unknown struct type");
  945. goto fail;
  946. }
  947. cur_value.type_index = type_idx;
  948. cur_value.data = NULL;
  949. wasm_set_refheaptype_typeidx(
  950. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  951. if (!push_const_expr_stack(
  952. &const_expr_ctx, flag, cur_ref_type.ref_type,
  953. &cur_ref_type, (uint8)opcode1, &cur_value,
  954. error_buf, error_buf_size)) {
  955. goto fail;
  956. }
  957. break;
  958. }
  959. case WASM_OP_ARRAY_NEW:
  960. case WASM_OP_ARRAY_NEW_DEFAULT:
  961. case WASM_OP_ARRAY_NEW_FIXED:
  962. {
  963. WASMArrayNewInitValues *array_init_values = NULL;
  964. WASMArrayType *array_type = NULL;
  965. WASMRefType *elem_ref_type = NULL;
  966. uint64 total_size;
  967. uint8 elem_type;
  968. read_leb_uint32(p, p_end, cur_value.type_index);
  969. type_idx = cur_value.type_index;
  970. if (!check_type_index(module, module->type_count,
  971. type_idx, error_buf,
  972. error_buf_size)) {
  973. goto fail;
  974. }
  975. array_type = (WASMArrayType *)module->types[type_idx];
  976. if (array_type->base_type.type_flag
  977. != WASM_TYPE_ARRAY) {
  978. set_error_buf(error_buf, error_buf_size,
  979. "unknown array type");
  980. goto fail;
  981. }
  982. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  983. elem_type = array_type->elem_type;
  984. if (wasm_is_type_multi_byte_type(elem_type)) {
  985. elem_ref_type = array_type->elem_ref_type;
  986. }
  987. if (is_packed_type(elem_type)) {
  988. elem_type = VALUE_TYPE_I32;
  989. }
  990. if (opcode1 == WASM_OP_ARRAY_NEW) {
  991. WASMValue len_val = { 0 };
  992. uint64 size = 0;
  993. if (!pop_const_expr_stack(
  994. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  995. NULL, NULL, &len_val, error_buf,
  996. error_buf_size)) {
  997. goto fail;
  998. }
  999. size =
  1000. sizeof(WASMArrayNewInitValues)
  1001. + sizeof(WASMValue) * (uint64)len_val.i32;
  1002. if (!(array_init_values = loader_malloc(
  1003. size, error_buf, error_buf_size))) {
  1004. goto fail;
  1005. }
  1006. array_init_values->type_idx = type_idx;
  1007. array_init_values->length = len_val.i32;
  1008. if (!pop_const_expr_stack(
  1009. &const_expr_ctx, NULL, elem_type,
  1010. elem_ref_type, NULL,
  1011. &array_init_values->elem_data[0],
  1012. error_buf, error_buf_size)) {
  1013. destroy_init_expr_data_recursive(
  1014. module, array_init_values);
  1015. goto fail;
  1016. }
  1017. cur_value.data = array_init_values;
  1018. }
  1019. else {
  1020. /* WASM_OP_ARRAY_NEW_FIXED */
  1021. uint32 len;
  1022. read_leb_uint32(p, p_end, len);
  1023. total_size =
  1024. (uint64)offsetof(WASMArrayNewInitValues,
  1025. elem_data)
  1026. + (uint64)sizeof(WASMValue) * len;
  1027. if (!(array_init_values =
  1028. loader_malloc(total_size, error_buf,
  1029. error_buf_size))) {
  1030. goto fail;
  1031. }
  1032. array_init_values->type_idx = type_idx;
  1033. array_init_values->length = len;
  1034. for (i = len; i > 0; i--) {
  1035. if (!pop_const_expr_stack(
  1036. &const_expr_ctx, NULL, elem_type,
  1037. elem_ref_type, NULL,
  1038. &array_init_values
  1039. ->elem_data[i - 1],
  1040. error_buf, error_buf_size)) {
  1041. destroy_init_expr_data_recursive(
  1042. module, array_init_values);
  1043. goto fail;
  1044. }
  1045. }
  1046. cur_value.data = array_init_values;
  1047. }
  1048. }
  1049. else {
  1050. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1051. WASMValue len_val;
  1052. uint32 len;
  1053. /* POP(i32) */
  1054. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1055. VALUE_TYPE_I32, NULL,
  1056. NULL, &len_val, error_buf,
  1057. error_buf_size)) {
  1058. goto fail;
  1059. }
  1060. len = len_val.i32;
  1061. cur_value.array_new_default.type_index = type_idx;
  1062. cur_value.array_new_default.length = len;
  1063. }
  1064. wasm_set_refheaptype_typeidx(
  1065. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1066. if (!push_const_expr_stack(
  1067. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1068. &cur_ref_type, (uint8)opcode1, &cur_value,
  1069. error_buf, error_buf_size)) {
  1070. if (array_init_values) {
  1071. destroy_init_expr_data_recursive(
  1072. module, array_init_values);
  1073. }
  1074. goto fail;
  1075. }
  1076. break;
  1077. }
  1078. case WASM_OP_ANY_CONVERT_EXTERN:
  1079. {
  1080. set_error_buf(error_buf, error_buf_size,
  1081. "unsupported constant expression of "
  1082. "extern.internalize");
  1083. goto fail;
  1084. }
  1085. case WASM_OP_EXTERN_CONVERT_ANY:
  1086. {
  1087. set_error_buf(error_buf, error_buf_size,
  1088. "unsupported constant expression of "
  1089. "extern.externalize");
  1090. goto fail;
  1091. }
  1092. case WASM_OP_REF_I31:
  1093. {
  1094. /* POP(i32) */
  1095. if (!pop_const_expr_stack(
  1096. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1097. NULL, &cur_value, error_buf, error_buf_size)) {
  1098. goto fail;
  1099. }
  1100. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1101. false, HEAP_TYPE_I31);
  1102. if (!push_const_expr_stack(
  1103. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1104. &cur_ref_type, (uint8)opcode1, &cur_value,
  1105. error_buf, error_buf_size)) {
  1106. goto fail;
  1107. }
  1108. break;
  1109. }
  1110. default:
  1111. set_error_buf(
  1112. error_buf, error_buf_size,
  1113. "type mismatch or constant expression required");
  1114. goto fail;
  1115. }
  1116. break;
  1117. }
  1118. #endif /* end of WASM_ENABLE_GC != 0 */
  1119. default:
  1120. {
  1121. set_error_buf(error_buf, error_buf_size,
  1122. "illegal opcode "
  1123. "or constant expression required "
  1124. "or type mismatch");
  1125. goto fail;
  1126. }
  1127. }
  1128. CHECK_BUF(p, p_end, 1);
  1129. flag = read_uint8(p);
  1130. }
  1131. /* There should be only one value left on the init value stack */
  1132. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1133. #if WASM_ENABLE_GC != 0
  1134. ref_type, &opcode,
  1135. #endif
  1136. &cur_value, error_buf, error_buf_size)) {
  1137. goto fail;
  1138. }
  1139. if (const_expr_ctx.sp != 0) {
  1140. set_error_buf(error_buf, error_buf_size,
  1141. "type mismatch: illegal constant opcode sequence");
  1142. goto fail;
  1143. }
  1144. init_expr->init_expr_type = flag;
  1145. init_expr->u = cur_value;
  1146. #if WASM_ENABLE_GC != 0
  1147. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1148. switch (opcode) {
  1149. case WASM_OP_STRUCT_NEW:
  1150. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1151. break;
  1152. case WASM_OP_STRUCT_NEW_DEFAULT:
  1153. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1154. break;
  1155. case WASM_OP_ARRAY_NEW:
  1156. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1157. break;
  1158. case WASM_OP_ARRAY_NEW_DEFAULT:
  1159. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1160. break;
  1161. case WASM_OP_ARRAY_NEW_FIXED:
  1162. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1163. break;
  1164. case WASM_OP_REF_I31:
  1165. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1166. break;
  1167. default:
  1168. bh_assert(0);
  1169. break;
  1170. }
  1171. }
  1172. #endif /* end of WASM_ENABLE_GC != 0 */
  1173. *p_buf = p;
  1174. destroy_const_expr_stack(&const_expr_ctx);
  1175. return true;
  1176. fail:
  1177. destroy_const_expr_stack(&const_expr_ctx);
  1178. return false;
  1179. }
  1180. static bool
  1181. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1182. {
  1183. if (mutable >= 2) {
  1184. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1185. return false;
  1186. }
  1187. return true;
  1188. }
  1189. #if WASM_ENABLE_GC != 0
  1190. static void
  1191. destroy_func_type(WASMFuncType *type)
  1192. {
  1193. /* Destroy the reference type hash set */
  1194. if (type->ref_type_maps)
  1195. wasm_runtime_free(type->ref_type_maps);
  1196. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1197. && WASM_ENABLE_LAZY_JIT != 0
  1198. if (type->call_to_llvm_jit_from_fast_jit)
  1199. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1200. #endif
  1201. /* Free the type */
  1202. wasm_runtime_free(type);
  1203. }
  1204. static void
  1205. destroy_struct_type(WASMStructType *type)
  1206. {
  1207. if (type->ref_type_maps)
  1208. wasm_runtime_free(type->ref_type_maps);
  1209. wasm_runtime_free(type);
  1210. }
  1211. static void
  1212. destroy_array_type(WASMArrayType *type)
  1213. {
  1214. wasm_runtime_free(type);
  1215. }
  1216. static void
  1217. destroy_wasm_type(WASMType *type)
  1218. {
  1219. if (type->ref_count > 1) {
  1220. /* The type is referenced by other types
  1221. of current wasm module */
  1222. type->ref_count--;
  1223. return;
  1224. }
  1225. if (type->type_flag == WASM_TYPE_FUNC)
  1226. destroy_func_type((WASMFuncType *)type);
  1227. else if (type->type_flag == WASM_TYPE_STRUCT)
  1228. destroy_struct_type((WASMStructType *)type);
  1229. else if (type->type_flag == WASM_TYPE_ARRAY)
  1230. destroy_array_type((WASMArrayType *)type);
  1231. else {
  1232. bh_assert(0);
  1233. }
  1234. }
  1235. /* Resolve (ref null ht) or (ref ht) */
  1236. static bool
  1237. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1238. WASMModule *module, uint32 type_count, bool nullable,
  1239. WASMRefType *ref_type, char *error_buf,
  1240. uint32 error_buf_size)
  1241. {
  1242. const uint8 *p = *p_buf, *p_end = buf_end;
  1243. ref_type->ref_type =
  1244. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1245. ref_type->ref_ht_common.nullable = nullable;
  1246. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1247. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1248. /* heap type is (type i), i : typeidx, >= 0 */
  1249. if (!check_type_index(module, type_count,
  1250. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1251. error_buf_size)) {
  1252. return false;
  1253. }
  1254. }
  1255. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1256. /* heap type is func, extern, any, eq, i31 or data */
  1257. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1258. return false;
  1259. }
  1260. *p_buf = p;
  1261. return true;
  1262. fail:
  1263. return false;
  1264. }
  1265. static bool
  1266. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1267. WASMModule *module, uint32 type_count,
  1268. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1269. bool allow_packed_type, char *error_buf,
  1270. uint32 error_buf_size)
  1271. {
  1272. const uint8 *p = *p_buf, *p_end = buf_end;
  1273. uint8 type;
  1274. memset(ref_type, 0, sizeof(WASMRefType));
  1275. CHECK_BUF(p, p_end, 1);
  1276. type = read_uint8(p);
  1277. if (wasm_is_reftype_htref_nullable(type)) {
  1278. /* (ref null ht) */
  1279. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1280. ref_type, error_buf, error_buf_size))
  1281. return false;
  1282. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1283. *p_need_ref_type_map = true;
  1284. else {
  1285. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1286. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1287. multi-byte type to one-byte type to reduce the footprint and
  1288. the complexity of type equal/subtype checking */
  1289. ref_type->ref_type =
  1290. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1291. *p_need_ref_type_map = false;
  1292. }
  1293. }
  1294. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1295. /* (ref ht) */
  1296. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1297. ref_type, error_buf, error_buf_size))
  1298. return false;
  1299. *p_need_ref_type_map = true;
  1300. #if WASM_ENABLE_STRINGREF != 0
  1301. /* covert (ref string) to stringref */
  1302. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1303. ref_type->ref_type =
  1304. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1305. *p_need_ref_type_map = false;
  1306. }
  1307. #endif
  1308. }
  1309. else {
  1310. /* type which can be represented by one byte */
  1311. if (!is_valid_value_type_for_interpreter(type)
  1312. && !(allow_packed_type && is_packed_type(type))) {
  1313. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1314. return false;
  1315. }
  1316. ref_type->ref_type = type;
  1317. *p_need_ref_type_map = false;
  1318. #if WASM_ENABLE_WAMR_COMPILER != 0
  1319. /* If any value's type is v128, mark the module as SIMD used */
  1320. if (type == VALUE_TYPE_V128)
  1321. module->is_simd_used = true;
  1322. #endif
  1323. }
  1324. *p_buf = p;
  1325. return true;
  1326. fail:
  1327. return false;
  1328. }
  1329. static WASMRefType *
  1330. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1331. char *error_buf, uint32 error_buf_size)
  1332. {
  1333. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1334. if (!ret) {
  1335. set_error_buf(error_buf, error_buf_size,
  1336. "insert ref type to hash set failed");
  1337. }
  1338. return ret;
  1339. }
  1340. static bool
  1341. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1342. uint32 type_count, uint32 type_idx, char *error_buf,
  1343. uint32 error_buf_size)
  1344. {
  1345. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1346. uint32 param_count, result_count, i, j = 0;
  1347. uint32 param_cell_num, ret_cell_num;
  1348. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1349. uint64 total_size;
  1350. bool need_ref_type_map;
  1351. WASMRefType ref_type;
  1352. WASMFuncType *type = NULL;
  1353. /* Parse first time to resolve param count, result count and
  1354. ref type map count */
  1355. read_leb_uint32(p, p_end, param_count);
  1356. p_org = p;
  1357. for (i = 0; i < param_count; i++) {
  1358. if (!resolve_value_type(&p, p_end, module, type_count,
  1359. &need_ref_type_map, &ref_type, false, error_buf,
  1360. error_buf_size)) {
  1361. return false;
  1362. }
  1363. if (need_ref_type_map)
  1364. ref_type_map_count++;
  1365. }
  1366. read_leb_uint32(p, p_end, result_count);
  1367. for (i = 0; i < result_count; i++) {
  1368. if (!resolve_value_type(&p, p_end, module, type_count,
  1369. &need_ref_type_map, &ref_type, false, error_buf,
  1370. error_buf_size)) {
  1371. return false;
  1372. }
  1373. if (need_ref_type_map) {
  1374. ref_type_map_count++;
  1375. result_ref_type_map_count++;
  1376. }
  1377. }
  1378. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1379. "ref type map count: %d",
  1380. type_idx, param_count, result_count, ref_type_map_count);
  1381. /* Parse second time to resolve param types, result types and
  1382. ref type map info */
  1383. p = p_org;
  1384. total_size = offsetof(WASMFuncType, types)
  1385. + sizeof(uint8) * (uint64)(param_count + result_count);
  1386. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1387. return false;
  1388. }
  1389. if (ref_type_map_count > 0) {
  1390. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1391. if (!(type->ref_type_maps =
  1392. loader_malloc(total_size, error_buf, error_buf_size))) {
  1393. goto fail;
  1394. }
  1395. }
  1396. type->base_type.type_flag = WASM_TYPE_FUNC;
  1397. type->param_count = param_count;
  1398. type->result_count = result_count;
  1399. type->ref_type_map_count = ref_type_map_count;
  1400. if (ref_type_map_count > 0) {
  1401. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1402. - result_ref_type_map_count;
  1403. }
  1404. for (i = 0; i < param_count; i++) {
  1405. if (!resolve_value_type(&p, p_end, module, type_count,
  1406. &need_ref_type_map, &ref_type, false, error_buf,
  1407. error_buf_size)) {
  1408. goto fail;
  1409. }
  1410. type->types[i] = ref_type.ref_type;
  1411. if (need_ref_type_map) {
  1412. type->ref_type_maps[j].index = i;
  1413. if (!(type->ref_type_maps[j++].ref_type =
  1414. reftype_set_insert(module->ref_type_set, &ref_type,
  1415. error_buf, error_buf_size))) {
  1416. goto fail;
  1417. }
  1418. }
  1419. }
  1420. read_leb_uint32(p, p_end, result_count);
  1421. for (i = 0; i < result_count; i++) {
  1422. if (!resolve_value_type(&p, p_end, module, type_count,
  1423. &need_ref_type_map, &ref_type, false, error_buf,
  1424. error_buf_size)) {
  1425. goto fail;
  1426. }
  1427. type->types[param_count + i] = ref_type.ref_type;
  1428. if (need_ref_type_map) {
  1429. type->ref_type_maps[j].index = param_count + i;
  1430. if (!(type->ref_type_maps[j++].ref_type =
  1431. reftype_set_insert(module->ref_type_set, &ref_type,
  1432. error_buf, error_buf_size))) {
  1433. goto fail;
  1434. }
  1435. }
  1436. }
  1437. bh_assert(j == type->ref_type_map_count);
  1438. #if TRACE_WASM_LOADER != 0
  1439. os_printf("type %d = ", type_idx);
  1440. wasm_dump_func_type(type);
  1441. #endif
  1442. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1443. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1444. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1445. set_error_buf(error_buf, error_buf_size,
  1446. "param count or result count too large");
  1447. goto fail;
  1448. }
  1449. type->param_cell_num = (uint16)param_cell_num;
  1450. type->ret_cell_num = (uint16)ret_cell_num;
  1451. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1452. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1453. #endif
  1454. #if WASM_ENABLE_WAMR_COMPILER != 0
  1455. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1456. if (type->types[i] == VALUE_TYPE_V128)
  1457. module->is_simd_used = true;
  1458. }
  1459. #endif
  1460. *p_buf = p;
  1461. module->types[type_idx] = (WASMType *)type;
  1462. return true;
  1463. fail:
  1464. if (type)
  1465. destroy_func_type(type);
  1466. return false;
  1467. }
  1468. static bool
  1469. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1470. WASMModule *module, uint32 type_count, uint32 type_idx,
  1471. char *error_buf, uint32 error_buf_size)
  1472. {
  1473. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1474. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1475. uint32 i, j = 0, offset;
  1476. uint16 *reference_table;
  1477. uint64 total_size;
  1478. uint8 mutable;
  1479. bool need_ref_type_map;
  1480. WASMRefType ref_type;
  1481. WASMStructType *type = NULL;
  1482. /* Parse first time to resolve field count and ref type map count */
  1483. read_leb_uint32(p, p_end, field_count);
  1484. p_org = p;
  1485. for (i = 0; i < field_count; i++) {
  1486. if (!resolve_value_type(&p, p_end, module, type_count,
  1487. &need_ref_type_map, &ref_type, true, error_buf,
  1488. error_buf_size)) {
  1489. return false;
  1490. }
  1491. if (need_ref_type_map)
  1492. ref_type_map_count++;
  1493. if (wasm_is_type_reftype(ref_type.ref_type))
  1494. ref_field_count++;
  1495. CHECK_BUF(p, p_end, 1);
  1496. mutable = read_uint8(p);
  1497. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1498. return false;
  1499. }
  1500. }
  1501. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1502. type_idx, field_count, ref_type_map_count);
  1503. /* Parse second time to resolve field types and ref type map info */
  1504. p = p_org;
  1505. total_size = offsetof(WASMStructType, fields)
  1506. + sizeof(WASMStructFieldType) * (uint64)field_count
  1507. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1508. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1509. return false;
  1510. }
  1511. if (ref_type_map_count > 0) {
  1512. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1513. if (!(type->ref_type_maps =
  1514. loader_malloc(total_size, error_buf, error_buf_size))) {
  1515. goto fail;
  1516. }
  1517. }
  1518. type->reference_table = reference_table =
  1519. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1520. + sizeof(WASMStructFieldType) * field_count);
  1521. *reference_table++ = ref_field_count;
  1522. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1523. type->field_count = field_count;
  1524. type->ref_type_map_count = ref_type_map_count;
  1525. offset = (uint32)offsetof(WASMStructObject, field_data);
  1526. for (i = 0; i < field_count; i++) {
  1527. if (!resolve_value_type(&p, p_end, module, type_count,
  1528. &need_ref_type_map, &ref_type, true, error_buf,
  1529. error_buf_size)) {
  1530. goto fail;
  1531. }
  1532. type->fields[i].field_type = ref_type.ref_type;
  1533. if (need_ref_type_map) {
  1534. type->ref_type_maps[j].index = i;
  1535. if (!(type->ref_type_maps[j++].ref_type =
  1536. reftype_set_insert(module->ref_type_set, &ref_type,
  1537. error_buf, error_buf_size))) {
  1538. goto fail;
  1539. }
  1540. }
  1541. CHECK_BUF(p, p_end, 1);
  1542. type->fields[i].field_flags = read_uint8(p);
  1543. type->fields[i].field_size =
  1544. (uint8)wasm_reftype_size(ref_type.ref_type);
  1545. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1546. || defined(BUILD_TARGET_X86_32))
  1547. if (type->fields[i].field_size == 2)
  1548. offset = align_uint(offset, 2);
  1549. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1550. offset = align_uint(offset, 4);
  1551. #endif
  1552. type->fields[i].field_offset = offset;
  1553. if (wasm_is_type_reftype(ref_type.ref_type))
  1554. *reference_table++ = offset;
  1555. offset += type->fields[i].field_size;
  1556. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1557. type->fields[i].field_flags, type->fields[i].field_type);
  1558. }
  1559. type->total_size = offset;
  1560. bh_assert(j == type->ref_type_map_count);
  1561. #if TRACE_WASM_LOADER != 0
  1562. os_printf("type %d = ", type_idx);
  1563. wasm_dump_struct_type(type);
  1564. #endif
  1565. *p_buf = p;
  1566. module->types[type_idx] = (WASMType *)type;
  1567. return true;
  1568. fail:
  1569. if (type)
  1570. destroy_struct_type(type);
  1571. return false;
  1572. }
  1573. static bool
  1574. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1575. WASMModule *module, uint32 type_count, uint32 type_idx,
  1576. char *error_buf, uint32 error_buf_size)
  1577. {
  1578. const uint8 *p = *p_buf, *p_end = buf_end;
  1579. uint8 mutable;
  1580. bool need_ref_type_map;
  1581. WASMRefType ref_type;
  1582. WASMArrayType *type = NULL;
  1583. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1584. &ref_type, true, error_buf, error_buf_size)) {
  1585. return false;
  1586. }
  1587. CHECK_BUF(p, p_end, 1);
  1588. mutable = read_uint8(p);
  1589. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1590. return false;
  1591. }
  1592. LOG_VERBOSE("type %u: array", type_idx);
  1593. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1594. error_buf_size))) {
  1595. return false;
  1596. }
  1597. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1598. type->elem_flags = mutable;
  1599. type->elem_type = ref_type.ref_type;
  1600. if (need_ref_type_map) {
  1601. if (!(type->elem_ref_type =
  1602. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1603. error_buf_size))) {
  1604. goto fail;
  1605. }
  1606. }
  1607. #if TRACE_WASM_LOADER != 0
  1608. os_printf("type %d = ", type_idx);
  1609. wasm_dump_array_type(type);
  1610. #endif
  1611. *p_buf = p;
  1612. module->types[type_idx] = (WASMType *)type;
  1613. return true;
  1614. fail:
  1615. if (type)
  1616. destroy_array_type(type);
  1617. return false;
  1618. }
  1619. static bool
  1620. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1621. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1622. {
  1623. if (heap_type >= 0) {
  1624. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1625. error_buf_size)) {
  1626. return false;
  1627. }
  1628. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1629. heap_type);
  1630. }
  1631. else {
  1632. if (!wasm_is_valid_heap_type(heap_type)) {
  1633. set_error_buf(error_buf, error_buf_size, "unknown type");
  1634. return false;
  1635. }
  1636. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1637. heap_type);
  1638. if (nullable) {
  1639. /* For (ref null func/extern/any/eq/i31/data),
  1640. they are same as
  1641. funcref/externref/anyref/eqref/i31ref/dataref,
  1642. we convert the multi-byte type to one-byte
  1643. type to reduce the footprint and the
  1644. complexity of type equal/subtype checking */
  1645. ref_type->ref_type =
  1646. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1647. }
  1648. }
  1649. return true;
  1650. }
  1651. static void
  1652. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1653. WASMRefType *ref_type2)
  1654. {
  1655. /**
  1656. * The difference rt1 ∖ rt2 between two reference types is defined as
  1657. * follows:
  1658. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1659. * (ref ht2) = (ref null?1 ht1)
  1660. */
  1661. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1662. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1663. ref_type1, wasm_reftype_struct_size(ref_type1));
  1664. }
  1665. else {
  1666. ref_type_diff->ref_type = ref_type1->ref_type;
  1667. }
  1668. if (ref_type2->ref_ht_common.nullable) {
  1669. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1670. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1671. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1672. (int32)ref_type_diff->ref_type - 0x80);
  1673. }
  1674. else {
  1675. ref_type_diff->ref_ht_typeidx.nullable = false;
  1676. }
  1677. }
  1678. }
  1679. #else /* else of WASM_ENABLE_GC != 0 */
  1680. static void
  1681. destroy_wasm_type(WASMType *type)
  1682. {
  1683. if (type->ref_count > 1) {
  1684. /* The type is referenced by other types
  1685. of current wasm module */
  1686. type->ref_count--;
  1687. return;
  1688. }
  1689. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1690. && WASM_ENABLE_LAZY_JIT != 0
  1691. if (type->call_to_llvm_jit_from_fast_jit)
  1692. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1693. #endif
  1694. wasm_runtime_free(type);
  1695. }
  1696. #endif /* end of WASM_ENABLE_GC != 0 */
  1697. static bool
  1698. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1699. char *error_buf, uint32 error_buf_size)
  1700. {
  1701. const uint8 *p = buf, *p_end = buf_end;
  1702. uint32 type_count, i;
  1703. uint64 total_size;
  1704. uint8 flag;
  1705. #if WASM_ENABLE_GC != 0
  1706. uint32 processed_type_count = 0;
  1707. #endif
  1708. read_leb_uint32(p, p_end, type_count);
  1709. if (type_count) {
  1710. module->type_count = type_count;
  1711. total_size = sizeof(WASMType *) * (uint64)type_count;
  1712. if (!(module->types =
  1713. loader_malloc(total_size, error_buf, error_buf_size))) {
  1714. return false;
  1715. }
  1716. #if WASM_ENABLE_GC == 0
  1717. for (i = 0; i < type_count; i++) {
  1718. WASMFuncType *type;
  1719. const uint8 *p_org;
  1720. uint32 param_count, result_count, j;
  1721. uint32 param_cell_num, ret_cell_num;
  1722. CHECK_BUF(p, p_end, 1);
  1723. flag = read_uint8(p);
  1724. if (flag != 0x60) {
  1725. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1726. return false;
  1727. }
  1728. read_leb_uint32(p, p_end, param_count);
  1729. /* Resolve param count and result count firstly */
  1730. p_org = p;
  1731. CHECK_BUF(p, p_end, param_count);
  1732. p += param_count;
  1733. read_leb_uint32(p, p_end, result_count);
  1734. CHECK_BUF(p, p_end, result_count);
  1735. p = p_org;
  1736. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1737. set_error_buf(error_buf, error_buf_size,
  1738. "param count or result count too large");
  1739. return false;
  1740. }
  1741. total_size = offsetof(WASMFuncType, types)
  1742. + sizeof(uint8) * (uint64)(param_count + result_count);
  1743. if (!(type = module->types[i] =
  1744. loader_malloc(total_size, error_buf, error_buf_size))) {
  1745. return false;
  1746. }
  1747. /* Resolve param types and result types */
  1748. type->ref_count = 1;
  1749. type->param_count = (uint16)param_count;
  1750. type->result_count = (uint16)result_count;
  1751. for (j = 0; j < param_count; j++) {
  1752. CHECK_BUF(p, p_end, 1);
  1753. type->types[j] = read_uint8(p);
  1754. }
  1755. read_leb_uint32(p, p_end, result_count);
  1756. for (j = 0; j < result_count; j++) {
  1757. CHECK_BUF(p, p_end, 1);
  1758. type->types[param_count + j] = read_uint8(p);
  1759. }
  1760. for (j = 0; j < param_count + result_count; j++) {
  1761. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1762. set_error_buf(error_buf, error_buf_size,
  1763. "unknown value type");
  1764. return false;
  1765. }
  1766. }
  1767. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1768. ret_cell_num =
  1769. wasm_get_cell_num(type->types + param_count, result_count);
  1770. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1771. set_error_buf(error_buf, error_buf_size,
  1772. "param count or result count too large");
  1773. return false;
  1774. }
  1775. type->param_cell_num = (uint16)param_cell_num;
  1776. type->ret_cell_num = (uint16)ret_cell_num;
  1777. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1778. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1779. #endif
  1780. #if WASM_ENABLE_WAMR_COMPILER != 0
  1781. for (j = 0; j < type->param_count + type->result_count; j++) {
  1782. if (type->types[j] == VALUE_TYPE_V128)
  1783. module->is_simd_used = true;
  1784. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1785. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1786. module->is_ref_types_used = true;
  1787. }
  1788. #endif
  1789. /* If there is already a same type created, use it instead */
  1790. for (j = 0; j < i; j++) {
  1791. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1792. if (module->types[j]->ref_count == UINT16_MAX) {
  1793. set_error_buf(error_buf, error_buf_size,
  1794. "wasm type's ref count too large");
  1795. return false;
  1796. }
  1797. destroy_wasm_type(type);
  1798. module->types[i] = module->types[j];
  1799. module->types[j]->ref_count++;
  1800. break;
  1801. }
  1802. }
  1803. }
  1804. #else /* else of WASM_ENABLE_GC == 0 */
  1805. for (i = 0; i < type_count; i++) {
  1806. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1807. uint32 rec_count = 1, j;
  1808. bool is_sub_final = true;
  1809. CHECK_BUF(p, p_end, 1);
  1810. flag = read_uint8(p);
  1811. if (flag == DEFINED_TYPE_REC) {
  1812. read_leb_uint32(p, p_end, rec_count);
  1813. if (rec_count > 1) {
  1814. uint64 new_total_size;
  1815. /* integer overflow */
  1816. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1817. set_error_buf(error_buf, error_buf_size,
  1818. "recursive type count too large");
  1819. return false;
  1820. }
  1821. module->type_count += rec_count - 1;
  1822. new_total_size =
  1823. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1824. if (new_total_size > UINT32_MAX) {
  1825. set_error_buf(error_buf, error_buf_size,
  1826. "allocate memory failed");
  1827. return false;
  1828. }
  1829. MEM_REALLOC(module->types, (uint32)total_size,
  1830. (uint32)new_total_size);
  1831. total_size = new_total_size;
  1832. }
  1833. LOG_VERBOSE("Processing rec group [%d-%d]",
  1834. processed_type_count,
  1835. processed_type_count + rec_count - 1);
  1836. }
  1837. else {
  1838. p--;
  1839. }
  1840. for (j = 0; j < rec_count; j++) {
  1841. WASMType *cur_type = NULL;
  1842. CHECK_BUF(p, p_end, 1);
  1843. flag = read_uint8(p);
  1844. parent_type_idx = -1;
  1845. if (flag == DEFINED_TYPE_SUB
  1846. || flag == DEFINED_TYPE_SUB_FINAL) {
  1847. read_leb_uint32(p, p_end, super_type_count);
  1848. if (super_type_count > 1) {
  1849. set_error_buf(error_buf, error_buf_size,
  1850. "super type count too large");
  1851. return false;
  1852. }
  1853. if (super_type_count > 0) {
  1854. read_leb_uint32(p, p_end, parent_type_idx);
  1855. if (parent_type_idx >= processed_type_count + j) {
  1856. set_error_buf_v(error_buf, error_buf_size,
  1857. "unknown type %d", parent_type_idx);
  1858. return false;
  1859. }
  1860. if (module->types[parent_type_idx]->is_sub_final) {
  1861. set_error_buf(error_buf, error_buf_size,
  1862. "sub type can not inherit from "
  1863. "a final super type");
  1864. return false;
  1865. }
  1866. }
  1867. if (flag == DEFINED_TYPE_SUB)
  1868. is_sub_final = false;
  1869. CHECK_BUF(p, p_end, 1);
  1870. flag = read_uint8(p);
  1871. }
  1872. if (flag == DEFINED_TYPE_FUNC) {
  1873. if (!resolve_func_type(&p, buf_end, module,
  1874. processed_type_count + rec_count,
  1875. processed_type_count + j, error_buf,
  1876. error_buf_size)) {
  1877. return false;
  1878. }
  1879. }
  1880. else if (flag == DEFINED_TYPE_STRUCT) {
  1881. if (!resolve_struct_type(&p, buf_end, module,
  1882. processed_type_count + rec_count,
  1883. processed_type_count + j,
  1884. error_buf, error_buf_size)) {
  1885. return false;
  1886. }
  1887. }
  1888. else if (flag == DEFINED_TYPE_ARRAY) {
  1889. if (!resolve_array_type(&p, buf_end, module,
  1890. processed_type_count + rec_count,
  1891. processed_type_count + j, error_buf,
  1892. error_buf_size)) {
  1893. return false;
  1894. }
  1895. }
  1896. else {
  1897. set_error_buf(error_buf, error_buf_size,
  1898. "invalid type flag");
  1899. return false;
  1900. }
  1901. cur_type = module->types[processed_type_count + j];
  1902. cur_type->ref_count = 1;
  1903. cur_type->parent_type_idx = parent_type_idx;
  1904. cur_type->is_sub_final = is_sub_final;
  1905. cur_type->rec_count = rec_count;
  1906. cur_type->rec_idx = j;
  1907. cur_type->rec_begin_type_idx = processed_type_count;
  1908. }
  1909. /* resolve subtyping relationship in current rec group */
  1910. for (j = 0; j < rec_count; j++) {
  1911. WASMType *cur_type = module->types[processed_type_count + j];
  1912. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1913. WASMType *parent_type =
  1914. module->types[cur_type->parent_type_idx];
  1915. cur_type->parent_type = parent_type;
  1916. cur_type->root_type = parent_type->root_type;
  1917. if (parent_type->inherit_depth == UINT16_MAX) {
  1918. set_error_buf(error_buf, error_buf_size,
  1919. "parent type's inherit depth too large");
  1920. return false;
  1921. }
  1922. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1923. }
  1924. else {
  1925. cur_type->parent_type = NULL;
  1926. cur_type->root_type = cur_type;
  1927. cur_type->inherit_depth = 0;
  1928. }
  1929. }
  1930. for (j = 0; j < rec_count; j++) {
  1931. WASMType *cur_type = module->types[processed_type_count + j];
  1932. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1933. WASMType *parent_type =
  1934. module->types[cur_type->parent_type_idx];
  1935. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1936. module->types,
  1937. module->type_count)) {
  1938. set_error_buf_v(error_buf, error_buf_size,
  1939. "sub type %u does not match super type",
  1940. processed_type_count + j);
  1941. return false;
  1942. }
  1943. }
  1944. }
  1945. /* If there is already an equivalence type or a group of equivalence
  1946. recursive types created, use it or them instead */
  1947. for (j = 0; j < processed_type_count;) {
  1948. WASMType *src_type = module->types[j];
  1949. WASMType *cur_type = module->types[processed_type_count];
  1950. uint32 k, src_rec_count;
  1951. src_rec_count = src_type->rec_count;
  1952. if (src_rec_count != rec_count) {
  1953. /* no type equivalence */
  1954. j += src_rec_count;
  1955. continue;
  1956. }
  1957. for (k = 0; k < rec_count; k++) {
  1958. src_type = module->types[j + k];
  1959. cur_type = module->types[processed_type_count + k];
  1960. if (!wasm_type_equal(src_type, cur_type, module->types,
  1961. module->type_count)) {
  1962. break;
  1963. }
  1964. }
  1965. if (k < rec_count) {
  1966. /* no type equivalence */
  1967. j += src_rec_count;
  1968. continue;
  1969. }
  1970. /* type equivalence */
  1971. for (k = 0; k < rec_count; k++) {
  1972. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1973. set_error_buf(error_buf, error_buf_size,
  1974. "wasm type's ref count too large");
  1975. return false;
  1976. }
  1977. destroy_wasm_type(module->types[processed_type_count + k]);
  1978. module->types[processed_type_count + k] =
  1979. module->types[j + k];
  1980. module->types[j + k]->ref_count++;
  1981. }
  1982. break;
  1983. }
  1984. if (rec_count > 1) {
  1985. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1986. processed_type_count,
  1987. processed_type_count + rec_count - 1);
  1988. }
  1989. processed_type_count += rec_count;
  1990. }
  1991. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1992. * module->type_count,
  1993. error_buf, error_buf_size))) {
  1994. return false;
  1995. }
  1996. #endif /* end of WASM_ENABLE_GC == 0 */
  1997. }
  1998. if (p != p_end) {
  1999. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2000. return false;
  2001. }
  2002. LOG_VERBOSE("Load type section success.\n");
  2003. return true;
  2004. fail:
  2005. return false;
  2006. }
  2007. static void
  2008. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  2009. uint32 *max_size)
  2010. {
  2011. uint32 default_max_size;
  2012. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  2013. * ABI unchanged */
  2014. (void)is_table64;
  2015. if (UINT32_MAX / 2 > init_size)
  2016. default_max_size = init_size * 2;
  2017. else
  2018. default_max_size = UINT32_MAX;
  2019. if (default_max_size < WASM_TABLE_MAX_SIZE)
  2020. default_max_size = WASM_TABLE_MAX_SIZE;
  2021. if (max_size_flag) {
  2022. /* module defines the table limitation */
  2023. bh_assert(init_size <= *max_size);
  2024. if (init_size < *max_size) {
  2025. *max_size =
  2026. *max_size < default_max_size ? *max_size : default_max_size;
  2027. }
  2028. }
  2029. else {
  2030. /* partial defined table limitation, gives a default value */
  2031. *max_size = default_max_size;
  2032. }
  2033. }
  2034. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2035. /**
  2036. * Find export item of a module with export info:
  2037. * module name, field name and export kind
  2038. */
  2039. static WASMExport *
  2040. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2041. const char *field_name, uint8 export_kind,
  2042. char *error_buf, uint32 error_buf_size)
  2043. {
  2044. WASMExport *export =
  2045. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2046. export_kind, error_buf, error_buf_size);
  2047. return export;
  2048. }
  2049. #endif
  2050. #if WASM_ENABLE_MULTI_MODULE != 0
  2051. static WASMTable *
  2052. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2053. uint32 init_size, uint32 max_size, char *error_buf,
  2054. uint32 error_buf_size)
  2055. {
  2056. WASMModuleCommon *module_reg;
  2057. WASMTable *table = NULL;
  2058. WASMExport *export = NULL;
  2059. WASMModule *module = NULL;
  2060. module_reg = wasm_runtime_find_module_registered(module_name);
  2061. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2062. LOG_DEBUG("can not find a module named %s for table", module_name);
  2063. set_error_buf(error_buf, error_buf_size, "unknown import");
  2064. return NULL;
  2065. }
  2066. module = (WASMModule *)module_reg;
  2067. export =
  2068. wasm_loader_find_export(module, module_name, table_name,
  2069. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2070. if (!export) {
  2071. return NULL;
  2072. }
  2073. /* resolve table and check the init/max size */
  2074. if (export->index < module->import_table_count) {
  2075. table =
  2076. module->import_tables[export->index].u.table.import_table_linked;
  2077. }
  2078. else {
  2079. table = &(module->tables[export->index - module->import_table_count]);
  2080. }
  2081. if (table->table_type.init_size < init_size
  2082. || table->table_type.max_size > max_size) {
  2083. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2084. module_name, table_name, table->table_type.init_size,
  2085. table->table_type.max_size, init_size, max_size);
  2086. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2087. return NULL;
  2088. }
  2089. return table;
  2090. }
  2091. static WASMMemory *
  2092. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2093. uint32 init_page_count, uint32 max_page_count,
  2094. char *error_buf, uint32 error_buf_size)
  2095. {
  2096. WASMModuleCommon *module_reg;
  2097. WASMMemory *memory = NULL;
  2098. WASMExport *export = NULL;
  2099. WASMModule *module = NULL;
  2100. module_reg = wasm_runtime_find_module_registered(module_name);
  2101. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2102. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2103. set_error_buf(error_buf, error_buf_size, "unknown import");
  2104. return NULL;
  2105. }
  2106. module = (WASMModule *)module_reg;
  2107. export =
  2108. wasm_loader_find_export(module, module_name, memory_name,
  2109. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2110. if (!export) {
  2111. return NULL;
  2112. }
  2113. /* resolve memory and check the init/max page count */
  2114. if (export->index < module->import_memory_count) {
  2115. memory = module->import_memories[export->index]
  2116. .u.memory.import_memory_linked;
  2117. }
  2118. else {
  2119. memory =
  2120. &(module->memories[export->index - module->import_memory_count]);
  2121. }
  2122. if (memory->init_page_count < init_page_count
  2123. || memory->max_page_count > max_page_count) {
  2124. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2125. module_name, memory_name, memory->init_page_count,
  2126. memory->max_page_count, init_page_count, max_page_count);
  2127. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2128. return NULL;
  2129. }
  2130. return memory;
  2131. }
  2132. static WASMGlobal *
  2133. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2134. uint8 type, bool is_mutable, char *error_buf,
  2135. uint32 error_buf_size)
  2136. {
  2137. WASMModuleCommon *module_reg;
  2138. WASMGlobal *global = NULL;
  2139. WASMExport *export = NULL;
  2140. WASMModule *module = NULL;
  2141. module_reg = wasm_runtime_find_module_registered(module_name);
  2142. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2143. LOG_DEBUG("can not find a module named %s for global", module_name);
  2144. set_error_buf(error_buf, error_buf_size, "unknown import");
  2145. return NULL;
  2146. }
  2147. module = (WASMModule *)module_reg;
  2148. export =
  2149. wasm_loader_find_export(module, module_name, global_name,
  2150. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2151. if (!export) {
  2152. return NULL;
  2153. }
  2154. /* resolve and check the global */
  2155. if (export->index < module->import_global_count) {
  2156. global =
  2157. module->import_globals[export->index].u.global.import_global_linked;
  2158. }
  2159. else {
  2160. global =
  2161. &(module->globals[export->index - module->import_global_count]);
  2162. }
  2163. if (global->type.val_type != type
  2164. || global->type.is_mutable != is_mutable) {
  2165. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2166. module_name, global_name, global->type.val_type,
  2167. global->type.is_mutable, type, is_mutable);
  2168. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2169. return NULL;
  2170. }
  2171. return global;
  2172. }
  2173. #if WASM_ENABLE_TAGS != 0
  2174. static WASMTag *
  2175. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2176. const WASMType *expected_tag_type,
  2177. uint32 *linked_tag_index, char *error_buf,
  2178. uint32 error_buf_size)
  2179. {
  2180. WASMModuleCommon *module_reg;
  2181. WASMTag *tag = NULL;
  2182. WASMExport *export = NULL;
  2183. WASMModule *module = NULL;
  2184. module_reg = wasm_runtime_find_module_registered(module_name);
  2185. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2186. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2187. tag_name);
  2188. set_error_buf(error_buf, error_buf_size, "unknown import");
  2189. return NULL;
  2190. }
  2191. module = (WASMModule *)module_reg;
  2192. export =
  2193. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2194. error_buf, error_buf_size);
  2195. if (!export) {
  2196. return NULL;
  2197. }
  2198. /* resolve tag type and tag */
  2199. if (export->index < module->import_tag_count) {
  2200. /* importing an imported tag from the submodule */
  2201. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2202. }
  2203. else {
  2204. /* importing an section tag from the submodule */
  2205. tag = module->tags[export->index - module->import_tag_count];
  2206. }
  2207. /* check function type */
  2208. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2209. module->type_count)) {
  2210. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2211. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2212. return NULL;
  2213. }
  2214. if (linked_tag_index != NULL) {
  2215. *linked_tag_index = export->index;
  2216. }
  2217. return tag;
  2218. }
  2219. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2220. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2221. static bool
  2222. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2223. const WASMModule *parent_module,
  2224. const char *sub_module_name, const char *function_name,
  2225. WASMFunctionImport *function, bool no_resolve,
  2226. char *error_buf, uint32 error_buf_size)
  2227. {
  2228. const uint8 *p = *p_buf, *p_end = buf_end;
  2229. uint32 declare_type_index = 0;
  2230. read_leb_uint32(p, p_end, declare_type_index);
  2231. *p_buf = p;
  2232. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2233. error_buf_size)) {
  2234. return false;
  2235. }
  2236. #if WASM_ENABLE_GC != 0
  2237. function->type_idx = declare_type_index;
  2238. #endif
  2239. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2240. declare_type_index = wasm_get_smallest_type_idx(
  2241. parent_module->types, parent_module->type_count, declare_type_index);
  2242. #endif
  2243. function->func_type =
  2244. (WASMFuncType *)parent_module->types[declare_type_index];
  2245. function->module_name = (char *)sub_module_name;
  2246. function->field_name = (char *)function_name;
  2247. function->attachment = NULL;
  2248. function->signature = NULL;
  2249. function->call_conv_raw = false;
  2250. /* lookup registered native symbols first */
  2251. if (!no_resolve) {
  2252. wasm_resolve_import_func(parent_module, function);
  2253. }
  2254. return true;
  2255. fail:
  2256. return false;
  2257. }
  2258. static bool
  2259. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2260. uint32 error_buf_size)
  2261. {
  2262. if (max_size < init_size) {
  2263. set_error_buf(error_buf, error_buf_size,
  2264. "size minimum must not be greater than maximum");
  2265. return false;
  2266. }
  2267. return true;
  2268. }
  2269. static bool
  2270. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2271. WASMModule *parent_module, const char *sub_module_name,
  2272. const char *table_name, WASMTableImport *table,
  2273. char *error_buf, uint32 error_buf_size)
  2274. {
  2275. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2276. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2277. declare_max_size = 0;
  2278. #if WASM_ENABLE_MULTI_MODULE != 0
  2279. WASMModule *sub_module = NULL;
  2280. WASMTable *linked_table = NULL;
  2281. #endif
  2282. #if WASM_ENABLE_GC != 0
  2283. WASMRefType ref_type;
  2284. bool need_ref_type_map;
  2285. #endif
  2286. bool is_table64 = false;
  2287. #if WASM_ENABLE_GC == 0
  2288. CHECK_BUF(p, p_end, 1);
  2289. /* 0x70 or 0x6F */
  2290. declare_elem_type = read_uint8(p);
  2291. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2292. #if WASM_ENABLE_REF_TYPES != 0
  2293. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2294. #endif
  2295. ) {
  2296. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2297. return false;
  2298. }
  2299. #else /* else of WASM_ENABLE_GC == 0 */
  2300. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2301. &need_ref_type_map, &ref_type, false, error_buf,
  2302. error_buf_size)) {
  2303. return false;
  2304. }
  2305. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2306. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2307. return false;
  2308. }
  2309. declare_elem_type = ref_type.ref_type;
  2310. if (need_ref_type_map) {
  2311. if (!(table->table_type.elem_ref_type =
  2312. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2313. error_buf, error_buf_size))) {
  2314. return false;
  2315. }
  2316. }
  2317. #if TRACE_WASM_LOADER != 0
  2318. os_printf("import table type: ");
  2319. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2320. os_printf("\n");
  2321. #endif
  2322. #endif /* end of WASM_ENABLE_GC == 0 */
  2323. p_org = p;
  2324. read_leb_uint32(p, p_end, table_flag);
  2325. is_table64 = table_flag & TABLE64_FLAG;
  2326. if (p - p_org > 1) {
  2327. LOG_VERBOSE("integer representation too long(import table)");
  2328. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2329. return false;
  2330. }
  2331. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2332. return false;
  2333. }
  2334. read_leb_uint32(p, p_end, declare_init_size);
  2335. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2336. read_leb_uint32(p, p_end, declare_max_size);
  2337. if (!check_table_max_size(declare_init_size, declare_max_size,
  2338. error_buf, error_buf_size))
  2339. return false;
  2340. }
  2341. adjust_table_max_size(is_table64, declare_init_size,
  2342. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2343. *p_buf = p;
  2344. #if WASM_ENABLE_MULTI_MODULE != 0
  2345. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2346. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2347. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2348. error_buf_size);
  2349. if (sub_module) {
  2350. linked_table = wasm_loader_resolve_table(
  2351. sub_module_name, table_name, declare_init_size,
  2352. declare_max_size, error_buf, error_buf_size);
  2353. if (linked_table) {
  2354. /* reset with linked table limit */
  2355. declare_elem_type = linked_table->table_type.elem_type;
  2356. declare_init_size = linked_table->table_type.init_size;
  2357. declare_max_size = linked_table->table_type.max_size;
  2358. table_flag = linked_table->table_type.flags;
  2359. table->import_table_linked = linked_table;
  2360. table->import_module = sub_module;
  2361. }
  2362. }
  2363. }
  2364. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2365. /* (table (export "table") 10 20 funcref) */
  2366. /* (table (export "table64") 10 20 funcref) */
  2367. /* we need this section working in wamrc */
  2368. if (!strcmp("spectest", sub_module_name)) {
  2369. const uint32 spectest_table_init_size = 10;
  2370. const uint32 spectest_table_max_size = 20;
  2371. if (strcmp("table", table_name)
  2372. #if WASM_ENABLE_MEMORY64 != 0
  2373. && strcmp("table64", table_name)
  2374. #endif
  2375. ) {
  2376. set_error_buf(error_buf, error_buf_size,
  2377. "incompatible import type or unknown import");
  2378. return false;
  2379. }
  2380. if (declare_init_size > spectest_table_init_size
  2381. || declare_max_size < spectest_table_max_size) {
  2382. set_error_buf(error_buf, error_buf_size,
  2383. "incompatible import type");
  2384. return false;
  2385. }
  2386. declare_init_size = spectest_table_init_size;
  2387. declare_max_size = spectest_table_max_size;
  2388. }
  2389. /* now we believe all declaration are ok */
  2390. table->table_type.elem_type = declare_elem_type;
  2391. table->table_type.init_size = declare_init_size;
  2392. table->table_type.flags = table_flag;
  2393. table->table_type.max_size = declare_max_size;
  2394. #if WASM_ENABLE_WAMR_COMPILER != 0
  2395. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2396. parent_module->is_ref_types_used = true;
  2397. #endif
  2398. (void)parent_module;
  2399. return true;
  2400. fail:
  2401. return false;
  2402. }
  2403. static bool
  2404. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2405. uint32 error_buf_size)
  2406. {
  2407. uint32 default_max_size =
  2408. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2409. if (!is_memory64 && init_size > default_max_size) {
  2410. set_error_buf(error_buf, error_buf_size,
  2411. "memory size must be at most 65536 pages (4GiB)");
  2412. return false;
  2413. }
  2414. #if WASM_ENABLE_MEMORY64 != 0
  2415. else if (is_memory64 && init_size > default_max_size) {
  2416. set_error_buf(
  2417. error_buf, error_buf_size,
  2418. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2419. return false;
  2420. }
  2421. #endif
  2422. return true;
  2423. }
  2424. static bool
  2425. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2426. char *error_buf, uint32 error_buf_size)
  2427. {
  2428. uint32 default_max_size =
  2429. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2430. if (max_size < init_size) {
  2431. set_error_buf(error_buf, error_buf_size,
  2432. "size minimum must not be greater than maximum");
  2433. return false;
  2434. }
  2435. if (!is_memory64 && max_size > default_max_size) {
  2436. set_error_buf(error_buf, error_buf_size,
  2437. "memory size must be at most 65536 pages (4GiB)");
  2438. return false;
  2439. }
  2440. #if WASM_ENABLE_MEMORY64 != 0
  2441. else if (is_memory64 && max_size > default_max_size) {
  2442. set_error_buf(
  2443. error_buf, error_buf_size,
  2444. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2445. return false;
  2446. }
  2447. #endif
  2448. return true;
  2449. }
  2450. static bool
  2451. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2452. WASMModule *parent_module, const char *sub_module_name,
  2453. const char *memory_name, WASMMemoryImport *memory,
  2454. char *error_buf, uint32 error_buf_size)
  2455. {
  2456. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2457. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2458. uint32 pool_size = wasm_runtime_memory_pool_size();
  2459. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2460. / DEFAULT_NUM_BYTES_PER_PAGE;
  2461. #else
  2462. uint32 max_page_count;
  2463. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2464. uint32 mem_flag = 0;
  2465. bool is_memory64 = false;
  2466. uint32 declare_init_page_count = 0;
  2467. uint32 declare_max_page_count = 0;
  2468. #if WASM_ENABLE_MULTI_MODULE != 0
  2469. WASMModule *sub_module = NULL;
  2470. WASMMemory *linked_memory = NULL;
  2471. #endif
  2472. p_org = p;
  2473. read_leb_uint32(p, p_end, mem_flag);
  2474. is_memory64 = mem_flag & MEMORY64_FLAG;
  2475. if (p - p_org > 1) {
  2476. LOG_VERBOSE("integer representation too long(import memory)");
  2477. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2478. return false;
  2479. }
  2480. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2481. return false;
  2482. }
  2483. read_leb_uint32(p, p_end, declare_init_page_count);
  2484. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2485. error_buf_size)) {
  2486. return false;
  2487. }
  2488. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2489. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2490. #endif
  2491. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2492. read_leb_uint32(p, p_end, declare_max_page_count);
  2493. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2494. declare_max_page_count, error_buf,
  2495. error_buf_size)) {
  2496. return false;
  2497. }
  2498. if (declare_max_page_count > max_page_count) {
  2499. declare_max_page_count = max_page_count;
  2500. }
  2501. }
  2502. else {
  2503. /* Limit the maximum memory size to max_page_count */
  2504. declare_max_page_count = max_page_count;
  2505. }
  2506. #if WASM_ENABLE_MULTI_MODULE != 0
  2507. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2508. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2509. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2510. error_buf_size);
  2511. if (sub_module) {
  2512. linked_memory = wasm_loader_resolve_memory(
  2513. sub_module_name, memory_name, declare_init_page_count,
  2514. declare_max_page_count, error_buf, error_buf_size);
  2515. if (linked_memory) {
  2516. /**
  2517. * reset with linked memory limit
  2518. */
  2519. memory->import_module = sub_module;
  2520. memory->import_memory_linked = linked_memory;
  2521. declare_init_page_count = linked_memory->init_page_count;
  2522. declare_max_page_count = linked_memory->max_page_count;
  2523. }
  2524. }
  2525. }
  2526. #endif
  2527. /* (memory (export "memory") 1 2) */
  2528. if (!strcmp("spectest", sub_module_name)) {
  2529. uint32 spectest_memory_init_page = 1;
  2530. uint32 spectest_memory_max_page = 2;
  2531. if (strcmp("memory", memory_name)) {
  2532. set_error_buf(error_buf, error_buf_size,
  2533. "incompatible import type or unknown import");
  2534. return false;
  2535. }
  2536. if (declare_init_page_count > spectest_memory_init_page
  2537. || declare_max_page_count < spectest_memory_max_page) {
  2538. set_error_buf(error_buf, error_buf_size,
  2539. "incompatible import type");
  2540. return false;
  2541. }
  2542. declare_init_page_count = spectest_memory_init_page;
  2543. declare_max_page_count = spectest_memory_max_page;
  2544. }
  2545. #if WASM_ENABLE_WASI_TEST != 0
  2546. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2547. else if (!strcmp("foo", sub_module_name)) {
  2548. uint32 spectest_memory_init_page = 1;
  2549. uint32 spectest_memory_max_page = 1;
  2550. if (strcmp("bar", memory_name)) {
  2551. set_error_buf(error_buf, error_buf_size,
  2552. "incompatible import type or unknown import");
  2553. return false;
  2554. }
  2555. if (declare_init_page_count > spectest_memory_init_page
  2556. || declare_max_page_count < spectest_memory_max_page) {
  2557. set_error_buf(error_buf, error_buf_size,
  2558. "incompatible import type");
  2559. return false;
  2560. }
  2561. declare_init_page_count = spectest_memory_init_page;
  2562. declare_max_page_count = spectest_memory_max_page;
  2563. }
  2564. #endif
  2565. /* now we believe all declaration are ok */
  2566. memory->mem_type.flags = mem_flag;
  2567. memory->mem_type.init_page_count = declare_init_page_count;
  2568. memory->mem_type.max_page_count = declare_max_page_count;
  2569. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2570. *p_buf = p;
  2571. (void)parent_module;
  2572. return true;
  2573. fail:
  2574. return false;
  2575. }
  2576. #if WASM_ENABLE_TAGS != 0
  2577. static bool
  2578. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2579. const WASMModule *parent_module, /* this module ! */
  2580. const char *sub_module_name, const char *tag_name,
  2581. WASMTagImport *tag, /* structure to fill */
  2582. char *error_buf, uint32 error_buf_size)
  2583. {
  2584. /* attribute and type of the import statement */
  2585. uint8 declare_tag_attribute;
  2586. uint32 declare_type_index;
  2587. const uint8 *p = *p_buf, *p_end = buf_end;
  2588. #if WASM_ENABLE_MULTI_MODULE != 0
  2589. WASMModule *sub_module = NULL;
  2590. #endif
  2591. /* get the one byte attribute */
  2592. CHECK_BUF(p, p_end, 1);
  2593. declare_tag_attribute = read_uint8(p);
  2594. if (declare_tag_attribute != 0) {
  2595. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2596. goto fail;
  2597. }
  2598. /* get type */
  2599. read_leb_uint32(p, p_end, declare_type_index);
  2600. /* compare against module->types */
  2601. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2602. error_buf_size)) {
  2603. goto fail;
  2604. }
  2605. WASMFuncType *declare_tag_type =
  2606. (WASMFuncType *)parent_module->types[declare_type_index];
  2607. /* check, that the type of the declared tag returns void */
  2608. if (declare_tag_type->result_count != 0) {
  2609. set_error_buf(error_buf, error_buf_size,
  2610. "tag type signature does not return void");
  2611. goto fail;
  2612. }
  2613. #if WASM_ENABLE_MULTI_MODULE != 0
  2614. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2615. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2616. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2617. error_buf_size);
  2618. if (sub_module) {
  2619. /* wasm_loader_resolve_tag checks, that the imported tag
  2620. * and the declared tag have the same type
  2621. */
  2622. uint32 linked_tag_index = 0;
  2623. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2624. sub_module_name, tag_name, declare_tag_type,
  2625. &linked_tag_index /* out */, error_buf, error_buf_size);
  2626. if (linked_tag) {
  2627. tag->import_module = sub_module;
  2628. tag->import_tag_linked = linked_tag;
  2629. tag->import_tag_index_linked = linked_tag_index;
  2630. }
  2631. }
  2632. }
  2633. #endif
  2634. /* store to module tag declarations */
  2635. tag->attribute = declare_tag_attribute;
  2636. tag->type = declare_type_index;
  2637. tag->module_name = (char *)sub_module_name;
  2638. tag->field_name = (char *)tag_name;
  2639. tag->tag_type = declare_tag_type;
  2640. *p_buf = p;
  2641. (void)parent_module;
  2642. LOG_VERBOSE("Load tag import success\n");
  2643. return true;
  2644. fail:
  2645. return false;
  2646. }
  2647. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2648. static bool
  2649. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2650. WASMModule *parent_module, char *sub_module_name,
  2651. char *global_name, WASMGlobalImport *global, char *error_buf,
  2652. uint32 error_buf_size)
  2653. {
  2654. const uint8 *p = *p_buf, *p_end = buf_end;
  2655. uint8 declare_type = 0;
  2656. uint8 declare_mutable = 0;
  2657. #if WASM_ENABLE_MULTI_MODULE != 0
  2658. WASMModule *sub_module = NULL;
  2659. WASMGlobal *linked_global = NULL;
  2660. #endif
  2661. #if WASM_ENABLE_GC != 0
  2662. WASMRefType ref_type;
  2663. bool need_ref_type_map;
  2664. #endif
  2665. bool ret = false;
  2666. #if WASM_ENABLE_GC == 0
  2667. CHECK_BUF(p, p_end, 2);
  2668. /* global type */
  2669. declare_type = read_uint8(p);
  2670. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2671. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2672. return false;
  2673. }
  2674. declare_mutable = read_uint8(p);
  2675. #else
  2676. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2677. &need_ref_type_map, &ref_type, false, error_buf,
  2678. error_buf_size)) {
  2679. return false;
  2680. }
  2681. declare_type = ref_type.ref_type;
  2682. if (need_ref_type_map) {
  2683. if (!(global->ref_type =
  2684. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2685. error_buf, error_buf_size))) {
  2686. return false;
  2687. }
  2688. }
  2689. #if TRACE_WASM_LOADER != 0
  2690. os_printf("import global type: ");
  2691. wasm_dump_value_type(declare_type, global->ref_type);
  2692. os_printf("\n");
  2693. #endif
  2694. CHECK_BUF(p, p_end, 1);
  2695. declare_mutable = read_uint8(p);
  2696. #endif /* end of WASM_ENABLE_GC == 0 */
  2697. *p_buf = p;
  2698. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2699. return false;
  2700. }
  2701. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2702. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2703. global);
  2704. if (ret) {
  2705. if (global->type.val_type != declare_type
  2706. || global->type.is_mutable != declare_mutable) {
  2707. set_error_buf(error_buf, error_buf_size,
  2708. "incompatible import type");
  2709. return false;
  2710. }
  2711. global->is_linked = true;
  2712. }
  2713. #endif
  2714. #if WASM_ENABLE_MULTI_MODULE != 0
  2715. if (!global->is_linked
  2716. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2717. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2718. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2719. error_buf_size);
  2720. if (sub_module) {
  2721. /* check sub modules */
  2722. linked_global = wasm_loader_resolve_global(
  2723. sub_module_name, global_name, declare_type, declare_mutable,
  2724. error_buf, error_buf_size);
  2725. if (linked_global) {
  2726. global->import_module = sub_module;
  2727. global->import_global_linked = linked_global;
  2728. global->is_linked = true;
  2729. }
  2730. }
  2731. }
  2732. #endif
  2733. global->module_name = sub_module_name;
  2734. global->field_name = global_name;
  2735. global->type.val_type = declare_type;
  2736. global->type.is_mutable = (declare_mutable == 1);
  2737. #if WASM_ENABLE_WAMR_COMPILER != 0
  2738. if (global->type.val_type == VALUE_TYPE_V128)
  2739. parent_module->is_simd_used = true;
  2740. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2741. parent_module->is_ref_types_used = true;
  2742. #endif
  2743. (void)parent_module;
  2744. (void)ret;
  2745. return true;
  2746. fail:
  2747. return false;
  2748. }
  2749. static bool
  2750. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2751. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2752. {
  2753. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2754. #if WASM_ENABLE_GC != 0
  2755. WASMRefType ref_type;
  2756. bool need_ref_type_map;
  2757. #endif
  2758. bool is_table64 = false;
  2759. #if WASM_ENABLE_GC == 0
  2760. CHECK_BUF(p, p_end, 1);
  2761. /* 0x70 or 0x6F */
  2762. table->table_type.elem_type = read_uint8(p);
  2763. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2764. #if WASM_ENABLE_REF_TYPES != 0
  2765. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2766. #endif
  2767. ) {
  2768. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2769. return false;
  2770. }
  2771. #else /* else of WASM_ENABLE_GC == 0 */
  2772. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2773. &need_ref_type_map, &ref_type, false, error_buf,
  2774. error_buf_size)) {
  2775. return false;
  2776. }
  2777. table->table_type.elem_type = ref_type.ref_type;
  2778. if (need_ref_type_map) {
  2779. if (!(table->table_type.elem_ref_type =
  2780. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2781. error_buf_size))) {
  2782. return false;
  2783. }
  2784. }
  2785. #if TRACE_WASM_LOADER != 0
  2786. os_printf("table type: ");
  2787. wasm_dump_value_type(table->table_type.elem_type,
  2788. table->table_type.elem_ref_type);
  2789. os_printf("\n");
  2790. #endif
  2791. #endif /* end of WASM_ENABLE_GC == 0 */
  2792. p_org = p;
  2793. read_leb_uint32(p, p_end, table->table_type.flags);
  2794. is_table64 = table->table_type.flags & TABLE64_FLAG;
  2795. if (p - p_org > 1) {
  2796. LOG_VERBOSE("integer representation too long(table)");
  2797. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2798. return false;
  2799. }
  2800. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  2801. error_buf_size, false)) {
  2802. return false;
  2803. }
  2804. read_leb_uint32(p, p_end, table->table_type.init_size);
  2805. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  2806. read_leb_uint32(p, p_end, table->table_type.max_size);
  2807. if (!check_table_max_size(table->table_type.init_size,
  2808. table->table_type.max_size, error_buf,
  2809. error_buf_size))
  2810. return false;
  2811. }
  2812. adjust_table_max_size(is_table64, table->table_type.init_size,
  2813. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  2814. &table->table_type.max_size);
  2815. #if WASM_ENABLE_WAMR_COMPILER != 0
  2816. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2817. module->is_ref_types_used = true;
  2818. #endif
  2819. *p_buf = p;
  2820. return true;
  2821. fail:
  2822. return false;
  2823. }
  2824. static bool
  2825. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2826. char *error_buf, uint32 error_buf_size)
  2827. {
  2828. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2829. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2830. uint32 pool_size = wasm_runtime_memory_pool_size();
  2831. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2832. / DEFAULT_NUM_BYTES_PER_PAGE;
  2833. #else
  2834. uint32 max_page_count;
  2835. #endif
  2836. bool is_memory64 = false;
  2837. p_org = p;
  2838. read_leb_uint32(p, p_end, memory->flags);
  2839. is_memory64 = memory->flags & MEMORY64_FLAG;
  2840. if (p - p_org > 1) {
  2841. LOG_VERBOSE("integer representation too long(memory)");
  2842. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2843. return false;
  2844. }
  2845. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2846. false)) {
  2847. return false;
  2848. }
  2849. read_leb_uint32(p, p_end, memory->init_page_count);
  2850. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2851. error_buf_size))
  2852. return false;
  2853. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2854. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2855. #endif
  2856. if (memory->flags & 1) {
  2857. read_leb_uint32(p, p_end, memory->max_page_count);
  2858. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2859. memory->max_page_count, error_buf,
  2860. error_buf_size))
  2861. return false;
  2862. if (memory->max_page_count > max_page_count)
  2863. memory->max_page_count = max_page_count;
  2864. }
  2865. else {
  2866. /* Limit the maximum memory size to max_page_count */
  2867. memory->max_page_count = max_page_count;
  2868. }
  2869. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2870. *p_buf = p;
  2871. return true;
  2872. fail:
  2873. return false;
  2874. }
  2875. static int
  2876. cmp_export_name(const void *a, const void *b)
  2877. {
  2878. return strcmp(*(char **)a, *(char **)b);
  2879. }
  2880. static bool
  2881. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2882. bool is_load_from_file_buf, bool no_resolve,
  2883. char *error_buf, uint32 error_buf_size)
  2884. {
  2885. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2886. uint32 import_count, name_len, type_index, i, u32, flags;
  2887. uint64 total_size;
  2888. WASMImport *import;
  2889. WASMImport *import_functions = NULL, *import_tables = NULL;
  2890. WASMImport *import_memories = NULL, *import_globals = NULL;
  2891. #if WASM_ENABLE_TAGS != 0
  2892. WASMImport *import_tags = NULL;
  2893. #endif
  2894. char *sub_module_name, *field_name;
  2895. uint8 u8, kind, global_type;
  2896. read_leb_uint32(p, p_end, import_count);
  2897. if (import_count) {
  2898. module->import_count = import_count;
  2899. total_size = sizeof(WASMImport) * (uint64)import_count;
  2900. if (!(module->imports =
  2901. loader_malloc(total_size, error_buf, error_buf_size))) {
  2902. return false;
  2903. }
  2904. p_old = p;
  2905. /* Scan firstly to get import count of each type */
  2906. for (i = 0; i < import_count; i++) {
  2907. /* module name */
  2908. read_leb_uint32(p, p_end, name_len);
  2909. CHECK_BUF(p, p_end, name_len);
  2910. p += name_len;
  2911. /* field name */
  2912. read_leb_uint32(p, p_end, name_len);
  2913. CHECK_BUF(p, p_end, name_len);
  2914. p += name_len;
  2915. CHECK_BUF(p, p_end, 1);
  2916. /* 0x00/0x01/0x02/0x03/0x04 */
  2917. kind = read_uint8(p);
  2918. switch (kind) {
  2919. case IMPORT_KIND_FUNC: /* import function */
  2920. read_leb_uint32(p, p_end, type_index);
  2921. module->import_function_count++;
  2922. break;
  2923. case IMPORT_KIND_TABLE: /* import table */
  2924. CHECK_BUF(p, p_end, 1);
  2925. /* 0x70 */
  2926. u8 = read_uint8(p);
  2927. #if WASM_ENABLE_GC != 0
  2928. if (wasm_is_reftype_htref_nullable(u8)) {
  2929. int32 heap_type;
  2930. read_leb_int32(p, p_end, heap_type);
  2931. (void)heap_type;
  2932. }
  2933. #endif
  2934. read_leb_uint32(p, p_end, flags);
  2935. read_leb_uint32(p, p_end, u32);
  2936. if (flags & 1)
  2937. read_leb_uint32(p, p_end, u32);
  2938. module->import_table_count++;
  2939. if (module->import_table_count > 1) {
  2940. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2941. set_error_buf(error_buf, error_buf_size,
  2942. "multiple tables");
  2943. return false;
  2944. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2945. module->is_ref_types_used = true;
  2946. #endif
  2947. }
  2948. break;
  2949. case IMPORT_KIND_MEMORY: /* import memory */
  2950. read_leb_uint32(p, p_end, flags);
  2951. read_leb_uint32(p, p_end, u32);
  2952. if (flags & 1)
  2953. read_leb_uint32(p, p_end, u32);
  2954. module->import_memory_count++;
  2955. #if WASM_ENABLE_MULTI_MEMORY == 0
  2956. if (module->import_memory_count > 1) {
  2957. set_error_buf(error_buf, error_buf_size,
  2958. "multiple memories");
  2959. return false;
  2960. }
  2961. #endif
  2962. break;
  2963. #if WASM_ENABLE_TAGS != 0
  2964. case IMPORT_KIND_TAG: /* import tags */
  2965. /* it only counts the number of tags to import */
  2966. module->import_tag_count++;
  2967. CHECK_BUF(p, p_end, 1);
  2968. u8 = read_uint8(p);
  2969. read_leb_uint32(p, p_end, type_index);
  2970. break;
  2971. #endif
  2972. case IMPORT_KIND_GLOBAL: /* import global */
  2973. #if WASM_ENABLE_GC != 0
  2974. /* valtype */
  2975. CHECK_BUF(p, p_end, 1);
  2976. global_type = read_uint8(p);
  2977. if (wasm_is_reftype_htref_nullable(global_type)) {
  2978. int32 heap_type;
  2979. read_leb_int32(p, p_end, heap_type);
  2980. (void)heap_type;
  2981. }
  2982. /* mutability */
  2983. CHECK_BUF(p, p_end, 1);
  2984. p += 1;
  2985. #else
  2986. CHECK_BUF(p, p_end, 2);
  2987. p += 2;
  2988. #endif
  2989. (void)global_type;
  2990. module->import_global_count++;
  2991. break;
  2992. default:
  2993. set_error_buf(error_buf, error_buf_size,
  2994. "invalid import kind");
  2995. return false;
  2996. }
  2997. }
  2998. if (module->import_function_count)
  2999. import_functions = module->import_functions = module->imports;
  3000. if (module->import_table_count)
  3001. import_tables = module->import_tables =
  3002. module->imports + module->import_function_count;
  3003. if (module->import_memory_count)
  3004. import_memories = module->import_memories =
  3005. module->imports + module->import_function_count
  3006. + module->import_table_count;
  3007. #if WASM_ENABLE_TAGS != 0
  3008. if (module->import_tag_count)
  3009. import_tags = module->import_tags =
  3010. module->imports + module->import_function_count
  3011. + module->import_table_count + module->import_memory_count;
  3012. if (module->import_global_count)
  3013. import_globals = module->import_globals =
  3014. module->imports + module->import_function_count
  3015. + module->import_table_count + module->import_memory_count
  3016. + module->import_tag_count;
  3017. #else
  3018. if (module->import_global_count)
  3019. import_globals = module->import_globals =
  3020. module->imports + module->import_function_count
  3021. + module->import_table_count + module->import_memory_count;
  3022. #endif
  3023. p = p_old;
  3024. /* Scan again to resolve the data */
  3025. for (i = 0; i < import_count; i++) {
  3026. /* load module name */
  3027. read_leb_uint32(p, p_end, name_len);
  3028. CHECK_BUF(p, p_end, name_len);
  3029. if (!(sub_module_name = wasm_const_str_list_insert(
  3030. p, name_len, module, is_load_from_file_buf, error_buf,
  3031. error_buf_size))) {
  3032. return false;
  3033. }
  3034. p += name_len;
  3035. /* load field name */
  3036. read_leb_uint32(p, p_end, name_len);
  3037. CHECK_BUF(p, p_end, name_len);
  3038. if (!(field_name = wasm_const_str_list_insert(
  3039. p, name_len, module, is_load_from_file_buf, error_buf,
  3040. error_buf_size))) {
  3041. return false;
  3042. }
  3043. p += name_len;
  3044. CHECK_BUF(p, p_end, 1);
  3045. /* 0x00/0x01/0x02/0x03/0x4 */
  3046. kind = read_uint8(p);
  3047. switch (kind) {
  3048. case IMPORT_KIND_FUNC: /* import function */
  3049. bh_assert(import_functions);
  3050. import = import_functions++;
  3051. if (!load_function_import(&p, p_end, module,
  3052. sub_module_name, field_name,
  3053. &import->u.function, no_resolve,
  3054. error_buf, error_buf_size)) {
  3055. return false;
  3056. }
  3057. break;
  3058. case IMPORT_KIND_TABLE: /* import table */
  3059. bh_assert(import_tables);
  3060. import = import_tables++;
  3061. if (!load_table_import(&p, p_end, module, sub_module_name,
  3062. field_name, &import->u.table,
  3063. error_buf, error_buf_size)) {
  3064. LOG_DEBUG("can not import such a table (%s,%s)",
  3065. sub_module_name, field_name);
  3066. return false;
  3067. }
  3068. break;
  3069. case IMPORT_KIND_MEMORY: /* import memory */
  3070. bh_assert(import_memories);
  3071. import = import_memories++;
  3072. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3073. field_name, &import->u.memory,
  3074. error_buf, error_buf_size)) {
  3075. return false;
  3076. }
  3077. break;
  3078. #if WASM_ENABLE_TAGS != 0
  3079. case IMPORT_KIND_TAG:
  3080. bh_assert(import_tags);
  3081. import = import_tags++;
  3082. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3083. field_name, &import->u.tag, error_buf,
  3084. error_buf_size)) {
  3085. return false;
  3086. }
  3087. break;
  3088. #endif
  3089. case IMPORT_KIND_GLOBAL: /* import global */
  3090. bh_assert(import_globals);
  3091. import = import_globals++;
  3092. if (!load_global_import(&p, p_end, module, sub_module_name,
  3093. field_name, &import->u.global,
  3094. error_buf, error_buf_size)) {
  3095. return false;
  3096. }
  3097. break;
  3098. default:
  3099. set_error_buf(error_buf, error_buf_size,
  3100. "invalid import kind");
  3101. return false;
  3102. }
  3103. import->kind = kind;
  3104. import->u.names.module_name = sub_module_name;
  3105. import->u.names.field_name = field_name;
  3106. }
  3107. #if WASM_ENABLE_LIBC_WASI != 0
  3108. import = module->import_functions;
  3109. for (i = 0; i < module->import_function_count; i++, import++) {
  3110. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3111. || !strcmp(import->u.names.module_name,
  3112. "wasi_snapshot_preview1")) {
  3113. module->import_wasi_api = true;
  3114. break;
  3115. }
  3116. }
  3117. #endif
  3118. }
  3119. if (p != p_end) {
  3120. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3121. return false;
  3122. }
  3123. LOG_VERBOSE("Load import section success.\n");
  3124. (void)u8;
  3125. (void)u32;
  3126. (void)type_index;
  3127. return true;
  3128. fail:
  3129. return false;
  3130. }
  3131. static bool
  3132. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3133. uint32 error_buf_size)
  3134. {
  3135. WASMFuncType *param_type = func->func_type;
  3136. uint32 param_count = param_type->param_count;
  3137. uint8 *param_types = param_type->types;
  3138. uint32 local_count = func->local_count;
  3139. uint8 *local_types = func->local_types;
  3140. uint32 i, local_offset = 0;
  3141. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3142. /*
  3143. * Only allocate memory when total_size is not 0,
  3144. * or the return value of malloc(0) might be NULL on some platforms,
  3145. * which causes wasm loader return false.
  3146. */
  3147. if (total_size > 0
  3148. && !(func->local_offsets =
  3149. loader_malloc(total_size, error_buf, error_buf_size))) {
  3150. return false;
  3151. }
  3152. for (i = 0; i < param_count; i++) {
  3153. func->local_offsets[i] = (uint16)local_offset;
  3154. local_offset += wasm_value_type_cell_num(param_types[i]);
  3155. }
  3156. for (i = 0; i < local_count; i++) {
  3157. func->local_offsets[param_count + i] = (uint16)local_offset;
  3158. local_offset += wasm_value_type_cell_num(local_types[i]);
  3159. }
  3160. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3161. return true;
  3162. }
  3163. static bool
  3164. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3165. const uint8 *buf_code, const uint8 *buf_code_end,
  3166. WASMModule *module, char *error_buf,
  3167. uint32 error_buf_size)
  3168. {
  3169. const uint8 *p = buf, *p_end = buf_end;
  3170. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3171. uint32 func_count;
  3172. uint64 total_size;
  3173. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3174. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3175. uint8 type;
  3176. WASMFunction *func;
  3177. #if WASM_ENABLE_GC != 0
  3178. bool need_ref_type_map;
  3179. WASMRefType ref_type;
  3180. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3181. #endif
  3182. read_leb_uint32(p, p_end, func_count);
  3183. if (buf_code)
  3184. read_leb_uint32(p_code, buf_code_end, code_count);
  3185. if (func_count != code_count) {
  3186. set_error_buf(error_buf, error_buf_size,
  3187. "function and code section have inconsistent lengths or "
  3188. "unexpected end");
  3189. return false;
  3190. }
  3191. if (is_indices_overflow(module->import_function_count, func_count,
  3192. error_buf, error_buf_size))
  3193. return false;
  3194. if (func_count) {
  3195. module->function_count = func_count;
  3196. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3197. if (!(module->functions =
  3198. loader_malloc(total_size, error_buf, error_buf_size))) {
  3199. return false;
  3200. }
  3201. for (i = 0; i < func_count; i++) {
  3202. /* Resolve function type */
  3203. read_leb_uint32(p, p_end, type_index);
  3204. if (!check_function_type(module, type_index, error_buf,
  3205. error_buf_size)) {
  3206. return false;
  3207. }
  3208. #if WASM_ENABLE_GC != 0
  3209. type_index_org = type_index;
  3210. #endif
  3211. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3212. && WASM_ENABLE_GC == 0
  3213. type_index = wasm_get_smallest_type_idx(
  3214. module->types, module->type_count, type_index);
  3215. #endif
  3216. read_leb_uint32(p_code, buf_code_end, code_size);
  3217. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3218. set_error_buf(error_buf, error_buf_size,
  3219. "invalid function code size");
  3220. return false;
  3221. }
  3222. /* Resolve local set count */
  3223. p_code_end = p_code + code_size;
  3224. local_count = 0;
  3225. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3226. p_code_save = p_code;
  3227. #if WASM_ENABLE_GC != 0
  3228. ref_type_map_count = 0;
  3229. #endif
  3230. /* Calculate total local count */
  3231. for (j = 0; j < local_set_count; j++) {
  3232. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3233. if (sub_local_count > UINT32_MAX - local_count) {
  3234. set_error_buf(error_buf, error_buf_size, "too many locals");
  3235. return false;
  3236. }
  3237. #if WASM_ENABLE_GC == 0
  3238. CHECK_BUF(p_code, buf_code_end, 1);
  3239. /* 0x7F/0x7E/0x7D/0x7C */
  3240. type = read_uint8(p_code);
  3241. local_count += sub_local_count;
  3242. #if WASM_ENABLE_WAMR_COMPILER != 0
  3243. /* If any value's type is v128, mark the module as SIMD used */
  3244. if (type == VALUE_TYPE_V128)
  3245. module->is_simd_used = true;
  3246. #endif
  3247. #else
  3248. if (!resolve_value_type(&p_code, buf_code_end, module,
  3249. module->type_count, &need_ref_type_map,
  3250. &ref_type, false, error_buf,
  3251. error_buf_size)) {
  3252. return false;
  3253. }
  3254. local_count += sub_local_count;
  3255. if (need_ref_type_map)
  3256. ref_type_map_count += sub_local_count;
  3257. #endif
  3258. }
  3259. /* Code size in code entry can't be smaller than size of vec(locals)
  3260. * + expr(at least 1 for opcode end). And expressions are encoded by
  3261. * their instruction sequence terminated with an explicit 0x0B
  3262. * opcode for end. */
  3263. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3264. set_error_buf(
  3265. error_buf, error_buf_size,
  3266. "section size mismatch: function body END opcode expected");
  3267. return false;
  3268. }
  3269. /* Alloc memory, layout: function structure + local types */
  3270. code_size = (uint32)(p_code_end - p_code);
  3271. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3272. if (!(func = module->functions[i] =
  3273. loader_malloc(total_size, error_buf, error_buf_size))) {
  3274. return false;
  3275. }
  3276. #if WASM_ENABLE_GC != 0
  3277. if (ref_type_map_count > 0) {
  3278. total_size =
  3279. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3280. if (!(func->local_ref_type_maps = loader_malloc(
  3281. total_size, error_buf, error_buf_size))) {
  3282. return false;
  3283. }
  3284. func->local_ref_type_map_count = ref_type_map_count;
  3285. }
  3286. #endif
  3287. /* Set function type, local count, code size and code body */
  3288. func->func_type = (WASMFuncType *)module->types[type_index];
  3289. func->local_count = local_count;
  3290. if (local_count > 0)
  3291. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3292. func->code_size = code_size;
  3293. /*
  3294. * we shall make a copy of code body [p_code, p_code + code_size]
  3295. * when we are worrying about inappropriate releasing behaviour.
  3296. * all code bodies are actually in a buffer which user allocates in
  3297. * his embedding environment and we don't have power on them.
  3298. * it will be like:
  3299. * code_body_cp = malloc(code_size);
  3300. * memcpy(code_body_cp, p_code, code_size);
  3301. * func->code = code_body_cp;
  3302. */
  3303. func->code = (uint8 *)p_code;
  3304. #if WASM_ENABLE_GC != 0
  3305. func->type_idx = type_index_org;
  3306. #endif
  3307. #if WASM_ENABLE_GC != 0
  3308. t = 0;
  3309. #endif
  3310. /* Load each local type */
  3311. p_code = p_code_save;
  3312. local_type_index = 0;
  3313. for (j = 0; j < local_set_count; j++) {
  3314. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3315. /* Note: sub_local_count is allowed to be 0 */
  3316. if (local_type_index > UINT32_MAX - sub_local_count
  3317. || local_type_index + sub_local_count > local_count) {
  3318. set_error_buf(error_buf, error_buf_size,
  3319. "invalid local count");
  3320. return false;
  3321. }
  3322. #if WASM_ENABLE_GC == 0
  3323. CHECK_BUF(p_code, buf_code_end, 1);
  3324. /* 0x7F/0x7E/0x7D/0x7C */
  3325. type = read_uint8(p_code);
  3326. if (!is_valid_value_type_for_interpreter(type)) {
  3327. if (type == VALUE_TYPE_V128)
  3328. set_error_buf(error_buf, error_buf_size,
  3329. "v128 value type requires simd feature");
  3330. else if (type == VALUE_TYPE_FUNCREF
  3331. || type == VALUE_TYPE_EXTERNREF)
  3332. set_error_buf(error_buf, error_buf_size,
  3333. "ref value type requires "
  3334. "reference types feature");
  3335. else
  3336. set_error_buf_v(error_buf, error_buf_size,
  3337. "invalid local type 0x%02X", type);
  3338. return false;
  3339. }
  3340. #else
  3341. if (!resolve_value_type(&p_code, buf_code_end, module,
  3342. module->type_count, &need_ref_type_map,
  3343. &ref_type, false, error_buf,
  3344. error_buf_size)) {
  3345. return false;
  3346. }
  3347. if (need_ref_type_map) {
  3348. WASMRefType *ref_type_tmp;
  3349. if (!(ref_type_tmp = reftype_set_insert(
  3350. module->ref_type_set, &ref_type, error_buf,
  3351. error_buf_size))) {
  3352. return false;
  3353. }
  3354. for (k = 0; k < sub_local_count; k++) {
  3355. func->local_ref_type_maps[t + k].ref_type =
  3356. ref_type_tmp;
  3357. func->local_ref_type_maps[t + k].index =
  3358. local_type_index + k;
  3359. }
  3360. t += sub_local_count;
  3361. }
  3362. type = ref_type.ref_type;
  3363. #endif
  3364. for (k = 0; k < sub_local_count; k++) {
  3365. func->local_types[local_type_index++] = type;
  3366. }
  3367. #if WASM_ENABLE_WAMR_COMPILER != 0
  3368. if (type == VALUE_TYPE_V128)
  3369. module->is_simd_used = true;
  3370. else if (type == VALUE_TYPE_FUNCREF
  3371. || type == VALUE_TYPE_EXTERNREF)
  3372. module->is_ref_types_used = true;
  3373. #endif
  3374. }
  3375. bh_assert(local_type_index == func->local_count);
  3376. #if WASM_ENABLE_GC != 0
  3377. bh_assert(t == func->local_ref_type_map_count);
  3378. #if TRACE_WASM_LOADER != 0
  3379. os_printf("func %u, local types: [", i);
  3380. k = 0;
  3381. for (j = 0; j < func->local_count; j++) {
  3382. WASMRefType *ref_type_tmp = NULL;
  3383. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3384. bh_assert(j == func->local_ref_type_maps[k].index);
  3385. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3386. }
  3387. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3388. if (j < func->local_count - 1)
  3389. os_printf(" ");
  3390. }
  3391. os_printf("]\n");
  3392. #endif
  3393. #endif
  3394. func->param_cell_num = func->func_type->param_cell_num;
  3395. func->ret_cell_num = func->func_type->ret_cell_num;
  3396. local_cell_num =
  3397. wasm_get_cell_num(func->local_types, func->local_count);
  3398. if (local_cell_num > UINT16_MAX) {
  3399. set_error_buf(error_buf, error_buf_size,
  3400. "local count too large");
  3401. return false;
  3402. }
  3403. func->local_cell_num = (uint16)local_cell_num;
  3404. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3405. return false;
  3406. p_code = p_code_end;
  3407. }
  3408. }
  3409. if (p != p_end) {
  3410. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3411. return false;
  3412. }
  3413. LOG_VERBOSE("Load function section success.\n");
  3414. return true;
  3415. fail:
  3416. return false;
  3417. }
  3418. static bool
  3419. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3420. char *error_buf, uint32 error_buf_size)
  3421. {
  3422. const uint8 *p = buf, *p_end = buf_end;
  3423. uint32 table_count, i;
  3424. uint64 total_size;
  3425. WASMTable *table;
  3426. read_leb_uint32(p, p_end, table_count);
  3427. if (module->import_table_count + table_count > 1) {
  3428. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3429. /* a total of one table is allowed */
  3430. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3431. return false;
  3432. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3433. module->is_ref_types_used = true;
  3434. #endif
  3435. }
  3436. if (table_count) {
  3437. module->table_count = table_count;
  3438. total_size = sizeof(WASMTable) * (uint64)table_count;
  3439. if (!(module->tables =
  3440. loader_malloc(total_size, error_buf, error_buf_size))) {
  3441. return false;
  3442. }
  3443. /* load each table */
  3444. table = module->tables;
  3445. for (i = 0; i < table_count; i++, table++) {
  3446. #if WASM_ENABLE_GC != 0
  3447. uint8 flag;
  3448. bool has_init = false;
  3449. CHECK_BUF(p, p_end, 1);
  3450. flag = read_uint8(p);
  3451. if (flag == TABLE_INIT_EXPR_FLAG) {
  3452. CHECK_BUF(p, p_end, 1);
  3453. flag = read_uint8(p);
  3454. if (flag != 0x00) {
  3455. set_error_buf(error_buf, error_buf_size,
  3456. "invalid leading bytes for table");
  3457. return false;
  3458. }
  3459. has_init = true;
  3460. }
  3461. else {
  3462. p--;
  3463. }
  3464. #endif /* end of WASM_ENABLE_GC != 0 */
  3465. if (!load_table(&p, p_end, module, table, error_buf,
  3466. error_buf_size))
  3467. return false;
  3468. #if WASM_ENABLE_GC != 0
  3469. if (has_init) {
  3470. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3471. table->table_type.elem_type,
  3472. table->table_type.elem_ref_type, error_buf,
  3473. error_buf_size))
  3474. return false;
  3475. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3476. && table->init_expr.init_expr_type
  3477. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3478. set_error_buf(
  3479. error_buf, error_buf_size,
  3480. "unsupported initializer expression for table");
  3481. return false;
  3482. }
  3483. }
  3484. else {
  3485. if (wasm_is_reftype_htref_non_nullable(
  3486. table->table_type.elem_type)) {
  3487. set_error_buf(
  3488. error_buf, error_buf_size,
  3489. "type mismatch: non-nullable table without init expr");
  3490. return false;
  3491. }
  3492. }
  3493. #endif /* end of WASM_ENABLE_GC != 0 */
  3494. #if WASM_ENABLE_WAMR_COMPILER != 0
  3495. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3496. module->is_ref_types_used = true;
  3497. #endif
  3498. }
  3499. }
  3500. if (p != p_end) {
  3501. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3502. return false;
  3503. }
  3504. LOG_VERBOSE("Load table section success.\n");
  3505. return true;
  3506. fail:
  3507. return false;
  3508. }
  3509. static bool
  3510. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3511. char *error_buf, uint32 error_buf_size)
  3512. {
  3513. const uint8 *p = buf, *p_end = buf_end;
  3514. uint32 memory_count, i;
  3515. uint64 total_size;
  3516. WASMMemory *memory;
  3517. read_leb_uint32(p, p_end, memory_count);
  3518. #if WASM_ENABLE_MULTI_MEMORY == 0
  3519. /* a total of one memory is allowed */
  3520. if (module->import_memory_count + memory_count > 1) {
  3521. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3522. return false;
  3523. }
  3524. #endif
  3525. if (memory_count) {
  3526. module->memory_count = memory_count;
  3527. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3528. if (!(module->memories =
  3529. loader_malloc(total_size, error_buf, error_buf_size))) {
  3530. return false;
  3531. }
  3532. /* load each memory */
  3533. memory = module->memories;
  3534. for (i = 0; i < memory_count; i++, memory++)
  3535. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3536. return false;
  3537. }
  3538. if (p != p_end) {
  3539. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3540. return false;
  3541. }
  3542. LOG_VERBOSE("Load memory section success.\n");
  3543. return true;
  3544. fail:
  3545. return false;
  3546. }
  3547. static bool
  3548. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3549. char *error_buf, uint32 error_buf_size)
  3550. {
  3551. const uint8 *p = buf, *p_end = buf_end;
  3552. uint32 global_count, i;
  3553. uint64 total_size;
  3554. WASMGlobal *global;
  3555. uint8 mutable;
  3556. #if WASM_ENABLE_GC != 0
  3557. bool need_ref_type_map;
  3558. WASMRefType ref_type;
  3559. #endif
  3560. read_leb_uint32(p, p_end, global_count);
  3561. if (is_indices_overflow(module->import_global_count, global_count,
  3562. error_buf, error_buf_size))
  3563. return false;
  3564. module->global_count = 0;
  3565. if (global_count) {
  3566. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3567. if (!(module->globals =
  3568. loader_malloc(total_size, error_buf, error_buf_size))) {
  3569. return false;
  3570. }
  3571. global = module->globals;
  3572. for (i = 0; i < global_count; i++, global++) {
  3573. #if WASM_ENABLE_GC == 0
  3574. CHECK_BUF(p, p_end, 2);
  3575. /* global type */
  3576. global->type.val_type = read_uint8(p);
  3577. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3578. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3579. return false;
  3580. }
  3581. mutable = read_uint8(p);
  3582. #else
  3583. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3584. &need_ref_type_map, &ref_type, false,
  3585. error_buf, error_buf_size)) {
  3586. return false;
  3587. }
  3588. global->type.val_type = ref_type.ref_type;
  3589. CHECK_BUF(p, p_end, 1);
  3590. mutable = read_uint8(p);
  3591. #endif /* end of WASM_ENABLE_GC */
  3592. #if WASM_ENABLE_WAMR_COMPILER != 0
  3593. if (global->type.val_type == VALUE_TYPE_V128)
  3594. module->is_simd_used = true;
  3595. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3596. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3597. module->is_ref_types_used = true;
  3598. #endif
  3599. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3600. return false;
  3601. }
  3602. global->type.is_mutable = mutable ? true : false;
  3603. /* initialize expression */
  3604. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3605. global->type.val_type,
  3606. #if WASM_ENABLE_GC == 0
  3607. NULL,
  3608. #else
  3609. &ref_type,
  3610. #endif
  3611. error_buf, error_buf_size))
  3612. return false;
  3613. #if WASM_ENABLE_GC != 0
  3614. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3615. uint8 global_type;
  3616. WASMRefType *global_ref_type;
  3617. uint32 global_idx = global->init_expr.u.global_index;
  3618. if (global->init_expr.u.global_index
  3619. >= module->import_global_count + i) {
  3620. set_error_buf(error_buf, error_buf_size, "unknown global");
  3621. return false;
  3622. }
  3623. if (global_idx < module->import_global_count) {
  3624. global_type = module->import_globals[global_idx]
  3625. .u.global.type.val_type;
  3626. global_ref_type =
  3627. module->import_globals[global_idx].u.global.ref_type;
  3628. }
  3629. else {
  3630. global_type =
  3631. module
  3632. ->globals[global_idx - module->import_global_count]
  3633. .type.val_type;
  3634. global_ref_type =
  3635. module
  3636. ->globals[global_idx - module->import_global_count]
  3637. .ref_type;
  3638. }
  3639. if (!wasm_reftype_is_subtype_of(
  3640. global_type, global_ref_type, global->type.val_type,
  3641. global->ref_type, module->types, module->type_count)) {
  3642. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3643. return false;
  3644. }
  3645. }
  3646. if (need_ref_type_map) {
  3647. if (!(global->ref_type =
  3648. reftype_set_insert(module->ref_type_set, &ref_type,
  3649. error_buf, error_buf_size))) {
  3650. return false;
  3651. }
  3652. }
  3653. #if TRACE_WASM_LOADER != 0
  3654. os_printf("global type: ");
  3655. wasm_dump_value_type(global->type, global->ref_type);
  3656. os_printf("\n");
  3657. #endif
  3658. #endif
  3659. module->global_count++;
  3660. }
  3661. bh_assert(module->global_count == global_count);
  3662. }
  3663. if (p != p_end) {
  3664. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3665. return false;
  3666. }
  3667. LOG_VERBOSE("Load global section success.\n");
  3668. return true;
  3669. fail:
  3670. return false;
  3671. }
  3672. static bool
  3673. check_duplicate_exports(WASMModule *module, char *error_buf,
  3674. uint32 error_buf_size)
  3675. {
  3676. uint32 i;
  3677. bool result = false;
  3678. char *names_buf[32], **names = names_buf;
  3679. if (module->export_count > 32) {
  3680. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3681. error_buf_size);
  3682. if (!names) {
  3683. return result;
  3684. }
  3685. }
  3686. for (i = 0; i < module->export_count; i++) {
  3687. names[i] = module->exports[i].name;
  3688. }
  3689. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3690. for (i = 1; i < module->export_count; i++) {
  3691. if (!strcmp(names[i], names[i - 1])) {
  3692. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3693. goto cleanup;
  3694. }
  3695. }
  3696. result = true;
  3697. cleanup:
  3698. if (module->export_count > 32) {
  3699. wasm_runtime_free(names);
  3700. }
  3701. return result;
  3702. }
  3703. static bool
  3704. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3705. bool is_load_from_file_buf, char *error_buf,
  3706. uint32 error_buf_size)
  3707. {
  3708. const uint8 *p = buf, *p_end = buf_end;
  3709. uint32 export_count, i, index;
  3710. uint64 total_size;
  3711. uint32 str_len;
  3712. WASMExport *export;
  3713. read_leb_uint32(p, p_end, export_count);
  3714. if (export_count) {
  3715. module->export_count = export_count;
  3716. total_size = sizeof(WASMExport) * (uint64)export_count;
  3717. if (!(module->exports =
  3718. loader_malloc(total_size, error_buf, error_buf_size))) {
  3719. return false;
  3720. }
  3721. export = module->exports;
  3722. for (i = 0; i < export_count; i++, export ++) {
  3723. #if WASM_ENABLE_THREAD_MGR == 0
  3724. if (p == p_end) {
  3725. /* export section with inconsistent count:
  3726. n export declared, but less than n given */
  3727. set_error_buf(error_buf, error_buf_size,
  3728. "length out of bounds");
  3729. return false;
  3730. }
  3731. #endif
  3732. read_leb_uint32(p, p_end, str_len);
  3733. CHECK_BUF(p, p_end, str_len);
  3734. if (!(export->name = wasm_const_str_list_insert(
  3735. p, str_len, module, is_load_from_file_buf, error_buf,
  3736. error_buf_size))) {
  3737. return false;
  3738. }
  3739. p += str_len;
  3740. CHECK_BUF(p, p_end, 1);
  3741. export->kind = read_uint8(p);
  3742. read_leb_uint32(p, p_end, index);
  3743. export->index = index;
  3744. switch (export->kind) {
  3745. /* function index */
  3746. case EXPORT_KIND_FUNC:
  3747. if (index >= module->function_count
  3748. + module->import_function_count) {
  3749. set_error_buf(error_buf, error_buf_size,
  3750. "unknown function");
  3751. return false;
  3752. }
  3753. #if WASM_ENABLE_SIMD != 0
  3754. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  3755. || (WASM_ENABLE_FAST_INTERP != 0)
  3756. /* TODO: check func type, if it has v128 param or result,
  3757. report error */
  3758. #endif
  3759. #endif
  3760. break;
  3761. /* table index */
  3762. case EXPORT_KIND_TABLE:
  3763. if (index
  3764. >= module->table_count + module->import_table_count) {
  3765. set_error_buf(error_buf, error_buf_size,
  3766. "unknown table");
  3767. return false;
  3768. }
  3769. break;
  3770. /* memory index */
  3771. case EXPORT_KIND_MEMORY:
  3772. if (index
  3773. >= module->memory_count + module->import_memory_count) {
  3774. set_error_buf(error_buf, error_buf_size,
  3775. "unknown memory");
  3776. return false;
  3777. }
  3778. break;
  3779. #if WASM_ENABLE_TAGS != 0
  3780. /* export tag */
  3781. case EXPORT_KIND_TAG:
  3782. if (index >= module->tag_count + module->import_tag_count) {
  3783. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3784. return false;
  3785. }
  3786. break;
  3787. #endif
  3788. /* global index */
  3789. case EXPORT_KIND_GLOBAL:
  3790. if (index
  3791. >= module->global_count + module->import_global_count) {
  3792. set_error_buf(error_buf, error_buf_size,
  3793. "unknown global");
  3794. return false;
  3795. }
  3796. break;
  3797. default:
  3798. set_error_buf(error_buf, error_buf_size,
  3799. "invalid export kind");
  3800. return false;
  3801. }
  3802. }
  3803. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  3804. return false;
  3805. }
  3806. }
  3807. if (p != p_end) {
  3808. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3809. return false;
  3810. }
  3811. LOG_VERBOSE("Load export section success.\n");
  3812. return true;
  3813. fail:
  3814. return false;
  3815. }
  3816. static bool
  3817. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3818. uint32 error_buf_size)
  3819. {
  3820. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3821. if (table_index != 0) {
  3822. set_error_buf(
  3823. error_buf, error_buf_size,
  3824. "zero byte expected. The module uses reference types feature "
  3825. "which is disabled in the runtime.");
  3826. return false;
  3827. }
  3828. #endif
  3829. if (table_index >= module->import_table_count + module->table_count) {
  3830. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3831. table_index);
  3832. return false;
  3833. }
  3834. return true;
  3835. }
  3836. static bool
  3837. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3838. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3839. {
  3840. const uint8 *p = *p_buf, *p_end = buf_end;
  3841. uint32 table_index;
  3842. read_leb_uint32(p, p_end, table_index);
  3843. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3844. return false;
  3845. }
  3846. *p_table_index = table_index;
  3847. *p_buf = p;
  3848. return true;
  3849. fail:
  3850. return false;
  3851. }
  3852. /* Element segments must match element type of table */
  3853. static bool
  3854. check_table_elem_type(WASMModule *module, uint32 table_index,
  3855. uint32 type_from_elem_seg, char *error_buf,
  3856. uint32 error_buf_size)
  3857. {
  3858. uint32 table_declared_elem_type;
  3859. if (table_index < module->import_table_count)
  3860. table_declared_elem_type =
  3861. module->import_tables[table_index].u.table.table_type.elem_type;
  3862. else
  3863. table_declared_elem_type =
  3864. module->tables[table_index - module->import_table_count]
  3865. .table_type.elem_type;
  3866. if (table_declared_elem_type == type_from_elem_seg)
  3867. return true;
  3868. #if WASM_ENABLE_GC != 0
  3869. /*
  3870. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3871. */
  3872. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3873. return true;
  3874. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3875. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3876. return true;
  3877. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3878. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3879. return true;
  3880. #endif
  3881. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3882. return false;
  3883. }
  3884. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3885. static bool
  3886. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3887. uint32 *p_elem_type,
  3888. #if WASM_ENABLE_GC != 0
  3889. WASMRefType **p_elem_ref_type,
  3890. #endif
  3891. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3892. {
  3893. const uint8 *p = *p_buf, *p_end = buf_end;
  3894. uint8 elem_type;
  3895. #if WASM_ENABLE_GC != 0
  3896. WASMRefType elem_ref_type;
  3897. bool need_ref_type_map;
  3898. #endif
  3899. CHECK_BUF(p, p_end, 1);
  3900. elem_type = read_uint8(p);
  3901. if (elemkind_zero) {
  3902. if (elem_type != 0) {
  3903. set_error_buf(error_buf, error_buf_size,
  3904. "invalid reference type or unknown type");
  3905. return false;
  3906. }
  3907. else {
  3908. *p_elem_type = VALUE_TYPE_FUNCREF;
  3909. *p_buf = p;
  3910. return true;
  3911. }
  3912. }
  3913. #if WASM_ENABLE_GC == 0
  3914. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3915. set_error_buf(error_buf, error_buf_size,
  3916. "invalid reference type or unknown type");
  3917. return false;
  3918. }
  3919. *p_elem_type = elem_type;
  3920. #else
  3921. p--;
  3922. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3923. module->type_count, &need_ref_type_map,
  3924. &elem_ref_type, false, error_buf, error_buf_size)) {
  3925. return false;
  3926. }
  3927. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3928. set_error_buf(error_buf, error_buf_size,
  3929. "invalid reference type or unknown type");
  3930. return false;
  3931. }
  3932. *p_elem_type = elem_ref_type.ref_type;
  3933. if (need_ref_type_map) {
  3934. if (!(*p_elem_ref_type =
  3935. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3936. error_buf, error_buf_size))) {
  3937. return false;
  3938. }
  3939. }
  3940. #endif
  3941. *p_buf = p;
  3942. return true;
  3943. fail:
  3944. return false;
  3945. }
  3946. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3947. static bool
  3948. load_func_index_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 function_count, function_index = 0, i;
  3954. uint64 total_size;
  3955. read_leb_uint32(p, p_end, function_count);
  3956. table_segment->value_count = function_count;
  3957. total_size = sizeof(InitializerExpression) * (uint64)function_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 < function_count; i++) {
  3965. InitializerExpression *init_expr = &table_segment->init_values[i];
  3966. read_leb_uint32(p, p_end, function_index);
  3967. if (!check_function_index(module, function_index, error_buf,
  3968. error_buf_size)) {
  3969. return false;
  3970. }
  3971. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3972. init_expr->u.ref_index = function_index;
  3973. }
  3974. *p_buf = p;
  3975. return true;
  3976. fail:
  3977. return false;
  3978. }
  3979. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3980. static bool
  3981. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3982. WASMModule *module, WASMTableSeg *table_segment,
  3983. char *error_buf, uint32 error_buf_size)
  3984. {
  3985. const uint8 *p = *p_buf, *p_end = buf_end;
  3986. uint32 ref_count, i;
  3987. uint64 total_size;
  3988. read_leb_uint32(p, p_end, ref_count);
  3989. table_segment->value_count = ref_count;
  3990. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3991. if (total_size > 0
  3992. && !(table_segment->init_values =
  3993. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3994. error_buf_size))) {
  3995. return false;
  3996. }
  3997. for (i = 0; i < ref_count; i++) {
  3998. InitializerExpression *init_expr = &table_segment->init_values[i];
  3999. if (!load_init_expr(module, &p, p_end, init_expr,
  4000. table_segment->elem_type,
  4001. #if WASM_ENABLE_GC == 0
  4002. NULL,
  4003. #else
  4004. table_segment->elem_ref_type,
  4005. #endif
  4006. error_buf, error_buf_size))
  4007. return false;
  4008. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  4009. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  4010. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  4011. && init_expr->init_expr_type
  4012. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  4013. }
  4014. *p_buf = p;
  4015. return true;
  4016. fail:
  4017. return false;
  4018. }
  4019. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4020. static bool
  4021. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4022. WASMModule *module, char *error_buf,
  4023. uint32 error_buf_size)
  4024. {
  4025. const uint8 *p = buf, *p_end = buf_end;
  4026. uint8 table_elem_idx_type;
  4027. uint32 table_segment_count, i;
  4028. uint64 total_size;
  4029. WASMTableSeg *table_segment;
  4030. read_leb_uint32(p, p_end, table_segment_count);
  4031. if (table_segment_count) {
  4032. module->table_seg_count = table_segment_count;
  4033. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4034. if (!(module->table_segments =
  4035. loader_malloc(total_size, error_buf, error_buf_size))) {
  4036. return false;
  4037. }
  4038. table_segment = module->table_segments;
  4039. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4040. if (p >= p_end) {
  4041. set_error_buf(error_buf, error_buf_size,
  4042. "invalid value type or "
  4043. "invalid elements segment kind");
  4044. return false;
  4045. }
  4046. table_elem_idx_type = VALUE_TYPE_I32;
  4047. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4048. read_leb_uint32(p, p_end, table_segment->mode);
  4049. /* last three bits */
  4050. table_segment->mode = table_segment->mode & 0x07;
  4051. switch (table_segment->mode) {
  4052. /* elemkind/elemtype + active */
  4053. case 0:
  4054. case 4:
  4055. {
  4056. #if WASM_ENABLE_GC != 0
  4057. if (table_segment->mode == 0) {
  4058. /* vec(funcidx), set elem type to (ref func) */
  4059. WASMRefType elem_ref_type = { 0 };
  4060. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4061. wasm_set_refheaptype_common(
  4062. &elem_ref_type.ref_ht_common, false,
  4063. HEAP_TYPE_FUNC);
  4064. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4065. module->ref_type_set, &elem_ref_type,
  4066. error_buf, error_buf_size)))
  4067. return false;
  4068. }
  4069. else {
  4070. /* vec(expr), set elem type to funcref */
  4071. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4072. }
  4073. #else
  4074. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4075. #endif
  4076. table_segment->table_index = 0;
  4077. if (!check_table_index(module, table_segment->table_index,
  4078. error_buf, error_buf_size))
  4079. return false;
  4080. #if WASM_ENABLE_MEMORY64 != 0
  4081. table_elem_idx_type =
  4082. is_table_64bit(module, table_segment->table_index)
  4083. ? VALUE_TYPE_I64
  4084. : VALUE_TYPE_I32;
  4085. #endif
  4086. if (!load_init_expr(module, &p, p_end,
  4087. &table_segment->base_offset,
  4088. table_elem_idx_type, NULL, error_buf,
  4089. error_buf_size))
  4090. return false;
  4091. if (table_segment->mode == 0) {
  4092. /* vec(funcidx) */
  4093. if (!load_func_index_vec(&p, p_end, module,
  4094. table_segment, error_buf,
  4095. error_buf_size))
  4096. return false;
  4097. }
  4098. else {
  4099. /* vec(expr) */
  4100. if (!load_init_expr_vec(&p, p_end, module,
  4101. table_segment, error_buf,
  4102. error_buf_size))
  4103. return false;
  4104. }
  4105. if (!check_table_elem_type(module,
  4106. table_segment->table_index,
  4107. table_segment->elem_type,
  4108. error_buf, error_buf_size))
  4109. return false;
  4110. break;
  4111. }
  4112. /* elemkind + passive/declarative */
  4113. case 1:
  4114. case 3:
  4115. if (!load_elem_type(module, &p, p_end,
  4116. &table_segment->elem_type,
  4117. #if WASM_ENABLE_GC != 0
  4118. &table_segment->elem_ref_type,
  4119. #endif
  4120. true, error_buf, error_buf_size))
  4121. return false;
  4122. /* vec(funcidx) */
  4123. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4124. error_buf, error_buf_size))
  4125. return false;
  4126. break;
  4127. /* elemkind/elemtype + table_idx + active */
  4128. case 2:
  4129. case 6:
  4130. if (!load_table_index(&p, p_end, module,
  4131. &table_segment->table_index,
  4132. error_buf, error_buf_size))
  4133. return false;
  4134. #if WASM_ENABLE_MEMORY64 != 0
  4135. table_elem_idx_type =
  4136. is_table_64bit(module, table_segment->table_index)
  4137. ? VALUE_TYPE_I64
  4138. : VALUE_TYPE_I32;
  4139. #endif
  4140. if (!load_init_expr(module, &p, p_end,
  4141. &table_segment->base_offset,
  4142. table_elem_idx_type, NULL, error_buf,
  4143. error_buf_size))
  4144. return false;
  4145. if (!load_elem_type(module, &p, p_end,
  4146. &table_segment->elem_type,
  4147. #if WASM_ENABLE_GC != 0
  4148. &table_segment->elem_ref_type,
  4149. #endif
  4150. table_segment->mode == 2 ? true : false,
  4151. error_buf, error_buf_size))
  4152. return false;
  4153. if (table_segment->mode == 2) {
  4154. /* vec(funcidx) */
  4155. if (!load_func_index_vec(&p, p_end, module,
  4156. table_segment, error_buf,
  4157. error_buf_size))
  4158. return false;
  4159. }
  4160. else {
  4161. /* vec(expr) */
  4162. if (!load_init_expr_vec(&p, p_end, module,
  4163. table_segment, error_buf,
  4164. error_buf_size))
  4165. return false;
  4166. }
  4167. if (!check_table_elem_type(module,
  4168. table_segment->table_index,
  4169. table_segment->elem_type,
  4170. error_buf, error_buf_size))
  4171. return false;
  4172. break;
  4173. case 5:
  4174. case 7:
  4175. if (!load_elem_type(module, &p, p_end,
  4176. &table_segment->elem_type,
  4177. #if WASM_ENABLE_GC != 0
  4178. &table_segment->elem_ref_type,
  4179. #endif
  4180. false, error_buf, error_buf_size))
  4181. return false;
  4182. /* vec(expr) */
  4183. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4184. error_buf, error_buf_size))
  4185. return false;
  4186. break;
  4187. default:
  4188. set_error_buf(error_buf, error_buf_size,
  4189. "unknown element segment kind");
  4190. return false;
  4191. }
  4192. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4193. /*
  4194. * like: 00 41 05 0b 04 00 01 00 01
  4195. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4196. */
  4197. if (!load_table_index(&p, p_end, module,
  4198. &table_segment->table_index, error_buf,
  4199. error_buf_size))
  4200. return false;
  4201. #if WASM_ENABLE_MEMORY64 != 0
  4202. table_elem_idx_type =
  4203. is_table_64bit(module, table_segment->table_index)
  4204. ? VALUE_TYPE_I64
  4205. : VALUE_TYPE_I32;
  4206. #endif
  4207. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4208. table_elem_idx_type, NULL, error_buf,
  4209. error_buf_size))
  4210. return false;
  4211. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4212. error_buf, error_buf_size))
  4213. return false;
  4214. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4215. if (!check_table_elem_type(module, table_segment->table_index,
  4216. table_segment->elem_type, error_buf,
  4217. error_buf_size))
  4218. return false;
  4219. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4220. #if WASM_ENABLE_MEMORY64 != 0
  4221. if (table_elem_idx_type == VALUE_TYPE_I64
  4222. && table_segment->base_offset.u.u64 > UINT32_MAX) {
  4223. set_error_buf(error_buf, error_buf_size,
  4224. "In table64, table base offset can't be "
  4225. "larger than UINT32_MAX");
  4226. return false;
  4227. }
  4228. #endif
  4229. #if WASM_ENABLE_WAMR_COMPILER != 0
  4230. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4231. module->is_ref_types_used = true;
  4232. #endif
  4233. }
  4234. }
  4235. if (p != p_end) {
  4236. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4237. return false;
  4238. }
  4239. LOG_VERBOSE("Load table segment section success.\n");
  4240. return true;
  4241. fail:
  4242. return false;
  4243. }
  4244. #if WASM_ENABLE_BULK_MEMORY != 0
  4245. static bool
  4246. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4247. int data_seg_len, char *error_buf,
  4248. uint32 error_buf_size)
  4249. {
  4250. if (has_datacount_section && datacount_len != data_seg_len) {
  4251. set_error_buf(error_buf, error_buf_size,
  4252. "data count and data section have inconsistent lengths");
  4253. return false;
  4254. }
  4255. return true;
  4256. }
  4257. #endif
  4258. static bool
  4259. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4260. WASMModule *module,
  4261. #if WASM_ENABLE_BULK_MEMORY != 0
  4262. bool has_datacount_section,
  4263. #endif
  4264. bool clone_data_seg, char *error_buf,
  4265. uint32 error_buf_size)
  4266. {
  4267. const uint8 *p = buf, *p_end = buf_end;
  4268. uint32 data_seg_count, i, mem_index, data_seg_len;
  4269. uint64 total_size;
  4270. WASMDataSeg *dataseg;
  4271. InitializerExpression init_expr;
  4272. #if WASM_ENABLE_BULK_MEMORY != 0
  4273. bool is_passive = false;
  4274. uint32 mem_flag;
  4275. #endif
  4276. uint8 mem_offset_type = VALUE_TYPE_I32;
  4277. read_leb_uint32(p, p_end, data_seg_count);
  4278. #if WASM_ENABLE_BULK_MEMORY != 0
  4279. if (!check_data_count_consistency(has_datacount_section,
  4280. module->data_seg_count1, data_seg_count,
  4281. error_buf, error_buf_size)) {
  4282. return false;
  4283. }
  4284. #endif
  4285. if (data_seg_count) {
  4286. module->data_seg_count = data_seg_count;
  4287. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4288. if (!(module->data_segments =
  4289. loader_malloc(total_size, error_buf, error_buf_size))) {
  4290. return false;
  4291. }
  4292. for (i = 0; i < data_seg_count; i++) {
  4293. read_leb_uint32(p, p_end, mem_index);
  4294. #if WASM_ENABLE_BULK_MEMORY != 0
  4295. is_passive = false;
  4296. mem_flag = mem_index & 0x03;
  4297. switch (mem_flag) {
  4298. case 0x01:
  4299. is_passive = true;
  4300. #if WASM_ENABLE_WAMR_COMPILER != 0
  4301. module->is_bulk_memory_used = true;
  4302. #endif
  4303. break;
  4304. case 0x00:
  4305. /* no memory index, treat index as 0 */
  4306. mem_index = 0;
  4307. goto check_mem_index;
  4308. case 0x02:
  4309. /* read following memory index */
  4310. read_leb_uint32(p, p_end, mem_index);
  4311. #if WASM_ENABLE_WAMR_COMPILER != 0
  4312. module->is_bulk_memory_used = true;
  4313. #endif
  4314. check_mem_index:
  4315. if (mem_index
  4316. >= module->import_memory_count + module->memory_count) {
  4317. set_error_buf_v(error_buf, error_buf_size,
  4318. "unknown memory %d", mem_index);
  4319. return false;
  4320. }
  4321. break;
  4322. case 0x03:
  4323. default:
  4324. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4325. return false;
  4326. break;
  4327. }
  4328. #else
  4329. if (mem_index
  4330. >= module->import_memory_count + module->memory_count) {
  4331. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4332. mem_index);
  4333. return false;
  4334. }
  4335. #endif /* WASM_ENABLE_BULK_MEMORY */
  4336. #if WASM_ENABLE_BULK_MEMORY != 0
  4337. if (!is_passive)
  4338. #endif
  4339. {
  4340. #if WASM_ENABLE_MEMORY64 != 0
  4341. /* This memory_flag is from memory instead of data segment */
  4342. uint8 memory_flag;
  4343. if (module->import_memory_count > 0) {
  4344. memory_flag = module->import_memories[mem_index]
  4345. .u.memory.mem_type.flags;
  4346. }
  4347. else {
  4348. memory_flag =
  4349. module
  4350. ->memories[mem_index - module->import_memory_count]
  4351. .flags;
  4352. }
  4353. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4354. : VALUE_TYPE_I32;
  4355. #else
  4356. mem_offset_type = VALUE_TYPE_I32;
  4357. #endif
  4358. }
  4359. #if WASM_ENABLE_BULK_MEMORY != 0
  4360. if (!is_passive)
  4361. #endif
  4362. if (!load_init_expr(module, &p, p_end, &init_expr,
  4363. mem_offset_type, NULL, error_buf,
  4364. error_buf_size))
  4365. return false;
  4366. read_leb_uint32(p, p_end, data_seg_len);
  4367. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4368. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4369. return false;
  4370. }
  4371. #if WASM_ENABLE_BULK_MEMORY != 0
  4372. dataseg->is_passive = is_passive;
  4373. if (!is_passive)
  4374. #endif
  4375. {
  4376. bh_memcpy_s(&dataseg->base_offset,
  4377. sizeof(InitializerExpression), &init_expr,
  4378. sizeof(InitializerExpression));
  4379. dataseg->memory_index = mem_index;
  4380. }
  4381. dataseg->data_length = data_seg_len;
  4382. CHECK_BUF(p, p_end, data_seg_len);
  4383. if (clone_data_seg) {
  4384. if (!(dataseg->data = loader_malloc(
  4385. dataseg->data_length, error_buf, error_buf_size))) {
  4386. return false;
  4387. }
  4388. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4389. data_seg_len);
  4390. }
  4391. else {
  4392. dataseg->data = (uint8 *)p;
  4393. }
  4394. dataseg->is_data_cloned = clone_data_seg;
  4395. p += data_seg_len;
  4396. }
  4397. }
  4398. if (p != p_end) {
  4399. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4400. return false;
  4401. }
  4402. LOG_VERBOSE("Load data segment section success.\n");
  4403. return true;
  4404. fail:
  4405. return false;
  4406. }
  4407. #if WASM_ENABLE_BULK_MEMORY != 0
  4408. static bool
  4409. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4410. WASMModule *module, char *error_buf,
  4411. uint32 error_buf_size)
  4412. {
  4413. const uint8 *p = buf, *p_end = buf_end;
  4414. uint32 data_seg_count1 = 0;
  4415. read_leb_uint32(p, p_end, data_seg_count1);
  4416. module->data_seg_count1 = data_seg_count1;
  4417. if (p != p_end) {
  4418. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4419. return false;
  4420. }
  4421. #if WASM_ENABLE_WAMR_COMPILER != 0
  4422. module->is_bulk_memory_used = true;
  4423. #endif
  4424. LOG_VERBOSE("Load datacount section success.\n");
  4425. return true;
  4426. fail:
  4427. return false;
  4428. }
  4429. #endif
  4430. #if WASM_ENABLE_TAGS != 0
  4431. static bool
  4432. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4433. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4434. uint32 error_buf_size)
  4435. {
  4436. (void)buf_code;
  4437. (void)buf_code_end;
  4438. const uint8 *p = buf, *p_end = buf_end;
  4439. size_t total_size = 0;
  4440. /* number of tags defined in the section */
  4441. uint32 section_tag_count = 0;
  4442. uint8 tag_attribute;
  4443. uint32 tag_type;
  4444. WASMTag *tag = NULL;
  4445. /* get tag count */
  4446. read_leb_uint32(p, p_end, section_tag_count);
  4447. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4448. error_buf, error_buf_size))
  4449. return false;
  4450. module->tag_count = section_tag_count;
  4451. if (section_tag_count) {
  4452. total_size = sizeof(WASMTag *) * module->tag_count;
  4453. if (!(module->tags =
  4454. loader_malloc(total_size, error_buf, error_buf_size))) {
  4455. return false;
  4456. }
  4457. /* load each tag, imported tags precede the tags */
  4458. uint32 tag_index;
  4459. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4460. /* get the one byte attribute */
  4461. CHECK_BUF(p, p_end, 1);
  4462. tag_attribute = read_uint8(p);
  4463. /* get type */
  4464. read_leb_uint32(p, p_end, tag_type);
  4465. /* compare against module->types */
  4466. if (!check_function_type(module, tag_type, error_buf,
  4467. error_buf_size)) {
  4468. return false;
  4469. }
  4470. /* get return type (must be 0) */
  4471. /* check, that the type of the referred tag returns void */
  4472. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4473. if (func_type->result_count != 0) {
  4474. set_error_buf(error_buf, error_buf_size,
  4475. "non-empty tag result type");
  4476. goto fail;
  4477. }
  4478. if (!(tag = module->tags[tag_index] = loader_malloc(
  4479. sizeof(WASMTag), error_buf, error_buf_size))) {
  4480. return false;
  4481. }
  4482. /* store to module tag declarations */
  4483. tag->attribute = tag_attribute;
  4484. tag->type = tag_type;
  4485. tag->tag_type = func_type;
  4486. }
  4487. }
  4488. if (p != p_end) {
  4489. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4490. return false;
  4491. }
  4492. LOG_VERBOSE("Load tag section success.\n");
  4493. return true;
  4494. fail:
  4495. return false;
  4496. }
  4497. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4498. static bool
  4499. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4500. const uint8 *buf_func_end, WASMModule *module,
  4501. char *error_buf, uint32 error_buf_size)
  4502. {
  4503. const uint8 *p = buf, *p_end = buf_end;
  4504. const uint8 *p_func = buf_func;
  4505. uint32 func_count = 0, code_count;
  4506. /* code has been loaded in function section, so pass it here, just check
  4507. * whether function and code section have inconsistent lengths */
  4508. read_leb_uint32(p, p_end, code_count);
  4509. if (buf_func)
  4510. read_leb_uint32(p_func, buf_func_end, func_count);
  4511. if (func_count != code_count) {
  4512. set_error_buf(error_buf, error_buf_size,
  4513. "function and code section have inconsistent lengths");
  4514. return false;
  4515. }
  4516. LOG_VERBOSE("Load code segment section success.\n");
  4517. (void)module;
  4518. return true;
  4519. fail:
  4520. return false;
  4521. }
  4522. static bool
  4523. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4524. char *error_buf, uint32 error_buf_size)
  4525. {
  4526. const uint8 *p = buf, *p_end = buf_end;
  4527. WASMFuncType *type;
  4528. uint32 start_function;
  4529. read_leb_uint32(p, p_end, start_function);
  4530. if (start_function
  4531. >= module->function_count + module->import_function_count) {
  4532. set_error_buf(error_buf, error_buf_size, "unknown function");
  4533. return false;
  4534. }
  4535. if (start_function < module->import_function_count)
  4536. type = module->import_functions[start_function].u.function.func_type;
  4537. else
  4538. type = module->functions[start_function - module->import_function_count]
  4539. ->func_type;
  4540. if (type->param_count != 0 || type->result_count != 0) {
  4541. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4542. return false;
  4543. }
  4544. module->start_function = start_function;
  4545. if (p != p_end) {
  4546. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4547. return false;
  4548. }
  4549. LOG_VERBOSE("Load start section success.\n");
  4550. return true;
  4551. fail:
  4552. return false;
  4553. }
  4554. #if WASM_ENABLE_STRINGREF != 0
  4555. static bool
  4556. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4557. WASMModule *module, bool is_load_from_file_buf,
  4558. char *error_buf, uint32 error_buf_size)
  4559. {
  4560. const uint8 *p = buf, *p_end = buf_end;
  4561. int32 deferred_count, immediate_count, string_length, i;
  4562. uint64 total_size;
  4563. read_leb_uint32(p, p_end, deferred_count);
  4564. read_leb_uint32(p, p_end, immediate_count);
  4565. /* proposal set deferred_count for future extension */
  4566. if (deferred_count != 0) {
  4567. goto fail;
  4568. }
  4569. if (immediate_count > 0) {
  4570. total_size = sizeof(char *) * (uint64)immediate_count;
  4571. if (!(module->string_literal_ptrs =
  4572. loader_malloc(total_size, error_buf, error_buf_size))) {
  4573. goto fail;
  4574. }
  4575. module->string_literal_count = immediate_count;
  4576. total_size = sizeof(uint32) * (uint64)immediate_count;
  4577. if (!(module->string_literal_lengths =
  4578. loader_malloc(total_size, error_buf, error_buf_size))) {
  4579. goto fail;
  4580. }
  4581. for (i = 0; i < immediate_count; i++) {
  4582. read_leb_uint32(p, p_end, string_length);
  4583. CHECK_BUF(p, p_end, string_length);
  4584. module->string_literal_ptrs[i] = p;
  4585. module->string_literal_lengths[i] = string_length;
  4586. p += string_length;
  4587. }
  4588. }
  4589. if (p != p_end) {
  4590. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4591. goto fail;
  4592. }
  4593. LOG_VERBOSE("Load stringref section success.\n");
  4594. return true;
  4595. fail:
  4596. return false;
  4597. }
  4598. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4599. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4600. static bool
  4601. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4602. bool is_load_from_file_buf, char *error_buf,
  4603. uint32 error_buf_size)
  4604. {
  4605. const uint8 *p = buf, *p_end = buf_end;
  4606. uint32 name_type, subsection_size;
  4607. uint32 previous_name_type = 0;
  4608. uint32 num_func_name;
  4609. uint32 func_index;
  4610. uint32 previous_func_index = ~0U;
  4611. uint32 func_name_len;
  4612. uint32 name_index;
  4613. int i = 0;
  4614. if (p >= p_end) {
  4615. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4616. return false;
  4617. }
  4618. while (p < p_end) {
  4619. read_leb_uint32(p, p_end, name_type);
  4620. if (i != 0) {
  4621. if (name_type == previous_name_type) {
  4622. set_error_buf(error_buf, error_buf_size,
  4623. "duplicate sub-section");
  4624. return false;
  4625. }
  4626. if (name_type < previous_name_type) {
  4627. set_error_buf(error_buf, error_buf_size,
  4628. "out-of-order sub-section");
  4629. return false;
  4630. }
  4631. }
  4632. previous_name_type = name_type;
  4633. read_leb_uint32(p, p_end, subsection_size);
  4634. CHECK_BUF(p, p_end, subsection_size);
  4635. switch (name_type) {
  4636. case SUB_SECTION_TYPE_FUNC:
  4637. if (subsection_size) {
  4638. read_leb_uint32(p, p_end, num_func_name);
  4639. for (name_index = 0; name_index < num_func_name;
  4640. name_index++) {
  4641. read_leb_uint32(p, p_end, func_index);
  4642. if (func_index == previous_func_index) {
  4643. set_error_buf(error_buf, error_buf_size,
  4644. "duplicate function name");
  4645. return false;
  4646. }
  4647. if (func_index < previous_func_index
  4648. && previous_func_index != ~0U) {
  4649. set_error_buf(error_buf, error_buf_size,
  4650. "out-of-order function index ");
  4651. return false;
  4652. }
  4653. previous_func_index = func_index;
  4654. read_leb_uint32(p, p_end, func_name_len);
  4655. CHECK_BUF(p, p_end, func_name_len);
  4656. /* Skip the import functions */
  4657. if (func_index >= module->import_function_count) {
  4658. func_index -= module->import_function_count;
  4659. if (func_index >= module->function_count) {
  4660. set_error_buf(error_buf, error_buf_size,
  4661. "out-of-range function index");
  4662. return false;
  4663. }
  4664. if (!(module->functions[func_index]->field_name =
  4665. wasm_const_str_list_insert(
  4666. p, func_name_len, module,
  4667. #if WASM_ENABLE_WAMR_COMPILER != 0
  4668. false,
  4669. #else
  4670. is_load_from_file_buf,
  4671. #endif
  4672. error_buf, error_buf_size))) {
  4673. return false;
  4674. }
  4675. }
  4676. p += func_name_len;
  4677. }
  4678. }
  4679. break;
  4680. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4681. */
  4682. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4683. default:
  4684. p = p + subsection_size;
  4685. break;
  4686. }
  4687. i++;
  4688. }
  4689. return true;
  4690. fail:
  4691. return false;
  4692. }
  4693. #endif
  4694. static bool
  4695. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4696. bool is_load_from_file_buf, char *error_buf,
  4697. uint32 error_buf_size)
  4698. {
  4699. const uint8 *p = buf, *p_end = buf_end;
  4700. char section_name[32];
  4701. uint32 name_len, buffer_len;
  4702. if (p >= p_end) {
  4703. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4704. return false;
  4705. }
  4706. read_leb_uint32(p, p_end, name_len);
  4707. if (p + name_len > p_end) {
  4708. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4709. return false;
  4710. }
  4711. if (!wasm_check_utf8_str(p, name_len)) {
  4712. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4713. return false;
  4714. }
  4715. buffer_len = sizeof(section_name);
  4716. memset(section_name, 0, buffer_len);
  4717. if (name_len < buffer_len) {
  4718. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4719. }
  4720. else {
  4721. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4722. memset(section_name + buffer_len - 4, '.', 3);
  4723. }
  4724. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4725. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4726. module->name_section_buf = buf;
  4727. module->name_section_buf_end = buf_end;
  4728. p += name_len;
  4729. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4730. error_buf, error_buf_size)) {
  4731. return false;
  4732. }
  4733. LOG_VERBOSE("Load custom name section success.");
  4734. }
  4735. #endif
  4736. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4737. {
  4738. WASMCustomSection *section =
  4739. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4740. if (!section) {
  4741. return false;
  4742. }
  4743. section->name_addr = (char *)p;
  4744. section->name_len = name_len;
  4745. section->content_addr = (uint8 *)(p + name_len);
  4746. section->content_len = (uint32)(p_end - p - name_len);
  4747. section->next = module->custom_section_list;
  4748. module->custom_section_list = section;
  4749. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4750. return true;
  4751. }
  4752. #endif
  4753. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4754. (void)is_load_from_file_buf;
  4755. (void)module;
  4756. return true;
  4757. fail:
  4758. return false;
  4759. }
  4760. static void
  4761. calculate_global_data_offset(WASMModule *module)
  4762. {
  4763. uint32 i, data_offset;
  4764. data_offset = 0;
  4765. for (i = 0; i < module->import_global_count; i++) {
  4766. WASMGlobalImport *import_global =
  4767. &((module->import_globals + i)->u.global);
  4768. #if WASM_ENABLE_FAST_JIT != 0
  4769. import_global->data_offset = data_offset;
  4770. #endif
  4771. data_offset += wasm_value_type_size(import_global->type.val_type);
  4772. }
  4773. for (i = 0; i < module->global_count; i++) {
  4774. WASMGlobal *global = module->globals + i;
  4775. #if WASM_ENABLE_FAST_JIT != 0
  4776. global->data_offset = data_offset;
  4777. #endif
  4778. data_offset += wasm_value_type_size(global->type.val_type);
  4779. }
  4780. module->global_data_size = data_offset;
  4781. }
  4782. #if WASM_ENABLE_FAST_JIT != 0
  4783. static bool
  4784. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4785. uint32 error_buf_size)
  4786. {
  4787. #if WASM_ENABLE_LAZY_JIT != 0
  4788. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4789. #endif
  4790. uint32 i;
  4791. if (!module->function_count)
  4792. return true;
  4793. if (!(module->fast_jit_func_ptrs =
  4794. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4795. error_buf_size))) {
  4796. return false;
  4797. }
  4798. #if WASM_ENABLE_LAZY_JIT != 0
  4799. for (i = 0; i < module->function_count; i++) {
  4800. module->fast_jit_func_ptrs[i] =
  4801. jit_globals->compile_fast_jit_and_then_call;
  4802. }
  4803. #endif
  4804. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4805. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4806. set_error_buf(error_buf, error_buf_size,
  4807. "init fast jit thread lock failed");
  4808. return false;
  4809. }
  4810. module->fast_jit_thread_locks_inited[i] = true;
  4811. }
  4812. return true;
  4813. }
  4814. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4815. #if WASM_ENABLE_JIT != 0
  4816. static bool
  4817. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4818. uint32 error_buf_size)
  4819. {
  4820. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4821. AOTCompOption option = { 0 };
  4822. char *aot_last_error;
  4823. uint64 size;
  4824. #if WASM_ENABLE_GC != 0
  4825. bool gc_enabled = true;
  4826. #else
  4827. bool gc_enabled = false;
  4828. #endif
  4829. if (module->function_count == 0)
  4830. return true;
  4831. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4832. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4833. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4834. return false;
  4835. }
  4836. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4837. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4838. os_mutex_destroy(&module->tierup_wait_lock);
  4839. return false;
  4840. }
  4841. module->tierup_wait_lock_inited = true;
  4842. #endif
  4843. size = sizeof(void *) * (uint64)module->function_count
  4844. + sizeof(bool) * (uint64)module->function_count;
  4845. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4846. return false;
  4847. }
  4848. module->func_ptrs_compiled =
  4849. (bool *)((uint8 *)module->func_ptrs
  4850. + sizeof(void *) * module->function_count);
  4851. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4852. if (!module->comp_data) {
  4853. aot_last_error = aot_get_last_error();
  4854. bh_assert(aot_last_error != NULL);
  4855. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4856. return false;
  4857. }
  4858. option.is_jit_mode = true;
  4859. option.opt_level = llvm_jit_options->opt_level;
  4860. option.size_level = llvm_jit_options->size_level;
  4861. option.segue_flags = llvm_jit_options->segue_flags;
  4862. option.quick_invoke_c_api_import =
  4863. llvm_jit_options->quick_invoke_c_api_import;
  4864. #if WASM_ENABLE_BULK_MEMORY != 0
  4865. option.enable_bulk_memory = true;
  4866. #endif
  4867. #if WASM_ENABLE_THREAD_MGR != 0
  4868. option.enable_thread_mgr = true;
  4869. #endif
  4870. #if WASM_ENABLE_TAIL_CALL != 0
  4871. option.enable_tail_call = true;
  4872. #endif
  4873. #if WASM_ENABLE_SIMD != 0
  4874. option.enable_simd = true;
  4875. #endif
  4876. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4877. option.enable_ref_types = true;
  4878. #elif WASM_ENABLE_GC != 0
  4879. option.enable_gc = true;
  4880. #endif
  4881. option.enable_aux_stack_check = true;
  4882. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4883. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4884. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4885. aot_call_stack_features_init_default(&option.call_stack_features);
  4886. #endif
  4887. #if WASM_ENABLE_PERF_PROFILING != 0
  4888. option.enable_perf_profiling = true;
  4889. #endif
  4890. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4891. option.enable_memory_profiling = true;
  4892. option.enable_stack_estimation = true;
  4893. #endif
  4894. #if WASM_ENABLE_SHARED_HEAP != 0
  4895. option.enable_shared_heap = true;
  4896. #endif
  4897. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4898. if (!module->comp_ctx) {
  4899. aot_last_error = aot_get_last_error();
  4900. bh_assert(aot_last_error != NULL);
  4901. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4902. return false;
  4903. }
  4904. return true;
  4905. }
  4906. static bool
  4907. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4908. uint32 error_buf_size)
  4909. {
  4910. char *aot_last_error;
  4911. uint32 i;
  4912. if (module->function_count == 0)
  4913. return true;
  4914. if (!aot_compile_wasm(module->comp_ctx)) {
  4915. aot_last_error = aot_get_last_error();
  4916. bh_assert(aot_last_error != NULL);
  4917. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4918. return false;
  4919. }
  4920. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4921. if (module->orcjit_stop_compiling)
  4922. return false;
  4923. #endif
  4924. bh_print_time("Begin to lookup llvm jit functions");
  4925. for (i = 0; i < module->function_count; i++) {
  4926. LLVMOrcJITTargetAddress func_addr = 0;
  4927. LLVMErrorRef error;
  4928. char func_name[48];
  4929. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4930. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4931. func_name);
  4932. if (error != LLVMErrorSuccess) {
  4933. char *err_msg = LLVMGetErrorMessage(error);
  4934. set_error_buf_v(error_buf, error_buf_size,
  4935. "failed to compile llvm jit function: %s", err_msg);
  4936. LLVMDisposeErrorMessage(err_msg);
  4937. return false;
  4938. }
  4939. /**
  4940. * No need to lock the func_ptr[func_idx] here as it is basic
  4941. * data type, the load/store for it can be finished by one cpu
  4942. * instruction, and there can be only one cpu instruction
  4943. * loading/storing at the same time.
  4944. */
  4945. module->func_ptrs[i] = (void *)func_addr;
  4946. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4947. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4948. if (module->orcjit_stop_compiling)
  4949. return false;
  4950. #endif
  4951. }
  4952. bh_print_time("End lookup llvm jit functions");
  4953. return true;
  4954. }
  4955. #endif /* end of WASM_ENABLE_JIT != 0 */
  4956. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4957. && WASM_ENABLE_LAZY_JIT != 0
  4958. static void *
  4959. init_llvm_jit_functions_stage2_callback(void *arg)
  4960. {
  4961. WASMModule *module = (WASMModule *)arg;
  4962. char error_buf[128];
  4963. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4964. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4965. module->orcjit_stop_compiling = true;
  4966. return NULL;
  4967. }
  4968. os_mutex_lock(&module->tierup_wait_lock);
  4969. module->llvm_jit_inited = true;
  4970. os_cond_broadcast(&module->tierup_wait_cond);
  4971. os_mutex_unlock(&module->tierup_wait_lock);
  4972. return NULL;
  4973. }
  4974. #endif
  4975. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4976. /* The callback function to compile jit functions */
  4977. static void *
  4978. orcjit_thread_callback(void *arg)
  4979. {
  4980. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4981. #if WASM_ENABLE_JIT != 0
  4982. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4983. #endif
  4984. WASMModule *module = thread_arg->module;
  4985. uint32 group_idx = thread_arg->group_idx;
  4986. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4987. uint32 func_count = module->function_count;
  4988. uint32 i;
  4989. #if WASM_ENABLE_FAST_JIT != 0
  4990. /* Compile fast jit functions of this group */
  4991. for (i = group_idx; i < func_count; i += group_stride) {
  4992. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4993. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4994. break;
  4995. }
  4996. if (module->orcjit_stop_compiling) {
  4997. return NULL;
  4998. }
  4999. }
  5000. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5001. os_mutex_lock(&module->tierup_wait_lock);
  5002. module->fast_jit_ready_groups++;
  5003. os_mutex_unlock(&module->tierup_wait_lock);
  5004. #endif
  5005. #endif
  5006. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5007. && WASM_ENABLE_LAZY_JIT != 0
  5008. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  5009. for (i = group_idx; i < func_count;
  5010. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5011. uint32 j;
  5012. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5013. if (i + j * group_stride < func_count) {
  5014. if (!jit_compiler_set_call_to_fast_jit(
  5015. module,
  5016. i + j * group_stride + module->import_function_count)) {
  5017. LOG_ERROR(
  5018. "failed to compile call_to_fast_jit for func %u\n",
  5019. i + j * group_stride + module->import_function_count);
  5020. module->orcjit_stop_compiling = true;
  5021. return NULL;
  5022. }
  5023. }
  5024. if (module->orcjit_stop_compiling) {
  5025. return NULL;
  5026. }
  5027. }
  5028. }
  5029. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  5030. fast jit functions are compiled */
  5031. os_mutex_lock(&module->tierup_wait_lock);
  5032. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5033. && module->fast_jit_ready_groups >= group_stride)) {
  5034. os_cond_reltimedwait(&module->tierup_wait_cond,
  5035. &module->tierup_wait_lock, 10000);
  5036. if (module->orcjit_stop_compiling) {
  5037. /* init_llvm_jit_functions_stage2 failed */
  5038. os_mutex_unlock(&module->tierup_wait_lock);
  5039. return NULL;
  5040. }
  5041. }
  5042. os_mutex_unlock(&module->tierup_wait_lock);
  5043. #endif
  5044. #if WASM_ENABLE_JIT != 0
  5045. /* Compile llvm jit functions of this group */
  5046. for (i = group_idx; i < func_count;
  5047. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5048. LLVMOrcJITTargetAddress func_addr = 0;
  5049. LLVMErrorRef error;
  5050. char func_name[48];
  5051. typedef void (*F)(void);
  5052. union {
  5053. F f;
  5054. void *v;
  5055. } u;
  5056. uint32 j;
  5057. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5058. "_wrapper");
  5059. LOG_DEBUG("compile llvm jit func %s", func_name);
  5060. error =
  5061. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5062. if (error != LLVMErrorSuccess) {
  5063. char *err_msg = LLVMGetErrorMessage(error);
  5064. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5065. LLVMDisposeErrorMessage(err_msg);
  5066. break;
  5067. }
  5068. /* Call the jit wrapper function to trigger its compilation, so as
  5069. to compile the actual jit functions, since we add the latter to
  5070. function list in the PartitionFunction callback */
  5071. u.v = (void *)func_addr;
  5072. u.f();
  5073. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5074. if (i + j * group_stride < func_count) {
  5075. module->func_ptrs_compiled[i + j * group_stride] = true;
  5076. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5077. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5078. i + j * group_stride);
  5079. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5080. func_name);
  5081. if (error != LLVMErrorSuccess) {
  5082. char *err_msg = LLVMGetErrorMessage(error);
  5083. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5084. err_msg);
  5085. LLVMDisposeErrorMessage(err_msg);
  5086. /* Ignore current llvm jit func, as its func ptr is
  5087. previous set to call_to_fast_jit, which also works */
  5088. continue;
  5089. }
  5090. jit_compiler_set_llvm_jit_func_ptr(
  5091. module,
  5092. i + j * group_stride + module->import_function_count,
  5093. (void *)func_addr);
  5094. /* Try to switch to call this llvm jit function instead of
  5095. fast jit function from fast jit jitted code */
  5096. jit_compiler_set_call_to_llvm_jit(
  5097. module,
  5098. i + j * group_stride + module->import_function_count);
  5099. #endif
  5100. }
  5101. }
  5102. if (module->orcjit_stop_compiling) {
  5103. break;
  5104. }
  5105. }
  5106. #endif
  5107. return NULL;
  5108. }
  5109. static void
  5110. orcjit_stop_compile_threads(WASMModule *module)
  5111. {
  5112. #if WASM_ENABLE_LAZY_JIT != 0
  5113. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5114. / sizeof(OrcJitThreadArg));
  5115. module->orcjit_stop_compiling = true;
  5116. for (i = 0; i < thread_num; i++) {
  5117. if (module->orcjit_threads[i])
  5118. os_thread_join(module->orcjit_threads[i], NULL);
  5119. }
  5120. #endif
  5121. }
  5122. static bool
  5123. compile_jit_functions(WASMModule *module, char *error_buf,
  5124. uint32 error_buf_size)
  5125. {
  5126. uint32 thread_num =
  5127. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5128. uint32 i, j;
  5129. bh_print_time("Begin to compile jit functions");
  5130. /* Create threads to compile the jit functions */
  5131. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5132. #if WASM_ENABLE_JIT != 0
  5133. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5134. #endif
  5135. module->orcjit_thread_args[i].module = module;
  5136. module->orcjit_thread_args[i].group_idx = i;
  5137. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5138. (void *)&module->orcjit_thread_args[i],
  5139. APP_THREAD_STACK_SIZE_DEFAULT)
  5140. != 0) {
  5141. set_error_buf(error_buf, error_buf_size,
  5142. "create orcjit compile thread failed");
  5143. /* Terminate the threads created */
  5144. module->orcjit_stop_compiling = true;
  5145. for (j = 0; j < i; j++) {
  5146. os_thread_join(module->orcjit_threads[j], NULL);
  5147. }
  5148. return false;
  5149. }
  5150. }
  5151. #if WASM_ENABLE_LAZY_JIT == 0
  5152. /* Wait until all jit functions are compiled for eager mode */
  5153. for (i = 0; i < thread_num; i++) {
  5154. if (module->orcjit_threads[i])
  5155. os_thread_join(module->orcjit_threads[i], NULL);
  5156. }
  5157. #if WASM_ENABLE_FAST_JIT != 0
  5158. /* Ensure all the fast-jit functions are compiled */
  5159. for (i = 0; i < module->function_count; i++) {
  5160. if (!jit_compiler_is_compiled(module,
  5161. i + module->import_function_count)) {
  5162. set_error_buf(error_buf, error_buf_size,
  5163. "failed to compile fast jit function");
  5164. return false;
  5165. }
  5166. }
  5167. #endif
  5168. #if WASM_ENABLE_JIT != 0
  5169. /* Ensure all the llvm-jit functions are compiled */
  5170. for (i = 0; i < module->function_count; i++) {
  5171. if (!module->func_ptrs_compiled[i]) {
  5172. set_error_buf(error_buf, error_buf_size,
  5173. "failed to compile llvm jit function");
  5174. return false;
  5175. }
  5176. }
  5177. #endif
  5178. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5179. bh_print_time("End compile jit functions");
  5180. return true;
  5181. }
  5182. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5183. static bool
  5184. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5185. uint32 cur_func_idx, char *error_buf,
  5186. uint32 error_buf_size);
  5187. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5188. void **
  5189. wasm_interp_get_handle_table(void);
  5190. static void **handle_table;
  5191. #endif
  5192. static bool
  5193. load_from_sections(WASMModule *module, WASMSection *sections,
  5194. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5195. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5196. {
  5197. WASMExport *export;
  5198. WASMSection *section = sections;
  5199. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5200. *buf_func = NULL, *buf_func_end = NULL;
  5201. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5202. WASMGlobal *aux_stack_top_global = NULL, *global;
  5203. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5204. aux_stack_top = (uint64)-1LL;
  5205. uint32 global_index, func_index, i;
  5206. uint32 aux_data_end_global_index = (uint32)-1;
  5207. uint32 aux_heap_base_global_index = (uint32)-1;
  5208. WASMFuncType *func_type;
  5209. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5210. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5211. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5212. #if WASM_ENABLE_BULK_MEMORY != 0
  5213. bool has_datacount_section = false;
  5214. #endif
  5215. /* Find code and function sections if have */
  5216. while (section) {
  5217. if (section->section_type == SECTION_TYPE_CODE) {
  5218. buf_code = section->section_body;
  5219. buf_code_end = buf_code + section->section_body_size;
  5220. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5221. module->buf_code = (uint8 *)buf_code;
  5222. module->buf_code_size = section->section_body_size;
  5223. #endif
  5224. }
  5225. else if (section->section_type == SECTION_TYPE_FUNC) {
  5226. buf_func = section->section_body;
  5227. buf_func_end = buf_func + section->section_body_size;
  5228. }
  5229. section = section->next;
  5230. }
  5231. section = sections;
  5232. while (section) {
  5233. buf = section->section_body;
  5234. buf_end = buf + section->section_body_size;
  5235. switch (section->section_type) {
  5236. case SECTION_TYPE_USER:
  5237. /* unsupported user section, ignore it. */
  5238. if (!load_user_section(buf, buf_end, module,
  5239. reuse_const_strings, error_buf,
  5240. error_buf_size))
  5241. return false;
  5242. break;
  5243. case SECTION_TYPE_TYPE:
  5244. if (!load_type_section(buf, buf_end, module, error_buf,
  5245. error_buf_size))
  5246. return false;
  5247. break;
  5248. case SECTION_TYPE_IMPORT:
  5249. if (!load_import_section(buf, buf_end, module,
  5250. reuse_const_strings, no_resolve,
  5251. error_buf, error_buf_size))
  5252. return false;
  5253. break;
  5254. case SECTION_TYPE_FUNC:
  5255. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5256. module, error_buf, error_buf_size))
  5257. return false;
  5258. break;
  5259. case SECTION_TYPE_TABLE:
  5260. if (!load_table_section(buf, buf_end, module, error_buf,
  5261. error_buf_size))
  5262. return false;
  5263. break;
  5264. case SECTION_TYPE_MEMORY:
  5265. if (!load_memory_section(buf, buf_end, module, error_buf,
  5266. error_buf_size))
  5267. return false;
  5268. break;
  5269. #if WASM_ENABLE_TAGS != 0
  5270. case SECTION_TYPE_TAG:
  5271. /* load tag declaration section */
  5272. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5273. module, error_buf, error_buf_size))
  5274. return false;
  5275. break;
  5276. #endif
  5277. case SECTION_TYPE_GLOBAL:
  5278. if (!load_global_section(buf, buf_end, module, error_buf,
  5279. error_buf_size))
  5280. return false;
  5281. break;
  5282. case SECTION_TYPE_EXPORT:
  5283. if (!load_export_section(buf, buf_end, module,
  5284. reuse_const_strings, error_buf,
  5285. error_buf_size))
  5286. return false;
  5287. break;
  5288. case SECTION_TYPE_START:
  5289. if (!load_start_section(buf, buf_end, module, error_buf,
  5290. error_buf_size))
  5291. return false;
  5292. break;
  5293. case SECTION_TYPE_ELEM:
  5294. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5295. error_buf_size))
  5296. return false;
  5297. break;
  5298. case SECTION_TYPE_CODE:
  5299. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5300. module, error_buf, error_buf_size))
  5301. return false;
  5302. break;
  5303. case SECTION_TYPE_DATA:
  5304. if (!load_data_segment_section(buf, buf_end, module,
  5305. #if WASM_ENABLE_BULK_MEMORY != 0
  5306. has_datacount_section,
  5307. #endif
  5308. clone_data_seg, error_buf,
  5309. error_buf_size))
  5310. return false;
  5311. break;
  5312. #if WASM_ENABLE_BULK_MEMORY != 0
  5313. case SECTION_TYPE_DATACOUNT:
  5314. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5315. error_buf_size))
  5316. return false;
  5317. has_datacount_section = true;
  5318. break;
  5319. #endif
  5320. #if WASM_ENABLE_STRINGREF != 0
  5321. case SECTION_TYPE_STRINGREF:
  5322. if (!load_stringref_section(buf, buf_end, module,
  5323. reuse_const_strings, error_buf,
  5324. error_buf_size))
  5325. return false;
  5326. break;
  5327. #endif
  5328. default:
  5329. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5330. return false;
  5331. }
  5332. section = section->next;
  5333. }
  5334. #if WASM_ENABLE_BULK_MEMORY != 0
  5335. if (!check_data_count_consistency(
  5336. has_datacount_section, module->data_seg_count1,
  5337. module->data_seg_count, error_buf, error_buf_size)) {
  5338. return false;
  5339. }
  5340. #endif
  5341. module->aux_data_end_global_index = (uint32)-1;
  5342. module->aux_heap_base_global_index = (uint32)-1;
  5343. module->aux_stack_top_global_index = (uint32)-1;
  5344. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5345. export = module->exports;
  5346. for (i = 0; i < module->export_count; i++, export ++) {
  5347. if (export->kind == EXPORT_KIND_GLOBAL) {
  5348. if (!strcmp(export->name, "__heap_base")) {
  5349. if (export->index < module->import_global_count) {
  5350. LOG_DEBUG("Skip the process if __heap_base is imported "
  5351. "instead of being a local global");
  5352. continue;
  5353. }
  5354. /* only process linker-generated symbols */
  5355. global_index = export->index - module->import_global_count;
  5356. global = module->globals + global_index;
  5357. if (global->type.val_type == VALUE_TYPE_I32
  5358. && !global->type.is_mutable
  5359. && global->init_expr.init_expr_type
  5360. == INIT_EXPR_TYPE_I32_CONST) {
  5361. aux_heap_base_global = global;
  5362. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5363. aux_heap_base_global_index = export->index;
  5364. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5365. aux_heap_base);
  5366. }
  5367. }
  5368. else if (!strcmp(export->name, "__data_end")) {
  5369. if (export->index < module->import_global_count) {
  5370. LOG_DEBUG("Skip the process if __data_end is imported "
  5371. "instead of being a local global");
  5372. continue;
  5373. }
  5374. /* only process linker-generated symbols */
  5375. global_index = export->index - module->import_global_count;
  5376. global = module->globals + global_index;
  5377. if (global->type.val_type == VALUE_TYPE_I32
  5378. && !global->type.is_mutable
  5379. && global->init_expr.init_expr_type
  5380. == INIT_EXPR_TYPE_I32_CONST) {
  5381. aux_data_end_global = global;
  5382. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5383. aux_data_end_global_index = export->index;
  5384. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5385. aux_data_end);
  5386. aux_data_end = align_uint64(aux_data_end, 16);
  5387. }
  5388. }
  5389. /* For module compiled with -pthread option, the global is:
  5390. [0] stack_top <-- 0
  5391. [1] tls_pointer
  5392. [2] tls_size
  5393. [3] data_end <-- 3
  5394. [4] global_base
  5395. [5] heap_base <-- 5
  5396. [6] dso_handle
  5397. For module compiled without -pthread option:
  5398. [0] stack_top <-- 0
  5399. [1] data_end <-- 1
  5400. [2] global_base
  5401. [3] heap_base <-- 3
  5402. [4] dso_handle
  5403. */
  5404. if (aux_data_end_global && aux_heap_base_global
  5405. && aux_data_end <= aux_heap_base) {
  5406. module->aux_data_end_global_index = aux_data_end_global_index;
  5407. module->aux_data_end = aux_data_end;
  5408. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5409. module->aux_heap_base = aux_heap_base;
  5410. /* Resolve aux stack top global */
  5411. for (global_index = 0; global_index < module->global_count;
  5412. global_index++) {
  5413. global = module->globals + global_index;
  5414. if (global->type.is_mutable /* heap_base and data_end is
  5415. not mutable */
  5416. && global->type.val_type == VALUE_TYPE_I32
  5417. && global->init_expr.init_expr_type
  5418. == INIT_EXPR_TYPE_I32_CONST
  5419. && (uint64)(uint32)global->init_expr.u.i32
  5420. <= aux_heap_base) {
  5421. aux_stack_top_global = global;
  5422. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5423. module->aux_stack_top_global_index =
  5424. module->import_global_count + global_index;
  5425. module->aux_stack_bottom = aux_stack_top;
  5426. module->aux_stack_size =
  5427. aux_stack_top > aux_data_end
  5428. ? (uint32)(aux_stack_top - aux_data_end)
  5429. : (uint32)aux_stack_top;
  5430. LOG_VERBOSE(
  5431. "Found aux stack top global, value: %" PRIu64 ", "
  5432. "global index: %d, stack size: %d",
  5433. aux_stack_top, global_index,
  5434. module->aux_stack_size);
  5435. break;
  5436. }
  5437. }
  5438. if (!aux_stack_top_global) {
  5439. /* Auxiliary stack global isn't found, it must be unused
  5440. in the wasm app, as if it is used, the global must be
  5441. defined. Here we set it to __heap_base global and set
  5442. its size to 0. */
  5443. aux_stack_top_global = aux_heap_base_global;
  5444. aux_stack_top = aux_heap_base;
  5445. module->aux_stack_top_global_index =
  5446. module->aux_heap_base_global_index;
  5447. module->aux_stack_bottom = aux_stack_top;
  5448. module->aux_stack_size = 0;
  5449. }
  5450. break;
  5451. }
  5452. }
  5453. }
  5454. module->malloc_function = (uint32)-1;
  5455. module->free_function = (uint32)-1;
  5456. module->retain_function = (uint32)-1;
  5457. /* Resolve malloc/free function exported by wasm module */
  5458. #if WASM_ENABLE_MEMORY64 != 0
  5459. if (has_module_memory64(module))
  5460. malloc_free_io_type = VALUE_TYPE_I64;
  5461. #endif
  5462. export = module->exports;
  5463. for (i = 0; i < module->export_count; i++, export ++) {
  5464. if (export->kind == EXPORT_KIND_FUNC) {
  5465. if (!strcmp(export->name, "malloc")
  5466. && export->index >= module->import_function_count) {
  5467. func_index = export->index - module->import_function_count;
  5468. func_type = module->functions[func_index]->func_type;
  5469. if (func_type->param_count == 1 && func_type->result_count == 1
  5470. && func_type->types[0] == malloc_free_io_type
  5471. && func_type->types[1] == malloc_free_io_type) {
  5472. bh_assert(module->malloc_function == (uint32)-1);
  5473. module->malloc_function = export->index;
  5474. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5475. export->name, export->index);
  5476. }
  5477. }
  5478. else if (!strcmp(export->name, "__new")
  5479. && export->index >= module->import_function_count) {
  5480. /* __new && __pin for AssemblyScript */
  5481. func_index = export->index - module->import_function_count;
  5482. func_type = module->functions[func_index]->func_type;
  5483. if (func_type->param_count == 2 && func_type->result_count == 1
  5484. && func_type->types[0] == malloc_free_io_type
  5485. && func_type->types[1] == VALUE_TYPE_I32
  5486. && func_type->types[2] == malloc_free_io_type) {
  5487. uint32 j;
  5488. WASMExport *export_tmp;
  5489. bh_assert(module->malloc_function == (uint32)-1);
  5490. module->malloc_function = export->index;
  5491. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5492. export->name, export->index);
  5493. /* resolve retain function.
  5494. If not found, reset malloc function index */
  5495. export_tmp = module->exports;
  5496. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5497. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5498. && (!strcmp(export_tmp->name, "__retain")
  5499. || (!strcmp(export_tmp->name, "__pin")))
  5500. && (export_tmp->index
  5501. >= module->import_function_count)) {
  5502. func_index = export_tmp->index
  5503. - module->import_function_count;
  5504. func_type =
  5505. module->functions[func_index]->func_type;
  5506. if (func_type->param_count == 1
  5507. && func_type->result_count == 1
  5508. && func_type->types[0] == malloc_free_io_type
  5509. && func_type->types[1] == malloc_free_io_type) {
  5510. bh_assert(module->retain_function
  5511. == (uint32)-1);
  5512. module->retain_function = export_tmp->index;
  5513. LOG_VERBOSE("Found retain function, name: %s, "
  5514. "index: %u",
  5515. export_tmp->name,
  5516. export_tmp->index);
  5517. break;
  5518. }
  5519. }
  5520. }
  5521. if (j == module->export_count) {
  5522. module->malloc_function = (uint32)-1;
  5523. LOG_VERBOSE("Can't find retain function,"
  5524. "reset malloc function index to -1");
  5525. }
  5526. }
  5527. }
  5528. else if (((!strcmp(export->name, "free"))
  5529. || (!strcmp(export->name, "__release"))
  5530. || (!strcmp(export->name, "__unpin")))
  5531. && export->index >= module->import_function_count) {
  5532. func_index = export->index - module->import_function_count;
  5533. func_type = module->functions[func_index]->func_type;
  5534. if (func_type->param_count == 1 && func_type->result_count == 0
  5535. && func_type->types[0] == malloc_free_io_type) {
  5536. bh_assert(module->free_function == (uint32)-1);
  5537. module->free_function = export->index;
  5538. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5539. export->name, export->index);
  5540. }
  5541. }
  5542. }
  5543. }
  5544. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5545. handle_table = wasm_interp_get_handle_table();
  5546. #endif
  5547. for (i = 0; i < module->function_count; i++) {
  5548. WASMFunction *func = module->functions[i];
  5549. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5550. error_buf_size)) {
  5551. return false;
  5552. }
  5553. if (i == module->function_count - 1
  5554. && func->code + func->code_size != buf_code_end) {
  5555. set_error_buf(error_buf, error_buf_size,
  5556. "code section size mismatch");
  5557. return false;
  5558. }
  5559. }
  5560. if (!module->possible_memory_grow) {
  5561. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5562. if (aux_data_end_global && aux_heap_base_global
  5563. && aux_stack_top_global) {
  5564. uint64 init_memory_size;
  5565. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5566. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5567. * valid range of uint32 */
  5568. if (shrunk_memory_size <= UINT32_MAX) {
  5569. if (module->import_memory_count) {
  5570. WASMMemoryImport *memory_import =
  5571. &module->import_memories[0].u.memory;
  5572. init_memory_size =
  5573. (uint64)memory_import->mem_type.num_bytes_per_page
  5574. * memory_import->mem_type.init_page_count;
  5575. if (shrunk_memory_size <= init_memory_size) {
  5576. /* Reset memory info to decrease memory usage */
  5577. memory_import->mem_type.num_bytes_per_page =
  5578. (uint32)shrunk_memory_size;
  5579. memory_import->mem_type.init_page_count = 1;
  5580. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5581. shrunk_memory_size);
  5582. }
  5583. }
  5584. if (module->memory_count) {
  5585. WASMMemory *memory = &module->memories[0];
  5586. init_memory_size = (uint64)memory->num_bytes_per_page
  5587. * memory->init_page_count;
  5588. if (shrunk_memory_size <= init_memory_size) {
  5589. /* Reset memory info to decrease memory usage */
  5590. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5591. memory->init_page_count = 1;
  5592. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5593. shrunk_memory_size);
  5594. }
  5595. }
  5596. }
  5597. }
  5598. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  5599. #if WASM_ENABLE_MULTI_MODULE == 0
  5600. if (module->import_memory_count) {
  5601. WASMMemoryImport *memory_import =
  5602. &module->import_memories[0].u.memory;
  5603. /* Only resize the memory to one big page if num_bytes_per_page is
  5604. * in valid range of uint32 */
  5605. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5606. memory_import->mem_type.num_bytes_per_page *=
  5607. memory_import->mem_type.init_page_count;
  5608. if (memory_import->mem_type.init_page_count > 0)
  5609. memory_import->mem_type.init_page_count =
  5610. memory_import->mem_type.max_page_count = 1;
  5611. else
  5612. memory_import->mem_type.init_page_count =
  5613. memory_import->mem_type.max_page_count = 0;
  5614. }
  5615. }
  5616. if (module->memory_count) {
  5617. WASMMemory *memory = &module->memories[0];
  5618. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5619. * valid range of uint32 */
  5620. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5621. memory->num_bytes_per_page *= memory->init_page_count;
  5622. if (memory->init_page_count > 0)
  5623. memory->init_page_count = memory->max_page_count = 1;
  5624. else
  5625. memory->init_page_count = memory->max_page_count = 0;
  5626. }
  5627. }
  5628. #endif
  5629. }
  5630. #if WASM_ENABLE_MEMORY64 != 0
  5631. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5632. false))
  5633. return false;
  5634. #endif
  5635. calculate_global_data_offset(module);
  5636. #if WASM_ENABLE_FAST_JIT != 0
  5637. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5638. return false;
  5639. }
  5640. #endif
  5641. #if WASM_ENABLE_JIT != 0
  5642. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5643. return false;
  5644. }
  5645. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5646. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5647. return false;
  5648. }
  5649. #else
  5650. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5651. thread fast jit execution, since applying llvm optimizations in
  5652. aot_compile_wasm may cost a lot of time.
  5653. Create thread with enough native stack to apply llvm optimizations */
  5654. if (os_thread_create(&module->llvm_jit_init_thread,
  5655. init_llvm_jit_functions_stage2_callback,
  5656. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5657. != 0) {
  5658. set_error_buf(error_buf, error_buf_size,
  5659. "create orcjit compile thread failed");
  5660. return false;
  5661. }
  5662. #endif
  5663. #endif
  5664. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5665. /* Create threads to compile the jit functions */
  5666. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5667. return false;
  5668. }
  5669. #endif
  5670. #if WASM_ENABLE_MEMORY_TRACING != 0
  5671. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5672. #endif
  5673. return true;
  5674. }
  5675. static WASMModule *
  5676. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5677. {
  5678. WASMModule *module =
  5679. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5680. bh_list_status ret;
  5681. if (!module) {
  5682. return NULL;
  5683. }
  5684. module->module_type = Wasm_Module_Bytecode;
  5685. /* Set start_function to -1, means no start function */
  5686. module->start_function = (uint32)-1;
  5687. module->name = name;
  5688. module->is_binary_freeable = false;
  5689. #if WASM_ENABLE_FAST_INTERP == 0
  5690. module->br_table_cache_list = &module->br_table_cache_list_head;
  5691. ret = bh_list_init(module->br_table_cache_list);
  5692. bh_assert(ret == BH_LIST_SUCCESS);
  5693. #endif
  5694. #if WASM_ENABLE_MULTI_MODULE != 0
  5695. module->import_module_list = &module->import_module_list_head;
  5696. ret = bh_list_init(module->import_module_list);
  5697. bh_assert(ret == BH_LIST_SUCCESS);
  5698. #endif
  5699. #if WASM_ENABLE_DEBUG_INTERP != 0
  5700. ret = bh_list_init(&module->fast_opcode_list);
  5701. bh_assert(ret == BH_LIST_SUCCESS);
  5702. #endif
  5703. #if WASM_ENABLE_GC != 0
  5704. if (!(module->ref_type_set =
  5705. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5706. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5707. goto fail1;
  5708. }
  5709. if (os_mutex_init(&module->rtt_type_lock)) {
  5710. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5711. goto fail2;
  5712. }
  5713. #endif
  5714. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5715. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5716. && WASM_ENABLE_LAZY_JIT != 0)
  5717. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5718. set_error_buf(error_buf, error_buf_size,
  5719. "init instance list lock failed");
  5720. goto fail3;
  5721. }
  5722. #endif
  5723. #if WASM_ENABLE_LIBC_WASI != 0
  5724. #if WASM_ENABLE_UVWASI == 0
  5725. module->wasi_args.stdio[0] = os_invalid_raw_handle();
  5726. module->wasi_args.stdio[1] = os_invalid_raw_handle();
  5727. module->wasi_args.stdio[2] = os_invalid_raw_handle();
  5728. #else
  5729. module->wasi_args.stdio[0] = os_get_invalid_handle();
  5730. module->wasi_args.stdio[1] = os_get_invalid_handle();
  5731. module->wasi_args.stdio[2] = os_get_invalid_handle();
  5732. #endif /* WASM_ENABLE_UVWASI == 0 */
  5733. #endif /* WASM_ENABLE_LIBC_WASI != 0 */
  5734. (void)ret;
  5735. return module;
  5736. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5737. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5738. && WASM_ENABLE_LAZY_JIT != 0)
  5739. fail3:
  5740. #endif
  5741. #if WASM_ENABLE_GC != 0
  5742. os_mutex_destroy(&module->rtt_type_lock);
  5743. fail2:
  5744. bh_hash_map_destroy(module->ref_type_set);
  5745. fail1:
  5746. #endif
  5747. wasm_runtime_free(module);
  5748. return NULL;
  5749. }
  5750. #if WASM_ENABLE_DEBUG_INTERP != 0
  5751. static bool
  5752. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5753. uint32 error_buf_size)
  5754. {
  5755. WASMFastOPCodeNode *fast_op =
  5756. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5757. if (fast_op) {
  5758. fast_op->offset = pos - module->load_addr;
  5759. fast_op->orig_op = orig_op;
  5760. bh_list_insert(&module->fast_opcode_list, fast_op);
  5761. }
  5762. return fast_op ? true : false;
  5763. }
  5764. #endif
  5765. WASMModule *
  5766. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5767. uint32 error_buf_size)
  5768. {
  5769. WASMModule *module = create_module("", error_buf, error_buf_size);
  5770. if (!module)
  5771. return NULL;
  5772. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  5773. error_buf_size)) {
  5774. wasm_loader_unload(module);
  5775. return NULL;
  5776. }
  5777. LOG_VERBOSE("Load module from sections success.\n");
  5778. return module;
  5779. }
  5780. static void
  5781. destroy_sections(WASMSection *section_list)
  5782. {
  5783. WASMSection *section = section_list, *next;
  5784. while (section) {
  5785. next = section->next;
  5786. wasm_runtime_free(section);
  5787. section = next;
  5788. }
  5789. }
  5790. /* clang-format off */
  5791. static uint8 section_ids[] = {
  5792. SECTION_TYPE_USER,
  5793. SECTION_TYPE_TYPE,
  5794. SECTION_TYPE_IMPORT,
  5795. SECTION_TYPE_FUNC,
  5796. SECTION_TYPE_TABLE,
  5797. SECTION_TYPE_MEMORY,
  5798. #if WASM_ENABLE_TAGS != 0
  5799. SECTION_TYPE_TAG,
  5800. #endif
  5801. #if WASM_ENABLE_STRINGREF != 0
  5802. /* must immediately precede the global section,
  5803. or where the global section would be */
  5804. SECTION_TYPE_STRINGREF,
  5805. #endif
  5806. SECTION_TYPE_GLOBAL,
  5807. SECTION_TYPE_EXPORT,
  5808. SECTION_TYPE_START,
  5809. SECTION_TYPE_ELEM,
  5810. #if WASM_ENABLE_BULK_MEMORY != 0
  5811. SECTION_TYPE_DATACOUNT,
  5812. #endif
  5813. SECTION_TYPE_CODE,
  5814. SECTION_TYPE_DATA
  5815. };
  5816. /* clang-format on */
  5817. static uint8
  5818. get_section_index(uint8 section_type)
  5819. {
  5820. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5821. for (uint8 i = 0; i < max_id; i++) {
  5822. if (section_type == section_ids[i])
  5823. return i;
  5824. }
  5825. return (uint8)-1;
  5826. }
  5827. static bool
  5828. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5829. char *error_buf, uint32 error_buf_size)
  5830. {
  5831. WASMSection *section_list_end = NULL, *section;
  5832. const uint8 *p = buf, *p_end = buf + size;
  5833. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5834. uint32 section_size;
  5835. bh_assert(!*p_section_list);
  5836. p += 8;
  5837. while (p < p_end) {
  5838. CHECK_BUF(p, p_end, 1);
  5839. section_type = read_uint8(p);
  5840. section_index = get_section_index(section_type);
  5841. if (section_index != (uint8)-1) {
  5842. if (section_type != SECTION_TYPE_USER) {
  5843. /* Custom sections may be inserted at any place,
  5844. while other sections must occur at most once
  5845. and in prescribed order. */
  5846. if (last_section_index != (uint8)-1
  5847. && (section_index <= last_section_index)) {
  5848. set_error_buf(error_buf, error_buf_size,
  5849. "unexpected content after last section or "
  5850. "junk after last section");
  5851. return false;
  5852. }
  5853. last_section_index = section_index;
  5854. }
  5855. read_leb_uint32(p, p_end, section_size);
  5856. CHECK_BUF1(p, p_end, section_size);
  5857. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5858. error_buf_size))) {
  5859. return false;
  5860. }
  5861. section->section_type = section_type;
  5862. section->section_body = (uint8 *)p;
  5863. section->section_body_size = section_size;
  5864. if (!section_list_end)
  5865. *p_section_list = section_list_end = section;
  5866. else {
  5867. section_list_end->next = section;
  5868. section_list_end = section;
  5869. }
  5870. p += section_size;
  5871. }
  5872. else {
  5873. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5874. return false;
  5875. }
  5876. }
  5877. return true;
  5878. fail:
  5879. return false;
  5880. }
  5881. static void
  5882. exchange32(uint8 *p_data)
  5883. {
  5884. uint8 value = *p_data;
  5885. *p_data = *(p_data + 3);
  5886. *(p_data + 3) = value;
  5887. value = *(p_data + 1);
  5888. *(p_data + 1) = *(p_data + 2);
  5889. *(p_data + 2) = value;
  5890. }
  5891. static union {
  5892. int a;
  5893. char b;
  5894. } __ue = { .a = 1 };
  5895. #define is_little_endian() (__ue.b == 1)
  5896. static bool
  5897. load(const uint8 *buf, uint32 size, WASMModule *module,
  5898. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  5899. uint32 error_buf_size)
  5900. {
  5901. const uint8 *buf_end = buf + size;
  5902. const uint8 *p = buf, *p_end = buf_end;
  5903. uint32 magic_number, version;
  5904. WASMSection *section_list = NULL;
  5905. CHECK_BUF1(p, p_end, sizeof(uint32));
  5906. magic_number = read_uint32(p);
  5907. if (!is_little_endian())
  5908. exchange32((uint8 *)&magic_number);
  5909. if (magic_number != WASM_MAGIC_NUMBER) {
  5910. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5911. return false;
  5912. }
  5913. CHECK_BUF1(p, p_end, sizeof(uint32));
  5914. version = read_uint32(p);
  5915. if (!is_little_endian())
  5916. exchange32((uint8 *)&version);
  5917. if (version != WASM_CURRENT_VERSION) {
  5918. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5919. return false;
  5920. }
  5921. module->package_version = version;
  5922. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5923. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5924. no_resolve, error_buf, error_buf_size)) {
  5925. destroy_sections(section_list);
  5926. return false;
  5927. }
  5928. destroy_sections(section_list);
  5929. return true;
  5930. fail:
  5931. return false;
  5932. }
  5933. #if WASM_ENABLE_LIBC_WASI != 0
  5934. /**
  5935. * refer to
  5936. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5937. */
  5938. static bool
  5939. check_wasi_abi_compatibility(const WASMModule *module,
  5940. #if WASM_ENABLE_MULTI_MODULE != 0
  5941. bool main_module,
  5942. #endif
  5943. char *error_buf, uint32 error_buf_size)
  5944. {
  5945. /**
  5946. * be careful with:
  5947. * wasi compatible modules(command/reactor) which don't import any wasi
  5948. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5949. * reactor can depend on nothing. At the same time, each has its own entry
  5950. * point.
  5951. *
  5952. * observations:
  5953. * - clang always injects `_start` into a command
  5954. * - clang always injects `_initialize` into a reactor
  5955. * - `iwasm -f` allows to run a function in the reactor
  5956. *
  5957. * strong assumptions:
  5958. * - no one will define either `_start` or `_initialize` on purpose
  5959. * - `_start` should always be `void _start(void)`
  5960. * - `_initialize` should always be `void _initialize(void)`
  5961. *
  5962. */
  5963. /* clang-format off */
  5964. /**
  5965. *
  5966. * | | import_wasi_api True | | import_wasi_api False | |
  5967. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5968. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5969. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5970. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5971. */
  5972. /* clang-format on */
  5973. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5974. uint32 import_function_count = module->import_function_count;
  5975. WASMFuncType *func_type;
  5976. /* (func (export "_start") (...) */
  5977. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5978. error_buf, error_buf_size);
  5979. if (start) {
  5980. if (start->index < import_function_count) {
  5981. set_error_buf(
  5982. error_buf, error_buf_size,
  5983. "the builtin _start function can not be an import function");
  5984. return false;
  5985. }
  5986. func_type =
  5987. module->functions[start->index - import_function_count]->func_type;
  5988. if (func_type->param_count || func_type->result_count) {
  5989. set_error_buf(error_buf, error_buf_size,
  5990. "the signature of builtin _start function is wrong");
  5991. return false;
  5992. }
  5993. }
  5994. else {
  5995. /* (func (export "_initialize") (...) */
  5996. initialize =
  5997. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5998. error_buf, error_buf_size);
  5999. if (initialize) {
  6000. if (initialize->index < import_function_count) {
  6001. set_error_buf(error_buf, error_buf_size,
  6002. "the builtin _initialize function can not be an "
  6003. "import function");
  6004. return false;
  6005. }
  6006. func_type =
  6007. module->functions[initialize->index - import_function_count]
  6008. ->func_type;
  6009. if (func_type->param_count || func_type->result_count) {
  6010. set_error_buf(
  6011. error_buf, error_buf_size,
  6012. "the signature of builtin _initialize function is wrong");
  6013. return false;
  6014. }
  6015. }
  6016. }
  6017. /* filter out non-wasi compatible modules */
  6018. if (!module->import_wasi_api && !start && !initialize) {
  6019. return true;
  6020. }
  6021. /* should have one at least */
  6022. if (module->import_wasi_api && !start && !initialize) {
  6023. LOG_WARNING("warning: a module with WASI apis should be either "
  6024. "a command or a reactor");
  6025. }
  6026. /*
  6027. * there is at least one of `_start` and `_initialize` in below cases.
  6028. * according to the assumption, they should be all wasi compatible
  6029. */
  6030. #if WASM_ENABLE_MULTI_MODULE != 0
  6031. /* filter out commands (with `_start`) cases */
  6032. if (start && !main_module) {
  6033. set_error_buf(
  6034. error_buf, error_buf_size,
  6035. "a command (with _start function) can not be a sub-module");
  6036. return false;
  6037. }
  6038. #endif
  6039. /*
  6040. * it is ok a reactor acts as a main module,
  6041. * so skip the check about (with `_initialize`)
  6042. */
  6043. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  6044. error_buf, error_buf_size);
  6045. if (!memory
  6046. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  6047. /*
  6048. * with wasi-threads, it's still an open question if a memory
  6049. * should be exported.
  6050. *
  6051. * https://github.com/WebAssembly/wasi-threads/issues/22
  6052. * https://github.com/WebAssembly/WASI/issues/502
  6053. *
  6054. * Note: this code assumes the number of memories is at most 1.
  6055. */
  6056. && module->import_memory_count == 0
  6057. #endif
  6058. ) {
  6059. set_error_buf(error_buf, error_buf_size,
  6060. "a module with WASI apis must export memory by default");
  6061. return false;
  6062. }
  6063. return true;
  6064. }
  6065. #endif
  6066. WASMModule *
  6067. wasm_loader_load(uint8 *buf, uint32 size,
  6068. #if WASM_ENABLE_MULTI_MODULE != 0
  6069. bool main_module,
  6070. #endif
  6071. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6072. {
  6073. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6074. if (!module) {
  6075. return NULL;
  6076. }
  6077. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6078. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6079. module->load_addr = (uint8 *)buf;
  6080. module->load_size = size;
  6081. #endif
  6082. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6083. error_buf, error_buf_size)) {
  6084. goto fail;
  6085. }
  6086. #if WASM_ENABLE_LIBC_WASI != 0
  6087. /* Check the WASI application ABI */
  6088. if (!check_wasi_abi_compatibility(module,
  6089. #if WASM_ENABLE_MULTI_MODULE != 0
  6090. main_module,
  6091. #endif
  6092. error_buf, error_buf_size)) {
  6093. goto fail;
  6094. }
  6095. #endif
  6096. LOG_VERBOSE("Load module success.\n");
  6097. return module;
  6098. fail:
  6099. wasm_loader_unload(module);
  6100. return NULL;
  6101. }
  6102. void
  6103. wasm_loader_unload(WASMModule *module)
  6104. {
  6105. uint32 i;
  6106. if (!module)
  6107. return;
  6108. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6109. && WASM_ENABLE_LAZY_JIT != 0
  6110. module->orcjit_stop_compiling = true;
  6111. if (module->llvm_jit_init_thread)
  6112. os_thread_join(module->llvm_jit_init_thread, NULL);
  6113. #endif
  6114. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6115. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6116. module internal data after they were freed */
  6117. orcjit_stop_compile_threads(module);
  6118. #endif
  6119. #if WASM_ENABLE_JIT != 0
  6120. if (module->func_ptrs)
  6121. wasm_runtime_free(module->func_ptrs);
  6122. if (module->comp_ctx)
  6123. aot_destroy_comp_context(module->comp_ctx);
  6124. if (module->comp_data)
  6125. aot_destroy_comp_data(module->comp_data);
  6126. #endif
  6127. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6128. && WASM_ENABLE_LAZY_JIT != 0
  6129. if (module->tierup_wait_lock_inited) {
  6130. os_mutex_destroy(&module->tierup_wait_lock);
  6131. os_cond_destroy(&module->tierup_wait_cond);
  6132. }
  6133. #endif
  6134. if (module->imports)
  6135. wasm_runtime_free(module->imports);
  6136. if (module->functions) {
  6137. for (i = 0; i < module->function_count; i++) {
  6138. if (module->functions[i]) {
  6139. if (module->functions[i]->local_offsets)
  6140. wasm_runtime_free(module->functions[i]->local_offsets);
  6141. #if WASM_ENABLE_FAST_INTERP != 0
  6142. if (module->functions[i]->code_compiled)
  6143. wasm_runtime_free(module->functions[i]->code_compiled);
  6144. if (module->functions[i]->consts)
  6145. wasm_runtime_free(module->functions[i]->consts);
  6146. #endif
  6147. #if WASM_ENABLE_FAST_JIT != 0
  6148. if (module->functions[i]->fast_jit_jitted_code) {
  6149. jit_code_cache_free(
  6150. module->functions[i]->fast_jit_jitted_code);
  6151. }
  6152. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6153. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6154. jit_code_cache_free(
  6155. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6156. }
  6157. #endif
  6158. #endif
  6159. #if WASM_ENABLE_GC != 0
  6160. if (module->functions[i]->local_ref_type_maps) {
  6161. wasm_runtime_free(
  6162. module->functions[i]->local_ref_type_maps);
  6163. }
  6164. #endif
  6165. wasm_runtime_free(module->functions[i]);
  6166. }
  6167. }
  6168. wasm_runtime_free(module->functions);
  6169. }
  6170. if (module->tables) {
  6171. #if WASM_ENABLE_GC != 0
  6172. for (i = 0; i < module->table_count; i++) {
  6173. destroy_init_expr(module, &module->tables[i].init_expr);
  6174. }
  6175. #endif
  6176. wasm_runtime_free(module->tables);
  6177. }
  6178. if (module->memories)
  6179. wasm_runtime_free(module->memories);
  6180. if (module->globals) {
  6181. #if WASM_ENABLE_GC != 0
  6182. for (i = 0; i < module->global_count; i++) {
  6183. destroy_init_expr(module, &module->globals[i].init_expr);
  6184. }
  6185. #endif
  6186. wasm_runtime_free(module->globals);
  6187. }
  6188. #if WASM_ENABLE_TAGS != 0
  6189. if (module->tags) {
  6190. for (i = 0; i < module->tag_count; i++) {
  6191. if (module->tags[i])
  6192. wasm_runtime_free(module->tags[i]);
  6193. }
  6194. wasm_runtime_free(module->tags);
  6195. }
  6196. #endif
  6197. if (module->exports)
  6198. wasm_runtime_free(module->exports);
  6199. if (module->table_segments) {
  6200. for (i = 0; i < module->table_seg_count; i++) {
  6201. if (module->table_segments[i].init_values) {
  6202. #if WASM_ENABLE_GC != 0
  6203. uint32 j;
  6204. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6205. destroy_init_expr(
  6206. module, &module->table_segments[i].init_values[j]);
  6207. }
  6208. #endif
  6209. wasm_runtime_free(module->table_segments[i].init_values);
  6210. }
  6211. }
  6212. wasm_runtime_free(module->table_segments);
  6213. }
  6214. if (module->data_segments) {
  6215. for (i = 0; i < module->data_seg_count; i++) {
  6216. if (module->data_segments[i]) {
  6217. if (module->data_segments[i]->is_data_cloned)
  6218. wasm_runtime_free(module->data_segments[i]->data);
  6219. wasm_runtime_free(module->data_segments[i]);
  6220. }
  6221. }
  6222. wasm_runtime_free(module->data_segments);
  6223. }
  6224. if (module->types) {
  6225. for (i = 0; i < module->type_count; i++) {
  6226. if (module->types[i])
  6227. destroy_wasm_type(module->types[i]);
  6228. }
  6229. wasm_runtime_free(module->types);
  6230. }
  6231. if (module->const_str_list) {
  6232. StringNode *node = module->const_str_list, *node_next;
  6233. while (node) {
  6234. node_next = node->next;
  6235. wasm_runtime_free(node);
  6236. node = node_next;
  6237. }
  6238. }
  6239. #if WASM_ENABLE_STRINGREF != 0
  6240. if (module->string_literal_ptrs) {
  6241. wasm_runtime_free((void *)module->string_literal_ptrs);
  6242. }
  6243. if (module->string_literal_lengths) {
  6244. wasm_runtime_free(module->string_literal_lengths);
  6245. }
  6246. #endif
  6247. #if WASM_ENABLE_FAST_INTERP == 0
  6248. if (module->br_table_cache_list) {
  6249. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6250. BrTableCache *node_next;
  6251. while (node) {
  6252. node_next = bh_list_elem_next(node);
  6253. wasm_runtime_free(node);
  6254. node = node_next;
  6255. }
  6256. }
  6257. #endif
  6258. #if WASM_ENABLE_MULTI_MODULE != 0
  6259. /* just release the sub module list */
  6260. if (module->import_module_list) {
  6261. WASMRegisteredModule *node =
  6262. bh_list_first_elem(module->import_module_list);
  6263. while (node) {
  6264. WASMRegisteredModule *next = bh_list_elem_next(node);
  6265. bh_list_remove(module->import_module_list, node);
  6266. /*
  6267. * unload(sub_module) will be triggered during runtime_destroy().
  6268. * every module in the global module list will be unloaded one by
  6269. * one. so don't worry.
  6270. */
  6271. wasm_runtime_free(node);
  6272. /*
  6273. * the module file reading buffer will be released
  6274. * in runtime_destroy()
  6275. */
  6276. node = next;
  6277. }
  6278. }
  6279. #endif
  6280. #if WASM_ENABLE_DEBUG_INTERP != 0
  6281. WASMFastOPCodeNode *fast_opcode =
  6282. bh_list_first_elem(&module->fast_opcode_list);
  6283. while (fast_opcode) {
  6284. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6285. wasm_runtime_free(fast_opcode);
  6286. fast_opcode = next;
  6287. }
  6288. #endif
  6289. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6290. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6291. && WASM_ENABLE_LAZY_JIT != 0)
  6292. os_mutex_destroy(&module->instance_list_lock);
  6293. #endif
  6294. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6295. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6296. #endif
  6297. #if WASM_ENABLE_FAST_JIT != 0
  6298. if (module->fast_jit_func_ptrs) {
  6299. wasm_runtime_free(module->fast_jit_func_ptrs);
  6300. }
  6301. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6302. if (module->fast_jit_thread_locks_inited[i]) {
  6303. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6304. }
  6305. }
  6306. #endif
  6307. #if WASM_ENABLE_GC != 0
  6308. os_mutex_destroy(&module->rtt_type_lock);
  6309. bh_hash_map_destroy(module->ref_type_set);
  6310. if (module->rtt_types) {
  6311. for (i = 0; i < module->type_count; i++) {
  6312. if (module->rtt_types[i])
  6313. wasm_runtime_free(module->rtt_types[i]);
  6314. }
  6315. wasm_runtime_free(module->rtt_types);
  6316. }
  6317. #if WASM_ENABLE_STRINGREF != 0
  6318. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6319. if (module->stringref_rtts[i])
  6320. wasm_runtime_free(module->stringref_rtts[i]);
  6321. }
  6322. #endif
  6323. #endif
  6324. wasm_runtime_free(module);
  6325. }
  6326. bool
  6327. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6328. const uint8 *start_addr, const uint8 *code_end_addr,
  6329. uint8 label_type, uint8 **p_else_addr,
  6330. uint8 **p_end_addr)
  6331. {
  6332. const uint8 *p = start_addr, *p_end = code_end_addr;
  6333. uint8 *else_addr = NULL;
  6334. char error_buf[128];
  6335. uint32 block_nested_depth = 1, count, i, j, t;
  6336. uint32 error_buf_size = sizeof(error_buf);
  6337. uint8 opcode, u8;
  6338. BlockAddr block_stack[16] = { { 0 } }, *block;
  6339. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6340. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6341. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6342. if (block[j].start_addr == start_addr) {
  6343. /* Cache hit */
  6344. *p_else_addr = block[j].else_addr;
  6345. *p_end_addr = block[j].end_addr;
  6346. return true;
  6347. }
  6348. }
  6349. /* Cache unhit */
  6350. block_stack[0].start_addr = start_addr;
  6351. while (p < code_end_addr) {
  6352. opcode = *p++;
  6353. #if WASM_ENABLE_DEBUG_INTERP != 0
  6354. op_break_retry:
  6355. #endif
  6356. switch (opcode) {
  6357. case WASM_OP_UNREACHABLE:
  6358. case WASM_OP_NOP:
  6359. break;
  6360. #if WASM_ENABLE_EXCE_HANDLING != 0
  6361. case WASM_OP_TRY:
  6362. u8 = read_uint8(p);
  6363. if (block_nested_depth
  6364. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6365. block_stack[block_nested_depth].start_addr = p;
  6366. block_stack[block_nested_depth].else_addr = NULL;
  6367. }
  6368. block_nested_depth++;
  6369. break;
  6370. case EXT_OP_TRY:
  6371. skip_leb_uint32(p, p_end);
  6372. if (block_nested_depth
  6373. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6374. block_stack[block_nested_depth].start_addr = p;
  6375. block_stack[block_nested_depth].else_addr = NULL;
  6376. }
  6377. block_nested_depth++;
  6378. break;
  6379. case WASM_OP_CATCH:
  6380. if (block_nested_depth == 1) {
  6381. *p_end_addr = (uint8 *)(p - 1);
  6382. /* stop search and return the address of the catch block */
  6383. return true;
  6384. }
  6385. break;
  6386. case WASM_OP_CATCH_ALL:
  6387. if (block_nested_depth == 1) {
  6388. *p_end_addr = (uint8 *)(p - 1);
  6389. /* stop search and return the address of the catch_all block
  6390. */
  6391. return true;
  6392. }
  6393. break;
  6394. case WASM_OP_THROW:
  6395. /* skip tag_index */
  6396. skip_leb(p);
  6397. break;
  6398. case WASM_OP_RETHROW:
  6399. /* skip depth */
  6400. skip_leb(p);
  6401. break;
  6402. case WASM_OP_DELEGATE:
  6403. if (block_nested_depth == 1) {
  6404. *p_end_addr = (uint8 *)(p - 1);
  6405. return true;
  6406. }
  6407. else {
  6408. skip_leb(p);
  6409. /* the DELEGATE opcode ends the tryblock, */
  6410. block_nested_depth--;
  6411. if (block_nested_depth
  6412. < sizeof(block_stack) / sizeof(BlockAddr))
  6413. block_stack[block_nested_depth].end_addr =
  6414. (uint8 *)(p - 1);
  6415. }
  6416. break;
  6417. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6418. case WASM_OP_BLOCK:
  6419. case WASM_OP_LOOP:
  6420. case WASM_OP_IF:
  6421. {
  6422. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6423. u8 = read_uint8(p);
  6424. if (is_byte_a_type(u8)) {
  6425. #if WASM_ENABLE_GC != 0
  6426. if (wasm_is_type_multi_byte_type(u8)) {
  6427. /* the possible extra bytes of GC ref type have been
  6428. modified to OP_NOP, no need to resolve them again */
  6429. }
  6430. #endif
  6431. }
  6432. else {
  6433. p--;
  6434. /* block type */
  6435. skip_leb_int32(p, p_end);
  6436. }
  6437. if (block_nested_depth
  6438. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6439. block_stack[block_nested_depth].start_addr = p;
  6440. block_stack[block_nested_depth].else_addr = NULL;
  6441. }
  6442. block_nested_depth++;
  6443. break;
  6444. }
  6445. case EXT_OP_BLOCK:
  6446. case EXT_OP_LOOP:
  6447. case EXT_OP_IF:
  6448. /* block type */
  6449. skip_leb_int32(p, p_end);
  6450. if (block_nested_depth
  6451. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6452. block_stack[block_nested_depth].start_addr = p;
  6453. block_stack[block_nested_depth].else_addr = NULL;
  6454. }
  6455. block_nested_depth++;
  6456. break;
  6457. case WASM_OP_ELSE:
  6458. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6459. else_addr = (uint8 *)(p - 1);
  6460. if (block_nested_depth - 1
  6461. < sizeof(block_stack) / sizeof(BlockAddr))
  6462. block_stack[block_nested_depth - 1].else_addr =
  6463. (uint8 *)(p - 1);
  6464. break;
  6465. case WASM_OP_END:
  6466. if (block_nested_depth == 1) {
  6467. if (label_type == LABEL_TYPE_IF)
  6468. *p_else_addr = else_addr;
  6469. *p_end_addr = (uint8 *)(p - 1);
  6470. block_stack[0].end_addr = (uint8 *)(p - 1);
  6471. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6472. t++) {
  6473. start_addr = block_stack[t].start_addr;
  6474. if (start_addr) {
  6475. i = ((uintptr_t)start_addr)
  6476. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6477. block =
  6478. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6479. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6480. if (!block[j].start_addr)
  6481. break;
  6482. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6483. memmove(block + 1, block,
  6484. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6485. * sizeof(BlockAddr));
  6486. j = 0;
  6487. }
  6488. block[j].start_addr = block_stack[t].start_addr;
  6489. block[j].else_addr = block_stack[t].else_addr;
  6490. block[j].end_addr = block_stack[t].end_addr;
  6491. }
  6492. else
  6493. break;
  6494. }
  6495. return true;
  6496. }
  6497. else {
  6498. block_nested_depth--;
  6499. if (block_nested_depth
  6500. < sizeof(block_stack) / sizeof(BlockAddr))
  6501. block_stack[block_nested_depth].end_addr =
  6502. (uint8 *)(p - 1);
  6503. }
  6504. break;
  6505. case WASM_OP_BR:
  6506. case WASM_OP_BR_IF:
  6507. skip_leb_uint32(p, p_end); /* labelidx */
  6508. break;
  6509. case WASM_OP_BR_TABLE:
  6510. read_leb_uint32(p, p_end, count); /* label num */
  6511. #if WASM_ENABLE_FAST_INTERP != 0
  6512. for (i = 0; i <= count; i++) /* labelidxs */
  6513. skip_leb_uint32(p, p_end);
  6514. #else
  6515. p += count + 1;
  6516. while (*p == WASM_OP_NOP)
  6517. p++;
  6518. #endif
  6519. break;
  6520. #if WASM_ENABLE_FAST_INTERP == 0
  6521. case EXT_OP_BR_TABLE_CACHE:
  6522. read_leb_uint32(p, p_end, count); /* label num */
  6523. while (*p == WASM_OP_NOP)
  6524. p++;
  6525. break;
  6526. #endif
  6527. case WASM_OP_RETURN:
  6528. break;
  6529. case WASM_OP_CALL:
  6530. #if WASM_ENABLE_TAIL_CALL != 0
  6531. case WASM_OP_RETURN_CALL:
  6532. #endif
  6533. skip_leb_uint32(p, p_end); /* funcidx */
  6534. break;
  6535. case WASM_OP_CALL_INDIRECT:
  6536. #if WASM_ENABLE_TAIL_CALL != 0
  6537. case WASM_OP_RETURN_CALL_INDIRECT:
  6538. #endif
  6539. skip_leb_uint32(p, p_end); /* typeidx */
  6540. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6541. skip_leb_uint32(p, p_end); /* tableidx */
  6542. #else
  6543. u8 = read_uint8(p); /* 0x00 */
  6544. #endif
  6545. break;
  6546. #if WASM_ENABLE_GC != 0
  6547. case WASM_OP_CALL_REF:
  6548. case WASM_OP_RETURN_CALL_REF:
  6549. skip_leb_uint32(p, p_end); /* typeidx */
  6550. break;
  6551. #endif
  6552. case WASM_OP_DROP:
  6553. case WASM_OP_SELECT:
  6554. case WASM_OP_DROP_64:
  6555. case WASM_OP_SELECT_64:
  6556. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_SIMD != 0
  6557. case WASM_OP_SELECT_128:
  6558. #endif
  6559. break;
  6560. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6561. case WASM_OP_SELECT_T:
  6562. {
  6563. skip_leb_uint32(p, p_end); /* vec length */
  6564. u8 = read_uint8(p); /* typeidx */
  6565. /* the possible extra bytes of GC ref type have been
  6566. modified to OP_NOP, no need to resolve them again */
  6567. break;
  6568. }
  6569. case WASM_OP_TABLE_GET:
  6570. case WASM_OP_TABLE_SET:
  6571. skip_leb_uint32(p, p_end); /* table index */
  6572. break;
  6573. case WASM_OP_REF_NULL:
  6574. {
  6575. u8 = read_uint8(p); /* type */
  6576. if (is_byte_a_type(u8)) {
  6577. #if WASM_ENABLE_GC != 0
  6578. if (wasm_is_type_multi_byte_type(u8)) {
  6579. /* the possible extra bytes of GC ref type have been
  6580. modified to OP_NOP, no need to resolve them again */
  6581. }
  6582. #endif
  6583. }
  6584. else {
  6585. p--;
  6586. skip_leb_uint32(p, p_end);
  6587. }
  6588. break;
  6589. }
  6590. case WASM_OP_REF_IS_NULL:
  6591. break;
  6592. case WASM_OP_REF_FUNC:
  6593. skip_leb_uint32(p, p_end); /* func index */
  6594. break;
  6595. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6596. #if WASM_ENABLE_GC != 0
  6597. case WASM_OP_REF_AS_NON_NULL:
  6598. case WASM_OP_REF_EQ:
  6599. break;
  6600. case WASM_OP_BR_ON_NULL:
  6601. case WASM_OP_BR_ON_NON_NULL:
  6602. skip_leb_uint32(p, p_end); /* label index */
  6603. break;
  6604. #endif /* end of WASM_ENABLE_GC != 0 */
  6605. case WASM_OP_GET_LOCAL:
  6606. case WASM_OP_SET_LOCAL:
  6607. case WASM_OP_TEE_LOCAL:
  6608. case WASM_OP_GET_GLOBAL:
  6609. case WASM_OP_SET_GLOBAL:
  6610. case WASM_OP_GET_GLOBAL_64:
  6611. case WASM_OP_SET_GLOBAL_64:
  6612. #if WASM_ENABLE_SIMDE != 0
  6613. case WASM_OP_GET_GLOBAL_V128:
  6614. case WASM_OP_SET_GLOBAL_V128:
  6615. #endif
  6616. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6617. skip_leb_uint32(p, p_end); /* local index */
  6618. break;
  6619. case EXT_OP_GET_LOCAL_FAST:
  6620. case EXT_OP_SET_LOCAL_FAST:
  6621. case EXT_OP_TEE_LOCAL_FAST:
  6622. CHECK_BUF(p, p_end, 1);
  6623. p++;
  6624. break;
  6625. case WASM_OP_I32_LOAD:
  6626. case WASM_OP_I64_LOAD:
  6627. case WASM_OP_F32_LOAD:
  6628. case WASM_OP_F64_LOAD:
  6629. case WASM_OP_I32_LOAD8_S:
  6630. case WASM_OP_I32_LOAD8_U:
  6631. case WASM_OP_I32_LOAD16_S:
  6632. case WASM_OP_I32_LOAD16_U:
  6633. case WASM_OP_I64_LOAD8_S:
  6634. case WASM_OP_I64_LOAD8_U:
  6635. case WASM_OP_I64_LOAD16_S:
  6636. case WASM_OP_I64_LOAD16_U:
  6637. case WASM_OP_I64_LOAD32_S:
  6638. case WASM_OP_I64_LOAD32_U:
  6639. case WASM_OP_I32_STORE:
  6640. case WASM_OP_I64_STORE:
  6641. case WASM_OP_F32_STORE:
  6642. case WASM_OP_F64_STORE:
  6643. case WASM_OP_I32_STORE8:
  6644. case WASM_OP_I32_STORE16:
  6645. case WASM_OP_I64_STORE8:
  6646. case WASM_OP_I64_STORE16:
  6647. case WASM_OP_I64_STORE32:
  6648. skip_leb_align(p, p_end); /* align */
  6649. skip_leb_mem_offset(p, p_end); /* offset */
  6650. break;
  6651. case WASM_OP_MEMORY_SIZE:
  6652. case WASM_OP_MEMORY_GROW:
  6653. skip_leb_memidx(p, p_end); /* memidx */
  6654. break;
  6655. case WASM_OP_I32_CONST:
  6656. skip_leb_int32(p, p_end);
  6657. break;
  6658. case WASM_OP_I64_CONST:
  6659. skip_leb_int64(p, p_end);
  6660. break;
  6661. case WASM_OP_F32_CONST:
  6662. p += sizeof(float32);
  6663. break;
  6664. case WASM_OP_F64_CONST:
  6665. p += sizeof(float64);
  6666. break;
  6667. case WASM_OP_I32_EQZ:
  6668. case WASM_OP_I32_EQ:
  6669. case WASM_OP_I32_NE:
  6670. case WASM_OP_I32_LT_S:
  6671. case WASM_OP_I32_LT_U:
  6672. case WASM_OP_I32_GT_S:
  6673. case WASM_OP_I32_GT_U:
  6674. case WASM_OP_I32_LE_S:
  6675. case WASM_OP_I32_LE_U:
  6676. case WASM_OP_I32_GE_S:
  6677. case WASM_OP_I32_GE_U:
  6678. case WASM_OP_I64_EQZ:
  6679. case WASM_OP_I64_EQ:
  6680. case WASM_OP_I64_NE:
  6681. case WASM_OP_I64_LT_S:
  6682. case WASM_OP_I64_LT_U:
  6683. case WASM_OP_I64_GT_S:
  6684. case WASM_OP_I64_GT_U:
  6685. case WASM_OP_I64_LE_S:
  6686. case WASM_OP_I64_LE_U:
  6687. case WASM_OP_I64_GE_S:
  6688. case WASM_OP_I64_GE_U:
  6689. case WASM_OP_F32_EQ:
  6690. case WASM_OP_F32_NE:
  6691. case WASM_OP_F32_LT:
  6692. case WASM_OP_F32_GT:
  6693. case WASM_OP_F32_LE:
  6694. case WASM_OP_F32_GE:
  6695. case WASM_OP_F64_EQ:
  6696. case WASM_OP_F64_NE:
  6697. case WASM_OP_F64_LT:
  6698. case WASM_OP_F64_GT:
  6699. case WASM_OP_F64_LE:
  6700. case WASM_OP_F64_GE:
  6701. case WASM_OP_I32_CLZ:
  6702. case WASM_OP_I32_CTZ:
  6703. case WASM_OP_I32_POPCNT:
  6704. case WASM_OP_I32_ADD:
  6705. case WASM_OP_I32_SUB:
  6706. case WASM_OP_I32_MUL:
  6707. case WASM_OP_I32_DIV_S:
  6708. case WASM_OP_I32_DIV_U:
  6709. case WASM_OP_I32_REM_S:
  6710. case WASM_OP_I32_REM_U:
  6711. case WASM_OP_I32_AND:
  6712. case WASM_OP_I32_OR:
  6713. case WASM_OP_I32_XOR:
  6714. case WASM_OP_I32_SHL:
  6715. case WASM_OP_I32_SHR_S:
  6716. case WASM_OP_I32_SHR_U:
  6717. case WASM_OP_I32_ROTL:
  6718. case WASM_OP_I32_ROTR:
  6719. case WASM_OP_I64_CLZ:
  6720. case WASM_OP_I64_CTZ:
  6721. case WASM_OP_I64_POPCNT:
  6722. case WASM_OP_I64_ADD:
  6723. case WASM_OP_I64_SUB:
  6724. case WASM_OP_I64_MUL:
  6725. case WASM_OP_I64_DIV_S:
  6726. case WASM_OP_I64_DIV_U:
  6727. case WASM_OP_I64_REM_S:
  6728. case WASM_OP_I64_REM_U:
  6729. case WASM_OP_I64_AND:
  6730. case WASM_OP_I64_OR:
  6731. case WASM_OP_I64_XOR:
  6732. case WASM_OP_I64_SHL:
  6733. case WASM_OP_I64_SHR_S:
  6734. case WASM_OP_I64_SHR_U:
  6735. case WASM_OP_I64_ROTL:
  6736. case WASM_OP_I64_ROTR:
  6737. case WASM_OP_F32_ABS:
  6738. case WASM_OP_F32_NEG:
  6739. case WASM_OP_F32_CEIL:
  6740. case WASM_OP_F32_FLOOR:
  6741. case WASM_OP_F32_TRUNC:
  6742. case WASM_OP_F32_NEAREST:
  6743. case WASM_OP_F32_SQRT:
  6744. case WASM_OP_F32_ADD:
  6745. case WASM_OP_F32_SUB:
  6746. case WASM_OP_F32_MUL:
  6747. case WASM_OP_F32_DIV:
  6748. case WASM_OP_F32_MIN:
  6749. case WASM_OP_F32_MAX:
  6750. case WASM_OP_F32_COPYSIGN:
  6751. case WASM_OP_F64_ABS:
  6752. case WASM_OP_F64_NEG:
  6753. case WASM_OP_F64_CEIL:
  6754. case WASM_OP_F64_FLOOR:
  6755. case WASM_OP_F64_TRUNC:
  6756. case WASM_OP_F64_NEAREST:
  6757. case WASM_OP_F64_SQRT:
  6758. case WASM_OP_F64_ADD:
  6759. case WASM_OP_F64_SUB:
  6760. case WASM_OP_F64_MUL:
  6761. case WASM_OP_F64_DIV:
  6762. case WASM_OP_F64_MIN:
  6763. case WASM_OP_F64_MAX:
  6764. case WASM_OP_F64_COPYSIGN:
  6765. case WASM_OP_I32_WRAP_I64:
  6766. case WASM_OP_I32_TRUNC_S_F32:
  6767. case WASM_OP_I32_TRUNC_U_F32:
  6768. case WASM_OP_I32_TRUNC_S_F64:
  6769. case WASM_OP_I32_TRUNC_U_F64:
  6770. case WASM_OP_I64_EXTEND_S_I32:
  6771. case WASM_OP_I64_EXTEND_U_I32:
  6772. case WASM_OP_I64_TRUNC_S_F32:
  6773. case WASM_OP_I64_TRUNC_U_F32:
  6774. case WASM_OP_I64_TRUNC_S_F64:
  6775. case WASM_OP_I64_TRUNC_U_F64:
  6776. case WASM_OP_F32_CONVERT_S_I32:
  6777. case WASM_OP_F32_CONVERT_U_I32:
  6778. case WASM_OP_F32_CONVERT_S_I64:
  6779. case WASM_OP_F32_CONVERT_U_I64:
  6780. case WASM_OP_F32_DEMOTE_F64:
  6781. case WASM_OP_F64_CONVERT_S_I32:
  6782. case WASM_OP_F64_CONVERT_U_I32:
  6783. case WASM_OP_F64_CONVERT_S_I64:
  6784. case WASM_OP_F64_CONVERT_U_I64:
  6785. case WASM_OP_F64_PROMOTE_F32:
  6786. case WASM_OP_I32_REINTERPRET_F32:
  6787. case WASM_OP_I64_REINTERPRET_F64:
  6788. case WASM_OP_F32_REINTERPRET_I32:
  6789. case WASM_OP_F64_REINTERPRET_I64:
  6790. case WASM_OP_I32_EXTEND8_S:
  6791. case WASM_OP_I32_EXTEND16_S:
  6792. case WASM_OP_I64_EXTEND8_S:
  6793. case WASM_OP_I64_EXTEND16_S:
  6794. case WASM_OP_I64_EXTEND32_S:
  6795. break;
  6796. #if WASM_ENABLE_GC != 0
  6797. case WASM_OP_GC_PREFIX:
  6798. {
  6799. uint32 opcode1;
  6800. read_leb_uint32(p, p_end, opcode1);
  6801. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6802. is no larger than UINT8_MAX */
  6803. opcode = (uint8)opcode1;
  6804. switch (opcode) {
  6805. case WASM_OP_STRUCT_NEW:
  6806. case WASM_OP_STRUCT_NEW_DEFAULT:
  6807. skip_leb_uint32(p, p_end); /* typeidx */
  6808. break;
  6809. case WASM_OP_STRUCT_GET:
  6810. case WASM_OP_STRUCT_GET_S:
  6811. case WASM_OP_STRUCT_GET_U:
  6812. case WASM_OP_STRUCT_SET:
  6813. skip_leb_uint32(p, p_end); /* typeidx */
  6814. skip_leb_uint32(p, p_end); /* fieldidx */
  6815. break;
  6816. case WASM_OP_ARRAY_NEW:
  6817. case WASM_OP_ARRAY_NEW_DEFAULT:
  6818. case WASM_OP_ARRAY_GET:
  6819. case WASM_OP_ARRAY_GET_S:
  6820. case WASM_OP_ARRAY_GET_U:
  6821. case WASM_OP_ARRAY_SET:
  6822. case WASM_OP_ARRAY_FILL:
  6823. skip_leb_uint32(p, p_end); /* typeidx */
  6824. break;
  6825. case WASM_OP_ARRAY_COPY:
  6826. skip_leb_uint32(p, p_end); /* typeidx1 */
  6827. skip_leb_uint32(p, p_end); /* typeidx2 */
  6828. break;
  6829. case WASM_OP_ARRAY_LEN:
  6830. break;
  6831. case WASM_OP_ARRAY_NEW_FIXED:
  6832. case WASM_OP_ARRAY_NEW_DATA:
  6833. case WASM_OP_ARRAY_NEW_ELEM:
  6834. skip_leb_uint32(p, p_end); /* typeidx */
  6835. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6836. break;
  6837. case WASM_OP_REF_I31:
  6838. case WASM_OP_I31_GET_S:
  6839. case WASM_OP_I31_GET_U:
  6840. break;
  6841. case WASM_OP_REF_TEST:
  6842. case WASM_OP_REF_CAST:
  6843. case WASM_OP_REF_TEST_NULLABLE:
  6844. case WASM_OP_REF_CAST_NULLABLE:
  6845. skip_leb_int32(p, p_end); /* heaptype */
  6846. break;
  6847. case WASM_OP_BR_ON_CAST:
  6848. case WASM_OP_BR_ON_CAST_FAIL:
  6849. p += sizeof(uint8); /* castflag */
  6850. skip_leb_uint32(p, p_end); /* labelidx */
  6851. skip_leb_int32(p, p_end); /* heaptype */
  6852. skip_leb_int32(p, p_end); /* heaptype2 */
  6853. break;
  6854. case WASM_OP_ANY_CONVERT_EXTERN:
  6855. case WASM_OP_EXTERN_CONVERT_ANY:
  6856. break;
  6857. #if WASM_ENABLE_STRINGREF != 0
  6858. case WASM_OP_STRING_NEW_UTF8:
  6859. case WASM_OP_STRING_NEW_WTF16:
  6860. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6861. case WASM_OP_STRING_NEW_WTF8:
  6862. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6863. break;
  6864. case WASM_OP_STRING_CONST:
  6865. skip_leb_int32(p, p_end); /* contents */
  6866. break;
  6867. case WASM_OP_STRING_MEASURE_UTF8:
  6868. case WASM_OP_STRING_MEASURE_WTF8:
  6869. case WASM_OP_STRING_MEASURE_WTF16:
  6870. break;
  6871. case WASM_OP_STRING_ENCODE_UTF8:
  6872. case WASM_OP_STRING_ENCODE_WTF16:
  6873. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6874. case WASM_OP_STRING_ENCODE_WTF8:
  6875. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6876. break;
  6877. case WASM_OP_STRING_CONCAT:
  6878. case WASM_OP_STRING_EQ:
  6879. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6880. case WASM_OP_STRING_AS_WTF8:
  6881. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6882. break;
  6883. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6884. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6885. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6886. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6887. break;
  6888. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6889. case WASM_OP_STRING_AS_WTF16:
  6890. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6891. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6892. break;
  6893. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6894. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6895. break;
  6896. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6897. case WASM_OP_STRING_AS_ITER:
  6898. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6899. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6900. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6901. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6902. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6903. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6904. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6905. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6906. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6907. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6908. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6909. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6910. break;
  6911. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6912. default:
  6913. return false;
  6914. }
  6915. break;
  6916. }
  6917. #endif /* end of WASM_ENABLE_GC != 0 */
  6918. case WASM_OP_MISC_PREFIX:
  6919. {
  6920. uint32 opcode1;
  6921. read_leb_uint32(p, p_end, opcode1);
  6922. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6923. is no larger than UINT8_MAX */
  6924. opcode = (uint8)opcode1;
  6925. switch (opcode) {
  6926. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6927. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6928. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6929. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6930. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6931. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6932. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6933. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6934. break;
  6935. #if WASM_ENABLE_BULK_MEMORY != 0
  6936. case WASM_OP_MEMORY_INIT:
  6937. skip_leb_uint32(p, p_end);
  6938. skip_leb_memidx(p, p_end);
  6939. break;
  6940. case WASM_OP_DATA_DROP:
  6941. skip_leb_uint32(p, p_end);
  6942. break;
  6943. case WASM_OP_MEMORY_COPY:
  6944. skip_leb_memidx(p, p_end);
  6945. skip_leb_memidx(p, p_end);
  6946. break;
  6947. case WASM_OP_MEMORY_FILL:
  6948. skip_leb_memidx(p, p_end);
  6949. break;
  6950. #endif /* WASM_ENABLE_BULK_MEMORY */
  6951. #if WASM_ENABLE_REF_TYPES != 0
  6952. case WASM_OP_TABLE_INIT:
  6953. case WASM_OP_TABLE_COPY:
  6954. /* tableidx */
  6955. skip_leb_uint32(p, p_end);
  6956. /* elemidx */
  6957. skip_leb_uint32(p, p_end);
  6958. break;
  6959. case WASM_OP_ELEM_DROP:
  6960. /* elemidx */
  6961. skip_leb_uint32(p, p_end);
  6962. break;
  6963. case WASM_OP_TABLE_SIZE:
  6964. case WASM_OP_TABLE_GROW:
  6965. case WASM_OP_TABLE_FILL:
  6966. skip_leb_uint32(p, p_end); /* table idx */
  6967. break;
  6968. #endif /* WASM_ENABLE_REF_TYPES */
  6969. default:
  6970. return false;
  6971. }
  6972. break;
  6973. }
  6974. #if WASM_ENABLE_SIMD != 0
  6975. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  6976. || (WASM_ENABLE_FAST_INTERP != 0)
  6977. case WASM_OP_SIMD_PREFIX:
  6978. {
  6979. uint32 opcode1;
  6980. read_leb_uint32(p, p_end, opcode1);
  6981. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6982. is no larger than UINT8_MAX */
  6983. opcode = (uint8)opcode1;
  6984. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6985. */
  6986. switch (opcode) {
  6987. case SIMD_v128_load:
  6988. case SIMD_v128_load8x8_s:
  6989. case SIMD_v128_load8x8_u:
  6990. case SIMD_v128_load16x4_s:
  6991. case SIMD_v128_load16x4_u:
  6992. case SIMD_v128_load32x2_s:
  6993. case SIMD_v128_load32x2_u:
  6994. case SIMD_v128_load8_splat:
  6995. case SIMD_v128_load16_splat:
  6996. case SIMD_v128_load32_splat:
  6997. case SIMD_v128_load64_splat:
  6998. case SIMD_v128_store:
  6999. /* memarg align */
  7000. skip_leb_uint32(p, p_end);
  7001. /* memarg offset */
  7002. skip_leb_mem_offset(p, p_end);
  7003. break;
  7004. case SIMD_v128_const:
  7005. case SIMD_v8x16_shuffle:
  7006. /* immByte[16] immLaneId[16] */
  7007. CHECK_BUF1(p, p_end, 16);
  7008. p += 16;
  7009. break;
  7010. case SIMD_i8x16_extract_lane_s:
  7011. case SIMD_i8x16_extract_lane_u:
  7012. case SIMD_i8x16_replace_lane:
  7013. case SIMD_i16x8_extract_lane_s:
  7014. case SIMD_i16x8_extract_lane_u:
  7015. case SIMD_i16x8_replace_lane:
  7016. case SIMD_i32x4_extract_lane:
  7017. case SIMD_i32x4_replace_lane:
  7018. case SIMD_i64x2_extract_lane:
  7019. case SIMD_i64x2_replace_lane:
  7020. case SIMD_f32x4_extract_lane:
  7021. case SIMD_f32x4_replace_lane:
  7022. case SIMD_f64x2_extract_lane:
  7023. case SIMD_f64x2_replace_lane:
  7024. /* ImmLaneId */
  7025. CHECK_BUF(p, p_end, 1);
  7026. p++;
  7027. break;
  7028. case SIMD_v128_load8_lane:
  7029. case SIMD_v128_load16_lane:
  7030. case SIMD_v128_load32_lane:
  7031. case SIMD_v128_load64_lane:
  7032. case SIMD_v128_store8_lane:
  7033. case SIMD_v128_store16_lane:
  7034. case SIMD_v128_store32_lane:
  7035. case SIMD_v128_store64_lane:
  7036. /* memarg align */
  7037. skip_leb_uint32(p, p_end);
  7038. /* memarg offset */
  7039. skip_leb_mem_offset(p, p_end);
  7040. /* ImmLaneId */
  7041. CHECK_BUF(p, p_end, 1);
  7042. p++;
  7043. break;
  7044. case SIMD_v128_load32_zero:
  7045. case SIMD_v128_load64_zero:
  7046. /* memarg align */
  7047. skip_leb_uint32(p, p_end);
  7048. /* memarg offset */
  7049. skip_leb_mem_offset(p, p_end);
  7050. break;
  7051. default:
  7052. /*
  7053. * since latest SIMD specific used almost every value
  7054. * from 0x00 to 0xff, the default branch will present
  7055. * all opcodes without imm
  7056. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  7057. */
  7058. break;
  7059. }
  7060. break;
  7061. }
  7062. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  7063. (WASM_ENABLE_FAST_INTERP != 0) */
  7064. #endif /* end of WASM_ENABLE_SIMD */
  7065. #if WASM_ENABLE_SHARED_MEMORY != 0
  7066. case WASM_OP_ATOMIC_PREFIX:
  7067. {
  7068. uint32 opcode1;
  7069. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  7070. read_leb_uint32(p, p_end, opcode1);
  7071. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7072. is no larger than UINT8_MAX */
  7073. opcode = (uint8)opcode1;
  7074. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7075. skip_leb_uint32(p, p_end); /* align */
  7076. skip_leb_mem_offset(p, p_end); /* offset */
  7077. }
  7078. else {
  7079. /* atomic.fence doesn't have memarg */
  7080. p++;
  7081. }
  7082. break;
  7083. }
  7084. #endif
  7085. #if WASM_ENABLE_DEBUG_INTERP != 0
  7086. case DEBUG_OP_BREAK:
  7087. {
  7088. WASMDebugInstance *debug_instance =
  7089. wasm_exec_env_get_instance(exec_env);
  7090. char original_opcode[1];
  7091. uint64 size = 1;
  7092. WASMModuleInstance *module_inst =
  7093. (WASMModuleInstance *)exec_env->module_inst;
  7094. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7095. ? (p - 1) - module_inst->module->load_addr
  7096. : ~0;
  7097. if (debug_instance) {
  7098. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7099. original_opcode, &size)
  7100. && size == 1) {
  7101. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7102. "with %02x: ",
  7103. original_opcode[0]);
  7104. opcode = original_opcode[0];
  7105. goto op_break_retry;
  7106. }
  7107. }
  7108. break;
  7109. }
  7110. #endif
  7111. default:
  7112. return false;
  7113. }
  7114. }
  7115. (void)u8;
  7116. (void)exec_env;
  7117. return false;
  7118. fail:
  7119. return false;
  7120. }
  7121. #if WASM_ENABLE_FAST_INTERP != 0
  7122. #if WASM_DEBUG_PREPROCESSOR != 0
  7123. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7124. #else
  7125. #define LOG_OP(...) (void)0
  7126. #endif
  7127. #define PATCH_ELSE 0
  7128. #define PATCH_END 1
  7129. typedef struct BranchBlockPatch {
  7130. struct BranchBlockPatch *next;
  7131. uint8 patch_type;
  7132. uint8 *code_compiled;
  7133. } BranchBlockPatch;
  7134. #endif
  7135. typedef struct BranchBlock {
  7136. uint8 label_type;
  7137. BlockType block_type;
  7138. uint8 *start_addr;
  7139. uint8 *else_addr;
  7140. uint8 *end_addr;
  7141. uint32 stack_cell_num;
  7142. #if WASM_ENABLE_GC != 0
  7143. uint32 reftype_map_num;
  7144. /* Indicate which local is used inside current block, used to validate
  7145. * local.get with non-nullable ref types */
  7146. uint8 *local_use_mask;
  7147. uint32 local_use_mask_size;
  7148. #endif
  7149. #if WASM_ENABLE_FAST_INTERP != 0
  7150. uint16 dynamic_offset;
  7151. uint8 *code_compiled;
  7152. BranchBlockPatch *patch_list;
  7153. /* This is used to save params frame_offset of of if block */
  7154. int16 *param_frame_offsets;
  7155. /* This is used to recover the dynamic offset for else branch,
  7156. * and also to remember the start offset of dynamic space which
  7157. * stores the block arguments for loop block, so we can use it
  7158. * to copy the stack operands to the loop block's arguments in
  7159. * wasm_loader_emit_br_info for opcode br. */
  7160. uint16 start_dynamic_offset;
  7161. #endif
  7162. /* Indicate the operand stack is in polymorphic state.
  7163. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7164. * to polymorphic state until the block's 'end' opcode is processed.
  7165. * If stack is in polymorphic state and stack is empty, instruction can
  7166. * pop any type of value directly without decreasing stack top pointer
  7167. * and stack cell num. */
  7168. bool is_stack_polymorphic;
  7169. } BranchBlock;
  7170. typedef struct WASMLoaderContext {
  7171. /* frame ref stack */
  7172. uint8 *frame_ref;
  7173. uint8 *frame_ref_bottom;
  7174. uint8 *frame_ref_boundary;
  7175. uint32 frame_ref_size;
  7176. uint32 stack_cell_num;
  7177. uint32 max_stack_cell_num;
  7178. #if WASM_ENABLE_GC != 0
  7179. /* frame reftype map stack */
  7180. WASMRefTypeMap *frame_reftype_map;
  7181. WASMRefTypeMap *frame_reftype_map_bottom;
  7182. WASMRefTypeMap *frame_reftype_map_boundary;
  7183. uint32 frame_reftype_map_size;
  7184. uint32 reftype_map_num;
  7185. uint32 max_reftype_map_num;
  7186. /* Current module */
  7187. WASMModule *module;
  7188. /* Current module's ref_type_set */
  7189. HashMap *ref_type_set;
  7190. /* Always point to local variable ref_type of
  7191. wasm_loader_prepare_bytecode */
  7192. WASMRefType *ref_type_tmp;
  7193. #endif
  7194. /* frame csp stack */
  7195. BranchBlock *frame_csp;
  7196. BranchBlock *frame_csp_bottom;
  7197. BranchBlock *frame_csp_boundary;
  7198. uint32 frame_csp_size;
  7199. uint32 csp_num;
  7200. uint32 max_csp_num;
  7201. #if WASM_ENABLE_FAST_INTERP != 0
  7202. /* frame offset stack */
  7203. int16 *frame_offset;
  7204. int16 *frame_offset_bottom;
  7205. int16 *frame_offset_boundary;
  7206. uint32 frame_offset_size;
  7207. int16 dynamic_offset;
  7208. int16 start_dynamic_offset;
  7209. int16 max_dynamic_offset;
  7210. /* preserved local offset */
  7211. int16 preserved_local_offset;
  7212. /* const buffer for i64 and f64 consts, note that the raw bytes
  7213. * of i64 and f64 are the same, so we read an i64 value from an
  7214. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7215. int64 *i64_consts;
  7216. uint32 i64_const_max_num;
  7217. uint32 i64_const_num;
  7218. /* const buffer for i32 and f32 consts */
  7219. int32 *i32_consts;
  7220. uint32 i32_const_max_num;
  7221. uint32 i32_const_num;
  7222. /* const buffer for V128 */
  7223. V128 *v128_consts;
  7224. uint32 v128_const_max_num;
  7225. uint32 v128_const_num;
  7226. /* processed code */
  7227. uint8 *p_code_compiled;
  7228. uint8 *p_code_compiled_end;
  7229. uint32 code_compiled_size;
  7230. /* If the last opcode will be dropped, the peak memory usage will be larger
  7231. * than the final code_compiled_size, we record the peak size to ensure
  7232. * there will not be invalid memory access during second traverse */
  7233. uint32 code_compiled_peak_size;
  7234. #endif
  7235. } WASMLoaderContext;
  7236. #define CHECK_CSP_PUSH() \
  7237. do { \
  7238. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7239. MEM_REALLOC( \
  7240. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7241. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7242. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7243. ctx->frame_csp_boundary = \
  7244. ctx->frame_csp_bottom \
  7245. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7246. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7247. } \
  7248. } while (0)
  7249. #define CHECK_CSP_POP() \
  7250. do { \
  7251. if (ctx->csp_num < 1) { \
  7252. set_error_buf(error_buf, error_buf_size, \
  7253. "type mismatch: " \
  7254. "expect data but block stack was empty"); \
  7255. goto fail; \
  7256. } \
  7257. } while (0)
  7258. #if WASM_ENABLE_FAST_INTERP != 0
  7259. static bool
  7260. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7261. uint32 error_buf_size)
  7262. {
  7263. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7264. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7265. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7266. ctx->frame_offset_size + 16);
  7267. ctx->frame_offset_size += 16;
  7268. ctx->frame_offset_boundary =
  7269. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7270. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7271. }
  7272. return true;
  7273. fail:
  7274. return false;
  7275. }
  7276. static bool
  7277. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7278. {
  7279. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7280. return false;
  7281. return true;
  7282. }
  7283. static void
  7284. free_label_patch_list(BranchBlock *frame_csp)
  7285. {
  7286. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7287. BranchBlockPatch *next;
  7288. while (label_patch != NULL) {
  7289. next = label_patch->next;
  7290. wasm_runtime_free(label_patch);
  7291. label_patch = next;
  7292. }
  7293. frame_csp->patch_list = NULL;
  7294. }
  7295. static void
  7296. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7297. {
  7298. BranchBlock *tmp_csp = frame_csp;
  7299. uint32 i;
  7300. for (i = 0; i < csp_num; i++) {
  7301. free_label_patch_list(tmp_csp);
  7302. tmp_csp++;
  7303. }
  7304. }
  7305. static void
  7306. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7307. {
  7308. BranchBlock *tmp_csp = frame_csp;
  7309. uint32 i;
  7310. for (i = 0; i < csp_num; i++) {
  7311. if (tmp_csp->param_frame_offsets)
  7312. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7313. tmp_csp++;
  7314. }
  7315. }
  7316. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7317. #if WASM_ENABLE_GC != 0
  7318. static bool
  7319. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7320. char *error_buf, uint32 error_buf_size)
  7321. {
  7322. BranchBlock *current_csp = ctx->frame_csp - 1;
  7323. uint32 local_mask_size;
  7324. if (local_count == 0) {
  7325. current_csp->local_use_mask_size = 0;
  7326. return true;
  7327. }
  7328. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7329. * else branch, we don't need to allocate memory again */
  7330. if (!current_csp->local_use_mask) {
  7331. local_mask_size = (local_count + 7) / sizeof(uint8);
  7332. if (!(current_csp->local_use_mask =
  7333. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7334. return false;
  7335. }
  7336. current_csp->local_use_mask_size = local_mask_size;
  7337. }
  7338. else {
  7339. local_mask_size = current_csp->local_use_mask_size;
  7340. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7341. }
  7342. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7343. /* For non-function blocks, inherit the use status from parent block */
  7344. BranchBlock *parent_csp = current_csp - 1;
  7345. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7346. bh_assert(parent_csp->local_use_mask);
  7347. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7348. parent_csp->local_use_mask, local_mask_size);
  7349. }
  7350. return true;
  7351. }
  7352. static void
  7353. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7354. {
  7355. BranchBlock *current_csp = ctx->frame_csp - 1;
  7356. bh_assert(current_csp->local_use_mask
  7357. || current_csp->local_use_mask_size == 0);
  7358. if (current_csp->local_use_mask) {
  7359. wasm_runtime_free(current_csp->local_use_mask);
  7360. }
  7361. current_csp->local_use_mask = NULL;
  7362. current_csp->local_use_mask_size = 0;
  7363. }
  7364. static void
  7365. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7366. {
  7367. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7368. uint32 i;
  7369. for (i = 0; i < ctx->csp_num; i++) {
  7370. if (tmp_csp->local_use_mask) {
  7371. wasm_runtime_free(tmp_csp->local_use_mask);
  7372. tmp_csp->local_use_mask = NULL;
  7373. tmp_csp->local_use_mask_size = 0;
  7374. }
  7375. tmp_csp++;
  7376. }
  7377. }
  7378. static void
  7379. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7380. {
  7381. BranchBlock *current_csp = ctx->frame_csp - 1;
  7382. uint32 byte_offset = index / sizeof(uint8);
  7383. uint32 bit_offset = index % sizeof(uint8);
  7384. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7385. bh_assert(current_csp->local_use_mask);
  7386. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7387. }
  7388. static bool
  7389. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7390. {
  7391. BranchBlock *current_csp = ctx->frame_csp - 1;
  7392. uint32 byte_offset = index / sizeof(uint8);
  7393. uint32 bit_offset = index % sizeof(uint8);
  7394. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7395. bh_assert(current_csp->local_use_mask);
  7396. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7397. ? true
  7398. : false;
  7399. }
  7400. #endif /* end of WASM_ENABLE_GC != 0 */
  7401. static void
  7402. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7403. {
  7404. if (ctx) {
  7405. if (ctx->frame_ref_bottom)
  7406. wasm_runtime_free(ctx->frame_ref_bottom);
  7407. #if WASM_ENABLE_GC != 0
  7408. if (ctx->frame_reftype_map_bottom)
  7409. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7410. #endif
  7411. if (ctx->frame_csp_bottom) {
  7412. #if WASM_ENABLE_FAST_INTERP != 0
  7413. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7414. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7415. ctx->csp_num);
  7416. #endif
  7417. #if WASM_ENABLE_GC != 0
  7418. wasm_loader_clean_all_local_use_masks(ctx);
  7419. #endif
  7420. wasm_runtime_free(ctx->frame_csp_bottom);
  7421. }
  7422. #if WASM_ENABLE_FAST_INTERP != 0
  7423. if (ctx->frame_offset_bottom)
  7424. wasm_runtime_free(ctx->frame_offset_bottom);
  7425. if (ctx->i64_consts)
  7426. wasm_runtime_free(ctx->i64_consts);
  7427. if (ctx->i32_consts)
  7428. wasm_runtime_free(ctx->i32_consts);
  7429. if (ctx->v128_consts)
  7430. wasm_runtime_free(ctx->v128_consts);
  7431. #endif
  7432. wasm_runtime_free(ctx);
  7433. }
  7434. }
  7435. static WASMLoaderContext *
  7436. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7437. {
  7438. WASMLoaderContext *loader_ctx =
  7439. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7440. if (!loader_ctx)
  7441. return NULL;
  7442. loader_ctx->frame_ref_size = 32;
  7443. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7444. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7445. goto fail;
  7446. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7447. #if WASM_ENABLE_GC != 0
  7448. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7449. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7450. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7451. error_buf_size)))
  7452. goto fail;
  7453. loader_ctx->frame_reftype_map_boundary =
  7454. loader_ctx->frame_reftype_map_bottom + 16;
  7455. #endif
  7456. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7457. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7458. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7459. goto fail;
  7460. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7461. #if WASM_ENABLE_EXCE_HANDLING != 0
  7462. func->exception_handler_count = 0;
  7463. #endif
  7464. #if WASM_ENABLE_FAST_INTERP != 0
  7465. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7466. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7467. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7468. error_buf_size)))
  7469. goto fail;
  7470. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7471. loader_ctx->i64_const_max_num = 8;
  7472. if (!(loader_ctx->i64_consts =
  7473. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7474. error_buf, error_buf_size)))
  7475. goto fail;
  7476. loader_ctx->i32_const_max_num = 8;
  7477. if (!(loader_ctx->i32_consts =
  7478. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7479. error_buf, error_buf_size)))
  7480. goto fail;
  7481. loader_ctx->v128_const_max_num = 8;
  7482. if (!(loader_ctx->v128_consts =
  7483. loader_malloc(sizeof(V128) * loader_ctx->v128_const_max_num,
  7484. error_buf, error_buf_size)))
  7485. goto fail;
  7486. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7487. set_error_buf(error_buf, error_buf_size,
  7488. "fast interpreter offset overflow");
  7489. goto fail;
  7490. }
  7491. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7492. loader_ctx->max_dynamic_offset =
  7493. func->param_cell_num + func->local_cell_num;
  7494. #endif
  7495. return loader_ctx;
  7496. fail:
  7497. wasm_loader_ctx_destroy(loader_ctx);
  7498. return NULL;
  7499. }
  7500. static bool
  7501. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7502. uint32 error_buf_size)
  7503. {
  7504. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7505. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7506. /* Increase the frame ref stack */
  7507. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7508. ctx->frame_ref_size + 16);
  7509. ctx->frame_ref_size += 16;
  7510. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7511. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7512. }
  7513. #if WASM_ENABLE_GC != 0
  7514. if (wasm_is_type_multi_byte_type(type)
  7515. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7516. /* Increase the frame reftype map stack */
  7517. bh_assert(
  7518. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7519. * sizeof(WASMRefTypeMap))
  7520. == ctx->frame_reftype_map_size);
  7521. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7522. ctx->frame_reftype_map_size
  7523. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7524. ctx->frame_reftype_map =
  7525. ctx->frame_reftype_map_bottom
  7526. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7527. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7528. ctx->frame_reftype_map_boundary =
  7529. ctx->frame_reftype_map_bottom
  7530. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7531. }
  7532. #endif
  7533. return true;
  7534. fail:
  7535. return false;
  7536. }
  7537. static bool
  7538. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7539. uint32 error_buf_size)
  7540. {
  7541. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7542. uint32 i;
  7543. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7544. return false;
  7545. #if WASM_ENABLE_GC != 0
  7546. if (wasm_is_type_multi_byte_type(type)) {
  7547. WASMRefType *ref_type;
  7548. if (!(ref_type =
  7549. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7550. error_buf, error_buf_size))) {
  7551. return false;
  7552. }
  7553. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7554. /* Increase the frame reftype map stack */
  7555. bh_assert((uint32)((ctx->frame_reftype_map
  7556. - ctx->frame_reftype_map_bottom)
  7557. * sizeof(WASMRefTypeMap))
  7558. == ctx->frame_reftype_map_size);
  7559. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7560. ctx->frame_reftype_map_size,
  7561. ctx->frame_reftype_map_size
  7562. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7563. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7564. + ctx->frame_reftype_map_size
  7565. / ((uint32)sizeof(WASMRefTypeMap));
  7566. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7567. ctx->frame_reftype_map_boundary =
  7568. ctx->frame_reftype_map_bottom
  7569. + ctx->frame_reftype_map_size
  7570. / ((uint32)sizeof(WASMRefTypeMap));
  7571. }
  7572. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7573. ctx->frame_reftype_map->ref_type = ref_type;
  7574. ctx->frame_reftype_map++;
  7575. ctx->reftype_map_num++;
  7576. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7577. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7578. }
  7579. #endif
  7580. for (i = 0; i < type_cell_num; i++)
  7581. *ctx->frame_ref++ = type;
  7582. ctx->stack_cell_num += type_cell_num;
  7583. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7584. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7585. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7586. set_error_buf(error_buf, error_buf_size,
  7587. "operand stack depth limit exceeded");
  7588. return false;
  7589. }
  7590. }
  7591. return true;
  7592. #if WASM_ENABLE_GC != 0
  7593. fail:
  7594. return false;
  7595. #endif
  7596. }
  7597. static bool
  7598. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7599. int32 stack_cell_num,
  7600. #if WASM_ENABLE_GC != 0
  7601. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7602. #endif
  7603. uint8 type,
  7604. #if WASM_ENABLE_GC != 0
  7605. WASMRefType *ref_type,
  7606. #endif
  7607. char *error_buf, uint32 error_buf_size)
  7608. {
  7609. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7610. #if WASM_ENABLE_GC != 0
  7611. WASMRefType *frame_reftype = NULL;
  7612. #endif
  7613. if (stack_cell_num < type_cell_num) {
  7614. set_error_buf(error_buf, error_buf_size,
  7615. "type mismatch: expect data but stack was empty");
  7616. return false;
  7617. }
  7618. #if WASM_ENABLE_GC == 0
  7619. for (i = 0; i < type_cell_num; i++) {
  7620. if (*(frame_ref - 1 - i) != type) {
  7621. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7622. "type mismatch: expect ", type2str(type),
  7623. " but got other");
  7624. return false;
  7625. }
  7626. }
  7627. #else
  7628. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7629. bh_assert(reftype_map_num > 0);
  7630. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7631. }
  7632. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7633. ref_type, ctx->module->types,
  7634. ctx->module->type_count)) {
  7635. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7636. "type mismatch: expect ", type2str(type),
  7637. " but got other");
  7638. return false;
  7639. }
  7640. for (i = 0; i < type_cell_num - 1; i++) {
  7641. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7642. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7643. "type mismatch: expect ", type2str(type),
  7644. " but got other");
  7645. return false;
  7646. }
  7647. }
  7648. #endif
  7649. return true;
  7650. }
  7651. static bool
  7652. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7653. uint32 error_buf_size)
  7654. {
  7655. int32 block_stack_cell_num =
  7656. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7657. #if WASM_ENABLE_GC != 0
  7658. int32 reftype_map_num =
  7659. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7660. #endif
  7661. if (block_stack_cell_num > 0) {
  7662. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7663. /* the stack top is a value of any type, return success */
  7664. return true;
  7665. }
  7666. #if WASM_ENABLE_GC != 0
  7667. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7668. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7669. WASMRefType *stack_top_ref_type = NULL;
  7670. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7671. bh_assert(reftype_map_num > 0);
  7672. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7673. }
  7674. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7675. ctx->ref_type_tmp, ctx->module->types,
  7676. ctx->module->type_count)) {
  7677. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7678. uint32 ref_type_struct_size =
  7679. wasm_reftype_struct_size(stack_top_ref_type);
  7680. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7681. stack_top_ref_type, ref_type_struct_size);
  7682. }
  7683. return true;
  7684. }
  7685. }
  7686. #endif
  7687. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7688. #if WASM_ENABLE_GC != 0
  7689. ctx->frame_reftype_map, reftype_map_num,
  7690. #endif
  7691. type,
  7692. #if WASM_ENABLE_GC != 0
  7693. ctx->ref_type_tmp,
  7694. #endif
  7695. error_buf, error_buf_size)) {
  7696. return false;
  7697. }
  7698. return true;
  7699. }
  7700. static bool
  7701. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7702. uint32 error_buf_size)
  7703. {
  7704. BranchBlock *cur_block = ctx->frame_csp - 1;
  7705. int32 available_stack_cell =
  7706. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7707. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7708. /* Directly return success if current block is in stack
  7709. polymorphic state while stack is empty. */
  7710. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7711. return true;
  7712. if (type == VALUE_TYPE_VOID)
  7713. return true;
  7714. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7715. return false;
  7716. bh_assert(available_stack_cell > 0);
  7717. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7718. type = VALUE_TYPE_ANY;
  7719. cell_num_to_pop = 1;
  7720. }
  7721. ctx->frame_ref -= cell_num_to_pop;
  7722. ctx->stack_cell_num -= cell_num_to_pop;
  7723. #if WASM_ENABLE_GC != 0
  7724. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7725. ctx->frame_reftype_map--;
  7726. ctx->reftype_map_num--;
  7727. }
  7728. #endif
  7729. return true;
  7730. }
  7731. #if WASM_ENABLE_GC != 0
  7732. /* Get the stack top element of current block */
  7733. static bool
  7734. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7735. WASMRefType **p_ref_type, char *error_buf,
  7736. uint32 error_buf_size)
  7737. {
  7738. BranchBlock *cur_block = ctx->frame_csp - 1;
  7739. int32 available_stack_cell =
  7740. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7741. if (available_stack_cell <= 0) {
  7742. /* Directly return success if current block is in stack
  7743. polymorphic state while stack is empty. */
  7744. if (cur_block->is_stack_polymorphic) {
  7745. *p_type = VALUE_TYPE_ANY;
  7746. return true;
  7747. }
  7748. else {
  7749. set_error_buf(
  7750. error_buf, error_buf_size,
  7751. "type mismatch: expect data but block stack was empty");
  7752. return false;
  7753. }
  7754. }
  7755. *p_type = *(ctx->frame_ref - 1);
  7756. if (wasm_is_type_multi_byte_type(*p_type)) {
  7757. int32 available_reftype_map =
  7758. (int32)(ctx->reftype_map_num
  7759. - (ctx->frame_csp - 1)->reftype_map_num);
  7760. bh_assert(available_reftype_map > 0);
  7761. (void)available_reftype_map;
  7762. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7763. }
  7764. return true;
  7765. }
  7766. #if WASM_ENABLE_FAST_INTERP != 0
  7767. static bool
  7768. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7769. char *error_buf, uint32 error_buf_size);
  7770. #endif
  7771. /* Check whether the stack top elem is a heap object, and if yes,
  7772. pop and return it */
  7773. static bool
  7774. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7775. WASMRefType *ref_ht_ret, char *error_buf,
  7776. uint32 error_buf_size)
  7777. {
  7778. uint8 type = 0;
  7779. WASMRefType *ref_type = NULL;
  7780. /* Get stack top element */
  7781. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7782. error_buf_size)) {
  7783. return false;
  7784. }
  7785. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7786. /* stack top isn't a ref type */
  7787. && !wasm_is_type_reftype(type)) {
  7788. set_error_buf(error_buf, error_buf_size,
  7789. "type mismatch: expect heap object but got others");
  7790. return false;
  7791. }
  7792. /* POP stack top */
  7793. if (wasm_is_type_multi_byte_type(type)) {
  7794. bh_assert(ref_type);
  7795. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7796. wasm_reftype_struct_size(ref_type));
  7797. }
  7798. #if WASM_ENABLE_FAST_INTERP != 0
  7799. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7800. error_buf_size)) {
  7801. return false;
  7802. }
  7803. #else
  7804. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7805. return false;
  7806. }
  7807. #endif
  7808. if (p_type)
  7809. *p_type = type;
  7810. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7811. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7812. wasm_reftype_struct_size(ref_type));
  7813. }
  7814. return true;
  7815. }
  7816. /* Check whether the stack top elem is subtype of (ref null ht),
  7817. and if yes, pop it and return the converted (ref ht) */
  7818. static bool
  7819. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7820. WASMRefType *ref_ht_ret, char *error_buf,
  7821. uint32 error_buf_size)
  7822. {
  7823. uint8 type = 0;
  7824. WASMRefType ref_type = { 0 };
  7825. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7826. error_buf_size)) {
  7827. return false;
  7828. }
  7829. /* Convert to related (ref ht) and return */
  7830. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7831. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7832. */
  7833. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7834. HEAP_TYPE_ARRAY
  7835. + (type - REF_TYPE_ARRAYREF));
  7836. type = ref_ht_ret->ref_type;
  7837. }
  7838. else if (wasm_is_reftype_htref_nullable(type)
  7839. || wasm_is_reftype_htref_non_nullable(type)) {
  7840. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7841. wasm_reftype_struct_size(&ref_type));
  7842. /* Convert to (ref ht) */
  7843. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7844. ref_ht_ret->ref_ht_common.nullable = false;
  7845. type = ref_ht_ret->ref_type;
  7846. }
  7847. *p_type = type;
  7848. return true;
  7849. }
  7850. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7851. and if yes, pop it and return the type_idx */
  7852. static bool
  7853. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7854. uint32 *p_type_idx, char *error_buf,
  7855. uint32 error_buf_size)
  7856. {
  7857. uint8 type = 0;
  7858. int32 type_idx = -1;
  7859. WASMRefType *ref_type = NULL;
  7860. /* Get stack top element */
  7861. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7862. error_buf_size)) {
  7863. return false;
  7864. }
  7865. if (type != VALUE_TYPE_ANY) {
  7866. /* stack top isn't (ref null $t) */
  7867. if (!((wasm_is_reftype_htref_nullable(type)
  7868. || wasm_is_reftype_htref_non_nullable(type))
  7869. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7870. set_error_buf(error_buf, error_buf_size,
  7871. "type mismatch: expect (ref null $t) but got others");
  7872. return false;
  7873. }
  7874. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7875. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7876. wasm_reftype_struct_size(ref_type));
  7877. }
  7878. /* POP stack top */
  7879. #if WASM_ENABLE_FAST_INTERP != 0
  7880. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7881. error_buf_size)) {
  7882. return false;
  7883. }
  7884. #else
  7885. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7886. return false;
  7887. }
  7888. #endif
  7889. /* Convert to type_idx and return */
  7890. *p_type = type;
  7891. if (type != VALUE_TYPE_ANY)
  7892. *p_type_idx = (uint32)type_idx;
  7893. return true;
  7894. }
  7895. #endif /* WASM_ENABLE_GC != 0 */
  7896. #if WASM_ENABLE_FAST_INTERP == 0
  7897. static bool
  7898. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7899. uint8 type_push, uint8 type_pop, char *error_buf,
  7900. uint32 error_buf_size)
  7901. {
  7902. for (int i = 0; i < pop_cnt; i++) {
  7903. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7904. error_buf_size))
  7905. return false;
  7906. }
  7907. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7908. return false;
  7909. return true;
  7910. }
  7911. #endif
  7912. static bool
  7913. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7914. BlockType block_type, uint8 *start_addr,
  7915. char *error_buf, uint32 error_buf_size)
  7916. {
  7917. CHECK_CSP_PUSH();
  7918. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7919. ctx->frame_csp->label_type = label_type;
  7920. ctx->frame_csp->block_type = block_type;
  7921. ctx->frame_csp->start_addr = start_addr;
  7922. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7923. #if WASM_ENABLE_GC != 0
  7924. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7925. #endif
  7926. #if WASM_ENABLE_FAST_INTERP != 0
  7927. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7928. ctx->frame_csp->patch_list = NULL;
  7929. #endif
  7930. ctx->frame_csp++;
  7931. ctx->csp_num++;
  7932. if (ctx->csp_num > ctx->max_csp_num) {
  7933. ctx->max_csp_num = ctx->csp_num;
  7934. if (ctx->max_csp_num > UINT16_MAX) {
  7935. set_error_buf(error_buf, error_buf_size,
  7936. "label stack depth limit exceeded");
  7937. return false;
  7938. }
  7939. }
  7940. return true;
  7941. fail:
  7942. return false;
  7943. }
  7944. static bool
  7945. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7946. uint32 error_buf_size)
  7947. {
  7948. CHECK_CSP_POP();
  7949. #if WASM_ENABLE_FAST_INTERP != 0
  7950. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7951. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7952. #endif
  7953. ctx->frame_csp--;
  7954. ctx->csp_num--;
  7955. return true;
  7956. fail:
  7957. return false;
  7958. }
  7959. #if WASM_ENABLE_FAST_INTERP != 0
  7960. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7961. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7962. #define emit_label(opcode) \
  7963. do { \
  7964. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7965. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7966. } while (0)
  7967. #define skip_label() \
  7968. do { \
  7969. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7970. LOG_OP("\ndelete last op\n"); \
  7971. } while (0)
  7972. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7973. #if UINTPTR_MAX == UINT64_MAX
  7974. #define emit_label(opcode) \
  7975. do { \
  7976. int32 offset = \
  7977. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7978. /* emit int32 relative offset in 64-bit target */ \
  7979. wasm_loader_emit_uint32(loader_ctx, offset); \
  7980. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7981. } while (0)
  7982. #else
  7983. #define emit_label(opcode) \
  7984. do { \
  7985. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7986. /* emit uint32 label address in 32-bit target */ \
  7987. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7988. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7989. } while (0)
  7990. #endif
  7991. #define skip_label() \
  7992. do { \
  7993. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7994. LOG_OP("\ndelete last op\n"); \
  7995. } while (0)
  7996. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7997. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7998. #define emit_label(opcode) \
  7999. do { \
  8000. wasm_loader_emit_uint8(loader_ctx, opcode); \
  8001. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8002. } while (0)
  8003. #define skip_label() \
  8004. do { \
  8005. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  8006. LOG_OP("\ndelete last op\n"); \
  8007. } while (0)
  8008. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  8009. #define emit_empty_label_addr_and_frame_ip(type) \
  8010. do { \
  8011. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  8012. loader_ctx->p_code_compiled, error_buf, \
  8013. error_buf_size)) \
  8014. goto fail; \
  8015. /* label address, to be patched */ \
  8016. wasm_loader_emit_ptr(loader_ctx, NULL); \
  8017. } while (0)
  8018. #define emit_br_info(frame_csp, is_br) \
  8019. do { \
  8020. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  8021. error_buf_size)) \
  8022. goto fail; \
  8023. } while (0)
  8024. #define LAST_OP_OUTPUT_I32() \
  8025. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  8026. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  8027. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  8028. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  8029. || (last_op >= WASM_OP_I32_WRAP_I64 \
  8030. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  8031. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  8032. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  8033. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  8034. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  8035. || (last_op == EXT_OP_COPY_STACK_TOP)
  8036. #define LAST_OP_OUTPUT_I64() \
  8037. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  8038. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  8039. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  8040. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  8041. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  8042. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  8043. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  8044. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  8045. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  8046. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  8047. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  8048. #define GET_CONST_OFFSET(type, val) \
  8049. do { \
  8050. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  8051. &operand_offset, error_buf, \
  8052. error_buf_size))) \
  8053. goto fail; \
  8054. } while (0)
  8055. #define GET_CONST_F32_OFFSET(type, fval) \
  8056. do { \
  8057. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8058. &operand_offset, error_buf, \
  8059. error_buf_size))) \
  8060. goto fail; \
  8061. } while (0)
  8062. #define GET_CONST_F64_OFFSET(type, fval) \
  8063. do { \
  8064. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8065. &operand_offset, error_buf, \
  8066. error_buf_size))) \
  8067. goto fail; \
  8068. } while (0)
  8069. #define emit_operand(ctx, offset) \
  8070. do { \
  8071. wasm_loader_emit_int16(ctx, offset); \
  8072. LOG_OP("%d\t", offset); \
  8073. } while (0)
  8074. #define emit_byte(ctx, byte) \
  8075. do { \
  8076. wasm_loader_emit_uint8(ctx, byte); \
  8077. LOG_OP("%d\t", byte); \
  8078. } while (0)
  8079. #define emit_uint32(ctx, value) \
  8080. do { \
  8081. wasm_loader_emit_uint32(ctx, value); \
  8082. LOG_OP("%d\t", value); \
  8083. } while (0)
  8084. #define emit_uint64(ctx, value) \
  8085. do { \
  8086. wasm_loader_emit_const(ctx, &value, false); \
  8087. LOG_OP("%lld\t", value); \
  8088. } while (0)
  8089. #define emit_float32(ctx, value) \
  8090. do { \
  8091. wasm_loader_emit_const(ctx, &value, true); \
  8092. LOG_OP("%f\t", value); \
  8093. } while (0)
  8094. #define emit_float64(ctx, value) \
  8095. do { \
  8096. wasm_loader_emit_const(ctx, &value, false); \
  8097. LOG_OP("%f\t", value); \
  8098. } while (0)
  8099. static bool
  8100. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8101. {
  8102. if (!(ctx->p_code_compiled =
  8103. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8104. return false;
  8105. ctx->p_code_compiled_end =
  8106. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8107. /* clean up frame ref */
  8108. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8109. ctx->frame_ref = ctx->frame_ref_bottom;
  8110. ctx->stack_cell_num = 0;
  8111. #if WASM_ENABLE_GC != 0
  8112. /* clean up reftype map */
  8113. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8114. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8115. ctx->reftype_map_num = 0;
  8116. #endif
  8117. /* clean up frame csp */
  8118. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8119. ctx->frame_csp = ctx->frame_csp_bottom;
  8120. ctx->csp_num = 0;
  8121. ctx->max_csp_num = 0;
  8122. /* clean up frame offset */
  8123. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8124. ctx->frame_offset = ctx->frame_offset_bottom;
  8125. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8126. /* init preserved local offsets */
  8127. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8128. /* const buf is reserved */
  8129. return true;
  8130. }
  8131. static void
  8132. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8133. {
  8134. ctx->code_compiled_size += size;
  8135. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8136. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8137. }
  8138. }
  8139. static void
  8140. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8141. {
  8142. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8143. if (ctx->p_code_compiled) {
  8144. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8145. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8146. #endif
  8147. bh_memcpy_s(ctx->p_code_compiled,
  8148. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8149. value, size);
  8150. ctx->p_code_compiled += size;
  8151. }
  8152. else {
  8153. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8154. bh_assert((ctx->code_compiled_size & 1) == 0);
  8155. #endif
  8156. increase_compiled_code_space(ctx, size);
  8157. }
  8158. }
  8159. static void
  8160. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8161. {
  8162. if (ctx->p_code_compiled) {
  8163. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8164. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8165. #endif
  8166. STORE_U32(ctx->p_code_compiled, value);
  8167. ctx->p_code_compiled += sizeof(uint32);
  8168. }
  8169. else {
  8170. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8171. bh_assert((ctx->code_compiled_size & 1) == 0);
  8172. #endif
  8173. increase_compiled_code_space(ctx, sizeof(uint32));
  8174. }
  8175. }
  8176. static void
  8177. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8178. {
  8179. if (ctx->p_code_compiled) {
  8180. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8181. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8182. #endif
  8183. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8184. ctx->p_code_compiled += sizeof(int16);
  8185. }
  8186. else {
  8187. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8188. bh_assert((ctx->code_compiled_size & 1) == 0);
  8189. #endif
  8190. increase_compiled_code_space(ctx, sizeof(uint16));
  8191. }
  8192. }
  8193. static void
  8194. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8195. {
  8196. if (ctx->p_code_compiled) {
  8197. *(ctx->p_code_compiled) = value;
  8198. ctx->p_code_compiled += sizeof(uint8);
  8199. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8200. ctx->p_code_compiled++;
  8201. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8202. #endif
  8203. }
  8204. else {
  8205. increase_compiled_code_space(ctx, sizeof(uint8));
  8206. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8207. increase_compiled_code_space(ctx, sizeof(uint8));
  8208. bh_assert((ctx->code_compiled_size & 1) == 0);
  8209. #endif
  8210. }
  8211. }
  8212. static void
  8213. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8214. {
  8215. if (ctx->p_code_compiled) {
  8216. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8217. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8218. #endif
  8219. STORE_PTR(ctx->p_code_compiled, value);
  8220. ctx->p_code_compiled += sizeof(void *);
  8221. }
  8222. else {
  8223. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8224. bh_assert((ctx->code_compiled_size & 1) == 0);
  8225. #endif
  8226. increase_compiled_code_space(ctx, sizeof(void *));
  8227. }
  8228. }
  8229. static void
  8230. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8231. {
  8232. if (ctx->p_code_compiled) {
  8233. ctx->p_code_compiled -= size;
  8234. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8235. if (size == sizeof(uint8)) {
  8236. ctx->p_code_compiled--;
  8237. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8238. }
  8239. #endif
  8240. }
  8241. else {
  8242. ctx->code_compiled_size -= size;
  8243. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8244. if (size == sizeof(uint8)) {
  8245. ctx->code_compiled_size--;
  8246. bh_assert((ctx->code_compiled_size & 1) == 0);
  8247. }
  8248. #endif
  8249. }
  8250. }
  8251. static bool
  8252. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8253. uint32 local_index, uint32 local_type,
  8254. bool *preserved, char *error_buf,
  8255. uint32 error_buf_size)
  8256. {
  8257. uint32 i = 0;
  8258. int16 preserved_offset = (int16)local_index;
  8259. *preserved = false;
  8260. while (i < loader_ctx->stack_cell_num) {
  8261. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8262. /* move previous local into dynamic space before a set/tee_local opcode
  8263. */
  8264. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8265. if (!(*preserved)) {
  8266. *preserved = true;
  8267. skip_label();
  8268. preserved_offset = loader_ctx->preserved_local_offset;
  8269. if (loader_ctx->p_code_compiled) {
  8270. bh_assert(preserved_offset != (int16)local_index);
  8271. }
  8272. if (is_32bit_type(local_type)) {
  8273. /* Only increase preserve offset in the second traversal */
  8274. if (loader_ctx->p_code_compiled)
  8275. loader_ctx->preserved_local_offset++;
  8276. emit_label(EXT_OP_COPY_STACK_TOP);
  8277. }
  8278. #if WASM_ENABLE_SIMDE != 0
  8279. else if (local_type == VALUE_TYPE_V128) {
  8280. if (loader_ctx->p_code_compiled)
  8281. loader_ctx->preserved_local_offset += 4;
  8282. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8283. }
  8284. #endif
  8285. else {
  8286. if (loader_ctx->p_code_compiled)
  8287. loader_ctx->preserved_local_offset += 2;
  8288. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8289. }
  8290. /* overflow */
  8291. if (preserved_offset > loader_ctx->preserved_local_offset) {
  8292. set_error_buf_v(error_buf, error_buf_size,
  8293. "too much local cells 0x%x",
  8294. loader_ctx->preserved_local_offset);
  8295. return false;
  8296. }
  8297. emit_operand(loader_ctx, local_index);
  8298. emit_operand(loader_ctx, preserved_offset);
  8299. emit_label(opcode);
  8300. }
  8301. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8302. }
  8303. if (cur_type == VALUE_TYPE_V128) {
  8304. i += 4;
  8305. }
  8306. else if (is_32bit_type(cur_type)) {
  8307. i++;
  8308. }
  8309. else {
  8310. i += 2;
  8311. }
  8312. }
  8313. (void)error_buf;
  8314. (void)error_buf_size;
  8315. return true;
  8316. }
  8317. static bool
  8318. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8319. char *error_buf, uint32 error_buf_size)
  8320. {
  8321. uint32 i = 0;
  8322. bool preserve_local;
  8323. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8324. blocks will not influence the value of these locals */
  8325. while (i < loader_ctx->stack_cell_num) {
  8326. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8327. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8328. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8329. && (cur_offset >= 0)) {
  8330. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8331. cur_type, &preserve_local,
  8332. error_buf, error_buf_size)))
  8333. return false;
  8334. }
  8335. if (cur_type == VALUE_TYPE_V128) {
  8336. i += 4;
  8337. }
  8338. else if (is_32bit_type(cur_type)) {
  8339. i++;
  8340. }
  8341. else {
  8342. i += 2;
  8343. }
  8344. }
  8345. return true;
  8346. }
  8347. static bool
  8348. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8349. uint8 *p_code_compiled, char *error_buf,
  8350. uint32 error_buf_size)
  8351. {
  8352. BranchBlockPatch *patch =
  8353. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8354. if (!patch) {
  8355. return false;
  8356. }
  8357. patch->patch_type = patch_type;
  8358. patch->code_compiled = p_code_compiled;
  8359. if (!frame_csp->patch_list) {
  8360. frame_csp->patch_list = patch;
  8361. patch->next = NULL;
  8362. }
  8363. else {
  8364. patch->next = frame_csp->patch_list;
  8365. frame_csp->patch_list = patch;
  8366. }
  8367. return true;
  8368. }
  8369. static void
  8370. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8371. {
  8372. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8373. BranchBlockPatch *node = frame_csp->patch_list;
  8374. BranchBlockPatch *node_prev = NULL, *node_next;
  8375. if (!ctx->p_code_compiled)
  8376. return;
  8377. while (node) {
  8378. node_next = node->next;
  8379. if (node->patch_type == patch_type) {
  8380. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8381. if (node_prev == NULL) {
  8382. frame_csp->patch_list = node_next;
  8383. }
  8384. else {
  8385. node_prev->next = node_next;
  8386. }
  8387. wasm_runtime_free(node);
  8388. }
  8389. else {
  8390. node_prev = node;
  8391. }
  8392. node = node_next;
  8393. }
  8394. }
  8395. static bool
  8396. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8397. bool is_br, char *error_buf, uint32 error_buf_size)
  8398. {
  8399. /* br info layout:
  8400. * a) arity of target block
  8401. * b) total cell num of arity values
  8402. * c) each arity value's cell num
  8403. * d) each arity value's src frame offset
  8404. * e) each arity values's dst dynamic offset
  8405. * f) branch target address
  8406. *
  8407. * Note: b-e are omitted when arity is 0 so that
  8408. * interpreter can recover the br info quickly.
  8409. */
  8410. BlockType *block_type = &frame_csp->block_type;
  8411. uint8 *types = NULL, cell;
  8412. #if WASM_ENABLE_GC != 0
  8413. WASMRefTypeMap *reftype_maps;
  8414. uint32 reftype_map_count;
  8415. #endif
  8416. uint32 arity = 0;
  8417. int32 i;
  8418. int16 *frame_offset = ctx->frame_offset;
  8419. uint16 dynamic_offset;
  8420. /* Note: loop's arity is different from if and block. loop's arity is
  8421. * its parameter count while if and block arity is result count.
  8422. */
  8423. #if WASM_ENABLE_GC == 0
  8424. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8425. arity = block_type_get_param_types(block_type, &types);
  8426. else
  8427. arity = block_type_get_result_types(block_type, &types);
  8428. #else
  8429. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8430. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8431. &reftype_map_count);
  8432. else
  8433. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8434. &reftype_map_count);
  8435. #endif
  8436. /* Part a */
  8437. emit_uint32(ctx, arity);
  8438. if (arity) {
  8439. /* Part b */
  8440. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8441. /* Part c */
  8442. for (i = (int32)arity - 1; i >= 0; i--) {
  8443. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8444. emit_byte(ctx, cell);
  8445. }
  8446. /* Part d */
  8447. for (i = (int32)arity - 1; i >= 0; i--) {
  8448. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8449. frame_offset -= cell;
  8450. emit_operand(ctx, *(int16 *)(frame_offset));
  8451. }
  8452. /* Part e */
  8453. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8454. /* Use start_dynamic_offset which was set in
  8455. copy_params_to_dynamic_space */
  8456. dynamic_offset = frame_csp->start_dynamic_offset
  8457. + wasm_get_cell_num(types, arity);
  8458. else
  8459. dynamic_offset =
  8460. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8461. if (is_br)
  8462. ctx->dynamic_offset = dynamic_offset;
  8463. for (i = (int32)arity - 1; i >= 0; i--) {
  8464. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8465. dynamic_offset -= cell;
  8466. emit_operand(ctx, dynamic_offset);
  8467. }
  8468. }
  8469. /* Part f */
  8470. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8471. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8472. }
  8473. else {
  8474. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8475. error_buf, error_buf_size))
  8476. return false;
  8477. /* label address, to be patched */
  8478. wasm_loader_emit_ptr(ctx, NULL);
  8479. }
  8480. return true;
  8481. }
  8482. static bool
  8483. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8484. bool disable_emit, int16 operand_offset,
  8485. char *error_buf, uint32 error_buf_size)
  8486. {
  8487. uint32 cell_num_to_push, i;
  8488. if (type == VALUE_TYPE_VOID)
  8489. return true;
  8490. /* only check memory overflow in first traverse */
  8491. if (ctx->p_code_compiled == NULL) {
  8492. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8493. return false;
  8494. }
  8495. if (disable_emit)
  8496. *(ctx->frame_offset)++ = operand_offset;
  8497. else {
  8498. emit_operand(ctx, ctx->dynamic_offset);
  8499. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8500. ctx->dynamic_offset++;
  8501. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8502. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8503. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8504. goto fail;
  8505. }
  8506. }
  8507. }
  8508. if (is_32bit_type(type))
  8509. return true;
  8510. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8511. for (i = 0; i < cell_num_to_push; i++) {
  8512. if (ctx->p_code_compiled == NULL) {
  8513. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8514. return false;
  8515. }
  8516. ctx->frame_offset++;
  8517. if (!disable_emit) {
  8518. ctx->dynamic_offset++;
  8519. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8520. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8521. if (ctx->max_dynamic_offset >= INT16_MAX)
  8522. goto fail;
  8523. }
  8524. }
  8525. }
  8526. return true;
  8527. fail:
  8528. set_error_buf(error_buf, error_buf_size,
  8529. "fast interpreter offset overflow");
  8530. return false;
  8531. }
  8532. /* This function should be in front of wasm_loader_pop_frame_ref
  8533. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8534. will be modified by wasm_loader_pop_frame_ref */
  8535. static bool
  8536. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8537. char *error_buf, uint32 error_buf_size)
  8538. {
  8539. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8540. then current block is the function block */
  8541. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8542. BranchBlock *cur_block = ctx->frame_csp - depth;
  8543. int32 available_stack_cell =
  8544. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8545. uint32 cell_num_to_pop;
  8546. /* Directly return success if current block is in stack
  8547. polymorphic state while stack is empty. */
  8548. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8549. return true;
  8550. if (type == VALUE_TYPE_VOID)
  8551. return true;
  8552. /* Change type to ANY when the stack top is ANY, so as to avoid
  8553. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8554. two offsets */
  8555. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8556. type = VALUE_TYPE_ANY;
  8557. cell_num_to_pop = wasm_value_type_cell_num(type);
  8558. /* Check the offset stack bottom to ensure the frame offset
  8559. stack will not go underflow. But we don't thrown error
  8560. and return true here, because the error msg should be
  8561. given in wasm_loader_pop_frame_ref */
  8562. if (!check_offset_pop(ctx, cell_num_to_pop))
  8563. return true;
  8564. ctx->frame_offset -= cell_num_to_pop;
  8565. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8566. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8567. ctx->dynamic_offset -= cell_num_to_pop;
  8568. emit_operand(ctx, *(ctx->frame_offset));
  8569. (void)error_buf;
  8570. (void)error_buf_size;
  8571. return true;
  8572. }
  8573. static bool
  8574. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8575. bool disable_emit, int16 operand_offset,
  8576. char *error_buf, uint32 error_buf_size)
  8577. {
  8578. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8579. error_buf, error_buf_size)))
  8580. return false;
  8581. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8582. return false;
  8583. return true;
  8584. }
  8585. static bool
  8586. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8587. char *error_buf, uint32 error_buf_size)
  8588. {
  8589. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8590. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8591. return false;
  8592. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8593. return false;
  8594. return true;
  8595. }
  8596. static bool
  8597. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8598. uint8 type_push, uint8 type_pop,
  8599. bool disable_emit, int16 operand_offset,
  8600. char *error_buf, uint32 error_buf_size)
  8601. {
  8602. uint8 i;
  8603. for (i = 0; i < pop_cnt; i++) {
  8604. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8605. error_buf_size))
  8606. return false;
  8607. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8608. error_buf_size))
  8609. return false;
  8610. }
  8611. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8612. operand_offset, error_buf,
  8613. error_buf_size))
  8614. return false;
  8615. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8616. return false;
  8617. return true;
  8618. }
  8619. static int
  8620. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  8621. {
  8622. int64 i64_const1 = *(int64 *)p_i64_const1;
  8623. int64 i64_const2 = *(int64 *)p_i64_const2;
  8624. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  8625. }
  8626. static int
  8627. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  8628. {
  8629. int32 i32_const1 = *(int32 *)p_i32_const1;
  8630. int32 i32_const2 = *(int32 *)p_i32_const2;
  8631. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  8632. }
  8633. static int
  8634. cmp_v128_const(const void *p_v128_const1, const void *p_v128_const2)
  8635. {
  8636. V128 v128_const1 = *(V128 *)p_v128_const1;
  8637. V128 v128_const2 = *(V128 *)p_v128_const2;
  8638. return memcmp(&v128_const1, &v128_const2, sizeof(V128));
  8639. }
  8640. static bool
  8641. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8642. int16 *offset, char *error_buf,
  8643. uint32 error_buf_size)
  8644. {
  8645. if (!ctx->p_code_compiled) {
  8646. /* Treat i64 and f64 as the same by reading i64 value from
  8647. the raw bytes */
  8648. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8649. /* No slot left, emit const instead */
  8650. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  8651. *offset = 0;
  8652. return true;
  8653. }
  8654. /* Traverse the list if the const num is small */
  8655. if (ctx->i64_const_num < 10) {
  8656. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  8657. if (ctx->i64_consts[i] == *(int64 *)value) {
  8658. *offset = -1;
  8659. return true;
  8660. }
  8661. }
  8662. }
  8663. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  8664. MEM_REALLOC(ctx->i64_consts,
  8665. sizeof(int64) * ctx->i64_const_max_num,
  8666. sizeof(int64) * (ctx->i64_const_max_num * 2));
  8667. ctx->i64_const_max_num *= 2;
  8668. }
  8669. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  8670. }
  8671. else if (type == VALUE_TYPE_V128) {
  8672. /* No slot left, emit const instead */
  8673. if (ctx->v128_const_num * 4 > INT16_MAX - 2) {
  8674. *offset = 0;
  8675. return true;
  8676. }
  8677. /* Traverse the list if the const num is small */
  8678. if (ctx->v128_const_num < 10) {
  8679. for (uint32 i = 0; i < ctx->v128_const_num; i++) {
  8680. if (memcmp(&ctx->v128_consts[i], value, sizeof(V128))
  8681. == 0) {
  8682. *offset = -1;
  8683. return true;
  8684. }
  8685. }
  8686. }
  8687. if (ctx->v128_const_num >= ctx->v128_const_max_num) {
  8688. MEM_REALLOC(ctx->v128_consts,
  8689. sizeof(V128) * ctx->v128_const_max_num,
  8690. sizeof(V128) * (ctx->v128_const_max_num * 2));
  8691. ctx->v128_const_max_num *= 2;
  8692. }
  8693. ctx->v128_consts[ctx->v128_const_num++] = *(V128 *)value;
  8694. }
  8695. else {
  8696. /* Treat i32 and f32 as the same by reading i32 value from
  8697. the raw bytes */
  8698. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  8699. /* No slot left, emit const instead */
  8700. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  8701. *offset = 0;
  8702. return true;
  8703. }
  8704. /* Traverse the list if the const num is small */
  8705. if (ctx->i32_const_num < 10) {
  8706. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  8707. if (ctx->i32_consts[i] == *(int32 *)value) {
  8708. *offset = -1;
  8709. return true;
  8710. }
  8711. }
  8712. }
  8713. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  8714. MEM_REALLOC(ctx->i32_consts,
  8715. sizeof(int32) * ctx->i32_const_max_num,
  8716. sizeof(int32) * (ctx->i32_const_max_num * 2));
  8717. ctx->i32_const_max_num *= 2;
  8718. }
  8719. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  8720. }
  8721. *offset = -1;
  8722. return true;
  8723. }
  8724. else {
  8725. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8726. int64 key = *(int64 *)value, *i64_const;
  8727. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  8728. sizeof(int64), cmp_i64_const);
  8729. if (!i64_const) { /* not found, emit const instead */
  8730. *offset = 0;
  8731. return true;
  8732. }
  8733. /* constant index is encoded as negative value */
  8734. *offset = -(int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  8735. + (int32)(i64_const - ctx->i64_consts) * 2;
  8736. }
  8737. else if (type == VALUE_TYPE_V128) {
  8738. V128 key = *(V128 *)value, *v128_const;
  8739. v128_const = bsearch(&key, ctx->v128_consts, ctx->v128_const_num,
  8740. sizeof(V128), cmp_v128_const);
  8741. if (!v128_const) { /* not found, emit const instead */
  8742. *offset = 0;
  8743. return true;
  8744. }
  8745. /* constant index is encoded as negative value */
  8746. *offset = -(int32)(ctx->v128_const_num)
  8747. + (int32)(v128_const - ctx->v128_consts);
  8748. }
  8749. else {
  8750. int32 key = *(int32 *)value, *i32_const;
  8751. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  8752. sizeof(int32), cmp_i32_const);
  8753. if (!i32_const) { /* not found, emit const instead */
  8754. *offset = 0;
  8755. return true;
  8756. }
  8757. /* constant index is encoded as negative value */
  8758. *offset = -(int32)(ctx->i32_const_num)
  8759. + (int32)(i32_const - ctx->i32_consts);
  8760. }
  8761. return true;
  8762. }
  8763. fail:
  8764. return false;
  8765. }
  8766. /*
  8767. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8768. -- Mostly used for the binary / compare operation
  8769. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8770. -- Mostly used in block / control instructions
  8771. The POP will always emit the offset on the top of the frame_offset stack
  8772. PUSH can be used in two ways:
  8773. 1. directly PUSH:
  8774. PUSH_XXX();
  8775. will allocate a dynamic space and emit
  8776. 2. silent PUSH:
  8777. operand_offset = xxx; disable_emit = true;
  8778. PUSH_XXX();
  8779. only push the frame_offset stack, no emit
  8780. */
  8781. #define TEMPLATE_PUSH(Type) \
  8782. do { \
  8783. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8784. disable_emit, operand_offset, \
  8785. error_buf, error_buf_size)) \
  8786. goto fail; \
  8787. } while (0)
  8788. #define TEMPLATE_PUSH_REF(Type) \
  8789. do { \
  8790. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8791. operand_offset, error_buf, \
  8792. error_buf_size)) \
  8793. goto fail; \
  8794. } while (0)
  8795. #define TEMPLATE_POP(Type) \
  8796. do { \
  8797. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8798. error_buf, error_buf_size)) \
  8799. goto fail; \
  8800. } while (0)
  8801. #define TEMPLATE_POP_REF(Type) \
  8802. do { \
  8803. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8804. error_buf_size)) \
  8805. goto fail; \
  8806. } while (0)
  8807. #define PUSH_OFFSET_TYPE(type) \
  8808. do { \
  8809. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8810. operand_offset, error_buf, \
  8811. error_buf_size))) \
  8812. goto fail; \
  8813. } while (0)
  8814. #define POP_OFFSET_TYPE(type) \
  8815. do { \
  8816. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8817. error_buf_size))) \
  8818. goto fail; \
  8819. } while (0)
  8820. #define POP_AND_PUSH(type_pop, type_push) \
  8821. do { \
  8822. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8823. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8824. operand_offset, error_buf, error_buf_size))) \
  8825. goto fail; \
  8826. } while (0)
  8827. /* type of POPs should be the same */
  8828. #define POP2_AND_PUSH(type_pop, type_push) \
  8829. do { \
  8830. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8831. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8832. operand_offset, error_buf, error_buf_size))) \
  8833. goto fail; \
  8834. } while (0)
  8835. #else /* WASM_ENABLE_FAST_INTERP */
  8836. #define TEMPLATE_PUSH(Type) \
  8837. do { \
  8838. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8839. error_buf, error_buf_size))) \
  8840. goto fail; \
  8841. } while (0)
  8842. #define TEMPLATE_PUSH_REF(Type) \
  8843. do { \
  8844. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8845. error_buf_size))) \
  8846. goto fail; \
  8847. } while (0)
  8848. #define TEMPLATE_POP(Type) \
  8849. do { \
  8850. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8851. error_buf, error_buf_size))) \
  8852. goto fail; \
  8853. } while (0)
  8854. #define TEMPLATE_POP_REF(Type) \
  8855. do { \
  8856. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8857. error_buf_size))) \
  8858. goto fail; \
  8859. } while (0)
  8860. #define POP_AND_PUSH(type_pop, type_push) \
  8861. do { \
  8862. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8863. type_pop, error_buf, \
  8864. error_buf_size))) \
  8865. goto fail; \
  8866. } while (0)
  8867. /* type of POPs should be the same */
  8868. #define POP2_AND_PUSH(type_pop, type_push) \
  8869. do { \
  8870. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8871. type_pop, error_buf, \
  8872. error_buf_size))) \
  8873. goto fail; \
  8874. } while (0)
  8875. #endif /* WASM_ENABLE_FAST_INTERP */
  8876. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8877. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8878. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8879. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8880. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8881. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8882. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8883. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8884. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8885. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8886. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8887. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  8888. #define POP_I32() TEMPLATE_POP(I32)
  8889. #define POP_F32() TEMPLATE_POP(F32)
  8890. #define POP_I64() TEMPLATE_POP(I64)
  8891. #define POP_F64() TEMPLATE_POP(F64)
  8892. #define POP_V128() TEMPLATE_POP(V128)
  8893. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8894. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8895. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8896. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8897. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  8898. #if WASM_ENABLE_FAST_INTERP != 0
  8899. static bool
  8900. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8901. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8902. {
  8903. int16 operand_offset = 0;
  8904. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8905. : loader_ctx->frame_csp;
  8906. BlockType *block_type = &block->block_type;
  8907. uint8 *return_types = NULL;
  8908. #if WASM_ENABLE_GC != 0
  8909. WASMRefTypeMap *reftype_maps = NULL;
  8910. uint32 reftype_map_count;
  8911. #endif
  8912. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8913. int32 i = 0;
  8914. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8915. *frame_offset_org = NULL;
  8916. #if WASM_ENABLE_GC == 0
  8917. return_count = block_type_get_result_types(block_type, &return_types);
  8918. #else
  8919. return_count = block_type_get_result_types(
  8920. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8921. #endif
  8922. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64/V128
  8923. * instead of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8924. if (return_count == 1) {
  8925. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8926. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8927. /* insert op_copy before else opcode */
  8928. if (opcode == WASM_OP_ELSE)
  8929. skip_label();
  8930. #if WASM_ENABLE_SIMDE != 0
  8931. if (cell == 4) {
  8932. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8933. }
  8934. #endif
  8935. if (cell <= 2) {
  8936. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8937. : EXT_OP_COPY_STACK_TOP_I64);
  8938. }
  8939. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8940. emit_operand(loader_ctx, block->dynamic_offset);
  8941. if (opcode == WASM_OP_ELSE) {
  8942. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8943. }
  8944. else {
  8945. loader_ctx->frame_offset -= cell;
  8946. loader_ctx->dynamic_offset = block->dynamic_offset;
  8947. PUSH_OFFSET_TYPE(return_types[0]);
  8948. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8949. }
  8950. if (opcode == WASM_OP_ELSE)
  8951. emit_label(opcode);
  8952. }
  8953. return true;
  8954. }
  8955. /* Copy stack top values to block's results which are in dynamic space.
  8956. * The instruction format:
  8957. * Part a: values count
  8958. * Part b: all values total cell num
  8959. * Part c: each value's cell_num, src offset and dst offset
  8960. * Part d: each value's src offset and dst offset
  8961. * Part e: each value's dst offset
  8962. */
  8963. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8964. dynamic_offset = dynamic_offset_org =
  8965. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8966. /* First traversal to get the count of values needed to be copied. */
  8967. for (i = (int32)return_count - 1; i >= 0; i--) {
  8968. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8969. if (frame_offset - cells < loader_ctx->frame_offset_bottom) {
  8970. set_error_buf(error_buf, error_buf_size, "frame offset underflow");
  8971. goto fail;
  8972. }
  8973. if (cells == 4) {
  8974. bool needs_copy = false;
  8975. int16 v128_dynamic = dynamic_offset - cells;
  8976. for (int j = 0; j < 4; j++) {
  8977. if (*(frame_offset - j - 1) != (v128_dynamic + j)) {
  8978. needs_copy = true;
  8979. break;
  8980. }
  8981. }
  8982. if (needs_copy) {
  8983. value_count++;
  8984. total_cel_num += cells;
  8985. }
  8986. frame_offset -= cells;
  8987. dynamic_offset = v128_dynamic;
  8988. }
  8989. else {
  8990. frame_offset -= cells;
  8991. dynamic_offset -= cells;
  8992. if (dynamic_offset != *frame_offset) {
  8993. value_count++;
  8994. total_cel_num += cells;
  8995. }
  8996. }
  8997. }
  8998. if (value_count) {
  8999. uint32 j = 0;
  9000. uint8 *emit_data = NULL, *cells = NULL;
  9001. int16 *src_offsets = NULL;
  9002. uint16 *dst_offsets = NULL;
  9003. uint64 size =
  9004. (uint64)value_count
  9005. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  9006. /* Allocate memory for the emit data */
  9007. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9008. return false;
  9009. cells = emit_data;
  9010. src_offsets = (int16 *)(cells + value_count);
  9011. dst_offsets = (uint16 *)(src_offsets + value_count);
  9012. /* insert op_copy before else opcode */
  9013. if (opcode == WASM_OP_ELSE)
  9014. skip_label();
  9015. emit_label(EXT_OP_COPY_STACK_VALUES);
  9016. /* Part a) */
  9017. emit_uint32(loader_ctx, value_count);
  9018. /* Part b) */
  9019. emit_uint32(loader_ctx, total_cel_num);
  9020. /* Second traversal to get each value's cell num, src offset and dst
  9021. * offset. */
  9022. frame_offset = frame_offset_org;
  9023. dynamic_offset = dynamic_offset_org;
  9024. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  9025. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  9026. if (cell == 4) {
  9027. bool needs_copy = false;
  9028. int16 v128_dynamic = dynamic_offset - cell;
  9029. for (int k = 0; k < 4; k++) {
  9030. if (*(frame_offset - k - 1) != (v128_dynamic + k)) {
  9031. needs_copy = true;
  9032. break;
  9033. }
  9034. }
  9035. if (needs_copy) {
  9036. cells[j] = cell;
  9037. src_offsets[j] = *(frame_offset - cell);
  9038. dst_offsets[j] = v128_dynamic;
  9039. j++;
  9040. }
  9041. frame_offset -= cell;
  9042. dynamic_offset = v128_dynamic;
  9043. }
  9044. else {
  9045. frame_offset -= cell;
  9046. dynamic_offset -= cell;
  9047. if (dynamic_offset != *frame_offset) {
  9048. cells[j] = cell;
  9049. /* src offset */
  9050. src_offsets[j] = *frame_offset;
  9051. /* dst offset */
  9052. dst_offsets[j] = dynamic_offset;
  9053. j++;
  9054. }
  9055. }
  9056. if (opcode == WASM_OP_ELSE) {
  9057. if (cell == 4) {
  9058. for (int k = 0; k < cell; k++) {
  9059. *(frame_offset + k) = dynamic_offset + k;
  9060. }
  9061. }
  9062. else {
  9063. *frame_offset = dynamic_offset;
  9064. }
  9065. }
  9066. else {
  9067. loader_ctx->frame_offset = frame_offset;
  9068. loader_ctx->dynamic_offset = dynamic_offset;
  9069. if (!(wasm_loader_push_frame_offset(
  9070. loader_ctx, return_types[i], disable_emit,
  9071. operand_offset, error_buf, error_buf_size))) {
  9072. wasm_runtime_free(emit_data);
  9073. goto fail;
  9074. }
  9075. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9076. loader_ctx->frame_offset = frame_offset_org;
  9077. loader_ctx->dynamic_offset = dynamic_offset_org;
  9078. }
  9079. }
  9080. bh_assert(j == value_count);
  9081. /* Emit the cells, src_offsets and dst_offsets */
  9082. for (j = 0; j < value_count; j++)
  9083. emit_byte(loader_ctx, cells[j]);
  9084. for (j = 0; j < value_count; j++)
  9085. emit_operand(loader_ctx, src_offsets[j]);
  9086. for (j = 0; j < value_count; j++)
  9087. emit_operand(loader_ctx, dst_offsets[j]);
  9088. if (opcode == WASM_OP_ELSE)
  9089. emit_label(opcode);
  9090. wasm_runtime_free(emit_data);
  9091. }
  9092. return true;
  9093. fail:
  9094. return false;
  9095. }
  9096. #endif /* WASM_ENABLE_FAST_INTERP */
  9097. #define PUSH_TYPE(type) \
  9098. do { \
  9099. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  9100. error_buf_size))) \
  9101. goto fail; \
  9102. } while (0)
  9103. #define POP_TYPE(type) \
  9104. do { \
  9105. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  9106. error_buf_size))) \
  9107. goto fail; \
  9108. } while (0)
  9109. #if WASM_ENABLE_GC == 0
  9110. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9111. do { \
  9112. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9113. _start_addr, error_buf, \
  9114. error_buf_size)) \
  9115. goto fail; \
  9116. } while (0)
  9117. #define POP_CSP() \
  9118. do { \
  9119. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9120. goto fail; \
  9121. } while (0)
  9122. #else
  9123. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9124. do { \
  9125. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9126. _start_addr, error_buf, \
  9127. error_buf_size)) \
  9128. goto fail; \
  9129. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  9130. error_buf, error_buf_size)) { \
  9131. goto fail; \
  9132. } \
  9133. } while (0)
  9134. #define POP_CSP() \
  9135. do { \
  9136. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  9137. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9138. goto fail; \
  9139. } while (0)
  9140. #endif /* end of WASM_ENABLE_GC == 0 */
  9141. #if WASM_ENABLE_GC == 0
  9142. #define GET_LOCAL_REFTYPE() (void)0
  9143. #else
  9144. #define GET_LOCAL_REFTYPE() \
  9145. do { \
  9146. if (wasm_is_type_multi_byte_type(local_type)) { \
  9147. WASMRefType *_ref_type; \
  9148. if (local_idx < param_count) \
  9149. _ref_type = wasm_reftype_map_find( \
  9150. param_reftype_maps, param_reftype_map_count, local_idx); \
  9151. else \
  9152. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  9153. local_reftype_map_count, \
  9154. local_idx - param_count); \
  9155. bh_assert(_ref_type); \
  9156. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  9157. wasm_reftype_struct_size(_ref_type)); \
  9158. } \
  9159. } while (0)
  9160. #endif /* end of WASM_ENABLE_GC == 0 */
  9161. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  9162. do { \
  9163. read_leb_uint32(p, p_end, local_idx); \
  9164. if (local_idx >= param_count + local_count) { \
  9165. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  9166. goto fail; \
  9167. } \
  9168. local_type = local_idx < param_count \
  9169. ? param_types[local_idx] \
  9170. : local_types[local_idx - param_count]; \
  9171. local_offset = local_offsets[local_idx]; \
  9172. GET_LOCAL_REFTYPE(); \
  9173. } while (0)
  9174. static bool
  9175. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  9176. {
  9177. if (module->memory_count == 0 && module->import_memory_count == 0) {
  9178. set_error_buf(error_buf, error_buf_size, "unknown memory");
  9179. return false;
  9180. }
  9181. return true;
  9182. }
  9183. #define CHECK_MEMORY() \
  9184. do { \
  9185. if (!check_memory(module, error_buf, error_buf_size)) \
  9186. goto fail; \
  9187. } while (0)
  9188. static bool
  9189. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9190. uint32 error_buf_size)
  9191. {
  9192. uint8 mem_access_aligns[] = {
  9193. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9194. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9195. };
  9196. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9197. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9198. set_error_buf(error_buf, error_buf_size,
  9199. "invalid memop flags: alignment must not be larger "
  9200. "than natural");
  9201. return false;
  9202. }
  9203. return true;
  9204. }
  9205. #if WASM_ENABLE_SIMD != 0
  9206. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  9207. || (WASM_ENABLE_FAST_INTERP != 0)
  9208. static bool
  9209. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9210. uint32 error_buf_size)
  9211. {
  9212. uint8 mem_access_aligns[] = {
  9213. 4, /* load */
  9214. 3, 3, 3, 3, 3, 3, /* load and extend */
  9215. 0, 1, 2, 3, /* load and splat */
  9216. 4, /* store */
  9217. };
  9218. uint8 mem_access_aligns_load_lane[] = {
  9219. 0, 1, 2, 3, /* load lane */
  9220. 0, 1, 2, 3, /* store lane */
  9221. 2, 3 /* store zero */
  9222. };
  9223. if (!((opcode <= SIMD_v128_store)
  9224. || (SIMD_v128_load8_lane <= opcode
  9225. && opcode <= SIMD_v128_load64_zero))) {
  9226. set_error_buf(error_buf, error_buf_size,
  9227. "the opcode doesn't include memarg");
  9228. return false;
  9229. }
  9230. if ((opcode <= SIMD_v128_store
  9231. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9232. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9233. && align > mem_access_aligns_load_lane[opcode
  9234. - SIMD_v128_load8_lane])) {
  9235. set_error_buf(error_buf, error_buf_size,
  9236. "invalid memop flags: alignment must not be larger "
  9237. "than natural");
  9238. return false;
  9239. }
  9240. return true;
  9241. }
  9242. static bool
  9243. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9244. uint32 error_buf_size)
  9245. {
  9246. switch (opcode) {
  9247. case SIMD_i8x16_extract_lane_s:
  9248. case SIMD_i8x16_extract_lane_u:
  9249. case SIMD_i8x16_replace_lane:
  9250. if (lane >= 16) {
  9251. goto fail;
  9252. }
  9253. break;
  9254. case SIMD_i16x8_extract_lane_s:
  9255. case SIMD_i16x8_extract_lane_u:
  9256. case SIMD_i16x8_replace_lane:
  9257. if (lane >= 8) {
  9258. goto fail;
  9259. }
  9260. break;
  9261. case SIMD_i32x4_extract_lane:
  9262. case SIMD_i32x4_replace_lane:
  9263. case SIMD_f32x4_extract_lane:
  9264. case SIMD_f32x4_replace_lane:
  9265. if (lane >= 4) {
  9266. goto fail;
  9267. }
  9268. break;
  9269. case SIMD_i64x2_extract_lane:
  9270. case SIMD_i64x2_replace_lane:
  9271. case SIMD_f64x2_extract_lane:
  9272. case SIMD_f64x2_replace_lane:
  9273. if (lane >= 2) {
  9274. goto fail;
  9275. }
  9276. break;
  9277. case SIMD_v128_load8_lane:
  9278. case SIMD_v128_load16_lane:
  9279. case SIMD_v128_load32_lane:
  9280. case SIMD_v128_load64_lane:
  9281. case SIMD_v128_store8_lane:
  9282. case SIMD_v128_store16_lane:
  9283. case SIMD_v128_store32_lane:
  9284. case SIMD_v128_store64_lane:
  9285. case SIMD_v128_load32_zero:
  9286. case SIMD_v128_load64_zero:
  9287. {
  9288. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9289. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9290. goto fail;
  9291. }
  9292. break;
  9293. }
  9294. default:
  9295. goto fail;
  9296. }
  9297. return true;
  9298. fail:
  9299. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9300. return false;
  9301. }
  9302. static bool
  9303. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9304. {
  9305. uint8 i;
  9306. for (i = 0; i != 16; ++i) {
  9307. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9308. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9309. return false;
  9310. }
  9311. }
  9312. return true;
  9313. }
  9314. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9315. #endif /* end of WASM_ENABLE_SIMD */
  9316. #if WASM_ENABLE_SHARED_MEMORY != 0
  9317. static bool
  9318. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9319. uint32 error_buf_size)
  9320. {
  9321. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9322. uint8 mem_access_aligns[] = {
  9323. 2, 3, 0, 1, 0, 1, 2,
  9324. };
  9325. uint8 expect;
  9326. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9327. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9328. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9329. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9330. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9331. }
  9332. else {
  9333. /* 7 opcodes in every group */
  9334. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9335. }
  9336. if (align != expect) {
  9337. set_error_buf(error_buf, error_buf_size,
  9338. "alignment isn't equal to natural");
  9339. return false;
  9340. }
  9341. return true;
  9342. }
  9343. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9344. static bool
  9345. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9346. char *error_buf, uint32 error_buf_size)
  9347. {
  9348. BranchBlock *target_block, *cur_block;
  9349. BlockType *target_block_type;
  9350. uint8 type, *types = NULL, *frame_ref;
  9351. uint32 arity = 0;
  9352. int32 i, available_stack_cell;
  9353. uint16 cell_num;
  9354. #if WASM_ENABLE_GC != 0
  9355. WASMRefTypeMap *frame_reftype_map;
  9356. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9357. WASMRefType *ref_type;
  9358. uint32 reftype_map_count = 0;
  9359. int32 available_reftype_map;
  9360. bool is_type_multi_byte;
  9361. #endif
  9362. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9363. uint8 *frame_ref_after_popped = NULL;
  9364. uint8 frame_ref_tmp[4] = { 0 };
  9365. uint8 *frame_ref_buf = frame_ref_tmp;
  9366. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9367. #if WASM_ENABLE_GC != 0
  9368. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9369. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9370. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9371. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9372. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9373. #endif
  9374. #if WASM_ENABLE_FAST_INTERP != 0
  9375. int16 *frame_offset_old = loader_ctx->frame_offset;
  9376. int16 *frame_offset_after_popped = NULL;
  9377. int16 frame_offset_tmp[4] = { 0 };
  9378. int16 *frame_offset_buf = frame_offset_tmp;
  9379. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9380. #endif
  9381. bool ret = false;
  9382. bh_assert(loader_ctx->csp_num > 0);
  9383. if (loader_ctx->csp_num - 1 < depth) {
  9384. set_error_buf(error_buf, error_buf_size,
  9385. "unknown label, "
  9386. "unexpected end of section or function");
  9387. return false;
  9388. }
  9389. cur_block = loader_ctx->frame_csp - 1;
  9390. target_block = loader_ctx->frame_csp - (depth + 1);
  9391. target_block_type = &target_block->block_type;
  9392. frame_ref = loader_ctx->frame_ref;
  9393. #if WASM_ENABLE_GC != 0
  9394. frame_reftype_map = loader_ctx->frame_reftype_map;
  9395. #endif
  9396. /* Note: loop's arity is different from if and block. loop's arity is
  9397. * its parameter count while if and block arity is result count.
  9398. */
  9399. #if WASM_ENABLE_GC == 0
  9400. if (target_block->label_type == LABEL_TYPE_LOOP)
  9401. arity = block_type_get_param_types(target_block_type, &types);
  9402. else
  9403. arity = block_type_get_result_types(target_block_type, &types);
  9404. #else
  9405. if (target_block->label_type == LABEL_TYPE_LOOP)
  9406. arity = block_type_get_param_types(target_block_type, &types,
  9407. &reftype_maps, &reftype_map_count);
  9408. else
  9409. arity = block_type_get_result_types(target_block_type, &types,
  9410. &reftype_maps, &reftype_map_count);
  9411. #endif
  9412. /* If the stack is in polymorphic state, just clear the stack
  9413. * and then re-push the values to make the stack top values
  9414. * match block type. */
  9415. if (cur_block->is_stack_polymorphic) {
  9416. #if WASM_ENABLE_GC != 0
  9417. int32 j = (int32)reftype_map_count - 1;
  9418. #endif
  9419. for (i = (int32)arity - 1; i >= 0; i--) {
  9420. #if WASM_ENABLE_GC != 0
  9421. if (wasm_is_type_multi_byte_type(types[i])) {
  9422. bh_assert(reftype_maps[j].index == i);
  9423. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9424. reftype_maps[j].ref_type,
  9425. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9426. j--;
  9427. }
  9428. #endif
  9429. #if WASM_ENABLE_FAST_INTERP != 0
  9430. POP_OFFSET_TYPE(types[i]);
  9431. #endif
  9432. POP_TYPE(types[i]);
  9433. }
  9434. /* Backup stack data since it may be changed in the below
  9435. push operations, and the stack data may be used when
  9436. checking other target blocks of opcode br_table */
  9437. if (opcode == WASM_OP_BR_TABLE) {
  9438. uint64 total_size;
  9439. frame_ref_after_popped = loader_ctx->frame_ref;
  9440. total_size = (uint64)sizeof(uint8)
  9441. * (frame_ref_old - frame_ref_after_popped);
  9442. if (total_size > sizeof(frame_ref_tmp)
  9443. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9444. error_buf_size))) {
  9445. goto fail;
  9446. }
  9447. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9448. frame_ref_after_popped, (uint32)total_size);
  9449. #if WASM_ENABLE_GC != 0
  9450. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9451. total_size =
  9452. (uint64)sizeof(WASMRefTypeMap)
  9453. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9454. if (total_size > sizeof(frame_reftype_map_tmp)
  9455. && !(frame_reftype_map_buf = loader_malloc(
  9456. total_size, error_buf, error_buf_size))) {
  9457. goto fail;
  9458. }
  9459. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9460. frame_reftype_map_after_popped, (uint32)total_size);
  9461. #endif
  9462. #if WASM_ENABLE_FAST_INTERP != 0
  9463. frame_offset_after_popped = loader_ctx->frame_offset;
  9464. total_size = (uint64)sizeof(int16)
  9465. * (frame_offset_old - frame_offset_after_popped);
  9466. if (total_size > sizeof(frame_offset_tmp)
  9467. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9468. error_buf_size))) {
  9469. goto fail;
  9470. }
  9471. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9472. frame_offset_after_popped, (uint32)total_size);
  9473. #endif
  9474. }
  9475. #if WASM_ENABLE_GC != 0
  9476. j = 0;
  9477. #endif
  9478. for (i = 0; i < (int32)arity; i++) {
  9479. #if WASM_ENABLE_GC != 0
  9480. if (wasm_is_type_multi_byte_type(types[i])) {
  9481. bh_assert(reftype_maps[j].index == i);
  9482. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9483. reftype_maps[j].ref_type,
  9484. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9485. j++;
  9486. }
  9487. #endif
  9488. #if WASM_ENABLE_FAST_INTERP != 0
  9489. bool disable_emit = true;
  9490. int16 operand_offset = 0;
  9491. PUSH_OFFSET_TYPE(types[i]);
  9492. #endif
  9493. PUSH_TYPE(types[i]);
  9494. }
  9495. #if WASM_ENABLE_FAST_INTERP != 0
  9496. emit_br_info(target_block, opcode == WASM_OP_BR);
  9497. #endif
  9498. /* Restore the stack data, note that frame_ref_bottom,
  9499. frame_reftype_map_bottom, frame_offset_bottom may be
  9500. re-allocated in the above push operations */
  9501. if (opcode == WASM_OP_BR_TABLE) {
  9502. uint32 total_size;
  9503. /* The stack operand num should not be smaller than before
  9504. after pop and push operations */
  9505. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9506. loader_ctx->stack_cell_num = stack_cell_num_old;
  9507. loader_ctx->frame_ref =
  9508. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9509. total_size = (uint32)(sizeof(uint8)
  9510. * (frame_ref_old - frame_ref_after_popped));
  9511. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9512. frame_ref_buf, total_size);
  9513. #if WASM_ENABLE_GC != 0
  9514. /* The stack operand num should not be smaller than before
  9515. after pop and push operations */
  9516. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9517. loader_ctx->reftype_map_num = reftype_map_num_old;
  9518. loader_ctx->frame_reftype_map =
  9519. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9520. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9521. * (frame_reftype_map_old
  9522. - frame_reftype_map_after_popped));
  9523. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9524. total_size, frame_reftype_map_buf, total_size);
  9525. #endif
  9526. #if WASM_ENABLE_FAST_INTERP != 0
  9527. loader_ctx->frame_offset =
  9528. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9529. total_size =
  9530. (uint32)(sizeof(int16)
  9531. * (frame_offset_old - frame_offset_after_popped));
  9532. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9533. total_size, frame_offset_buf, total_size);
  9534. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9535. #endif
  9536. }
  9537. ret = true;
  9538. goto cleanup_and_return;
  9539. }
  9540. available_stack_cell =
  9541. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9542. #if WASM_ENABLE_GC != 0
  9543. available_reftype_map =
  9544. (int32)(loader_ctx->reftype_map_num
  9545. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9546. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9547. #endif
  9548. /* Check stack top values match target block type */
  9549. for (i = (int32)arity - 1; i >= 0; i--) {
  9550. type = types[i];
  9551. #if WASM_ENABLE_GC != 0
  9552. ref_type = NULL;
  9553. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9554. if (is_type_multi_byte) {
  9555. bh_assert(reftype_map);
  9556. ref_type = reftype_map->ref_type;
  9557. }
  9558. #endif
  9559. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9560. break;
  9561. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9562. #if WASM_ENABLE_GC != 0
  9563. frame_reftype_map, available_reftype_map,
  9564. #endif
  9565. type,
  9566. #if WASM_ENABLE_GC != 0
  9567. ref_type,
  9568. #endif
  9569. error_buf, error_buf_size)) {
  9570. goto fail;
  9571. }
  9572. cell_num = wasm_value_type_cell_num(types[i]);
  9573. frame_ref -= cell_num;
  9574. available_stack_cell -= cell_num;
  9575. #if WASM_ENABLE_GC != 0
  9576. if (is_type_multi_byte) {
  9577. frame_reftype_map--;
  9578. available_reftype_map--;
  9579. reftype_map--;
  9580. }
  9581. #endif
  9582. }
  9583. #if WASM_ENABLE_FAST_INTERP != 0
  9584. emit_br_info(target_block, opcode == WASM_OP_BR);
  9585. #endif
  9586. ret = true;
  9587. cleanup_and_return:
  9588. fail:
  9589. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9590. wasm_runtime_free(frame_ref_buf);
  9591. #if WASM_ENABLE_GC != 0
  9592. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9593. wasm_runtime_free(frame_reftype_map_buf);
  9594. #endif
  9595. #if WASM_ENABLE_FAST_INTERP != 0
  9596. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9597. wasm_runtime_free(frame_offset_buf);
  9598. #endif
  9599. return ret;
  9600. }
  9601. static BranchBlock *
  9602. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9603. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9604. {
  9605. uint8 *p = *p_buf, *p_end = buf_end;
  9606. BranchBlock *frame_csp_tmp;
  9607. uint32 depth;
  9608. read_leb_uint32(p, p_end, depth);
  9609. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9610. error_buf_size)) {
  9611. goto fail;
  9612. }
  9613. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9614. *p_buf = p;
  9615. return frame_csp_tmp;
  9616. fail:
  9617. return NULL;
  9618. }
  9619. #if WASM_ENABLE_EXCE_HANDLING != 0
  9620. static BranchBlock *
  9621. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9622. uint8 *buf_end, char *error_buf,
  9623. uint32 error_buf_size)
  9624. {
  9625. uint8 *p = *p_buf, *p_end = buf_end;
  9626. BranchBlock *frame_csp_tmp;
  9627. uint32 depth;
  9628. read_leb_uint32(p, p_end, depth);
  9629. /*
  9630. * Note: "delegate 0" means the surrounding block, not the
  9631. * try-delegate block itself.
  9632. *
  9633. * Note: the caller hasn't popped the try-delegate frame yet.
  9634. */
  9635. bh_assert(loader_ctx->csp_num > 0);
  9636. if (loader_ctx->csp_num - 1 <= depth) {
  9637. #if WASM_ENABLE_SPEC_TEST == 0
  9638. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9639. #else
  9640. set_error_buf(error_buf, error_buf_size, "unknown label");
  9641. #endif
  9642. goto fail;
  9643. }
  9644. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9645. #if WASM_ENABLE_FAST_INTERP != 0
  9646. emit_br_info(frame_csp_tmp, false);
  9647. #endif
  9648. *p_buf = p;
  9649. return frame_csp_tmp;
  9650. fail:
  9651. return NULL;
  9652. }
  9653. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9654. static bool
  9655. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9656. char *error_buf, uint32 error_buf_size)
  9657. {
  9658. BlockType *block_type = &block->block_type;
  9659. uint8 *return_types = NULL;
  9660. uint32 return_count = 0;
  9661. int32 available_stack_cell, return_cell_num, i;
  9662. uint8 *frame_ref = NULL;
  9663. #if WASM_ENABLE_GC != 0
  9664. WASMRefTypeMap *frame_reftype_map;
  9665. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9666. WASMRefType *ref_type;
  9667. uint32 param_count, return_reftype_map_count = 0;
  9668. int32 available_reftype_map =
  9669. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9670. #endif
  9671. available_stack_cell =
  9672. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9673. #if WASM_ENABLE_GC == 0
  9674. return_count = block_type_get_result_types(block_type, &return_types);
  9675. #else
  9676. return_count = block_type_get_result_types(block_type, &return_types,
  9677. &return_reftype_maps,
  9678. &return_reftype_map_count);
  9679. param_count =
  9680. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9681. (void)param_count;
  9682. #endif
  9683. return_cell_num =
  9684. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9685. /* If the stack is in polymorphic state, just clear the stack
  9686. * and then re-push the values to make the stack top values
  9687. * match block type. */
  9688. if (block->is_stack_polymorphic) {
  9689. #if WASM_ENABLE_GC != 0
  9690. int32 j = (int32)return_reftype_map_count - 1;
  9691. #endif
  9692. for (i = (int32)return_count - 1; i >= 0; i--) {
  9693. #if WASM_ENABLE_GC != 0
  9694. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9695. bh_assert(return_reftype_maps[j].index == i + param_count);
  9696. bh_memcpy_s(
  9697. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9698. return_reftype_maps[j].ref_type,
  9699. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9700. j--;
  9701. }
  9702. #endif
  9703. #if WASM_ENABLE_FAST_INTERP != 0
  9704. POP_OFFSET_TYPE(return_types[i]);
  9705. #endif
  9706. POP_TYPE(return_types[i]);
  9707. }
  9708. /* Check stack is empty */
  9709. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9710. set_error_buf(
  9711. error_buf, error_buf_size,
  9712. "type mismatch: stack size does not match block type");
  9713. goto fail;
  9714. }
  9715. #if WASM_ENABLE_GC != 0
  9716. j = 0;
  9717. #endif
  9718. for (i = 0; i < (int32)return_count; i++) {
  9719. #if WASM_ENABLE_GC != 0
  9720. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9721. bh_assert(return_reftype_maps[j].index == i + param_count);
  9722. bh_memcpy_s(
  9723. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9724. return_reftype_maps[j].ref_type,
  9725. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9726. j++;
  9727. }
  9728. #endif
  9729. #if WASM_ENABLE_FAST_INTERP != 0
  9730. bool disable_emit = true;
  9731. int16 operand_offset = 0;
  9732. PUSH_OFFSET_TYPE(return_types[i]);
  9733. #endif
  9734. PUSH_TYPE(return_types[i]);
  9735. }
  9736. return true;
  9737. }
  9738. if (available_stack_cell != return_cell_num) {
  9739. #if WASM_ENABLE_EXCE_HANDLING != 0
  9740. /* testspec: this error message format is expected by try_catch.wast */
  9741. snprintf(
  9742. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9743. block->label_type == LABEL_TYPE_TRY
  9744. || (block->label_type == LABEL_TYPE_CATCH
  9745. && return_cell_num > 0)
  9746. ? "instruction"
  9747. : "block",
  9748. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9749. " but stack has ",
  9750. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9751. : "");
  9752. goto fail;
  9753. #else
  9754. set_error_buf(error_buf, error_buf_size,
  9755. "type mismatch: stack size does not match block type");
  9756. goto fail;
  9757. #endif
  9758. }
  9759. /* Check stack values match return types */
  9760. frame_ref = loader_ctx->frame_ref;
  9761. #if WASM_ENABLE_GC != 0
  9762. frame_reftype_map = loader_ctx->frame_reftype_map;
  9763. return_reftype_map =
  9764. return_reftype_map_count
  9765. ? return_reftype_maps + return_reftype_map_count - 1
  9766. : NULL;
  9767. #endif
  9768. for (i = (int32)return_count - 1; i >= 0; i--) {
  9769. uint8 type = return_types[i];
  9770. #if WASM_ENABLE_GC != 0
  9771. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9772. ref_type = NULL;
  9773. if (is_type_multi_byte) {
  9774. bh_assert(return_reftype_map);
  9775. ref_type = return_reftype_map->ref_type;
  9776. }
  9777. #endif
  9778. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9779. #if WASM_ENABLE_GC != 0
  9780. frame_reftype_map, available_reftype_map,
  9781. #endif
  9782. type,
  9783. #if WASM_ENABLE_GC != 0
  9784. ref_type,
  9785. #endif
  9786. error_buf, error_buf_size))
  9787. return false;
  9788. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9789. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9790. #if WASM_ENABLE_GC != 0
  9791. if (is_type_multi_byte) {
  9792. frame_reftype_map--;
  9793. available_reftype_map--;
  9794. return_reftype_map--;
  9795. }
  9796. #endif
  9797. }
  9798. return true;
  9799. fail:
  9800. return false;
  9801. }
  9802. #if WASM_ENABLE_FAST_INTERP != 0
  9803. /* Copy parameters to dynamic space.
  9804. * 1) POP original parameter out;
  9805. * 2) Push and copy original values to dynamic space.
  9806. * The copy instruction format:
  9807. * Part a: param count
  9808. * Part b: all param total cell num
  9809. * Part c: each param's cell_num, src offset and dst offset
  9810. * Part d: each param's src offset
  9811. * Part e: each param's dst offset
  9812. */
  9813. static bool
  9814. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9815. uint32 error_buf_size)
  9816. {
  9817. bool ret = false;
  9818. int16 *frame_offset = NULL;
  9819. uint8 *cells = NULL, cell;
  9820. int16 *src_offsets = NULL;
  9821. uint8 *emit_data = NULL;
  9822. uint32 i;
  9823. BranchBlock *block = loader_ctx->frame_csp - 1;
  9824. BlockType *block_type = &block->block_type;
  9825. WASMFuncType *wasm_type = block_type->u.type;
  9826. uint32 param_count = block_type->u.type->param_count;
  9827. int16 condition_offset = 0;
  9828. bool disable_emit = false;
  9829. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9830. int16 operand_offset = 0;
  9831. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9832. bh_assert(size > 0);
  9833. /* For if block, we also need copy the condition operand offset. */
  9834. if (is_if_block)
  9835. size += sizeof(*cells) + sizeof(*src_offsets);
  9836. /* Allocate memory for the emit data */
  9837. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9838. return false;
  9839. cells = emit_data;
  9840. src_offsets = (int16 *)(cells + param_count);
  9841. if (is_if_block)
  9842. condition_offset = *loader_ctx->frame_offset;
  9843. /* POP original parameter out */
  9844. for (i = 0; i < param_count; i++) {
  9845. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9846. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9847. }
  9848. frame_offset = loader_ctx->frame_offset;
  9849. /* Get each param's cell num and src offset */
  9850. for (i = 0; i < param_count; i++) {
  9851. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9852. cells[i] = cell;
  9853. src_offsets[i] = *frame_offset;
  9854. frame_offset += cell;
  9855. }
  9856. /* emit copy instruction */
  9857. emit_label(EXT_OP_COPY_STACK_VALUES);
  9858. /* Part a) */
  9859. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9860. /* Part b) */
  9861. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9862. : wasm_type->param_cell_num);
  9863. /* Part c) */
  9864. for (i = 0; i < param_count; i++)
  9865. emit_byte(loader_ctx, cells[i]);
  9866. if (is_if_block)
  9867. emit_byte(loader_ctx, 1);
  9868. /* Part d) */
  9869. for (i = 0; i < param_count; i++)
  9870. emit_operand(loader_ctx, src_offsets[i]);
  9871. if (is_if_block)
  9872. emit_operand(loader_ctx, condition_offset);
  9873. /* Since the start offset to save the block's params and
  9874. * the start offset to save the block's results may be
  9875. * different, we remember the dynamic offset for loop block
  9876. * so that we can use it to copy the stack operands to the
  9877. * loop block's params in wasm_loader_emit_br_info. */
  9878. if (block->label_type == LABEL_TYPE_LOOP)
  9879. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9880. /* Part e) */
  9881. /* Push to dynamic space. The push will emit the dst offset. */
  9882. for (i = 0; i < param_count; i++)
  9883. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9884. if (is_if_block)
  9885. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9886. ret = true;
  9887. fail:
  9888. /* Free the emit data */
  9889. wasm_runtime_free(emit_data);
  9890. return ret;
  9891. }
  9892. #endif
  9893. #if WASM_ENABLE_GC == 0
  9894. #define RESET_REFTYPE_MAP_STACK() (void)0
  9895. #else
  9896. #define RESET_REFTYPE_MAP_STACK() \
  9897. do { \
  9898. loader_ctx->reftype_map_num = \
  9899. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9900. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9901. + loader_ctx->reftype_map_num; \
  9902. } while (0)
  9903. #endif
  9904. /* reset the stack to the state of before entering the last block */
  9905. #if WASM_ENABLE_FAST_INTERP != 0
  9906. #define RESET_STACK() \
  9907. do { \
  9908. loader_ctx->stack_cell_num = \
  9909. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9910. loader_ctx->frame_ref = \
  9911. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9912. loader_ctx->frame_offset = \
  9913. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9914. RESET_REFTYPE_MAP_STACK(); \
  9915. } while (0)
  9916. #else
  9917. #define RESET_STACK() \
  9918. do { \
  9919. loader_ctx->stack_cell_num = \
  9920. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9921. loader_ctx->frame_ref = \
  9922. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9923. RESET_REFTYPE_MAP_STACK(); \
  9924. } while (0)
  9925. #endif
  9926. /* set current block's stack polymorphic state */
  9927. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9928. do { \
  9929. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9930. _cur_block->is_stack_polymorphic = flag; \
  9931. } while (0)
  9932. #define BLOCK_HAS_PARAM(block_type) \
  9933. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9934. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9935. do { \
  9936. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9937. error_buf_size))) { \
  9938. goto fail; \
  9939. } \
  9940. } while (0)
  9941. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9942. static bool
  9943. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9944. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9945. uint32 error_buf_size)
  9946. {
  9947. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9948. return false;
  9949. }
  9950. if (table_idx < module->import_table_count) {
  9951. if (p_elem_type)
  9952. *p_elem_type =
  9953. module->import_tables[table_idx].u.table.table_type.elem_type;
  9954. #if WASM_ENABLE_GC != 0
  9955. if (p_ref_type)
  9956. *((WASMRefType **)p_ref_type) =
  9957. module->import_tables[table_idx]
  9958. .u.table.table_type.elem_ref_type;
  9959. #endif
  9960. }
  9961. else {
  9962. if (p_elem_type)
  9963. *p_elem_type =
  9964. module->tables[table_idx - module->import_table_count]
  9965. .table_type.elem_type;
  9966. #if WASM_ENABLE_GC != 0
  9967. if (p_ref_type)
  9968. *((WASMRefType **)p_ref_type) =
  9969. module->tables[table_idx - module->import_table_count]
  9970. .table_type.elem_ref_type;
  9971. #endif
  9972. }
  9973. return true;
  9974. }
  9975. static bool
  9976. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9977. uint8 *p_elem_type, void **p_elem_ref_type,
  9978. char *error_buf, uint32 error_buf_size)
  9979. {
  9980. if (table_seg_idx >= module->table_seg_count) {
  9981. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9982. table_seg_idx);
  9983. return false;
  9984. }
  9985. if (p_elem_type) {
  9986. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9987. }
  9988. #if WASM_ENABLE_GC != 0
  9989. if (p_elem_ref_type)
  9990. *((WASMRefType **)p_elem_ref_type) =
  9991. module->table_segments[table_seg_idx].elem_ref_type;
  9992. #endif
  9993. return true;
  9994. }
  9995. #endif
  9996. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9997. const uint8 *
  9998. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9999. uint32 *len)
  10000. {
  10001. WASMCustomSection *section = module->custom_section_list;
  10002. while (section) {
  10003. if ((section->name_len == strlen(name))
  10004. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  10005. if (len) {
  10006. *len = section->content_len;
  10007. }
  10008. return section->content_addr;
  10009. }
  10010. section = section->next;
  10011. }
  10012. return NULL;
  10013. }
  10014. #endif
  10015. #if 0
  10016. #define HANDLE_OPCODE(opcode) #opcode
  10017. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  10018. #undef HANDLE_OPCODE
  10019. #endif
  10020. #if WASM_ENABLE_FAST_INTERP == 0
  10021. #define pb_read_leb_uint32 read_leb_uint32
  10022. #define pb_read_leb_int32 read_leb_int32
  10023. #define pb_read_leb_int64 read_leb_int64
  10024. #define pb_read_leb_memarg read_leb_memarg
  10025. #define pb_read_leb_mem_offset read_leb_mem_offset
  10026. #else
  10027. /* Read leb without malformed format check */
  10028. static uint64
  10029. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  10030. {
  10031. uint8 *buf = *p_buf;
  10032. uint64 result = 0, byte = 0;
  10033. uint32 shift = 0;
  10034. do {
  10035. byte = *buf++;
  10036. result |= ((byte & 0x7f) << shift);
  10037. shift += 7;
  10038. } while (byte & 0x80);
  10039. if (sign && (shift < maxbits) && (byte & 0x40)) {
  10040. /* Sign extend */
  10041. result |= (~((uint64)0)) << shift;
  10042. }
  10043. *p_buf = buf;
  10044. return result;
  10045. }
  10046. #define pb_read_leb_uint32(p, p_end, res) \
  10047. do { \
  10048. if (!loader_ctx->p_code_compiled) \
  10049. /* Enable format check in the first scan */ \
  10050. read_leb_uint32(p, p_end, res); \
  10051. else \
  10052. /* Disable format check in the second scan */ \
  10053. res = (uint32)read_leb_quick(&p, 32, false); \
  10054. } while (0)
  10055. #define pb_read_leb_int32(p, p_end, res) \
  10056. do { \
  10057. if (!loader_ctx->p_code_compiled) \
  10058. /* Enable format check in the first scan */ \
  10059. read_leb_int32(p, p_end, res); \
  10060. else \
  10061. /* Disable format check in the second scan */ \
  10062. res = (int32)read_leb_quick(&p, 32, true); \
  10063. } while (0)
  10064. #define pb_read_leb_int64(p, p_end, res) \
  10065. do { \
  10066. if (!loader_ctx->p_code_compiled) \
  10067. /* Enable format check in the first scan */ \
  10068. read_leb_int64(p, p_end, res); \
  10069. else \
  10070. /* Disable format check in the second scan */ \
  10071. res = (int64)read_leb_quick(&p, 64, true); \
  10072. } while (0)
  10073. #if WASM_ENABLE_MULTI_MEMORY != 0
  10074. #define pb_read_leb_memarg read_leb_memarg
  10075. #else
  10076. #define pb_read_leb_memarg pb_read_leb_uint32
  10077. #endif
  10078. #if WASM_ENABLE_MEMORY64 != 0
  10079. #define pb_read_leb_mem_offset read_leb_mem_offset
  10080. #else
  10081. #define pb_read_leb_mem_offset pb_read_leb_uint32
  10082. #endif
  10083. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  10084. static bool
  10085. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  10086. uint32 cur_func_idx, char *error_buf,
  10087. uint32 error_buf_size)
  10088. {
  10089. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  10090. uint32 param_count, local_count, global_count;
  10091. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  10092. table_elem_idx_type;
  10093. BlockType func_block_type;
  10094. uint16 *local_offsets, local_offset;
  10095. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  10096. uint32 table_seg_idx, data_seg_idx, count, align, i;
  10097. mem_offset_t mem_offset;
  10098. int32 i32_const = 0;
  10099. int64 i64_const;
  10100. uint8 opcode;
  10101. bool return_value = false;
  10102. WASMLoaderContext *loader_ctx;
  10103. BranchBlock *frame_csp_tmp;
  10104. #if WASM_ENABLE_GC != 0
  10105. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  10106. uint32 param_reftype_map_count, local_reftype_map_count;
  10107. int32 heap_type;
  10108. WASMRefType wasm_ref_type = { 0 };
  10109. bool need_ref_type_map;
  10110. #endif
  10111. #if WASM_ENABLE_FAST_INTERP != 0
  10112. int16 operand_offset = 0;
  10113. uint8 last_op = 0;
  10114. bool disable_emit, preserve_local = false, if_condition_available = true;
  10115. float32 f32_const;
  10116. float64 f64_const;
  10117. /*
  10118. * It means that the fast interpreter detected an exception while preparing,
  10119. * typically near the block opcode, but it did not immediately trigger
  10120. * the exception. The loader should be capable of identifying it near
  10121. * the end opcode and then raising the exception.
  10122. */
  10123. bool pending_exception = false;
  10124. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  10125. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  10126. #endif
  10127. #if WASM_ENABLE_MEMORY64 != 0
  10128. bool is_memory64 = has_module_memory64(module);
  10129. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  10130. #else
  10131. mem_offset_type = VALUE_TYPE_I32;
  10132. table_elem_idx_type = VALUE_TYPE_I32;
  10133. #endif
  10134. uint32 memidx;
  10135. global_count = module->import_global_count + module->global_count;
  10136. param_count = func->func_type->param_count;
  10137. param_types = func->func_type->types;
  10138. func_block_type.is_value_type = false;
  10139. func_block_type.u.type = func->func_type;
  10140. local_count = func->local_count;
  10141. local_types = func->local_types;
  10142. local_offsets = func->local_offsets;
  10143. #if WASM_ENABLE_GC != 0
  10144. param_reftype_maps = func->func_type->ref_type_maps;
  10145. param_reftype_map_count = func->func_type->ref_type_map_count;
  10146. local_reftype_maps = func->local_ref_type_maps;
  10147. local_reftype_map_count = func->local_ref_type_map_count;
  10148. #endif
  10149. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  10150. goto fail;
  10151. }
  10152. #if WASM_ENABLE_GC != 0
  10153. loader_ctx->module = module;
  10154. loader_ctx->ref_type_set = module->ref_type_set;
  10155. loader_ctx->ref_type_tmp = &wasm_ref_type;
  10156. #endif
  10157. #if WASM_ENABLE_FAST_INTERP != 0
  10158. /* For the first traverse, the initial value of preserved_local_offset has
  10159. * not been determined, we use the INT16_MAX to represent that a slot has
  10160. * been copied to preserve space. For second traverse, this field will be
  10161. * set to the appropriate value in wasm_loader_ctx_reinit.
  10162. * This is for Issue #1230,
  10163. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  10164. * drop opcodes need to know which slots are preserved, so those slots will
  10165. * not be treated as dynamically allocated slots */
  10166. loader_ctx->preserved_local_offset = INT16_MAX;
  10167. re_scan:
  10168. if (loader_ctx->code_compiled_size > 0) {
  10169. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  10170. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  10171. goto fail;
  10172. }
  10173. p = func->code;
  10174. func->code_compiled = loader_ctx->p_code_compiled;
  10175. func->code_compiled_size = loader_ctx->code_compiled_size;
  10176. if (loader_ctx->i64_const_num > 0) {
  10177. int64 *i64_consts_old = loader_ctx->i64_consts;
  10178. /* Sort the i64 consts */
  10179. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  10180. cmp_i64_const);
  10181. /* Remove the duplicated i64 consts */
  10182. uint32 k = 1;
  10183. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  10184. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  10185. i64_consts_old[k++] = i64_consts_old[i];
  10186. }
  10187. }
  10188. if (k < loader_ctx->i64_const_num) {
  10189. int64 *i64_consts_new;
  10190. /* Try to reallocate memory with a smaller size */
  10191. if ((i64_consts_new =
  10192. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  10193. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  10194. i64_consts_old, (uint32)sizeof(int64) * k);
  10195. /* Free the old memory */
  10196. wasm_runtime_free(i64_consts_old);
  10197. loader_ctx->i64_consts = i64_consts_new;
  10198. loader_ctx->i64_const_max_num = k;
  10199. }
  10200. loader_ctx->i64_const_num = k;
  10201. }
  10202. }
  10203. if (loader_ctx->v128_const_num > 0) {
  10204. V128 *v128_consts_old = loader_ctx->v128_consts;
  10205. /* Sort the v128 consts */
  10206. qsort(v128_consts_old, loader_ctx->v128_const_num, sizeof(V128),
  10207. cmp_v128_const);
  10208. /* Remove the duplicated v128 consts */
  10209. uint32 k = 1;
  10210. for (i = 1; i < loader_ctx->v128_const_num; i++) {
  10211. if (!(memcmp(&v128_consts_old[i], &v128_consts_old[i - 1],
  10212. sizeof(V128))
  10213. == 0)) {
  10214. v128_consts_old[k++] = v128_consts_old[i];
  10215. }
  10216. }
  10217. if (k < loader_ctx->v128_const_num) {
  10218. V128 *v128_consts_new;
  10219. /* Try to reallocate memory with a smaller size */
  10220. if ((v128_consts_new =
  10221. wasm_runtime_malloc((uint32)sizeof(V128) * k))) {
  10222. bh_memcpy_s(v128_consts_new, (uint32)sizeof(V128) * k,
  10223. v128_consts_old, (uint32)sizeof(V128) * k);
  10224. /* Free the old memory */
  10225. wasm_runtime_free(v128_consts_old);
  10226. loader_ctx->v128_consts = v128_consts_new;
  10227. loader_ctx->v128_const_max_num = k;
  10228. }
  10229. loader_ctx->v128_const_num = k;
  10230. }
  10231. }
  10232. if (loader_ctx->i32_const_num > 0) {
  10233. int32 *i32_consts_old = loader_ctx->i32_consts;
  10234. /* Sort the i32 consts */
  10235. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10236. cmp_i32_const);
  10237. /* Remove the duplicated i32 consts */
  10238. uint32 k = 1;
  10239. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10240. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10241. i32_consts_old[k++] = i32_consts_old[i];
  10242. }
  10243. }
  10244. if (k < loader_ctx->i32_const_num) {
  10245. int32 *i32_consts_new;
  10246. /* Try to reallocate memory with a smaller size */
  10247. if ((i32_consts_new =
  10248. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10249. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10250. i32_consts_old, (uint32)sizeof(int32) * k);
  10251. /* Free the old memory */
  10252. wasm_runtime_free(i32_consts_old);
  10253. loader_ctx->i32_consts = i32_consts_new;
  10254. loader_ctx->i32_const_max_num = k;
  10255. }
  10256. loader_ctx->i32_const_num = k;
  10257. }
  10258. }
  10259. }
  10260. #endif
  10261. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10262. while (p < p_end) {
  10263. opcode = *p++;
  10264. #if WASM_ENABLE_FAST_INTERP != 0
  10265. p_org = p;
  10266. disable_emit = false;
  10267. emit_label(opcode);
  10268. #endif
  10269. switch (opcode) {
  10270. case WASM_OP_UNREACHABLE:
  10271. RESET_STACK();
  10272. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10273. break;
  10274. case WASM_OP_NOP:
  10275. #if WASM_ENABLE_FAST_INTERP != 0
  10276. skip_label();
  10277. #endif
  10278. break;
  10279. case WASM_OP_IF:
  10280. {
  10281. #if WASM_ENABLE_FAST_INTERP != 0
  10282. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10283. int32 available_stack_cell =
  10284. (int32)(loader_ctx->stack_cell_num
  10285. - parent_block->stack_cell_num);
  10286. if (available_stack_cell <= 0
  10287. && parent_block->is_stack_polymorphic)
  10288. if_condition_available = false;
  10289. else
  10290. if_condition_available = true;
  10291. PRESERVE_LOCAL_FOR_BLOCK();
  10292. #endif
  10293. #if WASM_ENABLE_GC == 0
  10294. POP_I32();
  10295. #endif
  10296. goto handle_op_block_and_loop;
  10297. }
  10298. case WASM_OP_BLOCK:
  10299. case WASM_OP_LOOP:
  10300. #if WASM_ENABLE_EXCE_HANDLING != 0
  10301. case WASM_OP_TRY:
  10302. if (opcode == WASM_OP_TRY) {
  10303. /*
  10304. * keep track of exception handlers to account for
  10305. * memory allocation
  10306. */
  10307. func->exception_handler_count++;
  10308. /*
  10309. * try is a block
  10310. * do nothing special, but execution continues to
  10311. * to handle_op_block_and_loop,
  10312. * and that be pushes the csp
  10313. */
  10314. }
  10315. #endif
  10316. #if WASM_ENABLE_FAST_INTERP != 0
  10317. PRESERVE_LOCAL_FOR_BLOCK();
  10318. #endif
  10319. handle_op_block_and_loop:
  10320. {
  10321. uint8 value_type;
  10322. BlockType block_type;
  10323. #if WASM_ENABLE_FAST_INTERP != 0
  10324. uint32 available_params = 0;
  10325. #endif
  10326. CHECK_BUF(p, p_end, 1);
  10327. value_type = read_uint8(p);
  10328. if (is_byte_a_type(value_type)) {
  10329. /* If the first byte is one of these special values:
  10330. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10331. * the single return value. */
  10332. block_type.is_value_type = true;
  10333. block_type.u.value_type.type = value_type;
  10334. #if WASM_ENABLE_WAMR_COMPILER != 0
  10335. if (value_type == VALUE_TYPE_V128)
  10336. module->is_simd_used = true;
  10337. else if (value_type == VALUE_TYPE_FUNCREF
  10338. || value_type == VALUE_TYPE_EXTERNREF)
  10339. module->is_ref_types_used = true;
  10340. #endif
  10341. #if WASM_ENABLE_GC != 0
  10342. if (value_type != VALUE_TYPE_VOID) {
  10343. p_org = p;
  10344. p--;
  10345. if (!resolve_value_type((const uint8 **)&p, p_end,
  10346. module, module->type_count,
  10347. &need_ref_type_map,
  10348. &wasm_ref_type, false,
  10349. error_buf, error_buf_size)) {
  10350. goto fail;
  10351. }
  10352. if (need_ref_type_map) {
  10353. block_type.u.value_type.ref_type_map.index = 0;
  10354. if (!(block_type.u.value_type.ref_type_map
  10355. .ref_type = reftype_set_insert(
  10356. module->ref_type_set, &wasm_ref_type,
  10357. error_buf, error_buf_size))) {
  10358. goto fail;
  10359. }
  10360. }
  10361. /* Set again as the type might be changed, e.g.
  10362. (ref null any) to anyref */
  10363. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10364. #if WASM_ENABLE_FAST_INTERP == 0
  10365. while (p_org < p) {
  10366. #if WASM_ENABLE_DEBUG_INTERP != 0
  10367. if (!record_fast_op(module, p_org, *p_org,
  10368. error_buf, error_buf_size)) {
  10369. goto fail;
  10370. }
  10371. #endif
  10372. /* Ignore extra bytes for interpreter */
  10373. *p_org++ = WASM_OP_NOP;
  10374. }
  10375. #endif
  10376. }
  10377. #endif /* end of WASM_ENABLE_GC != 0 */
  10378. }
  10379. else {
  10380. int32 type_index;
  10381. /* Resolve the leb128 encoded type index as block type */
  10382. p--;
  10383. p_org = p - 1;
  10384. pb_read_leb_int32(p, p_end, type_index);
  10385. if (!check_function_type(module, type_index, error_buf,
  10386. error_buf_size)) {
  10387. goto fail;
  10388. }
  10389. block_type.is_value_type = false;
  10390. block_type.u.type =
  10391. (WASMFuncType *)module->types[type_index];
  10392. #if WASM_ENABLE_FAST_INTERP == 0
  10393. /* If block use type index as block type, change the opcode
  10394. * to new extended opcode so that interpreter can resolve
  10395. * the block quickly.
  10396. */
  10397. #if WASM_ENABLE_DEBUG_INTERP != 0
  10398. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10399. error_buf_size)) {
  10400. goto fail;
  10401. }
  10402. #endif
  10403. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10404. #endif
  10405. }
  10406. #if WASM_ENABLE_GC != 0
  10407. if (opcode == WASM_OP_IF) {
  10408. POP_I32();
  10409. }
  10410. #endif
  10411. /* Pop block parameters from stack */
  10412. if (BLOCK_HAS_PARAM(block_type)) {
  10413. WASMFuncType *wasm_type = block_type.u.type;
  10414. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10415. #if WASM_ENABLE_FAST_INTERP != 0
  10416. uint32 cell_num;
  10417. available_params = block_type.u.type->param_count;
  10418. #endif
  10419. for (i = 0; i < block_type.u.type->param_count; i++) {
  10420. int32 available_stack_cell =
  10421. (int32)(loader_ctx->stack_cell_num
  10422. - cur_block->stack_cell_num);
  10423. if (available_stack_cell <= 0
  10424. && cur_block->is_stack_polymorphic) {
  10425. #if WASM_ENABLE_FAST_INTERP != 0
  10426. available_params = i;
  10427. #endif
  10428. break;
  10429. }
  10430. POP_TYPE(
  10431. wasm_type->types[wasm_type->param_count - i - 1]);
  10432. #if WASM_ENABLE_FAST_INTERP != 0
  10433. /* decrease the frame_offset pointer accordingly to keep
  10434. * consistent with frame_ref stack */
  10435. cell_num = wasm_value_type_cell_num(
  10436. wasm_type->types[wasm_type->param_count - i - 1]);
  10437. loader_ctx->frame_offset -= cell_num;
  10438. if (loader_ctx->frame_offset
  10439. < loader_ctx->frame_offset_bottom) {
  10440. LOG_DEBUG(
  10441. "frame_offset underflow, roll back and "
  10442. "let following stack checker report it\n");
  10443. loader_ctx->frame_offset += cell_num;
  10444. pending_exception = true;
  10445. break;
  10446. }
  10447. #endif
  10448. }
  10449. }
  10450. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10451. block_type, p);
  10452. /* Pass parameters to block */
  10453. if (BLOCK_HAS_PARAM(block_type)) {
  10454. WASMFuncType *func_type = block_type.u.type;
  10455. #if WASM_ENABLE_GC != 0
  10456. WASMRefType *ref_type;
  10457. uint32 j = 0;
  10458. #endif
  10459. for (i = 0; i < func_type->param_count; i++) {
  10460. #if WASM_ENABLE_FAST_INTERP != 0
  10461. uint32 cell_num =
  10462. wasm_value_type_cell_num(func_type->types[i]);
  10463. if (i >= available_params) {
  10464. /* make sure enough space */
  10465. if (loader_ctx->p_code_compiled == NULL) {
  10466. loader_ctx->frame_offset += cell_num;
  10467. if (!check_offset_push(loader_ctx, error_buf,
  10468. error_buf_size))
  10469. goto fail;
  10470. /* for following dummy value assignment */
  10471. loader_ctx->frame_offset -= cell_num;
  10472. }
  10473. /* If there isn't enough data on stack, push a dummy
  10474. * offset to keep the stack consistent with
  10475. * frame_ref.
  10476. * Since the stack is already in polymorphic state,
  10477. * the opcode will not be executed, so the dummy
  10478. * offset won't cause any error */
  10479. for (uint32 n = 0; n < cell_num; n++) {
  10480. *loader_ctx->frame_offset++ = 0;
  10481. }
  10482. }
  10483. else {
  10484. loader_ctx->frame_offset += cell_num;
  10485. }
  10486. #endif
  10487. #if WASM_ENABLE_GC != 0
  10488. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10489. bh_assert(func_type->ref_type_maps[j].index == i);
  10490. ref_type = func_type->ref_type_maps[j].ref_type;
  10491. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10492. ref_type,
  10493. wasm_reftype_struct_size(ref_type));
  10494. j++;
  10495. }
  10496. #endif
  10497. PUSH_TYPE(func_type->types[i]);
  10498. }
  10499. }
  10500. #if WASM_ENABLE_FAST_INTERP != 0
  10501. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10502. skip_label();
  10503. if (BLOCK_HAS_PARAM(block_type)) {
  10504. /* Make sure params are in dynamic space */
  10505. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10506. error_buf_size))
  10507. goto fail;
  10508. }
  10509. if (opcode == WASM_OP_LOOP) {
  10510. (loader_ctx->frame_csp - 1)->code_compiled =
  10511. loader_ctx->p_code_compiled;
  10512. }
  10513. }
  10514. #if WASM_ENABLE_EXCE_HANDLING != 0
  10515. else if (opcode == WASM_OP_TRY) {
  10516. skip_label();
  10517. }
  10518. #endif
  10519. else if (opcode == WASM_OP_IF) {
  10520. BranchBlock *block = loader_ctx->frame_csp - 1;
  10521. /* If block has parameters, we should make sure they are in
  10522. * dynamic space. Otherwise, when else branch is missing,
  10523. * the later opcode may consume incorrect operand offset.
  10524. * Spec case:
  10525. * (func (export "params-id") (param i32) (result i32)
  10526. * (i32.const 1)
  10527. * (i32.const 2)
  10528. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10529. * (then)) (i32.add)
  10530. * )
  10531. *
  10532. * So we should emit a copy instruction before the if.
  10533. *
  10534. * And we also need to save the parameter offsets and
  10535. * recover them before entering else branch.
  10536. *
  10537. */
  10538. if (BLOCK_HAS_PARAM(block_type)) {
  10539. uint64 size;
  10540. /* In polymorphic state, there may be no if condition on
  10541. * the stack, so the offset may not emitted */
  10542. if (if_condition_available) {
  10543. /* skip the if condition operand offset */
  10544. wasm_loader_emit_backspace(loader_ctx,
  10545. sizeof(int16));
  10546. }
  10547. /* skip the if label */
  10548. skip_label();
  10549. /* Emit a copy instruction */
  10550. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10551. error_buf_size))
  10552. goto fail;
  10553. /* Emit the if instruction */
  10554. emit_label(opcode);
  10555. /* Emit the new condition operand offset */
  10556. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10557. /* Save top param_count values of frame_offset stack, so
  10558. * that we can recover it before executing else branch
  10559. */
  10560. size = sizeof(int16)
  10561. * (uint64)block_type.u.type->param_cell_num;
  10562. if (!(block->param_frame_offsets = loader_malloc(
  10563. size, error_buf, error_buf_size)))
  10564. goto fail;
  10565. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10566. loader_ctx->frame_offset
  10567. - size / sizeof(int16),
  10568. (uint32)size);
  10569. }
  10570. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10571. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10572. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10573. }
  10574. #endif
  10575. break;
  10576. }
  10577. #if WASM_ENABLE_EXCE_HANDLING != 0
  10578. case WASM_OP_THROW:
  10579. {
  10580. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10581. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10582. uint8 label_type = cur_block->label_type;
  10583. uint32 tag_index = 0;
  10584. pb_read_leb_int32(p, p_end, tag_index);
  10585. /* check validity of tag_index against module->tag_count */
  10586. /* check tag index is within the tag index space */
  10587. if (tag_index >= module->import_tag_count + module->tag_count) {
  10588. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10589. tag_index);
  10590. goto fail;
  10591. }
  10592. /* the tag_type is stored in either the WASMTag (section tags)
  10593. * or WASMTagImport (import tag) */
  10594. WASMFuncType *tag_type = NULL;
  10595. if (tag_index < module->import_tag_count) {
  10596. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10597. }
  10598. else {
  10599. tag_type =
  10600. module->tags[tag_index - module->import_tag_count]
  10601. ->tag_type;
  10602. }
  10603. if (tag_type->result_count != 0) {
  10604. set_error_buf(error_buf, error_buf_size,
  10605. "tag type signature does not return void");
  10606. goto fail;
  10607. }
  10608. int32 available_stack_cell =
  10609. (int32)(loader_ctx->stack_cell_num
  10610. - cur_block->stack_cell_num);
  10611. int32 tti;
  10612. /* Check stack values match return types by comparing tag param
  10613. * types with stack cells */
  10614. uint8 *frame_ref = loader_ctx->frame_ref;
  10615. #if WASM_ENABLE_GC != 0
  10616. WASMRefTypeMap *frame_reftype_map =
  10617. loader_ctx->frame_reftype_map;
  10618. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10619. /* Temporarily set these values since they may be used in
  10620. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10621. param_reftype_maps = tag_type->ref_type_maps;
  10622. /* For tag_type function, it shouldn't have result_count = 0 */
  10623. param_reftype_map_count = tag_type->ref_type_map_count;
  10624. param_count = tag_type->param_count;
  10625. #endif
  10626. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10627. #if WASM_ENABLE_GC != 0
  10628. local_type = tag_type->types[tti];
  10629. local_idx = tti;
  10630. /* Get the wasm_ref_type if the local_type is multibyte
  10631. type */
  10632. GET_LOCAL_REFTYPE();
  10633. #endif
  10634. if (!check_stack_top_values(
  10635. loader_ctx, frame_ref, available_stack_cell,
  10636. #if WASM_ENABLE_GC != 0
  10637. frame_reftype_map, frame_reftype_map_num,
  10638. #endif
  10639. tag_type->types[tti],
  10640. #if WASM_ENABLE_GC != 0
  10641. &wasm_ref_type,
  10642. #endif
  10643. error_buf, error_buf_size)) {
  10644. snprintf(error_buf, error_buf_size,
  10645. "type mismatch: instruction requires [%s] but "
  10646. "stack has [%s]",
  10647. tag_type->param_count > 0
  10648. ? type2str(tag_type->types[tti])
  10649. : "",
  10650. available_stack_cell > 0
  10651. ? type2str(*(loader_ctx->frame_ref - 1))
  10652. : "");
  10653. goto fail;
  10654. }
  10655. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10656. available_stack_cell -=
  10657. wasm_value_type_cell_num(tag_type->types[tti]);
  10658. }
  10659. #if WASM_ENABLE_GC != 0
  10660. /* Restore the values */
  10661. param_reftype_maps = func->func_type->ref_type_maps;
  10662. param_reftype_map_count = func->func_type->ref_type_map_count;
  10663. param_count = func->func_type->param_count;
  10664. #endif
  10665. /* throw is stack polymorphic */
  10666. (void)label_type;
  10667. RESET_STACK();
  10668. break;
  10669. }
  10670. case WASM_OP_RETHROW:
  10671. {
  10672. /* must be done before checking branch block */
  10673. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10674. /* check the target catching block: LABEL_TYPE_CATCH */
  10675. if (!(frame_csp_tmp =
  10676. check_branch_block(loader_ctx, &p, p_end, opcode,
  10677. error_buf, error_buf_size)))
  10678. goto fail;
  10679. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10680. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10681. /* trap according to spectest (rethrow.wast) */
  10682. set_error_buf(error_buf, error_buf_size,
  10683. "invalid rethrow label");
  10684. goto fail;
  10685. }
  10686. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10687. uint8 label_type = cur_block->label_type;
  10688. (void)label_type;
  10689. /* rethrow is stack polymorphic */
  10690. RESET_STACK();
  10691. break;
  10692. }
  10693. case WASM_OP_DELEGATE:
  10694. {
  10695. /* check target block is valid */
  10696. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10697. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10698. goto fail;
  10699. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10700. uint8 label_type = cur_block->label_type;
  10701. (void)label_type;
  10702. /* DELEGATE ends the block */
  10703. POP_CSP();
  10704. break;
  10705. }
  10706. case WASM_OP_CATCH:
  10707. {
  10708. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10709. uint8 label_type = cur_block->label_type;
  10710. uint32 tag_index = 0;
  10711. pb_read_leb_int32(p, p_end, tag_index);
  10712. /* check validity of tag_index against module->tag_count */
  10713. /* check tag index is within the tag index space */
  10714. if (tag_index >= module->import_tag_count + module->tag_count) {
  10715. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10716. __FUNCTION__);
  10717. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10718. goto fail;
  10719. }
  10720. /* the tag_type is stored in either the WASMTag (section tags)
  10721. * or WASMTagImport (import tag) */
  10722. WASMFuncType *func_type = NULL;
  10723. if (tag_index < module->import_tag_count) {
  10724. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10725. }
  10726. else {
  10727. func_type =
  10728. module->tags[tag_index - module->import_tag_count]
  10729. ->tag_type;
  10730. }
  10731. if (func_type->result_count != 0) {
  10732. set_error_buf(error_buf, error_buf_size,
  10733. "tag type signature does not return void");
  10734. goto fail;
  10735. }
  10736. /* check validity of current label (expect LABEL_TYPE_TRY or
  10737. * LABEL_TYPE_CATCH) */
  10738. if ((LABEL_TYPE_CATCH != label_type)
  10739. && (LABEL_TYPE_TRY != label_type)) {
  10740. set_error_buf(error_buf, error_buf_size,
  10741. "Unexpected block sequence encountered.");
  10742. goto fail;
  10743. }
  10744. /*
  10745. * replace frame_csp by LABEL_TYPE_CATCH
  10746. */
  10747. cur_block->label_type = LABEL_TYPE_CATCH;
  10748. /* RESET_STACK removes the values pushed in TRY or previous
  10749. * CATCH Blocks */
  10750. RESET_STACK();
  10751. #if WASM_ENABLE_GC != 0
  10752. WASMRefType *ref_type;
  10753. uint32 j = 0;
  10754. #endif
  10755. /* push types on the stack according to caught type */
  10756. for (i = 0; i < func_type->param_count; i++) {
  10757. #if WASM_ENABLE_GC != 0
  10758. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10759. bh_assert(func_type->ref_type_maps[j].index == i);
  10760. ref_type = func_type->ref_type_maps[j].ref_type;
  10761. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10762. ref_type,
  10763. wasm_reftype_struct_size(ref_type));
  10764. j++;
  10765. }
  10766. #endif
  10767. PUSH_TYPE(func_type->types[i]);
  10768. }
  10769. break;
  10770. }
  10771. case WASM_OP_CATCH_ALL:
  10772. {
  10773. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10774. /* expecting a TRY or CATCH, anything else will be considered an
  10775. * error */
  10776. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10777. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10778. set_error_buf(error_buf, error_buf_size,
  10779. "Unexpected block sequence encountered.");
  10780. goto fail;
  10781. }
  10782. /* no immediates */
  10783. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10784. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10785. /* RESET_STACK removes the values pushed in TRY or previous
  10786. * CATCH Blocks */
  10787. RESET_STACK();
  10788. /* catch_all has no tagtype and therefore no parameters */
  10789. break;
  10790. }
  10791. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10792. case WASM_OP_ELSE:
  10793. handle_op_else:
  10794. {
  10795. BranchBlock *block = NULL;
  10796. BlockType block_type;
  10797. if (loader_ctx->csp_num < 2
  10798. /* the matched if isn't found */
  10799. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10800. /* duplicated else is found */
  10801. || (loader_ctx->frame_csp - 1)->else_addr) {
  10802. set_error_buf(
  10803. error_buf, error_buf_size,
  10804. "opcode else found without matched opcode if");
  10805. goto fail;
  10806. }
  10807. block = loader_ctx->frame_csp - 1;
  10808. /* check whether if branch's stack matches its result type */
  10809. if (!check_block_stack(loader_ctx, block, error_buf,
  10810. error_buf_size))
  10811. goto fail;
  10812. block->else_addr = p - 1;
  10813. block_type = block->block_type;
  10814. #if WASM_ENABLE_GC != 0
  10815. if (!wasm_loader_init_local_use_masks(
  10816. loader_ctx, local_count, error_buf, error_buf_size)) {
  10817. goto fail;
  10818. }
  10819. #endif
  10820. #if WASM_ENABLE_FAST_INTERP != 0
  10821. /* if the result of if branch is in local or const area, add a
  10822. * copy op */
  10823. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10824. error_buf, error_buf_size)) {
  10825. goto fail;
  10826. }
  10827. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10828. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10829. #endif
  10830. RESET_STACK();
  10831. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10832. /* Pass parameters to if-false branch */
  10833. if (BLOCK_HAS_PARAM(block_type)) {
  10834. for (i = 0; i < block_type.u.type->param_count; i++)
  10835. PUSH_TYPE(block_type.u.type->types[i]);
  10836. }
  10837. #if WASM_ENABLE_FAST_INTERP != 0
  10838. /* Recover top param_count values of frame_offset stack */
  10839. if (BLOCK_HAS_PARAM((block_type))) {
  10840. uint32 size;
  10841. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10842. bh_memcpy_s(loader_ctx->frame_offset, size,
  10843. block->param_frame_offsets, size);
  10844. loader_ctx->frame_offset += (size / sizeof(int16));
  10845. }
  10846. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10847. #endif
  10848. break;
  10849. }
  10850. case WASM_OP_END:
  10851. {
  10852. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10853. /* check whether block stack matches its result type */
  10854. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10855. error_buf_size))
  10856. goto fail;
  10857. /* if there is no else branch, make a virtual else opcode for
  10858. easier integrity check and to copy the correct results to
  10859. the block return address for fast-interp mode:
  10860. change if block from `if ... end` to `if ... else end` */
  10861. if (cur_block->label_type == LABEL_TYPE_IF
  10862. && !cur_block->else_addr) {
  10863. opcode = WASM_OP_ELSE;
  10864. p--;
  10865. #if WASM_ENABLE_FAST_INTERP != 0
  10866. p_org = p;
  10867. skip_label();
  10868. disable_emit = false;
  10869. emit_label(opcode);
  10870. #endif
  10871. goto handle_op_else;
  10872. }
  10873. POP_CSP();
  10874. #if WASM_ENABLE_FAST_INTERP != 0
  10875. skip_label();
  10876. /* copy the result to the block return address */
  10877. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10878. error_buf, error_buf_size)) {
  10879. /* it could be tmp frame_csp allocated from opcode like
  10880. * OP_BR and not counted in loader_ctx->csp_num, it won't
  10881. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  10882. * to free the loader_ctx->frame_csp if fails */
  10883. free_label_patch_list(loader_ctx->frame_csp);
  10884. goto fail;
  10885. }
  10886. apply_label_patch(loader_ctx, 0, PATCH_END);
  10887. free_label_patch_list(loader_ctx->frame_csp);
  10888. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10889. int32 idx;
  10890. uint8 ret_type;
  10891. emit_label(WASM_OP_RETURN);
  10892. for (idx = (int32)func->func_type->result_count - 1;
  10893. idx >= 0; idx--) {
  10894. ret_type = *(func->func_type->types
  10895. + func->func_type->param_count + idx);
  10896. POP_OFFSET_TYPE(ret_type);
  10897. }
  10898. }
  10899. #endif
  10900. if (loader_ctx->csp_num > 0) {
  10901. loader_ctx->frame_csp->end_addr = p - 1;
  10902. }
  10903. else {
  10904. /* end of function block, function will return */
  10905. if (p < p_end) {
  10906. set_error_buf(error_buf, error_buf_size,
  10907. "section size mismatch");
  10908. goto fail;
  10909. }
  10910. }
  10911. #if WASM_ENABLE_FAST_INTERP != 0
  10912. if (pending_exception) {
  10913. set_error_buf(
  10914. error_buf, error_buf_size,
  10915. "There is a pending exception needs to be handled");
  10916. goto fail;
  10917. }
  10918. #endif
  10919. break;
  10920. }
  10921. case WASM_OP_BR:
  10922. {
  10923. if (!(frame_csp_tmp =
  10924. check_branch_block(loader_ctx, &p, p_end, opcode,
  10925. error_buf, error_buf_size)))
  10926. goto fail;
  10927. RESET_STACK();
  10928. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10929. break;
  10930. }
  10931. case WASM_OP_BR_IF:
  10932. {
  10933. POP_I32();
  10934. if (!(frame_csp_tmp =
  10935. check_branch_block(loader_ctx, &p, p_end, opcode,
  10936. error_buf, error_buf_size)))
  10937. goto fail;
  10938. break;
  10939. }
  10940. case WASM_OP_BR_TABLE:
  10941. {
  10942. uint32 depth = 0, default_arity, arity = 0;
  10943. BranchBlock *target_block;
  10944. BlockType *target_block_type;
  10945. #if WASM_ENABLE_FAST_INTERP == 0
  10946. BrTableCache *br_table_cache = NULL;
  10947. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10948. uint32 j;
  10949. #endif
  10950. pb_read_leb_uint32(p, p_end, count);
  10951. #if WASM_ENABLE_FAST_INTERP != 0
  10952. emit_uint32(loader_ctx, count);
  10953. #endif
  10954. POP_I32();
  10955. /* Get each depth and check it */
  10956. p_org = p;
  10957. for (i = 0; i <= count; i++) {
  10958. pb_read_leb_uint32(p, p_end, depth);
  10959. bh_assert(loader_ctx->csp_num > 0);
  10960. if (loader_ctx->csp_num - 1 < depth) {
  10961. set_error_buf(error_buf, error_buf_size,
  10962. "unknown label, "
  10963. "unexpected end of section or function");
  10964. goto fail;
  10965. }
  10966. }
  10967. p = p_org;
  10968. /* Get the default block's arity */
  10969. target_block = loader_ctx->frame_csp - (depth + 1);
  10970. target_block_type = &target_block->block_type;
  10971. default_arity = block_type_get_arity(target_block_type,
  10972. target_block->label_type);
  10973. #if WASM_ENABLE_FAST_INTERP == 0
  10974. p_depth_begin = p_depth = p;
  10975. #endif
  10976. for (i = 0; i <= count; i++) {
  10977. p_org = p;
  10978. pb_read_leb_uint32(p, p_end, depth);
  10979. p = p_org;
  10980. /* Get the target block's arity and check it */
  10981. target_block = loader_ctx->frame_csp - (depth + 1);
  10982. target_block_type = &target_block->block_type;
  10983. arity = block_type_get_arity(target_block_type,
  10984. target_block->label_type);
  10985. if (arity != default_arity) {
  10986. set_error_buf(error_buf, error_buf_size,
  10987. "type mismatch: br_table targets must "
  10988. "all use same result type");
  10989. goto fail;
  10990. }
  10991. if (!(frame_csp_tmp =
  10992. check_branch_block(loader_ctx, &p, p_end, opcode,
  10993. error_buf, error_buf_size))) {
  10994. goto fail;
  10995. }
  10996. #if WASM_ENABLE_FAST_INTERP == 0
  10997. if (br_table_cache) {
  10998. br_table_cache->br_depths[i] = depth;
  10999. }
  11000. else {
  11001. if (depth > 255) {
  11002. /* The depth cannot be stored in one byte,
  11003. create br_table cache to store each depth */
  11004. #if WASM_ENABLE_DEBUG_INTERP != 0
  11005. if (!record_fast_op(module, p_opcode, *p_opcode,
  11006. error_buf, error_buf_size)) {
  11007. goto fail;
  11008. }
  11009. #endif
  11010. if (!(br_table_cache = loader_malloc(
  11011. offsetof(BrTableCache, br_depths)
  11012. + sizeof(uint32)
  11013. * (uint64)(count + 1),
  11014. error_buf, error_buf_size))) {
  11015. goto fail;
  11016. }
  11017. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  11018. br_table_cache->br_table_op_addr = p_opcode;
  11019. br_table_cache->br_count = count;
  11020. /* Copy previous depths which are one byte */
  11021. for (j = 0; j < i; j++) {
  11022. br_table_cache->br_depths[j] = p_depth_begin[j];
  11023. }
  11024. br_table_cache->br_depths[i] = depth;
  11025. bh_list_insert(module->br_table_cache_list,
  11026. br_table_cache);
  11027. }
  11028. else {
  11029. /* The depth can be stored in one byte, use the
  11030. byte of the leb to store it */
  11031. *p_depth++ = (uint8)depth;
  11032. }
  11033. }
  11034. #endif
  11035. }
  11036. #if WASM_ENABLE_FAST_INTERP == 0
  11037. /* Set the tailing bytes to nop */
  11038. if (br_table_cache)
  11039. p_depth = p_depth_begin;
  11040. while (p_depth < p)
  11041. *p_depth++ = WASM_OP_NOP;
  11042. #endif
  11043. RESET_STACK();
  11044. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11045. break;
  11046. }
  11047. case WASM_OP_RETURN:
  11048. {
  11049. WASMFuncType *func_type = func->func_type;
  11050. int32 idx;
  11051. uint8 ret_type;
  11052. #if WASM_ENABLE_GC != 0
  11053. uint32 j = func_type->ref_type_map_count - 1;
  11054. #endif
  11055. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  11056. idx--) {
  11057. ret_type =
  11058. *(func_type->types + func_type->param_count + idx);
  11059. #if WASM_ENABLE_GC != 0
  11060. if (wasm_is_type_multi_byte_type(ret_type)) {
  11061. WASMRefType *ref_type =
  11062. func_type->ref_type_maps[j].ref_type;
  11063. bh_assert(func_type->ref_type_maps[j].index
  11064. == func_type->param_count + idx);
  11065. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11066. ref_type,
  11067. wasm_reftype_struct_size(ref_type));
  11068. j--;
  11069. }
  11070. #endif
  11071. #if WASM_ENABLE_FAST_INTERP != 0
  11072. /* emit the offset after return opcode */
  11073. POP_OFFSET_TYPE(ret_type);
  11074. #endif
  11075. POP_TYPE(ret_type);
  11076. }
  11077. RESET_STACK();
  11078. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11079. break;
  11080. }
  11081. case WASM_OP_CALL:
  11082. #if WASM_ENABLE_TAIL_CALL != 0
  11083. case WASM_OP_RETURN_CALL:
  11084. #endif
  11085. #if WASM_ENABLE_GC != 0
  11086. case WASM_OP_CALL_REF:
  11087. case WASM_OP_RETURN_CALL_REF:
  11088. #endif
  11089. {
  11090. WASMFuncType *func_type;
  11091. uint8 type;
  11092. int32 idx;
  11093. #if WASM_ENABLE_GC != 0
  11094. WASMRefType *ref_type;
  11095. uint32 type_idx1;
  11096. int32 j;
  11097. #endif
  11098. #if WASM_ENABLE_GC != 0
  11099. if (opcode == WASM_OP_CALL_REF
  11100. || opcode == WASM_OP_RETURN_CALL_REF) {
  11101. pb_read_leb_uint32(p, p_end, type_idx1);
  11102. if (!check_type_index(module, module->type_count, type_idx1,
  11103. error_buf, error_buf_size)) {
  11104. goto fail;
  11105. }
  11106. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  11107. set_error_buf(error_buf, error_buf_size,
  11108. "unknown function type");
  11109. goto fail;
  11110. }
  11111. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  11112. &type_idx, error_buf,
  11113. error_buf_size)) {
  11114. goto fail;
  11115. }
  11116. if (type == VALUE_TYPE_ANY) {
  11117. type_idx = type_idx1;
  11118. }
  11119. if (!check_type_index(module, module->type_count, type_idx,
  11120. error_buf, error_buf_size)) {
  11121. goto fail;
  11122. }
  11123. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  11124. set_error_buf(error_buf, error_buf_size,
  11125. "unknown function type");
  11126. goto fail;
  11127. }
  11128. if (!wasm_func_type_is_super_of(
  11129. (WASMFuncType *)module->types[type_idx1],
  11130. (WASMFuncType *)module->types[type_idx])) {
  11131. set_error_buf(error_buf, error_buf_size,
  11132. "function type mismatch");
  11133. goto fail;
  11134. }
  11135. func_type = (WASMFuncType *)module->types[type_idx];
  11136. }
  11137. else
  11138. #endif
  11139. {
  11140. pb_read_leb_uint32(p, p_end, func_idx);
  11141. #if WASM_ENABLE_FAST_INTERP != 0
  11142. /* we need to emit func_idx before arguments */
  11143. emit_uint32(loader_ctx, func_idx);
  11144. #endif
  11145. if (!check_function_index(module, func_idx, error_buf,
  11146. error_buf_size)) {
  11147. goto fail;
  11148. }
  11149. if (func_idx < module->import_function_count)
  11150. func_type = module->import_functions[func_idx]
  11151. .u.function.func_type;
  11152. else
  11153. func_type =
  11154. module
  11155. ->functions[func_idx
  11156. - module->import_function_count]
  11157. ->func_type;
  11158. }
  11159. if (func_type->param_count > 0) {
  11160. #if WASM_ENABLE_GC != 0
  11161. j = (int32)(func_type->result_ref_type_maps
  11162. - func_type->ref_type_maps - 1);
  11163. #endif
  11164. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11165. idx--) {
  11166. #if WASM_ENABLE_GC != 0
  11167. if (wasm_is_type_multi_byte_type(
  11168. func_type->types[idx])) {
  11169. ref_type = func_type->ref_type_maps[j].ref_type;
  11170. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11171. ref_type,
  11172. wasm_reftype_struct_size(ref_type));
  11173. j--;
  11174. }
  11175. #endif
  11176. #if WASM_ENABLE_FAST_INTERP != 0
  11177. POP_OFFSET_TYPE(func_type->types[idx]);
  11178. #endif
  11179. POP_TYPE(func_type->types[idx]);
  11180. }
  11181. }
  11182. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11183. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  11184. #endif
  11185. #if WASM_ENABLE_GC != 0
  11186. j = (int32)(func_type->result_ref_type_maps
  11187. - func_type->ref_type_maps);
  11188. #endif
  11189. for (i = 0; i < func_type->result_count; i++) {
  11190. #if WASM_ENABLE_GC != 0
  11191. if (wasm_is_type_multi_byte_type(
  11192. func_type->types[func_type->param_count + i])) {
  11193. ref_type = func_type->ref_type_maps[j].ref_type;
  11194. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11195. ref_type,
  11196. wasm_reftype_struct_size(ref_type));
  11197. j++;
  11198. }
  11199. #endif
  11200. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11201. #if WASM_ENABLE_FAST_INTERP != 0
  11202. /* Here we emit each return value's dynamic_offset. But
  11203. * in fact these offsets are continuous, so interpreter
  11204. * only need to get the first return value's offset.
  11205. */
  11206. PUSH_OFFSET_TYPE(
  11207. func_type->types[func_type->param_count + i]);
  11208. #endif
  11209. }
  11210. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11211. }
  11212. else {
  11213. #if WASM_ENABLE_GC == 0
  11214. if (func_type->result_count
  11215. != func->func_type->result_count) {
  11216. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11217. "type mismatch: expect ",
  11218. func->func_type->result_count,
  11219. " return values but got other");
  11220. goto fail;
  11221. }
  11222. for (i = 0; i < func_type->result_count; i++) {
  11223. type = func->func_type
  11224. ->types[func->func_type->param_count + i];
  11225. if (func_type->types[func_type->param_count + i]
  11226. != type) {
  11227. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11228. "type mismatch: expect ",
  11229. type2str(type), " but got other");
  11230. goto fail;
  11231. }
  11232. }
  11233. #else
  11234. if (!wasm_func_type_result_is_subtype_of(
  11235. func_type, func->func_type, module->types,
  11236. module->type_count)) {
  11237. set_error_buf(
  11238. error_buf, error_buf_size,
  11239. "type mismatch: invalid func result types");
  11240. goto fail;
  11241. }
  11242. #endif
  11243. RESET_STACK();
  11244. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11245. }
  11246. #endif
  11247. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11248. || WASM_ENABLE_WAMR_COMPILER != 0
  11249. func->has_op_func_call = true;
  11250. #endif
  11251. (void)type;
  11252. break;
  11253. }
  11254. /*
  11255. * if disable reference type: call_indirect typeidx, 0x00
  11256. * if enable reference type: call_indirect typeidx, tableidx
  11257. */
  11258. case WASM_OP_CALL_INDIRECT:
  11259. #if WASM_ENABLE_TAIL_CALL != 0
  11260. case WASM_OP_RETURN_CALL_INDIRECT:
  11261. #endif
  11262. {
  11263. int32 idx;
  11264. WASMFuncType *func_type;
  11265. uint32 tbl_elem_type;
  11266. #if WASM_ENABLE_GC != 0
  11267. WASMRefType *elem_ref_type = NULL;
  11268. #endif
  11269. pb_read_leb_uint32(p, p_end, type_idx);
  11270. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11271. #if WASM_ENABLE_WAMR_COMPILER != 0
  11272. if (p + 1 < p_end && *p != 0x00) {
  11273. /*
  11274. * Any non-0x00 byte requires the ref types proposal.
  11275. * This is different from checking the table_idx value
  11276. * since `0x80 0x00` etc. are all valid encodings of zero.
  11277. */
  11278. module->is_ref_types_used = true;
  11279. }
  11280. #endif
  11281. pb_read_leb_uint32(p, p_end, table_idx);
  11282. #else
  11283. CHECK_BUF(p, p_end, 1);
  11284. table_idx = read_uint8(p);
  11285. #endif
  11286. if (!check_table_index(module, table_idx, error_buf,
  11287. error_buf_size)) {
  11288. goto fail;
  11289. }
  11290. tbl_elem_type =
  11291. table_idx < module->import_table_count
  11292. ? module->import_tables[table_idx]
  11293. .u.table.table_type.elem_type
  11294. : module->tables[table_idx - module->import_table_count]
  11295. .table_type.elem_type;
  11296. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11297. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11298. set_error_buf_v(error_buf, error_buf_size,
  11299. "type mismatch: instruction requires table "
  11300. "of functions but table %u has externref",
  11301. table_idx);
  11302. goto fail;
  11303. }
  11304. #elif WASM_ENABLE_GC != 0
  11305. /* Table element must match type ref null func */
  11306. elem_ref_type =
  11307. table_idx < module->import_table_count
  11308. ? module->import_tables[table_idx]
  11309. .u.table.table_type.elem_ref_type
  11310. : module->tables[table_idx - module->import_table_count]
  11311. .table_type.elem_ref_type;
  11312. if (!wasm_reftype_is_subtype_of(
  11313. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11314. module->types, module->type_count)) {
  11315. set_error_buf_v(error_buf, error_buf_size,
  11316. "type mismatch: instruction requires "
  11317. "reference type t match type ref null func"
  11318. "in table %u",
  11319. table_idx);
  11320. goto fail;
  11321. }
  11322. #else
  11323. (void)tbl_elem_type;
  11324. #endif
  11325. #if WASM_ENABLE_FAST_INTERP != 0
  11326. /* we need to emit before arguments */
  11327. #if WASM_ENABLE_TAIL_CALL != 0
  11328. emit_byte(loader_ctx, opcode);
  11329. #endif
  11330. emit_uint32(loader_ctx, type_idx);
  11331. emit_uint32(loader_ctx, table_idx);
  11332. #endif
  11333. #if WASM_ENABLE_MEMORY64 != 0
  11334. table_elem_idx_type = is_table_64bit(module, table_idx)
  11335. ? VALUE_TYPE_I64
  11336. : VALUE_TYPE_I32;
  11337. #endif
  11338. /* skip elem idx */
  11339. POP_TBL_ELEM_IDX();
  11340. if (!check_function_type(module, type_idx, error_buf,
  11341. error_buf_size)) {
  11342. goto fail;
  11343. }
  11344. func_type = (WASMFuncType *)module->types[type_idx];
  11345. if (func_type->param_count > 0) {
  11346. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11347. idx--) {
  11348. #if WASM_ENABLE_FAST_INTERP != 0
  11349. POP_OFFSET_TYPE(func_type->types[idx]);
  11350. #endif
  11351. POP_TYPE(func_type->types[idx]);
  11352. }
  11353. }
  11354. #if WASM_ENABLE_TAIL_CALL != 0
  11355. if (opcode == WASM_OP_CALL_INDIRECT) {
  11356. #endif
  11357. for (i = 0; i < func_type->result_count; i++) {
  11358. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11359. #if WASM_ENABLE_FAST_INTERP != 0
  11360. PUSH_OFFSET_TYPE(
  11361. func_type->types[func_type->param_count + i]);
  11362. #endif
  11363. }
  11364. #if WASM_ENABLE_TAIL_CALL != 0
  11365. }
  11366. else {
  11367. uint8 type;
  11368. if (func_type->result_count
  11369. != func->func_type->result_count) {
  11370. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11371. "type mismatch: expect ",
  11372. func->func_type->result_count,
  11373. " return values but got other");
  11374. goto fail;
  11375. }
  11376. for (i = 0; i < func_type->result_count; i++) {
  11377. type = func->func_type
  11378. ->types[func->func_type->param_count + i];
  11379. if (func_type->types[func_type->param_count + i]
  11380. != type) {
  11381. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11382. "type mismatch: expect ",
  11383. type2str(type), " but got other");
  11384. goto fail;
  11385. }
  11386. }
  11387. RESET_STACK();
  11388. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11389. }
  11390. #endif
  11391. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11392. || WASM_ENABLE_WAMR_COMPILER != 0
  11393. func->has_op_func_call = true;
  11394. #endif
  11395. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11396. func->has_op_call_indirect = true;
  11397. #endif
  11398. break;
  11399. }
  11400. case WASM_OP_DROP:
  11401. {
  11402. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11403. int32 available_stack_cell =
  11404. (int32)(loader_ctx->stack_cell_num
  11405. - cur_block->stack_cell_num);
  11406. if (available_stack_cell <= 0
  11407. && !cur_block->is_stack_polymorphic) {
  11408. set_error_buf(error_buf, error_buf_size,
  11409. "type mismatch, opcode drop was found "
  11410. "but stack was empty");
  11411. goto fail;
  11412. }
  11413. if (available_stack_cell > 0) {
  11414. #if WASM_ENABLE_GC != 0
  11415. if (wasm_is_type_multi_byte_type(
  11416. *(loader_ctx->frame_ref - 1))) {
  11417. bh_assert((int32)(loader_ctx->reftype_map_num
  11418. - cur_block->reftype_map_num)
  11419. > 0);
  11420. loader_ctx->frame_reftype_map--;
  11421. loader_ctx->reftype_map_num--;
  11422. }
  11423. #endif
  11424. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11425. loader_ctx->frame_ref--;
  11426. loader_ctx->stack_cell_num--;
  11427. #if WASM_ENABLE_FAST_INTERP != 0
  11428. skip_label();
  11429. loader_ctx->frame_offset--;
  11430. if ((*(loader_ctx->frame_offset)
  11431. > loader_ctx->start_dynamic_offset)
  11432. && (*(loader_ctx->frame_offset)
  11433. < loader_ctx->max_dynamic_offset))
  11434. loader_ctx->dynamic_offset--;
  11435. #endif
  11436. }
  11437. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11438. loader_ctx->frame_ref -= 2;
  11439. loader_ctx->stack_cell_num -= 2;
  11440. #if WASM_ENABLE_FAST_INTERP == 0
  11441. *(p - 1) = WASM_OP_DROP_64;
  11442. #endif
  11443. #if WASM_ENABLE_FAST_INTERP != 0
  11444. skip_label();
  11445. loader_ctx->frame_offset -= 2;
  11446. if ((*(loader_ctx->frame_offset)
  11447. > loader_ctx->start_dynamic_offset)
  11448. && (*(loader_ctx->frame_offset)
  11449. < loader_ctx->max_dynamic_offset))
  11450. loader_ctx->dynamic_offset -= 2;
  11451. #endif
  11452. }
  11453. #if WASM_ENABLE_SIMD != 0
  11454. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11455. || (WASM_ENABLE_FAST_INTERP != 0)
  11456. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11457. loader_ctx->frame_ref -= 4;
  11458. loader_ctx->stack_cell_num -= 4;
  11459. #if WASM_ENABLE_FAST_INTERP != 0
  11460. skip_label();
  11461. loader_ctx->frame_offset -= 4;
  11462. if ((*(loader_ctx->frame_offset)
  11463. > loader_ctx->start_dynamic_offset)
  11464. && (*(loader_ctx->frame_offset)
  11465. < loader_ctx->max_dynamic_offset))
  11466. loader_ctx->dynamic_offset -= 4;
  11467. #endif
  11468. }
  11469. #endif
  11470. #endif
  11471. else {
  11472. set_error_buf(error_buf, error_buf_size,
  11473. "type mismatch");
  11474. goto fail;
  11475. }
  11476. }
  11477. else {
  11478. #if WASM_ENABLE_FAST_INTERP != 0
  11479. skip_label();
  11480. #endif
  11481. }
  11482. break;
  11483. }
  11484. case WASM_OP_SELECT:
  11485. {
  11486. uint8 ref_type;
  11487. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11488. int32 available_stack_cell;
  11489. #if WASM_ENABLE_FAST_INTERP != 0
  11490. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11491. #endif
  11492. POP_I32();
  11493. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11494. - cur_block->stack_cell_num);
  11495. if (available_stack_cell <= 0
  11496. && !cur_block->is_stack_polymorphic) {
  11497. set_error_buf(error_buf, error_buf_size,
  11498. "type mismatch or invalid result arity, "
  11499. "opcode select was found "
  11500. "but stack was empty");
  11501. goto fail;
  11502. }
  11503. if (available_stack_cell > 0) {
  11504. switch (*(loader_ctx->frame_ref - 1)) {
  11505. case VALUE_TYPE_I32:
  11506. case VALUE_TYPE_F32:
  11507. case VALUE_TYPE_ANY:
  11508. break;
  11509. case VALUE_TYPE_I64:
  11510. case VALUE_TYPE_F64:
  11511. #if WASM_ENABLE_FAST_INTERP == 0
  11512. *(p - 1) = WASM_OP_SELECT_64;
  11513. #else
  11514. if (loader_ctx->p_code_compiled) {
  11515. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11516. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11517. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11518. *(void **)(p_code_compiled_tmp
  11519. - sizeof(void *)) =
  11520. handle_table[opcode_tmp];
  11521. #elif UINTPTR_MAX == UINT64_MAX
  11522. /* emit int32 relative offset in 64-bit target
  11523. */
  11524. int32 offset =
  11525. (int32)((uint8 *)handle_table[opcode_tmp]
  11526. - (uint8 *)handle_table[0]);
  11527. *(int32 *)(p_code_compiled_tmp
  11528. - sizeof(int32)) = offset;
  11529. #else
  11530. /* emit uint32 label address in 32-bit target */
  11531. *(uint32 *)(p_code_compiled_tmp
  11532. - sizeof(uint32)) =
  11533. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11534. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11535. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11536. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11537. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11538. #else
  11539. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11540. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11541. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11542. }
  11543. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11544. break;
  11545. #if WASM_ENABLE_SIMD != 0
  11546. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11547. || (WASM_ENABLE_FAST_INTERP != 0)
  11548. case VALUE_TYPE_V128:
  11549. #if WASM_ENABLE_FAST_INTERP == 0
  11550. *(p - 1) = WASM_OP_SELECT_128;
  11551. #else
  11552. if (loader_ctx->p_code_compiled) {
  11553. uint8 opcode_tmp = WASM_OP_SELECT_128;
  11554. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11555. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11556. *(void **)(p_code_compiled_tmp
  11557. - sizeof(void *)) =
  11558. handle_table[opcode_tmp];
  11559. #elif UINTPTR_MAX == UINT64_MAX
  11560. /* emit int32 relative offset in 64-bit target
  11561. */
  11562. int32 offset =
  11563. (int32)((uint8 *)handle_table[opcode_tmp]
  11564. - (uint8 *)handle_table[0]);
  11565. *(int32 *)(p_code_compiled_tmp
  11566. - sizeof(int32)) = offset;
  11567. #else
  11568. /* emit uint32 label address in 32-bit target */
  11569. *(uint32 *)(p_code_compiled_tmp
  11570. - sizeof(uint32)) =
  11571. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11572. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11573. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11574. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11575. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11576. #else
  11577. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11578. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11579. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11580. }
  11581. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11582. break;
  11583. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  11584. (WASM_ENABLE_FAST_INTERP != 0) */
  11585. #endif /* WASM_ENABLE_SIMD != 0 */
  11586. default:
  11587. {
  11588. set_error_buf(error_buf, error_buf_size,
  11589. "type mismatch");
  11590. goto fail;
  11591. }
  11592. }
  11593. ref_type = *(loader_ctx->frame_ref - 1);
  11594. #if WASM_ENABLE_FAST_INTERP != 0
  11595. POP_OFFSET_TYPE(ref_type);
  11596. POP_TYPE(ref_type);
  11597. POP_OFFSET_TYPE(ref_type);
  11598. POP_TYPE(ref_type);
  11599. PUSH_OFFSET_TYPE(ref_type);
  11600. PUSH_TYPE(ref_type);
  11601. #else
  11602. POP2_AND_PUSH(ref_type, ref_type);
  11603. #endif
  11604. }
  11605. else {
  11606. #if WASM_ENABLE_FAST_INTERP != 0
  11607. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11608. #endif
  11609. PUSH_TYPE(VALUE_TYPE_ANY);
  11610. }
  11611. break;
  11612. }
  11613. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11614. case WASM_OP_SELECT_T:
  11615. {
  11616. uint8 vec_len, type;
  11617. #if WASM_ENABLE_GC != 0
  11618. WASMRefType *ref_type = NULL;
  11619. #endif
  11620. #if WASM_ENABLE_FAST_INTERP != 0
  11621. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11622. #endif
  11623. pb_read_leb_uint32(p, p_end, vec_len);
  11624. if (vec_len != 1) {
  11625. /* typed select must have exactly one result */
  11626. set_error_buf(error_buf, error_buf_size,
  11627. "invalid result arity");
  11628. goto fail;
  11629. }
  11630. #if WASM_ENABLE_GC == 0
  11631. CHECK_BUF(p, p_end, 1);
  11632. type = read_uint8(p);
  11633. if (!is_valid_value_type_for_interpreter(type)) {
  11634. set_error_buf(error_buf, error_buf_size,
  11635. "unknown value type");
  11636. goto fail;
  11637. }
  11638. #else
  11639. p_org = p + 1;
  11640. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11641. module->type_count, &need_ref_type_map,
  11642. &wasm_ref_type, false, error_buf,
  11643. error_buf_size)) {
  11644. goto fail;
  11645. }
  11646. type = wasm_ref_type.ref_type;
  11647. if (need_ref_type_map) {
  11648. if (!(ref_type = reftype_set_insert(
  11649. module->ref_type_set, &wasm_ref_type, error_buf,
  11650. error_buf_size))) {
  11651. goto fail;
  11652. }
  11653. }
  11654. #if WASM_ENABLE_FAST_INTERP == 0
  11655. while (p_org < p) {
  11656. #if WASM_ENABLE_DEBUG_INTERP != 0
  11657. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11658. error_buf_size)) {
  11659. goto fail;
  11660. }
  11661. #endif
  11662. /* Ignore extra bytes for interpreter */
  11663. *p_org++ = WASM_OP_NOP;
  11664. }
  11665. #endif
  11666. #endif /* end of WASM_ENABLE_GC == 0 */
  11667. POP_I32();
  11668. #if WASM_ENABLE_FAST_INTERP != 0
  11669. if (loader_ctx->p_code_compiled) {
  11670. uint8 opcode_tmp = WASM_OP_SELECT;
  11671. if (type == VALUE_TYPE_V128) {
  11672. #if WASM_ENABLE_JIT != 0 \
  11673. || WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_SIMD != 0
  11674. opcode_tmp = WASM_OP_SELECT_128;
  11675. #else
  11676. set_error_buf(error_buf, error_buf_size,
  11677. "v128 value type requires simd feature");
  11678. #endif
  11679. }
  11680. else {
  11681. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11682. opcode_tmp = WASM_OP_SELECT_64;
  11683. #if WASM_ENABLE_GC != 0
  11684. if (wasm_is_type_reftype(type))
  11685. opcode_tmp = WASM_OP_SELECT_T;
  11686. #endif
  11687. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11688. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11689. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11690. handle_table[opcode_tmp];
  11691. #else
  11692. #if UINTPTR_MAX == UINT64_MAX
  11693. /* emit int32 relative offset in 64-bit target */
  11694. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11695. - (uint8 *)handle_table[0]);
  11696. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11697. offset;
  11698. #else
  11699. /* emit uint32 label address in 32-bit target */
  11700. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11701. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11702. #endif
  11703. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11704. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11705. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11706. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11707. #else
  11708. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11709. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11710. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11711. }
  11712. }
  11713. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11714. POP_REF(type);
  11715. #if WASM_ENABLE_GC != 0
  11716. if (need_ref_type_map) {
  11717. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11718. wasm_reftype_struct_size(ref_type));
  11719. }
  11720. #endif
  11721. POP_REF(type);
  11722. #if WASM_ENABLE_GC != 0
  11723. if (need_ref_type_map) {
  11724. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11725. wasm_reftype_struct_size(ref_type));
  11726. }
  11727. #endif
  11728. PUSH_REF(type);
  11729. #if WASM_ENABLE_WAMR_COMPILER != 0
  11730. module->is_ref_types_used = true;
  11731. #endif
  11732. (void)vec_len;
  11733. break;
  11734. }
  11735. /* table.get x. tables[x]. [it] -> [t] */
  11736. /* table.set x. tables[x]. [it t] -> [] */
  11737. case WASM_OP_TABLE_GET:
  11738. case WASM_OP_TABLE_SET:
  11739. {
  11740. uint8 decl_ref_type;
  11741. #if WASM_ENABLE_GC != 0
  11742. WASMRefType *ref_type;
  11743. #endif
  11744. pb_read_leb_uint32(p, p_end, table_idx);
  11745. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11746. #if WASM_ENABLE_GC != 0
  11747. (void **)&ref_type,
  11748. #else
  11749. NULL,
  11750. #endif
  11751. error_buf, error_buf_size))
  11752. goto fail;
  11753. #if WASM_ENABLE_GC != 0
  11754. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11755. bh_assert(ref_type);
  11756. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11757. ref_type, wasm_reftype_struct_size(ref_type));
  11758. }
  11759. #endif
  11760. #if WASM_ENABLE_FAST_INTERP != 0
  11761. emit_uint32(loader_ctx, table_idx);
  11762. #endif
  11763. #if WASM_ENABLE_MEMORY64 != 0
  11764. table_elem_idx_type = is_table_64bit(module, table_idx)
  11765. ? VALUE_TYPE_I64
  11766. : VALUE_TYPE_I32;
  11767. #endif
  11768. if (opcode == WASM_OP_TABLE_GET) {
  11769. POP_TBL_ELEM_IDX();
  11770. #if WASM_ENABLE_FAST_INTERP != 0
  11771. PUSH_OFFSET_TYPE(decl_ref_type);
  11772. #endif
  11773. PUSH_TYPE(decl_ref_type);
  11774. }
  11775. else {
  11776. #if WASM_ENABLE_FAST_INTERP != 0
  11777. POP_OFFSET_TYPE(decl_ref_type);
  11778. #endif
  11779. POP_TYPE(decl_ref_type);
  11780. POP_TBL_ELEM_IDX();
  11781. }
  11782. #if WASM_ENABLE_WAMR_COMPILER != 0
  11783. module->is_ref_types_used = true;
  11784. #endif
  11785. break;
  11786. }
  11787. case WASM_OP_REF_NULL:
  11788. {
  11789. uint8 ref_type;
  11790. #if WASM_ENABLE_GC == 0
  11791. CHECK_BUF(p, p_end, 1);
  11792. ref_type = read_uint8(p);
  11793. if (ref_type != VALUE_TYPE_FUNCREF
  11794. && ref_type != VALUE_TYPE_EXTERNREF) {
  11795. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11796. goto fail;
  11797. }
  11798. #else
  11799. pb_read_leb_int32(p, p_end, heap_type);
  11800. if (heap_type >= 0) {
  11801. if (!check_type_index(module, module->type_count, heap_type,
  11802. error_buf, error_buf_size)) {
  11803. goto fail;
  11804. }
  11805. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11806. true, heap_type);
  11807. ref_type = wasm_ref_type.ref_type;
  11808. }
  11809. else {
  11810. if (!wasm_is_valid_heap_type(heap_type)) {
  11811. set_error_buf(error_buf, error_buf_size,
  11812. "unknown type");
  11813. goto fail;
  11814. }
  11815. ref_type = (uint8)((int32)0x80 + heap_type);
  11816. }
  11817. #endif /* end of WASM_ENABLE_GC == 0 */
  11818. #if WASM_ENABLE_FAST_INTERP != 0
  11819. PUSH_OFFSET_TYPE(ref_type);
  11820. #endif
  11821. PUSH_TYPE(ref_type);
  11822. #if WASM_ENABLE_WAMR_COMPILER != 0
  11823. module->is_ref_types_used = true;
  11824. #endif
  11825. break;
  11826. }
  11827. case WASM_OP_REF_IS_NULL:
  11828. {
  11829. #if WASM_ENABLE_GC == 0
  11830. #if WASM_ENABLE_FAST_INTERP != 0
  11831. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11832. int32 block_stack_cell_num =
  11833. (int32)(loader_ctx->stack_cell_num
  11834. - cur_block->stack_cell_num);
  11835. if (block_stack_cell_num <= 0) {
  11836. if (!cur_block->is_stack_polymorphic) {
  11837. set_error_buf(
  11838. error_buf, error_buf_size,
  11839. "type mismatch: expect data but stack was empty");
  11840. goto fail;
  11841. }
  11842. }
  11843. else {
  11844. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11845. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11846. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11847. if (!wasm_loader_pop_frame_ref_offset(
  11848. loader_ctx, *(loader_ctx->frame_ref - 1),
  11849. error_buf, error_buf_size)) {
  11850. goto fail;
  11851. }
  11852. }
  11853. else {
  11854. set_error_buf(error_buf, error_buf_size,
  11855. "type mismatch");
  11856. goto fail;
  11857. }
  11858. }
  11859. #else
  11860. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11861. error_buf, error_buf_size)
  11862. && !wasm_loader_pop_frame_ref(loader_ctx,
  11863. VALUE_TYPE_EXTERNREF,
  11864. error_buf, error_buf_size)) {
  11865. goto fail;
  11866. }
  11867. #endif
  11868. #else /* else of WASM_ENABLE_GC == 0 */
  11869. uint8 type;
  11870. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11871. error_buf, error_buf_size)) {
  11872. goto fail;
  11873. }
  11874. #endif
  11875. PUSH_I32();
  11876. #if WASM_ENABLE_WAMR_COMPILER != 0
  11877. module->is_ref_types_used = true;
  11878. #endif
  11879. break;
  11880. }
  11881. case WASM_OP_REF_FUNC:
  11882. {
  11883. pb_read_leb_uint32(p, p_end, func_idx);
  11884. if (!check_function_index(module, func_idx, error_buf,
  11885. error_buf_size)) {
  11886. goto fail;
  11887. }
  11888. /* Refer to a forward-declared function:
  11889. the function must be an import, exported, or present in
  11890. a table elem segment or global initializer to be used as
  11891. the operand to ref.func */
  11892. if (func_idx >= module->import_function_count) {
  11893. WASMTableSeg *table_seg = module->table_segments;
  11894. bool func_declared = false;
  11895. uint32 j;
  11896. for (i = 0; i < module->global_count; i++) {
  11897. if (module->globals[i].type.val_type
  11898. == VALUE_TYPE_FUNCREF
  11899. && module->globals[i].init_expr.init_expr_type
  11900. == INIT_EXPR_TYPE_FUNCREF_CONST
  11901. && module->globals[i].init_expr.u.u32 == func_idx) {
  11902. func_declared = true;
  11903. break;
  11904. }
  11905. }
  11906. if (!func_declared) {
  11907. /* Check whether the function is declared in table segs,
  11908. note that it doesn't matter whether the table seg's
  11909. mode is passive, active or declarative. */
  11910. for (i = 0; i < module->table_seg_count;
  11911. i++, table_seg++) {
  11912. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11913. #if WASM_ENABLE_GC != 0
  11914. /* elem type is (ref null? func) or
  11915. (ref null? $t) */
  11916. || ((table_seg->elem_type
  11917. == REF_TYPE_HT_NON_NULLABLE
  11918. || table_seg->elem_type
  11919. == REF_TYPE_HT_NULLABLE)
  11920. && (table_seg->elem_ref_type->ref_ht_common
  11921. .heap_type
  11922. == HEAP_TYPE_FUNC
  11923. || table_seg->elem_ref_type
  11924. ->ref_ht_common.heap_type
  11925. > 0))
  11926. #endif
  11927. ) {
  11928. for (j = 0; j < table_seg->value_count; j++) {
  11929. if (table_seg->init_values[j].u.ref_index
  11930. == func_idx) {
  11931. func_declared = true;
  11932. break;
  11933. }
  11934. }
  11935. }
  11936. }
  11937. }
  11938. if (!func_declared) {
  11939. /* Check whether the function is exported */
  11940. for (i = 0; i < module->export_count; i++) {
  11941. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11942. && module->exports[i].index == func_idx) {
  11943. func_declared = true;
  11944. break;
  11945. }
  11946. }
  11947. }
  11948. if (!func_declared) {
  11949. set_error_buf(error_buf, error_buf_size,
  11950. "undeclared function reference");
  11951. goto fail;
  11952. }
  11953. }
  11954. #if WASM_ENABLE_FAST_INTERP != 0
  11955. emit_uint32(loader_ctx, func_idx);
  11956. #endif
  11957. #if WASM_ENABLE_GC == 0
  11958. PUSH_FUNCREF();
  11959. #else
  11960. if (func_idx < module->import_function_count)
  11961. type_idx =
  11962. module->import_functions[func_idx].u.function.type_idx;
  11963. else
  11964. type_idx = module
  11965. ->functions[func_idx
  11966. - module->import_function_count]
  11967. ->type_idx;
  11968. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11969. false, type_idx);
  11970. PUSH_REF(wasm_ref_type.ref_type);
  11971. #endif
  11972. #if WASM_ENABLE_WAMR_COMPILER != 0
  11973. module->is_ref_types_used = true;
  11974. #endif
  11975. break;
  11976. }
  11977. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11978. #if WASM_ENABLE_GC != 0
  11979. case WASM_OP_REF_AS_NON_NULL:
  11980. case WASM_OP_BR_ON_NULL:
  11981. {
  11982. uint8 type;
  11983. WASMRefType ref_type;
  11984. /* POP (ref null ht) and get the converted (ref ht) */
  11985. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11986. error_buf, error_buf_size)) {
  11987. goto fail;
  11988. }
  11989. if (opcode == WASM_OP_BR_ON_NULL) {
  11990. if (!(frame_csp_tmp =
  11991. check_branch_block(loader_ctx, &p, p_end, opcode,
  11992. error_buf, error_buf_size))) {
  11993. goto fail;
  11994. }
  11995. #if WASM_ENABLE_FAST_INTERP != 0
  11996. disable_emit = true;
  11997. #endif
  11998. }
  11999. /* PUSH the converted (ref ht) */
  12000. if (type != VALUE_TYPE_ANY) {
  12001. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  12002. sizeof(WASMRefType));
  12003. }
  12004. PUSH_REF(type);
  12005. break;
  12006. }
  12007. case WASM_OP_BR_ON_NON_NULL:
  12008. {
  12009. uint8 type;
  12010. WASMRefType ref_type;
  12011. uint32 available_stack_cell =
  12012. loader_ctx->stack_cell_num
  12013. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  12014. /* POP (ref null ht) and get the converted (ref ht) */
  12015. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  12016. error_buf, error_buf_size)) {
  12017. goto fail;
  12018. }
  12019. #if WASM_ENABLE_FAST_INTERP != 0
  12020. disable_emit = true;
  12021. #endif
  12022. /* Temporarily PUSH back (ref ht), check brach block and
  12023. then POP it */
  12024. if (available_stack_cell
  12025. > 0) { /* stack isn't in polymorphic state */
  12026. if (type != VALUE_TYPE_ANY) {
  12027. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12028. &ref_type, sizeof(WASMRefType));
  12029. }
  12030. PUSH_REF(type);
  12031. }
  12032. if (!(frame_csp_tmp =
  12033. check_branch_block(loader_ctx, &p, p_end, opcode,
  12034. error_buf, error_buf_size))) {
  12035. goto fail;
  12036. }
  12037. if (available_stack_cell
  12038. > 0) { /* stack isn't in polymorphic state */
  12039. POP_REF(type);
  12040. #if WASM_ENABLE_FAST_INTERP != 0
  12041. /* Erase the opnd offset emitted by POP_REF() */
  12042. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12043. #endif
  12044. }
  12045. break;
  12046. }
  12047. case WASM_OP_REF_EQ:
  12048. POP_REF(REF_TYPE_EQREF);
  12049. POP_REF(REF_TYPE_EQREF);
  12050. PUSH_I32();
  12051. break;
  12052. #endif /* end of WASM_ENABLE_GC != 0 */
  12053. case WASM_OP_GET_LOCAL:
  12054. {
  12055. p_org = p - 1;
  12056. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12057. PUSH_TYPE(local_type);
  12058. #if WASM_ENABLE_GC != 0
  12059. /* Cannot get a non-nullable and unset local */
  12060. if (local_idx >= param_count
  12061. && wasm_is_reftype_htref_non_nullable(local_type)
  12062. && !wasm_loader_get_local_status(loader_ctx,
  12063. local_idx - param_count)) {
  12064. set_error_buf(error_buf, error_buf_size,
  12065. "uninitialized local");
  12066. goto fail;
  12067. }
  12068. #endif
  12069. #if WASM_ENABLE_FAST_INTERP != 0
  12070. /* Get Local is optimized out */
  12071. skip_label();
  12072. disable_emit = true;
  12073. operand_offset = local_offset;
  12074. PUSH_OFFSET_TYPE(local_type);
  12075. #else
  12076. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12077. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12078. if (local_offset < 0x80
  12079. #if WASM_ENABLE_GC != 0
  12080. && !wasm_is_type_reftype(local_type)
  12081. #endif
  12082. ) {
  12083. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  12084. if (is_32bit_type(local_type)) {
  12085. *p_org++ = (uint8)local_offset;
  12086. }
  12087. else {
  12088. *p_org++ = (uint8)(local_offset | 0x80);
  12089. }
  12090. while (p_org < p) {
  12091. *p_org++ = WASM_OP_NOP;
  12092. }
  12093. }
  12094. #endif
  12095. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12096. break;
  12097. }
  12098. case WASM_OP_SET_LOCAL:
  12099. {
  12100. p_org = p - 1;
  12101. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12102. #if WASM_ENABLE_FAST_INTERP != 0
  12103. if (!(preserve_referenced_local(
  12104. loader_ctx, opcode, local_offset, local_type,
  12105. &preserve_local, error_buf, error_buf_size)))
  12106. goto fail;
  12107. if (local_offset < 256
  12108. #if WASM_ENABLE_GC != 0
  12109. && !wasm_is_type_reftype(local_type)
  12110. #endif
  12111. ) {
  12112. skip_label();
  12113. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  12114. if (loader_ctx->p_code_compiled)
  12115. STORE_U16(loader_ctx->p_code_compiled - 2,
  12116. local_offset);
  12117. loader_ctx->frame_offset--;
  12118. loader_ctx->dynamic_offset--;
  12119. }
  12120. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  12121. if (loader_ctx->p_code_compiled)
  12122. STORE_U16(loader_ctx->p_code_compiled - 2,
  12123. local_offset);
  12124. loader_ctx->frame_offset -= 2;
  12125. loader_ctx->dynamic_offset -= 2;
  12126. }
  12127. else {
  12128. if (is_32bit_type(local_type)) {
  12129. emit_label(EXT_OP_SET_LOCAL_FAST);
  12130. emit_byte(loader_ctx, (uint8)local_offset);
  12131. }
  12132. else if (is_64bit_type(local_type)) {
  12133. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  12134. emit_byte(loader_ctx, (uint8)local_offset);
  12135. }
  12136. #if WASM_ENABLE_SIMDE != 0
  12137. else if (local_type == VALUE_TYPE_V128) {
  12138. emit_label(EXT_OP_SET_LOCAL_FAST_V128);
  12139. emit_byte(loader_ctx, (uint8)local_offset);
  12140. }
  12141. #endif
  12142. else {
  12143. set_error_buf(error_buf, error_buf_size,
  12144. "unknown local type");
  12145. goto fail;
  12146. }
  12147. POP_OFFSET_TYPE(local_type);
  12148. }
  12149. }
  12150. else { /* local index larger than 255, reserve leb */
  12151. emit_uint32(loader_ctx, local_idx);
  12152. POP_OFFSET_TYPE(local_type);
  12153. }
  12154. #else
  12155. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12156. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12157. if (local_offset < 0x80
  12158. #if WASM_ENABLE_GC != 0
  12159. && !wasm_is_type_reftype(local_type)
  12160. #endif
  12161. ) {
  12162. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  12163. if (is_32bit_type(local_type)) {
  12164. *p_org++ = (uint8)local_offset;
  12165. }
  12166. else {
  12167. *p_org++ = (uint8)(local_offset | 0x80);
  12168. }
  12169. while (p_org < p) {
  12170. *p_org++ = WASM_OP_NOP;
  12171. }
  12172. }
  12173. #endif
  12174. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12175. #if WASM_ENABLE_GC != 0
  12176. if (local_idx >= param_count) {
  12177. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12178. }
  12179. #endif
  12180. POP_TYPE(local_type);
  12181. break;
  12182. }
  12183. case WASM_OP_TEE_LOCAL:
  12184. {
  12185. p_org = p - 1;
  12186. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12187. #if WASM_ENABLE_FAST_INTERP != 0
  12188. /* If the stack is in polymorphic state, do fake pop and push on
  12189. offset stack to keep the depth of offset stack to be the
  12190. same with ref stack */
  12191. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12192. if (cur_block->is_stack_polymorphic) {
  12193. POP_OFFSET_TYPE(local_type);
  12194. PUSH_OFFSET_TYPE(local_type);
  12195. }
  12196. #endif
  12197. POP_TYPE(local_type);
  12198. PUSH_TYPE(local_type);
  12199. #if WASM_ENABLE_FAST_INTERP != 0
  12200. if (!(preserve_referenced_local(
  12201. loader_ctx, opcode, local_offset, local_type,
  12202. &preserve_local, error_buf, error_buf_size)))
  12203. goto fail;
  12204. if (local_offset < 256
  12205. #if WASM_ENABLE_GC != 0
  12206. && !wasm_is_type_reftype(local_type)
  12207. #endif
  12208. ) {
  12209. skip_label();
  12210. if (is_32bit_type(local_type)) {
  12211. emit_label(EXT_OP_TEE_LOCAL_FAST);
  12212. emit_byte(loader_ctx, (uint8)local_offset);
  12213. }
  12214. #if WASM_ENABLE_SIMDE != 0
  12215. else if (local_type == VALUE_TYPE_V128) {
  12216. emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
  12217. emit_byte(loader_ctx, (uint8)local_offset);
  12218. }
  12219. #endif
  12220. else {
  12221. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  12222. emit_byte(loader_ctx, (uint8)local_offset);
  12223. }
  12224. }
  12225. else { /* local index larger than 255, reserve leb */
  12226. emit_uint32(loader_ctx, local_idx);
  12227. }
  12228. emit_operand(loader_ctx,
  12229. *(loader_ctx->frame_offset
  12230. - wasm_value_type_cell_num(local_type)));
  12231. #else
  12232. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12233. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12234. if (local_offset < 0x80
  12235. #if WASM_ENABLE_GC != 0
  12236. && !wasm_is_type_reftype(local_type)
  12237. #endif
  12238. ) {
  12239. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  12240. if (is_32bit_type(local_type)) {
  12241. *p_org++ = (uint8)local_offset;
  12242. }
  12243. else {
  12244. *p_org++ = (uint8)(local_offset | 0x80);
  12245. }
  12246. while (p_org < p) {
  12247. *p_org++ = WASM_OP_NOP;
  12248. }
  12249. }
  12250. #endif
  12251. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12252. #if WASM_ENABLE_GC != 0
  12253. if (local_idx >= param_count) {
  12254. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12255. }
  12256. #endif
  12257. break;
  12258. }
  12259. case WASM_OP_GET_GLOBAL:
  12260. {
  12261. #if WASM_ENABLE_GC != 0
  12262. WASMRefType *ref_type;
  12263. #endif
  12264. p_org = p - 1;
  12265. pb_read_leb_uint32(p, p_end, global_idx);
  12266. if (global_idx >= global_count) {
  12267. set_error_buf(error_buf, error_buf_size, "unknown global");
  12268. goto fail;
  12269. }
  12270. global_type = global_idx < module->import_global_count
  12271. ? module->import_globals[global_idx]
  12272. .u.global.type.val_type
  12273. : module
  12274. ->globals[global_idx
  12275. - module->import_global_count]
  12276. .type.val_type;
  12277. #if WASM_ENABLE_GC != 0
  12278. ref_type =
  12279. global_idx < module->import_global_count
  12280. ? module->import_globals[global_idx].u.global.ref_type
  12281. : module
  12282. ->globals[global_idx
  12283. - module->import_global_count]
  12284. .ref_type;
  12285. if (wasm_is_type_multi_byte_type(global_type)) {
  12286. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12287. wasm_reftype_struct_size(ref_type));
  12288. }
  12289. #endif
  12290. PUSH_TYPE(global_type);
  12291. #if WASM_ENABLE_FAST_INTERP == 0
  12292. if (global_type == VALUE_TYPE_I64
  12293. || global_type == VALUE_TYPE_F64) {
  12294. #if WASM_ENABLE_DEBUG_INTERP != 0
  12295. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12296. error_buf_size)) {
  12297. goto fail;
  12298. }
  12299. #endif
  12300. *p_org = WASM_OP_GET_GLOBAL_64;
  12301. }
  12302. #else /* else of WASM_ENABLE_FAST_INTERP */
  12303. if (global_type == VALUE_TYPE_I64
  12304. || global_type == VALUE_TYPE_F64) {
  12305. skip_label();
  12306. emit_label(WASM_OP_GET_GLOBAL_64);
  12307. }
  12308. #if WASM_ENABLE_SIMDE != 0
  12309. if (global_type == VALUE_TYPE_V128) {
  12310. skip_label();
  12311. emit_label(WASM_OP_GET_GLOBAL_V128);
  12312. }
  12313. #endif /* end of WASM_ENABLE_SIMDE */
  12314. emit_uint32(loader_ctx, global_idx);
  12315. PUSH_OFFSET_TYPE(global_type);
  12316. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12317. break;
  12318. }
  12319. case WASM_OP_SET_GLOBAL:
  12320. {
  12321. bool is_mutable = false;
  12322. #if WASM_ENABLE_GC != 0
  12323. WASMRefType *ref_type;
  12324. #endif
  12325. p_org = p - 1;
  12326. pb_read_leb_uint32(p, p_end, global_idx);
  12327. if (global_idx >= global_count) {
  12328. set_error_buf(error_buf, error_buf_size, "unknown global");
  12329. goto fail;
  12330. }
  12331. is_mutable = global_idx < module->import_global_count
  12332. ? module->import_globals[global_idx]
  12333. .u.global.type.is_mutable
  12334. : module
  12335. ->globals[global_idx
  12336. - module->import_global_count]
  12337. .type.is_mutable;
  12338. if (!is_mutable) {
  12339. #if WASM_ENABLE_GC == 0
  12340. set_error_buf(error_buf, error_buf_size,
  12341. "global is immutable");
  12342. #else
  12343. set_error_buf(error_buf, error_buf_size,
  12344. "immutable global");
  12345. #endif
  12346. goto fail;
  12347. }
  12348. global_type = global_idx < module->import_global_count
  12349. ? module->import_globals[global_idx]
  12350. .u.global.type.val_type
  12351. : module
  12352. ->globals[global_idx
  12353. - module->import_global_count]
  12354. .type.val_type;
  12355. #if WASM_ENABLE_GC != 0
  12356. ref_type =
  12357. global_idx < module->import_global_count
  12358. ? module->import_globals[global_idx].u.global.ref_type
  12359. : module
  12360. ->globals[global_idx
  12361. - module->import_global_count]
  12362. .ref_type;
  12363. if (wasm_is_type_multi_byte_type(global_type)) {
  12364. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12365. wasm_reftype_struct_size(ref_type));
  12366. }
  12367. #endif
  12368. #if WASM_ENABLE_FAST_INTERP == 0
  12369. if (global_type == VALUE_TYPE_I64
  12370. || global_type == VALUE_TYPE_F64) {
  12371. #if WASM_ENABLE_DEBUG_INTERP != 0
  12372. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12373. error_buf_size)) {
  12374. goto fail;
  12375. }
  12376. #endif
  12377. *p_org = WASM_OP_SET_GLOBAL_64;
  12378. }
  12379. else if (module->aux_stack_size > 0
  12380. && global_idx == module->aux_stack_top_global_index) {
  12381. #if WASM_ENABLE_DEBUG_INTERP != 0
  12382. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12383. error_buf_size)) {
  12384. goto fail;
  12385. }
  12386. #endif
  12387. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12388. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12389. func->has_op_set_global_aux_stack = true;
  12390. #endif
  12391. }
  12392. #else /* else of WASM_ENABLE_FAST_INTERP */
  12393. if (global_type == VALUE_TYPE_I64
  12394. || global_type == VALUE_TYPE_F64) {
  12395. skip_label();
  12396. emit_label(WASM_OP_SET_GLOBAL_64);
  12397. }
  12398. else if (module->aux_stack_size > 0
  12399. && global_idx == module->aux_stack_top_global_index) {
  12400. skip_label();
  12401. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12402. }
  12403. #if WASM_ENABLE_SIMDE != 0
  12404. else if (global_type == VALUE_TYPE_V128) {
  12405. skip_label();
  12406. emit_label(WASM_OP_SET_GLOBAL_V128);
  12407. }
  12408. #endif /* end of WASM_ENABLE_SIMDE */
  12409. emit_uint32(loader_ctx, global_idx);
  12410. POP_OFFSET_TYPE(global_type);
  12411. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12412. POP_TYPE(global_type);
  12413. break;
  12414. }
  12415. /* load */
  12416. case WASM_OP_I32_LOAD:
  12417. case WASM_OP_I32_LOAD8_S:
  12418. case WASM_OP_I32_LOAD8_U:
  12419. case WASM_OP_I32_LOAD16_S:
  12420. case WASM_OP_I32_LOAD16_U:
  12421. case WASM_OP_I64_LOAD:
  12422. case WASM_OP_I64_LOAD8_S:
  12423. case WASM_OP_I64_LOAD8_U:
  12424. case WASM_OP_I64_LOAD16_S:
  12425. case WASM_OP_I64_LOAD16_U:
  12426. case WASM_OP_I64_LOAD32_S:
  12427. case WASM_OP_I64_LOAD32_U:
  12428. case WASM_OP_F32_LOAD:
  12429. case WASM_OP_F64_LOAD:
  12430. /* store */
  12431. case WASM_OP_I32_STORE:
  12432. case WASM_OP_I32_STORE8:
  12433. case WASM_OP_I32_STORE16:
  12434. case WASM_OP_I64_STORE:
  12435. case WASM_OP_I64_STORE8:
  12436. case WASM_OP_I64_STORE16:
  12437. case WASM_OP_I64_STORE32:
  12438. case WASM_OP_F32_STORE:
  12439. case WASM_OP_F64_STORE:
  12440. {
  12441. #if WASM_ENABLE_FAST_INTERP != 0
  12442. /* change F32/F64 into I32/I64 */
  12443. if (opcode == WASM_OP_F32_LOAD) {
  12444. skip_label();
  12445. emit_label(WASM_OP_I32_LOAD);
  12446. }
  12447. else if (opcode == WASM_OP_F64_LOAD) {
  12448. skip_label();
  12449. emit_label(WASM_OP_I64_LOAD);
  12450. }
  12451. else if (opcode == WASM_OP_F32_STORE) {
  12452. skip_label();
  12453. emit_label(WASM_OP_I32_STORE);
  12454. }
  12455. else if (opcode == WASM_OP_F64_STORE) {
  12456. skip_label();
  12457. emit_label(WASM_OP_I64_STORE);
  12458. }
  12459. #endif
  12460. CHECK_MEMORY();
  12461. pb_read_leb_memarg(p, p_end, align); /* align */
  12462. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12463. if (!check_memory_access_align(opcode, align, error_buf,
  12464. error_buf_size)) {
  12465. goto fail;
  12466. }
  12467. #if WASM_ENABLE_FAST_INTERP != 0
  12468. emit_uint32(loader_ctx, mem_offset);
  12469. #endif
  12470. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12471. func->has_memory_operations = true;
  12472. #endif
  12473. switch (opcode) {
  12474. /* load */
  12475. case WASM_OP_I32_LOAD:
  12476. case WASM_OP_I32_LOAD8_S:
  12477. case WASM_OP_I32_LOAD8_U:
  12478. case WASM_OP_I32_LOAD16_S:
  12479. case WASM_OP_I32_LOAD16_U:
  12480. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12481. break;
  12482. case WASM_OP_I64_LOAD:
  12483. case WASM_OP_I64_LOAD8_S:
  12484. case WASM_OP_I64_LOAD8_U:
  12485. case WASM_OP_I64_LOAD16_S:
  12486. case WASM_OP_I64_LOAD16_U:
  12487. case WASM_OP_I64_LOAD32_S:
  12488. case WASM_OP_I64_LOAD32_U:
  12489. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12490. break;
  12491. case WASM_OP_F32_LOAD:
  12492. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12493. break;
  12494. case WASM_OP_F64_LOAD:
  12495. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12496. break;
  12497. /* store */
  12498. case WASM_OP_I32_STORE:
  12499. case WASM_OP_I32_STORE8:
  12500. case WASM_OP_I32_STORE16:
  12501. POP_I32();
  12502. POP_MEM_OFFSET();
  12503. break;
  12504. case WASM_OP_I64_STORE:
  12505. case WASM_OP_I64_STORE8:
  12506. case WASM_OP_I64_STORE16:
  12507. case WASM_OP_I64_STORE32:
  12508. POP_I64();
  12509. POP_MEM_OFFSET();
  12510. break;
  12511. case WASM_OP_F32_STORE:
  12512. POP_F32();
  12513. POP_MEM_OFFSET();
  12514. break;
  12515. case WASM_OP_F64_STORE:
  12516. POP_F64();
  12517. POP_MEM_OFFSET();
  12518. break;
  12519. default:
  12520. break;
  12521. }
  12522. break;
  12523. }
  12524. case WASM_OP_MEMORY_SIZE:
  12525. CHECK_MEMORY();
  12526. pb_read_leb_uint32(p, p_end, memidx);
  12527. check_memidx(module, memidx);
  12528. PUSH_PAGE_COUNT();
  12529. module->possible_memory_grow = true;
  12530. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12531. func->has_memory_operations = true;
  12532. #endif
  12533. break;
  12534. case WASM_OP_MEMORY_GROW:
  12535. CHECK_MEMORY();
  12536. pb_read_leb_uint32(p, p_end, memidx);
  12537. check_memidx(module, memidx);
  12538. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12539. module->possible_memory_grow = true;
  12540. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12541. || WASM_ENABLE_WAMR_COMPILER != 0
  12542. func->has_op_memory_grow = true;
  12543. #endif
  12544. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12545. func->has_memory_operations = true;
  12546. #endif
  12547. break;
  12548. case WASM_OP_I32_CONST:
  12549. pb_read_leb_int32(p, p_end, i32_const);
  12550. #if WASM_ENABLE_FAST_INTERP != 0
  12551. skip_label();
  12552. disable_emit = true;
  12553. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12554. if (operand_offset == 0) {
  12555. disable_emit = false;
  12556. emit_label(WASM_OP_I32_CONST);
  12557. emit_uint32(loader_ctx, i32_const);
  12558. }
  12559. #else
  12560. (void)i32_const;
  12561. #endif
  12562. PUSH_I32();
  12563. break;
  12564. case WASM_OP_I64_CONST:
  12565. pb_read_leb_int64(p, p_end, i64_const);
  12566. #if WASM_ENABLE_FAST_INTERP != 0
  12567. skip_label();
  12568. disable_emit = true;
  12569. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12570. if (operand_offset == 0) {
  12571. disable_emit = false;
  12572. emit_label(WASM_OP_I64_CONST);
  12573. emit_uint64(loader_ctx, i64_const);
  12574. }
  12575. #endif
  12576. PUSH_I64();
  12577. break;
  12578. case WASM_OP_F32_CONST:
  12579. CHECK_BUF(p, p_end, sizeof(float32));
  12580. p += sizeof(float32);
  12581. #if WASM_ENABLE_FAST_INTERP != 0
  12582. skip_label();
  12583. disable_emit = true;
  12584. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12585. sizeof(float32));
  12586. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12587. if (operand_offset == 0) {
  12588. disable_emit = false;
  12589. emit_label(WASM_OP_F32_CONST);
  12590. emit_float32(loader_ctx, f32_const);
  12591. }
  12592. #endif
  12593. PUSH_F32();
  12594. break;
  12595. case WASM_OP_F64_CONST:
  12596. CHECK_BUF(p, p_end, sizeof(float64));
  12597. p += sizeof(float64);
  12598. #if WASM_ENABLE_FAST_INTERP != 0
  12599. skip_label();
  12600. disable_emit = true;
  12601. /* Some MCU may require 8-byte align */
  12602. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12603. sizeof(float64));
  12604. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12605. if (operand_offset == 0) {
  12606. disable_emit = false;
  12607. emit_label(WASM_OP_F64_CONST);
  12608. emit_float64(loader_ctx, f64_const);
  12609. }
  12610. #endif
  12611. PUSH_F64();
  12612. break;
  12613. case WASM_OP_I32_EQZ:
  12614. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12615. break;
  12616. case WASM_OP_I32_EQ:
  12617. case WASM_OP_I32_NE:
  12618. case WASM_OP_I32_LT_S:
  12619. case WASM_OP_I32_LT_U:
  12620. case WASM_OP_I32_GT_S:
  12621. case WASM_OP_I32_GT_U:
  12622. case WASM_OP_I32_LE_S:
  12623. case WASM_OP_I32_LE_U:
  12624. case WASM_OP_I32_GE_S:
  12625. case WASM_OP_I32_GE_U:
  12626. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12627. break;
  12628. case WASM_OP_I64_EQZ:
  12629. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12630. break;
  12631. case WASM_OP_I64_EQ:
  12632. case WASM_OP_I64_NE:
  12633. case WASM_OP_I64_LT_S:
  12634. case WASM_OP_I64_LT_U:
  12635. case WASM_OP_I64_GT_S:
  12636. case WASM_OP_I64_GT_U:
  12637. case WASM_OP_I64_LE_S:
  12638. case WASM_OP_I64_LE_U:
  12639. case WASM_OP_I64_GE_S:
  12640. case WASM_OP_I64_GE_U:
  12641. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12642. break;
  12643. case WASM_OP_F32_EQ:
  12644. case WASM_OP_F32_NE:
  12645. case WASM_OP_F32_LT:
  12646. case WASM_OP_F32_GT:
  12647. case WASM_OP_F32_LE:
  12648. case WASM_OP_F32_GE:
  12649. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12650. break;
  12651. case WASM_OP_F64_EQ:
  12652. case WASM_OP_F64_NE:
  12653. case WASM_OP_F64_LT:
  12654. case WASM_OP_F64_GT:
  12655. case WASM_OP_F64_LE:
  12656. case WASM_OP_F64_GE:
  12657. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12658. break;
  12659. case WASM_OP_I32_CLZ:
  12660. case WASM_OP_I32_CTZ:
  12661. case WASM_OP_I32_POPCNT:
  12662. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12663. break;
  12664. case WASM_OP_I32_ADD:
  12665. case WASM_OP_I32_SUB:
  12666. case WASM_OP_I32_MUL:
  12667. case WASM_OP_I32_DIV_S:
  12668. case WASM_OP_I32_DIV_U:
  12669. case WASM_OP_I32_REM_S:
  12670. case WASM_OP_I32_REM_U:
  12671. case WASM_OP_I32_AND:
  12672. case WASM_OP_I32_OR:
  12673. case WASM_OP_I32_XOR:
  12674. case WASM_OP_I32_SHL:
  12675. case WASM_OP_I32_SHR_S:
  12676. case WASM_OP_I32_SHR_U:
  12677. case WASM_OP_I32_ROTL:
  12678. case WASM_OP_I32_ROTR:
  12679. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12680. break;
  12681. case WASM_OP_I64_CLZ:
  12682. case WASM_OP_I64_CTZ:
  12683. case WASM_OP_I64_POPCNT:
  12684. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12685. break;
  12686. case WASM_OP_I64_ADD:
  12687. case WASM_OP_I64_SUB:
  12688. case WASM_OP_I64_MUL:
  12689. case WASM_OP_I64_DIV_S:
  12690. case WASM_OP_I64_DIV_U:
  12691. case WASM_OP_I64_REM_S:
  12692. case WASM_OP_I64_REM_U:
  12693. case WASM_OP_I64_AND:
  12694. case WASM_OP_I64_OR:
  12695. case WASM_OP_I64_XOR:
  12696. case WASM_OP_I64_SHL:
  12697. case WASM_OP_I64_SHR_S:
  12698. case WASM_OP_I64_SHR_U:
  12699. case WASM_OP_I64_ROTL:
  12700. case WASM_OP_I64_ROTR:
  12701. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12702. break;
  12703. case WASM_OP_F32_ABS:
  12704. case WASM_OP_F32_NEG:
  12705. case WASM_OP_F32_CEIL:
  12706. case WASM_OP_F32_FLOOR:
  12707. case WASM_OP_F32_TRUNC:
  12708. case WASM_OP_F32_NEAREST:
  12709. case WASM_OP_F32_SQRT:
  12710. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12711. break;
  12712. case WASM_OP_F32_ADD:
  12713. case WASM_OP_F32_SUB:
  12714. case WASM_OP_F32_MUL:
  12715. case WASM_OP_F32_DIV:
  12716. case WASM_OP_F32_MIN:
  12717. case WASM_OP_F32_MAX:
  12718. case WASM_OP_F32_COPYSIGN:
  12719. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12720. break;
  12721. case WASM_OP_F64_ABS:
  12722. case WASM_OP_F64_NEG:
  12723. case WASM_OP_F64_CEIL:
  12724. case WASM_OP_F64_FLOOR:
  12725. case WASM_OP_F64_TRUNC:
  12726. case WASM_OP_F64_NEAREST:
  12727. case WASM_OP_F64_SQRT:
  12728. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12729. break;
  12730. case WASM_OP_F64_ADD:
  12731. case WASM_OP_F64_SUB:
  12732. case WASM_OP_F64_MUL:
  12733. case WASM_OP_F64_DIV:
  12734. case WASM_OP_F64_MIN:
  12735. case WASM_OP_F64_MAX:
  12736. case WASM_OP_F64_COPYSIGN:
  12737. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12738. break;
  12739. case WASM_OP_I32_WRAP_I64:
  12740. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12741. break;
  12742. case WASM_OP_I32_TRUNC_S_F32:
  12743. case WASM_OP_I32_TRUNC_U_F32:
  12744. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12745. break;
  12746. case WASM_OP_I32_TRUNC_S_F64:
  12747. case WASM_OP_I32_TRUNC_U_F64:
  12748. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12749. break;
  12750. case WASM_OP_I64_EXTEND_S_I32:
  12751. case WASM_OP_I64_EXTEND_U_I32:
  12752. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12753. break;
  12754. case WASM_OP_I64_TRUNC_S_F32:
  12755. case WASM_OP_I64_TRUNC_U_F32:
  12756. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12757. break;
  12758. case WASM_OP_I64_TRUNC_S_F64:
  12759. case WASM_OP_I64_TRUNC_U_F64:
  12760. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12761. break;
  12762. case WASM_OP_F32_CONVERT_S_I32:
  12763. case WASM_OP_F32_CONVERT_U_I32:
  12764. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12765. break;
  12766. case WASM_OP_F32_CONVERT_S_I64:
  12767. case WASM_OP_F32_CONVERT_U_I64:
  12768. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12769. break;
  12770. case WASM_OP_F32_DEMOTE_F64:
  12771. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12772. break;
  12773. case WASM_OP_F64_CONVERT_S_I32:
  12774. case WASM_OP_F64_CONVERT_U_I32:
  12775. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12776. break;
  12777. case WASM_OP_F64_CONVERT_S_I64:
  12778. case WASM_OP_F64_CONVERT_U_I64:
  12779. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12780. break;
  12781. case WASM_OP_F64_PROMOTE_F32:
  12782. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12783. break;
  12784. case WASM_OP_I32_REINTERPRET_F32:
  12785. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12786. break;
  12787. case WASM_OP_I64_REINTERPRET_F64:
  12788. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12789. break;
  12790. case WASM_OP_F32_REINTERPRET_I32:
  12791. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12792. break;
  12793. case WASM_OP_F64_REINTERPRET_I64:
  12794. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12795. break;
  12796. case WASM_OP_I32_EXTEND8_S:
  12797. case WASM_OP_I32_EXTEND16_S:
  12798. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12799. break;
  12800. case WASM_OP_I64_EXTEND8_S:
  12801. case WASM_OP_I64_EXTEND16_S:
  12802. case WASM_OP_I64_EXTEND32_S:
  12803. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12804. break;
  12805. #if WASM_ENABLE_GC != 0
  12806. case WASM_OP_GC_PREFIX:
  12807. {
  12808. uint32 opcode1;
  12809. pb_read_leb_uint32(p, p_end, opcode1);
  12810. #if WASM_ENABLE_FAST_INTERP != 0
  12811. emit_byte(loader_ctx, ((uint8)opcode1));
  12812. #endif
  12813. switch (opcode1) {
  12814. case WASM_OP_STRUCT_NEW:
  12815. case WASM_OP_STRUCT_NEW_DEFAULT:
  12816. {
  12817. pb_read_leb_uint32(p, p_end, type_idx);
  12818. #if WASM_ENABLE_FAST_INTERP != 0
  12819. emit_uint32(loader_ctx, type_idx);
  12820. #endif
  12821. if (!check_type_index(module, module->type_count,
  12822. type_idx, error_buf,
  12823. error_buf_size)) {
  12824. goto fail;
  12825. }
  12826. if (module->types[type_idx]->type_flag
  12827. != WASM_TYPE_STRUCT) {
  12828. set_error_buf(error_buf, error_buf_size,
  12829. "unknown struct type");
  12830. goto fail;
  12831. }
  12832. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12833. int32 j, k;
  12834. uint8 value_type;
  12835. uint32 ref_type_struct_size;
  12836. WASMStructType *struct_type =
  12837. (WASMStructType *)module->types[type_idx];
  12838. k = struct_type->ref_type_map_count - 1;
  12839. for (j = struct_type->field_count - 1; j >= 0;
  12840. j--) {
  12841. value_type = struct_type->fields[j].field_type;
  12842. if (wasm_is_type_reftype(value_type)) {
  12843. if (wasm_is_type_multi_byte_type(
  12844. value_type)) {
  12845. ref_type_struct_size =
  12846. wasm_reftype_struct_size(
  12847. struct_type->ref_type_maps[k]
  12848. .ref_type);
  12849. bh_memcpy_s(
  12850. &wasm_ref_type,
  12851. (uint32)sizeof(WASMRefType),
  12852. struct_type->ref_type_maps[k]
  12853. .ref_type,
  12854. ref_type_struct_size);
  12855. k--;
  12856. }
  12857. POP_REF(value_type);
  12858. }
  12859. else {
  12860. switch (value_type) {
  12861. case VALUE_TYPE_I32:
  12862. case PACKED_TYPE_I8:
  12863. case PACKED_TYPE_I16:
  12864. POP_I32();
  12865. break;
  12866. case VALUE_TYPE_I64:
  12867. POP_I64();
  12868. break;
  12869. case VALUE_TYPE_F32:
  12870. POP_F32();
  12871. break;
  12872. case VALUE_TYPE_F64:
  12873. POP_F64();
  12874. break;
  12875. default:
  12876. set_error_buf(error_buf,
  12877. error_buf_size,
  12878. "unknown type");
  12879. goto fail;
  12880. }
  12881. }
  12882. }
  12883. }
  12884. /* PUSH struct obj, (ref $t) */
  12885. wasm_set_refheaptype_typeidx(
  12886. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12887. PUSH_REF(wasm_ref_type.ref_type);
  12888. break;
  12889. }
  12890. case WASM_OP_STRUCT_GET:
  12891. case WASM_OP_STRUCT_GET_S:
  12892. case WASM_OP_STRUCT_GET_U:
  12893. case WASM_OP_STRUCT_SET:
  12894. {
  12895. WASMStructType *struct_type;
  12896. WASMRefType *ref_type = NULL;
  12897. uint32 field_idx;
  12898. uint8 field_type;
  12899. pb_read_leb_uint32(p, p_end, type_idx);
  12900. #if WASM_ENABLE_FAST_INTERP != 0
  12901. emit_uint32(loader_ctx, type_idx);
  12902. #endif
  12903. if (!check_type_index(module, module->type_count,
  12904. type_idx, error_buf,
  12905. error_buf_size)) {
  12906. goto fail;
  12907. }
  12908. if (module->types[type_idx]->type_flag
  12909. != WASM_TYPE_STRUCT) {
  12910. set_error_buf(error_buf, error_buf_size,
  12911. "unknown struct type");
  12912. goto fail;
  12913. }
  12914. struct_type = (WASMStructType *)module->types[type_idx];
  12915. pb_read_leb_uint32(p, p_end, field_idx);
  12916. #if WASM_ENABLE_FAST_INTERP != 0
  12917. emit_uint32(loader_ctx, field_idx);
  12918. #endif
  12919. if (field_idx >= struct_type->field_count) {
  12920. set_error_buf(error_buf, error_buf_size,
  12921. "unknown struct field");
  12922. goto fail;
  12923. }
  12924. if (opcode1 == WASM_OP_STRUCT_SET
  12925. && !(struct_type->fields[field_idx].field_flags
  12926. & 1)) {
  12927. set_error_buf(error_buf, error_buf_size,
  12928. "field is immutable");
  12929. goto fail;
  12930. }
  12931. field_type = struct_type->fields[field_idx].field_type;
  12932. if (is_packed_type(field_type)) {
  12933. if (opcode1 == WASM_OP_STRUCT_GET) {
  12934. set_error_buf(error_buf, error_buf_size,
  12935. "type mismatch");
  12936. goto fail;
  12937. }
  12938. else {
  12939. field_type = VALUE_TYPE_I32;
  12940. }
  12941. }
  12942. if (wasm_is_type_multi_byte_type(field_type)) {
  12943. ref_type = wasm_reftype_map_find(
  12944. struct_type->ref_type_maps,
  12945. struct_type->ref_type_map_count, field_idx);
  12946. bh_assert(ref_type);
  12947. }
  12948. if (opcode1 == WASM_OP_STRUCT_SET) {
  12949. /* POP field */
  12950. if (wasm_is_type_multi_byte_type(field_type)) {
  12951. bh_memcpy_s(&wasm_ref_type,
  12952. (uint32)sizeof(WASMRefType),
  12953. ref_type,
  12954. wasm_reftype_struct_size(ref_type));
  12955. }
  12956. POP_REF(field_type);
  12957. /* POP struct obj, (ref null $t) */
  12958. wasm_set_refheaptype_typeidx(
  12959. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12960. POP_REF(wasm_ref_type.ref_type);
  12961. }
  12962. else {
  12963. /* POP struct obj, (ref null $t) */
  12964. wasm_set_refheaptype_typeidx(
  12965. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12966. POP_REF(wasm_ref_type.ref_type);
  12967. /* PUSH field */
  12968. if (wasm_is_type_multi_byte_type(field_type)) {
  12969. bh_memcpy_s(&wasm_ref_type,
  12970. (uint32)sizeof(WASMRefType),
  12971. ref_type,
  12972. wasm_reftype_struct_size(ref_type));
  12973. }
  12974. PUSH_REF(field_type);
  12975. }
  12976. break;
  12977. }
  12978. case WASM_OP_ARRAY_NEW:
  12979. case WASM_OP_ARRAY_NEW_DEFAULT:
  12980. case WASM_OP_ARRAY_NEW_FIXED:
  12981. case WASM_OP_ARRAY_NEW_DATA:
  12982. case WASM_OP_ARRAY_NEW_ELEM:
  12983. {
  12984. WASMArrayType *array_type;
  12985. uint8 elem_type;
  12986. uint32 u32 = 0;
  12987. pb_read_leb_uint32(p, p_end, type_idx);
  12988. #if WASM_ENABLE_FAST_INTERP != 0
  12989. emit_uint32(loader_ctx, type_idx);
  12990. #endif
  12991. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12992. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12993. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12994. pb_read_leb_uint32(p, p_end, u32);
  12995. #if WASM_ENABLE_FAST_INTERP != 0
  12996. emit_uint32(loader_ctx, u32);
  12997. #endif
  12998. }
  12999. if (!check_array_type(module, type_idx, error_buf,
  13000. error_buf_size)) {
  13001. goto fail;
  13002. }
  13003. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  13004. /* length */
  13005. POP_I32();
  13006. }
  13007. array_type = (WASMArrayType *)module->types[type_idx];
  13008. elem_type = array_type->elem_type;
  13009. if (opcode1 == WASM_OP_ARRAY_NEW
  13010. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13011. if (wasm_is_type_multi_byte_type(elem_type)) {
  13012. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13013. array_type->elem_ref_type,
  13014. wasm_reftype_struct_size(
  13015. array_type->elem_ref_type));
  13016. }
  13017. if (is_packed_type(elem_type)) {
  13018. elem_type = VALUE_TYPE_I32;
  13019. }
  13020. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13021. uint32 N = u32;
  13022. for (i = 0; i < N; i++) {
  13023. if (wasm_is_type_multi_byte_type(
  13024. elem_type)) {
  13025. bh_memcpy_s(
  13026. &wasm_ref_type, sizeof(WASMRefType),
  13027. array_type->elem_ref_type,
  13028. wasm_reftype_struct_size(
  13029. array_type->elem_ref_type));
  13030. }
  13031. POP_REF(elem_type);
  13032. }
  13033. }
  13034. else
  13035. POP_REF(elem_type);
  13036. }
  13037. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  13038. /* offset of data segment */
  13039. POP_I32();
  13040. if (u32 >= module->data_seg_count) {
  13041. set_error_buf(error_buf, error_buf_size,
  13042. "unknown data segment");
  13043. goto fail;
  13044. }
  13045. if (wasm_is_type_reftype(elem_type)) {
  13046. set_error_buf(error_buf, error_buf_size,
  13047. "array elem type mismatch");
  13048. goto fail;
  13049. }
  13050. }
  13051. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13052. WASMTableSeg *table_seg =
  13053. module->table_segments + u32;
  13054. /* offset of element segment */
  13055. POP_I32();
  13056. if (u32 >= module->table_seg_count) {
  13057. set_error_buf(error_buf, error_buf_size,
  13058. "unknown element segment");
  13059. goto fail;
  13060. }
  13061. if (!wasm_reftype_is_subtype_of(
  13062. table_seg->elem_type,
  13063. table_seg->elem_ref_type, elem_type,
  13064. array_type->elem_ref_type, module->types,
  13065. module->type_count)) {
  13066. set_error_buf(error_buf, error_buf_size,
  13067. "array elem type mismatch");
  13068. goto fail;
  13069. }
  13070. }
  13071. /* PUSH array obj, (ref $t) */
  13072. wasm_set_refheaptype_typeidx(
  13073. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13074. PUSH_REF(wasm_ref_type.ref_type);
  13075. break;
  13076. }
  13077. case WASM_OP_ARRAY_GET:
  13078. case WASM_OP_ARRAY_GET_S:
  13079. case WASM_OP_ARRAY_GET_U:
  13080. case WASM_OP_ARRAY_SET:
  13081. {
  13082. uint8 elem_type;
  13083. WASMArrayType *array_type;
  13084. WASMRefType *ref_type = NULL;
  13085. pb_read_leb_uint32(p, p_end, type_idx);
  13086. #if WASM_ENABLE_FAST_INTERP != 0
  13087. emit_uint32(loader_ctx, type_idx);
  13088. #endif
  13089. if (!check_array_type(module, type_idx, error_buf,
  13090. error_buf_size)) {
  13091. goto fail;
  13092. }
  13093. array_type = (WASMArrayType *)module->types[type_idx];
  13094. if (opcode1 == WASM_OP_ARRAY_SET
  13095. && !(array_type->elem_flags & 1)) {
  13096. set_error_buf(error_buf, error_buf_size,
  13097. "array is immutable");
  13098. goto fail;
  13099. }
  13100. elem_type = array_type->elem_type;
  13101. if (is_packed_type(elem_type)) {
  13102. if (opcode1 != WASM_OP_ARRAY_GET_S
  13103. && opcode1 != WASM_OP_ARRAY_GET_U
  13104. && opcode1 != WASM_OP_ARRAY_SET) {
  13105. set_error_buf(error_buf, error_buf_size,
  13106. "type mismatch");
  13107. goto fail;
  13108. }
  13109. else {
  13110. elem_type = VALUE_TYPE_I32;
  13111. }
  13112. }
  13113. ref_type = array_type->elem_ref_type;
  13114. if (opcode1 == WASM_OP_ARRAY_SET) {
  13115. /* POP elem to set */
  13116. if (wasm_is_type_multi_byte_type(elem_type)) {
  13117. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13118. ref_type,
  13119. wasm_reftype_struct_size(ref_type));
  13120. }
  13121. POP_REF(elem_type);
  13122. }
  13123. /* elem idx */
  13124. POP_I32();
  13125. /* POP array obj, (ref null $t) */
  13126. wasm_set_refheaptype_typeidx(
  13127. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13128. POP_REF(wasm_ref_type.ref_type);
  13129. if (opcode1 != WASM_OP_ARRAY_SET) {
  13130. /* PUSH elem */
  13131. if (wasm_is_type_multi_byte_type(elem_type)) {
  13132. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13133. ref_type,
  13134. wasm_reftype_struct_size(ref_type));
  13135. }
  13136. PUSH_REF(elem_type);
  13137. }
  13138. break;
  13139. }
  13140. case WASM_OP_ARRAY_LEN:
  13141. {
  13142. POP_REF(REF_TYPE_ARRAYREF);
  13143. /* length */
  13144. PUSH_I32();
  13145. break;
  13146. }
  13147. case WASM_OP_ARRAY_FILL:
  13148. {
  13149. WASMArrayType *array_type;
  13150. uint8 elem_type;
  13151. /* typeidx */
  13152. pb_read_leb_uint32(p, p_end, type_idx);
  13153. #if WASM_ENABLE_FAST_INTERP != 0
  13154. emit_uint32(loader_ctx, type_idx);
  13155. #endif
  13156. if (!check_array_type(module, type_idx, error_buf,
  13157. error_buf_size)) {
  13158. goto fail;
  13159. }
  13160. array_type = (WASMArrayType *)module->types[type_idx];
  13161. if (!(array_type->elem_flags & 1)) {
  13162. set_error_buf(error_buf, error_buf_size,
  13163. "array is immutable");
  13164. goto fail;
  13165. }
  13166. elem_type = array_type->elem_type;
  13167. if (is_packed_type(elem_type)) {
  13168. elem_type = VALUE_TYPE_I32;
  13169. }
  13170. POP_I32(); /* length */
  13171. #if WASM_ENABLE_FAST_INTERP != 0
  13172. POP_OFFSET_TYPE(elem_type);
  13173. #endif
  13174. POP_TYPE(elem_type);
  13175. POP_I32(); /* start */
  13176. /* POP array obj, (ref null $t) */
  13177. wasm_set_refheaptype_typeidx(
  13178. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13179. POP_REF(wasm_ref_type.ref_type);
  13180. break;
  13181. }
  13182. case WASM_OP_ARRAY_COPY:
  13183. {
  13184. uint32 src_type_idx;
  13185. uint8 src_elem_type, dst_elem_type;
  13186. WASMRefType src_ref_type = { 0 },
  13187. *src_elem_ref_type = NULL;
  13188. WASMRefType dst_ref_type = { 0 },
  13189. *dst_elem_ref_type = NULL;
  13190. WASMArrayType *array_type;
  13191. /* typeidx1 */
  13192. pb_read_leb_uint32(p, p_end, type_idx);
  13193. #if WASM_ENABLE_FAST_INTERP != 0
  13194. emit_uint32(loader_ctx, type_idx);
  13195. #endif
  13196. /* typeidx2 */
  13197. pb_read_leb_uint32(p, p_end, src_type_idx);
  13198. #if WASM_ENABLE_FAST_INTERP != 0
  13199. emit_uint32(loader_ctx, src_type_idx);
  13200. #endif
  13201. if (!check_array_type(module, type_idx, error_buf,
  13202. error_buf_size)) {
  13203. goto fail;
  13204. }
  13205. if (!check_array_type(module, src_type_idx, error_buf,
  13206. error_buf_size)) {
  13207. goto fail;
  13208. }
  13209. POP_I32();
  13210. POP_I32();
  13211. /* POP array obj, (ref null $t) */
  13212. wasm_set_refheaptype_typeidx(
  13213. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  13214. POP_REF(wasm_ref_type.ref_type);
  13215. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  13216. &wasm_ref_type,
  13217. wasm_reftype_struct_size(&wasm_ref_type));
  13218. POP_I32();
  13219. /* POP array obj, (ref null $t) */
  13220. wasm_set_refheaptype_typeidx(
  13221. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13222. POP_REF(wasm_ref_type.ref_type);
  13223. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  13224. &wasm_ref_type,
  13225. wasm_reftype_struct_size(&wasm_ref_type));
  13226. array_type = (WASMArrayType *)module->types[type_idx];
  13227. if (!(array_type->elem_flags & 1)) {
  13228. set_error_buf(error_buf, error_buf_size,
  13229. "destination array is immutable");
  13230. goto fail;
  13231. }
  13232. dst_elem_type = array_type->elem_type;
  13233. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  13234. dst_elem_ref_type = array_type->elem_ref_type;
  13235. }
  13236. array_type =
  13237. (WASMArrayType *)module->types[src_type_idx];
  13238. src_elem_type = array_type->elem_type;
  13239. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  13240. src_elem_ref_type = array_type->elem_ref_type;
  13241. }
  13242. if (!wasm_reftype_is_subtype_of(
  13243. src_elem_type, src_elem_ref_type, dst_elem_type,
  13244. dst_elem_ref_type, module->types,
  13245. module->type_count)) {
  13246. set_error_buf(error_buf, error_buf_size,
  13247. "array types do not match");
  13248. goto fail;
  13249. }
  13250. break;
  13251. }
  13252. case WASM_OP_REF_I31:
  13253. {
  13254. POP_I32();
  13255. wasm_set_refheaptype_common(
  13256. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  13257. PUSH_REF(wasm_ref_type.ref_type);
  13258. break;
  13259. }
  13260. case WASM_OP_I31_GET_S:
  13261. case WASM_OP_I31_GET_U:
  13262. {
  13263. POP_REF(REF_TYPE_I31REF);
  13264. PUSH_I32();
  13265. break;
  13266. }
  13267. case WASM_OP_REF_TEST:
  13268. case WASM_OP_REF_CAST:
  13269. case WASM_OP_REF_TEST_NULLABLE:
  13270. case WASM_OP_REF_CAST_NULLABLE:
  13271. {
  13272. uint8 type;
  13273. pb_read_leb_int32(p, p_end, heap_type);
  13274. #if WASM_ENABLE_FAST_INTERP != 0
  13275. emit_uint32(loader_ctx, (uint32)heap_type);
  13276. #endif
  13277. if (heap_type >= 0) {
  13278. if (!check_type_index(module, module->type_count,
  13279. heap_type, error_buf,
  13280. error_buf_size)) {
  13281. goto fail;
  13282. }
  13283. }
  13284. else {
  13285. if (!wasm_is_valid_heap_type(heap_type)) {
  13286. set_error_buf(error_buf, error_buf_size,
  13287. "unknown type");
  13288. goto fail;
  13289. }
  13290. }
  13291. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13292. &wasm_ref_type, error_buf,
  13293. error_buf_size)) {
  13294. goto fail;
  13295. }
  13296. if (opcode1 == WASM_OP_REF_TEST
  13297. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  13298. PUSH_I32();
  13299. else {
  13300. bool nullable =
  13301. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  13302. : false;
  13303. if (heap_type >= 0 || !nullable) {
  13304. wasm_set_refheaptype_typeidx(
  13305. &wasm_ref_type.ref_ht_typeidx, nullable,
  13306. heap_type);
  13307. PUSH_REF(wasm_ref_type.ref_type);
  13308. }
  13309. else {
  13310. PUSH_REF((uint8)((int32)0x80 + heap_type));
  13311. }
  13312. }
  13313. break;
  13314. }
  13315. case WASM_OP_BR_ON_CAST:
  13316. case WASM_OP_BR_ON_CAST_FAIL:
  13317. {
  13318. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13319. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13320. uint8 type_tmp, castflags;
  13321. uint32 depth;
  13322. int32 heap_type_dst;
  13323. bool src_nullable, dst_nullable;
  13324. CHECK_BUF(p, p_end, 1);
  13325. castflags = read_uint8(p);
  13326. #if WASM_ENABLE_FAST_INTERP != 0
  13327. /* Emit heap_type firstly */
  13328. emit_byte(loader_ctx, castflags);
  13329. #endif
  13330. p_org = p;
  13331. pb_read_leb_uint32(p, p_end, depth);
  13332. pb_read_leb_int32(p, p_end, heap_type);
  13333. #if WASM_ENABLE_FAST_INTERP != 0
  13334. /* Emit heap_type firstly */
  13335. emit_uint32(loader_ctx, (uint32)heap_type);
  13336. #endif
  13337. pb_read_leb_int32(p, p_end, heap_type_dst);
  13338. #if WASM_ENABLE_FAST_INTERP != 0
  13339. /* Emit heap_type firstly */
  13340. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13341. #endif
  13342. (void)depth;
  13343. /*
  13344. * castflags should be 0~3:
  13345. * 0: (non-null, non-null)
  13346. * 1: (null, non-null)
  13347. * 2: (non-null, null)
  13348. * 3: (null, null)
  13349. */
  13350. if (castflags > 3) {
  13351. set_error_buf(error_buf, error_buf_size,
  13352. "invalid castflags");
  13353. break;
  13354. }
  13355. src_nullable =
  13356. (castflags == 1) || (castflags == 3) ? true : false;
  13357. dst_nullable =
  13358. (castflags == 2) || (castflags == 3) ? true : false;
  13359. /* Pop and backup the stack top's ref type */
  13360. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13361. &ref_type_tmp, error_buf,
  13362. error_buf_size)) {
  13363. goto fail;
  13364. }
  13365. /* The reference type rt1 must be valid */
  13366. if (!init_ref_type(module, &ref_type1, src_nullable,
  13367. heap_type, error_buf,
  13368. error_buf_size)) {
  13369. goto fail;
  13370. }
  13371. /* The reference type rt2 must be valid. */
  13372. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13373. heap_type_dst, error_buf,
  13374. error_buf_size)) {
  13375. goto fail;
  13376. }
  13377. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13378. &ref_type2);
  13379. /* The reference type rt2 must match rt1. */
  13380. if (!wasm_reftype_is_subtype_of(
  13381. ref_type2.ref_type, &ref_type2,
  13382. ref_type1.ref_type, &ref_type1, module->types,
  13383. module->type_count)) {
  13384. set_error_buf(error_buf, error_buf_size,
  13385. "type mismatch");
  13386. goto fail;
  13387. }
  13388. p = p_org;
  13389. /* Push ref type casted for branch block check */
  13390. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13391. /* The reference type rt2 must match rt′. */
  13392. type_tmp = ref_type2.ref_type;
  13393. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13394. bh_memcpy_s(
  13395. &wasm_ref_type,
  13396. wasm_reftype_struct_size(&ref_type2),
  13397. &ref_type2,
  13398. wasm_reftype_struct_size(&ref_type2));
  13399. }
  13400. }
  13401. else {
  13402. /* The reference type rt′1 must match rt′. */
  13403. type_tmp = ref_type_diff.ref_type;
  13404. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13405. bh_memcpy_s(
  13406. &wasm_ref_type,
  13407. wasm_reftype_struct_size(&ref_type_diff),
  13408. &ref_type_diff,
  13409. wasm_reftype_struct_size(&ref_type_diff));
  13410. }
  13411. }
  13412. PUSH_REF(type_tmp);
  13413. if (!(frame_csp_tmp = check_branch_block(
  13414. loader_ctx, &p, p_end, opcode, error_buf,
  13415. error_buf_size))) {
  13416. goto fail;
  13417. }
  13418. /* Ignore heap_types */
  13419. skip_leb_uint32(p, p_end);
  13420. skip_leb_uint32(p, p_end);
  13421. /* Restore the original stack top's ref type */
  13422. POP_REF(type_tmp);
  13423. #if WASM_ENABLE_FAST_INTERP != 0
  13424. /* Erase the opnd offset emitted by POP_REF() */
  13425. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13426. #endif
  13427. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13428. type_tmp = ref_type_diff.ref_type;
  13429. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13430. bh_memcpy_s(
  13431. &wasm_ref_type,
  13432. wasm_reftype_struct_size(&ref_type_diff),
  13433. &ref_type_diff,
  13434. wasm_reftype_struct_size(&ref_type_diff));
  13435. }
  13436. }
  13437. else {
  13438. type_tmp = ref_type2.ref_type;
  13439. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13440. bh_memcpy_s(
  13441. &wasm_ref_type,
  13442. wasm_reftype_struct_size(&ref_type2),
  13443. &ref_type2,
  13444. wasm_reftype_struct_size(&ref_type2));
  13445. }
  13446. }
  13447. PUSH_REF(type_tmp);
  13448. #if WASM_ENABLE_FAST_INTERP != 0
  13449. /* Erase the opnd offset emitted by PUSH_REF() */
  13450. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13451. #endif
  13452. break;
  13453. }
  13454. case WASM_OP_ANY_CONVERT_EXTERN:
  13455. {
  13456. uint8 type;
  13457. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13458. &wasm_ref_type, error_buf,
  13459. error_buf_size)) {
  13460. goto fail;
  13461. }
  13462. if (!(type == REF_TYPE_EXTERNREF
  13463. || (type == REF_TYPE_HT_NON_NULLABLE
  13464. && wasm_ref_type.ref_ht_common.heap_type
  13465. == HEAP_TYPE_EXTERN)
  13466. || type == VALUE_TYPE_ANY)) {
  13467. set_error_buf(error_buf, error_buf_size,
  13468. "type mismatch");
  13469. goto fail;
  13470. }
  13471. if (type == REF_TYPE_EXTERNREF)
  13472. type = REF_TYPE_ANYREF;
  13473. else {
  13474. wasm_ref_type.ref_ht_common.heap_type =
  13475. HEAP_TYPE_ANY;
  13476. }
  13477. PUSH_REF(type);
  13478. break;
  13479. }
  13480. case WASM_OP_EXTERN_CONVERT_ANY:
  13481. {
  13482. uint8 type;
  13483. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13484. &wasm_ref_type, error_buf,
  13485. error_buf_size)) {
  13486. goto fail;
  13487. }
  13488. if (type == REF_TYPE_EXTERNREF
  13489. || ((type == REF_TYPE_HT_NULLABLE
  13490. || type == REF_TYPE_HT_NON_NULLABLE)
  13491. && wasm_ref_type.ref_ht_common.heap_type
  13492. == HEAP_TYPE_EXTERN)) {
  13493. set_error_buf(error_buf, error_buf_size,
  13494. "type mismatch");
  13495. goto fail;
  13496. }
  13497. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13498. /* push (ref null extern) */
  13499. type = REF_TYPE_EXTERNREF;
  13500. }
  13501. else {
  13502. /* push (ref extern) */
  13503. type = REF_TYPE_HT_NON_NULLABLE;
  13504. wasm_set_refheaptype_common(
  13505. &wasm_ref_type.ref_ht_common, false,
  13506. HEAP_TYPE_EXTERN);
  13507. }
  13508. PUSH_REF(type);
  13509. break;
  13510. }
  13511. #if WASM_ENABLE_STRINGREF != 0
  13512. case WASM_OP_STRING_NEW_UTF8:
  13513. case WASM_OP_STRING_NEW_WTF16:
  13514. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13515. case WASM_OP_STRING_NEW_WTF8:
  13516. {
  13517. uint32 memidx;
  13518. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13519. func->has_memory_operations = true;
  13520. #endif
  13521. pb_read_leb_uint32(p, p_end, memidx);
  13522. #if WASM_ENABLE_FAST_INTERP != 0
  13523. emit_uint32(loader_ctx, (uint32)memidx);
  13524. #endif
  13525. POP_I32();
  13526. POP_I32();
  13527. PUSH_REF(REF_TYPE_STRINGREF);
  13528. (void)memidx;
  13529. break;
  13530. }
  13531. case WASM_OP_STRING_CONST:
  13532. {
  13533. uint32 contents;
  13534. pb_read_leb_uint32(p, p_end, contents);
  13535. #if WASM_ENABLE_FAST_INTERP != 0
  13536. emit_uint32(loader_ctx, (uint32)contents);
  13537. #endif
  13538. PUSH_REF(REF_TYPE_STRINGREF);
  13539. (void)contents;
  13540. break;
  13541. }
  13542. case WASM_OP_STRING_MEASURE_UTF8:
  13543. case WASM_OP_STRING_MEASURE_WTF8:
  13544. case WASM_OP_STRING_MEASURE_WTF16:
  13545. {
  13546. POP_STRINGREF();
  13547. PUSH_I32();
  13548. break;
  13549. }
  13550. case WASM_OP_STRING_ENCODE_UTF8:
  13551. case WASM_OP_STRING_ENCODE_WTF16:
  13552. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13553. case WASM_OP_STRING_ENCODE_WTF8:
  13554. {
  13555. uint32 memidx;
  13556. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13557. func->has_memory_operations = true;
  13558. #endif
  13559. pb_read_leb_uint32(p, p_end, memidx);
  13560. #if WASM_ENABLE_FAST_INTERP != 0
  13561. emit_uint32(loader_ctx, (uint32)memidx);
  13562. #endif
  13563. POP_I32();
  13564. POP_STRINGREF();
  13565. PUSH_I32();
  13566. (void)memidx;
  13567. break;
  13568. }
  13569. case WASM_OP_STRING_CONCAT:
  13570. {
  13571. POP_STRINGREF();
  13572. POP_STRINGREF();
  13573. PUSH_REF(REF_TYPE_STRINGREF);
  13574. break;
  13575. }
  13576. case WASM_OP_STRING_EQ:
  13577. {
  13578. POP_STRINGREF();
  13579. POP_STRINGREF();
  13580. PUSH_I32();
  13581. break;
  13582. }
  13583. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13584. {
  13585. POP_STRINGREF();
  13586. PUSH_I32();
  13587. break;
  13588. }
  13589. case WASM_OP_STRING_AS_WTF8:
  13590. {
  13591. POP_STRINGREF();
  13592. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13593. break;
  13594. }
  13595. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13596. {
  13597. POP_I32();
  13598. POP_I32();
  13599. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13600. PUSH_I32();
  13601. break;
  13602. }
  13603. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13604. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13605. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13606. {
  13607. uint32 memidx;
  13608. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13609. func->has_memory_operations = true;
  13610. #endif
  13611. pb_read_leb_uint32(p, p_end, memidx);
  13612. #if WASM_ENABLE_FAST_INTERP != 0
  13613. emit_uint32(loader_ctx, (uint32)memidx);
  13614. #endif
  13615. POP_I32();
  13616. POP_I32();
  13617. POP_I32();
  13618. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13619. PUSH_I32();
  13620. PUSH_I32();
  13621. (void)memidx;
  13622. break;
  13623. }
  13624. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13625. {
  13626. POP_I32();
  13627. POP_I32();
  13628. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13629. PUSH_REF(REF_TYPE_STRINGREF);
  13630. break;
  13631. }
  13632. case WASM_OP_STRING_AS_WTF16:
  13633. {
  13634. POP_STRINGREF();
  13635. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13636. break;
  13637. }
  13638. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13639. {
  13640. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13641. PUSH_I32();
  13642. break;
  13643. }
  13644. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13645. {
  13646. POP_I32();
  13647. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13648. PUSH_I32();
  13649. break;
  13650. }
  13651. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13652. {
  13653. uint32 memidx;
  13654. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13655. func->has_memory_operations = true;
  13656. #endif
  13657. pb_read_leb_uint32(p, p_end, memidx);
  13658. #if WASM_ENABLE_FAST_INTERP != 0
  13659. emit_uint32(loader_ctx, (uint32)memidx);
  13660. #endif
  13661. POP_I32();
  13662. POP_I32();
  13663. POP_I32();
  13664. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13665. PUSH_I32();
  13666. (void)memidx;
  13667. break;
  13668. }
  13669. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13670. {
  13671. POP_I32();
  13672. POP_I32();
  13673. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13674. PUSH_REF(REF_TYPE_STRINGREF);
  13675. break;
  13676. }
  13677. case WASM_OP_STRING_AS_ITER:
  13678. {
  13679. POP_STRINGREF();
  13680. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13681. break;
  13682. }
  13683. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13684. {
  13685. POP_REF(REF_TYPE_STRINGVIEWITER);
  13686. PUSH_I32();
  13687. break;
  13688. }
  13689. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13690. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13691. {
  13692. POP_I32();
  13693. POP_REF(REF_TYPE_STRINGVIEWITER);
  13694. PUSH_I32();
  13695. break;
  13696. }
  13697. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13698. {
  13699. POP_I32();
  13700. POP_REF(REF_TYPE_STRINGVIEWITER);
  13701. PUSH_REF(REF_TYPE_STRINGREF);
  13702. break;
  13703. }
  13704. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13705. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13706. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13707. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13708. {
  13709. POP_I32();
  13710. POP_I32();
  13711. POP_REF(REF_TYPE_ARRAYREF);
  13712. PUSH_REF(REF_TYPE_STRINGREF);
  13713. break;
  13714. }
  13715. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13716. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13717. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13718. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13719. {
  13720. POP_I32();
  13721. POP_REF(REF_TYPE_ARRAYREF);
  13722. POP_STRINGREF();
  13723. PUSH_I32();
  13724. break;
  13725. }
  13726. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13727. default:
  13728. set_error_buf_v(error_buf, error_buf_size,
  13729. "%s %02x %02x", "unsupported opcode",
  13730. 0xfb, opcode1);
  13731. goto fail;
  13732. }
  13733. break;
  13734. }
  13735. #endif /* end of WASM_ENABLE_GC != 0 */
  13736. case WASM_OP_MISC_PREFIX:
  13737. {
  13738. uint32 opcode1;
  13739. pb_read_leb_uint32(p, p_end, opcode1);
  13740. #if WASM_ENABLE_FAST_INTERP != 0
  13741. emit_byte(loader_ctx, ((uint8)opcode1));
  13742. #endif
  13743. switch (opcode1) {
  13744. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13745. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13746. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13747. break;
  13748. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13749. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13750. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13751. break;
  13752. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13753. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13754. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13755. break;
  13756. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13757. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13758. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13759. break;
  13760. #if WASM_ENABLE_BULK_MEMORY != 0
  13761. case WASM_OP_MEMORY_INIT:
  13762. {
  13763. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13764. #if WASM_ENABLE_FAST_INTERP != 0
  13765. emit_uint32(loader_ctx, data_seg_idx);
  13766. #endif
  13767. if (module->import_memory_count == 0
  13768. && module->memory_count == 0)
  13769. goto fail_unknown_memory;
  13770. pb_read_leb_uint32(p, p_end, memidx);
  13771. check_memidx(module, memidx);
  13772. if (data_seg_idx >= module->data_seg_count) {
  13773. set_error_buf_v(error_buf, error_buf_size,
  13774. "unknown data segment %d",
  13775. data_seg_idx);
  13776. goto fail;
  13777. }
  13778. if (module->data_seg_count1 == 0)
  13779. goto fail_data_cnt_sec_require;
  13780. POP_I32();
  13781. POP_I32();
  13782. POP_MEM_OFFSET();
  13783. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13784. func->has_memory_operations = true;
  13785. #endif
  13786. #if WASM_ENABLE_WAMR_COMPILER != 0
  13787. module->is_bulk_memory_used = true;
  13788. #endif
  13789. break;
  13790. }
  13791. case WASM_OP_DATA_DROP:
  13792. {
  13793. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13794. #if WASM_ENABLE_FAST_INTERP != 0
  13795. emit_uint32(loader_ctx, data_seg_idx);
  13796. #endif
  13797. if (data_seg_idx >= module->data_seg_count) {
  13798. set_error_buf(error_buf, error_buf_size,
  13799. "unknown data segment");
  13800. goto fail;
  13801. }
  13802. if (module->data_seg_count1 == 0)
  13803. goto fail_data_cnt_sec_require;
  13804. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13805. func->has_memory_operations = true;
  13806. #endif
  13807. #if WASM_ENABLE_WAMR_COMPILER != 0
  13808. module->is_bulk_memory_used = true;
  13809. #endif
  13810. break;
  13811. }
  13812. case WASM_OP_MEMORY_COPY:
  13813. {
  13814. CHECK_BUF(p, p_end, sizeof(int16));
  13815. /* check both src and dst memory index */
  13816. pb_read_leb_uint32(p, p_end, memidx);
  13817. check_memidx(module, memidx);
  13818. pb_read_leb_uint32(p, p_end, memidx);
  13819. check_memidx(module, memidx);
  13820. if (module->import_memory_count == 0
  13821. && module->memory_count == 0)
  13822. goto fail_unknown_memory;
  13823. POP_MEM_OFFSET();
  13824. POP_MEM_OFFSET();
  13825. POP_MEM_OFFSET();
  13826. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13827. func->has_memory_operations = true;
  13828. #endif
  13829. #if WASM_ENABLE_WAMR_COMPILER != 0
  13830. module->is_bulk_memory_used = true;
  13831. #endif
  13832. break;
  13833. }
  13834. case WASM_OP_MEMORY_FILL:
  13835. {
  13836. pb_read_leb_uint32(p, p_end, memidx);
  13837. check_memidx(module, memidx);
  13838. if (module->import_memory_count == 0
  13839. && module->memory_count == 0) {
  13840. goto fail_unknown_memory;
  13841. }
  13842. POP_MEM_OFFSET();
  13843. POP_I32();
  13844. POP_MEM_OFFSET();
  13845. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13846. func->has_memory_operations = true;
  13847. #endif
  13848. #if WASM_ENABLE_WAMR_COMPILER != 0
  13849. module->is_bulk_memory_used = true;
  13850. #endif
  13851. break;
  13852. }
  13853. fail_unknown_memory:
  13854. set_error_buf(error_buf, error_buf_size,
  13855. "unknown memory 0");
  13856. goto fail;
  13857. fail_data_cnt_sec_require:
  13858. set_error_buf(error_buf, error_buf_size,
  13859. "data count section required");
  13860. goto fail;
  13861. #endif /* WASM_ENABLE_BULK_MEMORY */
  13862. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13863. case WASM_OP_TABLE_INIT:
  13864. {
  13865. uint8 seg_type = 0, tbl_type = 0;
  13866. #if WASM_ENABLE_GC != 0
  13867. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13868. #endif
  13869. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13870. pb_read_leb_uint32(p, p_end, table_idx);
  13871. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13872. #if WASM_ENABLE_GC != 0
  13873. (void **)&tbl_ref_type,
  13874. #else
  13875. NULL,
  13876. #endif
  13877. error_buf, error_buf_size))
  13878. goto fail;
  13879. if (!get_table_seg_elem_type(module, table_seg_idx,
  13880. &seg_type,
  13881. #if WASM_ENABLE_GC != 0
  13882. (void **)&seg_ref_type,
  13883. #else
  13884. NULL,
  13885. #endif
  13886. error_buf, error_buf_size))
  13887. goto fail;
  13888. #if WASM_ENABLE_GC == 0
  13889. if (seg_type != tbl_type) {
  13890. set_error_buf(error_buf, error_buf_size,
  13891. "type mismatch");
  13892. goto fail;
  13893. }
  13894. #else
  13895. if (!wasm_reftype_is_subtype_of(
  13896. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13897. module->types, module->type_count)) {
  13898. set_error_buf(error_buf, error_buf_size,
  13899. "type mismatch");
  13900. goto fail;
  13901. }
  13902. #endif
  13903. #if WASM_ENABLE_FAST_INTERP != 0
  13904. emit_uint32(loader_ctx, table_seg_idx);
  13905. emit_uint32(loader_ctx, table_idx);
  13906. #endif
  13907. POP_I32();
  13908. POP_I32();
  13909. #if WASM_ENABLE_MEMORY64 != 0
  13910. table_elem_idx_type = is_table_64bit(module, table_idx)
  13911. ? VALUE_TYPE_I64
  13912. : VALUE_TYPE_I32;
  13913. #endif
  13914. POP_TBL_ELEM_IDX();
  13915. #if WASM_ENABLE_WAMR_COMPILER != 0
  13916. module->is_ref_types_used = true;
  13917. #endif
  13918. break;
  13919. }
  13920. case WASM_OP_ELEM_DROP:
  13921. {
  13922. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13923. if (!get_table_seg_elem_type(module, table_seg_idx,
  13924. NULL, NULL, error_buf,
  13925. error_buf_size))
  13926. goto fail;
  13927. #if WASM_ENABLE_FAST_INTERP != 0
  13928. emit_uint32(loader_ctx, table_seg_idx);
  13929. #endif
  13930. #if WASM_ENABLE_WAMR_COMPILER != 0
  13931. module->is_ref_types_used = true;
  13932. #endif
  13933. break;
  13934. }
  13935. case WASM_OP_TABLE_COPY:
  13936. {
  13937. uint8 src_type, dst_type, src_tbl_idx_type,
  13938. dst_tbl_idx_type, min_tbl_idx_type;
  13939. #if WASM_ENABLE_GC != 0
  13940. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13941. #endif
  13942. uint32 src_tbl_idx, dst_tbl_idx;
  13943. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  13944. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13945. #if WASM_ENABLE_GC != 0
  13946. (void **)&dst_ref_type,
  13947. #else
  13948. NULL,
  13949. #endif
  13950. error_buf, error_buf_size))
  13951. goto fail;
  13952. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  13953. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13954. #if WASM_ENABLE_GC != 0
  13955. (void **)&src_ref_type,
  13956. #else
  13957. NULL,
  13958. #endif
  13959. error_buf, error_buf_size))
  13960. goto fail;
  13961. #if WASM_ENABLE_GC == 0
  13962. if (src_type != dst_type) {
  13963. set_error_buf(error_buf, error_buf_size,
  13964. "type mismatch");
  13965. goto fail;
  13966. }
  13967. #else
  13968. if (!wasm_reftype_is_subtype_of(
  13969. src_type, src_ref_type, dst_type, dst_ref_type,
  13970. module->types, module->type_count)) {
  13971. set_error_buf(error_buf, error_buf_size,
  13972. "type mismatch");
  13973. goto fail;
  13974. }
  13975. #endif
  13976. #if WASM_ENABLE_FAST_INTERP != 0
  13977. emit_uint32(loader_ctx, dst_tbl_idx);
  13978. emit_uint32(loader_ctx, src_tbl_idx);
  13979. #endif
  13980. #if WASM_ENABLE_MEMORY64 != 0
  13981. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  13982. ? VALUE_TYPE_I64
  13983. : VALUE_TYPE_I32;
  13984. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  13985. ? VALUE_TYPE_I64
  13986. : VALUE_TYPE_I32;
  13987. min_tbl_idx_type =
  13988. (src_tbl_idx_type == VALUE_TYPE_I32
  13989. || dst_tbl_idx_type == VALUE_TYPE_I32)
  13990. ? VALUE_TYPE_I32
  13991. : VALUE_TYPE_I64;
  13992. #else
  13993. src_tbl_idx_type = VALUE_TYPE_I32;
  13994. dst_tbl_idx_type = VALUE_TYPE_I32;
  13995. min_tbl_idx_type = VALUE_TYPE_I32;
  13996. #endif
  13997. table_elem_idx_type = min_tbl_idx_type;
  13998. POP_TBL_ELEM_IDX();
  13999. table_elem_idx_type = src_tbl_idx_type;
  14000. POP_TBL_ELEM_IDX();
  14001. table_elem_idx_type = dst_tbl_idx_type;
  14002. POP_TBL_ELEM_IDX();
  14003. #if WASM_ENABLE_WAMR_COMPILER != 0
  14004. module->is_ref_types_used = true;
  14005. #endif
  14006. break;
  14007. }
  14008. case WASM_OP_TABLE_SIZE:
  14009. {
  14010. pb_read_leb_uint32(p, p_end, table_idx);
  14011. /* TODO: shall we create a new function to check
  14012. table idx instead of using below function? */
  14013. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  14014. error_buf, error_buf_size))
  14015. goto fail;
  14016. #if WASM_ENABLE_FAST_INTERP != 0
  14017. emit_uint32(loader_ctx, table_idx);
  14018. #endif
  14019. #if WASM_ENABLE_MEMORY64 != 0
  14020. table_elem_idx_type = is_table_64bit(module, table_idx)
  14021. ? VALUE_TYPE_I64
  14022. : VALUE_TYPE_I32;
  14023. #endif
  14024. PUSH_TBL_ELEM_IDX();
  14025. #if WASM_ENABLE_WAMR_COMPILER != 0
  14026. module->is_ref_types_used = true;
  14027. #endif
  14028. break;
  14029. }
  14030. case WASM_OP_TABLE_GROW:
  14031. case WASM_OP_TABLE_FILL:
  14032. {
  14033. uint8 decl_type;
  14034. #if WASM_ENABLE_GC != 0
  14035. WASMRefType *ref_type = NULL;
  14036. #endif
  14037. pb_read_leb_uint32(p, p_end, table_idx);
  14038. if (!get_table_elem_type(module, table_idx, &decl_type,
  14039. #if WASM_ENABLE_GC != 0
  14040. (void **)&ref_type,
  14041. #else
  14042. NULL,
  14043. #endif
  14044. error_buf, error_buf_size))
  14045. goto fail;
  14046. #if WASM_ENABLE_GC != 0
  14047. if (wasm_is_type_multi_byte_type(decl_type)) {
  14048. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  14049. ref_type,
  14050. wasm_reftype_struct_size(ref_type));
  14051. }
  14052. #endif
  14053. if (opcode1 == WASM_OP_TABLE_GROW) {
  14054. if (table_idx < module->import_table_count) {
  14055. module->import_tables[table_idx]
  14056. .u.table.table_type.possible_grow = true;
  14057. }
  14058. else {
  14059. module
  14060. ->tables[table_idx
  14061. - module->import_table_count]
  14062. .table_type.possible_grow = true;
  14063. }
  14064. }
  14065. #if WASM_ENABLE_FAST_INTERP != 0
  14066. emit_uint32(loader_ctx, table_idx);
  14067. #endif
  14068. #if WASM_ENABLE_MEMORY64 != 0
  14069. table_elem_idx_type = is_table_64bit(module, table_idx)
  14070. ? VALUE_TYPE_I64
  14071. : VALUE_TYPE_I32;
  14072. #endif
  14073. POP_TBL_ELEM_IDX();
  14074. #if WASM_ENABLE_FAST_INTERP != 0
  14075. POP_OFFSET_TYPE(decl_type);
  14076. #endif
  14077. POP_TYPE(decl_type);
  14078. if (opcode1 == WASM_OP_TABLE_GROW)
  14079. PUSH_TBL_ELEM_IDX();
  14080. else
  14081. POP_TBL_ELEM_IDX();
  14082. #if WASM_ENABLE_WAMR_COMPILER != 0
  14083. module->is_ref_types_used = true;
  14084. #endif
  14085. break;
  14086. }
  14087. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  14088. default:
  14089. set_error_buf_v(error_buf, error_buf_size,
  14090. "%s %02x %02x", "unsupported opcode",
  14091. 0xfc, opcode1);
  14092. goto fail;
  14093. }
  14094. break;
  14095. }
  14096. #if WASM_ENABLE_SIMD != 0
  14097. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  14098. || (WASM_ENABLE_FAST_INTERP != 0)
  14099. case WASM_OP_SIMD_PREFIX:
  14100. {
  14101. uint32 opcode1;
  14102. #if WASM_ENABLE_WAMR_COMPILER != 0
  14103. /* Mark the SIMD instruction is used in this module */
  14104. module->is_simd_used = true;
  14105. #endif
  14106. pb_read_leb_uint32(p, p_end, opcode1);
  14107. #if WASM_ENABLE_FAST_INTERP != 0
  14108. emit_byte(loader_ctx, opcode1);
  14109. #endif
  14110. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  14111. */
  14112. switch (opcode1) {
  14113. /* memory instruction */
  14114. case SIMD_v128_load:
  14115. case SIMD_v128_load8x8_s:
  14116. case SIMD_v128_load8x8_u:
  14117. case SIMD_v128_load16x4_s:
  14118. case SIMD_v128_load16x4_u:
  14119. case SIMD_v128_load32x2_s:
  14120. case SIMD_v128_load32x2_u:
  14121. case SIMD_v128_load8_splat:
  14122. case SIMD_v128_load16_splat:
  14123. case SIMD_v128_load32_splat:
  14124. case SIMD_v128_load64_splat:
  14125. {
  14126. CHECK_MEMORY();
  14127. pb_read_leb_uint32(p, p_end, align); /* align */
  14128. if (!check_simd_memory_access_align(
  14129. opcode1, align, error_buf, error_buf_size)) {
  14130. goto fail;
  14131. }
  14132. pb_read_leb_mem_offset(p, p_end,
  14133. mem_offset); /* offset */
  14134. #if WASM_ENABLE_FAST_INTERP != 0
  14135. emit_uint32(loader_ctx, mem_offset);
  14136. #endif
  14137. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14138. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14139. func->has_memory_operations = true;
  14140. #endif
  14141. break;
  14142. }
  14143. case SIMD_v128_store:
  14144. {
  14145. CHECK_MEMORY();
  14146. pb_read_leb_uint32(p, p_end, align); /* align */
  14147. if (!check_simd_memory_access_align(
  14148. opcode1, align, error_buf, error_buf_size)) {
  14149. goto fail;
  14150. }
  14151. pb_read_leb_mem_offset(p, p_end,
  14152. mem_offset); /* offset */
  14153. #if WASM_ENABLE_FAST_INTERP != 0
  14154. emit_uint32(loader_ctx, mem_offset);
  14155. #endif
  14156. POP_V128();
  14157. POP_MEM_OFFSET();
  14158. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14159. func->has_memory_operations = true;
  14160. #endif
  14161. break;
  14162. }
  14163. /* basic operation */
  14164. case SIMD_v128_const:
  14165. {
  14166. #if WASM_ENABLE_FAST_INTERP != 0
  14167. uint64 high, low;
  14168. #endif
  14169. CHECK_BUF1(p, p_end, 16);
  14170. #if WASM_ENABLE_FAST_INTERP != 0
  14171. wasm_runtime_read_v128(p, &high, &low);
  14172. emit_uint64(loader_ctx, high);
  14173. emit_uint64(loader_ctx, low);
  14174. #endif
  14175. p += 16;
  14176. PUSH_V128();
  14177. break;
  14178. }
  14179. case SIMD_v8x16_shuffle:
  14180. {
  14181. V128 mask;
  14182. CHECK_BUF1(p, p_end, 16);
  14183. mask = read_i8x16(p, error_buf, error_buf_size);
  14184. if (!check_simd_shuffle_mask(mask, error_buf,
  14185. error_buf_size)) {
  14186. goto fail;
  14187. }
  14188. #if WASM_ENABLE_FAST_INTERP != 0
  14189. uint64 high, low;
  14190. wasm_runtime_read_v128(p, &high, &low);
  14191. emit_uint64(loader_ctx, high);
  14192. emit_uint64(loader_ctx, low);
  14193. #endif
  14194. p += 16;
  14195. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14196. break;
  14197. }
  14198. case SIMD_v8x16_swizzle:
  14199. {
  14200. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14201. break;
  14202. }
  14203. /* splat operation */
  14204. case SIMD_i8x16_splat:
  14205. case SIMD_i16x8_splat:
  14206. case SIMD_i32x4_splat:
  14207. case SIMD_i64x2_splat:
  14208. case SIMD_f32x4_splat:
  14209. case SIMD_f64x2_splat:
  14210. {
  14211. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  14212. VALUE_TYPE_I32, VALUE_TYPE_I64,
  14213. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  14214. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  14215. VALUE_TYPE_V128);
  14216. break;
  14217. }
  14218. /* lane operation */
  14219. case SIMD_i8x16_extract_lane_s:
  14220. case SIMD_i8x16_extract_lane_u:
  14221. case SIMD_i8x16_replace_lane:
  14222. case SIMD_i16x8_extract_lane_s:
  14223. case SIMD_i16x8_extract_lane_u:
  14224. case SIMD_i16x8_replace_lane:
  14225. case SIMD_i32x4_extract_lane:
  14226. case SIMD_i32x4_replace_lane:
  14227. case SIMD_i64x2_extract_lane:
  14228. case SIMD_i64x2_replace_lane:
  14229. case SIMD_f32x4_extract_lane:
  14230. case SIMD_f32x4_replace_lane:
  14231. case SIMD_f64x2_extract_lane:
  14232. case SIMD_f64x2_replace_lane:
  14233. {
  14234. uint8 lane;
  14235. /* clang-format off */
  14236. uint8 replace[] = {
  14237. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  14238. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  14239. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  14240. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  14241. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  14242. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  14243. };
  14244. uint8 push_type[] = {
  14245. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14246. VALUE_TYPE_V128,
  14247. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14248. VALUE_TYPE_V128,
  14249. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  14250. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  14251. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  14252. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  14253. };
  14254. /* clang-format on */
  14255. CHECK_BUF(p, p_end, 1);
  14256. lane = read_uint8(p);
  14257. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14258. error_buf_size)) {
  14259. goto fail;
  14260. }
  14261. #if WASM_ENABLE_FAST_INTERP != 0
  14262. emit_byte(loader_ctx, lane);
  14263. #endif
  14264. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  14265. #if WASM_ENABLE_FAST_INTERP != 0
  14266. if (!(wasm_loader_pop_frame_ref_offset(
  14267. loader_ctx,
  14268. replace[opcode1
  14269. - SIMD_i8x16_extract_lane_s],
  14270. error_buf, error_buf_size)))
  14271. goto fail;
  14272. #else
  14273. if (!(wasm_loader_pop_frame_ref(
  14274. loader_ctx,
  14275. replace[opcode1
  14276. - SIMD_i8x16_extract_lane_s],
  14277. error_buf, error_buf_size)))
  14278. goto fail;
  14279. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  14280. }
  14281. POP_AND_PUSH(
  14282. VALUE_TYPE_V128,
  14283. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  14284. break;
  14285. }
  14286. /* i8x16 compare operation */
  14287. case SIMD_i8x16_eq:
  14288. case SIMD_i8x16_ne:
  14289. case SIMD_i8x16_lt_s:
  14290. case SIMD_i8x16_lt_u:
  14291. case SIMD_i8x16_gt_s:
  14292. case SIMD_i8x16_gt_u:
  14293. case SIMD_i8x16_le_s:
  14294. case SIMD_i8x16_le_u:
  14295. case SIMD_i8x16_ge_s:
  14296. case SIMD_i8x16_ge_u:
  14297. /* i16x8 compare operation */
  14298. case SIMD_i16x8_eq:
  14299. case SIMD_i16x8_ne:
  14300. case SIMD_i16x8_lt_s:
  14301. case SIMD_i16x8_lt_u:
  14302. case SIMD_i16x8_gt_s:
  14303. case SIMD_i16x8_gt_u:
  14304. case SIMD_i16x8_le_s:
  14305. case SIMD_i16x8_le_u:
  14306. case SIMD_i16x8_ge_s:
  14307. case SIMD_i16x8_ge_u:
  14308. /* i32x4 compare operation */
  14309. case SIMD_i32x4_eq:
  14310. case SIMD_i32x4_ne:
  14311. case SIMD_i32x4_lt_s:
  14312. case SIMD_i32x4_lt_u:
  14313. case SIMD_i32x4_gt_s:
  14314. case SIMD_i32x4_gt_u:
  14315. case SIMD_i32x4_le_s:
  14316. case SIMD_i32x4_le_u:
  14317. case SIMD_i32x4_ge_s:
  14318. case SIMD_i32x4_ge_u:
  14319. /* f32x4 compare operation */
  14320. case SIMD_f32x4_eq:
  14321. case SIMD_f32x4_ne:
  14322. case SIMD_f32x4_lt:
  14323. case SIMD_f32x4_gt:
  14324. case SIMD_f32x4_le:
  14325. case SIMD_f32x4_ge:
  14326. /* f64x2 compare operation */
  14327. case SIMD_f64x2_eq:
  14328. case SIMD_f64x2_ne:
  14329. case SIMD_f64x2_lt:
  14330. case SIMD_f64x2_gt:
  14331. case SIMD_f64x2_le:
  14332. case SIMD_f64x2_ge:
  14333. {
  14334. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14335. break;
  14336. }
  14337. /* v128 operation */
  14338. case SIMD_v128_not:
  14339. {
  14340. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14341. break;
  14342. }
  14343. case SIMD_v128_and:
  14344. case SIMD_v128_andnot:
  14345. case SIMD_v128_or:
  14346. case SIMD_v128_xor:
  14347. {
  14348. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14349. break;
  14350. }
  14351. case SIMD_v128_bitselect:
  14352. {
  14353. POP_V128();
  14354. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14355. break;
  14356. }
  14357. case SIMD_v128_any_true:
  14358. {
  14359. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14360. break;
  14361. }
  14362. /* Load Lane Operation */
  14363. case SIMD_v128_load8_lane:
  14364. case SIMD_v128_load16_lane:
  14365. case SIMD_v128_load32_lane:
  14366. case SIMD_v128_load64_lane:
  14367. case SIMD_v128_store8_lane:
  14368. case SIMD_v128_store16_lane:
  14369. case SIMD_v128_store32_lane:
  14370. case SIMD_v128_store64_lane:
  14371. {
  14372. uint8 lane;
  14373. CHECK_MEMORY();
  14374. pb_read_leb_uint32(p, p_end, align); /* align */
  14375. if (!check_simd_memory_access_align(
  14376. opcode1, align, error_buf, error_buf_size)) {
  14377. goto fail;
  14378. }
  14379. pb_read_leb_mem_offset(p, p_end,
  14380. mem_offset); /* offset */
  14381. CHECK_BUF(p, p_end, 1);
  14382. lane = read_uint8(p);
  14383. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14384. error_buf_size)) {
  14385. goto fail;
  14386. }
  14387. #if WASM_ENABLE_FAST_INTERP != 0
  14388. emit_uint32(loader_ctx, mem_offset);
  14389. #endif
  14390. POP_V128();
  14391. POP_MEM_OFFSET();
  14392. #if WASM_ENABLE_FAST_INTERP != 0
  14393. emit_byte(loader_ctx, lane);
  14394. #endif
  14395. if (opcode1 < SIMD_v128_store8_lane) {
  14396. PUSH_V128();
  14397. }
  14398. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14399. func->has_memory_operations = true;
  14400. #endif
  14401. break;
  14402. }
  14403. case SIMD_v128_load32_zero:
  14404. case SIMD_v128_load64_zero:
  14405. {
  14406. CHECK_MEMORY();
  14407. pb_read_leb_uint32(p, p_end, align); /* align */
  14408. if (!check_simd_memory_access_align(
  14409. opcode1, align, error_buf, error_buf_size)) {
  14410. goto fail;
  14411. }
  14412. pb_read_leb_mem_offset(p, p_end,
  14413. mem_offset); /* offset */
  14414. #if WASM_ENABLE_FAST_INTERP != 0
  14415. emit_uint32(loader_ctx, mem_offset);
  14416. #endif
  14417. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14418. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14419. func->has_memory_operations = true;
  14420. #endif
  14421. break;
  14422. }
  14423. /* Float conversion */
  14424. case SIMD_f32x4_demote_f64x2_zero:
  14425. case SIMD_f64x2_promote_low_f32x4_zero:
  14426. {
  14427. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14428. break;
  14429. }
  14430. /* i8x16 Operation */
  14431. case SIMD_i8x16_abs:
  14432. case SIMD_i8x16_neg:
  14433. case SIMD_i8x16_popcnt:
  14434. {
  14435. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14436. break;
  14437. }
  14438. case SIMD_i8x16_all_true:
  14439. case SIMD_i8x16_bitmask:
  14440. {
  14441. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14442. break;
  14443. }
  14444. case SIMD_i8x16_narrow_i16x8_s:
  14445. case SIMD_i8x16_narrow_i16x8_u:
  14446. {
  14447. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14448. break;
  14449. }
  14450. case SIMD_f32x4_ceil:
  14451. case SIMD_f32x4_floor:
  14452. case SIMD_f32x4_trunc:
  14453. case SIMD_f32x4_nearest:
  14454. {
  14455. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14456. break;
  14457. }
  14458. case SIMD_i8x16_shl:
  14459. case SIMD_i8x16_shr_s:
  14460. case SIMD_i8x16_shr_u:
  14461. {
  14462. POP_I32();
  14463. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14464. break;
  14465. }
  14466. case SIMD_i8x16_add:
  14467. case SIMD_i8x16_add_sat_s:
  14468. case SIMD_i8x16_add_sat_u:
  14469. case SIMD_i8x16_sub:
  14470. case SIMD_i8x16_sub_sat_s:
  14471. case SIMD_i8x16_sub_sat_u:
  14472. {
  14473. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14474. break;
  14475. }
  14476. case SIMD_f64x2_ceil:
  14477. case SIMD_f64x2_floor:
  14478. {
  14479. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14480. break;
  14481. }
  14482. case SIMD_i8x16_min_s:
  14483. case SIMD_i8x16_min_u:
  14484. case SIMD_i8x16_max_s:
  14485. case SIMD_i8x16_max_u:
  14486. {
  14487. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14488. break;
  14489. }
  14490. case SIMD_f64x2_trunc:
  14491. {
  14492. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14493. break;
  14494. }
  14495. case SIMD_i8x16_avgr_u:
  14496. {
  14497. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14498. break;
  14499. }
  14500. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14501. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14502. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14503. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14504. /* i16x8 operation */
  14505. case SIMD_i16x8_abs:
  14506. case SIMD_i16x8_neg:
  14507. {
  14508. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14509. break;
  14510. }
  14511. case SIMD_i16x8_q15mulr_sat_s:
  14512. {
  14513. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14514. break;
  14515. }
  14516. case SIMD_i16x8_all_true:
  14517. case SIMD_i16x8_bitmask:
  14518. {
  14519. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14520. break;
  14521. }
  14522. case SIMD_i16x8_narrow_i32x4_s:
  14523. case SIMD_i16x8_narrow_i32x4_u:
  14524. {
  14525. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14526. break;
  14527. }
  14528. case SIMD_i16x8_extend_low_i8x16_s:
  14529. case SIMD_i16x8_extend_high_i8x16_s:
  14530. case SIMD_i16x8_extend_low_i8x16_u:
  14531. case SIMD_i16x8_extend_high_i8x16_u:
  14532. {
  14533. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14534. break;
  14535. }
  14536. case SIMD_i16x8_shl:
  14537. case SIMD_i16x8_shr_s:
  14538. case SIMD_i16x8_shr_u:
  14539. {
  14540. POP_I32();
  14541. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14542. break;
  14543. }
  14544. case SIMD_i16x8_add:
  14545. case SIMD_i16x8_add_sat_s:
  14546. case SIMD_i16x8_add_sat_u:
  14547. case SIMD_i16x8_sub:
  14548. case SIMD_i16x8_sub_sat_s:
  14549. case SIMD_i16x8_sub_sat_u:
  14550. {
  14551. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14552. break;
  14553. }
  14554. case SIMD_f64x2_nearest:
  14555. {
  14556. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14557. break;
  14558. }
  14559. case SIMD_i16x8_mul:
  14560. case SIMD_i16x8_min_s:
  14561. case SIMD_i16x8_min_u:
  14562. case SIMD_i16x8_max_s:
  14563. case SIMD_i16x8_max_u:
  14564. case SIMD_i16x8_avgr_u:
  14565. case SIMD_i16x8_extmul_low_i8x16_s:
  14566. case SIMD_i16x8_extmul_high_i8x16_s:
  14567. case SIMD_i16x8_extmul_low_i8x16_u:
  14568. case SIMD_i16x8_extmul_high_i8x16_u:
  14569. {
  14570. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14571. break;
  14572. }
  14573. /* i32x4 operation */
  14574. case SIMD_i32x4_abs:
  14575. case SIMD_i32x4_neg:
  14576. {
  14577. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14578. break;
  14579. }
  14580. case SIMD_i32x4_all_true:
  14581. case SIMD_i32x4_bitmask:
  14582. {
  14583. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14584. break;
  14585. }
  14586. case SIMD_i32x4_extend_low_i16x8_s:
  14587. case SIMD_i32x4_extend_high_i16x8_s:
  14588. case SIMD_i32x4_extend_low_i16x8_u:
  14589. case SIMD_i32x4_extend_high_i16x8_u:
  14590. {
  14591. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14592. break;
  14593. }
  14594. case SIMD_i32x4_shl:
  14595. case SIMD_i32x4_shr_s:
  14596. case SIMD_i32x4_shr_u:
  14597. {
  14598. POP_I32();
  14599. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14600. break;
  14601. }
  14602. case SIMD_i32x4_add:
  14603. case SIMD_i32x4_sub:
  14604. case SIMD_i32x4_mul:
  14605. case SIMD_i32x4_min_s:
  14606. case SIMD_i32x4_min_u:
  14607. case SIMD_i32x4_max_s:
  14608. case SIMD_i32x4_max_u:
  14609. case SIMD_i32x4_dot_i16x8_s:
  14610. case SIMD_i32x4_extmul_low_i16x8_s:
  14611. case SIMD_i32x4_extmul_high_i16x8_s:
  14612. case SIMD_i32x4_extmul_low_i16x8_u:
  14613. case SIMD_i32x4_extmul_high_i16x8_u:
  14614. {
  14615. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14616. break;
  14617. }
  14618. /* i64x2 operation */
  14619. case SIMD_i64x2_abs:
  14620. case SIMD_i64x2_neg:
  14621. {
  14622. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14623. break;
  14624. }
  14625. case SIMD_i64x2_all_true:
  14626. case SIMD_i64x2_bitmask:
  14627. {
  14628. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14629. break;
  14630. }
  14631. case SIMD_i64x2_extend_low_i32x4_s:
  14632. case SIMD_i64x2_extend_high_i32x4_s:
  14633. case SIMD_i64x2_extend_low_i32x4_u:
  14634. case SIMD_i64x2_extend_high_i32x4_u:
  14635. {
  14636. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14637. break;
  14638. }
  14639. case SIMD_i64x2_shl:
  14640. case SIMD_i64x2_shr_s:
  14641. case SIMD_i64x2_shr_u:
  14642. {
  14643. POP_I32();
  14644. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14645. break;
  14646. }
  14647. case SIMD_i64x2_add:
  14648. case SIMD_i64x2_sub:
  14649. case SIMD_i64x2_mul:
  14650. case SIMD_i64x2_eq:
  14651. case SIMD_i64x2_ne:
  14652. case SIMD_i64x2_lt_s:
  14653. case SIMD_i64x2_gt_s:
  14654. case SIMD_i64x2_le_s:
  14655. case SIMD_i64x2_ge_s:
  14656. case SIMD_i64x2_extmul_low_i32x4_s:
  14657. case SIMD_i64x2_extmul_high_i32x4_s:
  14658. case SIMD_i64x2_extmul_low_i32x4_u:
  14659. case SIMD_i64x2_extmul_high_i32x4_u:
  14660. {
  14661. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14662. break;
  14663. }
  14664. /* f32x4 operation */
  14665. case SIMD_f32x4_abs:
  14666. case SIMD_f32x4_neg:
  14667. case SIMD_f32x4_sqrt:
  14668. {
  14669. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14670. break;
  14671. }
  14672. case SIMD_f32x4_add:
  14673. case SIMD_f32x4_sub:
  14674. case SIMD_f32x4_mul:
  14675. case SIMD_f32x4_div:
  14676. case SIMD_f32x4_min:
  14677. case SIMD_f32x4_max:
  14678. case SIMD_f32x4_pmin:
  14679. case SIMD_f32x4_pmax:
  14680. {
  14681. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14682. break;
  14683. }
  14684. /* f64x2 operation */
  14685. case SIMD_f64x2_abs:
  14686. case SIMD_f64x2_neg:
  14687. case SIMD_f64x2_sqrt:
  14688. {
  14689. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14690. break;
  14691. }
  14692. case SIMD_f64x2_add:
  14693. case SIMD_f64x2_sub:
  14694. case SIMD_f64x2_mul:
  14695. case SIMD_f64x2_div:
  14696. case SIMD_f64x2_min:
  14697. case SIMD_f64x2_max:
  14698. case SIMD_f64x2_pmin:
  14699. case SIMD_f64x2_pmax:
  14700. {
  14701. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14702. break;
  14703. }
  14704. case SIMD_i32x4_trunc_sat_f32x4_s:
  14705. case SIMD_i32x4_trunc_sat_f32x4_u:
  14706. case SIMD_f32x4_convert_i32x4_s:
  14707. case SIMD_f32x4_convert_i32x4_u:
  14708. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14709. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14710. case SIMD_f64x2_convert_low_i32x4_s:
  14711. case SIMD_f64x2_convert_low_i32x4_u:
  14712. {
  14713. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14714. break;
  14715. }
  14716. default:
  14717. {
  14718. if (error_buf != NULL) {
  14719. snprintf(error_buf, error_buf_size,
  14720. "WASM module load failed: "
  14721. "invalid opcode 0xfd %02x.",
  14722. opcode1);
  14723. }
  14724. goto fail;
  14725. }
  14726. }
  14727. break;
  14728. }
  14729. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  14730. (WASM_ENABLE_FAST_INTERP != 0) */
  14731. #endif /* end of WASM_ENABLE_SIMD */
  14732. #if WASM_ENABLE_SHARED_MEMORY != 0
  14733. case WASM_OP_ATOMIC_PREFIX:
  14734. {
  14735. uint32 opcode1;
  14736. pb_read_leb_uint32(p, p_end, opcode1);
  14737. #if WASM_ENABLE_FAST_INTERP != 0
  14738. emit_byte(loader_ctx, opcode1);
  14739. #endif
  14740. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14741. CHECK_MEMORY();
  14742. pb_read_leb_uint32(p, p_end, align); /* align */
  14743. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14744. if (!check_memory_align_equal(opcode1, align, error_buf,
  14745. error_buf_size)) {
  14746. goto fail;
  14747. }
  14748. #if WASM_ENABLE_FAST_INTERP != 0
  14749. emit_uint32(loader_ctx, mem_offset);
  14750. #endif
  14751. }
  14752. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14753. func->has_memory_operations = true;
  14754. #endif
  14755. switch (opcode1) {
  14756. case WASM_OP_ATOMIC_NOTIFY:
  14757. POP_I32();
  14758. POP_MEM_OFFSET();
  14759. PUSH_I32();
  14760. break;
  14761. case WASM_OP_ATOMIC_WAIT32:
  14762. POP_I64();
  14763. POP_I32();
  14764. POP_MEM_OFFSET();
  14765. PUSH_I32();
  14766. break;
  14767. case WASM_OP_ATOMIC_WAIT64:
  14768. POP_I64();
  14769. POP_I64();
  14770. POP_MEM_OFFSET();
  14771. PUSH_I32();
  14772. break;
  14773. case WASM_OP_ATOMIC_FENCE:
  14774. /* reserved byte 0x00 */
  14775. if (*p++ != 0x00) {
  14776. set_error_buf(error_buf, error_buf_size,
  14777. "zero byte expected");
  14778. goto fail;
  14779. }
  14780. break;
  14781. case WASM_OP_ATOMIC_I32_LOAD:
  14782. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14783. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14784. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14785. break;
  14786. case WASM_OP_ATOMIC_I32_STORE:
  14787. case WASM_OP_ATOMIC_I32_STORE8:
  14788. case WASM_OP_ATOMIC_I32_STORE16:
  14789. POP_I32();
  14790. POP_MEM_OFFSET();
  14791. break;
  14792. case WASM_OP_ATOMIC_I64_LOAD:
  14793. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14794. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14795. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14796. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14797. break;
  14798. case WASM_OP_ATOMIC_I64_STORE:
  14799. case WASM_OP_ATOMIC_I64_STORE8:
  14800. case WASM_OP_ATOMIC_I64_STORE16:
  14801. case WASM_OP_ATOMIC_I64_STORE32:
  14802. POP_I64();
  14803. POP_MEM_OFFSET();
  14804. break;
  14805. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14806. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14807. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14808. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14809. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14810. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14811. case WASM_OP_ATOMIC_RMW_I32_AND:
  14812. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14813. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14814. case WASM_OP_ATOMIC_RMW_I32_OR:
  14815. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14816. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14817. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14818. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14819. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14820. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14821. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14822. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14823. POP_I32();
  14824. POP_MEM_OFFSET();
  14825. PUSH_I32();
  14826. break;
  14827. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14828. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14829. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14830. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14831. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14832. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14833. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14834. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14835. case WASM_OP_ATOMIC_RMW_I64_AND:
  14836. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14837. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14838. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14839. case WASM_OP_ATOMIC_RMW_I64_OR:
  14840. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14841. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14842. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14843. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14844. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14845. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14846. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14847. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14848. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14849. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14850. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14851. POP_I64();
  14852. POP_MEM_OFFSET();
  14853. PUSH_I64();
  14854. break;
  14855. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14856. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14857. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14858. POP_I32();
  14859. POP_I32();
  14860. POP_MEM_OFFSET();
  14861. PUSH_I32();
  14862. break;
  14863. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14864. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14865. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14866. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14867. POP_I64();
  14868. POP_I64();
  14869. POP_MEM_OFFSET();
  14870. PUSH_I64();
  14871. break;
  14872. default:
  14873. set_error_buf_v(error_buf, error_buf_size,
  14874. "%s %02x %02x", "unsupported opcode",
  14875. 0xfe, opcode1);
  14876. goto fail;
  14877. }
  14878. break;
  14879. }
  14880. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14881. default:
  14882. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14883. "unsupported opcode", opcode);
  14884. goto fail;
  14885. }
  14886. #if WASM_ENABLE_FAST_INTERP != 0
  14887. last_op = opcode;
  14888. #endif
  14889. }
  14890. if (loader_ctx->csp_num > 0) {
  14891. /* unmatched end opcodes result from unbalanced control flow structures,
  14892. * for example, br_table with inconsistent target count (1 declared, 2
  14893. * given), or simply superfluous end opcodes */
  14894. set_error_buf(
  14895. error_buf, error_buf_size,
  14896. "unexpected end opcodes from unbalanced control flow structures");
  14897. goto fail;
  14898. }
  14899. #if WASM_ENABLE_FAST_INTERP != 0
  14900. if (loader_ctx->p_code_compiled == NULL)
  14901. goto re_scan;
  14902. func->const_cell_num = loader_ctx->i64_const_num * 2
  14903. + loader_ctx->v128_const_num * 4
  14904. + loader_ctx->i32_const_num;
  14905. if (func->const_cell_num > 0) {
  14906. if (!(func->consts =
  14907. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  14908. error_buf, error_buf_size)))
  14909. goto fail;
  14910. if (loader_ctx->i64_const_num > 0) {
  14911. bh_memcpy_s(func->consts,
  14912. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  14913. loader_ctx->i64_consts,
  14914. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  14915. }
  14916. if (loader_ctx->i32_const_num > 0) {
  14917. bh_memcpy_s(func->consts
  14918. + sizeof(int64) * loader_ctx->i64_const_num,
  14919. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  14920. loader_ctx->i32_consts,
  14921. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  14922. }
  14923. if (loader_ctx->v128_const_num > 0) {
  14924. bh_memcpy_s(func->consts,
  14925. (uint32)sizeof(V128) * loader_ctx->v128_const_num,
  14926. loader_ctx->v128_consts,
  14927. (uint32)sizeof(V128) * loader_ctx->v128_const_num);
  14928. }
  14929. }
  14930. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14931. - loader_ctx->start_dynamic_offset + 1;
  14932. #else
  14933. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14934. #endif
  14935. func->max_block_num = loader_ctx->max_csp_num;
  14936. return_value = true;
  14937. fail:
  14938. wasm_loader_ctx_destroy(loader_ctx);
  14939. (void)table_idx;
  14940. (void)table_seg_idx;
  14941. (void)data_seg_idx;
  14942. (void)i64_const;
  14943. (void)local_offset;
  14944. (void)p_org;
  14945. (void)mem_offset;
  14946. (void)align;
  14947. return return_value;
  14948. }