wasm_loader.c 612 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527
  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. || wasm_is_type_multi_byte_type(type1)) {
  738. p--;
  739. read_leb_uint32(p, p_end, type_idx);
  740. if (!check_type_index(module, module->type_count, type_idx,
  741. error_buf, error_buf_size))
  742. goto fail;
  743. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  744. true, type_idx);
  745. if (!push_const_expr_stack(&const_expr_ctx, flag,
  746. cur_ref_type.ref_type,
  747. &cur_ref_type, 0, &cur_value,
  748. error_buf, error_buf_size))
  749. goto fail;
  750. }
  751. else {
  752. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  753. NULL, 0, &cur_value, error_buf,
  754. error_buf_size))
  755. goto fail;
  756. }
  757. #endif
  758. #if WASM_ENABLE_WAMR_COMPILER != 0
  759. module->is_ref_types_used = true;
  760. #endif
  761. break;
  762. }
  763. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  764. /* get_global */
  765. case INIT_EXPR_TYPE_GET_GLOBAL:
  766. {
  767. uint32 global_idx;
  768. uint8 global_type;
  769. read_leb_uint32(p, p_end, cur_value.global_index);
  770. global_idx = cur_value.global_index;
  771. /*
  772. * Currently, constant expressions occurring as initializers
  773. * of globals are further constrained in that contained
  774. * global.get instructions are
  775. * only allowed to refer to imported globals.
  776. *
  777. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  778. */
  779. if (global_idx >= module->import_global_count
  780. /* make spec test happy */
  781. #if WASM_ENABLE_GC != 0
  782. + module->global_count
  783. #endif
  784. ) {
  785. set_error_buf_v(error_buf, error_buf_size,
  786. "unknown global %u", global_idx);
  787. goto fail;
  788. }
  789. if (
  790. /* make spec test happy */
  791. #if WASM_ENABLE_GC != 0
  792. global_idx < module->import_global_count &&
  793. #endif
  794. module->import_globals[global_idx]
  795. .u.global.type.is_mutable) {
  796. set_error_buf_v(error_buf, error_buf_size,
  797. "constant expression required");
  798. goto fail;
  799. }
  800. if (global_idx < module->import_global_count) {
  801. global_type = module->import_globals[global_idx]
  802. .u.global.type.val_type;
  803. #if WASM_ENABLE_GC != 0
  804. if (wasm_is_type_multi_byte_type(global_type)) {
  805. WASMRefType *global_ref_type =
  806. module->import_globals[global_idx]
  807. .u.global.ref_type;
  808. bh_memcpy_s(&cur_ref_type,
  809. wasm_reftype_struct_size(global_ref_type),
  810. global_ref_type,
  811. wasm_reftype_struct_size(global_ref_type));
  812. }
  813. #endif
  814. }
  815. else {
  816. global_type =
  817. module
  818. ->globals[global_idx - module->import_global_count]
  819. .type.val_type;
  820. #if WASM_ENABLE_GC != 0
  821. if (wasm_is_type_multi_byte_type(global_type)) {
  822. WASMRefType *global_ref_type =
  823. module
  824. ->globals[global_idx
  825. - module->import_global_count]
  826. .ref_type;
  827. bh_memcpy_s(&cur_ref_type,
  828. wasm_reftype_struct_size(global_ref_type),
  829. global_ref_type,
  830. wasm_reftype_struct_size(global_ref_type));
  831. }
  832. #endif
  833. }
  834. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  835. #if WASM_ENABLE_GC != 0
  836. &cur_ref_type, 0,
  837. #endif
  838. &cur_value, error_buf,
  839. error_buf_size))
  840. goto fail;
  841. break;
  842. }
  843. #if WASM_ENABLE_GC != 0
  844. /* struct.new and array.new */
  845. case WASM_OP_GC_PREFIX:
  846. {
  847. read_leb_uint32(p, p_end, opcode1);
  848. switch (opcode1) {
  849. case WASM_OP_STRUCT_NEW:
  850. {
  851. WASMStructType *struct_type;
  852. WASMStructNewInitValues *struct_init_values = NULL;
  853. uint32 field_count;
  854. read_leb_uint32(p, p_end, type_idx);
  855. if (!check_type_index(module, module->type_count,
  856. type_idx, error_buf,
  857. error_buf_size)) {
  858. goto fail;
  859. }
  860. struct_type = (WASMStructType *)module->types[type_idx];
  861. if (struct_type->base_type.type_flag
  862. != WASM_TYPE_STRUCT) {
  863. set_error_buf(error_buf, error_buf_size,
  864. "unknown struct type");
  865. goto fail;
  866. }
  867. field_count = struct_type->field_count;
  868. if (!(struct_init_values = loader_malloc(
  869. offsetof(WASMStructNewInitValues, fields)
  870. + (uint64)field_count * sizeof(WASMValue),
  871. error_buf, error_buf_size))) {
  872. goto fail;
  873. }
  874. struct_init_values->type_idx = type_idx;
  875. struct_init_values->count = field_count;
  876. for (i = field_count; i > 0; i--) {
  877. WASMRefType *field_ref_type = NULL;
  878. uint32 field_idx = i - 1;
  879. uint8 field_type =
  880. struct_type->fields[field_idx].field_type;
  881. if (wasm_is_type_multi_byte_type(field_type)) {
  882. field_ref_type = wasm_reftype_map_find(
  883. struct_type->ref_type_maps,
  884. struct_type->ref_type_map_count, field_idx);
  885. }
  886. if (is_packed_type(field_type)) {
  887. field_type = VALUE_TYPE_I32;
  888. }
  889. if (!pop_const_expr_stack(
  890. &const_expr_ctx, NULL, field_type,
  891. field_ref_type, NULL,
  892. &struct_init_values->fields[field_idx],
  893. error_buf, error_buf_size)) {
  894. destroy_init_expr_data_recursive(
  895. module, struct_init_values);
  896. goto fail;
  897. }
  898. }
  899. cur_value.data = struct_init_values;
  900. wasm_set_refheaptype_typeidx(
  901. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  902. if (!push_const_expr_stack(
  903. &const_expr_ctx, flag, cur_ref_type.ref_type,
  904. &cur_ref_type, (uint8)opcode1, &cur_value,
  905. error_buf, error_buf_size)) {
  906. destroy_init_expr_data_recursive(
  907. module, struct_init_values);
  908. goto fail;
  909. }
  910. break;
  911. }
  912. case WASM_OP_STRUCT_NEW_DEFAULT:
  913. {
  914. read_leb_uint32(p, p_end, cur_value.type_index);
  915. type_idx = cur_value.type_index;
  916. if (!check_type_index(module, module->type_count,
  917. type_idx, error_buf,
  918. error_buf_size)) {
  919. goto fail;
  920. }
  921. if (module->types[type_idx]->type_flag
  922. != WASM_TYPE_STRUCT) {
  923. set_error_buf(error_buf, error_buf_size,
  924. "unknown struct type");
  925. goto fail;
  926. }
  927. cur_value.type_index = type_idx;
  928. cur_value.data = NULL;
  929. wasm_set_refheaptype_typeidx(
  930. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  931. if (!push_const_expr_stack(
  932. &const_expr_ctx, flag, cur_ref_type.ref_type,
  933. &cur_ref_type, (uint8)opcode1, &cur_value,
  934. error_buf, error_buf_size)) {
  935. goto fail;
  936. }
  937. break;
  938. }
  939. case WASM_OP_ARRAY_NEW:
  940. case WASM_OP_ARRAY_NEW_DEFAULT:
  941. case WASM_OP_ARRAY_NEW_FIXED:
  942. {
  943. WASMArrayNewInitValues *array_init_values = NULL;
  944. WASMArrayType *array_type = NULL;
  945. WASMRefType *elem_ref_type = NULL;
  946. uint64 total_size;
  947. uint8 elem_type;
  948. read_leb_uint32(p, p_end, cur_value.type_index);
  949. type_idx = cur_value.type_index;
  950. if (!check_type_index(module, module->type_count,
  951. type_idx, error_buf,
  952. error_buf_size)) {
  953. goto fail;
  954. }
  955. array_type = (WASMArrayType *)module->types[type_idx];
  956. if (array_type->base_type.type_flag
  957. != WASM_TYPE_ARRAY) {
  958. set_error_buf(error_buf, error_buf_size,
  959. "unknown array type");
  960. goto fail;
  961. }
  962. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  963. elem_type = array_type->elem_type;
  964. if (wasm_is_type_multi_byte_type(elem_type)) {
  965. elem_ref_type = array_type->elem_ref_type;
  966. }
  967. if (is_packed_type(elem_type)) {
  968. elem_type = VALUE_TYPE_I32;
  969. }
  970. if (opcode1 == WASM_OP_ARRAY_NEW) {
  971. WASMValue len_val = { 0 };
  972. uint64 size = 0;
  973. if (!pop_const_expr_stack(
  974. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  975. NULL, NULL, &len_val, error_buf,
  976. error_buf_size)) {
  977. goto fail;
  978. }
  979. size =
  980. sizeof(WASMArrayNewInitValues)
  981. + sizeof(WASMValue) * (uint64)len_val.i32;
  982. if (!(array_init_values = loader_malloc(
  983. size, error_buf, error_buf_size))) {
  984. goto fail;
  985. }
  986. array_init_values->type_idx = type_idx;
  987. array_init_values->length = len_val.i32;
  988. if (!pop_const_expr_stack(
  989. &const_expr_ctx, NULL, elem_type,
  990. elem_ref_type, NULL,
  991. &array_init_values->elem_data[0],
  992. error_buf, error_buf_size)) {
  993. destroy_init_expr_data_recursive(
  994. module, array_init_values);
  995. goto fail;
  996. }
  997. cur_value.data = array_init_values;
  998. }
  999. else {
  1000. /* WASM_OP_ARRAY_NEW_FIXED */
  1001. uint32 len;
  1002. read_leb_uint32(p, p_end, len);
  1003. total_size =
  1004. (uint64)offsetof(WASMArrayNewInitValues,
  1005. elem_data)
  1006. + (uint64)sizeof(WASMValue) * len;
  1007. if (!(array_init_values =
  1008. loader_malloc(total_size, error_buf,
  1009. error_buf_size))) {
  1010. goto fail;
  1011. }
  1012. array_init_values->type_idx = type_idx;
  1013. array_init_values->length = len;
  1014. for (i = len; i > 0; i--) {
  1015. if (!pop_const_expr_stack(
  1016. &const_expr_ctx, NULL, elem_type,
  1017. elem_ref_type, NULL,
  1018. &array_init_values
  1019. ->elem_data[i - 1],
  1020. error_buf, error_buf_size)) {
  1021. destroy_init_expr_data_recursive(
  1022. module, array_init_values);
  1023. goto fail;
  1024. }
  1025. }
  1026. cur_value.data = array_init_values;
  1027. }
  1028. }
  1029. else {
  1030. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1031. WASMValue len_val;
  1032. uint32 len;
  1033. /* POP(i32) */
  1034. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1035. VALUE_TYPE_I32, NULL,
  1036. NULL, &len_val, error_buf,
  1037. error_buf_size)) {
  1038. goto fail;
  1039. }
  1040. len = len_val.i32;
  1041. cur_value.array_new_default.type_index = type_idx;
  1042. cur_value.array_new_default.length = len;
  1043. }
  1044. wasm_set_refheaptype_typeidx(
  1045. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1046. if (!push_const_expr_stack(
  1047. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1048. &cur_ref_type, (uint8)opcode1, &cur_value,
  1049. error_buf, error_buf_size)) {
  1050. if (array_init_values) {
  1051. destroy_init_expr_data_recursive(
  1052. module, array_init_values);
  1053. }
  1054. goto fail;
  1055. }
  1056. break;
  1057. }
  1058. case WASM_OP_ANY_CONVERT_EXTERN:
  1059. {
  1060. set_error_buf(error_buf, error_buf_size,
  1061. "unsupported constant expression of "
  1062. "extern.internalize");
  1063. goto fail;
  1064. }
  1065. case WASM_OP_EXTERN_CONVERT_ANY:
  1066. {
  1067. set_error_buf(error_buf, error_buf_size,
  1068. "unsupported constant expression of "
  1069. "extern.externalize");
  1070. goto fail;
  1071. }
  1072. case WASM_OP_REF_I31:
  1073. {
  1074. /* POP(i32) */
  1075. if (!pop_const_expr_stack(
  1076. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1077. NULL, &cur_value, error_buf, error_buf_size)) {
  1078. goto fail;
  1079. }
  1080. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1081. false, HEAP_TYPE_I31);
  1082. if (!push_const_expr_stack(
  1083. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1084. &cur_ref_type, (uint8)opcode1, &cur_value,
  1085. error_buf, error_buf_size)) {
  1086. goto fail;
  1087. }
  1088. break;
  1089. }
  1090. default:
  1091. set_error_buf(
  1092. error_buf, error_buf_size,
  1093. "type mismatch or constant expression required");
  1094. goto fail;
  1095. }
  1096. break;
  1097. }
  1098. #endif /* end of WASM_ENABLE_GC != 0 */
  1099. default:
  1100. {
  1101. set_error_buf(error_buf, error_buf_size,
  1102. "illegal opcode "
  1103. "or constant expression required "
  1104. "or type mismatch");
  1105. goto fail;
  1106. }
  1107. }
  1108. CHECK_BUF(p, p_end, 1);
  1109. flag = read_uint8(p);
  1110. }
  1111. /* There should be only one value left on the init value stack */
  1112. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1113. #if WASM_ENABLE_GC != 0
  1114. ref_type, &opcode,
  1115. #endif
  1116. &cur_value, error_buf, error_buf_size)) {
  1117. goto fail;
  1118. }
  1119. if (const_expr_ctx.sp != 0) {
  1120. set_error_buf(error_buf, error_buf_size,
  1121. "type mismatch: illegal constant opcode sequence");
  1122. goto fail;
  1123. }
  1124. init_expr->init_expr_type = flag;
  1125. init_expr->u = cur_value;
  1126. #if WASM_ENABLE_GC != 0
  1127. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1128. switch (opcode) {
  1129. case WASM_OP_STRUCT_NEW:
  1130. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1131. break;
  1132. case WASM_OP_STRUCT_NEW_DEFAULT:
  1133. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1134. break;
  1135. case WASM_OP_ARRAY_NEW:
  1136. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1137. break;
  1138. case WASM_OP_ARRAY_NEW_DEFAULT:
  1139. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1140. break;
  1141. case WASM_OP_ARRAY_NEW_FIXED:
  1142. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1143. break;
  1144. case WASM_OP_REF_I31:
  1145. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1146. break;
  1147. default:
  1148. bh_assert(0);
  1149. break;
  1150. }
  1151. }
  1152. #endif /* end of WASM_ENABLE_GC != 0 */
  1153. *p_buf = p;
  1154. destroy_const_expr_stack(&const_expr_ctx);
  1155. return true;
  1156. fail:
  1157. destroy_const_expr_stack(&const_expr_ctx);
  1158. return false;
  1159. }
  1160. static bool
  1161. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1162. {
  1163. if (mutable >= 2) {
  1164. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1165. return false;
  1166. }
  1167. return true;
  1168. }
  1169. #if WASM_ENABLE_GC != 0
  1170. static void
  1171. destroy_func_type(WASMFuncType *type)
  1172. {
  1173. /* Destroy the reference type hash set */
  1174. if (type->ref_type_maps)
  1175. wasm_runtime_free(type->ref_type_maps);
  1176. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1177. && WASM_ENABLE_LAZY_JIT != 0
  1178. if (type->call_to_llvm_jit_from_fast_jit)
  1179. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1180. #endif
  1181. /* Free the type */
  1182. wasm_runtime_free(type);
  1183. }
  1184. static void
  1185. destroy_struct_type(WASMStructType *type)
  1186. {
  1187. if (type->ref_type_maps)
  1188. wasm_runtime_free(type->ref_type_maps);
  1189. wasm_runtime_free(type);
  1190. }
  1191. static void
  1192. destroy_array_type(WASMArrayType *type)
  1193. {
  1194. wasm_runtime_free(type);
  1195. }
  1196. static void
  1197. destroy_wasm_type(WASMType *type)
  1198. {
  1199. if (type->ref_count > 1) {
  1200. /* The type is referenced by other types
  1201. of current wasm module */
  1202. type->ref_count--;
  1203. return;
  1204. }
  1205. if (type->type_flag == WASM_TYPE_FUNC)
  1206. destroy_func_type((WASMFuncType *)type);
  1207. else if (type->type_flag == WASM_TYPE_STRUCT)
  1208. destroy_struct_type((WASMStructType *)type);
  1209. else if (type->type_flag == WASM_TYPE_ARRAY)
  1210. destroy_array_type((WASMArrayType *)type);
  1211. else {
  1212. bh_assert(0);
  1213. }
  1214. }
  1215. /* Resolve (ref null ht) or (ref ht) */
  1216. static bool
  1217. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1218. WASMModule *module, uint32 type_count, bool nullable,
  1219. WASMRefType *ref_type, char *error_buf,
  1220. uint32 error_buf_size)
  1221. {
  1222. const uint8 *p = *p_buf, *p_end = buf_end;
  1223. ref_type->ref_type =
  1224. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1225. ref_type->ref_ht_common.nullable = nullable;
  1226. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1227. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1228. /* heap type is (type i), i : typeidx, >= 0 */
  1229. if (!check_type_index(module, type_count,
  1230. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1231. error_buf_size)) {
  1232. return false;
  1233. }
  1234. }
  1235. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1236. /* heap type is func, extern, any, eq, i31 or data */
  1237. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1238. return false;
  1239. }
  1240. *p_buf = p;
  1241. return true;
  1242. fail:
  1243. return false;
  1244. }
  1245. static bool
  1246. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1247. WASMModule *module, uint32 type_count,
  1248. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1249. bool allow_packed_type, char *error_buf,
  1250. uint32 error_buf_size)
  1251. {
  1252. const uint8 *p = *p_buf, *p_end = buf_end;
  1253. uint8 type;
  1254. memset(ref_type, 0, sizeof(WASMRefType));
  1255. CHECK_BUF(p, p_end, 1);
  1256. type = read_uint8(p);
  1257. if (wasm_is_reftype_htref_nullable(type)) {
  1258. /* (ref null ht) */
  1259. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1260. ref_type, error_buf, error_buf_size))
  1261. return false;
  1262. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1263. *p_need_ref_type_map = true;
  1264. else {
  1265. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1266. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1267. multi-byte type to one-byte type to reduce the footprint and
  1268. the complexity of type equal/subtype checking */
  1269. ref_type->ref_type =
  1270. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1271. *p_need_ref_type_map = false;
  1272. }
  1273. }
  1274. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1275. /* (ref ht) */
  1276. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1277. ref_type, error_buf, error_buf_size))
  1278. return false;
  1279. *p_need_ref_type_map = true;
  1280. #if WASM_ENABLE_STRINGREF != 0
  1281. /* covert (ref string) to stringref */
  1282. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1283. ref_type->ref_type =
  1284. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1285. *p_need_ref_type_map = false;
  1286. }
  1287. #endif
  1288. }
  1289. else {
  1290. /* type which can be represented by one byte */
  1291. if (!is_valid_value_type_for_interpreter(type)
  1292. && !(allow_packed_type && is_packed_type(type))) {
  1293. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1294. return false;
  1295. }
  1296. ref_type->ref_type = type;
  1297. *p_need_ref_type_map = false;
  1298. #if WASM_ENABLE_WAMR_COMPILER != 0
  1299. /* If any value's type is v128, mark the module as SIMD used */
  1300. if (type == VALUE_TYPE_V128)
  1301. module->is_simd_used = true;
  1302. #endif
  1303. }
  1304. *p_buf = p;
  1305. return true;
  1306. fail:
  1307. return false;
  1308. }
  1309. static WASMRefType *
  1310. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1311. char *error_buf, uint32 error_buf_size)
  1312. {
  1313. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1314. if (!ret) {
  1315. set_error_buf(error_buf, error_buf_size,
  1316. "insert ref type to hash set failed");
  1317. }
  1318. return ret;
  1319. }
  1320. static bool
  1321. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1322. uint32 type_count, uint32 type_idx, char *error_buf,
  1323. uint32 error_buf_size)
  1324. {
  1325. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1326. uint32 param_count, result_count, i, j = 0;
  1327. uint32 param_cell_num, ret_cell_num;
  1328. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1329. uint64 total_size;
  1330. bool need_ref_type_map;
  1331. WASMRefType ref_type;
  1332. WASMFuncType *type = NULL;
  1333. /* Parse first time to resolve param count, result count and
  1334. ref type map count */
  1335. read_leb_uint32(p, p_end, param_count);
  1336. p_org = p;
  1337. for (i = 0; i < param_count; i++) {
  1338. if (!resolve_value_type(&p, p_end, module, type_count,
  1339. &need_ref_type_map, &ref_type, false, error_buf,
  1340. error_buf_size)) {
  1341. return false;
  1342. }
  1343. if (need_ref_type_map)
  1344. ref_type_map_count++;
  1345. }
  1346. read_leb_uint32(p, p_end, result_count);
  1347. for (i = 0; i < result_count; i++) {
  1348. if (!resolve_value_type(&p, p_end, module, type_count,
  1349. &need_ref_type_map, &ref_type, false, error_buf,
  1350. error_buf_size)) {
  1351. return false;
  1352. }
  1353. if (need_ref_type_map) {
  1354. ref_type_map_count++;
  1355. result_ref_type_map_count++;
  1356. }
  1357. }
  1358. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1359. "ref type map count: %d",
  1360. type_idx, param_count, result_count, ref_type_map_count);
  1361. /* Parse second time to resolve param types, result types and
  1362. ref type map info */
  1363. p = p_org;
  1364. total_size = offsetof(WASMFuncType, types)
  1365. + sizeof(uint8) * (uint64)(param_count + result_count);
  1366. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1367. return false;
  1368. }
  1369. if (ref_type_map_count > 0) {
  1370. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1371. if (!(type->ref_type_maps =
  1372. loader_malloc(total_size, error_buf, error_buf_size))) {
  1373. goto fail;
  1374. }
  1375. }
  1376. type->base_type.type_flag = WASM_TYPE_FUNC;
  1377. type->param_count = param_count;
  1378. type->result_count = result_count;
  1379. type->ref_type_map_count = ref_type_map_count;
  1380. if (ref_type_map_count > 0) {
  1381. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1382. - result_ref_type_map_count;
  1383. }
  1384. for (i = 0; i < param_count; i++) {
  1385. if (!resolve_value_type(&p, p_end, module, type_count,
  1386. &need_ref_type_map, &ref_type, false, error_buf,
  1387. error_buf_size)) {
  1388. goto fail;
  1389. }
  1390. type->types[i] = ref_type.ref_type;
  1391. if (need_ref_type_map) {
  1392. type->ref_type_maps[j].index = i;
  1393. if (!(type->ref_type_maps[j++].ref_type =
  1394. reftype_set_insert(module->ref_type_set, &ref_type,
  1395. error_buf, error_buf_size))) {
  1396. goto fail;
  1397. }
  1398. }
  1399. }
  1400. read_leb_uint32(p, p_end, result_count);
  1401. for (i = 0; i < result_count; i++) {
  1402. if (!resolve_value_type(&p, p_end, module, type_count,
  1403. &need_ref_type_map, &ref_type, false, error_buf,
  1404. error_buf_size)) {
  1405. goto fail;
  1406. }
  1407. type->types[param_count + i] = ref_type.ref_type;
  1408. if (need_ref_type_map) {
  1409. type->ref_type_maps[j].index = param_count + i;
  1410. if (!(type->ref_type_maps[j++].ref_type =
  1411. reftype_set_insert(module->ref_type_set, &ref_type,
  1412. error_buf, error_buf_size))) {
  1413. goto fail;
  1414. }
  1415. }
  1416. }
  1417. bh_assert(j == type->ref_type_map_count);
  1418. #if TRACE_WASM_LOADER != 0
  1419. os_printf("type %d = ", type_idx);
  1420. wasm_dump_func_type(type);
  1421. #endif
  1422. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1423. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1424. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1425. set_error_buf(error_buf, error_buf_size,
  1426. "param count or result count too large");
  1427. goto fail;
  1428. }
  1429. type->param_cell_num = (uint16)param_cell_num;
  1430. type->ret_cell_num = (uint16)ret_cell_num;
  1431. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1432. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1433. #endif
  1434. #if WASM_ENABLE_WAMR_COMPILER != 0
  1435. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1436. if (type->types[i] == VALUE_TYPE_V128)
  1437. module->is_simd_used = true;
  1438. }
  1439. #endif
  1440. *p_buf = p;
  1441. module->types[type_idx] = (WASMType *)type;
  1442. return true;
  1443. fail:
  1444. if (type)
  1445. destroy_func_type(type);
  1446. return false;
  1447. }
  1448. static bool
  1449. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1450. WASMModule *module, uint32 type_count, uint32 type_idx,
  1451. char *error_buf, uint32 error_buf_size)
  1452. {
  1453. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1454. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1455. uint32 i, j = 0, offset;
  1456. uint16 *reference_table;
  1457. uint64 total_size;
  1458. uint8 mutable;
  1459. bool need_ref_type_map;
  1460. WASMRefType ref_type;
  1461. WASMStructType *type = NULL;
  1462. /* Parse first time to resolve field count and ref type map count */
  1463. read_leb_uint32(p, p_end, field_count);
  1464. p_org = p;
  1465. for (i = 0; i < field_count; i++) {
  1466. if (!resolve_value_type(&p, p_end, module, type_count,
  1467. &need_ref_type_map, &ref_type, true, error_buf,
  1468. error_buf_size)) {
  1469. return false;
  1470. }
  1471. if (need_ref_type_map)
  1472. ref_type_map_count++;
  1473. if (wasm_is_type_reftype(ref_type.ref_type))
  1474. ref_field_count++;
  1475. CHECK_BUF(p, p_end, 1);
  1476. mutable = read_uint8(p);
  1477. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1478. return false;
  1479. }
  1480. }
  1481. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1482. type_idx, field_count, ref_type_map_count);
  1483. /* Parse second time to resolve field types and ref type map info */
  1484. p = p_org;
  1485. total_size = offsetof(WASMStructType, fields)
  1486. + sizeof(WASMStructFieldType) * (uint64)field_count
  1487. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1488. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1489. return false;
  1490. }
  1491. if (ref_type_map_count > 0) {
  1492. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1493. if (!(type->ref_type_maps =
  1494. loader_malloc(total_size, error_buf, error_buf_size))) {
  1495. goto fail;
  1496. }
  1497. }
  1498. type->reference_table = reference_table =
  1499. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1500. + sizeof(WASMStructFieldType) * field_count);
  1501. *reference_table++ = ref_field_count;
  1502. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1503. type->field_count = field_count;
  1504. type->ref_type_map_count = ref_type_map_count;
  1505. offset = (uint32)offsetof(WASMStructObject, field_data);
  1506. for (i = 0; i < field_count; i++) {
  1507. if (!resolve_value_type(&p, p_end, module, type_count,
  1508. &need_ref_type_map, &ref_type, true, error_buf,
  1509. error_buf_size)) {
  1510. goto fail;
  1511. }
  1512. type->fields[i].field_type = ref_type.ref_type;
  1513. if (need_ref_type_map) {
  1514. type->ref_type_maps[j].index = i;
  1515. if (!(type->ref_type_maps[j++].ref_type =
  1516. reftype_set_insert(module->ref_type_set, &ref_type,
  1517. error_buf, error_buf_size))) {
  1518. goto fail;
  1519. }
  1520. }
  1521. CHECK_BUF(p, p_end, 1);
  1522. type->fields[i].field_flags = read_uint8(p);
  1523. type->fields[i].field_size =
  1524. (uint8)wasm_reftype_size(ref_type.ref_type);
  1525. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1526. || defined(BUILD_TARGET_X86_32))
  1527. if (type->fields[i].field_size == 2)
  1528. offset = align_uint(offset, 2);
  1529. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1530. offset = align_uint(offset, 4);
  1531. #endif
  1532. type->fields[i].field_offset = offset;
  1533. if (wasm_is_type_reftype(ref_type.ref_type))
  1534. *reference_table++ = offset;
  1535. offset += type->fields[i].field_size;
  1536. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1537. type->fields[i].field_flags, type->fields[i].field_type);
  1538. }
  1539. type->total_size = offset;
  1540. bh_assert(j == type->ref_type_map_count);
  1541. #if TRACE_WASM_LOADER != 0
  1542. os_printf("type %d = ", type_idx);
  1543. wasm_dump_struct_type(type);
  1544. #endif
  1545. *p_buf = p;
  1546. module->types[type_idx] = (WASMType *)type;
  1547. return true;
  1548. fail:
  1549. if (type)
  1550. destroy_struct_type(type);
  1551. return false;
  1552. }
  1553. static bool
  1554. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1555. WASMModule *module, uint32 type_count, uint32 type_idx,
  1556. char *error_buf, uint32 error_buf_size)
  1557. {
  1558. const uint8 *p = *p_buf, *p_end = buf_end;
  1559. uint8 mutable;
  1560. bool need_ref_type_map;
  1561. WASMRefType ref_type;
  1562. WASMArrayType *type = NULL;
  1563. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1564. &ref_type, true, error_buf, error_buf_size)) {
  1565. return false;
  1566. }
  1567. CHECK_BUF(p, p_end, 1);
  1568. mutable = read_uint8(p);
  1569. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1570. return false;
  1571. }
  1572. LOG_VERBOSE("type %u: array", type_idx);
  1573. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1574. error_buf_size))) {
  1575. return false;
  1576. }
  1577. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1578. type->elem_flags = mutable;
  1579. type->elem_type = ref_type.ref_type;
  1580. if (need_ref_type_map) {
  1581. if (!(type->elem_ref_type =
  1582. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1583. error_buf_size))) {
  1584. goto fail;
  1585. }
  1586. }
  1587. #if TRACE_WASM_LOADER != 0
  1588. os_printf("type %d = ", type_idx);
  1589. wasm_dump_array_type(type);
  1590. #endif
  1591. *p_buf = p;
  1592. module->types[type_idx] = (WASMType *)type;
  1593. return true;
  1594. fail:
  1595. if (type)
  1596. destroy_array_type(type);
  1597. return false;
  1598. }
  1599. static bool
  1600. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1601. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1602. {
  1603. if (heap_type >= 0) {
  1604. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1605. error_buf_size)) {
  1606. return false;
  1607. }
  1608. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1609. heap_type);
  1610. }
  1611. else {
  1612. if (!wasm_is_valid_heap_type(heap_type)) {
  1613. set_error_buf(error_buf, error_buf_size, "unknown type");
  1614. return false;
  1615. }
  1616. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1617. heap_type);
  1618. if (nullable) {
  1619. /* For (ref null func/extern/any/eq/i31/data),
  1620. they are same as
  1621. funcref/externref/anyref/eqref/i31ref/dataref,
  1622. we convert the multi-byte type to one-byte
  1623. type to reduce the footprint and the
  1624. complexity of type equal/subtype checking */
  1625. ref_type->ref_type =
  1626. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1627. }
  1628. }
  1629. return true;
  1630. }
  1631. static void
  1632. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1633. WASMRefType *ref_type2)
  1634. {
  1635. /**
  1636. * The difference rt1 ∖ rt2 between two reference types is defined as
  1637. * follows:
  1638. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1639. * (ref ht2) = (ref null?1 ht1)
  1640. */
  1641. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1642. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1643. ref_type1, wasm_reftype_struct_size(ref_type1));
  1644. }
  1645. else {
  1646. ref_type_diff->ref_type = ref_type1->ref_type;
  1647. }
  1648. if (ref_type2->ref_ht_common.nullable) {
  1649. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1650. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1651. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1652. (int32)ref_type_diff->ref_type - 0x80);
  1653. }
  1654. else {
  1655. ref_type_diff->ref_ht_typeidx.nullable = false;
  1656. }
  1657. }
  1658. }
  1659. #else /* else of WASM_ENABLE_GC != 0 */
  1660. static void
  1661. destroy_wasm_type(WASMType *type)
  1662. {
  1663. if (type->ref_count > 1) {
  1664. /* The type is referenced by other types
  1665. of current wasm module */
  1666. type->ref_count--;
  1667. return;
  1668. }
  1669. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1670. && WASM_ENABLE_LAZY_JIT != 0
  1671. if (type->call_to_llvm_jit_from_fast_jit)
  1672. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1673. #endif
  1674. wasm_runtime_free(type);
  1675. }
  1676. #endif /* end of WASM_ENABLE_GC != 0 */
  1677. static bool
  1678. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1679. char *error_buf, uint32 error_buf_size)
  1680. {
  1681. const uint8 *p = buf, *p_end = buf_end;
  1682. uint32 type_count, i;
  1683. uint64 total_size;
  1684. uint8 flag;
  1685. #if WASM_ENABLE_GC != 0
  1686. uint32 processed_type_count = 0;
  1687. #endif
  1688. read_leb_uint32(p, p_end, type_count);
  1689. if (type_count) {
  1690. module->type_count = type_count;
  1691. total_size = sizeof(WASMType *) * (uint64)type_count;
  1692. if (!(module->types =
  1693. loader_malloc(total_size, error_buf, error_buf_size))) {
  1694. return false;
  1695. }
  1696. #if WASM_ENABLE_GC == 0
  1697. for (i = 0; i < type_count; i++) {
  1698. WASMFuncType *type;
  1699. const uint8 *p_org;
  1700. uint32 param_count, result_count, j;
  1701. uint32 param_cell_num, ret_cell_num;
  1702. CHECK_BUF(p, p_end, 1);
  1703. flag = read_uint8(p);
  1704. if (flag != 0x60) {
  1705. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1706. return false;
  1707. }
  1708. read_leb_uint32(p, p_end, param_count);
  1709. /* Resolve param count and result count firstly */
  1710. p_org = p;
  1711. CHECK_BUF(p, p_end, param_count);
  1712. p += param_count;
  1713. read_leb_uint32(p, p_end, result_count);
  1714. CHECK_BUF(p, p_end, result_count);
  1715. p = p_org;
  1716. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1717. set_error_buf(error_buf, error_buf_size,
  1718. "param count or result count too large");
  1719. return false;
  1720. }
  1721. total_size = offsetof(WASMFuncType, types)
  1722. + sizeof(uint8) * (uint64)(param_count + result_count);
  1723. if (!(type = module->types[i] =
  1724. loader_malloc(total_size, error_buf, error_buf_size))) {
  1725. return false;
  1726. }
  1727. /* Resolve param types and result types */
  1728. type->ref_count = 1;
  1729. type->param_count = (uint16)param_count;
  1730. type->result_count = (uint16)result_count;
  1731. for (j = 0; j < param_count; j++) {
  1732. CHECK_BUF(p, p_end, 1);
  1733. type->types[j] = read_uint8(p);
  1734. }
  1735. read_leb_uint32(p, p_end, result_count);
  1736. for (j = 0; j < result_count; j++) {
  1737. CHECK_BUF(p, p_end, 1);
  1738. type->types[param_count + j] = read_uint8(p);
  1739. }
  1740. for (j = 0; j < param_count + result_count; j++) {
  1741. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1742. set_error_buf(error_buf, error_buf_size,
  1743. "unknown value type");
  1744. return false;
  1745. }
  1746. }
  1747. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1748. ret_cell_num =
  1749. wasm_get_cell_num(type->types + param_count, result_count);
  1750. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1751. set_error_buf(error_buf, error_buf_size,
  1752. "param count or result count too large");
  1753. return false;
  1754. }
  1755. type->param_cell_num = (uint16)param_cell_num;
  1756. type->ret_cell_num = (uint16)ret_cell_num;
  1757. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1758. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1759. #endif
  1760. #if WASM_ENABLE_WAMR_COMPILER != 0
  1761. for (j = 0; j < type->param_count + type->result_count; j++) {
  1762. if (type->types[j] == VALUE_TYPE_V128)
  1763. module->is_simd_used = true;
  1764. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1765. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1766. module->is_ref_types_used = true;
  1767. }
  1768. #endif
  1769. /* If there is already a same type created, use it instead */
  1770. for (j = 0; j < i; j++) {
  1771. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1772. if (module->types[j]->ref_count == UINT16_MAX) {
  1773. set_error_buf(error_buf, error_buf_size,
  1774. "wasm type's ref count too large");
  1775. return false;
  1776. }
  1777. destroy_wasm_type(type);
  1778. module->types[i] = module->types[j];
  1779. module->types[j]->ref_count++;
  1780. break;
  1781. }
  1782. }
  1783. }
  1784. #else /* else of WASM_ENABLE_GC == 0 */
  1785. for (i = 0; i < type_count; i++) {
  1786. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1787. uint32 rec_count = 1, j;
  1788. bool is_sub_final = true;
  1789. CHECK_BUF(p, p_end, 1);
  1790. flag = read_uint8(p);
  1791. if (flag == DEFINED_TYPE_REC) {
  1792. read_leb_uint32(p, p_end, rec_count);
  1793. if (rec_count > 1) {
  1794. uint64 new_total_size;
  1795. /* integer overflow */
  1796. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1797. set_error_buf(error_buf, error_buf_size,
  1798. "recursive type count too large");
  1799. return false;
  1800. }
  1801. module->type_count += rec_count - 1;
  1802. new_total_size =
  1803. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1804. if (new_total_size > UINT32_MAX) {
  1805. set_error_buf(error_buf, error_buf_size,
  1806. "allocate memory failed");
  1807. return false;
  1808. }
  1809. MEM_REALLOC(module->types, (uint32)total_size,
  1810. (uint32)new_total_size);
  1811. total_size = new_total_size;
  1812. }
  1813. LOG_VERBOSE("Processing rec group [%d-%d]",
  1814. processed_type_count,
  1815. processed_type_count + rec_count - 1);
  1816. }
  1817. else {
  1818. p--;
  1819. }
  1820. for (j = 0; j < rec_count; j++) {
  1821. WASMType *cur_type = NULL;
  1822. CHECK_BUF(p, p_end, 1);
  1823. flag = read_uint8(p);
  1824. parent_type_idx = -1;
  1825. if (flag == DEFINED_TYPE_SUB
  1826. || flag == DEFINED_TYPE_SUB_FINAL) {
  1827. read_leb_uint32(p, p_end, super_type_count);
  1828. if (super_type_count > 1) {
  1829. set_error_buf(error_buf, error_buf_size,
  1830. "super type count too large");
  1831. return false;
  1832. }
  1833. if (super_type_count > 0) {
  1834. read_leb_uint32(p, p_end, parent_type_idx);
  1835. if (parent_type_idx >= processed_type_count + j) {
  1836. set_error_buf_v(error_buf, error_buf_size,
  1837. "unknown type %d", parent_type_idx);
  1838. return false;
  1839. }
  1840. if (module->types[parent_type_idx]->is_sub_final) {
  1841. set_error_buf(error_buf, error_buf_size,
  1842. "sub type can not inherit from "
  1843. "a final super type");
  1844. return false;
  1845. }
  1846. }
  1847. if (flag == DEFINED_TYPE_SUB)
  1848. is_sub_final = false;
  1849. CHECK_BUF(p, p_end, 1);
  1850. flag = read_uint8(p);
  1851. }
  1852. if (flag == DEFINED_TYPE_FUNC) {
  1853. if (!resolve_func_type(&p, buf_end, module,
  1854. processed_type_count + rec_count,
  1855. processed_type_count + j, error_buf,
  1856. error_buf_size)) {
  1857. return false;
  1858. }
  1859. }
  1860. else if (flag == DEFINED_TYPE_STRUCT) {
  1861. if (!resolve_struct_type(&p, buf_end, module,
  1862. processed_type_count + rec_count,
  1863. processed_type_count + j,
  1864. error_buf, error_buf_size)) {
  1865. return false;
  1866. }
  1867. }
  1868. else if (flag == DEFINED_TYPE_ARRAY) {
  1869. if (!resolve_array_type(&p, buf_end, module,
  1870. processed_type_count + rec_count,
  1871. processed_type_count + j, error_buf,
  1872. error_buf_size)) {
  1873. return false;
  1874. }
  1875. }
  1876. else {
  1877. set_error_buf(error_buf, error_buf_size,
  1878. "invalid type flag");
  1879. return false;
  1880. }
  1881. cur_type = module->types[processed_type_count + j];
  1882. cur_type->ref_count = 1;
  1883. cur_type->parent_type_idx = parent_type_idx;
  1884. cur_type->is_sub_final = is_sub_final;
  1885. cur_type->rec_count = rec_count;
  1886. cur_type->rec_idx = j;
  1887. cur_type->rec_begin_type_idx = processed_type_count;
  1888. }
  1889. /* resolve subtyping relationship in current rec group */
  1890. for (j = 0; j < rec_count; j++) {
  1891. WASMType *cur_type = module->types[processed_type_count + j];
  1892. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1893. WASMType *parent_type =
  1894. module->types[cur_type->parent_type_idx];
  1895. cur_type->parent_type = parent_type;
  1896. cur_type->root_type = parent_type->root_type;
  1897. if (parent_type->inherit_depth == UINT16_MAX) {
  1898. set_error_buf(error_buf, error_buf_size,
  1899. "parent type's inherit depth too large");
  1900. return false;
  1901. }
  1902. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1903. }
  1904. else {
  1905. cur_type->parent_type = NULL;
  1906. cur_type->root_type = cur_type;
  1907. cur_type->inherit_depth = 0;
  1908. }
  1909. }
  1910. for (j = 0; j < rec_count; j++) {
  1911. WASMType *cur_type = module->types[processed_type_count + j];
  1912. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1913. WASMType *parent_type =
  1914. module->types[cur_type->parent_type_idx];
  1915. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1916. module->types,
  1917. module->type_count)) {
  1918. set_error_buf_v(error_buf, error_buf_size,
  1919. "sub type %u does not match super type",
  1920. processed_type_count + j);
  1921. return false;
  1922. }
  1923. }
  1924. }
  1925. /* If there is already an equivalence type or a group of equivalence
  1926. recursive types created, use it or them instead */
  1927. for (j = 0; j < processed_type_count;) {
  1928. WASMType *src_type = module->types[j];
  1929. WASMType *cur_type = module->types[processed_type_count];
  1930. uint32 k, src_rec_count;
  1931. src_rec_count = src_type->rec_count;
  1932. if (src_rec_count != rec_count) {
  1933. /* no type equivalence */
  1934. j += src_rec_count;
  1935. continue;
  1936. }
  1937. for (k = 0; k < rec_count; k++) {
  1938. src_type = module->types[j + k];
  1939. cur_type = module->types[processed_type_count + k];
  1940. if (!wasm_type_equal(src_type, cur_type, module->types,
  1941. module->type_count)) {
  1942. break;
  1943. }
  1944. }
  1945. if (k < rec_count) {
  1946. /* no type equivalence */
  1947. j += src_rec_count;
  1948. continue;
  1949. }
  1950. /* type equivalence */
  1951. for (k = 0; k < rec_count; k++) {
  1952. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1953. set_error_buf(error_buf, error_buf_size,
  1954. "wasm type's ref count too large");
  1955. return false;
  1956. }
  1957. destroy_wasm_type(module->types[processed_type_count + k]);
  1958. module->types[processed_type_count + k] =
  1959. module->types[j + k];
  1960. module->types[j + k]->ref_count++;
  1961. }
  1962. break;
  1963. }
  1964. if (rec_count > 1) {
  1965. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1966. processed_type_count,
  1967. processed_type_count + rec_count - 1);
  1968. }
  1969. processed_type_count += rec_count;
  1970. }
  1971. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1972. * module->type_count,
  1973. error_buf, error_buf_size))) {
  1974. return false;
  1975. }
  1976. #endif /* end of WASM_ENABLE_GC == 0 */
  1977. }
  1978. if (p != p_end) {
  1979. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1980. return false;
  1981. }
  1982. LOG_VERBOSE("Load type section success.\n");
  1983. return true;
  1984. fail:
  1985. return false;
  1986. }
  1987. static void
  1988. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  1989. uint32 *max_size)
  1990. {
  1991. uint32 default_max_size;
  1992. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  1993. * ABI unchanged */
  1994. (void)is_table64;
  1995. if (UINT32_MAX / 2 > init_size)
  1996. default_max_size = init_size * 2;
  1997. else
  1998. default_max_size = UINT32_MAX;
  1999. if (default_max_size < WASM_TABLE_MAX_SIZE)
  2000. default_max_size = WASM_TABLE_MAX_SIZE;
  2001. if (max_size_flag) {
  2002. /* module defines the table limitation */
  2003. bh_assert(init_size <= *max_size);
  2004. if (init_size < *max_size) {
  2005. *max_size =
  2006. *max_size < default_max_size ? *max_size : default_max_size;
  2007. }
  2008. }
  2009. else {
  2010. /* partial defined table limitation, gives a default value */
  2011. *max_size = default_max_size;
  2012. }
  2013. }
  2014. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2015. /**
  2016. * Find export item of a module with export info:
  2017. * module name, field name and export kind
  2018. */
  2019. static WASMExport *
  2020. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2021. const char *field_name, uint8 export_kind,
  2022. char *error_buf, uint32 error_buf_size)
  2023. {
  2024. WASMExport *export =
  2025. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2026. export_kind, error_buf, error_buf_size);
  2027. return export;
  2028. }
  2029. #endif
  2030. #if WASM_ENABLE_MULTI_MODULE != 0
  2031. static WASMTable *
  2032. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2033. uint32 init_size, uint32 max_size, char *error_buf,
  2034. uint32 error_buf_size)
  2035. {
  2036. WASMModuleCommon *module_reg;
  2037. WASMTable *table = NULL;
  2038. WASMExport *export = NULL;
  2039. WASMModule *module = NULL;
  2040. module_reg = wasm_runtime_find_module_registered(module_name);
  2041. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2042. LOG_DEBUG("can not find a module named %s for table", module_name);
  2043. set_error_buf(error_buf, error_buf_size, "unknown import");
  2044. return NULL;
  2045. }
  2046. module = (WASMModule *)module_reg;
  2047. export =
  2048. wasm_loader_find_export(module, module_name, table_name,
  2049. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2050. if (!export) {
  2051. return NULL;
  2052. }
  2053. /* resolve table and check the init/max size */
  2054. if (export->index < module->import_table_count) {
  2055. table =
  2056. module->import_tables[export->index].u.table.import_table_linked;
  2057. }
  2058. else {
  2059. table = &(module->tables[export->index - module->import_table_count]);
  2060. }
  2061. if (table->table_type.init_size < init_size
  2062. || table->table_type.max_size > max_size) {
  2063. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2064. module_name, table_name, table->table_type.init_size,
  2065. table->table_type.max_size, init_size, max_size);
  2066. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2067. return NULL;
  2068. }
  2069. return table;
  2070. }
  2071. static WASMMemory *
  2072. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2073. uint32 init_page_count, uint32 max_page_count,
  2074. char *error_buf, uint32 error_buf_size)
  2075. {
  2076. WASMModuleCommon *module_reg;
  2077. WASMMemory *memory = NULL;
  2078. WASMExport *export = NULL;
  2079. WASMModule *module = NULL;
  2080. module_reg = wasm_runtime_find_module_registered(module_name);
  2081. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2082. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2083. set_error_buf(error_buf, error_buf_size, "unknown import");
  2084. return NULL;
  2085. }
  2086. module = (WASMModule *)module_reg;
  2087. export =
  2088. wasm_loader_find_export(module, module_name, memory_name,
  2089. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2090. if (!export) {
  2091. return NULL;
  2092. }
  2093. /* resolve memory and check the init/max page count */
  2094. if (export->index < module->import_memory_count) {
  2095. memory = module->import_memories[export->index]
  2096. .u.memory.import_memory_linked;
  2097. }
  2098. else {
  2099. memory =
  2100. &(module->memories[export->index - module->import_memory_count]);
  2101. }
  2102. if (memory->init_page_count < init_page_count
  2103. || memory->max_page_count > max_page_count) {
  2104. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2105. module_name, memory_name, memory->init_page_count,
  2106. memory->max_page_count, init_page_count, max_page_count);
  2107. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2108. return NULL;
  2109. }
  2110. return memory;
  2111. }
  2112. static WASMGlobal *
  2113. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2114. uint8 type, bool is_mutable, char *error_buf,
  2115. uint32 error_buf_size)
  2116. {
  2117. WASMModuleCommon *module_reg;
  2118. WASMGlobal *global = NULL;
  2119. WASMExport *export = NULL;
  2120. WASMModule *module = NULL;
  2121. module_reg = wasm_runtime_find_module_registered(module_name);
  2122. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2123. LOG_DEBUG("can not find a module named %s for global", module_name);
  2124. set_error_buf(error_buf, error_buf_size, "unknown import");
  2125. return NULL;
  2126. }
  2127. module = (WASMModule *)module_reg;
  2128. export =
  2129. wasm_loader_find_export(module, module_name, global_name,
  2130. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2131. if (!export) {
  2132. return NULL;
  2133. }
  2134. /* resolve and check the global */
  2135. if (export->index < module->import_global_count) {
  2136. global =
  2137. module->import_globals[export->index].u.global.import_global_linked;
  2138. }
  2139. else {
  2140. global =
  2141. &(module->globals[export->index - module->import_global_count]);
  2142. }
  2143. if (global->type.val_type != type
  2144. || global->type.is_mutable != is_mutable) {
  2145. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2146. module_name, global_name, global->type.val_type,
  2147. global->type.is_mutable, type, is_mutable);
  2148. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2149. return NULL;
  2150. }
  2151. return global;
  2152. }
  2153. #if WASM_ENABLE_TAGS != 0
  2154. static WASMTag *
  2155. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2156. const WASMType *expected_tag_type,
  2157. uint32 *linked_tag_index, char *error_buf,
  2158. uint32 error_buf_size)
  2159. {
  2160. WASMModuleCommon *module_reg;
  2161. WASMTag *tag = NULL;
  2162. WASMExport *export = NULL;
  2163. WASMModule *module = NULL;
  2164. module_reg = wasm_runtime_find_module_registered(module_name);
  2165. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2166. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2167. tag_name);
  2168. set_error_buf(error_buf, error_buf_size, "unknown import");
  2169. return NULL;
  2170. }
  2171. module = (WASMModule *)module_reg;
  2172. export =
  2173. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2174. error_buf, error_buf_size);
  2175. if (!export) {
  2176. return NULL;
  2177. }
  2178. /* resolve tag type and tag */
  2179. if (export->index < module->import_tag_count) {
  2180. /* importing an imported tag from the submodule */
  2181. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2182. }
  2183. else {
  2184. /* importing an section tag from the submodule */
  2185. tag = module->tags[export->index - module->import_tag_count];
  2186. }
  2187. /* check function type */
  2188. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2189. module->type_count)) {
  2190. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2191. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2192. return NULL;
  2193. }
  2194. if (linked_tag_index != NULL) {
  2195. *linked_tag_index = export->index;
  2196. }
  2197. return tag;
  2198. }
  2199. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2200. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2201. static bool
  2202. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2203. const WASMModule *parent_module,
  2204. const char *sub_module_name, const char *function_name,
  2205. WASMFunctionImport *function, bool no_resolve,
  2206. char *error_buf, uint32 error_buf_size)
  2207. {
  2208. const uint8 *p = *p_buf, *p_end = buf_end;
  2209. uint32 declare_type_index = 0;
  2210. read_leb_uint32(p, p_end, declare_type_index);
  2211. *p_buf = p;
  2212. if (declare_type_index >= parent_module->type_count) {
  2213. set_error_buf(error_buf, error_buf_size, "unknown type");
  2214. return false;
  2215. }
  2216. #if WASM_ENABLE_GC != 0
  2217. function->type_idx = declare_type_index;
  2218. #endif
  2219. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2220. declare_type_index = wasm_get_smallest_type_idx(
  2221. parent_module->types, parent_module->type_count, declare_type_index);
  2222. #endif
  2223. function->func_type =
  2224. (WASMFuncType *)parent_module->types[declare_type_index];
  2225. function->module_name = (char *)sub_module_name;
  2226. function->field_name = (char *)function_name;
  2227. function->attachment = NULL;
  2228. function->signature = NULL;
  2229. function->call_conv_raw = false;
  2230. /* lookup registered native symbols first */
  2231. if (!no_resolve) {
  2232. wasm_resolve_import_func(parent_module, function);
  2233. }
  2234. return true;
  2235. fail:
  2236. return false;
  2237. }
  2238. static bool
  2239. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2240. uint32 error_buf_size)
  2241. {
  2242. if (max_size < init_size) {
  2243. set_error_buf(error_buf, error_buf_size,
  2244. "size minimum must not be greater than maximum");
  2245. return false;
  2246. }
  2247. return true;
  2248. }
  2249. static bool
  2250. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2251. WASMModule *parent_module, const char *sub_module_name,
  2252. const char *table_name, WASMTableImport *table,
  2253. char *error_buf, uint32 error_buf_size)
  2254. {
  2255. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2256. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2257. declare_max_size = 0;
  2258. #if WASM_ENABLE_MULTI_MODULE != 0
  2259. WASMModule *sub_module = NULL;
  2260. WASMTable *linked_table = NULL;
  2261. #endif
  2262. #if WASM_ENABLE_GC != 0
  2263. WASMRefType ref_type;
  2264. bool need_ref_type_map;
  2265. #endif
  2266. bool is_table64 = false;
  2267. #if WASM_ENABLE_GC == 0
  2268. CHECK_BUF(p, p_end, 1);
  2269. /* 0x70 or 0x6F */
  2270. declare_elem_type = read_uint8(p);
  2271. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2272. #if WASM_ENABLE_REF_TYPES != 0
  2273. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2274. #endif
  2275. ) {
  2276. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2277. return false;
  2278. }
  2279. #else /* else of WASM_ENABLE_GC == 0 */
  2280. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2281. &need_ref_type_map, &ref_type, false, error_buf,
  2282. error_buf_size)) {
  2283. return false;
  2284. }
  2285. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2286. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2287. return false;
  2288. }
  2289. declare_elem_type = ref_type.ref_type;
  2290. if (need_ref_type_map) {
  2291. if (!(table->table_type.elem_ref_type =
  2292. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2293. error_buf, error_buf_size))) {
  2294. return false;
  2295. }
  2296. }
  2297. #if TRACE_WASM_LOADER != 0
  2298. os_printf("import table type: ");
  2299. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2300. os_printf("\n");
  2301. #endif
  2302. #endif /* end of WASM_ENABLE_GC == 0 */
  2303. p_org = p;
  2304. read_leb_uint32(p, p_end, table_flag);
  2305. is_table64 = table_flag & TABLE64_FLAG;
  2306. if (p - p_org > 1) {
  2307. LOG_VERBOSE("integer representation too long(import table)");
  2308. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2309. return false;
  2310. }
  2311. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2312. return false;
  2313. }
  2314. read_leb_uint32(p, p_end, declare_init_size);
  2315. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2316. read_leb_uint32(p, p_end, declare_max_size);
  2317. if (!check_table_max_size(declare_init_size, declare_max_size,
  2318. error_buf, error_buf_size))
  2319. return false;
  2320. }
  2321. adjust_table_max_size(is_table64, declare_init_size,
  2322. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2323. *p_buf = p;
  2324. #if WASM_ENABLE_MULTI_MODULE != 0
  2325. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2326. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2327. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2328. error_buf_size);
  2329. if (sub_module) {
  2330. linked_table = wasm_loader_resolve_table(
  2331. sub_module_name, table_name, declare_init_size,
  2332. declare_max_size, error_buf, error_buf_size);
  2333. if (linked_table) {
  2334. /* reset with linked table limit */
  2335. declare_elem_type = linked_table->table_type.elem_type;
  2336. declare_init_size = linked_table->table_type.init_size;
  2337. declare_max_size = linked_table->table_type.max_size;
  2338. table_flag = linked_table->table_type.flags;
  2339. table->import_table_linked = linked_table;
  2340. table->import_module = sub_module;
  2341. }
  2342. }
  2343. }
  2344. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2345. /* (table (export "table") 10 20 funcref) */
  2346. /* (table (export "table64") 10 20 funcref) */
  2347. /* we need this section working in wamrc */
  2348. if (!strcmp("spectest", sub_module_name)) {
  2349. const uint32 spectest_table_init_size = 10;
  2350. const uint32 spectest_table_max_size = 20;
  2351. if (strcmp("table", table_name)
  2352. #if WASM_ENABLE_MEMORY64 != 0
  2353. && strcmp("table64", table_name)
  2354. #endif
  2355. ) {
  2356. set_error_buf(error_buf, error_buf_size,
  2357. "incompatible import type or unknown import");
  2358. return false;
  2359. }
  2360. if (declare_init_size > spectest_table_init_size
  2361. || declare_max_size < spectest_table_max_size) {
  2362. set_error_buf(error_buf, error_buf_size,
  2363. "incompatible import type");
  2364. return false;
  2365. }
  2366. declare_init_size = spectest_table_init_size;
  2367. declare_max_size = spectest_table_max_size;
  2368. }
  2369. /* now we believe all declaration are ok */
  2370. table->table_type.elem_type = declare_elem_type;
  2371. table->table_type.init_size = declare_init_size;
  2372. table->table_type.flags = table_flag;
  2373. table->table_type.max_size = declare_max_size;
  2374. #if WASM_ENABLE_WAMR_COMPILER != 0
  2375. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2376. parent_module->is_ref_types_used = true;
  2377. #endif
  2378. (void)parent_module;
  2379. return true;
  2380. fail:
  2381. return false;
  2382. }
  2383. static bool
  2384. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2385. uint32 error_buf_size)
  2386. {
  2387. uint32 default_max_size =
  2388. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2389. if (!is_memory64 && init_size > default_max_size) {
  2390. set_error_buf(error_buf, error_buf_size,
  2391. "memory size must be at most 65536 pages (4GiB)");
  2392. return false;
  2393. }
  2394. #if WASM_ENABLE_MEMORY64 != 0
  2395. else if (is_memory64 && init_size > default_max_size) {
  2396. set_error_buf(
  2397. error_buf, error_buf_size,
  2398. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2399. return false;
  2400. }
  2401. #endif
  2402. return true;
  2403. }
  2404. static bool
  2405. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2406. char *error_buf, uint32 error_buf_size)
  2407. {
  2408. uint32 default_max_size =
  2409. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2410. if (max_size < init_size) {
  2411. set_error_buf(error_buf, error_buf_size,
  2412. "size minimum must not be greater than maximum");
  2413. return false;
  2414. }
  2415. if (!is_memory64 && max_size > default_max_size) {
  2416. set_error_buf(error_buf, error_buf_size,
  2417. "memory size must be at most 65536 pages (4GiB)");
  2418. return false;
  2419. }
  2420. #if WASM_ENABLE_MEMORY64 != 0
  2421. else if (is_memory64 && max_size > default_max_size) {
  2422. set_error_buf(
  2423. error_buf, error_buf_size,
  2424. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2425. return false;
  2426. }
  2427. #endif
  2428. return true;
  2429. }
  2430. static bool
  2431. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2432. WASMModule *parent_module, const char *sub_module_name,
  2433. const char *memory_name, WASMMemoryImport *memory,
  2434. char *error_buf, uint32 error_buf_size)
  2435. {
  2436. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2437. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2438. uint32 pool_size = wasm_runtime_memory_pool_size();
  2439. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2440. / DEFAULT_NUM_BYTES_PER_PAGE;
  2441. #else
  2442. uint32 max_page_count;
  2443. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2444. uint32 mem_flag = 0;
  2445. bool is_memory64 = false;
  2446. uint32 declare_init_page_count = 0;
  2447. uint32 declare_max_page_count = 0;
  2448. #if WASM_ENABLE_MULTI_MODULE != 0
  2449. WASMModule *sub_module = NULL;
  2450. WASMMemory *linked_memory = NULL;
  2451. #endif
  2452. p_org = p;
  2453. read_leb_uint32(p, p_end, mem_flag);
  2454. is_memory64 = mem_flag & MEMORY64_FLAG;
  2455. if (p - p_org > 1) {
  2456. LOG_VERBOSE("integer representation too long(import memory)");
  2457. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2458. return false;
  2459. }
  2460. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2461. return false;
  2462. }
  2463. read_leb_uint32(p, p_end, declare_init_page_count);
  2464. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2465. error_buf_size)) {
  2466. return false;
  2467. }
  2468. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2469. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2470. #endif
  2471. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2472. read_leb_uint32(p, p_end, declare_max_page_count);
  2473. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2474. declare_max_page_count, error_buf,
  2475. error_buf_size)) {
  2476. return false;
  2477. }
  2478. if (declare_max_page_count > max_page_count) {
  2479. declare_max_page_count = max_page_count;
  2480. }
  2481. }
  2482. else {
  2483. /* Limit the maximum memory size to max_page_count */
  2484. declare_max_page_count = max_page_count;
  2485. }
  2486. #if WASM_ENABLE_MULTI_MODULE != 0
  2487. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2488. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2489. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2490. error_buf_size);
  2491. if (sub_module) {
  2492. linked_memory = wasm_loader_resolve_memory(
  2493. sub_module_name, memory_name, declare_init_page_count,
  2494. declare_max_page_count, error_buf, error_buf_size);
  2495. if (linked_memory) {
  2496. /**
  2497. * reset with linked memory limit
  2498. */
  2499. memory->import_module = sub_module;
  2500. memory->import_memory_linked = linked_memory;
  2501. declare_init_page_count = linked_memory->init_page_count;
  2502. declare_max_page_count = linked_memory->max_page_count;
  2503. }
  2504. }
  2505. }
  2506. #endif
  2507. /* (memory (export "memory") 1 2) */
  2508. if (!strcmp("spectest", sub_module_name)) {
  2509. uint32 spectest_memory_init_page = 1;
  2510. uint32 spectest_memory_max_page = 2;
  2511. if (strcmp("memory", memory_name)) {
  2512. set_error_buf(error_buf, error_buf_size,
  2513. "incompatible import type or unknown import");
  2514. return false;
  2515. }
  2516. if (declare_init_page_count > spectest_memory_init_page
  2517. || declare_max_page_count < spectest_memory_max_page) {
  2518. set_error_buf(error_buf, error_buf_size,
  2519. "incompatible import type");
  2520. return false;
  2521. }
  2522. declare_init_page_count = spectest_memory_init_page;
  2523. declare_max_page_count = spectest_memory_max_page;
  2524. }
  2525. #if WASM_ENABLE_WASI_TEST != 0
  2526. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2527. else if (!strcmp("foo", sub_module_name)) {
  2528. uint32 spectest_memory_init_page = 1;
  2529. uint32 spectest_memory_max_page = 1;
  2530. if (strcmp("bar", memory_name)) {
  2531. set_error_buf(error_buf, error_buf_size,
  2532. "incompatible import type or unknown import");
  2533. return false;
  2534. }
  2535. if (declare_init_page_count > spectest_memory_init_page
  2536. || declare_max_page_count < spectest_memory_max_page) {
  2537. set_error_buf(error_buf, error_buf_size,
  2538. "incompatible import type");
  2539. return false;
  2540. }
  2541. declare_init_page_count = spectest_memory_init_page;
  2542. declare_max_page_count = spectest_memory_max_page;
  2543. }
  2544. #endif
  2545. /* now we believe all declaration are ok */
  2546. memory->mem_type.flags = mem_flag;
  2547. memory->mem_type.init_page_count = declare_init_page_count;
  2548. memory->mem_type.max_page_count = declare_max_page_count;
  2549. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2550. *p_buf = p;
  2551. (void)parent_module;
  2552. return true;
  2553. fail:
  2554. return false;
  2555. }
  2556. #if WASM_ENABLE_TAGS != 0
  2557. static bool
  2558. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2559. const WASMModule *parent_module, /* this module ! */
  2560. const char *sub_module_name, const char *tag_name,
  2561. WASMTagImport *tag, /* structure to fill */
  2562. char *error_buf, uint32 error_buf_size)
  2563. {
  2564. /* attribute and type of the import statement */
  2565. uint8 declare_tag_attribute;
  2566. uint32 declare_type_index;
  2567. const uint8 *p = *p_buf, *p_end = buf_end;
  2568. #if WASM_ENABLE_MULTI_MODULE != 0
  2569. WASMModule *sub_module = NULL;
  2570. #endif
  2571. /* get the one byte attribute */
  2572. CHECK_BUF(p, p_end, 1);
  2573. declare_tag_attribute = read_uint8(p);
  2574. if (declare_tag_attribute != 0) {
  2575. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2576. goto fail;
  2577. }
  2578. /* get type */
  2579. read_leb_uint32(p, p_end, declare_type_index);
  2580. /* compare against module->types */
  2581. if (declare_type_index >= parent_module->type_count) {
  2582. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2583. goto fail;
  2584. }
  2585. WASMFuncType *declare_tag_type =
  2586. (WASMFuncType *)parent_module->types[declare_type_index];
  2587. /* check, that the type of the declared tag returns void */
  2588. if (declare_tag_type->result_count != 0) {
  2589. set_error_buf(error_buf, error_buf_size,
  2590. "tag type signature does not return void");
  2591. goto fail;
  2592. }
  2593. #if WASM_ENABLE_MULTI_MODULE != 0
  2594. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2595. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2596. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2597. error_buf_size);
  2598. if (sub_module) {
  2599. /* wasm_loader_resolve_tag checks, that the imported tag
  2600. * and the declared tag have the same type
  2601. */
  2602. uint32 linked_tag_index = 0;
  2603. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2604. sub_module_name, tag_name, declare_tag_type,
  2605. &linked_tag_index /* out */, error_buf, error_buf_size);
  2606. if (linked_tag) {
  2607. tag->import_module = sub_module;
  2608. tag->import_tag_linked = linked_tag;
  2609. tag->import_tag_index_linked = linked_tag_index;
  2610. }
  2611. }
  2612. }
  2613. #endif
  2614. /* store to module tag declarations */
  2615. tag->attribute = declare_tag_attribute;
  2616. tag->type = declare_type_index;
  2617. tag->module_name = (char *)sub_module_name;
  2618. tag->field_name = (char *)tag_name;
  2619. tag->tag_type = declare_tag_type;
  2620. *p_buf = p;
  2621. (void)parent_module;
  2622. LOG_VERBOSE("Load tag import success\n");
  2623. return true;
  2624. fail:
  2625. return false;
  2626. }
  2627. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2628. static bool
  2629. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2630. WASMModule *parent_module, char *sub_module_name,
  2631. char *global_name, WASMGlobalImport *global, char *error_buf,
  2632. uint32 error_buf_size)
  2633. {
  2634. const uint8 *p = *p_buf, *p_end = buf_end;
  2635. uint8 declare_type = 0;
  2636. uint8 declare_mutable = 0;
  2637. #if WASM_ENABLE_MULTI_MODULE != 0
  2638. WASMModule *sub_module = NULL;
  2639. WASMGlobal *linked_global = NULL;
  2640. #endif
  2641. #if WASM_ENABLE_GC != 0
  2642. WASMRefType ref_type;
  2643. bool need_ref_type_map;
  2644. #endif
  2645. bool ret = false;
  2646. #if WASM_ENABLE_GC == 0
  2647. CHECK_BUF(p, p_end, 2);
  2648. /* global type */
  2649. declare_type = read_uint8(p);
  2650. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2651. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2652. return false;
  2653. }
  2654. declare_mutable = read_uint8(p);
  2655. #else
  2656. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2657. &need_ref_type_map, &ref_type, false, error_buf,
  2658. error_buf_size)) {
  2659. return false;
  2660. }
  2661. declare_type = ref_type.ref_type;
  2662. if (need_ref_type_map) {
  2663. if (!(global->ref_type =
  2664. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2665. error_buf, error_buf_size))) {
  2666. return false;
  2667. }
  2668. }
  2669. #if TRACE_WASM_LOADER != 0
  2670. os_printf("import global type: ");
  2671. wasm_dump_value_type(declare_type, global->ref_type);
  2672. os_printf("\n");
  2673. #endif
  2674. CHECK_BUF(p, p_end, 1);
  2675. declare_mutable = read_uint8(p);
  2676. #endif /* end of WASM_ENABLE_GC == 0 */
  2677. *p_buf = p;
  2678. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2679. return false;
  2680. }
  2681. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2682. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2683. global);
  2684. if (ret) {
  2685. if (global->type.val_type != declare_type
  2686. || global->type.is_mutable != declare_mutable) {
  2687. set_error_buf(error_buf, error_buf_size,
  2688. "incompatible import type");
  2689. return false;
  2690. }
  2691. global->is_linked = true;
  2692. }
  2693. #endif
  2694. #if WASM_ENABLE_MULTI_MODULE != 0
  2695. if (!global->is_linked
  2696. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2697. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2698. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2699. error_buf_size);
  2700. if (sub_module) {
  2701. /* check sub modules */
  2702. linked_global = wasm_loader_resolve_global(
  2703. sub_module_name, global_name, declare_type, declare_mutable,
  2704. error_buf, error_buf_size);
  2705. if (linked_global) {
  2706. global->import_module = sub_module;
  2707. global->import_global_linked = linked_global;
  2708. global->is_linked = true;
  2709. }
  2710. }
  2711. }
  2712. #endif
  2713. global->module_name = sub_module_name;
  2714. global->field_name = global_name;
  2715. global->type.val_type = declare_type;
  2716. global->type.is_mutable = (declare_mutable == 1);
  2717. #if WASM_ENABLE_WAMR_COMPILER != 0
  2718. if (global->type.val_type == VALUE_TYPE_V128)
  2719. parent_module->is_simd_used = true;
  2720. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2721. parent_module->is_ref_types_used = true;
  2722. #endif
  2723. (void)parent_module;
  2724. (void)ret;
  2725. return true;
  2726. fail:
  2727. return false;
  2728. }
  2729. static bool
  2730. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2731. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2732. {
  2733. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2734. #if WASM_ENABLE_GC != 0
  2735. WASMRefType ref_type;
  2736. bool need_ref_type_map;
  2737. #endif
  2738. bool is_table64 = false;
  2739. #if WASM_ENABLE_GC == 0
  2740. CHECK_BUF(p, p_end, 1);
  2741. /* 0x70 or 0x6F */
  2742. table->table_type.elem_type = read_uint8(p);
  2743. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2744. #if WASM_ENABLE_REF_TYPES != 0
  2745. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2746. #endif
  2747. ) {
  2748. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2749. return false;
  2750. }
  2751. #else /* else of WASM_ENABLE_GC == 0 */
  2752. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2753. &need_ref_type_map, &ref_type, false, error_buf,
  2754. error_buf_size)) {
  2755. return false;
  2756. }
  2757. table->table_type.elem_type = ref_type.ref_type;
  2758. if (need_ref_type_map) {
  2759. if (!(table->table_type.elem_ref_type =
  2760. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2761. error_buf_size))) {
  2762. return false;
  2763. }
  2764. }
  2765. #if TRACE_WASM_LOADER != 0
  2766. os_printf("table type: ");
  2767. wasm_dump_value_type(table->table_type.elem_type,
  2768. table->table_type.elem_ref_type);
  2769. os_printf("\n");
  2770. #endif
  2771. #endif /* end of WASM_ENABLE_GC == 0 */
  2772. p_org = p;
  2773. read_leb_uint32(p, p_end, table->table_type.flags);
  2774. is_table64 = table->table_type.flags & TABLE64_FLAG;
  2775. if (p - p_org > 1) {
  2776. LOG_VERBOSE("integer representation too long(table)");
  2777. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2778. return false;
  2779. }
  2780. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  2781. error_buf_size, false)) {
  2782. return false;
  2783. }
  2784. read_leb_uint32(p, p_end, table->table_type.init_size);
  2785. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  2786. read_leb_uint32(p, p_end, table->table_type.max_size);
  2787. if (!check_table_max_size(table->table_type.init_size,
  2788. table->table_type.max_size, error_buf,
  2789. error_buf_size))
  2790. return false;
  2791. }
  2792. adjust_table_max_size(is_table64, table->table_type.init_size,
  2793. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  2794. &table->table_type.max_size);
  2795. #if WASM_ENABLE_WAMR_COMPILER != 0
  2796. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2797. module->is_ref_types_used = true;
  2798. #endif
  2799. *p_buf = p;
  2800. return true;
  2801. fail:
  2802. return false;
  2803. }
  2804. static bool
  2805. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2806. char *error_buf, uint32 error_buf_size)
  2807. {
  2808. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2809. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2810. uint32 pool_size = wasm_runtime_memory_pool_size();
  2811. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2812. / DEFAULT_NUM_BYTES_PER_PAGE;
  2813. #else
  2814. uint32 max_page_count;
  2815. #endif
  2816. bool is_memory64 = false;
  2817. p_org = p;
  2818. read_leb_uint32(p, p_end, memory->flags);
  2819. is_memory64 = memory->flags & MEMORY64_FLAG;
  2820. if (p - p_org > 1) {
  2821. LOG_VERBOSE("integer representation too long(memory)");
  2822. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2823. return false;
  2824. }
  2825. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2826. false)) {
  2827. return false;
  2828. }
  2829. read_leb_uint32(p, p_end, memory->init_page_count);
  2830. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2831. error_buf_size))
  2832. return false;
  2833. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2834. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2835. #endif
  2836. if (memory->flags & 1) {
  2837. read_leb_uint32(p, p_end, memory->max_page_count);
  2838. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2839. memory->max_page_count, error_buf,
  2840. error_buf_size))
  2841. return false;
  2842. if (memory->max_page_count > max_page_count)
  2843. memory->max_page_count = max_page_count;
  2844. }
  2845. else {
  2846. /* Limit the maximum memory size to max_page_count */
  2847. memory->max_page_count = max_page_count;
  2848. }
  2849. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2850. *p_buf = p;
  2851. return true;
  2852. fail:
  2853. return false;
  2854. }
  2855. static int
  2856. cmp_export_name(const void *a, const void *b)
  2857. {
  2858. return strcmp(*(char **)a, *(char **)b);
  2859. }
  2860. static bool
  2861. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2862. bool is_load_from_file_buf, bool no_resolve,
  2863. char *error_buf, uint32 error_buf_size)
  2864. {
  2865. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2866. uint32 import_count, name_len, type_index, i, u32, flags;
  2867. uint64 total_size;
  2868. WASMImport *import;
  2869. WASMImport *import_functions = NULL, *import_tables = NULL;
  2870. WASMImport *import_memories = NULL, *import_globals = NULL;
  2871. #if WASM_ENABLE_TAGS != 0
  2872. WASMImport *import_tags = NULL;
  2873. #endif
  2874. char *sub_module_name, *field_name;
  2875. uint8 u8, kind, global_type;
  2876. read_leb_uint32(p, p_end, import_count);
  2877. if (import_count) {
  2878. module->import_count = import_count;
  2879. total_size = sizeof(WASMImport) * (uint64)import_count;
  2880. if (!(module->imports =
  2881. loader_malloc(total_size, error_buf, error_buf_size))) {
  2882. return false;
  2883. }
  2884. p_old = p;
  2885. /* Scan firstly to get import count of each type */
  2886. for (i = 0; i < import_count; i++) {
  2887. /* module name */
  2888. read_leb_uint32(p, p_end, name_len);
  2889. CHECK_BUF(p, p_end, name_len);
  2890. p += name_len;
  2891. /* field name */
  2892. read_leb_uint32(p, p_end, name_len);
  2893. CHECK_BUF(p, p_end, name_len);
  2894. p += name_len;
  2895. CHECK_BUF(p, p_end, 1);
  2896. /* 0x00/0x01/0x02/0x03/0x04 */
  2897. kind = read_uint8(p);
  2898. switch (kind) {
  2899. case IMPORT_KIND_FUNC: /* import function */
  2900. read_leb_uint32(p, p_end, type_index);
  2901. module->import_function_count++;
  2902. break;
  2903. case IMPORT_KIND_TABLE: /* import table */
  2904. CHECK_BUF(p, p_end, 1);
  2905. /* 0x70 */
  2906. u8 = read_uint8(p);
  2907. read_leb_uint32(p, p_end, flags);
  2908. read_leb_uint32(p, p_end, u32);
  2909. if (flags & 1)
  2910. read_leb_uint32(p, p_end, u32);
  2911. module->import_table_count++;
  2912. if (module->import_table_count > 1) {
  2913. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2914. set_error_buf(error_buf, error_buf_size,
  2915. "multiple tables");
  2916. return false;
  2917. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2918. module->is_ref_types_used = true;
  2919. #endif
  2920. }
  2921. break;
  2922. case IMPORT_KIND_MEMORY: /* import memory */
  2923. read_leb_uint32(p, p_end, flags);
  2924. read_leb_uint32(p, p_end, u32);
  2925. if (flags & 1)
  2926. read_leb_uint32(p, p_end, u32);
  2927. module->import_memory_count++;
  2928. #if WASM_ENABLE_MULTI_MEMORY == 0
  2929. if (module->import_memory_count > 1) {
  2930. set_error_buf(error_buf, error_buf_size,
  2931. "multiple memories");
  2932. return false;
  2933. }
  2934. #endif
  2935. break;
  2936. #if WASM_ENABLE_TAGS != 0
  2937. case IMPORT_KIND_TAG: /* import tags */
  2938. /* it only counts the number of tags to import */
  2939. module->import_tag_count++;
  2940. CHECK_BUF(p, p_end, 1);
  2941. u8 = read_uint8(p);
  2942. read_leb_uint32(p, p_end, type_index);
  2943. break;
  2944. #endif
  2945. case IMPORT_KIND_GLOBAL: /* import global */
  2946. #if WASM_ENABLE_GC != 0
  2947. /* valtype */
  2948. CHECK_BUF(p, p_end, 1);
  2949. global_type = read_uint8(p);
  2950. if (wasm_is_type_multi_byte_type(global_type)) {
  2951. int32 heap_type;
  2952. read_leb_int32(p, p_end, heap_type);
  2953. (void)heap_type;
  2954. }
  2955. /* mutability */
  2956. CHECK_BUF(p, p_end, 1);
  2957. p += 1;
  2958. #else
  2959. CHECK_BUF(p, p_end, 2);
  2960. p += 2;
  2961. #endif
  2962. (void)global_type;
  2963. module->import_global_count++;
  2964. break;
  2965. default:
  2966. set_error_buf(error_buf, error_buf_size,
  2967. "invalid import kind");
  2968. return false;
  2969. }
  2970. }
  2971. if (module->import_function_count)
  2972. import_functions = module->import_functions = module->imports;
  2973. if (module->import_table_count)
  2974. import_tables = module->import_tables =
  2975. module->imports + module->import_function_count;
  2976. if (module->import_memory_count)
  2977. import_memories = module->import_memories =
  2978. module->imports + module->import_function_count
  2979. + module->import_table_count;
  2980. #if WASM_ENABLE_TAGS != 0
  2981. if (module->import_tag_count)
  2982. import_tags = module->import_tags =
  2983. module->imports + module->import_function_count
  2984. + module->import_table_count + module->import_memory_count;
  2985. if (module->import_global_count)
  2986. import_globals = module->import_globals =
  2987. module->imports + module->import_function_count
  2988. + module->import_table_count + module->import_memory_count
  2989. + module->import_tag_count;
  2990. #else
  2991. if (module->import_global_count)
  2992. import_globals = module->import_globals =
  2993. module->imports + module->import_function_count
  2994. + module->import_table_count + module->import_memory_count;
  2995. #endif
  2996. p = p_old;
  2997. /* Scan again to resolve the data */
  2998. for (i = 0; i < import_count; i++) {
  2999. /* load module name */
  3000. read_leb_uint32(p, p_end, name_len);
  3001. CHECK_BUF(p, p_end, name_len);
  3002. if (!(sub_module_name = wasm_const_str_list_insert(
  3003. p, name_len, module, is_load_from_file_buf, error_buf,
  3004. error_buf_size))) {
  3005. return false;
  3006. }
  3007. p += name_len;
  3008. /* load field name */
  3009. read_leb_uint32(p, p_end, name_len);
  3010. CHECK_BUF(p, p_end, name_len);
  3011. if (!(field_name = wasm_const_str_list_insert(
  3012. p, name_len, module, is_load_from_file_buf, error_buf,
  3013. error_buf_size))) {
  3014. return false;
  3015. }
  3016. p += name_len;
  3017. CHECK_BUF(p, p_end, 1);
  3018. /* 0x00/0x01/0x02/0x03/0x4 */
  3019. kind = read_uint8(p);
  3020. switch (kind) {
  3021. case IMPORT_KIND_FUNC: /* import function */
  3022. bh_assert(import_functions);
  3023. import = import_functions++;
  3024. if (!load_function_import(&p, p_end, module,
  3025. sub_module_name, field_name,
  3026. &import->u.function, no_resolve,
  3027. error_buf, error_buf_size)) {
  3028. return false;
  3029. }
  3030. break;
  3031. case IMPORT_KIND_TABLE: /* import table */
  3032. bh_assert(import_tables);
  3033. import = import_tables++;
  3034. if (!load_table_import(&p, p_end, module, sub_module_name,
  3035. field_name, &import->u.table,
  3036. error_buf, error_buf_size)) {
  3037. LOG_DEBUG("can not import such a table (%s,%s)",
  3038. sub_module_name, field_name);
  3039. return false;
  3040. }
  3041. break;
  3042. case IMPORT_KIND_MEMORY: /* import memory */
  3043. bh_assert(import_memories);
  3044. import = import_memories++;
  3045. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3046. field_name, &import->u.memory,
  3047. error_buf, error_buf_size)) {
  3048. return false;
  3049. }
  3050. break;
  3051. #if WASM_ENABLE_TAGS != 0
  3052. case IMPORT_KIND_TAG:
  3053. bh_assert(import_tags);
  3054. import = import_tags++;
  3055. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3056. field_name, &import->u.tag, error_buf,
  3057. error_buf_size)) {
  3058. return false;
  3059. }
  3060. break;
  3061. #endif
  3062. case IMPORT_KIND_GLOBAL: /* import global */
  3063. bh_assert(import_globals);
  3064. import = import_globals++;
  3065. if (!load_global_import(&p, p_end, module, sub_module_name,
  3066. field_name, &import->u.global,
  3067. error_buf, error_buf_size)) {
  3068. return false;
  3069. }
  3070. break;
  3071. default:
  3072. set_error_buf(error_buf, error_buf_size,
  3073. "invalid import kind");
  3074. return false;
  3075. }
  3076. import->kind = kind;
  3077. import->u.names.module_name = sub_module_name;
  3078. import->u.names.field_name = field_name;
  3079. }
  3080. #if WASM_ENABLE_LIBC_WASI != 0
  3081. import = module->import_functions;
  3082. for (i = 0; i < module->import_function_count; i++, import++) {
  3083. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3084. || !strcmp(import->u.names.module_name,
  3085. "wasi_snapshot_preview1")) {
  3086. module->import_wasi_api = true;
  3087. break;
  3088. }
  3089. }
  3090. #endif
  3091. }
  3092. if (p != p_end) {
  3093. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3094. return false;
  3095. }
  3096. LOG_VERBOSE("Load import section success.\n");
  3097. (void)u8;
  3098. (void)u32;
  3099. (void)type_index;
  3100. return true;
  3101. fail:
  3102. return false;
  3103. }
  3104. static bool
  3105. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3106. uint32 error_buf_size)
  3107. {
  3108. WASMFuncType *param_type = func->func_type;
  3109. uint32 param_count = param_type->param_count;
  3110. uint8 *param_types = param_type->types;
  3111. uint32 local_count = func->local_count;
  3112. uint8 *local_types = func->local_types;
  3113. uint32 i, local_offset = 0;
  3114. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3115. /*
  3116. * Only allocate memory when total_size is not 0,
  3117. * or the return value of malloc(0) might be NULL on some platforms,
  3118. * which causes wasm loader return false.
  3119. */
  3120. if (total_size > 0
  3121. && !(func->local_offsets =
  3122. loader_malloc(total_size, error_buf, error_buf_size))) {
  3123. return false;
  3124. }
  3125. for (i = 0; i < param_count; i++) {
  3126. func->local_offsets[i] = (uint16)local_offset;
  3127. local_offset += wasm_value_type_cell_num(param_types[i]);
  3128. }
  3129. for (i = 0; i < local_count; i++) {
  3130. func->local_offsets[param_count + i] = (uint16)local_offset;
  3131. local_offset += wasm_value_type_cell_num(local_types[i]);
  3132. }
  3133. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3134. return true;
  3135. }
  3136. static bool
  3137. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3138. const uint8 *buf_code, const uint8 *buf_code_end,
  3139. WASMModule *module, char *error_buf,
  3140. uint32 error_buf_size)
  3141. {
  3142. const uint8 *p = buf, *p_end = buf_end;
  3143. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3144. uint32 func_count;
  3145. uint64 total_size;
  3146. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3147. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3148. uint8 type;
  3149. WASMFunction *func;
  3150. #if WASM_ENABLE_GC != 0
  3151. bool need_ref_type_map;
  3152. WASMRefType ref_type;
  3153. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3154. #endif
  3155. read_leb_uint32(p, p_end, func_count);
  3156. if (buf_code)
  3157. read_leb_uint32(p_code, buf_code_end, code_count);
  3158. if (func_count != code_count) {
  3159. set_error_buf(error_buf, error_buf_size,
  3160. "function and code section have inconsistent lengths or "
  3161. "unexpected end");
  3162. return false;
  3163. }
  3164. if (is_indices_overflow(module->import_function_count, func_count,
  3165. error_buf, error_buf_size))
  3166. return false;
  3167. if (func_count) {
  3168. module->function_count = func_count;
  3169. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3170. if (!(module->functions =
  3171. loader_malloc(total_size, error_buf, error_buf_size))) {
  3172. return false;
  3173. }
  3174. for (i = 0; i < func_count; i++) {
  3175. /* Resolve function type */
  3176. read_leb_uint32(p, p_end, type_index);
  3177. if (type_index >= module->type_count) {
  3178. set_error_buf(error_buf, error_buf_size, "unknown type");
  3179. return false;
  3180. }
  3181. #if WASM_ENABLE_GC != 0
  3182. type_index_org = type_index;
  3183. #endif
  3184. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3185. && WASM_ENABLE_GC == 0
  3186. type_index = wasm_get_smallest_type_idx(
  3187. module->types, module->type_count, type_index);
  3188. #endif
  3189. read_leb_uint32(p_code, buf_code_end, code_size);
  3190. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3191. set_error_buf(error_buf, error_buf_size,
  3192. "invalid function code size");
  3193. return false;
  3194. }
  3195. /* Resolve local set count */
  3196. p_code_end = p_code + code_size;
  3197. local_count = 0;
  3198. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3199. p_code_save = p_code;
  3200. #if WASM_ENABLE_GC != 0
  3201. ref_type_map_count = 0;
  3202. #endif
  3203. /* Calculate total local count */
  3204. for (j = 0; j < local_set_count; j++) {
  3205. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3206. if (sub_local_count > UINT32_MAX - local_count) {
  3207. set_error_buf(error_buf, error_buf_size, "too many locals");
  3208. return false;
  3209. }
  3210. #if WASM_ENABLE_GC == 0
  3211. CHECK_BUF(p_code, buf_code_end, 1);
  3212. /* 0x7F/0x7E/0x7D/0x7C */
  3213. type = read_uint8(p_code);
  3214. local_count += sub_local_count;
  3215. #if WASM_ENABLE_WAMR_COMPILER != 0
  3216. /* If any value's type is v128, mark the module as SIMD used */
  3217. if (type == VALUE_TYPE_V128)
  3218. module->is_simd_used = true;
  3219. #endif
  3220. #else
  3221. if (!resolve_value_type(&p_code, buf_code_end, module,
  3222. module->type_count, &need_ref_type_map,
  3223. &ref_type, false, error_buf,
  3224. error_buf_size)) {
  3225. return false;
  3226. }
  3227. local_count += sub_local_count;
  3228. if (need_ref_type_map)
  3229. ref_type_map_count += sub_local_count;
  3230. #endif
  3231. }
  3232. /* Code size in code entry can't be smaller than size of vec(locals)
  3233. * + expr(at least 1 for opcode end). And expressions are encoded by
  3234. * their instruction sequence terminated with an explicit 0x0B
  3235. * opcode for end. */
  3236. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3237. set_error_buf(
  3238. error_buf, error_buf_size,
  3239. "section size mismatch: function body END opcode expected");
  3240. return false;
  3241. }
  3242. /* Alloc memory, layout: function structure + local types */
  3243. code_size = (uint32)(p_code_end - p_code);
  3244. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3245. if (!(func = module->functions[i] =
  3246. loader_malloc(total_size, error_buf, error_buf_size))) {
  3247. return false;
  3248. }
  3249. #if WASM_ENABLE_GC != 0
  3250. if (ref_type_map_count > 0) {
  3251. total_size =
  3252. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3253. if (!(func->local_ref_type_maps = loader_malloc(
  3254. total_size, error_buf, error_buf_size))) {
  3255. return false;
  3256. }
  3257. func->local_ref_type_map_count = ref_type_map_count;
  3258. }
  3259. #endif
  3260. /* Set function type, local count, code size and code body */
  3261. func->func_type = (WASMFuncType *)module->types[type_index];
  3262. func->local_count = local_count;
  3263. if (local_count > 0)
  3264. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3265. func->code_size = code_size;
  3266. /*
  3267. * we shall make a copy of code body [p_code, p_code + code_size]
  3268. * when we are worrying about inappropriate releasing behaviour.
  3269. * all code bodies are actually in a buffer which user allocates in
  3270. * his embedding environment and we don't have power on them.
  3271. * it will be like:
  3272. * code_body_cp = malloc(code_size);
  3273. * memcpy(code_body_cp, p_code, code_size);
  3274. * func->code = code_body_cp;
  3275. */
  3276. func->code = (uint8 *)p_code;
  3277. #if WASM_ENABLE_GC != 0
  3278. func->type_idx = type_index_org;
  3279. #endif
  3280. #if WASM_ENABLE_GC != 0
  3281. t = 0;
  3282. #endif
  3283. /* Load each local type */
  3284. p_code = p_code_save;
  3285. local_type_index = 0;
  3286. for (j = 0; j < local_set_count; j++) {
  3287. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3288. /* Note: sub_local_count is allowed to be 0 */
  3289. if (local_type_index > UINT32_MAX - sub_local_count
  3290. || local_type_index + sub_local_count > local_count) {
  3291. set_error_buf(error_buf, error_buf_size,
  3292. "invalid local count");
  3293. return false;
  3294. }
  3295. #if WASM_ENABLE_GC == 0
  3296. CHECK_BUF(p_code, buf_code_end, 1);
  3297. /* 0x7F/0x7E/0x7D/0x7C */
  3298. type = read_uint8(p_code);
  3299. if (!is_valid_value_type_for_interpreter(type)) {
  3300. if (type == VALUE_TYPE_V128)
  3301. set_error_buf(error_buf, error_buf_size,
  3302. "v128 value type requires simd feature");
  3303. else if (type == VALUE_TYPE_FUNCREF
  3304. || type == VALUE_TYPE_EXTERNREF)
  3305. set_error_buf(error_buf, error_buf_size,
  3306. "ref value type requires "
  3307. "reference types feature");
  3308. else
  3309. set_error_buf_v(error_buf, error_buf_size,
  3310. "invalid local type 0x%02X", type);
  3311. return false;
  3312. }
  3313. #else
  3314. if (!resolve_value_type(&p_code, buf_code_end, module,
  3315. module->type_count, &need_ref_type_map,
  3316. &ref_type, false, error_buf,
  3317. error_buf_size)) {
  3318. return false;
  3319. }
  3320. if (need_ref_type_map) {
  3321. WASMRefType *ref_type_tmp;
  3322. if (!(ref_type_tmp = reftype_set_insert(
  3323. module->ref_type_set, &ref_type, error_buf,
  3324. error_buf_size))) {
  3325. return false;
  3326. }
  3327. for (k = 0; k < sub_local_count; k++) {
  3328. func->local_ref_type_maps[t + k].ref_type =
  3329. ref_type_tmp;
  3330. func->local_ref_type_maps[t + k].index =
  3331. local_type_index + k;
  3332. }
  3333. t += sub_local_count;
  3334. }
  3335. type = ref_type.ref_type;
  3336. #endif
  3337. for (k = 0; k < sub_local_count; k++) {
  3338. func->local_types[local_type_index++] = type;
  3339. }
  3340. #if WASM_ENABLE_WAMR_COMPILER != 0
  3341. if (type == VALUE_TYPE_V128)
  3342. module->is_simd_used = true;
  3343. else if (type == VALUE_TYPE_FUNCREF
  3344. || type == VALUE_TYPE_EXTERNREF)
  3345. module->is_ref_types_used = true;
  3346. #endif
  3347. }
  3348. bh_assert(local_type_index == func->local_count);
  3349. #if WASM_ENABLE_GC != 0
  3350. bh_assert(t == func->local_ref_type_map_count);
  3351. #if TRACE_WASM_LOADER != 0
  3352. os_printf("func %u, local types: [", i);
  3353. k = 0;
  3354. for (j = 0; j < func->local_count; j++) {
  3355. WASMRefType *ref_type_tmp = NULL;
  3356. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3357. bh_assert(j == func->local_ref_type_maps[k].index);
  3358. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3359. }
  3360. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3361. if (j < func->local_count - 1)
  3362. os_printf(" ");
  3363. }
  3364. os_printf("]\n");
  3365. #endif
  3366. #endif
  3367. func->param_cell_num = func->func_type->param_cell_num;
  3368. func->ret_cell_num = func->func_type->ret_cell_num;
  3369. local_cell_num =
  3370. wasm_get_cell_num(func->local_types, func->local_count);
  3371. if (local_cell_num > UINT16_MAX) {
  3372. set_error_buf(error_buf, error_buf_size,
  3373. "local count too large");
  3374. return false;
  3375. }
  3376. func->local_cell_num = (uint16)local_cell_num;
  3377. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3378. return false;
  3379. p_code = p_code_end;
  3380. }
  3381. }
  3382. if (p != p_end) {
  3383. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3384. return false;
  3385. }
  3386. LOG_VERBOSE("Load function section success.\n");
  3387. return true;
  3388. fail:
  3389. return false;
  3390. }
  3391. static bool
  3392. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3393. char *error_buf, uint32 error_buf_size)
  3394. {
  3395. const uint8 *p = buf, *p_end = buf_end;
  3396. uint32 table_count, i;
  3397. uint64 total_size;
  3398. WASMTable *table;
  3399. read_leb_uint32(p, p_end, table_count);
  3400. if (module->import_table_count + table_count > 1) {
  3401. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3402. /* a total of one table is allowed */
  3403. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3404. return false;
  3405. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3406. module->is_ref_types_used = true;
  3407. #endif
  3408. }
  3409. if (table_count) {
  3410. module->table_count = table_count;
  3411. total_size = sizeof(WASMTable) * (uint64)table_count;
  3412. if (!(module->tables =
  3413. loader_malloc(total_size, error_buf, error_buf_size))) {
  3414. return false;
  3415. }
  3416. /* load each table */
  3417. table = module->tables;
  3418. for (i = 0; i < table_count; i++, table++) {
  3419. #if WASM_ENABLE_GC != 0
  3420. uint8 flag;
  3421. bool has_init = false;
  3422. CHECK_BUF(p, p_end, 1);
  3423. flag = read_uint8(p);
  3424. if (flag == TABLE_INIT_EXPR_FLAG) {
  3425. CHECK_BUF(p, p_end, 1);
  3426. flag = read_uint8(p);
  3427. if (flag != 0x00) {
  3428. set_error_buf(error_buf, error_buf_size,
  3429. "invalid leading bytes for table");
  3430. return false;
  3431. }
  3432. has_init = true;
  3433. }
  3434. else {
  3435. p--;
  3436. }
  3437. #endif /* end of WASM_ENABLE_GC != 0 */
  3438. if (!load_table(&p, p_end, module, table, error_buf,
  3439. error_buf_size))
  3440. return false;
  3441. #if WASM_ENABLE_GC != 0
  3442. if (has_init) {
  3443. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3444. table->table_type.elem_type,
  3445. table->table_type.elem_ref_type, error_buf,
  3446. error_buf_size))
  3447. return false;
  3448. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3449. && table->init_expr.init_expr_type
  3450. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3451. set_error_buf(
  3452. error_buf, error_buf_size,
  3453. "unsupported initializer expression for table");
  3454. return false;
  3455. }
  3456. }
  3457. else {
  3458. if (wasm_is_reftype_htref_non_nullable(
  3459. table->table_type.elem_type)) {
  3460. set_error_buf(
  3461. error_buf, error_buf_size,
  3462. "type mismatch: non-nullable table without init expr");
  3463. return false;
  3464. }
  3465. }
  3466. #endif /* end of WASM_ENABLE_GC != 0 */
  3467. #if WASM_ENABLE_WAMR_COMPILER != 0
  3468. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3469. module->is_ref_types_used = true;
  3470. #endif
  3471. }
  3472. }
  3473. if (p != p_end) {
  3474. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3475. return false;
  3476. }
  3477. LOG_VERBOSE("Load table section success.\n");
  3478. return true;
  3479. fail:
  3480. return false;
  3481. }
  3482. static bool
  3483. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3484. char *error_buf, uint32 error_buf_size)
  3485. {
  3486. const uint8 *p = buf, *p_end = buf_end;
  3487. uint32 memory_count, i;
  3488. uint64 total_size;
  3489. WASMMemory *memory;
  3490. read_leb_uint32(p, p_end, memory_count);
  3491. #if WASM_ENABLE_MULTI_MEMORY == 0
  3492. /* a total of one memory is allowed */
  3493. if (module->import_memory_count + memory_count > 1) {
  3494. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3495. return false;
  3496. }
  3497. #endif
  3498. if (memory_count) {
  3499. module->memory_count = memory_count;
  3500. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3501. if (!(module->memories =
  3502. loader_malloc(total_size, error_buf, error_buf_size))) {
  3503. return false;
  3504. }
  3505. /* load each memory */
  3506. memory = module->memories;
  3507. for (i = 0; i < memory_count; i++, memory++)
  3508. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3509. return false;
  3510. }
  3511. if (p != p_end) {
  3512. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3513. return false;
  3514. }
  3515. LOG_VERBOSE("Load memory section success.\n");
  3516. return true;
  3517. fail:
  3518. return false;
  3519. }
  3520. static bool
  3521. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3522. char *error_buf, uint32 error_buf_size)
  3523. {
  3524. const uint8 *p = buf, *p_end = buf_end;
  3525. uint32 global_count, i;
  3526. uint64 total_size;
  3527. WASMGlobal *global;
  3528. uint8 mutable;
  3529. #if WASM_ENABLE_GC != 0
  3530. bool need_ref_type_map;
  3531. WASMRefType ref_type;
  3532. #endif
  3533. read_leb_uint32(p, p_end, global_count);
  3534. if (is_indices_overflow(module->import_global_count, global_count,
  3535. error_buf, error_buf_size))
  3536. return false;
  3537. module->global_count = 0;
  3538. if (global_count) {
  3539. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3540. if (!(module->globals =
  3541. loader_malloc(total_size, error_buf, error_buf_size))) {
  3542. return false;
  3543. }
  3544. global = module->globals;
  3545. for (i = 0; i < global_count; i++, global++) {
  3546. #if WASM_ENABLE_GC == 0
  3547. CHECK_BUF(p, p_end, 2);
  3548. /* global type */
  3549. global->type.val_type = read_uint8(p);
  3550. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3551. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3552. return false;
  3553. }
  3554. mutable = read_uint8(p);
  3555. #else
  3556. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3557. &need_ref_type_map, &ref_type, false,
  3558. error_buf, error_buf_size)) {
  3559. return false;
  3560. }
  3561. global->type.val_type = ref_type.ref_type;
  3562. CHECK_BUF(p, p_end, 1);
  3563. mutable = read_uint8(p);
  3564. #endif /* end of WASM_ENABLE_GC */
  3565. #if WASM_ENABLE_WAMR_COMPILER != 0
  3566. if (global->type.val_type == VALUE_TYPE_V128)
  3567. module->is_simd_used = true;
  3568. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3569. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3570. module->is_ref_types_used = true;
  3571. #endif
  3572. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3573. return false;
  3574. }
  3575. global->type.is_mutable = mutable ? true : false;
  3576. /* initialize expression */
  3577. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3578. global->type.val_type,
  3579. #if WASM_ENABLE_GC == 0
  3580. NULL,
  3581. #else
  3582. &ref_type,
  3583. #endif
  3584. error_buf, error_buf_size))
  3585. return false;
  3586. #if WASM_ENABLE_GC != 0
  3587. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3588. uint8 global_type;
  3589. WASMRefType *global_ref_type;
  3590. uint32 global_idx = global->init_expr.u.global_index;
  3591. if (global->init_expr.u.global_index
  3592. >= module->import_global_count + i) {
  3593. set_error_buf(error_buf, error_buf_size, "unknown global");
  3594. return false;
  3595. }
  3596. if (global_idx < module->import_global_count) {
  3597. global_type = module->import_globals[global_idx]
  3598. .u.global.type.val_type;
  3599. global_ref_type =
  3600. module->import_globals[global_idx].u.global.ref_type;
  3601. }
  3602. else {
  3603. global_type =
  3604. module
  3605. ->globals[global_idx - module->import_global_count]
  3606. .type.val_type;
  3607. global_ref_type =
  3608. module
  3609. ->globals[global_idx - module->import_global_count]
  3610. .ref_type;
  3611. }
  3612. if (!wasm_reftype_is_subtype_of(
  3613. global_type, global_ref_type, global->type.val_type,
  3614. global->ref_type, module->types, module->type_count)) {
  3615. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3616. return false;
  3617. }
  3618. }
  3619. if (need_ref_type_map) {
  3620. if (!(global->ref_type =
  3621. reftype_set_insert(module->ref_type_set, &ref_type,
  3622. error_buf, error_buf_size))) {
  3623. return false;
  3624. }
  3625. }
  3626. #if TRACE_WASM_LOADER != 0
  3627. os_printf("global type: ");
  3628. wasm_dump_value_type(global->type, global->ref_type);
  3629. os_printf("\n");
  3630. #endif
  3631. #endif
  3632. module->global_count++;
  3633. }
  3634. bh_assert(module->global_count == global_count);
  3635. }
  3636. if (p != p_end) {
  3637. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3638. return false;
  3639. }
  3640. LOG_VERBOSE("Load global section success.\n");
  3641. return true;
  3642. fail:
  3643. return false;
  3644. }
  3645. static bool
  3646. check_duplicate_exports(WASMModule *module, char *error_buf,
  3647. uint32 error_buf_size)
  3648. {
  3649. uint32 i;
  3650. bool result = false;
  3651. char *names_buf[32], **names = names_buf;
  3652. if (module->export_count > 32) {
  3653. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3654. error_buf_size);
  3655. if (!names) {
  3656. return result;
  3657. }
  3658. }
  3659. for (i = 0; i < module->export_count; i++) {
  3660. names[i] = module->exports[i].name;
  3661. }
  3662. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3663. for (i = 1; i < module->export_count; i++) {
  3664. if (!strcmp(names[i], names[i - 1])) {
  3665. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3666. goto cleanup;
  3667. }
  3668. }
  3669. result = true;
  3670. cleanup:
  3671. if (module->export_count > 32) {
  3672. wasm_runtime_free(names);
  3673. }
  3674. return result;
  3675. }
  3676. static bool
  3677. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3678. bool is_load_from_file_buf, char *error_buf,
  3679. uint32 error_buf_size)
  3680. {
  3681. const uint8 *p = buf, *p_end = buf_end;
  3682. uint32 export_count, i, index;
  3683. uint64 total_size;
  3684. uint32 str_len;
  3685. WASMExport *export;
  3686. read_leb_uint32(p, p_end, export_count);
  3687. if (export_count) {
  3688. module->export_count = export_count;
  3689. total_size = sizeof(WASMExport) * (uint64)export_count;
  3690. if (!(module->exports =
  3691. loader_malloc(total_size, error_buf, error_buf_size))) {
  3692. return false;
  3693. }
  3694. export = module->exports;
  3695. for (i = 0; i < export_count; i++, export ++) {
  3696. #if WASM_ENABLE_THREAD_MGR == 0
  3697. if (p == p_end) {
  3698. /* export section with inconsistent count:
  3699. n export declared, but less than n given */
  3700. set_error_buf(error_buf, error_buf_size,
  3701. "length out of bounds");
  3702. return false;
  3703. }
  3704. #endif
  3705. read_leb_uint32(p, p_end, str_len);
  3706. CHECK_BUF(p, p_end, str_len);
  3707. if (!(export->name = wasm_const_str_list_insert(
  3708. p, str_len, module, is_load_from_file_buf, error_buf,
  3709. error_buf_size))) {
  3710. return false;
  3711. }
  3712. p += str_len;
  3713. CHECK_BUF(p, p_end, 1);
  3714. export->kind = read_uint8(p);
  3715. read_leb_uint32(p, p_end, index);
  3716. export->index = index;
  3717. switch (export->kind) {
  3718. /* function index */
  3719. case EXPORT_KIND_FUNC:
  3720. if (index >= module->function_count
  3721. + module->import_function_count) {
  3722. set_error_buf(error_buf, error_buf_size,
  3723. "unknown function");
  3724. return false;
  3725. }
  3726. #if WASM_ENABLE_SIMD != 0
  3727. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  3728. || (WASM_ENABLE_FAST_INTERP != 0)
  3729. /* TODO: check func type, if it has v128 param or result,
  3730. report error */
  3731. #endif
  3732. #endif
  3733. break;
  3734. /* table index */
  3735. case EXPORT_KIND_TABLE:
  3736. if (index
  3737. >= module->table_count + module->import_table_count) {
  3738. set_error_buf(error_buf, error_buf_size,
  3739. "unknown table");
  3740. return false;
  3741. }
  3742. break;
  3743. /* memory index */
  3744. case EXPORT_KIND_MEMORY:
  3745. if (index
  3746. >= module->memory_count + module->import_memory_count) {
  3747. set_error_buf(error_buf, error_buf_size,
  3748. "unknown memory");
  3749. return false;
  3750. }
  3751. break;
  3752. #if WASM_ENABLE_TAGS != 0
  3753. /* export tag */
  3754. case EXPORT_KIND_TAG:
  3755. if (index >= module->tag_count + module->import_tag_count) {
  3756. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3757. return false;
  3758. }
  3759. break;
  3760. #endif
  3761. /* global index */
  3762. case EXPORT_KIND_GLOBAL:
  3763. if (index
  3764. >= module->global_count + module->import_global_count) {
  3765. set_error_buf(error_buf, error_buf_size,
  3766. "unknown global");
  3767. return false;
  3768. }
  3769. break;
  3770. default:
  3771. set_error_buf(error_buf, error_buf_size,
  3772. "invalid export kind");
  3773. return false;
  3774. }
  3775. }
  3776. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  3777. return false;
  3778. }
  3779. }
  3780. if (p != p_end) {
  3781. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3782. return false;
  3783. }
  3784. LOG_VERBOSE("Load export section success.\n");
  3785. return true;
  3786. fail:
  3787. return false;
  3788. }
  3789. static bool
  3790. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3791. uint32 error_buf_size)
  3792. {
  3793. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3794. if (table_index != 0) {
  3795. set_error_buf(
  3796. error_buf, error_buf_size,
  3797. "zero byte expected. The module uses reference types feature "
  3798. "which is disabled in the runtime.");
  3799. return false;
  3800. }
  3801. #endif
  3802. if (table_index >= module->import_table_count + module->table_count) {
  3803. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3804. table_index);
  3805. return false;
  3806. }
  3807. return true;
  3808. }
  3809. static bool
  3810. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3811. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3812. {
  3813. const uint8 *p = *p_buf, *p_end = buf_end;
  3814. uint32 table_index;
  3815. read_leb_uint32(p, p_end, table_index);
  3816. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3817. return false;
  3818. }
  3819. *p_table_index = table_index;
  3820. *p_buf = p;
  3821. return true;
  3822. fail:
  3823. return false;
  3824. }
  3825. /* Element segments must match element type of table */
  3826. static bool
  3827. check_table_elem_type(WASMModule *module, uint32 table_index,
  3828. uint32 type_from_elem_seg, char *error_buf,
  3829. uint32 error_buf_size)
  3830. {
  3831. uint32 table_declared_elem_type;
  3832. if (table_index < module->import_table_count)
  3833. table_declared_elem_type =
  3834. module->import_tables[table_index].u.table.table_type.elem_type;
  3835. else
  3836. table_declared_elem_type =
  3837. module->tables[table_index - module->import_table_count]
  3838. .table_type.elem_type;
  3839. if (table_declared_elem_type == type_from_elem_seg)
  3840. return true;
  3841. #if WASM_ENABLE_GC != 0
  3842. /*
  3843. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3844. */
  3845. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3846. return true;
  3847. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3848. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3849. return true;
  3850. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3851. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3852. return true;
  3853. #endif
  3854. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3855. return false;
  3856. }
  3857. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3858. static bool
  3859. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3860. uint32 *p_elem_type,
  3861. #if WASM_ENABLE_GC != 0
  3862. WASMRefType **p_elem_ref_type,
  3863. #endif
  3864. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3865. {
  3866. const uint8 *p = *p_buf, *p_end = buf_end;
  3867. uint8 elem_type;
  3868. #if WASM_ENABLE_GC != 0
  3869. WASMRefType elem_ref_type;
  3870. bool need_ref_type_map;
  3871. #endif
  3872. CHECK_BUF(p, p_end, 1);
  3873. elem_type = read_uint8(p);
  3874. if (elemkind_zero) {
  3875. if (elem_type != 0) {
  3876. set_error_buf(error_buf, error_buf_size,
  3877. "invalid reference type or unknown type");
  3878. return false;
  3879. }
  3880. else {
  3881. *p_elem_type = VALUE_TYPE_FUNCREF;
  3882. *p_buf = p;
  3883. return true;
  3884. }
  3885. }
  3886. #if WASM_ENABLE_GC == 0
  3887. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3888. set_error_buf(error_buf, error_buf_size,
  3889. "invalid reference type or unknown type");
  3890. return false;
  3891. }
  3892. *p_elem_type = elem_type;
  3893. #else
  3894. p--;
  3895. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3896. module->type_count, &need_ref_type_map,
  3897. &elem_ref_type, false, error_buf, error_buf_size)) {
  3898. return false;
  3899. }
  3900. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3901. set_error_buf(error_buf, error_buf_size,
  3902. "invalid reference type or unknown type");
  3903. return false;
  3904. }
  3905. *p_elem_type = elem_ref_type.ref_type;
  3906. if (need_ref_type_map) {
  3907. if (!(*p_elem_ref_type =
  3908. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3909. error_buf, error_buf_size))) {
  3910. return false;
  3911. }
  3912. }
  3913. #endif
  3914. *p_buf = p;
  3915. return true;
  3916. fail:
  3917. return false;
  3918. }
  3919. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3920. static bool
  3921. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3922. WASMModule *module, WASMTableSeg *table_segment,
  3923. char *error_buf, uint32 error_buf_size)
  3924. {
  3925. const uint8 *p = *p_buf, *p_end = buf_end;
  3926. uint32 function_count, function_index = 0, i;
  3927. uint64 total_size;
  3928. read_leb_uint32(p, p_end, function_count);
  3929. table_segment->value_count = function_count;
  3930. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3931. if (total_size > 0
  3932. && !(table_segment->init_values =
  3933. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3934. error_buf_size))) {
  3935. return false;
  3936. }
  3937. for (i = 0; i < function_count; i++) {
  3938. InitializerExpression *init_expr = &table_segment->init_values[i];
  3939. read_leb_uint32(p, p_end, function_index);
  3940. if (!check_function_index(module, function_index, error_buf,
  3941. error_buf_size)) {
  3942. return false;
  3943. }
  3944. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3945. init_expr->u.ref_index = function_index;
  3946. }
  3947. *p_buf = p;
  3948. return true;
  3949. fail:
  3950. return false;
  3951. }
  3952. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3953. static bool
  3954. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3955. WASMModule *module, WASMTableSeg *table_segment,
  3956. char *error_buf, uint32 error_buf_size)
  3957. {
  3958. const uint8 *p = *p_buf, *p_end = buf_end;
  3959. uint32 ref_count, i;
  3960. uint64 total_size;
  3961. read_leb_uint32(p, p_end, ref_count);
  3962. table_segment->value_count = ref_count;
  3963. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3964. if (total_size > 0
  3965. && !(table_segment->init_values =
  3966. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3967. error_buf_size))) {
  3968. return false;
  3969. }
  3970. for (i = 0; i < ref_count; i++) {
  3971. InitializerExpression *init_expr = &table_segment->init_values[i];
  3972. if (!load_init_expr(module, &p, p_end, init_expr,
  3973. table_segment->elem_type,
  3974. #if WASM_ENABLE_GC == 0
  3975. NULL,
  3976. #else
  3977. table_segment->elem_ref_type,
  3978. #endif
  3979. error_buf, error_buf_size))
  3980. return false;
  3981. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3982. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3983. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3984. && init_expr->init_expr_type
  3985. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3986. }
  3987. *p_buf = p;
  3988. return true;
  3989. fail:
  3990. return false;
  3991. }
  3992. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3993. static bool
  3994. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3995. WASMModule *module, char *error_buf,
  3996. uint32 error_buf_size)
  3997. {
  3998. const uint8 *p = buf, *p_end = buf_end;
  3999. uint8 table_elem_idx_type;
  4000. uint32 table_segment_count, i;
  4001. uint64 total_size;
  4002. WASMTableSeg *table_segment;
  4003. read_leb_uint32(p, p_end, table_segment_count);
  4004. if (table_segment_count) {
  4005. module->table_seg_count = table_segment_count;
  4006. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4007. if (!(module->table_segments =
  4008. loader_malloc(total_size, error_buf, error_buf_size))) {
  4009. return false;
  4010. }
  4011. table_segment = module->table_segments;
  4012. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4013. if (p >= p_end) {
  4014. set_error_buf(error_buf, error_buf_size,
  4015. "invalid value type or "
  4016. "invalid elements segment kind");
  4017. return false;
  4018. }
  4019. table_elem_idx_type = VALUE_TYPE_I32;
  4020. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4021. read_leb_uint32(p, p_end, table_segment->mode);
  4022. /* last three bits */
  4023. table_segment->mode = table_segment->mode & 0x07;
  4024. switch (table_segment->mode) {
  4025. /* elemkind/elemtype + active */
  4026. case 0:
  4027. case 4:
  4028. {
  4029. #if WASM_ENABLE_GC != 0
  4030. if (table_segment->mode == 0) {
  4031. /* vec(funcidx), set elem type to (ref func) */
  4032. WASMRefType elem_ref_type = { 0 };
  4033. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4034. wasm_set_refheaptype_common(
  4035. &elem_ref_type.ref_ht_common, false,
  4036. HEAP_TYPE_FUNC);
  4037. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4038. module->ref_type_set, &elem_ref_type,
  4039. error_buf, error_buf_size)))
  4040. return false;
  4041. }
  4042. else {
  4043. /* vec(expr), set elem type to funcref */
  4044. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4045. }
  4046. #else
  4047. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4048. #endif
  4049. table_segment->table_index = 0;
  4050. if (!check_table_index(module, table_segment->table_index,
  4051. error_buf, error_buf_size))
  4052. return false;
  4053. #if WASM_ENABLE_MEMORY64 != 0
  4054. table_elem_idx_type =
  4055. is_table_64bit(module, table_segment->table_index)
  4056. ? VALUE_TYPE_I64
  4057. : VALUE_TYPE_I32;
  4058. #endif
  4059. if (!load_init_expr(module, &p, p_end,
  4060. &table_segment->base_offset,
  4061. table_elem_idx_type, NULL, error_buf,
  4062. error_buf_size))
  4063. return false;
  4064. if (table_segment->mode == 0) {
  4065. /* vec(funcidx) */
  4066. if (!load_func_index_vec(&p, p_end, module,
  4067. table_segment, error_buf,
  4068. error_buf_size))
  4069. return false;
  4070. }
  4071. else {
  4072. /* vec(expr) */
  4073. if (!load_init_expr_vec(&p, p_end, module,
  4074. table_segment, error_buf,
  4075. error_buf_size))
  4076. return false;
  4077. }
  4078. if (!check_table_elem_type(module,
  4079. table_segment->table_index,
  4080. table_segment->elem_type,
  4081. error_buf, error_buf_size))
  4082. return false;
  4083. break;
  4084. }
  4085. /* elemkind + passive/declarative */
  4086. case 1:
  4087. case 3:
  4088. if (!load_elem_type(module, &p, p_end,
  4089. &table_segment->elem_type,
  4090. #if WASM_ENABLE_GC != 0
  4091. &table_segment->elem_ref_type,
  4092. #endif
  4093. true, error_buf, error_buf_size))
  4094. return false;
  4095. /* vec(funcidx) */
  4096. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4097. error_buf, error_buf_size))
  4098. return false;
  4099. break;
  4100. /* elemkind/elemtype + table_idx + active */
  4101. case 2:
  4102. case 6:
  4103. if (!load_table_index(&p, p_end, module,
  4104. &table_segment->table_index,
  4105. error_buf, error_buf_size))
  4106. return false;
  4107. #if WASM_ENABLE_MEMORY64 != 0
  4108. table_elem_idx_type =
  4109. is_table_64bit(module, table_segment->table_index)
  4110. ? VALUE_TYPE_I64
  4111. : VALUE_TYPE_I32;
  4112. #endif
  4113. if (!load_init_expr(module, &p, p_end,
  4114. &table_segment->base_offset,
  4115. table_elem_idx_type, NULL, error_buf,
  4116. error_buf_size))
  4117. return false;
  4118. if (!load_elem_type(module, &p, p_end,
  4119. &table_segment->elem_type,
  4120. #if WASM_ENABLE_GC != 0
  4121. &table_segment->elem_ref_type,
  4122. #endif
  4123. table_segment->mode == 2 ? true : false,
  4124. error_buf, error_buf_size))
  4125. return false;
  4126. if (table_segment->mode == 2) {
  4127. /* vec(funcidx) */
  4128. if (!load_func_index_vec(&p, p_end, module,
  4129. table_segment, error_buf,
  4130. error_buf_size))
  4131. return false;
  4132. }
  4133. else {
  4134. /* vec(expr) */
  4135. if (!load_init_expr_vec(&p, p_end, module,
  4136. table_segment, error_buf,
  4137. error_buf_size))
  4138. return false;
  4139. }
  4140. if (!check_table_elem_type(module,
  4141. table_segment->table_index,
  4142. table_segment->elem_type,
  4143. error_buf, error_buf_size))
  4144. return false;
  4145. break;
  4146. case 5:
  4147. case 7:
  4148. if (!load_elem_type(module, &p, p_end,
  4149. &table_segment->elem_type,
  4150. #if WASM_ENABLE_GC != 0
  4151. &table_segment->elem_ref_type,
  4152. #endif
  4153. false, error_buf, error_buf_size))
  4154. return false;
  4155. /* vec(expr) */
  4156. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4157. error_buf, error_buf_size))
  4158. return false;
  4159. break;
  4160. default:
  4161. set_error_buf(error_buf, error_buf_size,
  4162. "unknown element segment kind");
  4163. return false;
  4164. }
  4165. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4166. /*
  4167. * like: 00 41 05 0b 04 00 01 00 01
  4168. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4169. */
  4170. if (!load_table_index(&p, p_end, module,
  4171. &table_segment->table_index, error_buf,
  4172. error_buf_size))
  4173. return false;
  4174. #if WASM_ENABLE_MEMORY64 != 0
  4175. table_elem_idx_type =
  4176. is_table_64bit(module, table_segment->table_index)
  4177. ? VALUE_TYPE_I64
  4178. : VALUE_TYPE_I32;
  4179. #endif
  4180. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4181. table_elem_idx_type, NULL, error_buf,
  4182. error_buf_size))
  4183. return false;
  4184. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4185. error_buf, error_buf_size))
  4186. return false;
  4187. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4188. if (!check_table_elem_type(module, table_segment->table_index,
  4189. table_segment->elem_type, error_buf,
  4190. error_buf_size))
  4191. return false;
  4192. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4193. #if WASM_ENABLE_MEMORY64 != 0
  4194. if (table_elem_idx_type == VALUE_TYPE_I64
  4195. && table_segment->base_offset.u.u64 > UINT32_MAX) {
  4196. set_error_buf(error_buf, error_buf_size,
  4197. "In table64, table base offset can't be "
  4198. "larger than UINT32_MAX");
  4199. return false;
  4200. }
  4201. #endif
  4202. #if WASM_ENABLE_WAMR_COMPILER != 0
  4203. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4204. module->is_ref_types_used = true;
  4205. #endif
  4206. }
  4207. }
  4208. if (p != p_end) {
  4209. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4210. return false;
  4211. }
  4212. LOG_VERBOSE("Load table segment section success.\n");
  4213. return true;
  4214. fail:
  4215. return false;
  4216. }
  4217. #if WASM_ENABLE_BULK_MEMORY != 0
  4218. static bool
  4219. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4220. int data_seg_len, char *error_buf,
  4221. uint32 error_buf_size)
  4222. {
  4223. if (has_datacount_section && datacount_len != data_seg_len) {
  4224. set_error_buf(error_buf, error_buf_size,
  4225. "data count and data section have inconsistent lengths");
  4226. return false;
  4227. }
  4228. return true;
  4229. }
  4230. #endif
  4231. static bool
  4232. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4233. WASMModule *module,
  4234. #if WASM_ENABLE_BULK_MEMORY != 0
  4235. bool has_datacount_section,
  4236. #endif
  4237. bool clone_data_seg, char *error_buf,
  4238. uint32 error_buf_size)
  4239. {
  4240. const uint8 *p = buf, *p_end = buf_end;
  4241. uint32 data_seg_count, i, mem_index, data_seg_len;
  4242. uint64 total_size;
  4243. WASMDataSeg *dataseg;
  4244. InitializerExpression init_expr;
  4245. #if WASM_ENABLE_BULK_MEMORY != 0
  4246. bool is_passive = false;
  4247. uint32 mem_flag;
  4248. #endif
  4249. uint8 mem_offset_type = VALUE_TYPE_I32;
  4250. read_leb_uint32(p, p_end, data_seg_count);
  4251. #if WASM_ENABLE_BULK_MEMORY != 0
  4252. if (!check_data_count_consistency(has_datacount_section,
  4253. module->data_seg_count1, data_seg_count,
  4254. error_buf, error_buf_size)) {
  4255. return false;
  4256. }
  4257. #endif
  4258. if (data_seg_count) {
  4259. module->data_seg_count = data_seg_count;
  4260. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4261. if (!(module->data_segments =
  4262. loader_malloc(total_size, error_buf, error_buf_size))) {
  4263. return false;
  4264. }
  4265. for (i = 0; i < data_seg_count; i++) {
  4266. read_leb_uint32(p, p_end, mem_index);
  4267. #if WASM_ENABLE_BULK_MEMORY != 0
  4268. is_passive = false;
  4269. mem_flag = mem_index & 0x03;
  4270. switch (mem_flag) {
  4271. case 0x01:
  4272. is_passive = true;
  4273. #if WASM_ENABLE_WAMR_COMPILER != 0
  4274. module->is_bulk_memory_used = true;
  4275. #endif
  4276. break;
  4277. case 0x00:
  4278. /* no memory index, treat index as 0 */
  4279. mem_index = 0;
  4280. goto check_mem_index;
  4281. case 0x02:
  4282. /* read following memory index */
  4283. read_leb_uint32(p, p_end, mem_index);
  4284. #if WASM_ENABLE_WAMR_COMPILER != 0
  4285. module->is_bulk_memory_used = true;
  4286. #endif
  4287. check_mem_index:
  4288. if (mem_index
  4289. >= module->import_memory_count + module->memory_count) {
  4290. set_error_buf_v(error_buf, error_buf_size,
  4291. "unknown memory %d", mem_index);
  4292. return false;
  4293. }
  4294. break;
  4295. case 0x03:
  4296. default:
  4297. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4298. return false;
  4299. break;
  4300. }
  4301. #else
  4302. if (mem_index
  4303. >= module->import_memory_count + module->memory_count) {
  4304. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4305. mem_index);
  4306. return false;
  4307. }
  4308. #endif /* WASM_ENABLE_BULK_MEMORY */
  4309. #if WASM_ENABLE_BULK_MEMORY != 0
  4310. if (!is_passive)
  4311. #endif
  4312. {
  4313. #if WASM_ENABLE_MEMORY64 != 0
  4314. /* This memory_flag is from memory instead of data segment */
  4315. uint8 memory_flag;
  4316. if (module->import_memory_count > 0) {
  4317. memory_flag = module->import_memories[mem_index]
  4318. .u.memory.mem_type.flags;
  4319. }
  4320. else {
  4321. memory_flag =
  4322. module
  4323. ->memories[mem_index - module->import_memory_count]
  4324. .flags;
  4325. }
  4326. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4327. : VALUE_TYPE_I32;
  4328. #else
  4329. mem_offset_type = VALUE_TYPE_I32;
  4330. #endif
  4331. }
  4332. #if WASM_ENABLE_BULK_MEMORY != 0
  4333. if (!is_passive)
  4334. #endif
  4335. if (!load_init_expr(module, &p, p_end, &init_expr,
  4336. mem_offset_type, NULL, error_buf,
  4337. error_buf_size))
  4338. return false;
  4339. read_leb_uint32(p, p_end, data_seg_len);
  4340. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4341. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4342. return false;
  4343. }
  4344. #if WASM_ENABLE_BULK_MEMORY != 0
  4345. dataseg->is_passive = is_passive;
  4346. if (!is_passive)
  4347. #endif
  4348. {
  4349. bh_memcpy_s(&dataseg->base_offset,
  4350. sizeof(InitializerExpression), &init_expr,
  4351. sizeof(InitializerExpression));
  4352. dataseg->memory_index = mem_index;
  4353. }
  4354. dataseg->data_length = data_seg_len;
  4355. CHECK_BUF(p, p_end, data_seg_len);
  4356. if (clone_data_seg) {
  4357. if (!(dataseg->data = loader_malloc(
  4358. dataseg->data_length, error_buf, error_buf_size))) {
  4359. return false;
  4360. }
  4361. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4362. data_seg_len);
  4363. }
  4364. else {
  4365. dataseg->data = (uint8 *)p;
  4366. }
  4367. dataseg->is_data_cloned = clone_data_seg;
  4368. p += data_seg_len;
  4369. }
  4370. }
  4371. if (p != p_end) {
  4372. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4373. return false;
  4374. }
  4375. LOG_VERBOSE("Load data segment section success.\n");
  4376. return true;
  4377. fail:
  4378. return false;
  4379. }
  4380. #if WASM_ENABLE_BULK_MEMORY != 0
  4381. static bool
  4382. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4383. WASMModule *module, char *error_buf,
  4384. uint32 error_buf_size)
  4385. {
  4386. const uint8 *p = buf, *p_end = buf_end;
  4387. uint32 data_seg_count1 = 0;
  4388. read_leb_uint32(p, p_end, data_seg_count1);
  4389. module->data_seg_count1 = data_seg_count1;
  4390. if (p != p_end) {
  4391. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4392. return false;
  4393. }
  4394. #if WASM_ENABLE_WAMR_COMPILER != 0
  4395. module->is_bulk_memory_used = true;
  4396. #endif
  4397. LOG_VERBOSE("Load datacount section success.\n");
  4398. return true;
  4399. fail:
  4400. return false;
  4401. }
  4402. #endif
  4403. #if WASM_ENABLE_TAGS != 0
  4404. static bool
  4405. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4406. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4407. uint32 error_buf_size)
  4408. {
  4409. (void)buf_code;
  4410. (void)buf_code_end;
  4411. const uint8 *p = buf, *p_end = buf_end;
  4412. size_t total_size = 0;
  4413. /* number of tags defined in the section */
  4414. uint32 section_tag_count = 0;
  4415. uint8 tag_attribute;
  4416. uint32 tag_type;
  4417. WASMTag *tag = NULL;
  4418. /* get tag count */
  4419. read_leb_uint32(p, p_end, section_tag_count);
  4420. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4421. error_buf, error_buf_size))
  4422. return false;
  4423. module->tag_count = section_tag_count;
  4424. if (section_tag_count) {
  4425. total_size = sizeof(WASMTag *) * module->tag_count;
  4426. if (!(module->tags =
  4427. loader_malloc(total_size, error_buf, error_buf_size))) {
  4428. return false;
  4429. }
  4430. /* load each tag, imported tags precede the tags */
  4431. uint32 tag_index;
  4432. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4433. /* get the one byte attribute */
  4434. CHECK_BUF(p, p_end, 1);
  4435. tag_attribute = read_uint8(p);
  4436. /* get type */
  4437. read_leb_uint32(p, p_end, tag_type);
  4438. /* compare against module->types */
  4439. if (tag_type >= module->type_count) {
  4440. set_error_buf(error_buf, error_buf_size, "unknown type");
  4441. return false;
  4442. }
  4443. /* get return type (must be 0) */
  4444. /* check, that the type of the referred tag returns void */
  4445. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4446. if (func_type->result_count != 0) {
  4447. set_error_buf(error_buf, error_buf_size,
  4448. "non-empty tag result type");
  4449. goto fail;
  4450. }
  4451. if (!(tag = module->tags[tag_index] = loader_malloc(
  4452. sizeof(WASMTag), error_buf, error_buf_size))) {
  4453. return false;
  4454. }
  4455. /* store to module tag declarations */
  4456. tag->attribute = tag_attribute;
  4457. tag->type = tag_type;
  4458. tag->tag_type = func_type;
  4459. }
  4460. }
  4461. if (p != p_end) {
  4462. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4463. return false;
  4464. }
  4465. LOG_VERBOSE("Load tag section success.\n");
  4466. return true;
  4467. fail:
  4468. return false;
  4469. }
  4470. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4471. static bool
  4472. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4473. const uint8 *buf_func_end, WASMModule *module,
  4474. char *error_buf, uint32 error_buf_size)
  4475. {
  4476. const uint8 *p = buf, *p_end = buf_end;
  4477. const uint8 *p_func = buf_func;
  4478. uint32 func_count = 0, code_count;
  4479. /* code has been loaded in function section, so pass it here, just check
  4480. * whether function and code section have inconsistent lengths */
  4481. read_leb_uint32(p, p_end, code_count);
  4482. if (buf_func)
  4483. read_leb_uint32(p_func, buf_func_end, func_count);
  4484. if (func_count != code_count) {
  4485. set_error_buf(error_buf, error_buf_size,
  4486. "function and code section have inconsistent lengths");
  4487. return false;
  4488. }
  4489. LOG_VERBOSE("Load code segment section success.\n");
  4490. (void)module;
  4491. return true;
  4492. fail:
  4493. return false;
  4494. }
  4495. static bool
  4496. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4497. char *error_buf, uint32 error_buf_size)
  4498. {
  4499. const uint8 *p = buf, *p_end = buf_end;
  4500. WASMFuncType *type;
  4501. uint32 start_function;
  4502. read_leb_uint32(p, p_end, start_function);
  4503. if (start_function
  4504. >= module->function_count + module->import_function_count) {
  4505. set_error_buf(error_buf, error_buf_size, "unknown function");
  4506. return false;
  4507. }
  4508. if (start_function < module->import_function_count)
  4509. type = module->import_functions[start_function].u.function.func_type;
  4510. else
  4511. type = module->functions[start_function - module->import_function_count]
  4512. ->func_type;
  4513. if (type->param_count != 0 || type->result_count != 0) {
  4514. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4515. return false;
  4516. }
  4517. module->start_function = start_function;
  4518. if (p != p_end) {
  4519. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4520. return false;
  4521. }
  4522. LOG_VERBOSE("Load start section success.\n");
  4523. return true;
  4524. fail:
  4525. return false;
  4526. }
  4527. #if WASM_ENABLE_STRINGREF != 0
  4528. static bool
  4529. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4530. WASMModule *module, bool is_load_from_file_buf,
  4531. char *error_buf, uint32 error_buf_size)
  4532. {
  4533. const uint8 *p = buf, *p_end = buf_end;
  4534. int32 deferred_count, immediate_count, string_length, i;
  4535. uint64 total_size;
  4536. read_leb_uint32(p, p_end, deferred_count);
  4537. read_leb_uint32(p, p_end, immediate_count);
  4538. /* proposal set deferred_count for future extension */
  4539. if (deferred_count != 0) {
  4540. goto fail;
  4541. }
  4542. if (immediate_count > 0) {
  4543. total_size = sizeof(char *) * (uint64)immediate_count;
  4544. if (!(module->string_literal_ptrs =
  4545. loader_malloc(total_size, error_buf, error_buf_size))) {
  4546. goto fail;
  4547. }
  4548. module->string_literal_count = immediate_count;
  4549. total_size = sizeof(uint32) * (uint64)immediate_count;
  4550. if (!(module->string_literal_lengths =
  4551. loader_malloc(total_size, error_buf, error_buf_size))) {
  4552. goto fail;
  4553. }
  4554. for (i = 0; i < immediate_count; i++) {
  4555. read_leb_uint32(p, p_end, string_length);
  4556. CHECK_BUF(p, p_end, string_length);
  4557. module->string_literal_ptrs[i] = p;
  4558. module->string_literal_lengths[i] = string_length;
  4559. p += string_length;
  4560. }
  4561. }
  4562. if (p != p_end) {
  4563. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4564. goto fail;
  4565. }
  4566. LOG_VERBOSE("Load stringref section success.\n");
  4567. return true;
  4568. fail:
  4569. return false;
  4570. }
  4571. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4572. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4573. static bool
  4574. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4575. bool is_load_from_file_buf, char *error_buf,
  4576. uint32 error_buf_size)
  4577. {
  4578. const uint8 *p = buf, *p_end = buf_end;
  4579. uint32 name_type, subsection_size;
  4580. uint32 previous_name_type = 0;
  4581. uint32 num_func_name;
  4582. uint32 func_index;
  4583. uint32 previous_func_index = ~0U;
  4584. uint32 func_name_len;
  4585. uint32 name_index;
  4586. int i = 0;
  4587. if (p >= p_end) {
  4588. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4589. return false;
  4590. }
  4591. while (p < p_end) {
  4592. read_leb_uint32(p, p_end, name_type);
  4593. if (i != 0) {
  4594. if (name_type == previous_name_type) {
  4595. set_error_buf(error_buf, error_buf_size,
  4596. "duplicate sub-section");
  4597. return false;
  4598. }
  4599. if (name_type < previous_name_type) {
  4600. set_error_buf(error_buf, error_buf_size,
  4601. "out-of-order sub-section");
  4602. return false;
  4603. }
  4604. }
  4605. previous_name_type = name_type;
  4606. read_leb_uint32(p, p_end, subsection_size);
  4607. CHECK_BUF(p, p_end, subsection_size);
  4608. switch (name_type) {
  4609. case SUB_SECTION_TYPE_FUNC:
  4610. if (subsection_size) {
  4611. read_leb_uint32(p, p_end, num_func_name);
  4612. for (name_index = 0; name_index < num_func_name;
  4613. name_index++) {
  4614. read_leb_uint32(p, p_end, func_index);
  4615. if (func_index == previous_func_index) {
  4616. set_error_buf(error_buf, error_buf_size,
  4617. "duplicate function name");
  4618. return false;
  4619. }
  4620. if (func_index < previous_func_index
  4621. && previous_func_index != ~0U) {
  4622. set_error_buf(error_buf, error_buf_size,
  4623. "out-of-order function index ");
  4624. return false;
  4625. }
  4626. previous_func_index = func_index;
  4627. read_leb_uint32(p, p_end, func_name_len);
  4628. CHECK_BUF(p, p_end, func_name_len);
  4629. /* Skip the import functions */
  4630. if (func_index >= module->import_function_count) {
  4631. func_index -= module->import_function_count;
  4632. if (func_index >= module->function_count) {
  4633. set_error_buf(error_buf, error_buf_size,
  4634. "out-of-range function index");
  4635. return false;
  4636. }
  4637. if (!(module->functions[func_index]->field_name =
  4638. wasm_const_str_list_insert(
  4639. p, func_name_len, module,
  4640. #if WASM_ENABLE_WAMR_COMPILER != 0
  4641. false,
  4642. #else
  4643. is_load_from_file_buf,
  4644. #endif
  4645. error_buf, error_buf_size))) {
  4646. return false;
  4647. }
  4648. }
  4649. p += func_name_len;
  4650. }
  4651. }
  4652. break;
  4653. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4654. */
  4655. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4656. default:
  4657. p = p + subsection_size;
  4658. break;
  4659. }
  4660. i++;
  4661. }
  4662. return true;
  4663. fail:
  4664. return false;
  4665. }
  4666. #endif
  4667. static bool
  4668. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4669. bool is_load_from_file_buf, char *error_buf,
  4670. uint32 error_buf_size)
  4671. {
  4672. const uint8 *p = buf, *p_end = buf_end;
  4673. char section_name[32];
  4674. uint32 name_len, buffer_len;
  4675. if (p >= p_end) {
  4676. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4677. return false;
  4678. }
  4679. read_leb_uint32(p, p_end, name_len);
  4680. if (p + name_len > p_end) {
  4681. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4682. return false;
  4683. }
  4684. if (!wasm_check_utf8_str(p, name_len)) {
  4685. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4686. return false;
  4687. }
  4688. buffer_len = sizeof(section_name);
  4689. memset(section_name, 0, buffer_len);
  4690. if (name_len < buffer_len) {
  4691. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4692. }
  4693. else {
  4694. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4695. memset(section_name + buffer_len - 4, '.', 3);
  4696. }
  4697. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4698. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4699. module->name_section_buf = buf;
  4700. module->name_section_buf_end = buf_end;
  4701. p += name_len;
  4702. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4703. error_buf, error_buf_size)) {
  4704. return false;
  4705. }
  4706. LOG_VERBOSE("Load custom name section success.");
  4707. }
  4708. #endif
  4709. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4710. {
  4711. WASMCustomSection *section =
  4712. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4713. if (!section) {
  4714. return false;
  4715. }
  4716. section->name_addr = (char *)p;
  4717. section->name_len = name_len;
  4718. section->content_addr = (uint8 *)(p + name_len);
  4719. section->content_len = (uint32)(p_end - p - name_len);
  4720. section->next = module->custom_section_list;
  4721. module->custom_section_list = section;
  4722. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4723. return true;
  4724. }
  4725. #endif
  4726. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4727. (void)is_load_from_file_buf;
  4728. (void)module;
  4729. return true;
  4730. fail:
  4731. return false;
  4732. }
  4733. static void
  4734. calculate_global_data_offset(WASMModule *module)
  4735. {
  4736. uint32 i, data_offset;
  4737. data_offset = 0;
  4738. for (i = 0; i < module->import_global_count; i++) {
  4739. WASMGlobalImport *import_global =
  4740. &((module->import_globals + i)->u.global);
  4741. #if WASM_ENABLE_FAST_JIT != 0
  4742. import_global->data_offset = data_offset;
  4743. #endif
  4744. data_offset += wasm_value_type_size(import_global->type.val_type);
  4745. }
  4746. for (i = 0; i < module->global_count; i++) {
  4747. WASMGlobal *global = module->globals + i;
  4748. #if WASM_ENABLE_FAST_JIT != 0
  4749. global->data_offset = data_offset;
  4750. #endif
  4751. data_offset += wasm_value_type_size(global->type.val_type);
  4752. }
  4753. module->global_data_size = data_offset;
  4754. }
  4755. #if WASM_ENABLE_FAST_JIT != 0
  4756. static bool
  4757. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4758. uint32 error_buf_size)
  4759. {
  4760. #if WASM_ENABLE_LAZY_JIT != 0
  4761. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4762. #endif
  4763. uint32 i;
  4764. if (!module->function_count)
  4765. return true;
  4766. if (!(module->fast_jit_func_ptrs =
  4767. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4768. error_buf_size))) {
  4769. return false;
  4770. }
  4771. #if WASM_ENABLE_LAZY_JIT != 0
  4772. for (i = 0; i < module->function_count; i++) {
  4773. module->fast_jit_func_ptrs[i] =
  4774. jit_globals->compile_fast_jit_and_then_call;
  4775. }
  4776. #endif
  4777. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4778. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4779. set_error_buf(error_buf, error_buf_size,
  4780. "init fast jit thread lock failed");
  4781. return false;
  4782. }
  4783. module->fast_jit_thread_locks_inited[i] = true;
  4784. }
  4785. return true;
  4786. }
  4787. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4788. #if WASM_ENABLE_JIT != 0
  4789. static bool
  4790. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4791. uint32 error_buf_size)
  4792. {
  4793. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4794. AOTCompOption option = { 0 };
  4795. char *aot_last_error;
  4796. uint64 size;
  4797. #if WASM_ENABLE_GC != 0
  4798. bool gc_enabled = true;
  4799. #else
  4800. bool gc_enabled = false;
  4801. #endif
  4802. if (module->function_count == 0)
  4803. return true;
  4804. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4805. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4806. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4807. return false;
  4808. }
  4809. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4810. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4811. os_mutex_destroy(&module->tierup_wait_lock);
  4812. return false;
  4813. }
  4814. module->tierup_wait_lock_inited = true;
  4815. #endif
  4816. size = sizeof(void *) * (uint64)module->function_count
  4817. + sizeof(bool) * (uint64)module->function_count;
  4818. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4819. return false;
  4820. }
  4821. module->func_ptrs_compiled =
  4822. (bool *)((uint8 *)module->func_ptrs
  4823. + sizeof(void *) * module->function_count);
  4824. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4825. if (!module->comp_data) {
  4826. aot_last_error = aot_get_last_error();
  4827. bh_assert(aot_last_error != NULL);
  4828. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4829. return false;
  4830. }
  4831. option.is_jit_mode = true;
  4832. option.opt_level = llvm_jit_options->opt_level;
  4833. option.size_level = llvm_jit_options->size_level;
  4834. option.segue_flags = llvm_jit_options->segue_flags;
  4835. option.quick_invoke_c_api_import =
  4836. llvm_jit_options->quick_invoke_c_api_import;
  4837. #if WASM_ENABLE_BULK_MEMORY != 0
  4838. option.enable_bulk_memory = true;
  4839. #endif
  4840. #if WASM_ENABLE_THREAD_MGR != 0
  4841. option.enable_thread_mgr = true;
  4842. #endif
  4843. #if WASM_ENABLE_TAIL_CALL != 0
  4844. option.enable_tail_call = true;
  4845. #endif
  4846. #if WASM_ENABLE_SIMD != 0
  4847. option.enable_simd = true;
  4848. #endif
  4849. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4850. option.enable_ref_types = true;
  4851. #elif WASM_ENABLE_GC != 0
  4852. option.enable_gc = true;
  4853. #endif
  4854. option.enable_aux_stack_check = true;
  4855. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4856. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4857. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4858. aot_call_stack_features_init_default(&option.call_stack_features);
  4859. #endif
  4860. #if WASM_ENABLE_PERF_PROFILING != 0
  4861. option.enable_perf_profiling = true;
  4862. #endif
  4863. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4864. option.enable_memory_profiling = true;
  4865. option.enable_stack_estimation = true;
  4866. #endif
  4867. #if WASM_ENABLE_SHARED_HEAP != 0
  4868. option.enable_shared_heap = true;
  4869. #endif
  4870. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4871. if (!module->comp_ctx) {
  4872. aot_last_error = aot_get_last_error();
  4873. bh_assert(aot_last_error != NULL);
  4874. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4875. return false;
  4876. }
  4877. return true;
  4878. }
  4879. static bool
  4880. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4881. uint32 error_buf_size)
  4882. {
  4883. char *aot_last_error;
  4884. uint32 i;
  4885. if (module->function_count == 0)
  4886. return true;
  4887. if (!aot_compile_wasm(module->comp_ctx)) {
  4888. aot_last_error = aot_get_last_error();
  4889. bh_assert(aot_last_error != NULL);
  4890. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4891. return false;
  4892. }
  4893. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4894. if (module->orcjit_stop_compiling)
  4895. return false;
  4896. #endif
  4897. bh_print_time("Begin to lookup llvm jit functions");
  4898. for (i = 0; i < module->function_count; i++) {
  4899. LLVMOrcJITTargetAddress func_addr = 0;
  4900. LLVMErrorRef error;
  4901. char func_name[48];
  4902. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4903. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4904. func_name);
  4905. if (error != LLVMErrorSuccess) {
  4906. char *err_msg = LLVMGetErrorMessage(error);
  4907. set_error_buf_v(error_buf, error_buf_size,
  4908. "failed to compile llvm jit function: %s", err_msg);
  4909. LLVMDisposeErrorMessage(err_msg);
  4910. return false;
  4911. }
  4912. /**
  4913. * No need to lock the func_ptr[func_idx] here as it is basic
  4914. * data type, the load/store for it can be finished by one cpu
  4915. * instruction, and there can be only one cpu instruction
  4916. * loading/storing at the same time.
  4917. */
  4918. module->func_ptrs[i] = (void *)func_addr;
  4919. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4920. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4921. if (module->orcjit_stop_compiling)
  4922. return false;
  4923. #endif
  4924. }
  4925. bh_print_time("End lookup llvm jit functions");
  4926. return true;
  4927. }
  4928. #endif /* end of WASM_ENABLE_JIT != 0 */
  4929. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4930. && WASM_ENABLE_LAZY_JIT != 0
  4931. static void *
  4932. init_llvm_jit_functions_stage2_callback(void *arg)
  4933. {
  4934. WASMModule *module = (WASMModule *)arg;
  4935. char error_buf[128];
  4936. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4937. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4938. module->orcjit_stop_compiling = true;
  4939. return NULL;
  4940. }
  4941. os_mutex_lock(&module->tierup_wait_lock);
  4942. module->llvm_jit_inited = true;
  4943. os_cond_broadcast(&module->tierup_wait_cond);
  4944. os_mutex_unlock(&module->tierup_wait_lock);
  4945. return NULL;
  4946. }
  4947. #endif
  4948. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4949. /* The callback function to compile jit functions */
  4950. static void *
  4951. orcjit_thread_callback(void *arg)
  4952. {
  4953. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4954. #if WASM_ENABLE_JIT != 0
  4955. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4956. #endif
  4957. WASMModule *module = thread_arg->module;
  4958. uint32 group_idx = thread_arg->group_idx;
  4959. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4960. uint32 func_count = module->function_count;
  4961. uint32 i;
  4962. #if WASM_ENABLE_FAST_JIT != 0
  4963. /* Compile fast jit functions of this group */
  4964. for (i = group_idx; i < func_count; i += group_stride) {
  4965. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4966. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4967. break;
  4968. }
  4969. if (module->orcjit_stop_compiling) {
  4970. return NULL;
  4971. }
  4972. }
  4973. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4974. os_mutex_lock(&module->tierup_wait_lock);
  4975. module->fast_jit_ready_groups++;
  4976. os_mutex_unlock(&module->tierup_wait_lock);
  4977. #endif
  4978. #endif
  4979. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4980. && WASM_ENABLE_LAZY_JIT != 0
  4981. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4982. for (i = group_idx; i < func_count;
  4983. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4984. uint32 j;
  4985. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4986. if (i + j * group_stride < func_count) {
  4987. if (!jit_compiler_set_call_to_fast_jit(
  4988. module,
  4989. i + j * group_stride + module->import_function_count)) {
  4990. LOG_ERROR(
  4991. "failed to compile call_to_fast_jit for func %u\n",
  4992. i + j * group_stride + module->import_function_count);
  4993. module->orcjit_stop_compiling = true;
  4994. return NULL;
  4995. }
  4996. }
  4997. if (module->orcjit_stop_compiling) {
  4998. return NULL;
  4999. }
  5000. }
  5001. }
  5002. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  5003. fast jit functions are compiled */
  5004. os_mutex_lock(&module->tierup_wait_lock);
  5005. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5006. && module->fast_jit_ready_groups >= group_stride)) {
  5007. os_cond_reltimedwait(&module->tierup_wait_cond,
  5008. &module->tierup_wait_lock, 10000);
  5009. if (module->orcjit_stop_compiling) {
  5010. /* init_llvm_jit_functions_stage2 failed */
  5011. os_mutex_unlock(&module->tierup_wait_lock);
  5012. return NULL;
  5013. }
  5014. }
  5015. os_mutex_unlock(&module->tierup_wait_lock);
  5016. #endif
  5017. #if WASM_ENABLE_JIT != 0
  5018. /* Compile llvm jit functions of this group */
  5019. for (i = group_idx; i < func_count;
  5020. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5021. LLVMOrcJITTargetAddress func_addr = 0;
  5022. LLVMErrorRef error;
  5023. char func_name[48];
  5024. typedef void (*F)(void);
  5025. union {
  5026. F f;
  5027. void *v;
  5028. } u;
  5029. uint32 j;
  5030. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5031. "_wrapper");
  5032. LOG_DEBUG("compile llvm jit func %s", func_name);
  5033. error =
  5034. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5035. if (error != LLVMErrorSuccess) {
  5036. char *err_msg = LLVMGetErrorMessage(error);
  5037. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5038. LLVMDisposeErrorMessage(err_msg);
  5039. break;
  5040. }
  5041. /* Call the jit wrapper function to trigger its compilation, so as
  5042. to compile the actual jit functions, since we add the latter to
  5043. function list in the PartitionFunction callback */
  5044. u.v = (void *)func_addr;
  5045. u.f();
  5046. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5047. if (i + j * group_stride < func_count) {
  5048. module->func_ptrs_compiled[i + j * group_stride] = true;
  5049. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5050. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5051. i + j * group_stride);
  5052. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5053. func_name);
  5054. if (error != LLVMErrorSuccess) {
  5055. char *err_msg = LLVMGetErrorMessage(error);
  5056. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5057. err_msg);
  5058. LLVMDisposeErrorMessage(err_msg);
  5059. /* Ignore current llvm jit func, as its func ptr is
  5060. previous set to call_to_fast_jit, which also works */
  5061. continue;
  5062. }
  5063. jit_compiler_set_llvm_jit_func_ptr(
  5064. module,
  5065. i + j * group_stride + module->import_function_count,
  5066. (void *)func_addr);
  5067. /* Try to switch to call this llvm jit function instead of
  5068. fast jit function from fast jit jitted code */
  5069. jit_compiler_set_call_to_llvm_jit(
  5070. module,
  5071. i + j * group_stride + module->import_function_count);
  5072. #endif
  5073. }
  5074. }
  5075. if (module->orcjit_stop_compiling) {
  5076. break;
  5077. }
  5078. }
  5079. #endif
  5080. return NULL;
  5081. }
  5082. static void
  5083. orcjit_stop_compile_threads(WASMModule *module)
  5084. {
  5085. #if WASM_ENABLE_LAZY_JIT != 0
  5086. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5087. / sizeof(OrcJitThreadArg));
  5088. module->orcjit_stop_compiling = true;
  5089. for (i = 0; i < thread_num; i++) {
  5090. if (module->orcjit_threads[i])
  5091. os_thread_join(module->orcjit_threads[i], NULL);
  5092. }
  5093. #endif
  5094. }
  5095. static bool
  5096. compile_jit_functions(WASMModule *module, char *error_buf,
  5097. uint32 error_buf_size)
  5098. {
  5099. uint32 thread_num =
  5100. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5101. uint32 i, j;
  5102. bh_print_time("Begin to compile jit functions");
  5103. /* Create threads to compile the jit functions */
  5104. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5105. #if WASM_ENABLE_JIT != 0
  5106. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5107. #endif
  5108. module->orcjit_thread_args[i].module = module;
  5109. module->orcjit_thread_args[i].group_idx = i;
  5110. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5111. (void *)&module->orcjit_thread_args[i],
  5112. APP_THREAD_STACK_SIZE_DEFAULT)
  5113. != 0) {
  5114. set_error_buf(error_buf, error_buf_size,
  5115. "create orcjit compile thread failed");
  5116. /* Terminate the threads created */
  5117. module->orcjit_stop_compiling = true;
  5118. for (j = 0; j < i; j++) {
  5119. os_thread_join(module->orcjit_threads[j], NULL);
  5120. }
  5121. return false;
  5122. }
  5123. }
  5124. #if WASM_ENABLE_LAZY_JIT == 0
  5125. /* Wait until all jit functions are compiled for eager mode */
  5126. for (i = 0; i < thread_num; i++) {
  5127. if (module->orcjit_threads[i])
  5128. os_thread_join(module->orcjit_threads[i], NULL);
  5129. }
  5130. #if WASM_ENABLE_FAST_JIT != 0
  5131. /* Ensure all the fast-jit functions are compiled */
  5132. for (i = 0; i < module->function_count; i++) {
  5133. if (!jit_compiler_is_compiled(module,
  5134. i + module->import_function_count)) {
  5135. set_error_buf(error_buf, error_buf_size,
  5136. "failed to compile fast jit function");
  5137. return false;
  5138. }
  5139. }
  5140. #endif
  5141. #if WASM_ENABLE_JIT != 0
  5142. /* Ensure all the llvm-jit functions are compiled */
  5143. for (i = 0; i < module->function_count; i++) {
  5144. if (!module->func_ptrs_compiled[i]) {
  5145. set_error_buf(error_buf, error_buf_size,
  5146. "failed to compile llvm jit function");
  5147. return false;
  5148. }
  5149. }
  5150. #endif
  5151. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5152. bh_print_time("End compile jit functions");
  5153. return true;
  5154. }
  5155. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5156. static bool
  5157. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5158. uint32 cur_func_idx, char *error_buf,
  5159. uint32 error_buf_size);
  5160. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5161. void **
  5162. wasm_interp_get_handle_table(void);
  5163. static void **handle_table;
  5164. #endif
  5165. static bool
  5166. load_from_sections(WASMModule *module, WASMSection *sections,
  5167. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5168. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5169. {
  5170. WASMExport *export;
  5171. WASMSection *section = sections;
  5172. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5173. *buf_func = NULL, *buf_func_end = NULL;
  5174. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5175. WASMGlobal *aux_stack_top_global = NULL, *global;
  5176. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5177. aux_stack_top = (uint64)-1LL;
  5178. uint32 global_index, func_index, i;
  5179. uint32 aux_data_end_global_index = (uint32)-1;
  5180. uint32 aux_heap_base_global_index = (uint32)-1;
  5181. WASMFuncType *func_type;
  5182. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5183. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5184. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5185. #if WASM_ENABLE_BULK_MEMORY != 0
  5186. bool has_datacount_section = false;
  5187. #endif
  5188. /* Find code and function sections if have */
  5189. while (section) {
  5190. if (section->section_type == SECTION_TYPE_CODE) {
  5191. buf_code = section->section_body;
  5192. buf_code_end = buf_code + section->section_body_size;
  5193. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5194. module->buf_code = (uint8 *)buf_code;
  5195. module->buf_code_size = section->section_body_size;
  5196. #endif
  5197. }
  5198. else if (section->section_type == SECTION_TYPE_FUNC) {
  5199. buf_func = section->section_body;
  5200. buf_func_end = buf_func + section->section_body_size;
  5201. }
  5202. section = section->next;
  5203. }
  5204. section = sections;
  5205. while (section) {
  5206. buf = section->section_body;
  5207. buf_end = buf + section->section_body_size;
  5208. switch (section->section_type) {
  5209. case SECTION_TYPE_USER:
  5210. /* unsupported user section, ignore it. */
  5211. if (!load_user_section(buf, buf_end, module,
  5212. reuse_const_strings, error_buf,
  5213. error_buf_size))
  5214. return false;
  5215. break;
  5216. case SECTION_TYPE_TYPE:
  5217. if (!load_type_section(buf, buf_end, module, error_buf,
  5218. error_buf_size))
  5219. return false;
  5220. break;
  5221. case SECTION_TYPE_IMPORT:
  5222. if (!load_import_section(buf, buf_end, module,
  5223. reuse_const_strings, no_resolve,
  5224. error_buf, error_buf_size))
  5225. return false;
  5226. break;
  5227. case SECTION_TYPE_FUNC:
  5228. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5229. module, error_buf, error_buf_size))
  5230. return false;
  5231. break;
  5232. case SECTION_TYPE_TABLE:
  5233. if (!load_table_section(buf, buf_end, module, error_buf,
  5234. error_buf_size))
  5235. return false;
  5236. break;
  5237. case SECTION_TYPE_MEMORY:
  5238. if (!load_memory_section(buf, buf_end, module, error_buf,
  5239. error_buf_size))
  5240. return false;
  5241. break;
  5242. #if WASM_ENABLE_TAGS != 0
  5243. case SECTION_TYPE_TAG:
  5244. /* load tag declaration section */
  5245. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5246. module, error_buf, error_buf_size))
  5247. return false;
  5248. break;
  5249. #endif
  5250. case SECTION_TYPE_GLOBAL:
  5251. if (!load_global_section(buf, buf_end, module, error_buf,
  5252. error_buf_size))
  5253. return false;
  5254. break;
  5255. case SECTION_TYPE_EXPORT:
  5256. if (!load_export_section(buf, buf_end, module,
  5257. reuse_const_strings, error_buf,
  5258. error_buf_size))
  5259. return false;
  5260. break;
  5261. case SECTION_TYPE_START:
  5262. if (!load_start_section(buf, buf_end, module, error_buf,
  5263. error_buf_size))
  5264. return false;
  5265. break;
  5266. case SECTION_TYPE_ELEM:
  5267. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5268. error_buf_size))
  5269. return false;
  5270. break;
  5271. case SECTION_TYPE_CODE:
  5272. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5273. module, error_buf, error_buf_size))
  5274. return false;
  5275. break;
  5276. case SECTION_TYPE_DATA:
  5277. if (!load_data_segment_section(buf, buf_end, module,
  5278. #if WASM_ENABLE_BULK_MEMORY != 0
  5279. has_datacount_section,
  5280. #endif
  5281. clone_data_seg, error_buf,
  5282. error_buf_size))
  5283. return false;
  5284. break;
  5285. #if WASM_ENABLE_BULK_MEMORY != 0
  5286. case SECTION_TYPE_DATACOUNT:
  5287. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5288. error_buf_size))
  5289. return false;
  5290. has_datacount_section = true;
  5291. break;
  5292. #endif
  5293. #if WASM_ENABLE_STRINGREF != 0
  5294. case SECTION_TYPE_STRINGREF:
  5295. if (!load_stringref_section(buf, buf_end, module,
  5296. reuse_const_strings, error_buf,
  5297. error_buf_size))
  5298. return false;
  5299. break;
  5300. #endif
  5301. default:
  5302. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5303. return false;
  5304. }
  5305. section = section->next;
  5306. }
  5307. #if WASM_ENABLE_BULK_MEMORY != 0
  5308. if (!check_data_count_consistency(
  5309. has_datacount_section, module->data_seg_count1,
  5310. module->data_seg_count, error_buf, error_buf_size)) {
  5311. return false;
  5312. }
  5313. #endif
  5314. module->aux_data_end_global_index = (uint32)-1;
  5315. module->aux_heap_base_global_index = (uint32)-1;
  5316. module->aux_stack_top_global_index = (uint32)-1;
  5317. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5318. export = module->exports;
  5319. for (i = 0; i < module->export_count; i++, export ++) {
  5320. if (export->kind == EXPORT_KIND_GLOBAL) {
  5321. if (!strcmp(export->name, "__heap_base")) {
  5322. if (export->index < module->import_global_count) {
  5323. LOG_DEBUG("Skip the process if __heap_base is imported "
  5324. "instead of being a local global");
  5325. continue;
  5326. }
  5327. /* only process linker-generated symbols */
  5328. global_index = export->index - module->import_global_count;
  5329. global = module->globals + global_index;
  5330. if (global->type.val_type == VALUE_TYPE_I32
  5331. && !global->type.is_mutable
  5332. && global->init_expr.init_expr_type
  5333. == INIT_EXPR_TYPE_I32_CONST) {
  5334. aux_heap_base_global = global;
  5335. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5336. aux_heap_base_global_index = export->index;
  5337. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5338. aux_heap_base);
  5339. }
  5340. }
  5341. else if (!strcmp(export->name, "__data_end")) {
  5342. if (export->index < module->import_global_count) {
  5343. LOG_DEBUG("Skip the process if __data_end is imported "
  5344. "instead of being a local global");
  5345. continue;
  5346. }
  5347. /* only process linker-generated symbols */
  5348. global_index = export->index - module->import_global_count;
  5349. global = module->globals + global_index;
  5350. if (global->type.val_type == VALUE_TYPE_I32
  5351. && !global->type.is_mutable
  5352. && global->init_expr.init_expr_type
  5353. == INIT_EXPR_TYPE_I32_CONST) {
  5354. aux_data_end_global = global;
  5355. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5356. aux_data_end_global_index = export->index;
  5357. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5358. aux_data_end);
  5359. aux_data_end = align_uint64(aux_data_end, 16);
  5360. }
  5361. }
  5362. /* For module compiled with -pthread option, the global is:
  5363. [0] stack_top <-- 0
  5364. [1] tls_pointer
  5365. [2] tls_size
  5366. [3] data_end <-- 3
  5367. [4] global_base
  5368. [5] heap_base <-- 5
  5369. [6] dso_handle
  5370. For module compiled without -pthread option:
  5371. [0] stack_top <-- 0
  5372. [1] data_end <-- 1
  5373. [2] global_base
  5374. [3] heap_base <-- 3
  5375. [4] dso_handle
  5376. */
  5377. if (aux_data_end_global && aux_heap_base_global
  5378. && aux_data_end <= aux_heap_base) {
  5379. module->aux_data_end_global_index = aux_data_end_global_index;
  5380. module->aux_data_end = aux_data_end;
  5381. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5382. module->aux_heap_base = aux_heap_base;
  5383. /* Resolve aux stack top global */
  5384. for (global_index = 0; global_index < module->global_count;
  5385. global_index++) {
  5386. global = module->globals + global_index;
  5387. if (global->type.is_mutable /* heap_base and data_end is
  5388. not mutable */
  5389. && global->type.val_type == VALUE_TYPE_I32
  5390. && global->init_expr.init_expr_type
  5391. == INIT_EXPR_TYPE_I32_CONST
  5392. && (uint64)(uint32)global->init_expr.u.i32
  5393. <= aux_heap_base) {
  5394. aux_stack_top_global = global;
  5395. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5396. module->aux_stack_top_global_index =
  5397. module->import_global_count + global_index;
  5398. module->aux_stack_bottom = aux_stack_top;
  5399. module->aux_stack_size =
  5400. aux_stack_top > aux_data_end
  5401. ? (uint32)(aux_stack_top - aux_data_end)
  5402. : (uint32)aux_stack_top;
  5403. LOG_VERBOSE(
  5404. "Found aux stack top global, value: %" PRIu64 ", "
  5405. "global index: %d, stack size: %d",
  5406. aux_stack_top, global_index,
  5407. module->aux_stack_size);
  5408. break;
  5409. }
  5410. }
  5411. if (!aux_stack_top_global) {
  5412. /* Auxiliary stack global isn't found, it must be unused
  5413. in the wasm app, as if it is used, the global must be
  5414. defined. Here we set it to __heap_base global and set
  5415. its size to 0. */
  5416. aux_stack_top_global = aux_heap_base_global;
  5417. aux_stack_top = aux_heap_base;
  5418. module->aux_stack_top_global_index =
  5419. module->aux_heap_base_global_index;
  5420. module->aux_stack_bottom = aux_stack_top;
  5421. module->aux_stack_size = 0;
  5422. }
  5423. break;
  5424. }
  5425. }
  5426. }
  5427. module->malloc_function = (uint32)-1;
  5428. module->free_function = (uint32)-1;
  5429. module->retain_function = (uint32)-1;
  5430. /* Resolve malloc/free function exported by wasm module */
  5431. #if WASM_ENABLE_MEMORY64 != 0
  5432. if (has_module_memory64(module))
  5433. malloc_free_io_type = VALUE_TYPE_I64;
  5434. #endif
  5435. export = module->exports;
  5436. for (i = 0; i < module->export_count; i++, export ++) {
  5437. if (export->kind == EXPORT_KIND_FUNC) {
  5438. if (!strcmp(export->name, "malloc")
  5439. && export->index >= module->import_function_count) {
  5440. func_index = export->index - module->import_function_count;
  5441. func_type = module->functions[func_index]->func_type;
  5442. if (func_type->param_count == 1 && func_type->result_count == 1
  5443. && func_type->types[0] == malloc_free_io_type
  5444. && func_type->types[1] == malloc_free_io_type) {
  5445. bh_assert(module->malloc_function == (uint32)-1);
  5446. module->malloc_function = export->index;
  5447. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5448. export->name, export->index);
  5449. }
  5450. }
  5451. else if (!strcmp(export->name, "__new")
  5452. && export->index >= module->import_function_count) {
  5453. /* __new && __pin for AssemblyScript */
  5454. func_index = export->index - module->import_function_count;
  5455. func_type = module->functions[func_index]->func_type;
  5456. if (func_type->param_count == 2 && func_type->result_count == 1
  5457. && func_type->types[0] == malloc_free_io_type
  5458. && func_type->types[1] == VALUE_TYPE_I32
  5459. && func_type->types[2] == malloc_free_io_type) {
  5460. uint32 j;
  5461. WASMExport *export_tmp;
  5462. bh_assert(module->malloc_function == (uint32)-1);
  5463. module->malloc_function = export->index;
  5464. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5465. export->name, export->index);
  5466. /* resolve retain function.
  5467. If not found, reset malloc function index */
  5468. export_tmp = module->exports;
  5469. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5470. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5471. && (!strcmp(export_tmp->name, "__retain")
  5472. || (!strcmp(export_tmp->name, "__pin")))
  5473. && (export_tmp->index
  5474. >= module->import_function_count)) {
  5475. func_index = export_tmp->index
  5476. - module->import_function_count;
  5477. func_type =
  5478. module->functions[func_index]->func_type;
  5479. if (func_type->param_count == 1
  5480. && func_type->result_count == 1
  5481. && func_type->types[0] == malloc_free_io_type
  5482. && func_type->types[1] == malloc_free_io_type) {
  5483. bh_assert(module->retain_function
  5484. == (uint32)-1);
  5485. module->retain_function = export_tmp->index;
  5486. LOG_VERBOSE("Found retain function, name: %s, "
  5487. "index: %u",
  5488. export_tmp->name,
  5489. export_tmp->index);
  5490. break;
  5491. }
  5492. }
  5493. }
  5494. if (j == module->export_count) {
  5495. module->malloc_function = (uint32)-1;
  5496. LOG_VERBOSE("Can't find retain function,"
  5497. "reset malloc function index to -1");
  5498. }
  5499. }
  5500. }
  5501. else if (((!strcmp(export->name, "free"))
  5502. || (!strcmp(export->name, "__release"))
  5503. || (!strcmp(export->name, "__unpin")))
  5504. && export->index >= module->import_function_count) {
  5505. func_index = export->index - module->import_function_count;
  5506. func_type = module->functions[func_index]->func_type;
  5507. if (func_type->param_count == 1 && func_type->result_count == 0
  5508. && func_type->types[0] == malloc_free_io_type) {
  5509. bh_assert(module->free_function == (uint32)-1);
  5510. module->free_function = export->index;
  5511. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5512. export->name, export->index);
  5513. }
  5514. }
  5515. }
  5516. }
  5517. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5518. handle_table = wasm_interp_get_handle_table();
  5519. #endif
  5520. for (i = 0; i < module->function_count; i++) {
  5521. WASMFunction *func = module->functions[i];
  5522. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5523. error_buf_size)) {
  5524. return false;
  5525. }
  5526. if (i == module->function_count - 1
  5527. && func->code + func->code_size != buf_code_end) {
  5528. set_error_buf(error_buf, error_buf_size,
  5529. "code section size mismatch");
  5530. return false;
  5531. }
  5532. }
  5533. if (!module->possible_memory_grow) {
  5534. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5535. if (aux_data_end_global && aux_heap_base_global
  5536. && aux_stack_top_global) {
  5537. uint64 init_memory_size;
  5538. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5539. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5540. * valid range of uint32 */
  5541. if (shrunk_memory_size <= UINT32_MAX) {
  5542. if (module->import_memory_count) {
  5543. WASMMemoryImport *memory_import =
  5544. &module->import_memories[0].u.memory;
  5545. init_memory_size =
  5546. (uint64)memory_import->mem_type.num_bytes_per_page
  5547. * memory_import->mem_type.init_page_count;
  5548. if (shrunk_memory_size <= init_memory_size) {
  5549. /* Reset memory info to decrease memory usage */
  5550. memory_import->mem_type.num_bytes_per_page =
  5551. (uint32)shrunk_memory_size;
  5552. memory_import->mem_type.init_page_count = 1;
  5553. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5554. shrunk_memory_size);
  5555. }
  5556. }
  5557. if (module->memory_count) {
  5558. WASMMemory *memory = &module->memories[0];
  5559. init_memory_size = (uint64)memory->num_bytes_per_page
  5560. * memory->init_page_count;
  5561. if (shrunk_memory_size <= init_memory_size) {
  5562. /* Reset memory info to decrease memory usage */
  5563. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5564. memory->init_page_count = 1;
  5565. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5566. shrunk_memory_size);
  5567. }
  5568. }
  5569. }
  5570. }
  5571. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  5572. #if WASM_ENABLE_MULTI_MODULE == 0
  5573. if (module->import_memory_count) {
  5574. WASMMemoryImport *memory_import =
  5575. &module->import_memories[0].u.memory;
  5576. /* Only resize the memory to one big page if num_bytes_per_page is
  5577. * in valid range of uint32 */
  5578. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5579. memory_import->mem_type.num_bytes_per_page *=
  5580. memory_import->mem_type.init_page_count;
  5581. if (memory_import->mem_type.init_page_count > 0)
  5582. memory_import->mem_type.init_page_count =
  5583. memory_import->mem_type.max_page_count = 1;
  5584. else
  5585. memory_import->mem_type.init_page_count =
  5586. memory_import->mem_type.max_page_count = 0;
  5587. }
  5588. }
  5589. if (module->memory_count) {
  5590. WASMMemory *memory = &module->memories[0];
  5591. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5592. * valid range of uint32 */
  5593. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5594. memory->num_bytes_per_page *= memory->init_page_count;
  5595. if (memory->init_page_count > 0)
  5596. memory->init_page_count = memory->max_page_count = 1;
  5597. else
  5598. memory->init_page_count = memory->max_page_count = 0;
  5599. }
  5600. }
  5601. #endif
  5602. }
  5603. #if WASM_ENABLE_MEMORY64 != 0
  5604. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5605. false))
  5606. return false;
  5607. #endif
  5608. calculate_global_data_offset(module);
  5609. #if WASM_ENABLE_FAST_JIT != 0
  5610. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5611. return false;
  5612. }
  5613. #endif
  5614. #if WASM_ENABLE_JIT != 0
  5615. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5616. return false;
  5617. }
  5618. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5619. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5620. return false;
  5621. }
  5622. #else
  5623. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5624. thread fast jit execution, since applying llvm optimizations in
  5625. aot_compile_wasm may cost a lot of time.
  5626. Create thread with enough native stack to apply llvm optimizations */
  5627. if (os_thread_create(&module->llvm_jit_init_thread,
  5628. init_llvm_jit_functions_stage2_callback,
  5629. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5630. != 0) {
  5631. set_error_buf(error_buf, error_buf_size,
  5632. "create orcjit compile thread failed");
  5633. return false;
  5634. }
  5635. #endif
  5636. #endif
  5637. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5638. /* Create threads to compile the jit functions */
  5639. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5640. return false;
  5641. }
  5642. #endif
  5643. #if WASM_ENABLE_MEMORY_TRACING != 0
  5644. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5645. #endif
  5646. return true;
  5647. }
  5648. static WASMModule *
  5649. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5650. {
  5651. WASMModule *module =
  5652. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5653. bh_list_status ret;
  5654. if (!module) {
  5655. return NULL;
  5656. }
  5657. module->module_type = Wasm_Module_Bytecode;
  5658. /* Set start_function to -1, means no start function */
  5659. module->start_function = (uint32)-1;
  5660. module->name = name;
  5661. module->is_binary_freeable = false;
  5662. #if WASM_ENABLE_FAST_INTERP == 0
  5663. module->br_table_cache_list = &module->br_table_cache_list_head;
  5664. ret = bh_list_init(module->br_table_cache_list);
  5665. bh_assert(ret == BH_LIST_SUCCESS);
  5666. #endif
  5667. #if WASM_ENABLE_MULTI_MODULE != 0
  5668. module->import_module_list = &module->import_module_list_head;
  5669. ret = bh_list_init(module->import_module_list);
  5670. bh_assert(ret == BH_LIST_SUCCESS);
  5671. #endif
  5672. #if WASM_ENABLE_DEBUG_INTERP != 0
  5673. ret = bh_list_init(&module->fast_opcode_list);
  5674. bh_assert(ret == BH_LIST_SUCCESS);
  5675. #endif
  5676. #if WASM_ENABLE_GC != 0
  5677. if (!(module->ref_type_set =
  5678. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5679. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5680. goto fail1;
  5681. }
  5682. if (os_mutex_init(&module->rtt_type_lock)) {
  5683. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5684. goto fail2;
  5685. }
  5686. #endif
  5687. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5688. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5689. && WASM_ENABLE_LAZY_JIT != 0)
  5690. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5691. set_error_buf(error_buf, error_buf_size,
  5692. "init instance list lock failed");
  5693. goto fail3;
  5694. }
  5695. #endif
  5696. #if WASM_ENABLE_LIBC_WASI != 0
  5697. module->wasi_args.stdio[0] = os_invalid_raw_handle();
  5698. module->wasi_args.stdio[1] = os_invalid_raw_handle();
  5699. module->wasi_args.stdio[2] = os_invalid_raw_handle();
  5700. #endif
  5701. (void)ret;
  5702. return module;
  5703. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5704. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5705. && WASM_ENABLE_LAZY_JIT != 0)
  5706. fail3:
  5707. #endif
  5708. #if WASM_ENABLE_GC != 0
  5709. os_mutex_destroy(&module->rtt_type_lock);
  5710. fail2:
  5711. bh_hash_map_destroy(module->ref_type_set);
  5712. fail1:
  5713. #endif
  5714. wasm_runtime_free(module);
  5715. return NULL;
  5716. }
  5717. #if WASM_ENABLE_DEBUG_INTERP != 0
  5718. static bool
  5719. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5720. uint32 error_buf_size)
  5721. {
  5722. WASMFastOPCodeNode *fast_op =
  5723. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5724. if (fast_op) {
  5725. fast_op->offset = pos - module->load_addr;
  5726. fast_op->orig_op = orig_op;
  5727. bh_list_insert(&module->fast_opcode_list, fast_op);
  5728. }
  5729. return fast_op ? true : false;
  5730. }
  5731. #endif
  5732. WASMModule *
  5733. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5734. uint32 error_buf_size)
  5735. {
  5736. WASMModule *module = create_module("", error_buf, error_buf_size);
  5737. if (!module)
  5738. return NULL;
  5739. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  5740. error_buf_size)) {
  5741. wasm_loader_unload(module);
  5742. return NULL;
  5743. }
  5744. LOG_VERBOSE("Load module from sections success.\n");
  5745. return module;
  5746. }
  5747. static void
  5748. destroy_sections(WASMSection *section_list)
  5749. {
  5750. WASMSection *section = section_list, *next;
  5751. while (section) {
  5752. next = section->next;
  5753. wasm_runtime_free(section);
  5754. section = next;
  5755. }
  5756. }
  5757. /* clang-format off */
  5758. static uint8 section_ids[] = {
  5759. SECTION_TYPE_USER,
  5760. SECTION_TYPE_TYPE,
  5761. SECTION_TYPE_IMPORT,
  5762. SECTION_TYPE_FUNC,
  5763. SECTION_TYPE_TABLE,
  5764. SECTION_TYPE_MEMORY,
  5765. #if WASM_ENABLE_TAGS != 0
  5766. SECTION_TYPE_TAG,
  5767. #endif
  5768. #if WASM_ENABLE_STRINGREF != 0
  5769. /* must immediately precede the global section,
  5770. or where the global section would be */
  5771. SECTION_TYPE_STRINGREF,
  5772. #endif
  5773. SECTION_TYPE_GLOBAL,
  5774. SECTION_TYPE_EXPORT,
  5775. SECTION_TYPE_START,
  5776. SECTION_TYPE_ELEM,
  5777. #if WASM_ENABLE_BULK_MEMORY != 0
  5778. SECTION_TYPE_DATACOUNT,
  5779. #endif
  5780. SECTION_TYPE_CODE,
  5781. SECTION_TYPE_DATA
  5782. };
  5783. /* clang-format on */
  5784. static uint8
  5785. get_section_index(uint8 section_type)
  5786. {
  5787. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5788. for (uint8 i = 0; i < max_id; i++) {
  5789. if (section_type == section_ids[i])
  5790. return i;
  5791. }
  5792. return (uint8)-1;
  5793. }
  5794. static bool
  5795. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5796. char *error_buf, uint32 error_buf_size)
  5797. {
  5798. WASMSection *section_list_end = NULL, *section;
  5799. const uint8 *p = buf, *p_end = buf + size;
  5800. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5801. uint32 section_size;
  5802. bh_assert(!*p_section_list);
  5803. p += 8;
  5804. while (p < p_end) {
  5805. CHECK_BUF(p, p_end, 1);
  5806. section_type = read_uint8(p);
  5807. section_index = get_section_index(section_type);
  5808. if (section_index != (uint8)-1) {
  5809. if (section_type != SECTION_TYPE_USER) {
  5810. /* Custom sections may be inserted at any place,
  5811. while other sections must occur at most once
  5812. and in prescribed order. */
  5813. if (last_section_index != (uint8)-1
  5814. && (section_index <= last_section_index)) {
  5815. set_error_buf(error_buf, error_buf_size,
  5816. "unexpected content after last section or "
  5817. "junk after last section");
  5818. return false;
  5819. }
  5820. last_section_index = section_index;
  5821. }
  5822. read_leb_uint32(p, p_end, section_size);
  5823. CHECK_BUF1(p, p_end, section_size);
  5824. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5825. error_buf_size))) {
  5826. return false;
  5827. }
  5828. section->section_type = section_type;
  5829. section->section_body = (uint8 *)p;
  5830. section->section_body_size = section_size;
  5831. if (!section_list_end)
  5832. *p_section_list = section_list_end = section;
  5833. else {
  5834. section_list_end->next = section;
  5835. section_list_end = section;
  5836. }
  5837. p += section_size;
  5838. }
  5839. else {
  5840. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5841. return false;
  5842. }
  5843. }
  5844. return true;
  5845. fail:
  5846. return false;
  5847. }
  5848. static void
  5849. exchange32(uint8 *p_data)
  5850. {
  5851. uint8 value = *p_data;
  5852. *p_data = *(p_data + 3);
  5853. *(p_data + 3) = value;
  5854. value = *(p_data + 1);
  5855. *(p_data + 1) = *(p_data + 2);
  5856. *(p_data + 2) = value;
  5857. }
  5858. static union {
  5859. int a;
  5860. char b;
  5861. } __ue = { .a = 1 };
  5862. #define is_little_endian() (__ue.b == 1)
  5863. static bool
  5864. load(const uint8 *buf, uint32 size, WASMModule *module,
  5865. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  5866. uint32 error_buf_size)
  5867. {
  5868. const uint8 *buf_end = buf + size;
  5869. const uint8 *p = buf, *p_end = buf_end;
  5870. uint32 magic_number, version;
  5871. WASMSection *section_list = NULL;
  5872. CHECK_BUF1(p, p_end, sizeof(uint32));
  5873. magic_number = read_uint32(p);
  5874. if (!is_little_endian())
  5875. exchange32((uint8 *)&magic_number);
  5876. if (magic_number != WASM_MAGIC_NUMBER) {
  5877. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5878. return false;
  5879. }
  5880. CHECK_BUF1(p, p_end, sizeof(uint32));
  5881. version = read_uint32(p);
  5882. if (!is_little_endian())
  5883. exchange32((uint8 *)&version);
  5884. if (version != WASM_CURRENT_VERSION) {
  5885. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5886. return false;
  5887. }
  5888. module->package_version = version;
  5889. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5890. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5891. no_resolve, error_buf, error_buf_size)) {
  5892. destroy_sections(section_list);
  5893. return false;
  5894. }
  5895. destroy_sections(section_list);
  5896. return true;
  5897. fail:
  5898. return false;
  5899. }
  5900. #if WASM_ENABLE_LIBC_WASI != 0
  5901. /**
  5902. * refer to
  5903. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5904. */
  5905. static bool
  5906. check_wasi_abi_compatibility(const WASMModule *module,
  5907. #if WASM_ENABLE_MULTI_MODULE != 0
  5908. bool main_module,
  5909. #endif
  5910. char *error_buf, uint32 error_buf_size)
  5911. {
  5912. /**
  5913. * be careful with:
  5914. * wasi compatible modules(command/reactor) which don't import any wasi
  5915. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5916. * reactor can depend on nothing. At the same time, each has its own entry
  5917. * point.
  5918. *
  5919. * observations:
  5920. * - clang always injects `_start` into a command
  5921. * - clang always injects `_initialize` into a reactor
  5922. * - `iwasm -f` allows to run a function in the reactor
  5923. *
  5924. * strong assumptions:
  5925. * - no one will define either `_start` or `_initialize` on purpose
  5926. * - `_start` should always be `void _start(void)`
  5927. * - `_initialize` should always be `void _initialize(void)`
  5928. *
  5929. */
  5930. /* clang-format off */
  5931. /**
  5932. *
  5933. * | | import_wasi_api True | | import_wasi_api False | |
  5934. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5935. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5936. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5937. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5938. */
  5939. /* clang-format on */
  5940. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5941. uint32 import_function_count = module->import_function_count;
  5942. WASMFuncType *func_type;
  5943. /* (func (export "_start") (...) */
  5944. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5945. error_buf, error_buf_size);
  5946. if (start) {
  5947. if (start->index < import_function_count) {
  5948. set_error_buf(
  5949. error_buf, error_buf_size,
  5950. "the builtin _start function can not be an import function");
  5951. return false;
  5952. }
  5953. func_type =
  5954. module->functions[start->index - import_function_count]->func_type;
  5955. if (func_type->param_count || func_type->result_count) {
  5956. set_error_buf(error_buf, error_buf_size,
  5957. "the signature of builtin _start function is wrong");
  5958. return false;
  5959. }
  5960. }
  5961. else {
  5962. /* (func (export "_initialize") (...) */
  5963. initialize =
  5964. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5965. error_buf, error_buf_size);
  5966. if (initialize) {
  5967. if (initialize->index < import_function_count) {
  5968. set_error_buf(error_buf, error_buf_size,
  5969. "the builtin _initialize function can not be an "
  5970. "import function");
  5971. return false;
  5972. }
  5973. func_type =
  5974. module->functions[initialize->index - import_function_count]
  5975. ->func_type;
  5976. if (func_type->param_count || func_type->result_count) {
  5977. set_error_buf(
  5978. error_buf, error_buf_size,
  5979. "the signature of builtin _initialize function is wrong");
  5980. return false;
  5981. }
  5982. }
  5983. }
  5984. /* filter out non-wasi compatible modules */
  5985. if (!module->import_wasi_api && !start && !initialize) {
  5986. return true;
  5987. }
  5988. /* should have one at least */
  5989. if (module->import_wasi_api && !start && !initialize) {
  5990. LOG_WARNING("warning: a module with WASI apis should be either "
  5991. "a command or a reactor");
  5992. }
  5993. /*
  5994. * there is at least one of `_start` and `_initialize` in below cases.
  5995. * according to the assumption, they should be all wasi compatible
  5996. */
  5997. #if WASM_ENABLE_MULTI_MODULE != 0
  5998. /* filter out commands (with `_start`) cases */
  5999. if (start && !main_module) {
  6000. set_error_buf(
  6001. error_buf, error_buf_size,
  6002. "a command (with _start function) can not be a sub-module");
  6003. return false;
  6004. }
  6005. #endif
  6006. /*
  6007. * it is ok a reactor acts as a main module,
  6008. * so skip the check about (with `_initialize`)
  6009. */
  6010. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  6011. error_buf, error_buf_size);
  6012. if (!memory
  6013. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  6014. /*
  6015. * with wasi-threads, it's still an open question if a memory
  6016. * should be exported.
  6017. *
  6018. * https://github.com/WebAssembly/wasi-threads/issues/22
  6019. * https://github.com/WebAssembly/WASI/issues/502
  6020. *
  6021. * Note: this code assumes the number of memories is at most 1.
  6022. */
  6023. && module->import_memory_count == 0
  6024. #endif
  6025. ) {
  6026. set_error_buf(error_buf, error_buf_size,
  6027. "a module with WASI apis must export memory by default");
  6028. return false;
  6029. }
  6030. return true;
  6031. }
  6032. #endif
  6033. WASMModule *
  6034. wasm_loader_load(uint8 *buf, uint32 size,
  6035. #if WASM_ENABLE_MULTI_MODULE != 0
  6036. bool main_module,
  6037. #endif
  6038. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6039. {
  6040. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6041. if (!module) {
  6042. return NULL;
  6043. }
  6044. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6045. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6046. module->load_addr = (uint8 *)buf;
  6047. module->load_size = size;
  6048. #endif
  6049. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6050. error_buf, error_buf_size)) {
  6051. goto fail;
  6052. }
  6053. #if WASM_ENABLE_LIBC_WASI != 0
  6054. /* Check the WASI application ABI */
  6055. if (!check_wasi_abi_compatibility(module,
  6056. #if WASM_ENABLE_MULTI_MODULE != 0
  6057. main_module,
  6058. #endif
  6059. error_buf, error_buf_size)) {
  6060. goto fail;
  6061. }
  6062. #endif
  6063. LOG_VERBOSE("Load module success.\n");
  6064. return module;
  6065. fail:
  6066. wasm_loader_unload(module);
  6067. return NULL;
  6068. }
  6069. void
  6070. wasm_loader_unload(WASMModule *module)
  6071. {
  6072. uint32 i;
  6073. if (!module)
  6074. return;
  6075. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6076. && WASM_ENABLE_LAZY_JIT != 0
  6077. module->orcjit_stop_compiling = true;
  6078. if (module->llvm_jit_init_thread)
  6079. os_thread_join(module->llvm_jit_init_thread, NULL);
  6080. #endif
  6081. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6082. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6083. module internal data after they were freed */
  6084. orcjit_stop_compile_threads(module);
  6085. #endif
  6086. #if WASM_ENABLE_JIT != 0
  6087. if (module->func_ptrs)
  6088. wasm_runtime_free(module->func_ptrs);
  6089. if (module->comp_ctx)
  6090. aot_destroy_comp_context(module->comp_ctx);
  6091. if (module->comp_data)
  6092. aot_destroy_comp_data(module->comp_data);
  6093. #endif
  6094. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6095. && WASM_ENABLE_LAZY_JIT != 0
  6096. if (module->tierup_wait_lock_inited) {
  6097. os_mutex_destroy(&module->tierup_wait_lock);
  6098. os_cond_destroy(&module->tierup_wait_cond);
  6099. }
  6100. #endif
  6101. if (module->imports)
  6102. wasm_runtime_free(module->imports);
  6103. if (module->functions) {
  6104. for (i = 0; i < module->function_count; i++) {
  6105. if (module->functions[i]) {
  6106. if (module->functions[i]->local_offsets)
  6107. wasm_runtime_free(module->functions[i]->local_offsets);
  6108. #if WASM_ENABLE_FAST_INTERP != 0
  6109. if (module->functions[i]->code_compiled)
  6110. wasm_runtime_free(module->functions[i]->code_compiled);
  6111. if (module->functions[i]->consts)
  6112. wasm_runtime_free(module->functions[i]->consts);
  6113. #endif
  6114. #if WASM_ENABLE_FAST_JIT != 0
  6115. if (module->functions[i]->fast_jit_jitted_code) {
  6116. jit_code_cache_free(
  6117. module->functions[i]->fast_jit_jitted_code);
  6118. }
  6119. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6120. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6121. jit_code_cache_free(
  6122. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6123. }
  6124. #endif
  6125. #endif
  6126. #if WASM_ENABLE_GC != 0
  6127. if (module->functions[i]->local_ref_type_maps) {
  6128. wasm_runtime_free(
  6129. module->functions[i]->local_ref_type_maps);
  6130. }
  6131. #endif
  6132. wasm_runtime_free(module->functions[i]);
  6133. }
  6134. }
  6135. wasm_runtime_free(module->functions);
  6136. }
  6137. if (module->tables) {
  6138. #if WASM_ENABLE_GC != 0
  6139. for (i = 0; i < module->table_count; i++) {
  6140. destroy_init_expr(module, &module->tables[i].init_expr);
  6141. }
  6142. #endif
  6143. wasm_runtime_free(module->tables);
  6144. }
  6145. if (module->memories)
  6146. wasm_runtime_free(module->memories);
  6147. if (module->globals) {
  6148. #if WASM_ENABLE_GC != 0
  6149. for (i = 0; i < module->global_count; i++) {
  6150. destroy_init_expr(module, &module->globals[i].init_expr);
  6151. }
  6152. #endif
  6153. wasm_runtime_free(module->globals);
  6154. }
  6155. #if WASM_ENABLE_TAGS != 0
  6156. if (module->tags) {
  6157. for (i = 0; i < module->tag_count; i++) {
  6158. if (module->tags[i])
  6159. wasm_runtime_free(module->tags[i]);
  6160. }
  6161. wasm_runtime_free(module->tags);
  6162. }
  6163. #endif
  6164. if (module->exports)
  6165. wasm_runtime_free(module->exports);
  6166. if (module->table_segments) {
  6167. for (i = 0; i < module->table_seg_count; i++) {
  6168. if (module->table_segments[i].init_values) {
  6169. #if WASM_ENABLE_GC != 0
  6170. uint32 j;
  6171. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6172. destroy_init_expr(
  6173. module, &module->table_segments[i].init_values[j]);
  6174. }
  6175. #endif
  6176. wasm_runtime_free(module->table_segments[i].init_values);
  6177. }
  6178. }
  6179. wasm_runtime_free(module->table_segments);
  6180. }
  6181. if (module->data_segments) {
  6182. for (i = 0; i < module->data_seg_count; i++) {
  6183. if (module->data_segments[i]) {
  6184. if (module->data_segments[i]->is_data_cloned)
  6185. wasm_runtime_free(module->data_segments[i]->data);
  6186. wasm_runtime_free(module->data_segments[i]);
  6187. }
  6188. }
  6189. wasm_runtime_free(module->data_segments);
  6190. }
  6191. if (module->types) {
  6192. for (i = 0; i < module->type_count; i++) {
  6193. if (module->types[i])
  6194. destroy_wasm_type(module->types[i]);
  6195. }
  6196. wasm_runtime_free(module->types);
  6197. }
  6198. if (module->const_str_list) {
  6199. StringNode *node = module->const_str_list, *node_next;
  6200. while (node) {
  6201. node_next = node->next;
  6202. wasm_runtime_free(node);
  6203. node = node_next;
  6204. }
  6205. }
  6206. #if WASM_ENABLE_STRINGREF != 0
  6207. if (module->string_literal_ptrs) {
  6208. wasm_runtime_free((void *)module->string_literal_ptrs);
  6209. }
  6210. if (module->string_literal_lengths) {
  6211. wasm_runtime_free(module->string_literal_lengths);
  6212. }
  6213. #endif
  6214. #if WASM_ENABLE_FAST_INTERP == 0
  6215. if (module->br_table_cache_list) {
  6216. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6217. BrTableCache *node_next;
  6218. while (node) {
  6219. node_next = bh_list_elem_next(node);
  6220. wasm_runtime_free(node);
  6221. node = node_next;
  6222. }
  6223. }
  6224. #endif
  6225. #if WASM_ENABLE_MULTI_MODULE != 0
  6226. /* just release the sub module list */
  6227. if (module->import_module_list) {
  6228. WASMRegisteredModule *node =
  6229. bh_list_first_elem(module->import_module_list);
  6230. while (node) {
  6231. WASMRegisteredModule *next = bh_list_elem_next(node);
  6232. bh_list_remove(module->import_module_list, node);
  6233. /*
  6234. * unload(sub_module) will be triggered during runtime_destroy().
  6235. * every module in the global module list will be unloaded one by
  6236. * one. so don't worry.
  6237. */
  6238. wasm_runtime_free(node);
  6239. /*
  6240. * the module file reading buffer will be released
  6241. * in runtime_destroy()
  6242. */
  6243. node = next;
  6244. }
  6245. }
  6246. #endif
  6247. #if WASM_ENABLE_DEBUG_INTERP != 0
  6248. WASMFastOPCodeNode *fast_opcode =
  6249. bh_list_first_elem(&module->fast_opcode_list);
  6250. while (fast_opcode) {
  6251. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6252. wasm_runtime_free(fast_opcode);
  6253. fast_opcode = next;
  6254. }
  6255. #endif
  6256. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6257. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6258. && WASM_ENABLE_LAZY_JIT != 0)
  6259. os_mutex_destroy(&module->instance_list_lock);
  6260. #endif
  6261. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6262. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6263. #endif
  6264. #if WASM_ENABLE_FAST_JIT != 0
  6265. if (module->fast_jit_func_ptrs) {
  6266. wasm_runtime_free(module->fast_jit_func_ptrs);
  6267. }
  6268. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6269. if (module->fast_jit_thread_locks_inited[i]) {
  6270. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6271. }
  6272. }
  6273. #endif
  6274. #if WASM_ENABLE_GC != 0
  6275. os_mutex_destroy(&module->rtt_type_lock);
  6276. bh_hash_map_destroy(module->ref_type_set);
  6277. if (module->rtt_types) {
  6278. for (i = 0; i < module->type_count; i++) {
  6279. if (module->rtt_types[i])
  6280. wasm_runtime_free(module->rtt_types[i]);
  6281. }
  6282. wasm_runtime_free(module->rtt_types);
  6283. }
  6284. #if WASM_ENABLE_STRINGREF != 0
  6285. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6286. if (module->stringref_rtts[i])
  6287. wasm_runtime_free(module->stringref_rtts[i]);
  6288. }
  6289. #endif
  6290. #endif
  6291. wasm_runtime_free(module);
  6292. }
  6293. bool
  6294. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6295. const uint8 *start_addr, const uint8 *code_end_addr,
  6296. uint8 label_type, uint8 **p_else_addr,
  6297. uint8 **p_end_addr)
  6298. {
  6299. const uint8 *p = start_addr, *p_end = code_end_addr;
  6300. uint8 *else_addr = NULL;
  6301. char error_buf[128];
  6302. uint32 block_nested_depth = 1, count, i, j, t;
  6303. uint32 error_buf_size = sizeof(error_buf);
  6304. uint8 opcode, u8;
  6305. BlockAddr block_stack[16] = { { 0 } }, *block;
  6306. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6307. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6308. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6309. if (block[j].start_addr == start_addr) {
  6310. /* Cache hit */
  6311. *p_else_addr = block[j].else_addr;
  6312. *p_end_addr = block[j].end_addr;
  6313. return true;
  6314. }
  6315. }
  6316. /* Cache unhit */
  6317. block_stack[0].start_addr = start_addr;
  6318. while (p < code_end_addr) {
  6319. opcode = *p++;
  6320. #if WASM_ENABLE_DEBUG_INTERP != 0
  6321. op_break_retry:
  6322. #endif
  6323. switch (opcode) {
  6324. case WASM_OP_UNREACHABLE:
  6325. case WASM_OP_NOP:
  6326. break;
  6327. #if WASM_ENABLE_EXCE_HANDLING != 0
  6328. case WASM_OP_TRY:
  6329. u8 = read_uint8(p);
  6330. if (block_nested_depth
  6331. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6332. block_stack[block_nested_depth].start_addr = p;
  6333. block_stack[block_nested_depth].else_addr = NULL;
  6334. }
  6335. block_nested_depth++;
  6336. break;
  6337. case EXT_OP_TRY:
  6338. skip_leb_uint32(p, p_end);
  6339. if (block_nested_depth
  6340. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6341. block_stack[block_nested_depth].start_addr = p;
  6342. block_stack[block_nested_depth].else_addr = NULL;
  6343. }
  6344. block_nested_depth++;
  6345. break;
  6346. case WASM_OP_CATCH:
  6347. if (block_nested_depth == 1) {
  6348. *p_end_addr = (uint8 *)(p - 1);
  6349. /* stop search and return the address of the catch block */
  6350. return true;
  6351. }
  6352. break;
  6353. case WASM_OP_CATCH_ALL:
  6354. if (block_nested_depth == 1) {
  6355. *p_end_addr = (uint8 *)(p - 1);
  6356. /* stop search and return the address of the catch_all block
  6357. */
  6358. return true;
  6359. }
  6360. break;
  6361. case WASM_OP_THROW:
  6362. /* skip tag_index */
  6363. skip_leb(p);
  6364. break;
  6365. case WASM_OP_RETHROW:
  6366. /* skip depth */
  6367. skip_leb(p);
  6368. break;
  6369. case WASM_OP_DELEGATE:
  6370. if (block_nested_depth == 1) {
  6371. *p_end_addr = (uint8 *)(p - 1);
  6372. return true;
  6373. }
  6374. else {
  6375. skip_leb(p);
  6376. /* the DELEGATE opcode ends the tryblock, */
  6377. block_nested_depth--;
  6378. if (block_nested_depth
  6379. < sizeof(block_stack) / sizeof(BlockAddr))
  6380. block_stack[block_nested_depth].end_addr =
  6381. (uint8 *)(p - 1);
  6382. }
  6383. break;
  6384. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6385. case WASM_OP_BLOCK:
  6386. case WASM_OP_LOOP:
  6387. case WASM_OP_IF:
  6388. {
  6389. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6390. u8 = read_uint8(p);
  6391. if (is_byte_a_type(u8)) {
  6392. #if WASM_ENABLE_GC != 0
  6393. if (wasm_is_type_multi_byte_type(u8)) {
  6394. /* the possible extra bytes of GC ref type have been
  6395. modified to OP_NOP, no need to resolve them again */
  6396. }
  6397. #endif
  6398. }
  6399. else {
  6400. p--;
  6401. /* block type */
  6402. skip_leb_int32(p, p_end);
  6403. }
  6404. if (block_nested_depth
  6405. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6406. block_stack[block_nested_depth].start_addr = p;
  6407. block_stack[block_nested_depth].else_addr = NULL;
  6408. }
  6409. block_nested_depth++;
  6410. break;
  6411. }
  6412. case EXT_OP_BLOCK:
  6413. case EXT_OP_LOOP:
  6414. case EXT_OP_IF:
  6415. /* block type */
  6416. skip_leb_int32(p, p_end);
  6417. if (block_nested_depth
  6418. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6419. block_stack[block_nested_depth].start_addr = p;
  6420. block_stack[block_nested_depth].else_addr = NULL;
  6421. }
  6422. block_nested_depth++;
  6423. break;
  6424. case WASM_OP_ELSE:
  6425. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6426. else_addr = (uint8 *)(p - 1);
  6427. if (block_nested_depth - 1
  6428. < sizeof(block_stack) / sizeof(BlockAddr))
  6429. block_stack[block_nested_depth - 1].else_addr =
  6430. (uint8 *)(p - 1);
  6431. break;
  6432. case WASM_OP_END:
  6433. if (block_nested_depth == 1) {
  6434. if (label_type == LABEL_TYPE_IF)
  6435. *p_else_addr = else_addr;
  6436. *p_end_addr = (uint8 *)(p - 1);
  6437. block_stack[0].end_addr = (uint8 *)(p - 1);
  6438. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6439. t++) {
  6440. start_addr = block_stack[t].start_addr;
  6441. if (start_addr) {
  6442. i = ((uintptr_t)start_addr)
  6443. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6444. block =
  6445. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6446. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6447. if (!block[j].start_addr)
  6448. break;
  6449. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6450. memmove(block + 1, block,
  6451. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6452. * sizeof(BlockAddr));
  6453. j = 0;
  6454. }
  6455. block[j].start_addr = block_stack[t].start_addr;
  6456. block[j].else_addr = block_stack[t].else_addr;
  6457. block[j].end_addr = block_stack[t].end_addr;
  6458. }
  6459. else
  6460. break;
  6461. }
  6462. return true;
  6463. }
  6464. else {
  6465. block_nested_depth--;
  6466. if (block_nested_depth
  6467. < sizeof(block_stack) / sizeof(BlockAddr))
  6468. block_stack[block_nested_depth].end_addr =
  6469. (uint8 *)(p - 1);
  6470. }
  6471. break;
  6472. case WASM_OP_BR:
  6473. case WASM_OP_BR_IF:
  6474. skip_leb_uint32(p, p_end); /* labelidx */
  6475. break;
  6476. case WASM_OP_BR_TABLE:
  6477. read_leb_uint32(p, p_end, count); /* label num */
  6478. #if WASM_ENABLE_FAST_INTERP != 0
  6479. for (i = 0; i <= count; i++) /* labelidxs */
  6480. skip_leb_uint32(p, p_end);
  6481. #else
  6482. p += count + 1;
  6483. while (*p == WASM_OP_NOP)
  6484. p++;
  6485. #endif
  6486. break;
  6487. #if WASM_ENABLE_FAST_INTERP == 0
  6488. case EXT_OP_BR_TABLE_CACHE:
  6489. read_leb_uint32(p, p_end, count); /* label num */
  6490. while (*p == WASM_OP_NOP)
  6491. p++;
  6492. break;
  6493. #endif
  6494. case WASM_OP_RETURN:
  6495. break;
  6496. case WASM_OP_CALL:
  6497. #if WASM_ENABLE_TAIL_CALL != 0
  6498. case WASM_OP_RETURN_CALL:
  6499. #endif
  6500. skip_leb_uint32(p, p_end); /* funcidx */
  6501. break;
  6502. case WASM_OP_CALL_INDIRECT:
  6503. #if WASM_ENABLE_TAIL_CALL != 0
  6504. case WASM_OP_RETURN_CALL_INDIRECT:
  6505. #endif
  6506. skip_leb_uint32(p, p_end); /* typeidx */
  6507. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6508. skip_leb_uint32(p, p_end); /* tableidx */
  6509. #else
  6510. u8 = read_uint8(p); /* 0x00 */
  6511. #endif
  6512. break;
  6513. #if WASM_ENABLE_GC != 0
  6514. case WASM_OP_CALL_REF:
  6515. case WASM_OP_RETURN_CALL_REF:
  6516. skip_leb_uint32(p, p_end); /* typeidx */
  6517. break;
  6518. #endif
  6519. case WASM_OP_DROP:
  6520. case WASM_OP_SELECT:
  6521. case WASM_OP_DROP_64:
  6522. case WASM_OP_SELECT_64:
  6523. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_SIMD != 0
  6524. case WASM_OP_SELECT_128:
  6525. #endif
  6526. break;
  6527. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6528. case WASM_OP_SELECT_T:
  6529. {
  6530. skip_leb_uint32(p, p_end); /* vec length */
  6531. u8 = read_uint8(p); /* typeidx */
  6532. /* the possible extra bytes of GC ref type have been
  6533. modified to OP_NOP, no need to resolve them again */
  6534. break;
  6535. }
  6536. case WASM_OP_TABLE_GET:
  6537. case WASM_OP_TABLE_SET:
  6538. skip_leb_uint32(p, p_end); /* table index */
  6539. break;
  6540. case WASM_OP_REF_NULL:
  6541. {
  6542. u8 = read_uint8(p); /* type */
  6543. if (is_byte_a_type(u8)) {
  6544. #if WASM_ENABLE_GC != 0
  6545. if (wasm_is_type_multi_byte_type(u8)) {
  6546. /* the possible extra bytes of GC ref type have been
  6547. modified to OP_NOP, no need to resolve them again */
  6548. }
  6549. #endif
  6550. }
  6551. else {
  6552. p--;
  6553. skip_leb_uint32(p, p_end);
  6554. }
  6555. break;
  6556. }
  6557. case WASM_OP_REF_IS_NULL:
  6558. break;
  6559. case WASM_OP_REF_FUNC:
  6560. skip_leb_uint32(p, p_end); /* func index */
  6561. break;
  6562. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6563. #if WASM_ENABLE_GC != 0
  6564. case WASM_OP_REF_AS_NON_NULL:
  6565. case WASM_OP_REF_EQ:
  6566. break;
  6567. case WASM_OP_BR_ON_NULL:
  6568. case WASM_OP_BR_ON_NON_NULL:
  6569. skip_leb_uint32(p, p_end); /* label index */
  6570. break;
  6571. #endif /* end of WASM_ENABLE_GC != 0 */
  6572. case WASM_OP_GET_LOCAL:
  6573. case WASM_OP_SET_LOCAL:
  6574. case WASM_OP_TEE_LOCAL:
  6575. case WASM_OP_GET_GLOBAL:
  6576. case WASM_OP_SET_GLOBAL:
  6577. case WASM_OP_GET_GLOBAL_64:
  6578. case WASM_OP_SET_GLOBAL_64:
  6579. #if WASM_ENABLE_SIMDE != 0
  6580. case WASM_OP_GET_GLOBAL_V128:
  6581. case WASM_OP_SET_GLOBAL_V128:
  6582. #endif
  6583. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6584. skip_leb_uint32(p, p_end); /* local index */
  6585. break;
  6586. case EXT_OP_GET_LOCAL_FAST:
  6587. case EXT_OP_SET_LOCAL_FAST:
  6588. case EXT_OP_TEE_LOCAL_FAST:
  6589. CHECK_BUF(p, p_end, 1);
  6590. p++;
  6591. break;
  6592. case WASM_OP_I32_LOAD:
  6593. case WASM_OP_I64_LOAD:
  6594. case WASM_OP_F32_LOAD:
  6595. case WASM_OP_F64_LOAD:
  6596. case WASM_OP_I32_LOAD8_S:
  6597. case WASM_OP_I32_LOAD8_U:
  6598. case WASM_OP_I32_LOAD16_S:
  6599. case WASM_OP_I32_LOAD16_U:
  6600. case WASM_OP_I64_LOAD8_S:
  6601. case WASM_OP_I64_LOAD8_U:
  6602. case WASM_OP_I64_LOAD16_S:
  6603. case WASM_OP_I64_LOAD16_U:
  6604. case WASM_OP_I64_LOAD32_S:
  6605. case WASM_OP_I64_LOAD32_U:
  6606. case WASM_OP_I32_STORE:
  6607. case WASM_OP_I64_STORE:
  6608. case WASM_OP_F32_STORE:
  6609. case WASM_OP_F64_STORE:
  6610. case WASM_OP_I32_STORE8:
  6611. case WASM_OP_I32_STORE16:
  6612. case WASM_OP_I64_STORE8:
  6613. case WASM_OP_I64_STORE16:
  6614. case WASM_OP_I64_STORE32:
  6615. skip_leb_align(p, p_end); /* align */
  6616. skip_leb_mem_offset(p, p_end); /* offset */
  6617. break;
  6618. case WASM_OP_MEMORY_SIZE:
  6619. case WASM_OP_MEMORY_GROW:
  6620. skip_leb_memidx(p, p_end); /* memidx */
  6621. break;
  6622. case WASM_OP_I32_CONST:
  6623. skip_leb_int32(p, p_end);
  6624. break;
  6625. case WASM_OP_I64_CONST:
  6626. skip_leb_int64(p, p_end);
  6627. break;
  6628. case WASM_OP_F32_CONST:
  6629. p += sizeof(float32);
  6630. break;
  6631. case WASM_OP_F64_CONST:
  6632. p += sizeof(float64);
  6633. break;
  6634. case WASM_OP_I32_EQZ:
  6635. case WASM_OP_I32_EQ:
  6636. case WASM_OP_I32_NE:
  6637. case WASM_OP_I32_LT_S:
  6638. case WASM_OP_I32_LT_U:
  6639. case WASM_OP_I32_GT_S:
  6640. case WASM_OP_I32_GT_U:
  6641. case WASM_OP_I32_LE_S:
  6642. case WASM_OP_I32_LE_U:
  6643. case WASM_OP_I32_GE_S:
  6644. case WASM_OP_I32_GE_U:
  6645. case WASM_OP_I64_EQZ:
  6646. case WASM_OP_I64_EQ:
  6647. case WASM_OP_I64_NE:
  6648. case WASM_OP_I64_LT_S:
  6649. case WASM_OP_I64_LT_U:
  6650. case WASM_OP_I64_GT_S:
  6651. case WASM_OP_I64_GT_U:
  6652. case WASM_OP_I64_LE_S:
  6653. case WASM_OP_I64_LE_U:
  6654. case WASM_OP_I64_GE_S:
  6655. case WASM_OP_I64_GE_U:
  6656. case WASM_OP_F32_EQ:
  6657. case WASM_OP_F32_NE:
  6658. case WASM_OP_F32_LT:
  6659. case WASM_OP_F32_GT:
  6660. case WASM_OP_F32_LE:
  6661. case WASM_OP_F32_GE:
  6662. case WASM_OP_F64_EQ:
  6663. case WASM_OP_F64_NE:
  6664. case WASM_OP_F64_LT:
  6665. case WASM_OP_F64_GT:
  6666. case WASM_OP_F64_LE:
  6667. case WASM_OP_F64_GE:
  6668. case WASM_OP_I32_CLZ:
  6669. case WASM_OP_I32_CTZ:
  6670. case WASM_OP_I32_POPCNT:
  6671. case WASM_OP_I32_ADD:
  6672. case WASM_OP_I32_SUB:
  6673. case WASM_OP_I32_MUL:
  6674. case WASM_OP_I32_DIV_S:
  6675. case WASM_OP_I32_DIV_U:
  6676. case WASM_OP_I32_REM_S:
  6677. case WASM_OP_I32_REM_U:
  6678. case WASM_OP_I32_AND:
  6679. case WASM_OP_I32_OR:
  6680. case WASM_OP_I32_XOR:
  6681. case WASM_OP_I32_SHL:
  6682. case WASM_OP_I32_SHR_S:
  6683. case WASM_OP_I32_SHR_U:
  6684. case WASM_OP_I32_ROTL:
  6685. case WASM_OP_I32_ROTR:
  6686. case WASM_OP_I64_CLZ:
  6687. case WASM_OP_I64_CTZ:
  6688. case WASM_OP_I64_POPCNT:
  6689. case WASM_OP_I64_ADD:
  6690. case WASM_OP_I64_SUB:
  6691. case WASM_OP_I64_MUL:
  6692. case WASM_OP_I64_DIV_S:
  6693. case WASM_OP_I64_DIV_U:
  6694. case WASM_OP_I64_REM_S:
  6695. case WASM_OP_I64_REM_U:
  6696. case WASM_OP_I64_AND:
  6697. case WASM_OP_I64_OR:
  6698. case WASM_OP_I64_XOR:
  6699. case WASM_OP_I64_SHL:
  6700. case WASM_OP_I64_SHR_S:
  6701. case WASM_OP_I64_SHR_U:
  6702. case WASM_OP_I64_ROTL:
  6703. case WASM_OP_I64_ROTR:
  6704. case WASM_OP_F32_ABS:
  6705. case WASM_OP_F32_NEG:
  6706. case WASM_OP_F32_CEIL:
  6707. case WASM_OP_F32_FLOOR:
  6708. case WASM_OP_F32_TRUNC:
  6709. case WASM_OP_F32_NEAREST:
  6710. case WASM_OP_F32_SQRT:
  6711. case WASM_OP_F32_ADD:
  6712. case WASM_OP_F32_SUB:
  6713. case WASM_OP_F32_MUL:
  6714. case WASM_OP_F32_DIV:
  6715. case WASM_OP_F32_MIN:
  6716. case WASM_OP_F32_MAX:
  6717. case WASM_OP_F32_COPYSIGN:
  6718. case WASM_OP_F64_ABS:
  6719. case WASM_OP_F64_NEG:
  6720. case WASM_OP_F64_CEIL:
  6721. case WASM_OP_F64_FLOOR:
  6722. case WASM_OP_F64_TRUNC:
  6723. case WASM_OP_F64_NEAREST:
  6724. case WASM_OP_F64_SQRT:
  6725. case WASM_OP_F64_ADD:
  6726. case WASM_OP_F64_SUB:
  6727. case WASM_OP_F64_MUL:
  6728. case WASM_OP_F64_DIV:
  6729. case WASM_OP_F64_MIN:
  6730. case WASM_OP_F64_MAX:
  6731. case WASM_OP_F64_COPYSIGN:
  6732. case WASM_OP_I32_WRAP_I64:
  6733. case WASM_OP_I32_TRUNC_S_F32:
  6734. case WASM_OP_I32_TRUNC_U_F32:
  6735. case WASM_OP_I32_TRUNC_S_F64:
  6736. case WASM_OP_I32_TRUNC_U_F64:
  6737. case WASM_OP_I64_EXTEND_S_I32:
  6738. case WASM_OP_I64_EXTEND_U_I32:
  6739. case WASM_OP_I64_TRUNC_S_F32:
  6740. case WASM_OP_I64_TRUNC_U_F32:
  6741. case WASM_OP_I64_TRUNC_S_F64:
  6742. case WASM_OP_I64_TRUNC_U_F64:
  6743. case WASM_OP_F32_CONVERT_S_I32:
  6744. case WASM_OP_F32_CONVERT_U_I32:
  6745. case WASM_OP_F32_CONVERT_S_I64:
  6746. case WASM_OP_F32_CONVERT_U_I64:
  6747. case WASM_OP_F32_DEMOTE_F64:
  6748. case WASM_OP_F64_CONVERT_S_I32:
  6749. case WASM_OP_F64_CONVERT_U_I32:
  6750. case WASM_OP_F64_CONVERT_S_I64:
  6751. case WASM_OP_F64_CONVERT_U_I64:
  6752. case WASM_OP_F64_PROMOTE_F32:
  6753. case WASM_OP_I32_REINTERPRET_F32:
  6754. case WASM_OP_I64_REINTERPRET_F64:
  6755. case WASM_OP_F32_REINTERPRET_I32:
  6756. case WASM_OP_F64_REINTERPRET_I64:
  6757. case WASM_OP_I32_EXTEND8_S:
  6758. case WASM_OP_I32_EXTEND16_S:
  6759. case WASM_OP_I64_EXTEND8_S:
  6760. case WASM_OP_I64_EXTEND16_S:
  6761. case WASM_OP_I64_EXTEND32_S:
  6762. break;
  6763. #if WASM_ENABLE_GC != 0
  6764. case WASM_OP_GC_PREFIX:
  6765. {
  6766. uint32 opcode1;
  6767. read_leb_uint32(p, p_end, opcode1);
  6768. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6769. is no larger than UINT8_MAX */
  6770. opcode = (uint8)opcode1;
  6771. switch (opcode) {
  6772. case WASM_OP_STRUCT_NEW:
  6773. case WASM_OP_STRUCT_NEW_DEFAULT:
  6774. skip_leb_uint32(p, p_end); /* typeidx */
  6775. break;
  6776. case WASM_OP_STRUCT_GET:
  6777. case WASM_OP_STRUCT_GET_S:
  6778. case WASM_OP_STRUCT_GET_U:
  6779. case WASM_OP_STRUCT_SET:
  6780. skip_leb_uint32(p, p_end); /* typeidx */
  6781. skip_leb_uint32(p, p_end); /* fieldidx */
  6782. break;
  6783. case WASM_OP_ARRAY_NEW:
  6784. case WASM_OP_ARRAY_NEW_DEFAULT:
  6785. case WASM_OP_ARRAY_GET:
  6786. case WASM_OP_ARRAY_GET_S:
  6787. case WASM_OP_ARRAY_GET_U:
  6788. case WASM_OP_ARRAY_SET:
  6789. case WASM_OP_ARRAY_FILL:
  6790. skip_leb_uint32(p, p_end); /* typeidx */
  6791. break;
  6792. case WASM_OP_ARRAY_COPY:
  6793. skip_leb_uint32(p, p_end); /* typeidx1 */
  6794. skip_leb_uint32(p, p_end); /* typeidx2 */
  6795. break;
  6796. case WASM_OP_ARRAY_LEN:
  6797. break;
  6798. case WASM_OP_ARRAY_NEW_FIXED:
  6799. case WASM_OP_ARRAY_NEW_DATA:
  6800. case WASM_OP_ARRAY_NEW_ELEM:
  6801. skip_leb_uint32(p, p_end); /* typeidx */
  6802. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6803. break;
  6804. case WASM_OP_REF_I31:
  6805. case WASM_OP_I31_GET_S:
  6806. case WASM_OP_I31_GET_U:
  6807. break;
  6808. case WASM_OP_REF_TEST:
  6809. case WASM_OP_REF_CAST:
  6810. case WASM_OP_REF_TEST_NULLABLE:
  6811. case WASM_OP_REF_CAST_NULLABLE:
  6812. skip_leb_int32(p, p_end); /* heaptype */
  6813. break;
  6814. case WASM_OP_BR_ON_CAST:
  6815. case WASM_OP_BR_ON_CAST_FAIL:
  6816. p += sizeof(uint8); /* castflag */
  6817. skip_leb_uint32(p, p_end); /* labelidx */
  6818. skip_leb_int32(p, p_end); /* heaptype */
  6819. skip_leb_int32(p, p_end); /* heaptype2 */
  6820. break;
  6821. case WASM_OP_ANY_CONVERT_EXTERN:
  6822. case WASM_OP_EXTERN_CONVERT_ANY:
  6823. break;
  6824. #if WASM_ENABLE_STRINGREF != 0
  6825. case WASM_OP_STRING_NEW_UTF8:
  6826. case WASM_OP_STRING_NEW_WTF16:
  6827. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6828. case WASM_OP_STRING_NEW_WTF8:
  6829. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6830. break;
  6831. case WASM_OP_STRING_CONST:
  6832. skip_leb_int32(p, p_end); /* contents */
  6833. break;
  6834. case WASM_OP_STRING_MEASURE_UTF8:
  6835. case WASM_OP_STRING_MEASURE_WTF8:
  6836. case WASM_OP_STRING_MEASURE_WTF16:
  6837. break;
  6838. case WASM_OP_STRING_ENCODE_UTF8:
  6839. case WASM_OP_STRING_ENCODE_WTF16:
  6840. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6841. case WASM_OP_STRING_ENCODE_WTF8:
  6842. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6843. break;
  6844. case WASM_OP_STRING_CONCAT:
  6845. case WASM_OP_STRING_EQ:
  6846. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6847. case WASM_OP_STRING_AS_WTF8:
  6848. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6849. break;
  6850. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6851. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6852. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6853. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6854. break;
  6855. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6856. case WASM_OP_STRING_AS_WTF16:
  6857. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6858. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6859. break;
  6860. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6861. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6862. break;
  6863. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6864. case WASM_OP_STRING_AS_ITER:
  6865. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6866. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6867. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6868. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6869. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6870. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6871. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6872. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6873. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6874. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6875. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6876. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6877. break;
  6878. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6879. default:
  6880. return false;
  6881. }
  6882. break;
  6883. }
  6884. #endif /* end of WASM_ENABLE_GC != 0 */
  6885. case WASM_OP_MISC_PREFIX:
  6886. {
  6887. uint32 opcode1;
  6888. read_leb_uint32(p, p_end, opcode1);
  6889. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6890. is no larger than UINT8_MAX */
  6891. opcode = (uint8)opcode1;
  6892. switch (opcode) {
  6893. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6894. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6895. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6896. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6897. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6898. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6899. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6900. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6901. break;
  6902. #if WASM_ENABLE_BULK_MEMORY != 0
  6903. case WASM_OP_MEMORY_INIT:
  6904. skip_leb_uint32(p, p_end);
  6905. skip_leb_memidx(p, p_end);
  6906. break;
  6907. case WASM_OP_DATA_DROP:
  6908. skip_leb_uint32(p, p_end);
  6909. break;
  6910. case WASM_OP_MEMORY_COPY:
  6911. skip_leb_memidx(p, p_end);
  6912. skip_leb_memidx(p, p_end);
  6913. break;
  6914. case WASM_OP_MEMORY_FILL:
  6915. skip_leb_memidx(p, p_end);
  6916. break;
  6917. #endif /* WASM_ENABLE_BULK_MEMORY */
  6918. #if WASM_ENABLE_REF_TYPES != 0
  6919. case WASM_OP_TABLE_INIT:
  6920. case WASM_OP_TABLE_COPY:
  6921. /* tableidx */
  6922. skip_leb_uint32(p, p_end);
  6923. /* elemidx */
  6924. skip_leb_uint32(p, p_end);
  6925. break;
  6926. case WASM_OP_ELEM_DROP:
  6927. /* elemidx */
  6928. skip_leb_uint32(p, p_end);
  6929. break;
  6930. case WASM_OP_TABLE_SIZE:
  6931. case WASM_OP_TABLE_GROW:
  6932. case WASM_OP_TABLE_FILL:
  6933. skip_leb_uint32(p, p_end); /* table idx */
  6934. break;
  6935. #endif /* WASM_ENABLE_REF_TYPES */
  6936. default:
  6937. return false;
  6938. }
  6939. break;
  6940. }
  6941. #if WASM_ENABLE_SIMD != 0
  6942. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  6943. || (WASM_ENABLE_FAST_INTERP != 0)
  6944. case WASM_OP_SIMD_PREFIX:
  6945. {
  6946. uint32 opcode1;
  6947. read_leb_uint32(p, p_end, opcode1);
  6948. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6949. is no larger than UINT8_MAX */
  6950. opcode = (uint8)opcode1;
  6951. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6952. */
  6953. switch (opcode) {
  6954. case SIMD_v128_load:
  6955. case SIMD_v128_load8x8_s:
  6956. case SIMD_v128_load8x8_u:
  6957. case SIMD_v128_load16x4_s:
  6958. case SIMD_v128_load16x4_u:
  6959. case SIMD_v128_load32x2_s:
  6960. case SIMD_v128_load32x2_u:
  6961. case SIMD_v128_load8_splat:
  6962. case SIMD_v128_load16_splat:
  6963. case SIMD_v128_load32_splat:
  6964. case SIMD_v128_load64_splat:
  6965. case SIMD_v128_store:
  6966. /* memarg align */
  6967. skip_leb_uint32(p, p_end);
  6968. /* memarg offset */
  6969. skip_leb_mem_offset(p, p_end);
  6970. break;
  6971. case SIMD_v128_const:
  6972. case SIMD_v8x16_shuffle:
  6973. /* immByte[16] immLaneId[16] */
  6974. CHECK_BUF1(p, p_end, 16);
  6975. p += 16;
  6976. break;
  6977. case SIMD_i8x16_extract_lane_s:
  6978. case SIMD_i8x16_extract_lane_u:
  6979. case SIMD_i8x16_replace_lane:
  6980. case SIMD_i16x8_extract_lane_s:
  6981. case SIMD_i16x8_extract_lane_u:
  6982. case SIMD_i16x8_replace_lane:
  6983. case SIMD_i32x4_extract_lane:
  6984. case SIMD_i32x4_replace_lane:
  6985. case SIMD_i64x2_extract_lane:
  6986. case SIMD_i64x2_replace_lane:
  6987. case SIMD_f32x4_extract_lane:
  6988. case SIMD_f32x4_replace_lane:
  6989. case SIMD_f64x2_extract_lane:
  6990. case SIMD_f64x2_replace_lane:
  6991. /* ImmLaneId */
  6992. CHECK_BUF(p, p_end, 1);
  6993. p++;
  6994. break;
  6995. case SIMD_v128_load8_lane:
  6996. case SIMD_v128_load16_lane:
  6997. case SIMD_v128_load32_lane:
  6998. case SIMD_v128_load64_lane:
  6999. case SIMD_v128_store8_lane:
  7000. case SIMD_v128_store16_lane:
  7001. case SIMD_v128_store32_lane:
  7002. case SIMD_v128_store64_lane:
  7003. /* memarg align */
  7004. skip_leb_uint32(p, p_end);
  7005. /* memarg offset */
  7006. skip_leb_mem_offset(p, p_end);
  7007. /* ImmLaneId */
  7008. CHECK_BUF(p, p_end, 1);
  7009. p++;
  7010. break;
  7011. case SIMD_v128_load32_zero:
  7012. case SIMD_v128_load64_zero:
  7013. /* memarg align */
  7014. skip_leb_uint32(p, p_end);
  7015. /* memarg offset */
  7016. skip_leb_mem_offset(p, p_end);
  7017. break;
  7018. default:
  7019. /*
  7020. * since latest SIMD specific used almost every value
  7021. * from 0x00 to 0xff, the default branch will present
  7022. * all opcodes without imm
  7023. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  7024. */
  7025. break;
  7026. }
  7027. break;
  7028. }
  7029. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  7030. (WASM_ENABLE_FAST_INTERP != 0) */
  7031. #endif /* end of WASM_ENABLE_SIMD */
  7032. #if WASM_ENABLE_SHARED_MEMORY != 0
  7033. case WASM_OP_ATOMIC_PREFIX:
  7034. {
  7035. uint32 opcode1;
  7036. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  7037. read_leb_uint32(p, p_end, opcode1);
  7038. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7039. is no larger than UINT8_MAX */
  7040. opcode = (uint8)opcode1;
  7041. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7042. skip_leb_uint32(p, p_end); /* align */
  7043. skip_leb_mem_offset(p, p_end); /* offset */
  7044. }
  7045. else {
  7046. /* atomic.fence doesn't have memarg */
  7047. p++;
  7048. }
  7049. break;
  7050. }
  7051. #endif
  7052. #if WASM_ENABLE_DEBUG_INTERP != 0
  7053. case DEBUG_OP_BREAK:
  7054. {
  7055. WASMDebugInstance *debug_instance =
  7056. wasm_exec_env_get_instance(exec_env);
  7057. char original_opcode[1];
  7058. uint64 size = 1;
  7059. WASMModuleInstance *module_inst =
  7060. (WASMModuleInstance *)exec_env->module_inst;
  7061. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7062. ? (p - 1) - module_inst->module->load_addr
  7063. : ~0;
  7064. if (debug_instance) {
  7065. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7066. original_opcode, &size)
  7067. && size == 1) {
  7068. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7069. "with %02x: ",
  7070. original_opcode[0]);
  7071. opcode = original_opcode[0];
  7072. goto op_break_retry;
  7073. }
  7074. }
  7075. break;
  7076. }
  7077. #endif
  7078. default:
  7079. return false;
  7080. }
  7081. }
  7082. (void)u8;
  7083. (void)exec_env;
  7084. return false;
  7085. fail:
  7086. return false;
  7087. }
  7088. #if WASM_ENABLE_FAST_INTERP != 0
  7089. #if WASM_DEBUG_PREPROCESSOR != 0
  7090. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7091. #else
  7092. #define LOG_OP(...) (void)0
  7093. #endif
  7094. #define PATCH_ELSE 0
  7095. #define PATCH_END 1
  7096. typedef struct BranchBlockPatch {
  7097. struct BranchBlockPatch *next;
  7098. uint8 patch_type;
  7099. uint8 *code_compiled;
  7100. } BranchBlockPatch;
  7101. #endif
  7102. typedef struct BranchBlock {
  7103. uint8 label_type;
  7104. BlockType block_type;
  7105. uint8 *start_addr;
  7106. uint8 *else_addr;
  7107. uint8 *end_addr;
  7108. uint32 stack_cell_num;
  7109. #if WASM_ENABLE_GC != 0
  7110. uint32 reftype_map_num;
  7111. /* Indicate which local is used inside current block, used to validate
  7112. * local.get with non-nullable ref types */
  7113. uint8 *local_use_mask;
  7114. uint32 local_use_mask_size;
  7115. #endif
  7116. #if WASM_ENABLE_FAST_INTERP != 0
  7117. uint16 dynamic_offset;
  7118. uint8 *code_compiled;
  7119. BranchBlockPatch *patch_list;
  7120. /* This is used to save params frame_offset of of if block */
  7121. int16 *param_frame_offsets;
  7122. /* This is used to recover the dynamic offset for else branch,
  7123. * and also to remember the start offset of dynamic space which
  7124. * stores the block arguments for loop block, so we can use it
  7125. * to copy the stack operands to the loop block's arguments in
  7126. * wasm_loader_emit_br_info for opcode br. */
  7127. uint16 start_dynamic_offset;
  7128. #endif
  7129. /* Indicate the operand stack is in polymorphic state.
  7130. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7131. * to polymorphic state until the block's 'end' opcode is processed.
  7132. * If stack is in polymorphic state and stack is empty, instruction can
  7133. * pop any type of value directly without decreasing stack top pointer
  7134. * and stack cell num. */
  7135. bool is_stack_polymorphic;
  7136. } BranchBlock;
  7137. typedef struct WASMLoaderContext {
  7138. /* frame ref stack */
  7139. uint8 *frame_ref;
  7140. uint8 *frame_ref_bottom;
  7141. uint8 *frame_ref_boundary;
  7142. uint32 frame_ref_size;
  7143. uint32 stack_cell_num;
  7144. uint32 max_stack_cell_num;
  7145. #if WASM_ENABLE_GC != 0
  7146. /* frame reftype map stack */
  7147. WASMRefTypeMap *frame_reftype_map;
  7148. WASMRefTypeMap *frame_reftype_map_bottom;
  7149. WASMRefTypeMap *frame_reftype_map_boundary;
  7150. uint32 frame_reftype_map_size;
  7151. uint32 reftype_map_num;
  7152. uint32 max_reftype_map_num;
  7153. /* Current module */
  7154. WASMModule *module;
  7155. /* Current module's ref_type_set */
  7156. HashMap *ref_type_set;
  7157. /* Always point to local variable ref_type of
  7158. wasm_loader_prepare_bytecode */
  7159. WASMRefType *ref_type_tmp;
  7160. #endif
  7161. /* frame csp stack */
  7162. BranchBlock *frame_csp;
  7163. BranchBlock *frame_csp_bottom;
  7164. BranchBlock *frame_csp_boundary;
  7165. uint32 frame_csp_size;
  7166. uint32 csp_num;
  7167. uint32 max_csp_num;
  7168. #if WASM_ENABLE_FAST_INTERP != 0
  7169. /* frame offset stack */
  7170. int16 *frame_offset;
  7171. int16 *frame_offset_bottom;
  7172. int16 *frame_offset_boundary;
  7173. uint32 frame_offset_size;
  7174. int16 dynamic_offset;
  7175. int16 start_dynamic_offset;
  7176. int16 max_dynamic_offset;
  7177. /* preserved local offset */
  7178. int16 preserved_local_offset;
  7179. /* const buffer for i64 and f64 consts, note that the raw bytes
  7180. * of i64 and f64 are the same, so we read an i64 value from an
  7181. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7182. int64 *i64_consts;
  7183. uint32 i64_const_max_num;
  7184. uint32 i64_const_num;
  7185. /* const buffer for i32 and f32 consts */
  7186. int32 *i32_consts;
  7187. uint32 i32_const_max_num;
  7188. uint32 i32_const_num;
  7189. /* const buffer for V128 */
  7190. V128 *v128_consts;
  7191. uint32 v128_const_max_num;
  7192. uint32 v128_const_num;
  7193. /* processed code */
  7194. uint8 *p_code_compiled;
  7195. uint8 *p_code_compiled_end;
  7196. uint32 code_compiled_size;
  7197. /* If the last opcode will be dropped, the peak memory usage will be larger
  7198. * than the final code_compiled_size, we record the peak size to ensure
  7199. * there will not be invalid memory access during second traverse */
  7200. uint32 code_compiled_peak_size;
  7201. #endif
  7202. } WASMLoaderContext;
  7203. #define CHECK_CSP_PUSH() \
  7204. do { \
  7205. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7206. MEM_REALLOC( \
  7207. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7208. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7209. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7210. ctx->frame_csp_boundary = \
  7211. ctx->frame_csp_bottom \
  7212. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7213. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7214. } \
  7215. } while (0)
  7216. #define CHECK_CSP_POP() \
  7217. do { \
  7218. if (ctx->csp_num < 1) { \
  7219. set_error_buf(error_buf, error_buf_size, \
  7220. "type mismatch: " \
  7221. "expect data but block stack was empty"); \
  7222. goto fail; \
  7223. } \
  7224. } while (0)
  7225. #if WASM_ENABLE_FAST_INTERP != 0
  7226. static bool
  7227. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7228. uint32 error_buf_size)
  7229. {
  7230. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7231. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7232. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7233. ctx->frame_offset_size + 16);
  7234. ctx->frame_offset_size += 16;
  7235. ctx->frame_offset_boundary =
  7236. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7237. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7238. }
  7239. return true;
  7240. fail:
  7241. return false;
  7242. }
  7243. static bool
  7244. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7245. {
  7246. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7247. return false;
  7248. return true;
  7249. }
  7250. static void
  7251. free_label_patch_list(BranchBlock *frame_csp)
  7252. {
  7253. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7254. BranchBlockPatch *next;
  7255. while (label_patch != NULL) {
  7256. next = label_patch->next;
  7257. wasm_runtime_free(label_patch);
  7258. label_patch = next;
  7259. }
  7260. frame_csp->patch_list = NULL;
  7261. }
  7262. static void
  7263. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7264. {
  7265. BranchBlock *tmp_csp = frame_csp;
  7266. uint32 i;
  7267. for (i = 0; i < csp_num; i++) {
  7268. free_label_patch_list(tmp_csp);
  7269. tmp_csp++;
  7270. }
  7271. }
  7272. static void
  7273. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7274. {
  7275. BranchBlock *tmp_csp = frame_csp;
  7276. uint32 i;
  7277. for (i = 0; i < csp_num; i++) {
  7278. if (tmp_csp->param_frame_offsets)
  7279. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7280. tmp_csp++;
  7281. }
  7282. }
  7283. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7284. #if WASM_ENABLE_GC != 0
  7285. static bool
  7286. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7287. char *error_buf, uint32 error_buf_size)
  7288. {
  7289. BranchBlock *current_csp = ctx->frame_csp - 1;
  7290. uint32 local_mask_size;
  7291. if (local_count == 0) {
  7292. current_csp->local_use_mask_size = 0;
  7293. return true;
  7294. }
  7295. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7296. * else branch, we don't need to allocate memory again */
  7297. if (!current_csp->local_use_mask) {
  7298. local_mask_size = (local_count + 7) / sizeof(uint8);
  7299. if (!(current_csp->local_use_mask =
  7300. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7301. return false;
  7302. }
  7303. current_csp->local_use_mask_size = local_mask_size;
  7304. }
  7305. else {
  7306. local_mask_size = current_csp->local_use_mask_size;
  7307. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7308. }
  7309. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7310. /* For non-function blocks, inherit the use status from parent block */
  7311. BranchBlock *parent_csp = current_csp - 1;
  7312. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7313. bh_assert(parent_csp->local_use_mask);
  7314. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7315. parent_csp->local_use_mask, local_mask_size);
  7316. }
  7317. return true;
  7318. }
  7319. static void
  7320. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7321. {
  7322. BranchBlock *current_csp = ctx->frame_csp - 1;
  7323. bh_assert(current_csp->local_use_mask
  7324. || current_csp->local_use_mask_size == 0);
  7325. if (current_csp->local_use_mask) {
  7326. wasm_runtime_free(current_csp->local_use_mask);
  7327. }
  7328. current_csp->local_use_mask = NULL;
  7329. current_csp->local_use_mask_size = 0;
  7330. }
  7331. static void
  7332. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7333. {
  7334. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7335. uint32 i;
  7336. for (i = 0; i < ctx->csp_num; i++) {
  7337. if (tmp_csp->local_use_mask) {
  7338. wasm_runtime_free(tmp_csp->local_use_mask);
  7339. tmp_csp->local_use_mask = NULL;
  7340. tmp_csp->local_use_mask_size = 0;
  7341. }
  7342. tmp_csp++;
  7343. }
  7344. }
  7345. static void
  7346. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7347. {
  7348. BranchBlock *current_csp = ctx->frame_csp - 1;
  7349. uint32 byte_offset = index / sizeof(uint8);
  7350. uint32 bit_offset = index % sizeof(uint8);
  7351. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7352. bh_assert(current_csp->local_use_mask);
  7353. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7354. }
  7355. static bool
  7356. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7357. {
  7358. BranchBlock *current_csp = ctx->frame_csp - 1;
  7359. uint32 byte_offset = index / sizeof(uint8);
  7360. uint32 bit_offset = index % sizeof(uint8);
  7361. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7362. bh_assert(current_csp->local_use_mask);
  7363. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7364. ? true
  7365. : false;
  7366. }
  7367. #endif /* end of WASM_ENABLE_GC != 0 */
  7368. static void
  7369. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7370. {
  7371. if (ctx) {
  7372. if (ctx->frame_ref_bottom)
  7373. wasm_runtime_free(ctx->frame_ref_bottom);
  7374. #if WASM_ENABLE_GC != 0
  7375. if (ctx->frame_reftype_map_bottom)
  7376. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7377. #endif
  7378. if (ctx->frame_csp_bottom) {
  7379. #if WASM_ENABLE_FAST_INTERP != 0
  7380. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7381. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7382. ctx->csp_num);
  7383. #endif
  7384. #if WASM_ENABLE_GC != 0
  7385. wasm_loader_clean_all_local_use_masks(ctx);
  7386. #endif
  7387. wasm_runtime_free(ctx->frame_csp_bottom);
  7388. }
  7389. #if WASM_ENABLE_FAST_INTERP != 0
  7390. if (ctx->frame_offset_bottom)
  7391. wasm_runtime_free(ctx->frame_offset_bottom);
  7392. if (ctx->i64_consts)
  7393. wasm_runtime_free(ctx->i64_consts);
  7394. if (ctx->i32_consts)
  7395. wasm_runtime_free(ctx->i32_consts);
  7396. if (ctx->v128_consts)
  7397. wasm_runtime_free(ctx->v128_consts);
  7398. #endif
  7399. wasm_runtime_free(ctx);
  7400. }
  7401. }
  7402. static WASMLoaderContext *
  7403. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7404. {
  7405. WASMLoaderContext *loader_ctx =
  7406. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7407. if (!loader_ctx)
  7408. return NULL;
  7409. loader_ctx->frame_ref_size = 32;
  7410. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7411. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7412. goto fail;
  7413. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7414. #if WASM_ENABLE_GC != 0
  7415. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7416. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7417. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7418. error_buf_size)))
  7419. goto fail;
  7420. loader_ctx->frame_reftype_map_boundary =
  7421. loader_ctx->frame_reftype_map_bottom + 16;
  7422. #endif
  7423. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7424. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7425. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7426. goto fail;
  7427. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7428. #if WASM_ENABLE_EXCE_HANDLING != 0
  7429. func->exception_handler_count = 0;
  7430. #endif
  7431. #if WASM_ENABLE_FAST_INTERP != 0
  7432. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7433. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7434. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7435. error_buf_size)))
  7436. goto fail;
  7437. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7438. loader_ctx->i64_const_max_num = 8;
  7439. if (!(loader_ctx->i64_consts =
  7440. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7441. error_buf, error_buf_size)))
  7442. goto fail;
  7443. loader_ctx->i32_const_max_num = 8;
  7444. if (!(loader_ctx->i32_consts =
  7445. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7446. error_buf, error_buf_size)))
  7447. goto fail;
  7448. loader_ctx->v128_const_max_num = 8;
  7449. if (!(loader_ctx->v128_consts =
  7450. loader_malloc(sizeof(V128) * loader_ctx->v128_const_max_num,
  7451. error_buf, error_buf_size)))
  7452. goto fail;
  7453. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7454. set_error_buf(error_buf, error_buf_size,
  7455. "fast interpreter offset overflow");
  7456. goto fail;
  7457. }
  7458. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7459. loader_ctx->max_dynamic_offset =
  7460. func->param_cell_num + func->local_cell_num;
  7461. #endif
  7462. return loader_ctx;
  7463. fail:
  7464. wasm_loader_ctx_destroy(loader_ctx);
  7465. return NULL;
  7466. }
  7467. static bool
  7468. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7469. uint32 error_buf_size)
  7470. {
  7471. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7472. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7473. /* Increase the frame ref stack */
  7474. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7475. ctx->frame_ref_size + 16);
  7476. ctx->frame_ref_size += 16;
  7477. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7478. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7479. }
  7480. #if WASM_ENABLE_GC != 0
  7481. if (wasm_is_type_multi_byte_type(type)
  7482. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7483. /* Increase the frame reftype map stack */
  7484. bh_assert(
  7485. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7486. * sizeof(WASMRefTypeMap))
  7487. == ctx->frame_reftype_map_size);
  7488. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7489. ctx->frame_reftype_map_size
  7490. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7491. ctx->frame_reftype_map =
  7492. ctx->frame_reftype_map_bottom
  7493. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7494. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7495. ctx->frame_reftype_map_boundary =
  7496. ctx->frame_reftype_map_bottom
  7497. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7498. }
  7499. #endif
  7500. return true;
  7501. fail:
  7502. return false;
  7503. }
  7504. static bool
  7505. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7506. uint32 error_buf_size)
  7507. {
  7508. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7509. uint32 i;
  7510. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7511. return false;
  7512. #if WASM_ENABLE_GC != 0
  7513. if (wasm_is_type_multi_byte_type(type)) {
  7514. WASMRefType *ref_type;
  7515. if (!(ref_type =
  7516. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7517. error_buf, error_buf_size))) {
  7518. return false;
  7519. }
  7520. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7521. /* Increase the frame reftype map stack */
  7522. bh_assert((uint32)((ctx->frame_reftype_map
  7523. - ctx->frame_reftype_map_bottom)
  7524. * sizeof(WASMRefTypeMap))
  7525. == ctx->frame_reftype_map_size);
  7526. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7527. ctx->frame_reftype_map_size,
  7528. ctx->frame_reftype_map_size
  7529. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7530. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7531. + ctx->frame_reftype_map_size
  7532. / ((uint32)sizeof(WASMRefTypeMap));
  7533. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7534. ctx->frame_reftype_map_boundary =
  7535. ctx->frame_reftype_map_bottom
  7536. + ctx->frame_reftype_map_size
  7537. / ((uint32)sizeof(WASMRefTypeMap));
  7538. }
  7539. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7540. ctx->frame_reftype_map->ref_type = ref_type;
  7541. ctx->frame_reftype_map++;
  7542. ctx->reftype_map_num++;
  7543. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7544. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7545. }
  7546. #endif
  7547. for (i = 0; i < type_cell_num; i++)
  7548. *ctx->frame_ref++ = type;
  7549. ctx->stack_cell_num += type_cell_num;
  7550. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7551. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7552. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7553. set_error_buf(error_buf, error_buf_size,
  7554. "operand stack depth limit exceeded");
  7555. return false;
  7556. }
  7557. }
  7558. return true;
  7559. #if WASM_ENABLE_GC != 0
  7560. fail:
  7561. return false;
  7562. #endif
  7563. }
  7564. static bool
  7565. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7566. int32 stack_cell_num,
  7567. #if WASM_ENABLE_GC != 0
  7568. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7569. #endif
  7570. uint8 type,
  7571. #if WASM_ENABLE_GC != 0
  7572. WASMRefType *ref_type,
  7573. #endif
  7574. char *error_buf, uint32 error_buf_size)
  7575. {
  7576. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7577. #if WASM_ENABLE_GC != 0
  7578. WASMRefType *frame_reftype = NULL;
  7579. #endif
  7580. if (stack_cell_num < type_cell_num) {
  7581. set_error_buf(error_buf, error_buf_size,
  7582. "type mismatch: expect data but stack was empty");
  7583. return false;
  7584. }
  7585. #if WASM_ENABLE_GC == 0
  7586. for (i = 0; i < type_cell_num; i++) {
  7587. if (*(frame_ref - 1 - i) != type) {
  7588. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7589. "type mismatch: expect ", type2str(type),
  7590. " but got other");
  7591. return false;
  7592. }
  7593. }
  7594. #else
  7595. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7596. bh_assert(reftype_map_num > 0);
  7597. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7598. }
  7599. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7600. ref_type, ctx->module->types,
  7601. ctx->module->type_count)) {
  7602. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7603. "type mismatch: expect ", type2str(type),
  7604. " but got other");
  7605. return false;
  7606. }
  7607. for (i = 0; i < type_cell_num - 1; i++) {
  7608. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7609. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7610. "type mismatch: expect ", type2str(type),
  7611. " but got other");
  7612. return false;
  7613. }
  7614. }
  7615. #endif
  7616. return true;
  7617. }
  7618. static bool
  7619. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7620. uint32 error_buf_size)
  7621. {
  7622. int32 block_stack_cell_num =
  7623. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7624. #if WASM_ENABLE_GC != 0
  7625. int32 reftype_map_num =
  7626. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7627. #endif
  7628. if (block_stack_cell_num > 0) {
  7629. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7630. /* the stack top is a value of any type, return success */
  7631. return true;
  7632. }
  7633. #if WASM_ENABLE_GC != 0
  7634. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7635. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7636. WASMRefType *stack_top_ref_type = NULL;
  7637. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7638. bh_assert(reftype_map_num > 0);
  7639. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7640. }
  7641. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7642. ctx->ref_type_tmp, ctx->module->types,
  7643. ctx->module->type_count)) {
  7644. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7645. uint32 ref_type_struct_size =
  7646. wasm_reftype_struct_size(stack_top_ref_type);
  7647. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7648. stack_top_ref_type, ref_type_struct_size);
  7649. }
  7650. return true;
  7651. }
  7652. }
  7653. #endif
  7654. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7655. #if WASM_ENABLE_GC != 0
  7656. ctx->frame_reftype_map, reftype_map_num,
  7657. #endif
  7658. type,
  7659. #if WASM_ENABLE_GC != 0
  7660. ctx->ref_type_tmp,
  7661. #endif
  7662. error_buf, error_buf_size)) {
  7663. return false;
  7664. }
  7665. return true;
  7666. }
  7667. static bool
  7668. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7669. uint32 error_buf_size)
  7670. {
  7671. BranchBlock *cur_block = ctx->frame_csp - 1;
  7672. int32 available_stack_cell =
  7673. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7674. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7675. /* Directly return success if current block is in stack
  7676. polymorphic state while stack is empty. */
  7677. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7678. return true;
  7679. if (type == VALUE_TYPE_VOID)
  7680. return true;
  7681. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7682. return false;
  7683. bh_assert(available_stack_cell > 0);
  7684. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7685. type = VALUE_TYPE_ANY;
  7686. cell_num_to_pop = 1;
  7687. }
  7688. ctx->frame_ref -= cell_num_to_pop;
  7689. ctx->stack_cell_num -= cell_num_to_pop;
  7690. #if WASM_ENABLE_GC != 0
  7691. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7692. ctx->frame_reftype_map--;
  7693. ctx->reftype_map_num--;
  7694. }
  7695. #endif
  7696. return true;
  7697. }
  7698. #if WASM_ENABLE_GC != 0
  7699. /* Get the stack top element of current block */
  7700. static bool
  7701. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7702. WASMRefType **p_ref_type, char *error_buf,
  7703. uint32 error_buf_size)
  7704. {
  7705. BranchBlock *cur_block = ctx->frame_csp - 1;
  7706. int32 available_stack_cell =
  7707. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7708. if (available_stack_cell <= 0) {
  7709. /* Directly return success if current block is in stack
  7710. polymorphic state while stack is empty. */
  7711. if (cur_block->is_stack_polymorphic) {
  7712. *p_type = VALUE_TYPE_ANY;
  7713. return true;
  7714. }
  7715. else {
  7716. set_error_buf(
  7717. error_buf, error_buf_size,
  7718. "type mismatch: expect data but block stack was empty");
  7719. return false;
  7720. }
  7721. }
  7722. *p_type = *(ctx->frame_ref - 1);
  7723. if (wasm_is_type_multi_byte_type(*p_type)) {
  7724. int32 available_reftype_map =
  7725. (int32)(ctx->reftype_map_num
  7726. - (ctx->frame_csp - 1)->reftype_map_num);
  7727. bh_assert(available_reftype_map > 0);
  7728. (void)available_reftype_map;
  7729. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7730. }
  7731. return true;
  7732. }
  7733. #if WASM_ENABLE_FAST_INTERP != 0
  7734. static bool
  7735. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7736. char *error_buf, uint32 error_buf_size);
  7737. #endif
  7738. /* Check whether the stack top elem is a heap object, and if yes,
  7739. pop and return it */
  7740. static bool
  7741. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7742. WASMRefType *ref_ht_ret, char *error_buf,
  7743. uint32 error_buf_size)
  7744. {
  7745. uint8 type = 0;
  7746. WASMRefType *ref_type = NULL;
  7747. /* Get stack top element */
  7748. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7749. error_buf_size)) {
  7750. return false;
  7751. }
  7752. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7753. /* stack top isn't a ref type */
  7754. && !wasm_is_type_reftype(type)) {
  7755. set_error_buf(error_buf, error_buf_size,
  7756. "type mismatch: expect heap object but got others");
  7757. return false;
  7758. }
  7759. /* POP stack top */
  7760. if (wasm_is_type_multi_byte_type(type)) {
  7761. bh_assert(ref_type);
  7762. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7763. wasm_reftype_struct_size(ref_type));
  7764. }
  7765. #if WASM_ENABLE_FAST_INTERP != 0
  7766. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7767. error_buf_size)) {
  7768. return false;
  7769. }
  7770. #else
  7771. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7772. return false;
  7773. }
  7774. #endif
  7775. if (p_type)
  7776. *p_type = type;
  7777. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7778. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7779. wasm_reftype_struct_size(ref_type));
  7780. }
  7781. return true;
  7782. }
  7783. /* Check whether the stack top elem is subtype of (ref null ht),
  7784. and if yes, pop it and return the converted (ref ht) */
  7785. static bool
  7786. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7787. WASMRefType *ref_ht_ret, char *error_buf,
  7788. uint32 error_buf_size)
  7789. {
  7790. uint8 type = 0;
  7791. WASMRefType ref_type = { 0 };
  7792. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7793. error_buf_size)) {
  7794. return false;
  7795. }
  7796. /* Convert to related (ref ht) and return */
  7797. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7798. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7799. */
  7800. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7801. HEAP_TYPE_ARRAY
  7802. + (type - REF_TYPE_ARRAYREF));
  7803. type = ref_ht_ret->ref_type;
  7804. }
  7805. else if (wasm_is_reftype_htref_nullable(type)
  7806. || wasm_is_reftype_htref_non_nullable(type)) {
  7807. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7808. wasm_reftype_struct_size(&ref_type));
  7809. /* Convert to (ref ht) */
  7810. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7811. ref_ht_ret->ref_ht_common.nullable = false;
  7812. type = ref_ht_ret->ref_type;
  7813. }
  7814. *p_type = type;
  7815. return true;
  7816. }
  7817. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7818. and if yes, pop it and return the type_idx */
  7819. static bool
  7820. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7821. uint32 *p_type_idx, char *error_buf,
  7822. uint32 error_buf_size)
  7823. {
  7824. uint8 type = 0;
  7825. int32 type_idx = -1;
  7826. WASMRefType *ref_type = NULL;
  7827. /* Get stack top element */
  7828. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7829. error_buf_size)) {
  7830. return false;
  7831. }
  7832. if (type != VALUE_TYPE_ANY) {
  7833. /* stack top isn't (ref null $t) */
  7834. if (!((wasm_is_reftype_htref_nullable(type)
  7835. || wasm_is_reftype_htref_non_nullable(type))
  7836. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7837. set_error_buf(error_buf, error_buf_size,
  7838. "type mismatch: expect (ref null $t) but got others");
  7839. return false;
  7840. }
  7841. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7842. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7843. wasm_reftype_struct_size(ref_type));
  7844. }
  7845. /* POP stack top */
  7846. #if WASM_ENABLE_FAST_INTERP != 0
  7847. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7848. error_buf_size)) {
  7849. return false;
  7850. }
  7851. #else
  7852. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7853. return false;
  7854. }
  7855. #endif
  7856. /* Convert to type_idx and return */
  7857. *p_type = type;
  7858. if (type != VALUE_TYPE_ANY)
  7859. *p_type_idx = (uint32)type_idx;
  7860. return true;
  7861. }
  7862. #endif /* WASM_ENABLE_GC != 0 */
  7863. #if WASM_ENABLE_FAST_INTERP == 0
  7864. static bool
  7865. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7866. uint8 type_push, uint8 type_pop, char *error_buf,
  7867. uint32 error_buf_size)
  7868. {
  7869. for (int i = 0; i < pop_cnt; i++) {
  7870. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7871. error_buf_size))
  7872. return false;
  7873. }
  7874. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7875. return false;
  7876. return true;
  7877. }
  7878. #endif
  7879. static bool
  7880. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7881. BlockType block_type, uint8 *start_addr,
  7882. char *error_buf, uint32 error_buf_size)
  7883. {
  7884. CHECK_CSP_PUSH();
  7885. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7886. ctx->frame_csp->label_type = label_type;
  7887. ctx->frame_csp->block_type = block_type;
  7888. ctx->frame_csp->start_addr = start_addr;
  7889. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7890. #if WASM_ENABLE_GC != 0
  7891. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7892. #endif
  7893. #if WASM_ENABLE_FAST_INTERP != 0
  7894. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7895. ctx->frame_csp->patch_list = NULL;
  7896. #endif
  7897. ctx->frame_csp++;
  7898. ctx->csp_num++;
  7899. if (ctx->csp_num > ctx->max_csp_num) {
  7900. ctx->max_csp_num = ctx->csp_num;
  7901. if (ctx->max_csp_num > UINT16_MAX) {
  7902. set_error_buf(error_buf, error_buf_size,
  7903. "label stack depth limit exceeded");
  7904. return false;
  7905. }
  7906. }
  7907. return true;
  7908. fail:
  7909. return false;
  7910. }
  7911. static bool
  7912. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7913. uint32 error_buf_size)
  7914. {
  7915. CHECK_CSP_POP();
  7916. #if WASM_ENABLE_FAST_INTERP != 0
  7917. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7918. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7919. #endif
  7920. ctx->frame_csp--;
  7921. ctx->csp_num--;
  7922. return true;
  7923. fail:
  7924. return false;
  7925. }
  7926. #if WASM_ENABLE_FAST_INTERP != 0
  7927. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7928. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7929. #define emit_label(opcode) \
  7930. do { \
  7931. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7932. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7933. } while (0)
  7934. #define skip_label() \
  7935. do { \
  7936. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7937. LOG_OP("\ndelete last op\n"); \
  7938. } while (0)
  7939. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7940. #if UINTPTR_MAX == UINT64_MAX
  7941. #define emit_label(opcode) \
  7942. do { \
  7943. int32 offset = \
  7944. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7945. /* emit int32 relative offset in 64-bit target */ \
  7946. wasm_loader_emit_uint32(loader_ctx, offset); \
  7947. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7948. } while (0)
  7949. #else
  7950. #define emit_label(opcode) \
  7951. do { \
  7952. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7953. /* emit uint32 label address in 32-bit target */ \
  7954. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7955. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7956. } while (0)
  7957. #endif
  7958. #define skip_label() \
  7959. do { \
  7960. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7961. LOG_OP("\ndelete last op\n"); \
  7962. } while (0)
  7963. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7964. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7965. #define emit_label(opcode) \
  7966. do { \
  7967. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7968. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7969. } while (0)
  7970. #define skip_label() \
  7971. do { \
  7972. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7973. LOG_OP("\ndelete last op\n"); \
  7974. } while (0)
  7975. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7976. #define emit_empty_label_addr_and_frame_ip(type) \
  7977. do { \
  7978. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7979. loader_ctx->p_code_compiled, error_buf, \
  7980. error_buf_size)) \
  7981. goto fail; \
  7982. /* label address, to be patched */ \
  7983. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7984. } while (0)
  7985. #define emit_br_info(frame_csp, is_br) \
  7986. do { \
  7987. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7988. error_buf_size)) \
  7989. goto fail; \
  7990. } while (0)
  7991. #define LAST_OP_OUTPUT_I32() \
  7992. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7993. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7994. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7995. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7996. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7997. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7998. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7999. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  8000. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  8001. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  8002. || (last_op == EXT_OP_COPY_STACK_TOP)
  8003. #define LAST_OP_OUTPUT_I64() \
  8004. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  8005. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  8006. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  8007. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  8008. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  8009. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  8010. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  8011. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  8012. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  8013. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  8014. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  8015. #define GET_CONST_OFFSET(type, val) \
  8016. do { \
  8017. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  8018. &operand_offset, error_buf, \
  8019. error_buf_size))) \
  8020. goto fail; \
  8021. } while (0)
  8022. #define GET_CONST_F32_OFFSET(type, fval) \
  8023. do { \
  8024. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8025. &operand_offset, error_buf, \
  8026. error_buf_size))) \
  8027. goto fail; \
  8028. } while (0)
  8029. #define GET_CONST_F64_OFFSET(type, fval) \
  8030. do { \
  8031. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8032. &operand_offset, error_buf, \
  8033. error_buf_size))) \
  8034. goto fail; \
  8035. } while (0)
  8036. #define emit_operand(ctx, offset) \
  8037. do { \
  8038. wasm_loader_emit_int16(ctx, offset); \
  8039. LOG_OP("%d\t", offset); \
  8040. } while (0)
  8041. #define emit_byte(ctx, byte) \
  8042. do { \
  8043. wasm_loader_emit_uint8(ctx, byte); \
  8044. LOG_OP("%d\t", byte); \
  8045. } while (0)
  8046. #define emit_uint32(ctx, value) \
  8047. do { \
  8048. wasm_loader_emit_uint32(ctx, value); \
  8049. LOG_OP("%d\t", value); \
  8050. } while (0)
  8051. #define emit_uint64(ctx, value) \
  8052. do { \
  8053. wasm_loader_emit_const(ctx, &value, false); \
  8054. LOG_OP("%lld\t", value); \
  8055. } while (0)
  8056. #define emit_float32(ctx, value) \
  8057. do { \
  8058. wasm_loader_emit_const(ctx, &value, true); \
  8059. LOG_OP("%f\t", value); \
  8060. } while (0)
  8061. #define emit_float64(ctx, value) \
  8062. do { \
  8063. wasm_loader_emit_const(ctx, &value, false); \
  8064. LOG_OP("%f\t", value); \
  8065. } while (0)
  8066. static bool
  8067. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8068. {
  8069. if (!(ctx->p_code_compiled =
  8070. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8071. return false;
  8072. ctx->p_code_compiled_end =
  8073. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8074. /* clean up frame ref */
  8075. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8076. ctx->frame_ref = ctx->frame_ref_bottom;
  8077. ctx->stack_cell_num = 0;
  8078. #if WASM_ENABLE_GC != 0
  8079. /* clean up reftype map */
  8080. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8081. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8082. ctx->reftype_map_num = 0;
  8083. #endif
  8084. /* clean up frame csp */
  8085. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8086. ctx->frame_csp = ctx->frame_csp_bottom;
  8087. ctx->csp_num = 0;
  8088. ctx->max_csp_num = 0;
  8089. /* clean up frame offset */
  8090. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8091. ctx->frame_offset = ctx->frame_offset_bottom;
  8092. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8093. /* init preserved local offsets */
  8094. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8095. /* const buf is reserved */
  8096. return true;
  8097. }
  8098. static void
  8099. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8100. {
  8101. ctx->code_compiled_size += size;
  8102. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8103. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8104. }
  8105. }
  8106. static void
  8107. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8108. {
  8109. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8110. if (ctx->p_code_compiled) {
  8111. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8112. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8113. #endif
  8114. bh_memcpy_s(ctx->p_code_compiled,
  8115. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8116. value, size);
  8117. ctx->p_code_compiled += size;
  8118. }
  8119. else {
  8120. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8121. bh_assert((ctx->code_compiled_size & 1) == 0);
  8122. #endif
  8123. increase_compiled_code_space(ctx, size);
  8124. }
  8125. }
  8126. static void
  8127. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8128. {
  8129. if (ctx->p_code_compiled) {
  8130. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8131. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8132. #endif
  8133. STORE_U32(ctx->p_code_compiled, value);
  8134. ctx->p_code_compiled += sizeof(uint32);
  8135. }
  8136. else {
  8137. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8138. bh_assert((ctx->code_compiled_size & 1) == 0);
  8139. #endif
  8140. increase_compiled_code_space(ctx, sizeof(uint32));
  8141. }
  8142. }
  8143. static void
  8144. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8145. {
  8146. if (ctx->p_code_compiled) {
  8147. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8148. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8149. #endif
  8150. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8151. ctx->p_code_compiled += sizeof(int16);
  8152. }
  8153. else {
  8154. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8155. bh_assert((ctx->code_compiled_size & 1) == 0);
  8156. #endif
  8157. increase_compiled_code_space(ctx, sizeof(uint16));
  8158. }
  8159. }
  8160. static void
  8161. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8162. {
  8163. if (ctx->p_code_compiled) {
  8164. *(ctx->p_code_compiled) = value;
  8165. ctx->p_code_compiled += sizeof(uint8);
  8166. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8167. ctx->p_code_compiled++;
  8168. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8169. #endif
  8170. }
  8171. else {
  8172. increase_compiled_code_space(ctx, sizeof(uint8));
  8173. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8174. increase_compiled_code_space(ctx, sizeof(uint8));
  8175. bh_assert((ctx->code_compiled_size & 1) == 0);
  8176. #endif
  8177. }
  8178. }
  8179. static void
  8180. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8181. {
  8182. if (ctx->p_code_compiled) {
  8183. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8184. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8185. #endif
  8186. STORE_PTR(ctx->p_code_compiled, value);
  8187. ctx->p_code_compiled += sizeof(void *);
  8188. }
  8189. else {
  8190. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8191. bh_assert((ctx->code_compiled_size & 1) == 0);
  8192. #endif
  8193. increase_compiled_code_space(ctx, sizeof(void *));
  8194. }
  8195. }
  8196. static void
  8197. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8198. {
  8199. if (ctx->p_code_compiled) {
  8200. ctx->p_code_compiled -= size;
  8201. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8202. if (size == sizeof(uint8)) {
  8203. ctx->p_code_compiled--;
  8204. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8205. }
  8206. #endif
  8207. }
  8208. else {
  8209. ctx->code_compiled_size -= size;
  8210. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8211. if (size == sizeof(uint8)) {
  8212. ctx->code_compiled_size--;
  8213. bh_assert((ctx->code_compiled_size & 1) == 0);
  8214. }
  8215. #endif
  8216. }
  8217. }
  8218. static bool
  8219. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8220. uint32 local_index, uint32 local_type,
  8221. bool *preserved, char *error_buf,
  8222. uint32 error_buf_size)
  8223. {
  8224. uint32 i = 0;
  8225. int16 preserved_offset = (int16)local_index;
  8226. *preserved = false;
  8227. while (i < loader_ctx->stack_cell_num) {
  8228. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8229. /* move previous local into dynamic space before a set/tee_local opcode
  8230. */
  8231. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8232. if (!(*preserved)) {
  8233. *preserved = true;
  8234. skip_label();
  8235. preserved_offset = loader_ctx->preserved_local_offset;
  8236. if (loader_ctx->p_code_compiled) {
  8237. bh_assert(preserved_offset != (int16)local_index);
  8238. }
  8239. if (is_32bit_type(local_type)) {
  8240. /* Only increase preserve offset in the second traversal */
  8241. if (loader_ctx->p_code_compiled)
  8242. loader_ctx->preserved_local_offset++;
  8243. emit_label(EXT_OP_COPY_STACK_TOP);
  8244. }
  8245. #if WASM_ENABLE_SIMDE != 0
  8246. else if (local_type == VALUE_TYPE_V128) {
  8247. if (loader_ctx->p_code_compiled)
  8248. loader_ctx->preserved_local_offset += 4;
  8249. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8250. }
  8251. #endif
  8252. else {
  8253. if (loader_ctx->p_code_compiled)
  8254. loader_ctx->preserved_local_offset += 2;
  8255. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8256. }
  8257. /* overflow */
  8258. if (preserved_offset > loader_ctx->preserved_local_offset) {
  8259. set_error_buf_v(error_buf, error_buf_size,
  8260. "too much local cells 0x%x",
  8261. loader_ctx->preserved_local_offset);
  8262. return false;
  8263. }
  8264. emit_operand(loader_ctx, local_index);
  8265. emit_operand(loader_ctx, preserved_offset);
  8266. emit_label(opcode);
  8267. }
  8268. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8269. }
  8270. if (cur_type == VALUE_TYPE_V128) {
  8271. i += 4;
  8272. }
  8273. else if (is_32bit_type(cur_type)) {
  8274. i++;
  8275. }
  8276. else {
  8277. i += 2;
  8278. }
  8279. }
  8280. (void)error_buf;
  8281. (void)error_buf_size;
  8282. return true;
  8283. }
  8284. static bool
  8285. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8286. char *error_buf, uint32 error_buf_size)
  8287. {
  8288. uint32 i = 0;
  8289. bool preserve_local;
  8290. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8291. blocks will not influence the value of these locals */
  8292. while (i < loader_ctx->stack_cell_num) {
  8293. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8294. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8295. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8296. && (cur_offset >= 0)) {
  8297. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8298. cur_type, &preserve_local,
  8299. error_buf, error_buf_size)))
  8300. return false;
  8301. }
  8302. if (cur_type == VALUE_TYPE_V128) {
  8303. i += 4;
  8304. }
  8305. else if (is_32bit_type(cur_type)) {
  8306. i++;
  8307. }
  8308. else {
  8309. i += 2;
  8310. }
  8311. }
  8312. return true;
  8313. }
  8314. static bool
  8315. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8316. uint8 *p_code_compiled, char *error_buf,
  8317. uint32 error_buf_size)
  8318. {
  8319. BranchBlockPatch *patch =
  8320. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8321. if (!patch) {
  8322. return false;
  8323. }
  8324. patch->patch_type = patch_type;
  8325. patch->code_compiled = p_code_compiled;
  8326. if (!frame_csp->patch_list) {
  8327. frame_csp->patch_list = patch;
  8328. patch->next = NULL;
  8329. }
  8330. else {
  8331. patch->next = frame_csp->patch_list;
  8332. frame_csp->patch_list = patch;
  8333. }
  8334. return true;
  8335. }
  8336. static void
  8337. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8338. {
  8339. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8340. BranchBlockPatch *node = frame_csp->patch_list;
  8341. BranchBlockPatch *node_prev = NULL, *node_next;
  8342. if (!ctx->p_code_compiled)
  8343. return;
  8344. while (node) {
  8345. node_next = node->next;
  8346. if (node->patch_type == patch_type) {
  8347. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8348. if (node_prev == NULL) {
  8349. frame_csp->patch_list = node_next;
  8350. }
  8351. else {
  8352. node_prev->next = node_next;
  8353. }
  8354. wasm_runtime_free(node);
  8355. }
  8356. else {
  8357. node_prev = node;
  8358. }
  8359. node = node_next;
  8360. }
  8361. }
  8362. static bool
  8363. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8364. bool is_br, char *error_buf, uint32 error_buf_size)
  8365. {
  8366. /* br info layout:
  8367. * a) arity of target block
  8368. * b) total cell num of arity values
  8369. * c) each arity value's cell num
  8370. * d) each arity value's src frame offset
  8371. * e) each arity values's dst dynamic offset
  8372. * f) branch target address
  8373. *
  8374. * Note: b-e are omitted when arity is 0 so that
  8375. * interpreter can recover the br info quickly.
  8376. */
  8377. BlockType *block_type = &frame_csp->block_type;
  8378. uint8 *types = NULL, cell;
  8379. #if WASM_ENABLE_GC != 0
  8380. WASMRefTypeMap *reftype_maps;
  8381. uint32 reftype_map_count;
  8382. #endif
  8383. uint32 arity = 0;
  8384. int32 i;
  8385. int16 *frame_offset = ctx->frame_offset;
  8386. uint16 dynamic_offset;
  8387. /* Note: loop's arity is different from if and block. loop's arity is
  8388. * its parameter count while if and block arity is result count.
  8389. */
  8390. #if WASM_ENABLE_GC == 0
  8391. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8392. arity = block_type_get_param_types(block_type, &types);
  8393. else
  8394. arity = block_type_get_result_types(block_type, &types);
  8395. #else
  8396. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8397. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8398. &reftype_map_count);
  8399. else
  8400. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8401. &reftype_map_count);
  8402. #endif
  8403. /* Part a */
  8404. emit_uint32(ctx, arity);
  8405. if (arity) {
  8406. /* Part b */
  8407. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8408. /* Part c */
  8409. for (i = (int32)arity - 1; i >= 0; i--) {
  8410. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8411. emit_byte(ctx, cell);
  8412. }
  8413. /* Part d */
  8414. for (i = (int32)arity - 1; i >= 0; i--) {
  8415. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8416. frame_offset -= cell;
  8417. emit_operand(ctx, *(int16 *)(frame_offset));
  8418. }
  8419. /* Part e */
  8420. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8421. /* Use start_dynamic_offset which was set in
  8422. copy_params_to_dynamic_space */
  8423. dynamic_offset = frame_csp->start_dynamic_offset
  8424. + wasm_get_cell_num(types, arity);
  8425. else
  8426. dynamic_offset =
  8427. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8428. if (is_br)
  8429. ctx->dynamic_offset = dynamic_offset;
  8430. for (i = (int32)arity - 1; i >= 0; i--) {
  8431. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8432. dynamic_offset -= cell;
  8433. emit_operand(ctx, dynamic_offset);
  8434. }
  8435. }
  8436. /* Part f */
  8437. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8438. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8439. }
  8440. else {
  8441. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8442. error_buf, error_buf_size))
  8443. return false;
  8444. /* label address, to be patched */
  8445. wasm_loader_emit_ptr(ctx, NULL);
  8446. }
  8447. return true;
  8448. }
  8449. static bool
  8450. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8451. bool disable_emit, int16 operand_offset,
  8452. char *error_buf, uint32 error_buf_size)
  8453. {
  8454. uint32 cell_num_to_push, i;
  8455. if (type == VALUE_TYPE_VOID)
  8456. return true;
  8457. /* only check memory overflow in first traverse */
  8458. if (ctx->p_code_compiled == NULL) {
  8459. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8460. return false;
  8461. }
  8462. if (disable_emit)
  8463. *(ctx->frame_offset)++ = operand_offset;
  8464. else {
  8465. emit_operand(ctx, ctx->dynamic_offset);
  8466. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8467. ctx->dynamic_offset++;
  8468. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8469. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8470. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8471. goto fail;
  8472. }
  8473. }
  8474. }
  8475. if (is_32bit_type(type))
  8476. return true;
  8477. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8478. for (i = 0; i < cell_num_to_push; i++) {
  8479. if (ctx->p_code_compiled == NULL) {
  8480. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8481. return false;
  8482. }
  8483. ctx->frame_offset++;
  8484. if (!disable_emit) {
  8485. ctx->dynamic_offset++;
  8486. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8487. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8488. if (ctx->max_dynamic_offset >= INT16_MAX)
  8489. goto fail;
  8490. }
  8491. }
  8492. }
  8493. return true;
  8494. fail:
  8495. set_error_buf(error_buf, error_buf_size,
  8496. "fast interpreter offset overflow");
  8497. return false;
  8498. }
  8499. /* This function should be in front of wasm_loader_pop_frame_ref
  8500. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8501. will be modified by wasm_loader_pop_frame_ref */
  8502. static bool
  8503. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8504. char *error_buf, uint32 error_buf_size)
  8505. {
  8506. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8507. then current block is the function block */
  8508. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8509. BranchBlock *cur_block = ctx->frame_csp - depth;
  8510. int32 available_stack_cell =
  8511. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8512. uint32 cell_num_to_pop;
  8513. /* Directly return success if current block is in stack
  8514. polymorphic state while stack is empty. */
  8515. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8516. return true;
  8517. if (type == VALUE_TYPE_VOID)
  8518. return true;
  8519. /* Change type to ANY when the stack top is ANY, so as to avoid
  8520. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8521. two offsets */
  8522. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8523. type = VALUE_TYPE_ANY;
  8524. cell_num_to_pop = wasm_value_type_cell_num(type);
  8525. /* Check the offset stack bottom to ensure the frame offset
  8526. stack will not go underflow. But we don't thrown error
  8527. and return true here, because the error msg should be
  8528. given in wasm_loader_pop_frame_ref */
  8529. if (!check_offset_pop(ctx, cell_num_to_pop))
  8530. return true;
  8531. ctx->frame_offset -= cell_num_to_pop;
  8532. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8533. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8534. ctx->dynamic_offset -= cell_num_to_pop;
  8535. emit_operand(ctx, *(ctx->frame_offset));
  8536. (void)error_buf;
  8537. (void)error_buf_size;
  8538. return true;
  8539. }
  8540. static bool
  8541. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8542. bool disable_emit, int16 operand_offset,
  8543. char *error_buf, uint32 error_buf_size)
  8544. {
  8545. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8546. error_buf, error_buf_size)))
  8547. return false;
  8548. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8549. return false;
  8550. return true;
  8551. }
  8552. static bool
  8553. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8554. char *error_buf, uint32 error_buf_size)
  8555. {
  8556. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8557. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8558. return false;
  8559. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8560. return false;
  8561. return true;
  8562. }
  8563. static bool
  8564. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8565. uint8 type_push, uint8 type_pop,
  8566. bool disable_emit, int16 operand_offset,
  8567. char *error_buf, uint32 error_buf_size)
  8568. {
  8569. uint8 i;
  8570. for (i = 0; i < pop_cnt; i++) {
  8571. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8572. error_buf_size))
  8573. return false;
  8574. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8575. error_buf_size))
  8576. return false;
  8577. }
  8578. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8579. operand_offset, error_buf,
  8580. error_buf_size))
  8581. return false;
  8582. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8583. return false;
  8584. return true;
  8585. }
  8586. static int
  8587. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  8588. {
  8589. int64 i64_const1 = *(int64 *)p_i64_const1;
  8590. int64 i64_const2 = *(int64 *)p_i64_const2;
  8591. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  8592. }
  8593. static int
  8594. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  8595. {
  8596. int32 i32_const1 = *(int32 *)p_i32_const1;
  8597. int32 i32_const2 = *(int32 *)p_i32_const2;
  8598. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  8599. }
  8600. static int
  8601. cmp_v128_const(const void *p_v128_const1, const void *p_v128_const2)
  8602. {
  8603. V128 v128_const1 = *(V128 *)p_v128_const1;
  8604. V128 v128_const2 = *(V128 *)p_v128_const2;
  8605. return memcmp(&v128_const1, &v128_const2, sizeof(V128));
  8606. }
  8607. static bool
  8608. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8609. int16 *offset, char *error_buf,
  8610. uint32 error_buf_size)
  8611. {
  8612. if (!ctx->p_code_compiled) {
  8613. /* Treat i64 and f64 as the same by reading i64 value from
  8614. the raw bytes */
  8615. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8616. /* No slot left, emit const instead */
  8617. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  8618. *offset = 0;
  8619. return true;
  8620. }
  8621. /* Traverse the list if the const num is small */
  8622. if (ctx->i64_const_num < 10) {
  8623. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  8624. if (ctx->i64_consts[i] == *(int64 *)value) {
  8625. *offset = -1;
  8626. return true;
  8627. }
  8628. }
  8629. }
  8630. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  8631. MEM_REALLOC(ctx->i64_consts,
  8632. sizeof(int64) * ctx->i64_const_max_num,
  8633. sizeof(int64) * (ctx->i64_const_max_num * 2));
  8634. ctx->i64_const_max_num *= 2;
  8635. }
  8636. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  8637. }
  8638. else if (type == VALUE_TYPE_V128) {
  8639. /* No slot left, emit const instead */
  8640. if (ctx->v128_const_num * 4 > INT16_MAX - 2) {
  8641. *offset = 0;
  8642. return true;
  8643. }
  8644. /* Traverse the list if the const num is small */
  8645. if (ctx->v128_const_num < 10) {
  8646. for (uint32 i = 0; i < ctx->v128_const_num; i++) {
  8647. if (memcmp(&ctx->v128_consts[i], value, sizeof(V128))
  8648. == 0) {
  8649. *offset = -1;
  8650. return true;
  8651. }
  8652. }
  8653. }
  8654. if (ctx->v128_const_num >= ctx->v128_const_max_num) {
  8655. MEM_REALLOC(ctx->v128_consts,
  8656. sizeof(V128) * ctx->v128_const_max_num,
  8657. sizeof(V128) * (ctx->v128_const_max_num * 2));
  8658. ctx->v128_const_max_num *= 2;
  8659. }
  8660. ctx->v128_consts[ctx->v128_const_num++] = *(V128 *)value;
  8661. }
  8662. else {
  8663. /* Treat i32 and f32 as the same by reading i32 value from
  8664. the raw bytes */
  8665. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  8666. /* No slot left, emit const instead */
  8667. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  8668. *offset = 0;
  8669. return true;
  8670. }
  8671. /* Traverse the list if the const num is small */
  8672. if (ctx->i32_const_num < 10) {
  8673. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  8674. if (ctx->i32_consts[i] == *(int32 *)value) {
  8675. *offset = -1;
  8676. return true;
  8677. }
  8678. }
  8679. }
  8680. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  8681. MEM_REALLOC(ctx->i32_consts,
  8682. sizeof(int32) * ctx->i32_const_max_num,
  8683. sizeof(int32) * (ctx->i32_const_max_num * 2));
  8684. ctx->i32_const_max_num *= 2;
  8685. }
  8686. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  8687. }
  8688. *offset = -1;
  8689. return true;
  8690. }
  8691. else {
  8692. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8693. int64 key = *(int64 *)value, *i64_const;
  8694. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  8695. sizeof(int64), cmp_i64_const);
  8696. if (!i64_const) { /* not found, emit const instead */
  8697. *offset = 0;
  8698. return true;
  8699. }
  8700. /* constant index is encoded as negative value */
  8701. *offset = -(int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  8702. + (int32)(i64_const - ctx->i64_consts) * 2;
  8703. }
  8704. else if (type == VALUE_TYPE_V128) {
  8705. V128 key = *(V128 *)value, *v128_const;
  8706. v128_const = bsearch(&key, ctx->v128_consts, ctx->v128_const_num,
  8707. sizeof(V128), cmp_v128_const);
  8708. if (!v128_const) { /* not found, emit const instead */
  8709. *offset = 0;
  8710. return true;
  8711. }
  8712. /* constant index is encoded as negative value */
  8713. *offset = -(int32)(ctx->v128_const_num)
  8714. + (int32)(v128_const - ctx->v128_consts);
  8715. }
  8716. else {
  8717. int32 key = *(int32 *)value, *i32_const;
  8718. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  8719. sizeof(int32), cmp_i32_const);
  8720. if (!i32_const) { /* not found, emit const instead */
  8721. *offset = 0;
  8722. return true;
  8723. }
  8724. /* constant index is encoded as negative value */
  8725. *offset = -(int32)(ctx->i32_const_num)
  8726. + (int32)(i32_const - ctx->i32_consts);
  8727. }
  8728. return true;
  8729. }
  8730. fail:
  8731. return false;
  8732. }
  8733. /*
  8734. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8735. -- Mostly used for the binary / compare operation
  8736. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8737. -- Mostly used in block / control instructions
  8738. The POP will always emit the offset on the top of the frame_offset stack
  8739. PUSH can be used in two ways:
  8740. 1. directly PUSH:
  8741. PUSH_XXX();
  8742. will allocate a dynamic space and emit
  8743. 2. silent PUSH:
  8744. operand_offset = xxx; disable_emit = true;
  8745. PUSH_XXX();
  8746. only push the frame_offset stack, no emit
  8747. */
  8748. #define TEMPLATE_PUSH(Type) \
  8749. do { \
  8750. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8751. disable_emit, operand_offset, \
  8752. error_buf, error_buf_size)) \
  8753. goto fail; \
  8754. } while (0)
  8755. #define TEMPLATE_PUSH_REF(Type) \
  8756. do { \
  8757. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8758. operand_offset, error_buf, \
  8759. error_buf_size)) \
  8760. goto fail; \
  8761. } while (0)
  8762. #define TEMPLATE_POP(Type) \
  8763. do { \
  8764. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8765. error_buf, error_buf_size)) \
  8766. goto fail; \
  8767. } while (0)
  8768. #define TEMPLATE_POP_REF(Type) \
  8769. do { \
  8770. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8771. error_buf_size)) \
  8772. goto fail; \
  8773. } while (0)
  8774. #define PUSH_OFFSET_TYPE(type) \
  8775. do { \
  8776. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8777. operand_offset, error_buf, \
  8778. error_buf_size))) \
  8779. goto fail; \
  8780. } while (0)
  8781. #define POP_OFFSET_TYPE(type) \
  8782. do { \
  8783. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8784. error_buf_size))) \
  8785. goto fail; \
  8786. } while (0)
  8787. #define POP_AND_PUSH(type_pop, type_push) \
  8788. do { \
  8789. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8790. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8791. operand_offset, error_buf, error_buf_size))) \
  8792. goto fail; \
  8793. } while (0)
  8794. /* type of POPs should be the same */
  8795. #define POP2_AND_PUSH(type_pop, type_push) \
  8796. do { \
  8797. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8798. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8799. operand_offset, error_buf, error_buf_size))) \
  8800. goto fail; \
  8801. } while (0)
  8802. #else /* WASM_ENABLE_FAST_INTERP */
  8803. #define TEMPLATE_PUSH(Type) \
  8804. do { \
  8805. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8806. error_buf, error_buf_size))) \
  8807. goto fail; \
  8808. } while (0)
  8809. #define TEMPLATE_PUSH_REF(Type) \
  8810. do { \
  8811. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8812. error_buf_size))) \
  8813. goto fail; \
  8814. } while (0)
  8815. #define TEMPLATE_POP(Type) \
  8816. do { \
  8817. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8818. error_buf, error_buf_size))) \
  8819. goto fail; \
  8820. } while (0)
  8821. #define TEMPLATE_POP_REF(Type) \
  8822. do { \
  8823. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8824. error_buf_size))) \
  8825. goto fail; \
  8826. } while (0)
  8827. #define POP_AND_PUSH(type_pop, type_push) \
  8828. do { \
  8829. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8830. type_pop, error_buf, \
  8831. error_buf_size))) \
  8832. goto fail; \
  8833. } while (0)
  8834. /* type of POPs should be the same */
  8835. #define POP2_AND_PUSH(type_pop, type_push) \
  8836. do { \
  8837. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8838. type_pop, error_buf, \
  8839. error_buf_size))) \
  8840. goto fail; \
  8841. } while (0)
  8842. #endif /* WASM_ENABLE_FAST_INTERP */
  8843. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8844. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8845. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8846. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8847. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8848. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8849. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8850. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8851. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8852. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8853. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8854. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  8855. #define POP_I32() TEMPLATE_POP(I32)
  8856. #define POP_F32() TEMPLATE_POP(F32)
  8857. #define POP_I64() TEMPLATE_POP(I64)
  8858. #define POP_F64() TEMPLATE_POP(F64)
  8859. #define POP_V128() TEMPLATE_POP(V128)
  8860. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8861. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8862. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8863. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8864. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  8865. #if WASM_ENABLE_FAST_INTERP != 0
  8866. static bool
  8867. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8868. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8869. {
  8870. int16 operand_offset = 0;
  8871. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8872. : loader_ctx->frame_csp;
  8873. BlockType *block_type = &block->block_type;
  8874. uint8 *return_types = NULL;
  8875. #if WASM_ENABLE_GC != 0
  8876. WASMRefTypeMap *reftype_maps = NULL;
  8877. uint32 reftype_map_count;
  8878. #endif
  8879. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8880. int32 i = 0;
  8881. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8882. *frame_offset_org = NULL;
  8883. #if WASM_ENABLE_GC == 0
  8884. return_count = block_type_get_result_types(block_type, &return_types);
  8885. #else
  8886. return_count = block_type_get_result_types(
  8887. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8888. #endif
  8889. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64/V128
  8890. * instead of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8891. if (return_count == 1) {
  8892. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8893. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8894. /* insert op_copy before else opcode */
  8895. if (opcode == WASM_OP_ELSE)
  8896. skip_label();
  8897. #if WASM_ENABLE_SIMDE != 0
  8898. if (cell == 4) {
  8899. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8900. }
  8901. #endif
  8902. if (cell <= 2) {
  8903. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8904. : EXT_OP_COPY_STACK_TOP_I64);
  8905. }
  8906. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8907. emit_operand(loader_ctx, block->dynamic_offset);
  8908. if (opcode == WASM_OP_ELSE) {
  8909. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8910. }
  8911. else {
  8912. loader_ctx->frame_offset -= cell;
  8913. loader_ctx->dynamic_offset = block->dynamic_offset;
  8914. PUSH_OFFSET_TYPE(return_types[0]);
  8915. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8916. }
  8917. if (opcode == WASM_OP_ELSE)
  8918. emit_label(opcode);
  8919. }
  8920. return true;
  8921. }
  8922. /* Copy stack top values to block's results which are in dynamic space.
  8923. * The instruction format:
  8924. * Part a: values count
  8925. * Part b: all values total cell num
  8926. * Part c: each value's cell_num, src offset and dst offset
  8927. * Part d: each value's src offset and dst offset
  8928. * Part e: each value's dst offset
  8929. */
  8930. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8931. dynamic_offset = dynamic_offset_org =
  8932. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8933. /* First traversal to get the count of values needed to be copied. */
  8934. for (i = (int32)return_count - 1; i >= 0; i--) {
  8935. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8936. if (frame_offset - cells < loader_ctx->frame_offset_bottom) {
  8937. set_error_buf(error_buf, error_buf_size, "frame offset underflow");
  8938. goto fail;
  8939. }
  8940. if (cells == 4) {
  8941. bool needs_copy = false;
  8942. int16 v128_dynamic = dynamic_offset - cells;
  8943. for (int j = 0; j < 4; j++) {
  8944. if (*(frame_offset - j - 1) != (v128_dynamic + j)) {
  8945. needs_copy = true;
  8946. break;
  8947. }
  8948. }
  8949. if (needs_copy) {
  8950. value_count++;
  8951. total_cel_num += cells;
  8952. }
  8953. frame_offset -= cells;
  8954. dynamic_offset = v128_dynamic;
  8955. }
  8956. else {
  8957. frame_offset -= cells;
  8958. dynamic_offset -= cells;
  8959. if (dynamic_offset != *frame_offset) {
  8960. value_count++;
  8961. total_cel_num += cells;
  8962. }
  8963. }
  8964. }
  8965. if (value_count) {
  8966. uint32 j = 0;
  8967. uint8 *emit_data = NULL, *cells = NULL;
  8968. int16 *src_offsets = NULL;
  8969. uint16 *dst_offsets = NULL;
  8970. uint64 size =
  8971. (uint64)value_count
  8972. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8973. /* Allocate memory for the emit data */
  8974. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8975. return false;
  8976. cells = emit_data;
  8977. src_offsets = (int16 *)(cells + value_count);
  8978. dst_offsets = (uint16 *)(src_offsets + value_count);
  8979. /* insert op_copy before else opcode */
  8980. if (opcode == WASM_OP_ELSE)
  8981. skip_label();
  8982. emit_label(EXT_OP_COPY_STACK_VALUES);
  8983. /* Part a) */
  8984. emit_uint32(loader_ctx, value_count);
  8985. /* Part b) */
  8986. emit_uint32(loader_ctx, total_cel_num);
  8987. /* Second traversal to get each value's cell num, src offset and dst
  8988. * offset. */
  8989. frame_offset = frame_offset_org;
  8990. dynamic_offset = dynamic_offset_org;
  8991. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8992. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8993. if (cell == 4) {
  8994. bool needs_copy = false;
  8995. int16 v128_dynamic = dynamic_offset - cell;
  8996. for (int k = 0; k < 4; k++) {
  8997. if (*(frame_offset - k - 1) != (v128_dynamic + k)) {
  8998. needs_copy = true;
  8999. break;
  9000. }
  9001. }
  9002. if (needs_copy) {
  9003. cells[j] = cell;
  9004. src_offsets[j] = *(frame_offset - cell);
  9005. dst_offsets[j] = v128_dynamic;
  9006. j++;
  9007. }
  9008. frame_offset -= cell;
  9009. dynamic_offset = v128_dynamic;
  9010. }
  9011. else {
  9012. frame_offset -= cell;
  9013. dynamic_offset -= cell;
  9014. if (dynamic_offset != *frame_offset) {
  9015. cells[j] = cell;
  9016. /* src offset */
  9017. src_offsets[j] = *frame_offset;
  9018. /* dst offset */
  9019. dst_offsets[j] = dynamic_offset;
  9020. j++;
  9021. }
  9022. }
  9023. if (opcode == WASM_OP_ELSE) {
  9024. if (cell == 4) {
  9025. for (int k = 0; k < cell; k++) {
  9026. *(frame_offset + k) = dynamic_offset + k;
  9027. }
  9028. }
  9029. else {
  9030. *frame_offset = dynamic_offset;
  9031. }
  9032. }
  9033. else {
  9034. loader_ctx->frame_offset = frame_offset;
  9035. loader_ctx->dynamic_offset = dynamic_offset;
  9036. if (!(wasm_loader_push_frame_offset(
  9037. loader_ctx, return_types[i], disable_emit,
  9038. operand_offset, error_buf, error_buf_size))) {
  9039. wasm_runtime_free(emit_data);
  9040. goto fail;
  9041. }
  9042. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9043. loader_ctx->frame_offset = frame_offset_org;
  9044. loader_ctx->dynamic_offset = dynamic_offset_org;
  9045. }
  9046. }
  9047. bh_assert(j == value_count);
  9048. /* Emit the cells, src_offsets and dst_offsets */
  9049. for (j = 0; j < value_count; j++)
  9050. emit_byte(loader_ctx, cells[j]);
  9051. for (j = 0; j < value_count; j++)
  9052. emit_operand(loader_ctx, src_offsets[j]);
  9053. for (j = 0; j < value_count; j++)
  9054. emit_operand(loader_ctx, dst_offsets[j]);
  9055. if (opcode == WASM_OP_ELSE)
  9056. emit_label(opcode);
  9057. wasm_runtime_free(emit_data);
  9058. }
  9059. return true;
  9060. fail:
  9061. return false;
  9062. }
  9063. #endif /* WASM_ENABLE_FAST_INTERP */
  9064. #define PUSH_TYPE(type) \
  9065. do { \
  9066. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  9067. error_buf_size))) \
  9068. goto fail; \
  9069. } while (0)
  9070. #define POP_TYPE(type) \
  9071. do { \
  9072. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  9073. error_buf_size))) \
  9074. goto fail; \
  9075. } while (0)
  9076. #if WASM_ENABLE_GC == 0
  9077. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9078. do { \
  9079. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9080. _start_addr, error_buf, \
  9081. error_buf_size)) \
  9082. goto fail; \
  9083. } while (0)
  9084. #define POP_CSP() \
  9085. do { \
  9086. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9087. goto fail; \
  9088. } while (0)
  9089. #else
  9090. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9091. do { \
  9092. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9093. _start_addr, error_buf, \
  9094. error_buf_size)) \
  9095. goto fail; \
  9096. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  9097. error_buf, error_buf_size)) { \
  9098. goto fail; \
  9099. } \
  9100. } while (0)
  9101. #define POP_CSP() \
  9102. do { \
  9103. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  9104. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9105. goto fail; \
  9106. } while (0)
  9107. #endif /* end of WASM_ENABLE_GC == 0 */
  9108. #if WASM_ENABLE_GC == 0
  9109. #define GET_LOCAL_REFTYPE() (void)0
  9110. #else
  9111. #define GET_LOCAL_REFTYPE() \
  9112. do { \
  9113. if (wasm_is_type_multi_byte_type(local_type)) { \
  9114. WASMRefType *_ref_type; \
  9115. if (local_idx < param_count) \
  9116. _ref_type = wasm_reftype_map_find( \
  9117. param_reftype_maps, param_reftype_map_count, local_idx); \
  9118. else \
  9119. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  9120. local_reftype_map_count, \
  9121. local_idx - param_count); \
  9122. bh_assert(_ref_type); \
  9123. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  9124. wasm_reftype_struct_size(_ref_type)); \
  9125. } \
  9126. } while (0)
  9127. #endif /* end of WASM_ENABLE_GC == 0 */
  9128. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  9129. do { \
  9130. read_leb_uint32(p, p_end, local_idx); \
  9131. if (local_idx >= param_count + local_count) { \
  9132. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  9133. goto fail; \
  9134. } \
  9135. local_type = local_idx < param_count \
  9136. ? param_types[local_idx] \
  9137. : local_types[local_idx - param_count]; \
  9138. local_offset = local_offsets[local_idx]; \
  9139. GET_LOCAL_REFTYPE(); \
  9140. } while (0)
  9141. static bool
  9142. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  9143. {
  9144. if (module->memory_count == 0 && module->import_memory_count == 0) {
  9145. set_error_buf(error_buf, error_buf_size, "unknown memory");
  9146. return false;
  9147. }
  9148. return true;
  9149. }
  9150. #define CHECK_MEMORY() \
  9151. do { \
  9152. if (!check_memory(module, error_buf, error_buf_size)) \
  9153. goto fail; \
  9154. } while (0)
  9155. static bool
  9156. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9157. uint32 error_buf_size)
  9158. {
  9159. uint8 mem_access_aligns[] = {
  9160. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9161. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9162. };
  9163. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9164. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9165. set_error_buf(error_buf, error_buf_size,
  9166. "invalid memop flags: alignment must not be larger "
  9167. "than natural");
  9168. return false;
  9169. }
  9170. return true;
  9171. }
  9172. #if WASM_ENABLE_SIMD != 0
  9173. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  9174. || (WASM_ENABLE_FAST_INTERP != 0)
  9175. static bool
  9176. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9177. uint32 error_buf_size)
  9178. {
  9179. uint8 mem_access_aligns[] = {
  9180. 4, /* load */
  9181. 3, 3, 3, 3, 3, 3, /* load and extend */
  9182. 0, 1, 2, 3, /* load and splat */
  9183. 4, /* store */
  9184. };
  9185. uint8 mem_access_aligns_load_lane[] = {
  9186. 0, 1, 2, 3, /* load lane */
  9187. 0, 1, 2, 3, /* store lane */
  9188. 2, 3 /* store zero */
  9189. };
  9190. if (!((opcode <= SIMD_v128_store)
  9191. || (SIMD_v128_load8_lane <= opcode
  9192. && opcode <= SIMD_v128_load64_zero))) {
  9193. set_error_buf(error_buf, error_buf_size,
  9194. "the opcode doesn't include memarg");
  9195. return false;
  9196. }
  9197. if ((opcode <= SIMD_v128_store
  9198. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9199. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9200. && align > mem_access_aligns_load_lane[opcode
  9201. - SIMD_v128_load8_lane])) {
  9202. set_error_buf(error_buf, error_buf_size,
  9203. "invalid memop flags: alignment must not be larger "
  9204. "than natural");
  9205. return false;
  9206. }
  9207. return true;
  9208. }
  9209. static bool
  9210. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9211. uint32 error_buf_size)
  9212. {
  9213. switch (opcode) {
  9214. case SIMD_i8x16_extract_lane_s:
  9215. case SIMD_i8x16_extract_lane_u:
  9216. case SIMD_i8x16_replace_lane:
  9217. if (lane >= 16) {
  9218. goto fail;
  9219. }
  9220. break;
  9221. case SIMD_i16x8_extract_lane_s:
  9222. case SIMD_i16x8_extract_lane_u:
  9223. case SIMD_i16x8_replace_lane:
  9224. if (lane >= 8) {
  9225. goto fail;
  9226. }
  9227. break;
  9228. case SIMD_i32x4_extract_lane:
  9229. case SIMD_i32x4_replace_lane:
  9230. case SIMD_f32x4_extract_lane:
  9231. case SIMD_f32x4_replace_lane:
  9232. if (lane >= 4) {
  9233. goto fail;
  9234. }
  9235. break;
  9236. case SIMD_i64x2_extract_lane:
  9237. case SIMD_i64x2_replace_lane:
  9238. case SIMD_f64x2_extract_lane:
  9239. case SIMD_f64x2_replace_lane:
  9240. if (lane >= 2) {
  9241. goto fail;
  9242. }
  9243. break;
  9244. case SIMD_v128_load8_lane:
  9245. case SIMD_v128_load16_lane:
  9246. case SIMD_v128_load32_lane:
  9247. case SIMD_v128_load64_lane:
  9248. case SIMD_v128_store8_lane:
  9249. case SIMD_v128_store16_lane:
  9250. case SIMD_v128_store32_lane:
  9251. case SIMD_v128_store64_lane:
  9252. case SIMD_v128_load32_zero:
  9253. case SIMD_v128_load64_zero:
  9254. {
  9255. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9256. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9257. goto fail;
  9258. }
  9259. break;
  9260. }
  9261. default:
  9262. goto fail;
  9263. }
  9264. return true;
  9265. fail:
  9266. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9267. return false;
  9268. }
  9269. static bool
  9270. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9271. {
  9272. uint8 i;
  9273. for (i = 0; i != 16; ++i) {
  9274. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9275. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9276. return false;
  9277. }
  9278. }
  9279. return true;
  9280. }
  9281. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9282. #endif /* end of WASM_ENABLE_SIMD */
  9283. #if WASM_ENABLE_SHARED_MEMORY != 0
  9284. static bool
  9285. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9286. uint32 error_buf_size)
  9287. {
  9288. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9289. uint8 mem_access_aligns[] = {
  9290. 2, 3, 0, 1, 0, 1, 2,
  9291. };
  9292. uint8 expect;
  9293. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9294. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9295. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9296. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9297. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9298. }
  9299. else {
  9300. /* 7 opcodes in every group */
  9301. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9302. }
  9303. if (align != expect) {
  9304. set_error_buf(error_buf, error_buf_size,
  9305. "alignment isn't equal to natural");
  9306. return false;
  9307. }
  9308. return true;
  9309. }
  9310. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9311. static bool
  9312. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9313. char *error_buf, uint32 error_buf_size)
  9314. {
  9315. BranchBlock *target_block, *cur_block;
  9316. BlockType *target_block_type;
  9317. uint8 type, *types = NULL, *frame_ref;
  9318. uint32 arity = 0;
  9319. int32 i, available_stack_cell;
  9320. uint16 cell_num;
  9321. #if WASM_ENABLE_GC != 0
  9322. WASMRefTypeMap *frame_reftype_map;
  9323. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9324. WASMRefType *ref_type;
  9325. uint32 reftype_map_count = 0;
  9326. int32 available_reftype_map;
  9327. bool is_type_multi_byte;
  9328. #endif
  9329. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9330. uint8 *frame_ref_after_popped = NULL;
  9331. uint8 frame_ref_tmp[4] = { 0 };
  9332. uint8 *frame_ref_buf = frame_ref_tmp;
  9333. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9334. #if WASM_ENABLE_GC != 0
  9335. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9336. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9337. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9338. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9339. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9340. #endif
  9341. #if WASM_ENABLE_FAST_INTERP != 0
  9342. int16 *frame_offset_old = loader_ctx->frame_offset;
  9343. int16 *frame_offset_after_popped = NULL;
  9344. int16 frame_offset_tmp[4] = { 0 };
  9345. int16 *frame_offset_buf = frame_offset_tmp;
  9346. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9347. #endif
  9348. bool ret = false;
  9349. bh_assert(loader_ctx->csp_num > 0);
  9350. if (loader_ctx->csp_num - 1 < depth) {
  9351. set_error_buf(error_buf, error_buf_size,
  9352. "unknown label, "
  9353. "unexpected end of section or function");
  9354. return false;
  9355. }
  9356. cur_block = loader_ctx->frame_csp - 1;
  9357. target_block = loader_ctx->frame_csp - (depth + 1);
  9358. target_block_type = &target_block->block_type;
  9359. frame_ref = loader_ctx->frame_ref;
  9360. #if WASM_ENABLE_GC != 0
  9361. frame_reftype_map = loader_ctx->frame_reftype_map;
  9362. #endif
  9363. /* Note: loop's arity is different from if and block. loop's arity is
  9364. * its parameter count while if and block arity is result count.
  9365. */
  9366. #if WASM_ENABLE_GC == 0
  9367. if (target_block->label_type == LABEL_TYPE_LOOP)
  9368. arity = block_type_get_param_types(target_block_type, &types);
  9369. else
  9370. arity = block_type_get_result_types(target_block_type, &types);
  9371. #else
  9372. if (target_block->label_type == LABEL_TYPE_LOOP)
  9373. arity = block_type_get_param_types(target_block_type, &types,
  9374. &reftype_maps, &reftype_map_count);
  9375. else
  9376. arity = block_type_get_result_types(target_block_type, &types,
  9377. &reftype_maps, &reftype_map_count);
  9378. #endif
  9379. /* If the stack is in polymorphic state, just clear the stack
  9380. * and then re-push the values to make the stack top values
  9381. * match block type. */
  9382. if (cur_block->is_stack_polymorphic) {
  9383. #if WASM_ENABLE_GC != 0
  9384. int32 j = reftype_map_count - 1;
  9385. #endif
  9386. for (i = (int32)arity - 1; i >= 0; i--) {
  9387. #if WASM_ENABLE_GC != 0
  9388. if (wasm_is_type_multi_byte_type(types[i])) {
  9389. bh_assert(reftype_maps[j].index == i);
  9390. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9391. reftype_maps[j].ref_type,
  9392. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9393. j--;
  9394. }
  9395. #endif
  9396. #if WASM_ENABLE_FAST_INTERP != 0
  9397. POP_OFFSET_TYPE(types[i]);
  9398. #endif
  9399. POP_TYPE(types[i]);
  9400. }
  9401. /* Backup stack data since it may be changed in the below
  9402. push operations, and the stack data may be used when
  9403. checking other target blocks of opcode br_table */
  9404. if (opcode == WASM_OP_BR_TABLE) {
  9405. uint64 total_size;
  9406. frame_ref_after_popped = loader_ctx->frame_ref;
  9407. total_size = (uint64)sizeof(uint8)
  9408. * (frame_ref_old - frame_ref_after_popped);
  9409. if (total_size > sizeof(frame_ref_tmp)
  9410. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9411. error_buf_size))) {
  9412. goto fail;
  9413. }
  9414. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9415. frame_ref_after_popped, (uint32)total_size);
  9416. #if WASM_ENABLE_GC != 0
  9417. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9418. total_size =
  9419. (uint64)sizeof(WASMRefTypeMap)
  9420. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9421. if (total_size > sizeof(frame_reftype_map_tmp)
  9422. && !(frame_reftype_map_buf = loader_malloc(
  9423. total_size, error_buf, error_buf_size))) {
  9424. goto fail;
  9425. }
  9426. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9427. frame_reftype_map_after_popped, (uint32)total_size);
  9428. #endif
  9429. #if WASM_ENABLE_FAST_INTERP != 0
  9430. frame_offset_after_popped = loader_ctx->frame_offset;
  9431. total_size = (uint64)sizeof(int16)
  9432. * (frame_offset_old - frame_offset_after_popped);
  9433. if (total_size > sizeof(frame_offset_tmp)
  9434. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9435. error_buf_size))) {
  9436. goto fail;
  9437. }
  9438. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9439. frame_offset_after_popped, (uint32)total_size);
  9440. #endif
  9441. }
  9442. #if WASM_ENABLE_GC != 0
  9443. j = 0;
  9444. #endif
  9445. for (i = 0; i < (int32)arity; i++) {
  9446. #if WASM_ENABLE_GC != 0
  9447. if (wasm_is_type_multi_byte_type(types[i])) {
  9448. bh_assert(reftype_maps[j].index == i);
  9449. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9450. reftype_maps[j].ref_type,
  9451. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9452. j++;
  9453. }
  9454. #endif
  9455. #if WASM_ENABLE_FAST_INTERP != 0
  9456. bool disable_emit = true;
  9457. int16 operand_offset = 0;
  9458. PUSH_OFFSET_TYPE(types[i]);
  9459. #endif
  9460. PUSH_TYPE(types[i]);
  9461. }
  9462. #if WASM_ENABLE_FAST_INTERP != 0
  9463. emit_br_info(target_block, opcode == WASM_OP_BR);
  9464. #endif
  9465. /* Restore the stack data, note that frame_ref_bottom,
  9466. frame_reftype_map_bottom, frame_offset_bottom may be
  9467. re-allocated in the above push operations */
  9468. if (opcode == WASM_OP_BR_TABLE) {
  9469. uint32 total_size;
  9470. /* The stack operand num should not be smaller than before
  9471. after pop and push operations */
  9472. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9473. loader_ctx->stack_cell_num = stack_cell_num_old;
  9474. loader_ctx->frame_ref =
  9475. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9476. total_size = (uint32)(sizeof(uint8)
  9477. * (frame_ref_old - frame_ref_after_popped));
  9478. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9479. frame_ref_buf, total_size);
  9480. #if WASM_ENABLE_GC != 0
  9481. /* The stack operand num should not be smaller than before
  9482. after pop and push operations */
  9483. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9484. loader_ctx->reftype_map_num = reftype_map_num_old;
  9485. loader_ctx->frame_reftype_map =
  9486. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9487. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9488. * (frame_reftype_map_old
  9489. - frame_reftype_map_after_popped));
  9490. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9491. total_size, frame_reftype_map_buf, total_size);
  9492. #endif
  9493. #if WASM_ENABLE_FAST_INTERP != 0
  9494. loader_ctx->frame_offset =
  9495. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9496. total_size =
  9497. (uint32)(sizeof(int16)
  9498. * (frame_offset_old - frame_offset_after_popped));
  9499. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9500. total_size, frame_offset_buf, total_size);
  9501. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9502. #endif
  9503. }
  9504. ret = true;
  9505. goto cleanup_and_return;
  9506. }
  9507. available_stack_cell =
  9508. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9509. #if WASM_ENABLE_GC != 0
  9510. available_reftype_map =
  9511. (int32)(loader_ctx->reftype_map_num
  9512. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9513. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9514. #endif
  9515. /* Check stack top values match target block type */
  9516. for (i = (int32)arity - 1; i >= 0; i--) {
  9517. type = types[i];
  9518. #if WASM_ENABLE_GC != 0
  9519. ref_type = NULL;
  9520. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9521. if (is_type_multi_byte) {
  9522. bh_assert(reftype_map);
  9523. ref_type = reftype_map->ref_type;
  9524. }
  9525. #endif
  9526. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9527. break;
  9528. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9529. #if WASM_ENABLE_GC != 0
  9530. frame_reftype_map, available_reftype_map,
  9531. #endif
  9532. type,
  9533. #if WASM_ENABLE_GC != 0
  9534. ref_type,
  9535. #endif
  9536. error_buf, error_buf_size)) {
  9537. goto fail;
  9538. }
  9539. cell_num = wasm_value_type_cell_num(types[i]);
  9540. frame_ref -= cell_num;
  9541. available_stack_cell -= cell_num;
  9542. #if WASM_ENABLE_GC != 0
  9543. if (is_type_multi_byte) {
  9544. frame_reftype_map--;
  9545. available_reftype_map--;
  9546. reftype_map--;
  9547. }
  9548. #endif
  9549. }
  9550. #if WASM_ENABLE_FAST_INTERP != 0
  9551. emit_br_info(target_block, opcode == WASM_OP_BR);
  9552. #endif
  9553. ret = true;
  9554. cleanup_and_return:
  9555. fail:
  9556. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9557. wasm_runtime_free(frame_ref_buf);
  9558. #if WASM_ENABLE_GC != 0
  9559. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9560. wasm_runtime_free(frame_reftype_map_buf);
  9561. #endif
  9562. #if WASM_ENABLE_FAST_INTERP != 0
  9563. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9564. wasm_runtime_free(frame_offset_buf);
  9565. #endif
  9566. return ret;
  9567. }
  9568. static BranchBlock *
  9569. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9570. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9571. {
  9572. uint8 *p = *p_buf, *p_end = buf_end;
  9573. BranchBlock *frame_csp_tmp;
  9574. uint32 depth;
  9575. read_leb_uint32(p, p_end, depth);
  9576. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9577. error_buf_size)) {
  9578. goto fail;
  9579. }
  9580. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9581. *p_buf = p;
  9582. return frame_csp_tmp;
  9583. fail:
  9584. return NULL;
  9585. }
  9586. #if WASM_ENABLE_EXCE_HANDLING != 0
  9587. static BranchBlock *
  9588. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9589. uint8 *buf_end, char *error_buf,
  9590. uint32 error_buf_size)
  9591. {
  9592. uint8 *p = *p_buf, *p_end = buf_end;
  9593. BranchBlock *frame_csp_tmp;
  9594. uint32 depth;
  9595. read_leb_uint32(p, p_end, depth);
  9596. /*
  9597. * Note: "delegate 0" means the surrounding block, not the
  9598. * try-delegate block itself.
  9599. *
  9600. * Note: the caller hasn't popped the try-delegate frame yet.
  9601. */
  9602. bh_assert(loader_ctx->csp_num > 0);
  9603. if (loader_ctx->csp_num - 1 <= depth) {
  9604. #if WASM_ENABLE_SPEC_TEST == 0
  9605. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9606. #else
  9607. set_error_buf(error_buf, error_buf_size, "unknown label");
  9608. #endif
  9609. goto fail;
  9610. }
  9611. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9612. #if WASM_ENABLE_FAST_INTERP != 0
  9613. emit_br_info(frame_csp_tmp, false);
  9614. #endif
  9615. *p_buf = p;
  9616. return frame_csp_tmp;
  9617. fail:
  9618. return NULL;
  9619. }
  9620. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9621. static bool
  9622. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9623. char *error_buf, uint32 error_buf_size)
  9624. {
  9625. BlockType *block_type = &block->block_type;
  9626. uint8 *return_types = NULL;
  9627. uint32 return_count = 0;
  9628. int32 available_stack_cell, return_cell_num, i;
  9629. uint8 *frame_ref = NULL;
  9630. #if WASM_ENABLE_GC != 0
  9631. WASMRefTypeMap *frame_reftype_map;
  9632. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9633. WASMRefType *ref_type;
  9634. uint32 param_count, return_reftype_map_count = 0;
  9635. int32 available_reftype_map =
  9636. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9637. #endif
  9638. available_stack_cell =
  9639. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9640. #if WASM_ENABLE_GC == 0
  9641. return_count = block_type_get_result_types(block_type, &return_types);
  9642. #else
  9643. return_count = block_type_get_result_types(block_type, &return_types,
  9644. &return_reftype_maps,
  9645. &return_reftype_map_count);
  9646. param_count =
  9647. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9648. (void)param_count;
  9649. #endif
  9650. return_cell_num =
  9651. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9652. /* If the stack is in polymorphic state, just clear the stack
  9653. * and then re-push the values to make the stack top values
  9654. * match block type. */
  9655. if (block->is_stack_polymorphic) {
  9656. #if WASM_ENABLE_GC != 0
  9657. int32 j = return_reftype_map_count - 1;
  9658. #endif
  9659. for (i = (int32)return_count - 1; i >= 0; i--) {
  9660. #if WASM_ENABLE_GC != 0
  9661. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9662. bh_assert(return_reftype_maps[j].index == i + param_count);
  9663. bh_memcpy_s(
  9664. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9665. return_reftype_maps[j].ref_type,
  9666. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9667. j--;
  9668. }
  9669. #endif
  9670. #if WASM_ENABLE_FAST_INTERP != 0
  9671. POP_OFFSET_TYPE(return_types[i]);
  9672. #endif
  9673. POP_TYPE(return_types[i]);
  9674. }
  9675. /* Check stack is empty */
  9676. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9677. set_error_buf(
  9678. error_buf, error_buf_size,
  9679. "type mismatch: stack size does not match block type");
  9680. goto fail;
  9681. }
  9682. #if WASM_ENABLE_GC != 0
  9683. j = 0;
  9684. #endif
  9685. for (i = 0; i < (int32)return_count; i++) {
  9686. #if WASM_ENABLE_GC != 0
  9687. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9688. bh_assert(return_reftype_maps[j].index == i + param_count);
  9689. bh_memcpy_s(
  9690. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9691. return_reftype_maps[j].ref_type,
  9692. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9693. j++;
  9694. }
  9695. #endif
  9696. #if WASM_ENABLE_FAST_INTERP != 0
  9697. bool disable_emit = true;
  9698. int16 operand_offset = 0;
  9699. PUSH_OFFSET_TYPE(return_types[i]);
  9700. #endif
  9701. PUSH_TYPE(return_types[i]);
  9702. }
  9703. return true;
  9704. }
  9705. if (available_stack_cell != return_cell_num) {
  9706. #if WASM_ENABLE_EXCE_HANDLING != 0
  9707. /* testspec: this error message format is expected by try_catch.wast */
  9708. snprintf(
  9709. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9710. block->label_type == LABEL_TYPE_TRY
  9711. || (block->label_type == LABEL_TYPE_CATCH
  9712. && return_cell_num > 0)
  9713. ? "instruction"
  9714. : "block",
  9715. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9716. " but stack has ",
  9717. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9718. : "");
  9719. goto fail;
  9720. #else
  9721. set_error_buf(error_buf, error_buf_size,
  9722. "type mismatch: stack size does not match block type");
  9723. goto fail;
  9724. #endif
  9725. }
  9726. /* Check stack values match return types */
  9727. frame_ref = loader_ctx->frame_ref;
  9728. #if WASM_ENABLE_GC != 0
  9729. frame_reftype_map = loader_ctx->frame_reftype_map;
  9730. return_reftype_map =
  9731. return_reftype_map_count
  9732. ? return_reftype_maps + return_reftype_map_count - 1
  9733. : NULL;
  9734. #endif
  9735. for (i = (int32)return_count - 1; i >= 0; i--) {
  9736. uint8 type = return_types[i];
  9737. #if WASM_ENABLE_GC != 0
  9738. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9739. ref_type = NULL;
  9740. if (is_type_multi_byte) {
  9741. bh_assert(return_reftype_map);
  9742. ref_type = return_reftype_map->ref_type;
  9743. }
  9744. #endif
  9745. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9746. #if WASM_ENABLE_GC != 0
  9747. frame_reftype_map, available_reftype_map,
  9748. #endif
  9749. type,
  9750. #if WASM_ENABLE_GC != 0
  9751. ref_type,
  9752. #endif
  9753. error_buf, error_buf_size))
  9754. return false;
  9755. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9756. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9757. #if WASM_ENABLE_GC != 0
  9758. if (is_type_multi_byte) {
  9759. frame_reftype_map--;
  9760. available_reftype_map--;
  9761. return_reftype_map--;
  9762. }
  9763. #endif
  9764. }
  9765. return true;
  9766. fail:
  9767. return false;
  9768. }
  9769. #if WASM_ENABLE_FAST_INTERP != 0
  9770. /* Copy parameters to dynamic space.
  9771. * 1) POP original parameter out;
  9772. * 2) Push and copy original values to dynamic space.
  9773. * The copy instruction format:
  9774. * Part a: param count
  9775. * Part b: all param total cell num
  9776. * Part c: each param's cell_num, src offset and dst offset
  9777. * Part d: each param's src offset
  9778. * Part e: each param's dst offset
  9779. */
  9780. static bool
  9781. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9782. uint32 error_buf_size)
  9783. {
  9784. bool ret = false;
  9785. int16 *frame_offset = NULL;
  9786. uint8 *cells = NULL, cell;
  9787. int16 *src_offsets = NULL;
  9788. uint8 *emit_data = NULL;
  9789. uint32 i;
  9790. BranchBlock *block = loader_ctx->frame_csp - 1;
  9791. BlockType *block_type = &block->block_type;
  9792. WASMFuncType *wasm_type = block_type->u.type;
  9793. uint32 param_count = block_type->u.type->param_count;
  9794. int16 condition_offset = 0;
  9795. bool disable_emit = false;
  9796. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9797. int16 operand_offset = 0;
  9798. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9799. bh_assert(size > 0);
  9800. /* For if block, we also need copy the condition operand offset. */
  9801. if (is_if_block)
  9802. size += sizeof(*cells) + sizeof(*src_offsets);
  9803. /* Allocate memory for the emit data */
  9804. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9805. return false;
  9806. cells = emit_data;
  9807. src_offsets = (int16 *)(cells + param_count);
  9808. if (is_if_block)
  9809. condition_offset = *loader_ctx->frame_offset;
  9810. /* POP original parameter out */
  9811. for (i = 0; i < param_count; i++) {
  9812. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9813. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9814. }
  9815. frame_offset = loader_ctx->frame_offset;
  9816. /* Get each param's cell num and src offset */
  9817. for (i = 0; i < param_count; i++) {
  9818. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9819. cells[i] = cell;
  9820. src_offsets[i] = *frame_offset;
  9821. frame_offset += cell;
  9822. }
  9823. /* emit copy instruction */
  9824. emit_label(EXT_OP_COPY_STACK_VALUES);
  9825. /* Part a) */
  9826. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9827. /* Part b) */
  9828. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9829. : wasm_type->param_cell_num);
  9830. /* Part c) */
  9831. for (i = 0; i < param_count; i++)
  9832. emit_byte(loader_ctx, cells[i]);
  9833. if (is_if_block)
  9834. emit_byte(loader_ctx, 1);
  9835. /* Part d) */
  9836. for (i = 0; i < param_count; i++)
  9837. emit_operand(loader_ctx, src_offsets[i]);
  9838. if (is_if_block)
  9839. emit_operand(loader_ctx, condition_offset);
  9840. /* Since the start offset to save the block's params and
  9841. * the start offset to save the block's results may be
  9842. * different, we remember the dynamic offset for loop block
  9843. * so that we can use it to copy the stack operands to the
  9844. * loop block's params in wasm_loader_emit_br_info. */
  9845. if (block->label_type == LABEL_TYPE_LOOP)
  9846. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9847. /* Part e) */
  9848. /* Push to dynamic space. The push will emit the dst offset. */
  9849. for (i = 0; i < param_count; i++)
  9850. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9851. if (is_if_block)
  9852. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9853. ret = true;
  9854. fail:
  9855. /* Free the emit data */
  9856. wasm_runtime_free(emit_data);
  9857. return ret;
  9858. }
  9859. #endif
  9860. #if WASM_ENABLE_GC == 0
  9861. #define RESET_REFTYPE_MAP_STACK() (void)0
  9862. #else
  9863. #define RESET_REFTYPE_MAP_STACK() \
  9864. do { \
  9865. loader_ctx->reftype_map_num = \
  9866. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9867. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9868. + loader_ctx->reftype_map_num; \
  9869. } while (0)
  9870. #endif
  9871. /* reset the stack to the state of before entering the last block */
  9872. #if WASM_ENABLE_FAST_INTERP != 0
  9873. #define RESET_STACK() \
  9874. do { \
  9875. loader_ctx->stack_cell_num = \
  9876. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9877. loader_ctx->frame_ref = \
  9878. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9879. loader_ctx->frame_offset = \
  9880. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9881. RESET_REFTYPE_MAP_STACK(); \
  9882. } while (0)
  9883. #else
  9884. #define RESET_STACK() \
  9885. do { \
  9886. loader_ctx->stack_cell_num = \
  9887. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9888. loader_ctx->frame_ref = \
  9889. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9890. RESET_REFTYPE_MAP_STACK(); \
  9891. } while (0)
  9892. #endif
  9893. /* set current block's stack polymorphic state */
  9894. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9895. do { \
  9896. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9897. _cur_block->is_stack_polymorphic = flag; \
  9898. } while (0)
  9899. #define BLOCK_HAS_PARAM(block_type) \
  9900. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9901. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9902. do { \
  9903. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9904. error_buf_size))) { \
  9905. goto fail; \
  9906. } \
  9907. } while (0)
  9908. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9909. static bool
  9910. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9911. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9912. uint32 error_buf_size)
  9913. {
  9914. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9915. return false;
  9916. }
  9917. if (table_idx < module->import_table_count) {
  9918. if (p_elem_type)
  9919. *p_elem_type =
  9920. module->import_tables[table_idx].u.table.table_type.elem_type;
  9921. #if WASM_ENABLE_GC != 0
  9922. if (p_ref_type)
  9923. *((WASMRefType **)p_ref_type) =
  9924. module->import_tables[table_idx]
  9925. .u.table.table_type.elem_ref_type;
  9926. #endif
  9927. }
  9928. else {
  9929. if (p_elem_type)
  9930. *p_elem_type =
  9931. module->tables[table_idx - module->import_table_count]
  9932. .table_type.elem_type;
  9933. #if WASM_ENABLE_GC != 0
  9934. if (p_ref_type)
  9935. *((WASMRefType **)p_ref_type) =
  9936. module->tables[table_idx - module->import_table_count]
  9937. .table_type.elem_ref_type;
  9938. #endif
  9939. }
  9940. return true;
  9941. }
  9942. static bool
  9943. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9944. uint8 *p_elem_type, void **p_elem_ref_type,
  9945. char *error_buf, uint32 error_buf_size)
  9946. {
  9947. if (table_seg_idx >= module->table_seg_count) {
  9948. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9949. table_seg_idx);
  9950. return false;
  9951. }
  9952. if (p_elem_type) {
  9953. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9954. }
  9955. #if WASM_ENABLE_GC != 0
  9956. if (p_elem_ref_type)
  9957. *((WASMRefType **)p_elem_ref_type) =
  9958. module->table_segments[table_seg_idx].elem_ref_type;
  9959. #endif
  9960. return true;
  9961. }
  9962. #endif
  9963. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9964. const uint8 *
  9965. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9966. uint32 *len)
  9967. {
  9968. WASMCustomSection *section = module->custom_section_list;
  9969. while (section) {
  9970. if ((section->name_len == strlen(name))
  9971. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9972. if (len) {
  9973. *len = section->content_len;
  9974. }
  9975. return section->content_addr;
  9976. }
  9977. section = section->next;
  9978. }
  9979. return NULL;
  9980. }
  9981. #endif
  9982. #if 0
  9983. #define HANDLE_OPCODE(opcode) #opcode
  9984. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9985. #undef HANDLE_OPCODE
  9986. #endif
  9987. #if WASM_ENABLE_FAST_INTERP == 0
  9988. #define pb_read_leb_uint32 read_leb_uint32
  9989. #define pb_read_leb_int32 read_leb_int32
  9990. #define pb_read_leb_int64 read_leb_int64
  9991. #define pb_read_leb_memarg read_leb_memarg
  9992. #define pb_read_leb_mem_offset read_leb_mem_offset
  9993. #else
  9994. /* Read leb without malformed format check */
  9995. static uint64
  9996. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  9997. {
  9998. uint8 *buf = *p_buf;
  9999. uint64 result = 0, byte = 0;
  10000. uint32 shift = 0;
  10001. do {
  10002. byte = *buf++;
  10003. result |= ((byte & 0x7f) << shift);
  10004. shift += 7;
  10005. } while (byte & 0x80);
  10006. if (sign && (shift < maxbits) && (byte & 0x40)) {
  10007. /* Sign extend */
  10008. result |= (~((uint64)0)) << shift;
  10009. }
  10010. *p_buf = buf;
  10011. return result;
  10012. }
  10013. #define pb_read_leb_uint32(p, p_end, res) \
  10014. do { \
  10015. if (!loader_ctx->p_code_compiled) \
  10016. /* Enable format check in the first scan */ \
  10017. read_leb_uint32(p, p_end, res); \
  10018. else \
  10019. /* Disable format check in the second scan */ \
  10020. res = (uint32)read_leb_quick(&p, 32, false); \
  10021. } while (0)
  10022. #define pb_read_leb_int32(p, p_end, res) \
  10023. do { \
  10024. if (!loader_ctx->p_code_compiled) \
  10025. /* Enable format check in the first scan */ \
  10026. read_leb_int32(p, p_end, res); \
  10027. else \
  10028. /* Disable format check in the second scan */ \
  10029. res = (int32)read_leb_quick(&p, 32, true); \
  10030. } while (0)
  10031. #define pb_read_leb_int64(p, p_end, res) \
  10032. do { \
  10033. if (!loader_ctx->p_code_compiled) \
  10034. /* Enable format check in the first scan */ \
  10035. read_leb_int64(p, p_end, res); \
  10036. else \
  10037. /* Disable format check in the second scan */ \
  10038. res = (int64)read_leb_quick(&p, 64, true); \
  10039. } while (0)
  10040. #if WASM_ENABLE_MULTI_MEMORY != 0
  10041. #define pb_read_leb_memarg read_leb_memarg
  10042. #else
  10043. #define pb_read_leb_memarg pb_read_leb_uint32
  10044. #endif
  10045. #if WASM_ENABLE_MEMORY64 != 0
  10046. #define pb_read_leb_mem_offset read_leb_mem_offset
  10047. #else
  10048. #define pb_read_leb_mem_offset pb_read_leb_uint32
  10049. #endif
  10050. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  10051. static bool
  10052. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  10053. uint32 cur_func_idx, char *error_buf,
  10054. uint32 error_buf_size)
  10055. {
  10056. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  10057. uint32 param_count, local_count, global_count;
  10058. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  10059. table_elem_idx_type;
  10060. BlockType func_block_type;
  10061. uint16 *local_offsets, local_offset;
  10062. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  10063. uint32 table_seg_idx, data_seg_idx, count, align, i;
  10064. mem_offset_t mem_offset;
  10065. int32 i32_const = 0;
  10066. int64 i64_const;
  10067. uint8 opcode;
  10068. bool return_value = false;
  10069. WASMLoaderContext *loader_ctx;
  10070. BranchBlock *frame_csp_tmp;
  10071. #if WASM_ENABLE_GC != 0
  10072. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  10073. uint32 param_reftype_map_count, local_reftype_map_count;
  10074. int32 heap_type;
  10075. WASMRefType wasm_ref_type = { 0 };
  10076. bool need_ref_type_map;
  10077. #endif
  10078. #if WASM_ENABLE_FAST_INTERP != 0
  10079. int16 operand_offset = 0;
  10080. uint8 last_op = 0;
  10081. bool disable_emit, preserve_local = false, if_condition_available = true;
  10082. float32 f32_const;
  10083. float64 f64_const;
  10084. /*
  10085. * It means that the fast interpreter detected an exception while preparing,
  10086. * typically near the block opcode, but it did not immediately trigger
  10087. * the exception. The loader should be capable of identifying it near
  10088. * the end opcode and then raising the exception.
  10089. */
  10090. bool pending_exception = false;
  10091. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  10092. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  10093. #endif
  10094. #if WASM_ENABLE_MEMORY64 != 0
  10095. bool is_memory64 = has_module_memory64(module);
  10096. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  10097. #else
  10098. mem_offset_type = VALUE_TYPE_I32;
  10099. table_elem_idx_type = VALUE_TYPE_I32;
  10100. #endif
  10101. uint32 memidx;
  10102. global_count = module->import_global_count + module->global_count;
  10103. param_count = func->func_type->param_count;
  10104. param_types = func->func_type->types;
  10105. func_block_type.is_value_type = false;
  10106. func_block_type.u.type = func->func_type;
  10107. local_count = func->local_count;
  10108. local_types = func->local_types;
  10109. local_offsets = func->local_offsets;
  10110. #if WASM_ENABLE_GC != 0
  10111. param_reftype_maps = func->func_type->ref_type_maps;
  10112. param_reftype_map_count = func->func_type->ref_type_map_count;
  10113. local_reftype_maps = func->local_ref_type_maps;
  10114. local_reftype_map_count = func->local_ref_type_map_count;
  10115. #endif
  10116. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  10117. goto fail;
  10118. }
  10119. #if WASM_ENABLE_GC != 0
  10120. loader_ctx->module = module;
  10121. loader_ctx->ref_type_set = module->ref_type_set;
  10122. loader_ctx->ref_type_tmp = &wasm_ref_type;
  10123. #endif
  10124. #if WASM_ENABLE_FAST_INTERP != 0
  10125. /* For the first traverse, the initial value of preserved_local_offset has
  10126. * not been determined, we use the INT16_MAX to represent that a slot has
  10127. * been copied to preserve space. For second traverse, this field will be
  10128. * set to the appropriate value in wasm_loader_ctx_reinit.
  10129. * This is for Issue #1230,
  10130. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  10131. * drop opcodes need to know which slots are preserved, so those slots will
  10132. * not be treated as dynamically allocated slots */
  10133. loader_ctx->preserved_local_offset = INT16_MAX;
  10134. re_scan:
  10135. if (loader_ctx->code_compiled_size > 0) {
  10136. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  10137. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  10138. goto fail;
  10139. }
  10140. p = func->code;
  10141. func->code_compiled = loader_ctx->p_code_compiled;
  10142. func->code_compiled_size = loader_ctx->code_compiled_size;
  10143. if (loader_ctx->i64_const_num > 0) {
  10144. int64 *i64_consts_old = loader_ctx->i64_consts;
  10145. /* Sort the i64 consts */
  10146. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  10147. cmp_i64_const);
  10148. /* Remove the duplicated i64 consts */
  10149. uint32 k = 1;
  10150. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  10151. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  10152. i64_consts_old[k++] = i64_consts_old[i];
  10153. }
  10154. }
  10155. if (k < loader_ctx->i64_const_num) {
  10156. int64 *i64_consts_new;
  10157. /* Try to reallocate memory with a smaller size */
  10158. if ((i64_consts_new =
  10159. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  10160. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  10161. i64_consts_old, (uint32)sizeof(int64) * k);
  10162. /* Free the old memory */
  10163. wasm_runtime_free(i64_consts_old);
  10164. loader_ctx->i64_consts = i64_consts_new;
  10165. loader_ctx->i64_const_max_num = k;
  10166. }
  10167. loader_ctx->i64_const_num = k;
  10168. }
  10169. }
  10170. if (loader_ctx->v128_const_num > 0) {
  10171. V128 *v128_consts_old = loader_ctx->v128_consts;
  10172. /* Sort the v128 consts */
  10173. qsort(v128_consts_old, loader_ctx->v128_const_num, sizeof(V128),
  10174. cmp_v128_const);
  10175. /* Remove the duplicated v128 consts */
  10176. uint32 k = 1;
  10177. for (i = 1; i < loader_ctx->v128_const_num; i++) {
  10178. if (!(memcmp(&v128_consts_old[i], &v128_consts_old[i - 1],
  10179. sizeof(V128))
  10180. == 0)) {
  10181. v128_consts_old[k++] = v128_consts_old[i];
  10182. }
  10183. }
  10184. if (k < loader_ctx->v128_const_num) {
  10185. V128 *v128_consts_new;
  10186. /* Try to reallocate memory with a smaller size */
  10187. if ((v128_consts_new =
  10188. wasm_runtime_malloc((uint32)sizeof(V128) * k))) {
  10189. bh_memcpy_s(v128_consts_new, (uint32)sizeof(V128) * k,
  10190. v128_consts_old, (uint32)sizeof(V128) * k);
  10191. /* Free the old memory */
  10192. wasm_runtime_free(v128_consts_old);
  10193. loader_ctx->v128_consts = v128_consts_new;
  10194. loader_ctx->v128_const_max_num = k;
  10195. }
  10196. loader_ctx->v128_const_num = k;
  10197. }
  10198. }
  10199. if (loader_ctx->i32_const_num > 0) {
  10200. int32 *i32_consts_old = loader_ctx->i32_consts;
  10201. /* Sort the i32 consts */
  10202. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10203. cmp_i32_const);
  10204. /* Remove the duplicated i32 consts */
  10205. uint32 k = 1;
  10206. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10207. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10208. i32_consts_old[k++] = i32_consts_old[i];
  10209. }
  10210. }
  10211. if (k < loader_ctx->i32_const_num) {
  10212. int32 *i32_consts_new;
  10213. /* Try to reallocate memory with a smaller size */
  10214. if ((i32_consts_new =
  10215. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10216. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10217. i32_consts_old, (uint32)sizeof(int32) * k);
  10218. /* Free the old memory */
  10219. wasm_runtime_free(i32_consts_old);
  10220. loader_ctx->i32_consts = i32_consts_new;
  10221. loader_ctx->i32_const_max_num = k;
  10222. }
  10223. loader_ctx->i32_const_num = k;
  10224. }
  10225. }
  10226. }
  10227. #endif
  10228. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10229. while (p < p_end) {
  10230. opcode = *p++;
  10231. #if WASM_ENABLE_FAST_INTERP != 0
  10232. p_org = p;
  10233. disable_emit = false;
  10234. emit_label(opcode);
  10235. #endif
  10236. switch (opcode) {
  10237. case WASM_OP_UNREACHABLE:
  10238. RESET_STACK();
  10239. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10240. break;
  10241. case WASM_OP_NOP:
  10242. #if WASM_ENABLE_FAST_INTERP != 0
  10243. skip_label();
  10244. #endif
  10245. break;
  10246. case WASM_OP_IF:
  10247. {
  10248. #if WASM_ENABLE_FAST_INTERP != 0
  10249. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10250. int32 available_stack_cell =
  10251. (int32)(loader_ctx->stack_cell_num
  10252. - parent_block->stack_cell_num);
  10253. if (available_stack_cell <= 0
  10254. && parent_block->is_stack_polymorphic)
  10255. if_condition_available = false;
  10256. else
  10257. if_condition_available = true;
  10258. PRESERVE_LOCAL_FOR_BLOCK();
  10259. #endif
  10260. #if WASM_ENABLE_GC == 0
  10261. POP_I32();
  10262. #endif
  10263. goto handle_op_block_and_loop;
  10264. }
  10265. case WASM_OP_BLOCK:
  10266. case WASM_OP_LOOP:
  10267. #if WASM_ENABLE_EXCE_HANDLING != 0
  10268. case WASM_OP_TRY:
  10269. if (opcode == WASM_OP_TRY) {
  10270. /*
  10271. * keep track of exception handlers to account for
  10272. * memory allocation
  10273. */
  10274. func->exception_handler_count++;
  10275. /*
  10276. * try is a block
  10277. * do nothing special, but execution continues to
  10278. * to handle_op_block_and_loop,
  10279. * and that be pushes the csp
  10280. */
  10281. }
  10282. #endif
  10283. #if WASM_ENABLE_FAST_INTERP != 0
  10284. PRESERVE_LOCAL_FOR_BLOCK();
  10285. #endif
  10286. handle_op_block_and_loop:
  10287. {
  10288. uint8 value_type;
  10289. BlockType block_type;
  10290. #if WASM_ENABLE_FAST_INTERP != 0
  10291. uint32 available_params = 0;
  10292. #endif
  10293. CHECK_BUF(p, p_end, 1);
  10294. value_type = read_uint8(p);
  10295. if (is_byte_a_type(value_type)) {
  10296. /* If the first byte is one of these special values:
  10297. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10298. * the single return value. */
  10299. block_type.is_value_type = true;
  10300. block_type.u.value_type.type = value_type;
  10301. #if WASM_ENABLE_WAMR_COMPILER != 0
  10302. if (value_type == VALUE_TYPE_V128)
  10303. module->is_simd_used = true;
  10304. else if (value_type == VALUE_TYPE_FUNCREF
  10305. || value_type == VALUE_TYPE_EXTERNREF)
  10306. module->is_ref_types_used = true;
  10307. #endif
  10308. #if WASM_ENABLE_GC != 0
  10309. if (value_type != VALUE_TYPE_VOID) {
  10310. p_org = p;
  10311. p--;
  10312. if (!resolve_value_type((const uint8 **)&p, p_end,
  10313. module, module->type_count,
  10314. &need_ref_type_map,
  10315. &wasm_ref_type, false,
  10316. error_buf, error_buf_size)) {
  10317. goto fail;
  10318. }
  10319. if (need_ref_type_map) {
  10320. block_type.u.value_type.ref_type_map.index = 0;
  10321. if (!(block_type.u.value_type.ref_type_map
  10322. .ref_type = reftype_set_insert(
  10323. module->ref_type_set, &wasm_ref_type,
  10324. error_buf, error_buf_size))) {
  10325. goto fail;
  10326. }
  10327. }
  10328. /* Set again as the type might be changed, e.g.
  10329. (ref null any) to anyref */
  10330. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10331. #if WASM_ENABLE_FAST_INTERP == 0
  10332. while (p_org < p) {
  10333. #if WASM_ENABLE_DEBUG_INTERP != 0
  10334. if (!record_fast_op(module, p_org, *p_org,
  10335. error_buf, error_buf_size)) {
  10336. goto fail;
  10337. }
  10338. #endif
  10339. /* Ignore extra bytes for interpreter */
  10340. *p_org++ = WASM_OP_NOP;
  10341. }
  10342. #endif
  10343. }
  10344. #endif /* end of WASM_ENABLE_GC != 0 */
  10345. }
  10346. else {
  10347. int32 type_index;
  10348. /* Resolve the leb128 encoded type index as block type */
  10349. p--;
  10350. p_org = p - 1;
  10351. pb_read_leb_int32(p, p_end, type_index);
  10352. if ((uint32)type_index >= module->type_count) {
  10353. set_error_buf(error_buf, error_buf_size,
  10354. "unknown type");
  10355. goto fail;
  10356. }
  10357. block_type.is_value_type = false;
  10358. block_type.u.type =
  10359. (WASMFuncType *)module->types[type_index];
  10360. #if WASM_ENABLE_FAST_INTERP == 0
  10361. /* If block use type index as block type, change the opcode
  10362. * to new extended opcode so that interpreter can resolve
  10363. * the block quickly.
  10364. */
  10365. #if WASM_ENABLE_DEBUG_INTERP != 0
  10366. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10367. error_buf_size)) {
  10368. goto fail;
  10369. }
  10370. #endif
  10371. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10372. #endif
  10373. }
  10374. #if WASM_ENABLE_GC != 0
  10375. if (opcode == WASM_OP_IF) {
  10376. POP_I32();
  10377. }
  10378. #endif
  10379. /* Pop block parameters from stack */
  10380. if (BLOCK_HAS_PARAM(block_type)) {
  10381. WASMFuncType *wasm_type = block_type.u.type;
  10382. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10383. #if WASM_ENABLE_FAST_INTERP != 0
  10384. uint32 cell_num;
  10385. available_params = block_type.u.type->param_count;
  10386. #endif
  10387. for (i = 0; i < block_type.u.type->param_count; i++) {
  10388. int32 available_stack_cell =
  10389. (int32)(loader_ctx->stack_cell_num
  10390. - cur_block->stack_cell_num);
  10391. if (available_stack_cell <= 0
  10392. && cur_block->is_stack_polymorphic) {
  10393. #if WASM_ENABLE_FAST_INTERP != 0
  10394. available_params = i;
  10395. #endif
  10396. break;
  10397. }
  10398. POP_TYPE(
  10399. wasm_type->types[wasm_type->param_count - i - 1]);
  10400. #if WASM_ENABLE_FAST_INTERP != 0
  10401. /* decrease the frame_offset pointer accordingly to keep
  10402. * consistent with frame_ref stack */
  10403. cell_num = wasm_value_type_cell_num(
  10404. wasm_type->types[wasm_type->param_count - i - 1]);
  10405. loader_ctx->frame_offset -= cell_num;
  10406. if (loader_ctx->frame_offset
  10407. < loader_ctx->frame_offset_bottom) {
  10408. LOG_DEBUG(
  10409. "frame_offset underflow, roll back and "
  10410. "let following stack checker report it\n");
  10411. loader_ctx->frame_offset += cell_num;
  10412. pending_exception = true;
  10413. break;
  10414. }
  10415. #endif
  10416. }
  10417. }
  10418. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10419. block_type, p);
  10420. /* Pass parameters to block */
  10421. if (BLOCK_HAS_PARAM(block_type)) {
  10422. WASMFuncType *func_type = block_type.u.type;
  10423. #if WASM_ENABLE_GC != 0
  10424. WASMRefType *ref_type;
  10425. uint32 j = 0;
  10426. #endif
  10427. for (i = 0; i < func_type->param_count; i++) {
  10428. #if WASM_ENABLE_FAST_INTERP != 0
  10429. uint32 cell_num =
  10430. wasm_value_type_cell_num(func_type->types[i]);
  10431. if (i >= available_params) {
  10432. /* make sure enough space */
  10433. if (loader_ctx->p_code_compiled == NULL) {
  10434. loader_ctx->frame_offset += cell_num;
  10435. if (!check_offset_push(loader_ctx, error_buf,
  10436. error_buf_size))
  10437. goto fail;
  10438. /* for following dummy value assignment */
  10439. loader_ctx->frame_offset -= cell_num;
  10440. }
  10441. /* If there isn't enough data on stack, push a dummy
  10442. * offset to keep the stack consistent with
  10443. * frame_ref.
  10444. * Since the stack is already in polymorphic state,
  10445. * the opcode will not be executed, so the dummy
  10446. * offset won't cause any error */
  10447. for (uint32 n = 0; n < cell_num; n++) {
  10448. *loader_ctx->frame_offset++ = 0;
  10449. }
  10450. }
  10451. else {
  10452. loader_ctx->frame_offset += cell_num;
  10453. }
  10454. #endif
  10455. #if WASM_ENABLE_GC != 0
  10456. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10457. bh_assert(func_type->ref_type_maps[j].index == i);
  10458. ref_type = func_type->ref_type_maps[j].ref_type;
  10459. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10460. ref_type,
  10461. wasm_reftype_struct_size(ref_type));
  10462. j++;
  10463. }
  10464. #endif
  10465. PUSH_TYPE(func_type->types[i]);
  10466. }
  10467. }
  10468. #if WASM_ENABLE_FAST_INTERP != 0
  10469. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10470. skip_label();
  10471. if (BLOCK_HAS_PARAM(block_type)) {
  10472. /* Make sure params are in dynamic space */
  10473. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10474. error_buf_size))
  10475. goto fail;
  10476. }
  10477. if (opcode == WASM_OP_LOOP) {
  10478. (loader_ctx->frame_csp - 1)->code_compiled =
  10479. loader_ctx->p_code_compiled;
  10480. }
  10481. }
  10482. #if WASM_ENABLE_EXCE_HANDLING != 0
  10483. else if (opcode == WASM_OP_TRY) {
  10484. skip_label();
  10485. }
  10486. #endif
  10487. else if (opcode == WASM_OP_IF) {
  10488. BranchBlock *block = loader_ctx->frame_csp - 1;
  10489. /* If block has parameters, we should make sure they are in
  10490. * dynamic space. Otherwise, when else branch is missing,
  10491. * the later opcode may consume incorrect operand offset.
  10492. * Spec case:
  10493. * (func (export "params-id") (param i32) (result i32)
  10494. * (i32.const 1)
  10495. * (i32.const 2)
  10496. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10497. * (then)) (i32.add)
  10498. * )
  10499. *
  10500. * So we should emit a copy instruction before the if.
  10501. *
  10502. * And we also need to save the parameter offsets and
  10503. * recover them before entering else branch.
  10504. *
  10505. */
  10506. if (BLOCK_HAS_PARAM(block_type)) {
  10507. uint64 size;
  10508. /* In polymorphic state, there may be no if condition on
  10509. * the stack, so the offset may not emitted */
  10510. if (if_condition_available) {
  10511. /* skip the if condition operand offset */
  10512. wasm_loader_emit_backspace(loader_ctx,
  10513. sizeof(int16));
  10514. }
  10515. /* skip the if label */
  10516. skip_label();
  10517. /* Emit a copy instruction */
  10518. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10519. error_buf_size))
  10520. goto fail;
  10521. /* Emit the if instruction */
  10522. emit_label(opcode);
  10523. /* Emit the new condition operand offset */
  10524. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10525. /* Save top param_count values of frame_offset stack, so
  10526. * that we can recover it before executing else branch
  10527. */
  10528. size = sizeof(int16)
  10529. * (uint64)block_type.u.type->param_cell_num;
  10530. if (!(block->param_frame_offsets = loader_malloc(
  10531. size, error_buf, error_buf_size)))
  10532. goto fail;
  10533. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10534. loader_ctx->frame_offset
  10535. - size / sizeof(int16),
  10536. (uint32)size);
  10537. }
  10538. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10539. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10540. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10541. }
  10542. #endif
  10543. break;
  10544. }
  10545. #if WASM_ENABLE_EXCE_HANDLING != 0
  10546. case WASM_OP_THROW:
  10547. {
  10548. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10549. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10550. uint8 label_type = cur_block->label_type;
  10551. uint32 tag_index = 0;
  10552. pb_read_leb_int32(p, p_end, tag_index);
  10553. /* check validity of tag_index against module->tag_count */
  10554. /* check tag index is within the tag index space */
  10555. if (tag_index >= module->import_tag_count + module->tag_count) {
  10556. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10557. tag_index);
  10558. goto fail;
  10559. }
  10560. /* the tag_type is stored in either the WASMTag (section tags)
  10561. * or WASMTagImport (import tag) */
  10562. WASMFuncType *tag_type = NULL;
  10563. if (tag_index < module->import_tag_count) {
  10564. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10565. }
  10566. else {
  10567. tag_type =
  10568. module->tags[tag_index - module->import_tag_count]
  10569. ->tag_type;
  10570. }
  10571. if (tag_type->result_count != 0) {
  10572. set_error_buf(error_buf, error_buf_size,
  10573. "tag type signature does not return void");
  10574. goto fail;
  10575. }
  10576. int32 available_stack_cell =
  10577. (int32)(loader_ctx->stack_cell_num
  10578. - cur_block->stack_cell_num);
  10579. int32 tti;
  10580. /* Check stack values match return types by comparing tag param
  10581. * types with stack cells */
  10582. uint8 *frame_ref = loader_ctx->frame_ref;
  10583. #if WASM_ENABLE_GC != 0
  10584. WASMRefTypeMap *frame_reftype_map =
  10585. loader_ctx->frame_reftype_map;
  10586. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10587. /* Temporarily set these values since they may be used in
  10588. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10589. param_reftype_maps = tag_type->ref_type_maps;
  10590. /* For tag_type function, it shouldn't have result_count = 0 */
  10591. param_reftype_map_count = tag_type->ref_type_map_count;
  10592. param_count = tag_type->param_count;
  10593. #endif
  10594. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10595. #if WASM_ENABLE_GC != 0
  10596. local_type = tag_type->types[tti];
  10597. local_idx = tti;
  10598. /* Get the wasm_ref_type if the local_type is multibyte
  10599. type */
  10600. GET_LOCAL_REFTYPE();
  10601. #endif
  10602. if (!check_stack_top_values(
  10603. loader_ctx, frame_ref, available_stack_cell,
  10604. #if WASM_ENABLE_GC != 0
  10605. frame_reftype_map, frame_reftype_map_num,
  10606. #endif
  10607. tag_type->types[tti],
  10608. #if WASM_ENABLE_GC != 0
  10609. &wasm_ref_type,
  10610. #endif
  10611. error_buf, error_buf_size)) {
  10612. snprintf(error_buf, error_buf_size,
  10613. "type mismatch: instruction requires [%s] but "
  10614. "stack has [%s]",
  10615. tag_type->param_count > 0
  10616. ? type2str(tag_type->types[tti])
  10617. : "",
  10618. available_stack_cell > 0
  10619. ? type2str(*(loader_ctx->frame_ref - 1))
  10620. : "");
  10621. goto fail;
  10622. }
  10623. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10624. available_stack_cell -=
  10625. wasm_value_type_cell_num(tag_type->types[tti]);
  10626. }
  10627. #if WASM_ENABLE_GC != 0
  10628. /* Restore the values */
  10629. param_reftype_maps = func->func_type->ref_type_maps;
  10630. param_reftype_map_count = func->func_type->ref_type_map_count;
  10631. param_count = func->func_type->param_count;
  10632. #endif
  10633. /* throw is stack polymorphic */
  10634. (void)label_type;
  10635. RESET_STACK();
  10636. break;
  10637. }
  10638. case WASM_OP_RETHROW:
  10639. {
  10640. /* must be done before checking branch block */
  10641. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10642. /* check the target catching block: LABEL_TYPE_CATCH */
  10643. if (!(frame_csp_tmp =
  10644. check_branch_block(loader_ctx, &p, p_end, opcode,
  10645. error_buf, error_buf_size)))
  10646. goto fail;
  10647. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10648. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10649. /* trap according to spectest (rethrow.wast) */
  10650. set_error_buf(error_buf, error_buf_size,
  10651. "invalid rethrow label");
  10652. goto fail;
  10653. }
  10654. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10655. uint8 label_type = cur_block->label_type;
  10656. (void)label_type;
  10657. /* rethrow is stack polymorphic */
  10658. RESET_STACK();
  10659. break;
  10660. }
  10661. case WASM_OP_DELEGATE:
  10662. {
  10663. /* check target block is valid */
  10664. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10665. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10666. goto fail;
  10667. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10668. uint8 label_type = cur_block->label_type;
  10669. (void)label_type;
  10670. /* DELEGATE ends the block */
  10671. POP_CSP();
  10672. break;
  10673. }
  10674. case WASM_OP_CATCH:
  10675. {
  10676. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10677. uint8 label_type = cur_block->label_type;
  10678. uint32 tag_index = 0;
  10679. pb_read_leb_int32(p, p_end, tag_index);
  10680. /* check validity of tag_index against module->tag_count */
  10681. /* check tag index is within the tag index space */
  10682. if (tag_index >= module->import_tag_count + module->tag_count) {
  10683. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10684. __FUNCTION__);
  10685. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10686. goto fail;
  10687. }
  10688. /* the tag_type is stored in either the WASMTag (section tags)
  10689. * or WASMTagImport (import tag) */
  10690. WASMFuncType *func_type = NULL;
  10691. if (tag_index < module->import_tag_count) {
  10692. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10693. }
  10694. else {
  10695. func_type =
  10696. module->tags[tag_index - module->import_tag_count]
  10697. ->tag_type;
  10698. }
  10699. if (func_type->result_count != 0) {
  10700. set_error_buf(error_buf, error_buf_size,
  10701. "tag type signature does not return void");
  10702. goto fail;
  10703. }
  10704. /* check validity of current label (expect LABEL_TYPE_TRY or
  10705. * LABEL_TYPE_CATCH) */
  10706. if ((LABEL_TYPE_CATCH != label_type)
  10707. && (LABEL_TYPE_TRY != label_type)) {
  10708. set_error_buf(error_buf, error_buf_size,
  10709. "Unexpected block sequence encountered.");
  10710. goto fail;
  10711. }
  10712. /*
  10713. * replace frame_csp by LABEL_TYPE_CATCH
  10714. */
  10715. cur_block->label_type = LABEL_TYPE_CATCH;
  10716. /* RESET_STACK removes the values pushed in TRY or previous
  10717. * CATCH Blocks */
  10718. RESET_STACK();
  10719. #if WASM_ENABLE_GC != 0
  10720. WASMRefType *ref_type;
  10721. uint32 j = 0;
  10722. #endif
  10723. /* push types on the stack according to caught type */
  10724. for (i = 0; i < func_type->param_count; i++) {
  10725. #if WASM_ENABLE_GC != 0
  10726. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10727. bh_assert(func_type->ref_type_maps[j].index == i);
  10728. ref_type = func_type->ref_type_maps[j].ref_type;
  10729. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10730. ref_type,
  10731. wasm_reftype_struct_size(ref_type));
  10732. j++;
  10733. }
  10734. #endif
  10735. PUSH_TYPE(func_type->types[i]);
  10736. }
  10737. break;
  10738. }
  10739. case WASM_OP_CATCH_ALL:
  10740. {
  10741. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10742. /* expecting a TRY or CATCH, anything else will be considered an
  10743. * error */
  10744. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10745. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10746. set_error_buf(error_buf, error_buf_size,
  10747. "Unexpected block sequence encountered.");
  10748. goto fail;
  10749. }
  10750. /* no immediates */
  10751. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10752. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10753. /* RESET_STACK removes the values pushed in TRY or previous
  10754. * CATCH Blocks */
  10755. RESET_STACK();
  10756. /* catch_all has no tagtype and therefore no parameters */
  10757. break;
  10758. }
  10759. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10760. case WASM_OP_ELSE:
  10761. handle_op_else:
  10762. {
  10763. BranchBlock *block = NULL;
  10764. BlockType block_type;
  10765. if (loader_ctx->csp_num < 2
  10766. /* the matched if isn't found */
  10767. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10768. /* duplicated else is found */
  10769. || (loader_ctx->frame_csp - 1)->else_addr) {
  10770. set_error_buf(
  10771. error_buf, error_buf_size,
  10772. "opcode else found without matched opcode if");
  10773. goto fail;
  10774. }
  10775. block = loader_ctx->frame_csp - 1;
  10776. /* check whether if branch's stack matches its result type */
  10777. if (!check_block_stack(loader_ctx, block, error_buf,
  10778. error_buf_size))
  10779. goto fail;
  10780. block->else_addr = p - 1;
  10781. block_type = block->block_type;
  10782. #if WASM_ENABLE_GC != 0
  10783. if (!wasm_loader_init_local_use_masks(
  10784. loader_ctx, local_count, error_buf, error_buf_size)) {
  10785. goto fail;
  10786. }
  10787. #endif
  10788. #if WASM_ENABLE_FAST_INTERP != 0
  10789. /* if the result of if branch is in local or const area, add a
  10790. * copy op */
  10791. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10792. error_buf, error_buf_size)) {
  10793. goto fail;
  10794. }
  10795. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10796. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10797. #endif
  10798. RESET_STACK();
  10799. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10800. /* Pass parameters to if-false branch */
  10801. if (BLOCK_HAS_PARAM(block_type)) {
  10802. for (i = 0; i < block_type.u.type->param_count; i++)
  10803. PUSH_TYPE(block_type.u.type->types[i]);
  10804. }
  10805. #if WASM_ENABLE_FAST_INTERP != 0
  10806. /* Recover top param_count values of frame_offset stack */
  10807. if (BLOCK_HAS_PARAM((block_type))) {
  10808. uint32 size;
  10809. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10810. bh_memcpy_s(loader_ctx->frame_offset, size,
  10811. block->param_frame_offsets, size);
  10812. loader_ctx->frame_offset += (size / sizeof(int16));
  10813. }
  10814. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10815. #endif
  10816. break;
  10817. }
  10818. case WASM_OP_END:
  10819. {
  10820. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10821. /* check whether block stack matches its result type */
  10822. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10823. error_buf_size))
  10824. goto fail;
  10825. /* if there is no else branch, make a virtual else opcode for
  10826. easier integrity check and to copy the correct results to
  10827. the block return address for fast-interp mode:
  10828. change if block from `if ... end` to `if ... else end` */
  10829. if (cur_block->label_type == LABEL_TYPE_IF
  10830. && !cur_block->else_addr) {
  10831. opcode = WASM_OP_ELSE;
  10832. p--;
  10833. #if WASM_ENABLE_FAST_INTERP != 0
  10834. p_org = p;
  10835. skip_label();
  10836. disable_emit = false;
  10837. emit_label(opcode);
  10838. #endif
  10839. goto handle_op_else;
  10840. }
  10841. POP_CSP();
  10842. #if WASM_ENABLE_FAST_INTERP != 0
  10843. skip_label();
  10844. /* copy the result to the block return address */
  10845. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10846. error_buf, error_buf_size)) {
  10847. /* it could be tmp frame_csp allocated from opcode like
  10848. * OP_BR and not counted in loader_ctx->csp_num, it won't
  10849. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  10850. * to free the loader_ctx->frame_csp if fails */
  10851. free_label_patch_list(loader_ctx->frame_csp);
  10852. goto fail;
  10853. }
  10854. apply_label_patch(loader_ctx, 0, PATCH_END);
  10855. free_label_patch_list(loader_ctx->frame_csp);
  10856. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10857. int32 idx;
  10858. uint8 ret_type;
  10859. emit_label(WASM_OP_RETURN);
  10860. for (idx = (int32)func->func_type->result_count - 1;
  10861. idx >= 0; idx--) {
  10862. ret_type = *(func->func_type->types
  10863. + func->func_type->param_count + idx);
  10864. POP_OFFSET_TYPE(ret_type);
  10865. }
  10866. }
  10867. #endif
  10868. if (loader_ctx->csp_num > 0) {
  10869. loader_ctx->frame_csp->end_addr = p - 1;
  10870. }
  10871. else {
  10872. /* end of function block, function will return */
  10873. if (p < p_end) {
  10874. set_error_buf(error_buf, error_buf_size,
  10875. "section size mismatch");
  10876. goto fail;
  10877. }
  10878. }
  10879. #if WASM_ENABLE_FAST_INTERP != 0
  10880. if (pending_exception) {
  10881. set_error_buf(
  10882. error_buf, error_buf_size,
  10883. "There is a pending exception needs to be handled");
  10884. goto fail;
  10885. }
  10886. #endif
  10887. break;
  10888. }
  10889. case WASM_OP_BR:
  10890. {
  10891. if (!(frame_csp_tmp =
  10892. check_branch_block(loader_ctx, &p, p_end, opcode,
  10893. error_buf, error_buf_size)))
  10894. goto fail;
  10895. RESET_STACK();
  10896. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10897. break;
  10898. }
  10899. case WASM_OP_BR_IF:
  10900. {
  10901. POP_I32();
  10902. if (!(frame_csp_tmp =
  10903. check_branch_block(loader_ctx, &p, p_end, opcode,
  10904. error_buf, error_buf_size)))
  10905. goto fail;
  10906. break;
  10907. }
  10908. case WASM_OP_BR_TABLE:
  10909. {
  10910. uint32 depth = 0, default_arity, arity = 0;
  10911. BranchBlock *target_block;
  10912. BlockType *target_block_type;
  10913. #if WASM_ENABLE_FAST_INTERP == 0
  10914. BrTableCache *br_table_cache = NULL;
  10915. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10916. uint32 j;
  10917. #endif
  10918. pb_read_leb_uint32(p, p_end, count);
  10919. #if WASM_ENABLE_FAST_INTERP != 0
  10920. emit_uint32(loader_ctx, count);
  10921. #endif
  10922. POP_I32();
  10923. /* Get each depth and check it */
  10924. p_org = p;
  10925. for (i = 0; i <= count; i++) {
  10926. pb_read_leb_uint32(p, p_end, depth);
  10927. bh_assert(loader_ctx->csp_num > 0);
  10928. if (loader_ctx->csp_num - 1 < depth) {
  10929. set_error_buf(error_buf, error_buf_size,
  10930. "unknown label, "
  10931. "unexpected end of section or function");
  10932. goto fail;
  10933. }
  10934. }
  10935. p = p_org;
  10936. /* Get the default block's arity */
  10937. target_block = loader_ctx->frame_csp - (depth + 1);
  10938. target_block_type = &target_block->block_type;
  10939. default_arity = block_type_get_arity(target_block_type,
  10940. target_block->label_type);
  10941. #if WASM_ENABLE_FAST_INTERP == 0
  10942. p_depth_begin = p_depth = p;
  10943. #endif
  10944. for (i = 0; i <= count; i++) {
  10945. p_org = p;
  10946. pb_read_leb_uint32(p, p_end, depth);
  10947. p = p_org;
  10948. /* Get the target block's arity and check it */
  10949. target_block = loader_ctx->frame_csp - (depth + 1);
  10950. target_block_type = &target_block->block_type;
  10951. arity = block_type_get_arity(target_block_type,
  10952. target_block->label_type);
  10953. if (arity != default_arity) {
  10954. set_error_buf(error_buf, error_buf_size,
  10955. "type mismatch: br_table targets must "
  10956. "all use same result type");
  10957. goto fail;
  10958. }
  10959. if (!(frame_csp_tmp =
  10960. check_branch_block(loader_ctx, &p, p_end, opcode,
  10961. error_buf, error_buf_size))) {
  10962. goto fail;
  10963. }
  10964. #if WASM_ENABLE_FAST_INTERP == 0
  10965. if (br_table_cache) {
  10966. br_table_cache->br_depths[i] = depth;
  10967. }
  10968. else {
  10969. if (depth > 255) {
  10970. /* The depth cannot be stored in one byte,
  10971. create br_table cache to store each depth */
  10972. #if WASM_ENABLE_DEBUG_INTERP != 0
  10973. if (!record_fast_op(module, p_opcode, *p_opcode,
  10974. error_buf, error_buf_size)) {
  10975. goto fail;
  10976. }
  10977. #endif
  10978. if (!(br_table_cache = loader_malloc(
  10979. offsetof(BrTableCache, br_depths)
  10980. + sizeof(uint32)
  10981. * (uint64)(count + 1),
  10982. error_buf, error_buf_size))) {
  10983. goto fail;
  10984. }
  10985. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10986. br_table_cache->br_table_op_addr = p_opcode;
  10987. br_table_cache->br_count = count;
  10988. /* Copy previous depths which are one byte */
  10989. for (j = 0; j < i; j++) {
  10990. br_table_cache->br_depths[j] = p_depth_begin[j];
  10991. }
  10992. br_table_cache->br_depths[i] = depth;
  10993. bh_list_insert(module->br_table_cache_list,
  10994. br_table_cache);
  10995. }
  10996. else {
  10997. /* The depth can be stored in one byte, use the
  10998. byte of the leb to store it */
  10999. *p_depth++ = (uint8)depth;
  11000. }
  11001. }
  11002. #endif
  11003. }
  11004. #if WASM_ENABLE_FAST_INTERP == 0
  11005. /* Set the tailing bytes to nop */
  11006. if (br_table_cache)
  11007. p_depth = p_depth_begin;
  11008. while (p_depth < p)
  11009. *p_depth++ = WASM_OP_NOP;
  11010. #endif
  11011. RESET_STACK();
  11012. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11013. break;
  11014. }
  11015. case WASM_OP_RETURN:
  11016. {
  11017. WASMFuncType *func_type = func->func_type;
  11018. int32 idx;
  11019. uint8 ret_type;
  11020. #if WASM_ENABLE_GC != 0
  11021. uint32 j = func_type->ref_type_map_count - 1;
  11022. #endif
  11023. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  11024. idx--) {
  11025. ret_type =
  11026. *(func_type->types + func_type->param_count + idx);
  11027. #if WASM_ENABLE_GC != 0
  11028. if (wasm_is_type_multi_byte_type(ret_type)) {
  11029. WASMRefType *ref_type =
  11030. func_type->ref_type_maps[j].ref_type;
  11031. bh_assert(func_type->ref_type_maps[j].index
  11032. == func_type->param_count + idx);
  11033. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11034. ref_type,
  11035. wasm_reftype_struct_size(ref_type));
  11036. j--;
  11037. }
  11038. #endif
  11039. #if WASM_ENABLE_FAST_INTERP != 0
  11040. /* emit the offset after return opcode */
  11041. POP_OFFSET_TYPE(ret_type);
  11042. #endif
  11043. POP_TYPE(ret_type);
  11044. }
  11045. RESET_STACK();
  11046. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11047. break;
  11048. }
  11049. case WASM_OP_CALL:
  11050. #if WASM_ENABLE_TAIL_CALL != 0
  11051. case WASM_OP_RETURN_CALL:
  11052. #endif
  11053. #if WASM_ENABLE_GC != 0
  11054. case WASM_OP_CALL_REF:
  11055. case WASM_OP_RETURN_CALL_REF:
  11056. #endif
  11057. {
  11058. WASMFuncType *func_type;
  11059. uint8 type;
  11060. int32 idx;
  11061. #if WASM_ENABLE_GC != 0
  11062. WASMRefType *ref_type;
  11063. uint32 type_idx1;
  11064. int32 j;
  11065. #endif
  11066. #if WASM_ENABLE_GC != 0
  11067. if (opcode == WASM_OP_CALL_REF
  11068. || opcode == WASM_OP_RETURN_CALL_REF) {
  11069. pb_read_leb_uint32(p, p_end, type_idx1);
  11070. if (!check_type_index(module, module->type_count, type_idx1,
  11071. error_buf, error_buf_size)) {
  11072. goto fail;
  11073. }
  11074. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  11075. set_error_buf(error_buf, error_buf_size,
  11076. "unknown function type");
  11077. goto fail;
  11078. }
  11079. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  11080. &type_idx, error_buf,
  11081. error_buf_size)) {
  11082. goto fail;
  11083. }
  11084. if (type == VALUE_TYPE_ANY) {
  11085. type_idx = type_idx1;
  11086. }
  11087. if (!check_type_index(module, module->type_count, type_idx,
  11088. error_buf, error_buf_size)) {
  11089. goto fail;
  11090. }
  11091. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  11092. set_error_buf(error_buf, error_buf_size,
  11093. "unknown function type");
  11094. goto fail;
  11095. }
  11096. if (!wasm_func_type_is_super_of(
  11097. (WASMFuncType *)module->types[type_idx1],
  11098. (WASMFuncType *)module->types[type_idx])) {
  11099. set_error_buf(error_buf, error_buf_size,
  11100. "function type mismatch");
  11101. goto fail;
  11102. }
  11103. func_type = (WASMFuncType *)module->types[type_idx];
  11104. }
  11105. else
  11106. #endif
  11107. {
  11108. pb_read_leb_uint32(p, p_end, func_idx);
  11109. #if WASM_ENABLE_FAST_INTERP != 0
  11110. /* we need to emit func_idx before arguments */
  11111. emit_uint32(loader_ctx, func_idx);
  11112. #endif
  11113. if (!check_function_index(module, func_idx, error_buf,
  11114. error_buf_size)) {
  11115. goto fail;
  11116. }
  11117. if (func_idx < module->import_function_count)
  11118. func_type = module->import_functions[func_idx]
  11119. .u.function.func_type;
  11120. else
  11121. func_type =
  11122. module
  11123. ->functions[func_idx
  11124. - module->import_function_count]
  11125. ->func_type;
  11126. }
  11127. if (func_type->param_count > 0) {
  11128. #if WASM_ENABLE_GC != 0
  11129. j = (int32)(func_type->result_ref_type_maps
  11130. - func_type->ref_type_maps - 1);
  11131. #endif
  11132. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11133. idx--) {
  11134. #if WASM_ENABLE_GC != 0
  11135. if (wasm_is_type_multi_byte_type(
  11136. func_type->types[idx])) {
  11137. ref_type = func_type->ref_type_maps[j].ref_type;
  11138. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11139. ref_type,
  11140. wasm_reftype_struct_size(ref_type));
  11141. j--;
  11142. }
  11143. #endif
  11144. #if WASM_ENABLE_FAST_INTERP != 0
  11145. POP_OFFSET_TYPE(func_type->types[idx]);
  11146. #endif
  11147. POP_TYPE(func_type->types[idx]);
  11148. }
  11149. }
  11150. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11151. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  11152. #endif
  11153. #if WASM_ENABLE_GC != 0
  11154. j = (int32)(func_type->result_ref_type_maps
  11155. - func_type->ref_type_maps);
  11156. #endif
  11157. for (i = 0; i < func_type->result_count; i++) {
  11158. #if WASM_ENABLE_GC != 0
  11159. if (wasm_is_type_multi_byte_type(
  11160. func_type->types[func_type->param_count + i])) {
  11161. ref_type = func_type->ref_type_maps[j].ref_type;
  11162. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11163. ref_type,
  11164. wasm_reftype_struct_size(ref_type));
  11165. j++;
  11166. }
  11167. #endif
  11168. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11169. #if WASM_ENABLE_FAST_INTERP != 0
  11170. /* Here we emit each return value's dynamic_offset. But
  11171. * in fact these offsets are continuous, so interpreter
  11172. * only need to get the first return value's offset.
  11173. */
  11174. PUSH_OFFSET_TYPE(
  11175. func_type->types[func_type->param_count + i]);
  11176. #endif
  11177. }
  11178. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11179. }
  11180. else {
  11181. #if WASM_ENABLE_GC == 0
  11182. if (func_type->result_count
  11183. != func->func_type->result_count) {
  11184. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11185. "type mismatch: expect ",
  11186. func->func_type->result_count,
  11187. " return values but got other");
  11188. goto fail;
  11189. }
  11190. for (i = 0; i < func_type->result_count; i++) {
  11191. type = func->func_type
  11192. ->types[func->func_type->param_count + i];
  11193. if (func_type->types[func_type->param_count + i]
  11194. != type) {
  11195. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11196. "type mismatch: expect ",
  11197. type2str(type), " but got other");
  11198. goto fail;
  11199. }
  11200. }
  11201. #else
  11202. if (!wasm_func_type_result_is_subtype_of(
  11203. func_type, func->func_type, module->types,
  11204. module->type_count)) {
  11205. set_error_buf(
  11206. error_buf, error_buf_size,
  11207. "type mismatch: invalid func result types");
  11208. goto fail;
  11209. }
  11210. #endif
  11211. RESET_STACK();
  11212. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11213. }
  11214. #endif
  11215. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11216. || WASM_ENABLE_WAMR_COMPILER != 0
  11217. func->has_op_func_call = true;
  11218. #endif
  11219. (void)type;
  11220. break;
  11221. }
  11222. /*
  11223. * if disable reference type: call_indirect typeidx, 0x00
  11224. * if enable reference type: call_indirect typeidx, tableidx
  11225. */
  11226. case WASM_OP_CALL_INDIRECT:
  11227. #if WASM_ENABLE_TAIL_CALL != 0
  11228. case WASM_OP_RETURN_CALL_INDIRECT:
  11229. #endif
  11230. {
  11231. int32 idx;
  11232. WASMFuncType *func_type;
  11233. uint32 tbl_elem_type;
  11234. #if WASM_ENABLE_GC != 0
  11235. WASMRefType *elem_ref_type = NULL;
  11236. #endif
  11237. pb_read_leb_uint32(p, p_end, type_idx);
  11238. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11239. #if WASM_ENABLE_WAMR_COMPILER != 0
  11240. if (p + 1 < p_end && *p != 0x00) {
  11241. /*
  11242. * Any non-0x00 byte requires the ref types proposal.
  11243. * This is different from checking the table_idx value
  11244. * since `0x80 0x00` etc. are all valid encodings of zero.
  11245. */
  11246. module->is_ref_types_used = true;
  11247. }
  11248. #endif
  11249. pb_read_leb_uint32(p, p_end, table_idx);
  11250. #else
  11251. CHECK_BUF(p, p_end, 1);
  11252. table_idx = read_uint8(p);
  11253. #endif
  11254. if (!check_table_index(module, table_idx, error_buf,
  11255. error_buf_size)) {
  11256. goto fail;
  11257. }
  11258. tbl_elem_type =
  11259. table_idx < module->import_table_count
  11260. ? module->import_tables[table_idx]
  11261. .u.table.table_type.elem_type
  11262. : module->tables[table_idx - module->import_table_count]
  11263. .table_type.elem_type;
  11264. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11265. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11266. set_error_buf_v(error_buf, error_buf_size,
  11267. "type mismatch: instruction requires table "
  11268. "of functions but table %u has externref",
  11269. table_idx);
  11270. goto fail;
  11271. }
  11272. #elif WASM_ENABLE_GC != 0
  11273. /* Table element must match type ref null func */
  11274. elem_ref_type =
  11275. table_idx < module->import_table_count
  11276. ? module->import_tables[table_idx]
  11277. .u.table.table_type.elem_ref_type
  11278. : module->tables[table_idx - module->import_table_count]
  11279. .table_type.elem_ref_type;
  11280. if (!wasm_reftype_is_subtype_of(
  11281. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11282. module->types, module->type_count)) {
  11283. set_error_buf_v(error_buf, error_buf_size,
  11284. "type mismatch: instruction requires "
  11285. "reference type t match type ref null func"
  11286. "in table %u",
  11287. table_idx);
  11288. goto fail;
  11289. }
  11290. #else
  11291. (void)tbl_elem_type;
  11292. #endif
  11293. #if WASM_ENABLE_FAST_INTERP != 0
  11294. /* we need to emit before arguments */
  11295. #if WASM_ENABLE_TAIL_CALL != 0
  11296. emit_byte(loader_ctx, opcode);
  11297. #endif
  11298. emit_uint32(loader_ctx, type_idx);
  11299. emit_uint32(loader_ctx, table_idx);
  11300. #endif
  11301. #if WASM_ENABLE_MEMORY64 != 0
  11302. table_elem_idx_type = is_table_64bit(module, table_idx)
  11303. ? VALUE_TYPE_I64
  11304. : VALUE_TYPE_I32;
  11305. #endif
  11306. /* skip elem idx */
  11307. POP_TBL_ELEM_IDX();
  11308. if (type_idx >= module->type_count) {
  11309. set_error_buf(error_buf, error_buf_size, "unknown type");
  11310. goto fail;
  11311. }
  11312. func_type = (WASMFuncType *)module->types[type_idx];
  11313. if (func_type->param_count > 0) {
  11314. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11315. idx--) {
  11316. #if WASM_ENABLE_FAST_INTERP != 0
  11317. POP_OFFSET_TYPE(func_type->types[idx]);
  11318. #endif
  11319. POP_TYPE(func_type->types[idx]);
  11320. }
  11321. }
  11322. #if WASM_ENABLE_TAIL_CALL != 0
  11323. if (opcode == WASM_OP_CALL_INDIRECT) {
  11324. #endif
  11325. for (i = 0; i < func_type->result_count; i++) {
  11326. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11327. #if WASM_ENABLE_FAST_INTERP != 0
  11328. PUSH_OFFSET_TYPE(
  11329. func_type->types[func_type->param_count + i]);
  11330. #endif
  11331. }
  11332. #if WASM_ENABLE_TAIL_CALL != 0
  11333. }
  11334. else {
  11335. uint8 type;
  11336. if (func_type->result_count
  11337. != func->func_type->result_count) {
  11338. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11339. "type mismatch: expect ",
  11340. func->func_type->result_count,
  11341. " return values but got other");
  11342. goto fail;
  11343. }
  11344. for (i = 0; i < func_type->result_count; i++) {
  11345. type = func->func_type
  11346. ->types[func->func_type->param_count + i];
  11347. if (func_type->types[func_type->param_count + i]
  11348. != type) {
  11349. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11350. "type mismatch: expect ",
  11351. type2str(type), " but got other");
  11352. goto fail;
  11353. }
  11354. }
  11355. RESET_STACK();
  11356. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11357. }
  11358. #endif
  11359. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11360. || WASM_ENABLE_WAMR_COMPILER != 0
  11361. func->has_op_func_call = true;
  11362. #endif
  11363. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11364. func->has_op_call_indirect = true;
  11365. #endif
  11366. break;
  11367. }
  11368. case WASM_OP_DROP:
  11369. {
  11370. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11371. int32 available_stack_cell =
  11372. (int32)(loader_ctx->stack_cell_num
  11373. - cur_block->stack_cell_num);
  11374. if (available_stack_cell <= 0
  11375. && !cur_block->is_stack_polymorphic) {
  11376. set_error_buf(error_buf, error_buf_size,
  11377. "type mismatch, opcode drop was found "
  11378. "but stack was empty");
  11379. goto fail;
  11380. }
  11381. if (available_stack_cell > 0) {
  11382. #if WASM_ENABLE_GC != 0
  11383. if (wasm_is_type_multi_byte_type(
  11384. *(loader_ctx->frame_ref - 1))) {
  11385. bh_assert((int32)(loader_ctx->reftype_map_num
  11386. - cur_block->reftype_map_num)
  11387. > 0);
  11388. loader_ctx->frame_reftype_map--;
  11389. loader_ctx->reftype_map_num--;
  11390. }
  11391. #endif
  11392. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11393. loader_ctx->frame_ref--;
  11394. loader_ctx->stack_cell_num--;
  11395. #if WASM_ENABLE_FAST_INTERP != 0
  11396. skip_label();
  11397. loader_ctx->frame_offset--;
  11398. if ((*(loader_ctx->frame_offset)
  11399. > loader_ctx->start_dynamic_offset)
  11400. && (*(loader_ctx->frame_offset)
  11401. < loader_ctx->max_dynamic_offset))
  11402. loader_ctx->dynamic_offset--;
  11403. #endif
  11404. }
  11405. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11406. loader_ctx->frame_ref -= 2;
  11407. loader_ctx->stack_cell_num -= 2;
  11408. #if WASM_ENABLE_FAST_INTERP == 0
  11409. *(p - 1) = WASM_OP_DROP_64;
  11410. #endif
  11411. #if WASM_ENABLE_FAST_INTERP != 0
  11412. skip_label();
  11413. loader_ctx->frame_offset -= 2;
  11414. if ((*(loader_ctx->frame_offset)
  11415. > loader_ctx->start_dynamic_offset)
  11416. && (*(loader_ctx->frame_offset)
  11417. < loader_ctx->max_dynamic_offset))
  11418. loader_ctx->dynamic_offset -= 2;
  11419. #endif
  11420. }
  11421. #if WASM_ENABLE_SIMD != 0
  11422. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11423. || (WASM_ENABLE_FAST_INTERP != 0)
  11424. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11425. loader_ctx->frame_ref -= 4;
  11426. loader_ctx->stack_cell_num -= 4;
  11427. #if WASM_ENABLE_FAST_INTERP != 0
  11428. skip_label();
  11429. loader_ctx->frame_offset -= 4;
  11430. if ((*(loader_ctx->frame_offset)
  11431. > loader_ctx->start_dynamic_offset)
  11432. && (*(loader_ctx->frame_offset)
  11433. < loader_ctx->max_dynamic_offset))
  11434. loader_ctx->dynamic_offset -= 4;
  11435. #endif
  11436. }
  11437. #endif
  11438. #endif
  11439. else {
  11440. set_error_buf(error_buf, error_buf_size,
  11441. "type mismatch");
  11442. goto fail;
  11443. }
  11444. }
  11445. else {
  11446. #if WASM_ENABLE_FAST_INTERP != 0
  11447. skip_label();
  11448. #endif
  11449. }
  11450. break;
  11451. }
  11452. case WASM_OP_SELECT:
  11453. {
  11454. uint8 ref_type;
  11455. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11456. int32 available_stack_cell;
  11457. #if WASM_ENABLE_FAST_INTERP != 0
  11458. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11459. #endif
  11460. POP_I32();
  11461. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11462. - cur_block->stack_cell_num);
  11463. if (available_stack_cell <= 0
  11464. && !cur_block->is_stack_polymorphic) {
  11465. set_error_buf(error_buf, error_buf_size,
  11466. "type mismatch or invalid result arity, "
  11467. "opcode select was found "
  11468. "but stack was empty");
  11469. goto fail;
  11470. }
  11471. if (available_stack_cell > 0) {
  11472. switch (*(loader_ctx->frame_ref - 1)) {
  11473. case VALUE_TYPE_I32:
  11474. case VALUE_TYPE_F32:
  11475. case VALUE_TYPE_ANY:
  11476. break;
  11477. case VALUE_TYPE_I64:
  11478. case VALUE_TYPE_F64:
  11479. #if WASM_ENABLE_FAST_INTERP == 0
  11480. *(p - 1) = WASM_OP_SELECT_64;
  11481. #else
  11482. if (loader_ctx->p_code_compiled) {
  11483. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11484. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11485. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11486. *(void **)(p_code_compiled_tmp
  11487. - sizeof(void *)) =
  11488. handle_table[opcode_tmp];
  11489. #elif UINTPTR_MAX == UINT64_MAX
  11490. /* emit int32 relative offset in 64-bit target
  11491. */
  11492. int32 offset =
  11493. (int32)((uint8 *)handle_table[opcode_tmp]
  11494. - (uint8 *)handle_table[0]);
  11495. *(int32 *)(p_code_compiled_tmp
  11496. - sizeof(int32)) = offset;
  11497. #else
  11498. /* emit uint32 label address in 32-bit target */
  11499. *(uint32 *)(p_code_compiled_tmp
  11500. - sizeof(uint32)) =
  11501. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11502. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11503. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11504. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11505. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11506. #else
  11507. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11508. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11509. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11510. }
  11511. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11512. break;
  11513. #if WASM_ENABLE_SIMD != 0
  11514. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11515. || (WASM_ENABLE_FAST_INTERP != 0)
  11516. case VALUE_TYPE_V128:
  11517. #if WASM_ENABLE_FAST_INTERP == 0
  11518. *(p - 1) = WASM_OP_SELECT_128;
  11519. #else
  11520. if (loader_ctx->p_code_compiled) {
  11521. uint8 opcode_tmp = WASM_OP_SELECT_128;
  11522. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11523. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11524. *(void **)(p_code_compiled_tmp
  11525. - sizeof(void *)) =
  11526. handle_table[opcode_tmp];
  11527. #elif UINTPTR_MAX == UINT64_MAX
  11528. /* emit int32 relative offset in 64-bit target
  11529. */
  11530. int32 offset =
  11531. (int32)((uint8 *)handle_table[opcode_tmp]
  11532. - (uint8 *)handle_table[0]);
  11533. *(int32 *)(p_code_compiled_tmp
  11534. - sizeof(int32)) = offset;
  11535. #else
  11536. /* emit uint32 label address in 32-bit target */
  11537. *(uint32 *)(p_code_compiled_tmp
  11538. - sizeof(uint32)) =
  11539. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11540. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11541. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11542. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11543. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11544. #else
  11545. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11546. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11547. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11548. }
  11549. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11550. break;
  11551. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  11552. (WASM_ENABLE_FAST_INTERP != 0) */
  11553. #endif /* WASM_ENABLE_SIMD != 0 */
  11554. default:
  11555. {
  11556. set_error_buf(error_buf, error_buf_size,
  11557. "type mismatch");
  11558. goto fail;
  11559. }
  11560. }
  11561. ref_type = *(loader_ctx->frame_ref - 1);
  11562. #if WASM_ENABLE_FAST_INTERP != 0
  11563. POP_OFFSET_TYPE(ref_type);
  11564. POP_TYPE(ref_type);
  11565. POP_OFFSET_TYPE(ref_type);
  11566. POP_TYPE(ref_type);
  11567. PUSH_OFFSET_TYPE(ref_type);
  11568. PUSH_TYPE(ref_type);
  11569. #else
  11570. POP2_AND_PUSH(ref_type, ref_type);
  11571. #endif
  11572. }
  11573. else {
  11574. #if WASM_ENABLE_FAST_INTERP != 0
  11575. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11576. #endif
  11577. PUSH_TYPE(VALUE_TYPE_ANY);
  11578. }
  11579. break;
  11580. }
  11581. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11582. case WASM_OP_SELECT_T:
  11583. {
  11584. uint8 vec_len, type;
  11585. #if WASM_ENABLE_GC != 0
  11586. WASMRefType *ref_type = NULL;
  11587. #endif
  11588. #if WASM_ENABLE_FAST_INTERP != 0
  11589. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11590. #endif
  11591. pb_read_leb_uint32(p, p_end, vec_len);
  11592. if (vec_len != 1) {
  11593. /* typed select must have exactly one result */
  11594. set_error_buf(error_buf, error_buf_size,
  11595. "invalid result arity");
  11596. goto fail;
  11597. }
  11598. #if WASM_ENABLE_GC == 0
  11599. CHECK_BUF(p, p_end, 1);
  11600. type = read_uint8(p);
  11601. if (!is_valid_value_type_for_interpreter(type)) {
  11602. set_error_buf(error_buf, error_buf_size,
  11603. "unknown value type");
  11604. goto fail;
  11605. }
  11606. #else
  11607. p_org = p + 1;
  11608. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11609. module->type_count, &need_ref_type_map,
  11610. &wasm_ref_type, false, error_buf,
  11611. error_buf_size)) {
  11612. goto fail;
  11613. }
  11614. type = wasm_ref_type.ref_type;
  11615. if (need_ref_type_map) {
  11616. if (!(ref_type = reftype_set_insert(
  11617. module->ref_type_set, &wasm_ref_type, error_buf,
  11618. error_buf_size))) {
  11619. goto fail;
  11620. }
  11621. }
  11622. #if WASM_ENABLE_FAST_INTERP == 0
  11623. while (p_org < p) {
  11624. #if WASM_ENABLE_DEBUG_INTERP != 0
  11625. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11626. error_buf_size)) {
  11627. goto fail;
  11628. }
  11629. #endif
  11630. /* Ignore extra bytes for interpreter */
  11631. *p_org++ = WASM_OP_NOP;
  11632. }
  11633. #endif
  11634. #endif /* end of WASM_ENABLE_GC == 0 */
  11635. POP_I32();
  11636. #if WASM_ENABLE_FAST_INTERP != 0
  11637. if (loader_ctx->p_code_compiled) {
  11638. uint8 opcode_tmp = WASM_OP_SELECT;
  11639. if (type == VALUE_TYPE_V128) {
  11640. #if WASM_ENABLE_JIT != 0 \
  11641. || WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_SIMD != 0
  11642. opcode_tmp = WASM_OP_SELECT_128;
  11643. #else
  11644. set_error_buf(error_buf, error_buf_size,
  11645. "v128 value type requires simd feature");
  11646. #endif
  11647. }
  11648. else {
  11649. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11650. opcode_tmp = WASM_OP_SELECT_64;
  11651. #if WASM_ENABLE_GC != 0
  11652. if (wasm_is_type_reftype(type))
  11653. opcode_tmp = WASM_OP_SELECT_T;
  11654. #endif
  11655. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11656. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11657. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11658. handle_table[opcode_tmp];
  11659. #else
  11660. #if UINTPTR_MAX == UINT64_MAX
  11661. /* emit int32 relative offset in 64-bit target */
  11662. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11663. - (uint8 *)handle_table[0]);
  11664. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11665. offset;
  11666. #else
  11667. /* emit uint32 label address in 32-bit target */
  11668. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11669. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11670. #endif
  11671. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11672. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11673. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11674. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11675. #else
  11676. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11677. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11678. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11679. }
  11680. }
  11681. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11682. POP_REF(type);
  11683. #if WASM_ENABLE_GC != 0
  11684. if (need_ref_type_map) {
  11685. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11686. wasm_reftype_struct_size(ref_type));
  11687. }
  11688. #endif
  11689. POP_REF(type);
  11690. #if WASM_ENABLE_GC != 0
  11691. if (need_ref_type_map) {
  11692. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11693. wasm_reftype_struct_size(ref_type));
  11694. }
  11695. #endif
  11696. PUSH_REF(type);
  11697. #if WASM_ENABLE_WAMR_COMPILER != 0
  11698. module->is_ref_types_used = true;
  11699. #endif
  11700. (void)vec_len;
  11701. break;
  11702. }
  11703. /* table.get x. tables[x]. [it] -> [t] */
  11704. /* table.set x. tables[x]. [it t] -> [] */
  11705. case WASM_OP_TABLE_GET:
  11706. case WASM_OP_TABLE_SET:
  11707. {
  11708. uint8 decl_ref_type;
  11709. #if WASM_ENABLE_GC != 0
  11710. WASMRefType *ref_type;
  11711. #endif
  11712. pb_read_leb_uint32(p, p_end, table_idx);
  11713. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11714. #if WASM_ENABLE_GC != 0
  11715. (void **)&ref_type,
  11716. #else
  11717. NULL,
  11718. #endif
  11719. error_buf, error_buf_size))
  11720. goto fail;
  11721. #if WASM_ENABLE_GC != 0
  11722. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11723. bh_assert(ref_type);
  11724. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11725. ref_type, wasm_reftype_struct_size(ref_type));
  11726. }
  11727. #endif
  11728. #if WASM_ENABLE_FAST_INTERP != 0
  11729. emit_uint32(loader_ctx, table_idx);
  11730. #endif
  11731. #if WASM_ENABLE_MEMORY64 != 0
  11732. table_elem_idx_type = is_table_64bit(module, table_idx)
  11733. ? VALUE_TYPE_I64
  11734. : VALUE_TYPE_I32;
  11735. #endif
  11736. if (opcode == WASM_OP_TABLE_GET) {
  11737. POP_TBL_ELEM_IDX();
  11738. #if WASM_ENABLE_FAST_INTERP != 0
  11739. PUSH_OFFSET_TYPE(decl_ref_type);
  11740. #endif
  11741. PUSH_TYPE(decl_ref_type);
  11742. }
  11743. else {
  11744. #if WASM_ENABLE_FAST_INTERP != 0
  11745. POP_OFFSET_TYPE(decl_ref_type);
  11746. #endif
  11747. POP_TYPE(decl_ref_type);
  11748. POP_TBL_ELEM_IDX();
  11749. }
  11750. #if WASM_ENABLE_WAMR_COMPILER != 0
  11751. module->is_ref_types_used = true;
  11752. #endif
  11753. break;
  11754. }
  11755. case WASM_OP_REF_NULL:
  11756. {
  11757. uint8 ref_type;
  11758. #if WASM_ENABLE_GC == 0
  11759. CHECK_BUF(p, p_end, 1);
  11760. ref_type = read_uint8(p);
  11761. if (ref_type != VALUE_TYPE_FUNCREF
  11762. && ref_type != VALUE_TYPE_EXTERNREF) {
  11763. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11764. goto fail;
  11765. }
  11766. #else
  11767. pb_read_leb_int32(p, p_end, heap_type);
  11768. if (heap_type >= 0) {
  11769. if (!check_type_index(module, module->type_count, heap_type,
  11770. error_buf, error_buf_size)) {
  11771. goto fail;
  11772. }
  11773. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11774. true, heap_type);
  11775. ref_type = wasm_ref_type.ref_type;
  11776. }
  11777. else {
  11778. if (!wasm_is_valid_heap_type(heap_type)) {
  11779. set_error_buf(error_buf, error_buf_size,
  11780. "unknown type");
  11781. goto fail;
  11782. }
  11783. ref_type = (uint8)((int32)0x80 + heap_type);
  11784. }
  11785. #endif /* end of WASM_ENABLE_GC == 0 */
  11786. #if WASM_ENABLE_FAST_INTERP != 0
  11787. PUSH_OFFSET_TYPE(ref_type);
  11788. #endif
  11789. PUSH_TYPE(ref_type);
  11790. #if WASM_ENABLE_WAMR_COMPILER != 0
  11791. module->is_ref_types_used = true;
  11792. #endif
  11793. break;
  11794. }
  11795. case WASM_OP_REF_IS_NULL:
  11796. {
  11797. #if WASM_ENABLE_GC == 0
  11798. #if WASM_ENABLE_FAST_INTERP != 0
  11799. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11800. int32 block_stack_cell_num =
  11801. (int32)(loader_ctx->stack_cell_num
  11802. - cur_block->stack_cell_num);
  11803. if (block_stack_cell_num <= 0) {
  11804. if (!cur_block->is_stack_polymorphic) {
  11805. set_error_buf(
  11806. error_buf, error_buf_size,
  11807. "type mismatch: expect data but stack was empty");
  11808. goto fail;
  11809. }
  11810. }
  11811. else {
  11812. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11813. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11814. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11815. if (!wasm_loader_pop_frame_ref_offset(
  11816. loader_ctx, *(loader_ctx->frame_ref - 1),
  11817. error_buf, error_buf_size)) {
  11818. goto fail;
  11819. }
  11820. }
  11821. else {
  11822. set_error_buf(error_buf, error_buf_size,
  11823. "type mismatch");
  11824. goto fail;
  11825. }
  11826. }
  11827. #else
  11828. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11829. error_buf, error_buf_size)
  11830. && !wasm_loader_pop_frame_ref(loader_ctx,
  11831. VALUE_TYPE_EXTERNREF,
  11832. error_buf, error_buf_size)) {
  11833. goto fail;
  11834. }
  11835. #endif
  11836. #else /* else of WASM_ENABLE_GC == 0 */
  11837. uint8 type;
  11838. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11839. error_buf, error_buf_size)) {
  11840. goto fail;
  11841. }
  11842. #endif
  11843. PUSH_I32();
  11844. #if WASM_ENABLE_WAMR_COMPILER != 0
  11845. module->is_ref_types_used = true;
  11846. #endif
  11847. break;
  11848. }
  11849. case WASM_OP_REF_FUNC:
  11850. {
  11851. pb_read_leb_uint32(p, p_end, func_idx);
  11852. if (!check_function_index(module, func_idx, error_buf,
  11853. error_buf_size)) {
  11854. goto fail;
  11855. }
  11856. /* Refer to a forward-declared function:
  11857. the function must be an import, exported, or present in
  11858. a table elem segment or global initializer to be used as
  11859. the operand to ref.func */
  11860. if (func_idx >= module->import_function_count) {
  11861. WASMTableSeg *table_seg = module->table_segments;
  11862. bool func_declared = false;
  11863. uint32 j;
  11864. for (i = 0; i < module->global_count; i++) {
  11865. if (module->globals[i].type.val_type
  11866. == VALUE_TYPE_FUNCREF
  11867. && module->globals[i].init_expr.init_expr_type
  11868. == INIT_EXPR_TYPE_FUNCREF_CONST
  11869. && module->globals[i].init_expr.u.u32 == func_idx) {
  11870. func_declared = true;
  11871. break;
  11872. }
  11873. }
  11874. if (!func_declared) {
  11875. /* Check whether the function is declared in table segs,
  11876. note that it doesn't matter whether the table seg's
  11877. mode is passive, active or declarative. */
  11878. for (i = 0; i < module->table_seg_count;
  11879. i++, table_seg++) {
  11880. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11881. #if WASM_ENABLE_GC != 0
  11882. /* elem type is (ref null? func) or
  11883. (ref null? $t) */
  11884. || ((table_seg->elem_type
  11885. == REF_TYPE_HT_NON_NULLABLE
  11886. || table_seg->elem_type
  11887. == REF_TYPE_HT_NULLABLE)
  11888. && (table_seg->elem_ref_type->ref_ht_common
  11889. .heap_type
  11890. == HEAP_TYPE_FUNC
  11891. || table_seg->elem_ref_type
  11892. ->ref_ht_common.heap_type
  11893. > 0))
  11894. #endif
  11895. ) {
  11896. for (j = 0; j < table_seg->value_count; j++) {
  11897. if (table_seg->init_values[j].u.ref_index
  11898. == func_idx) {
  11899. func_declared = true;
  11900. break;
  11901. }
  11902. }
  11903. }
  11904. }
  11905. }
  11906. if (!func_declared) {
  11907. /* Check whether the function is exported */
  11908. for (i = 0; i < module->export_count; i++) {
  11909. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11910. && module->exports[i].index == func_idx) {
  11911. func_declared = true;
  11912. break;
  11913. }
  11914. }
  11915. }
  11916. if (!func_declared) {
  11917. set_error_buf(error_buf, error_buf_size,
  11918. "undeclared function reference");
  11919. goto fail;
  11920. }
  11921. }
  11922. #if WASM_ENABLE_FAST_INTERP != 0
  11923. emit_uint32(loader_ctx, func_idx);
  11924. #endif
  11925. #if WASM_ENABLE_GC == 0
  11926. PUSH_FUNCREF();
  11927. #else
  11928. if (func_idx < module->import_function_count)
  11929. type_idx =
  11930. module->import_functions[func_idx].u.function.type_idx;
  11931. else
  11932. type_idx = module
  11933. ->functions[func_idx
  11934. - module->import_function_count]
  11935. ->type_idx;
  11936. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11937. false, type_idx);
  11938. PUSH_REF(wasm_ref_type.ref_type);
  11939. #endif
  11940. #if WASM_ENABLE_WAMR_COMPILER != 0
  11941. module->is_ref_types_used = true;
  11942. #endif
  11943. break;
  11944. }
  11945. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11946. #if WASM_ENABLE_GC != 0
  11947. case WASM_OP_REF_AS_NON_NULL:
  11948. case WASM_OP_BR_ON_NULL:
  11949. {
  11950. uint8 type;
  11951. WASMRefType ref_type;
  11952. /* POP (ref null ht) and get the converted (ref ht) */
  11953. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11954. error_buf, error_buf_size)) {
  11955. goto fail;
  11956. }
  11957. if (opcode == WASM_OP_BR_ON_NULL) {
  11958. if (!(frame_csp_tmp =
  11959. check_branch_block(loader_ctx, &p, p_end, opcode,
  11960. error_buf, error_buf_size))) {
  11961. goto fail;
  11962. }
  11963. #if WASM_ENABLE_FAST_INTERP != 0
  11964. disable_emit = true;
  11965. #endif
  11966. }
  11967. /* PUSH the converted (ref ht) */
  11968. if (type != VALUE_TYPE_ANY) {
  11969. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11970. sizeof(WASMRefType));
  11971. }
  11972. PUSH_REF(type);
  11973. break;
  11974. }
  11975. case WASM_OP_BR_ON_NON_NULL:
  11976. {
  11977. uint8 type;
  11978. WASMRefType ref_type;
  11979. uint32 available_stack_cell =
  11980. loader_ctx->stack_cell_num
  11981. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11982. /* POP (ref null ht) and get the converted (ref ht) */
  11983. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11984. error_buf, error_buf_size)) {
  11985. goto fail;
  11986. }
  11987. #if WASM_ENABLE_FAST_INTERP != 0
  11988. disable_emit = true;
  11989. #endif
  11990. /* Temporarily PUSH back (ref ht), check brach block and
  11991. then POP it */
  11992. if (available_stack_cell
  11993. > 0) { /* stack isn't in polymorphic state */
  11994. if (type != VALUE_TYPE_ANY) {
  11995. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11996. &ref_type, sizeof(WASMRefType));
  11997. }
  11998. PUSH_REF(type);
  11999. }
  12000. if (!(frame_csp_tmp =
  12001. check_branch_block(loader_ctx, &p, p_end, opcode,
  12002. error_buf, error_buf_size))) {
  12003. goto fail;
  12004. }
  12005. if (available_stack_cell
  12006. > 0) { /* stack isn't in polymorphic state */
  12007. POP_REF(type);
  12008. #if WASM_ENABLE_FAST_INTERP != 0
  12009. /* Erase the opnd offset emitted by POP_REF() */
  12010. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12011. #endif
  12012. }
  12013. break;
  12014. }
  12015. case WASM_OP_REF_EQ:
  12016. POP_REF(REF_TYPE_EQREF);
  12017. POP_REF(REF_TYPE_EQREF);
  12018. PUSH_I32();
  12019. break;
  12020. #endif /* end of WASM_ENABLE_GC != 0 */
  12021. case WASM_OP_GET_LOCAL:
  12022. {
  12023. p_org = p - 1;
  12024. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12025. PUSH_TYPE(local_type);
  12026. #if WASM_ENABLE_GC != 0
  12027. /* Cannot get a non-nullable and unset local */
  12028. if (local_idx >= param_count
  12029. && wasm_is_reftype_htref_non_nullable(local_type)
  12030. && !wasm_loader_get_local_status(loader_ctx,
  12031. local_idx - param_count)) {
  12032. set_error_buf(error_buf, error_buf_size,
  12033. "uninitialized local");
  12034. goto fail;
  12035. }
  12036. #endif
  12037. #if WASM_ENABLE_FAST_INTERP != 0
  12038. /* Get Local is optimized out */
  12039. skip_label();
  12040. disable_emit = true;
  12041. operand_offset = local_offset;
  12042. PUSH_OFFSET_TYPE(local_type);
  12043. #else
  12044. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12045. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12046. if (local_offset < 0x80
  12047. #if WASM_ENABLE_GC != 0
  12048. && !wasm_is_type_reftype(local_type)
  12049. #endif
  12050. ) {
  12051. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  12052. if (is_32bit_type(local_type)) {
  12053. *p_org++ = (uint8)local_offset;
  12054. }
  12055. else {
  12056. *p_org++ = (uint8)(local_offset | 0x80);
  12057. }
  12058. while (p_org < p) {
  12059. *p_org++ = WASM_OP_NOP;
  12060. }
  12061. }
  12062. #endif
  12063. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12064. break;
  12065. }
  12066. case WASM_OP_SET_LOCAL:
  12067. {
  12068. p_org = p - 1;
  12069. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12070. #if WASM_ENABLE_FAST_INTERP != 0
  12071. if (!(preserve_referenced_local(
  12072. loader_ctx, opcode, local_offset, local_type,
  12073. &preserve_local, error_buf, error_buf_size)))
  12074. goto fail;
  12075. if (local_offset < 256
  12076. #if WASM_ENABLE_GC != 0
  12077. && !wasm_is_type_reftype(local_type)
  12078. #endif
  12079. ) {
  12080. skip_label();
  12081. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  12082. if (loader_ctx->p_code_compiled)
  12083. STORE_U16(loader_ctx->p_code_compiled - 2,
  12084. local_offset);
  12085. loader_ctx->frame_offset--;
  12086. loader_ctx->dynamic_offset--;
  12087. }
  12088. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  12089. if (loader_ctx->p_code_compiled)
  12090. STORE_U16(loader_ctx->p_code_compiled - 2,
  12091. local_offset);
  12092. loader_ctx->frame_offset -= 2;
  12093. loader_ctx->dynamic_offset -= 2;
  12094. }
  12095. else {
  12096. if (is_32bit_type(local_type)) {
  12097. emit_label(EXT_OP_SET_LOCAL_FAST);
  12098. emit_byte(loader_ctx, (uint8)local_offset);
  12099. }
  12100. else if (is_64bit_type(local_type)) {
  12101. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  12102. emit_byte(loader_ctx, (uint8)local_offset);
  12103. }
  12104. #if WASM_ENABLE_SIMDE != 0
  12105. else if (local_type == VALUE_TYPE_V128) {
  12106. emit_label(EXT_OP_SET_LOCAL_FAST_V128);
  12107. emit_byte(loader_ctx, (uint8)local_offset);
  12108. }
  12109. #endif
  12110. else {
  12111. set_error_buf(error_buf, error_buf_size,
  12112. "unknown local type");
  12113. goto fail;
  12114. }
  12115. POP_OFFSET_TYPE(local_type);
  12116. }
  12117. }
  12118. else { /* local index larger than 255, reserve leb */
  12119. emit_uint32(loader_ctx, local_idx);
  12120. POP_OFFSET_TYPE(local_type);
  12121. }
  12122. #else
  12123. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12124. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12125. if (local_offset < 0x80
  12126. #if WASM_ENABLE_GC != 0
  12127. && !wasm_is_type_reftype(local_type)
  12128. #endif
  12129. ) {
  12130. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  12131. if (is_32bit_type(local_type)) {
  12132. *p_org++ = (uint8)local_offset;
  12133. }
  12134. else {
  12135. *p_org++ = (uint8)(local_offset | 0x80);
  12136. }
  12137. while (p_org < p) {
  12138. *p_org++ = WASM_OP_NOP;
  12139. }
  12140. }
  12141. #endif
  12142. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12143. #if WASM_ENABLE_GC != 0
  12144. if (local_idx >= param_count) {
  12145. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12146. }
  12147. #endif
  12148. POP_TYPE(local_type);
  12149. break;
  12150. }
  12151. case WASM_OP_TEE_LOCAL:
  12152. {
  12153. p_org = p - 1;
  12154. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12155. #if WASM_ENABLE_FAST_INTERP != 0
  12156. /* If the stack is in polymorphic state, do fake pop and push on
  12157. offset stack to keep the depth of offset stack to be the
  12158. same with ref stack */
  12159. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12160. if (cur_block->is_stack_polymorphic) {
  12161. POP_OFFSET_TYPE(local_type);
  12162. PUSH_OFFSET_TYPE(local_type);
  12163. }
  12164. #endif
  12165. POP_TYPE(local_type);
  12166. PUSH_TYPE(local_type);
  12167. #if WASM_ENABLE_FAST_INTERP != 0
  12168. if (!(preserve_referenced_local(
  12169. loader_ctx, opcode, local_offset, local_type,
  12170. &preserve_local, error_buf, error_buf_size)))
  12171. goto fail;
  12172. if (local_offset < 256
  12173. #if WASM_ENABLE_GC != 0
  12174. && !wasm_is_type_reftype(local_type)
  12175. #endif
  12176. ) {
  12177. skip_label();
  12178. if (is_32bit_type(local_type)) {
  12179. emit_label(EXT_OP_TEE_LOCAL_FAST);
  12180. emit_byte(loader_ctx, (uint8)local_offset);
  12181. }
  12182. #if WASM_ENABLE_SIMDE != 0
  12183. else if (local_type == VALUE_TYPE_V128) {
  12184. emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
  12185. emit_byte(loader_ctx, (uint8)local_offset);
  12186. }
  12187. #endif
  12188. else {
  12189. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  12190. emit_byte(loader_ctx, (uint8)local_offset);
  12191. }
  12192. }
  12193. else { /* local index larger than 255, reserve leb */
  12194. emit_uint32(loader_ctx, local_idx);
  12195. }
  12196. emit_operand(loader_ctx,
  12197. *(loader_ctx->frame_offset
  12198. - wasm_value_type_cell_num(local_type)));
  12199. #else
  12200. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12201. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12202. if (local_offset < 0x80
  12203. #if WASM_ENABLE_GC != 0
  12204. && !wasm_is_type_reftype(local_type)
  12205. #endif
  12206. ) {
  12207. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  12208. if (is_32bit_type(local_type)) {
  12209. *p_org++ = (uint8)local_offset;
  12210. }
  12211. else {
  12212. *p_org++ = (uint8)(local_offset | 0x80);
  12213. }
  12214. while (p_org < p) {
  12215. *p_org++ = WASM_OP_NOP;
  12216. }
  12217. }
  12218. #endif
  12219. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12220. #if WASM_ENABLE_GC != 0
  12221. if (local_idx >= param_count) {
  12222. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12223. }
  12224. #endif
  12225. break;
  12226. }
  12227. case WASM_OP_GET_GLOBAL:
  12228. {
  12229. #if WASM_ENABLE_GC != 0
  12230. WASMRefType *ref_type;
  12231. #endif
  12232. p_org = p - 1;
  12233. pb_read_leb_uint32(p, p_end, global_idx);
  12234. if (global_idx >= global_count) {
  12235. set_error_buf(error_buf, error_buf_size, "unknown global");
  12236. goto fail;
  12237. }
  12238. global_type = global_idx < module->import_global_count
  12239. ? module->import_globals[global_idx]
  12240. .u.global.type.val_type
  12241. : module
  12242. ->globals[global_idx
  12243. - module->import_global_count]
  12244. .type.val_type;
  12245. #if WASM_ENABLE_GC != 0
  12246. ref_type =
  12247. global_idx < module->import_global_count
  12248. ? module->import_globals[global_idx].u.global.ref_type
  12249. : module
  12250. ->globals[global_idx
  12251. - module->import_global_count]
  12252. .ref_type;
  12253. if (wasm_is_type_multi_byte_type(global_type)) {
  12254. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12255. wasm_reftype_struct_size(ref_type));
  12256. }
  12257. #endif
  12258. PUSH_TYPE(global_type);
  12259. #if WASM_ENABLE_FAST_INTERP == 0
  12260. if (global_type == VALUE_TYPE_I64
  12261. || global_type == VALUE_TYPE_F64) {
  12262. #if WASM_ENABLE_DEBUG_INTERP != 0
  12263. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12264. error_buf_size)) {
  12265. goto fail;
  12266. }
  12267. #endif
  12268. *p_org = WASM_OP_GET_GLOBAL_64;
  12269. }
  12270. #else /* else of WASM_ENABLE_FAST_INTERP */
  12271. if (global_type == VALUE_TYPE_I64
  12272. || global_type == VALUE_TYPE_F64) {
  12273. skip_label();
  12274. emit_label(WASM_OP_GET_GLOBAL_64);
  12275. }
  12276. #if WASM_ENABLE_SIMDE != 0
  12277. if (global_type == VALUE_TYPE_V128) {
  12278. skip_label();
  12279. emit_label(WASM_OP_GET_GLOBAL_V128);
  12280. }
  12281. #endif /* end of WASM_ENABLE_SIMDE */
  12282. emit_uint32(loader_ctx, global_idx);
  12283. PUSH_OFFSET_TYPE(global_type);
  12284. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12285. break;
  12286. }
  12287. case WASM_OP_SET_GLOBAL:
  12288. {
  12289. bool is_mutable = false;
  12290. #if WASM_ENABLE_GC != 0
  12291. WASMRefType *ref_type;
  12292. #endif
  12293. p_org = p - 1;
  12294. pb_read_leb_uint32(p, p_end, global_idx);
  12295. if (global_idx >= global_count) {
  12296. set_error_buf(error_buf, error_buf_size, "unknown global");
  12297. goto fail;
  12298. }
  12299. is_mutable = global_idx < module->import_global_count
  12300. ? module->import_globals[global_idx]
  12301. .u.global.type.is_mutable
  12302. : module
  12303. ->globals[global_idx
  12304. - module->import_global_count]
  12305. .type.is_mutable;
  12306. if (!is_mutable) {
  12307. #if WASM_ENABLE_GC == 0
  12308. set_error_buf(error_buf, error_buf_size,
  12309. "global is immutable");
  12310. #else
  12311. set_error_buf(error_buf, error_buf_size,
  12312. "immutable global");
  12313. #endif
  12314. goto fail;
  12315. }
  12316. global_type = global_idx < module->import_global_count
  12317. ? module->import_globals[global_idx]
  12318. .u.global.type.val_type
  12319. : module
  12320. ->globals[global_idx
  12321. - module->import_global_count]
  12322. .type.val_type;
  12323. #if WASM_ENABLE_GC != 0
  12324. ref_type =
  12325. global_idx < module->import_global_count
  12326. ? module->import_globals[global_idx].u.global.ref_type
  12327. : module
  12328. ->globals[global_idx
  12329. - module->import_global_count]
  12330. .ref_type;
  12331. if (wasm_is_type_multi_byte_type(global_type)) {
  12332. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12333. wasm_reftype_struct_size(ref_type));
  12334. }
  12335. #endif
  12336. #if WASM_ENABLE_FAST_INTERP == 0
  12337. if (global_type == VALUE_TYPE_I64
  12338. || global_type == VALUE_TYPE_F64) {
  12339. #if WASM_ENABLE_DEBUG_INTERP != 0
  12340. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12341. error_buf_size)) {
  12342. goto fail;
  12343. }
  12344. #endif
  12345. *p_org = WASM_OP_SET_GLOBAL_64;
  12346. }
  12347. else if (module->aux_stack_size > 0
  12348. && global_idx == module->aux_stack_top_global_index) {
  12349. #if WASM_ENABLE_DEBUG_INTERP != 0
  12350. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12351. error_buf_size)) {
  12352. goto fail;
  12353. }
  12354. #endif
  12355. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12356. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12357. func->has_op_set_global_aux_stack = true;
  12358. #endif
  12359. }
  12360. #else /* else of WASM_ENABLE_FAST_INTERP */
  12361. if (global_type == VALUE_TYPE_I64
  12362. || global_type == VALUE_TYPE_F64) {
  12363. skip_label();
  12364. emit_label(WASM_OP_SET_GLOBAL_64);
  12365. }
  12366. else if (module->aux_stack_size > 0
  12367. && global_idx == module->aux_stack_top_global_index) {
  12368. skip_label();
  12369. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12370. }
  12371. #if WASM_ENABLE_SIMDE != 0
  12372. else if (global_type == VALUE_TYPE_V128) {
  12373. skip_label();
  12374. emit_label(WASM_OP_SET_GLOBAL_V128);
  12375. }
  12376. #endif /* end of WASM_ENABLE_SIMDE */
  12377. emit_uint32(loader_ctx, global_idx);
  12378. POP_OFFSET_TYPE(global_type);
  12379. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12380. POP_TYPE(global_type);
  12381. break;
  12382. }
  12383. /* load */
  12384. case WASM_OP_I32_LOAD:
  12385. case WASM_OP_I32_LOAD8_S:
  12386. case WASM_OP_I32_LOAD8_U:
  12387. case WASM_OP_I32_LOAD16_S:
  12388. case WASM_OP_I32_LOAD16_U:
  12389. case WASM_OP_I64_LOAD:
  12390. case WASM_OP_I64_LOAD8_S:
  12391. case WASM_OP_I64_LOAD8_U:
  12392. case WASM_OP_I64_LOAD16_S:
  12393. case WASM_OP_I64_LOAD16_U:
  12394. case WASM_OP_I64_LOAD32_S:
  12395. case WASM_OP_I64_LOAD32_U:
  12396. case WASM_OP_F32_LOAD:
  12397. case WASM_OP_F64_LOAD:
  12398. /* store */
  12399. case WASM_OP_I32_STORE:
  12400. case WASM_OP_I32_STORE8:
  12401. case WASM_OP_I32_STORE16:
  12402. case WASM_OP_I64_STORE:
  12403. case WASM_OP_I64_STORE8:
  12404. case WASM_OP_I64_STORE16:
  12405. case WASM_OP_I64_STORE32:
  12406. case WASM_OP_F32_STORE:
  12407. case WASM_OP_F64_STORE:
  12408. {
  12409. #if WASM_ENABLE_FAST_INTERP != 0
  12410. /* change F32/F64 into I32/I64 */
  12411. if (opcode == WASM_OP_F32_LOAD) {
  12412. skip_label();
  12413. emit_label(WASM_OP_I32_LOAD);
  12414. }
  12415. else if (opcode == WASM_OP_F64_LOAD) {
  12416. skip_label();
  12417. emit_label(WASM_OP_I64_LOAD);
  12418. }
  12419. else if (opcode == WASM_OP_F32_STORE) {
  12420. skip_label();
  12421. emit_label(WASM_OP_I32_STORE);
  12422. }
  12423. else if (opcode == WASM_OP_F64_STORE) {
  12424. skip_label();
  12425. emit_label(WASM_OP_I64_STORE);
  12426. }
  12427. #endif
  12428. CHECK_MEMORY();
  12429. pb_read_leb_memarg(p, p_end, align); /* align */
  12430. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12431. if (!check_memory_access_align(opcode, align, error_buf,
  12432. error_buf_size)) {
  12433. goto fail;
  12434. }
  12435. #if WASM_ENABLE_FAST_INTERP != 0
  12436. emit_uint32(loader_ctx, mem_offset);
  12437. #endif
  12438. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12439. func->has_memory_operations = true;
  12440. #endif
  12441. switch (opcode) {
  12442. /* load */
  12443. case WASM_OP_I32_LOAD:
  12444. case WASM_OP_I32_LOAD8_S:
  12445. case WASM_OP_I32_LOAD8_U:
  12446. case WASM_OP_I32_LOAD16_S:
  12447. case WASM_OP_I32_LOAD16_U:
  12448. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12449. break;
  12450. case WASM_OP_I64_LOAD:
  12451. case WASM_OP_I64_LOAD8_S:
  12452. case WASM_OP_I64_LOAD8_U:
  12453. case WASM_OP_I64_LOAD16_S:
  12454. case WASM_OP_I64_LOAD16_U:
  12455. case WASM_OP_I64_LOAD32_S:
  12456. case WASM_OP_I64_LOAD32_U:
  12457. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12458. break;
  12459. case WASM_OP_F32_LOAD:
  12460. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12461. break;
  12462. case WASM_OP_F64_LOAD:
  12463. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12464. break;
  12465. /* store */
  12466. case WASM_OP_I32_STORE:
  12467. case WASM_OP_I32_STORE8:
  12468. case WASM_OP_I32_STORE16:
  12469. POP_I32();
  12470. POP_MEM_OFFSET();
  12471. break;
  12472. case WASM_OP_I64_STORE:
  12473. case WASM_OP_I64_STORE8:
  12474. case WASM_OP_I64_STORE16:
  12475. case WASM_OP_I64_STORE32:
  12476. POP_I64();
  12477. POP_MEM_OFFSET();
  12478. break;
  12479. case WASM_OP_F32_STORE:
  12480. POP_F32();
  12481. POP_MEM_OFFSET();
  12482. break;
  12483. case WASM_OP_F64_STORE:
  12484. POP_F64();
  12485. POP_MEM_OFFSET();
  12486. break;
  12487. default:
  12488. break;
  12489. }
  12490. break;
  12491. }
  12492. case WASM_OP_MEMORY_SIZE:
  12493. CHECK_MEMORY();
  12494. pb_read_leb_uint32(p, p_end, memidx);
  12495. check_memidx(module, memidx);
  12496. PUSH_PAGE_COUNT();
  12497. module->possible_memory_grow = true;
  12498. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12499. func->has_memory_operations = true;
  12500. #endif
  12501. break;
  12502. case WASM_OP_MEMORY_GROW:
  12503. CHECK_MEMORY();
  12504. pb_read_leb_uint32(p, p_end, memidx);
  12505. check_memidx(module, memidx);
  12506. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12507. module->possible_memory_grow = true;
  12508. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12509. || WASM_ENABLE_WAMR_COMPILER != 0
  12510. func->has_op_memory_grow = true;
  12511. #endif
  12512. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12513. func->has_memory_operations = true;
  12514. #endif
  12515. break;
  12516. case WASM_OP_I32_CONST:
  12517. pb_read_leb_int32(p, p_end, i32_const);
  12518. #if WASM_ENABLE_FAST_INTERP != 0
  12519. skip_label();
  12520. disable_emit = true;
  12521. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12522. if (operand_offset == 0) {
  12523. disable_emit = false;
  12524. emit_label(WASM_OP_I32_CONST);
  12525. emit_uint32(loader_ctx, i32_const);
  12526. }
  12527. #else
  12528. (void)i32_const;
  12529. #endif
  12530. PUSH_I32();
  12531. break;
  12532. case WASM_OP_I64_CONST:
  12533. pb_read_leb_int64(p, p_end, i64_const);
  12534. #if WASM_ENABLE_FAST_INTERP != 0
  12535. skip_label();
  12536. disable_emit = true;
  12537. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12538. if (operand_offset == 0) {
  12539. disable_emit = false;
  12540. emit_label(WASM_OP_I64_CONST);
  12541. emit_uint64(loader_ctx, i64_const);
  12542. }
  12543. #endif
  12544. PUSH_I64();
  12545. break;
  12546. case WASM_OP_F32_CONST:
  12547. CHECK_BUF(p, p_end, sizeof(float32));
  12548. p += sizeof(float32);
  12549. #if WASM_ENABLE_FAST_INTERP != 0
  12550. skip_label();
  12551. disable_emit = true;
  12552. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12553. sizeof(float32));
  12554. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12555. if (operand_offset == 0) {
  12556. disable_emit = false;
  12557. emit_label(WASM_OP_F32_CONST);
  12558. emit_float32(loader_ctx, f32_const);
  12559. }
  12560. #endif
  12561. PUSH_F32();
  12562. break;
  12563. case WASM_OP_F64_CONST:
  12564. CHECK_BUF(p, p_end, sizeof(float64));
  12565. p += sizeof(float64);
  12566. #if WASM_ENABLE_FAST_INTERP != 0
  12567. skip_label();
  12568. disable_emit = true;
  12569. /* Some MCU may require 8-byte align */
  12570. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12571. sizeof(float64));
  12572. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12573. if (operand_offset == 0) {
  12574. disable_emit = false;
  12575. emit_label(WASM_OP_F64_CONST);
  12576. emit_float64(loader_ctx, f64_const);
  12577. }
  12578. #endif
  12579. PUSH_F64();
  12580. break;
  12581. case WASM_OP_I32_EQZ:
  12582. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12583. break;
  12584. case WASM_OP_I32_EQ:
  12585. case WASM_OP_I32_NE:
  12586. case WASM_OP_I32_LT_S:
  12587. case WASM_OP_I32_LT_U:
  12588. case WASM_OP_I32_GT_S:
  12589. case WASM_OP_I32_GT_U:
  12590. case WASM_OP_I32_LE_S:
  12591. case WASM_OP_I32_LE_U:
  12592. case WASM_OP_I32_GE_S:
  12593. case WASM_OP_I32_GE_U:
  12594. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12595. break;
  12596. case WASM_OP_I64_EQZ:
  12597. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12598. break;
  12599. case WASM_OP_I64_EQ:
  12600. case WASM_OP_I64_NE:
  12601. case WASM_OP_I64_LT_S:
  12602. case WASM_OP_I64_LT_U:
  12603. case WASM_OP_I64_GT_S:
  12604. case WASM_OP_I64_GT_U:
  12605. case WASM_OP_I64_LE_S:
  12606. case WASM_OP_I64_LE_U:
  12607. case WASM_OP_I64_GE_S:
  12608. case WASM_OP_I64_GE_U:
  12609. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12610. break;
  12611. case WASM_OP_F32_EQ:
  12612. case WASM_OP_F32_NE:
  12613. case WASM_OP_F32_LT:
  12614. case WASM_OP_F32_GT:
  12615. case WASM_OP_F32_LE:
  12616. case WASM_OP_F32_GE:
  12617. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12618. break;
  12619. case WASM_OP_F64_EQ:
  12620. case WASM_OP_F64_NE:
  12621. case WASM_OP_F64_LT:
  12622. case WASM_OP_F64_GT:
  12623. case WASM_OP_F64_LE:
  12624. case WASM_OP_F64_GE:
  12625. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12626. break;
  12627. case WASM_OP_I32_CLZ:
  12628. case WASM_OP_I32_CTZ:
  12629. case WASM_OP_I32_POPCNT:
  12630. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12631. break;
  12632. case WASM_OP_I32_ADD:
  12633. case WASM_OP_I32_SUB:
  12634. case WASM_OP_I32_MUL:
  12635. case WASM_OP_I32_DIV_S:
  12636. case WASM_OP_I32_DIV_U:
  12637. case WASM_OP_I32_REM_S:
  12638. case WASM_OP_I32_REM_U:
  12639. case WASM_OP_I32_AND:
  12640. case WASM_OP_I32_OR:
  12641. case WASM_OP_I32_XOR:
  12642. case WASM_OP_I32_SHL:
  12643. case WASM_OP_I32_SHR_S:
  12644. case WASM_OP_I32_SHR_U:
  12645. case WASM_OP_I32_ROTL:
  12646. case WASM_OP_I32_ROTR:
  12647. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12648. break;
  12649. case WASM_OP_I64_CLZ:
  12650. case WASM_OP_I64_CTZ:
  12651. case WASM_OP_I64_POPCNT:
  12652. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12653. break;
  12654. case WASM_OP_I64_ADD:
  12655. case WASM_OP_I64_SUB:
  12656. case WASM_OP_I64_MUL:
  12657. case WASM_OP_I64_DIV_S:
  12658. case WASM_OP_I64_DIV_U:
  12659. case WASM_OP_I64_REM_S:
  12660. case WASM_OP_I64_REM_U:
  12661. case WASM_OP_I64_AND:
  12662. case WASM_OP_I64_OR:
  12663. case WASM_OP_I64_XOR:
  12664. case WASM_OP_I64_SHL:
  12665. case WASM_OP_I64_SHR_S:
  12666. case WASM_OP_I64_SHR_U:
  12667. case WASM_OP_I64_ROTL:
  12668. case WASM_OP_I64_ROTR:
  12669. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12670. break;
  12671. case WASM_OP_F32_ABS:
  12672. case WASM_OP_F32_NEG:
  12673. case WASM_OP_F32_CEIL:
  12674. case WASM_OP_F32_FLOOR:
  12675. case WASM_OP_F32_TRUNC:
  12676. case WASM_OP_F32_NEAREST:
  12677. case WASM_OP_F32_SQRT:
  12678. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12679. break;
  12680. case WASM_OP_F32_ADD:
  12681. case WASM_OP_F32_SUB:
  12682. case WASM_OP_F32_MUL:
  12683. case WASM_OP_F32_DIV:
  12684. case WASM_OP_F32_MIN:
  12685. case WASM_OP_F32_MAX:
  12686. case WASM_OP_F32_COPYSIGN:
  12687. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12688. break;
  12689. case WASM_OP_F64_ABS:
  12690. case WASM_OP_F64_NEG:
  12691. case WASM_OP_F64_CEIL:
  12692. case WASM_OP_F64_FLOOR:
  12693. case WASM_OP_F64_TRUNC:
  12694. case WASM_OP_F64_NEAREST:
  12695. case WASM_OP_F64_SQRT:
  12696. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12697. break;
  12698. case WASM_OP_F64_ADD:
  12699. case WASM_OP_F64_SUB:
  12700. case WASM_OP_F64_MUL:
  12701. case WASM_OP_F64_DIV:
  12702. case WASM_OP_F64_MIN:
  12703. case WASM_OP_F64_MAX:
  12704. case WASM_OP_F64_COPYSIGN:
  12705. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12706. break;
  12707. case WASM_OP_I32_WRAP_I64:
  12708. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12709. break;
  12710. case WASM_OP_I32_TRUNC_S_F32:
  12711. case WASM_OP_I32_TRUNC_U_F32:
  12712. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12713. break;
  12714. case WASM_OP_I32_TRUNC_S_F64:
  12715. case WASM_OP_I32_TRUNC_U_F64:
  12716. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12717. break;
  12718. case WASM_OP_I64_EXTEND_S_I32:
  12719. case WASM_OP_I64_EXTEND_U_I32:
  12720. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12721. break;
  12722. case WASM_OP_I64_TRUNC_S_F32:
  12723. case WASM_OP_I64_TRUNC_U_F32:
  12724. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12725. break;
  12726. case WASM_OP_I64_TRUNC_S_F64:
  12727. case WASM_OP_I64_TRUNC_U_F64:
  12728. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12729. break;
  12730. case WASM_OP_F32_CONVERT_S_I32:
  12731. case WASM_OP_F32_CONVERT_U_I32:
  12732. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12733. break;
  12734. case WASM_OP_F32_CONVERT_S_I64:
  12735. case WASM_OP_F32_CONVERT_U_I64:
  12736. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12737. break;
  12738. case WASM_OP_F32_DEMOTE_F64:
  12739. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12740. break;
  12741. case WASM_OP_F64_CONVERT_S_I32:
  12742. case WASM_OP_F64_CONVERT_U_I32:
  12743. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12744. break;
  12745. case WASM_OP_F64_CONVERT_S_I64:
  12746. case WASM_OP_F64_CONVERT_U_I64:
  12747. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12748. break;
  12749. case WASM_OP_F64_PROMOTE_F32:
  12750. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12751. break;
  12752. case WASM_OP_I32_REINTERPRET_F32:
  12753. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12754. break;
  12755. case WASM_OP_I64_REINTERPRET_F64:
  12756. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12757. break;
  12758. case WASM_OP_F32_REINTERPRET_I32:
  12759. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12760. break;
  12761. case WASM_OP_F64_REINTERPRET_I64:
  12762. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12763. break;
  12764. case WASM_OP_I32_EXTEND8_S:
  12765. case WASM_OP_I32_EXTEND16_S:
  12766. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12767. break;
  12768. case WASM_OP_I64_EXTEND8_S:
  12769. case WASM_OP_I64_EXTEND16_S:
  12770. case WASM_OP_I64_EXTEND32_S:
  12771. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12772. break;
  12773. #if WASM_ENABLE_GC != 0
  12774. case WASM_OP_GC_PREFIX:
  12775. {
  12776. uint32 opcode1;
  12777. pb_read_leb_uint32(p, p_end, opcode1);
  12778. #if WASM_ENABLE_FAST_INTERP != 0
  12779. emit_byte(loader_ctx, ((uint8)opcode1));
  12780. #endif
  12781. switch (opcode1) {
  12782. case WASM_OP_STRUCT_NEW:
  12783. case WASM_OP_STRUCT_NEW_DEFAULT:
  12784. {
  12785. pb_read_leb_uint32(p, p_end, type_idx);
  12786. #if WASM_ENABLE_FAST_INTERP != 0
  12787. emit_uint32(loader_ctx, type_idx);
  12788. #endif
  12789. if (!check_type_index(module, module->type_count,
  12790. type_idx, error_buf,
  12791. error_buf_size)) {
  12792. goto fail;
  12793. }
  12794. if (module->types[type_idx]->type_flag
  12795. != WASM_TYPE_STRUCT) {
  12796. set_error_buf(error_buf, error_buf_size,
  12797. "unknown struct type");
  12798. goto fail;
  12799. }
  12800. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12801. int32 j, k;
  12802. uint8 value_type;
  12803. uint32 ref_type_struct_size;
  12804. WASMStructType *struct_type =
  12805. (WASMStructType *)module->types[type_idx];
  12806. k = struct_type->ref_type_map_count - 1;
  12807. for (j = struct_type->field_count - 1; j >= 0;
  12808. j--) {
  12809. value_type = struct_type->fields[j].field_type;
  12810. if (wasm_is_type_reftype(value_type)) {
  12811. if (wasm_is_type_multi_byte_type(
  12812. value_type)) {
  12813. ref_type_struct_size =
  12814. wasm_reftype_struct_size(
  12815. struct_type->ref_type_maps[k]
  12816. .ref_type);
  12817. bh_memcpy_s(
  12818. &wasm_ref_type,
  12819. (uint32)sizeof(WASMRefType),
  12820. struct_type->ref_type_maps[k]
  12821. .ref_type,
  12822. ref_type_struct_size);
  12823. k--;
  12824. }
  12825. POP_REF(value_type);
  12826. }
  12827. else {
  12828. switch (value_type) {
  12829. case VALUE_TYPE_I32:
  12830. case PACKED_TYPE_I8:
  12831. case PACKED_TYPE_I16:
  12832. POP_I32();
  12833. break;
  12834. case VALUE_TYPE_I64:
  12835. POP_I64();
  12836. break;
  12837. case VALUE_TYPE_F32:
  12838. POP_F32();
  12839. break;
  12840. case VALUE_TYPE_F64:
  12841. POP_F64();
  12842. break;
  12843. default:
  12844. set_error_buf(error_buf,
  12845. error_buf_size,
  12846. "unknown type");
  12847. goto fail;
  12848. }
  12849. }
  12850. }
  12851. }
  12852. /* PUSH struct obj, (ref $t) */
  12853. wasm_set_refheaptype_typeidx(
  12854. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12855. PUSH_REF(wasm_ref_type.ref_type);
  12856. break;
  12857. }
  12858. case WASM_OP_STRUCT_GET:
  12859. case WASM_OP_STRUCT_GET_S:
  12860. case WASM_OP_STRUCT_GET_U:
  12861. case WASM_OP_STRUCT_SET:
  12862. {
  12863. WASMStructType *struct_type;
  12864. WASMRefType *ref_type = NULL;
  12865. uint32 field_idx;
  12866. uint8 field_type;
  12867. pb_read_leb_uint32(p, p_end, type_idx);
  12868. #if WASM_ENABLE_FAST_INTERP != 0
  12869. emit_uint32(loader_ctx, type_idx);
  12870. #endif
  12871. if (!check_type_index(module, module->type_count,
  12872. type_idx, error_buf,
  12873. error_buf_size)) {
  12874. goto fail;
  12875. }
  12876. if (module->types[type_idx]->type_flag
  12877. != WASM_TYPE_STRUCT) {
  12878. set_error_buf(error_buf, error_buf_size,
  12879. "unknown struct type");
  12880. goto fail;
  12881. }
  12882. struct_type = (WASMStructType *)module->types[type_idx];
  12883. pb_read_leb_uint32(p, p_end, field_idx);
  12884. #if WASM_ENABLE_FAST_INTERP != 0
  12885. emit_uint32(loader_ctx, field_idx);
  12886. #endif
  12887. if (field_idx >= struct_type->field_count) {
  12888. set_error_buf(error_buf, error_buf_size,
  12889. "unknown struct field");
  12890. goto fail;
  12891. }
  12892. if (opcode1 == WASM_OP_STRUCT_SET
  12893. && !(struct_type->fields[field_idx].field_flags
  12894. & 1)) {
  12895. set_error_buf(error_buf, error_buf_size,
  12896. "field is immutable");
  12897. goto fail;
  12898. }
  12899. field_type = struct_type->fields[field_idx].field_type;
  12900. if (is_packed_type(field_type)) {
  12901. if (opcode1 == WASM_OP_STRUCT_GET) {
  12902. set_error_buf(error_buf, error_buf_size,
  12903. "type mismatch");
  12904. goto fail;
  12905. }
  12906. else {
  12907. field_type = VALUE_TYPE_I32;
  12908. }
  12909. }
  12910. if (wasm_is_type_multi_byte_type(field_type)) {
  12911. ref_type = wasm_reftype_map_find(
  12912. struct_type->ref_type_maps,
  12913. struct_type->ref_type_map_count, field_idx);
  12914. bh_assert(ref_type);
  12915. }
  12916. if (opcode1 == WASM_OP_STRUCT_SET) {
  12917. /* POP field */
  12918. if (wasm_is_type_multi_byte_type(field_type)) {
  12919. bh_memcpy_s(&wasm_ref_type,
  12920. (uint32)sizeof(WASMRefType),
  12921. ref_type,
  12922. wasm_reftype_struct_size(ref_type));
  12923. }
  12924. POP_REF(field_type);
  12925. /* POP struct obj, (ref null $t) */
  12926. wasm_set_refheaptype_typeidx(
  12927. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12928. POP_REF(wasm_ref_type.ref_type);
  12929. }
  12930. else {
  12931. /* POP struct obj, (ref null $t) */
  12932. wasm_set_refheaptype_typeidx(
  12933. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12934. POP_REF(wasm_ref_type.ref_type);
  12935. /* PUSH field */
  12936. if (wasm_is_type_multi_byte_type(field_type)) {
  12937. bh_memcpy_s(&wasm_ref_type,
  12938. (uint32)sizeof(WASMRefType),
  12939. ref_type,
  12940. wasm_reftype_struct_size(ref_type));
  12941. }
  12942. PUSH_REF(field_type);
  12943. }
  12944. break;
  12945. }
  12946. case WASM_OP_ARRAY_NEW:
  12947. case WASM_OP_ARRAY_NEW_DEFAULT:
  12948. case WASM_OP_ARRAY_NEW_FIXED:
  12949. case WASM_OP_ARRAY_NEW_DATA:
  12950. case WASM_OP_ARRAY_NEW_ELEM:
  12951. {
  12952. WASMArrayType *array_type;
  12953. uint8 elem_type;
  12954. uint32 u32 = 0;
  12955. pb_read_leb_uint32(p, p_end, type_idx);
  12956. #if WASM_ENABLE_FAST_INTERP != 0
  12957. emit_uint32(loader_ctx, type_idx);
  12958. #endif
  12959. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12960. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12961. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12962. pb_read_leb_uint32(p, p_end, u32);
  12963. #if WASM_ENABLE_FAST_INTERP != 0
  12964. emit_uint32(loader_ctx, u32);
  12965. #endif
  12966. }
  12967. if (!check_array_type(module, type_idx, error_buf,
  12968. error_buf_size)) {
  12969. goto fail;
  12970. }
  12971. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12972. /* length */
  12973. POP_I32();
  12974. }
  12975. array_type = (WASMArrayType *)module->types[type_idx];
  12976. elem_type = array_type->elem_type;
  12977. if (opcode1 == WASM_OP_ARRAY_NEW
  12978. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12979. if (wasm_is_type_multi_byte_type(elem_type)) {
  12980. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12981. array_type->elem_ref_type,
  12982. wasm_reftype_struct_size(
  12983. array_type->elem_ref_type));
  12984. }
  12985. if (is_packed_type(elem_type)) {
  12986. elem_type = VALUE_TYPE_I32;
  12987. }
  12988. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12989. uint32 N = u32;
  12990. for (i = 0; i < N; i++) {
  12991. if (wasm_is_type_multi_byte_type(
  12992. elem_type)) {
  12993. bh_memcpy_s(
  12994. &wasm_ref_type, sizeof(WASMRefType),
  12995. array_type->elem_ref_type,
  12996. wasm_reftype_struct_size(
  12997. array_type->elem_ref_type));
  12998. }
  12999. POP_REF(elem_type);
  13000. }
  13001. }
  13002. else
  13003. POP_REF(elem_type);
  13004. }
  13005. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  13006. /* offset of data segment */
  13007. POP_I32();
  13008. if (u32 >= module->data_seg_count) {
  13009. set_error_buf(error_buf, error_buf_size,
  13010. "unknown data segment");
  13011. goto fail;
  13012. }
  13013. if (wasm_is_type_reftype(elem_type)) {
  13014. set_error_buf(error_buf, error_buf_size,
  13015. "array elem type mismatch");
  13016. goto fail;
  13017. }
  13018. }
  13019. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13020. WASMTableSeg *table_seg =
  13021. module->table_segments + u32;
  13022. /* offset of element segment */
  13023. POP_I32();
  13024. if (u32 >= module->table_seg_count) {
  13025. set_error_buf(error_buf, error_buf_size,
  13026. "unknown element segment");
  13027. goto fail;
  13028. }
  13029. if (!wasm_reftype_is_subtype_of(
  13030. table_seg->elem_type,
  13031. table_seg->elem_ref_type, elem_type,
  13032. array_type->elem_ref_type, module->types,
  13033. module->type_count)) {
  13034. set_error_buf(error_buf, error_buf_size,
  13035. "array elem type mismatch");
  13036. goto fail;
  13037. }
  13038. }
  13039. /* PUSH array obj, (ref $t) */
  13040. wasm_set_refheaptype_typeidx(
  13041. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13042. PUSH_REF(wasm_ref_type.ref_type);
  13043. break;
  13044. }
  13045. case WASM_OP_ARRAY_GET:
  13046. case WASM_OP_ARRAY_GET_S:
  13047. case WASM_OP_ARRAY_GET_U:
  13048. case WASM_OP_ARRAY_SET:
  13049. {
  13050. uint8 elem_type;
  13051. WASMArrayType *array_type;
  13052. WASMRefType *ref_type = NULL;
  13053. pb_read_leb_uint32(p, p_end, type_idx);
  13054. #if WASM_ENABLE_FAST_INTERP != 0
  13055. emit_uint32(loader_ctx, type_idx);
  13056. #endif
  13057. if (!check_array_type(module, type_idx, error_buf,
  13058. error_buf_size)) {
  13059. goto fail;
  13060. }
  13061. array_type = (WASMArrayType *)module->types[type_idx];
  13062. if (opcode1 == WASM_OP_ARRAY_SET
  13063. && !(array_type->elem_flags & 1)) {
  13064. set_error_buf(error_buf, error_buf_size,
  13065. "array is immutable");
  13066. goto fail;
  13067. }
  13068. elem_type = array_type->elem_type;
  13069. if (is_packed_type(elem_type)) {
  13070. if (opcode1 != WASM_OP_ARRAY_GET_S
  13071. && opcode1 != WASM_OP_ARRAY_GET_U
  13072. && opcode1 != WASM_OP_ARRAY_SET) {
  13073. set_error_buf(error_buf, error_buf_size,
  13074. "type mismatch");
  13075. goto fail;
  13076. }
  13077. else {
  13078. elem_type = VALUE_TYPE_I32;
  13079. }
  13080. }
  13081. ref_type = array_type->elem_ref_type;
  13082. if (opcode1 == WASM_OP_ARRAY_SET) {
  13083. /* POP elem to set */
  13084. if (wasm_is_type_multi_byte_type(elem_type)) {
  13085. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13086. ref_type,
  13087. wasm_reftype_struct_size(ref_type));
  13088. }
  13089. POP_REF(elem_type);
  13090. }
  13091. /* elem idx */
  13092. POP_I32();
  13093. /* POP array obj, (ref null $t) */
  13094. wasm_set_refheaptype_typeidx(
  13095. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13096. POP_REF(wasm_ref_type.ref_type);
  13097. if (opcode1 != WASM_OP_ARRAY_SET) {
  13098. /* PUSH elem */
  13099. if (wasm_is_type_multi_byte_type(elem_type)) {
  13100. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13101. ref_type,
  13102. wasm_reftype_struct_size(ref_type));
  13103. }
  13104. PUSH_REF(elem_type);
  13105. }
  13106. break;
  13107. }
  13108. case WASM_OP_ARRAY_LEN:
  13109. {
  13110. POP_REF(REF_TYPE_ARRAYREF);
  13111. /* length */
  13112. PUSH_I32();
  13113. break;
  13114. }
  13115. case WASM_OP_ARRAY_FILL:
  13116. {
  13117. WASMArrayType *array_type;
  13118. uint8 elem_type;
  13119. /* typeidx */
  13120. pb_read_leb_uint32(p, p_end, type_idx);
  13121. #if WASM_ENABLE_FAST_INTERP != 0
  13122. emit_uint32(loader_ctx, type_idx);
  13123. #endif
  13124. if (!check_array_type(module, type_idx, error_buf,
  13125. error_buf_size)) {
  13126. goto fail;
  13127. }
  13128. array_type = (WASMArrayType *)module->types[type_idx];
  13129. if (!(array_type->elem_flags & 1)) {
  13130. set_error_buf(error_buf, error_buf_size,
  13131. "array is immutable");
  13132. goto fail;
  13133. }
  13134. elem_type = array_type->elem_type;
  13135. if (is_packed_type(elem_type)) {
  13136. elem_type = VALUE_TYPE_I32;
  13137. }
  13138. POP_I32(); /* length */
  13139. #if WASM_ENABLE_FAST_INTERP != 0
  13140. POP_OFFSET_TYPE(elem_type);
  13141. #endif
  13142. POP_TYPE(elem_type);
  13143. POP_I32(); /* start */
  13144. /* POP array obj, (ref null $t) */
  13145. wasm_set_refheaptype_typeidx(
  13146. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13147. POP_REF(wasm_ref_type.ref_type);
  13148. break;
  13149. }
  13150. case WASM_OP_ARRAY_COPY:
  13151. {
  13152. uint32 src_type_idx;
  13153. uint8 src_elem_type, dst_elem_type;
  13154. WASMRefType src_ref_type = { 0 },
  13155. *src_elem_ref_type = NULL;
  13156. WASMRefType dst_ref_type = { 0 },
  13157. *dst_elem_ref_type = NULL;
  13158. WASMArrayType *array_type;
  13159. /* typeidx1 */
  13160. pb_read_leb_uint32(p, p_end, type_idx);
  13161. #if WASM_ENABLE_FAST_INTERP != 0
  13162. emit_uint32(loader_ctx, type_idx);
  13163. #endif
  13164. /* typeidx2 */
  13165. pb_read_leb_uint32(p, p_end, src_type_idx);
  13166. #if WASM_ENABLE_FAST_INTERP != 0
  13167. emit_uint32(loader_ctx, src_type_idx);
  13168. #endif
  13169. if (!check_array_type(module, type_idx, error_buf,
  13170. error_buf_size)) {
  13171. goto fail;
  13172. }
  13173. if (!check_array_type(module, src_type_idx, error_buf,
  13174. error_buf_size)) {
  13175. goto fail;
  13176. }
  13177. POP_I32();
  13178. POP_I32();
  13179. /* POP array obj, (ref null $t) */
  13180. wasm_set_refheaptype_typeidx(
  13181. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  13182. POP_REF(wasm_ref_type.ref_type);
  13183. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  13184. &wasm_ref_type,
  13185. wasm_reftype_struct_size(&wasm_ref_type));
  13186. POP_I32();
  13187. /* POP array obj, (ref null $t) */
  13188. wasm_set_refheaptype_typeidx(
  13189. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13190. POP_REF(wasm_ref_type.ref_type);
  13191. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  13192. &wasm_ref_type,
  13193. wasm_reftype_struct_size(&wasm_ref_type));
  13194. array_type = (WASMArrayType *)module->types[type_idx];
  13195. if (!(array_type->elem_flags & 1)) {
  13196. set_error_buf(error_buf, error_buf_size,
  13197. "destination array is immutable");
  13198. goto fail;
  13199. }
  13200. dst_elem_type = array_type->elem_type;
  13201. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  13202. dst_elem_ref_type = array_type->elem_ref_type;
  13203. }
  13204. array_type =
  13205. (WASMArrayType *)module->types[src_type_idx];
  13206. src_elem_type = array_type->elem_type;
  13207. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  13208. src_elem_ref_type = array_type->elem_ref_type;
  13209. }
  13210. if (!wasm_reftype_is_subtype_of(
  13211. src_elem_type, src_elem_ref_type, dst_elem_type,
  13212. dst_elem_ref_type, module->types,
  13213. module->type_count)) {
  13214. set_error_buf(error_buf, error_buf_size,
  13215. "array types do not match");
  13216. goto fail;
  13217. }
  13218. break;
  13219. }
  13220. case WASM_OP_REF_I31:
  13221. {
  13222. POP_I32();
  13223. wasm_set_refheaptype_common(
  13224. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  13225. PUSH_REF(wasm_ref_type.ref_type);
  13226. break;
  13227. }
  13228. case WASM_OP_I31_GET_S:
  13229. case WASM_OP_I31_GET_U:
  13230. {
  13231. POP_REF(REF_TYPE_I31REF);
  13232. PUSH_I32();
  13233. break;
  13234. }
  13235. case WASM_OP_REF_TEST:
  13236. case WASM_OP_REF_CAST:
  13237. case WASM_OP_REF_TEST_NULLABLE:
  13238. case WASM_OP_REF_CAST_NULLABLE:
  13239. {
  13240. uint8 type;
  13241. pb_read_leb_int32(p, p_end, heap_type);
  13242. #if WASM_ENABLE_FAST_INTERP != 0
  13243. emit_uint32(loader_ctx, (uint32)heap_type);
  13244. #endif
  13245. if (heap_type >= 0) {
  13246. if (!check_type_index(module, module->type_count,
  13247. heap_type, error_buf,
  13248. error_buf_size)) {
  13249. goto fail;
  13250. }
  13251. }
  13252. else {
  13253. if (!wasm_is_valid_heap_type(heap_type)) {
  13254. set_error_buf(error_buf, error_buf_size,
  13255. "unknown type");
  13256. goto fail;
  13257. }
  13258. }
  13259. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13260. &wasm_ref_type, error_buf,
  13261. error_buf_size)) {
  13262. goto fail;
  13263. }
  13264. if (opcode1 == WASM_OP_REF_TEST
  13265. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  13266. PUSH_I32();
  13267. else {
  13268. bool nullable =
  13269. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  13270. : false;
  13271. if (heap_type >= 0 || !nullable) {
  13272. wasm_set_refheaptype_typeidx(
  13273. &wasm_ref_type.ref_ht_typeidx, nullable,
  13274. heap_type);
  13275. PUSH_REF(wasm_ref_type.ref_type);
  13276. }
  13277. else {
  13278. PUSH_REF((uint8)((int32)0x80 + heap_type));
  13279. }
  13280. }
  13281. break;
  13282. }
  13283. case WASM_OP_BR_ON_CAST:
  13284. case WASM_OP_BR_ON_CAST_FAIL:
  13285. {
  13286. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13287. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13288. uint8 type_tmp, castflags;
  13289. uint32 depth;
  13290. int32 heap_type_dst;
  13291. bool src_nullable, dst_nullable;
  13292. CHECK_BUF(p, p_end, 1);
  13293. castflags = read_uint8(p);
  13294. #if WASM_ENABLE_FAST_INTERP != 0
  13295. /* Emit heap_type firstly */
  13296. emit_byte(loader_ctx, castflags);
  13297. #endif
  13298. p_org = p;
  13299. pb_read_leb_uint32(p, p_end, depth);
  13300. pb_read_leb_int32(p, p_end, heap_type);
  13301. #if WASM_ENABLE_FAST_INTERP != 0
  13302. /* Emit heap_type firstly */
  13303. emit_uint32(loader_ctx, (uint32)heap_type);
  13304. #endif
  13305. pb_read_leb_int32(p, p_end, heap_type_dst);
  13306. #if WASM_ENABLE_FAST_INTERP != 0
  13307. /* Emit heap_type firstly */
  13308. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13309. #endif
  13310. (void)depth;
  13311. /*
  13312. * castflags should be 0~3:
  13313. * 0: (non-null, non-null)
  13314. * 1: (null, non-null)
  13315. * 2: (non-null, null)
  13316. * 3: (null, null)
  13317. */
  13318. if (castflags > 3) {
  13319. set_error_buf(error_buf, error_buf_size,
  13320. "invalid castflags");
  13321. break;
  13322. }
  13323. src_nullable =
  13324. (castflags == 1) || (castflags == 3) ? true : false;
  13325. dst_nullable =
  13326. (castflags == 2) || (castflags == 3) ? true : false;
  13327. /* Pop and backup the stack top's ref type */
  13328. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13329. &ref_type_tmp, error_buf,
  13330. error_buf_size)) {
  13331. goto fail;
  13332. }
  13333. /* The reference type rt1 must be valid */
  13334. if (!init_ref_type(module, &ref_type1, src_nullable,
  13335. heap_type, error_buf,
  13336. error_buf_size)) {
  13337. goto fail;
  13338. }
  13339. /* The reference type rt2 must be valid. */
  13340. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13341. heap_type_dst, error_buf,
  13342. error_buf_size)) {
  13343. goto fail;
  13344. }
  13345. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13346. &ref_type2);
  13347. /* The reference type rt2 must match rt1. */
  13348. if (!wasm_reftype_is_subtype_of(
  13349. ref_type2.ref_type, &ref_type2,
  13350. ref_type1.ref_type, &ref_type1, module->types,
  13351. module->type_count)) {
  13352. set_error_buf(error_buf, error_buf_size,
  13353. "type mismatch");
  13354. goto fail;
  13355. }
  13356. p = p_org;
  13357. /* Push ref type casted for branch block check */
  13358. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13359. /* The reference type rt2 must match rt′. */
  13360. type_tmp = ref_type2.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_type2),
  13365. &ref_type2,
  13366. wasm_reftype_struct_size(&ref_type2));
  13367. }
  13368. }
  13369. else {
  13370. /* The reference type rt′1 must match rt′. */
  13371. type_tmp = ref_type_diff.ref_type;
  13372. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13373. bh_memcpy_s(
  13374. &wasm_ref_type,
  13375. wasm_reftype_struct_size(&ref_type_diff),
  13376. &ref_type_diff,
  13377. wasm_reftype_struct_size(&ref_type_diff));
  13378. }
  13379. }
  13380. PUSH_REF(type_tmp);
  13381. if (!(frame_csp_tmp = check_branch_block(
  13382. loader_ctx, &p, p_end, opcode, error_buf,
  13383. error_buf_size))) {
  13384. goto fail;
  13385. }
  13386. /* Ignore heap_types */
  13387. skip_leb_uint32(p, p_end);
  13388. skip_leb_uint32(p, p_end);
  13389. /* Restore the original stack top's ref type */
  13390. POP_REF(type_tmp);
  13391. #if WASM_ENABLE_FAST_INTERP != 0
  13392. /* Erase the opnd offset emitted by POP_REF() */
  13393. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13394. #endif
  13395. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13396. type_tmp = ref_type_diff.ref_type;
  13397. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13398. bh_memcpy_s(
  13399. &wasm_ref_type,
  13400. wasm_reftype_struct_size(&ref_type_diff),
  13401. &ref_type_diff,
  13402. wasm_reftype_struct_size(&ref_type_diff));
  13403. }
  13404. }
  13405. else {
  13406. type_tmp = ref_type2.ref_type;
  13407. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13408. bh_memcpy_s(
  13409. &wasm_ref_type,
  13410. wasm_reftype_struct_size(&ref_type2),
  13411. &ref_type2,
  13412. wasm_reftype_struct_size(&ref_type2));
  13413. }
  13414. }
  13415. PUSH_REF(type_tmp);
  13416. #if WASM_ENABLE_FAST_INTERP != 0
  13417. /* Erase the opnd offset emitted by PUSH_REF() */
  13418. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13419. #endif
  13420. break;
  13421. }
  13422. case WASM_OP_ANY_CONVERT_EXTERN:
  13423. {
  13424. uint8 type;
  13425. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13426. &wasm_ref_type, error_buf,
  13427. error_buf_size)) {
  13428. goto fail;
  13429. }
  13430. if (!(type == REF_TYPE_EXTERNREF
  13431. || (type == REF_TYPE_HT_NON_NULLABLE
  13432. && wasm_ref_type.ref_ht_common.heap_type
  13433. == HEAP_TYPE_EXTERN)
  13434. || type == VALUE_TYPE_ANY)) {
  13435. set_error_buf(error_buf, error_buf_size,
  13436. "type mismatch");
  13437. goto fail;
  13438. }
  13439. if (type == REF_TYPE_EXTERNREF)
  13440. type = REF_TYPE_ANYREF;
  13441. else {
  13442. wasm_ref_type.ref_ht_common.heap_type =
  13443. HEAP_TYPE_ANY;
  13444. }
  13445. PUSH_REF(type);
  13446. break;
  13447. }
  13448. case WASM_OP_EXTERN_CONVERT_ANY:
  13449. {
  13450. uint8 type;
  13451. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13452. &wasm_ref_type, error_buf,
  13453. error_buf_size)) {
  13454. goto fail;
  13455. }
  13456. if (type == REF_TYPE_EXTERNREF
  13457. || ((type == REF_TYPE_HT_NULLABLE
  13458. || type == REF_TYPE_HT_NON_NULLABLE)
  13459. && wasm_ref_type.ref_ht_common.heap_type
  13460. == HEAP_TYPE_EXTERN)) {
  13461. set_error_buf(error_buf, error_buf_size,
  13462. "type mismatch");
  13463. goto fail;
  13464. }
  13465. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13466. /* push (ref null extern) */
  13467. type = REF_TYPE_EXTERNREF;
  13468. }
  13469. else {
  13470. /* push (ref extern) */
  13471. type = REF_TYPE_HT_NON_NULLABLE;
  13472. wasm_set_refheaptype_common(
  13473. &wasm_ref_type.ref_ht_common, false,
  13474. HEAP_TYPE_EXTERN);
  13475. }
  13476. PUSH_REF(type);
  13477. break;
  13478. }
  13479. #if WASM_ENABLE_STRINGREF != 0
  13480. case WASM_OP_STRING_NEW_UTF8:
  13481. case WASM_OP_STRING_NEW_WTF16:
  13482. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13483. case WASM_OP_STRING_NEW_WTF8:
  13484. {
  13485. uint32 memidx;
  13486. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13487. func->has_memory_operations = true;
  13488. #endif
  13489. pb_read_leb_uint32(p, p_end, memidx);
  13490. #if WASM_ENABLE_FAST_INTERP != 0
  13491. emit_uint32(loader_ctx, (uint32)memidx);
  13492. #endif
  13493. POP_I32();
  13494. POP_I32();
  13495. PUSH_REF(REF_TYPE_STRINGREF);
  13496. (void)memidx;
  13497. break;
  13498. }
  13499. case WASM_OP_STRING_CONST:
  13500. {
  13501. uint32 contents;
  13502. pb_read_leb_uint32(p, p_end, contents);
  13503. #if WASM_ENABLE_FAST_INTERP != 0
  13504. emit_uint32(loader_ctx, (uint32)contents);
  13505. #endif
  13506. PUSH_REF(REF_TYPE_STRINGREF);
  13507. (void)contents;
  13508. break;
  13509. }
  13510. case WASM_OP_STRING_MEASURE_UTF8:
  13511. case WASM_OP_STRING_MEASURE_WTF8:
  13512. case WASM_OP_STRING_MEASURE_WTF16:
  13513. {
  13514. POP_STRINGREF();
  13515. PUSH_I32();
  13516. break;
  13517. }
  13518. case WASM_OP_STRING_ENCODE_UTF8:
  13519. case WASM_OP_STRING_ENCODE_WTF16:
  13520. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13521. case WASM_OP_STRING_ENCODE_WTF8:
  13522. {
  13523. uint32 memidx;
  13524. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13525. func->has_memory_operations = true;
  13526. #endif
  13527. pb_read_leb_uint32(p, p_end, memidx);
  13528. #if WASM_ENABLE_FAST_INTERP != 0
  13529. emit_uint32(loader_ctx, (uint32)memidx);
  13530. #endif
  13531. POP_I32();
  13532. POP_STRINGREF();
  13533. PUSH_I32();
  13534. (void)memidx;
  13535. break;
  13536. }
  13537. case WASM_OP_STRING_CONCAT:
  13538. {
  13539. POP_STRINGREF();
  13540. POP_STRINGREF();
  13541. PUSH_REF(REF_TYPE_STRINGREF);
  13542. break;
  13543. }
  13544. case WASM_OP_STRING_EQ:
  13545. {
  13546. POP_STRINGREF();
  13547. POP_STRINGREF();
  13548. PUSH_I32();
  13549. break;
  13550. }
  13551. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13552. {
  13553. POP_STRINGREF();
  13554. PUSH_I32();
  13555. break;
  13556. }
  13557. case WASM_OP_STRING_AS_WTF8:
  13558. {
  13559. POP_STRINGREF();
  13560. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13561. break;
  13562. }
  13563. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13564. {
  13565. POP_I32();
  13566. POP_I32();
  13567. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13568. PUSH_I32();
  13569. break;
  13570. }
  13571. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13572. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13573. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13574. {
  13575. uint32 memidx;
  13576. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13577. func->has_memory_operations = true;
  13578. #endif
  13579. pb_read_leb_uint32(p, p_end, memidx);
  13580. #if WASM_ENABLE_FAST_INTERP != 0
  13581. emit_uint32(loader_ctx, (uint32)memidx);
  13582. #endif
  13583. POP_I32();
  13584. POP_I32();
  13585. POP_I32();
  13586. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13587. PUSH_I32();
  13588. PUSH_I32();
  13589. (void)memidx;
  13590. break;
  13591. }
  13592. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13593. {
  13594. POP_I32();
  13595. POP_I32();
  13596. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13597. PUSH_REF(REF_TYPE_STRINGREF);
  13598. break;
  13599. }
  13600. case WASM_OP_STRING_AS_WTF16:
  13601. {
  13602. POP_STRINGREF();
  13603. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13604. break;
  13605. }
  13606. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13607. {
  13608. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13609. PUSH_I32();
  13610. break;
  13611. }
  13612. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13613. {
  13614. POP_I32();
  13615. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13616. PUSH_I32();
  13617. break;
  13618. }
  13619. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13620. {
  13621. uint32 memidx;
  13622. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13623. func->has_memory_operations = true;
  13624. #endif
  13625. pb_read_leb_uint32(p, p_end, memidx);
  13626. #if WASM_ENABLE_FAST_INTERP != 0
  13627. emit_uint32(loader_ctx, (uint32)memidx);
  13628. #endif
  13629. POP_I32();
  13630. POP_I32();
  13631. POP_I32();
  13632. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13633. PUSH_I32();
  13634. (void)memidx;
  13635. break;
  13636. }
  13637. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13638. {
  13639. POP_I32();
  13640. POP_I32();
  13641. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13642. PUSH_REF(REF_TYPE_STRINGREF);
  13643. break;
  13644. }
  13645. case WASM_OP_STRING_AS_ITER:
  13646. {
  13647. POP_STRINGREF();
  13648. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13649. break;
  13650. }
  13651. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13652. {
  13653. POP_REF(REF_TYPE_STRINGVIEWITER);
  13654. PUSH_I32();
  13655. break;
  13656. }
  13657. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13658. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13659. {
  13660. POP_I32();
  13661. POP_REF(REF_TYPE_STRINGVIEWITER);
  13662. PUSH_I32();
  13663. break;
  13664. }
  13665. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13666. {
  13667. POP_I32();
  13668. POP_REF(REF_TYPE_STRINGVIEWITER);
  13669. PUSH_REF(REF_TYPE_STRINGREF);
  13670. break;
  13671. }
  13672. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13673. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13674. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13675. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13676. {
  13677. POP_I32();
  13678. POP_I32();
  13679. POP_REF(REF_TYPE_ARRAYREF);
  13680. PUSH_REF(REF_TYPE_STRINGREF);
  13681. break;
  13682. }
  13683. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13684. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13685. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13686. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13687. {
  13688. POP_I32();
  13689. POP_REF(REF_TYPE_ARRAYREF);
  13690. POP_STRINGREF();
  13691. PUSH_I32();
  13692. break;
  13693. }
  13694. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13695. default:
  13696. set_error_buf_v(error_buf, error_buf_size,
  13697. "%s %02x %02x", "unsupported opcode",
  13698. 0xfb, opcode1);
  13699. goto fail;
  13700. }
  13701. break;
  13702. }
  13703. #endif /* end of WASM_ENABLE_GC != 0 */
  13704. case WASM_OP_MISC_PREFIX:
  13705. {
  13706. uint32 opcode1;
  13707. pb_read_leb_uint32(p, p_end, opcode1);
  13708. #if WASM_ENABLE_FAST_INTERP != 0
  13709. emit_byte(loader_ctx, ((uint8)opcode1));
  13710. #endif
  13711. switch (opcode1) {
  13712. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13713. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13714. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13715. break;
  13716. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13717. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13718. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13719. break;
  13720. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13721. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13722. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13723. break;
  13724. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13725. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13726. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13727. break;
  13728. #if WASM_ENABLE_BULK_MEMORY != 0
  13729. case WASM_OP_MEMORY_INIT:
  13730. {
  13731. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13732. #if WASM_ENABLE_FAST_INTERP != 0
  13733. emit_uint32(loader_ctx, data_seg_idx);
  13734. #endif
  13735. if (module->import_memory_count == 0
  13736. && module->memory_count == 0)
  13737. goto fail_unknown_memory;
  13738. pb_read_leb_uint32(p, p_end, memidx);
  13739. check_memidx(module, memidx);
  13740. if (data_seg_idx >= module->data_seg_count) {
  13741. set_error_buf_v(error_buf, error_buf_size,
  13742. "unknown data segment %d",
  13743. data_seg_idx);
  13744. goto fail;
  13745. }
  13746. if (module->data_seg_count1 == 0)
  13747. goto fail_data_cnt_sec_require;
  13748. POP_I32();
  13749. POP_I32();
  13750. POP_MEM_OFFSET();
  13751. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13752. func->has_memory_operations = true;
  13753. #endif
  13754. #if WASM_ENABLE_WAMR_COMPILER != 0
  13755. module->is_bulk_memory_used = true;
  13756. #endif
  13757. break;
  13758. }
  13759. case WASM_OP_DATA_DROP:
  13760. {
  13761. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13762. #if WASM_ENABLE_FAST_INTERP != 0
  13763. emit_uint32(loader_ctx, data_seg_idx);
  13764. #endif
  13765. if (data_seg_idx >= module->data_seg_count) {
  13766. set_error_buf(error_buf, error_buf_size,
  13767. "unknown data segment");
  13768. goto fail;
  13769. }
  13770. if (module->data_seg_count1 == 0)
  13771. goto fail_data_cnt_sec_require;
  13772. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13773. func->has_memory_operations = true;
  13774. #endif
  13775. #if WASM_ENABLE_WAMR_COMPILER != 0
  13776. module->is_bulk_memory_used = true;
  13777. #endif
  13778. break;
  13779. }
  13780. case WASM_OP_MEMORY_COPY:
  13781. {
  13782. CHECK_BUF(p, p_end, sizeof(int16));
  13783. /* check both src and dst memory index */
  13784. pb_read_leb_uint32(p, p_end, memidx);
  13785. check_memidx(module, memidx);
  13786. pb_read_leb_uint32(p, p_end, memidx);
  13787. check_memidx(module, memidx);
  13788. if (module->import_memory_count == 0
  13789. && module->memory_count == 0)
  13790. goto fail_unknown_memory;
  13791. POP_MEM_OFFSET();
  13792. POP_MEM_OFFSET();
  13793. POP_MEM_OFFSET();
  13794. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13795. func->has_memory_operations = true;
  13796. #endif
  13797. #if WASM_ENABLE_WAMR_COMPILER != 0
  13798. module->is_bulk_memory_used = true;
  13799. #endif
  13800. break;
  13801. }
  13802. case WASM_OP_MEMORY_FILL:
  13803. {
  13804. pb_read_leb_uint32(p, p_end, memidx);
  13805. check_memidx(module, memidx);
  13806. if (module->import_memory_count == 0
  13807. && module->memory_count == 0) {
  13808. goto fail_unknown_memory;
  13809. }
  13810. POP_MEM_OFFSET();
  13811. POP_I32();
  13812. POP_MEM_OFFSET();
  13813. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13814. func->has_memory_operations = true;
  13815. #endif
  13816. #if WASM_ENABLE_WAMR_COMPILER != 0
  13817. module->is_bulk_memory_used = true;
  13818. #endif
  13819. break;
  13820. }
  13821. fail_unknown_memory:
  13822. set_error_buf(error_buf, error_buf_size,
  13823. "unknown memory 0");
  13824. goto fail;
  13825. fail_data_cnt_sec_require:
  13826. set_error_buf(error_buf, error_buf_size,
  13827. "data count section required");
  13828. goto fail;
  13829. #endif /* WASM_ENABLE_BULK_MEMORY */
  13830. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13831. case WASM_OP_TABLE_INIT:
  13832. {
  13833. uint8 seg_type = 0, tbl_type = 0;
  13834. #if WASM_ENABLE_GC != 0
  13835. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13836. #endif
  13837. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13838. pb_read_leb_uint32(p, p_end, table_idx);
  13839. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13840. #if WASM_ENABLE_GC != 0
  13841. (void **)&tbl_ref_type,
  13842. #else
  13843. NULL,
  13844. #endif
  13845. error_buf, error_buf_size))
  13846. goto fail;
  13847. if (!get_table_seg_elem_type(module, table_seg_idx,
  13848. &seg_type,
  13849. #if WASM_ENABLE_GC != 0
  13850. (void **)&seg_ref_type,
  13851. #else
  13852. NULL,
  13853. #endif
  13854. error_buf, error_buf_size))
  13855. goto fail;
  13856. #if WASM_ENABLE_GC == 0
  13857. if (seg_type != tbl_type) {
  13858. set_error_buf(error_buf, error_buf_size,
  13859. "type mismatch");
  13860. goto fail;
  13861. }
  13862. #else
  13863. if (!wasm_reftype_is_subtype_of(
  13864. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13865. module->types, module->type_count)) {
  13866. set_error_buf(error_buf, error_buf_size,
  13867. "type mismatch");
  13868. goto fail;
  13869. }
  13870. #endif
  13871. #if WASM_ENABLE_FAST_INTERP != 0
  13872. emit_uint32(loader_ctx, table_seg_idx);
  13873. emit_uint32(loader_ctx, table_idx);
  13874. #endif
  13875. POP_I32();
  13876. POP_I32();
  13877. #if WASM_ENABLE_MEMORY64 != 0
  13878. table_elem_idx_type = is_table_64bit(module, table_idx)
  13879. ? VALUE_TYPE_I64
  13880. : VALUE_TYPE_I32;
  13881. #endif
  13882. POP_TBL_ELEM_IDX();
  13883. #if WASM_ENABLE_WAMR_COMPILER != 0
  13884. module->is_ref_types_used = true;
  13885. #endif
  13886. break;
  13887. }
  13888. case WASM_OP_ELEM_DROP:
  13889. {
  13890. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13891. if (!get_table_seg_elem_type(module, table_seg_idx,
  13892. NULL, NULL, error_buf,
  13893. error_buf_size))
  13894. goto fail;
  13895. #if WASM_ENABLE_FAST_INTERP != 0
  13896. emit_uint32(loader_ctx, table_seg_idx);
  13897. #endif
  13898. #if WASM_ENABLE_WAMR_COMPILER != 0
  13899. module->is_ref_types_used = true;
  13900. #endif
  13901. break;
  13902. }
  13903. case WASM_OP_TABLE_COPY:
  13904. {
  13905. uint8 src_type, dst_type, src_tbl_idx_type,
  13906. dst_tbl_idx_type, min_tbl_idx_type;
  13907. #if WASM_ENABLE_GC != 0
  13908. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13909. #endif
  13910. uint32 src_tbl_idx, dst_tbl_idx;
  13911. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  13912. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13913. #if WASM_ENABLE_GC != 0
  13914. (void **)&dst_ref_type,
  13915. #else
  13916. NULL,
  13917. #endif
  13918. error_buf, error_buf_size))
  13919. goto fail;
  13920. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  13921. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13922. #if WASM_ENABLE_GC != 0
  13923. (void **)&src_ref_type,
  13924. #else
  13925. NULL,
  13926. #endif
  13927. error_buf, error_buf_size))
  13928. goto fail;
  13929. #if WASM_ENABLE_GC == 0
  13930. if (src_type != dst_type) {
  13931. set_error_buf(error_buf, error_buf_size,
  13932. "type mismatch");
  13933. goto fail;
  13934. }
  13935. #else
  13936. if (!wasm_reftype_is_subtype_of(
  13937. src_type, src_ref_type, dst_type, dst_ref_type,
  13938. module->types, module->type_count)) {
  13939. set_error_buf(error_buf, error_buf_size,
  13940. "type mismatch");
  13941. goto fail;
  13942. }
  13943. #endif
  13944. #if WASM_ENABLE_FAST_INTERP != 0
  13945. emit_uint32(loader_ctx, dst_tbl_idx);
  13946. emit_uint32(loader_ctx, src_tbl_idx);
  13947. #endif
  13948. #if WASM_ENABLE_MEMORY64 != 0
  13949. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  13950. ? VALUE_TYPE_I64
  13951. : VALUE_TYPE_I32;
  13952. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  13953. ? VALUE_TYPE_I64
  13954. : VALUE_TYPE_I32;
  13955. min_tbl_idx_type =
  13956. (src_tbl_idx_type == VALUE_TYPE_I32
  13957. || dst_tbl_idx_type == VALUE_TYPE_I32)
  13958. ? VALUE_TYPE_I32
  13959. : VALUE_TYPE_I64;
  13960. #else
  13961. src_tbl_idx_type = VALUE_TYPE_I32;
  13962. dst_tbl_idx_type = VALUE_TYPE_I32;
  13963. min_tbl_idx_type = VALUE_TYPE_I32;
  13964. #endif
  13965. table_elem_idx_type = min_tbl_idx_type;
  13966. POP_TBL_ELEM_IDX();
  13967. table_elem_idx_type = src_tbl_idx_type;
  13968. POP_TBL_ELEM_IDX();
  13969. table_elem_idx_type = dst_tbl_idx_type;
  13970. POP_TBL_ELEM_IDX();
  13971. #if WASM_ENABLE_WAMR_COMPILER != 0
  13972. module->is_ref_types_used = true;
  13973. #endif
  13974. break;
  13975. }
  13976. case WASM_OP_TABLE_SIZE:
  13977. {
  13978. pb_read_leb_uint32(p, p_end, table_idx);
  13979. /* TODO: shall we create a new function to check
  13980. table idx instead of using below function? */
  13981. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13982. error_buf, error_buf_size))
  13983. goto fail;
  13984. #if WASM_ENABLE_FAST_INTERP != 0
  13985. emit_uint32(loader_ctx, table_idx);
  13986. #endif
  13987. #if WASM_ENABLE_MEMORY64 != 0
  13988. table_elem_idx_type = is_table_64bit(module, table_idx)
  13989. ? VALUE_TYPE_I64
  13990. : VALUE_TYPE_I32;
  13991. #endif
  13992. PUSH_TBL_ELEM_IDX();
  13993. #if WASM_ENABLE_WAMR_COMPILER != 0
  13994. module->is_ref_types_used = true;
  13995. #endif
  13996. break;
  13997. }
  13998. case WASM_OP_TABLE_GROW:
  13999. case WASM_OP_TABLE_FILL:
  14000. {
  14001. uint8 decl_type;
  14002. #if WASM_ENABLE_GC != 0
  14003. WASMRefType *ref_type = NULL;
  14004. #endif
  14005. pb_read_leb_uint32(p, p_end, table_idx);
  14006. if (!get_table_elem_type(module, table_idx, &decl_type,
  14007. #if WASM_ENABLE_GC != 0
  14008. (void **)&ref_type,
  14009. #else
  14010. NULL,
  14011. #endif
  14012. error_buf, error_buf_size))
  14013. goto fail;
  14014. #if WASM_ENABLE_GC != 0
  14015. if (wasm_is_type_multi_byte_type(decl_type)) {
  14016. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  14017. ref_type,
  14018. wasm_reftype_struct_size(ref_type));
  14019. }
  14020. #endif
  14021. if (opcode1 == WASM_OP_TABLE_GROW) {
  14022. if (table_idx < module->import_table_count) {
  14023. module->import_tables[table_idx]
  14024. .u.table.table_type.possible_grow = true;
  14025. }
  14026. else {
  14027. module
  14028. ->tables[table_idx
  14029. - module->import_table_count]
  14030. .table_type.possible_grow = true;
  14031. }
  14032. }
  14033. #if WASM_ENABLE_FAST_INTERP != 0
  14034. emit_uint32(loader_ctx, table_idx);
  14035. #endif
  14036. #if WASM_ENABLE_MEMORY64 != 0
  14037. table_elem_idx_type = is_table_64bit(module, table_idx)
  14038. ? VALUE_TYPE_I64
  14039. : VALUE_TYPE_I32;
  14040. #endif
  14041. POP_TBL_ELEM_IDX();
  14042. #if WASM_ENABLE_FAST_INTERP != 0
  14043. POP_OFFSET_TYPE(decl_type);
  14044. #endif
  14045. POP_TYPE(decl_type);
  14046. if (opcode1 == WASM_OP_TABLE_GROW)
  14047. PUSH_TBL_ELEM_IDX();
  14048. else
  14049. POP_TBL_ELEM_IDX();
  14050. #if WASM_ENABLE_WAMR_COMPILER != 0
  14051. module->is_ref_types_used = true;
  14052. #endif
  14053. break;
  14054. }
  14055. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  14056. default:
  14057. set_error_buf_v(error_buf, error_buf_size,
  14058. "%s %02x %02x", "unsupported opcode",
  14059. 0xfc, opcode1);
  14060. goto fail;
  14061. }
  14062. break;
  14063. }
  14064. #if WASM_ENABLE_SIMD != 0
  14065. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  14066. || (WASM_ENABLE_FAST_INTERP != 0)
  14067. case WASM_OP_SIMD_PREFIX:
  14068. {
  14069. uint32 opcode1;
  14070. #if WASM_ENABLE_WAMR_COMPILER != 0
  14071. /* Mark the SIMD instruction is used in this module */
  14072. module->is_simd_used = true;
  14073. #endif
  14074. pb_read_leb_uint32(p, p_end, opcode1);
  14075. #if WASM_ENABLE_FAST_INTERP != 0
  14076. emit_byte(loader_ctx, opcode1);
  14077. #endif
  14078. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  14079. */
  14080. switch (opcode1) {
  14081. /* memory instruction */
  14082. case SIMD_v128_load:
  14083. case SIMD_v128_load8x8_s:
  14084. case SIMD_v128_load8x8_u:
  14085. case SIMD_v128_load16x4_s:
  14086. case SIMD_v128_load16x4_u:
  14087. case SIMD_v128_load32x2_s:
  14088. case SIMD_v128_load32x2_u:
  14089. case SIMD_v128_load8_splat:
  14090. case SIMD_v128_load16_splat:
  14091. case SIMD_v128_load32_splat:
  14092. case SIMD_v128_load64_splat:
  14093. {
  14094. CHECK_MEMORY();
  14095. pb_read_leb_uint32(p, p_end, align); /* align */
  14096. if (!check_simd_memory_access_align(
  14097. opcode1, align, error_buf, error_buf_size)) {
  14098. goto fail;
  14099. }
  14100. pb_read_leb_mem_offset(p, p_end,
  14101. mem_offset); /* offset */
  14102. #if WASM_ENABLE_FAST_INTERP != 0
  14103. emit_uint32(loader_ctx, mem_offset);
  14104. #endif
  14105. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14106. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14107. func->has_memory_operations = true;
  14108. #endif
  14109. break;
  14110. }
  14111. case SIMD_v128_store:
  14112. {
  14113. CHECK_MEMORY();
  14114. pb_read_leb_uint32(p, p_end, align); /* align */
  14115. if (!check_simd_memory_access_align(
  14116. opcode1, align, error_buf, error_buf_size)) {
  14117. goto fail;
  14118. }
  14119. pb_read_leb_mem_offset(p, p_end,
  14120. mem_offset); /* offset */
  14121. #if WASM_ENABLE_FAST_INTERP != 0
  14122. emit_uint32(loader_ctx, mem_offset);
  14123. #endif
  14124. POP_V128();
  14125. POP_MEM_OFFSET();
  14126. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14127. func->has_memory_operations = true;
  14128. #endif
  14129. break;
  14130. }
  14131. /* basic operation */
  14132. case SIMD_v128_const:
  14133. {
  14134. #if WASM_ENABLE_FAST_INTERP != 0
  14135. uint64 high, low;
  14136. #endif
  14137. CHECK_BUF1(p, p_end, 16);
  14138. #if WASM_ENABLE_FAST_INTERP != 0
  14139. wasm_runtime_read_v128(p, &high, &low);
  14140. emit_uint64(loader_ctx, high);
  14141. emit_uint64(loader_ctx, low);
  14142. #endif
  14143. p += 16;
  14144. PUSH_V128();
  14145. break;
  14146. }
  14147. case SIMD_v8x16_shuffle:
  14148. {
  14149. V128 mask;
  14150. CHECK_BUF1(p, p_end, 16);
  14151. mask = read_i8x16(p, error_buf, error_buf_size);
  14152. if (!check_simd_shuffle_mask(mask, error_buf,
  14153. error_buf_size)) {
  14154. goto fail;
  14155. }
  14156. #if WASM_ENABLE_FAST_INTERP != 0
  14157. uint64 high, low;
  14158. wasm_runtime_read_v128(p, &high, &low);
  14159. emit_uint64(loader_ctx, high);
  14160. emit_uint64(loader_ctx, low);
  14161. #endif
  14162. p += 16;
  14163. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14164. break;
  14165. }
  14166. case SIMD_v8x16_swizzle:
  14167. {
  14168. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14169. break;
  14170. }
  14171. /* splat operation */
  14172. case SIMD_i8x16_splat:
  14173. case SIMD_i16x8_splat:
  14174. case SIMD_i32x4_splat:
  14175. case SIMD_i64x2_splat:
  14176. case SIMD_f32x4_splat:
  14177. case SIMD_f64x2_splat:
  14178. {
  14179. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  14180. VALUE_TYPE_I32, VALUE_TYPE_I64,
  14181. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  14182. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  14183. VALUE_TYPE_V128);
  14184. break;
  14185. }
  14186. /* lane operation */
  14187. case SIMD_i8x16_extract_lane_s:
  14188. case SIMD_i8x16_extract_lane_u:
  14189. case SIMD_i8x16_replace_lane:
  14190. case SIMD_i16x8_extract_lane_s:
  14191. case SIMD_i16x8_extract_lane_u:
  14192. case SIMD_i16x8_replace_lane:
  14193. case SIMD_i32x4_extract_lane:
  14194. case SIMD_i32x4_replace_lane:
  14195. case SIMD_i64x2_extract_lane:
  14196. case SIMD_i64x2_replace_lane:
  14197. case SIMD_f32x4_extract_lane:
  14198. case SIMD_f32x4_replace_lane:
  14199. case SIMD_f64x2_extract_lane:
  14200. case SIMD_f64x2_replace_lane:
  14201. {
  14202. uint8 lane;
  14203. /* clang-format off */
  14204. uint8 replace[] = {
  14205. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  14206. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  14207. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  14208. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  14209. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  14210. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  14211. };
  14212. uint8 push_type[] = {
  14213. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14214. VALUE_TYPE_V128,
  14215. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14216. VALUE_TYPE_V128,
  14217. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  14218. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  14219. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  14220. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  14221. };
  14222. /* clang-format on */
  14223. CHECK_BUF(p, p_end, 1);
  14224. lane = read_uint8(p);
  14225. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14226. error_buf_size)) {
  14227. goto fail;
  14228. }
  14229. #if WASM_ENABLE_FAST_INTERP != 0
  14230. emit_byte(loader_ctx, lane);
  14231. #endif
  14232. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  14233. #if WASM_ENABLE_FAST_INTERP != 0
  14234. if (!(wasm_loader_pop_frame_ref_offset(
  14235. loader_ctx,
  14236. replace[opcode1
  14237. - SIMD_i8x16_extract_lane_s],
  14238. error_buf, error_buf_size)))
  14239. goto fail;
  14240. #else
  14241. if (!(wasm_loader_pop_frame_ref(
  14242. loader_ctx,
  14243. replace[opcode1
  14244. - SIMD_i8x16_extract_lane_s],
  14245. error_buf, error_buf_size)))
  14246. goto fail;
  14247. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  14248. }
  14249. POP_AND_PUSH(
  14250. VALUE_TYPE_V128,
  14251. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  14252. break;
  14253. }
  14254. /* i8x16 compare operation */
  14255. case SIMD_i8x16_eq:
  14256. case SIMD_i8x16_ne:
  14257. case SIMD_i8x16_lt_s:
  14258. case SIMD_i8x16_lt_u:
  14259. case SIMD_i8x16_gt_s:
  14260. case SIMD_i8x16_gt_u:
  14261. case SIMD_i8x16_le_s:
  14262. case SIMD_i8x16_le_u:
  14263. case SIMD_i8x16_ge_s:
  14264. case SIMD_i8x16_ge_u:
  14265. /* i16x8 compare operation */
  14266. case SIMD_i16x8_eq:
  14267. case SIMD_i16x8_ne:
  14268. case SIMD_i16x8_lt_s:
  14269. case SIMD_i16x8_lt_u:
  14270. case SIMD_i16x8_gt_s:
  14271. case SIMD_i16x8_gt_u:
  14272. case SIMD_i16x8_le_s:
  14273. case SIMD_i16x8_le_u:
  14274. case SIMD_i16x8_ge_s:
  14275. case SIMD_i16x8_ge_u:
  14276. /* i32x4 compare operation */
  14277. case SIMD_i32x4_eq:
  14278. case SIMD_i32x4_ne:
  14279. case SIMD_i32x4_lt_s:
  14280. case SIMD_i32x4_lt_u:
  14281. case SIMD_i32x4_gt_s:
  14282. case SIMD_i32x4_gt_u:
  14283. case SIMD_i32x4_le_s:
  14284. case SIMD_i32x4_le_u:
  14285. case SIMD_i32x4_ge_s:
  14286. case SIMD_i32x4_ge_u:
  14287. /* f32x4 compare operation */
  14288. case SIMD_f32x4_eq:
  14289. case SIMD_f32x4_ne:
  14290. case SIMD_f32x4_lt:
  14291. case SIMD_f32x4_gt:
  14292. case SIMD_f32x4_le:
  14293. case SIMD_f32x4_ge:
  14294. /* f64x2 compare operation */
  14295. case SIMD_f64x2_eq:
  14296. case SIMD_f64x2_ne:
  14297. case SIMD_f64x2_lt:
  14298. case SIMD_f64x2_gt:
  14299. case SIMD_f64x2_le:
  14300. case SIMD_f64x2_ge:
  14301. {
  14302. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14303. break;
  14304. }
  14305. /* v128 operation */
  14306. case SIMD_v128_not:
  14307. {
  14308. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14309. break;
  14310. }
  14311. case SIMD_v128_and:
  14312. case SIMD_v128_andnot:
  14313. case SIMD_v128_or:
  14314. case SIMD_v128_xor:
  14315. {
  14316. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14317. break;
  14318. }
  14319. case SIMD_v128_bitselect:
  14320. {
  14321. POP_V128();
  14322. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14323. break;
  14324. }
  14325. case SIMD_v128_any_true:
  14326. {
  14327. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14328. break;
  14329. }
  14330. /* Load Lane Operation */
  14331. case SIMD_v128_load8_lane:
  14332. case SIMD_v128_load16_lane:
  14333. case SIMD_v128_load32_lane:
  14334. case SIMD_v128_load64_lane:
  14335. case SIMD_v128_store8_lane:
  14336. case SIMD_v128_store16_lane:
  14337. case SIMD_v128_store32_lane:
  14338. case SIMD_v128_store64_lane:
  14339. {
  14340. uint8 lane;
  14341. CHECK_MEMORY();
  14342. pb_read_leb_uint32(p, p_end, align); /* align */
  14343. if (!check_simd_memory_access_align(
  14344. opcode1, align, error_buf, error_buf_size)) {
  14345. goto fail;
  14346. }
  14347. pb_read_leb_mem_offset(p, p_end,
  14348. mem_offset); /* offset */
  14349. CHECK_BUF(p, p_end, 1);
  14350. lane = read_uint8(p);
  14351. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14352. error_buf_size)) {
  14353. goto fail;
  14354. }
  14355. #if WASM_ENABLE_FAST_INTERP != 0
  14356. emit_uint32(loader_ctx, mem_offset);
  14357. #endif
  14358. POP_V128();
  14359. POP_MEM_OFFSET();
  14360. #if WASM_ENABLE_FAST_INTERP != 0
  14361. emit_byte(loader_ctx, lane);
  14362. #endif
  14363. if (opcode1 < SIMD_v128_store8_lane) {
  14364. PUSH_V128();
  14365. }
  14366. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14367. func->has_memory_operations = true;
  14368. #endif
  14369. break;
  14370. }
  14371. case SIMD_v128_load32_zero:
  14372. case SIMD_v128_load64_zero:
  14373. {
  14374. CHECK_MEMORY();
  14375. pb_read_leb_uint32(p, p_end, align); /* align */
  14376. if (!check_simd_memory_access_align(
  14377. opcode1, align, error_buf, error_buf_size)) {
  14378. goto fail;
  14379. }
  14380. pb_read_leb_mem_offset(p, p_end,
  14381. mem_offset); /* offset */
  14382. #if WASM_ENABLE_FAST_INTERP != 0
  14383. emit_uint32(loader_ctx, mem_offset);
  14384. #endif
  14385. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14386. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14387. func->has_memory_operations = true;
  14388. #endif
  14389. break;
  14390. }
  14391. /* Float conversion */
  14392. case SIMD_f32x4_demote_f64x2_zero:
  14393. case SIMD_f64x2_promote_low_f32x4_zero:
  14394. {
  14395. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14396. break;
  14397. }
  14398. /* i8x16 Operation */
  14399. case SIMD_i8x16_abs:
  14400. case SIMD_i8x16_neg:
  14401. case SIMD_i8x16_popcnt:
  14402. {
  14403. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14404. break;
  14405. }
  14406. case SIMD_i8x16_all_true:
  14407. case SIMD_i8x16_bitmask:
  14408. {
  14409. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14410. break;
  14411. }
  14412. case SIMD_i8x16_narrow_i16x8_s:
  14413. case SIMD_i8x16_narrow_i16x8_u:
  14414. {
  14415. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14416. break;
  14417. }
  14418. case SIMD_f32x4_ceil:
  14419. case SIMD_f32x4_floor:
  14420. case SIMD_f32x4_trunc:
  14421. case SIMD_f32x4_nearest:
  14422. {
  14423. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14424. break;
  14425. }
  14426. case SIMD_i8x16_shl:
  14427. case SIMD_i8x16_shr_s:
  14428. case SIMD_i8x16_shr_u:
  14429. {
  14430. POP_I32();
  14431. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14432. break;
  14433. }
  14434. case SIMD_i8x16_add:
  14435. case SIMD_i8x16_add_sat_s:
  14436. case SIMD_i8x16_add_sat_u:
  14437. case SIMD_i8x16_sub:
  14438. case SIMD_i8x16_sub_sat_s:
  14439. case SIMD_i8x16_sub_sat_u:
  14440. {
  14441. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14442. break;
  14443. }
  14444. case SIMD_f64x2_ceil:
  14445. case SIMD_f64x2_floor:
  14446. {
  14447. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14448. break;
  14449. }
  14450. case SIMD_i8x16_min_s:
  14451. case SIMD_i8x16_min_u:
  14452. case SIMD_i8x16_max_s:
  14453. case SIMD_i8x16_max_u:
  14454. {
  14455. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14456. break;
  14457. }
  14458. case SIMD_f64x2_trunc:
  14459. {
  14460. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14461. break;
  14462. }
  14463. case SIMD_i8x16_avgr_u:
  14464. {
  14465. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14466. break;
  14467. }
  14468. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14469. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14470. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14471. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14472. /* i16x8 operation */
  14473. case SIMD_i16x8_abs:
  14474. case SIMD_i16x8_neg:
  14475. {
  14476. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14477. break;
  14478. }
  14479. case SIMD_i16x8_q15mulr_sat_s:
  14480. {
  14481. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14482. break;
  14483. }
  14484. case SIMD_i16x8_all_true:
  14485. case SIMD_i16x8_bitmask:
  14486. {
  14487. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14488. break;
  14489. }
  14490. case SIMD_i16x8_narrow_i32x4_s:
  14491. case SIMD_i16x8_narrow_i32x4_u:
  14492. {
  14493. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14494. break;
  14495. }
  14496. case SIMD_i16x8_extend_low_i8x16_s:
  14497. case SIMD_i16x8_extend_high_i8x16_s:
  14498. case SIMD_i16x8_extend_low_i8x16_u:
  14499. case SIMD_i16x8_extend_high_i8x16_u:
  14500. {
  14501. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14502. break;
  14503. }
  14504. case SIMD_i16x8_shl:
  14505. case SIMD_i16x8_shr_s:
  14506. case SIMD_i16x8_shr_u:
  14507. {
  14508. POP_I32();
  14509. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14510. break;
  14511. }
  14512. case SIMD_i16x8_add:
  14513. case SIMD_i16x8_add_sat_s:
  14514. case SIMD_i16x8_add_sat_u:
  14515. case SIMD_i16x8_sub:
  14516. case SIMD_i16x8_sub_sat_s:
  14517. case SIMD_i16x8_sub_sat_u:
  14518. {
  14519. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14520. break;
  14521. }
  14522. case SIMD_f64x2_nearest:
  14523. {
  14524. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14525. break;
  14526. }
  14527. case SIMD_i16x8_mul:
  14528. case SIMD_i16x8_min_s:
  14529. case SIMD_i16x8_min_u:
  14530. case SIMD_i16x8_max_s:
  14531. case SIMD_i16x8_max_u:
  14532. case SIMD_i16x8_avgr_u:
  14533. case SIMD_i16x8_extmul_low_i8x16_s:
  14534. case SIMD_i16x8_extmul_high_i8x16_s:
  14535. case SIMD_i16x8_extmul_low_i8x16_u:
  14536. case SIMD_i16x8_extmul_high_i8x16_u:
  14537. {
  14538. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14539. break;
  14540. }
  14541. /* i32x4 operation */
  14542. case SIMD_i32x4_abs:
  14543. case SIMD_i32x4_neg:
  14544. {
  14545. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14546. break;
  14547. }
  14548. case SIMD_i32x4_all_true:
  14549. case SIMD_i32x4_bitmask:
  14550. {
  14551. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14552. break;
  14553. }
  14554. case SIMD_i32x4_extend_low_i16x8_s:
  14555. case SIMD_i32x4_extend_high_i16x8_s:
  14556. case SIMD_i32x4_extend_low_i16x8_u:
  14557. case SIMD_i32x4_extend_high_i16x8_u:
  14558. {
  14559. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14560. break;
  14561. }
  14562. case SIMD_i32x4_shl:
  14563. case SIMD_i32x4_shr_s:
  14564. case SIMD_i32x4_shr_u:
  14565. {
  14566. POP_I32();
  14567. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14568. break;
  14569. }
  14570. case SIMD_i32x4_add:
  14571. case SIMD_i32x4_sub:
  14572. case SIMD_i32x4_mul:
  14573. case SIMD_i32x4_min_s:
  14574. case SIMD_i32x4_min_u:
  14575. case SIMD_i32x4_max_s:
  14576. case SIMD_i32x4_max_u:
  14577. case SIMD_i32x4_dot_i16x8_s:
  14578. case SIMD_i32x4_extmul_low_i16x8_s:
  14579. case SIMD_i32x4_extmul_high_i16x8_s:
  14580. case SIMD_i32x4_extmul_low_i16x8_u:
  14581. case SIMD_i32x4_extmul_high_i16x8_u:
  14582. {
  14583. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14584. break;
  14585. }
  14586. /* i64x2 operation */
  14587. case SIMD_i64x2_abs:
  14588. case SIMD_i64x2_neg:
  14589. {
  14590. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14591. break;
  14592. }
  14593. case SIMD_i64x2_all_true:
  14594. case SIMD_i64x2_bitmask:
  14595. {
  14596. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14597. break;
  14598. }
  14599. case SIMD_i64x2_extend_low_i32x4_s:
  14600. case SIMD_i64x2_extend_high_i32x4_s:
  14601. case SIMD_i64x2_extend_low_i32x4_u:
  14602. case SIMD_i64x2_extend_high_i32x4_u:
  14603. {
  14604. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14605. break;
  14606. }
  14607. case SIMD_i64x2_shl:
  14608. case SIMD_i64x2_shr_s:
  14609. case SIMD_i64x2_shr_u:
  14610. {
  14611. POP_I32();
  14612. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14613. break;
  14614. }
  14615. case SIMD_i64x2_add:
  14616. case SIMD_i64x2_sub:
  14617. case SIMD_i64x2_mul:
  14618. case SIMD_i64x2_eq:
  14619. case SIMD_i64x2_ne:
  14620. case SIMD_i64x2_lt_s:
  14621. case SIMD_i64x2_gt_s:
  14622. case SIMD_i64x2_le_s:
  14623. case SIMD_i64x2_ge_s:
  14624. case SIMD_i64x2_extmul_low_i32x4_s:
  14625. case SIMD_i64x2_extmul_high_i32x4_s:
  14626. case SIMD_i64x2_extmul_low_i32x4_u:
  14627. case SIMD_i64x2_extmul_high_i32x4_u:
  14628. {
  14629. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14630. break;
  14631. }
  14632. /* f32x4 operation */
  14633. case SIMD_f32x4_abs:
  14634. case SIMD_f32x4_neg:
  14635. case SIMD_f32x4_sqrt:
  14636. {
  14637. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14638. break;
  14639. }
  14640. case SIMD_f32x4_add:
  14641. case SIMD_f32x4_sub:
  14642. case SIMD_f32x4_mul:
  14643. case SIMD_f32x4_div:
  14644. case SIMD_f32x4_min:
  14645. case SIMD_f32x4_max:
  14646. case SIMD_f32x4_pmin:
  14647. case SIMD_f32x4_pmax:
  14648. {
  14649. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14650. break;
  14651. }
  14652. /* f64x2 operation */
  14653. case SIMD_f64x2_abs:
  14654. case SIMD_f64x2_neg:
  14655. case SIMD_f64x2_sqrt:
  14656. {
  14657. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14658. break;
  14659. }
  14660. case SIMD_f64x2_add:
  14661. case SIMD_f64x2_sub:
  14662. case SIMD_f64x2_mul:
  14663. case SIMD_f64x2_div:
  14664. case SIMD_f64x2_min:
  14665. case SIMD_f64x2_max:
  14666. case SIMD_f64x2_pmin:
  14667. case SIMD_f64x2_pmax:
  14668. {
  14669. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14670. break;
  14671. }
  14672. case SIMD_i32x4_trunc_sat_f32x4_s:
  14673. case SIMD_i32x4_trunc_sat_f32x4_u:
  14674. case SIMD_f32x4_convert_i32x4_s:
  14675. case SIMD_f32x4_convert_i32x4_u:
  14676. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14677. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14678. case SIMD_f64x2_convert_low_i32x4_s:
  14679. case SIMD_f64x2_convert_low_i32x4_u:
  14680. {
  14681. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14682. break;
  14683. }
  14684. default:
  14685. {
  14686. if (error_buf != NULL) {
  14687. snprintf(error_buf, error_buf_size,
  14688. "WASM module load failed: "
  14689. "invalid opcode 0xfd %02x.",
  14690. opcode1);
  14691. }
  14692. goto fail;
  14693. }
  14694. }
  14695. break;
  14696. }
  14697. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  14698. (WASM_ENABLE_FAST_INTERP != 0) */
  14699. #endif /* end of WASM_ENABLE_SIMD */
  14700. #if WASM_ENABLE_SHARED_MEMORY != 0
  14701. case WASM_OP_ATOMIC_PREFIX:
  14702. {
  14703. uint32 opcode1;
  14704. pb_read_leb_uint32(p, p_end, opcode1);
  14705. #if WASM_ENABLE_FAST_INTERP != 0
  14706. emit_byte(loader_ctx, opcode1);
  14707. #endif
  14708. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14709. CHECK_MEMORY();
  14710. pb_read_leb_uint32(p, p_end, align); /* align */
  14711. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14712. if (!check_memory_align_equal(opcode1, align, error_buf,
  14713. error_buf_size)) {
  14714. goto fail;
  14715. }
  14716. #if WASM_ENABLE_FAST_INTERP != 0
  14717. emit_uint32(loader_ctx, mem_offset);
  14718. #endif
  14719. }
  14720. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14721. func->has_memory_operations = true;
  14722. #endif
  14723. switch (opcode1) {
  14724. case WASM_OP_ATOMIC_NOTIFY:
  14725. POP_I32();
  14726. POP_MEM_OFFSET();
  14727. PUSH_I32();
  14728. break;
  14729. case WASM_OP_ATOMIC_WAIT32:
  14730. POP_I64();
  14731. POP_I32();
  14732. POP_MEM_OFFSET();
  14733. PUSH_I32();
  14734. break;
  14735. case WASM_OP_ATOMIC_WAIT64:
  14736. POP_I64();
  14737. POP_I64();
  14738. POP_MEM_OFFSET();
  14739. PUSH_I32();
  14740. break;
  14741. case WASM_OP_ATOMIC_FENCE:
  14742. /* reserved byte 0x00 */
  14743. if (*p++ != 0x00) {
  14744. set_error_buf(error_buf, error_buf_size,
  14745. "zero byte expected");
  14746. goto fail;
  14747. }
  14748. break;
  14749. case WASM_OP_ATOMIC_I32_LOAD:
  14750. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14751. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14752. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14753. break;
  14754. case WASM_OP_ATOMIC_I32_STORE:
  14755. case WASM_OP_ATOMIC_I32_STORE8:
  14756. case WASM_OP_ATOMIC_I32_STORE16:
  14757. POP_I32();
  14758. POP_MEM_OFFSET();
  14759. break;
  14760. case WASM_OP_ATOMIC_I64_LOAD:
  14761. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14762. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14763. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14764. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14765. break;
  14766. case WASM_OP_ATOMIC_I64_STORE:
  14767. case WASM_OP_ATOMIC_I64_STORE8:
  14768. case WASM_OP_ATOMIC_I64_STORE16:
  14769. case WASM_OP_ATOMIC_I64_STORE32:
  14770. POP_I64();
  14771. POP_MEM_OFFSET();
  14772. break;
  14773. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14774. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14775. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14776. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14777. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14778. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14779. case WASM_OP_ATOMIC_RMW_I32_AND:
  14780. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14781. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14782. case WASM_OP_ATOMIC_RMW_I32_OR:
  14783. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14784. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14785. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14786. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14787. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14788. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14789. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14790. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14791. POP_I32();
  14792. POP_MEM_OFFSET();
  14793. PUSH_I32();
  14794. break;
  14795. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14796. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14797. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14798. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14799. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14800. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14801. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14802. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14803. case WASM_OP_ATOMIC_RMW_I64_AND:
  14804. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14805. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14806. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14807. case WASM_OP_ATOMIC_RMW_I64_OR:
  14808. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14809. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14810. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14811. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14812. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14813. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14814. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14815. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14816. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14817. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14818. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14819. POP_I64();
  14820. POP_MEM_OFFSET();
  14821. PUSH_I64();
  14822. break;
  14823. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14824. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14825. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14826. POP_I32();
  14827. POP_I32();
  14828. POP_MEM_OFFSET();
  14829. PUSH_I32();
  14830. break;
  14831. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14832. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14833. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14834. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14835. POP_I64();
  14836. POP_I64();
  14837. POP_MEM_OFFSET();
  14838. PUSH_I64();
  14839. break;
  14840. default:
  14841. set_error_buf_v(error_buf, error_buf_size,
  14842. "%s %02x %02x", "unsupported opcode",
  14843. 0xfe, opcode1);
  14844. goto fail;
  14845. }
  14846. break;
  14847. }
  14848. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14849. default:
  14850. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14851. "unsupported opcode", opcode);
  14852. goto fail;
  14853. }
  14854. #if WASM_ENABLE_FAST_INTERP != 0
  14855. last_op = opcode;
  14856. #endif
  14857. }
  14858. if (loader_ctx->csp_num > 0) {
  14859. /* unmatched end opcodes result from unbalanced control flow structures,
  14860. * for example, br_table with inconsistent target count (1 declared, 2
  14861. * given), or simply superfluous end opcodes */
  14862. set_error_buf(
  14863. error_buf, error_buf_size,
  14864. "unexpected end opcodes from unbalanced control flow structures");
  14865. goto fail;
  14866. }
  14867. #if WASM_ENABLE_FAST_INTERP != 0
  14868. if (loader_ctx->p_code_compiled == NULL)
  14869. goto re_scan;
  14870. func->const_cell_num = loader_ctx->i64_const_num * 2
  14871. + loader_ctx->v128_const_num * 4
  14872. + loader_ctx->i32_const_num;
  14873. if (func->const_cell_num > 0) {
  14874. if (!(func->consts =
  14875. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  14876. error_buf, error_buf_size)))
  14877. goto fail;
  14878. if (loader_ctx->i64_const_num > 0) {
  14879. bh_memcpy_s(func->consts,
  14880. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  14881. loader_ctx->i64_consts,
  14882. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  14883. }
  14884. if (loader_ctx->i32_const_num > 0) {
  14885. bh_memcpy_s(func->consts
  14886. + sizeof(int64) * loader_ctx->i64_const_num,
  14887. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  14888. loader_ctx->i32_consts,
  14889. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  14890. }
  14891. if (loader_ctx->v128_const_num > 0) {
  14892. bh_memcpy_s(func->consts,
  14893. (uint32)sizeof(V128) * loader_ctx->v128_const_num,
  14894. loader_ctx->v128_consts,
  14895. (uint32)sizeof(V128) * loader_ctx->v128_const_num);
  14896. }
  14897. }
  14898. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14899. - loader_ctx->start_dynamic_offset + 1;
  14900. #else
  14901. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14902. #endif
  14903. func->max_block_num = loader_ctx->max_csp_num;
  14904. return_value = true;
  14905. fail:
  14906. wasm_loader_ctx_destroy(loader_ctx);
  14907. (void)table_idx;
  14908. (void)table_seg_idx;
  14909. (void)data_seg_idx;
  14910. (void)i64_const;
  14911. (void)local_offset;
  14912. (void)p_org;
  14913. (void)mem_offset;
  14914. (void)align;
  14915. return return_value;
  14916. }