wasm_loader.c 610 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491
  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. #if WASM_ENABLE_GC != 0
  344. static bool
  345. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  346. char *error_buf, uint32 error_buf_size)
  347. {
  348. if (type_index >= type_count) {
  349. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  350. type_index);
  351. return false;
  352. }
  353. return true;
  354. }
  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. static bool
  371. check_function_index(const WASMModule *module, uint32 function_index,
  372. char *error_buf, uint32 error_buf_size)
  373. {
  374. if (function_index
  375. >= module->import_function_count + module->function_count) {
  376. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  377. function_index);
  378. return false;
  379. }
  380. return true;
  381. }
  382. typedef struct InitValue {
  383. uint8 type;
  384. uint8 flag;
  385. #if WASM_ENABLE_GC != 0
  386. uint8 gc_opcode;
  387. WASMRefType ref_type;
  388. #endif
  389. WASMValue value;
  390. } InitValue;
  391. typedef struct ConstExprContext {
  392. uint32 sp;
  393. uint32 size;
  394. WASMModule *module;
  395. InitValue *stack;
  396. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  397. } ConstExprContext;
  398. static void
  399. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  400. {
  401. ctx->sp = 0;
  402. ctx->module = module;
  403. ctx->stack = ctx->data;
  404. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  405. }
  406. static bool
  407. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  408. #if WASM_ENABLE_GC != 0
  409. WASMRefType *ref_type, uint8 gc_opcode,
  410. #endif
  411. WASMValue *value, char *error_buf, uint32 error_buf_size)
  412. {
  413. InitValue *cur_value;
  414. if (ctx->sp >= ctx->size) {
  415. if (ctx->stack != ctx->data) {
  416. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  417. (ctx->size + 4) * sizeof(InitValue));
  418. }
  419. else {
  420. if (!(ctx->stack =
  421. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  422. error_buf, error_buf_size))) {
  423. goto fail;
  424. }
  425. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  426. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  427. }
  428. ctx->size += 4;
  429. }
  430. cur_value = &ctx->stack[ctx->sp++];
  431. cur_value->type = type;
  432. cur_value->flag = flag;
  433. cur_value->value = *value;
  434. #if WASM_ENABLE_GC != 0
  435. cur_value->gc_opcode = gc_opcode;
  436. if (wasm_is_type_multi_byte_type(type)) {
  437. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  438. ref_type, wasm_reftype_struct_size(ref_type));
  439. }
  440. #endif
  441. return true;
  442. fail:
  443. return false;
  444. }
  445. #if WASM_ENABLE_GC != 0
  446. static void
  447. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  448. {
  449. WASMStructNewInitValues *struct_init_values =
  450. (WASMStructNewInitValues *)data;
  451. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  452. WASMType *wasm_type;
  453. uint32 i;
  454. if (!data)
  455. return;
  456. wasm_type = module->types[struct_init_values->type_idx];
  457. /* The data can only be type of `WASMStructNewInitValues *`
  458. or `WASMArrayNewInitValues *` */
  459. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  460. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  461. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  462. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  463. WASMRefType *ref_type;
  464. uint8 field_type;
  465. uint16 ref_type_map_index = 0;
  466. for (i = 0; i < struct_init_values->count; i++) {
  467. field_type = struct_type->fields[i].field_type;
  468. if (wasm_is_type_multi_byte_type(field_type))
  469. ref_type =
  470. struct_type->ref_type_maps[ref_type_map_index++].ref_type;
  471. else
  472. ref_type = NULL;
  473. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  474. REF_TYPE_STRUCTREF, NULL,
  475. module->types, module->type_count)
  476. || wasm_reftype_is_subtype_of(
  477. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  478. module->types, module->type_count)) {
  479. destroy_init_expr_data_recursive(
  480. module, struct_init_values->fields[i].data);
  481. }
  482. }
  483. }
  484. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  485. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  486. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  487. uint8 elem_type = array_type->elem_type;
  488. for (i = 0; i < array_init_values->length; i++) {
  489. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  490. REF_TYPE_STRUCTREF, NULL,
  491. module->types, module->type_count)
  492. || wasm_reftype_is_subtype_of(
  493. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  494. module->types, module->type_count)) {
  495. destroy_init_expr_data_recursive(
  496. module, array_init_values->elem_data[i].data);
  497. }
  498. }
  499. }
  500. wasm_runtime_free(data);
  501. }
  502. #endif
  503. static bool
  504. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  505. #if WASM_ENABLE_GC != 0
  506. WASMRefType *ref_type, uint8 *p_gc_opcode,
  507. #endif
  508. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  509. {
  510. InitValue *cur_value;
  511. if (ctx->sp == 0) {
  512. set_error_buf(error_buf, error_buf_size,
  513. "type mismatch: const expr stack underflow");
  514. return false;
  515. }
  516. cur_value = &ctx->stack[--ctx->sp];
  517. #if WASM_ENABLE_GC == 0
  518. if (cur_value->type != type) {
  519. set_error_buf(error_buf, error_buf_size, "type mismatch");
  520. return false;
  521. }
  522. #else
  523. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  524. ref_type, ctx->module->types,
  525. ctx->module->type_count)) {
  526. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  527. "type mismatch: expect ", type2str(type),
  528. " but got other");
  529. goto fail;
  530. }
  531. #endif
  532. if (p_flag)
  533. *p_flag = cur_value->flag;
  534. if (p_value)
  535. *p_value = cur_value->value;
  536. #if WASM_ENABLE_GC != 0
  537. if (p_gc_opcode)
  538. *p_gc_opcode = cur_value->gc_opcode;
  539. #endif
  540. return true;
  541. #if WASM_ENABLE_GC != 0
  542. fail:
  543. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  544. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  545. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  546. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  547. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  548. }
  549. return false;
  550. #endif
  551. }
  552. static void
  553. destroy_const_expr_stack(ConstExprContext *ctx)
  554. {
  555. #if WASM_ENABLE_GC != 0
  556. uint32 i;
  557. for (i = 0; i < ctx->sp; i++) {
  558. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  559. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  560. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  561. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  562. destroy_init_expr_data_recursive(ctx->module,
  563. ctx->stack[i].value.data);
  564. }
  565. }
  566. #endif
  567. if (ctx->stack != ctx->data) {
  568. wasm_runtime_free(ctx->stack);
  569. }
  570. }
  571. #if WASM_ENABLE_GC != 0
  572. static void
  573. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  574. {
  575. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  576. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  577. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  578. destroy_init_expr_data_recursive(module, expr->u.data);
  579. }
  580. }
  581. #endif /* end of WASM_ENABLE_GC != 0 */
  582. static bool
  583. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  584. InitializerExpression *init_expr, uint8 type, void *ref_type,
  585. char *error_buf, uint32 error_buf_size)
  586. {
  587. const uint8 *p = *p_buf, *p_end = buf_end;
  588. uint8 flag, *p_float;
  589. uint32 i;
  590. ConstExprContext const_expr_ctx = { 0 };
  591. WASMValue cur_value;
  592. #if WASM_ENABLE_GC != 0
  593. uint32 opcode1, type_idx;
  594. uint8 opcode;
  595. WASMRefType cur_ref_type = { 0 };
  596. #endif
  597. init_const_expr_stack(&const_expr_ctx, module);
  598. CHECK_BUF(p, p_end, 1);
  599. flag = read_uint8(p);
  600. while (flag != WASM_OP_END) {
  601. switch (flag) {
  602. /* i32.const */
  603. case INIT_EXPR_TYPE_I32_CONST:
  604. read_leb_int32(p, p_end, cur_value.i32);
  605. if (!push_const_expr_stack(
  606. &const_expr_ctx, flag, VALUE_TYPE_I32,
  607. #if WASM_ENABLE_GC != 0
  608. NULL, 0,
  609. #endif
  610. &cur_value, error_buf, error_buf_size))
  611. goto fail;
  612. break;
  613. /* i64.const */
  614. case INIT_EXPR_TYPE_I64_CONST:
  615. read_leb_int64(p, p_end, cur_value.i64);
  616. if (!push_const_expr_stack(
  617. &const_expr_ctx, flag, VALUE_TYPE_I64,
  618. #if WASM_ENABLE_GC != 0
  619. NULL, 0,
  620. #endif
  621. &cur_value, error_buf, error_buf_size))
  622. goto fail;
  623. break;
  624. /* f32.const */
  625. case INIT_EXPR_TYPE_F32_CONST:
  626. CHECK_BUF(p, p_end, 4);
  627. p_float = (uint8 *)&cur_value.f32;
  628. for (i = 0; i < sizeof(float32); i++)
  629. *p_float++ = *p++;
  630. if (!push_const_expr_stack(
  631. &const_expr_ctx, flag, VALUE_TYPE_F32,
  632. #if WASM_ENABLE_GC != 0
  633. NULL, 0,
  634. #endif
  635. &cur_value, error_buf, error_buf_size))
  636. goto fail;
  637. break;
  638. /* f64.const */
  639. case INIT_EXPR_TYPE_F64_CONST:
  640. CHECK_BUF(p, p_end, 8);
  641. p_float = (uint8 *)&cur_value.f64;
  642. for (i = 0; i < sizeof(float64); i++)
  643. *p_float++ = *p++;
  644. if (!push_const_expr_stack(
  645. &const_expr_ctx, flag, VALUE_TYPE_F64,
  646. #if WASM_ENABLE_GC != 0
  647. NULL, 0,
  648. #endif
  649. &cur_value, error_buf, error_buf_size))
  650. goto fail;
  651. break;
  652. #if WASM_ENABLE_SIMD != 0
  653. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  654. || (WASM_ENABLE_FAST_INTERP != 0)
  655. /* v128.const */
  656. case INIT_EXPR_TYPE_V128_CONST:
  657. {
  658. uint64 high, low;
  659. CHECK_BUF(p, p_end, 1);
  660. (void)read_uint8(p);
  661. CHECK_BUF(p, p_end, 16);
  662. wasm_runtime_read_v128(p, &high, &low);
  663. p += 16;
  664. cur_value.v128.i64x2[0] = high;
  665. cur_value.v128.i64x2[1] = low;
  666. if (!push_const_expr_stack(
  667. &const_expr_ctx, flag, VALUE_TYPE_V128,
  668. #if WASM_ENABLE_GC != 0
  669. NULL, 0,
  670. #endif
  671. &cur_value, error_buf, error_buf_size))
  672. goto fail;
  673. #if WASM_ENABLE_WAMR_COMPILER != 0
  674. /* If any init_expr is v128.const, mark SIMD used */
  675. module->is_simd_used = true;
  676. #endif
  677. break;
  678. }
  679. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  680. (WASM_ENABLE_FAST_INTERP != 0) */
  681. #endif /* end of WASM_ENABLE_SIMD */
  682. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  683. /* ref.func */
  684. case INIT_EXPR_TYPE_FUNCREF_CONST:
  685. {
  686. uint32 func_idx;
  687. read_leb_uint32(p, p_end, func_idx);
  688. cur_value.ref_index = func_idx;
  689. if (!check_function_index(module, func_idx, error_buf,
  690. error_buf_size)) {
  691. goto fail;
  692. }
  693. #if WASM_ENABLE_GC == 0
  694. if (!push_const_expr_stack(&const_expr_ctx, flag,
  695. VALUE_TYPE_FUNCREF, &cur_value,
  696. error_buf, error_buf_size))
  697. goto fail;
  698. #else
  699. if (func_idx < module->import_function_count) {
  700. type_idx =
  701. module->import_functions[func_idx].u.function.type_idx;
  702. }
  703. else {
  704. type_idx = module
  705. ->functions[func_idx
  706. - module->import_function_count]
  707. ->type_idx;
  708. }
  709. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  710. false, type_idx);
  711. if (!push_const_expr_stack(&const_expr_ctx, flag,
  712. cur_ref_type.ref_type, &cur_ref_type,
  713. 0, &cur_value, error_buf,
  714. error_buf_size))
  715. goto fail;
  716. #endif
  717. #if WASM_ENABLE_WAMR_COMPILER != 0
  718. module->is_ref_types_used = true;
  719. #endif
  720. break;
  721. }
  722. /* ref.null */
  723. case INIT_EXPR_TYPE_REFNULL_CONST:
  724. {
  725. uint8 type1;
  726. CHECK_BUF(p, p_end, 1);
  727. type1 = read_uint8(p);
  728. #if WASM_ENABLE_GC == 0
  729. cur_value.ref_index = NULL_REF;
  730. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  731. &cur_value, error_buf,
  732. error_buf_size))
  733. goto fail;
  734. #else
  735. cur_value.gc_obj = NULL_REF;
  736. if (!is_byte_a_type(type1)) {
  737. p--;
  738. read_leb_uint32(p, p_end, type_idx);
  739. if (!check_type_index(module, module->type_count, type_idx,
  740. error_buf, error_buf_size))
  741. goto fail;
  742. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  743. true, type_idx);
  744. if (!push_const_expr_stack(&const_expr_ctx, flag,
  745. cur_ref_type.ref_type,
  746. &cur_ref_type, 0, &cur_value,
  747. error_buf, error_buf_size))
  748. goto fail;
  749. }
  750. else {
  751. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  752. NULL, 0, &cur_value, error_buf,
  753. error_buf_size))
  754. goto fail;
  755. }
  756. #endif
  757. #if WASM_ENABLE_WAMR_COMPILER != 0
  758. module->is_ref_types_used = true;
  759. #endif
  760. break;
  761. }
  762. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  763. /* get_global */
  764. case INIT_EXPR_TYPE_GET_GLOBAL:
  765. {
  766. uint32 global_idx;
  767. uint8 global_type;
  768. read_leb_uint32(p, p_end, cur_value.global_index);
  769. global_idx = cur_value.global_index;
  770. /*
  771. * Currently, constant expressions occurring as initializers
  772. * of globals are further constrained in that contained
  773. * global.get instructions are
  774. * only allowed to refer to imported globals.
  775. *
  776. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  777. */
  778. if (global_idx >= module->import_global_count
  779. /* make spec test happy */
  780. #if WASM_ENABLE_GC != 0
  781. + module->global_count
  782. #endif
  783. ) {
  784. set_error_buf_v(error_buf, error_buf_size,
  785. "unknown global %u", global_idx);
  786. goto fail;
  787. }
  788. if (
  789. /* make spec test happy */
  790. #if WASM_ENABLE_GC != 0
  791. global_idx < module->import_global_count &&
  792. #endif
  793. module->import_globals[global_idx]
  794. .u.global.type.is_mutable) {
  795. set_error_buf_v(error_buf, error_buf_size,
  796. "constant expression required");
  797. goto fail;
  798. }
  799. if (global_idx < module->import_global_count) {
  800. global_type = module->import_globals[global_idx]
  801. .u.global.type.val_type;
  802. #if WASM_ENABLE_GC != 0
  803. if (wasm_is_type_multi_byte_type(global_type)) {
  804. WASMRefType *global_ref_type =
  805. module->import_globals[global_idx]
  806. .u.global.ref_type;
  807. bh_memcpy_s(&cur_ref_type,
  808. wasm_reftype_struct_size(global_ref_type),
  809. global_ref_type,
  810. wasm_reftype_struct_size(global_ref_type));
  811. }
  812. #endif
  813. }
  814. else {
  815. global_type =
  816. module
  817. ->globals[global_idx - module->import_global_count]
  818. .type.val_type;
  819. #if WASM_ENABLE_GC != 0
  820. if (wasm_is_type_multi_byte_type(global_type)) {
  821. WASMRefType *global_ref_type =
  822. module
  823. ->globals[global_idx
  824. - module->import_global_count]
  825. .ref_type;
  826. bh_memcpy_s(&cur_ref_type,
  827. wasm_reftype_struct_size(global_ref_type),
  828. global_ref_type,
  829. wasm_reftype_struct_size(global_ref_type));
  830. }
  831. #endif
  832. }
  833. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  834. #if WASM_ENABLE_GC != 0
  835. &cur_ref_type, 0,
  836. #endif
  837. &cur_value, error_buf,
  838. error_buf_size))
  839. goto fail;
  840. break;
  841. }
  842. #if WASM_ENABLE_GC != 0
  843. /* struct.new and array.new */
  844. case WASM_OP_GC_PREFIX:
  845. {
  846. read_leb_uint32(p, p_end, opcode1);
  847. switch (opcode1) {
  848. case WASM_OP_STRUCT_NEW:
  849. {
  850. WASMStructType *struct_type;
  851. WASMStructNewInitValues *struct_init_values = NULL;
  852. uint32 field_count;
  853. read_leb_uint32(p, p_end, type_idx);
  854. if (!check_type_index(module, module->type_count,
  855. type_idx, error_buf,
  856. error_buf_size)) {
  857. goto fail;
  858. }
  859. struct_type = (WASMStructType *)module->types[type_idx];
  860. if (struct_type->base_type.type_flag
  861. != WASM_TYPE_STRUCT) {
  862. set_error_buf(error_buf, error_buf_size,
  863. "unknown struct type");
  864. goto fail;
  865. }
  866. field_count = struct_type->field_count;
  867. if (!(struct_init_values = loader_malloc(
  868. offsetof(WASMStructNewInitValues, fields)
  869. + (uint64)field_count * sizeof(WASMValue),
  870. error_buf, error_buf_size))) {
  871. goto fail;
  872. }
  873. struct_init_values->type_idx = type_idx;
  874. struct_init_values->count = field_count;
  875. for (i = field_count; i > 0; i--) {
  876. WASMRefType *field_ref_type = NULL;
  877. uint32 field_idx = i - 1;
  878. uint8 field_type =
  879. struct_type->fields[field_idx].field_type;
  880. if (wasm_is_type_multi_byte_type(field_type)) {
  881. field_ref_type = wasm_reftype_map_find(
  882. struct_type->ref_type_maps,
  883. struct_type->ref_type_map_count, field_idx);
  884. }
  885. if (is_packed_type(field_type)) {
  886. field_type = VALUE_TYPE_I32;
  887. }
  888. if (!pop_const_expr_stack(
  889. &const_expr_ctx, NULL, field_type,
  890. field_ref_type, NULL,
  891. &struct_init_values->fields[field_idx],
  892. error_buf, error_buf_size)) {
  893. destroy_init_expr_data_recursive(
  894. module, struct_init_values);
  895. goto fail;
  896. }
  897. }
  898. cur_value.data = struct_init_values;
  899. wasm_set_refheaptype_typeidx(
  900. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  901. if (!push_const_expr_stack(
  902. &const_expr_ctx, flag, cur_ref_type.ref_type,
  903. &cur_ref_type, (uint8)opcode1, &cur_value,
  904. error_buf, error_buf_size)) {
  905. destroy_init_expr_data_recursive(
  906. module, struct_init_values);
  907. goto fail;
  908. }
  909. break;
  910. }
  911. case WASM_OP_STRUCT_NEW_DEFAULT:
  912. {
  913. read_leb_uint32(p, p_end, cur_value.type_index);
  914. type_idx = cur_value.type_index;
  915. if (!check_type_index(module, module->type_count,
  916. type_idx, error_buf,
  917. error_buf_size)) {
  918. goto fail;
  919. }
  920. if (module->types[type_idx]->type_flag
  921. != WASM_TYPE_STRUCT) {
  922. set_error_buf(error_buf, error_buf_size,
  923. "unknown struct type");
  924. goto fail;
  925. }
  926. cur_value.type_index = type_idx;
  927. cur_value.data = NULL;
  928. wasm_set_refheaptype_typeidx(
  929. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  930. if (!push_const_expr_stack(
  931. &const_expr_ctx, flag, cur_ref_type.ref_type,
  932. &cur_ref_type, (uint8)opcode1, &cur_value,
  933. error_buf, error_buf_size)) {
  934. goto fail;
  935. }
  936. break;
  937. }
  938. case WASM_OP_ARRAY_NEW:
  939. case WASM_OP_ARRAY_NEW_DEFAULT:
  940. case WASM_OP_ARRAY_NEW_FIXED:
  941. {
  942. WASMArrayNewInitValues *array_init_values = NULL;
  943. WASMArrayType *array_type = NULL;
  944. WASMRefType *elem_ref_type = NULL;
  945. uint64 total_size;
  946. uint8 elem_type;
  947. read_leb_uint32(p, p_end, cur_value.type_index);
  948. type_idx = cur_value.type_index;
  949. if (!check_type_index(module, module->type_count,
  950. type_idx, error_buf,
  951. error_buf_size)) {
  952. goto fail;
  953. }
  954. array_type = (WASMArrayType *)module->types[type_idx];
  955. if (array_type->base_type.type_flag
  956. != WASM_TYPE_ARRAY) {
  957. set_error_buf(error_buf, error_buf_size,
  958. "unknown array type");
  959. goto fail;
  960. }
  961. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  962. elem_type = array_type->elem_type;
  963. if (wasm_is_type_multi_byte_type(elem_type)) {
  964. elem_ref_type = array_type->elem_ref_type;
  965. }
  966. if (is_packed_type(elem_type)) {
  967. elem_type = VALUE_TYPE_I32;
  968. }
  969. if (opcode1 == WASM_OP_ARRAY_NEW) {
  970. WASMValue len_val = { 0 };
  971. uint64 size = 0;
  972. if (!pop_const_expr_stack(
  973. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  974. NULL, NULL, &len_val, error_buf,
  975. error_buf_size)) {
  976. goto fail;
  977. }
  978. size =
  979. sizeof(WASMArrayNewInitValues)
  980. + sizeof(WASMValue) * (uint64)len_val.i32;
  981. if (!(array_init_values = loader_malloc(
  982. size, error_buf, error_buf_size))) {
  983. goto fail;
  984. }
  985. array_init_values->type_idx = type_idx;
  986. array_init_values->length = len_val.i32;
  987. if (!pop_const_expr_stack(
  988. &const_expr_ctx, NULL, elem_type,
  989. elem_ref_type, NULL,
  990. &array_init_values->elem_data[0],
  991. error_buf, error_buf_size)) {
  992. destroy_init_expr_data_recursive(
  993. module, array_init_values);
  994. goto fail;
  995. }
  996. cur_value.data = array_init_values;
  997. }
  998. else {
  999. /* WASM_OP_ARRAY_NEW_FIXED */
  1000. uint32 len;
  1001. read_leb_uint32(p, p_end, len);
  1002. total_size =
  1003. (uint64)offsetof(WASMArrayNewInitValues,
  1004. elem_data)
  1005. + (uint64)sizeof(WASMValue) * len;
  1006. if (!(array_init_values =
  1007. loader_malloc(total_size, error_buf,
  1008. error_buf_size))) {
  1009. goto fail;
  1010. }
  1011. array_init_values->type_idx = type_idx;
  1012. array_init_values->length = len;
  1013. for (i = len; i > 0; i--) {
  1014. if (!pop_const_expr_stack(
  1015. &const_expr_ctx, NULL, elem_type,
  1016. elem_ref_type, NULL,
  1017. &array_init_values
  1018. ->elem_data[i - 1],
  1019. error_buf, error_buf_size)) {
  1020. destroy_init_expr_data_recursive(
  1021. module, array_init_values);
  1022. goto fail;
  1023. }
  1024. }
  1025. cur_value.data = array_init_values;
  1026. }
  1027. }
  1028. else {
  1029. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1030. WASMValue len_val;
  1031. uint32 len;
  1032. /* POP(i32) */
  1033. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1034. VALUE_TYPE_I32, NULL,
  1035. NULL, &len_val, error_buf,
  1036. error_buf_size)) {
  1037. goto fail;
  1038. }
  1039. len = len_val.i32;
  1040. cur_value.array_new_default.type_index = type_idx;
  1041. cur_value.array_new_default.length = len;
  1042. }
  1043. wasm_set_refheaptype_typeidx(
  1044. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1045. if (!push_const_expr_stack(
  1046. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1047. &cur_ref_type, (uint8)opcode1, &cur_value,
  1048. error_buf, error_buf_size)) {
  1049. if (array_init_values) {
  1050. destroy_init_expr_data_recursive(
  1051. module, array_init_values);
  1052. }
  1053. goto fail;
  1054. }
  1055. break;
  1056. }
  1057. case WASM_OP_ANY_CONVERT_EXTERN:
  1058. {
  1059. set_error_buf(error_buf, error_buf_size,
  1060. "unsupported constant expression of "
  1061. "extern.internalize");
  1062. goto fail;
  1063. }
  1064. case WASM_OP_EXTERN_CONVERT_ANY:
  1065. {
  1066. set_error_buf(error_buf, error_buf_size,
  1067. "unsupported constant expression of "
  1068. "extern.externalize");
  1069. goto fail;
  1070. }
  1071. case WASM_OP_REF_I31:
  1072. {
  1073. /* POP(i32) */
  1074. if (!pop_const_expr_stack(
  1075. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1076. NULL, &cur_value, error_buf, error_buf_size)) {
  1077. goto fail;
  1078. }
  1079. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1080. false, HEAP_TYPE_I31);
  1081. if (!push_const_expr_stack(
  1082. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1083. &cur_ref_type, (uint8)opcode1, &cur_value,
  1084. error_buf, error_buf_size)) {
  1085. goto fail;
  1086. }
  1087. break;
  1088. }
  1089. default:
  1090. set_error_buf(
  1091. error_buf, error_buf_size,
  1092. "type mismatch or constant expression required");
  1093. goto fail;
  1094. }
  1095. break;
  1096. }
  1097. #endif /* end of WASM_ENABLE_GC != 0 */
  1098. default:
  1099. {
  1100. set_error_buf(error_buf, error_buf_size,
  1101. "illegal opcode "
  1102. "or constant expression required "
  1103. "or type mismatch");
  1104. goto fail;
  1105. }
  1106. }
  1107. CHECK_BUF(p, p_end, 1);
  1108. flag = read_uint8(p);
  1109. }
  1110. /* There should be only one value left on the init value stack */
  1111. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1112. #if WASM_ENABLE_GC != 0
  1113. ref_type, &opcode,
  1114. #endif
  1115. &cur_value, error_buf, error_buf_size)) {
  1116. goto fail;
  1117. }
  1118. if (const_expr_ctx.sp != 0) {
  1119. set_error_buf(error_buf, error_buf_size,
  1120. "type mismatch: illegal constant opcode sequence");
  1121. goto fail;
  1122. }
  1123. init_expr->init_expr_type = flag;
  1124. init_expr->u = cur_value;
  1125. #if WASM_ENABLE_GC != 0
  1126. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1127. switch (opcode) {
  1128. case WASM_OP_STRUCT_NEW:
  1129. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1130. break;
  1131. case WASM_OP_STRUCT_NEW_DEFAULT:
  1132. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1133. break;
  1134. case WASM_OP_ARRAY_NEW:
  1135. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1136. break;
  1137. case WASM_OP_ARRAY_NEW_DEFAULT:
  1138. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1139. break;
  1140. case WASM_OP_ARRAY_NEW_FIXED:
  1141. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1142. break;
  1143. case WASM_OP_REF_I31:
  1144. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1145. break;
  1146. default:
  1147. bh_assert(0);
  1148. break;
  1149. }
  1150. }
  1151. #endif /* end of WASM_ENABLE_GC != 0 */
  1152. *p_buf = p;
  1153. destroy_const_expr_stack(&const_expr_ctx);
  1154. return true;
  1155. fail:
  1156. destroy_const_expr_stack(&const_expr_ctx);
  1157. return false;
  1158. }
  1159. static bool
  1160. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1161. {
  1162. if (mutable >= 2) {
  1163. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1164. return false;
  1165. }
  1166. return true;
  1167. }
  1168. #if WASM_ENABLE_GC != 0
  1169. static void
  1170. destroy_func_type(WASMFuncType *type)
  1171. {
  1172. /* Destroy the reference type hash set */
  1173. if (type->ref_type_maps)
  1174. wasm_runtime_free(type->ref_type_maps);
  1175. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1176. && WASM_ENABLE_LAZY_JIT != 0
  1177. if (type->call_to_llvm_jit_from_fast_jit)
  1178. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1179. #endif
  1180. /* Free the type */
  1181. wasm_runtime_free(type);
  1182. }
  1183. static void
  1184. destroy_struct_type(WASMStructType *type)
  1185. {
  1186. if (type->ref_type_maps)
  1187. wasm_runtime_free(type->ref_type_maps);
  1188. wasm_runtime_free(type);
  1189. }
  1190. static void
  1191. destroy_array_type(WASMArrayType *type)
  1192. {
  1193. wasm_runtime_free(type);
  1194. }
  1195. static void
  1196. destroy_wasm_type(WASMType *type)
  1197. {
  1198. if (type->ref_count > 1) {
  1199. /* The type is referenced by other types
  1200. of current wasm module */
  1201. type->ref_count--;
  1202. return;
  1203. }
  1204. if (type->type_flag == WASM_TYPE_FUNC)
  1205. destroy_func_type((WASMFuncType *)type);
  1206. else if (type->type_flag == WASM_TYPE_STRUCT)
  1207. destroy_struct_type((WASMStructType *)type);
  1208. else if (type->type_flag == WASM_TYPE_ARRAY)
  1209. destroy_array_type((WASMArrayType *)type);
  1210. else {
  1211. bh_assert(0);
  1212. }
  1213. }
  1214. /* Resolve (ref null ht) or (ref ht) */
  1215. static bool
  1216. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1217. WASMModule *module, uint32 type_count, bool nullable,
  1218. WASMRefType *ref_type, char *error_buf,
  1219. uint32 error_buf_size)
  1220. {
  1221. const uint8 *p = *p_buf, *p_end = buf_end;
  1222. ref_type->ref_type =
  1223. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1224. ref_type->ref_ht_common.nullable = nullable;
  1225. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1226. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1227. /* heap type is (type i), i : typeidx, >= 0 */
  1228. if (!check_type_index(module, type_count,
  1229. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1230. error_buf_size)) {
  1231. return false;
  1232. }
  1233. }
  1234. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1235. /* heap type is func, extern, any, eq, i31 or data */
  1236. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1237. return false;
  1238. }
  1239. *p_buf = p;
  1240. return true;
  1241. fail:
  1242. return false;
  1243. }
  1244. static bool
  1245. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1246. WASMModule *module, uint32 type_count,
  1247. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1248. bool allow_packed_type, char *error_buf,
  1249. uint32 error_buf_size)
  1250. {
  1251. const uint8 *p = *p_buf, *p_end = buf_end;
  1252. uint8 type;
  1253. memset(ref_type, 0, sizeof(WASMRefType));
  1254. CHECK_BUF(p, p_end, 1);
  1255. type = read_uint8(p);
  1256. if (wasm_is_reftype_htref_nullable(type)) {
  1257. /* (ref null ht) */
  1258. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1259. ref_type, error_buf, error_buf_size))
  1260. return false;
  1261. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1262. *p_need_ref_type_map = true;
  1263. else {
  1264. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1265. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1266. multi-byte type to one-byte type to reduce the footprint and
  1267. the complexity of type equal/subtype checking */
  1268. ref_type->ref_type =
  1269. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1270. *p_need_ref_type_map = false;
  1271. }
  1272. }
  1273. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1274. /* (ref ht) */
  1275. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1276. ref_type, error_buf, error_buf_size))
  1277. return false;
  1278. *p_need_ref_type_map = true;
  1279. #if WASM_ENABLE_STRINGREF != 0
  1280. /* covert (ref string) to stringref */
  1281. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1282. ref_type->ref_type =
  1283. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1284. *p_need_ref_type_map = false;
  1285. }
  1286. #endif
  1287. }
  1288. else {
  1289. /* type which can be represented by one byte */
  1290. if (!is_valid_value_type_for_interpreter(type)
  1291. && !(allow_packed_type && is_packed_type(type))) {
  1292. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1293. return false;
  1294. }
  1295. ref_type->ref_type = type;
  1296. *p_need_ref_type_map = false;
  1297. #if WASM_ENABLE_WAMR_COMPILER != 0
  1298. /* If any value's type is v128, mark the module as SIMD used */
  1299. if (type == VALUE_TYPE_V128)
  1300. module->is_simd_used = true;
  1301. #endif
  1302. }
  1303. *p_buf = p;
  1304. return true;
  1305. fail:
  1306. return false;
  1307. }
  1308. static WASMRefType *
  1309. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1310. char *error_buf, uint32 error_buf_size)
  1311. {
  1312. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1313. if (!ret) {
  1314. set_error_buf(error_buf, error_buf_size,
  1315. "insert ref type to hash set failed");
  1316. }
  1317. return ret;
  1318. }
  1319. static bool
  1320. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1321. uint32 type_count, uint32 type_idx, char *error_buf,
  1322. uint32 error_buf_size)
  1323. {
  1324. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1325. uint32 param_count, result_count, i, j = 0;
  1326. uint32 param_cell_num, ret_cell_num;
  1327. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1328. uint64 total_size;
  1329. bool need_ref_type_map;
  1330. WASMRefType ref_type;
  1331. WASMFuncType *type = NULL;
  1332. /* Parse first time to resolve param count, result count and
  1333. ref type map count */
  1334. read_leb_uint32(p, p_end, param_count);
  1335. p_org = p;
  1336. for (i = 0; i < param_count; i++) {
  1337. if (!resolve_value_type(&p, p_end, module, type_count,
  1338. &need_ref_type_map, &ref_type, false, error_buf,
  1339. error_buf_size)) {
  1340. return false;
  1341. }
  1342. if (need_ref_type_map)
  1343. ref_type_map_count++;
  1344. }
  1345. read_leb_uint32(p, p_end, result_count);
  1346. for (i = 0; i < result_count; i++) {
  1347. if (!resolve_value_type(&p, p_end, module, type_count,
  1348. &need_ref_type_map, &ref_type, false, error_buf,
  1349. error_buf_size)) {
  1350. return false;
  1351. }
  1352. if (need_ref_type_map) {
  1353. ref_type_map_count++;
  1354. result_ref_type_map_count++;
  1355. }
  1356. }
  1357. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1358. "ref type map count: %d",
  1359. type_idx, param_count, result_count, ref_type_map_count);
  1360. /* Parse second time to resolve param types, result types and
  1361. ref type map info */
  1362. p = p_org;
  1363. total_size = offsetof(WASMFuncType, types)
  1364. + sizeof(uint8) * (uint64)(param_count + result_count);
  1365. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1366. return false;
  1367. }
  1368. if (ref_type_map_count > 0) {
  1369. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1370. if (!(type->ref_type_maps =
  1371. loader_malloc(total_size, error_buf, error_buf_size))) {
  1372. goto fail;
  1373. }
  1374. }
  1375. type->base_type.type_flag = WASM_TYPE_FUNC;
  1376. type->param_count = param_count;
  1377. type->result_count = result_count;
  1378. type->ref_type_map_count = ref_type_map_count;
  1379. if (ref_type_map_count > 0) {
  1380. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1381. - result_ref_type_map_count;
  1382. }
  1383. for (i = 0; i < param_count; i++) {
  1384. if (!resolve_value_type(&p, p_end, module, type_count,
  1385. &need_ref_type_map, &ref_type, false, error_buf,
  1386. error_buf_size)) {
  1387. goto fail;
  1388. }
  1389. type->types[i] = ref_type.ref_type;
  1390. if (need_ref_type_map) {
  1391. type->ref_type_maps[j].index = i;
  1392. if (!(type->ref_type_maps[j++].ref_type =
  1393. reftype_set_insert(module->ref_type_set, &ref_type,
  1394. error_buf, error_buf_size))) {
  1395. goto fail;
  1396. }
  1397. }
  1398. }
  1399. read_leb_uint32(p, p_end, result_count);
  1400. for (i = 0; i < result_count; i++) {
  1401. if (!resolve_value_type(&p, p_end, module, type_count,
  1402. &need_ref_type_map, &ref_type, false, error_buf,
  1403. error_buf_size)) {
  1404. goto fail;
  1405. }
  1406. type->types[param_count + i] = ref_type.ref_type;
  1407. if (need_ref_type_map) {
  1408. type->ref_type_maps[j].index = param_count + i;
  1409. if (!(type->ref_type_maps[j++].ref_type =
  1410. reftype_set_insert(module->ref_type_set, &ref_type,
  1411. error_buf, error_buf_size))) {
  1412. goto fail;
  1413. }
  1414. }
  1415. }
  1416. bh_assert(j == type->ref_type_map_count);
  1417. #if TRACE_WASM_LOADER != 0
  1418. os_printf("type %d = ", type_idx);
  1419. wasm_dump_func_type(type);
  1420. #endif
  1421. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1422. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1423. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1424. set_error_buf(error_buf, error_buf_size,
  1425. "param count or result count too large");
  1426. goto fail;
  1427. }
  1428. type->param_cell_num = (uint16)param_cell_num;
  1429. type->ret_cell_num = (uint16)ret_cell_num;
  1430. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1431. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1432. #endif
  1433. #if WASM_ENABLE_WAMR_COMPILER != 0
  1434. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1435. if (type->types[i] == VALUE_TYPE_V128)
  1436. module->is_simd_used = true;
  1437. }
  1438. #endif
  1439. *p_buf = p;
  1440. module->types[type_idx] = (WASMType *)type;
  1441. return true;
  1442. fail:
  1443. if (type)
  1444. destroy_func_type(type);
  1445. return false;
  1446. }
  1447. static bool
  1448. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1449. WASMModule *module, uint32 type_count, uint32 type_idx,
  1450. char *error_buf, uint32 error_buf_size)
  1451. {
  1452. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1453. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1454. uint32 i, j = 0, offset;
  1455. uint16 *reference_table;
  1456. uint64 total_size;
  1457. uint8 mutable;
  1458. bool need_ref_type_map;
  1459. WASMRefType ref_type;
  1460. WASMStructType *type = NULL;
  1461. /* Parse first time to resolve field count and ref type map count */
  1462. read_leb_uint32(p, p_end, field_count);
  1463. p_org = p;
  1464. for (i = 0; i < field_count; i++) {
  1465. if (!resolve_value_type(&p, p_end, module, type_count,
  1466. &need_ref_type_map, &ref_type, true, error_buf,
  1467. error_buf_size)) {
  1468. return false;
  1469. }
  1470. if (need_ref_type_map)
  1471. ref_type_map_count++;
  1472. if (wasm_is_type_reftype(ref_type.ref_type))
  1473. ref_field_count++;
  1474. CHECK_BUF(p, p_end, 1);
  1475. mutable = read_uint8(p);
  1476. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1477. return false;
  1478. }
  1479. }
  1480. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1481. type_idx, field_count, ref_type_map_count);
  1482. /* Parse second time to resolve field types and ref type map info */
  1483. p = p_org;
  1484. total_size = offsetof(WASMStructType, fields)
  1485. + sizeof(WASMStructFieldType) * (uint64)field_count
  1486. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1487. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1488. return false;
  1489. }
  1490. if (ref_type_map_count > 0) {
  1491. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1492. if (!(type->ref_type_maps =
  1493. loader_malloc(total_size, error_buf, error_buf_size))) {
  1494. goto fail;
  1495. }
  1496. }
  1497. type->reference_table = reference_table =
  1498. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1499. + sizeof(WASMStructFieldType) * field_count);
  1500. *reference_table++ = ref_field_count;
  1501. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1502. type->field_count = field_count;
  1503. type->ref_type_map_count = ref_type_map_count;
  1504. offset = (uint32)offsetof(WASMStructObject, field_data);
  1505. for (i = 0; i < field_count; i++) {
  1506. if (!resolve_value_type(&p, p_end, module, type_count,
  1507. &need_ref_type_map, &ref_type, true, error_buf,
  1508. error_buf_size)) {
  1509. goto fail;
  1510. }
  1511. type->fields[i].field_type = ref_type.ref_type;
  1512. if (need_ref_type_map) {
  1513. type->ref_type_maps[j].index = i;
  1514. if (!(type->ref_type_maps[j++].ref_type =
  1515. reftype_set_insert(module->ref_type_set, &ref_type,
  1516. error_buf, error_buf_size))) {
  1517. goto fail;
  1518. }
  1519. }
  1520. CHECK_BUF(p, p_end, 1);
  1521. type->fields[i].field_flags = read_uint8(p);
  1522. type->fields[i].field_size =
  1523. (uint8)wasm_reftype_size(ref_type.ref_type);
  1524. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1525. || defined(BUILD_TARGET_X86_32))
  1526. if (type->fields[i].field_size == 2)
  1527. offset = align_uint(offset, 2);
  1528. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1529. offset = align_uint(offset, 4);
  1530. #endif
  1531. type->fields[i].field_offset = offset;
  1532. if (wasm_is_type_reftype(ref_type.ref_type))
  1533. *reference_table++ = offset;
  1534. offset += type->fields[i].field_size;
  1535. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1536. type->fields[i].field_flags, type->fields[i].field_type);
  1537. }
  1538. type->total_size = offset;
  1539. bh_assert(j == type->ref_type_map_count);
  1540. #if TRACE_WASM_LOADER != 0
  1541. os_printf("type %d = ", type_idx);
  1542. wasm_dump_struct_type(type);
  1543. #endif
  1544. *p_buf = p;
  1545. module->types[type_idx] = (WASMType *)type;
  1546. return true;
  1547. fail:
  1548. if (type)
  1549. destroy_struct_type(type);
  1550. return false;
  1551. }
  1552. static bool
  1553. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1554. WASMModule *module, uint32 type_count, uint32 type_idx,
  1555. char *error_buf, uint32 error_buf_size)
  1556. {
  1557. const uint8 *p = *p_buf, *p_end = buf_end;
  1558. uint8 mutable;
  1559. bool need_ref_type_map;
  1560. WASMRefType ref_type;
  1561. WASMArrayType *type = NULL;
  1562. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1563. &ref_type, true, error_buf, error_buf_size)) {
  1564. return false;
  1565. }
  1566. CHECK_BUF(p, p_end, 1);
  1567. mutable = read_uint8(p);
  1568. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1569. return false;
  1570. }
  1571. LOG_VERBOSE("type %u: array", type_idx);
  1572. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1573. error_buf_size))) {
  1574. return false;
  1575. }
  1576. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1577. type->elem_flags = mutable;
  1578. type->elem_type = ref_type.ref_type;
  1579. if (need_ref_type_map) {
  1580. if (!(type->elem_ref_type =
  1581. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1582. error_buf_size))) {
  1583. goto fail;
  1584. }
  1585. }
  1586. #if TRACE_WASM_LOADER != 0
  1587. os_printf("type %d = ", type_idx);
  1588. wasm_dump_array_type(type);
  1589. #endif
  1590. *p_buf = p;
  1591. module->types[type_idx] = (WASMType *)type;
  1592. return true;
  1593. fail:
  1594. if (type)
  1595. destroy_array_type(type);
  1596. return false;
  1597. }
  1598. static bool
  1599. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1600. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1601. {
  1602. if (heap_type >= 0) {
  1603. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1604. error_buf_size)) {
  1605. return false;
  1606. }
  1607. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1608. heap_type);
  1609. }
  1610. else {
  1611. if (!wasm_is_valid_heap_type(heap_type)) {
  1612. set_error_buf(error_buf, error_buf_size, "unknown type");
  1613. return false;
  1614. }
  1615. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1616. heap_type);
  1617. if (nullable) {
  1618. /* For (ref null func/extern/any/eq/i31/data),
  1619. they are same as
  1620. funcref/externref/anyref/eqref/i31ref/dataref,
  1621. we convert the multi-byte type to one-byte
  1622. type to reduce the footprint and the
  1623. complexity of type equal/subtype checking */
  1624. ref_type->ref_type =
  1625. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1626. }
  1627. }
  1628. return true;
  1629. }
  1630. static void
  1631. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1632. WASMRefType *ref_type2)
  1633. {
  1634. /**
  1635. * The difference rt1 ∖ rt2 between two reference types is defined as
  1636. * follows:
  1637. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1638. * (ref ht2) = (ref null?1 ht1)
  1639. */
  1640. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1641. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1642. ref_type1, wasm_reftype_struct_size(ref_type1));
  1643. }
  1644. else {
  1645. ref_type_diff->ref_type = ref_type1->ref_type;
  1646. }
  1647. if (ref_type2->ref_ht_common.nullable) {
  1648. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1649. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1650. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1651. (int32)ref_type_diff->ref_type - 0x80);
  1652. }
  1653. else {
  1654. ref_type_diff->ref_ht_typeidx.nullable = false;
  1655. }
  1656. }
  1657. }
  1658. #else /* else of WASM_ENABLE_GC != 0 */
  1659. static void
  1660. destroy_wasm_type(WASMType *type)
  1661. {
  1662. if (type->ref_count > 1) {
  1663. /* The type is referenced by other types
  1664. of current wasm module */
  1665. type->ref_count--;
  1666. return;
  1667. }
  1668. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1669. && WASM_ENABLE_LAZY_JIT != 0
  1670. if (type->call_to_llvm_jit_from_fast_jit)
  1671. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1672. #endif
  1673. wasm_runtime_free(type);
  1674. }
  1675. #endif /* end of WASM_ENABLE_GC != 0 */
  1676. static bool
  1677. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1678. char *error_buf, uint32 error_buf_size)
  1679. {
  1680. const uint8 *p = buf, *p_end = buf_end;
  1681. uint32 type_count, i;
  1682. uint64 total_size;
  1683. uint8 flag;
  1684. #if WASM_ENABLE_GC != 0
  1685. uint32 processed_type_count = 0;
  1686. #endif
  1687. read_leb_uint32(p, p_end, type_count);
  1688. if (type_count) {
  1689. module->type_count = type_count;
  1690. total_size = sizeof(WASMType *) * (uint64)type_count;
  1691. if (!(module->types =
  1692. loader_malloc(total_size, error_buf, error_buf_size))) {
  1693. return false;
  1694. }
  1695. #if WASM_ENABLE_GC == 0
  1696. for (i = 0; i < type_count; i++) {
  1697. WASMFuncType *type;
  1698. const uint8 *p_org;
  1699. uint32 param_count, result_count, j;
  1700. uint32 param_cell_num, ret_cell_num;
  1701. CHECK_BUF(p, p_end, 1);
  1702. flag = read_uint8(p);
  1703. if (flag != 0x60) {
  1704. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1705. return false;
  1706. }
  1707. read_leb_uint32(p, p_end, param_count);
  1708. /* Resolve param count and result count firstly */
  1709. p_org = p;
  1710. CHECK_BUF(p, p_end, param_count);
  1711. p += param_count;
  1712. read_leb_uint32(p, p_end, result_count);
  1713. CHECK_BUF(p, p_end, result_count);
  1714. p = p_org;
  1715. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1716. set_error_buf(error_buf, error_buf_size,
  1717. "param count or result count too large");
  1718. return false;
  1719. }
  1720. total_size = offsetof(WASMFuncType, types)
  1721. + sizeof(uint8) * (uint64)(param_count + result_count);
  1722. if (!(type = module->types[i] =
  1723. loader_malloc(total_size, error_buf, error_buf_size))) {
  1724. return false;
  1725. }
  1726. /* Resolve param types and result types */
  1727. type->ref_count = 1;
  1728. type->param_count = (uint16)param_count;
  1729. type->result_count = (uint16)result_count;
  1730. for (j = 0; j < param_count; j++) {
  1731. CHECK_BUF(p, p_end, 1);
  1732. type->types[j] = read_uint8(p);
  1733. }
  1734. read_leb_uint32(p, p_end, result_count);
  1735. for (j = 0; j < result_count; j++) {
  1736. CHECK_BUF(p, p_end, 1);
  1737. type->types[param_count + j] = read_uint8(p);
  1738. }
  1739. for (j = 0; j < param_count + result_count; j++) {
  1740. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1741. set_error_buf(error_buf, error_buf_size,
  1742. "unknown value type");
  1743. return false;
  1744. }
  1745. }
  1746. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1747. ret_cell_num =
  1748. wasm_get_cell_num(type->types + param_count, result_count);
  1749. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1750. set_error_buf(error_buf, error_buf_size,
  1751. "param count or result count too large");
  1752. return false;
  1753. }
  1754. type->param_cell_num = (uint16)param_cell_num;
  1755. type->ret_cell_num = (uint16)ret_cell_num;
  1756. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1757. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1758. #endif
  1759. #if WASM_ENABLE_WAMR_COMPILER != 0
  1760. for (j = 0; j < type->param_count + type->result_count; j++) {
  1761. if (type->types[j] == VALUE_TYPE_V128)
  1762. module->is_simd_used = true;
  1763. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1764. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1765. module->is_ref_types_used = true;
  1766. }
  1767. #endif
  1768. /* If there is already a same type created, use it instead */
  1769. for (j = 0; j < i; j++) {
  1770. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1771. if (module->types[j]->ref_count == UINT16_MAX) {
  1772. set_error_buf(error_buf, error_buf_size,
  1773. "wasm type's ref count too large");
  1774. return false;
  1775. }
  1776. destroy_wasm_type(type);
  1777. module->types[i] = module->types[j];
  1778. module->types[j]->ref_count++;
  1779. break;
  1780. }
  1781. }
  1782. }
  1783. #else /* else of WASM_ENABLE_GC == 0 */
  1784. for (i = 0; i < type_count; i++) {
  1785. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1786. uint32 rec_count = 1, j;
  1787. bool is_sub_final = true;
  1788. CHECK_BUF(p, p_end, 1);
  1789. flag = read_uint8(p);
  1790. if (flag == DEFINED_TYPE_REC) {
  1791. read_leb_uint32(p, p_end, rec_count);
  1792. if (rec_count > 1) {
  1793. uint64 new_total_size;
  1794. /* integer overflow */
  1795. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1796. set_error_buf(error_buf, error_buf_size,
  1797. "recursive type count too large");
  1798. return false;
  1799. }
  1800. module->type_count += rec_count - 1;
  1801. new_total_size =
  1802. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1803. if (new_total_size > UINT32_MAX) {
  1804. set_error_buf(error_buf, error_buf_size,
  1805. "allocate memory failed");
  1806. return false;
  1807. }
  1808. MEM_REALLOC(module->types, (uint32)total_size,
  1809. (uint32)new_total_size);
  1810. total_size = new_total_size;
  1811. }
  1812. LOG_VERBOSE("Processing rec group [%d-%d]",
  1813. processed_type_count,
  1814. processed_type_count + rec_count - 1);
  1815. }
  1816. else {
  1817. p--;
  1818. }
  1819. for (j = 0; j < rec_count; j++) {
  1820. WASMType *cur_type = NULL;
  1821. CHECK_BUF(p, p_end, 1);
  1822. flag = read_uint8(p);
  1823. parent_type_idx = -1;
  1824. if (flag == DEFINED_TYPE_SUB
  1825. || flag == DEFINED_TYPE_SUB_FINAL) {
  1826. read_leb_uint32(p, p_end, super_type_count);
  1827. if (super_type_count > 1) {
  1828. set_error_buf(error_buf, error_buf_size,
  1829. "super type count too large");
  1830. return false;
  1831. }
  1832. if (super_type_count > 0) {
  1833. read_leb_uint32(p, p_end, parent_type_idx);
  1834. if (parent_type_idx >= processed_type_count + j) {
  1835. set_error_buf_v(error_buf, error_buf_size,
  1836. "unknown type %d", parent_type_idx);
  1837. return false;
  1838. }
  1839. if (module->types[parent_type_idx]->is_sub_final) {
  1840. set_error_buf(error_buf, error_buf_size,
  1841. "sub type can not inherit from "
  1842. "a final super type");
  1843. return false;
  1844. }
  1845. }
  1846. if (flag == DEFINED_TYPE_SUB)
  1847. is_sub_final = false;
  1848. CHECK_BUF(p, p_end, 1);
  1849. flag = read_uint8(p);
  1850. }
  1851. if (flag == DEFINED_TYPE_FUNC) {
  1852. if (!resolve_func_type(&p, buf_end, module,
  1853. processed_type_count + rec_count,
  1854. processed_type_count + j, error_buf,
  1855. error_buf_size)) {
  1856. return false;
  1857. }
  1858. }
  1859. else if (flag == DEFINED_TYPE_STRUCT) {
  1860. if (!resolve_struct_type(&p, buf_end, module,
  1861. processed_type_count + rec_count,
  1862. processed_type_count + j,
  1863. error_buf, error_buf_size)) {
  1864. return false;
  1865. }
  1866. }
  1867. else if (flag == DEFINED_TYPE_ARRAY) {
  1868. if (!resolve_array_type(&p, buf_end, module,
  1869. processed_type_count + rec_count,
  1870. processed_type_count + j, error_buf,
  1871. error_buf_size)) {
  1872. return false;
  1873. }
  1874. }
  1875. else {
  1876. set_error_buf(error_buf, error_buf_size,
  1877. "invalid type flag");
  1878. return false;
  1879. }
  1880. cur_type = module->types[processed_type_count + j];
  1881. cur_type->ref_count = 1;
  1882. cur_type->parent_type_idx = parent_type_idx;
  1883. cur_type->is_sub_final = is_sub_final;
  1884. cur_type->rec_count = rec_count;
  1885. cur_type->rec_idx = j;
  1886. cur_type->rec_begin_type_idx = processed_type_count;
  1887. }
  1888. /* resolve subtyping relationship in current rec group */
  1889. for (j = 0; j < rec_count; j++) {
  1890. WASMType *cur_type = module->types[processed_type_count + j];
  1891. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1892. WASMType *parent_type =
  1893. module->types[cur_type->parent_type_idx];
  1894. cur_type->parent_type = parent_type;
  1895. cur_type->root_type = parent_type->root_type;
  1896. if (parent_type->inherit_depth == UINT16_MAX) {
  1897. set_error_buf(error_buf, error_buf_size,
  1898. "parent type's inherit depth too large");
  1899. return false;
  1900. }
  1901. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1902. }
  1903. else {
  1904. cur_type->parent_type = NULL;
  1905. cur_type->root_type = cur_type;
  1906. cur_type->inherit_depth = 0;
  1907. }
  1908. }
  1909. for (j = 0; j < rec_count; j++) {
  1910. WASMType *cur_type = module->types[processed_type_count + j];
  1911. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1912. WASMType *parent_type =
  1913. module->types[cur_type->parent_type_idx];
  1914. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1915. module->types,
  1916. module->type_count)) {
  1917. set_error_buf_v(error_buf, error_buf_size,
  1918. "sub type %u does not match super type",
  1919. processed_type_count + j);
  1920. return false;
  1921. }
  1922. }
  1923. }
  1924. /* If there is already an equivalence type or a group of equivalence
  1925. recursive types created, use it or them instead */
  1926. for (j = 0; j < processed_type_count;) {
  1927. WASMType *src_type = module->types[j];
  1928. WASMType *cur_type = module->types[processed_type_count];
  1929. uint32 k, src_rec_count;
  1930. src_rec_count = src_type->rec_count;
  1931. if (src_rec_count != rec_count) {
  1932. /* no type equivalence */
  1933. j += src_rec_count;
  1934. continue;
  1935. }
  1936. for (k = 0; k < rec_count; k++) {
  1937. src_type = module->types[j + k];
  1938. cur_type = module->types[processed_type_count + k];
  1939. if (!wasm_type_equal(src_type, cur_type, module->types,
  1940. module->type_count)) {
  1941. break;
  1942. }
  1943. }
  1944. if (k < rec_count) {
  1945. /* no type equivalence */
  1946. j += src_rec_count;
  1947. continue;
  1948. }
  1949. /* type equivalence */
  1950. for (k = 0; k < rec_count; k++) {
  1951. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1952. set_error_buf(error_buf, error_buf_size,
  1953. "wasm type's ref count too large");
  1954. return false;
  1955. }
  1956. destroy_wasm_type(module->types[processed_type_count + k]);
  1957. module->types[processed_type_count + k] =
  1958. module->types[j + k];
  1959. module->types[j + k]->ref_count++;
  1960. }
  1961. break;
  1962. }
  1963. if (rec_count > 1) {
  1964. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1965. processed_type_count,
  1966. processed_type_count + rec_count - 1);
  1967. }
  1968. processed_type_count += rec_count;
  1969. }
  1970. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1971. * module->type_count,
  1972. error_buf, error_buf_size))) {
  1973. return false;
  1974. }
  1975. #endif /* end of WASM_ENABLE_GC == 0 */
  1976. }
  1977. if (p != p_end) {
  1978. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1979. return false;
  1980. }
  1981. LOG_VERBOSE("Load type section success.\n");
  1982. return true;
  1983. fail:
  1984. return false;
  1985. }
  1986. static void
  1987. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  1988. uint32 *max_size)
  1989. {
  1990. uint32 default_max_size;
  1991. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  1992. * ABI unchanged */
  1993. (void)is_table64;
  1994. if (UINT32_MAX / 2 > init_size)
  1995. default_max_size = init_size * 2;
  1996. else
  1997. default_max_size = UINT32_MAX;
  1998. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1999. default_max_size = WASM_TABLE_MAX_SIZE;
  2000. if (max_size_flag) {
  2001. /* module defines the table limitation */
  2002. bh_assert(init_size <= *max_size);
  2003. if (init_size < *max_size) {
  2004. *max_size =
  2005. *max_size < default_max_size ? *max_size : default_max_size;
  2006. }
  2007. }
  2008. else {
  2009. /* partial defined table limitation, gives a default value */
  2010. *max_size = default_max_size;
  2011. }
  2012. }
  2013. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2014. /**
  2015. * Find export item of a module with export info:
  2016. * module name, field name and export kind
  2017. */
  2018. static WASMExport *
  2019. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2020. const char *field_name, uint8 export_kind,
  2021. char *error_buf, uint32 error_buf_size)
  2022. {
  2023. WASMExport *export =
  2024. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2025. export_kind, error_buf, error_buf_size);
  2026. return export;
  2027. }
  2028. #endif
  2029. #if WASM_ENABLE_MULTI_MODULE != 0
  2030. static WASMTable *
  2031. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2032. uint32 init_size, uint32 max_size, char *error_buf,
  2033. uint32 error_buf_size)
  2034. {
  2035. WASMModuleCommon *module_reg;
  2036. WASMTable *table = NULL;
  2037. WASMExport *export = NULL;
  2038. WASMModule *module = NULL;
  2039. module_reg = wasm_runtime_find_module_registered(module_name);
  2040. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2041. LOG_DEBUG("can not find a module named %s for table", module_name);
  2042. set_error_buf(error_buf, error_buf_size, "unknown import");
  2043. return NULL;
  2044. }
  2045. module = (WASMModule *)module_reg;
  2046. export =
  2047. wasm_loader_find_export(module, module_name, table_name,
  2048. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2049. if (!export) {
  2050. return NULL;
  2051. }
  2052. /* resolve table and check the init/max size */
  2053. if (export->index < module->import_table_count) {
  2054. table =
  2055. module->import_tables[export->index].u.table.import_table_linked;
  2056. }
  2057. else {
  2058. table = &(module->tables[export->index - module->import_table_count]);
  2059. }
  2060. if (table->table_type.init_size < init_size
  2061. || table->table_type.max_size > max_size) {
  2062. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2063. module_name, table_name, table->table_type.init_size,
  2064. table->table_type.max_size, init_size, max_size);
  2065. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2066. return NULL;
  2067. }
  2068. return table;
  2069. }
  2070. static WASMMemory *
  2071. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2072. uint32 init_page_count, uint32 max_page_count,
  2073. char *error_buf, uint32 error_buf_size)
  2074. {
  2075. WASMModuleCommon *module_reg;
  2076. WASMMemory *memory = NULL;
  2077. WASMExport *export = NULL;
  2078. WASMModule *module = NULL;
  2079. module_reg = wasm_runtime_find_module_registered(module_name);
  2080. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2081. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2082. set_error_buf(error_buf, error_buf_size, "unknown import");
  2083. return NULL;
  2084. }
  2085. module = (WASMModule *)module_reg;
  2086. export =
  2087. wasm_loader_find_export(module, module_name, memory_name,
  2088. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2089. if (!export) {
  2090. return NULL;
  2091. }
  2092. /* resolve memory and check the init/max page count */
  2093. if (export->index < module->import_memory_count) {
  2094. memory = module->import_memories[export->index]
  2095. .u.memory.import_memory_linked;
  2096. }
  2097. else {
  2098. memory =
  2099. &(module->memories[export->index - module->import_memory_count]);
  2100. }
  2101. if (memory->init_page_count < init_page_count
  2102. || memory->max_page_count > max_page_count) {
  2103. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2104. module_name, memory_name, memory->init_page_count,
  2105. memory->max_page_count, init_page_count, max_page_count);
  2106. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2107. return NULL;
  2108. }
  2109. return memory;
  2110. }
  2111. static WASMGlobal *
  2112. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2113. uint8 type, bool is_mutable, char *error_buf,
  2114. uint32 error_buf_size)
  2115. {
  2116. WASMModuleCommon *module_reg;
  2117. WASMGlobal *global = NULL;
  2118. WASMExport *export = NULL;
  2119. WASMModule *module = NULL;
  2120. module_reg = wasm_runtime_find_module_registered(module_name);
  2121. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2122. LOG_DEBUG("can not find a module named %s for global", module_name);
  2123. set_error_buf(error_buf, error_buf_size, "unknown import");
  2124. return NULL;
  2125. }
  2126. module = (WASMModule *)module_reg;
  2127. export =
  2128. wasm_loader_find_export(module, module_name, global_name,
  2129. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2130. if (!export) {
  2131. return NULL;
  2132. }
  2133. /* resolve and check the global */
  2134. if (export->index < module->import_global_count) {
  2135. global =
  2136. module->import_globals[export->index].u.global.import_global_linked;
  2137. }
  2138. else {
  2139. global =
  2140. &(module->globals[export->index - module->import_global_count]);
  2141. }
  2142. if (global->type.val_type != type
  2143. || global->type.is_mutable != is_mutable) {
  2144. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2145. module_name, global_name, global->type.val_type,
  2146. global->type.is_mutable, type, is_mutable);
  2147. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2148. return NULL;
  2149. }
  2150. return global;
  2151. }
  2152. #if WASM_ENABLE_TAGS != 0
  2153. static WASMTag *
  2154. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2155. const WASMType *expected_tag_type,
  2156. uint32 *linked_tag_index, char *error_buf,
  2157. uint32 error_buf_size)
  2158. {
  2159. WASMModuleCommon *module_reg;
  2160. WASMTag *tag = NULL;
  2161. WASMExport *export = NULL;
  2162. WASMModule *module = NULL;
  2163. module_reg = wasm_runtime_find_module_registered(module_name);
  2164. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2165. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2166. tag_name);
  2167. set_error_buf(error_buf, error_buf_size, "unknown import");
  2168. return NULL;
  2169. }
  2170. module = (WASMModule *)module_reg;
  2171. export =
  2172. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2173. error_buf, error_buf_size);
  2174. if (!export) {
  2175. return NULL;
  2176. }
  2177. /* resolve tag type and tag */
  2178. if (export->index < module->import_tag_count) {
  2179. /* importing an imported tag from the submodule */
  2180. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2181. }
  2182. else {
  2183. /* importing an section tag from the submodule */
  2184. tag = module->tags[export->index - module->import_tag_count];
  2185. }
  2186. /* check function type */
  2187. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2188. module->type_count)) {
  2189. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2190. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2191. return NULL;
  2192. }
  2193. if (linked_tag_index != NULL) {
  2194. *linked_tag_index = export->index;
  2195. }
  2196. return tag;
  2197. }
  2198. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2199. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2200. static bool
  2201. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2202. const WASMModule *parent_module,
  2203. const char *sub_module_name, const char *function_name,
  2204. WASMFunctionImport *function, bool no_resolve,
  2205. char *error_buf, uint32 error_buf_size)
  2206. {
  2207. const uint8 *p = *p_buf, *p_end = buf_end;
  2208. uint32 declare_type_index = 0;
  2209. read_leb_uint32(p, p_end, declare_type_index);
  2210. *p_buf = p;
  2211. if (declare_type_index >= parent_module->type_count) {
  2212. set_error_buf(error_buf, error_buf_size, "unknown type");
  2213. return false;
  2214. }
  2215. #if WASM_ENABLE_GC != 0
  2216. function->type_idx = declare_type_index;
  2217. #endif
  2218. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2219. declare_type_index = wasm_get_smallest_type_idx(
  2220. parent_module->types, parent_module->type_count, declare_type_index);
  2221. #endif
  2222. function->func_type =
  2223. (WASMFuncType *)parent_module->types[declare_type_index];
  2224. function->module_name = (char *)sub_module_name;
  2225. function->field_name = (char *)function_name;
  2226. function->attachment = NULL;
  2227. function->signature = NULL;
  2228. function->call_conv_raw = false;
  2229. /* lookup registered native symbols first */
  2230. if (!no_resolve) {
  2231. wasm_resolve_import_func(parent_module, function);
  2232. }
  2233. return true;
  2234. fail:
  2235. return false;
  2236. }
  2237. static bool
  2238. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2239. uint32 error_buf_size)
  2240. {
  2241. if (max_size < init_size) {
  2242. set_error_buf(error_buf, error_buf_size,
  2243. "size minimum must not be greater than maximum");
  2244. return false;
  2245. }
  2246. return true;
  2247. }
  2248. static bool
  2249. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2250. WASMModule *parent_module, const char *sub_module_name,
  2251. const char *table_name, WASMTableImport *table,
  2252. char *error_buf, uint32 error_buf_size)
  2253. {
  2254. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2255. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2256. declare_max_size = 0;
  2257. #if WASM_ENABLE_MULTI_MODULE != 0
  2258. WASMModule *sub_module = NULL;
  2259. WASMTable *linked_table = NULL;
  2260. #endif
  2261. #if WASM_ENABLE_GC != 0
  2262. WASMRefType ref_type;
  2263. bool need_ref_type_map;
  2264. #endif
  2265. bool is_table64 = false;
  2266. #if WASM_ENABLE_GC == 0
  2267. CHECK_BUF(p, p_end, 1);
  2268. /* 0x70 or 0x6F */
  2269. declare_elem_type = read_uint8(p);
  2270. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2271. #if WASM_ENABLE_REF_TYPES != 0
  2272. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2273. #endif
  2274. ) {
  2275. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2276. return false;
  2277. }
  2278. #else /* else of WASM_ENABLE_GC == 0 */
  2279. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2280. &need_ref_type_map, &ref_type, false, error_buf,
  2281. error_buf_size)) {
  2282. return false;
  2283. }
  2284. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2285. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2286. return false;
  2287. }
  2288. declare_elem_type = ref_type.ref_type;
  2289. if (need_ref_type_map) {
  2290. if (!(table->table_type.elem_ref_type =
  2291. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2292. error_buf, error_buf_size))) {
  2293. return false;
  2294. }
  2295. }
  2296. #if TRACE_WASM_LOADER != 0
  2297. os_printf("import table type: ");
  2298. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2299. os_printf("\n");
  2300. #endif
  2301. #endif /* end of WASM_ENABLE_GC == 0 */
  2302. p_org = p;
  2303. read_leb_uint32(p, p_end, table_flag);
  2304. is_table64 = table_flag & TABLE64_FLAG;
  2305. if (p - p_org > 1) {
  2306. LOG_VERBOSE("integer representation too long(import table)");
  2307. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2308. return false;
  2309. }
  2310. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2311. return false;
  2312. }
  2313. read_leb_uint32(p, p_end, declare_init_size);
  2314. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2315. read_leb_uint32(p, p_end, declare_max_size);
  2316. if (!check_table_max_size(declare_init_size, declare_max_size,
  2317. error_buf, error_buf_size))
  2318. return false;
  2319. }
  2320. adjust_table_max_size(is_table64, declare_init_size,
  2321. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2322. *p_buf = p;
  2323. #if WASM_ENABLE_MULTI_MODULE != 0
  2324. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2325. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2326. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2327. error_buf_size);
  2328. if (sub_module) {
  2329. linked_table = wasm_loader_resolve_table(
  2330. sub_module_name, table_name, declare_init_size,
  2331. declare_max_size, error_buf, error_buf_size);
  2332. if (linked_table) {
  2333. /* reset with linked table limit */
  2334. declare_elem_type = linked_table->table_type.elem_type;
  2335. declare_init_size = linked_table->table_type.init_size;
  2336. declare_max_size = linked_table->table_type.max_size;
  2337. table_flag = linked_table->table_type.flags;
  2338. table->import_table_linked = linked_table;
  2339. table->import_module = sub_module;
  2340. }
  2341. }
  2342. }
  2343. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2344. /* (table (export "table") 10 20 funcref) */
  2345. /* (table (export "table64") 10 20 funcref) */
  2346. /* we need this section working in wamrc */
  2347. if (!strcmp("spectest", sub_module_name)) {
  2348. const uint32 spectest_table_init_size = 10;
  2349. const uint32 spectest_table_max_size = 20;
  2350. if (strcmp("table", table_name)
  2351. #if WASM_ENABLE_MEMORY64 != 0
  2352. && strcmp("table64", table_name)
  2353. #endif
  2354. ) {
  2355. set_error_buf(error_buf, error_buf_size,
  2356. "incompatible import type or unknown import");
  2357. return false;
  2358. }
  2359. if (declare_init_size > spectest_table_init_size
  2360. || declare_max_size < spectest_table_max_size) {
  2361. set_error_buf(error_buf, error_buf_size,
  2362. "incompatible import type");
  2363. return false;
  2364. }
  2365. declare_init_size = spectest_table_init_size;
  2366. declare_max_size = spectest_table_max_size;
  2367. }
  2368. /* now we believe all declaration are ok */
  2369. table->table_type.elem_type = declare_elem_type;
  2370. table->table_type.init_size = declare_init_size;
  2371. table->table_type.flags = table_flag;
  2372. table->table_type.max_size = declare_max_size;
  2373. #if WASM_ENABLE_WAMR_COMPILER != 0
  2374. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2375. parent_module->is_ref_types_used = true;
  2376. #endif
  2377. (void)parent_module;
  2378. return true;
  2379. fail:
  2380. return false;
  2381. }
  2382. static bool
  2383. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2384. uint32 error_buf_size)
  2385. {
  2386. uint32 default_max_size =
  2387. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2388. if (!is_memory64 && init_size > default_max_size) {
  2389. set_error_buf(error_buf, error_buf_size,
  2390. "memory size must be at most 65536 pages (4GiB)");
  2391. return false;
  2392. }
  2393. #if WASM_ENABLE_MEMORY64 != 0
  2394. else if (is_memory64 && init_size > default_max_size) {
  2395. set_error_buf(
  2396. error_buf, error_buf_size,
  2397. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2398. return false;
  2399. }
  2400. #endif
  2401. return true;
  2402. }
  2403. static bool
  2404. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2405. char *error_buf, uint32 error_buf_size)
  2406. {
  2407. uint32 default_max_size =
  2408. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2409. if (max_size < init_size) {
  2410. set_error_buf(error_buf, error_buf_size,
  2411. "size minimum must not be greater than maximum");
  2412. return false;
  2413. }
  2414. if (!is_memory64 && max_size > default_max_size) {
  2415. set_error_buf(error_buf, error_buf_size,
  2416. "memory size must be at most 65536 pages (4GiB)");
  2417. return false;
  2418. }
  2419. #if WASM_ENABLE_MEMORY64 != 0
  2420. else if (is_memory64 && max_size > default_max_size) {
  2421. set_error_buf(
  2422. error_buf, error_buf_size,
  2423. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2424. return false;
  2425. }
  2426. #endif
  2427. return true;
  2428. }
  2429. static bool
  2430. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2431. WASMModule *parent_module, const char *sub_module_name,
  2432. const char *memory_name, WASMMemoryImport *memory,
  2433. char *error_buf, uint32 error_buf_size)
  2434. {
  2435. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2436. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2437. uint32 pool_size = wasm_runtime_memory_pool_size();
  2438. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2439. / DEFAULT_NUM_BYTES_PER_PAGE;
  2440. #else
  2441. uint32 max_page_count;
  2442. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2443. uint32 mem_flag = 0;
  2444. bool is_memory64 = false;
  2445. uint32 declare_init_page_count = 0;
  2446. uint32 declare_max_page_count = 0;
  2447. #if WASM_ENABLE_MULTI_MODULE != 0
  2448. WASMModule *sub_module = NULL;
  2449. WASMMemory *linked_memory = NULL;
  2450. #endif
  2451. p_org = p;
  2452. read_leb_uint32(p, p_end, mem_flag);
  2453. is_memory64 = mem_flag & MEMORY64_FLAG;
  2454. if (p - p_org > 1) {
  2455. LOG_VERBOSE("integer representation too long(import memory)");
  2456. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2457. return false;
  2458. }
  2459. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2460. return false;
  2461. }
  2462. read_leb_uint32(p, p_end, declare_init_page_count);
  2463. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2464. error_buf_size)) {
  2465. return false;
  2466. }
  2467. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2468. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2469. #endif
  2470. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2471. read_leb_uint32(p, p_end, declare_max_page_count);
  2472. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2473. declare_max_page_count, error_buf,
  2474. error_buf_size)) {
  2475. return false;
  2476. }
  2477. if (declare_max_page_count > max_page_count) {
  2478. declare_max_page_count = max_page_count;
  2479. }
  2480. }
  2481. else {
  2482. /* Limit the maximum memory size to max_page_count */
  2483. declare_max_page_count = max_page_count;
  2484. }
  2485. #if WASM_ENABLE_MULTI_MODULE != 0
  2486. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2487. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2488. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2489. error_buf_size);
  2490. if (sub_module) {
  2491. linked_memory = wasm_loader_resolve_memory(
  2492. sub_module_name, memory_name, declare_init_page_count,
  2493. declare_max_page_count, error_buf, error_buf_size);
  2494. if (linked_memory) {
  2495. /**
  2496. * reset with linked memory limit
  2497. */
  2498. memory->import_module = sub_module;
  2499. memory->import_memory_linked = linked_memory;
  2500. declare_init_page_count = linked_memory->init_page_count;
  2501. declare_max_page_count = linked_memory->max_page_count;
  2502. }
  2503. }
  2504. }
  2505. #endif
  2506. /* (memory (export "memory") 1 2) */
  2507. if (!strcmp("spectest", sub_module_name)) {
  2508. uint32 spectest_memory_init_page = 1;
  2509. uint32 spectest_memory_max_page = 2;
  2510. if (strcmp("memory", memory_name)) {
  2511. set_error_buf(error_buf, error_buf_size,
  2512. "incompatible import type or unknown import");
  2513. return false;
  2514. }
  2515. if (declare_init_page_count > spectest_memory_init_page
  2516. || declare_max_page_count < spectest_memory_max_page) {
  2517. set_error_buf(error_buf, error_buf_size,
  2518. "incompatible import type");
  2519. return false;
  2520. }
  2521. declare_init_page_count = spectest_memory_init_page;
  2522. declare_max_page_count = spectest_memory_max_page;
  2523. }
  2524. #if WASM_ENABLE_WASI_TEST != 0
  2525. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2526. else if (!strcmp("foo", sub_module_name)) {
  2527. uint32 spectest_memory_init_page = 1;
  2528. uint32 spectest_memory_max_page = 1;
  2529. if (strcmp("bar", memory_name)) {
  2530. set_error_buf(error_buf, error_buf_size,
  2531. "incompatible import type or unknown import");
  2532. return false;
  2533. }
  2534. if (declare_init_page_count > spectest_memory_init_page
  2535. || declare_max_page_count < spectest_memory_max_page) {
  2536. set_error_buf(error_buf, error_buf_size,
  2537. "incompatible import type");
  2538. return false;
  2539. }
  2540. declare_init_page_count = spectest_memory_init_page;
  2541. declare_max_page_count = spectest_memory_max_page;
  2542. }
  2543. #endif
  2544. /* now we believe all declaration are ok */
  2545. memory->mem_type.flags = mem_flag;
  2546. memory->mem_type.init_page_count = declare_init_page_count;
  2547. memory->mem_type.max_page_count = declare_max_page_count;
  2548. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2549. *p_buf = p;
  2550. (void)parent_module;
  2551. return true;
  2552. fail:
  2553. return false;
  2554. }
  2555. #if WASM_ENABLE_TAGS != 0
  2556. static bool
  2557. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2558. const WASMModule *parent_module, /* this module ! */
  2559. const char *sub_module_name, const char *tag_name,
  2560. WASMTagImport *tag, /* structure to fill */
  2561. char *error_buf, uint32 error_buf_size)
  2562. {
  2563. /* attribute and type of the import statement */
  2564. uint8 declare_tag_attribute;
  2565. uint32 declare_type_index;
  2566. const uint8 *p = *p_buf, *p_end = buf_end;
  2567. #if WASM_ENABLE_MULTI_MODULE != 0
  2568. WASMModule *sub_module = NULL;
  2569. #endif
  2570. /* get the one byte attribute */
  2571. CHECK_BUF(p, p_end, 1);
  2572. declare_tag_attribute = read_uint8(p);
  2573. if (declare_tag_attribute != 0) {
  2574. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2575. goto fail;
  2576. }
  2577. /* get type */
  2578. read_leb_uint32(p, p_end, declare_type_index);
  2579. /* compare against module->types */
  2580. if (declare_type_index >= parent_module->type_count) {
  2581. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2582. goto fail;
  2583. }
  2584. WASMFuncType *declare_tag_type =
  2585. (WASMFuncType *)parent_module->types[declare_type_index];
  2586. /* check, that the type of the declared tag returns void */
  2587. if (declare_tag_type->result_count != 0) {
  2588. set_error_buf(error_buf, error_buf_size,
  2589. "tag type signature does not return void");
  2590. goto fail;
  2591. }
  2592. #if WASM_ENABLE_MULTI_MODULE != 0
  2593. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2594. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2595. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2596. error_buf_size);
  2597. if (sub_module) {
  2598. /* wasm_loader_resolve_tag checks, that the imported tag
  2599. * and the declared tag have the same type
  2600. */
  2601. uint32 linked_tag_index = 0;
  2602. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2603. sub_module_name, tag_name, declare_tag_type,
  2604. &linked_tag_index /* out */, error_buf, error_buf_size);
  2605. if (linked_tag) {
  2606. tag->import_module = sub_module;
  2607. tag->import_tag_linked = linked_tag;
  2608. tag->import_tag_index_linked = linked_tag_index;
  2609. }
  2610. }
  2611. }
  2612. #endif
  2613. /* store to module tag declarations */
  2614. tag->attribute = declare_tag_attribute;
  2615. tag->type = declare_type_index;
  2616. tag->module_name = (char *)sub_module_name;
  2617. tag->field_name = (char *)tag_name;
  2618. tag->tag_type = declare_tag_type;
  2619. *p_buf = p;
  2620. (void)parent_module;
  2621. LOG_VERBOSE("Load tag import success\n");
  2622. return true;
  2623. fail:
  2624. return false;
  2625. }
  2626. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2627. static bool
  2628. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2629. WASMModule *parent_module, char *sub_module_name,
  2630. char *global_name, WASMGlobalImport *global, char *error_buf,
  2631. uint32 error_buf_size)
  2632. {
  2633. const uint8 *p = *p_buf, *p_end = buf_end;
  2634. uint8 declare_type = 0;
  2635. uint8 declare_mutable = 0;
  2636. #if WASM_ENABLE_MULTI_MODULE != 0
  2637. WASMModule *sub_module = NULL;
  2638. WASMGlobal *linked_global = NULL;
  2639. #endif
  2640. #if WASM_ENABLE_GC != 0
  2641. WASMRefType ref_type;
  2642. bool need_ref_type_map;
  2643. #endif
  2644. bool ret = false;
  2645. #if WASM_ENABLE_GC == 0
  2646. CHECK_BUF(p, p_end, 2);
  2647. /* global type */
  2648. declare_type = read_uint8(p);
  2649. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2650. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2651. return false;
  2652. }
  2653. declare_mutable = read_uint8(p);
  2654. #else
  2655. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2656. &need_ref_type_map, &ref_type, false, error_buf,
  2657. error_buf_size)) {
  2658. return false;
  2659. }
  2660. declare_type = ref_type.ref_type;
  2661. if (need_ref_type_map) {
  2662. if (!(global->ref_type =
  2663. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2664. error_buf, error_buf_size))) {
  2665. return false;
  2666. }
  2667. }
  2668. #if TRACE_WASM_LOADER != 0
  2669. os_printf("import global type: ");
  2670. wasm_dump_value_type(declare_type, global->ref_type);
  2671. os_printf("\n");
  2672. #endif
  2673. CHECK_BUF(p, p_end, 1);
  2674. declare_mutable = read_uint8(p);
  2675. #endif /* end of WASM_ENABLE_GC == 0 */
  2676. *p_buf = p;
  2677. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2678. return false;
  2679. }
  2680. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2681. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2682. global);
  2683. if (ret) {
  2684. if (global->type.val_type != declare_type
  2685. || global->type.is_mutable != declare_mutable) {
  2686. set_error_buf(error_buf, error_buf_size,
  2687. "incompatible import type");
  2688. return false;
  2689. }
  2690. global->is_linked = true;
  2691. }
  2692. #endif
  2693. #if WASM_ENABLE_MULTI_MODULE != 0
  2694. if (!global->is_linked
  2695. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2696. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2697. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2698. error_buf_size);
  2699. if (sub_module) {
  2700. /* check sub modules */
  2701. linked_global = wasm_loader_resolve_global(
  2702. sub_module_name, global_name, declare_type, declare_mutable,
  2703. error_buf, error_buf_size);
  2704. if (linked_global) {
  2705. global->import_module = sub_module;
  2706. global->import_global_linked = linked_global;
  2707. global->is_linked = true;
  2708. }
  2709. }
  2710. }
  2711. #endif
  2712. global->module_name = sub_module_name;
  2713. global->field_name = global_name;
  2714. global->type.val_type = declare_type;
  2715. global->type.is_mutable = (declare_mutable == 1);
  2716. #if WASM_ENABLE_WAMR_COMPILER != 0
  2717. if (global->type.val_type == VALUE_TYPE_V128)
  2718. parent_module->is_simd_used = true;
  2719. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2720. parent_module->is_ref_types_used = true;
  2721. #endif
  2722. (void)parent_module;
  2723. (void)ret;
  2724. return true;
  2725. fail:
  2726. return false;
  2727. }
  2728. static bool
  2729. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2730. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2731. {
  2732. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2733. #if WASM_ENABLE_GC != 0
  2734. WASMRefType ref_type;
  2735. bool need_ref_type_map;
  2736. #endif
  2737. bool is_table64 = false;
  2738. #if WASM_ENABLE_GC == 0
  2739. CHECK_BUF(p, p_end, 1);
  2740. /* 0x70 or 0x6F */
  2741. table->table_type.elem_type = read_uint8(p);
  2742. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2743. #if WASM_ENABLE_REF_TYPES != 0
  2744. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2745. #endif
  2746. ) {
  2747. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2748. return false;
  2749. }
  2750. #else /* else of WASM_ENABLE_GC == 0 */
  2751. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2752. &need_ref_type_map, &ref_type, false, error_buf,
  2753. error_buf_size)) {
  2754. return false;
  2755. }
  2756. table->table_type.elem_type = ref_type.ref_type;
  2757. if (need_ref_type_map) {
  2758. if (!(table->table_type.elem_ref_type =
  2759. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2760. error_buf_size))) {
  2761. return false;
  2762. }
  2763. }
  2764. #if TRACE_WASM_LOADER != 0
  2765. os_printf("table type: ");
  2766. wasm_dump_value_type(table->table_type.elem_type,
  2767. table->table_type.elem_ref_type);
  2768. os_printf("\n");
  2769. #endif
  2770. #endif /* end of WASM_ENABLE_GC == 0 */
  2771. p_org = p;
  2772. read_leb_uint32(p, p_end, table->table_type.flags);
  2773. is_table64 = table->table_type.flags & TABLE64_FLAG;
  2774. if (p - p_org > 1) {
  2775. LOG_VERBOSE("integer representation too long(table)");
  2776. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2777. return false;
  2778. }
  2779. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  2780. error_buf_size, false)) {
  2781. return false;
  2782. }
  2783. read_leb_uint32(p, p_end, table->table_type.init_size);
  2784. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  2785. read_leb_uint32(p, p_end, table->table_type.max_size);
  2786. if (!check_table_max_size(table->table_type.init_size,
  2787. table->table_type.max_size, error_buf,
  2788. error_buf_size))
  2789. return false;
  2790. }
  2791. adjust_table_max_size(is_table64, table->table_type.init_size,
  2792. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  2793. &table->table_type.max_size);
  2794. #if WASM_ENABLE_WAMR_COMPILER != 0
  2795. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2796. module->is_ref_types_used = true;
  2797. #endif
  2798. *p_buf = p;
  2799. return true;
  2800. fail:
  2801. return false;
  2802. }
  2803. static bool
  2804. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2805. char *error_buf, uint32 error_buf_size)
  2806. {
  2807. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2808. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2809. uint32 pool_size = wasm_runtime_memory_pool_size();
  2810. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2811. / DEFAULT_NUM_BYTES_PER_PAGE;
  2812. #else
  2813. uint32 max_page_count;
  2814. #endif
  2815. bool is_memory64 = false;
  2816. p_org = p;
  2817. read_leb_uint32(p, p_end, memory->flags);
  2818. is_memory64 = memory->flags & MEMORY64_FLAG;
  2819. if (p - p_org > 1) {
  2820. LOG_VERBOSE("integer representation too long(memory)");
  2821. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2822. return false;
  2823. }
  2824. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2825. false)) {
  2826. return false;
  2827. }
  2828. read_leb_uint32(p, p_end, memory->init_page_count);
  2829. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2830. error_buf_size))
  2831. return false;
  2832. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2833. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2834. #endif
  2835. if (memory->flags & 1) {
  2836. read_leb_uint32(p, p_end, memory->max_page_count);
  2837. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2838. memory->max_page_count, error_buf,
  2839. error_buf_size))
  2840. return false;
  2841. if (memory->max_page_count > max_page_count)
  2842. memory->max_page_count = max_page_count;
  2843. }
  2844. else {
  2845. /* Limit the maximum memory size to max_page_count */
  2846. memory->max_page_count = max_page_count;
  2847. }
  2848. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2849. *p_buf = p;
  2850. return true;
  2851. fail:
  2852. return false;
  2853. }
  2854. static int
  2855. cmp_export_name(const void *a, const void *b)
  2856. {
  2857. return strcmp(*(char **)a, *(char **)b);
  2858. }
  2859. static bool
  2860. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2861. bool is_load_from_file_buf, bool no_resolve,
  2862. char *error_buf, uint32 error_buf_size)
  2863. {
  2864. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2865. uint32 import_count, name_len, type_index, i, u32, flags;
  2866. uint64 total_size;
  2867. WASMImport *import;
  2868. WASMImport *import_functions = NULL, *import_tables = NULL;
  2869. WASMImport *import_memories = NULL, *import_globals = NULL;
  2870. #if WASM_ENABLE_TAGS != 0
  2871. WASMImport *import_tags = NULL;
  2872. #endif
  2873. char *sub_module_name, *field_name;
  2874. uint8 u8, kind, global_type;
  2875. read_leb_uint32(p, p_end, import_count);
  2876. if (import_count) {
  2877. module->import_count = import_count;
  2878. total_size = sizeof(WASMImport) * (uint64)import_count;
  2879. if (!(module->imports =
  2880. loader_malloc(total_size, error_buf, error_buf_size))) {
  2881. return false;
  2882. }
  2883. p_old = p;
  2884. /* Scan firstly to get import count of each type */
  2885. for (i = 0; i < import_count; i++) {
  2886. /* module name */
  2887. read_leb_uint32(p, p_end, name_len);
  2888. CHECK_BUF(p, p_end, name_len);
  2889. p += name_len;
  2890. /* field name */
  2891. read_leb_uint32(p, p_end, name_len);
  2892. CHECK_BUF(p, p_end, name_len);
  2893. p += name_len;
  2894. CHECK_BUF(p, p_end, 1);
  2895. /* 0x00/0x01/0x02/0x03/0x04 */
  2896. kind = read_uint8(p);
  2897. switch (kind) {
  2898. case IMPORT_KIND_FUNC: /* import function */
  2899. read_leb_uint32(p, p_end, type_index);
  2900. module->import_function_count++;
  2901. break;
  2902. case IMPORT_KIND_TABLE: /* import table */
  2903. CHECK_BUF(p, p_end, 1);
  2904. /* 0x70 */
  2905. u8 = read_uint8(p);
  2906. read_leb_uint32(p, p_end, flags);
  2907. read_leb_uint32(p, p_end, u32);
  2908. if (flags & 1)
  2909. read_leb_uint32(p, p_end, u32);
  2910. module->import_table_count++;
  2911. if (module->import_table_count > 1) {
  2912. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2913. set_error_buf(error_buf, error_buf_size,
  2914. "multiple tables");
  2915. return false;
  2916. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2917. module->is_ref_types_used = true;
  2918. #endif
  2919. }
  2920. break;
  2921. case IMPORT_KIND_MEMORY: /* import memory */
  2922. read_leb_uint32(p, p_end, flags);
  2923. read_leb_uint32(p, p_end, u32);
  2924. if (flags & 1)
  2925. read_leb_uint32(p, p_end, u32);
  2926. module->import_memory_count++;
  2927. #if WASM_ENABLE_MULTI_MEMORY == 0
  2928. if (module->import_memory_count > 1) {
  2929. set_error_buf(error_buf, error_buf_size,
  2930. "multiple memories");
  2931. return false;
  2932. }
  2933. #endif
  2934. break;
  2935. #if WASM_ENABLE_TAGS != 0
  2936. case IMPORT_KIND_TAG: /* import tags */
  2937. /* it only counts the number of tags to import */
  2938. module->import_tag_count++;
  2939. CHECK_BUF(p, p_end, 1);
  2940. u8 = read_uint8(p);
  2941. read_leb_uint32(p, p_end, type_index);
  2942. break;
  2943. #endif
  2944. case IMPORT_KIND_GLOBAL: /* import global */
  2945. #if WASM_ENABLE_GC != 0
  2946. /* valtype */
  2947. CHECK_BUF(p, p_end, 1);
  2948. global_type = read_uint8(p);
  2949. if (wasm_is_type_multi_byte_type(global_type)) {
  2950. int32 heap_type;
  2951. read_leb_int32(p, p_end, heap_type);
  2952. (void)heap_type;
  2953. }
  2954. /* mutability */
  2955. CHECK_BUF(p, p_end, 1);
  2956. p += 1;
  2957. #else
  2958. CHECK_BUF(p, p_end, 2);
  2959. p += 2;
  2960. #endif
  2961. (void)global_type;
  2962. module->import_global_count++;
  2963. break;
  2964. default:
  2965. set_error_buf(error_buf, error_buf_size,
  2966. "invalid import kind");
  2967. return false;
  2968. }
  2969. }
  2970. if (module->import_function_count)
  2971. import_functions = module->import_functions = module->imports;
  2972. if (module->import_table_count)
  2973. import_tables = module->import_tables =
  2974. module->imports + module->import_function_count;
  2975. if (module->import_memory_count)
  2976. import_memories = module->import_memories =
  2977. module->imports + module->import_function_count
  2978. + module->import_table_count;
  2979. #if WASM_ENABLE_TAGS != 0
  2980. if (module->import_tag_count)
  2981. import_tags = module->import_tags =
  2982. module->imports + module->import_function_count
  2983. + module->import_table_count + module->import_memory_count;
  2984. if (module->import_global_count)
  2985. import_globals = module->import_globals =
  2986. module->imports + module->import_function_count
  2987. + module->import_table_count + module->import_memory_count
  2988. + module->import_tag_count;
  2989. #else
  2990. if (module->import_global_count)
  2991. import_globals = module->import_globals =
  2992. module->imports + module->import_function_count
  2993. + module->import_table_count + module->import_memory_count;
  2994. #endif
  2995. p = p_old;
  2996. /* Scan again to resolve the data */
  2997. for (i = 0; i < import_count; i++) {
  2998. /* load module name */
  2999. read_leb_uint32(p, p_end, name_len);
  3000. CHECK_BUF(p, p_end, name_len);
  3001. if (!(sub_module_name = wasm_const_str_list_insert(
  3002. p, name_len, module, is_load_from_file_buf, error_buf,
  3003. error_buf_size))) {
  3004. return false;
  3005. }
  3006. p += name_len;
  3007. /* load field name */
  3008. read_leb_uint32(p, p_end, name_len);
  3009. CHECK_BUF(p, p_end, name_len);
  3010. if (!(field_name = wasm_const_str_list_insert(
  3011. p, name_len, module, is_load_from_file_buf, error_buf,
  3012. error_buf_size))) {
  3013. return false;
  3014. }
  3015. p += name_len;
  3016. CHECK_BUF(p, p_end, 1);
  3017. /* 0x00/0x01/0x02/0x03/0x4 */
  3018. kind = read_uint8(p);
  3019. switch (kind) {
  3020. case IMPORT_KIND_FUNC: /* import function */
  3021. bh_assert(import_functions);
  3022. import = import_functions++;
  3023. if (!load_function_import(&p, p_end, module,
  3024. sub_module_name, field_name,
  3025. &import->u.function, no_resolve,
  3026. error_buf, error_buf_size)) {
  3027. return false;
  3028. }
  3029. break;
  3030. case IMPORT_KIND_TABLE: /* import table */
  3031. bh_assert(import_tables);
  3032. import = import_tables++;
  3033. if (!load_table_import(&p, p_end, module, sub_module_name,
  3034. field_name, &import->u.table,
  3035. error_buf, error_buf_size)) {
  3036. LOG_DEBUG("can not import such a table (%s,%s)",
  3037. sub_module_name, field_name);
  3038. return false;
  3039. }
  3040. break;
  3041. case IMPORT_KIND_MEMORY: /* import memory */
  3042. bh_assert(import_memories);
  3043. import = import_memories++;
  3044. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3045. field_name, &import->u.memory,
  3046. error_buf, error_buf_size)) {
  3047. return false;
  3048. }
  3049. break;
  3050. #if WASM_ENABLE_TAGS != 0
  3051. case IMPORT_KIND_TAG:
  3052. bh_assert(import_tags);
  3053. import = import_tags++;
  3054. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3055. field_name, &import->u.tag, error_buf,
  3056. error_buf_size)) {
  3057. return false;
  3058. }
  3059. break;
  3060. #endif
  3061. case IMPORT_KIND_GLOBAL: /* import global */
  3062. bh_assert(import_globals);
  3063. import = import_globals++;
  3064. if (!load_global_import(&p, p_end, module, sub_module_name,
  3065. field_name, &import->u.global,
  3066. error_buf, error_buf_size)) {
  3067. return false;
  3068. }
  3069. break;
  3070. default:
  3071. set_error_buf(error_buf, error_buf_size,
  3072. "invalid import kind");
  3073. return false;
  3074. }
  3075. import->kind = kind;
  3076. import->u.names.module_name = sub_module_name;
  3077. import->u.names.field_name = field_name;
  3078. }
  3079. #if WASM_ENABLE_LIBC_WASI != 0
  3080. import = module->import_functions;
  3081. for (i = 0; i < module->import_function_count; i++, import++) {
  3082. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3083. || !strcmp(import->u.names.module_name,
  3084. "wasi_snapshot_preview1")) {
  3085. module->import_wasi_api = true;
  3086. break;
  3087. }
  3088. }
  3089. #endif
  3090. }
  3091. if (p != p_end) {
  3092. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3093. return false;
  3094. }
  3095. LOG_VERBOSE("Load import section success.\n");
  3096. (void)u8;
  3097. (void)u32;
  3098. (void)type_index;
  3099. return true;
  3100. fail:
  3101. return false;
  3102. }
  3103. static bool
  3104. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3105. uint32 error_buf_size)
  3106. {
  3107. WASMFuncType *param_type = func->func_type;
  3108. uint32 param_count = param_type->param_count;
  3109. uint8 *param_types = param_type->types;
  3110. uint32 local_count = func->local_count;
  3111. uint8 *local_types = func->local_types;
  3112. uint32 i, local_offset = 0;
  3113. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3114. /*
  3115. * Only allocate memory when total_size is not 0,
  3116. * or the return value of malloc(0) might be NULL on some platforms,
  3117. * which causes wasm loader return false.
  3118. */
  3119. if (total_size > 0
  3120. && !(func->local_offsets =
  3121. loader_malloc(total_size, error_buf, error_buf_size))) {
  3122. return false;
  3123. }
  3124. for (i = 0; i < param_count; i++) {
  3125. func->local_offsets[i] = (uint16)local_offset;
  3126. local_offset += wasm_value_type_cell_num(param_types[i]);
  3127. }
  3128. for (i = 0; i < local_count; i++) {
  3129. func->local_offsets[param_count + i] = (uint16)local_offset;
  3130. local_offset += wasm_value_type_cell_num(local_types[i]);
  3131. }
  3132. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3133. return true;
  3134. }
  3135. static bool
  3136. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3137. const uint8 *buf_code, const uint8 *buf_code_end,
  3138. WASMModule *module, char *error_buf,
  3139. uint32 error_buf_size)
  3140. {
  3141. const uint8 *p = buf, *p_end = buf_end;
  3142. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3143. uint32 func_count;
  3144. uint64 total_size;
  3145. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3146. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3147. uint8 type;
  3148. WASMFunction *func;
  3149. #if WASM_ENABLE_GC != 0
  3150. bool need_ref_type_map;
  3151. WASMRefType ref_type;
  3152. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3153. #endif
  3154. read_leb_uint32(p, p_end, func_count);
  3155. if (buf_code)
  3156. read_leb_uint32(p_code, buf_code_end, code_count);
  3157. if (func_count != code_count) {
  3158. set_error_buf(error_buf, error_buf_size,
  3159. "function and code section have inconsistent lengths or "
  3160. "unexpected end");
  3161. return false;
  3162. }
  3163. if (is_indices_overflow(module->import_function_count, func_count,
  3164. error_buf, error_buf_size))
  3165. return false;
  3166. if (func_count) {
  3167. module->function_count = func_count;
  3168. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3169. if (!(module->functions =
  3170. loader_malloc(total_size, error_buf, error_buf_size))) {
  3171. return false;
  3172. }
  3173. for (i = 0; i < func_count; i++) {
  3174. /* Resolve function type */
  3175. read_leb_uint32(p, p_end, type_index);
  3176. if (type_index >= module->type_count) {
  3177. set_error_buf(error_buf, error_buf_size, "unknown type");
  3178. return false;
  3179. }
  3180. #if WASM_ENABLE_GC != 0
  3181. type_index_org = type_index;
  3182. #endif
  3183. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3184. && WASM_ENABLE_GC == 0
  3185. type_index = wasm_get_smallest_type_idx(
  3186. module->types, module->type_count, type_index);
  3187. #endif
  3188. read_leb_uint32(p_code, buf_code_end, code_size);
  3189. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3190. set_error_buf(error_buf, error_buf_size,
  3191. "invalid function code size");
  3192. return false;
  3193. }
  3194. /* Resolve local set count */
  3195. p_code_end = p_code + code_size;
  3196. local_count = 0;
  3197. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3198. p_code_save = p_code;
  3199. #if WASM_ENABLE_GC != 0
  3200. ref_type_map_count = 0;
  3201. #endif
  3202. /* Calculate total local count */
  3203. for (j = 0; j < local_set_count; j++) {
  3204. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3205. if (sub_local_count > UINT32_MAX - local_count) {
  3206. set_error_buf(error_buf, error_buf_size, "too many locals");
  3207. return false;
  3208. }
  3209. #if WASM_ENABLE_GC == 0
  3210. CHECK_BUF(p_code, buf_code_end, 1);
  3211. /* 0x7F/0x7E/0x7D/0x7C */
  3212. type = read_uint8(p_code);
  3213. local_count += sub_local_count;
  3214. #if WASM_ENABLE_WAMR_COMPILER != 0
  3215. /* If any value's type is v128, mark the module as SIMD used */
  3216. if (type == VALUE_TYPE_V128)
  3217. module->is_simd_used = true;
  3218. #endif
  3219. #else
  3220. if (!resolve_value_type(&p_code, buf_code_end, module,
  3221. module->type_count, &need_ref_type_map,
  3222. &ref_type, false, error_buf,
  3223. error_buf_size)) {
  3224. return false;
  3225. }
  3226. local_count += sub_local_count;
  3227. if (need_ref_type_map)
  3228. ref_type_map_count += sub_local_count;
  3229. #endif
  3230. }
  3231. /* Code size in code entry can't be smaller than size of vec(locals)
  3232. * + expr(at least 1 for opcode end). And expressions are encoded by
  3233. * their instruction sequence terminated with an explicit 0x0B
  3234. * opcode for end. */
  3235. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3236. set_error_buf(
  3237. error_buf, error_buf_size,
  3238. "section size mismatch: function body END opcode expected");
  3239. return false;
  3240. }
  3241. /* Alloc memory, layout: function structure + local types */
  3242. code_size = (uint32)(p_code_end - p_code);
  3243. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3244. if (!(func = module->functions[i] =
  3245. loader_malloc(total_size, error_buf, error_buf_size))) {
  3246. return false;
  3247. }
  3248. #if WASM_ENABLE_GC != 0
  3249. if (ref_type_map_count > 0) {
  3250. total_size =
  3251. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3252. if (!(func->local_ref_type_maps = loader_malloc(
  3253. total_size, error_buf, error_buf_size))) {
  3254. return false;
  3255. }
  3256. func->local_ref_type_map_count = ref_type_map_count;
  3257. }
  3258. #endif
  3259. /* Set function type, local count, code size and code body */
  3260. func->func_type = (WASMFuncType *)module->types[type_index];
  3261. func->local_count = local_count;
  3262. if (local_count > 0)
  3263. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3264. func->code_size = code_size;
  3265. /*
  3266. * we shall make a copy of code body [p_code, p_code + code_size]
  3267. * when we are worrying about inappropriate releasing behaviour.
  3268. * all code bodies are actually in a buffer which user allocates in
  3269. * his embedding environment and we don't have power on them.
  3270. * it will be like:
  3271. * code_body_cp = malloc(code_size);
  3272. * memcpy(code_body_cp, p_code, code_size);
  3273. * func->code = code_body_cp;
  3274. */
  3275. func->code = (uint8 *)p_code;
  3276. #if WASM_ENABLE_GC != 0
  3277. func->type_idx = type_index_org;
  3278. #endif
  3279. #if WASM_ENABLE_GC != 0
  3280. t = 0;
  3281. #endif
  3282. /* Load each local type */
  3283. p_code = p_code_save;
  3284. local_type_index = 0;
  3285. for (j = 0; j < local_set_count; j++) {
  3286. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3287. /* Note: sub_local_count is allowed to be 0 */
  3288. if (local_type_index > UINT32_MAX - sub_local_count
  3289. || local_type_index + sub_local_count > local_count) {
  3290. set_error_buf(error_buf, error_buf_size,
  3291. "invalid local count");
  3292. return false;
  3293. }
  3294. #if WASM_ENABLE_GC == 0
  3295. CHECK_BUF(p_code, buf_code_end, 1);
  3296. /* 0x7F/0x7E/0x7D/0x7C */
  3297. type = read_uint8(p_code);
  3298. if (!is_valid_value_type_for_interpreter(type)) {
  3299. if (type == VALUE_TYPE_V128)
  3300. set_error_buf(error_buf, error_buf_size,
  3301. "v128 value type requires simd feature");
  3302. else if (type == VALUE_TYPE_FUNCREF
  3303. || type == VALUE_TYPE_EXTERNREF)
  3304. set_error_buf(error_buf, error_buf_size,
  3305. "ref value type requires "
  3306. "reference types feature");
  3307. else
  3308. set_error_buf_v(error_buf, error_buf_size,
  3309. "invalid local type 0x%02X", type);
  3310. return false;
  3311. }
  3312. #else
  3313. if (!resolve_value_type(&p_code, buf_code_end, module,
  3314. module->type_count, &need_ref_type_map,
  3315. &ref_type, false, error_buf,
  3316. error_buf_size)) {
  3317. return false;
  3318. }
  3319. if (need_ref_type_map) {
  3320. WASMRefType *ref_type_tmp;
  3321. if (!(ref_type_tmp = reftype_set_insert(
  3322. module->ref_type_set, &ref_type, error_buf,
  3323. error_buf_size))) {
  3324. return false;
  3325. }
  3326. for (k = 0; k < sub_local_count; k++) {
  3327. func->local_ref_type_maps[t + k].ref_type =
  3328. ref_type_tmp;
  3329. func->local_ref_type_maps[t + k].index =
  3330. local_type_index + k;
  3331. }
  3332. t += sub_local_count;
  3333. }
  3334. type = ref_type.ref_type;
  3335. #endif
  3336. for (k = 0; k < sub_local_count; k++) {
  3337. func->local_types[local_type_index++] = type;
  3338. }
  3339. #if WASM_ENABLE_WAMR_COMPILER != 0
  3340. if (type == VALUE_TYPE_V128)
  3341. module->is_simd_used = true;
  3342. else if (type == VALUE_TYPE_FUNCREF
  3343. || type == VALUE_TYPE_EXTERNREF)
  3344. module->is_ref_types_used = true;
  3345. #endif
  3346. }
  3347. bh_assert(local_type_index == func->local_count);
  3348. #if WASM_ENABLE_GC != 0
  3349. bh_assert(t == func->local_ref_type_map_count);
  3350. #if TRACE_WASM_LOADER != 0
  3351. os_printf("func %u, local types: [", i);
  3352. k = 0;
  3353. for (j = 0; j < func->local_count; j++) {
  3354. WASMRefType *ref_type_tmp = NULL;
  3355. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3356. bh_assert(j == func->local_ref_type_maps[k].index);
  3357. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3358. }
  3359. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3360. if (j < func->local_count - 1)
  3361. os_printf(" ");
  3362. }
  3363. os_printf("]\n");
  3364. #endif
  3365. #endif
  3366. func->param_cell_num = func->func_type->param_cell_num;
  3367. func->ret_cell_num = func->func_type->ret_cell_num;
  3368. local_cell_num =
  3369. wasm_get_cell_num(func->local_types, func->local_count);
  3370. if (local_cell_num > UINT16_MAX) {
  3371. set_error_buf(error_buf, error_buf_size,
  3372. "local count too large");
  3373. return false;
  3374. }
  3375. func->local_cell_num = (uint16)local_cell_num;
  3376. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3377. return false;
  3378. p_code = p_code_end;
  3379. }
  3380. }
  3381. if (p != p_end) {
  3382. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3383. return false;
  3384. }
  3385. LOG_VERBOSE("Load function section success.\n");
  3386. return true;
  3387. fail:
  3388. return false;
  3389. }
  3390. static bool
  3391. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3392. char *error_buf, uint32 error_buf_size)
  3393. {
  3394. const uint8 *p = buf, *p_end = buf_end;
  3395. uint32 table_count, i;
  3396. uint64 total_size;
  3397. WASMTable *table;
  3398. read_leb_uint32(p, p_end, table_count);
  3399. if (module->import_table_count + table_count > 1) {
  3400. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3401. /* a total of one table is allowed */
  3402. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3403. return false;
  3404. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3405. module->is_ref_types_used = true;
  3406. #endif
  3407. }
  3408. if (table_count) {
  3409. module->table_count = table_count;
  3410. total_size = sizeof(WASMTable) * (uint64)table_count;
  3411. if (!(module->tables =
  3412. loader_malloc(total_size, error_buf, error_buf_size))) {
  3413. return false;
  3414. }
  3415. /* load each table */
  3416. table = module->tables;
  3417. for (i = 0; i < table_count; i++, table++) {
  3418. #if WASM_ENABLE_GC != 0
  3419. uint8 flag;
  3420. bool has_init = false;
  3421. CHECK_BUF(buf, buf_end, 1);
  3422. flag = read_uint8(p);
  3423. if (flag == TABLE_INIT_EXPR_FLAG) {
  3424. CHECK_BUF(buf, buf_end, 1);
  3425. flag = read_uint8(p);
  3426. if (flag != 0x00) {
  3427. set_error_buf(error_buf, error_buf_size,
  3428. "invalid leading bytes for table");
  3429. return false;
  3430. }
  3431. has_init = true;
  3432. }
  3433. else {
  3434. p--;
  3435. }
  3436. #endif /* end of WASM_ENABLE_GC != 0 */
  3437. if (!load_table(&p, p_end, module, table, error_buf,
  3438. error_buf_size))
  3439. return false;
  3440. #if WASM_ENABLE_GC != 0
  3441. if (has_init) {
  3442. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3443. table->table_type.elem_type,
  3444. table->table_type.elem_ref_type, error_buf,
  3445. error_buf_size))
  3446. return false;
  3447. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3448. && table->init_expr.init_expr_type
  3449. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3450. set_error_buf(
  3451. error_buf, error_buf_size,
  3452. "unsupported initializer expression for table");
  3453. return false;
  3454. }
  3455. }
  3456. else {
  3457. if (wasm_is_reftype_htref_non_nullable(
  3458. table->table_type.elem_type)) {
  3459. set_error_buf(
  3460. error_buf, error_buf_size,
  3461. "type mismatch: non-nullable table without init expr");
  3462. return false;
  3463. }
  3464. }
  3465. #endif /* end of WASM_ENABLE_GC != 0 */
  3466. #if WASM_ENABLE_WAMR_COMPILER != 0
  3467. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3468. module->is_ref_types_used = true;
  3469. #endif
  3470. }
  3471. }
  3472. if (p != p_end) {
  3473. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3474. return false;
  3475. }
  3476. LOG_VERBOSE("Load table section success.\n");
  3477. return true;
  3478. fail:
  3479. return false;
  3480. }
  3481. static bool
  3482. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3483. char *error_buf, uint32 error_buf_size)
  3484. {
  3485. const uint8 *p = buf, *p_end = buf_end;
  3486. uint32 memory_count, i;
  3487. uint64 total_size;
  3488. WASMMemory *memory;
  3489. read_leb_uint32(p, p_end, memory_count);
  3490. #if WASM_ENABLE_MULTI_MEMORY == 0
  3491. /* a total of one memory is allowed */
  3492. if (module->import_memory_count + memory_count > 1) {
  3493. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3494. return false;
  3495. }
  3496. #endif
  3497. if (memory_count) {
  3498. module->memory_count = memory_count;
  3499. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3500. if (!(module->memories =
  3501. loader_malloc(total_size, error_buf, error_buf_size))) {
  3502. return false;
  3503. }
  3504. /* load each memory */
  3505. memory = module->memories;
  3506. for (i = 0; i < memory_count; i++, memory++)
  3507. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3508. return false;
  3509. }
  3510. if (p != p_end) {
  3511. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3512. return false;
  3513. }
  3514. LOG_VERBOSE("Load memory section success.\n");
  3515. return true;
  3516. fail:
  3517. return false;
  3518. }
  3519. static bool
  3520. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3521. char *error_buf, uint32 error_buf_size)
  3522. {
  3523. const uint8 *p = buf, *p_end = buf_end;
  3524. uint32 global_count, i;
  3525. uint64 total_size;
  3526. WASMGlobal *global;
  3527. uint8 mutable;
  3528. #if WASM_ENABLE_GC != 0
  3529. bool need_ref_type_map;
  3530. WASMRefType ref_type;
  3531. #endif
  3532. read_leb_uint32(p, p_end, global_count);
  3533. if (is_indices_overflow(module->import_global_count, global_count,
  3534. error_buf, error_buf_size))
  3535. return false;
  3536. module->global_count = 0;
  3537. if (global_count) {
  3538. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3539. if (!(module->globals =
  3540. loader_malloc(total_size, error_buf, error_buf_size))) {
  3541. return false;
  3542. }
  3543. global = module->globals;
  3544. for (i = 0; i < global_count; i++, global++) {
  3545. #if WASM_ENABLE_GC == 0
  3546. CHECK_BUF(p, p_end, 2);
  3547. /* global type */
  3548. global->type.val_type = read_uint8(p);
  3549. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3550. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3551. return false;
  3552. }
  3553. mutable = read_uint8(p);
  3554. #else
  3555. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3556. &need_ref_type_map, &ref_type, false,
  3557. error_buf, error_buf_size)) {
  3558. return false;
  3559. }
  3560. global->type.val_type = ref_type.ref_type;
  3561. CHECK_BUF(p, p_end, 1);
  3562. mutable = read_uint8(p);
  3563. #endif /* end of WASM_ENABLE_GC */
  3564. #if WASM_ENABLE_WAMR_COMPILER != 0
  3565. if (global->type.val_type == VALUE_TYPE_V128)
  3566. module->is_simd_used = true;
  3567. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3568. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3569. module->is_ref_types_used = true;
  3570. #endif
  3571. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3572. return false;
  3573. }
  3574. global->type.is_mutable = mutable ? true : false;
  3575. /* initialize expression */
  3576. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3577. global->type.val_type,
  3578. #if WASM_ENABLE_GC == 0
  3579. NULL,
  3580. #else
  3581. &ref_type,
  3582. #endif
  3583. error_buf, error_buf_size))
  3584. return false;
  3585. #if WASM_ENABLE_GC != 0
  3586. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3587. uint8 global_type;
  3588. WASMRefType *global_ref_type;
  3589. uint32 global_idx = global->init_expr.u.global_index;
  3590. if (global->init_expr.u.global_index
  3591. >= module->import_global_count + i) {
  3592. set_error_buf(error_buf, error_buf_size, "unknown global");
  3593. return false;
  3594. }
  3595. if (global_idx < module->import_global_count) {
  3596. global_type = module->import_globals[global_idx]
  3597. .u.global.type.val_type;
  3598. global_ref_type =
  3599. module->import_globals[global_idx].u.global.ref_type;
  3600. }
  3601. else {
  3602. global_type =
  3603. module
  3604. ->globals[global_idx - module->import_global_count]
  3605. .type.val_type;
  3606. global_ref_type =
  3607. module
  3608. ->globals[global_idx - module->import_global_count]
  3609. .ref_type;
  3610. }
  3611. if (!wasm_reftype_is_subtype_of(
  3612. global_type, global_ref_type, global->type.val_type,
  3613. global->ref_type, module->types, module->type_count)) {
  3614. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3615. return false;
  3616. }
  3617. }
  3618. if (need_ref_type_map) {
  3619. if (!(global->ref_type =
  3620. reftype_set_insert(module->ref_type_set, &ref_type,
  3621. error_buf, error_buf_size))) {
  3622. return false;
  3623. }
  3624. }
  3625. #if TRACE_WASM_LOADER != 0
  3626. os_printf("global type: ");
  3627. wasm_dump_value_type(global->type, global->ref_type);
  3628. os_printf("\n");
  3629. #endif
  3630. #endif
  3631. module->global_count++;
  3632. }
  3633. bh_assert(module->global_count == global_count);
  3634. }
  3635. if (p != p_end) {
  3636. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3637. return false;
  3638. }
  3639. LOG_VERBOSE("Load global section success.\n");
  3640. return true;
  3641. fail:
  3642. return false;
  3643. }
  3644. static bool
  3645. check_duplicate_exports(WASMModule *module, char *error_buf,
  3646. uint32 error_buf_size)
  3647. {
  3648. uint32 i;
  3649. bool result = false;
  3650. char *names_buf[32], **names = names_buf;
  3651. if (module->export_count > 32) {
  3652. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3653. error_buf_size);
  3654. if (!names) {
  3655. return result;
  3656. }
  3657. }
  3658. for (i = 0; i < module->export_count; i++) {
  3659. names[i] = module->exports[i].name;
  3660. }
  3661. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3662. for (i = 1; i < module->export_count; i++) {
  3663. if (!strcmp(names[i], names[i - 1])) {
  3664. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3665. goto cleanup;
  3666. }
  3667. }
  3668. result = true;
  3669. cleanup:
  3670. if (module->export_count > 32) {
  3671. wasm_runtime_free(names);
  3672. }
  3673. return result;
  3674. }
  3675. static bool
  3676. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3677. bool is_load_from_file_buf, char *error_buf,
  3678. uint32 error_buf_size)
  3679. {
  3680. const uint8 *p = buf, *p_end = buf_end;
  3681. uint32 export_count, i, index;
  3682. uint64 total_size;
  3683. uint32 str_len;
  3684. WASMExport *export;
  3685. read_leb_uint32(p, p_end, export_count);
  3686. if (export_count) {
  3687. module->export_count = export_count;
  3688. total_size = sizeof(WASMExport) * (uint64)export_count;
  3689. if (!(module->exports =
  3690. loader_malloc(total_size, error_buf, error_buf_size))) {
  3691. return false;
  3692. }
  3693. export = module->exports;
  3694. for (i = 0; i < export_count; i++, export ++) {
  3695. #if WASM_ENABLE_THREAD_MGR == 0
  3696. if (p == p_end) {
  3697. /* export section with inconsistent count:
  3698. n export declared, but less than n given */
  3699. set_error_buf(error_buf, error_buf_size,
  3700. "length out of bounds");
  3701. return false;
  3702. }
  3703. #endif
  3704. read_leb_uint32(p, p_end, str_len);
  3705. CHECK_BUF(p, p_end, str_len);
  3706. if (!(export->name = wasm_const_str_list_insert(
  3707. p, str_len, module, is_load_from_file_buf, error_buf,
  3708. error_buf_size))) {
  3709. return false;
  3710. }
  3711. p += str_len;
  3712. CHECK_BUF(p, p_end, 1);
  3713. export->kind = read_uint8(p);
  3714. read_leb_uint32(p, p_end, index);
  3715. export->index = index;
  3716. switch (export->kind) {
  3717. /* function index */
  3718. case EXPORT_KIND_FUNC:
  3719. if (index >= module->function_count
  3720. + module->import_function_count) {
  3721. set_error_buf(error_buf, error_buf_size,
  3722. "unknown function");
  3723. return false;
  3724. }
  3725. #if WASM_ENABLE_SIMD != 0
  3726. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  3727. || (WASM_ENABLE_FAST_INTERP != 0)
  3728. /* TODO: check func type, if it has v128 param or result,
  3729. report error */
  3730. #endif
  3731. #endif
  3732. break;
  3733. /* table index */
  3734. case EXPORT_KIND_TABLE:
  3735. if (index
  3736. >= module->table_count + module->import_table_count) {
  3737. set_error_buf(error_buf, error_buf_size,
  3738. "unknown table");
  3739. return false;
  3740. }
  3741. break;
  3742. /* memory index */
  3743. case EXPORT_KIND_MEMORY:
  3744. if (index
  3745. >= module->memory_count + module->import_memory_count) {
  3746. set_error_buf(error_buf, error_buf_size,
  3747. "unknown memory");
  3748. return false;
  3749. }
  3750. break;
  3751. #if WASM_ENABLE_TAGS != 0
  3752. /* export tag */
  3753. case EXPORT_KIND_TAG:
  3754. if (index >= module->tag_count + module->import_tag_count) {
  3755. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3756. return false;
  3757. }
  3758. break;
  3759. #endif
  3760. /* global index */
  3761. case EXPORT_KIND_GLOBAL:
  3762. if (index
  3763. >= module->global_count + module->import_global_count) {
  3764. set_error_buf(error_buf, error_buf_size,
  3765. "unknown global");
  3766. return false;
  3767. }
  3768. break;
  3769. default:
  3770. set_error_buf(error_buf, error_buf_size,
  3771. "invalid export kind");
  3772. return false;
  3773. }
  3774. }
  3775. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  3776. return false;
  3777. }
  3778. }
  3779. if (p != p_end) {
  3780. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3781. return false;
  3782. }
  3783. LOG_VERBOSE("Load export section success.\n");
  3784. return true;
  3785. fail:
  3786. return false;
  3787. }
  3788. static bool
  3789. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3790. uint32 error_buf_size)
  3791. {
  3792. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3793. if (table_index != 0) {
  3794. set_error_buf(
  3795. error_buf, error_buf_size,
  3796. "zero byte expected. The module uses reference types feature "
  3797. "which is disabled in the runtime.");
  3798. return false;
  3799. }
  3800. #endif
  3801. if (table_index >= module->import_table_count + module->table_count) {
  3802. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3803. table_index);
  3804. return false;
  3805. }
  3806. return true;
  3807. }
  3808. static bool
  3809. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3810. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3811. {
  3812. const uint8 *p = *p_buf, *p_end = buf_end;
  3813. uint32 table_index;
  3814. read_leb_uint32(p, p_end, table_index);
  3815. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3816. return false;
  3817. }
  3818. *p_table_index = table_index;
  3819. *p_buf = p;
  3820. return true;
  3821. fail:
  3822. return false;
  3823. }
  3824. /* Element segments must match element type of table */
  3825. static bool
  3826. check_table_elem_type(WASMModule *module, uint32 table_index,
  3827. uint32 type_from_elem_seg, char *error_buf,
  3828. uint32 error_buf_size)
  3829. {
  3830. uint32 table_declared_elem_type;
  3831. if (table_index < module->import_table_count)
  3832. table_declared_elem_type =
  3833. module->import_tables[table_index].u.table.table_type.elem_type;
  3834. else
  3835. table_declared_elem_type =
  3836. module->tables[table_index - module->import_table_count]
  3837. .table_type.elem_type;
  3838. if (table_declared_elem_type == type_from_elem_seg)
  3839. return true;
  3840. #if WASM_ENABLE_GC != 0
  3841. /*
  3842. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3843. */
  3844. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3845. return true;
  3846. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3847. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3848. return true;
  3849. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3850. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3851. return true;
  3852. #endif
  3853. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3854. return false;
  3855. }
  3856. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3857. static bool
  3858. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3859. uint32 *p_elem_type,
  3860. #if WASM_ENABLE_GC != 0
  3861. WASMRefType **p_elem_ref_type,
  3862. #endif
  3863. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3864. {
  3865. const uint8 *p = *p_buf, *p_end = buf_end;
  3866. uint8 elem_type;
  3867. #if WASM_ENABLE_GC != 0
  3868. WASMRefType elem_ref_type;
  3869. bool need_ref_type_map;
  3870. #endif
  3871. CHECK_BUF(p, p_end, 1);
  3872. elem_type = read_uint8(p);
  3873. if (elemkind_zero) {
  3874. if (elem_type != 0) {
  3875. set_error_buf(error_buf, error_buf_size,
  3876. "invalid reference type or unknown type");
  3877. return false;
  3878. }
  3879. else {
  3880. *p_elem_type = VALUE_TYPE_FUNCREF;
  3881. *p_buf = p;
  3882. return true;
  3883. }
  3884. }
  3885. #if WASM_ENABLE_GC == 0
  3886. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3887. set_error_buf(error_buf, error_buf_size,
  3888. "invalid reference type or unknown type");
  3889. return false;
  3890. }
  3891. *p_elem_type = elem_type;
  3892. #else
  3893. p--;
  3894. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3895. module->type_count, &need_ref_type_map,
  3896. &elem_ref_type, false, error_buf, error_buf_size)) {
  3897. return false;
  3898. }
  3899. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3900. set_error_buf(error_buf, error_buf_size,
  3901. "invalid reference type or unknown type");
  3902. return false;
  3903. }
  3904. *p_elem_type = elem_ref_type.ref_type;
  3905. if (need_ref_type_map) {
  3906. if (!(*p_elem_ref_type =
  3907. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3908. error_buf, error_buf_size))) {
  3909. return false;
  3910. }
  3911. }
  3912. #endif
  3913. *p_buf = p;
  3914. return true;
  3915. fail:
  3916. return false;
  3917. }
  3918. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3919. static bool
  3920. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3921. WASMModule *module, WASMTableSeg *table_segment,
  3922. char *error_buf, uint32 error_buf_size)
  3923. {
  3924. const uint8 *p = *p_buf, *p_end = buf_end;
  3925. uint32 function_count, function_index = 0, i;
  3926. uint64 total_size;
  3927. read_leb_uint32(p, p_end, function_count);
  3928. table_segment->value_count = function_count;
  3929. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3930. if (total_size > 0
  3931. && !(table_segment->init_values =
  3932. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3933. error_buf_size))) {
  3934. return false;
  3935. }
  3936. for (i = 0; i < function_count; i++) {
  3937. InitializerExpression *init_expr = &table_segment->init_values[i];
  3938. read_leb_uint32(p, p_end, function_index);
  3939. if (!check_function_index(module, function_index, error_buf,
  3940. error_buf_size)) {
  3941. return false;
  3942. }
  3943. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3944. init_expr->u.ref_index = function_index;
  3945. }
  3946. *p_buf = p;
  3947. return true;
  3948. fail:
  3949. return false;
  3950. }
  3951. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3952. static bool
  3953. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3954. WASMModule *module, WASMTableSeg *table_segment,
  3955. char *error_buf, uint32 error_buf_size)
  3956. {
  3957. const uint8 *p = *p_buf, *p_end = buf_end;
  3958. uint32 ref_count, i;
  3959. uint64 total_size;
  3960. read_leb_uint32(p, p_end, ref_count);
  3961. table_segment->value_count = ref_count;
  3962. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3963. if (total_size > 0
  3964. && !(table_segment->init_values =
  3965. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3966. error_buf_size))) {
  3967. return false;
  3968. }
  3969. for (i = 0; i < ref_count; i++) {
  3970. InitializerExpression *init_expr = &table_segment->init_values[i];
  3971. if (!load_init_expr(module, &p, p_end, init_expr,
  3972. table_segment->elem_type,
  3973. #if WASM_ENABLE_GC == 0
  3974. NULL,
  3975. #else
  3976. table_segment->elem_ref_type,
  3977. #endif
  3978. error_buf, error_buf_size))
  3979. return false;
  3980. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3981. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3982. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3983. && init_expr->init_expr_type
  3984. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3985. }
  3986. *p_buf = p;
  3987. return true;
  3988. fail:
  3989. return false;
  3990. }
  3991. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3992. static bool
  3993. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3994. WASMModule *module, char *error_buf,
  3995. uint32 error_buf_size)
  3996. {
  3997. const uint8 *p = buf, *p_end = buf_end;
  3998. uint8 table_elem_idx_type;
  3999. uint32 table_segment_count, i;
  4000. uint64 total_size;
  4001. WASMTableSeg *table_segment;
  4002. read_leb_uint32(p, p_end, table_segment_count);
  4003. if (table_segment_count) {
  4004. module->table_seg_count = table_segment_count;
  4005. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4006. if (!(module->table_segments =
  4007. loader_malloc(total_size, error_buf, error_buf_size))) {
  4008. return false;
  4009. }
  4010. table_segment = module->table_segments;
  4011. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4012. if (p >= p_end) {
  4013. set_error_buf(error_buf, error_buf_size,
  4014. "invalid value type or "
  4015. "invalid elements segment kind");
  4016. return false;
  4017. }
  4018. table_elem_idx_type = VALUE_TYPE_I32;
  4019. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4020. read_leb_uint32(p, p_end, table_segment->mode);
  4021. /* last three bits */
  4022. table_segment->mode = table_segment->mode & 0x07;
  4023. switch (table_segment->mode) {
  4024. /* elemkind/elemtype + active */
  4025. case 0:
  4026. case 4:
  4027. {
  4028. #if WASM_ENABLE_GC != 0
  4029. if (table_segment->mode == 0) {
  4030. /* vec(funcidx), set elem type to (ref func) */
  4031. WASMRefType elem_ref_type = { 0 };
  4032. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4033. wasm_set_refheaptype_common(
  4034. &elem_ref_type.ref_ht_common, false,
  4035. HEAP_TYPE_FUNC);
  4036. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4037. module->ref_type_set, &elem_ref_type,
  4038. error_buf, error_buf_size)))
  4039. return false;
  4040. }
  4041. else {
  4042. /* vec(expr), set elem type to funcref */
  4043. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4044. }
  4045. #else
  4046. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4047. #endif
  4048. table_segment->table_index = 0;
  4049. if (!check_table_index(module, table_segment->table_index,
  4050. error_buf, error_buf_size))
  4051. return false;
  4052. #if WASM_ENABLE_MEMORY64 != 0
  4053. table_elem_idx_type =
  4054. is_table_64bit(module, table_segment->table_index)
  4055. ? VALUE_TYPE_I64
  4056. : VALUE_TYPE_I32;
  4057. #endif
  4058. if (!load_init_expr(module, &p, p_end,
  4059. &table_segment->base_offset,
  4060. table_elem_idx_type, NULL, error_buf,
  4061. error_buf_size))
  4062. return false;
  4063. if (table_segment->mode == 0) {
  4064. /* vec(funcidx) */
  4065. if (!load_func_index_vec(&p, p_end, module,
  4066. table_segment, error_buf,
  4067. error_buf_size))
  4068. return false;
  4069. }
  4070. else {
  4071. /* vec(expr) */
  4072. if (!load_init_expr_vec(&p, p_end, module,
  4073. table_segment, error_buf,
  4074. error_buf_size))
  4075. return false;
  4076. }
  4077. if (!check_table_elem_type(module,
  4078. table_segment->table_index,
  4079. table_segment->elem_type,
  4080. error_buf, error_buf_size))
  4081. return false;
  4082. break;
  4083. }
  4084. /* elemkind + passive/declarative */
  4085. case 1:
  4086. case 3:
  4087. if (!load_elem_type(module, &p, p_end,
  4088. &table_segment->elem_type,
  4089. #if WASM_ENABLE_GC != 0
  4090. &table_segment->elem_ref_type,
  4091. #endif
  4092. true, error_buf, error_buf_size))
  4093. return false;
  4094. /* vec(funcidx) */
  4095. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4096. error_buf, error_buf_size))
  4097. return false;
  4098. break;
  4099. /* elemkind/elemtype + table_idx + active */
  4100. case 2:
  4101. case 6:
  4102. if (!load_table_index(&p, p_end, module,
  4103. &table_segment->table_index,
  4104. error_buf, error_buf_size))
  4105. return false;
  4106. #if WASM_ENABLE_MEMORY64 != 0
  4107. table_elem_idx_type =
  4108. is_table_64bit(module, table_segment->table_index)
  4109. ? VALUE_TYPE_I64
  4110. : VALUE_TYPE_I32;
  4111. #endif
  4112. if (!load_init_expr(module, &p, p_end,
  4113. &table_segment->base_offset,
  4114. table_elem_idx_type, NULL, error_buf,
  4115. error_buf_size))
  4116. return false;
  4117. if (!load_elem_type(module, &p, p_end,
  4118. &table_segment->elem_type,
  4119. #if WASM_ENABLE_GC != 0
  4120. &table_segment->elem_ref_type,
  4121. #endif
  4122. table_segment->mode == 2 ? true : false,
  4123. error_buf, error_buf_size))
  4124. return false;
  4125. if (table_segment->mode == 2) {
  4126. /* vec(funcidx) */
  4127. if (!load_func_index_vec(&p, p_end, module,
  4128. table_segment, error_buf,
  4129. error_buf_size))
  4130. return false;
  4131. }
  4132. else {
  4133. /* vec(expr) */
  4134. if (!load_init_expr_vec(&p, p_end, module,
  4135. table_segment, error_buf,
  4136. error_buf_size))
  4137. return false;
  4138. }
  4139. if (!check_table_elem_type(module,
  4140. table_segment->table_index,
  4141. table_segment->elem_type,
  4142. error_buf, error_buf_size))
  4143. return false;
  4144. break;
  4145. case 5:
  4146. case 7:
  4147. if (!load_elem_type(module, &p, p_end,
  4148. &table_segment->elem_type,
  4149. #if WASM_ENABLE_GC != 0
  4150. &table_segment->elem_ref_type,
  4151. #endif
  4152. false, error_buf, error_buf_size))
  4153. return false;
  4154. /* vec(expr) */
  4155. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4156. error_buf, error_buf_size))
  4157. return false;
  4158. break;
  4159. default:
  4160. set_error_buf(error_buf, error_buf_size,
  4161. "unknown element segment kind");
  4162. return false;
  4163. }
  4164. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4165. /*
  4166. * like: 00 41 05 0b 04 00 01 00 01
  4167. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4168. */
  4169. if (!load_table_index(&p, p_end, module,
  4170. &table_segment->table_index, error_buf,
  4171. error_buf_size))
  4172. return false;
  4173. #if WASM_ENABLE_MEMORY64 != 0
  4174. table_elem_idx_type =
  4175. is_table_64bit(module, table_segment->table_index)
  4176. ? VALUE_TYPE_I64
  4177. : VALUE_TYPE_I32;
  4178. #endif
  4179. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4180. table_elem_idx_type, NULL, error_buf,
  4181. error_buf_size))
  4182. return false;
  4183. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4184. error_buf, error_buf_size))
  4185. return false;
  4186. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4187. if (!check_table_elem_type(module, table_segment->table_index,
  4188. table_segment->elem_type, error_buf,
  4189. error_buf_size))
  4190. return false;
  4191. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4192. #if WASM_ENABLE_MEMORY64 != 0
  4193. if (table_elem_idx_type == VALUE_TYPE_I64
  4194. && table_segment->base_offset.u.u64 > UINT32_MAX) {
  4195. set_error_buf(error_buf, error_buf_size,
  4196. "In table64, table base offset can't be "
  4197. "larger than UINT32_MAX");
  4198. return false;
  4199. }
  4200. #endif
  4201. #if WASM_ENABLE_WAMR_COMPILER != 0
  4202. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4203. module->is_ref_types_used = true;
  4204. #endif
  4205. }
  4206. }
  4207. if (p != p_end) {
  4208. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4209. return false;
  4210. }
  4211. LOG_VERBOSE("Load table segment section success.\n");
  4212. return true;
  4213. fail:
  4214. return false;
  4215. }
  4216. #if WASM_ENABLE_BULK_MEMORY != 0
  4217. static bool
  4218. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4219. int data_seg_len, char *error_buf,
  4220. uint32 error_buf_size)
  4221. {
  4222. if (has_datacount_section && datacount_len != data_seg_len) {
  4223. set_error_buf(error_buf, error_buf_size,
  4224. "data count and data section have inconsistent lengths");
  4225. return false;
  4226. }
  4227. return true;
  4228. }
  4229. #endif
  4230. static bool
  4231. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4232. WASMModule *module,
  4233. #if WASM_ENABLE_BULK_MEMORY != 0
  4234. bool has_datacount_section,
  4235. #endif
  4236. bool clone_data_seg, char *error_buf,
  4237. uint32 error_buf_size)
  4238. {
  4239. const uint8 *p = buf, *p_end = buf_end;
  4240. uint32 data_seg_count, i, mem_index, data_seg_len;
  4241. uint64 total_size;
  4242. WASMDataSeg *dataseg;
  4243. InitializerExpression init_expr;
  4244. #if WASM_ENABLE_BULK_MEMORY != 0
  4245. bool is_passive = false;
  4246. uint32 mem_flag;
  4247. #endif
  4248. uint8 mem_offset_type = VALUE_TYPE_I32;
  4249. read_leb_uint32(p, p_end, data_seg_count);
  4250. #if WASM_ENABLE_BULK_MEMORY != 0
  4251. if (!check_data_count_consistency(has_datacount_section,
  4252. module->data_seg_count1, data_seg_count,
  4253. error_buf, error_buf_size)) {
  4254. return false;
  4255. }
  4256. #endif
  4257. if (data_seg_count) {
  4258. module->data_seg_count = data_seg_count;
  4259. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4260. if (!(module->data_segments =
  4261. loader_malloc(total_size, error_buf, error_buf_size))) {
  4262. return false;
  4263. }
  4264. for (i = 0; i < data_seg_count; i++) {
  4265. read_leb_uint32(p, p_end, mem_index);
  4266. #if WASM_ENABLE_BULK_MEMORY != 0
  4267. is_passive = false;
  4268. mem_flag = mem_index & 0x03;
  4269. switch (mem_flag) {
  4270. case 0x01:
  4271. is_passive = true;
  4272. #if WASM_ENABLE_WAMR_COMPILER != 0
  4273. module->is_bulk_memory_used = true;
  4274. #endif
  4275. break;
  4276. case 0x00:
  4277. /* no memory index, treat index as 0 */
  4278. mem_index = 0;
  4279. goto check_mem_index;
  4280. case 0x02:
  4281. /* read following memory index */
  4282. read_leb_uint32(p, p_end, mem_index);
  4283. #if WASM_ENABLE_WAMR_COMPILER != 0
  4284. module->is_bulk_memory_used = true;
  4285. #endif
  4286. check_mem_index:
  4287. if (mem_index
  4288. >= module->import_memory_count + module->memory_count) {
  4289. set_error_buf_v(error_buf, error_buf_size,
  4290. "unknown memory %d", mem_index);
  4291. return false;
  4292. }
  4293. break;
  4294. case 0x03:
  4295. default:
  4296. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4297. return false;
  4298. break;
  4299. }
  4300. #else
  4301. if (mem_index
  4302. >= module->import_memory_count + module->memory_count) {
  4303. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4304. mem_index);
  4305. return false;
  4306. }
  4307. #endif /* WASM_ENABLE_BULK_MEMORY */
  4308. #if WASM_ENABLE_BULK_MEMORY != 0
  4309. if (!is_passive)
  4310. #endif
  4311. {
  4312. #if WASM_ENABLE_MEMORY64 != 0
  4313. /* This memory_flag is from memory instead of data segment */
  4314. uint8 memory_flag;
  4315. if (module->import_memory_count > 0) {
  4316. memory_flag = module->import_memories[mem_index]
  4317. .u.memory.mem_type.flags;
  4318. }
  4319. else {
  4320. memory_flag =
  4321. module
  4322. ->memories[mem_index - module->import_memory_count]
  4323. .flags;
  4324. }
  4325. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4326. : VALUE_TYPE_I32;
  4327. #else
  4328. mem_offset_type = VALUE_TYPE_I32;
  4329. #endif
  4330. }
  4331. #if WASM_ENABLE_BULK_MEMORY != 0
  4332. if (!is_passive)
  4333. #endif
  4334. if (!load_init_expr(module, &p, p_end, &init_expr,
  4335. mem_offset_type, NULL, error_buf,
  4336. error_buf_size))
  4337. return false;
  4338. read_leb_uint32(p, p_end, data_seg_len);
  4339. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4340. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4341. return false;
  4342. }
  4343. #if WASM_ENABLE_BULK_MEMORY != 0
  4344. dataseg->is_passive = is_passive;
  4345. if (!is_passive)
  4346. #endif
  4347. {
  4348. bh_memcpy_s(&dataseg->base_offset,
  4349. sizeof(InitializerExpression), &init_expr,
  4350. sizeof(InitializerExpression));
  4351. dataseg->memory_index = mem_index;
  4352. }
  4353. dataseg->data_length = data_seg_len;
  4354. CHECK_BUF(p, p_end, data_seg_len);
  4355. if (clone_data_seg) {
  4356. if (!(dataseg->data = loader_malloc(
  4357. dataseg->data_length, error_buf, error_buf_size))) {
  4358. return false;
  4359. }
  4360. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4361. data_seg_len);
  4362. }
  4363. else {
  4364. dataseg->data = (uint8 *)p;
  4365. }
  4366. dataseg->is_data_cloned = clone_data_seg;
  4367. p += data_seg_len;
  4368. }
  4369. }
  4370. if (p != p_end) {
  4371. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4372. return false;
  4373. }
  4374. LOG_VERBOSE("Load data segment section success.\n");
  4375. return true;
  4376. fail:
  4377. return false;
  4378. }
  4379. #if WASM_ENABLE_BULK_MEMORY != 0
  4380. static bool
  4381. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4382. WASMModule *module, char *error_buf,
  4383. uint32 error_buf_size)
  4384. {
  4385. const uint8 *p = buf, *p_end = buf_end;
  4386. uint32 data_seg_count1 = 0;
  4387. read_leb_uint32(p, p_end, data_seg_count1);
  4388. module->data_seg_count1 = data_seg_count1;
  4389. if (p != p_end) {
  4390. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4391. return false;
  4392. }
  4393. #if WASM_ENABLE_WAMR_COMPILER != 0
  4394. module->is_bulk_memory_used = true;
  4395. #endif
  4396. LOG_VERBOSE("Load datacount section success.\n");
  4397. return true;
  4398. fail:
  4399. return false;
  4400. }
  4401. #endif
  4402. #if WASM_ENABLE_TAGS != 0
  4403. static bool
  4404. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4405. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4406. uint32 error_buf_size)
  4407. {
  4408. (void)buf_code;
  4409. (void)buf_code_end;
  4410. const uint8 *p = buf, *p_end = buf_end;
  4411. size_t total_size = 0;
  4412. /* number of tags defined in the section */
  4413. uint32 section_tag_count = 0;
  4414. uint8 tag_attribute;
  4415. uint32 tag_type;
  4416. WASMTag *tag = NULL;
  4417. /* get tag count */
  4418. read_leb_uint32(p, p_end, section_tag_count);
  4419. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4420. error_buf, error_buf_size))
  4421. return false;
  4422. module->tag_count = section_tag_count;
  4423. if (section_tag_count) {
  4424. total_size = sizeof(WASMTag *) * module->tag_count;
  4425. if (!(module->tags =
  4426. loader_malloc(total_size, error_buf, error_buf_size))) {
  4427. return false;
  4428. }
  4429. /* load each tag, imported tags precede the tags */
  4430. uint32 tag_index;
  4431. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4432. /* get the one byte attribute */
  4433. CHECK_BUF(p, p_end, 1);
  4434. tag_attribute = read_uint8(p);
  4435. /* get type */
  4436. read_leb_uint32(p, p_end, tag_type);
  4437. /* compare against module->types */
  4438. if (tag_type >= module->type_count) {
  4439. set_error_buf(error_buf, error_buf_size, "unknown type");
  4440. return false;
  4441. }
  4442. /* get return type (must be 0) */
  4443. /* check, that the type of the referred tag returns void */
  4444. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4445. if (func_type->result_count != 0) {
  4446. set_error_buf(error_buf, error_buf_size,
  4447. "non-empty tag result type");
  4448. goto fail;
  4449. }
  4450. if (!(tag = module->tags[tag_index] = loader_malloc(
  4451. sizeof(WASMTag), error_buf, error_buf_size))) {
  4452. return false;
  4453. }
  4454. /* store to module tag declarations */
  4455. tag->attribute = tag_attribute;
  4456. tag->type = tag_type;
  4457. tag->tag_type = func_type;
  4458. }
  4459. }
  4460. if (p != p_end) {
  4461. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4462. return false;
  4463. }
  4464. LOG_VERBOSE("Load tag section success.\n");
  4465. return true;
  4466. fail:
  4467. return false;
  4468. }
  4469. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4470. static bool
  4471. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4472. const uint8 *buf_func_end, WASMModule *module,
  4473. char *error_buf, uint32 error_buf_size)
  4474. {
  4475. const uint8 *p = buf, *p_end = buf_end;
  4476. const uint8 *p_func = buf_func;
  4477. uint32 func_count = 0, code_count;
  4478. /* code has been loaded in function section, so pass it here, just check
  4479. * whether function and code section have inconsistent lengths */
  4480. read_leb_uint32(p, p_end, code_count);
  4481. if (buf_func)
  4482. read_leb_uint32(p_func, buf_func_end, func_count);
  4483. if (func_count != code_count) {
  4484. set_error_buf(error_buf, error_buf_size,
  4485. "function and code section have inconsistent lengths");
  4486. return false;
  4487. }
  4488. LOG_VERBOSE("Load code segment section success.\n");
  4489. (void)module;
  4490. return true;
  4491. fail:
  4492. return false;
  4493. }
  4494. static bool
  4495. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4496. char *error_buf, uint32 error_buf_size)
  4497. {
  4498. const uint8 *p = buf, *p_end = buf_end;
  4499. WASMFuncType *type;
  4500. uint32 start_function;
  4501. read_leb_uint32(p, p_end, start_function);
  4502. if (start_function
  4503. >= module->function_count + module->import_function_count) {
  4504. set_error_buf(error_buf, error_buf_size, "unknown function");
  4505. return false;
  4506. }
  4507. if (start_function < module->import_function_count)
  4508. type = module->import_functions[start_function].u.function.func_type;
  4509. else
  4510. type = module->functions[start_function - module->import_function_count]
  4511. ->func_type;
  4512. if (type->param_count != 0 || type->result_count != 0) {
  4513. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4514. return false;
  4515. }
  4516. module->start_function = start_function;
  4517. if (p != p_end) {
  4518. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4519. return false;
  4520. }
  4521. LOG_VERBOSE("Load start section success.\n");
  4522. return true;
  4523. fail:
  4524. return false;
  4525. }
  4526. #if WASM_ENABLE_STRINGREF != 0
  4527. static bool
  4528. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4529. WASMModule *module, bool is_load_from_file_buf,
  4530. char *error_buf, uint32 error_buf_size)
  4531. {
  4532. const uint8 *p = buf, *p_end = buf_end;
  4533. int32 deferred_count, immediate_count, string_length, i;
  4534. uint64 total_size;
  4535. read_leb_uint32(p, p_end, deferred_count);
  4536. read_leb_uint32(p, p_end, immediate_count);
  4537. /* proposal set deferred_count for future extension */
  4538. if (deferred_count != 0) {
  4539. goto fail;
  4540. }
  4541. if (immediate_count > 0) {
  4542. total_size = sizeof(char *) * (uint64)immediate_count;
  4543. if (!(module->string_literal_ptrs =
  4544. loader_malloc(total_size, error_buf, error_buf_size))) {
  4545. goto fail;
  4546. }
  4547. module->string_literal_count = immediate_count;
  4548. total_size = sizeof(uint32) * (uint64)immediate_count;
  4549. if (!(module->string_literal_lengths =
  4550. loader_malloc(total_size, error_buf, error_buf_size))) {
  4551. goto fail;
  4552. }
  4553. for (i = 0; i < immediate_count; i++) {
  4554. read_leb_uint32(p, p_end, string_length);
  4555. CHECK_BUF(p, p_end, string_length);
  4556. module->string_literal_ptrs[i] = p;
  4557. module->string_literal_lengths[i] = string_length;
  4558. p += string_length;
  4559. }
  4560. }
  4561. if (p != p_end) {
  4562. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4563. goto fail;
  4564. }
  4565. LOG_VERBOSE("Load stringref section success.\n");
  4566. return true;
  4567. fail:
  4568. return false;
  4569. }
  4570. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4571. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4572. static bool
  4573. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4574. bool is_load_from_file_buf, char *error_buf,
  4575. uint32 error_buf_size)
  4576. {
  4577. const uint8 *p = buf, *p_end = buf_end;
  4578. uint32 name_type, subsection_size;
  4579. uint32 previous_name_type = 0;
  4580. uint32 num_func_name;
  4581. uint32 func_index;
  4582. uint32 previous_func_index = ~0U;
  4583. uint32 func_name_len;
  4584. uint32 name_index;
  4585. int i = 0;
  4586. if (p >= p_end) {
  4587. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4588. return false;
  4589. }
  4590. while (p < p_end) {
  4591. read_leb_uint32(p, p_end, name_type);
  4592. if (i != 0) {
  4593. if (name_type == previous_name_type) {
  4594. set_error_buf(error_buf, error_buf_size,
  4595. "duplicate sub-section");
  4596. return false;
  4597. }
  4598. if (name_type < previous_name_type) {
  4599. set_error_buf(error_buf, error_buf_size,
  4600. "out-of-order sub-section");
  4601. return false;
  4602. }
  4603. }
  4604. previous_name_type = name_type;
  4605. read_leb_uint32(p, p_end, subsection_size);
  4606. CHECK_BUF(p, p_end, subsection_size);
  4607. switch (name_type) {
  4608. case SUB_SECTION_TYPE_FUNC:
  4609. if (subsection_size) {
  4610. read_leb_uint32(p, p_end, num_func_name);
  4611. for (name_index = 0; name_index < num_func_name;
  4612. name_index++) {
  4613. read_leb_uint32(p, p_end, func_index);
  4614. if (func_index == previous_func_index) {
  4615. set_error_buf(error_buf, error_buf_size,
  4616. "duplicate function name");
  4617. return false;
  4618. }
  4619. if (func_index < previous_func_index
  4620. && previous_func_index != ~0U) {
  4621. set_error_buf(error_buf, error_buf_size,
  4622. "out-of-order function index ");
  4623. return false;
  4624. }
  4625. previous_func_index = func_index;
  4626. read_leb_uint32(p, p_end, func_name_len);
  4627. CHECK_BUF(p, p_end, func_name_len);
  4628. /* Skip the import functions */
  4629. if (func_index >= module->import_function_count) {
  4630. func_index -= module->import_function_count;
  4631. if (func_index >= module->function_count) {
  4632. set_error_buf(error_buf, error_buf_size,
  4633. "out-of-range function index");
  4634. return false;
  4635. }
  4636. if (!(module->functions[func_index]->field_name =
  4637. wasm_const_str_list_insert(
  4638. p, func_name_len, module,
  4639. #if WASM_ENABLE_WAMR_COMPILER != 0
  4640. false,
  4641. #else
  4642. is_load_from_file_buf,
  4643. #endif
  4644. error_buf, error_buf_size))) {
  4645. return false;
  4646. }
  4647. }
  4648. p += func_name_len;
  4649. }
  4650. }
  4651. break;
  4652. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4653. */
  4654. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4655. default:
  4656. p = p + subsection_size;
  4657. break;
  4658. }
  4659. i++;
  4660. }
  4661. return true;
  4662. fail:
  4663. return false;
  4664. }
  4665. #endif
  4666. static bool
  4667. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4668. bool is_load_from_file_buf, char *error_buf,
  4669. uint32 error_buf_size)
  4670. {
  4671. const uint8 *p = buf, *p_end = buf_end;
  4672. char section_name[32];
  4673. uint32 name_len, buffer_len;
  4674. if (p >= p_end) {
  4675. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4676. return false;
  4677. }
  4678. read_leb_uint32(p, p_end, name_len);
  4679. if (p + name_len > p_end) {
  4680. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4681. return false;
  4682. }
  4683. if (!wasm_check_utf8_str(p, name_len)) {
  4684. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4685. return false;
  4686. }
  4687. buffer_len = sizeof(section_name);
  4688. memset(section_name, 0, buffer_len);
  4689. if (name_len < buffer_len) {
  4690. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4691. }
  4692. else {
  4693. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4694. memset(section_name + buffer_len - 4, '.', 3);
  4695. }
  4696. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4697. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4698. module->name_section_buf = buf;
  4699. module->name_section_buf_end = buf_end;
  4700. p += name_len;
  4701. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4702. error_buf, error_buf_size)) {
  4703. return false;
  4704. }
  4705. LOG_VERBOSE("Load custom name section success.");
  4706. }
  4707. #endif
  4708. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4709. {
  4710. WASMCustomSection *section =
  4711. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4712. if (!section) {
  4713. return false;
  4714. }
  4715. section->name_addr = (char *)p;
  4716. section->name_len = name_len;
  4717. section->content_addr = (uint8 *)(p + name_len);
  4718. section->content_len = (uint32)(p_end - p - name_len);
  4719. section->next = module->custom_section_list;
  4720. module->custom_section_list = section;
  4721. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4722. return true;
  4723. }
  4724. #endif
  4725. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4726. (void)is_load_from_file_buf;
  4727. (void)module;
  4728. return true;
  4729. fail:
  4730. return false;
  4731. }
  4732. static void
  4733. calculate_global_data_offset(WASMModule *module)
  4734. {
  4735. uint32 i, data_offset;
  4736. data_offset = 0;
  4737. for (i = 0; i < module->import_global_count; i++) {
  4738. WASMGlobalImport *import_global =
  4739. &((module->import_globals + i)->u.global);
  4740. #if WASM_ENABLE_FAST_JIT != 0
  4741. import_global->data_offset = data_offset;
  4742. #endif
  4743. data_offset += wasm_value_type_size(import_global->type.val_type);
  4744. }
  4745. for (i = 0; i < module->global_count; i++) {
  4746. WASMGlobal *global = module->globals + i;
  4747. #if WASM_ENABLE_FAST_JIT != 0
  4748. global->data_offset = data_offset;
  4749. #endif
  4750. data_offset += wasm_value_type_size(global->type.val_type);
  4751. }
  4752. module->global_data_size = data_offset;
  4753. }
  4754. #if WASM_ENABLE_FAST_JIT != 0
  4755. static bool
  4756. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4757. uint32 error_buf_size)
  4758. {
  4759. #if WASM_ENABLE_LAZY_JIT != 0
  4760. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4761. #endif
  4762. uint32 i;
  4763. if (!module->function_count)
  4764. return true;
  4765. if (!(module->fast_jit_func_ptrs =
  4766. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4767. error_buf_size))) {
  4768. return false;
  4769. }
  4770. #if WASM_ENABLE_LAZY_JIT != 0
  4771. for (i = 0; i < module->function_count; i++) {
  4772. module->fast_jit_func_ptrs[i] =
  4773. jit_globals->compile_fast_jit_and_then_call;
  4774. }
  4775. #endif
  4776. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4777. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4778. set_error_buf(error_buf, error_buf_size,
  4779. "init fast jit thread lock failed");
  4780. return false;
  4781. }
  4782. module->fast_jit_thread_locks_inited[i] = true;
  4783. }
  4784. return true;
  4785. }
  4786. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4787. #if WASM_ENABLE_JIT != 0
  4788. static bool
  4789. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4790. uint32 error_buf_size)
  4791. {
  4792. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4793. AOTCompOption option = { 0 };
  4794. char *aot_last_error;
  4795. uint64 size;
  4796. #if WASM_ENABLE_GC != 0
  4797. bool gc_enabled = true;
  4798. #else
  4799. bool gc_enabled = false;
  4800. #endif
  4801. if (module->function_count == 0)
  4802. return true;
  4803. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4804. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4805. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4806. return false;
  4807. }
  4808. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4809. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4810. os_mutex_destroy(&module->tierup_wait_lock);
  4811. return false;
  4812. }
  4813. module->tierup_wait_lock_inited = true;
  4814. #endif
  4815. size = sizeof(void *) * (uint64)module->function_count
  4816. + sizeof(bool) * (uint64)module->function_count;
  4817. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4818. return false;
  4819. }
  4820. module->func_ptrs_compiled =
  4821. (bool *)((uint8 *)module->func_ptrs
  4822. + sizeof(void *) * module->function_count);
  4823. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4824. if (!module->comp_data) {
  4825. aot_last_error = aot_get_last_error();
  4826. bh_assert(aot_last_error != NULL);
  4827. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4828. return false;
  4829. }
  4830. option.is_jit_mode = true;
  4831. option.opt_level = llvm_jit_options->opt_level;
  4832. option.size_level = llvm_jit_options->size_level;
  4833. option.segue_flags = llvm_jit_options->segue_flags;
  4834. option.quick_invoke_c_api_import =
  4835. llvm_jit_options->quick_invoke_c_api_import;
  4836. #if WASM_ENABLE_BULK_MEMORY != 0
  4837. option.enable_bulk_memory = true;
  4838. #endif
  4839. #if WASM_ENABLE_THREAD_MGR != 0
  4840. option.enable_thread_mgr = true;
  4841. #endif
  4842. #if WASM_ENABLE_TAIL_CALL != 0
  4843. option.enable_tail_call = true;
  4844. #endif
  4845. #if WASM_ENABLE_SIMD != 0
  4846. option.enable_simd = true;
  4847. #endif
  4848. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4849. option.enable_ref_types = true;
  4850. #elif WASM_ENABLE_GC != 0
  4851. option.enable_gc = true;
  4852. #endif
  4853. option.enable_aux_stack_check = true;
  4854. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4855. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4856. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4857. aot_call_stack_features_init_default(&option.call_stack_features);
  4858. #endif
  4859. #if WASM_ENABLE_PERF_PROFILING != 0
  4860. option.enable_perf_profiling = true;
  4861. #endif
  4862. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4863. option.enable_memory_profiling = true;
  4864. option.enable_stack_estimation = true;
  4865. #endif
  4866. #if WASM_ENABLE_SHARED_HEAP != 0
  4867. option.enable_shared_heap = true;
  4868. #endif
  4869. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4870. if (!module->comp_ctx) {
  4871. aot_last_error = aot_get_last_error();
  4872. bh_assert(aot_last_error != NULL);
  4873. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4874. return false;
  4875. }
  4876. return true;
  4877. }
  4878. static bool
  4879. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4880. uint32 error_buf_size)
  4881. {
  4882. char *aot_last_error;
  4883. uint32 i;
  4884. if (module->function_count == 0)
  4885. return true;
  4886. if (!aot_compile_wasm(module->comp_ctx)) {
  4887. aot_last_error = aot_get_last_error();
  4888. bh_assert(aot_last_error != NULL);
  4889. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4890. return false;
  4891. }
  4892. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4893. if (module->orcjit_stop_compiling)
  4894. return false;
  4895. #endif
  4896. bh_print_time("Begin to lookup llvm jit functions");
  4897. for (i = 0; i < module->function_count; i++) {
  4898. LLVMOrcJITTargetAddress func_addr = 0;
  4899. LLVMErrorRef error;
  4900. char func_name[48];
  4901. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4902. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4903. func_name);
  4904. if (error != LLVMErrorSuccess) {
  4905. char *err_msg = LLVMGetErrorMessage(error);
  4906. set_error_buf_v(error_buf, error_buf_size,
  4907. "failed to compile llvm jit function: %s", err_msg);
  4908. LLVMDisposeErrorMessage(err_msg);
  4909. return false;
  4910. }
  4911. /**
  4912. * No need to lock the func_ptr[func_idx] here as it is basic
  4913. * data type, the load/store for it can be finished by one cpu
  4914. * instruction, and there can be only one cpu instruction
  4915. * loading/storing at the same time.
  4916. */
  4917. module->func_ptrs[i] = (void *)func_addr;
  4918. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4919. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4920. if (module->orcjit_stop_compiling)
  4921. return false;
  4922. #endif
  4923. }
  4924. bh_print_time("End lookup llvm jit functions");
  4925. return true;
  4926. }
  4927. #endif /* end of WASM_ENABLE_JIT != 0 */
  4928. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4929. && WASM_ENABLE_LAZY_JIT != 0
  4930. static void *
  4931. init_llvm_jit_functions_stage2_callback(void *arg)
  4932. {
  4933. WASMModule *module = (WASMModule *)arg;
  4934. char error_buf[128];
  4935. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4936. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4937. module->orcjit_stop_compiling = true;
  4938. return NULL;
  4939. }
  4940. os_mutex_lock(&module->tierup_wait_lock);
  4941. module->llvm_jit_inited = true;
  4942. os_cond_broadcast(&module->tierup_wait_cond);
  4943. os_mutex_unlock(&module->tierup_wait_lock);
  4944. return NULL;
  4945. }
  4946. #endif
  4947. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4948. /* The callback function to compile jit functions */
  4949. static void *
  4950. orcjit_thread_callback(void *arg)
  4951. {
  4952. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4953. #if WASM_ENABLE_JIT != 0
  4954. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4955. #endif
  4956. WASMModule *module = thread_arg->module;
  4957. uint32 group_idx = thread_arg->group_idx;
  4958. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4959. uint32 func_count = module->function_count;
  4960. uint32 i;
  4961. #if WASM_ENABLE_FAST_JIT != 0
  4962. /* Compile fast jit functions of this group */
  4963. for (i = group_idx; i < func_count; i += group_stride) {
  4964. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4965. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4966. break;
  4967. }
  4968. if (module->orcjit_stop_compiling) {
  4969. return NULL;
  4970. }
  4971. }
  4972. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4973. os_mutex_lock(&module->tierup_wait_lock);
  4974. module->fast_jit_ready_groups++;
  4975. os_mutex_unlock(&module->tierup_wait_lock);
  4976. #endif
  4977. #endif
  4978. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4979. && WASM_ENABLE_LAZY_JIT != 0
  4980. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4981. for (i = group_idx; i < func_count;
  4982. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4983. uint32 j;
  4984. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4985. if (i + j * group_stride < func_count) {
  4986. if (!jit_compiler_set_call_to_fast_jit(
  4987. module,
  4988. i + j * group_stride + module->import_function_count)) {
  4989. LOG_ERROR(
  4990. "failed to compile call_to_fast_jit for func %u\n",
  4991. i + j * group_stride + module->import_function_count);
  4992. module->orcjit_stop_compiling = true;
  4993. return NULL;
  4994. }
  4995. }
  4996. if (module->orcjit_stop_compiling) {
  4997. return NULL;
  4998. }
  4999. }
  5000. }
  5001. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  5002. fast jit functions are compiled */
  5003. os_mutex_lock(&module->tierup_wait_lock);
  5004. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5005. && module->fast_jit_ready_groups >= group_stride)) {
  5006. os_cond_reltimedwait(&module->tierup_wait_cond,
  5007. &module->tierup_wait_lock, 10000);
  5008. if (module->orcjit_stop_compiling) {
  5009. /* init_llvm_jit_functions_stage2 failed */
  5010. os_mutex_unlock(&module->tierup_wait_lock);
  5011. return NULL;
  5012. }
  5013. }
  5014. os_mutex_unlock(&module->tierup_wait_lock);
  5015. #endif
  5016. #if WASM_ENABLE_JIT != 0
  5017. /* Compile llvm jit functions of this group */
  5018. for (i = group_idx; i < func_count;
  5019. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5020. LLVMOrcJITTargetAddress func_addr = 0;
  5021. LLVMErrorRef error;
  5022. char func_name[48];
  5023. typedef void (*F)(void);
  5024. union {
  5025. F f;
  5026. void *v;
  5027. } u;
  5028. uint32 j;
  5029. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5030. "_wrapper");
  5031. LOG_DEBUG("compile llvm jit func %s", func_name);
  5032. error =
  5033. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5034. if (error != LLVMErrorSuccess) {
  5035. char *err_msg = LLVMGetErrorMessage(error);
  5036. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5037. LLVMDisposeErrorMessage(err_msg);
  5038. break;
  5039. }
  5040. /* Call the jit wrapper function to trigger its compilation, so as
  5041. to compile the actual jit functions, since we add the latter to
  5042. function list in the PartitionFunction callback */
  5043. u.v = (void *)func_addr;
  5044. u.f();
  5045. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5046. if (i + j * group_stride < func_count) {
  5047. module->func_ptrs_compiled[i + j * group_stride] = true;
  5048. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5049. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5050. i + j * group_stride);
  5051. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5052. func_name);
  5053. if (error != LLVMErrorSuccess) {
  5054. char *err_msg = LLVMGetErrorMessage(error);
  5055. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5056. err_msg);
  5057. LLVMDisposeErrorMessage(err_msg);
  5058. /* Ignore current llvm jit func, as its func ptr is
  5059. previous set to call_to_fast_jit, which also works */
  5060. continue;
  5061. }
  5062. jit_compiler_set_llvm_jit_func_ptr(
  5063. module,
  5064. i + j * group_stride + module->import_function_count,
  5065. (void *)func_addr);
  5066. /* Try to switch to call this llvm jit function instead of
  5067. fast jit function from fast jit jitted code */
  5068. jit_compiler_set_call_to_llvm_jit(
  5069. module,
  5070. i + j * group_stride + module->import_function_count);
  5071. #endif
  5072. }
  5073. }
  5074. if (module->orcjit_stop_compiling) {
  5075. break;
  5076. }
  5077. }
  5078. #endif
  5079. return NULL;
  5080. }
  5081. static void
  5082. orcjit_stop_compile_threads(WASMModule *module)
  5083. {
  5084. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5085. / sizeof(OrcJitThreadArg));
  5086. module->orcjit_stop_compiling = true;
  5087. for (i = 0; i < thread_num; i++) {
  5088. if (module->orcjit_threads[i])
  5089. os_thread_join(module->orcjit_threads[i], NULL);
  5090. }
  5091. }
  5092. static bool
  5093. compile_jit_functions(WASMModule *module, char *error_buf,
  5094. uint32 error_buf_size)
  5095. {
  5096. uint32 thread_num =
  5097. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5098. uint32 i, j;
  5099. bh_print_time("Begin to compile jit functions");
  5100. /* Create threads to compile the jit functions */
  5101. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5102. #if WASM_ENABLE_JIT != 0
  5103. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5104. #endif
  5105. module->orcjit_thread_args[i].module = module;
  5106. module->orcjit_thread_args[i].group_idx = i;
  5107. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5108. (void *)&module->orcjit_thread_args[i],
  5109. APP_THREAD_STACK_SIZE_DEFAULT)
  5110. != 0) {
  5111. set_error_buf(error_buf, error_buf_size,
  5112. "create orcjit compile thread failed");
  5113. /* Terminate the threads created */
  5114. module->orcjit_stop_compiling = true;
  5115. for (j = 0; j < i; j++) {
  5116. os_thread_join(module->orcjit_threads[j], NULL);
  5117. }
  5118. return false;
  5119. }
  5120. }
  5121. #if WASM_ENABLE_LAZY_JIT == 0
  5122. /* Wait until all jit functions are compiled for eager mode */
  5123. for (i = 0; i < thread_num; i++) {
  5124. if (module->orcjit_threads[i])
  5125. os_thread_join(module->orcjit_threads[i], NULL);
  5126. }
  5127. #if WASM_ENABLE_FAST_JIT != 0
  5128. /* Ensure all the fast-jit functions are compiled */
  5129. for (i = 0; i < module->function_count; i++) {
  5130. if (!jit_compiler_is_compiled(module,
  5131. i + module->import_function_count)) {
  5132. set_error_buf(error_buf, error_buf_size,
  5133. "failed to compile fast jit function");
  5134. return false;
  5135. }
  5136. }
  5137. #endif
  5138. #if WASM_ENABLE_JIT != 0
  5139. /* Ensure all the llvm-jit functions are compiled */
  5140. for (i = 0; i < module->function_count; i++) {
  5141. if (!module->func_ptrs_compiled[i]) {
  5142. set_error_buf(error_buf, error_buf_size,
  5143. "failed to compile llvm jit function");
  5144. return false;
  5145. }
  5146. }
  5147. #endif
  5148. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5149. bh_print_time("End compile jit functions");
  5150. return true;
  5151. }
  5152. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5153. static bool
  5154. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5155. uint32 cur_func_idx, char *error_buf,
  5156. uint32 error_buf_size);
  5157. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5158. void **
  5159. wasm_interp_get_handle_table(void);
  5160. static void **handle_table;
  5161. #endif
  5162. static bool
  5163. load_from_sections(WASMModule *module, WASMSection *sections,
  5164. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5165. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5166. {
  5167. WASMExport *export;
  5168. WASMSection *section = sections;
  5169. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5170. *buf_func = NULL, *buf_func_end = NULL;
  5171. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5172. WASMGlobal *aux_stack_top_global = NULL, *global;
  5173. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5174. aux_stack_top = (uint64)-1LL;
  5175. uint32 global_index, func_index, i;
  5176. uint32 aux_data_end_global_index = (uint32)-1;
  5177. uint32 aux_heap_base_global_index = (uint32)-1;
  5178. WASMFuncType *func_type;
  5179. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5180. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5181. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5182. #if WASM_ENABLE_BULK_MEMORY != 0
  5183. bool has_datacount_section = false;
  5184. #endif
  5185. /* Find code and function sections if have */
  5186. while (section) {
  5187. if (section->section_type == SECTION_TYPE_CODE) {
  5188. buf_code = section->section_body;
  5189. buf_code_end = buf_code + section->section_body_size;
  5190. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5191. module->buf_code = (uint8 *)buf_code;
  5192. module->buf_code_size = section->section_body_size;
  5193. #endif
  5194. }
  5195. else if (section->section_type == SECTION_TYPE_FUNC) {
  5196. buf_func = section->section_body;
  5197. buf_func_end = buf_func + section->section_body_size;
  5198. }
  5199. section = section->next;
  5200. }
  5201. section = sections;
  5202. while (section) {
  5203. buf = section->section_body;
  5204. buf_end = buf + section->section_body_size;
  5205. switch (section->section_type) {
  5206. case SECTION_TYPE_USER:
  5207. /* unsupported user section, ignore it. */
  5208. if (!load_user_section(buf, buf_end, module,
  5209. reuse_const_strings, error_buf,
  5210. error_buf_size))
  5211. return false;
  5212. break;
  5213. case SECTION_TYPE_TYPE:
  5214. if (!load_type_section(buf, buf_end, module, error_buf,
  5215. error_buf_size))
  5216. return false;
  5217. break;
  5218. case SECTION_TYPE_IMPORT:
  5219. if (!load_import_section(buf, buf_end, module,
  5220. reuse_const_strings, no_resolve,
  5221. error_buf, error_buf_size))
  5222. return false;
  5223. break;
  5224. case SECTION_TYPE_FUNC:
  5225. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5226. module, error_buf, error_buf_size))
  5227. return false;
  5228. break;
  5229. case SECTION_TYPE_TABLE:
  5230. if (!load_table_section(buf, buf_end, module, error_buf,
  5231. error_buf_size))
  5232. return false;
  5233. break;
  5234. case SECTION_TYPE_MEMORY:
  5235. if (!load_memory_section(buf, buf_end, module, error_buf,
  5236. error_buf_size))
  5237. return false;
  5238. break;
  5239. #if WASM_ENABLE_TAGS != 0
  5240. case SECTION_TYPE_TAG:
  5241. /* load tag declaration section */
  5242. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5243. module, error_buf, error_buf_size))
  5244. return false;
  5245. break;
  5246. #endif
  5247. case SECTION_TYPE_GLOBAL:
  5248. if (!load_global_section(buf, buf_end, module, error_buf,
  5249. error_buf_size))
  5250. return false;
  5251. break;
  5252. case SECTION_TYPE_EXPORT:
  5253. if (!load_export_section(buf, buf_end, module,
  5254. reuse_const_strings, error_buf,
  5255. error_buf_size))
  5256. return false;
  5257. break;
  5258. case SECTION_TYPE_START:
  5259. if (!load_start_section(buf, buf_end, module, error_buf,
  5260. error_buf_size))
  5261. return false;
  5262. break;
  5263. case SECTION_TYPE_ELEM:
  5264. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5265. error_buf_size))
  5266. return false;
  5267. break;
  5268. case SECTION_TYPE_CODE:
  5269. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5270. module, error_buf, error_buf_size))
  5271. return false;
  5272. break;
  5273. case SECTION_TYPE_DATA:
  5274. if (!load_data_segment_section(buf, buf_end, module,
  5275. #if WASM_ENABLE_BULK_MEMORY != 0
  5276. has_datacount_section,
  5277. #endif
  5278. clone_data_seg, error_buf,
  5279. error_buf_size))
  5280. return false;
  5281. break;
  5282. #if WASM_ENABLE_BULK_MEMORY != 0
  5283. case SECTION_TYPE_DATACOUNT:
  5284. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5285. error_buf_size))
  5286. return false;
  5287. has_datacount_section = true;
  5288. break;
  5289. #endif
  5290. #if WASM_ENABLE_STRINGREF != 0
  5291. case SECTION_TYPE_STRINGREF:
  5292. if (!load_stringref_section(buf, buf_end, module,
  5293. reuse_const_strings, error_buf,
  5294. error_buf_size))
  5295. return false;
  5296. break;
  5297. #endif
  5298. default:
  5299. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5300. return false;
  5301. }
  5302. section = section->next;
  5303. }
  5304. #if WASM_ENABLE_BULK_MEMORY != 0
  5305. if (!check_data_count_consistency(
  5306. has_datacount_section, module->data_seg_count1,
  5307. module->data_seg_count, error_buf, error_buf_size)) {
  5308. return false;
  5309. }
  5310. #endif
  5311. module->aux_data_end_global_index = (uint32)-1;
  5312. module->aux_heap_base_global_index = (uint32)-1;
  5313. module->aux_stack_top_global_index = (uint32)-1;
  5314. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5315. export = module->exports;
  5316. for (i = 0; i < module->export_count; i++, export ++) {
  5317. if (export->kind == EXPORT_KIND_GLOBAL) {
  5318. if (!strcmp(export->name, "__heap_base")) {
  5319. if (export->index < module->import_global_count) {
  5320. LOG_DEBUG("Skip the process if __heap_base is imported "
  5321. "instead of being a local global");
  5322. continue;
  5323. }
  5324. /* only process linker-generated symbols */
  5325. global_index = export->index - module->import_global_count;
  5326. global = module->globals + global_index;
  5327. if (global->type.val_type == VALUE_TYPE_I32
  5328. && !global->type.is_mutable
  5329. && global->init_expr.init_expr_type
  5330. == INIT_EXPR_TYPE_I32_CONST) {
  5331. aux_heap_base_global = global;
  5332. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5333. aux_heap_base_global_index = export->index;
  5334. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5335. aux_heap_base);
  5336. }
  5337. }
  5338. else if (!strcmp(export->name, "__data_end")) {
  5339. if (export->index < module->import_global_count) {
  5340. LOG_DEBUG("Skip the process if __data_end is imported "
  5341. "instead of being a local global");
  5342. continue;
  5343. }
  5344. /* only process linker-generated symbols */
  5345. global_index = export->index - module->import_global_count;
  5346. global = module->globals + global_index;
  5347. if (global->type.val_type == VALUE_TYPE_I32
  5348. && !global->type.is_mutable
  5349. && global->init_expr.init_expr_type
  5350. == INIT_EXPR_TYPE_I32_CONST) {
  5351. aux_data_end_global = global;
  5352. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5353. aux_data_end_global_index = export->index;
  5354. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5355. aux_data_end);
  5356. aux_data_end = align_uint64(aux_data_end, 16);
  5357. }
  5358. }
  5359. /* For module compiled with -pthread option, the global is:
  5360. [0] stack_top <-- 0
  5361. [1] tls_pointer
  5362. [2] tls_size
  5363. [3] data_end <-- 3
  5364. [4] global_base
  5365. [5] heap_base <-- 5
  5366. [6] dso_handle
  5367. For module compiled without -pthread option:
  5368. [0] stack_top <-- 0
  5369. [1] data_end <-- 1
  5370. [2] global_base
  5371. [3] heap_base <-- 3
  5372. [4] dso_handle
  5373. */
  5374. if (aux_data_end_global && aux_heap_base_global
  5375. && aux_data_end <= aux_heap_base) {
  5376. module->aux_data_end_global_index = aux_data_end_global_index;
  5377. module->aux_data_end = aux_data_end;
  5378. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5379. module->aux_heap_base = aux_heap_base;
  5380. /* Resolve aux stack top global */
  5381. for (global_index = 0; global_index < module->global_count;
  5382. global_index++) {
  5383. global = module->globals + global_index;
  5384. if (global->type.is_mutable /* heap_base and data_end is
  5385. not mutable */
  5386. && global->type.val_type == VALUE_TYPE_I32
  5387. && global->init_expr.init_expr_type
  5388. == INIT_EXPR_TYPE_I32_CONST
  5389. && (uint64)(uint32)global->init_expr.u.i32
  5390. <= aux_heap_base) {
  5391. aux_stack_top_global = global;
  5392. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5393. module->aux_stack_top_global_index =
  5394. module->import_global_count + global_index;
  5395. module->aux_stack_bottom = aux_stack_top;
  5396. module->aux_stack_size =
  5397. aux_stack_top > aux_data_end
  5398. ? (uint32)(aux_stack_top - aux_data_end)
  5399. : (uint32)aux_stack_top;
  5400. LOG_VERBOSE(
  5401. "Found aux stack top global, value: %" PRIu64 ", "
  5402. "global index: %d, stack size: %d",
  5403. aux_stack_top, global_index,
  5404. module->aux_stack_size);
  5405. break;
  5406. }
  5407. }
  5408. if (!aux_stack_top_global) {
  5409. /* Auxiliary stack global isn't found, it must be unused
  5410. in the wasm app, as if it is used, the global must be
  5411. defined. Here we set it to __heap_base global and set
  5412. its size to 0. */
  5413. aux_stack_top_global = aux_heap_base_global;
  5414. aux_stack_top = aux_heap_base;
  5415. module->aux_stack_top_global_index =
  5416. module->aux_heap_base_global_index;
  5417. module->aux_stack_bottom = aux_stack_top;
  5418. module->aux_stack_size = 0;
  5419. }
  5420. break;
  5421. }
  5422. }
  5423. }
  5424. module->malloc_function = (uint32)-1;
  5425. module->free_function = (uint32)-1;
  5426. module->retain_function = (uint32)-1;
  5427. /* Resolve malloc/free function exported by wasm module */
  5428. #if WASM_ENABLE_MEMORY64 != 0
  5429. if (has_module_memory64(module))
  5430. malloc_free_io_type = VALUE_TYPE_I64;
  5431. #endif
  5432. export = module->exports;
  5433. for (i = 0; i < module->export_count; i++, export ++) {
  5434. if (export->kind == EXPORT_KIND_FUNC) {
  5435. if (!strcmp(export->name, "malloc")
  5436. && export->index >= module->import_function_count) {
  5437. func_index = export->index - module->import_function_count;
  5438. func_type = module->functions[func_index]->func_type;
  5439. if (func_type->param_count == 1 && func_type->result_count == 1
  5440. && func_type->types[0] == malloc_free_io_type
  5441. && func_type->types[1] == malloc_free_io_type) {
  5442. bh_assert(module->malloc_function == (uint32)-1);
  5443. module->malloc_function = export->index;
  5444. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5445. export->name, export->index);
  5446. }
  5447. }
  5448. else if (!strcmp(export->name, "__new")
  5449. && export->index >= module->import_function_count) {
  5450. /* __new && __pin for AssemblyScript */
  5451. func_index = export->index - module->import_function_count;
  5452. func_type = module->functions[func_index]->func_type;
  5453. if (func_type->param_count == 2 && func_type->result_count == 1
  5454. && func_type->types[0] == malloc_free_io_type
  5455. && func_type->types[1] == VALUE_TYPE_I32
  5456. && func_type->types[2] == malloc_free_io_type) {
  5457. uint32 j;
  5458. WASMExport *export_tmp;
  5459. bh_assert(module->malloc_function == (uint32)-1);
  5460. module->malloc_function = export->index;
  5461. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5462. export->name, export->index);
  5463. /* resolve retain function.
  5464. If not found, reset malloc function index */
  5465. export_tmp = module->exports;
  5466. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5467. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5468. && (!strcmp(export_tmp->name, "__retain")
  5469. || (!strcmp(export_tmp->name, "__pin")))
  5470. && (export_tmp->index
  5471. >= module->import_function_count)) {
  5472. func_index = export_tmp->index
  5473. - module->import_function_count;
  5474. func_type =
  5475. module->functions[func_index]->func_type;
  5476. if (func_type->param_count == 1
  5477. && func_type->result_count == 1
  5478. && func_type->types[0] == malloc_free_io_type
  5479. && func_type->types[1] == malloc_free_io_type) {
  5480. bh_assert(module->retain_function
  5481. == (uint32)-1);
  5482. module->retain_function = export_tmp->index;
  5483. LOG_VERBOSE("Found retain function, name: %s, "
  5484. "index: %u",
  5485. export_tmp->name,
  5486. export_tmp->index);
  5487. break;
  5488. }
  5489. }
  5490. }
  5491. if (j == module->export_count) {
  5492. module->malloc_function = (uint32)-1;
  5493. LOG_VERBOSE("Can't find retain function,"
  5494. "reset malloc function index to -1");
  5495. }
  5496. }
  5497. }
  5498. else if (((!strcmp(export->name, "free"))
  5499. || (!strcmp(export->name, "__release"))
  5500. || (!strcmp(export->name, "__unpin")))
  5501. && export->index >= module->import_function_count) {
  5502. func_index = export->index - module->import_function_count;
  5503. func_type = module->functions[func_index]->func_type;
  5504. if (func_type->param_count == 1 && func_type->result_count == 0
  5505. && func_type->types[0] == malloc_free_io_type) {
  5506. bh_assert(module->free_function == (uint32)-1);
  5507. module->free_function = export->index;
  5508. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5509. export->name, export->index);
  5510. }
  5511. }
  5512. }
  5513. }
  5514. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5515. handle_table = wasm_interp_get_handle_table();
  5516. #endif
  5517. for (i = 0; i < module->function_count; i++) {
  5518. WASMFunction *func = module->functions[i];
  5519. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5520. error_buf_size)) {
  5521. return false;
  5522. }
  5523. if (i == module->function_count - 1
  5524. && func->code + func->code_size != buf_code_end) {
  5525. set_error_buf(error_buf, error_buf_size,
  5526. "code section size mismatch");
  5527. return false;
  5528. }
  5529. }
  5530. if (!module->possible_memory_grow) {
  5531. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5532. if (aux_data_end_global && aux_heap_base_global
  5533. && aux_stack_top_global) {
  5534. uint64 init_memory_size;
  5535. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5536. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5537. * valid range of uint32 */
  5538. if (shrunk_memory_size <= UINT32_MAX) {
  5539. if (module->import_memory_count) {
  5540. WASMMemoryImport *memory_import =
  5541. &module->import_memories[0].u.memory;
  5542. init_memory_size =
  5543. (uint64)memory_import->mem_type.num_bytes_per_page
  5544. * memory_import->mem_type.init_page_count;
  5545. if (shrunk_memory_size <= init_memory_size) {
  5546. /* Reset memory info to decrease memory usage */
  5547. memory_import->mem_type.num_bytes_per_page =
  5548. (uint32)shrunk_memory_size;
  5549. memory_import->mem_type.init_page_count = 1;
  5550. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5551. shrunk_memory_size);
  5552. }
  5553. }
  5554. if (module->memory_count) {
  5555. WASMMemory *memory = &module->memories[0];
  5556. init_memory_size = (uint64)memory->num_bytes_per_page
  5557. * memory->init_page_count;
  5558. if (shrunk_memory_size <= init_memory_size) {
  5559. /* Reset memory info to decrease memory usage */
  5560. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5561. memory->init_page_count = 1;
  5562. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5563. shrunk_memory_size);
  5564. }
  5565. }
  5566. }
  5567. }
  5568. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  5569. #if WASM_ENABLE_MULTI_MODULE == 0
  5570. if (module->import_memory_count) {
  5571. WASMMemoryImport *memory_import =
  5572. &module->import_memories[0].u.memory;
  5573. /* Only resize the memory to one big page if num_bytes_per_page is
  5574. * in valid range of uint32 */
  5575. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5576. memory_import->mem_type.num_bytes_per_page *=
  5577. memory_import->mem_type.init_page_count;
  5578. if (memory_import->mem_type.init_page_count > 0)
  5579. memory_import->mem_type.init_page_count =
  5580. memory_import->mem_type.max_page_count = 1;
  5581. else
  5582. memory_import->mem_type.init_page_count =
  5583. memory_import->mem_type.max_page_count = 0;
  5584. }
  5585. }
  5586. if (module->memory_count) {
  5587. WASMMemory *memory = &module->memories[0];
  5588. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5589. * valid range of uint32 */
  5590. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5591. memory->num_bytes_per_page *= memory->init_page_count;
  5592. if (memory->init_page_count > 0)
  5593. memory->init_page_count = memory->max_page_count = 1;
  5594. else
  5595. memory->init_page_count = memory->max_page_count = 0;
  5596. }
  5597. }
  5598. #endif
  5599. }
  5600. #if WASM_ENABLE_MEMORY64 != 0
  5601. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5602. false))
  5603. return false;
  5604. #endif
  5605. calculate_global_data_offset(module);
  5606. #if WASM_ENABLE_FAST_JIT != 0
  5607. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5608. return false;
  5609. }
  5610. #endif
  5611. #if WASM_ENABLE_JIT != 0
  5612. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5613. return false;
  5614. }
  5615. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5616. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5617. return false;
  5618. }
  5619. #else
  5620. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5621. thread fast jit execution, since applying llvm optimizations in
  5622. aot_compile_wasm may cost a lot of time.
  5623. Create thread with enough native stack to apply llvm optimizations */
  5624. if (os_thread_create(&module->llvm_jit_init_thread,
  5625. init_llvm_jit_functions_stage2_callback,
  5626. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5627. != 0) {
  5628. set_error_buf(error_buf, error_buf_size,
  5629. "create orcjit compile thread failed");
  5630. return false;
  5631. }
  5632. #endif
  5633. #endif
  5634. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5635. /* Create threads to compile the jit functions */
  5636. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5637. return false;
  5638. }
  5639. #endif
  5640. #if WASM_ENABLE_MEMORY_TRACING != 0
  5641. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5642. #endif
  5643. return true;
  5644. }
  5645. static WASMModule *
  5646. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5647. {
  5648. WASMModule *module =
  5649. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5650. bh_list_status ret;
  5651. if (!module) {
  5652. return NULL;
  5653. }
  5654. module->module_type = Wasm_Module_Bytecode;
  5655. /* Set start_function to -1, means no start function */
  5656. module->start_function = (uint32)-1;
  5657. module->name = name;
  5658. module->is_binary_freeable = false;
  5659. #if WASM_ENABLE_FAST_INTERP == 0
  5660. module->br_table_cache_list = &module->br_table_cache_list_head;
  5661. ret = bh_list_init(module->br_table_cache_list);
  5662. bh_assert(ret == BH_LIST_SUCCESS);
  5663. #endif
  5664. #if WASM_ENABLE_MULTI_MODULE != 0
  5665. module->import_module_list = &module->import_module_list_head;
  5666. ret = bh_list_init(module->import_module_list);
  5667. bh_assert(ret == BH_LIST_SUCCESS);
  5668. #endif
  5669. #if WASM_ENABLE_DEBUG_INTERP != 0
  5670. ret = bh_list_init(&module->fast_opcode_list);
  5671. bh_assert(ret == BH_LIST_SUCCESS);
  5672. #endif
  5673. #if WASM_ENABLE_GC != 0
  5674. if (!(module->ref_type_set =
  5675. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5676. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5677. goto fail1;
  5678. }
  5679. if (os_mutex_init(&module->rtt_type_lock)) {
  5680. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5681. goto fail2;
  5682. }
  5683. #endif
  5684. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5685. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5686. && WASM_ENABLE_LAZY_JIT != 0)
  5687. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5688. set_error_buf(error_buf, error_buf_size,
  5689. "init instance list lock failed");
  5690. goto fail3;
  5691. }
  5692. #endif
  5693. #if WASM_ENABLE_LIBC_WASI != 0
  5694. module->wasi_args.stdio[0] = os_invalid_raw_handle();
  5695. module->wasi_args.stdio[1] = os_invalid_raw_handle();
  5696. module->wasi_args.stdio[2] = os_invalid_raw_handle();
  5697. #endif
  5698. (void)ret;
  5699. return module;
  5700. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5701. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5702. && WASM_ENABLE_LAZY_JIT != 0)
  5703. fail3:
  5704. #endif
  5705. #if WASM_ENABLE_GC != 0
  5706. os_mutex_destroy(&module->rtt_type_lock);
  5707. fail2:
  5708. bh_hash_map_destroy(module->ref_type_set);
  5709. fail1:
  5710. #endif
  5711. wasm_runtime_free(module);
  5712. return NULL;
  5713. }
  5714. #if WASM_ENABLE_DEBUG_INTERP != 0
  5715. static bool
  5716. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5717. uint32 error_buf_size)
  5718. {
  5719. WASMFastOPCodeNode *fast_op =
  5720. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5721. if (fast_op) {
  5722. fast_op->offset = pos - module->load_addr;
  5723. fast_op->orig_op = orig_op;
  5724. bh_list_insert(&module->fast_opcode_list, fast_op);
  5725. }
  5726. return fast_op ? true : false;
  5727. }
  5728. #endif
  5729. WASMModule *
  5730. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5731. uint32 error_buf_size)
  5732. {
  5733. WASMModule *module = create_module("", error_buf, error_buf_size);
  5734. if (!module)
  5735. return NULL;
  5736. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  5737. error_buf_size)) {
  5738. wasm_loader_unload(module);
  5739. return NULL;
  5740. }
  5741. LOG_VERBOSE("Load module from sections success.\n");
  5742. return module;
  5743. }
  5744. static void
  5745. destroy_sections(WASMSection *section_list)
  5746. {
  5747. WASMSection *section = section_list, *next;
  5748. while (section) {
  5749. next = section->next;
  5750. wasm_runtime_free(section);
  5751. section = next;
  5752. }
  5753. }
  5754. /* clang-format off */
  5755. static uint8 section_ids[] = {
  5756. SECTION_TYPE_USER,
  5757. SECTION_TYPE_TYPE,
  5758. SECTION_TYPE_IMPORT,
  5759. SECTION_TYPE_FUNC,
  5760. SECTION_TYPE_TABLE,
  5761. SECTION_TYPE_MEMORY,
  5762. #if WASM_ENABLE_TAGS != 0
  5763. SECTION_TYPE_TAG,
  5764. #endif
  5765. #if WASM_ENABLE_STRINGREF != 0
  5766. /* must immediately precede the global section,
  5767. or where the global section would be */
  5768. SECTION_TYPE_STRINGREF,
  5769. #endif
  5770. SECTION_TYPE_GLOBAL,
  5771. SECTION_TYPE_EXPORT,
  5772. SECTION_TYPE_START,
  5773. SECTION_TYPE_ELEM,
  5774. #if WASM_ENABLE_BULK_MEMORY != 0
  5775. SECTION_TYPE_DATACOUNT,
  5776. #endif
  5777. SECTION_TYPE_CODE,
  5778. SECTION_TYPE_DATA
  5779. };
  5780. /* clang-format on */
  5781. static uint8
  5782. get_section_index(uint8 section_type)
  5783. {
  5784. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5785. for (uint8 i = 0; i < max_id; i++) {
  5786. if (section_type == section_ids[i])
  5787. return i;
  5788. }
  5789. return (uint8)-1;
  5790. }
  5791. static bool
  5792. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5793. char *error_buf, uint32 error_buf_size)
  5794. {
  5795. WASMSection *section_list_end = NULL, *section;
  5796. const uint8 *p = buf, *p_end = buf + size;
  5797. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5798. uint32 section_size;
  5799. bh_assert(!*p_section_list);
  5800. p += 8;
  5801. while (p < p_end) {
  5802. CHECK_BUF(p, p_end, 1);
  5803. section_type = read_uint8(p);
  5804. section_index = get_section_index(section_type);
  5805. if (section_index != (uint8)-1) {
  5806. if (section_type != SECTION_TYPE_USER) {
  5807. /* Custom sections may be inserted at any place,
  5808. while other sections must occur at most once
  5809. and in prescribed order. */
  5810. if (last_section_index != (uint8)-1
  5811. && (section_index <= last_section_index)) {
  5812. set_error_buf(error_buf, error_buf_size,
  5813. "unexpected content after last section or "
  5814. "junk after last section");
  5815. return false;
  5816. }
  5817. last_section_index = section_index;
  5818. }
  5819. read_leb_uint32(p, p_end, section_size);
  5820. CHECK_BUF1(p, p_end, section_size);
  5821. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5822. error_buf_size))) {
  5823. return false;
  5824. }
  5825. section->section_type = section_type;
  5826. section->section_body = (uint8 *)p;
  5827. section->section_body_size = section_size;
  5828. if (!section_list_end)
  5829. *p_section_list = section_list_end = section;
  5830. else {
  5831. section_list_end->next = section;
  5832. section_list_end = section;
  5833. }
  5834. p += section_size;
  5835. }
  5836. else {
  5837. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5838. return false;
  5839. }
  5840. }
  5841. return true;
  5842. fail:
  5843. return false;
  5844. }
  5845. static void
  5846. exchange32(uint8 *p_data)
  5847. {
  5848. uint8 value = *p_data;
  5849. *p_data = *(p_data + 3);
  5850. *(p_data + 3) = value;
  5851. value = *(p_data + 1);
  5852. *(p_data + 1) = *(p_data + 2);
  5853. *(p_data + 2) = value;
  5854. }
  5855. static union {
  5856. int a;
  5857. char b;
  5858. } __ue = { .a = 1 };
  5859. #define is_little_endian() (__ue.b == 1)
  5860. static bool
  5861. load(const uint8 *buf, uint32 size, WASMModule *module,
  5862. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  5863. uint32 error_buf_size)
  5864. {
  5865. const uint8 *buf_end = buf + size;
  5866. const uint8 *p = buf, *p_end = buf_end;
  5867. uint32 magic_number, version;
  5868. WASMSection *section_list = NULL;
  5869. CHECK_BUF1(p, p_end, sizeof(uint32));
  5870. magic_number = read_uint32(p);
  5871. if (!is_little_endian())
  5872. exchange32((uint8 *)&magic_number);
  5873. if (magic_number != WASM_MAGIC_NUMBER) {
  5874. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5875. return false;
  5876. }
  5877. CHECK_BUF1(p, p_end, sizeof(uint32));
  5878. version = read_uint32(p);
  5879. if (!is_little_endian())
  5880. exchange32((uint8 *)&version);
  5881. if (version != WASM_CURRENT_VERSION) {
  5882. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5883. return false;
  5884. }
  5885. module->package_version = version;
  5886. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5887. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5888. no_resolve, error_buf, error_buf_size)) {
  5889. destroy_sections(section_list);
  5890. return false;
  5891. }
  5892. destroy_sections(section_list);
  5893. return true;
  5894. fail:
  5895. return false;
  5896. }
  5897. #if WASM_ENABLE_LIBC_WASI != 0
  5898. /**
  5899. * refer to
  5900. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5901. */
  5902. static bool
  5903. check_wasi_abi_compatibility(const WASMModule *module,
  5904. #if WASM_ENABLE_MULTI_MODULE != 0
  5905. bool main_module,
  5906. #endif
  5907. char *error_buf, uint32 error_buf_size)
  5908. {
  5909. /**
  5910. * be careful with:
  5911. * wasi compatible modules(command/reactor) which don't import any wasi
  5912. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5913. * reactor can depend on nothing. At the same time, each has its own entry
  5914. * point.
  5915. *
  5916. * observations:
  5917. * - clang always injects `_start` into a command
  5918. * - clang always injects `_initialize` into a reactor
  5919. * - `iwasm -f` allows to run a function in the reactor
  5920. *
  5921. * strong assumptions:
  5922. * - no one will define either `_start` or `_initialize` on purpose
  5923. * - `_start` should always be `void _start(void)`
  5924. * - `_initialize` should always be `void _initialize(void)`
  5925. *
  5926. */
  5927. /* clang-format off */
  5928. /**
  5929. *
  5930. * | | import_wasi_api True | | import_wasi_api False | |
  5931. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5932. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5933. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5934. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5935. */
  5936. /* clang-format on */
  5937. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5938. uint32 import_function_count = module->import_function_count;
  5939. WASMFuncType *func_type;
  5940. /* (func (export "_start") (...) */
  5941. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5942. error_buf, error_buf_size);
  5943. if (start) {
  5944. if (start->index < import_function_count) {
  5945. set_error_buf(
  5946. error_buf, error_buf_size,
  5947. "the builtin _start function can not be an import function");
  5948. return false;
  5949. }
  5950. func_type =
  5951. module->functions[start->index - import_function_count]->func_type;
  5952. if (func_type->param_count || func_type->result_count) {
  5953. set_error_buf(error_buf, error_buf_size,
  5954. "the signature of builtin _start function is wrong");
  5955. return false;
  5956. }
  5957. }
  5958. else {
  5959. /* (func (export "_initialize") (...) */
  5960. initialize =
  5961. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5962. error_buf, error_buf_size);
  5963. if (initialize) {
  5964. if (initialize->index < import_function_count) {
  5965. set_error_buf(error_buf, error_buf_size,
  5966. "the builtin _initialize function can not be an "
  5967. "import function");
  5968. return false;
  5969. }
  5970. func_type =
  5971. module->functions[initialize->index - import_function_count]
  5972. ->func_type;
  5973. if (func_type->param_count || func_type->result_count) {
  5974. set_error_buf(
  5975. error_buf, error_buf_size,
  5976. "the signature of builtin _initialize function is wrong");
  5977. return false;
  5978. }
  5979. }
  5980. }
  5981. /* filter out non-wasi compatible modules */
  5982. if (!module->import_wasi_api && !start && !initialize) {
  5983. return true;
  5984. }
  5985. /* should have one at least */
  5986. if (module->import_wasi_api && !start && !initialize) {
  5987. LOG_WARNING("warning: a module with WASI apis should be either "
  5988. "a command or a reactor");
  5989. }
  5990. /*
  5991. * there is at least one of `_start` and `_initialize` in below cases.
  5992. * according to the assumption, they should be all wasi compatible
  5993. */
  5994. #if WASM_ENABLE_MULTI_MODULE != 0
  5995. /* filter out commands (with `_start`) cases */
  5996. if (start && !main_module) {
  5997. set_error_buf(
  5998. error_buf, error_buf_size,
  5999. "a command (with _start function) can not be a sub-module");
  6000. return false;
  6001. }
  6002. #endif
  6003. /*
  6004. * it is ok a reactor acts as a main module,
  6005. * so skip the check about (with `_initialize`)
  6006. */
  6007. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  6008. error_buf, error_buf_size);
  6009. if (!memory
  6010. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  6011. /*
  6012. * with wasi-threads, it's still an open question if a memory
  6013. * should be exported.
  6014. *
  6015. * https://github.com/WebAssembly/wasi-threads/issues/22
  6016. * https://github.com/WebAssembly/WASI/issues/502
  6017. *
  6018. * Note: this code assumes the number of memories is at most 1.
  6019. */
  6020. && module->import_memory_count == 0
  6021. #endif
  6022. ) {
  6023. set_error_buf(error_buf, error_buf_size,
  6024. "a module with WASI apis must export memory by default");
  6025. return false;
  6026. }
  6027. return true;
  6028. }
  6029. #endif
  6030. WASMModule *
  6031. wasm_loader_load(uint8 *buf, uint32 size,
  6032. #if WASM_ENABLE_MULTI_MODULE != 0
  6033. bool main_module,
  6034. #endif
  6035. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6036. {
  6037. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6038. if (!module) {
  6039. return NULL;
  6040. }
  6041. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6042. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6043. module->load_addr = (uint8 *)buf;
  6044. module->load_size = size;
  6045. #endif
  6046. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6047. error_buf, error_buf_size)) {
  6048. goto fail;
  6049. }
  6050. #if WASM_ENABLE_LIBC_WASI != 0
  6051. /* Check the WASI application ABI */
  6052. if (!check_wasi_abi_compatibility(module,
  6053. #if WASM_ENABLE_MULTI_MODULE != 0
  6054. main_module,
  6055. #endif
  6056. error_buf, error_buf_size)) {
  6057. goto fail;
  6058. }
  6059. #endif
  6060. LOG_VERBOSE("Load module success.\n");
  6061. return module;
  6062. fail:
  6063. wasm_loader_unload(module);
  6064. return NULL;
  6065. }
  6066. void
  6067. wasm_loader_unload(WASMModule *module)
  6068. {
  6069. uint32 i;
  6070. if (!module)
  6071. return;
  6072. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6073. && WASM_ENABLE_LAZY_JIT != 0
  6074. module->orcjit_stop_compiling = true;
  6075. if (module->llvm_jit_init_thread)
  6076. os_thread_join(module->llvm_jit_init_thread, NULL);
  6077. #endif
  6078. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6079. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6080. module internal data after they were freed */
  6081. orcjit_stop_compile_threads(module);
  6082. #endif
  6083. #if WASM_ENABLE_JIT != 0
  6084. if (module->func_ptrs)
  6085. wasm_runtime_free(module->func_ptrs);
  6086. if (module->comp_ctx)
  6087. aot_destroy_comp_context(module->comp_ctx);
  6088. if (module->comp_data)
  6089. aot_destroy_comp_data(module->comp_data);
  6090. #endif
  6091. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6092. && WASM_ENABLE_LAZY_JIT != 0
  6093. if (module->tierup_wait_lock_inited) {
  6094. os_mutex_destroy(&module->tierup_wait_lock);
  6095. os_cond_destroy(&module->tierup_wait_cond);
  6096. }
  6097. #endif
  6098. if (module->imports)
  6099. wasm_runtime_free(module->imports);
  6100. if (module->functions) {
  6101. for (i = 0; i < module->function_count; i++) {
  6102. if (module->functions[i]) {
  6103. if (module->functions[i]->local_offsets)
  6104. wasm_runtime_free(module->functions[i]->local_offsets);
  6105. #if WASM_ENABLE_FAST_INTERP != 0
  6106. if (module->functions[i]->code_compiled)
  6107. wasm_runtime_free(module->functions[i]->code_compiled);
  6108. if (module->functions[i]->consts)
  6109. wasm_runtime_free(module->functions[i]->consts);
  6110. #endif
  6111. #if WASM_ENABLE_FAST_JIT != 0
  6112. if (module->functions[i]->fast_jit_jitted_code) {
  6113. jit_code_cache_free(
  6114. module->functions[i]->fast_jit_jitted_code);
  6115. }
  6116. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6117. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6118. jit_code_cache_free(
  6119. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6120. }
  6121. #endif
  6122. #endif
  6123. #if WASM_ENABLE_GC != 0
  6124. if (module->functions[i]->local_ref_type_maps) {
  6125. wasm_runtime_free(
  6126. module->functions[i]->local_ref_type_maps);
  6127. }
  6128. #endif
  6129. wasm_runtime_free(module->functions[i]);
  6130. }
  6131. }
  6132. wasm_runtime_free(module->functions);
  6133. }
  6134. if (module->tables) {
  6135. #if WASM_ENABLE_GC != 0
  6136. for (i = 0; i < module->table_count; i++) {
  6137. destroy_init_expr(module, &module->tables[i].init_expr);
  6138. }
  6139. #endif
  6140. wasm_runtime_free(module->tables);
  6141. }
  6142. if (module->memories)
  6143. wasm_runtime_free(module->memories);
  6144. if (module->globals) {
  6145. #if WASM_ENABLE_GC != 0
  6146. for (i = 0; i < module->global_count; i++) {
  6147. destroy_init_expr(module, &module->globals[i].init_expr);
  6148. }
  6149. #endif
  6150. wasm_runtime_free(module->globals);
  6151. }
  6152. #if WASM_ENABLE_TAGS != 0
  6153. if (module->tags) {
  6154. for (i = 0; i < module->tag_count; i++) {
  6155. if (module->tags[i])
  6156. wasm_runtime_free(module->tags[i]);
  6157. }
  6158. wasm_runtime_free(module->tags);
  6159. }
  6160. #endif
  6161. if (module->exports)
  6162. wasm_runtime_free(module->exports);
  6163. if (module->table_segments) {
  6164. for (i = 0; i < module->table_seg_count; i++) {
  6165. if (module->table_segments[i].init_values) {
  6166. #if WASM_ENABLE_GC != 0
  6167. uint32 j;
  6168. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6169. destroy_init_expr(
  6170. module, &module->table_segments[i].init_values[j]);
  6171. }
  6172. #endif
  6173. wasm_runtime_free(module->table_segments[i].init_values);
  6174. }
  6175. }
  6176. wasm_runtime_free(module->table_segments);
  6177. }
  6178. if (module->data_segments) {
  6179. for (i = 0; i < module->data_seg_count; i++) {
  6180. if (module->data_segments[i]) {
  6181. if (module->data_segments[i]->is_data_cloned)
  6182. wasm_runtime_free(module->data_segments[i]->data);
  6183. wasm_runtime_free(module->data_segments[i]);
  6184. }
  6185. }
  6186. wasm_runtime_free(module->data_segments);
  6187. }
  6188. if (module->types) {
  6189. for (i = 0; i < module->type_count; i++) {
  6190. if (module->types[i])
  6191. destroy_wasm_type(module->types[i]);
  6192. }
  6193. wasm_runtime_free(module->types);
  6194. }
  6195. if (module->const_str_list) {
  6196. StringNode *node = module->const_str_list, *node_next;
  6197. while (node) {
  6198. node_next = node->next;
  6199. wasm_runtime_free(node);
  6200. node = node_next;
  6201. }
  6202. }
  6203. #if WASM_ENABLE_STRINGREF != 0
  6204. if (module->string_literal_ptrs) {
  6205. wasm_runtime_free((void *)module->string_literal_ptrs);
  6206. }
  6207. if (module->string_literal_lengths) {
  6208. wasm_runtime_free(module->string_literal_lengths);
  6209. }
  6210. #endif
  6211. #if WASM_ENABLE_FAST_INTERP == 0
  6212. if (module->br_table_cache_list) {
  6213. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6214. BrTableCache *node_next;
  6215. while (node) {
  6216. node_next = bh_list_elem_next(node);
  6217. wasm_runtime_free(node);
  6218. node = node_next;
  6219. }
  6220. }
  6221. #endif
  6222. #if WASM_ENABLE_MULTI_MODULE != 0
  6223. /* just release the sub module list */
  6224. if (module->import_module_list) {
  6225. WASMRegisteredModule *node =
  6226. bh_list_first_elem(module->import_module_list);
  6227. while (node) {
  6228. WASMRegisteredModule *next = bh_list_elem_next(node);
  6229. bh_list_remove(module->import_module_list, node);
  6230. /*
  6231. * unload(sub_module) will be triggered during runtime_destroy().
  6232. * every module in the global module list will be unloaded one by
  6233. * one. so don't worry.
  6234. */
  6235. wasm_runtime_free(node);
  6236. /*
  6237. * the module file reading buffer will be released
  6238. * in runtime_destroy()
  6239. */
  6240. node = next;
  6241. }
  6242. }
  6243. #endif
  6244. #if WASM_ENABLE_DEBUG_INTERP != 0
  6245. WASMFastOPCodeNode *fast_opcode =
  6246. bh_list_first_elem(&module->fast_opcode_list);
  6247. while (fast_opcode) {
  6248. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6249. wasm_runtime_free(fast_opcode);
  6250. fast_opcode = next;
  6251. }
  6252. #endif
  6253. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6254. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6255. && WASM_ENABLE_LAZY_JIT != 0)
  6256. os_mutex_destroy(&module->instance_list_lock);
  6257. #endif
  6258. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6259. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6260. #endif
  6261. #if WASM_ENABLE_FAST_JIT != 0
  6262. if (module->fast_jit_func_ptrs) {
  6263. wasm_runtime_free(module->fast_jit_func_ptrs);
  6264. }
  6265. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6266. if (module->fast_jit_thread_locks_inited[i]) {
  6267. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6268. }
  6269. }
  6270. #endif
  6271. #if WASM_ENABLE_GC != 0
  6272. os_mutex_destroy(&module->rtt_type_lock);
  6273. bh_hash_map_destroy(module->ref_type_set);
  6274. if (module->rtt_types) {
  6275. for (i = 0; i < module->type_count; i++) {
  6276. if (module->rtt_types[i])
  6277. wasm_runtime_free(module->rtt_types[i]);
  6278. }
  6279. wasm_runtime_free(module->rtt_types);
  6280. }
  6281. #if WASM_ENABLE_STRINGREF != 0
  6282. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6283. if (module->stringref_rtts[i])
  6284. wasm_runtime_free(module->stringref_rtts[i]);
  6285. }
  6286. #endif
  6287. #endif
  6288. wasm_runtime_free(module);
  6289. }
  6290. bool
  6291. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6292. const uint8 *start_addr, const uint8 *code_end_addr,
  6293. uint8 label_type, uint8 **p_else_addr,
  6294. uint8 **p_end_addr)
  6295. {
  6296. const uint8 *p = start_addr, *p_end = code_end_addr;
  6297. uint8 *else_addr = NULL;
  6298. char error_buf[128];
  6299. uint32 block_nested_depth = 1, count, i, j, t;
  6300. uint32 error_buf_size = sizeof(error_buf);
  6301. uint8 opcode, u8;
  6302. BlockAddr block_stack[16] = { { 0 } }, *block;
  6303. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6304. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6305. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6306. if (block[j].start_addr == start_addr) {
  6307. /* Cache hit */
  6308. *p_else_addr = block[j].else_addr;
  6309. *p_end_addr = block[j].end_addr;
  6310. return true;
  6311. }
  6312. }
  6313. /* Cache unhit */
  6314. block_stack[0].start_addr = start_addr;
  6315. while (p < code_end_addr) {
  6316. opcode = *p++;
  6317. #if WASM_ENABLE_DEBUG_INTERP != 0
  6318. op_break_retry:
  6319. #endif
  6320. switch (opcode) {
  6321. case WASM_OP_UNREACHABLE:
  6322. case WASM_OP_NOP:
  6323. break;
  6324. #if WASM_ENABLE_EXCE_HANDLING != 0
  6325. case WASM_OP_TRY:
  6326. u8 = read_uint8(p);
  6327. if (block_nested_depth
  6328. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6329. block_stack[block_nested_depth].start_addr = p;
  6330. block_stack[block_nested_depth].else_addr = NULL;
  6331. }
  6332. block_nested_depth++;
  6333. break;
  6334. case EXT_OP_TRY:
  6335. skip_leb_uint32(p, p_end);
  6336. if (block_nested_depth
  6337. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6338. block_stack[block_nested_depth].start_addr = p;
  6339. block_stack[block_nested_depth].else_addr = NULL;
  6340. }
  6341. block_nested_depth++;
  6342. break;
  6343. case WASM_OP_CATCH:
  6344. if (block_nested_depth == 1) {
  6345. *p_end_addr = (uint8 *)(p - 1);
  6346. /* stop search and return the address of the catch block */
  6347. return true;
  6348. }
  6349. break;
  6350. case WASM_OP_CATCH_ALL:
  6351. if (block_nested_depth == 1) {
  6352. *p_end_addr = (uint8 *)(p - 1);
  6353. /* stop search and return the address of the catch_all block
  6354. */
  6355. return true;
  6356. }
  6357. break;
  6358. case WASM_OP_THROW:
  6359. /* skip tag_index */
  6360. skip_leb(p);
  6361. break;
  6362. case WASM_OP_RETHROW:
  6363. /* skip depth */
  6364. skip_leb(p);
  6365. break;
  6366. case WASM_OP_DELEGATE:
  6367. if (block_nested_depth == 1) {
  6368. *p_end_addr = (uint8 *)(p - 1);
  6369. return true;
  6370. }
  6371. else {
  6372. skip_leb(p);
  6373. /* the DELEGATE opcode ends the tryblock, */
  6374. block_nested_depth--;
  6375. if (block_nested_depth
  6376. < sizeof(block_stack) / sizeof(BlockAddr))
  6377. block_stack[block_nested_depth].end_addr =
  6378. (uint8 *)(p - 1);
  6379. }
  6380. break;
  6381. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6382. case WASM_OP_BLOCK:
  6383. case WASM_OP_LOOP:
  6384. case WASM_OP_IF:
  6385. {
  6386. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6387. u8 = read_uint8(p);
  6388. if (is_byte_a_type(u8)) {
  6389. #if WASM_ENABLE_GC != 0
  6390. if (wasm_is_type_multi_byte_type(u8)) {
  6391. /* the possible extra bytes of GC ref type have been
  6392. modified to OP_NOP, no need to resolve them again */
  6393. }
  6394. #endif
  6395. }
  6396. else {
  6397. p--;
  6398. /* block type */
  6399. skip_leb_int32(p, p_end);
  6400. }
  6401. if (block_nested_depth
  6402. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6403. block_stack[block_nested_depth].start_addr = p;
  6404. block_stack[block_nested_depth].else_addr = NULL;
  6405. }
  6406. block_nested_depth++;
  6407. break;
  6408. }
  6409. case EXT_OP_BLOCK:
  6410. case EXT_OP_LOOP:
  6411. case EXT_OP_IF:
  6412. /* block type */
  6413. skip_leb_int32(p, p_end);
  6414. if (block_nested_depth
  6415. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6416. block_stack[block_nested_depth].start_addr = p;
  6417. block_stack[block_nested_depth].else_addr = NULL;
  6418. }
  6419. block_nested_depth++;
  6420. break;
  6421. case WASM_OP_ELSE:
  6422. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6423. else_addr = (uint8 *)(p - 1);
  6424. if (block_nested_depth - 1
  6425. < sizeof(block_stack) / sizeof(BlockAddr))
  6426. block_stack[block_nested_depth - 1].else_addr =
  6427. (uint8 *)(p - 1);
  6428. break;
  6429. case WASM_OP_END:
  6430. if (block_nested_depth == 1) {
  6431. if (label_type == LABEL_TYPE_IF)
  6432. *p_else_addr = else_addr;
  6433. *p_end_addr = (uint8 *)(p - 1);
  6434. block_stack[0].end_addr = (uint8 *)(p - 1);
  6435. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6436. t++) {
  6437. start_addr = block_stack[t].start_addr;
  6438. if (start_addr) {
  6439. i = ((uintptr_t)start_addr)
  6440. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6441. block =
  6442. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6443. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6444. if (!block[j].start_addr)
  6445. break;
  6446. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6447. memmove(block + 1, block,
  6448. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6449. * sizeof(BlockAddr));
  6450. j = 0;
  6451. }
  6452. block[j].start_addr = block_stack[t].start_addr;
  6453. block[j].else_addr = block_stack[t].else_addr;
  6454. block[j].end_addr = block_stack[t].end_addr;
  6455. }
  6456. else
  6457. break;
  6458. }
  6459. return true;
  6460. }
  6461. else {
  6462. block_nested_depth--;
  6463. if (block_nested_depth
  6464. < sizeof(block_stack) / sizeof(BlockAddr))
  6465. block_stack[block_nested_depth].end_addr =
  6466. (uint8 *)(p - 1);
  6467. }
  6468. break;
  6469. case WASM_OP_BR:
  6470. case WASM_OP_BR_IF:
  6471. skip_leb_uint32(p, p_end); /* labelidx */
  6472. break;
  6473. case WASM_OP_BR_TABLE:
  6474. read_leb_uint32(p, p_end, count); /* label num */
  6475. #if WASM_ENABLE_FAST_INTERP != 0
  6476. for (i = 0; i <= count; i++) /* labelidxs */
  6477. skip_leb_uint32(p, p_end);
  6478. #else
  6479. p += count + 1;
  6480. while (*p == WASM_OP_NOP)
  6481. p++;
  6482. #endif
  6483. break;
  6484. #if WASM_ENABLE_FAST_INTERP == 0
  6485. case EXT_OP_BR_TABLE_CACHE:
  6486. read_leb_uint32(p, p_end, count); /* label num */
  6487. while (*p == WASM_OP_NOP)
  6488. p++;
  6489. break;
  6490. #endif
  6491. case WASM_OP_RETURN:
  6492. break;
  6493. case WASM_OP_CALL:
  6494. #if WASM_ENABLE_TAIL_CALL != 0
  6495. case WASM_OP_RETURN_CALL:
  6496. #endif
  6497. skip_leb_uint32(p, p_end); /* funcidx */
  6498. break;
  6499. case WASM_OP_CALL_INDIRECT:
  6500. #if WASM_ENABLE_TAIL_CALL != 0
  6501. case WASM_OP_RETURN_CALL_INDIRECT:
  6502. #endif
  6503. skip_leb_uint32(p, p_end); /* typeidx */
  6504. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6505. skip_leb_uint32(p, p_end); /* tableidx */
  6506. #else
  6507. u8 = read_uint8(p); /* 0x00 */
  6508. #endif
  6509. break;
  6510. #if WASM_ENABLE_GC != 0
  6511. case WASM_OP_CALL_REF:
  6512. case WASM_OP_RETURN_CALL_REF:
  6513. skip_leb_uint32(p, p_end); /* typeidx */
  6514. break;
  6515. #endif
  6516. case WASM_OP_DROP:
  6517. case WASM_OP_SELECT:
  6518. case WASM_OP_DROP_64:
  6519. case WASM_OP_SELECT_64:
  6520. break;
  6521. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6522. case WASM_OP_SELECT_T:
  6523. {
  6524. skip_leb_uint32(p, p_end); /* vec length */
  6525. u8 = read_uint8(p); /* typeidx */
  6526. /* the possible extra bytes of GC ref type have been
  6527. modified to OP_NOP, no need to resolve them again */
  6528. break;
  6529. }
  6530. case WASM_OP_TABLE_GET:
  6531. case WASM_OP_TABLE_SET:
  6532. skip_leb_uint32(p, p_end); /* table index */
  6533. break;
  6534. case WASM_OP_REF_NULL:
  6535. {
  6536. u8 = read_uint8(p); /* type */
  6537. if (is_byte_a_type(u8)) {
  6538. #if WASM_ENABLE_GC != 0
  6539. if (wasm_is_type_multi_byte_type(u8)) {
  6540. /* the possible extra bytes of GC ref type have been
  6541. modified to OP_NOP, no need to resolve them again */
  6542. }
  6543. #endif
  6544. }
  6545. else {
  6546. p--;
  6547. skip_leb_uint32(p, p_end);
  6548. }
  6549. break;
  6550. }
  6551. case WASM_OP_REF_IS_NULL:
  6552. break;
  6553. case WASM_OP_REF_FUNC:
  6554. skip_leb_uint32(p, p_end); /* func index */
  6555. break;
  6556. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6557. #if WASM_ENABLE_GC != 0
  6558. case WASM_OP_REF_AS_NON_NULL:
  6559. case WASM_OP_REF_EQ:
  6560. break;
  6561. case WASM_OP_BR_ON_NULL:
  6562. case WASM_OP_BR_ON_NON_NULL:
  6563. skip_leb_uint32(p, p_end); /* label index */
  6564. break;
  6565. #endif /* end of WASM_ENABLE_GC != 0 */
  6566. case WASM_OP_GET_LOCAL:
  6567. case WASM_OP_SET_LOCAL:
  6568. case WASM_OP_TEE_LOCAL:
  6569. case WASM_OP_GET_GLOBAL:
  6570. case WASM_OP_SET_GLOBAL:
  6571. case WASM_OP_GET_GLOBAL_64:
  6572. case WASM_OP_SET_GLOBAL_64:
  6573. #if WASM_ENABLE_SIMDE != 0
  6574. case WASM_OP_GET_GLOBAL_V128:
  6575. case WASM_OP_SET_GLOBAL_V128:
  6576. #endif
  6577. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6578. skip_leb_uint32(p, p_end); /* local index */
  6579. break;
  6580. case EXT_OP_GET_LOCAL_FAST:
  6581. case EXT_OP_SET_LOCAL_FAST:
  6582. case EXT_OP_TEE_LOCAL_FAST:
  6583. CHECK_BUF(p, p_end, 1);
  6584. p++;
  6585. break;
  6586. case WASM_OP_I32_LOAD:
  6587. case WASM_OP_I64_LOAD:
  6588. case WASM_OP_F32_LOAD:
  6589. case WASM_OP_F64_LOAD:
  6590. case WASM_OP_I32_LOAD8_S:
  6591. case WASM_OP_I32_LOAD8_U:
  6592. case WASM_OP_I32_LOAD16_S:
  6593. case WASM_OP_I32_LOAD16_U:
  6594. case WASM_OP_I64_LOAD8_S:
  6595. case WASM_OP_I64_LOAD8_U:
  6596. case WASM_OP_I64_LOAD16_S:
  6597. case WASM_OP_I64_LOAD16_U:
  6598. case WASM_OP_I64_LOAD32_S:
  6599. case WASM_OP_I64_LOAD32_U:
  6600. case WASM_OP_I32_STORE:
  6601. case WASM_OP_I64_STORE:
  6602. case WASM_OP_F32_STORE:
  6603. case WASM_OP_F64_STORE:
  6604. case WASM_OP_I32_STORE8:
  6605. case WASM_OP_I32_STORE16:
  6606. case WASM_OP_I64_STORE8:
  6607. case WASM_OP_I64_STORE16:
  6608. case WASM_OP_I64_STORE32:
  6609. skip_leb_align(p, p_end); /* align */
  6610. skip_leb_mem_offset(p, p_end); /* offset */
  6611. break;
  6612. case WASM_OP_MEMORY_SIZE:
  6613. case WASM_OP_MEMORY_GROW:
  6614. skip_leb_memidx(p, p_end); /* memidx */
  6615. break;
  6616. case WASM_OP_I32_CONST:
  6617. skip_leb_int32(p, p_end);
  6618. break;
  6619. case WASM_OP_I64_CONST:
  6620. skip_leb_int64(p, p_end);
  6621. break;
  6622. case WASM_OP_F32_CONST:
  6623. p += sizeof(float32);
  6624. break;
  6625. case WASM_OP_F64_CONST:
  6626. p += sizeof(float64);
  6627. break;
  6628. case WASM_OP_I32_EQZ:
  6629. case WASM_OP_I32_EQ:
  6630. case WASM_OP_I32_NE:
  6631. case WASM_OP_I32_LT_S:
  6632. case WASM_OP_I32_LT_U:
  6633. case WASM_OP_I32_GT_S:
  6634. case WASM_OP_I32_GT_U:
  6635. case WASM_OP_I32_LE_S:
  6636. case WASM_OP_I32_LE_U:
  6637. case WASM_OP_I32_GE_S:
  6638. case WASM_OP_I32_GE_U:
  6639. case WASM_OP_I64_EQZ:
  6640. case WASM_OP_I64_EQ:
  6641. case WASM_OP_I64_NE:
  6642. case WASM_OP_I64_LT_S:
  6643. case WASM_OP_I64_LT_U:
  6644. case WASM_OP_I64_GT_S:
  6645. case WASM_OP_I64_GT_U:
  6646. case WASM_OP_I64_LE_S:
  6647. case WASM_OP_I64_LE_U:
  6648. case WASM_OP_I64_GE_S:
  6649. case WASM_OP_I64_GE_U:
  6650. case WASM_OP_F32_EQ:
  6651. case WASM_OP_F32_NE:
  6652. case WASM_OP_F32_LT:
  6653. case WASM_OP_F32_GT:
  6654. case WASM_OP_F32_LE:
  6655. case WASM_OP_F32_GE:
  6656. case WASM_OP_F64_EQ:
  6657. case WASM_OP_F64_NE:
  6658. case WASM_OP_F64_LT:
  6659. case WASM_OP_F64_GT:
  6660. case WASM_OP_F64_LE:
  6661. case WASM_OP_F64_GE:
  6662. case WASM_OP_I32_CLZ:
  6663. case WASM_OP_I32_CTZ:
  6664. case WASM_OP_I32_POPCNT:
  6665. case WASM_OP_I32_ADD:
  6666. case WASM_OP_I32_SUB:
  6667. case WASM_OP_I32_MUL:
  6668. case WASM_OP_I32_DIV_S:
  6669. case WASM_OP_I32_DIV_U:
  6670. case WASM_OP_I32_REM_S:
  6671. case WASM_OP_I32_REM_U:
  6672. case WASM_OP_I32_AND:
  6673. case WASM_OP_I32_OR:
  6674. case WASM_OP_I32_XOR:
  6675. case WASM_OP_I32_SHL:
  6676. case WASM_OP_I32_SHR_S:
  6677. case WASM_OP_I32_SHR_U:
  6678. case WASM_OP_I32_ROTL:
  6679. case WASM_OP_I32_ROTR:
  6680. case WASM_OP_I64_CLZ:
  6681. case WASM_OP_I64_CTZ:
  6682. case WASM_OP_I64_POPCNT:
  6683. case WASM_OP_I64_ADD:
  6684. case WASM_OP_I64_SUB:
  6685. case WASM_OP_I64_MUL:
  6686. case WASM_OP_I64_DIV_S:
  6687. case WASM_OP_I64_DIV_U:
  6688. case WASM_OP_I64_REM_S:
  6689. case WASM_OP_I64_REM_U:
  6690. case WASM_OP_I64_AND:
  6691. case WASM_OP_I64_OR:
  6692. case WASM_OP_I64_XOR:
  6693. case WASM_OP_I64_SHL:
  6694. case WASM_OP_I64_SHR_S:
  6695. case WASM_OP_I64_SHR_U:
  6696. case WASM_OP_I64_ROTL:
  6697. case WASM_OP_I64_ROTR:
  6698. case WASM_OP_F32_ABS:
  6699. case WASM_OP_F32_NEG:
  6700. case WASM_OP_F32_CEIL:
  6701. case WASM_OP_F32_FLOOR:
  6702. case WASM_OP_F32_TRUNC:
  6703. case WASM_OP_F32_NEAREST:
  6704. case WASM_OP_F32_SQRT:
  6705. case WASM_OP_F32_ADD:
  6706. case WASM_OP_F32_SUB:
  6707. case WASM_OP_F32_MUL:
  6708. case WASM_OP_F32_DIV:
  6709. case WASM_OP_F32_MIN:
  6710. case WASM_OP_F32_MAX:
  6711. case WASM_OP_F32_COPYSIGN:
  6712. case WASM_OP_F64_ABS:
  6713. case WASM_OP_F64_NEG:
  6714. case WASM_OP_F64_CEIL:
  6715. case WASM_OP_F64_FLOOR:
  6716. case WASM_OP_F64_TRUNC:
  6717. case WASM_OP_F64_NEAREST:
  6718. case WASM_OP_F64_SQRT:
  6719. case WASM_OP_F64_ADD:
  6720. case WASM_OP_F64_SUB:
  6721. case WASM_OP_F64_MUL:
  6722. case WASM_OP_F64_DIV:
  6723. case WASM_OP_F64_MIN:
  6724. case WASM_OP_F64_MAX:
  6725. case WASM_OP_F64_COPYSIGN:
  6726. case WASM_OP_I32_WRAP_I64:
  6727. case WASM_OP_I32_TRUNC_S_F32:
  6728. case WASM_OP_I32_TRUNC_U_F32:
  6729. case WASM_OP_I32_TRUNC_S_F64:
  6730. case WASM_OP_I32_TRUNC_U_F64:
  6731. case WASM_OP_I64_EXTEND_S_I32:
  6732. case WASM_OP_I64_EXTEND_U_I32:
  6733. case WASM_OP_I64_TRUNC_S_F32:
  6734. case WASM_OP_I64_TRUNC_U_F32:
  6735. case WASM_OP_I64_TRUNC_S_F64:
  6736. case WASM_OP_I64_TRUNC_U_F64:
  6737. case WASM_OP_F32_CONVERT_S_I32:
  6738. case WASM_OP_F32_CONVERT_U_I32:
  6739. case WASM_OP_F32_CONVERT_S_I64:
  6740. case WASM_OP_F32_CONVERT_U_I64:
  6741. case WASM_OP_F32_DEMOTE_F64:
  6742. case WASM_OP_F64_CONVERT_S_I32:
  6743. case WASM_OP_F64_CONVERT_U_I32:
  6744. case WASM_OP_F64_CONVERT_S_I64:
  6745. case WASM_OP_F64_CONVERT_U_I64:
  6746. case WASM_OP_F64_PROMOTE_F32:
  6747. case WASM_OP_I32_REINTERPRET_F32:
  6748. case WASM_OP_I64_REINTERPRET_F64:
  6749. case WASM_OP_F32_REINTERPRET_I32:
  6750. case WASM_OP_F64_REINTERPRET_I64:
  6751. case WASM_OP_I32_EXTEND8_S:
  6752. case WASM_OP_I32_EXTEND16_S:
  6753. case WASM_OP_I64_EXTEND8_S:
  6754. case WASM_OP_I64_EXTEND16_S:
  6755. case WASM_OP_I64_EXTEND32_S:
  6756. break;
  6757. #if WASM_ENABLE_GC != 0
  6758. case WASM_OP_GC_PREFIX:
  6759. {
  6760. uint32 opcode1;
  6761. read_leb_uint32(p, p_end, opcode1);
  6762. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6763. is no larger than UINT8_MAX */
  6764. opcode = (uint8)opcode1;
  6765. switch (opcode) {
  6766. case WASM_OP_STRUCT_NEW:
  6767. case WASM_OP_STRUCT_NEW_DEFAULT:
  6768. skip_leb_uint32(p, p_end); /* typeidx */
  6769. break;
  6770. case WASM_OP_STRUCT_GET:
  6771. case WASM_OP_STRUCT_GET_S:
  6772. case WASM_OP_STRUCT_GET_U:
  6773. case WASM_OP_STRUCT_SET:
  6774. skip_leb_uint32(p, p_end); /* typeidx */
  6775. skip_leb_uint32(p, p_end); /* fieldidx */
  6776. break;
  6777. case WASM_OP_ARRAY_NEW:
  6778. case WASM_OP_ARRAY_NEW_DEFAULT:
  6779. case WASM_OP_ARRAY_GET:
  6780. case WASM_OP_ARRAY_GET_S:
  6781. case WASM_OP_ARRAY_GET_U:
  6782. case WASM_OP_ARRAY_SET:
  6783. case WASM_OP_ARRAY_FILL:
  6784. skip_leb_uint32(p, p_end); /* typeidx */
  6785. break;
  6786. case WASM_OP_ARRAY_COPY:
  6787. skip_leb_uint32(p, p_end); /* typeidx1 */
  6788. skip_leb_uint32(p, p_end); /* typeidx2 */
  6789. break;
  6790. case WASM_OP_ARRAY_LEN:
  6791. break;
  6792. case WASM_OP_ARRAY_NEW_FIXED:
  6793. case WASM_OP_ARRAY_NEW_DATA:
  6794. case WASM_OP_ARRAY_NEW_ELEM:
  6795. skip_leb_uint32(p, p_end); /* typeidx */
  6796. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6797. break;
  6798. case WASM_OP_REF_I31:
  6799. case WASM_OP_I31_GET_S:
  6800. case WASM_OP_I31_GET_U:
  6801. break;
  6802. case WASM_OP_REF_TEST:
  6803. case WASM_OP_REF_CAST:
  6804. case WASM_OP_REF_TEST_NULLABLE:
  6805. case WASM_OP_REF_CAST_NULLABLE:
  6806. skip_leb_int32(p, p_end); /* heaptype */
  6807. break;
  6808. case WASM_OP_BR_ON_CAST:
  6809. case WASM_OP_BR_ON_CAST_FAIL:
  6810. p += sizeof(uint8); /* castflag */
  6811. skip_leb_uint32(p, p_end); /* labelidx */
  6812. skip_leb_int32(p, p_end); /* heaptype */
  6813. skip_leb_int32(p, p_end); /* heaptype2 */
  6814. break;
  6815. case WASM_OP_ANY_CONVERT_EXTERN:
  6816. case WASM_OP_EXTERN_CONVERT_ANY:
  6817. break;
  6818. #if WASM_ENABLE_STRINGREF != 0
  6819. case WASM_OP_STRING_NEW_UTF8:
  6820. case WASM_OP_STRING_NEW_WTF16:
  6821. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6822. case WASM_OP_STRING_NEW_WTF8:
  6823. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6824. break;
  6825. case WASM_OP_STRING_CONST:
  6826. skip_leb_int32(p, p_end); /* contents */
  6827. break;
  6828. case WASM_OP_STRING_MEASURE_UTF8:
  6829. case WASM_OP_STRING_MEASURE_WTF8:
  6830. case WASM_OP_STRING_MEASURE_WTF16:
  6831. break;
  6832. case WASM_OP_STRING_ENCODE_UTF8:
  6833. case WASM_OP_STRING_ENCODE_WTF16:
  6834. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6835. case WASM_OP_STRING_ENCODE_WTF8:
  6836. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6837. break;
  6838. case WASM_OP_STRING_CONCAT:
  6839. case WASM_OP_STRING_EQ:
  6840. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6841. case WASM_OP_STRING_AS_WTF8:
  6842. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6843. break;
  6844. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6845. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6846. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6847. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6848. break;
  6849. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6850. case WASM_OP_STRING_AS_WTF16:
  6851. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6852. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6853. break;
  6854. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6855. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6856. break;
  6857. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6858. case WASM_OP_STRING_AS_ITER:
  6859. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6860. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6861. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6862. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6863. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6864. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6865. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6866. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6867. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6868. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6869. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6870. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6871. break;
  6872. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6873. default:
  6874. return false;
  6875. }
  6876. break;
  6877. }
  6878. #endif /* end of WASM_ENABLE_GC != 0 */
  6879. case WASM_OP_MISC_PREFIX:
  6880. {
  6881. uint32 opcode1;
  6882. read_leb_uint32(p, p_end, opcode1);
  6883. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6884. is no larger than UINT8_MAX */
  6885. opcode = (uint8)opcode1;
  6886. switch (opcode) {
  6887. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6888. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6889. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6890. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6891. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6892. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6893. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6894. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6895. break;
  6896. #if WASM_ENABLE_BULK_MEMORY != 0
  6897. case WASM_OP_MEMORY_INIT:
  6898. skip_leb_uint32(p, p_end);
  6899. skip_leb_memidx(p, p_end);
  6900. break;
  6901. case WASM_OP_DATA_DROP:
  6902. skip_leb_uint32(p, p_end);
  6903. break;
  6904. case WASM_OP_MEMORY_COPY:
  6905. skip_leb_memidx(p, p_end);
  6906. skip_leb_memidx(p, p_end);
  6907. break;
  6908. case WASM_OP_MEMORY_FILL:
  6909. skip_leb_memidx(p, p_end);
  6910. break;
  6911. #endif /* WASM_ENABLE_BULK_MEMORY */
  6912. #if WASM_ENABLE_REF_TYPES != 0
  6913. case WASM_OP_TABLE_INIT:
  6914. case WASM_OP_TABLE_COPY:
  6915. /* tableidx */
  6916. skip_leb_uint32(p, p_end);
  6917. /* elemidx */
  6918. skip_leb_uint32(p, p_end);
  6919. break;
  6920. case WASM_OP_ELEM_DROP:
  6921. /* elemidx */
  6922. skip_leb_uint32(p, p_end);
  6923. break;
  6924. case WASM_OP_TABLE_SIZE:
  6925. case WASM_OP_TABLE_GROW:
  6926. case WASM_OP_TABLE_FILL:
  6927. skip_leb_uint32(p, p_end); /* table idx */
  6928. break;
  6929. #endif /* WASM_ENABLE_REF_TYPES */
  6930. default:
  6931. return false;
  6932. }
  6933. break;
  6934. }
  6935. #if WASM_ENABLE_SIMD != 0
  6936. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  6937. || (WASM_ENABLE_FAST_INTERP != 0)
  6938. case WASM_OP_SIMD_PREFIX:
  6939. {
  6940. uint32 opcode1;
  6941. read_leb_uint32(p, p_end, opcode1);
  6942. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6943. is no larger than UINT8_MAX */
  6944. opcode = (uint8)opcode1;
  6945. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6946. */
  6947. switch (opcode) {
  6948. case SIMD_v128_load:
  6949. case SIMD_v128_load8x8_s:
  6950. case SIMD_v128_load8x8_u:
  6951. case SIMD_v128_load16x4_s:
  6952. case SIMD_v128_load16x4_u:
  6953. case SIMD_v128_load32x2_s:
  6954. case SIMD_v128_load32x2_u:
  6955. case SIMD_v128_load8_splat:
  6956. case SIMD_v128_load16_splat:
  6957. case SIMD_v128_load32_splat:
  6958. case SIMD_v128_load64_splat:
  6959. case SIMD_v128_store:
  6960. /* memarg align */
  6961. skip_leb_uint32(p, p_end);
  6962. /* memarg offset */
  6963. skip_leb_mem_offset(p, p_end);
  6964. break;
  6965. case SIMD_v128_const:
  6966. case SIMD_v8x16_shuffle:
  6967. /* immByte[16] immLaneId[16] */
  6968. CHECK_BUF1(p, p_end, 16);
  6969. p += 16;
  6970. break;
  6971. case SIMD_i8x16_extract_lane_s:
  6972. case SIMD_i8x16_extract_lane_u:
  6973. case SIMD_i8x16_replace_lane:
  6974. case SIMD_i16x8_extract_lane_s:
  6975. case SIMD_i16x8_extract_lane_u:
  6976. case SIMD_i16x8_replace_lane:
  6977. case SIMD_i32x4_extract_lane:
  6978. case SIMD_i32x4_replace_lane:
  6979. case SIMD_i64x2_extract_lane:
  6980. case SIMD_i64x2_replace_lane:
  6981. case SIMD_f32x4_extract_lane:
  6982. case SIMD_f32x4_replace_lane:
  6983. case SIMD_f64x2_extract_lane:
  6984. case SIMD_f64x2_replace_lane:
  6985. /* ImmLaneId */
  6986. CHECK_BUF(p, p_end, 1);
  6987. p++;
  6988. break;
  6989. case SIMD_v128_load8_lane:
  6990. case SIMD_v128_load16_lane:
  6991. case SIMD_v128_load32_lane:
  6992. case SIMD_v128_load64_lane:
  6993. case SIMD_v128_store8_lane:
  6994. case SIMD_v128_store16_lane:
  6995. case SIMD_v128_store32_lane:
  6996. case SIMD_v128_store64_lane:
  6997. /* memarg align */
  6998. skip_leb_uint32(p, p_end);
  6999. /* memarg offset */
  7000. skip_leb_mem_offset(p, p_end);
  7001. /* ImmLaneId */
  7002. CHECK_BUF(p, p_end, 1);
  7003. p++;
  7004. break;
  7005. case SIMD_v128_load32_zero:
  7006. case SIMD_v128_load64_zero:
  7007. /* memarg align */
  7008. skip_leb_uint32(p, p_end);
  7009. /* memarg offset */
  7010. skip_leb_mem_offset(p, p_end);
  7011. break;
  7012. default:
  7013. /*
  7014. * since latest SIMD specific used almost every value
  7015. * from 0x00 to 0xff, the default branch will present
  7016. * all opcodes without imm
  7017. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  7018. */
  7019. break;
  7020. }
  7021. break;
  7022. }
  7023. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  7024. (WASM_ENABLE_FAST_INTERP != 0) */
  7025. #endif /* end of WASM_ENABLE_SIMD */
  7026. #if WASM_ENABLE_SHARED_MEMORY != 0
  7027. case WASM_OP_ATOMIC_PREFIX:
  7028. {
  7029. uint32 opcode1;
  7030. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  7031. read_leb_uint32(p, p_end, opcode1);
  7032. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7033. is no larger than UINT8_MAX */
  7034. opcode = (uint8)opcode1;
  7035. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7036. skip_leb_uint32(p, p_end); /* align */
  7037. skip_leb_mem_offset(p, p_end); /* offset */
  7038. }
  7039. else {
  7040. /* atomic.fence doesn't have memarg */
  7041. p++;
  7042. }
  7043. break;
  7044. }
  7045. #endif
  7046. #if WASM_ENABLE_DEBUG_INTERP != 0
  7047. case DEBUG_OP_BREAK:
  7048. {
  7049. WASMDebugInstance *debug_instance =
  7050. wasm_exec_env_get_instance(exec_env);
  7051. char original_opcode[1];
  7052. uint64 size = 1;
  7053. WASMModuleInstance *module_inst =
  7054. (WASMModuleInstance *)exec_env->module_inst;
  7055. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7056. ? (p - 1) - module_inst->module->load_addr
  7057. : ~0;
  7058. if (debug_instance) {
  7059. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7060. original_opcode, &size)
  7061. && size == 1) {
  7062. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7063. "with %02x: ",
  7064. original_opcode[0]);
  7065. opcode = original_opcode[0];
  7066. goto op_break_retry;
  7067. }
  7068. }
  7069. break;
  7070. }
  7071. #endif
  7072. default:
  7073. return false;
  7074. }
  7075. }
  7076. (void)u8;
  7077. (void)exec_env;
  7078. return false;
  7079. fail:
  7080. return false;
  7081. }
  7082. #if WASM_ENABLE_FAST_INTERP != 0
  7083. #if WASM_DEBUG_PREPROCESSOR != 0
  7084. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7085. #else
  7086. #define LOG_OP(...) (void)0
  7087. #endif
  7088. #define PATCH_ELSE 0
  7089. #define PATCH_END 1
  7090. typedef struct BranchBlockPatch {
  7091. struct BranchBlockPatch *next;
  7092. uint8 patch_type;
  7093. uint8 *code_compiled;
  7094. } BranchBlockPatch;
  7095. #endif
  7096. typedef struct BranchBlock {
  7097. uint8 label_type;
  7098. BlockType block_type;
  7099. uint8 *start_addr;
  7100. uint8 *else_addr;
  7101. uint8 *end_addr;
  7102. uint32 stack_cell_num;
  7103. #if WASM_ENABLE_GC != 0
  7104. uint32 reftype_map_num;
  7105. /* Indicate which local is used inside current block, used to validate
  7106. * local.get with non-nullable ref types */
  7107. uint8 *local_use_mask;
  7108. uint32 local_use_mask_size;
  7109. #endif
  7110. #if WASM_ENABLE_FAST_INTERP != 0
  7111. uint16 dynamic_offset;
  7112. uint8 *code_compiled;
  7113. BranchBlockPatch *patch_list;
  7114. /* This is used to save params frame_offset of of if block */
  7115. int16 *param_frame_offsets;
  7116. /* This is used to recover the dynamic offset for else branch,
  7117. * and also to remember the start offset of dynamic space which
  7118. * stores the block arguments for loop block, so we can use it
  7119. * to copy the stack operands to the loop block's arguments in
  7120. * wasm_loader_emit_br_info for opcode br. */
  7121. uint16 start_dynamic_offset;
  7122. #endif
  7123. /* Indicate the operand stack is in polymorphic state.
  7124. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7125. * to polymorphic state until the block's 'end' opcode is processed.
  7126. * If stack is in polymorphic state and stack is empty, instruction can
  7127. * pop any type of value directly without decreasing stack top pointer
  7128. * and stack cell num. */
  7129. bool is_stack_polymorphic;
  7130. } BranchBlock;
  7131. typedef struct WASMLoaderContext {
  7132. /* frame ref stack */
  7133. uint8 *frame_ref;
  7134. uint8 *frame_ref_bottom;
  7135. uint8 *frame_ref_boundary;
  7136. uint32 frame_ref_size;
  7137. uint32 stack_cell_num;
  7138. uint32 max_stack_cell_num;
  7139. #if WASM_ENABLE_GC != 0
  7140. /* frame reftype map stack */
  7141. WASMRefTypeMap *frame_reftype_map;
  7142. WASMRefTypeMap *frame_reftype_map_bottom;
  7143. WASMRefTypeMap *frame_reftype_map_boundary;
  7144. uint32 frame_reftype_map_size;
  7145. uint32 reftype_map_num;
  7146. uint32 max_reftype_map_num;
  7147. /* Current module */
  7148. WASMModule *module;
  7149. /* Current module's ref_type_set */
  7150. HashMap *ref_type_set;
  7151. /* Always point to local variable ref_type of
  7152. wasm_loader_prepare_bytecode */
  7153. WASMRefType *ref_type_tmp;
  7154. #endif
  7155. /* frame csp stack */
  7156. BranchBlock *frame_csp;
  7157. BranchBlock *frame_csp_bottom;
  7158. BranchBlock *frame_csp_boundary;
  7159. uint32 frame_csp_size;
  7160. uint32 csp_num;
  7161. uint32 max_csp_num;
  7162. #if WASM_ENABLE_FAST_INTERP != 0
  7163. /* frame offset stack */
  7164. int16 *frame_offset;
  7165. int16 *frame_offset_bottom;
  7166. int16 *frame_offset_boundary;
  7167. uint32 frame_offset_size;
  7168. int16 dynamic_offset;
  7169. int16 start_dynamic_offset;
  7170. int16 max_dynamic_offset;
  7171. /* preserved local offset */
  7172. int16 preserved_local_offset;
  7173. /* const buffer for i64 and f64 consts, note that the raw bytes
  7174. * of i64 and f64 are the same, so we read an i64 value from an
  7175. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7176. int64 *i64_consts;
  7177. uint32 i64_const_max_num;
  7178. uint32 i64_const_num;
  7179. /* const buffer for i32 and f32 consts */
  7180. int32 *i32_consts;
  7181. uint32 i32_const_max_num;
  7182. uint32 i32_const_num;
  7183. /* const buffer for V128 */
  7184. V128 *v128_consts;
  7185. uint32 v128_const_max_num;
  7186. uint32 v128_const_num;
  7187. /* processed code */
  7188. uint8 *p_code_compiled;
  7189. uint8 *p_code_compiled_end;
  7190. uint32 code_compiled_size;
  7191. /* If the last opcode will be dropped, the peak memory usage will be larger
  7192. * than the final code_compiled_size, we record the peak size to ensure
  7193. * there will not be invalid memory access during second traverse */
  7194. uint32 code_compiled_peak_size;
  7195. #endif
  7196. } WASMLoaderContext;
  7197. #define CHECK_CSP_PUSH() \
  7198. do { \
  7199. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7200. MEM_REALLOC( \
  7201. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7202. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7203. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7204. ctx->frame_csp_boundary = \
  7205. ctx->frame_csp_bottom \
  7206. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7207. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7208. } \
  7209. } while (0)
  7210. #define CHECK_CSP_POP() \
  7211. do { \
  7212. if (ctx->csp_num < 1) { \
  7213. set_error_buf(error_buf, error_buf_size, \
  7214. "type mismatch: " \
  7215. "expect data but block stack was empty"); \
  7216. goto fail; \
  7217. } \
  7218. } while (0)
  7219. #if WASM_ENABLE_FAST_INTERP != 0
  7220. static bool
  7221. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7222. uint32 error_buf_size)
  7223. {
  7224. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7225. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7226. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7227. ctx->frame_offset_size + 16);
  7228. ctx->frame_offset_size += 16;
  7229. ctx->frame_offset_boundary =
  7230. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7231. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7232. }
  7233. return true;
  7234. fail:
  7235. return false;
  7236. }
  7237. static bool
  7238. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7239. {
  7240. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7241. return false;
  7242. return true;
  7243. }
  7244. static void
  7245. free_label_patch_list(BranchBlock *frame_csp)
  7246. {
  7247. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7248. BranchBlockPatch *next;
  7249. while (label_patch != NULL) {
  7250. next = label_patch->next;
  7251. wasm_runtime_free(label_patch);
  7252. label_patch = next;
  7253. }
  7254. frame_csp->patch_list = NULL;
  7255. }
  7256. static void
  7257. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7258. {
  7259. BranchBlock *tmp_csp = frame_csp;
  7260. uint32 i;
  7261. for (i = 0; i < csp_num; i++) {
  7262. free_label_patch_list(tmp_csp);
  7263. tmp_csp++;
  7264. }
  7265. }
  7266. static void
  7267. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7268. {
  7269. BranchBlock *tmp_csp = frame_csp;
  7270. uint32 i;
  7271. for (i = 0; i < csp_num; i++) {
  7272. if (tmp_csp->param_frame_offsets)
  7273. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7274. tmp_csp++;
  7275. }
  7276. }
  7277. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7278. #if WASM_ENABLE_GC != 0
  7279. static bool
  7280. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7281. char *error_buf, uint32 error_buf_size)
  7282. {
  7283. BranchBlock *current_csp = ctx->frame_csp - 1;
  7284. uint32 local_mask_size;
  7285. if (local_count == 0) {
  7286. current_csp->local_use_mask_size = 0;
  7287. return true;
  7288. }
  7289. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7290. * else branch, we don't need to allocate memory again */
  7291. if (!current_csp->local_use_mask) {
  7292. local_mask_size = (local_count + 7) / sizeof(uint8);
  7293. if (!(current_csp->local_use_mask =
  7294. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7295. return false;
  7296. }
  7297. current_csp->local_use_mask_size = local_mask_size;
  7298. }
  7299. else {
  7300. local_mask_size = current_csp->local_use_mask_size;
  7301. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7302. }
  7303. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7304. /* For non-function blocks, inherit the use status from parent block */
  7305. BranchBlock *parent_csp = current_csp - 1;
  7306. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7307. bh_assert(parent_csp->local_use_mask);
  7308. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7309. parent_csp->local_use_mask, local_mask_size);
  7310. }
  7311. return true;
  7312. }
  7313. static void
  7314. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7315. {
  7316. BranchBlock *current_csp = ctx->frame_csp - 1;
  7317. bh_assert(current_csp->local_use_mask
  7318. || current_csp->local_use_mask_size == 0);
  7319. if (current_csp->local_use_mask) {
  7320. wasm_runtime_free(current_csp->local_use_mask);
  7321. }
  7322. current_csp->local_use_mask = NULL;
  7323. current_csp->local_use_mask_size = 0;
  7324. }
  7325. static void
  7326. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7327. {
  7328. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7329. uint32 i;
  7330. for (i = 0; i < ctx->csp_num; i++) {
  7331. if (tmp_csp->local_use_mask) {
  7332. wasm_runtime_free(tmp_csp->local_use_mask);
  7333. tmp_csp->local_use_mask = NULL;
  7334. tmp_csp->local_use_mask_size = 0;
  7335. }
  7336. tmp_csp++;
  7337. }
  7338. }
  7339. static void
  7340. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7341. {
  7342. BranchBlock *current_csp = ctx->frame_csp - 1;
  7343. uint32 byte_offset = index / sizeof(uint8);
  7344. uint32 bit_offset = index % sizeof(uint8);
  7345. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7346. bh_assert(current_csp->local_use_mask);
  7347. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7348. }
  7349. static bool
  7350. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7351. {
  7352. BranchBlock *current_csp = ctx->frame_csp - 1;
  7353. uint32 byte_offset = index / sizeof(uint8);
  7354. uint32 bit_offset = index % sizeof(uint8);
  7355. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7356. bh_assert(current_csp->local_use_mask);
  7357. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7358. ? true
  7359. : false;
  7360. }
  7361. #endif /* end of WASM_ENABLE_GC != 0 */
  7362. static void
  7363. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7364. {
  7365. if (ctx) {
  7366. if (ctx->frame_ref_bottom)
  7367. wasm_runtime_free(ctx->frame_ref_bottom);
  7368. #if WASM_ENABLE_GC != 0
  7369. if (ctx->frame_reftype_map_bottom)
  7370. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7371. #endif
  7372. if (ctx->frame_csp_bottom) {
  7373. #if WASM_ENABLE_FAST_INTERP != 0
  7374. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7375. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7376. ctx->csp_num);
  7377. #endif
  7378. #if WASM_ENABLE_GC != 0
  7379. wasm_loader_clean_all_local_use_masks(ctx);
  7380. #endif
  7381. wasm_runtime_free(ctx->frame_csp_bottom);
  7382. }
  7383. #if WASM_ENABLE_FAST_INTERP != 0
  7384. if (ctx->frame_offset_bottom)
  7385. wasm_runtime_free(ctx->frame_offset_bottom);
  7386. if (ctx->i64_consts)
  7387. wasm_runtime_free(ctx->i64_consts);
  7388. if (ctx->i32_consts)
  7389. wasm_runtime_free(ctx->i32_consts);
  7390. if (ctx->v128_consts)
  7391. wasm_runtime_free(ctx->v128_consts);
  7392. #endif
  7393. wasm_runtime_free(ctx);
  7394. }
  7395. }
  7396. static WASMLoaderContext *
  7397. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7398. {
  7399. WASMLoaderContext *loader_ctx =
  7400. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7401. if (!loader_ctx)
  7402. return NULL;
  7403. loader_ctx->frame_ref_size = 32;
  7404. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7405. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7406. goto fail;
  7407. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7408. #if WASM_ENABLE_GC != 0
  7409. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7410. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7411. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7412. error_buf_size)))
  7413. goto fail;
  7414. loader_ctx->frame_reftype_map_boundary =
  7415. loader_ctx->frame_reftype_map_bottom + 16;
  7416. #endif
  7417. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7418. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7419. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7420. goto fail;
  7421. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7422. #if WASM_ENABLE_EXCE_HANDLING != 0
  7423. func->exception_handler_count = 0;
  7424. #endif
  7425. #if WASM_ENABLE_FAST_INTERP != 0
  7426. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7427. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7428. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7429. error_buf_size)))
  7430. goto fail;
  7431. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7432. loader_ctx->i64_const_max_num = 8;
  7433. if (!(loader_ctx->i64_consts =
  7434. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7435. error_buf, error_buf_size)))
  7436. goto fail;
  7437. loader_ctx->i32_const_max_num = 8;
  7438. if (!(loader_ctx->i32_consts =
  7439. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7440. error_buf, error_buf_size)))
  7441. goto fail;
  7442. loader_ctx->v128_const_max_num = 8;
  7443. if (!(loader_ctx->v128_consts =
  7444. loader_malloc(sizeof(V128) * loader_ctx->v128_const_max_num,
  7445. error_buf, error_buf_size)))
  7446. goto fail;
  7447. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7448. set_error_buf(error_buf, error_buf_size,
  7449. "fast interpreter offset overflow");
  7450. goto fail;
  7451. }
  7452. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7453. loader_ctx->max_dynamic_offset =
  7454. func->param_cell_num + func->local_cell_num;
  7455. #endif
  7456. return loader_ctx;
  7457. fail:
  7458. wasm_loader_ctx_destroy(loader_ctx);
  7459. return NULL;
  7460. }
  7461. static bool
  7462. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7463. uint32 error_buf_size)
  7464. {
  7465. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7466. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7467. /* Increase the frame ref stack */
  7468. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7469. ctx->frame_ref_size + 16);
  7470. ctx->frame_ref_size += 16;
  7471. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7472. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7473. }
  7474. #if WASM_ENABLE_GC != 0
  7475. if (wasm_is_type_multi_byte_type(type)
  7476. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7477. /* Increase the frame reftype map stack */
  7478. bh_assert(
  7479. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7480. * sizeof(WASMRefTypeMap))
  7481. == ctx->frame_reftype_map_size);
  7482. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7483. ctx->frame_reftype_map_size
  7484. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7485. ctx->frame_reftype_map =
  7486. ctx->frame_reftype_map_bottom
  7487. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7488. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7489. ctx->frame_reftype_map_boundary =
  7490. ctx->frame_reftype_map_bottom
  7491. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7492. }
  7493. #endif
  7494. return true;
  7495. fail:
  7496. return false;
  7497. }
  7498. static bool
  7499. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7500. uint32 error_buf_size)
  7501. {
  7502. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7503. uint32 i;
  7504. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7505. return false;
  7506. #if WASM_ENABLE_GC != 0
  7507. if (wasm_is_type_multi_byte_type(type)) {
  7508. WASMRefType *ref_type;
  7509. if (!(ref_type =
  7510. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7511. error_buf, error_buf_size))) {
  7512. return false;
  7513. }
  7514. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7515. /* Increase the frame reftype map stack */
  7516. bh_assert((uint32)((ctx->frame_reftype_map
  7517. - ctx->frame_reftype_map_bottom)
  7518. * sizeof(WASMRefTypeMap))
  7519. == ctx->frame_reftype_map_size);
  7520. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7521. ctx->frame_reftype_map_size,
  7522. ctx->frame_reftype_map_size
  7523. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7524. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7525. + ctx->frame_reftype_map_size
  7526. / ((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
  7531. / ((uint32)sizeof(WASMRefTypeMap));
  7532. }
  7533. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7534. ctx->frame_reftype_map->ref_type = ref_type;
  7535. ctx->frame_reftype_map++;
  7536. ctx->reftype_map_num++;
  7537. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7538. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7539. }
  7540. #endif
  7541. for (i = 0; i < type_cell_num; i++)
  7542. *ctx->frame_ref++ = type;
  7543. ctx->stack_cell_num += type_cell_num;
  7544. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7545. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7546. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7547. set_error_buf(error_buf, error_buf_size,
  7548. "operand stack depth limit exceeded");
  7549. return false;
  7550. }
  7551. }
  7552. return true;
  7553. #if WASM_ENABLE_GC != 0
  7554. fail:
  7555. return false;
  7556. #endif
  7557. }
  7558. static bool
  7559. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7560. int32 stack_cell_num,
  7561. #if WASM_ENABLE_GC != 0
  7562. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7563. #endif
  7564. uint8 type,
  7565. #if WASM_ENABLE_GC != 0
  7566. WASMRefType *ref_type,
  7567. #endif
  7568. char *error_buf, uint32 error_buf_size)
  7569. {
  7570. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7571. #if WASM_ENABLE_GC != 0
  7572. WASMRefType *frame_reftype = NULL;
  7573. #endif
  7574. if (stack_cell_num < type_cell_num) {
  7575. set_error_buf(error_buf, error_buf_size,
  7576. "type mismatch: expect data but stack was empty");
  7577. return false;
  7578. }
  7579. #if WASM_ENABLE_GC == 0
  7580. for (i = 0; i < type_cell_num; i++) {
  7581. if (*(frame_ref - 1 - i) != type) {
  7582. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7583. "type mismatch: expect ", type2str(type),
  7584. " but got other");
  7585. return false;
  7586. }
  7587. }
  7588. #else
  7589. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7590. bh_assert(reftype_map_num > 0);
  7591. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7592. }
  7593. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7594. ref_type, ctx->module->types,
  7595. ctx->module->type_count)) {
  7596. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7597. "type mismatch: expect ", type2str(type),
  7598. " but got other");
  7599. return false;
  7600. }
  7601. for (i = 0; i < type_cell_num - 1; i++) {
  7602. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7603. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7604. "type mismatch: expect ", type2str(type),
  7605. " but got other");
  7606. return false;
  7607. }
  7608. }
  7609. #endif
  7610. return true;
  7611. }
  7612. static bool
  7613. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7614. uint32 error_buf_size)
  7615. {
  7616. int32 block_stack_cell_num =
  7617. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7618. #if WASM_ENABLE_GC != 0
  7619. int32 reftype_map_num =
  7620. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7621. #endif
  7622. if (block_stack_cell_num > 0) {
  7623. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7624. /* the stack top is a value of any type, return success */
  7625. return true;
  7626. }
  7627. #if WASM_ENABLE_GC != 0
  7628. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7629. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7630. WASMRefType *stack_top_ref_type = NULL;
  7631. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7632. bh_assert(reftype_map_num > 0);
  7633. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7634. }
  7635. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7636. ctx->ref_type_tmp, ctx->module->types,
  7637. ctx->module->type_count)) {
  7638. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7639. uint32 ref_type_struct_size =
  7640. wasm_reftype_struct_size(stack_top_ref_type);
  7641. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7642. stack_top_ref_type, ref_type_struct_size);
  7643. }
  7644. return true;
  7645. }
  7646. }
  7647. #endif
  7648. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7649. #if WASM_ENABLE_GC != 0
  7650. ctx->frame_reftype_map, reftype_map_num,
  7651. #endif
  7652. type,
  7653. #if WASM_ENABLE_GC != 0
  7654. ctx->ref_type_tmp,
  7655. #endif
  7656. error_buf, error_buf_size)) {
  7657. return false;
  7658. }
  7659. return true;
  7660. }
  7661. static bool
  7662. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7663. uint32 error_buf_size)
  7664. {
  7665. BranchBlock *cur_block = ctx->frame_csp - 1;
  7666. int32 available_stack_cell =
  7667. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7668. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7669. /* Directly return success if current block is in stack
  7670. polymorphic state while stack is empty. */
  7671. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7672. return true;
  7673. if (type == VALUE_TYPE_VOID)
  7674. return true;
  7675. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7676. return false;
  7677. bh_assert(available_stack_cell > 0);
  7678. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7679. type = VALUE_TYPE_ANY;
  7680. cell_num_to_pop = 1;
  7681. }
  7682. ctx->frame_ref -= cell_num_to_pop;
  7683. ctx->stack_cell_num -= cell_num_to_pop;
  7684. #if WASM_ENABLE_GC != 0
  7685. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7686. ctx->frame_reftype_map--;
  7687. ctx->reftype_map_num--;
  7688. }
  7689. #endif
  7690. return true;
  7691. }
  7692. #if WASM_ENABLE_GC != 0
  7693. /* Get the stack top element of current block */
  7694. static bool
  7695. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7696. WASMRefType **p_ref_type, char *error_buf,
  7697. uint32 error_buf_size)
  7698. {
  7699. BranchBlock *cur_block = ctx->frame_csp - 1;
  7700. int32 available_stack_cell =
  7701. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7702. if (available_stack_cell <= 0) {
  7703. /* Directly return success if current block is in stack
  7704. polymorphic state while stack is empty. */
  7705. if (cur_block->is_stack_polymorphic) {
  7706. *p_type = VALUE_TYPE_ANY;
  7707. return true;
  7708. }
  7709. else {
  7710. set_error_buf(
  7711. error_buf, error_buf_size,
  7712. "type mismatch: expect data but block stack was empty");
  7713. return false;
  7714. }
  7715. }
  7716. *p_type = *(ctx->frame_ref - 1);
  7717. if (wasm_is_type_multi_byte_type(*p_type)) {
  7718. int32 available_reftype_map =
  7719. (int32)(ctx->reftype_map_num
  7720. - (ctx->frame_csp - 1)->reftype_map_num);
  7721. bh_assert(available_reftype_map > 0);
  7722. (void)available_reftype_map;
  7723. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7724. }
  7725. return true;
  7726. }
  7727. #if WASM_ENABLE_FAST_INTERP != 0
  7728. static bool
  7729. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7730. char *error_buf, uint32 error_buf_size);
  7731. #endif
  7732. /* Check whether the stack top elem is a heap object, and if yes,
  7733. pop and return it */
  7734. static bool
  7735. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7736. WASMRefType *ref_ht_ret, char *error_buf,
  7737. uint32 error_buf_size)
  7738. {
  7739. uint8 type = 0;
  7740. WASMRefType *ref_type = NULL;
  7741. /* Get stack top element */
  7742. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7743. error_buf_size)) {
  7744. return false;
  7745. }
  7746. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7747. /* stack top isn't a ref type */
  7748. && !wasm_is_type_reftype(type)) {
  7749. set_error_buf(error_buf, error_buf_size,
  7750. "type mismatch: expect heap object but got others");
  7751. return false;
  7752. }
  7753. /* POP stack top */
  7754. if (wasm_is_type_multi_byte_type(type)) {
  7755. bh_assert(ref_type);
  7756. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7757. wasm_reftype_struct_size(ref_type));
  7758. }
  7759. #if WASM_ENABLE_FAST_INTERP != 0
  7760. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7761. error_buf_size)) {
  7762. return false;
  7763. }
  7764. #else
  7765. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7766. return false;
  7767. }
  7768. #endif
  7769. if (p_type)
  7770. *p_type = type;
  7771. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7772. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7773. wasm_reftype_struct_size(ref_type));
  7774. }
  7775. return true;
  7776. }
  7777. /* Check whether the stack top elem is subtype of (ref null ht),
  7778. and if yes, pop it and return the converted (ref ht) */
  7779. static bool
  7780. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7781. WASMRefType *ref_ht_ret, char *error_buf,
  7782. uint32 error_buf_size)
  7783. {
  7784. uint8 type = 0;
  7785. WASMRefType ref_type = { 0 };
  7786. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7787. error_buf_size)) {
  7788. return false;
  7789. }
  7790. /* Convert to related (ref ht) and return */
  7791. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7792. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7793. */
  7794. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7795. HEAP_TYPE_ARRAY
  7796. + (type - REF_TYPE_ARRAYREF));
  7797. type = ref_ht_ret->ref_type;
  7798. }
  7799. else if (wasm_is_reftype_htref_nullable(type)
  7800. || wasm_is_reftype_htref_non_nullable(type)) {
  7801. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7802. wasm_reftype_struct_size(&ref_type));
  7803. /* Convert to (ref ht) */
  7804. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7805. ref_ht_ret->ref_ht_common.nullable = false;
  7806. type = ref_ht_ret->ref_type;
  7807. }
  7808. *p_type = type;
  7809. return true;
  7810. }
  7811. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7812. and if yes, pop it and return the type_idx */
  7813. static bool
  7814. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7815. uint32 *p_type_idx, char *error_buf,
  7816. uint32 error_buf_size)
  7817. {
  7818. uint8 type = 0;
  7819. int32 type_idx = -1;
  7820. WASMRefType *ref_type = NULL;
  7821. /* Get stack top element */
  7822. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7823. error_buf_size)) {
  7824. return false;
  7825. }
  7826. if (type != VALUE_TYPE_ANY) {
  7827. /* stack top isn't (ref null $t) */
  7828. if (!((wasm_is_reftype_htref_nullable(type)
  7829. || wasm_is_reftype_htref_non_nullable(type))
  7830. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7831. set_error_buf(error_buf, error_buf_size,
  7832. "type mismatch: expect (ref null $t) but got others");
  7833. return false;
  7834. }
  7835. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7836. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7837. wasm_reftype_struct_size(ref_type));
  7838. }
  7839. /* POP stack top */
  7840. #if WASM_ENABLE_FAST_INTERP != 0
  7841. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7842. error_buf_size)) {
  7843. return false;
  7844. }
  7845. #else
  7846. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7847. return false;
  7848. }
  7849. #endif
  7850. /* Convert to type_idx and return */
  7851. *p_type = type;
  7852. if (type != VALUE_TYPE_ANY)
  7853. *p_type_idx = (uint32)type_idx;
  7854. return true;
  7855. }
  7856. #endif /* WASM_ENABLE_GC != 0 */
  7857. #if WASM_ENABLE_FAST_INTERP == 0
  7858. static bool
  7859. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7860. uint8 type_push, uint8 type_pop, char *error_buf,
  7861. uint32 error_buf_size)
  7862. {
  7863. for (int i = 0; i < pop_cnt; i++) {
  7864. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7865. error_buf_size))
  7866. return false;
  7867. }
  7868. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7869. return false;
  7870. return true;
  7871. }
  7872. #endif
  7873. static bool
  7874. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7875. BlockType block_type, uint8 *start_addr,
  7876. char *error_buf, uint32 error_buf_size)
  7877. {
  7878. CHECK_CSP_PUSH();
  7879. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7880. ctx->frame_csp->label_type = label_type;
  7881. ctx->frame_csp->block_type = block_type;
  7882. ctx->frame_csp->start_addr = start_addr;
  7883. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7884. #if WASM_ENABLE_GC != 0
  7885. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7886. #endif
  7887. #if WASM_ENABLE_FAST_INTERP != 0
  7888. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7889. ctx->frame_csp->patch_list = NULL;
  7890. #endif
  7891. ctx->frame_csp++;
  7892. ctx->csp_num++;
  7893. if (ctx->csp_num > ctx->max_csp_num) {
  7894. ctx->max_csp_num = ctx->csp_num;
  7895. if (ctx->max_csp_num > UINT16_MAX) {
  7896. set_error_buf(error_buf, error_buf_size,
  7897. "label stack depth limit exceeded");
  7898. return false;
  7899. }
  7900. }
  7901. return true;
  7902. fail:
  7903. return false;
  7904. }
  7905. static bool
  7906. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7907. uint32 error_buf_size)
  7908. {
  7909. CHECK_CSP_POP();
  7910. #if WASM_ENABLE_FAST_INTERP != 0
  7911. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7912. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7913. #endif
  7914. ctx->frame_csp--;
  7915. ctx->csp_num--;
  7916. return true;
  7917. fail:
  7918. return false;
  7919. }
  7920. #if WASM_ENABLE_FAST_INTERP != 0
  7921. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7922. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7923. #define emit_label(opcode) \
  7924. do { \
  7925. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7926. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7927. } while (0)
  7928. #define skip_label() \
  7929. do { \
  7930. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7931. LOG_OP("\ndelete last op\n"); \
  7932. } while (0)
  7933. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7934. #if UINTPTR_MAX == UINT64_MAX
  7935. #define emit_label(opcode) \
  7936. do { \
  7937. int32 offset = \
  7938. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7939. /* emit int32 relative offset in 64-bit target */ \
  7940. wasm_loader_emit_uint32(loader_ctx, offset); \
  7941. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7942. } while (0)
  7943. #else
  7944. #define emit_label(opcode) \
  7945. do { \
  7946. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7947. /* emit uint32 label address in 32-bit target */ \
  7948. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7949. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7950. } while (0)
  7951. #endif
  7952. #define skip_label() \
  7953. do { \
  7954. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7955. LOG_OP("\ndelete last op\n"); \
  7956. } while (0)
  7957. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7958. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7959. #define emit_label(opcode) \
  7960. do { \
  7961. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7962. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7963. } while (0)
  7964. #define skip_label() \
  7965. do { \
  7966. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7967. LOG_OP("\ndelete last op\n"); \
  7968. } while (0)
  7969. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7970. #define emit_empty_label_addr_and_frame_ip(type) \
  7971. do { \
  7972. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7973. loader_ctx->p_code_compiled, error_buf, \
  7974. error_buf_size)) \
  7975. goto fail; \
  7976. /* label address, to be patched */ \
  7977. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7978. } while (0)
  7979. #define emit_br_info(frame_csp, is_br) \
  7980. do { \
  7981. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7982. error_buf_size)) \
  7983. goto fail; \
  7984. } while (0)
  7985. #define LAST_OP_OUTPUT_I32() \
  7986. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7987. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7988. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7989. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7990. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7991. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7992. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7993. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7994. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7995. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7996. || (last_op == EXT_OP_COPY_STACK_TOP)
  7997. #define LAST_OP_OUTPUT_I64() \
  7998. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7999. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  8000. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  8001. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  8002. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  8003. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  8004. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  8005. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  8006. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  8007. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  8008. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  8009. #define GET_CONST_OFFSET(type, val) \
  8010. do { \
  8011. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  8012. &operand_offset, error_buf, \
  8013. error_buf_size))) \
  8014. goto fail; \
  8015. } while (0)
  8016. #define GET_CONST_F32_OFFSET(type, fval) \
  8017. do { \
  8018. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8019. &operand_offset, error_buf, \
  8020. error_buf_size))) \
  8021. goto fail; \
  8022. } while (0)
  8023. #define GET_CONST_F64_OFFSET(type, fval) \
  8024. do { \
  8025. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8026. &operand_offset, error_buf, \
  8027. error_buf_size))) \
  8028. goto fail; \
  8029. } while (0)
  8030. #define emit_operand(ctx, offset) \
  8031. do { \
  8032. wasm_loader_emit_int16(ctx, offset); \
  8033. LOG_OP("%d\t", offset); \
  8034. } while (0)
  8035. #define emit_byte(ctx, byte) \
  8036. do { \
  8037. wasm_loader_emit_uint8(ctx, byte); \
  8038. LOG_OP("%d\t", byte); \
  8039. } while (0)
  8040. #define emit_uint32(ctx, value) \
  8041. do { \
  8042. wasm_loader_emit_uint32(ctx, value); \
  8043. LOG_OP("%d\t", value); \
  8044. } while (0)
  8045. #define emit_uint64(ctx, value) \
  8046. do { \
  8047. wasm_loader_emit_const(ctx, &value, false); \
  8048. LOG_OP("%lld\t", value); \
  8049. } while (0)
  8050. #define emit_float32(ctx, value) \
  8051. do { \
  8052. wasm_loader_emit_const(ctx, &value, true); \
  8053. LOG_OP("%f\t", value); \
  8054. } while (0)
  8055. #define emit_float64(ctx, value) \
  8056. do { \
  8057. wasm_loader_emit_const(ctx, &value, false); \
  8058. LOG_OP("%f\t", value); \
  8059. } while (0)
  8060. static bool
  8061. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8062. {
  8063. if (!(ctx->p_code_compiled =
  8064. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8065. return false;
  8066. ctx->p_code_compiled_end =
  8067. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8068. /* clean up frame ref */
  8069. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8070. ctx->frame_ref = ctx->frame_ref_bottom;
  8071. ctx->stack_cell_num = 0;
  8072. #if WASM_ENABLE_GC != 0
  8073. /* clean up reftype map */
  8074. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8075. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8076. ctx->reftype_map_num = 0;
  8077. #endif
  8078. /* clean up frame csp */
  8079. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8080. ctx->frame_csp = ctx->frame_csp_bottom;
  8081. ctx->csp_num = 0;
  8082. ctx->max_csp_num = 0;
  8083. /* clean up frame offset */
  8084. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8085. ctx->frame_offset = ctx->frame_offset_bottom;
  8086. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8087. /* init preserved local offsets */
  8088. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8089. /* const buf is reserved */
  8090. return true;
  8091. }
  8092. static void
  8093. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8094. {
  8095. ctx->code_compiled_size += size;
  8096. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8097. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8098. }
  8099. }
  8100. static void
  8101. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8102. {
  8103. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8104. if (ctx->p_code_compiled) {
  8105. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8106. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8107. #endif
  8108. bh_memcpy_s(ctx->p_code_compiled,
  8109. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8110. value, size);
  8111. ctx->p_code_compiled += size;
  8112. }
  8113. else {
  8114. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8115. bh_assert((ctx->code_compiled_size & 1) == 0);
  8116. #endif
  8117. increase_compiled_code_space(ctx, size);
  8118. }
  8119. }
  8120. static void
  8121. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8122. {
  8123. if (ctx->p_code_compiled) {
  8124. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8125. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8126. #endif
  8127. STORE_U32(ctx->p_code_compiled, value);
  8128. ctx->p_code_compiled += sizeof(uint32);
  8129. }
  8130. else {
  8131. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8132. bh_assert((ctx->code_compiled_size & 1) == 0);
  8133. #endif
  8134. increase_compiled_code_space(ctx, sizeof(uint32));
  8135. }
  8136. }
  8137. static void
  8138. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8139. {
  8140. if (ctx->p_code_compiled) {
  8141. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8142. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8143. #endif
  8144. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8145. ctx->p_code_compiled += sizeof(int16);
  8146. }
  8147. else {
  8148. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8149. bh_assert((ctx->code_compiled_size & 1) == 0);
  8150. #endif
  8151. increase_compiled_code_space(ctx, sizeof(uint16));
  8152. }
  8153. }
  8154. static void
  8155. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8156. {
  8157. if (ctx->p_code_compiled) {
  8158. *(ctx->p_code_compiled) = value;
  8159. ctx->p_code_compiled += sizeof(uint8);
  8160. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8161. ctx->p_code_compiled++;
  8162. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8163. #endif
  8164. }
  8165. else {
  8166. increase_compiled_code_space(ctx, sizeof(uint8));
  8167. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8168. increase_compiled_code_space(ctx, sizeof(uint8));
  8169. bh_assert((ctx->code_compiled_size & 1) == 0);
  8170. #endif
  8171. }
  8172. }
  8173. static void
  8174. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8175. {
  8176. if (ctx->p_code_compiled) {
  8177. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8178. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8179. #endif
  8180. STORE_PTR(ctx->p_code_compiled, value);
  8181. ctx->p_code_compiled += sizeof(void *);
  8182. }
  8183. else {
  8184. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8185. bh_assert((ctx->code_compiled_size & 1) == 0);
  8186. #endif
  8187. increase_compiled_code_space(ctx, sizeof(void *));
  8188. }
  8189. }
  8190. static void
  8191. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8192. {
  8193. if (ctx->p_code_compiled) {
  8194. ctx->p_code_compiled -= size;
  8195. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8196. if (size == sizeof(uint8)) {
  8197. ctx->p_code_compiled--;
  8198. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8199. }
  8200. #endif
  8201. }
  8202. else {
  8203. ctx->code_compiled_size -= size;
  8204. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8205. if (size == sizeof(uint8)) {
  8206. ctx->code_compiled_size--;
  8207. bh_assert((ctx->code_compiled_size & 1) == 0);
  8208. }
  8209. #endif
  8210. }
  8211. }
  8212. static bool
  8213. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8214. uint32 local_index, uint32 local_type,
  8215. bool *preserved, char *error_buf,
  8216. uint32 error_buf_size)
  8217. {
  8218. uint32 i = 0;
  8219. int16 preserved_offset = (int16)local_index;
  8220. *preserved = false;
  8221. while (i < loader_ctx->stack_cell_num) {
  8222. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8223. /* move previous local into dynamic space before a set/tee_local opcode
  8224. */
  8225. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8226. if (!(*preserved)) {
  8227. *preserved = true;
  8228. skip_label();
  8229. preserved_offset = loader_ctx->preserved_local_offset;
  8230. if (loader_ctx->p_code_compiled) {
  8231. bh_assert(preserved_offset != (int16)local_index);
  8232. }
  8233. if (is_32bit_type(local_type)) {
  8234. /* Only increase preserve offset in the second traversal */
  8235. if (loader_ctx->p_code_compiled)
  8236. loader_ctx->preserved_local_offset++;
  8237. emit_label(EXT_OP_COPY_STACK_TOP);
  8238. }
  8239. #if WASM_ENABLE_SIMDE != 0
  8240. else if (local_type == VALUE_TYPE_V128) {
  8241. if (loader_ctx->p_code_compiled)
  8242. loader_ctx->preserved_local_offset += 4;
  8243. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8244. }
  8245. #endif
  8246. else {
  8247. if (loader_ctx->p_code_compiled)
  8248. loader_ctx->preserved_local_offset += 2;
  8249. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8250. }
  8251. /* overflow */
  8252. if (preserved_offset > loader_ctx->preserved_local_offset) {
  8253. set_error_buf_v(error_buf, error_buf_size,
  8254. "too much local cells 0x%x",
  8255. loader_ctx->preserved_local_offset);
  8256. return false;
  8257. }
  8258. emit_operand(loader_ctx, local_index);
  8259. emit_operand(loader_ctx, preserved_offset);
  8260. emit_label(opcode);
  8261. }
  8262. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8263. }
  8264. if (cur_type == VALUE_TYPE_V128) {
  8265. i += 4;
  8266. }
  8267. else if (is_32bit_type(cur_type)) {
  8268. i++;
  8269. }
  8270. else {
  8271. i += 2;
  8272. }
  8273. }
  8274. (void)error_buf;
  8275. (void)error_buf_size;
  8276. return true;
  8277. }
  8278. static bool
  8279. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8280. char *error_buf, uint32 error_buf_size)
  8281. {
  8282. uint32 i = 0;
  8283. bool preserve_local;
  8284. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8285. blocks will not influence the value of these locals */
  8286. while (i < loader_ctx->stack_cell_num) {
  8287. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8288. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8289. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8290. && (cur_offset >= 0)) {
  8291. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8292. cur_type, &preserve_local,
  8293. error_buf, error_buf_size)))
  8294. return false;
  8295. }
  8296. if (cur_type == VALUE_TYPE_V128) {
  8297. i += 4;
  8298. }
  8299. else if (is_32bit_type(cur_type)) {
  8300. i++;
  8301. }
  8302. else {
  8303. i += 2;
  8304. }
  8305. }
  8306. return true;
  8307. }
  8308. static bool
  8309. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8310. uint8 *p_code_compiled, char *error_buf,
  8311. uint32 error_buf_size)
  8312. {
  8313. BranchBlockPatch *patch =
  8314. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8315. if (!patch) {
  8316. return false;
  8317. }
  8318. patch->patch_type = patch_type;
  8319. patch->code_compiled = p_code_compiled;
  8320. if (!frame_csp->patch_list) {
  8321. frame_csp->patch_list = patch;
  8322. patch->next = NULL;
  8323. }
  8324. else {
  8325. patch->next = frame_csp->patch_list;
  8326. frame_csp->patch_list = patch;
  8327. }
  8328. return true;
  8329. }
  8330. static void
  8331. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8332. {
  8333. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8334. BranchBlockPatch *node = frame_csp->patch_list;
  8335. BranchBlockPatch *node_prev = NULL, *node_next;
  8336. if (!ctx->p_code_compiled)
  8337. return;
  8338. while (node) {
  8339. node_next = node->next;
  8340. if (node->patch_type == patch_type) {
  8341. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8342. if (node_prev == NULL) {
  8343. frame_csp->patch_list = node_next;
  8344. }
  8345. else {
  8346. node_prev->next = node_next;
  8347. }
  8348. wasm_runtime_free(node);
  8349. }
  8350. else {
  8351. node_prev = node;
  8352. }
  8353. node = node_next;
  8354. }
  8355. }
  8356. static bool
  8357. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8358. bool is_br, char *error_buf, uint32 error_buf_size)
  8359. {
  8360. /* br info layout:
  8361. * a) arity of target block
  8362. * b) total cell num of arity values
  8363. * c) each arity value's cell num
  8364. * d) each arity value's src frame offset
  8365. * e) each arity values's dst dynamic offset
  8366. * f) branch target address
  8367. *
  8368. * Note: b-e are omitted when arity is 0 so that
  8369. * interpreter can recover the br info quickly.
  8370. */
  8371. BlockType *block_type = &frame_csp->block_type;
  8372. uint8 *types = NULL, cell;
  8373. #if WASM_ENABLE_GC != 0
  8374. WASMRefTypeMap *reftype_maps;
  8375. uint32 reftype_map_count;
  8376. #endif
  8377. uint32 arity = 0;
  8378. int32 i;
  8379. int16 *frame_offset = ctx->frame_offset;
  8380. uint16 dynamic_offset;
  8381. /* Note: loop's arity is different from if and block. loop's arity is
  8382. * its parameter count while if and block arity is result count.
  8383. */
  8384. #if WASM_ENABLE_GC == 0
  8385. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8386. arity = block_type_get_param_types(block_type, &types);
  8387. else
  8388. arity = block_type_get_result_types(block_type, &types);
  8389. #else
  8390. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8391. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8392. &reftype_map_count);
  8393. else
  8394. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8395. &reftype_map_count);
  8396. #endif
  8397. /* Part a */
  8398. emit_uint32(ctx, arity);
  8399. if (arity) {
  8400. /* Part b */
  8401. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8402. /* Part c */
  8403. for (i = (int32)arity - 1; i >= 0; i--) {
  8404. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8405. emit_byte(ctx, cell);
  8406. }
  8407. /* Part d */
  8408. for (i = (int32)arity - 1; i >= 0; i--) {
  8409. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8410. frame_offset -= cell;
  8411. emit_operand(ctx, *(int16 *)(frame_offset));
  8412. }
  8413. /* Part e */
  8414. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8415. /* Use start_dynamic_offset which was set in
  8416. copy_params_to_dynamic_space */
  8417. dynamic_offset = frame_csp->start_dynamic_offset
  8418. + wasm_get_cell_num(types, arity);
  8419. else
  8420. dynamic_offset =
  8421. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8422. if (is_br)
  8423. ctx->dynamic_offset = dynamic_offset;
  8424. for (i = (int32)arity - 1; i >= 0; i--) {
  8425. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8426. dynamic_offset -= cell;
  8427. emit_operand(ctx, dynamic_offset);
  8428. }
  8429. }
  8430. /* Part f */
  8431. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8432. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8433. }
  8434. else {
  8435. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8436. error_buf, error_buf_size))
  8437. return false;
  8438. /* label address, to be patched */
  8439. wasm_loader_emit_ptr(ctx, NULL);
  8440. }
  8441. return true;
  8442. }
  8443. static bool
  8444. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8445. bool disable_emit, int16 operand_offset,
  8446. char *error_buf, uint32 error_buf_size)
  8447. {
  8448. uint32 cell_num_to_push, i;
  8449. if (type == VALUE_TYPE_VOID)
  8450. return true;
  8451. /* only check memory overflow in first traverse */
  8452. if (ctx->p_code_compiled == NULL) {
  8453. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8454. return false;
  8455. }
  8456. if (disable_emit)
  8457. *(ctx->frame_offset)++ = operand_offset;
  8458. else {
  8459. emit_operand(ctx, ctx->dynamic_offset);
  8460. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8461. ctx->dynamic_offset++;
  8462. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8463. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8464. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8465. goto fail;
  8466. }
  8467. }
  8468. }
  8469. if (is_32bit_type(type))
  8470. return true;
  8471. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8472. for (i = 0; i < cell_num_to_push; i++) {
  8473. if (ctx->p_code_compiled == NULL) {
  8474. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8475. return false;
  8476. }
  8477. ctx->frame_offset++;
  8478. if (!disable_emit) {
  8479. ctx->dynamic_offset++;
  8480. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8481. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8482. if (ctx->max_dynamic_offset >= INT16_MAX)
  8483. goto fail;
  8484. }
  8485. }
  8486. }
  8487. return true;
  8488. fail:
  8489. set_error_buf(error_buf, error_buf_size,
  8490. "fast interpreter offset overflow");
  8491. return false;
  8492. }
  8493. /* This function should be in front of wasm_loader_pop_frame_ref
  8494. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8495. will be modified by wasm_loader_pop_frame_ref */
  8496. static bool
  8497. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8498. char *error_buf, uint32 error_buf_size)
  8499. {
  8500. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8501. then current block is the function block */
  8502. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8503. BranchBlock *cur_block = ctx->frame_csp - depth;
  8504. int32 available_stack_cell =
  8505. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8506. uint32 cell_num_to_pop;
  8507. /* Directly return success if current block is in stack
  8508. polymorphic state while stack is empty. */
  8509. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8510. return true;
  8511. if (type == VALUE_TYPE_VOID)
  8512. return true;
  8513. /* Change type to ANY when the stack top is ANY, so as to avoid
  8514. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8515. two offsets */
  8516. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8517. type = VALUE_TYPE_ANY;
  8518. cell_num_to_pop = wasm_value_type_cell_num(type);
  8519. /* Check the offset stack bottom to ensure the frame offset
  8520. stack will not go underflow. But we don't thrown error
  8521. and return true here, because the error msg should be
  8522. given in wasm_loader_pop_frame_ref */
  8523. if (!check_offset_pop(ctx, cell_num_to_pop))
  8524. return true;
  8525. ctx->frame_offset -= cell_num_to_pop;
  8526. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8527. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8528. ctx->dynamic_offset -= cell_num_to_pop;
  8529. emit_operand(ctx, *(ctx->frame_offset));
  8530. (void)error_buf;
  8531. (void)error_buf_size;
  8532. return true;
  8533. }
  8534. static bool
  8535. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8536. bool disable_emit, int16 operand_offset,
  8537. char *error_buf, uint32 error_buf_size)
  8538. {
  8539. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8540. error_buf, error_buf_size)))
  8541. return false;
  8542. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8543. return false;
  8544. return true;
  8545. }
  8546. static bool
  8547. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8548. char *error_buf, uint32 error_buf_size)
  8549. {
  8550. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8551. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8552. return false;
  8553. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8554. return false;
  8555. return true;
  8556. }
  8557. static bool
  8558. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8559. uint8 type_push, uint8 type_pop,
  8560. bool disable_emit, int16 operand_offset,
  8561. char *error_buf, uint32 error_buf_size)
  8562. {
  8563. uint8 i;
  8564. for (i = 0; i < pop_cnt; i++) {
  8565. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8566. error_buf_size))
  8567. return false;
  8568. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8569. error_buf_size))
  8570. return false;
  8571. }
  8572. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8573. operand_offset, error_buf,
  8574. error_buf_size))
  8575. return false;
  8576. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8577. return false;
  8578. return true;
  8579. }
  8580. static int
  8581. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  8582. {
  8583. int64 i64_const1 = *(int64 *)p_i64_const1;
  8584. int64 i64_const2 = *(int64 *)p_i64_const2;
  8585. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  8586. }
  8587. static int
  8588. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  8589. {
  8590. int32 i32_const1 = *(int32 *)p_i32_const1;
  8591. int32 i32_const2 = *(int32 *)p_i32_const2;
  8592. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  8593. }
  8594. static int
  8595. cmp_v128_const(const void *p_v128_const1, const void *p_v128_const2)
  8596. {
  8597. V128 v128_const1 = *(V128 *)p_v128_const1;
  8598. V128 v128_const2 = *(V128 *)p_v128_const2;
  8599. return memcmp(&v128_const1, &v128_const2, sizeof(V128));
  8600. }
  8601. static bool
  8602. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8603. int16 *offset, char *error_buf,
  8604. uint32 error_buf_size)
  8605. {
  8606. if (!ctx->p_code_compiled) {
  8607. /* Treat i64 and f64 as the same by reading i64 value from
  8608. the raw bytes */
  8609. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8610. /* No slot left, emit const instead */
  8611. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  8612. *offset = 0;
  8613. return true;
  8614. }
  8615. /* Traverse the list if the const num is small */
  8616. if (ctx->i64_const_num < 10) {
  8617. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  8618. if (ctx->i64_consts[i] == *(int64 *)value) {
  8619. *offset = -1;
  8620. return true;
  8621. }
  8622. }
  8623. }
  8624. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  8625. MEM_REALLOC(ctx->i64_consts,
  8626. sizeof(int64) * ctx->i64_const_max_num,
  8627. sizeof(int64) * (ctx->i64_const_max_num * 2));
  8628. ctx->i64_const_max_num *= 2;
  8629. }
  8630. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  8631. }
  8632. else if (type == VALUE_TYPE_V128) {
  8633. /* No slot left, emit const instead */
  8634. if (ctx->v128_const_num * 4 > INT16_MAX - 2) {
  8635. *offset = 0;
  8636. return true;
  8637. }
  8638. /* Traverse the list if the const num is small */
  8639. if (ctx->v128_const_num < 10) {
  8640. for (uint32 i = 0; i < ctx->v128_const_num; i++) {
  8641. if (memcmp(&ctx->v128_consts[i], value, sizeof(V128))
  8642. == 0) {
  8643. *offset = -1;
  8644. return true;
  8645. }
  8646. }
  8647. }
  8648. if (ctx->v128_const_num >= ctx->v128_const_max_num) {
  8649. MEM_REALLOC(ctx->v128_consts,
  8650. sizeof(V128) * ctx->v128_const_max_num,
  8651. sizeof(V128) * (ctx->v128_const_max_num * 2));
  8652. ctx->v128_const_max_num *= 2;
  8653. }
  8654. ctx->v128_consts[ctx->v128_const_num++] = *(V128 *)value;
  8655. }
  8656. else {
  8657. /* Treat i32 and f32 as the same by reading i32 value from
  8658. the raw bytes */
  8659. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  8660. /* No slot left, emit const instead */
  8661. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  8662. *offset = 0;
  8663. return true;
  8664. }
  8665. /* Traverse the list if the const num is small */
  8666. if (ctx->i32_const_num < 10) {
  8667. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  8668. if (ctx->i32_consts[i] == *(int32 *)value) {
  8669. *offset = -1;
  8670. return true;
  8671. }
  8672. }
  8673. }
  8674. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  8675. MEM_REALLOC(ctx->i32_consts,
  8676. sizeof(int32) * ctx->i32_const_max_num,
  8677. sizeof(int32) * (ctx->i32_const_max_num * 2));
  8678. ctx->i32_const_max_num *= 2;
  8679. }
  8680. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  8681. }
  8682. *offset = -1;
  8683. return true;
  8684. }
  8685. else {
  8686. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8687. int64 key = *(int64 *)value, *i64_const;
  8688. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  8689. sizeof(int64), cmp_i64_const);
  8690. if (!i64_const) { /* not found, emit const instead */
  8691. *offset = 0;
  8692. return true;
  8693. }
  8694. /* constant index is encoded as negative value */
  8695. *offset = -(int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  8696. + (int32)(i64_const - ctx->i64_consts) * 2;
  8697. }
  8698. else if (type == VALUE_TYPE_V128) {
  8699. V128 key = *(V128 *)value, *v128_const;
  8700. v128_const = bsearch(&key, ctx->v128_consts, ctx->v128_const_num,
  8701. sizeof(V128), cmp_v128_const);
  8702. if (!v128_const) { /* not found, emit const instead */
  8703. *offset = 0;
  8704. return true;
  8705. }
  8706. /* constant index is encoded as negative value */
  8707. *offset = -(int32)(ctx->v128_const_num)
  8708. + (int32)(v128_const - ctx->v128_consts);
  8709. }
  8710. else {
  8711. int32 key = *(int32 *)value, *i32_const;
  8712. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  8713. sizeof(int32), cmp_i32_const);
  8714. if (!i32_const) { /* not found, emit const instead */
  8715. *offset = 0;
  8716. return true;
  8717. }
  8718. /* constant index is encoded as negative value */
  8719. *offset = -(int32)(ctx->i32_const_num)
  8720. + (int32)(i32_const - ctx->i32_consts);
  8721. }
  8722. return true;
  8723. }
  8724. fail:
  8725. return false;
  8726. }
  8727. /*
  8728. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8729. -- Mostly used for the binary / compare operation
  8730. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8731. -- Mostly used in block / control instructions
  8732. The POP will always emit the offset on the top of the frame_offset stack
  8733. PUSH can be used in two ways:
  8734. 1. directly PUSH:
  8735. PUSH_XXX();
  8736. will allocate a dynamic space and emit
  8737. 2. silent PUSH:
  8738. operand_offset = xxx; disable_emit = true;
  8739. PUSH_XXX();
  8740. only push the frame_offset stack, no emit
  8741. */
  8742. #define TEMPLATE_PUSH(Type) \
  8743. do { \
  8744. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8745. disable_emit, operand_offset, \
  8746. error_buf, error_buf_size)) \
  8747. goto fail; \
  8748. } while (0)
  8749. #define TEMPLATE_PUSH_REF(Type) \
  8750. do { \
  8751. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8752. operand_offset, error_buf, \
  8753. error_buf_size)) \
  8754. goto fail; \
  8755. } while (0)
  8756. #define TEMPLATE_POP(Type) \
  8757. do { \
  8758. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8759. error_buf, error_buf_size)) \
  8760. goto fail; \
  8761. } while (0)
  8762. #define TEMPLATE_POP_REF(Type) \
  8763. do { \
  8764. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8765. error_buf_size)) \
  8766. goto fail; \
  8767. } while (0)
  8768. #define PUSH_OFFSET_TYPE(type) \
  8769. do { \
  8770. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8771. operand_offset, error_buf, \
  8772. error_buf_size))) \
  8773. goto fail; \
  8774. } while (0)
  8775. #define POP_OFFSET_TYPE(type) \
  8776. do { \
  8777. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8778. error_buf_size))) \
  8779. goto fail; \
  8780. } while (0)
  8781. #define POP_AND_PUSH(type_pop, type_push) \
  8782. do { \
  8783. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8784. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8785. operand_offset, error_buf, error_buf_size))) \
  8786. goto fail; \
  8787. } while (0)
  8788. /* type of POPs should be the same */
  8789. #define POP2_AND_PUSH(type_pop, type_push) \
  8790. do { \
  8791. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8792. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8793. operand_offset, error_buf, error_buf_size))) \
  8794. goto fail; \
  8795. } while (0)
  8796. #else /* WASM_ENABLE_FAST_INTERP */
  8797. #define TEMPLATE_PUSH(Type) \
  8798. do { \
  8799. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8800. error_buf, error_buf_size))) \
  8801. goto fail; \
  8802. } while (0)
  8803. #define TEMPLATE_PUSH_REF(Type) \
  8804. do { \
  8805. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8806. error_buf_size))) \
  8807. goto fail; \
  8808. } while (0)
  8809. #define TEMPLATE_POP(Type) \
  8810. do { \
  8811. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8812. error_buf, error_buf_size))) \
  8813. goto fail; \
  8814. } while (0)
  8815. #define TEMPLATE_POP_REF(Type) \
  8816. do { \
  8817. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8818. error_buf_size))) \
  8819. goto fail; \
  8820. } while (0)
  8821. #define POP_AND_PUSH(type_pop, type_push) \
  8822. do { \
  8823. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8824. type_pop, error_buf, \
  8825. error_buf_size))) \
  8826. goto fail; \
  8827. } while (0)
  8828. /* type of POPs should be the same */
  8829. #define POP2_AND_PUSH(type_pop, type_push) \
  8830. do { \
  8831. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8832. type_pop, error_buf, \
  8833. error_buf_size))) \
  8834. goto fail; \
  8835. } while (0)
  8836. #endif /* WASM_ENABLE_FAST_INTERP */
  8837. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8838. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8839. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8840. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8841. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8842. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8843. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8844. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8845. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8846. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8847. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8848. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  8849. #define POP_I32() TEMPLATE_POP(I32)
  8850. #define POP_F32() TEMPLATE_POP(F32)
  8851. #define POP_I64() TEMPLATE_POP(I64)
  8852. #define POP_F64() TEMPLATE_POP(F64)
  8853. #define POP_V128() TEMPLATE_POP(V128)
  8854. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8855. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8856. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8857. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8858. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  8859. #if WASM_ENABLE_FAST_INTERP != 0
  8860. static bool
  8861. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8862. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8863. {
  8864. int16 operand_offset = 0;
  8865. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8866. : loader_ctx->frame_csp;
  8867. BlockType *block_type = &block->block_type;
  8868. uint8 *return_types = NULL;
  8869. #if WASM_ENABLE_GC != 0
  8870. WASMRefTypeMap *reftype_maps = NULL;
  8871. uint32 reftype_map_count;
  8872. #endif
  8873. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8874. int32 i = 0;
  8875. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8876. *frame_offset_org = NULL;
  8877. #if WASM_ENABLE_GC == 0
  8878. return_count = block_type_get_result_types(block_type, &return_types);
  8879. #else
  8880. return_count = block_type_get_result_types(
  8881. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8882. #endif
  8883. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64/V128
  8884. * instead of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8885. if (return_count == 1) {
  8886. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8887. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8888. /* insert op_copy before else opcode */
  8889. if (opcode == WASM_OP_ELSE)
  8890. skip_label();
  8891. #if WASM_ENABLE_SIMDE != 0
  8892. if (cell == 4) {
  8893. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8894. }
  8895. #endif
  8896. if (cell <= 2) {
  8897. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8898. : EXT_OP_COPY_STACK_TOP_I64);
  8899. }
  8900. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8901. emit_operand(loader_ctx, block->dynamic_offset);
  8902. if (opcode == WASM_OP_ELSE) {
  8903. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8904. }
  8905. else {
  8906. loader_ctx->frame_offset -= cell;
  8907. loader_ctx->dynamic_offset = block->dynamic_offset;
  8908. PUSH_OFFSET_TYPE(return_types[0]);
  8909. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8910. }
  8911. if (opcode == WASM_OP_ELSE)
  8912. emit_label(opcode);
  8913. }
  8914. return true;
  8915. }
  8916. /* Copy stack top values to block's results which are in dynamic space.
  8917. * The instruction format:
  8918. * Part a: values count
  8919. * Part b: all values total cell num
  8920. * Part c: each value's cell_num, src offset and dst offset
  8921. * Part d: each value's src offset and dst offset
  8922. * Part e: each value's dst offset
  8923. */
  8924. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8925. dynamic_offset = dynamic_offset_org =
  8926. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8927. /* First traversal to get the count of values needed to be copied. */
  8928. for (i = (int32)return_count - 1; i >= 0; i--) {
  8929. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8930. if (frame_offset - cells < loader_ctx->frame_offset_bottom) {
  8931. set_error_buf(error_buf, error_buf_size, "frame offset underflow");
  8932. goto fail;
  8933. }
  8934. if (cells == 4) {
  8935. bool needs_copy = false;
  8936. int16 v128_dynamic = dynamic_offset - cells;
  8937. for (int j = 0; j < 4; j++) {
  8938. if (*(frame_offset - j - 1) != (v128_dynamic + j)) {
  8939. needs_copy = true;
  8940. break;
  8941. }
  8942. }
  8943. if (needs_copy) {
  8944. value_count++;
  8945. total_cel_num += cells;
  8946. }
  8947. frame_offset -= cells;
  8948. dynamic_offset = v128_dynamic;
  8949. }
  8950. else {
  8951. frame_offset -= cells;
  8952. dynamic_offset -= cells;
  8953. if (dynamic_offset != *frame_offset) {
  8954. value_count++;
  8955. total_cel_num += cells;
  8956. }
  8957. }
  8958. }
  8959. if (value_count) {
  8960. uint32 j = 0;
  8961. uint8 *emit_data = NULL, *cells = NULL;
  8962. int16 *src_offsets = NULL;
  8963. uint16 *dst_offsets = NULL;
  8964. uint64 size =
  8965. (uint64)value_count
  8966. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8967. /* Allocate memory for the emit data */
  8968. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8969. return false;
  8970. cells = emit_data;
  8971. src_offsets = (int16 *)(cells + value_count);
  8972. dst_offsets = (uint16 *)(src_offsets + value_count);
  8973. /* insert op_copy before else opcode */
  8974. if (opcode == WASM_OP_ELSE)
  8975. skip_label();
  8976. emit_label(EXT_OP_COPY_STACK_VALUES);
  8977. /* Part a) */
  8978. emit_uint32(loader_ctx, value_count);
  8979. /* Part b) */
  8980. emit_uint32(loader_ctx, total_cel_num);
  8981. /* Second traversal to get each value's cell num, src offset and dst
  8982. * offset. */
  8983. frame_offset = frame_offset_org;
  8984. dynamic_offset = dynamic_offset_org;
  8985. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8986. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8987. if (cell == 4) {
  8988. bool needs_copy = false;
  8989. int16 v128_dynamic = dynamic_offset - cell;
  8990. for (int k = 0; k < 4; k++) {
  8991. if (*(frame_offset - k - 1) != (v128_dynamic + k)) {
  8992. needs_copy = true;
  8993. break;
  8994. }
  8995. }
  8996. if (needs_copy) {
  8997. cells[j] = cell;
  8998. src_offsets[j] = *(frame_offset - cell);
  8999. dst_offsets[j] = v128_dynamic;
  9000. j++;
  9001. }
  9002. frame_offset -= cell;
  9003. dynamic_offset = v128_dynamic;
  9004. }
  9005. else {
  9006. frame_offset -= cell;
  9007. dynamic_offset -= cell;
  9008. if (dynamic_offset != *frame_offset) {
  9009. cells[j] = cell;
  9010. /* src offset */
  9011. src_offsets[j] = *frame_offset;
  9012. /* dst offset */
  9013. dst_offsets[j] = dynamic_offset;
  9014. j++;
  9015. }
  9016. }
  9017. if (opcode == WASM_OP_ELSE) {
  9018. if (cell == 4) {
  9019. for (int k = 0; k < cell; k++) {
  9020. *(frame_offset + k) = dynamic_offset + k;
  9021. }
  9022. }
  9023. else {
  9024. *frame_offset = dynamic_offset;
  9025. }
  9026. }
  9027. else {
  9028. loader_ctx->frame_offset = frame_offset;
  9029. loader_ctx->dynamic_offset = dynamic_offset;
  9030. if (!(wasm_loader_push_frame_offset(
  9031. loader_ctx, return_types[i], disable_emit,
  9032. operand_offset, error_buf, error_buf_size))) {
  9033. wasm_runtime_free(emit_data);
  9034. goto fail;
  9035. }
  9036. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9037. loader_ctx->frame_offset = frame_offset_org;
  9038. loader_ctx->dynamic_offset = dynamic_offset_org;
  9039. }
  9040. }
  9041. bh_assert(j == value_count);
  9042. /* Emit the cells, src_offsets and dst_offsets */
  9043. for (j = 0; j < value_count; j++)
  9044. emit_byte(loader_ctx, cells[j]);
  9045. for (j = 0; j < value_count; j++)
  9046. emit_operand(loader_ctx, src_offsets[j]);
  9047. for (j = 0; j < value_count; j++)
  9048. emit_operand(loader_ctx, dst_offsets[j]);
  9049. if (opcode == WASM_OP_ELSE)
  9050. emit_label(opcode);
  9051. wasm_runtime_free(emit_data);
  9052. }
  9053. return true;
  9054. fail:
  9055. return false;
  9056. }
  9057. #endif /* WASM_ENABLE_FAST_INTERP */
  9058. #define PUSH_TYPE(type) \
  9059. do { \
  9060. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  9061. error_buf_size))) \
  9062. goto fail; \
  9063. } while (0)
  9064. #define POP_TYPE(type) \
  9065. do { \
  9066. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  9067. error_buf_size))) \
  9068. goto fail; \
  9069. } while (0)
  9070. #if WASM_ENABLE_GC == 0
  9071. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9072. do { \
  9073. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9074. _start_addr, error_buf, \
  9075. error_buf_size)) \
  9076. goto fail; \
  9077. } while (0)
  9078. #define POP_CSP() \
  9079. do { \
  9080. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9081. goto fail; \
  9082. } while (0)
  9083. #else
  9084. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9085. do { \
  9086. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9087. _start_addr, error_buf, \
  9088. error_buf_size)) \
  9089. goto fail; \
  9090. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  9091. error_buf, error_buf_size)) { \
  9092. goto fail; \
  9093. } \
  9094. } while (0)
  9095. #define POP_CSP() \
  9096. do { \
  9097. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  9098. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9099. goto fail; \
  9100. } while (0)
  9101. #endif /* end of WASM_ENABLE_GC == 0 */
  9102. #if WASM_ENABLE_GC == 0
  9103. #define GET_LOCAL_REFTYPE() (void)0
  9104. #else
  9105. #define GET_LOCAL_REFTYPE() \
  9106. do { \
  9107. if (wasm_is_type_multi_byte_type(local_type)) { \
  9108. WASMRefType *_ref_type; \
  9109. if (local_idx < param_count) \
  9110. _ref_type = wasm_reftype_map_find( \
  9111. param_reftype_maps, param_reftype_map_count, local_idx); \
  9112. else \
  9113. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  9114. local_reftype_map_count, \
  9115. local_idx - param_count); \
  9116. bh_assert(_ref_type); \
  9117. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  9118. wasm_reftype_struct_size(_ref_type)); \
  9119. } \
  9120. } while (0)
  9121. #endif /* end of WASM_ENABLE_GC == 0 */
  9122. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  9123. do { \
  9124. read_leb_uint32(p, p_end, local_idx); \
  9125. if (local_idx >= param_count + local_count) { \
  9126. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  9127. goto fail; \
  9128. } \
  9129. local_type = local_idx < param_count \
  9130. ? param_types[local_idx] \
  9131. : local_types[local_idx - param_count]; \
  9132. local_offset = local_offsets[local_idx]; \
  9133. GET_LOCAL_REFTYPE(); \
  9134. } while (0)
  9135. static bool
  9136. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  9137. {
  9138. if (module->memory_count == 0 && module->import_memory_count == 0) {
  9139. set_error_buf(error_buf, error_buf_size, "unknown memory");
  9140. return false;
  9141. }
  9142. return true;
  9143. }
  9144. #define CHECK_MEMORY() \
  9145. do { \
  9146. if (!check_memory(module, error_buf, error_buf_size)) \
  9147. goto fail; \
  9148. } while (0)
  9149. static bool
  9150. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9151. uint32 error_buf_size)
  9152. {
  9153. uint8 mem_access_aligns[] = {
  9154. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9155. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9156. };
  9157. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9158. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9159. set_error_buf(error_buf, error_buf_size,
  9160. "invalid memop flags: alignment must not be larger "
  9161. "than natural");
  9162. return false;
  9163. }
  9164. return true;
  9165. }
  9166. #if WASM_ENABLE_SIMD != 0
  9167. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  9168. || (WASM_ENABLE_FAST_INTERP != 0)
  9169. static bool
  9170. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9171. uint32 error_buf_size)
  9172. {
  9173. uint8 mem_access_aligns[] = {
  9174. 4, /* load */
  9175. 3, 3, 3, 3, 3, 3, /* load and extend */
  9176. 0, 1, 2, 3, /* load and splat */
  9177. 4, /* store */
  9178. };
  9179. uint8 mem_access_aligns_load_lane[] = {
  9180. 0, 1, 2, 3, /* load lane */
  9181. 0, 1, 2, 3, /* store lane */
  9182. 2, 3 /* store zero */
  9183. };
  9184. if (!((opcode <= SIMD_v128_store)
  9185. || (SIMD_v128_load8_lane <= opcode
  9186. && opcode <= SIMD_v128_load64_zero))) {
  9187. set_error_buf(error_buf, error_buf_size,
  9188. "the opcode doesn't include memarg");
  9189. return false;
  9190. }
  9191. if ((opcode <= SIMD_v128_store
  9192. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9193. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9194. && align > mem_access_aligns_load_lane[opcode
  9195. - SIMD_v128_load8_lane])) {
  9196. set_error_buf(error_buf, error_buf_size,
  9197. "invalid memop flags: alignment must not be larger "
  9198. "than natural");
  9199. return false;
  9200. }
  9201. return true;
  9202. }
  9203. static bool
  9204. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9205. uint32 error_buf_size)
  9206. {
  9207. switch (opcode) {
  9208. case SIMD_i8x16_extract_lane_s:
  9209. case SIMD_i8x16_extract_lane_u:
  9210. case SIMD_i8x16_replace_lane:
  9211. if (lane >= 16) {
  9212. goto fail;
  9213. }
  9214. break;
  9215. case SIMD_i16x8_extract_lane_s:
  9216. case SIMD_i16x8_extract_lane_u:
  9217. case SIMD_i16x8_replace_lane:
  9218. if (lane >= 8) {
  9219. goto fail;
  9220. }
  9221. break;
  9222. case SIMD_i32x4_extract_lane:
  9223. case SIMD_i32x4_replace_lane:
  9224. case SIMD_f32x4_extract_lane:
  9225. case SIMD_f32x4_replace_lane:
  9226. if (lane >= 4) {
  9227. goto fail;
  9228. }
  9229. break;
  9230. case SIMD_i64x2_extract_lane:
  9231. case SIMD_i64x2_replace_lane:
  9232. case SIMD_f64x2_extract_lane:
  9233. case SIMD_f64x2_replace_lane:
  9234. if (lane >= 2) {
  9235. goto fail;
  9236. }
  9237. break;
  9238. case SIMD_v128_load8_lane:
  9239. case SIMD_v128_load16_lane:
  9240. case SIMD_v128_load32_lane:
  9241. case SIMD_v128_load64_lane:
  9242. case SIMD_v128_store8_lane:
  9243. case SIMD_v128_store16_lane:
  9244. case SIMD_v128_store32_lane:
  9245. case SIMD_v128_store64_lane:
  9246. case SIMD_v128_load32_zero:
  9247. case SIMD_v128_load64_zero:
  9248. {
  9249. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9250. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9251. goto fail;
  9252. }
  9253. break;
  9254. }
  9255. default:
  9256. goto fail;
  9257. }
  9258. return true;
  9259. fail:
  9260. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9261. return false;
  9262. }
  9263. static bool
  9264. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9265. {
  9266. uint8 i;
  9267. for (i = 0; i != 16; ++i) {
  9268. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9269. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9270. return false;
  9271. }
  9272. }
  9273. return true;
  9274. }
  9275. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9276. #endif /* end of WASM_ENABLE_SIMD */
  9277. #if WASM_ENABLE_SHARED_MEMORY != 0
  9278. static bool
  9279. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9280. uint32 error_buf_size)
  9281. {
  9282. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9283. uint8 mem_access_aligns[] = {
  9284. 2, 3, 0, 1, 0, 1, 2,
  9285. };
  9286. uint8 expect;
  9287. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9288. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9289. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9290. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9291. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9292. }
  9293. else {
  9294. /* 7 opcodes in every group */
  9295. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9296. }
  9297. if (align != expect) {
  9298. set_error_buf(error_buf, error_buf_size,
  9299. "alignment isn't equal to natural");
  9300. return false;
  9301. }
  9302. return true;
  9303. }
  9304. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9305. static bool
  9306. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9307. char *error_buf, uint32 error_buf_size)
  9308. {
  9309. BranchBlock *target_block, *cur_block;
  9310. BlockType *target_block_type;
  9311. uint8 type, *types = NULL, *frame_ref;
  9312. uint32 arity = 0;
  9313. int32 i, available_stack_cell;
  9314. uint16 cell_num;
  9315. #if WASM_ENABLE_GC != 0
  9316. WASMRefTypeMap *frame_reftype_map;
  9317. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9318. WASMRefType *ref_type;
  9319. uint32 reftype_map_count = 0;
  9320. int32 available_reftype_map;
  9321. bool is_type_multi_byte;
  9322. #endif
  9323. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9324. uint8 *frame_ref_after_popped = NULL;
  9325. uint8 frame_ref_tmp[4] = { 0 };
  9326. uint8 *frame_ref_buf = frame_ref_tmp;
  9327. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9328. #if WASM_ENABLE_GC != 0
  9329. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9330. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9331. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9332. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9333. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9334. #endif
  9335. #if WASM_ENABLE_FAST_INTERP != 0
  9336. int16 *frame_offset_old = loader_ctx->frame_offset;
  9337. int16 *frame_offset_after_popped = NULL;
  9338. int16 frame_offset_tmp[4] = { 0 };
  9339. int16 *frame_offset_buf = frame_offset_tmp;
  9340. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9341. #endif
  9342. bool ret = false;
  9343. bh_assert(loader_ctx->csp_num > 0);
  9344. if (loader_ctx->csp_num - 1 < depth) {
  9345. set_error_buf(error_buf, error_buf_size,
  9346. "unknown label, "
  9347. "unexpected end of section or function");
  9348. return false;
  9349. }
  9350. cur_block = loader_ctx->frame_csp - 1;
  9351. target_block = loader_ctx->frame_csp - (depth + 1);
  9352. target_block_type = &target_block->block_type;
  9353. frame_ref = loader_ctx->frame_ref;
  9354. #if WASM_ENABLE_GC != 0
  9355. frame_reftype_map = loader_ctx->frame_reftype_map;
  9356. #endif
  9357. /* Note: loop's arity is different from if and block. loop's arity is
  9358. * its parameter count while if and block arity is result count.
  9359. */
  9360. #if WASM_ENABLE_GC == 0
  9361. if (target_block->label_type == LABEL_TYPE_LOOP)
  9362. arity = block_type_get_param_types(target_block_type, &types);
  9363. else
  9364. arity = block_type_get_result_types(target_block_type, &types);
  9365. #else
  9366. if (target_block->label_type == LABEL_TYPE_LOOP)
  9367. arity = block_type_get_param_types(target_block_type, &types,
  9368. &reftype_maps, &reftype_map_count);
  9369. else
  9370. arity = block_type_get_result_types(target_block_type, &types,
  9371. &reftype_maps, &reftype_map_count);
  9372. #endif
  9373. /* If the stack is in polymorphic state, just clear the stack
  9374. * and then re-push the values to make the stack top values
  9375. * match block type. */
  9376. if (cur_block->is_stack_polymorphic) {
  9377. #if WASM_ENABLE_GC != 0
  9378. int32 j = reftype_map_count - 1;
  9379. #endif
  9380. for (i = (int32)arity - 1; i >= 0; i--) {
  9381. #if WASM_ENABLE_GC != 0
  9382. if (wasm_is_type_multi_byte_type(types[i])) {
  9383. bh_assert(reftype_maps[j].index == i);
  9384. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9385. reftype_maps[j].ref_type,
  9386. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9387. j--;
  9388. }
  9389. #endif
  9390. #if WASM_ENABLE_FAST_INTERP != 0
  9391. POP_OFFSET_TYPE(types[i]);
  9392. #endif
  9393. POP_TYPE(types[i]);
  9394. }
  9395. /* Backup stack data since it may be changed in the below
  9396. push operations, and the stack data may be used when
  9397. checking other target blocks of opcode br_table */
  9398. if (opcode == WASM_OP_BR_TABLE) {
  9399. uint64 total_size;
  9400. frame_ref_after_popped = loader_ctx->frame_ref;
  9401. total_size = (uint64)sizeof(uint8)
  9402. * (frame_ref_old - frame_ref_after_popped);
  9403. if (total_size > sizeof(frame_ref_tmp)
  9404. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9405. error_buf_size))) {
  9406. goto fail;
  9407. }
  9408. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9409. frame_ref_after_popped, (uint32)total_size);
  9410. #if WASM_ENABLE_GC != 0
  9411. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9412. total_size =
  9413. (uint64)sizeof(WASMRefTypeMap)
  9414. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9415. if (total_size > sizeof(frame_reftype_map_tmp)
  9416. && !(frame_reftype_map_buf = loader_malloc(
  9417. total_size, error_buf, error_buf_size))) {
  9418. goto fail;
  9419. }
  9420. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9421. frame_reftype_map_after_popped, (uint32)total_size);
  9422. #endif
  9423. #if WASM_ENABLE_FAST_INTERP != 0
  9424. frame_offset_after_popped = loader_ctx->frame_offset;
  9425. total_size = (uint64)sizeof(int16)
  9426. * (frame_offset_old - frame_offset_after_popped);
  9427. if (total_size > sizeof(frame_offset_tmp)
  9428. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9429. error_buf_size))) {
  9430. goto fail;
  9431. }
  9432. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9433. frame_offset_after_popped, (uint32)total_size);
  9434. #endif
  9435. }
  9436. #if WASM_ENABLE_GC != 0
  9437. j = 0;
  9438. #endif
  9439. for (i = 0; i < (int32)arity; i++) {
  9440. #if WASM_ENABLE_GC != 0
  9441. if (wasm_is_type_multi_byte_type(types[i])) {
  9442. bh_assert(reftype_maps[j].index == i);
  9443. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9444. reftype_maps[j].ref_type,
  9445. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9446. j++;
  9447. }
  9448. #endif
  9449. #if WASM_ENABLE_FAST_INTERP != 0
  9450. bool disable_emit = true;
  9451. int16 operand_offset = 0;
  9452. PUSH_OFFSET_TYPE(types[i]);
  9453. #endif
  9454. PUSH_TYPE(types[i]);
  9455. }
  9456. #if WASM_ENABLE_FAST_INTERP != 0
  9457. emit_br_info(target_block, opcode == WASM_OP_BR);
  9458. #endif
  9459. /* Restore the stack data, note that frame_ref_bottom,
  9460. frame_reftype_map_bottom, frame_offset_bottom may be
  9461. re-allocated in the above push operations */
  9462. if (opcode == WASM_OP_BR_TABLE) {
  9463. uint32 total_size;
  9464. /* The stack operand num should not be smaller than before
  9465. after pop and push operations */
  9466. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9467. loader_ctx->stack_cell_num = stack_cell_num_old;
  9468. loader_ctx->frame_ref =
  9469. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9470. total_size = (uint32)(sizeof(uint8)
  9471. * (frame_ref_old - frame_ref_after_popped));
  9472. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9473. frame_ref_buf, total_size);
  9474. #if WASM_ENABLE_GC != 0
  9475. /* The stack operand num should not be smaller than before
  9476. after pop and push operations */
  9477. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9478. loader_ctx->reftype_map_num = reftype_map_num_old;
  9479. loader_ctx->frame_reftype_map =
  9480. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9481. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9482. * (frame_reftype_map_old
  9483. - frame_reftype_map_after_popped));
  9484. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9485. total_size, frame_reftype_map_buf, total_size);
  9486. #endif
  9487. #if WASM_ENABLE_FAST_INTERP != 0
  9488. loader_ctx->frame_offset =
  9489. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9490. total_size =
  9491. (uint32)(sizeof(int16)
  9492. * (frame_offset_old - frame_offset_after_popped));
  9493. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9494. total_size, frame_offset_buf, total_size);
  9495. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9496. #endif
  9497. }
  9498. ret = true;
  9499. goto cleanup_and_return;
  9500. }
  9501. available_stack_cell =
  9502. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9503. #if WASM_ENABLE_GC != 0
  9504. available_reftype_map =
  9505. (int32)(loader_ctx->reftype_map_num
  9506. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9507. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9508. #endif
  9509. /* Check stack top values match target block type */
  9510. for (i = (int32)arity - 1; i >= 0; i--) {
  9511. type = types[i];
  9512. #if WASM_ENABLE_GC != 0
  9513. ref_type = NULL;
  9514. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9515. if (is_type_multi_byte) {
  9516. bh_assert(reftype_map);
  9517. ref_type = reftype_map->ref_type;
  9518. }
  9519. #endif
  9520. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9521. break;
  9522. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9523. #if WASM_ENABLE_GC != 0
  9524. frame_reftype_map, available_reftype_map,
  9525. #endif
  9526. type,
  9527. #if WASM_ENABLE_GC != 0
  9528. ref_type,
  9529. #endif
  9530. error_buf, error_buf_size)) {
  9531. goto fail;
  9532. }
  9533. cell_num = wasm_value_type_cell_num(types[i]);
  9534. frame_ref -= cell_num;
  9535. available_stack_cell -= cell_num;
  9536. #if WASM_ENABLE_GC != 0
  9537. if (is_type_multi_byte) {
  9538. frame_reftype_map--;
  9539. available_reftype_map--;
  9540. reftype_map--;
  9541. }
  9542. #endif
  9543. }
  9544. #if WASM_ENABLE_FAST_INTERP != 0
  9545. emit_br_info(target_block, opcode == WASM_OP_BR);
  9546. #endif
  9547. ret = true;
  9548. cleanup_and_return:
  9549. fail:
  9550. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9551. wasm_runtime_free(frame_ref_buf);
  9552. #if WASM_ENABLE_GC != 0
  9553. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9554. wasm_runtime_free(frame_reftype_map_buf);
  9555. #endif
  9556. #if WASM_ENABLE_FAST_INTERP != 0
  9557. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9558. wasm_runtime_free(frame_offset_buf);
  9559. #endif
  9560. return ret;
  9561. }
  9562. static BranchBlock *
  9563. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9564. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9565. {
  9566. uint8 *p = *p_buf, *p_end = buf_end;
  9567. BranchBlock *frame_csp_tmp;
  9568. uint32 depth;
  9569. read_leb_uint32(p, p_end, depth);
  9570. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9571. error_buf_size)) {
  9572. goto fail;
  9573. }
  9574. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9575. *p_buf = p;
  9576. return frame_csp_tmp;
  9577. fail:
  9578. return NULL;
  9579. }
  9580. #if WASM_ENABLE_EXCE_HANDLING != 0
  9581. static BranchBlock *
  9582. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9583. uint8 *buf_end, char *error_buf,
  9584. uint32 error_buf_size)
  9585. {
  9586. uint8 *p = *p_buf, *p_end = buf_end;
  9587. BranchBlock *frame_csp_tmp;
  9588. uint32 depth;
  9589. read_leb_uint32(p, p_end, depth);
  9590. /*
  9591. * Note: "delegate 0" means the surrounding block, not the
  9592. * try-delegate block itself.
  9593. *
  9594. * Note: the caller hasn't popped the try-delegate frame yet.
  9595. */
  9596. bh_assert(loader_ctx->csp_num > 0);
  9597. if (loader_ctx->csp_num - 1 <= depth) {
  9598. #if WASM_ENABLE_SPEC_TEST == 0
  9599. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9600. #else
  9601. set_error_buf(error_buf, error_buf_size, "unknown label");
  9602. #endif
  9603. goto fail;
  9604. }
  9605. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9606. #if WASM_ENABLE_FAST_INTERP != 0
  9607. emit_br_info(frame_csp_tmp, false);
  9608. #endif
  9609. *p_buf = p;
  9610. return frame_csp_tmp;
  9611. fail:
  9612. return NULL;
  9613. }
  9614. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9615. static bool
  9616. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9617. char *error_buf, uint32 error_buf_size)
  9618. {
  9619. BlockType *block_type = &block->block_type;
  9620. uint8 *return_types = NULL;
  9621. uint32 return_count = 0;
  9622. int32 available_stack_cell, return_cell_num, i;
  9623. uint8 *frame_ref = NULL;
  9624. #if WASM_ENABLE_GC != 0
  9625. WASMRefTypeMap *frame_reftype_map;
  9626. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9627. WASMRefType *ref_type;
  9628. uint32 param_count, return_reftype_map_count = 0;
  9629. int32 available_reftype_map =
  9630. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9631. #endif
  9632. available_stack_cell =
  9633. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9634. #if WASM_ENABLE_GC == 0
  9635. return_count = block_type_get_result_types(block_type, &return_types);
  9636. #else
  9637. return_count = block_type_get_result_types(block_type, &return_types,
  9638. &return_reftype_maps,
  9639. &return_reftype_map_count);
  9640. param_count =
  9641. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9642. (void)param_count;
  9643. #endif
  9644. return_cell_num =
  9645. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9646. /* If the stack is in polymorphic state, just clear the stack
  9647. * and then re-push the values to make the stack top values
  9648. * match block type. */
  9649. if (block->is_stack_polymorphic) {
  9650. #if WASM_ENABLE_GC != 0
  9651. int32 j = return_reftype_map_count - 1;
  9652. #endif
  9653. for (i = (int32)return_count - 1; i >= 0; i--) {
  9654. #if WASM_ENABLE_GC != 0
  9655. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9656. bh_assert(return_reftype_maps[j].index == i + param_count);
  9657. bh_memcpy_s(
  9658. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9659. return_reftype_maps[j].ref_type,
  9660. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9661. j--;
  9662. }
  9663. #endif
  9664. #if WASM_ENABLE_FAST_INTERP != 0
  9665. POP_OFFSET_TYPE(return_types[i]);
  9666. #endif
  9667. POP_TYPE(return_types[i]);
  9668. }
  9669. /* Check stack is empty */
  9670. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9671. set_error_buf(
  9672. error_buf, error_buf_size,
  9673. "type mismatch: stack size does not match block type");
  9674. goto fail;
  9675. }
  9676. #if WASM_ENABLE_GC != 0
  9677. j = 0;
  9678. #endif
  9679. for (i = 0; i < (int32)return_count; i++) {
  9680. #if WASM_ENABLE_GC != 0
  9681. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9682. bh_assert(return_reftype_maps[j].index == i + param_count);
  9683. bh_memcpy_s(
  9684. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9685. return_reftype_maps[j].ref_type,
  9686. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9687. j++;
  9688. }
  9689. #endif
  9690. #if WASM_ENABLE_FAST_INTERP != 0
  9691. bool disable_emit = true;
  9692. int16 operand_offset = 0;
  9693. PUSH_OFFSET_TYPE(return_types[i]);
  9694. #endif
  9695. PUSH_TYPE(return_types[i]);
  9696. }
  9697. return true;
  9698. }
  9699. if (available_stack_cell != return_cell_num) {
  9700. #if WASM_ENABLE_EXCE_HANDLING != 0
  9701. /* testspec: this error message format is expected by try_catch.wast */
  9702. snprintf(
  9703. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9704. block->label_type == LABEL_TYPE_TRY
  9705. || (block->label_type == LABEL_TYPE_CATCH
  9706. && return_cell_num > 0)
  9707. ? "instruction"
  9708. : "block",
  9709. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9710. " but stack has ",
  9711. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9712. : "");
  9713. goto fail;
  9714. #else
  9715. set_error_buf(error_buf, error_buf_size,
  9716. "type mismatch: stack size does not match block type");
  9717. goto fail;
  9718. #endif
  9719. }
  9720. /* Check stack values match return types */
  9721. frame_ref = loader_ctx->frame_ref;
  9722. #if WASM_ENABLE_GC != 0
  9723. frame_reftype_map = loader_ctx->frame_reftype_map;
  9724. return_reftype_map =
  9725. return_reftype_map_count
  9726. ? return_reftype_maps + return_reftype_map_count - 1
  9727. : NULL;
  9728. #endif
  9729. for (i = (int32)return_count - 1; i >= 0; i--) {
  9730. uint8 type = return_types[i];
  9731. #if WASM_ENABLE_GC != 0
  9732. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9733. ref_type = NULL;
  9734. if (is_type_multi_byte) {
  9735. bh_assert(return_reftype_map);
  9736. ref_type = return_reftype_map->ref_type;
  9737. }
  9738. #endif
  9739. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9740. #if WASM_ENABLE_GC != 0
  9741. frame_reftype_map, available_reftype_map,
  9742. #endif
  9743. type,
  9744. #if WASM_ENABLE_GC != 0
  9745. ref_type,
  9746. #endif
  9747. error_buf, error_buf_size))
  9748. return false;
  9749. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9750. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9751. #if WASM_ENABLE_GC != 0
  9752. if (is_type_multi_byte) {
  9753. frame_reftype_map--;
  9754. available_reftype_map--;
  9755. return_reftype_map--;
  9756. }
  9757. #endif
  9758. }
  9759. return true;
  9760. fail:
  9761. return false;
  9762. }
  9763. #if WASM_ENABLE_FAST_INTERP != 0
  9764. /* Copy parameters to dynamic space.
  9765. * 1) POP original parameter out;
  9766. * 2) Push and copy original values to dynamic space.
  9767. * The copy instruction format:
  9768. * Part a: param count
  9769. * Part b: all param total cell num
  9770. * Part c: each param's cell_num, src offset and dst offset
  9771. * Part d: each param's src offset
  9772. * Part e: each param's dst offset
  9773. */
  9774. static bool
  9775. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9776. uint32 error_buf_size)
  9777. {
  9778. bool ret = false;
  9779. int16 *frame_offset = NULL;
  9780. uint8 *cells = NULL, cell;
  9781. int16 *src_offsets = NULL;
  9782. uint8 *emit_data = NULL;
  9783. uint32 i;
  9784. BranchBlock *block = loader_ctx->frame_csp - 1;
  9785. BlockType *block_type = &block->block_type;
  9786. WASMFuncType *wasm_type = block_type->u.type;
  9787. uint32 param_count = block_type->u.type->param_count;
  9788. int16 condition_offset = 0;
  9789. bool disable_emit = false;
  9790. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9791. int16 operand_offset = 0;
  9792. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9793. bh_assert(size > 0);
  9794. /* For if block, we also need copy the condition operand offset. */
  9795. if (is_if_block)
  9796. size += sizeof(*cells) + sizeof(*src_offsets);
  9797. /* Allocate memory for the emit data */
  9798. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9799. return false;
  9800. cells = emit_data;
  9801. src_offsets = (int16 *)(cells + param_count);
  9802. if (is_if_block)
  9803. condition_offset = *loader_ctx->frame_offset;
  9804. /* POP original parameter out */
  9805. for (i = 0; i < param_count; i++) {
  9806. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9807. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9808. }
  9809. frame_offset = loader_ctx->frame_offset;
  9810. /* Get each param's cell num and src offset */
  9811. for (i = 0; i < param_count; i++) {
  9812. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9813. cells[i] = cell;
  9814. src_offsets[i] = *frame_offset;
  9815. frame_offset += cell;
  9816. }
  9817. /* emit copy instruction */
  9818. emit_label(EXT_OP_COPY_STACK_VALUES);
  9819. /* Part a) */
  9820. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9821. /* Part b) */
  9822. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9823. : wasm_type->param_cell_num);
  9824. /* Part c) */
  9825. for (i = 0; i < param_count; i++)
  9826. emit_byte(loader_ctx, cells[i]);
  9827. if (is_if_block)
  9828. emit_byte(loader_ctx, 1);
  9829. /* Part d) */
  9830. for (i = 0; i < param_count; i++)
  9831. emit_operand(loader_ctx, src_offsets[i]);
  9832. if (is_if_block)
  9833. emit_operand(loader_ctx, condition_offset);
  9834. /* Since the start offset to save the block's params and
  9835. * the start offset to save the block's results may be
  9836. * different, we remember the dynamic offset for loop block
  9837. * so that we can use it to copy the stack operands to the
  9838. * loop block's params in wasm_loader_emit_br_info. */
  9839. if (block->label_type == LABEL_TYPE_LOOP)
  9840. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9841. /* Part e) */
  9842. /* Push to dynamic space. The push will emit the dst offset. */
  9843. for (i = 0; i < param_count; i++)
  9844. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9845. if (is_if_block)
  9846. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9847. ret = true;
  9848. fail:
  9849. /* Free the emit data */
  9850. wasm_runtime_free(emit_data);
  9851. return ret;
  9852. }
  9853. #endif
  9854. #if WASM_ENABLE_GC == 0
  9855. #define RESET_REFTYPE_MAP_STACK() (void)0
  9856. #else
  9857. #define RESET_REFTYPE_MAP_STACK() \
  9858. do { \
  9859. loader_ctx->reftype_map_num = \
  9860. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9861. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9862. + loader_ctx->reftype_map_num; \
  9863. } while (0)
  9864. #endif
  9865. /* reset the stack to the state of before entering the last block */
  9866. #if WASM_ENABLE_FAST_INTERP != 0
  9867. #define RESET_STACK() \
  9868. do { \
  9869. loader_ctx->stack_cell_num = \
  9870. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9871. loader_ctx->frame_ref = \
  9872. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9873. loader_ctx->frame_offset = \
  9874. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9875. RESET_REFTYPE_MAP_STACK(); \
  9876. } while (0)
  9877. #else
  9878. #define RESET_STACK() \
  9879. do { \
  9880. loader_ctx->stack_cell_num = \
  9881. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9882. loader_ctx->frame_ref = \
  9883. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9884. RESET_REFTYPE_MAP_STACK(); \
  9885. } while (0)
  9886. #endif
  9887. /* set current block's stack polymorphic state */
  9888. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9889. do { \
  9890. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9891. _cur_block->is_stack_polymorphic = flag; \
  9892. } while (0)
  9893. #define BLOCK_HAS_PARAM(block_type) \
  9894. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9895. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9896. do { \
  9897. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9898. error_buf_size))) { \
  9899. goto fail; \
  9900. } \
  9901. } while (0)
  9902. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9903. static bool
  9904. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9905. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9906. uint32 error_buf_size)
  9907. {
  9908. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9909. return false;
  9910. }
  9911. if (table_idx < module->import_table_count) {
  9912. if (p_elem_type)
  9913. *p_elem_type =
  9914. module->import_tables[table_idx].u.table.table_type.elem_type;
  9915. #if WASM_ENABLE_GC != 0
  9916. if (p_ref_type)
  9917. *((WASMRefType **)p_ref_type) =
  9918. module->import_tables[table_idx]
  9919. .u.table.table_type.elem_ref_type;
  9920. #endif
  9921. }
  9922. else {
  9923. if (p_elem_type)
  9924. *p_elem_type =
  9925. module->tables[table_idx - module->import_table_count]
  9926. .table_type.elem_type;
  9927. #if WASM_ENABLE_GC != 0
  9928. if (p_ref_type)
  9929. *((WASMRefType **)p_ref_type) =
  9930. module->tables[table_idx - module->import_table_count]
  9931. .table_type.elem_ref_type;
  9932. #endif
  9933. }
  9934. return true;
  9935. }
  9936. static bool
  9937. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9938. uint8 *p_elem_type, void **p_elem_ref_type,
  9939. char *error_buf, uint32 error_buf_size)
  9940. {
  9941. if (table_seg_idx >= module->table_seg_count) {
  9942. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9943. table_seg_idx);
  9944. return false;
  9945. }
  9946. if (p_elem_type) {
  9947. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9948. }
  9949. #if WASM_ENABLE_GC != 0
  9950. if (p_elem_ref_type)
  9951. *((WASMRefType **)p_elem_ref_type) =
  9952. module->table_segments[table_seg_idx].elem_ref_type;
  9953. #endif
  9954. return true;
  9955. }
  9956. #endif
  9957. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9958. const uint8 *
  9959. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9960. uint32 *len)
  9961. {
  9962. WASMCustomSection *section = module->custom_section_list;
  9963. while (section) {
  9964. if ((section->name_len == strlen(name))
  9965. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9966. if (len) {
  9967. *len = section->content_len;
  9968. }
  9969. return section->content_addr;
  9970. }
  9971. section = section->next;
  9972. }
  9973. return NULL;
  9974. }
  9975. #endif
  9976. #if 0
  9977. #define HANDLE_OPCODE(opcode) #opcode
  9978. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9979. #undef HANDLE_OPCODE
  9980. #endif
  9981. #if WASM_ENABLE_FAST_INTERP == 0
  9982. #define pb_read_leb_uint32 read_leb_uint32
  9983. #define pb_read_leb_int32 read_leb_int32
  9984. #define pb_read_leb_int64 read_leb_int64
  9985. #define pb_read_leb_memarg read_leb_memarg
  9986. #define pb_read_leb_mem_offset read_leb_mem_offset
  9987. #else
  9988. /* Read leb without malformed format check */
  9989. static uint64
  9990. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  9991. {
  9992. uint8 *buf = *p_buf;
  9993. uint64 result = 0, byte = 0;
  9994. uint32 shift = 0;
  9995. do {
  9996. byte = *buf++;
  9997. result |= ((byte & 0x7f) << shift);
  9998. shift += 7;
  9999. } while (byte & 0x80);
  10000. if (sign && (shift < maxbits) && (byte & 0x40)) {
  10001. /* Sign extend */
  10002. result |= (~((uint64)0)) << shift;
  10003. }
  10004. *p_buf = buf;
  10005. return result;
  10006. }
  10007. #define pb_read_leb_uint32(p, p_end, res) \
  10008. do { \
  10009. if (!loader_ctx->p_code_compiled) \
  10010. /* Enable format check in the first scan */ \
  10011. read_leb_uint32(p, p_end, res); \
  10012. else \
  10013. /* Disable format check in the second scan */ \
  10014. res = (uint32)read_leb_quick(&p, 32, false); \
  10015. } while (0)
  10016. #define pb_read_leb_int32(p, p_end, res) \
  10017. do { \
  10018. if (!loader_ctx->p_code_compiled) \
  10019. /* Enable format check in the first scan */ \
  10020. read_leb_int32(p, p_end, res); \
  10021. else \
  10022. /* Disable format check in the second scan */ \
  10023. res = (int32)read_leb_quick(&p, 32, true); \
  10024. } while (0)
  10025. #define pb_read_leb_int64(p, p_end, res) \
  10026. do { \
  10027. if (!loader_ctx->p_code_compiled) \
  10028. /* Enable format check in the first scan */ \
  10029. read_leb_int64(p, p_end, res); \
  10030. else \
  10031. /* Disable format check in the second scan */ \
  10032. res = (int64)read_leb_quick(&p, 64, true); \
  10033. } while (0)
  10034. #if WASM_ENABLE_MULTI_MEMORY != 0
  10035. #define pb_read_leb_memarg read_leb_memarg
  10036. #else
  10037. #define pb_read_leb_memarg pb_read_leb_uint32
  10038. #endif
  10039. #if WASM_ENABLE_MEMORY64 != 0
  10040. #define pb_read_leb_mem_offset read_leb_mem_offset
  10041. #else
  10042. #define pb_read_leb_mem_offset pb_read_leb_uint32
  10043. #endif
  10044. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  10045. static bool
  10046. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  10047. uint32 cur_func_idx, char *error_buf,
  10048. uint32 error_buf_size)
  10049. {
  10050. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  10051. uint32 param_count, local_count, global_count;
  10052. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  10053. table_elem_idx_type;
  10054. BlockType func_block_type;
  10055. uint16 *local_offsets, local_offset;
  10056. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  10057. uint32 table_seg_idx, data_seg_idx, count, align, i;
  10058. mem_offset_t mem_offset;
  10059. int32 i32_const = 0;
  10060. int64 i64_const;
  10061. uint8 opcode;
  10062. bool return_value = false;
  10063. WASMLoaderContext *loader_ctx;
  10064. BranchBlock *frame_csp_tmp;
  10065. #if WASM_ENABLE_GC != 0
  10066. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  10067. uint32 param_reftype_map_count, local_reftype_map_count;
  10068. int32 heap_type;
  10069. WASMRefType wasm_ref_type = { 0 };
  10070. bool need_ref_type_map;
  10071. #endif
  10072. #if WASM_ENABLE_FAST_INTERP != 0
  10073. int16 operand_offset = 0;
  10074. uint8 last_op = 0;
  10075. bool disable_emit, preserve_local = false, if_condition_available = true;
  10076. float32 f32_const;
  10077. float64 f64_const;
  10078. /*
  10079. * It means that the fast interpreter detected an exception while preparing,
  10080. * typically near the block opcode, but it did not immediately trigger
  10081. * the exception. The loader should be capable of identifying it near
  10082. * the end opcode and then raising the exception.
  10083. */
  10084. bool pending_exception = false;
  10085. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  10086. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  10087. #endif
  10088. #if WASM_ENABLE_MEMORY64 != 0
  10089. bool is_memory64 = has_module_memory64(module);
  10090. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  10091. #else
  10092. mem_offset_type = VALUE_TYPE_I32;
  10093. table_elem_idx_type = VALUE_TYPE_I32;
  10094. #endif
  10095. uint32 memidx;
  10096. global_count = module->import_global_count + module->global_count;
  10097. param_count = func->func_type->param_count;
  10098. param_types = func->func_type->types;
  10099. func_block_type.is_value_type = false;
  10100. func_block_type.u.type = func->func_type;
  10101. local_count = func->local_count;
  10102. local_types = func->local_types;
  10103. local_offsets = func->local_offsets;
  10104. #if WASM_ENABLE_GC != 0
  10105. param_reftype_maps = func->func_type->ref_type_maps;
  10106. param_reftype_map_count = func->func_type->ref_type_map_count;
  10107. local_reftype_maps = func->local_ref_type_maps;
  10108. local_reftype_map_count = func->local_ref_type_map_count;
  10109. #endif
  10110. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  10111. goto fail;
  10112. }
  10113. #if WASM_ENABLE_GC != 0
  10114. loader_ctx->module = module;
  10115. loader_ctx->ref_type_set = module->ref_type_set;
  10116. loader_ctx->ref_type_tmp = &wasm_ref_type;
  10117. #endif
  10118. #if WASM_ENABLE_FAST_INTERP != 0
  10119. /* For the first traverse, the initial value of preserved_local_offset has
  10120. * not been determined, we use the INT16_MAX to represent that a slot has
  10121. * been copied to preserve space. For second traverse, this field will be
  10122. * set to the appropriate value in wasm_loader_ctx_reinit.
  10123. * This is for Issue #1230,
  10124. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  10125. * drop opcodes need to know which slots are preserved, so those slots will
  10126. * not be treated as dynamically allocated slots */
  10127. loader_ctx->preserved_local_offset = INT16_MAX;
  10128. re_scan:
  10129. if (loader_ctx->code_compiled_size > 0) {
  10130. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  10131. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  10132. goto fail;
  10133. }
  10134. p = func->code;
  10135. func->code_compiled = loader_ctx->p_code_compiled;
  10136. func->code_compiled_size = loader_ctx->code_compiled_size;
  10137. if (loader_ctx->i64_const_num > 0) {
  10138. int64 *i64_consts_old = loader_ctx->i64_consts;
  10139. /* Sort the i64 consts */
  10140. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  10141. cmp_i64_const);
  10142. /* Remove the duplicated i64 consts */
  10143. uint32 k = 1;
  10144. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  10145. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  10146. i64_consts_old[k++] = i64_consts_old[i];
  10147. }
  10148. }
  10149. if (k < loader_ctx->i64_const_num) {
  10150. int64 *i64_consts_new;
  10151. /* Try to reallocate memory with a smaller size */
  10152. if ((i64_consts_new =
  10153. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  10154. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  10155. i64_consts_old, (uint32)sizeof(int64) * k);
  10156. /* Free the old memory */
  10157. wasm_runtime_free(i64_consts_old);
  10158. loader_ctx->i64_consts = i64_consts_new;
  10159. loader_ctx->i64_const_max_num = k;
  10160. }
  10161. loader_ctx->i64_const_num = k;
  10162. }
  10163. }
  10164. if (loader_ctx->v128_const_num > 0) {
  10165. V128 *v128_consts_old = loader_ctx->v128_consts;
  10166. /* Sort the v128 consts */
  10167. qsort(v128_consts_old, loader_ctx->v128_const_num, sizeof(V128),
  10168. cmp_v128_const);
  10169. /* Remove the duplicated v128 consts */
  10170. uint32 k = 1;
  10171. for (i = 1; i < loader_ctx->v128_const_num; i++) {
  10172. if (!(memcmp(&v128_consts_old[i], &v128_consts_old[i - 1],
  10173. sizeof(V128))
  10174. == 0)) {
  10175. v128_consts_old[k++] = v128_consts_old[i];
  10176. }
  10177. }
  10178. if (k < loader_ctx->v128_const_num) {
  10179. V128 *v128_consts_new;
  10180. /* Try to reallocate memory with a smaller size */
  10181. if ((v128_consts_new =
  10182. wasm_runtime_malloc((uint32)sizeof(V128) * k))) {
  10183. bh_memcpy_s(v128_consts_new, (uint32)sizeof(V128) * k,
  10184. v128_consts_old, (uint32)sizeof(V128) * k);
  10185. /* Free the old memory */
  10186. wasm_runtime_free(v128_consts_old);
  10187. loader_ctx->v128_consts = v128_consts_new;
  10188. loader_ctx->v128_const_max_num = k;
  10189. }
  10190. loader_ctx->v128_const_num = k;
  10191. }
  10192. }
  10193. if (loader_ctx->i32_const_num > 0) {
  10194. int32 *i32_consts_old = loader_ctx->i32_consts;
  10195. /* Sort the i32 consts */
  10196. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10197. cmp_i32_const);
  10198. /* Remove the duplicated i32 consts */
  10199. uint32 k = 1;
  10200. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10201. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10202. i32_consts_old[k++] = i32_consts_old[i];
  10203. }
  10204. }
  10205. if (k < loader_ctx->i32_const_num) {
  10206. int32 *i32_consts_new;
  10207. /* Try to reallocate memory with a smaller size */
  10208. if ((i32_consts_new =
  10209. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10210. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10211. i32_consts_old, (uint32)sizeof(int32) * k);
  10212. /* Free the old memory */
  10213. wasm_runtime_free(i32_consts_old);
  10214. loader_ctx->i32_consts = i32_consts_new;
  10215. loader_ctx->i32_const_max_num = k;
  10216. }
  10217. loader_ctx->i32_const_num = k;
  10218. }
  10219. }
  10220. }
  10221. #endif
  10222. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10223. while (p < p_end) {
  10224. opcode = *p++;
  10225. #if WASM_ENABLE_FAST_INTERP != 0
  10226. p_org = p;
  10227. disable_emit = false;
  10228. emit_label(opcode);
  10229. #endif
  10230. switch (opcode) {
  10231. case WASM_OP_UNREACHABLE:
  10232. RESET_STACK();
  10233. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10234. break;
  10235. case WASM_OP_NOP:
  10236. #if WASM_ENABLE_FAST_INTERP != 0
  10237. skip_label();
  10238. #endif
  10239. break;
  10240. case WASM_OP_IF:
  10241. {
  10242. #if WASM_ENABLE_FAST_INTERP != 0
  10243. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10244. int32 available_stack_cell =
  10245. (int32)(loader_ctx->stack_cell_num
  10246. - parent_block->stack_cell_num);
  10247. if (available_stack_cell <= 0
  10248. && parent_block->is_stack_polymorphic)
  10249. if_condition_available = false;
  10250. else
  10251. if_condition_available = true;
  10252. PRESERVE_LOCAL_FOR_BLOCK();
  10253. #endif
  10254. #if WASM_ENABLE_GC == 0
  10255. POP_I32();
  10256. #endif
  10257. goto handle_op_block_and_loop;
  10258. }
  10259. case WASM_OP_BLOCK:
  10260. case WASM_OP_LOOP:
  10261. #if WASM_ENABLE_EXCE_HANDLING != 0
  10262. case WASM_OP_TRY:
  10263. if (opcode == WASM_OP_TRY) {
  10264. /*
  10265. * keep track of exception handlers to account for
  10266. * memory allocation
  10267. */
  10268. func->exception_handler_count++;
  10269. /*
  10270. * try is a block
  10271. * do nothing special, but execution continues to
  10272. * to handle_op_block_and_loop,
  10273. * and that be pushes the csp
  10274. */
  10275. }
  10276. #endif
  10277. #if WASM_ENABLE_FAST_INTERP != 0
  10278. PRESERVE_LOCAL_FOR_BLOCK();
  10279. #endif
  10280. handle_op_block_and_loop:
  10281. {
  10282. uint8 value_type;
  10283. BlockType block_type;
  10284. #if WASM_ENABLE_FAST_INTERP != 0
  10285. uint32 available_params = 0;
  10286. #endif
  10287. CHECK_BUF(p, p_end, 1);
  10288. value_type = read_uint8(p);
  10289. if (is_byte_a_type(value_type)) {
  10290. /* If the first byte is one of these special values:
  10291. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10292. * the single return value. */
  10293. block_type.is_value_type = true;
  10294. block_type.u.value_type.type = value_type;
  10295. #if WASM_ENABLE_WAMR_COMPILER != 0
  10296. if (value_type == VALUE_TYPE_V128)
  10297. module->is_simd_used = true;
  10298. else if (value_type == VALUE_TYPE_FUNCREF
  10299. || value_type == VALUE_TYPE_EXTERNREF)
  10300. module->is_ref_types_used = true;
  10301. #endif
  10302. #if WASM_ENABLE_GC != 0
  10303. if (value_type != VALUE_TYPE_VOID) {
  10304. p_org = p;
  10305. p--;
  10306. if (!resolve_value_type((const uint8 **)&p, p_end,
  10307. module, module->type_count,
  10308. &need_ref_type_map,
  10309. &wasm_ref_type, false,
  10310. error_buf, error_buf_size)) {
  10311. goto fail;
  10312. }
  10313. if (need_ref_type_map) {
  10314. block_type.u.value_type.ref_type_map.index = 0;
  10315. if (!(block_type.u.value_type.ref_type_map
  10316. .ref_type = reftype_set_insert(
  10317. module->ref_type_set, &wasm_ref_type,
  10318. error_buf, error_buf_size))) {
  10319. goto fail;
  10320. }
  10321. }
  10322. /* Set again as the type might be changed, e.g.
  10323. (ref null any) to anyref */
  10324. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10325. #if WASM_ENABLE_FAST_INTERP == 0
  10326. while (p_org < p) {
  10327. #if WASM_ENABLE_DEBUG_INTERP != 0
  10328. if (!record_fast_op(module, p_org, *p_org,
  10329. error_buf, error_buf_size)) {
  10330. goto fail;
  10331. }
  10332. #endif
  10333. /* Ignore extra bytes for interpreter */
  10334. *p_org++ = WASM_OP_NOP;
  10335. }
  10336. #endif
  10337. }
  10338. #endif /* end of WASM_ENABLE_GC != 0 */
  10339. }
  10340. else {
  10341. int32 type_index;
  10342. /* Resolve the leb128 encoded type index as block type */
  10343. p--;
  10344. p_org = p - 1;
  10345. pb_read_leb_int32(p, p_end, type_index);
  10346. if ((uint32)type_index >= module->type_count) {
  10347. set_error_buf(error_buf, error_buf_size,
  10348. "unknown type");
  10349. goto fail;
  10350. }
  10351. block_type.is_value_type = false;
  10352. block_type.u.type =
  10353. (WASMFuncType *)module->types[type_index];
  10354. #if WASM_ENABLE_FAST_INTERP == 0
  10355. /* If block use type index as block type, change the opcode
  10356. * to new extended opcode so that interpreter can resolve
  10357. * the block quickly.
  10358. */
  10359. #if WASM_ENABLE_DEBUG_INTERP != 0
  10360. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10361. error_buf_size)) {
  10362. goto fail;
  10363. }
  10364. #endif
  10365. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10366. #endif
  10367. }
  10368. #if WASM_ENABLE_GC != 0
  10369. if (opcode == WASM_OP_IF) {
  10370. POP_I32();
  10371. }
  10372. #endif
  10373. /* Pop block parameters from stack */
  10374. if (BLOCK_HAS_PARAM(block_type)) {
  10375. WASMFuncType *wasm_type = block_type.u.type;
  10376. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10377. #if WASM_ENABLE_FAST_INTERP != 0
  10378. uint32 cell_num;
  10379. available_params = block_type.u.type->param_count;
  10380. #endif
  10381. for (i = 0; i < block_type.u.type->param_count; i++) {
  10382. int32 available_stack_cell =
  10383. (int32)(loader_ctx->stack_cell_num
  10384. - cur_block->stack_cell_num);
  10385. if (available_stack_cell <= 0
  10386. && cur_block->is_stack_polymorphic) {
  10387. #if WASM_ENABLE_FAST_INTERP != 0
  10388. available_params = i;
  10389. #endif
  10390. break;
  10391. }
  10392. POP_TYPE(
  10393. wasm_type->types[wasm_type->param_count - i - 1]);
  10394. #if WASM_ENABLE_FAST_INTERP != 0
  10395. /* decrease the frame_offset pointer accordingly to keep
  10396. * consistent with frame_ref stack */
  10397. cell_num = wasm_value_type_cell_num(
  10398. wasm_type->types[wasm_type->param_count - i - 1]);
  10399. loader_ctx->frame_offset -= cell_num;
  10400. if (loader_ctx->frame_offset
  10401. < loader_ctx->frame_offset_bottom) {
  10402. LOG_DEBUG(
  10403. "frame_offset underflow, roll back and "
  10404. "let following stack checker report it\n");
  10405. loader_ctx->frame_offset += cell_num;
  10406. pending_exception = true;
  10407. break;
  10408. }
  10409. #endif
  10410. }
  10411. }
  10412. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10413. block_type, p);
  10414. /* Pass parameters to block */
  10415. if (BLOCK_HAS_PARAM(block_type)) {
  10416. WASMFuncType *func_type = block_type.u.type;
  10417. #if WASM_ENABLE_GC != 0
  10418. WASMRefType *ref_type;
  10419. uint32 j = 0;
  10420. #endif
  10421. for (i = 0; i < func_type->param_count; i++) {
  10422. #if WASM_ENABLE_FAST_INTERP != 0
  10423. uint32 cell_num =
  10424. wasm_value_type_cell_num(func_type->types[i]);
  10425. if (i >= available_params) {
  10426. /* make sure enough space */
  10427. if (loader_ctx->p_code_compiled == NULL) {
  10428. loader_ctx->frame_offset += cell_num;
  10429. if (!check_offset_push(loader_ctx, error_buf,
  10430. error_buf_size))
  10431. goto fail;
  10432. /* for following dummy value assignment */
  10433. loader_ctx->frame_offset -= cell_num;
  10434. }
  10435. /* If there isn't enough data on stack, push a dummy
  10436. * offset to keep the stack consistent with
  10437. * frame_ref.
  10438. * Since the stack is already in polymorphic state,
  10439. * the opcode will not be executed, so the dummy
  10440. * offset won't cause any error */
  10441. for (uint32 n = 0; n < cell_num; n++) {
  10442. *loader_ctx->frame_offset++ = 0;
  10443. }
  10444. }
  10445. else {
  10446. loader_ctx->frame_offset += cell_num;
  10447. }
  10448. #endif
  10449. #if WASM_ENABLE_GC != 0
  10450. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10451. bh_assert(func_type->ref_type_maps[j].index == i);
  10452. ref_type = func_type->ref_type_maps[j].ref_type;
  10453. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10454. ref_type,
  10455. wasm_reftype_struct_size(ref_type));
  10456. j++;
  10457. }
  10458. #endif
  10459. PUSH_TYPE(func_type->types[i]);
  10460. }
  10461. }
  10462. #if WASM_ENABLE_FAST_INTERP != 0
  10463. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10464. skip_label();
  10465. if (BLOCK_HAS_PARAM(block_type)) {
  10466. /* Make sure params are in dynamic space */
  10467. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10468. error_buf_size))
  10469. goto fail;
  10470. }
  10471. if (opcode == WASM_OP_LOOP) {
  10472. (loader_ctx->frame_csp - 1)->code_compiled =
  10473. loader_ctx->p_code_compiled;
  10474. }
  10475. }
  10476. #if WASM_ENABLE_EXCE_HANDLING != 0
  10477. else if (opcode == WASM_OP_TRY) {
  10478. skip_label();
  10479. }
  10480. #endif
  10481. else if (opcode == WASM_OP_IF) {
  10482. BranchBlock *block = loader_ctx->frame_csp - 1;
  10483. /* If block has parameters, we should make sure they are in
  10484. * dynamic space. Otherwise, when else branch is missing,
  10485. * the later opcode may consume incorrect operand offset.
  10486. * Spec case:
  10487. * (func (export "params-id") (param i32) (result i32)
  10488. * (i32.const 1)
  10489. * (i32.const 2)
  10490. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10491. * (then)) (i32.add)
  10492. * )
  10493. *
  10494. * So we should emit a copy instruction before the if.
  10495. *
  10496. * And we also need to save the parameter offsets and
  10497. * recover them before entering else branch.
  10498. *
  10499. */
  10500. if (BLOCK_HAS_PARAM(block_type)) {
  10501. uint64 size;
  10502. /* In polymorphic state, there may be no if condition on
  10503. * the stack, so the offset may not emitted */
  10504. if (if_condition_available) {
  10505. /* skip the if condition operand offset */
  10506. wasm_loader_emit_backspace(loader_ctx,
  10507. sizeof(int16));
  10508. }
  10509. /* skip the if label */
  10510. skip_label();
  10511. /* Emit a copy instruction */
  10512. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10513. error_buf_size))
  10514. goto fail;
  10515. /* Emit the if instruction */
  10516. emit_label(opcode);
  10517. /* Emit the new condition operand offset */
  10518. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10519. /* Save top param_count values of frame_offset stack, so
  10520. * that we can recover it before executing else branch
  10521. */
  10522. size = sizeof(int16)
  10523. * (uint64)block_type.u.type->param_cell_num;
  10524. if (!(block->param_frame_offsets = loader_malloc(
  10525. size, error_buf, error_buf_size)))
  10526. goto fail;
  10527. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10528. loader_ctx->frame_offset
  10529. - size / sizeof(int16),
  10530. (uint32)size);
  10531. }
  10532. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10533. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10534. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10535. }
  10536. #endif
  10537. break;
  10538. }
  10539. #if WASM_ENABLE_EXCE_HANDLING != 0
  10540. case WASM_OP_THROW:
  10541. {
  10542. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10543. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10544. uint8 label_type = cur_block->label_type;
  10545. uint32 tag_index = 0;
  10546. pb_read_leb_int32(p, p_end, tag_index);
  10547. /* check validity of tag_index against module->tag_count */
  10548. /* check tag index is within the tag index space */
  10549. if (tag_index >= module->import_tag_count + module->tag_count) {
  10550. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10551. tag_index);
  10552. goto fail;
  10553. }
  10554. /* the tag_type is stored in either the WASMTag (section tags)
  10555. * or WASMTagImport (import tag) */
  10556. WASMFuncType *tag_type = NULL;
  10557. if (tag_index < module->import_tag_count) {
  10558. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10559. }
  10560. else {
  10561. tag_type =
  10562. module->tags[tag_index - module->import_tag_count]
  10563. ->tag_type;
  10564. }
  10565. if (tag_type->result_count != 0) {
  10566. set_error_buf(error_buf, error_buf_size,
  10567. "tag type signature does not return void");
  10568. goto fail;
  10569. }
  10570. int32 available_stack_cell =
  10571. (int32)(loader_ctx->stack_cell_num
  10572. - cur_block->stack_cell_num);
  10573. int32 tti;
  10574. /* Check stack values match return types by comparing tag param
  10575. * types with stack cells */
  10576. uint8 *frame_ref = loader_ctx->frame_ref;
  10577. #if WASM_ENABLE_GC != 0
  10578. WASMRefTypeMap *frame_reftype_map =
  10579. loader_ctx->frame_reftype_map;
  10580. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10581. /* Temporarily set these values since they may be used in
  10582. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10583. param_reftype_maps = tag_type->ref_type_maps;
  10584. /* For tag_type function, it shouldn't have result_count = 0 */
  10585. param_reftype_map_count = tag_type->ref_type_map_count;
  10586. param_count = tag_type->param_count;
  10587. #endif
  10588. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10589. #if WASM_ENABLE_GC != 0
  10590. local_type = tag_type->types[tti];
  10591. local_idx = tti;
  10592. /* Get the wasm_ref_type if the local_type is multibyte
  10593. type */
  10594. GET_LOCAL_REFTYPE();
  10595. #endif
  10596. if (!check_stack_top_values(
  10597. loader_ctx, frame_ref, available_stack_cell,
  10598. #if WASM_ENABLE_GC != 0
  10599. frame_reftype_map, frame_reftype_map_num,
  10600. #endif
  10601. tag_type->types[tti],
  10602. #if WASM_ENABLE_GC != 0
  10603. &wasm_ref_type,
  10604. #endif
  10605. error_buf, error_buf_size)) {
  10606. snprintf(error_buf, error_buf_size,
  10607. "type mismatch: instruction requires [%s] but "
  10608. "stack has [%s]",
  10609. tag_type->param_count > 0
  10610. ? type2str(tag_type->types[tti])
  10611. : "",
  10612. available_stack_cell > 0
  10613. ? type2str(*(loader_ctx->frame_ref - 1))
  10614. : "");
  10615. goto fail;
  10616. }
  10617. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10618. available_stack_cell -=
  10619. wasm_value_type_cell_num(tag_type->types[tti]);
  10620. }
  10621. #if WASM_ENABLE_GC != 0
  10622. /* Restore the values */
  10623. param_reftype_maps = func->func_type->ref_type_maps;
  10624. param_reftype_map_count = func->func_type->ref_type_map_count;
  10625. param_count = func->func_type->param_count;
  10626. #endif
  10627. /* throw is stack polymorphic */
  10628. (void)label_type;
  10629. RESET_STACK();
  10630. break;
  10631. }
  10632. case WASM_OP_RETHROW:
  10633. {
  10634. /* must be done before checking branch block */
  10635. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10636. /* check the target catching block: LABEL_TYPE_CATCH */
  10637. if (!(frame_csp_tmp =
  10638. check_branch_block(loader_ctx, &p, p_end, opcode,
  10639. error_buf, error_buf_size)))
  10640. goto fail;
  10641. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10642. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10643. /* trap according to spectest (rethrow.wast) */
  10644. set_error_buf(error_buf, error_buf_size,
  10645. "invalid rethrow label");
  10646. goto fail;
  10647. }
  10648. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10649. uint8 label_type = cur_block->label_type;
  10650. (void)label_type;
  10651. /* rethrow is stack polymorphic */
  10652. RESET_STACK();
  10653. break;
  10654. }
  10655. case WASM_OP_DELEGATE:
  10656. {
  10657. /* check target block is valid */
  10658. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10659. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10660. goto fail;
  10661. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10662. uint8 label_type = cur_block->label_type;
  10663. (void)label_type;
  10664. /* DELEGATE ends the block */
  10665. POP_CSP();
  10666. break;
  10667. }
  10668. case WASM_OP_CATCH:
  10669. {
  10670. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10671. uint8 label_type = cur_block->label_type;
  10672. uint32 tag_index = 0;
  10673. pb_read_leb_int32(p, p_end, tag_index);
  10674. /* check validity of tag_index against module->tag_count */
  10675. /* check tag index is within the tag index space */
  10676. if (tag_index >= module->import_tag_count + module->tag_count) {
  10677. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10678. __FUNCTION__);
  10679. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10680. goto fail;
  10681. }
  10682. /* the tag_type is stored in either the WASMTag (section tags)
  10683. * or WASMTagImport (import tag) */
  10684. WASMFuncType *func_type = NULL;
  10685. if (tag_index < module->import_tag_count) {
  10686. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10687. }
  10688. else {
  10689. func_type =
  10690. module->tags[tag_index - module->import_tag_count]
  10691. ->tag_type;
  10692. }
  10693. if (func_type->result_count != 0) {
  10694. set_error_buf(error_buf, error_buf_size,
  10695. "tag type signature does not return void");
  10696. goto fail;
  10697. }
  10698. /* check validity of current label (expect LABEL_TYPE_TRY or
  10699. * LABEL_TYPE_CATCH) */
  10700. if ((LABEL_TYPE_CATCH != label_type)
  10701. && (LABEL_TYPE_TRY != label_type)) {
  10702. set_error_buf(error_buf, error_buf_size,
  10703. "Unexpected block sequence encountered.");
  10704. goto fail;
  10705. }
  10706. /*
  10707. * replace frame_csp by LABEL_TYPE_CATCH
  10708. */
  10709. cur_block->label_type = LABEL_TYPE_CATCH;
  10710. /* RESET_STACK removes the values pushed in TRY or previous
  10711. * CATCH Blocks */
  10712. RESET_STACK();
  10713. #if WASM_ENABLE_GC != 0
  10714. WASMRefType *ref_type;
  10715. uint32 j = 0;
  10716. #endif
  10717. /* push types on the stack according to caught type */
  10718. for (i = 0; i < func_type->param_count; i++) {
  10719. #if WASM_ENABLE_GC != 0
  10720. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10721. bh_assert(func_type->ref_type_maps[j].index == i);
  10722. ref_type = func_type->ref_type_maps[j].ref_type;
  10723. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10724. ref_type,
  10725. wasm_reftype_struct_size(ref_type));
  10726. j++;
  10727. }
  10728. #endif
  10729. PUSH_TYPE(func_type->types[i]);
  10730. }
  10731. break;
  10732. }
  10733. case WASM_OP_CATCH_ALL:
  10734. {
  10735. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10736. /* expecting a TRY or CATCH, anything else will be considered an
  10737. * error */
  10738. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10739. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10740. set_error_buf(error_buf, error_buf_size,
  10741. "Unexpected block sequence encountered.");
  10742. goto fail;
  10743. }
  10744. /* no immediates */
  10745. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10746. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10747. /* RESET_STACK removes the values pushed in TRY or previous
  10748. * CATCH Blocks */
  10749. RESET_STACK();
  10750. /* catch_all has no tagtype and therefore no parameters */
  10751. break;
  10752. }
  10753. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10754. case WASM_OP_ELSE:
  10755. handle_op_else:
  10756. {
  10757. BranchBlock *block = NULL;
  10758. BlockType block_type;
  10759. if (loader_ctx->csp_num < 2
  10760. /* the matched if isn't found */
  10761. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10762. /* duplicated else is found */
  10763. || (loader_ctx->frame_csp - 1)->else_addr) {
  10764. set_error_buf(
  10765. error_buf, error_buf_size,
  10766. "opcode else found without matched opcode if");
  10767. goto fail;
  10768. }
  10769. block = loader_ctx->frame_csp - 1;
  10770. /* check whether if branch's stack matches its result type */
  10771. if (!check_block_stack(loader_ctx, block, error_buf,
  10772. error_buf_size))
  10773. goto fail;
  10774. block->else_addr = p - 1;
  10775. block_type = block->block_type;
  10776. #if WASM_ENABLE_GC != 0
  10777. if (!wasm_loader_init_local_use_masks(
  10778. loader_ctx, local_count, error_buf, error_buf_size)) {
  10779. goto fail;
  10780. }
  10781. #endif
  10782. #if WASM_ENABLE_FAST_INTERP != 0
  10783. /* if the result of if branch is in local or const area, add a
  10784. * copy op */
  10785. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10786. error_buf, error_buf_size)) {
  10787. goto fail;
  10788. }
  10789. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10790. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10791. #endif
  10792. RESET_STACK();
  10793. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10794. /* Pass parameters to if-false branch */
  10795. if (BLOCK_HAS_PARAM(block_type)) {
  10796. for (i = 0; i < block_type.u.type->param_count; i++)
  10797. PUSH_TYPE(block_type.u.type->types[i]);
  10798. }
  10799. #if WASM_ENABLE_FAST_INTERP != 0
  10800. /* Recover top param_count values of frame_offset stack */
  10801. if (BLOCK_HAS_PARAM((block_type))) {
  10802. uint32 size;
  10803. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10804. bh_memcpy_s(loader_ctx->frame_offset, size,
  10805. block->param_frame_offsets, size);
  10806. loader_ctx->frame_offset += (size / sizeof(int16));
  10807. }
  10808. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10809. #endif
  10810. break;
  10811. }
  10812. case WASM_OP_END:
  10813. {
  10814. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10815. /* check whether block stack matches its result type */
  10816. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10817. error_buf_size))
  10818. goto fail;
  10819. /* if there is no else branch, make a virtual else opcode for
  10820. easier integrity check and to copy the correct results to
  10821. the block return address for fast-interp mode:
  10822. change if block from `if ... end` to `if ... else end` */
  10823. if (cur_block->label_type == LABEL_TYPE_IF
  10824. && !cur_block->else_addr) {
  10825. opcode = WASM_OP_ELSE;
  10826. p--;
  10827. #if WASM_ENABLE_FAST_INTERP != 0
  10828. p_org = p;
  10829. skip_label();
  10830. disable_emit = false;
  10831. emit_label(opcode);
  10832. #endif
  10833. goto handle_op_else;
  10834. }
  10835. POP_CSP();
  10836. #if WASM_ENABLE_FAST_INTERP != 0
  10837. skip_label();
  10838. /* copy the result to the block return address */
  10839. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10840. error_buf, error_buf_size)) {
  10841. /* it could be tmp frame_csp allocated from opcode like
  10842. * OP_BR and not counted in loader_ctx->csp_num, it won't
  10843. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  10844. * to free the loader_ctx->frame_csp if fails */
  10845. free_label_patch_list(loader_ctx->frame_csp);
  10846. goto fail;
  10847. }
  10848. apply_label_patch(loader_ctx, 0, PATCH_END);
  10849. free_label_patch_list(loader_ctx->frame_csp);
  10850. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10851. int32 idx;
  10852. uint8 ret_type;
  10853. emit_label(WASM_OP_RETURN);
  10854. for (idx = (int32)func->func_type->result_count - 1;
  10855. idx >= 0; idx--) {
  10856. ret_type = *(func->func_type->types
  10857. + func->func_type->param_count + idx);
  10858. POP_OFFSET_TYPE(ret_type);
  10859. }
  10860. }
  10861. #endif
  10862. if (loader_ctx->csp_num > 0) {
  10863. loader_ctx->frame_csp->end_addr = p - 1;
  10864. }
  10865. else {
  10866. /* end of function block, function will return */
  10867. if (p < p_end) {
  10868. set_error_buf(error_buf, error_buf_size,
  10869. "section size mismatch");
  10870. goto fail;
  10871. }
  10872. }
  10873. #if WASM_ENABLE_FAST_INTERP != 0
  10874. if (pending_exception) {
  10875. set_error_buf(
  10876. error_buf, error_buf_size,
  10877. "There is a pending exception needs to be handled");
  10878. goto fail;
  10879. }
  10880. #endif
  10881. break;
  10882. }
  10883. case WASM_OP_BR:
  10884. {
  10885. if (!(frame_csp_tmp =
  10886. check_branch_block(loader_ctx, &p, p_end, opcode,
  10887. error_buf, error_buf_size)))
  10888. goto fail;
  10889. RESET_STACK();
  10890. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10891. break;
  10892. }
  10893. case WASM_OP_BR_IF:
  10894. {
  10895. POP_I32();
  10896. if (!(frame_csp_tmp =
  10897. check_branch_block(loader_ctx, &p, p_end, opcode,
  10898. error_buf, error_buf_size)))
  10899. goto fail;
  10900. break;
  10901. }
  10902. case WASM_OP_BR_TABLE:
  10903. {
  10904. uint32 depth = 0, default_arity, arity = 0;
  10905. BranchBlock *target_block;
  10906. BlockType *target_block_type;
  10907. #if WASM_ENABLE_FAST_INTERP == 0
  10908. BrTableCache *br_table_cache = NULL;
  10909. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10910. uint32 j;
  10911. #endif
  10912. pb_read_leb_uint32(p, p_end, count);
  10913. #if WASM_ENABLE_FAST_INTERP != 0
  10914. emit_uint32(loader_ctx, count);
  10915. #endif
  10916. POP_I32();
  10917. /* Get each depth and check it */
  10918. p_org = p;
  10919. for (i = 0; i <= count; i++) {
  10920. pb_read_leb_uint32(p, p_end, depth);
  10921. bh_assert(loader_ctx->csp_num > 0);
  10922. if (loader_ctx->csp_num - 1 < depth) {
  10923. set_error_buf(error_buf, error_buf_size,
  10924. "unknown label, "
  10925. "unexpected end of section or function");
  10926. goto fail;
  10927. }
  10928. }
  10929. p = p_org;
  10930. /* Get the default block's arity */
  10931. target_block = loader_ctx->frame_csp - (depth + 1);
  10932. target_block_type = &target_block->block_type;
  10933. default_arity = block_type_get_arity(target_block_type,
  10934. target_block->label_type);
  10935. #if WASM_ENABLE_FAST_INTERP == 0
  10936. p_depth_begin = p_depth = p;
  10937. #endif
  10938. for (i = 0; i <= count; i++) {
  10939. p_org = p;
  10940. pb_read_leb_uint32(p, p_end, depth);
  10941. p = p_org;
  10942. /* Get the target block's arity and check it */
  10943. target_block = loader_ctx->frame_csp - (depth + 1);
  10944. target_block_type = &target_block->block_type;
  10945. arity = block_type_get_arity(target_block_type,
  10946. target_block->label_type);
  10947. if (arity != default_arity) {
  10948. set_error_buf(error_buf, error_buf_size,
  10949. "type mismatch: br_table targets must "
  10950. "all use same result type");
  10951. goto fail;
  10952. }
  10953. if (!(frame_csp_tmp =
  10954. check_branch_block(loader_ctx, &p, p_end, opcode,
  10955. error_buf, error_buf_size))) {
  10956. goto fail;
  10957. }
  10958. #if WASM_ENABLE_FAST_INTERP == 0
  10959. if (br_table_cache) {
  10960. br_table_cache->br_depths[i] = depth;
  10961. }
  10962. else {
  10963. if (depth > 255) {
  10964. /* The depth cannot be stored in one byte,
  10965. create br_table cache to store each depth */
  10966. #if WASM_ENABLE_DEBUG_INTERP != 0
  10967. if (!record_fast_op(module, p_opcode, *p_opcode,
  10968. error_buf, error_buf_size)) {
  10969. goto fail;
  10970. }
  10971. #endif
  10972. if (!(br_table_cache = loader_malloc(
  10973. offsetof(BrTableCache, br_depths)
  10974. + sizeof(uint32)
  10975. * (uint64)(count + 1),
  10976. error_buf, error_buf_size))) {
  10977. goto fail;
  10978. }
  10979. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10980. br_table_cache->br_table_op_addr = p_opcode;
  10981. br_table_cache->br_count = count;
  10982. /* Copy previous depths which are one byte */
  10983. for (j = 0; j < i; j++) {
  10984. br_table_cache->br_depths[j] = p_depth_begin[j];
  10985. }
  10986. br_table_cache->br_depths[i] = depth;
  10987. bh_list_insert(module->br_table_cache_list,
  10988. br_table_cache);
  10989. }
  10990. else {
  10991. /* The depth can be stored in one byte, use the
  10992. byte of the leb to store it */
  10993. *p_depth++ = (uint8)depth;
  10994. }
  10995. }
  10996. #endif
  10997. }
  10998. #if WASM_ENABLE_FAST_INTERP == 0
  10999. /* Set the tailing bytes to nop */
  11000. if (br_table_cache)
  11001. p_depth = p_depth_begin;
  11002. while (p_depth < p)
  11003. *p_depth++ = WASM_OP_NOP;
  11004. #endif
  11005. RESET_STACK();
  11006. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11007. break;
  11008. }
  11009. case WASM_OP_RETURN:
  11010. {
  11011. WASMFuncType *func_type = func->func_type;
  11012. int32 idx;
  11013. uint8 ret_type;
  11014. #if WASM_ENABLE_GC != 0
  11015. uint32 j = func_type->ref_type_map_count - 1;
  11016. #endif
  11017. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  11018. idx--) {
  11019. ret_type =
  11020. *(func_type->types + func_type->param_count + idx);
  11021. #if WASM_ENABLE_GC != 0
  11022. if (wasm_is_type_multi_byte_type(ret_type)) {
  11023. WASMRefType *ref_type =
  11024. func_type->ref_type_maps[j].ref_type;
  11025. bh_assert(func_type->ref_type_maps[j].index
  11026. == func_type->param_count + idx);
  11027. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11028. ref_type,
  11029. wasm_reftype_struct_size(ref_type));
  11030. j--;
  11031. }
  11032. #endif
  11033. #if WASM_ENABLE_FAST_INTERP != 0
  11034. /* emit the offset after return opcode */
  11035. POP_OFFSET_TYPE(ret_type);
  11036. #endif
  11037. POP_TYPE(ret_type);
  11038. }
  11039. RESET_STACK();
  11040. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11041. break;
  11042. }
  11043. case WASM_OP_CALL:
  11044. #if WASM_ENABLE_TAIL_CALL != 0
  11045. case WASM_OP_RETURN_CALL:
  11046. #endif
  11047. #if WASM_ENABLE_GC != 0
  11048. case WASM_OP_CALL_REF:
  11049. case WASM_OP_RETURN_CALL_REF:
  11050. #endif
  11051. {
  11052. WASMFuncType *func_type;
  11053. uint8 type;
  11054. int32 idx;
  11055. #if WASM_ENABLE_GC != 0
  11056. WASMRefType *ref_type;
  11057. uint32 type_idx1;
  11058. int32 j;
  11059. #endif
  11060. #if WASM_ENABLE_GC != 0
  11061. if (opcode == WASM_OP_CALL_REF
  11062. || opcode == WASM_OP_RETURN_CALL_REF) {
  11063. pb_read_leb_uint32(p, p_end, type_idx1);
  11064. if (!check_type_index(module, module->type_count, type_idx1,
  11065. error_buf, error_buf_size)) {
  11066. goto fail;
  11067. }
  11068. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  11069. set_error_buf(error_buf, error_buf_size,
  11070. "unknown function type");
  11071. goto fail;
  11072. }
  11073. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  11074. &type_idx, error_buf,
  11075. error_buf_size)) {
  11076. goto fail;
  11077. }
  11078. if (type == VALUE_TYPE_ANY) {
  11079. type_idx = type_idx1;
  11080. }
  11081. if (!check_type_index(module, module->type_count, type_idx,
  11082. error_buf, error_buf_size)) {
  11083. goto fail;
  11084. }
  11085. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  11086. set_error_buf(error_buf, error_buf_size,
  11087. "unknown function type");
  11088. goto fail;
  11089. }
  11090. if (!wasm_func_type_is_super_of(
  11091. (WASMFuncType *)module->types[type_idx1],
  11092. (WASMFuncType *)module->types[type_idx])) {
  11093. set_error_buf(error_buf, error_buf_size,
  11094. "function type mismatch");
  11095. goto fail;
  11096. }
  11097. func_type = (WASMFuncType *)module->types[type_idx];
  11098. }
  11099. else
  11100. #endif
  11101. {
  11102. pb_read_leb_uint32(p, p_end, func_idx);
  11103. #if WASM_ENABLE_FAST_INTERP != 0
  11104. /* we need to emit func_idx before arguments */
  11105. emit_uint32(loader_ctx, func_idx);
  11106. #endif
  11107. if (!check_function_index(module, func_idx, error_buf,
  11108. error_buf_size)) {
  11109. goto fail;
  11110. }
  11111. if (func_idx < module->import_function_count)
  11112. func_type = module->import_functions[func_idx]
  11113. .u.function.func_type;
  11114. else
  11115. func_type =
  11116. module
  11117. ->functions[func_idx
  11118. - module->import_function_count]
  11119. ->func_type;
  11120. }
  11121. if (func_type->param_count > 0) {
  11122. #if WASM_ENABLE_GC != 0
  11123. j = (int32)(func_type->result_ref_type_maps
  11124. - func_type->ref_type_maps - 1);
  11125. #endif
  11126. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11127. idx--) {
  11128. #if WASM_ENABLE_GC != 0
  11129. if (wasm_is_type_multi_byte_type(
  11130. func_type->types[idx])) {
  11131. ref_type = func_type->ref_type_maps[j].ref_type;
  11132. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11133. ref_type,
  11134. wasm_reftype_struct_size(ref_type));
  11135. j--;
  11136. }
  11137. #endif
  11138. #if WASM_ENABLE_FAST_INTERP != 0
  11139. POP_OFFSET_TYPE(func_type->types[idx]);
  11140. #endif
  11141. POP_TYPE(func_type->types[idx]);
  11142. }
  11143. }
  11144. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11145. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  11146. #endif
  11147. #if WASM_ENABLE_GC != 0
  11148. j = (int32)(func_type->result_ref_type_maps
  11149. - func_type->ref_type_maps);
  11150. #endif
  11151. for (i = 0; i < func_type->result_count; i++) {
  11152. #if WASM_ENABLE_GC != 0
  11153. if (wasm_is_type_multi_byte_type(
  11154. func_type->types[func_type->param_count + i])) {
  11155. ref_type = func_type->ref_type_maps[j].ref_type;
  11156. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11157. ref_type,
  11158. wasm_reftype_struct_size(ref_type));
  11159. j++;
  11160. }
  11161. #endif
  11162. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11163. #if WASM_ENABLE_FAST_INTERP != 0
  11164. /* Here we emit each return value's dynamic_offset. But
  11165. * in fact these offsets are continuous, so interpreter
  11166. * only need to get the first return value's offset.
  11167. */
  11168. PUSH_OFFSET_TYPE(
  11169. func_type->types[func_type->param_count + i]);
  11170. #endif
  11171. }
  11172. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11173. }
  11174. else {
  11175. #if WASM_ENABLE_GC == 0
  11176. if (func_type->result_count
  11177. != func->func_type->result_count) {
  11178. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11179. "type mismatch: expect ",
  11180. func->func_type->result_count,
  11181. " return values but got other");
  11182. goto fail;
  11183. }
  11184. for (i = 0; i < func_type->result_count; i++) {
  11185. type = func->func_type
  11186. ->types[func->func_type->param_count + i];
  11187. if (func_type->types[func_type->param_count + i]
  11188. != type) {
  11189. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11190. "type mismatch: expect ",
  11191. type2str(type), " but got other");
  11192. goto fail;
  11193. }
  11194. }
  11195. #else
  11196. if (!wasm_func_type_result_is_subtype_of(
  11197. func_type, func->func_type, module->types,
  11198. module->type_count)) {
  11199. set_error_buf(
  11200. error_buf, error_buf_size,
  11201. "type mismatch: invalid func result types");
  11202. goto fail;
  11203. }
  11204. #endif
  11205. RESET_STACK();
  11206. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11207. }
  11208. #endif
  11209. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11210. || WASM_ENABLE_WAMR_COMPILER != 0
  11211. func->has_op_func_call = true;
  11212. #endif
  11213. (void)type;
  11214. break;
  11215. }
  11216. /*
  11217. * if disable reference type: call_indirect typeidx, 0x00
  11218. * if enable reference type: call_indirect typeidx, tableidx
  11219. */
  11220. case WASM_OP_CALL_INDIRECT:
  11221. #if WASM_ENABLE_TAIL_CALL != 0
  11222. case WASM_OP_RETURN_CALL_INDIRECT:
  11223. #endif
  11224. {
  11225. int32 idx;
  11226. WASMFuncType *func_type;
  11227. uint32 tbl_elem_type;
  11228. #if WASM_ENABLE_GC != 0
  11229. WASMRefType *elem_ref_type = NULL;
  11230. #endif
  11231. pb_read_leb_uint32(p, p_end, type_idx);
  11232. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11233. #if WASM_ENABLE_WAMR_COMPILER != 0
  11234. if (p + 1 < p_end && *p != 0x00) {
  11235. /*
  11236. * Any non-0x00 byte requires the ref types proposal.
  11237. * This is different from checking the table_idx value
  11238. * since `0x80 0x00` etc. are all valid encodings of zero.
  11239. */
  11240. module->is_ref_types_used = true;
  11241. }
  11242. #endif
  11243. pb_read_leb_uint32(p, p_end, table_idx);
  11244. #else
  11245. CHECK_BUF(p, p_end, 1);
  11246. table_idx = read_uint8(p);
  11247. #endif
  11248. if (!check_table_index(module, table_idx, error_buf,
  11249. error_buf_size)) {
  11250. goto fail;
  11251. }
  11252. tbl_elem_type =
  11253. table_idx < module->import_table_count
  11254. ? module->import_tables[table_idx]
  11255. .u.table.table_type.elem_type
  11256. : module->tables[table_idx - module->import_table_count]
  11257. .table_type.elem_type;
  11258. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11259. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11260. set_error_buf_v(error_buf, error_buf_size,
  11261. "type mismatch: instruction requires table "
  11262. "of functions but table %u has externref",
  11263. table_idx);
  11264. goto fail;
  11265. }
  11266. #elif WASM_ENABLE_GC != 0
  11267. /* Table element must match type ref null func */
  11268. elem_ref_type =
  11269. table_idx < module->import_table_count
  11270. ? module->import_tables[table_idx]
  11271. .u.table.table_type.elem_ref_type
  11272. : module->tables[table_idx - module->import_table_count]
  11273. .table_type.elem_ref_type;
  11274. if (!wasm_reftype_is_subtype_of(
  11275. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11276. module->types, module->type_count)) {
  11277. set_error_buf_v(error_buf, error_buf_size,
  11278. "type mismatch: instruction requires "
  11279. "reference type t match type ref null func"
  11280. "in table %u",
  11281. table_idx);
  11282. goto fail;
  11283. }
  11284. #else
  11285. (void)tbl_elem_type;
  11286. #endif
  11287. #if WASM_ENABLE_FAST_INTERP != 0
  11288. /* we need to emit before arguments */
  11289. #if WASM_ENABLE_TAIL_CALL != 0
  11290. emit_byte(loader_ctx, opcode);
  11291. #endif
  11292. emit_uint32(loader_ctx, type_idx);
  11293. emit_uint32(loader_ctx, table_idx);
  11294. #endif
  11295. #if WASM_ENABLE_MEMORY64 != 0
  11296. table_elem_idx_type = is_table_64bit(module, table_idx)
  11297. ? VALUE_TYPE_I64
  11298. : VALUE_TYPE_I32;
  11299. #endif
  11300. /* skip elem idx */
  11301. POP_TBL_ELEM_IDX();
  11302. if (type_idx >= module->type_count) {
  11303. set_error_buf(error_buf, error_buf_size, "unknown type");
  11304. goto fail;
  11305. }
  11306. func_type = (WASMFuncType *)module->types[type_idx];
  11307. if (func_type->param_count > 0) {
  11308. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11309. idx--) {
  11310. #if WASM_ENABLE_FAST_INTERP != 0
  11311. POP_OFFSET_TYPE(func_type->types[idx]);
  11312. #endif
  11313. POP_TYPE(func_type->types[idx]);
  11314. }
  11315. }
  11316. #if WASM_ENABLE_TAIL_CALL != 0
  11317. if (opcode == WASM_OP_CALL_INDIRECT) {
  11318. #endif
  11319. for (i = 0; i < func_type->result_count; i++) {
  11320. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11321. #if WASM_ENABLE_FAST_INTERP != 0
  11322. PUSH_OFFSET_TYPE(
  11323. func_type->types[func_type->param_count + i]);
  11324. #endif
  11325. }
  11326. #if WASM_ENABLE_TAIL_CALL != 0
  11327. }
  11328. else {
  11329. uint8 type;
  11330. if (func_type->result_count
  11331. != func->func_type->result_count) {
  11332. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11333. "type mismatch: expect ",
  11334. func->func_type->result_count,
  11335. " return values but got other");
  11336. goto fail;
  11337. }
  11338. for (i = 0; i < func_type->result_count; i++) {
  11339. type = func->func_type
  11340. ->types[func->func_type->param_count + i];
  11341. if (func_type->types[func_type->param_count + i]
  11342. != type) {
  11343. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11344. "type mismatch: expect ",
  11345. type2str(type), " but got other");
  11346. goto fail;
  11347. }
  11348. }
  11349. RESET_STACK();
  11350. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11351. }
  11352. #endif
  11353. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11354. || WASM_ENABLE_WAMR_COMPILER != 0
  11355. func->has_op_func_call = true;
  11356. #endif
  11357. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11358. func->has_op_call_indirect = true;
  11359. #endif
  11360. break;
  11361. }
  11362. case WASM_OP_DROP:
  11363. {
  11364. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11365. int32 available_stack_cell =
  11366. (int32)(loader_ctx->stack_cell_num
  11367. - cur_block->stack_cell_num);
  11368. if (available_stack_cell <= 0
  11369. && !cur_block->is_stack_polymorphic) {
  11370. set_error_buf(error_buf, error_buf_size,
  11371. "type mismatch, opcode drop was found "
  11372. "but stack was empty");
  11373. goto fail;
  11374. }
  11375. if (available_stack_cell > 0) {
  11376. #if WASM_ENABLE_GC != 0
  11377. if (wasm_is_type_multi_byte_type(
  11378. *(loader_ctx->frame_ref - 1))) {
  11379. bh_assert((int32)(loader_ctx->reftype_map_num
  11380. - cur_block->reftype_map_num)
  11381. > 0);
  11382. loader_ctx->frame_reftype_map--;
  11383. loader_ctx->reftype_map_num--;
  11384. }
  11385. #endif
  11386. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11387. loader_ctx->frame_ref--;
  11388. loader_ctx->stack_cell_num--;
  11389. #if WASM_ENABLE_FAST_INTERP != 0
  11390. skip_label();
  11391. loader_ctx->frame_offset--;
  11392. if ((*(loader_ctx->frame_offset)
  11393. > loader_ctx->start_dynamic_offset)
  11394. && (*(loader_ctx->frame_offset)
  11395. < loader_ctx->max_dynamic_offset))
  11396. loader_ctx->dynamic_offset--;
  11397. #endif
  11398. }
  11399. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11400. loader_ctx->frame_ref -= 2;
  11401. loader_ctx->stack_cell_num -= 2;
  11402. #if WASM_ENABLE_FAST_INTERP == 0
  11403. *(p - 1) = WASM_OP_DROP_64;
  11404. #endif
  11405. #if WASM_ENABLE_FAST_INTERP != 0
  11406. skip_label();
  11407. loader_ctx->frame_offset -= 2;
  11408. if ((*(loader_ctx->frame_offset)
  11409. > loader_ctx->start_dynamic_offset)
  11410. && (*(loader_ctx->frame_offset)
  11411. < loader_ctx->max_dynamic_offset))
  11412. loader_ctx->dynamic_offset -= 2;
  11413. #endif
  11414. }
  11415. #if WASM_ENABLE_SIMD != 0
  11416. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11417. || (WASM_ENABLE_FAST_INTERP != 0)
  11418. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11419. loader_ctx->frame_ref -= 4;
  11420. loader_ctx->stack_cell_num -= 4;
  11421. #if WASM_ENABLE_FAST_INTERP != 0
  11422. skip_label();
  11423. loader_ctx->frame_offset -= 4;
  11424. if ((*(loader_ctx->frame_offset)
  11425. > loader_ctx->start_dynamic_offset)
  11426. && (*(loader_ctx->frame_offset)
  11427. < loader_ctx->max_dynamic_offset))
  11428. loader_ctx->dynamic_offset -= 4;
  11429. #endif
  11430. }
  11431. #endif
  11432. #endif
  11433. else {
  11434. set_error_buf(error_buf, error_buf_size,
  11435. "type mismatch");
  11436. goto fail;
  11437. }
  11438. }
  11439. else {
  11440. #if WASM_ENABLE_FAST_INTERP != 0
  11441. skip_label();
  11442. #endif
  11443. }
  11444. break;
  11445. }
  11446. case WASM_OP_SELECT:
  11447. {
  11448. uint8 ref_type;
  11449. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11450. int32 available_stack_cell;
  11451. #if WASM_ENABLE_FAST_INTERP != 0
  11452. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11453. #endif
  11454. POP_I32();
  11455. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11456. - cur_block->stack_cell_num);
  11457. if (available_stack_cell <= 0
  11458. && !cur_block->is_stack_polymorphic) {
  11459. set_error_buf(error_buf, error_buf_size,
  11460. "type mismatch or invalid result arity, "
  11461. "opcode select was found "
  11462. "but stack was empty");
  11463. goto fail;
  11464. }
  11465. if (available_stack_cell > 0) {
  11466. switch (*(loader_ctx->frame_ref - 1)) {
  11467. case VALUE_TYPE_I32:
  11468. case VALUE_TYPE_F32:
  11469. case VALUE_TYPE_ANY:
  11470. break;
  11471. case VALUE_TYPE_I64:
  11472. case VALUE_TYPE_F64:
  11473. #if WASM_ENABLE_FAST_INTERP == 0
  11474. *(p - 1) = WASM_OP_SELECT_64;
  11475. #endif
  11476. #if WASM_ENABLE_FAST_INTERP != 0
  11477. if (loader_ctx->p_code_compiled) {
  11478. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11479. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11480. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11481. *(void **)(p_code_compiled_tmp
  11482. - sizeof(void *)) =
  11483. handle_table[opcode_tmp];
  11484. #else
  11485. #if UINTPTR_MAX == UINT64_MAX
  11486. /* emit int32 relative offset in 64-bit target
  11487. */
  11488. int32 offset =
  11489. (int32)((uint8 *)handle_table[opcode_tmp]
  11490. - (uint8 *)handle_table[0]);
  11491. *(int32 *)(p_code_compiled_tmp
  11492. - sizeof(int32)) = offset;
  11493. #else
  11494. /* emit uint32 label address in 32-bit target */
  11495. *(uint32 *)(p_code_compiled_tmp
  11496. - sizeof(uint32)) =
  11497. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11498. #endif
  11499. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11500. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11501. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11502. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11503. #else
  11504. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11505. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11506. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11507. }
  11508. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11509. break;
  11510. #if WASM_ENABLE_SIMD != 0
  11511. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11512. || (WASM_ENABLE_FAST_INTERP != 0)
  11513. case VALUE_TYPE_V128:
  11514. break;
  11515. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  11516. (WASM_ENABLE_FAST_INTERP != 0) */
  11517. #endif /* WASM_ENABLE_SIMD != 0 */
  11518. default:
  11519. {
  11520. set_error_buf(error_buf, error_buf_size,
  11521. "type mismatch");
  11522. goto fail;
  11523. }
  11524. }
  11525. ref_type = *(loader_ctx->frame_ref - 1);
  11526. #if WASM_ENABLE_FAST_INTERP != 0
  11527. POP_OFFSET_TYPE(ref_type);
  11528. POP_TYPE(ref_type);
  11529. POP_OFFSET_TYPE(ref_type);
  11530. POP_TYPE(ref_type);
  11531. PUSH_OFFSET_TYPE(ref_type);
  11532. PUSH_TYPE(ref_type);
  11533. #else
  11534. POP2_AND_PUSH(ref_type, ref_type);
  11535. #endif
  11536. }
  11537. else {
  11538. #if WASM_ENABLE_FAST_INTERP != 0
  11539. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11540. #endif
  11541. PUSH_TYPE(VALUE_TYPE_ANY);
  11542. }
  11543. break;
  11544. }
  11545. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11546. case WASM_OP_SELECT_T:
  11547. {
  11548. uint8 vec_len, type;
  11549. #if WASM_ENABLE_GC != 0
  11550. WASMRefType *ref_type = NULL;
  11551. #endif
  11552. #if WASM_ENABLE_FAST_INTERP != 0
  11553. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11554. #endif
  11555. pb_read_leb_uint32(p, p_end, vec_len);
  11556. if (vec_len != 1) {
  11557. /* typed select must have exactly one result */
  11558. set_error_buf(error_buf, error_buf_size,
  11559. "invalid result arity");
  11560. goto fail;
  11561. }
  11562. #if WASM_ENABLE_GC == 0
  11563. CHECK_BUF(p, p_end, 1);
  11564. type = read_uint8(p);
  11565. if (!is_valid_value_type_for_interpreter(type)) {
  11566. set_error_buf(error_buf, error_buf_size,
  11567. "unknown value type");
  11568. goto fail;
  11569. }
  11570. #else
  11571. p_org = p + 1;
  11572. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11573. module->type_count, &need_ref_type_map,
  11574. &wasm_ref_type, false, error_buf,
  11575. error_buf_size)) {
  11576. goto fail;
  11577. }
  11578. type = wasm_ref_type.ref_type;
  11579. if (need_ref_type_map) {
  11580. if (!(ref_type = reftype_set_insert(
  11581. module->ref_type_set, &wasm_ref_type, error_buf,
  11582. error_buf_size))) {
  11583. goto fail;
  11584. }
  11585. }
  11586. #if WASM_ENABLE_FAST_INTERP == 0
  11587. while (p_org < p) {
  11588. #if WASM_ENABLE_DEBUG_INTERP != 0
  11589. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11590. error_buf_size)) {
  11591. goto fail;
  11592. }
  11593. #endif
  11594. /* Ignore extra bytes for interpreter */
  11595. *p_org++ = WASM_OP_NOP;
  11596. }
  11597. #endif
  11598. #endif /* end of WASM_ENABLE_GC == 0 */
  11599. POP_I32();
  11600. #if WASM_ENABLE_FAST_INTERP != 0
  11601. if (loader_ctx->p_code_compiled) {
  11602. uint8 opcode_tmp = WASM_OP_SELECT;
  11603. if (type == VALUE_TYPE_V128) {
  11604. #if (WASM_ENABLE_SIMD == 0) \
  11605. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11606. && (WASM_ENABLE_FAST_INTERP == 0))
  11607. set_error_buf(error_buf, error_buf_size,
  11608. "SIMD v128 type isn't supported");
  11609. goto fail;
  11610. #endif
  11611. }
  11612. else {
  11613. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11614. opcode_tmp = WASM_OP_SELECT_64;
  11615. #if WASM_ENABLE_GC != 0
  11616. if (wasm_is_type_reftype(type))
  11617. opcode_tmp = WASM_OP_SELECT_T;
  11618. #endif
  11619. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11620. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11621. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11622. handle_table[opcode_tmp];
  11623. #else
  11624. #if UINTPTR_MAX == UINT64_MAX
  11625. /* emit int32 relative offset in 64-bit target */
  11626. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11627. - (uint8 *)handle_table[0]);
  11628. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11629. offset;
  11630. #else
  11631. /* emit uint32 label address in 32-bit target */
  11632. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11633. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11634. #endif
  11635. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11636. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11637. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11638. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11639. #else
  11640. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11641. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11642. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11643. }
  11644. }
  11645. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11646. POP_REF(type);
  11647. #if WASM_ENABLE_GC != 0
  11648. if (need_ref_type_map) {
  11649. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11650. wasm_reftype_struct_size(ref_type));
  11651. }
  11652. #endif
  11653. POP_REF(type);
  11654. #if WASM_ENABLE_GC != 0
  11655. if (need_ref_type_map) {
  11656. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11657. wasm_reftype_struct_size(ref_type));
  11658. }
  11659. #endif
  11660. PUSH_REF(type);
  11661. #if WASM_ENABLE_WAMR_COMPILER != 0
  11662. module->is_ref_types_used = true;
  11663. #endif
  11664. (void)vec_len;
  11665. break;
  11666. }
  11667. /* table.get x. tables[x]. [it] -> [t] */
  11668. /* table.set x. tables[x]. [it t] -> [] */
  11669. case WASM_OP_TABLE_GET:
  11670. case WASM_OP_TABLE_SET:
  11671. {
  11672. uint8 decl_ref_type;
  11673. #if WASM_ENABLE_GC != 0
  11674. WASMRefType *ref_type;
  11675. #endif
  11676. pb_read_leb_uint32(p, p_end, table_idx);
  11677. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11678. #if WASM_ENABLE_GC != 0
  11679. (void **)&ref_type,
  11680. #else
  11681. NULL,
  11682. #endif
  11683. error_buf, error_buf_size))
  11684. goto fail;
  11685. #if WASM_ENABLE_GC != 0
  11686. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11687. bh_assert(ref_type);
  11688. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11689. ref_type, wasm_reftype_struct_size(ref_type));
  11690. }
  11691. #endif
  11692. #if WASM_ENABLE_FAST_INTERP != 0
  11693. emit_uint32(loader_ctx, table_idx);
  11694. #endif
  11695. #if WASM_ENABLE_MEMORY64 != 0
  11696. table_elem_idx_type = is_table_64bit(module, table_idx)
  11697. ? VALUE_TYPE_I64
  11698. : VALUE_TYPE_I32;
  11699. #endif
  11700. if (opcode == WASM_OP_TABLE_GET) {
  11701. POP_TBL_ELEM_IDX();
  11702. #if WASM_ENABLE_FAST_INTERP != 0
  11703. PUSH_OFFSET_TYPE(decl_ref_type);
  11704. #endif
  11705. PUSH_TYPE(decl_ref_type);
  11706. }
  11707. else {
  11708. #if WASM_ENABLE_FAST_INTERP != 0
  11709. POP_OFFSET_TYPE(decl_ref_type);
  11710. #endif
  11711. POP_TYPE(decl_ref_type);
  11712. POP_TBL_ELEM_IDX();
  11713. }
  11714. #if WASM_ENABLE_WAMR_COMPILER != 0
  11715. module->is_ref_types_used = true;
  11716. #endif
  11717. break;
  11718. }
  11719. case WASM_OP_REF_NULL:
  11720. {
  11721. uint8 ref_type;
  11722. #if WASM_ENABLE_GC == 0
  11723. CHECK_BUF(p, p_end, 1);
  11724. ref_type = read_uint8(p);
  11725. if (ref_type != VALUE_TYPE_FUNCREF
  11726. && ref_type != VALUE_TYPE_EXTERNREF) {
  11727. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11728. goto fail;
  11729. }
  11730. #else
  11731. pb_read_leb_int32(p, p_end, heap_type);
  11732. if (heap_type >= 0) {
  11733. if (!check_type_index(module, module->type_count, heap_type,
  11734. error_buf, error_buf_size)) {
  11735. goto fail;
  11736. }
  11737. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11738. true, heap_type);
  11739. ref_type = wasm_ref_type.ref_type;
  11740. }
  11741. else {
  11742. if (!wasm_is_valid_heap_type(heap_type)) {
  11743. set_error_buf(error_buf, error_buf_size,
  11744. "unknown type");
  11745. goto fail;
  11746. }
  11747. ref_type = (uint8)((int32)0x80 + heap_type);
  11748. }
  11749. #endif /* end of WASM_ENABLE_GC == 0 */
  11750. #if WASM_ENABLE_FAST_INTERP != 0
  11751. PUSH_OFFSET_TYPE(ref_type);
  11752. #endif
  11753. PUSH_TYPE(ref_type);
  11754. #if WASM_ENABLE_WAMR_COMPILER != 0
  11755. module->is_ref_types_used = true;
  11756. #endif
  11757. break;
  11758. }
  11759. case WASM_OP_REF_IS_NULL:
  11760. {
  11761. #if WASM_ENABLE_GC == 0
  11762. #if WASM_ENABLE_FAST_INTERP != 0
  11763. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11764. int32 block_stack_cell_num =
  11765. (int32)(loader_ctx->stack_cell_num
  11766. - cur_block->stack_cell_num);
  11767. if (block_stack_cell_num <= 0) {
  11768. if (!cur_block->is_stack_polymorphic) {
  11769. set_error_buf(
  11770. error_buf, error_buf_size,
  11771. "type mismatch: expect data but stack was empty");
  11772. goto fail;
  11773. }
  11774. }
  11775. else {
  11776. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11777. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11778. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11779. if (!wasm_loader_pop_frame_ref_offset(
  11780. loader_ctx, *(loader_ctx->frame_ref - 1),
  11781. error_buf, error_buf_size)) {
  11782. goto fail;
  11783. }
  11784. }
  11785. else {
  11786. set_error_buf(error_buf, error_buf_size,
  11787. "type mismatch");
  11788. goto fail;
  11789. }
  11790. }
  11791. #else
  11792. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11793. error_buf, error_buf_size)
  11794. && !wasm_loader_pop_frame_ref(loader_ctx,
  11795. VALUE_TYPE_EXTERNREF,
  11796. error_buf, error_buf_size)) {
  11797. goto fail;
  11798. }
  11799. #endif
  11800. #else /* else of WASM_ENABLE_GC == 0 */
  11801. uint8 type;
  11802. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11803. error_buf, error_buf_size)) {
  11804. goto fail;
  11805. }
  11806. #endif
  11807. PUSH_I32();
  11808. #if WASM_ENABLE_WAMR_COMPILER != 0
  11809. module->is_ref_types_used = true;
  11810. #endif
  11811. break;
  11812. }
  11813. case WASM_OP_REF_FUNC:
  11814. {
  11815. pb_read_leb_uint32(p, p_end, func_idx);
  11816. if (!check_function_index(module, func_idx, error_buf,
  11817. error_buf_size)) {
  11818. goto fail;
  11819. }
  11820. /* Refer to a forward-declared function:
  11821. the function must be an import, exported, or present in
  11822. a table elem segment or global initializer to be used as
  11823. the operand to ref.func */
  11824. if (func_idx >= module->import_function_count) {
  11825. WASMTableSeg *table_seg = module->table_segments;
  11826. bool func_declared = false;
  11827. uint32 j;
  11828. for (i = 0; i < module->global_count; i++) {
  11829. if (module->globals[i].type.val_type
  11830. == VALUE_TYPE_FUNCREF
  11831. && module->globals[i].init_expr.init_expr_type
  11832. == INIT_EXPR_TYPE_FUNCREF_CONST
  11833. && module->globals[i].init_expr.u.u32 == func_idx) {
  11834. func_declared = true;
  11835. break;
  11836. }
  11837. }
  11838. if (!func_declared) {
  11839. /* Check whether the function is declared in table segs,
  11840. note that it doesn't matter whether the table seg's
  11841. mode is passive, active or declarative. */
  11842. for (i = 0; i < module->table_seg_count;
  11843. i++, table_seg++) {
  11844. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11845. #if WASM_ENABLE_GC != 0
  11846. /* elem type is (ref null? func) or
  11847. (ref null? $t) */
  11848. || ((table_seg->elem_type
  11849. == REF_TYPE_HT_NON_NULLABLE
  11850. || table_seg->elem_type
  11851. == REF_TYPE_HT_NULLABLE)
  11852. && (table_seg->elem_ref_type->ref_ht_common
  11853. .heap_type
  11854. == HEAP_TYPE_FUNC
  11855. || table_seg->elem_ref_type
  11856. ->ref_ht_common.heap_type
  11857. > 0))
  11858. #endif
  11859. ) {
  11860. for (j = 0; j < table_seg->value_count; j++) {
  11861. if (table_seg->init_values[j].u.ref_index
  11862. == func_idx) {
  11863. func_declared = true;
  11864. break;
  11865. }
  11866. }
  11867. }
  11868. }
  11869. }
  11870. if (!func_declared) {
  11871. /* Check whether the function is exported */
  11872. for (i = 0; i < module->export_count; i++) {
  11873. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11874. && module->exports[i].index == func_idx) {
  11875. func_declared = true;
  11876. break;
  11877. }
  11878. }
  11879. }
  11880. if (!func_declared) {
  11881. set_error_buf(error_buf, error_buf_size,
  11882. "undeclared function reference");
  11883. goto fail;
  11884. }
  11885. }
  11886. #if WASM_ENABLE_FAST_INTERP != 0
  11887. emit_uint32(loader_ctx, func_idx);
  11888. #endif
  11889. #if WASM_ENABLE_GC == 0
  11890. PUSH_FUNCREF();
  11891. #else
  11892. if (func_idx < module->import_function_count)
  11893. type_idx =
  11894. module->import_functions[func_idx].u.function.type_idx;
  11895. else
  11896. type_idx = module
  11897. ->functions[func_idx
  11898. - module->import_function_count]
  11899. ->type_idx;
  11900. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11901. false, type_idx);
  11902. PUSH_REF(wasm_ref_type.ref_type);
  11903. #endif
  11904. #if WASM_ENABLE_WAMR_COMPILER != 0
  11905. module->is_ref_types_used = true;
  11906. #endif
  11907. break;
  11908. }
  11909. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11910. #if WASM_ENABLE_GC != 0
  11911. case WASM_OP_REF_AS_NON_NULL:
  11912. case WASM_OP_BR_ON_NULL:
  11913. {
  11914. uint8 type;
  11915. WASMRefType ref_type;
  11916. /* POP (ref null ht) and get the converted (ref ht) */
  11917. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11918. error_buf, error_buf_size)) {
  11919. goto fail;
  11920. }
  11921. if (opcode == WASM_OP_BR_ON_NULL) {
  11922. if (!(frame_csp_tmp =
  11923. check_branch_block(loader_ctx, &p, p_end, opcode,
  11924. error_buf, error_buf_size))) {
  11925. goto fail;
  11926. }
  11927. #if WASM_ENABLE_FAST_INTERP != 0
  11928. disable_emit = true;
  11929. #endif
  11930. }
  11931. /* PUSH the converted (ref ht) */
  11932. if (type != VALUE_TYPE_ANY) {
  11933. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11934. sizeof(WASMRefType));
  11935. }
  11936. PUSH_REF(type);
  11937. break;
  11938. }
  11939. case WASM_OP_BR_ON_NON_NULL:
  11940. {
  11941. uint8 type;
  11942. WASMRefType ref_type;
  11943. uint32 available_stack_cell =
  11944. loader_ctx->stack_cell_num
  11945. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11946. /* POP (ref null ht) and get the converted (ref ht) */
  11947. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11948. error_buf, error_buf_size)) {
  11949. goto fail;
  11950. }
  11951. #if WASM_ENABLE_FAST_INTERP != 0
  11952. disable_emit = true;
  11953. #endif
  11954. /* Temporarily PUSH back (ref ht), check brach block and
  11955. then POP it */
  11956. if (available_stack_cell
  11957. > 0) { /* stack isn't in polymorphic state */
  11958. if (type != VALUE_TYPE_ANY) {
  11959. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11960. &ref_type, sizeof(WASMRefType));
  11961. }
  11962. PUSH_REF(type);
  11963. }
  11964. if (!(frame_csp_tmp =
  11965. check_branch_block(loader_ctx, &p, p_end, opcode,
  11966. error_buf, error_buf_size))) {
  11967. goto fail;
  11968. }
  11969. if (available_stack_cell
  11970. > 0) { /* stack isn't in polymorphic state */
  11971. POP_REF(type);
  11972. #if WASM_ENABLE_FAST_INTERP != 0
  11973. /* Erase the opnd offset emitted by POP_REF() */
  11974. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11975. #endif
  11976. }
  11977. break;
  11978. }
  11979. case WASM_OP_REF_EQ:
  11980. POP_REF(REF_TYPE_EQREF);
  11981. POP_REF(REF_TYPE_EQREF);
  11982. PUSH_I32();
  11983. break;
  11984. #endif /* end of WASM_ENABLE_GC != 0 */
  11985. case WASM_OP_GET_LOCAL:
  11986. {
  11987. p_org = p - 1;
  11988. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11989. PUSH_TYPE(local_type);
  11990. #if WASM_ENABLE_GC != 0
  11991. /* Cannot get a non-nullable and unset local */
  11992. if (local_idx >= param_count
  11993. && wasm_is_reftype_htref_non_nullable(local_type)
  11994. && !wasm_loader_get_local_status(loader_ctx,
  11995. local_idx - param_count)) {
  11996. set_error_buf(error_buf, error_buf_size,
  11997. "uninitialized local");
  11998. goto fail;
  11999. }
  12000. #endif
  12001. #if WASM_ENABLE_FAST_INTERP != 0
  12002. /* Get Local is optimized out */
  12003. skip_label();
  12004. disable_emit = true;
  12005. operand_offset = local_offset;
  12006. PUSH_OFFSET_TYPE(local_type);
  12007. #else
  12008. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12009. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12010. if (local_offset < 0x80
  12011. #if WASM_ENABLE_GC != 0
  12012. && !wasm_is_type_reftype(local_type)
  12013. #endif
  12014. ) {
  12015. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  12016. if (is_32bit_type(local_type)) {
  12017. *p_org++ = (uint8)local_offset;
  12018. }
  12019. else {
  12020. *p_org++ = (uint8)(local_offset | 0x80);
  12021. }
  12022. while (p_org < p) {
  12023. *p_org++ = WASM_OP_NOP;
  12024. }
  12025. }
  12026. #endif
  12027. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12028. break;
  12029. }
  12030. case WASM_OP_SET_LOCAL:
  12031. {
  12032. p_org = p - 1;
  12033. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12034. #if WASM_ENABLE_FAST_INTERP != 0
  12035. if (!(preserve_referenced_local(
  12036. loader_ctx, opcode, local_offset, local_type,
  12037. &preserve_local, error_buf, error_buf_size)))
  12038. goto fail;
  12039. if (local_offset < 256
  12040. #if WASM_ENABLE_GC != 0
  12041. && !wasm_is_type_reftype(local_type)
  12042. #endif
  12043. ) {
  12044. skip_label();
  12045. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  12046. if (loader_ctx->p_code_compiled)
  12047. STORE_U16(loader_ctx->p_code_compiled - 2,
  12048. local_offset);
  12049. loader_ctx->frame_offset--;
  12050. loader_ctx->dynamic_offset--;
  12051. }
  12052. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  12053. if (loader_ctx->p_code_compiled)
  12054. STORE_U16(loader_ctx->p_code_compiled - 2,
  12055. local_offset);
  12056. loader_ctx->frame_offset -= 2;
  12057. loader_ctx->dynamic_offset -= 2;
  12058. }
  12059. else {
  12060. if (is_32bit_type(local_type)) {
  12061. emit_label(EXT_OP_SET_LOCAL_FAST);
  12062. emit_byte(loader_ctx, (uint8)local_offset);
  12063. }
  12064. else if (is_64bit_type(local_type)) {
  12065. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  12066. emit_byte(loader_ctx, (uint8)local_offset);
  12067. }
  12068. #if WASM_ENABLE_SIMDE != 0
  12069. else if (local_type == VALUE_TYPE_V128) {
  12070. emit_label(EXT_OP_SET_LOCAL_FAST_V128);
  12071. emit_byte(loader_ctx, (uint8)local_offset);
  12072. }
  12073. #endif
  12074. else {
  12075. set_error_buf(error_buf, error_buf_size,
  12076. "unknown local type");
  12077. goto fail;
  12078. }
  12079. POP_OFFSET_TYPE(local_type);
  12080. }
  12081. }
  12082. else { /* local index larger than 255, reserve leb */
  12083. emit_uint32(loader_ctx, local_idx);
  12084. POP_OFFSET_TYPE(local_type);
  12085. }
  12086. #else
  12087. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12088. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12089. if (local_offset < 0x80
  12090. #if WASM_ENABLE_GC != 0
  12091. && !wasm_is_type_reftype(local_type)
  12092. #endif
  12093. ) {
  12094. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  12095. if (is_32bit_type(local_type)) {
  12096. *p_org++ = (uint8)local_offset;
  12097. }
  12098. else {
  12099. *p_org++ = (uint8)(local_offset | 0x80);
  12100. }
  12101. while (p_org < p) {
  12102. *p_org++ = WASM_OP_NOP;
  12103. }
  12104. }
  12105. #endif
  12106. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12107. #if WASM_ENABLE_GC != 0
  12108. if (local_idx >= param_count) {
  12109. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12110. }
  12111. #endif
  12112. POP_TYPE(local_type);
  12113. break;
  12114. }
  12115. case WASM_OP_TEE_LOCAL:
  12116. {
  12117. p_org = p - 1;
  12118. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12119. #if WASM_ENABLE_FAST_INTERP != 0
  12120. /* If the stack is in polymorphic state, do fake pop and push on
  12121. offset stack to keep the depth of offset stack to be the
  12122. same with ref stack */
  12123. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12124. if (cur_block->is_stack_polymorphic) {
  12125. POP_OFFSET_TYPE(local_type);
  12126. PUSH_OFFSET_TYPE(local_type);
  12127. }
  12128. #endif
  12129. POP_TYPE(local_type);
  12130. PUSH_TYPE(local_type);
  12131. #if WASM_ENABLE_FAST_INTERP != 0
  12132. if (!(preserve_referenced_local(
  12133. loader_ctx, opcode, local_offset, local_type,
  12134. &preserve_local, error_buf, error_buf_size)))
  12135. goto fail;
  12136. if (local_offset < 256
  12137. #if WASM_ENABLE_GC != 0
  12138. && !wasm_is_type_reftype(local_type)
  12139. #endif
  12140. ) {
  12141. skip_label();
  12142. if (is_32bit_type(local_type)) {
  12143. emit_label(EXT_OP_TEE_LOCAL_FAST);
  12144. emit_byte(loader_ctx, (uint8)local_offset);
  12145. }
  12146. #if WASM_ENABLE_SIMDE != 0
  12147. else if (local_type == VALUE_TYPE_V128) {
  12148. emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
  12149. emit_byte(loader_ctx, (uint8)local_offset);
  12150. }
  12151. #endif
  12152. else {
  12153. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  12154. emit_byte(loader_ctx, (uint8)local_offset);
  12155. }
  12156. }
  12157. else { /* local index larger than 255, reserve leb */
  12158. emit_uint32(loader_ctx, local_idx);
  12159. }
  12160. emit_operand(loader_ctx,
  12161. *(loader_ctx->frame_offset
  12162. - wasm_value_type_cell_num(local_type)));
  12163. #else
  12164. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12165. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12166. if (local_offset < 0x80
  12167. #if WASM_ENABLE_GC != 0
  12168. && !wasm_is_type_reftype(local_type)
  12169. #endif
  12170. ) {
  12171. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  12172. if (is_32bit_type(local_type)) {
  12173. *p_org++ = (uint8)local_offset;
  12174. }
  12175. else {
  12176. *p_org++ = (uint8)(local_offset | 0x80);
  12177. }
  12178. while (p_org < p) {
  12179. *p_org++ = WASM_OP_NOP;
  12180. }
  12181. }
  12182. #endif
  12183. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12184. #if WASM_ENABLE_GC != 0
  12185. if (local_idx >= param_count) {
  12186. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12187. }
  12188. #endif
  12189. break;
  12190. }
  12191. case WASM_OP_GET_GLOBAL:
  12192. {
  12193. #if WASM_ENABLE_GC != 0
  12194. WASMRefType *ref_type;
  12195. #endif
  12196. p_org = p - 1;
  12197. pb_read_leb_uint32(p, p_end, global_idx);
  12198. if (global_idx >= global_count) {
  12199. set_error_buf(error_buf, error_buf_size, "unknown global");
  12200. goto fail;
  12201. }
  12202. global_type = global_idx < module->import_global_count
  12203. ? module->import_globals[global_idx]
  12204. .u.global.type.val_type
  12205. : module
  12206. ->globals[global_idx
  12207. - module->import_global_count]
  12208. .type.val_type;
  12209. #if WASM_ENABLE_GC != 0
  12210. ref_type =
  12211. global_idx < module->import_global_count
  12212. ? module->import_globals[global_idx].u.global.ref_type
  12213. : module
  12214. ->globals[global_idx
  12215. - module->import_global_count]
  12216. .ref_type;
  12217. if (wasm_is_type_multi_byte_type(global_type)) {
  12218. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12219. wasm_reftype_struct_size(ref_type));
  12220. }
  12221. #endif
  12222. PUSH_TYPE(global_type);
  12223. #if WASM_ENABLE_FAST_INTERP == 0
  12224. if (global_type == VALUE_TYPE_I64
  12225. || global_type == VALUE_TYPE_F64) {
  12226. #if WASM_ENABLE_DEBUG_INTERP != 0
  12227. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12228. error_buf_size)) {
  12229. goto fail;
  12230. }
  12231. #endif
  12232. *p_org = WASM_OP_GET_GLOBAL_64;
  12233. }
  12234. #else /* else of WASM_ENABLE_FAST_INTERP */
  12235. if (global_type == VALUE_TYPE_I64
  12236. || global_type == VALUE_TYPE_F64) {
  12237. skip_label();
  12238. emit_label(WASM_OP_GET_GLOBAL_64);
  12239. }
  12240. #if WASM_ENABLE_SIMDE != 0
  12241. if (global_type == VALUE_TYPE_V128) {
  12242. skip_label();
  12243. emit_label(WASM_OP_GET_GLOBAL_V128);
  12244. }
  12245. #endif /* end of WASM_ENABLE_SIMDE */
  12246. emit_uint32(loader_ctx, global_idx);
  12247. PUSH_OFFSET_TYPE(global_type);
  12248. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12249. break;
  12250. }
  12251. case WASM_OP_SET_GLOBAL:
  12252. {
  12253. bool is_mutable = false;
  12254. #if WASM_ENABLE_GC != 0
  12255. WASMRefType *ref_type;
  12256. #endif
  12257. p_org = p - 1;
  12258. pb_read_leb_uint32(p, p_end, global_idx);
  12259. if (global_idx >= global_count) {
  12260. set_error_buf(error_buf, error_buf_size, "unknown global");
  12261. goto fail;
  12262. }
  12263. is_mutable = global_idx < module->import_global_count
  12264. ? module->import_globals[global_idx]
  12265. .u.global.type.is_mutable
  12266. : module
  12267. ->globals[global_idx
  12268. - module->import_global_count]
  12269. .type.is_mutable;
  12270. if (!is_mutable) {
  12271. #if WASM_ENABLE_GC == 0
  12272. set_error_buf(error_buf, error_buf_size,
  12273. "global is immutable");
  12274. #else
  12275. set_error_buf(error_buf, error_buf_size,
  12276. "immutable global");
  12277. #endif
  12278. goto fail;
  12279. }
  12280. global_type = global_idx < module->import_global_count
  12281. ? module->import_globals[global_idx]
  12282. .u.global.type.val_type
  12283. : module
  12284. ->globals[global_idx
  12285. - module->import_global_count]
  12286. .type.val_type;
  12287. #if WASM_ENABLE_GC != 0
  12288. ref_type =
  12289. global_idx < module->import_global_count
  12290. ? module->import_globals[global_idx].u.global.ref_type
  12291. : module
  12292. ->globals[global_idx
  12293. - module->import_global_count]
  12294. .ref_type;
  12295. if (wasm_is_type_multi_byte_type(global_type)) {
  12296. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12297. wasm_reftype_struct_size(ref_type));
  12298. }
  12299. #endif
  12300. #if WASM_ENABLE_FAST_INTERP == 0
  12301. if (global_type == VALUE_TYPE_I64
  12302. || global_type == VALUE_TYPE_F64) {
  12303. #if WASM_ENABLE_DEBUG_INTERP != 0
  12304. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12305. error_buf_size)) {
  12306. goto fail;
  12307. }
  12308. #endif
  12309. *p_org = WASM_OP_SET_GLOBAL_64;
  12310. }
  12311. else if (module->aux_stack_size > 0
  12312. && global_idx == module->aux_stack_top_global_index) {
  12313. #if WASM_ENABLE_DEBUG_INTERP != 0
  12314. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12315. error_buf_size)) {
  12316. goto fail;
  12317. }
  12318. #endif
  12319. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12320. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12321. func->has_op_set_global_aux_stack = true;
  12322. #endif
  12323. }
  12324. #else /* else of WASM_ENABLE_FAST_INTERP */
  12325. if (global_type == VALUE_TYPE_I64
  12326. || global_type == VALUE_TYPE_F64) {
  12327. skip_label();
  12328. emit_label(WASM_OP_SET_GLOBAL_64);
  12329. }
  12330. else if (module->aux_stack_size > 0
  12331. && global_idx == module->aux_stack_top_global_index) {
  12332. skip_label();
  12333. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12334. }
  12335. #if WASM_ENABLE_SIMDE != 0
  12336. else if (global_type == VALUE_TYPE_V128) {
  12337. skip_label();
  12338. emit_label(WASM_OP_SET_GLOBAL_V128);
  12339. }
  12340. #endif /* end of WASM_ENABLE_SIMDE */
  12341. emit_uint32(loader_ctx, global_idx);
  12342. POP_OFFSET_TYPE(global_type);
  12343. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12344. POP_TYPE(global_type);
  12345. break;
  12346. }
  12347. /* load */
  12348. case WASM_OP_I32_LOAD:
  12349. case WASM_OP_I32_LOAD8_S:
  12350. case WASM_OP_I32_LOAD8_U:
  12351. case WASM_OP_I32_LOAD16_S:
  12352. case WASM_OP_I32_LOAD16_U:
  12353. case WASM_OP_I64_LOAD:
  12354. case WASM_OP_I64_LOAD8_S:
  12355. case WASM_OP_I64_LOAD8_U:
  12356. case WASM_OP_I64_LOAD16_S:
  12357. case WASM_OP_I64_LOAD16_U:
  12358. case WASM_OP_I64_LOAD32_S:
  12359. case WASM_OP_I64_LOAD32_U:
  12360. case WASM_OP_F32_LOAD:
  12361. case WASM_OP_F64_LOAD:
  12362. /* store */
  12363. case WASM_OP_I32_STORE:
  12364. case WASM_OP_I32_STORE8:
  12365. case WASM_OP_I32_STORE16:
  12366. case WASM_OP_I64_STORE:
  12367. case WASM_OP_I64_STORE8:
  12368. case WASM_OP_I64_STORE16:
  12369. case WASM_OP_I64_STORE32:
  12370. case WASM_OP_F32_STORE:
  12371. case WASM_OP_F64_STORE:
  12372. {
  12373. #if WASM_ENABLE_FAST_INTERP != 0
  12374. /* change F32/F64 into I32/I64 */
  12375. if (opcode == WASM_OP_F32_LOAD) {
  12376. skip_label();
  12377. emit_label(WASM_OP_I32_LOAD);
  12378. }
  12379. else if (opcode == WASM_OP_F64_LOAD) {
  12380. skip_label();
  12381. emit_label(WASM_OP_I64_LOAD);
  12382. }
  12383. else if (opcode == WASM_OP_F32_STORE) {
  12384. skip_label();
  12385. emit_label(WASM_OP_I32_STORE);
  12386. }
  12387. else if (opcode == WASM_OP_F64_STORE) {
  12388. skip_label();
  12389. emit_label(WASM_OP_I64_STORE);
  12390. }
  12391. #endif
  12392. CHECK_MEMORY();
  12393. pb_read_leb_memarg(p, p_end, align); /* align */
  12394. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12395. if (!check_memory_access_align(opcode, align, error_buf,
  12396. error_buf_size)) {
  12397. goto fail;
  12398. }
  12399. #if WASM_ENABLE_FAST_INTERP != 0
  12400. emit_uint32(loader_ctx, mem_offset);
  12401. #endif
  12402. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12403. func->has_memory_operations = true;
  12404. #endif
  12405. switch (opcode) {
  12406. /* load */
  12407. case WASM_OP_I32_LOAD:
  12408. case WASM_OP_I32_LOAD8_S:
  12409. case WASM_OP_I32_LOAD8_U:
  12410. case WASM_OP_I32_LOAD16_S:
  12411. case WASM_OP_I32_LOAD16_U:
  12412. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12413. break;
  12414. case WASM_OP_I64_LOAD:
  12415. case WASM_OP_I64_LOAD8_S:
  12416. case WASM_OP_I64_LOAD8_U:
  12417. case WASM_OP_I64_LOAD16_S:
  12418. case WASM_OP_I64_LOAD16_U:
  12419. case WASM_OP_I64_LOAD32_S:
  12420. case WASM_OP_I64_LOAD32_U:
  12421. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12422. break;
  12423. case WASM_OP_F32_LOAD:
  12424. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12425. break;
  12426. case WASM_OP_F64_LOAD:
  12427. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12428. break;
  12429. /* store */
  12430. case WASM_OP_I32_STORE:
  12431. case WASM_OP_I32_STORE8:
  12432. case WASM_OP_I32_STORE16:
  12433. POP_I32();
  12434. POP_MEM_OFFSET();
  12435. break;
  12436. case WASM_OP_I64_STORE:
  12437. case WASM_OP_I64_STORE8:
  12438. case WASM_OP_I64_STORE16:
  12439. case WASM_OP_I64_STORE32:
  12440. POP_I64();
  12441. POP_MEM_OFFSET();
  12442. break;
  12443. case WASM_OP_F32_STORE:
  12444. POP_F32();
  12445. POP_MEM_OFFSET();
  12446. break;
  12447. case WASM_OP_F64_STORE:
  12448. POP_F64();
  12449. POP_MEM_OFFSET();
  12450. break;
  12451. default:
  12452. break;
  12453. }
  12454. break;
  12455. }
  12456. case WASM_OP_MEMORY_SIZE:
  12457. CHECK_MEMORY();
  12458. pb_read_leb_uint32(p, p_end, memidx);
  12459. check_memidx(module, memidx);
  12460. PUSH_PAGE_COUNT();
  12461. module->possible_memory_grow = true;
  12462. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12463. func->has_memory_operations = true;
  12464. #endif
  12465. break;
  12466. case WASM_OP_MEMORY_GROW:
  12467. CHECK_MEMORY();
  12468. pb_read_leb_uint32(p, p_end, memidx);
  12469. check_memidx(module, memidx);
  12470. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12471. module->possible_memory_grow = true;
  12472. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12473. || WASM_ENABLE_WAMR_COMPILER != 0
  12474. func->has_op_memory_grow = true;
  12475. #endif
  12476. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12477. func->has_memory_operations = true;
  12478. #endif
  12479. break;
  12480. case WASM_OP_I32_CONST:
  12481. pb_read_leb_int32(p, p_end, i32_const);
  12482. #if WASM_ENABLE_FAST_INTERP != 0
  12483. skip_label();
  12484. disable_emit = true;
  12485. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12486. if (operand_offset == 0) {
  12487. disable_emit = false;
  12488. emit_label(WASM_OP_I32_CONST);
  12489. emit_uint32(loader_ctx, i32_const);
  12490. }
  12491. #else
  12492. (void)i32_const;
  12493. #endif
  12494. PUSH_I32();
  12495. break;
  12496. case WASM_OP_I64_CONST:
  12497. pb_read_leb_int64(p, p_end, i64_const);
  12498. #if WASM_ENABLE_FAST_INTERP != 0
  12499. skip_label();
  12500. disable_emit = true;
  12501. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12502. if (operand_offset == 0) {
  12503. disable_emit = false;
  12504. emit_label(WASM_OP_I64_CONST);
  12505. emit_uint64(loader_ctx, i64_const);
  12506. }
  12507. #endif
  12508. PUSH_I64();
  12509. break;
  12510. case WASM_OP_F32_CONST:
  12511. CHECK_BUF(p, p_end, sizeof(float32));
  12512. p += sizeof(float32);
  12513. #if WASM_ENABLE_FAST_INTERP != 0
  12514. skip_label();
  12515. disable_emit = true;
  12516. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12517. sizeof(float32));
  12518. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12519. if (operand_offset == 0) {
  12520. disable_emit = false;
  12521. emit_label(WASM_OP_F32_CONST);
  12522. emit_float32(loader_ctx, f32_const);
  12523. }
  12524. #endif
  12525. PUSH_F32();
  12526. break;
  12527. case WASM_OP_F64_CONST:
  12528. CHECK_BUF(p, p_end, sizeof(float64));
  12529. p += sizeof(float64);
  12530. #if WASM_ENABLE_FAST_INTERP != 0
  12531. skip_label();
  12532. disable_emit = true;
  12533. /* Some MCU may require 8-byte align */
  12534. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12535. sizeof(float64));
  12536. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12537. if (operand_offset == 0) {
  12538. disable_emit = false;
  12539. emit_label(WASM_OP_F64_CONST);
  12540. emit_float64(loader_ctx, f64_const);
  12541. }
  12542. #endif
  12543. PUSH_F64();
  12544. break;
  12545. case WASM_OP_I32_EQZ:
  12546. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12547. break;
  12548. case WASM_OP_I32_EQ:
  12549. case WASM_OP_I32_NE:
  12550. case WASM_OP_I32_LT_S:
  12551. case WASM_OP_I32_LT_U:
  12552. case WASM_OP_I32_GT_S:
  12553. case WASM_OP_I32_GT_U:
  12554. case WASM_OP_I32_LE_S:
  12555. case WASM_OP_I32_LE_U:
  12556. case WASM_OP_I32_GE_S:
  12557. case WASM_OP_I32_GE_U:
  12558. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12559. break;
  12560. case WASM_OP_I64_EQZ:
  12561. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12562. break;
  12563. case WASM_OP_I64_EQ:
  12564. case WASM_OP_I64_NE:
  12565. case WASM_OP_I64_LT_S:
  12566. case WASM_OP_I64_LT_U:
  12567. case WASM_OP_I64_GT_S:
  12568. case WASM_OP_I64_GT_U:
  12569. case WASM_OP_I64_LE_S:
  12570. case WASM_OP_I64_LE_U:
  12571. case WASM_OP_I64_GE_S:
  12572. case WASM_OP_I64_GE_U:
  12573. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12574. break;
  12575. case WASM_OP_F32_EQ:
  12576. case WASM_OP_F32_NE:
  12577. case WASM_OP_F32_LT:
  12578. case WASM_OP_F32_GT:
  12579. case WASM_OP_F32_LE:
  12580. case WASM_OP_F32_GE:
  12581. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12582. break;
  12583. case WASM_OP_F64_EQ:
  12584. case WASM_OP_F64_NE:
  12585. case WASM_OP_F64_LT:
  12586. case WASM_OP_F64_GT:
  12587. case WASM_OP_F64_LE:
  12588. case WASM_OP_F64_GE:
  12589. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12590. break;
  12591. case WASM_OP_I32_CLZ:
  12592. case WASM_OP_I32_CTZ:
  12593. case WASM_OP_I32_POPCNT:
  12594. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12595. break;
  12596. case WASM_OP_I32_ADD:
  12597. case WASM_OP_I32_SUB:
  12598. case WASM_OP_I32_MUL:
  12599. case WASM_OP_I32_DIV_S:
  12600. case WASM_OP_I32_DIV_U:
  12601. case WASM_OP_I32_REM_S:
  12602. case WASM_OP_I32_REM_U:
  12603. case WASM_OP_I32_AND:
  12604. case WASM_OP_I32_OR:
  12605. case WASM_OP_I32_XOR:
  12606. case WASM_OP_I32_SHL:
  12607. case WASM_OP_I32_SHR_S:
  12608. case WASM_OP_I32_SHR_U:
  12609. case WASM_OP_I32_ROTL:
  12610. case WASM_OP_I32_ROTR:
  12611. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12612. break;
  12613. case WASM_OP_I64_CLZ:
  12614. case WASM_OP_I64_CTZ:
  12615. case WASM_OP_I64_POPCNT:
  12616. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12617. break;
  12618. case WASM_OP_I64_ADD:
  12619. case WASM_OP_I64_SUB:
  12620. case WASM_OP_I64_MUL:
  12621. case WASM_OP_I64_DIV_S:
  12622. case WASM_OP_I64_DIV_U:
  12623. case WASM_OP_I64_REM_S:
  12624. case WASM_OP_I64_REM_U:
  12625. case WASM_OP_I64_AND:
  12626. case WASM_OP_I64_OR:
  12627. case WASM_OP_I64_XOR:
  12628. case WASM_OP_I64_SHL:
  12629. case WASM_OP_I64_SHR_S:
  12630. case WASM_OP_I64_SHR_U:
  12631. case WASM_OP_I64_ROTL:
  12632. case WASM_OP_I64_ROTR:
  12633. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12634. break;
  12635. case WASM_OP_F32_ABS:
  12636. case WASM_OP_F32_NEG:
  12637. case WASM_OP_F32_CEIL:
  12638. case WASM_OP_F32_FLOOR:
  12639. case WASM_OP_F32_TRUNC:
  12640. case WASM_OP_F32_NEAREST:
  12641. case WASM_OP_F32_SQRT:
  12642. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12643. break;
  12644. case WASM_OP_F32_ADD:
  12645. case WASM_OP_F32_SUB:
  12646. case WASM_OP_F32_MUL:
  12647. case WASM_OP_F32_DIV:
  12648. case WASM_OP_F32_MIN:
  12649. case WASM_OP_F32_MAX:
  12650. case WASM_OP_F32_COPYSIGN:
  12651. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12652. break;
  12653. case WASM_OP_F64_ABS:
  12654. case WASM_OP_F64_NEG:
  12655. case WASM_OP_F64_CEIL:
  12656. case WASM_OP_F64_FLOOR:
  12657. case WASM_OP_F64_TRUNC:
  12658. case WASM_OP_F64_NEAREST:
  12659. case WASM_OP_F64_SQRT:
  12660. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12661. break;
  12662. case WASM_OP_F64_ADD:
  12663. case WASM_OP_F64_SUB:
  12664. case WASM_OP_F64_MUL:
  12665. case WASM_OP_F64_DIV:
  12666. case WASM_OP_F64_MIN:
  12667. case WASM_OP_F64_MAX:
  12668. case WASM_OP_F64_COPYSIGN:
  12669. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12670. break;
  12671. case WASM_OP_I32_WRAP_I64:
  12672. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12673. break;
  12674. case WASM_OP_I32_TRUNC_S_F32:
  12675. case WASM_OP_I32_TRUNC_U_F32:
  12676. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12677. break;
  12678. case WASM_OP_I32_TRUNC_S_F64:
  12679. case WASM_OP_I32_TRUNC_U_F64:
  12680. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12681. break;
  12682. case WASM_OP_I64_EXTEND_S_I32:
  12683. case WASM_OP_I64_EXTEND_U_I32:
  12684. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12685. break;
  12686. case WASM_OP_I64_TRUNC_S_F32:
  12687. case WASM_OP_I64_TRUNC_U_F32:
  12688. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12689. break;
  12690. case WASM_OP_I64_TRUNC_S_F64:
  12691. case WASM_OP_I64_TRUNC_U_F64:
  12692. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12693. break;
  12694. case WASM_OP_F32_CONVERT_S_I32:
  12695. case WASM_OP_F32_CONVERT_U_I32:
  12696. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12697. break;
  12698. case WASM_OP_F32_CONVERT_S_I64:
  12699. case WASM_OP_F32_CONVERT_U_I64:
  12700. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12701. break;
  12702. case WASM_OP_F32_DEMOTE_F64:
  12703. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12704. break;
  12705. case WASM_OP_F64_CONVERT_S_I32:
  12706. case WASM_OP_F64_CONVERT_U_I32:
  12707. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12708. break;
  12709. case WASM_OP_F64_CONVERT_S_I64:
  12710. case WASM_OP_F64_CONVERT_U_I64:
  12711. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12712. break;
  12713. case WASM_OP_F64_PROMOTE_F32:
  12714. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12715. break;
  12716. case WASM_OP_I32_REINTERPRET_F32:
  12717. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12718. break;
  12719. case WASM_OP_I64_REINTERPRET_F64:
  12720. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12721. break;
  12722. case WASM_OP_F32_REINTERPRET_I32:
  12723. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12724. break;
  12725. case WASM_OP_F64_REINTERPRET_I64:
  12726. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12727. break;
  12728. case WASM_OP_I32_EXTEND8_S:
  12729. case WASM_OP_I32_EXTEND16_S:
  12730. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12731. break;
  12732. case WASM_OP_I64_EXTEND8_S:
  12733. case WASM_OP_I64_EXTEND16_S:
  12734. case WASM_OP_I64_EXTEND32_S:
  12735. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12736. break;
  12737. #if WASM_ENABLE_GC != 0
  12738. case WASM_OP_GC_PREFIX:
  12739. {
  12740. uint32 opcode1;
  12741. pb_read_leb_uint32(p, p_end, opcode1);
  12742. #if WASM_ENABLE_FAST_INTERP != 0
  12743. emit_byte(loader_ctx, ((uint8)opcode1));
  12744. #endif
  12745. switch (opcode1) {
  12746. case WASM_OP_STRUCT_NEW:
  12747. case WASM_OP_STRUCT_NEW_DEFAULT:
  12748. {
  12749. pb_read_leb_uint32(p, p_end, type_idx);
  12750. #if WASM_ENABLE_FAST_INTERP != 0
  12751. emit_uint32(loader_ctx, type_idx);
  12752. #endif
  12753. if (!check_type_index(module, module->type_count,
  12754. type_idx, error_buf,
  12755. error_buf_size)) {
  12756. goto fail;
  12757. }
  12758. if (module->types[type_idx]->type_flag
  12759. != WASM_TYPE_STRUCT) {
  12760. set_error_buf(error_buf, error_buf_size,
  12761. "unknown struct type");
  12762. goto fail;
  12763. }
  12764. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12765. int32 j, k;
  12766. uint8 value_type;
  12767. uint32 ref_type_struct_size;
  12768. WASMStructType *struct_type =
  12769. (WASMStructType *)module->types[type_idx];
  12770. k = struct_type->ref_type_map_count - 1;
  12771. for (j = struct_type->field_count - 1; j >= 0;
  12772. j--) {
  12773. value_type = struct_type->fields[j].field_type;
  12774. if (wasm_is_type_reftype(value_type)) {
  12775. if (wasm_is_type_multi_byte_type(
  12776. value_type)) {
  12777. ref_type_struct_size =
  12778. wasm_reftype_struct_size(
  12779. struct_type->ref_type_maps[k]
  12780. .ref_type);
  12781. bh_memcpy_s(
  12782. &wasm_ref_type,
  12783. (uint32)sizeof(WASMRefType),
  12784. struct_type->ref_type_maps[k]
  12785. .ref_type,
  12786. ref_type_struct_size);
  12787. k--;
  12788. }
  12789. POP_REF(value_type);
  12790. }
  12791. else {
  12792. switch (value_type) {
  12793. case VALUE_TYPE_I32:
  12794. case PACKED_TYPE_I8:
  12795. case PACKED_TYPE_I16:
  12796. POP_I32();
  12797. break;
  12798. case VALUE_TYPE_I64:
  12799. POP_I64();
  12800. break;
  12801. case VALUE_TYPE_F32:
  12802. POP_F32();
  12803. break;
  12804. case VALUE_TYPE_F64:
  12805. POP_F64();
  12806. break;
  12807. default:
  12808. set_error_buf(error_buf,
  12809. error_buf_size,
  12810. "unknown type");
  12811. goto fail;
  12812. }
  12813. }
  12814. }
  12815. }
  12816. /* PUSH struct obj, (ref $t) */
  12817. wasm_set_refheaptype_typeidx(
  12818. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12819. PUSH_REF(wasm_ref_type.ref_type);
  12820. break;
  12821. }
  12822. case WASM_OP_STRUCT_GET:
  12823. case WASM_OP_STRUCT_GET_S:
  12824. case WASM_OP_STRUCT_GET_U:
  12825. case WASM_OP_STRUCT_SET:
  12826. {
  12827. WASMStructType *struct_type;
  12828. WASMRefType *ref_type = NULL;
  12829. uint32 field_idx;
  12830. uint8 field_type;
  12831. pb_read_leb_uint32(p, p_end, type_idx);
  12832. #if WASM_ENABLE_FAST_INTERP != 0
  12833. emit_uint32(loader_ctx, type_idx);
  12834. #endif
  12835. if (!check_type_index(module, module->type_count,
  12836. type_idx, error_buf,
  12837. error_buf_size)) {
  12838. goto fail;
  12839. }
  12840. if (module->types[type_idx]->type_flag
  12841. != WASM_TYPE_STRUCT) {
  12842. set_error_buf(error_buf, error_buf_size,
  12843. "unknown struct type");
  12844. goto fail;
  12845. }
  12846. struct_type = (WASMStructType *)module->types[type_idx];
  12847. pb_read_leb_uint32(p, p_end, field_idx);
  12848. #if WASM_ENABLE_FAST_INTERP != 0
  12849. emit_uint32(loader_ctx, field_idx);
  12850. #endif
  12851. if (field_idx >= struct_type->field_count) {
  12852. set_error_buf(error_buf, error_buf_size,
  12853. "unknown struct field");
  12854. goto fail;
  12855. }
  12856. if (opcode1 == WASM_OP_STRUCT_SET
  12857. && !(struct_type->fields[field_idx].field_flags
  12858. & 1)) {
  12859. set_error_buf(error_buf, error_buf_size,
  12860. "field is immutable");
  12861. goto fail;
  12862. }
  12863. field_type = struct_type->fields[field_idx].field_type;
  12864. if (is_packed_type(field_type)) {
  12865. if (opcode1 == WASM_OP_STRUCT_GET) {
  12866. set_error_buf(error_buf, error_buf_size,
  12867. "type mismatch");
  12868. goto fail;
  12869. }
  12870. else {
  12871. field_type = VALUE_TYPE_I32;
  12872. }
  12873. }
  12874. if (wasm_is_type_multi_byte_type(field_type)) {
  12875. ref_type = wasm_reftype_map_find(
  12876. struct_type->ref_type_maps,
  12877. struct_type->ref_type_map_count, field_idx);
  12878. bh_assert(ref_type);
  12879. }
  12880. if (opcode1 == WASM_OP_STRUCT_SET) {
  12881. /* POP field */
  12882. if (wasm_is_type_multi_byte_type(field_type)) {
  12883. bh_memcpy_s(&wasm_ref_type,
  12884. (uint32)sizeof(WASMRefType),
  12885. ref_type,
  12886. wasm_reftype_struct_size(ref_type));
  12887. }
  12888. POP_REF(field_type);
  12889. /* POP struct obj, (ref null $t) */
  12890. wasm_set_refheaptype_typeidx(
  12891. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12892. POP_REF(wasm_ref_type.ref_type);
  12893. }
  12894. else {
  12895. /* POP struct obj, (ref null $t) */
  12896. wasm_set_refheaptype_typeidx(
  12897. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12898. POP_REF(wasm_ref_type.ref_type);
  12899. /* PUSH field */
  12900. if (wasm_is_type_multi_byte_type(field_type)) {
  12901. bh_memcpy_s(&wasm_ref_type,
  12902. (uint32)sizeof(WASMRefType),
  12903. ref_type,
  12904. wasm_reftype_struct_size(ref_type));
  12905. }
  12906. PUSH_REF(field_type);
  12907. }
  12908. break;
  12909. }
  12910. case WASM_OP_ARRAY_NEW:
  12911. case WASM_OP_ARRAY_NEW_DEFAULT:
  12912. case WASM_OP_ARRAY_NEW_FIXED:
  12913. case WASM_OP_ARRAY_NEW_DATA:
  12914. case WASM_OP_ARRAY_NEW_ELEM:
  12915. {
  12916. WASMArrayType *array_type;
  12917. uint8 elem_type;
  12918. uint32 u32 = 0;
  12919. pb_read_leb_uint32(p, p_end, type_idx);
  12920. #if WASM_ENABLE_FAST_INTERP != 0
  12921. emit_uint32(loader_ctx, type_idx);
  12922. #endif
  12923. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12924. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12925. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12926. pb_read_leb_uint32(p, p_end, u32);
  12927. #if WASM_ENABLE_FAST_INTERP != 0
  12928. emit_uint32(loader_ctx, u32);
  12929. #endif
  12930. }
  12931. if (!check_array_type(module, type_idx, error_buf,
  12932. error_buf_size)) {
  12933. goto fail;
  12934. }
  12935. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12936. /* length */
  12937. POP_I32();
  12938. }
  12939. array_type = (WASMArrayType *)module->types[type_idx];
  12940. elem_type = array_type->elem_type;
  12941. if (opcode1 == WASM_OP_ARRAY_NEW
  12942. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12943. if (wasm_is_type_multi_byte_type(elem_type)) {
  12944. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12945. array_type->elem_ref_type,
  12946. wasm_reftype_struct_size(
  12947. array_type->elem_ref_type));
  12948. }
  12949. if (is_packed_type(elem_type)) {
  12950. elem_type = VALUE_TYPE_I32;
  12951. }
  12952. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12953. uint32 N = u32;
  12954. for (i = 0; i < N; i++) {
  12955. if (wasm_is_type_multi_byte_type(
  12956. elem_type)) {
  12957. bh_memcpy_s(
  12958. &wasm_ref_type, sizeof(WASMRefType),
  12959. array_type->elem_ref_type,
  12960. wasm_reftype_struct_size(
  12961. array_type->elem_ref_type));
  12962. }
  12963. POP_REF(elem_type);
  12964. }
  12965. }
  12966. else
  12967. POP_REF(elem_type);
  12968. }
  12969. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12970. /* offset of data segment */
  12971. POP_I32();
  12972. if (u32 >= module->data_seg_count) {
  12973. set_error_buf(error_buf, error_buf_size,
  12974. "unknown data segment");
  12975. goto fail;
  12976. }
  12977. if (wasm_is_type_reftype(elem_type)) {
  12978. set_error_buf(error_buf, error_buf_size,
  12979. "array elem type mismatch");
  12980. goto fail;
  12981. }
  12982. }
  12983. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12984. WASMTableSeg *table_seg =
  12985. module->table_segments + u32;
  12986. /* offset of element segment */
  12987. POP_I32();
  12988. if (u32 >= module->table_seg_count) {
  12989. set_error_buf(error_buf, error_buf_size,
  12990. "unknown element segment");
  12991. goto fail;
  12992. }
  12993. if (!wasm_reftype_is_subtype_of(
  12994. table_seg->elem_type,
  12995. table_seg->elem_ref_type, elem_type,
  12996. array_type->elem_ref_type, module->types,
  12997. module->type_count)) {
  12998. set_error_buf(error_buf, error_buf_size,
  12999. "array elem type mismatch");
  13000. goto fail;
  13001. }
  13002. }
  13003. /* PUSH array obj, (ref $t) */
  13004. wasm_set_refheaptype_typeidx(
  13005. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13006. PUSH_REF(wasm_ref_type.ref_type);
  13007. break;
  13008. }
  13009. case WASM_OP_ARRAY_GET:
  13010. case WASM_OP_ARRAY_GET_S:
  13011. case WASM_OP_ARRAY_GET_U:
  13012. case WASM_OP_ARRAY_SET:
  13013. {
  13014. uint8 elem_type;
  13015. WASMArrayType *array_type;
  13016. WASMRefType *ref_type = NULL;
  13017. pb_read_leb_uint32(p, p_end, type_idx);
  13018. #if WASM_ENABLE_FAST_INTERP != 0
  13019. emit_uint32(loader_ctx, type_idx);
  13020. #endif
  13021. if (!check_array_type(module, type_idx, error_buf,
  13022. error_buf_size)) {
  13023. goto fail;
  13024. }
  13025. array_type = (WASMArrayType *)module->types[type_idx];
  13026. if (opcode1 == WASM_OP_ARRAY_SET
  13027. && !(array_type->elem_flags & 1)) {
  13028. set_error_buf(error_buf, error_buf_size,
  13029. "array is immutable");
  13030. goto fail;
  13031. }
  13032. elem_type = array_type->elem_type;
  13033. if (is_packed_type(elem_type)) {
  13034. if (opcode1 != WASM_OP_ARRAY_GET_S
  13035. && opcode1 != WASM_OP_ARRAY_GET_U
  13036. && opcode1 != WASM_OP_ARRAY_SET) {
  13037. set_error_buf(error_buf, error_buf_size,
  13038. "type mismatch");
  13039. goto fail;
  13040. }
  13041. else {
  13042. elem_type = VALUE_TYPE_I32;
  13043. }
  13044. }
  13045. ref_type = array_type->elem_ref_type;
  13046. if (opcode1 == WASM_OP_ARRAY_SET) {
  13047. /* POP elem to set */
  13048. if (wasm_is_type_multi_byte_type(elem_type)) {
  13049. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13050. ref_type,
  13051. wasm_reftype_struct_size(ref_type));
  13052. }
  13053. POP_REF(elem_type);
  13054. }
  13055. /* elem idx */
  13056. POP_I32();
  13057. /* POP array obj, (ref null $t) */
  13058. wasm_set_refheaptype_typeidx(
  13059. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13060. POP_REF(wasm_ref_type.ref_type);
  13061. if (opcode1 != WASM_OP_ARRAY_SET) {
  13062. /* PUSH elem */
  13063. if (wasm_is_type_multi_byte_type(elem_type)) {
  13064. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13065. ref_type,
  13066. wasm_reftype_struct_size(ref_type));
  13067. }
  13068. PUSH_REF(elem_type);
  13069. }
  13070. break;
  13071. }
  13072. case WASM_OP_ARRAY_LEN:
  13073. {
  13074. POP_REF(REF_TYPE_ARRAYREF);
  13075. /* length */
  13076. PUSH_I32();
  13077. break;
  13078. }
  13079. case WASM_OP_ARRAY_FILL:
  13080. {
  13081. WASMArrayType *array_type;
  13082. uint8 elem_type;
  13083. /* typeidx */
  13084. pb_read_leb_uint32(p, p_end, type_idx);
  13085. #if WASM_ENABLE_FAST_INTERP != 0
  13086. emit_uint32(loader_ctx, type_idx);
  13087. #endif
  13088. if (!check_array_type(module, type_idx, error_buf,
  13089. error_buf_size)) {
  13090. goto fail;
  13091. }
  13092. array_type = (WASMArrayType *)module->types[type_idx];
  13093. if (!(array_type->elem_flags & 1)) {
  13094. set_error_buf(error_buf, error_buf_size,
  13095. "array is immutable");
  13096. goto fail;
  13097. }
  13098. elem_type = array_type->elem_type;
  13099. if (is_packed_type(elem_type)) {
  13100. elem_type = VALUE_TYPE_I32;
  13101. }
  13102. POP_I32(); /* length */
  13103. #if WASM_ENABLE_FAST_INTERP != 0
  13104. POP_OFFSET_TYPE(elem_type);
  13105. #endif
  13106. POP_TYPE(elem_type);
  13107. POP_I32(); /* start */
  13108. /* POP array obj, (ref null $t) */
  13109. wasm_set_refheaptype_typeidx(
  13110. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13111. POP_REF(wasm_ref_type.ref_type);
  13112. break;
  13113. }
  13114. case WASM_OP_ARRAY_COPY:
  13115. {
  13116. uint32 src_type_idx;
  13117. uint8 src_elem_type, dst_elem_type;
  13118. WASMRefType src_ref_type = { 0 },
  13119. *src_elem_ref_type = NULL;
  13120. WASMRefType dst_ref_type = { 0 },
  13121. *dst_elem_ref_type = NULL;
  13122. WASMArrayType *array_type;
  13123. /* typeidx1 */
  13124. pb_read_leb_uint32(p, p_end, type_idx);
  13125. #if WASM_ENABLE_FAST_INTERP != 0
  13126. emit_uint32(loader_ctx, type_idx);
  13127. #endif
  13128. /* typeidx2 */
  13129. pb_read_leb_uint32(p, p_end, src_type_idx);
  13130. #if WASM_ENABLE_FAST_INTERP != 0
  13131. emit_uint32(loader_ctx, src_type_idx);
  13132. #endif
  13133. if (!check_array_type(module, type_idx, error_buf,
  13134. error_buf_size)) {
  13135. goto fail;
  13136. }
  13137. if (!check_array_type(module, src_type_idx, error_buf,
  13138. error_buf_size)) {
  13139. goto fail;
  13140. }
  13141. POP_I32();
  13142. POP_I32();
  13143. /* POP array obj, (ref null $t) */
  13144. wasm_set_refheaptype_typeidx(
  13145. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  13146. POP_REF(wasm_ref_type.ref_type);
  13147. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  13148. &wasm_ref_type,
  13149. wasm_reftype_struct_size(&wasm_ref_type));
  13150. POP_I32();
  13151. /* POP array obj, (ref null $t) */
  13152. wasm_set_refheaptype_typeidx(
  13153. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13154. POP_REF(wasm_ref_type.ref_type);
  13155. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  13156. &wasm_ref_type,
  13157. wasm_reftype_struct_size(&wasm_ref_type));
  13158. array_type = (WASMArrayType *)module->types[type_idx];
  13159. if (!(array_type->elem_flags & 1)) {
  13160. set_error_buf(error_buf, error_buf_size,
  13161. "destination array is immutable");
  13162. goto fail;
  13163. }
  13164. dst_elem_type = array_type->elem_type;
  13165. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  13166. dst_elem_ref_type = array_type->elem_ref_type;
  13167. }
  13168. array_type =
  13169. (WASMArrayType *)module->types[src_type_idx];
  13170. src_elem_type = array_type->elem_type;
  13171. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  13172. src_elem_ref_type = array_type->elem_ref_type;
  13173. }
  13174. if (!wasm_reftype_is_subtype_of(
  13175. src_elem_type, src_elem_ref_type, dst_elem_type,
  13176. dst_elem_ref_type, module->types,
  13177. module->type_count)) {
  13178. set_error_buf(error_buf, error_buf_size,
  13179. "array types do not match");
  13180. goto fail;
  13181. }
  13182. break;
  13183. }
  13184. case WASM_OP_REF_I31:
  13185. {
  13186. POP_I32();
  13187. wasm_set_refheaptype_common(
  13188. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  13189. PUSH_REF(wasm_ref_type.ref_type);
  13190. break;
  13191. }
  13192. case WASM_OP_I31_GET_S:
  13193. case WASM_OP_I31_GET_U:
  13194. {
  13195. POP_REF(REF_TYPE_I31REF);
  13196. PUSH_I32();
  13197. break;
  13198. }
  13199. case WASM_OP_REF_TEST:
  13200. case WASM_OP_REF_CAST:
  13201. case WASM_OP_REF_TEST_NULLABLE:
  13202. case WASM_OP_REF_CAST_NULLABLE:
  13203. {
  13204. uint8 type;
  13205. pb_read_leb_int32(p, p_end, heap_type);
  13206. #if WASM_ENABLE_FAST_INTERP != 0
  13207. emit_uint32(loader_ctx, (uint32)heap_type);
  13208. #endif
  13209. if (heap_type >= 0) {
  13210. if (!check_type_index(module, module->type_count,
  13211. heap_type, error_buf,
  13212. error_buf_size)) {
  13213. goto fail;
  13214. }
  13215. }
  13216. else {
  13217. if (!wasm_is_valid_heap_type(heap_type)) {
  13218. set_error_buf(error_buf, error_buf_size,
  13219. "unknown type");
  13220. goto fail;
  13221. }
  13222. }
  13223. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13224. &wasm_ref_type, error_buf,
  13225. error_buf_size)) {
  13226. goto fail;
  13227. }
  13228. if (opcode1 == WASM_OP_REF_TEST
  13229. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  13230. PUSH_I32();
  13231. else {
  13232. bool nullable =
  13233. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  13234. : false;
  13235. if (heap_type >= 0 || !nullable) {
  13236. wasm_set_refheaptype_typeidx(
  13237. &wasm_ref_type.ref_ht_typeidx, nullable,
  13238. heap_type);
  13239. PUSH_REF(wasm_ref_type.ref_type);
  13240. }
  13241. else {
  13242. PUSH_REF((uint8)((int32)0x80 + heap_type));
  13243. }
  13244. }
  13245. break;
  13246. }
  13247. case WASM_OP_BR_ON_CAST:
  13248. case WASM_OP_BR_ON_CAST_FAIL:
  13249. {
  13250. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13251. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13252. uint8 type_tmp, castflags;
  13253. uint32 depth;
  13254. int32 heap_type_dst;
  13255. bool src_nullable, dst_nullable;
  13256. CHECK_BUF(p, p_end, 1);
  13257. castflags = read_uint8(p);
  13258. #if WASM_ENABLE_FAST_INTERP != 0
  13259. /* Emit heap_type firstly */
  13260. emit_byte(loader_ctx, castflags);
  13261. #endif
  13262. p_org = p;
  13263. pb_read_leb_uint32(p, p_end, depth);
  13264. pb_read_leb_int32(p, p_end, heap_type);
  13265. #if WASM_ENABLE_FAST_INTERP != 0
  13266. /* Emit heap_type firstly */
  13267. emit_uint32(loader_ctx, (uint32)heap_type);
  13268. #endif
  13269. pb_read_leb_int32(p, p_end, heap_type_dst);
  13270. #if WASM_ENABLE_FAST_INTERP != 0
  13271. /* Emit heap_type firstly */
  13272. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13273. #endif
  13274. (void)depth;
  13275. /*
  13276. * castflags should be 0~3:
  13277. * 0: (non-null, non-null)
  13278. * 1: (null, non-null)
  13279. * 2: (non-null, null)
  13280. * 3: (null, null)
  13281. */
  13282. if (castflags > 3) {
  13283. set_error_buf(error_buf, error_buf_size,
  13284. "invalid castflags");
  13285. break;
  13286. }
  13287. src_nullable =
  13288. (castflags == 1) || (castflags == 3) ? true : false;
  13289. dst_nullable =
  13290. (castflags == 2) || (castflags == 3) ? true : false;
  13291. /* Pop and backup the stack top's ref type */
  13292. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13293. &ref_type_tmp, error_buf,
  13294. error_buf_size)) {
  13295. goto fail;
  13296. }
  13297. /* The reference type rt1 must be valid */
  13298. if (!init_ref_type(module, &ref_type1, src_nullable,
  13299. heap_type, error_buf,
  13300. error_buf_size)) {
  13301. goto fail;
  13302. }
  13303. /* The reference type rt2 must be valid. */
  13304. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13305. heap_type_dst, error_buf,
  13306. error_buf_size)) {
  13307. goto fail;
  13308. }
  13309. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13310. &ref_type2);
  13311. /* The reference type rt2 must match rt1. */
  13312. if (!wasm_reftype_is_subtype_of(
  13313. ref_type2.ref_type, &ref_type2,
  13314. ref_type1.ref_type, &ref_type1, module->types,
  13315. module->type_count)) {
  13316. set_error_buf(error_buf, error_buf_size,
  13317. "type mismatch");
  13318. goto fail;
  13319. }
  13320. p = p_org;
  13321. /* Push ref type casted for branch block check */
  13322. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13323. /* The reference type rt2 must match rt′. */
  13324. type_tmp = ref_type2.ref_type;
  13325. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13326. bh_memcpy_s(
  13327. &wasm_ref_type,
  13328. wasm_reftype_struct_size(&ref_type2),
  13329. &ref_type2,
  13330. wasm_reftype_struct_size(&ref_type2));
  13331. }
  13332. }
  13333. else {
  13334. /* The reference type rt′1 must match rt′. */
  13335. type_tmp = ref_type_diff.ref_type;
  13336. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13337. bh_memcpy_s(
  13338. &wasm_ref_type,
  13339. wasm_reftype_struct_size(&ref_type_diff),
  13340. &ref_type_diff,
  13341. wasm_reftype_struct_size(&ref_type_diff));
  13342. }
  13343. }
  13344. PUSH_REF(type_tmp);
  13345. if (!(frame_csp_tmp = check_branch_block(
  13346. loader_ctx, &p, p_end, opcode, error_buf,
  13347. error_buf_size))) {
  13348. goto fail;
  13349. }
  13350. /* Ignore heap_types */
  13351. skip_leb_uint32(p, p_end);
  13352. skip_leb_uint32(p, p_end);
  13353. /* Restore the original stack top's ref type */
  13354. POP_REF(type_tmp);
  13355. #if WASM_ENABLE_FAST_INTERP != 0
  13356. /* Erase the opnd offset emitted by POP_REF() */
  13357. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13358. #endif
  13359. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13360. type_tmp = ref_type_diff.ref_type;
  13361. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13362. bh_memcpy_s(
  13363. &wasm_ref_type,
  13364. wasm_reftype_struct_size(&ref_type_diff),
  13365. &ref_type_diff,
  13366. wasm_reftype_struct_size(&ref_type_diff));
  13367. }
  13368. }
  13369. else {
  13370. type_tmp = ref_type2.ref_type;
  13371. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13372. bh_memcpy_s(
  13373. &wasm_ref_type,
  13374. wasm_reftype_struct_size(&ref_type2),
  13375. &ref_type2,
  13376. wasm_reftype_struct_size(&ref_type2));
  13377. }
  13378. }
  13379. PUSH_REF(type_tmp);
  13380. #if WASM_ENABLE_FAST_INTERP != 0
  13381. /* Erase the opnd offset emitted by PUSH_REF() */
  13382. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13383. #endif
  13384. break;
  13385. }
  13386. case WASM_OP_ANY_CONVERT_EXTERN:
  13387. {
  13388. uint8 type;
  13389. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13390. &wasm_ref_type, error_buf,
  13391. error_buf_size)) {
  13392. goto fail;
  13393. }
  13394. if (!(type == REF_TYPE_EXTERNREF
  13395. || (type == REF_TYPE_HT_NON_NULLABLE
  13396. && wasm_ref_type.ref_ht_common.heap_type
  13397. == HEAP_TYPE_EXTERN)
  13398. || type == VALUE_TYPE_ANY)) {
  13399. set_error_buf(error_buf, error_buf_size,
  13400. "type mismatch");
  13401. goto fail;
  13402. }
  13403. if (type == REF_TYPE_EXTERNREF)
  13404. type = REF_TYPE_ANYREF;
  13405. else {
  13406. wasm_ref_type.ref_ht_common.heap_type =
  13407. HEAP_TYPE_ANY;
  13408. }
  13409. PUSH_REF(type);
  13410. break;
  13411. }
  13412. case WASM_OP_EXTERN_CONVERT_ANY:
  13413. {
  13414. uint8 type;
  13415. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13416. &wasm_ref_type, error_buf,
  13417. error_buf_size)) {
  13418. goto fail;
  13419. }
  13420. if (type == REF_TYPE_EXTERNREF
  13421. || ((type == REF_TYPE_HT_NULLABLE
  13422. || type == REF_TYPE_HT_NON_NULLABLE)
  13423. && wasm_ref_type.ref_ht_common.heap_type
  13424. == HEAP_TYPE_EXTERN)) {
  13425. set_error_buf(error_buf, error_buf_size,
  13426. "type mismatch");
  13427. goto fail;
  13428. }
  13429. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13430. /* push (ref null extern) */
  13431. type = REF_TYPE_EXTERNREF;
  13432. }
  13433. else {
  13434. /* push (ref extern) */
  13435. type = REF_TYPE_HT_NON_NULLABLE;
  13436. wasm_set_refheaptype_common(
  13437. &wasm_ref_type.ref_ht_common, false,
  13438. HEAP_TYPE_EXTERN);
  13439. }
  13440. PUSH_REF(type);
  13441. break;
  13442. }
  13443. #if WASM_ENABLE_STRINGREF != 0
  13444. case WASM_OP_STRING_NEW_UTF8:
  13445. case WASM_OP_STRING_NEW_WTF16:
  13446. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13447. case WASM_OP_STRING_NEW_WTF8:
  13448. {
  13449. uint32 memidx;
  13450. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13451. func->has_memory_operations = true;
  13452. #endif
  13453. pb_read_leb_uint32(p, p_end, memidx);
  13454. #if WASM_ENABLE_FAST_INTERP != 0
  13455. emit_uint32(loader_ctx, (uint32)memidx);
  13456. #endif
  13457. POP_I32();
  13458. POP_I32();
  13459. PUSH_REF(REF_TYPE_STRINGREF);
  13460. (void)memidx;
  13461. break;
  13462. }
  13463. case WASM_OP_STRING_CONST:
  13464. {
  13465. uint32 contents;
  13466. pb_read_leb_uint32(p, p_end, contents);
  13467. #if WASM_ENABLE_FAST_INTERP != 0
  13468. emit_uint32(loader_ctx, (uint32)contents);
  13469. #endif
  13470. PUSH_REF(REF_TYPE_STRINGREF);
  13471. (void)contents;
  13472. break;
  13473. }
  13474. case WASM_OP_STRING_MEASURE_UTF8:
  13475. case WASM_OP_STRING_MEASURE_WTF8:
  13476. case WASM_OP_STRING_MEASURE_WTF16:
  13477. {
  13478. POP_STRINGREF();
  13479. PUSH_I32();
  13480. break;
  13481. }
  13482. case WASM_OP_STRING_ENCODE_UTF8:
  13483. case WASM_OP_STRING_ENCODE_WTF16:
  13484. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13485. case WASM_OP_STRING_ENCODE_WTF8:
  13486. {
  13487. uint32 memidx;
  13488. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13489. func->has_memory_operations = true;
  13490. #endif
  13491. pb_read_leb_uint32(p, p_end, memidx);
  13492. #if WASM_ENABLE_FAST_INTERP != 0
  13493. emit_uint32(loader_ctx, (uint32)memidx);
  13494. #endif
  13495. POP_I32();
  13496. POP_STRINGREF();
  13497. PUSH_I32();
  13498. (void)memidx;
  13499. break;
  13500. }
  13501. case WASM_OP_STRING_CONCAT:
  13502. {
  13503. POP_STRINGREF();
  13504. POP_STRINGREF();
  13505. PUSH_REF(REF_TYPE_STRINGREF);
  13506. break;
  13507. }
  13508. case WASM_OP_STRING_EQ:
  13509. {
  13510. POP_STRINGREF();
  13511. POP_STRINGREF();
  13512. PUSH_I32();
  13513. break;
  13514. }
  13515. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13516. {
  13517. POP_STRINGREF();
  13518. PUSH_I32();
  13519. break;
  13520. }
  13521. case WASM_OP_STRING_AS_WTF8:
  13522. {
  13523. POP_STRINGREF();
  13524. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13525. break;
  13526. }
  13527. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13528. {
  13529. POP_I32();
  13530. POP_I32();
  13531. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13532. PUSH_I32();
  13533. break;
  13534. }
  13535. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13536. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13537. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13538. {
  13539. uint32 memidx;
  13540. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13541. func->has_memory_operations = true;
  13542. #endif
  13543. pb_read_leb_uint32(p, p_end, memidx);
  13544. #if WASM_ENABLE_FAST_INTERP != 0
  13545. emit_uint32(loader_ctx, (uint32)memidx);
  13546. #endif
  13547. POP_I32();
  13548. POP_I32();
  13549. POP_I32();
  13550. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13551. PUSH_I32();
  13552. PUSH_I32();
  13553. (void)memidx;
  13554. break;
  13555. }
  13556. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13557. {
  13558. POP_I32();
  13559. POP_I32();
  13560. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13561. PUSH_REF(REF_TYPE_STRINGREF);
  13562. break;
  13563. }
  13564. case WASM_OP_STRING_AS_WTF16:
  13565. {
  13566. POP_STRINGREF();
  13567. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13568. break;
  13569. }
  13570. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13571. {
  13572. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13573. PUSH_I32();
  13574. break;
  13575. }
  13576. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13577. {
  13578. POP_I32();
  13579. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13580. PUSH_I32();
  13581. break;
  13582. }
  13583. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13584. {
  13585. uint32 memidx;
  13586. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13587. func->has_memory_operations = true;
  13588. #endif
  13589. pb_read_leb_uint32(p, p_end, memidx);
  13590. #if WASM_ENABLE_FAST_INTERP != 0
  13591. emit_uint32(loader_ctx, (uint32)memidx);
  13592. #endif
  13593. POP_I32();
  13594. POP_I32();
  13595. POP_I32();
  13596. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13597. PUSH_I32();
  13598. (void)memidx;
  13599. break;
  13600. }
  13601. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13602. {
  13603. POP_I32();
  13604. POP_I32();
  13605. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13606. PUSH_REF(REF_TYPE_STRINGREF);
  13607. break;
  13608. }
  13609. case WASM_OP_STRING_AS_ITER:
  13610. {
  13611. POP_STRINGREF();
  13612. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13613. break;
  13614. }
  13615. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13616. {
  13617. POP_REF(REF_TYPE_STRINGVIEWITER);
  13618. PUSH_I32();
  13619. break;
  13620. }
  13621. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13622. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13623. {
  13624. POP_I32();
  13625. POP_REF(REF_TYPE_STRINGVIEWITER);
  13626. PUSH_I32();
  13627. break;
  13628. }
  13629. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13630. {
  13631. POP_I32();
  13632. POP_REF(REF_TYPE_STRINGVIEWITER);
  13633. PUSH_REF(REF_TYPE_STRINGREF);
  13634. break;
  13635. }
  13636. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13637. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13638. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13639. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13640. {
  13641. POP_I32();
  13642. POP_I32();
  13643. POP_REF(REF_TYPE_ARRAYREF);
  13644. PUSH_REF(REF_TYPE_STRINGREF);
  13645. break;
  13646. }
  13647. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13648. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13649. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13650. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13651. {
  13652. POP_I32();
  13653. POP_REF(REF_TYPE_ARRAYREF);
  13654. POP_STRINGREF();
  13655. PUSH_I32();
  13656. break;
  13657. }
  13658. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13659. default:
  13660. set_error_buf_v(error_buf, error_buf_size,
  13661. "%s %02x %02x", "unsupported opcode",
  13662. 0xfb, opcode1);
  13663. goto fail;
  13664. }
  13665. break;
  13666. }
  13667. #endif /* end of WASM_ENABLE_GC != 0 */
  13668. case WASM_OP_MISC_PREFIX:
  13669. {
  13670. uint32 opcode1;
  13671. pb_read_leb_uint32(p, p_end, opcode1);
  13672. #if WASM_ENABLE_FAST_INTERP != 0
  13673. emit_byte(loader_ctx, ((uint8)opcode1));
  13674. #endif
  13675. switch (opcode1) {
  13676. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13677. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13678. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13679. break;
  13680. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13681. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13682. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13683. break;
  13684. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13685. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13686. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13687. break;
  13688. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13689. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13690. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13691. break;
  13692. #if WASM_ENABLE_BULK_MEMORY != 0
  13693. case WASM_OP_MEMORY_INIT:
  13694. {
  13695. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13696. #if WASM_ENABLE_FAST_INTERP != 0
  13697. emit_uint32(loader_ctx, data_seg_idx);
  13698. #endif
  13699. if (module->import_memory_count == 0
  13700. && module->memory_count == 0)
  13701. goto fail_unknown_memory;
  13702. pb_read_leb_uint32(p, p_end, memidx);
  13703. check_memidx(module, memidx);
  13704. if (data_seg_idx >= module->data_seg_count) {
  13705. set_error_buf_v(error_buf, error_buf_size,
  13706. "unknown data segment %d",
  13707. data_seg_idx);
  13708. goto fail;
  13709. }
  13710. if (module->data_seg_count1 == 0)
  13711. goto fail_data_cnt_sec_require;
  13712. POP_I32();
  13713. POP_I32();
  13714. POP_MEM_OFFSET();
  13715. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13716. func->has_memory_operations = true;
  13717. #endif
  13718. #if WASM_ENABLE_WAMR_COMPILER != 0
  13719. module->is_bulk_memory_used = true;
  13720. #endif
  13721. break;
  13722. }
  13723. case WASM_OP_DATA_DROP:
  13724. {
  13725. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13726. #if WASM_ENABLE_FAST_INTERP != 0
  13727. emit_uint32(loader_ctx, data_seg_idx);
  13728. #endif
  13729. if (data_seg_idx >= module->data_seg_count) {
  13730. set_error_buf(error_buf, error_buf_size,
  13731. "unknown data segment");
  13732. goto fail;
  13733. }
  13734. if (module->data_seg_count1 == 0)
  13735. goto fail_data_cnt_sec_require;
  13736. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13737. func->has_memory_operations = true;
  13738. #endif
  13739. #if WASM_ENABLE_WAMR_COMPILER != 0
  13740. module->is_bulk_memory_used = true;
  13741. #endif
  13742. break;
  13743. }
  13744. case WASM_OP_MEMORY_COPY:
  13745. {
  13746. CHECK_BUF(p, p_end, sizeof(int16));
  13747. /* check both src and dst memory index */
  13748. pb_read_leb_uint32(p, p_end, memidx);
  13749. check_memidx(module, memidx);
  13750. pb_read_leb_uint32(p, p_end, memidx);
  13751. check_memidx(module, memidx);
  13752. if (module->import_memory_count == 0
  13753. && module->memory_count == 0)
  13754. goto fail_unknown_memory;
  13755. POP_MEM_OFFSET();
  13756. POP_MEM_OFFSET();
  13757. POP_MEM_OFFSET();
  13758. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13759. func->has_memory_operations = true;
  13760. #endif
  13761. #if WASM_ENABLE_WAMR_COMPILER != 0
  13762. module->is_bulk_memory_used = true;
  13763. #endif
  13764. break;
  13765. }
  13766. case WASM_OP_MEMORY_FILL:
  13767. {
  13768. pb_read_leb_uint32(p, p_end, memidx);
  13769. check_memidx(module, memidx);
  13770. if (module->import_memory_count == 0
  13771. && module->memory_count == 0) {
  13772. goto fail_unknown_memory;
  13773. }
  13774. POP_MEM_OFFSET();
  13775. POP_I32();
  13776. POP_MEM_OFFSET();
  13777. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13778. func->has_memory_operations = true;
  13779. #endif
  13780. #if WASM_ENABLE_WAMR_COMPILER != 0
  13781. module->is_bulk_memory_used = true;
  13782. #endif
  13783. break;
  13784. }
  13785. fail_unknown_memory:
  13786. set_error_buf(error_buf, error_buf_size,
  13787. "unknown memory 0");
  13788. goto fail;
  13789. fail_data_cnt_sec_require:
  13790. set_error_buf(error_buf, error_buf_size,
  13791. "data count section required");
  13792. goto fail;
  13793. #endif /* WASM_ENABLE_BULK_MEMORY */
  13794. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13795. case WASM_OP_TABLE_INIT:
  13796. {
  13797. uint8 seg_type = 0, tbl_type = 0;
  13798. #if WASM_ENABLE_GC != 0
  13799. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13800. #endif
  13801. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13802. pb_read_leb_uint32(p, p_end, table_idx);
  13803. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13804. #if WASM_ENABLE_GC != 0
  13805. (void **)&tbl_ref_type,
  13806. #else
  13807. NULL,
  13808. #endif
  13809. error_buf, error_buf_size))
  13810. goto fail;
  13811. if (!get_table_seg_elem_type(module, table_seg_idx,
  13812. &seg_type,
  13813. #if WASM_ENABLE_GC != 0
  13814. (void **)&seg_ref_type,
  13815. #else
  13816. NULL,
  13817. #endif
  13818. error_buf, error_buf_size))
  13819. goto fail;
  13820. #if WASM_ENABLE_GC == 0
  13821. if (seg_type != tbl_type) {
  13822. set_error_buf(error_buf, error_buf_size,
  13823. "type mismatch");
  13824. goto fail;
  13825. }
  13826. #else
  13827. if (!wasm_reftype_is_subtype_of(
  13828. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13829. module->types, module->type_count)) {
  13830. set_error_buf(error_buf, error_buf_size,
  13831. "type mismatch");
  13832. goto fail;
  13833. }
  13834. #endif
  13835. #if WASM_ENABLE_FAST_INTERP != 0
  13836. emit_uint32(loader_ctx, table_seg_idx);
  13837. emit_uint32(loader_ctx, table_idx);
  13838. #endif
  13839. POP_I32();
  13840. POP_I32();
  13841. #if WASM_ENABLE_MEMORY64 != 0
  13842. table_elem_idx_type = is_table_64bit(module, table_idx)
  13843. ? VALUE_TYPE_I64
  13844. : VALUE_TYPE_I32;
  13845. #endif
  13846. POP_TBL_ELEM_IDX();
  13847. #if WASM_ENABLE_WAMR_COMPILER != 0
  13848. module->is_ref_types_used = true;
  13849. #endif
  13850. break;
  13851. }
  13852. case WASM_OP_ELEM_DROP:
  13853. {
  13854. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13855. if (!get_table_seg_elem_type(module, table_seg_idx,
  13856. NULL, NULL, error_buf,
  13857. error_buf_size))
  13858. goto fail;
  13859. #if WASM_ENABLE_FAST_INTERP != 0
  13860. emit_uint32(loader_ctx, table_seg_idx);
  13861. #endif
  13862. #if WASM_ENABLE_WAMR_COMPILER != 0
  13863. module->is_ref_types_used = true;
  13864. #endif
  13865. break;
  13866. }
  13867. case WASM_OP_TABLE_COPY:
  13868. {
  13869. uint8 src_type, dst_type, src_tbl_idx_type,
  13870. dst_tbl_idx_type, min_tbl_idx_type;
  13871. #if WASM_ENABLE_GC != 0
  13872. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13873. #endif
  13874. uint32 src_tbl_idx, dst_tbl_idx;
  13875. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  13876. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13877. #if WASM_ENABLE_GC != 0
  13878. (void **)&dst_ref_type,
  13879. #else
  13880. NULL,
  13881. #endif
  13882. error_buf, error_buf_size))
  13883. goto fail;
  13884. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  13885. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13886. #if WASM_ENABLE_GC != 0
  13887. (void **)&src_ref_type,
  13888. #else
  13889. NULL,
  13890. #endif
  13891. error_buf, error_buf_size))
  13892. goto fail;
  13893. #if WASM_ENABLE_GC == 0
  13894. if (src_type != dst_type) {
  13895. set_error_buf(error_buf, error_buf_size,
  13896. "type mismatch");
  13897. goto fail;
  13898. }
  13899. #else
  13900. if (!wasm_reftype_is_subtype_of(
  13901. src_type, src_ref_type, dst_type, dst_ref_type,
  13902. module->types, module->type_count)) {
  13903. set_error_buf(error_buf, error_buf_size,
  13904. "type mismatch");
  13905. goto fail;
  13906. }
  13907. #endif
  13908. #if WASM_ENABLE_FAST_INTERP != 0
  13909. emit_uint32(loader_ctx, dst_tbl_idx);
  13910. emit_uint32(loader_ctx, src_tbl_idx);
  13911. #endif
  13912. #if WASM_ENABLE_MEMORY64 != 0
  13913. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  13914. ? VALUE_TYPE_I64
  13915. : VALUE_TYPE_I32;
  13916. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  13917. ? VALUE_TYPE_I64
  13918. : VALUE_TYPE_I32;
  13919. min_tbl_idx_type =
  13920. (src_tbl_idx_type == VALUE_TYPE_I32
  13921. || dst_tbl_idx_type == VALUE_TYPE_I32)
  13922. ? VALUE_TYPE_I32
  13923. : VALUE_TYPE_I64;
  13924. #else
  13925. src_tbl_idx_type = VALUE_TYPE_I32;
  13926. dst_tbl_idx_type = VALUE_TYPE_I32;
  13927. min_tbl_idx_type = VALUE_TYPE_I32;
  13928. #endif
  13929. table_elem_idx_type = min_tbl_idx_type;
  13930. POP_TBL_ELEM_IDX();
  13931. table_elem_idx_type = src_tbl_idx_type;
  13932. POP_TBL_ELEM_IDX();
  13933. table_elem_idx_type = dst_tbl_idx_type;
  13934. POP_TBL_ELEM_IDX();
  13935. #if WASM_ENABLE_WAMR_COMPILER != 0
  13936. module->is_ref_types_used = true;
  13937. #endif
  13938. break;
  13939. }
  13940. case WASM_OP_TABLE_SIZE:
  13941. {
  13942. pb_read_leb_uint32(p, p_end, table_idx);
  13943. /* TODO: shall we create a new function to check
  13944. table idx instead of using below function? */
  13945. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13946. error_buf, error_buf_size))
  13947. goto fail;
  13948. #if WASM_ENABLE_FAST_INTERP != 0
  13949. emit_uint32(loader_ctx, table_idx);
  13950. #endif
  13951. #if WASM_ENABLE_MEMORY64 != 0
  13952. table_elem_idx_type = is_table_64bit(module, table_idx)
  13953. ? VALUE_TYPE_I64
  13954. : VALUE_TYPE_I32;
  13955. #endif
  13956. PUSH_TBL_ELEM_IDX();
  13957. #if WASM_ENABLE_WAMR_COMPILER != 0
  13958. module->is_ref_types_used = true;
  13959. #endif
  13960. break;
  13961. }
  13962. case WASM_OP_TABLE_GROW:
  13963. case WASM_OP_TABLE_FILL:
  13964. {
  13965. uint8 decl_type;
  13966. #if WASM_ENABLE_GC != 0
  13967. WASMRefType *ref_type = NULL;
  13968. #endif
  13969. pb_read_leb_uint32(p, p_end, table_idx);
  13970. if (!get_table_elem_type(module, table_idx, &decl_type,
  13971. #if WASM_ENABLE_GC != 0
  13972. (void **)&ref_type,
  13973. #else
  13974. NULL,
  13975. #endif
  13976. error_buf, error_buf_size))
  13977. goto fail;
  13978. #if WASM_ENABLE_GC != 0
  13979. if (wasm_is_type_multi_byte_type(decl_type)) {
  13980. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13981. ref_type,
  13982. wasm_reftype_struct_size(ref_type));
  13983. }
  13984. #endif
  13985. if (opcode1 == WASM_OP_TABLE_GROW) {
  13986. if (table_idx < module->import_table_count) {
  13987. module->import_tables[table_idx]
  13988. .u.table.table_type.possible_grow = true;
  13989. }
  13990. else {
  13991. module
  13992. ->tables[table_idx
  13993. - module->import_table_count]
  13994. .table_type.possible_grow = true;
  13995. }
  13996. }
  13997. #if WASM_ENABLE_FAST_INTERP != 0
  13998. emit_uint32(loader_ctx, table_idx);
  13999. #endif
  14000. #if WASM_ENABLE_MEMORY64 != 0
  14001. table_elem_idx_type = is_table_64bit(module, table_idx)
  14002. ? VALUE_TYPE_I64
  14003. : VALUE_TYPE_I32;
  14004. #endif
  14005. POP_TBL_ELEM_IDX();
  14006. #if WASM_ENABLE_FAST_INTERP != 0
  14007. POP_OFFSET_TYPE(decl_type);
  14008. #endif
  14009. POP_TYPE(decl_type);
  14010. if (opcode1 == WASM_OP_TABLE_GROW)
  14011. PUSH_TBL_ELEM_IDX();
  14012. else
  14013. POP_TBL_ELEM_IDX();
  14014. #if WASM_ENABLE_WAMR_COMPILER != 0
  14015. module->is_ref_types_used = true;
  14016. #endif
  14017. break;
  14018. }
  14019. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  14020. default:
  14021. set_error_buf_v(error_buf, error_buf_size,
  14022. "%s %02x %02x", "unsupported opcode",
  14023. 0xfc, opcode1);
  14024. goto fail;
  14025. }
  14026. break;
  14027. }
  14028. #if WASM_ENABLE_SIMD != 0
  14029. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  14030. || (WASM_ENABLE_FAST_INTERP != 0)
  14031. case WASM_OP_SIMD_PREFIX:
  14032. {
  14033. uint32 opcode1;
  14034. #if WASM_ENABLE_WAMR_COMPILER != 0
  14035. /* Mark the SIMD instruction is used in this module */
  14036. module->is_simd_used = true;
  14037. #endif
  14038. pb_read_leb_uint32(p, p_end, opcode1);
  14039. #if WASM_ENABLE_FAST_INTERP != 0
  14040. emit_byte(loader_ctx, opcode1);
  14041. #endif
  14042. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  14043. */
  14044. switch (opcode1) {
  14045. /* memory instruction */
  14046. case SIMD_v128_load:
  14047. case SIMD_v128_load8x8_s:
  14048. case SIMD_v128_load8x8_u:
  14049. case SIMD_v128_load16x4_s:
  14050. case SIMD_v128_load16x4_u:
  14051. case SIMD_v128_load32x2_s:
  14052. case SIMD_v128_load32x2_u:
  14053. case SIMD_v128_load8_splat:
  14054. case SIMD_v128_load16_splat:
  14055. case SIMD_v128_load32_splat:
  14056. case SIMD_v128_load64_splat:
  14057. {
  14058. CHECK_MEMORY();
  14059. pb_read_leb_uint32(p, p_end, align); /* align */
  14060. if (!check_simd_memory_access_align(
  14061. opcode1, align, error_buf, error_buf_size)) {
  14062. goto fail;
  14063. }
  14064. pb_read_leb_mem_offset(p, p_end,
  14065. mem_offset); /* offset */
  14066. #if WASM_ENABLE_FAST_INTERP != 0
  14067. emit_uint32(loader_ctx, mem_offset);
  14068. #endif
  14069. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14070. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14071. func->has_memory_operations = true;
  14072. #endif
  14073. break;
  14074. }
  14075. case SIMD_v128_store:
  14076. {
  14077. CHECK_MEMORY();
  14078. pb_read_leb_uint32(p, p_end, align); /* align */
  14079. if (!check_simd_memory_access_align(
  14080. opcode1, align, error_buf, error_buf_size)) {
  14081. goto fail;
  14082. }
  14083. pb_read_leb_mem_offset(p, p_end,
  14084. mem_offset); /* offset */
  14085. #if WASM_ENABLE_FAST_INTERP != 0
  14086. emit_uint32(loader_ctx, mem_offset);
  14087. #endif
  14088. POP_V128();
  14089. POP_MEM_OFFSET();
  14090. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14091. func->has_memory_operations = true;
  14092. #endif
  14093. break;
  14094. }
  14095. /* basic operation */
  14096. case SIMD_v128_const:
  14097. {
  14098. #if WASM_ENABLE_FAST_INTERP != 0
  14099. uint64 high, low;
  14100. #endif
  14101. CHECK_BUF1(p, p_end, 16);
  14102. #if WASM_ENABLE_FAST_INTERP != 0
  14103. wasm_runtime_read_v128(p, &high, &low);
  14104. emit_uint64(loader_ctx, high);
  14105. emit_uint64(loader_ctx, low);
  14106. #endif
  14107. p += 16;
  14108. PUSH_V128();
  14109. break;
  14110. }
  14111. case SIMD_v8x16_shuffle:
  14112. {
  14113. V128 mask;
  14114. CHECK_BUF1(p, p_end, 16);
  14115. mask = read_i8x16(p, error_buf, error_buf_size);
  14116. if (!check_simd_shuffle_mask(mask, error_buf,
  14117. error_buf_size)) {
  14118. goto fail;
  14119. }
  14120. #if WASM_ENABLE_FAST_INTERP != 0
  14121. uint64 high, low;
  14122. wasm_runtime_read_v128(p, &high, &low);
  14123. emit_uint64(loader_ctx, high);
  14124. emit_uint64(loader_ctx, low);
  14125. #endif
  14126. p += 16;
  14127. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14128. break;
  14129. }
  14130. case SIMD_v8x16_swizzle:
  14131. {
  14132. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14133. break;
  14134. }
  14135. /* splat operation */
  14136. case SIMD_i8x16_splat:
  14137. case SIMD_i16x8_splat:
  14138. case SIMD_i32x4_splat:
  14139. case SIMD_i64x2_splat:
  14140. case SIMD_f32x4_splat:
  14141. case SIMD_f64x2_splat:
  14142. {
  14143. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  14144. VALUE_TYPE_I32, VALUE_TYPE_I64,
  14145. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  14146. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  14147. VALUE_TYPE_V128);
  14148. break;
  14149. }
  14150. /* lane operation */
  14151. case SIMD_i8x16_extract_lane_s:
  14152. case SIMD_i8x16_extract_lane_u:
  14153. case SIMD_i8x16_replace_lane:
  14154. case SIMD_i16x8_extract_lane_s:
  14155. case SIMD_i16x8_extract_lane_u:
  14156. case SIMD_i16x8_replace_lane:
  14157. case SIMD_i32x4_extract_lane:
  14158. case SIMD_i32x4_replace_lane:
  14159. case SIMD_i64x2_extract_lane:
  14160. case SIMD_i64x2_replace_lane:
  14161. case SIMD_f32x4_extract_lane:
  14162. case SIMD_f32x4_replace_lane:
  14163. case SIMD_f64x2_extract_lane:
  14164. case SIMD_f64x2_replace_lane:
  14165. {
  14166. uint8 lane;
  14167. /* clang-format off */
  14168. uint8 replace[] = {
  14169. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  14170. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  14171. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  14172. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  14173. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  14174. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  14175. };
  14176. uint8 push_type[] = {
  14177. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14178. VALUE_TYPE_V128,
  14179. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14180. VALUE_TYPE_V128,
  14181. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  14182. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  14183. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  14184. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  14185. };
  14186. /* clang-format on */
  14187. CHECK_BUF(p, p_end, 1);
  14188. lane = read_uint8(p);
  14189. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14190. error_buf_size)) {
  14191. goto fail;
  14192. }
  14193. #if WASM_ENABLE_FAST_INTERP != 0
  14194. emit_byte(loader_ctx, lane);
  14195. #endif
  14196. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  14197. #if WASM_ENABLE_FAST_INTERP != 0
  14198. if (!(wasm_loader_pop_frame_ref_offset(
  14199. loader_ctx,
  14200. replace[opcode1
  14201. - SIMD_i8x16_extract_lane_s],
  14202. error_buf, error_buf_size)))
  14203. goto fail;
  14204. #else
  14205. if (!(wasm_loader_pop_frame_ref(
  14206. loader_ctx,
  14207. replace[opcode1
  14208. - SIMD_i8x16_extract_lane_s],
  14209. error_buf, error_buf_size)))
  14210. goto fail;
  14211. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  14212. }
  14213. POP_AND_PUSH(
  14214. VALUE_TYPE_V128,
  14215. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  14216. break;
  14217. }
  14218. /* i8x16 compare operation */
  14219. case SIMD_i8x16_eq:
  14220. case SIMD_i8x16_ne:
  14221. case SIMD_i8x16_lt_s:
  14222. case SIMD_i8x16_lt_u:
  14223. case SIMD_i8x16_gt_s:
  14224. case SIMD_i8x16_gt_u:
  14225. case SIMD_i8x16_le_s:
  14226. case SIMD_i8x16_le_u:
  14227. case SIMD_i8x16_ge_s:
  14228. case SIMD_i8x16_ge_u:
  14229. /* i16x8 compare operation */
  14230. case SIMD_i16x8_eq:
  14231. case SIMD_i16x8_ne:
  14232. case SIMD_i16x8_lt_s:
  14233. case SIMD_i16x8_lt_u:
  14234. case SIMD_i16x8_gt_s:
  14235. case SIMD_i16x8_gt_u:
  14236. case SIMD_i16x8_le_s:
  14237. case SIMD_i16x8_le_u:
  14238. case SIMD_i16x8_ge_s:
  14239. case SIMD_i16x8_ge_u:
  14240. /* i32x4 compare operation */
  14241. case SIMD_i32x4_eq:
  14242. case SIMD_i32x4_ne:
  14243. case SIMD_i32x4_lt_s:
  14244. case SIMD_i32x4_lt_u:
  14245. case SIMD_i32x4_gt_s:
  14246. case SIMD_i32x4_gt_u:
  14247. case SIMD_i32x4_le_s:
  14248. case SIMD_i32x4_le_u:
  14249. case SIMD_i32x4_ge_s:
  14250. case SIMD_i32x4_ge_u:
  14251. /* f32x4 compare operation */
  14252. case SIMD_f32x4_eq:
  14253. case SIMD_f32x4_ne:
  14254. case SIMD_f32x4_lt:
  14255. case SIMD_f32x4_gt:
  14256. case SIMD_f32x4_le:
  14257. case SIMD_f32x4_ge:
  14258. /* f64x2 compare operation */
  14259. case SIMD_f64x2_eq:
  14260. case SIMD_f64x2_ne:
  14261. case SIMD_f64x2_lt:
  14262. case SIMD_f64x2_gt:
  14263. case SIMD_f64x2_le:
  14264. case SIMD_f64x2_ge:
  14265. {
  14266. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14267. break;
  14268. }
  14269. /* v128 operation */
  14270. case SIMD_v128_not:
  14271. {
  14272. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14273. break;
  14274. }
  14275. case SIMD_v128_and:
  14276. case SIMD_v128_andnot:
  14277. case SIMD_v128_or:
  14278. case SIMD_v128_xor:
  14279. {
  14280. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14281. break;
  14282. }
  14283. case SIMD_v128_bitselect:
  14284. {
  14285. POP_V128();
  14286. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14287. break;
  14288. }
  14289. case SIMD_v128_any_true:
  14290. {
  14291. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14292. break;
  14293. }
  14294. /* Load Lane Operation */
  14295. case SIMD_v128_load8_lane:
  14296. case SIMD_v128_load16_lane:
  14297. case SIMD_v128_load32_lane:
  14298. case SIMD_v128_load64_lane:
  14299. case SIMD_v128_store8_lane:
  14300. case SIMD_v128_store16_lane:
  14301. case SIMD_v128_store32_lane:
  14302. case SIMD_v128_store64_lane:
  14303. {
  14304. uint8 lane;
  14305. CHECK_MEMORY();
  14306. pb_read_leb_uint32(p, p_end, align); /* align */
  14307. if (!check_simd_memory_access_align(
  14308. opcode1, align, error_buf, error_buf_size)) {
  14309. goto fail;
  14310. }
  14311. pb_read_leb_mem_offset(p, p_end,
  14312. mem_offset); /* offset */
  14313. CHECK_BUF(p, p_end, 1);
  14314. lane = read_uint8(p);
  14315. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14316. error_buf_size)) {
  14317. goto fail;
  14318. }
  14319. #if WASM_ENABLE_FAST_INTERP != 0
  14320. emit_uint32(loader_ctx, mem_offset);
  14321. #endif
  14322. POP_V128();
  14323. POP_MEM_OFFSET();
  14324. #if WASM_ENABLE_FAST_INTERP != 0
  14325. emit_byte(loader_ctx, lane);
  14326. #endif
  14327. if (opcode1 < SIMD_v128_store8_lane) {
  14328. PUSH_V128();
  14329. }
  14330. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14331. func->has_memory_operations = true;
  14332. #endif
  14333. break;
  14334. }
  14335. case SIMD_v128_load32_zero:
  14336. case SIMD_v128_load64_zero:
  14337. {
  14338. CHECK_MEMORY();
  14339. pb_read_leb_uint32(p, p_end, align); /* align */
  14340. if (!check_simd_memory_access_align(
  14341. opcode1, align, error_buf, error_buf_size)) {
  14342. goto fail;
  14343. }
  14344. pb_read_leb_mem_offset(p, p_end,
  14345. mem_offset); /* offset */
  14346. #if WASM_ENABLE_FAST_INTERP != 0
  14347. emit_uint32(loader_ctx, mem_offset);
  14348. #endif
  14349. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14350. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14351. func->has_memory_operations = true;
  14352. #endif
  14353. break;
  14354. }
  14355. /* Float conversion */
  14356. case SIMD_f32x4_demote_f64x2_zero:
  14357. case SIMD_f64x2_promote_low_f32x4_zero:
  14358. {
  14359. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14360. break;
  14361. }
  14362. /* i8x16 Operation */
  14363. case SIMD_i8x16_abs:
  14364. case SIMD_i8x16_neg:
  14365. case SIMD_i8x16_popcnt:
  14366. {
  14367. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14368. break;
  14369. }
  14370. case SIMD_i8x16_all_true:
  14371. case SIMD_i8x16_bitmask:
  14372. {
  14373. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14374. break;
  14375. }
  14376. case SIMD_i8x16_narrow_i16x8_s:
  14377. case SIMD_i8x16_narrow_i16x8_u:
  14378. {
  14379. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14380. break;
  14381. }
  14382. case SIMD_f32x4_ceil:
  14383. case SIMD_f32x4_floor:
  14384. case SIMD_f32x4_trunc:
  14385. case SIMD_f32x4_nearest:
  14386. {
  14387. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14388. break;
  14389. }
  14390. case SIMD_i8x16_shl:
  14391. case SIMD_i8x16_shr_s:
  14392. case SIMD_i8x16_shr_u:
  14393. {
  14394. POP_I32();
  14395. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14396. break;
  14397. }
  14398. case SIMD_i8x16_add:
  14399. case SIMD_i8x16_add_sat_s:
  14400. case SIMD_i8x16_add_sat_u:
  14401. case SIMD_i8x16_sub:
  14402. case SIMD_i8x16_sub_sat_s:
  14403. case SIMD_i8x16_sub_sat_u:
  14404. {
  14405. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14406. break;
  14407. }
  14408. case SIMD_f64x2_ceil:
  14409. case SIMD_f64x2_floor:
  14410. {
  14411. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14412. break;
  14413. }
  14414. case SIMD_i8x16_min_s:
  14415. case SIMD_i8x16_min_u:
  14416. case SIMD_i8x16_max_s:
  14417. case SIMD_i8x16_max_u:
  14418. {
  14419. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14420. break;
  14421. }
  14422. case SIMD_f64x2_trunc:
  14423. {
  14424. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14425. break;
  14426. }
  14427. case SIMD_i8x16_avgr_u:
  14428. {
  14429. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14430. break;
  14431. }
  14432. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14433. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14434. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14435. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14436. /* i16x8 operation */
  14437. case SIMD_i16x8_abs:
  14438. case SIMD_i16x8_neg:
  14439. {
  14440. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14441. break;
  14442. }
  14443. case SIMD_i16x8_q15mulr_sat_s:
  14444. {
  14445. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14446. break;
  14447. }
  14448. case SIMD_i16x8_all_true:
  14449. case SIMD_i16x8_bitmask:
  14450. {
  14451. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14452. break;
  14453. }
  14454. case SIMD_i16x8_narrow_i32x4_s:
  14455. case SIMD_i16x8_narrow_i32x4_u:
  14456. {
  14457. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14458. break;
  14459. }
  14460. case SIMD_i16x8_extend_low_i8x16_s:
  14461. case SIMD_i16x8_extend_high_i8x16_s:
  14462. case SIMD_i16x8_extend_low_i8x16_u:
  14463. case SIMD_i16x8_extend_high_i8x16_u:
  14464. {
  14465. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14466. break;
  14467. }
  14468. case SIMD_i16x8_shl:
  14469. case SIMD_i16x8_shr_s:
  14470. case SIMD_i16x8_shr_u:
  14471. {
  14472. POP_I32();
  14473. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14474. break;
  14475. }
  14476. case SIMD_i16x8_add:
  14477. case SIMD_i16x8_add_sat_s:
  14478. case SIMD_i16x8_add_sat_u:
  14479. case SIMD_i16x8_sub:
  14480. case SIMD_i16x8_sub_sat_s:
  14481. case SIMD_i16x8_sub_sat_u:
  14482. {
  14483. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14484. break;
  14485. }
  14486. case SIMD_f64x2_nearest:
  14487. {
  14488. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14489. break;
  14490. }
  14491. case SIMD_i16x8_mul:
  14492. case SIMD_i16x8_min_s:
  14493. case SIMD_i16x8_min_u:
  14494. case SIMD_i16x8_max_s:
  14495. case SIMD_i16x8_max_u:
  14496. case SIMD_i16x8_avgr_u:
  14497. case SIMD_i16x8_extmul_low_i8x16_s:
  14498. case SIMD_i16x8_extmul_high_i8x16_s:
  14499. case SIMD_i16x8_extmul_low_i8x16_u:
  14500. case SIMD_i16x8_extmul_high_i8x16_u:
  14501. {
  14502. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14503. break;
  14504. }
  14505. /* i32x4 operation */
  14506. case SIMD_i32x4_abs:
  14507. case SIMD_i32x4_neg:
  14508. {
  14509. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14510. break;
  14511. }
  14512. case SIMD_i32x4_all_true:
  14513. case SIMD_i32x4_bitmask:
  14514. {
  14515. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14516. break;
  14517. }
  14518. case SIMD_i32x4_extend_low_i16x8_s:
  14519. case SIMD_i32x4_extend_high_i16x8_s:
  14520. case SIMD_i32x4_extend_low_i16x8_u:
  14521. case SIMD_i32x4_extend_high_i16x8_u:
  14522. {
  14523. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14524. break;
  14525. }
  14526. case SIMD_i32x4_shl:
  14527. case SIMD_i32x4_shr_s:
  14528. case SIMD_i32x4_shr_u:
  14529. {
  14530. POP_I32();
  14531. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14532. break;
  14533. }
  14534. case SIMD_i32x4_add:
  14535. case SIMD_i32x4_sub:
  14536. case SIMD_i32x4_mul:
  14537. case SIMD_i32x4_min_s:
  14538. case SIMD_i32x4_min_u:
  14539. case SIMD_i32x4_max_s:
  14540. case SIMD_i32x4_max_u:
  14541. case SIMD_i32x4_dot_i16x8_s:
  14542. case SIMD_i32x4_extmul_low_i16x8_s:
  14543. case SIMD_i32x4_extmul_high_i16x8_s:
  14544. case SIMD_i32x4_extmul_low_i16x8_u:
  14545. case SIMD_i32x4_extmul_high_i16x8_u:
  14546. {
  14547. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14548. break;
  14549. }
  14550. /* i64x2 operation */
  14551. case SIMD_i64x2_abs:
  14552. case SIMD_i64x2_neg:
  14553. {
  14554. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14555. break;
  14556. }
  14557. case SIMD_i64x2_all_true:
  14558. case SIMD_i64x2_bitmask:
  14559. {
  14560. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14561. break;
  14562. }
  14563. case SIMD_i64x2_extend_low_i32x4_s:
  14564. case SIMD_i64x2_extend_high_i32x4_s:
  14565. case SIMD_i64x2_extend_low_i32x4_u:
  14566. case SIMD_i64x2_extend_high_i32x4_u:
  14567. {
  14568. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14569. break;
  14570. }
  14571. case SIMD_i64x2_shl:
  14572. case SIMD_i64x2_shr_s:
  14573. case SIMD_i64x2_shr_u:
  14574. {
  14575. POP_I32();
  14576. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14577. break;
  14578. }
  14579. case SIMD_i64x2_add:
  14580. case SIMD_i64x2_sub:
  14581. case SIMD_i64x2_mul:
  14582. case SIMD_i64x2_eq:
  14583. case SIMD_i64x2_ne:
  14584. case SIMD_i64x2_lt_s:
  14585. case SIMD_i64x2_gt_s:
  14586. case SIMD_i64x2_le_s:
  14587. case SIMD_i64x2_ge_s:
  14588. case SIMD_i64x2_extmul_low_i32x4_s:
  14589. case SIMD_i64x2_extmul_high_i32x4_s:
  14590. case SIMD_i64x2_extmul_low_i32x4_u:
  14591. case SIMD_i64x2_extmul_high_i32x4_u:
  14592. {
  14593. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14594. break;
  14595. }
  14596. /* f32x4 operation */
  14597. case SIMD_f32x4_abs:
  14598. case SIMD_f32x4_neg:
  14599. case SIMD_f32x4_sqrt:
  14600. {
  14601. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14602. break;
  14603. }
  14604. case SIMD_f32x4_add:
  14605. case SIMD_f32x4_sub:
  14606. case SIMD_f32x4_mul:
  14607. case SIMD_f32x4_div:
  14608. case SIMD_f32x4_min:
  14609. case SIMD_f32x4_max:
  14610. case SIMD_f32x4_pmin:
  14611. case SIMD_f32x4_pmax:
  14612. {
  14613. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14614. break;
  14615. }
  14616. /* f64x2 operation */
  14617. case SIMD_f64x2_abs:
  14618. case SIMD_f64x2_neg:
  14619. case SIMD_f64x2_sqrt:
  14620. {
  14621. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14622. break;
  14623. }
  14624. case SIMD_f64x2_add:
  14625. case SIMD_f64x2_sub:
  14626. case SIMD_f64x2_mul:
  14627. case SIMD_f64x2_div:
  14628. case SIMD_f64x2_min:
  14629. case SIMD_f64x2_max:
  14630. case SIMD_f64x2_pmin:
  14631. case SIMD_f64x2_pmax:
  14632. {
  14633. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14634. break;
  14635. }
  14636. case SIMD_i32x4_trunc_sat_f32x4_s:
  14637. case SIMD_i32x4_trunc_sat_f32x4_u:
  14638. case SIMD_f32x4_convert_i32x4_s:
  14639. case SIMD_f32x4_convert_i32x4_u:
  14640. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14641. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14642. case SIMD_f64x2_convert_low_i32x4_s:
  14643. case SIMD_f64x2_convert_low_i32x4_u:
  14644. {
  14645. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14646. break;
  14647. }
  14648. default:
  14649. {
  14650. if (error_buf != NULL) {
  14651. snprintf(error_buf, error_buf_size,
  14652. "WASM module load failed: "
  14653. "invalid opcode 0xfd %02x.",
  14654. opcode1);
  14655. }
  14656. goto fail;
  14657. }
  14658. }
  14659. break;
  14660. }
  14661. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  14662. (WASM_ENABLE_FAST_INTERP != 0) */
  14663. #endif /* end of WASM_ENABLE_SIMD */
  14664. #if WASM_ENABLE_SHARED_MEMORY != 0
  14665. case WASM_OP_ATOMIC_PREFIX:
  14666. {
  14667. uint32 opcode1;
  14668. pb_read_leb_uint32(p, p_end, opcode1);
  14669. #if WASM_ENABLE_FAST_INTERP != 0
  14670. emit_byte(loader_ctx, opcode1);
  14671. #endif
  14672. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14673. CHECK_MEMORY();
  14674. pb_read_leb_uint32(p, p_end, align); /* align */
  14675. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14676. if (!check_memory_align_equal(opcode1, align, error_buf,
  14677. error_buf_size)) {
  14678. goto fail;
  14679. }
  14680. #if WASM_ENABLE_FAST_INTERP != 0
  14681. emit_uint32(loader_ctx, mem_offset);
  14682. #endif
  14683. }
  14684. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14685. func->has_memory_operations = true;
  14686. #endif
  14687. switch (opcode1) {
  14688. case WASM_OP_ATOMIC_NOTIFY:
  14689. POP_I32();
  14690. POP_MEM_OFFSET();
  14691. PUSH_I32();
  14692. break;
  14693. case WASM_OP_ATOMIC_WAIT32:
  14694. POP_I64();
  14695. POP_I32();
  14696. POP_MEM_OFFSET();
  14697. PUSH_I32();
  14698. break;
  14699. case WASM_OP_ATOMIC_WAIT64:
  14700. POP_I64();
  14701. POP_I64();
  14702. POP_MEM_OFFSET();
  14703. PUSH_I32();
  14704. break;
  14705. case WASM_OP_ATOMIC_FENCE:
  14706. /* reserved byte 0x00 */
  14707. if (*p++ != 0x00) {
  14708. set_error_buf(error_buf, error_buf_size,
  14709. "zero byte expected");
  14710. goto fail;
  14711. }
  14712. break;
  14713. case WASM_OP_ATOMIC_I32_LOAD:
  14714. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14715. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14716. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14717. break;
  14718. case WASM_OP_ATOMIC_I32_STORE:
  14719. case WASM_OP_ATOMIC_I32_STORE8:
  14720. case WASM_OP_ATOMIC_I32_STORE16:
  14721. POP_I32();
  14722. POP_MEM_OFFSET();
  14723. break;
  14724. case WASM_OP_ATOMIC_I64_LOAD:
  14725. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14726. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14727. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14728. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14729. break;
  14730. case WASM_OP_ATOMIC_I64_STORE:
  14731. case WASM_OP_ATOMIC_I64_STORE8:
  14732. case WASM_OP_ATOMIC_I64_STORE16:
  14733. case WASM_OP_ATOMIC_I64_STORE32:
  14734. POP_I64();
  14735. POP_MEM_OFFSET();
  14736. break;
  14737. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14738. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14739. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14740. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14741. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14742. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14743. case WASM_OP_ATOMIC_RMW_I32_AND:
  14744. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14745. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14746. case WASM_OP_ATOMIC_RMW_I32_OR:
  14747. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14748. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14749. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14750. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14751. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14752. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14753. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14754. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14755. POP_I32();
  14756. POP_MEM_OFFSET();
  14757. PUSH_I32();
  14758. break;
  14759. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14760. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14761. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14762. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14763. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14764. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14765. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14766. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14767. case WASM_OP_ATOMIC_RMW_I64_AND:
  14768. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14769. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14770. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14771. case WASM_OP_ATOMIC_RMW_I64_OR:
  14772. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14773. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14774. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14775. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14776. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14777. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14778. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14779. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14780. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14781. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14782. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14783. POP_I64();
  14784. POP_MEM_OFFSET();
  14785. PUSH_I64();
  14786. break;
  14787. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14788. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14789. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14790. POP_I32();
  14791. POP_I32();
  14792. POP_MEM_OFFSET();
  14793. PUSH_I32();
  14794. break;
  14795. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14796. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14797. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14798. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14799. POP_I64();
  14800. POP_I64();
  14801. POP_MEM_OFFSET();
  14802. PUSH_I64();
  14803. break;
  14804. default:
  14805. set_error_buf_v(error_buf, error_buf_size,
  14806. "%s %02x %02x", "unsupported opcode",
  14807. 0xfe, opcode1);
  14808. goto fail;
  14809. }
  14810. break;
  14811. }
  14812. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14813. default:
  14814. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14815. "unsupported opcode", opcode);
  14816. goto fail;
  14817. }
  14818. #if WASM_ENABLE_FAST_INTERP != 0
  14819. last_op = opcode;
  14820. #endif
  14821. }
  14822. if (loader_ctx->csp_num > 0) {
  14823. /* unmatched end opcodes result from unbalanced control flow structures,
  14824. * for example, br_table with inconsistent target count (1 declared, 2
  14825. * given), or simply superfluous end opcodes */
  14826. set_error_buf(
  14827. error_buf, error_buf_size,
  14828. "unexpected end opcodes from unbalanced control flow structures");
  14829. goto fail;
  14830. }
  14831. #if WASM_ENABLE_FAST_INTERP != 0
  14832. if (loader_ctx->p_code_compiled == NULL)
  14833. goto re_scan;
  14834. func->const_cell_num = loader_ctx->i64_const_num * 2
  14835. + loader_ctx->v128_const_num * 4
  14836. + loader_ctx->i32_const_num;
  14837. if (func->const_cell_num > 0) {
  14838. if (!(func->consts =
  14839. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  14840. error_buf, error_buf_size)))
  14841. goto fail;
  14842. if (loader_ctx->i64_const_num > 0) {
  14843. bh_memcpy_s(func->consts,
  14844. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  14845. loader_ctx->i64_consts,
  14846. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  14847. }
  14848. if (loader_ctx->i32_const_num > 0) {
  14849. bh_memcpy_s(func->consts
  14850. + sizeof(int64) * loader_ctx->i64_const_num,
  14851. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  14852. loader_ctx->i32_consts,
  14853. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  14854. }
  14855. if (loader_ctx->v128_const_num > 0) {
  14856. bh_memcpy_s(func->consts,
  14857. (uint32)sizeof(V128) * loader_ctx->v128_const_num,
  14858. loader_ctx->v128_consts,
  14859. (uint32)sizeof(V128) * loader_ctx->v128_const_num);
  14860. }
  14861. }
  14862. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14863. - loader_ctx->start_dynamic_offset + 1;
  14864. #else
  14865. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14866. #endif
  14867. func->max_block_num = loader_ctx->max_csp_num;
  14868. return_value = true;
  14869. fail:
  14870. wasm_loader_ctx_destroy(loader_ctx);
  14871. (void)table_idx;
  14872. (void)table_seg_idx;
  14873. (void)data_seg_idx;
  14874. (void)i64_const;
  14875. (void)local_offset;
  14876. (void)p_org;
  14877. (void)mem_offset;
  14878. (void)align;
  14879. return return_value;
  14880. }