wasm_loader.c 598 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_platform.h"
  7. #include "wasm.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_runtime.h"
  10. #include "wasm_loader_common.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. #if WASM_ENABLE_MEMORY64 != 0
  36. static bool
  37. has_module_memory64(WASMModule *module)
  38. {
  39. /* TODO: multi-memories for now assuming the memory idx type is consistent
  40. * across multi-memories */
  41. if (module->import_memory_count > 0)
  42. return !!(module->import_memories[0].u.memory.mem_type.flags
  43. & MEMORY64_FLAG);
  44. else if (module->memory_count > 0)
  45. return !!(module->memories[0].flags & MEMORY64_FLAG);
  46. return false;
  47. }
  48. static bool
  49. is_table_64bit(WASMModule *module, uint32 table_idx)
  50. {
  51. if (table_idx < module->import_table_count)
  52. return !!(module->import_tables[table_idx].u.table.table_type.flags
  53. & TABLE64_FLAG);
  54. else
  55. return !!(module->tables[table_idx - module->import_table_count]
  56. .table_type.flags
  57. & TABLE64_FLAG);
  58. return false;
  59. }
  60. #endif
  61. static void
  62. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  63. {
  64. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  65. }
  66. #if WASM_ENABLE_MEMORY64 != 0
  67. static void
  68. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  69. {
  70. if (error_buf != NULL) {
  71. snprintf(error_buf, error_buf_size, "offset out of range");
  72. }
  73. }
  74. #endif
  75. static void
  76. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  77. {
  78. va_list args;
  79. char buf[128];
  80. if (error_buf != NULL) {
  81. va_start(args, format);
  82. vsnprintf(buf, sizeof(buf), format, args);
  83. va_end(args);
  84. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  85. }
  86. }
  87. static bool
  88. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  89. char *error_buf, uint32 error_buf_size)
  90. {
  91. if ((uintptr_t)buf + length < (uintptr_t)buf
  92. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  93. set_error_buf(error_buf, error_buf_size,
  94. "unexpected end of section or function");
  95. return false;
  96. }
  97. return true;
  98. }
  99. static bool
  100. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  101. char *error_buf, uint32 error_buf_size)
  102. {
  103. if ((uintptr_t)buf + length < (uintptr_t)buf
  104. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  105. set_error_buf(error_buf, error_buf_size, "unexpected end");
  106. return false;
  107. }
  108. return true;
  109. }
  110. #define CHECK_BUF(buf, buf_end, length) \
  111. do { \
  112. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  113. goto fail; \
  114. } \
  115. } while (0)
  116. #define CHECK_BUF1(buf, buf_end, length) \
  117. do { \
  118. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  119. goto fail; \
  120. } \
  121. } while (0)
  122. #define skip_leb(p) while (*p++ & 0x80)
  123. #define skip_leb_int64(p, p_end) skip_leb(p)
  124. #define skip_leb_uint32(p, p_end) skip_leb(p)
  125. #define skip_leb_int32(p, p_end) skip_leb(p)
  126. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  127. #define skip_leb_memidx(p, p_end) skip_leb(p)
  128. #if WASM_ENABLE_MULTI_MEMORY == 0
  129. #define skip_leb_align(p, p_end) skip_leb(p)
  130. #else
  131. /* Skip the following memidx if applicable */
  132. #define skip_leb_align(p, p_end) \
  133. do { \
  134. if (*p++ & OPT_MEMIDX_FLAG) \
  135. skip_leb_uint32(p, p_end); \
  136. } while (0)
  137. #endif
  138. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  139. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  140. #define read_leb_int64(p, p_end, res) \
  141. do { \
  142. uint64 res64; \
  143. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  144. error_buf_size)) \
  145. goto fail; \
  146. res = (int64)res64; \
  147. } while (0)
  148. #if WASM_ENABLE_MEMORY64 != 0
  149. #define read_leb_mem_offset(p, p_end, res) \
  150. do { \
  151. uint64 res64; \
  152. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  153. &res64, error_buf, error_buf_size)) { \
  154. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  155. goto fail; \
  156. } \
  157. res = (mem_offset_t)res64; \
  158. } while (0)
  159. #else
  160. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  161. #endif
  162. #define read_leb_uint32(p, p_end, res) \
  163. do { \
  164. uint64 res64; \
  165. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  166. error_buf_size)) \
  167. goto fail; \
  168. res = (uint32)res64; \
  169. } while (0)
  170. #define read_leb_int32(p, p_end, res) \
  171. do { \
  172. uint64 res64; \
  173. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  174. error_buf_size)) \
  175. goto fail; \
  176. res = (int32)res64; \
  177. } while (0)
  178. #if WASM_ENABLE_MULTI_MEMORY != 0
  179. #define check_memidx(module, memidx) \
  180. do { \
  181. if (memidx >= module->import_memory_count + module->memory_count) { \
  182. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  183. memidx); \
  184. goto fail; \
  185. } \
  186. } while (0)
  187. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  188. * alignment check */
  189. #define read_leb_memarg(p, p_end, res) \
  190. do { \
  191. read_leb_uint32(p, p_end, res); \
  192. if (res & OPT_MEMIDX_FLAG) { \
  193. res &= ~OPT_MEMIDX_FLAG; \
  194. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  195. check_memidx(module, memidx); \
  196. } \
  197. } while (0)
  198. #else
  199. /* reserved byte 0x00 */
  200. #define check_memidx(module, memidx) \
  201. do { \
  202. (void)module; \
  203. if (memidx != 0) { \
  204. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  205. goto fail; \
  206. } \
  207. } while (0)
  208. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  209. #endif
  210. static char *
  211. type2str(uint8 type)
  212. {
  213. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  214. #if WASM_ENABLE_GC != 0
  215. char *type_str_ref[] = { "stringview_iter",
  216. "stringview_wtf16",
  217. "(ref null ht)",
  218. "(ref ht)",
  219. "", /* reserved */
  220. "stringview_wtf8",
  221. "stringref",
  222. "", /* reserved */
  223. "", /* reserved */
  224. "arrayref",
  225. "structref",
  226. "i31ref",
  227. "eqref",
  228. "anyref",
  229. "externref",
  230. "funcref",
  231. "nullref",
  232. "nullexternref",
  233. "nullfuncref" };
  234. #endif
  235. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  236. return type_str[type - VALUE_TYPE_V128];
  237. #if WASM_ENABLE_GC != 0
  238. else if (wasm_is_type_reftype(type))
  239. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  240. #endif
  241. else if (type == VALUE_TYPE_FUNCREF)
  242. return "funcref";
  243. else if (type == VALUE_TYPE_EXTERNREF)
  244. return "externref";
  245. else
  246. return "unknown type";
  247. }
  248. static bool
  249. is_32bit_type(uint8 type)
  250. {
  251. if (type == VALUE_TYPE_I32
  252. || type == VALUE_TYPE_F32
  253. /* the operand stack is in polymorphic state */
  254. || type == VALUE_TYPE_ANY
  255. #if WASM_ENABLE_GC != 0
  256. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  257. #elif WASM_ENABLE_REF_TYPES != 0
  258. /* For reference types, we use uint32 index to represent
  259. the funcref and externref */
  260. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  261. #endif
  262. )
  263. return true;
  264. return false;
  265. }
  266. static bool
  267. is_64bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  270. #if WASM_ENABLE_GC != 0
  271. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  272. #endif
  273. )
  274. return true;
  275. return false;
  276. }
  277. #if WASM_ENABLE_GC != 0
  278. static bool
  279. is_packed_type(uint8 type)
  280. {
  281. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  282. }
  283. #endif
  284. static bool
  285. is_byte_a_type(uint8 type)
  286. {
  287. return (is_valid_value_type_for_interpreter(type)
  288. || (type == VALUE_TYPE_VOID))
  289. ? true
  290. : false;
  291. }
  292. #if WASM_ENABLE_SIMD != 0
  293. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  294. static V128
  295. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  296. {
  297. V128 result;
  298. uint8 i;
  299. for (i = 0; i != 16; ++i) {
  300. result.i8x16[i] = read_uint8(p_buf);
  301. }
  302. return result;
  303. }
  304. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  305. #endif /* end of WASM_ENABLE_SIMD */
  306. static void *
  307. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  308. {
  309. void *mem;
  310. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  311. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  312. return NULL;
  313. }
  314. memset(mem, 0, (uint32)size);
  315. return mem;
  316. }
  317. static void *
  318. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  319. uint32 error_buf_size)
  320. {
  321. uint8 *mem_new;
  322. bh_assert(size_new > size_old);
  323. if ((mem_new = wasm_runtime_realloc(mem_old, size_new))) {
  324. memset(mem_new + size_old, 0, size_new - size_old);
  325. return mem_new;
  326. }
  327. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  328. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  329. wasm_runtime_free(mem_old);
  330. }
  331. return mem_new;
  332. }
  333. #define MEM_REALLOC(mem, size_old, size_new) \
  334. do { \
  335. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  336. error_buf_size); \
  337. if (!mem_new) \
  338. goto fail; \
  339. mem = mem_new; \
  340. } while (0)
  341. #if WASM_ENABLE_GC != 0
  342. static bool
  343. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  344. char *error_buf, uint32 error_buf_size)
  345. {
  346. if (type_index >= type_count) {
  347. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  348. type_index);
  349. return false;
  350. }
  351. return true;
  352. }
  353. static bool
  354. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  355. uint32 error_buf_size)
  356. {
  357. if (!check_type_index(module, module->type_count, type_index, error_buf,
  358. error_buf_size)) {
  359. return false;
  360. }
  361. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  362. set_error_buf(error_buf, error_buf_size, "unkown array type");
  363. return false;
  364. }
  365. return true;
  366. }
  367. #endif
  368. static bool
  369. check_function_index(const WASMModule *module, uint32 function_index,
  370. char *error_buf, uint32 error_buf_size)
  371. {
  372. if (function_index
  373. >= module->import_function_count + module->function_count) {
  374. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  375. function_index);
  376. return false;
  377. }
  378. return true;
  379. }
  380. typedef struct InitValue {
  381. uint8 type;
  382. uint8 flag;
  383. #if WASM_ENABLE_GC != 0
  384. uint8 gc_opcode;
  385. WASMRefType ref_type;
  386. #endif
  387. WASMValue value;
  388. } InitValue;
  389. typedef struct ConstExprContext {
  390. uint32 sp;
  391. uint32 size;
  392. WASMModule *module;
  393. InitValue *stack;
  394. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  395. } ConstExprContext;
  396. static void
  397. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  398. {
  399. ctx->sp = 0;
  400. ctx->module = module;
  401. ctx->stack = ctx->data;
  402. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  403. }
  404. static bool
  405. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  406. #if WASM_ENABLE_GC != 0
  407. WASMRefType *ref_type, uint8 gc_opcode,
  408. #endif
  409. WASMValue *value, char *error_buf, uint32 error_buf_size)
  410. {
  411. InitValue *cur_value;
  412. if (ctx->sp >= ctx->size) {
  413. if (ctx->stack != ctx->data) {
  414. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  415. (ctx->size + 4) * sizeof(InitValue));
  416. }
  417. else {
  418. if (!(ctx->stack =
  419. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  420. error_buf, error_buf_size))) {
  421. goto fail;
  422. }
  423. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  424. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  425. }
  426. ctx->size += 4;
  427. }
  428. cur_value = &ctx->stack[ctx->sp++];
  429. cur_value->type = type;
  430. cur_value->flag = flag;
  431. cur_value->value = *value;
  432. #if WASM_ENABLE_GC != 0
  433. cur_value->gc_opcode = gc_opcode;
  434. if (wasm_is_type_multi_byte_type(type)) {
  435. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  436. ref_type, wasm_reftype_struct_size(ref_type));
  437. }
  438. #endif
  439. return true;
  440. fail:
  441. return false;
  442. }
  443. #if WASM_ENABLE_GC != 0
  444. static void
  445. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  446. {
  447. WASMStructNewInitValues *struct_init_values =
  448. (WASMStructNewInitValues *)data;
  449. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  450. WASMType *wasm_type;
  451. uint32 i;
  452. if (!data)
  453. return;
  454. wasm_type = module->types[struct_init_values->type_idx];
  455. /* The data can only be type of `WASMStructNewInitValues *`
  456. or `WASMArrayNewInitValues *` */
  457. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  458. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  459. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  460. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  461. WASMRefType *ref_type;
  462. uint8 field_type;
  463. uint16 ref_type_map_index = 0;
  464. for (i = 0; i < struct_init_values->count; i++) {
  465. field_type = struct_type->fields[i].field_type;
  466. if (wasm_is_type_multi_byte_type(field_type))
  467. ref_type =
  468. struct_type->ref_type_maps[ref_type_map_index++].ref_type;
  469. else
  470. ref_type = NULL;
  471. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  472. REF_TYPE_STRUCTREF, NULL,
  473. module->types, module->type_count)
  474. || wasm_reftype_is_subtype_of(
  475. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  476. module->types, module->type_count)) {
  477. destroy_init_expr_data_recursive(
  478. module, struct_init_values->fields[i].data);
  479. }
  480. }
  481. }
  482. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  483. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  484. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  485. uint8 elem_type = array_type->elem_type;
  486. for (i = 0; i < array_init_values->length; i++) {
  487. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  488. REF_TYPE_STRUCTREF, NULL,
  489. module->types, module->type_count)
  490. || wasm_reftype_is_subtype_of(
  491. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  492. module->types, module->type_count)) {
  493. destroy_init_expr_data_recursive(
  494. module, array_init_values->elem_data[i].data);
  495. }
  496. }
  497. }
  498. wasm_runtime_free(data);
  499. }
  500. #endif
  501. static bool
  502. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  503. #if WASM_ENABLE_GC != 0
  504. WASMRefType *ref_type, uint8 *p_gc_opcode,
  505. #endif
  506. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  507. {
  508. InitValue *cur_value;
  509. if (ctx->sp == 0) {
  510. set_error_buf(error_buf, error_buf_size,
  511. "type mismatch: const expr stack underflow");
  512. return false;
  513. }
  514. cur_value = &ctx->stack[--ctx->sp];
  515. #if WASM_ENABLE_GC == 0
  516. if (cur_value->type != type) {
  517. set_error_buf(error_buf, error_buf_size, "type mismatch");
  518. return false;
  519. }
  520. #else
  521. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  522. ref_type, ctx->module->types,
  523. ctx->module->type_count)) {
  524. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  525. "type mismatch: expect ", type2str(type),
  526. " but got other");
  527. goto fail;
  528. }
  529. #endif
  530. if (p_flag)
  531. *p_flag = cur_value->flag;
  532. if (p_value)
  533. *p_value = cur_value->value;
  534. #if WASM_ENABLE_GC != 0
  535. if (p_gc_opcode)
  536. *p_gc_opcode = cur_value->gc_opcode;
  537. #endif
  538. return true;
  539. #if WASM_ENABLE_GC != 0
  540. fail:
  541. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  542. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  543. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  544. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  545. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  546. }
  547. return false;
  548. #endif
  549. }
  550. static void
  551. destroy_const_expr_stack(ConstExprContext *ctx)
  552. {
  553. #if WASM_ENABLE_GC != 0
  554. uint32 i;
  555. for (i = 0; i < ctx->sp; i++) {
  556. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  557. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  558. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  559. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  560. destroy_init_expr_data_recursive(ctx->module,
  561. ctx->stack[i].value.data);
  562. }
  563. }
  564. #endif
  565. if (ctx->stack != ctx->data) {
  566. wasm_runtime_free(ctx->stack);
  567. }
  568. }
  569. #if WASM_ENABLE_GC != 0
  570. static void
  571. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  572. {
  573. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  574. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  575. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  576. destroy_init_expr_data_recursive(module, expr->u.data);
  577. }
  578. }
  579. #endif /* end of WASM_ENABLE_GC != 0 */
  580. static bool
  581. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  582. InitializerExpression *init_expr, uint8 type, void *ref_type,
  583. char *error_buf, uint32 error_buf_size)
  584. {
  585. const uint8 *p = *p_buf, *p_end = buf_end;
  586. uint8 flag, *p_float;
  587. uint32 i;
  588. ConstExprContext const_expr_ctx = { 0 };
  589. WASMValue cur_value;
  590. #if WASM_ENABLE_GC != 0
  591. uint32 opcode1, type_idx;
  592. uint8 opcode;
  593. WASMRefType cur_ref_type = { 0 };
  594. #endif
  595. init_const_expr_stack(&const_expr_ctx, module);
  596. CHECK_BUF(p, p_end, 1);
  597. flag = read_uint8(p);
  598. while (flag != WASM_OP_END) {
  599. switch (flag) {
  600. /* i32.const */
  601. case INIT_EXPR_TYPE_I32_CONST:
  602. read_leb_int32(p, p_end, cur_value.i32);
  603. if (!push_const_expr_stack(
  604. &const_expr_ctx, flag, VALUE_TYPE_I32,
  605. #if WASM_ENABLE_GC != 0
  606. NULL, 0,
  607. #endif
  608. &cur_value, error_buf, error_buf_size))
  609. goto fail;
  610. break;
  611. /* i64.const */
  612. case INIT_EXPR_TYPE_I64_CONST:
  613. read_leb_int64(p, p_end, cur_value.i64);
  614. if (!push_const_expr_stack(
  615. &const_expr_ctx, flag, VALUE_TYPE_I64,
  616. #if WASM_ENABLE_GC != 0
  617. NULL, 0,
  618. #endif
  619. &cur_value, error_buf, error_buf_size))
  620. goto fail;
  621. break;
  622. /* f32.const */
  623. case INIT_EXPR_TYPE_F32_CONST:
  624. CHECK_BUF(p, p_end, 4);
  625. p_float = (uint8 *)&cur_value.f32;
  626. for (i = 0; i < sizeof(float32); i++)
  627. *p_float++ = *p++;
  628. if (!push_const_expr_stack(
  629. &const_expr_ctx, flag, VALUE_TYPE_F32,
  630. #if WASM_ENABLE_GC != 0
  631. NULL, 0,
  632. #endif
  633. &cur_value, error_buf, error_buf_size))
  634. goto fail;
  635. break;
  636. /* f64.const */
  637. case INIT_EXPR_TYPE_F64_CONST:
  638. CHECK_BUF(p, p_end, 8);
  639. p_float = (uint8 *)&cur_value.f64;
  640. for (i = 0; i < sizeof(float64); i++)
  641. *p_float++ = *p++;
  642. if (!push_const_expr_stack(
  643. &const_expr_ctx, flag, VALUE_TYPE_F64,
  644. #if WASM_ENABLE_GC != 0
  645. NULL, 0,
  646. #endif
  647. &cur_value, error_buf, error_buf_size))
  648. goto fail;
  649. break;
  650. #if WASM_ENABLE_SIMD != 0
  651. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  652. /* v128.const */
  653. case INIT_EXPR_TYPE_V128_CONST:
  654. {
  655. uint64 high, low;
  656. CHECK_BUF(p, p_end, 1);
  657. (void)read_uint8(p);
  658. CHECK_BUF(p, p_end, 16);
  659. wasm_runtime_read_v128(p, &high, &low);
  660. p += 16;
  661. cur_value.v128.i64x2[0] = high;
  662. cur_value.v128.i64x2[1] = low;
  663. if (!push_const_expr_stack(
  664. &const_expr_ctx, flag, VALUE_TYPE_V128,
  665. #if WASM_ENABLE_GC != 0
  666. NULL, 0,
  667. #endif
  668. &cur_value, error_buf, error_buf_size))
  669. goto fail;
  670. #if WASM_ENABLE_WAMR_COMPILER != 0
  671. /* If any init_expr is v128.const, mark SIMD used */
  672. module->is_simd_used = true;
  673. #endif
  674. break;
  675. }
  676. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  677. #endif /* end of WASM_ENABLE_SIMD */
  678. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  679. /* ref.func */
  680. case INIT_EXPR_TYPE_FUNCREF_CONST:
  681. {
  682. uint32 func_idx;
  683. read_leb_uint32(p, p_end, func_idx);
  684. cur_value.ref_index = func_idx;
  685. if (!check_function_index(module, func_idx, error_buf,
  686. error_buf_size)) {
  687. goto fail;
  688. }
  689. #if WASM_ENABLE_GC == 0
  690. if (!push_const_expr_stack(&const_expr_ctx, flag,
  691. VALUE_TYPE_FUNCREF, &cur_value,
  692. error_buf, error_buf_size))
  693. goto fail;
  694. #else
  695. if (func_idx < module->import_function_count) {
  696. type_idx =
  697. module->import_functions[func_idx].u.function.type_idx;
  698. }
  699. else {
  700. type_idx = module
  701. ->functions[func_idx
  702. - module->import_function_count]
  703. ->type_idx;
  704. }
  705. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  706. false, type_idx);
  707. if (!push_const_expr_stack(&const_expr_ctx, flag,
  708. cur_ref_type.ref_type, &cur_ref_type,
  709. 0, &cur_value, error_buf,
  710. error_buf_size))
  711. goto fail;
  712. #endif
  713. #if WASM_ENABLE_WAMR_COMPILER != 0
  714. module->is_ref_types_used = true;
  715. #endif
  716. break;
  717. }
  718. /* ref.null */
  719. case INIT_EXPR_TYPE_REFNULL_CONST:
  720. {
  721. uint8 type1;
  722. CHECK_BUF(p, p_end, 1);
  723. type1 = read_uint8(p);
  724. #if WASM_ENABLE_GC == 0
  725. cur_value.ref_index = NULL_REF;
  726. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  727. &cur_value, error_buf,
  728. error_buf_size))
  729. goto fail;
  730. #else
  731. cur_value.gc_obj = NULL_REF;
  732. if (!is_byte_a_type(type1)) {
  733. p--;
  734. read_leb_uint32(p, p_end, type_idx);
  735. if (!check_type_index(module, module->type_count, type_idx,
  736. error_buf, error_buf_size))
  737. goto fail;
  738. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  739. true, type_idx);
  740. if (!push_const_expr_stack(&const_expr_ctx, flag,
  741. cur_ref_type.ref_type,
  742. &cur_ref_type, 0, &cur_value,
  743. error_buf, error_buf_size))
  744. goto fail;
  745. }
  746. else {
  747. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  748. NULL, 0, &cur_value, error_buf,
  749. error_buf_size))
  750. goto fail;
  751. }
  752. #endif
  753. #if WASM_ENABLE_WAMR_COMPILER != 0
  754. module->is_ref_types_used = true;
  755. #endif
  756. break;
  757. }
  758. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  759. /* get_global */
  760. case INIT_EXPR_TYPE_GET_GLOBAL:
  761. {
  762. uint32 global_idx;
  763. uint8 global_type;
  764. read_leb_uint32(p, p_end, cur_value.global_index);
  765. global_idx = cur_value.global_index;
  766. /*
  767. * Currently, constant expressions occurring as initializers
  768. * of globals are further constrained in that contained
  769. * global.get instructions are
  770. * only allowed to refer to imported globals.
  771. *
  772. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  773. */
  774. if (global_idx >= module->import_global_count
  775. /* make spec test happy */
  776. #if WASM_ENABLE_GC != 0
  777. + module->global_count
  778. #endif
  779. ) {
  780. set_error_buf_v(error_buf, error_buf_size,
  781. "unknown global %u", global_idx);
  782. goto fail;
  783. }
  784. if (
  785. /* make spec test happy */
  786. #if WASM_ENABLE_GC != 0
  787. global_idx < module->import_global_count &&
  788. #endif
  789. module->import_globals[global_idx]
  790. .u.global.type.is_mutable) {
  791. set_error_buf_v(error_buf, error_buf_size,
  792. "constant expression required");
  793. goto fail;
  794. }
  795. if (global_idx < module->import_global_count) {
  796. global_type = module->import_globals[global_idx]
  797. .u.global.type.val_type;
  798. #if WASM_ENABLE_GC != 0
  799. if (wasm_is_type_multi_byte_type(global_type)) {
  800. WASMRefType *global_ref_type =
  801. module->import_globals[global_idx]
  802. .u.global.ref_type;
  803. bh_memcpy_s(&cur_ref_type,
  804. wasm_reftype_struct_size(global_ref_type),
  805. global_ref_type,
  806. wasm_reftype_struct_size(global_ref_type));
  807. }
  808. #endif
  809. }
  810. else {
  811. global_type =
  812. module
  813. ->globals[global_idx - module->import_global_count]
  814. .type.val_type;
  815. #if WASM_ENABLE_GC != 0
  816. if (wasm_is_type_multi_byte_type(global_type)) {
  817. WASMRefType *global_ref_type =
  818. module
  819. ->globals[global_idx
  820. - module->import_global_count]
  821. .ref_type;
  822. bh_memcpy_s(&cur_ref_type,
  823. wasm_reftype_struct_size(global_ref_type),
  824. global_ref_type,
  825. wasm_reftype_struct_size(global_ref_type));
  826. }
  827. #endif
  828. }
  829. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  830. #if WASM_ENABLE_GC != 0
  831. &cur_ref_type, 0,
  832. #endif
  833. &cur_value, error_buf,
  834. error_buf_size))
  835. goto fail;
  836. break;
  837. }
  838. #if WASM_ENABLE_GC != 0
  839. /* struct.new and array.new */
  840. case WASM_OP_GC_PREFIX:
  841. {
  842. read_leb_uint32(p, p_end, opcode1);
  843. switch (opcode1) {
  844. case WASM_OP_STRUCT_NEW:
  845. {
  846. WASMStructType *struct_type;
  847. WASMStructNewInitValues *struct_init_values = NULL;
  848. uint32 field_count;
  849. read_leb_uint32(p, p_end, type_idx);
  850. if (!check_type_index(module, module->type_count,
  851. type_idx, error_buf,
  852. error_buf_size)) {
  853. goto fail;
  854. }
  855. struct_type = (WASMStructType *)module->types[type_idx];
  856. if (struct_type->base_type.type_flag
  857. != WASM_TYPE_STRUCT) {
  858. set_error_buf(error_buf, error_buf_size,
  859. "unkown struct type");
  860. goto fail;
  861. }
  862. field_count = struct_type->field_count;
  863. if (!(struct_init_values = loader_malloc(
  864. offsetof(WASMStructNewInitValues, fields)
  865. + (uint64)field_count * sizeof(WASMValue),
  866. error_buf, error_buf_size))) {
  867. goto fail;
  868. }
  869. struct_init_values->type_idx = type_idx;
  870. struct_init_values->count = field_count;
  871. for (i = field_count; i > 0; i--) {
  872. WASMRefType *field_ref_type = NULL;
  873. uint32 field_idx = i - 1;
  874. uint8 field_type =
  875. struct_type->fields[field_idx].field_type;
  876. if (wasm_is_type_multi_byte_type(field_type)) {
  877. field_ref_type = wasm_reftype_map_find(
  878. struct_type->ref_type_maps,
  879. struct_type->ref_type_map_count, field_idx);
  880. }
  881. if (is_packed_type(field_type)) {
  882. field_type = VALUE_TYPE_I32;
  883. }
  884. if (!pop_const_expr_stack(
  885. &const_expr_ctx, NULL, field_type,
  886. field_ref_type, NULL,
  887. &struct_init_values->fields[field_idx],
  888. error_buf, error_buf_size)) {
  889. destroy_init_expr_data_recursive(
  890. module, struct_init_values);
  891. goto fail;
  892. }
  893. }
  894. cur_value.data = struct_init_values;
  895. wasm_set_refheaptype_typeidx(
  896. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  897. if (!push_const_expr_stack(
  898. &const_expr_ctx, flag, cur_ref_type.ref_type,
  899. &cur_ref_type, (uint8)opcode1, &cur_value,
  900. error_buf, error_buf_size)) {
  901. destroy_init_expr_data_recursive(
  902. module, struct_init_values);
  903. goto fail;
  904. }
  905. break;
  906. }
  907. case WASM_OP_STRUCT_NEW_DEFAULT:
  908. {
  909. read_leb_uint32(p, p_end, cur_value.type_index);
  910. type_idx = cur_value.type_index;
  911. if (!check_type_index(module, module->type_count,
  912. type_idx, error_buf,
  913. error_buf_size)) {
  914. goto fail;
  915. }
  916. if (module->types[type_idx]->type_flag
  917. != WASM_TYPE_STRUCT) {
  918. set_error_buf(error_buf, error_buf_size,
  919. "unkown struct type");
  920. goto fail;
  921. }
  922. cur_value.type_index = type_idx;
  923. cur_value.data = NULL;
  924. wasm_set_refheaptype_typeidx(
  925. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  926. if (!push_const_expr_stack(
  927. &const_expr_ctx, flag, cur_ref_type.ref_type,
  928. &cur_ref_type, (uint8)opcode1, &cur_value,
  929. error_buf, error_buf_size)) {
  930. goto fail;
  931. }
  932. break;
  933. }
  934. case WASM_OP_ARRAY_NEW:
  935. case WASM_OP_ARRAY_NEW_DEFAULT:
  936. case WASM_OP_ARRAY_NEW_FIXED:
  937. {
  938. WASMArrayNewInitValues *array_init_values = NULL;
  939. WASMArrayType *array_type = NULL;
  940. WASMRefType *elem_ref_type = NULL;
  941. uint64 total_size;
  942. uint8 elem_type;
  943. read_leb_uint32(p, p_end, cur_value.type_index);
  944. type_idx = cur_value.type_index;
  945. if (!check_type_index(module, module->type_count,
  946. type_idx, error_buf,
  947. error_buf_size)) {
  948. goto fail;
  949. }
  950. array_type = (WASMArrayType *)module->types[type_idx];
  951. if (array_type->base_type.type_flag
  952. != WASM_TYPE_ARRAY) {
  953. set_error_buf(error_buf, error_buf_size,
  954. "unkown array type");
  955. goto fail;
  956. }
  957. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  958. elem_type = array_type->elem_type;
  959. if (wasm_is_type_multi_byte_type(elem_type)) {
  960. elem_ref_type = array_type->elem_ref_type;
  961. }
  962. if (is_packed_type(elem_type)) {
  963. elem_type = VALUE_TYPE_I32;
  964. }
  965. if (opcode1 == WASM_OP_ARRAY_NEW) {
  966. WASMValue len_val = { 0 };
  967. uint64 size = 0;
  968. if (!pop_const_expr_stack(
  969. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  970. NULL, NULL, &len_val, error_buf,
  971. error_buf_size)) {
  972. goto fail;
  973. }
  974. size =
  975. sizeof(WASMArrayNewInitValues)
  976. + sizeof(WASMValue) * (uint64)len_val.i32;
  977. if (!(array_init_values = loader_malloc(
  978. size, error_buf, error_buf_size))) {
  979. goto fail;
  980. }
  981. array_init_values->type_idx = type_idx;
  982. array_init_values->length = len_val.i32;
  983. if (!pop_const_expr_stack(
  984. &const_expr_ctx, NULL, elem_type,
  985. elem_ref_type, NULL,
  986. &array_init_values->elem_data[0],
  987. error_buf, error_buf_size)) {
  988. destroy_init_expr_data_recursive(
  989. module, array_init_values);
  990. goto fail;
  991. }
  992. cur_value.data = array_init_values;
  993. }
  994. else {
  995. /* WASM_OP_ARRAY_NEW_FIXED */
  996. uint32 len;
  997. read_leb_uint32(p, p_end, len);
  998. total_size =
  999. (uint64)offsetof(WASMArrayNewInitValues,
  1000. elem_data)
  1001. + (uint64)sizeof(WASMValue) * len;
  1002. if (!(array_init_values =
  1003. loader_malloc(total_size, error_buf,
  1004. error_buf_size))) {
  1005. goto fail;
  1006. }
  1007. array_init_values->type_idx = type_idx;
  1008. array_init_values->length = len;
  1009. for (i = len; i > 0; i--) {
  1010. if (!pop_const_expr_stack(
  1011. &const_expr_ctx, NULL, elem_type,
  1012. elem_ref_type, NULL,
  1013. &array_init_values
  1014. ->elem_data[i - 1],
  1015. error_buf, error_buf_size)) {
  1016. destroy_init_expr_data_recursive(
  1017. module, array_init_values);
  1018. goto fail;
  1019. }
  1020. }
  1021. cur_value.data = array_init_values;
  1022. }
  1023. }
  1024. else {
  1025. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1026. WASMValue len_val;
  1027. uint32 len;
  1028. /* POP(i32) */
  1029. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1030. VALUE_TYPE_I32, NULL,
  1031. NULL, &len_val, error_buf,
  1032. error_buf_size)) {
  1033. goto fail;
  1034. }
  1035. len = len_val.i32;
  1036. cur_value.array_new_default.type_index = type_idx;
  1037. cur_value.array_new_default.length = len;
  1038. }
  1039. wasm_set_refheaptype_typeidx(
  1040. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1041. if (!push_const_expr_stack(
  1042. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1043. &cur_ref_type, (uint8)opcode1, &cur_value,
  1044. error_buf, error_buf_size)) {
  1045. if (array_init_values) {
  1046. destroy_init_expr_data_recursive(
  1047. module, array_init_values);
  1048. }
  1049. goto fail;
  1050. }
  1051. break;
  1052. }
  1053. case WASM_OP_ANY_CONVERT_EXTERN:
  1054. {
  1055. set_error_buf(error_buf, error_buf_size,
  1056. "unsupported constant expression of "
  1057. "extern.internalize");
  1058. goto fail;
  1059. }
  1060. case WASM_OP_EXTERN_CONVERT_ANY:
  1061. {
  1062. set_error_buf(error_buf, error_buf_size,
  1063. "unsupported constant expression of "
  1064. "extern.externalize");
  1065. goto fail;
  1066. }
  1067. case WASM_OP_REF_I31:
  1068. {
  1069. /* POP(i32) */
  1070. if (!pop_const_expr_stack(
  1071. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1072. NULL, &cur_value, error_buf, error_buf_size)) {
  1073. goto fail;
  1074. }
  1075. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1076. false, HEAP_TYPE_I31);
  1077. if (!push_const_expr_stack(
  1078. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1079. &cur_ref_type, (uint8)opcode1, &cur_value,
  1080. error_buf, error_buf_size)) {
  1081. goto fail;
  1082. }
  1083. break;
  1084. }
  1085. default:
  1086. set_error_buf(
  1087. error_buf, error_buf_size,
  1088. "type mismatch or constant expression required");
  1089. goto fail;
  1090. }
  1091. break;
  1092. }
  1093. #endif /* end of WASM_ENABLE_GC != 0 */
  1094. default:
  1095. {
  1096. set_error_buf(error_buf, error_buf_size,
  1097. "illegal opcode "
  1098. "or constant expression required "
  1099. "or type mismatch");
  1100. goto fail;
  1101. }
  1102. }
  1103. CHECK_BUF(p, p_end, 1);
  1104. flag = read_uint8(p);
  1105. }
  1106. /* There should be only one value left on the init value stack */
  1107. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1108. #if WASM_ENABLE_GC != 0
  1109. ref_type, &opcode,
  1110. #endif
  1111. &cur_value, error_buf, error_buf_size)) {
  1112. goto fail;
  1113. }
  1114. if (const_expr_ctx.sp != 0) {
  1115. set_error_buf(error_buf, error_buf_size,
  1116. "type mismatch: illegal constant opcode sequence");
  1117. goto fail;
  1118. }
  1119. init_expr->init_expr_type = flag;
  1120. init_expr->u = cur_value;
  1121. #if WASM_ENABLE_GC != 0
  1122. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1123. switch (opcode) {
  1124. case WASM_OP_STRUCT_NEW:
  1125. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1126. break;
  1127. case WASM_OP_STRUCT_NEW_DEFAULT:
  1128. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1129. break;
  1130. case WASM_OP_ARRAY_NEW:
  1131. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1132. break;
  1133. case WASM_OP_ARRAY_NEW_DEFAULT:
  1134. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1135. break;
  1136. case WASM_OP_ARRAY_NEW_FIXED:
  1137. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1138. break;
  1139. case WASM_OP_REF_I31:
  1140. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1141. break;
  1142. default:
  1143. bh_assert(0);
  1144. break;
  1145. }
  1146. }
  1147. #endif /* end of WASM_ENABLE_GC != 0 */
  1148. *p_buf = p;
  1149. destroy_const_expr_stack(&const_expr_ctx);
  1150. return true;
  1151. fail:
  1152. destroy_const_expr_stack(&const_expr_ctx);
  1153. return false;
  1154. }
  1155. static bool
  1156. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1157. {
  1158. if (mutable >= 2) {
  1159. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1160. return false;
  1161. }
  1162. return true;
  1163. }
  1164. #if WASM_ENABLE_GC != 0
  1165. static void
  1166. destroy_func_type(WASMFuncType *type)
  1167. {
  1168. /* Destroy the reference type hash set */
  1169. if (type->ref_type_maps)
  1170. wasm_runtime_free(type->ref_type_maps);
  1171. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1172. && WASM_ENABLE_LAZY_JIT != 0
  1173. if (type->call_to_llvm_jit_from_fast_jit)
  1174. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1175. #endif
  1176. /* Free the type */
  1177. wasm_runtime_free(type);
  1178. }
  1179. static void
  1180. destroy_struct_type(WASMStructType *type)
  1181. {
  1182. if (type->ref_type_maps)
  1183. wasm_runtime_free(type->ref_type_maps);
  1184. wasm_runtime_free(type);
  1185. }
  1186. static void
  1187. destroy_array_type(WASMArrayType *type)
  1188. {
  1189. wasm_runtime_free(type);
  1190. }
  1191. static void
  1192. destroy_wasm_type(WASMType *type)
  1193. {
  1194. if (type->ref_count > 1) {
  1195. /* The type is referenced by other types
  1196. of current wasm module */
  1197. type->ref_count--;
  1198. return;
  1199. }
  1200. if (type->type_flag == WASM_TYPE_FUNC)
  1201. destroy_func_type((WASMFuncType *)type);
  1202. else if (type->type_flag == WASM_TYPE_STRUCT)
  1203. destroy_struct_type((WASMStructType *)type);
  1204. else if (type->type_flag == WASM_TYPE_ARRAY)
  1205. destroy_array_type((WASMArrayType *)type);
  1206. else {
  1207. bh_assert(0);
  1208. }
  1209. }
  1210. /* Resolve (ref null ht) or (ref ht) */
  1211. static bool
  1212. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1213. WASMModule *module, uint32 type_count, bool nullable,
  1214. WASMRefType *ref_type, char *error_buf,
  1215. uint32 error_buf_size)
  1216. {
  1217. const uint8 *p = *p_buf, *p_end = buf_end;
  1218. ref_type->ref_type =
  1219. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1220. ref_type->ref_ht_common.nullable = nullable;
  1221. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1222. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1223. /* heap type is (type i), i : typeidx, >= 0 */
  1224. if (!check_type_index(module, type_count,
  1225. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1226. error_buf_size)) {
  1227. return false;
  1228. }
  1229. }
  1230. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1231. /* heap type is func, extern, any, eq, i31 or data */
  1232. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1233. return false;
  1234. }
  1235. *p_buf = p;
  1236. return true;
  1237. fail:
  1238. return false;
  1239. }
  1240. static bool
  1241. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1242. WASMModule *module, uint32 type_count,
  1243. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1244. bool allow_packed_type, char *error_buf,
  1245. uint32 error_buf_size)
  1246. {
  1247. const uint8 *p = *p_buf, *p_end = buf_end;
  1248. uint8 type;
  1249. memset(ref_type, 0, sizeof(WASMRefType));
  1250. CHECK_BUF(p, p_end, 1);
  1251. type = read_uint8(p);
  1252. if (wasm_is_reftype_htref_nullable(type)) {
  1253. /* (ref null ht) */
  1254. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1255. ref_type, error_buf, error_buf_size))
  1256. return false;
  1257. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1258. *p_need_ref_type_map = true;
  1259. else {
  1260. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1261. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1262. multi-byte type to one-byte type to reduce the footprint and
  1263. the complexity of type equal/subtype checking */
  1264. ref_type->ref_type =
  1265. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1266. *p_need_ref_type_map = false;
  1267. }
  1268. }
  1269. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1270. /* (ref ht) */
  1271. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1272. ref_type, error_buf, error_buf_size))
  1273. return false;
  1274. *p_need_ref_type_map = true;
  1275. #if WASM_ENABLE_STRINGREF != 0
  1276. /* covert (ref string) to stringref */
  1277. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1278. ref_type->ref_type =
  1279. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1280. *p_need_ref_type_map = false;
  1281. }
  1282. #endif
  1283. }
  1284. else {
  1285. /* type which can be represented by one byte */
  1286. if (!is_valid_value_type_for_interpreter(type)
  1287. && !(allow_packed_type && is_packed_type(type))) {
  1288. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1289. return false;
  1290. }
  1291. ref_type->ref_type = type;
  1292. *p_need_ref_type_map = false;
  1293. #if WASM_ENABLE_WAMR_COMPILER != 0
  1294. /* If any value's type is v128, mark the module as SIMD used */
  1295. if (type == VALUE_TYPE_V128)
  1296. module->is_simd_used = true;
  1297. #endif
  1298. }
  1299. *p_buf = p;
  1300. return true;
  1301. fail:
  1302. return false;
  1303. }
  1304. static WASMRefType *
  1305. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1306. char *error_buf, uint32 error_buf_size)
  1307. {
  1308. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1309. if (!ret) {
  1310. set_error_buf(error_buf, error_buf_size,
  1311. "insert ref type to hash set failed");
  1312. }
  1313. return ret;
  1314. }
  1315. static bool
  1316. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1317. uint32 type_count, uint32 type_idx, char *error_buf,
  1318. uint32 error_buf_size)
  1319. {
  1320. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1321. uint32 param_count, result_count, i, j = 0;
  1322. uint32 param_cell_num, ret_cell_num;
  1323. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1324. uint64 total_size;
  1325. bool need_ref_type_map;
  1326. WASMRefType ref_type;
  1327. WASMFuncType *type = NULL;
  1328. /* Parse first time to resolve param count, result count and
  1329. ref type map count */
  1330. read_leb_uint32(p, p_end, param_count);
  1331. p_org = p;
  1332. for (i = 0; i < param_count; i++) {
  1333. if (!resolve_value_type(&p, p_end, module, type_count,
  1334. &need_ref_type_map, &ref_type, false, error_buf,
  1335. error_buf_size)) {
  1336. return false;
  1337. }
  1338. if (need_ref_type_map)
  1339. ref_type_map_count++;
  1340. }
  1341. read_leb_uint32(p, p_end, result_count);
  1342. for (i = 0; i < result_count; i++) {
  1343. if (!resolve_value_type(&p, p_end, module, type_count,
  1344. &need_ref_type_map, &ref_type, false, error_buf,
  1345. error_buf_size)) {
  1346. return false;
  1347. }
  1348. if (need_ref_type_map) {
  1349. ref_type_map_count++;
  1350. result_ref_type_map_count++;
  1351. }
  1352. }
  1353. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1354. "ref type map count: %d",
  1355. type_idx, param_count, result_count, ref_type_map_count);
  1356. /* Parse second time to resolve param types, result types and
  1357. ref type map info */
  1358. p = p_org;
  1359. total_size = offsetof(WASMFuncType, types)
  1360. + sizeof(uint8) * (uint64)(param_count + result_count);
  1361. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1362. return false;
  1363. }
  1364. if (ref_type_map_count > 0) {
  1365. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1366. if (!(type->ref_type_maps =
  1367. loader_malloc(total_size, error_buf, error_buf_size))) {
  1368. goto fail;
  1369. }
  1370. }
  1371. type->base_type.type_flag = WASM_TYPE_FUNC;
  1372. type->param_count = param_count;
  1373. type->result_count = result_count;
  1374. type->ref_type_map_count = ref_type_map_count;
  1375. if (ref_type_map_count > 0) {
  1376. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1377. - result_ref_type_map_count;
  1378. }
  1379. for (i = 0; i < param_count; i++) {
  1380. if (!resolve_value_type(&p, p_end, module, type_count,
  1381. &need_ref_type_map, &ref_type, false, error_buf,
  1382. error_buf_size)) {
  1383. goto fail;
  1384. }
  1385. type->types[i] = ref_type.ref_type;
  1386. if (need_ref_type_map) {
  1387. type->ref_type_maps[j].index = i;
  1388. if (!(type->ref_type_maps[j++].ref_type =
  1389. reftype_set_insert(module->ref_type_set, &ref_type,
  1390. error_buf, error_buf_size))) {
  1391. goto fail;
  1392. }
  1393. }
  1394. }
  1395. read_leb_uint32(p, p_end, result_count);
  1396. for (i = 0; i < result_count; i++) {
  1397. if (!resolve_value_type(&p, p_end, module, type_count,
  1398. &need_ref_type_map, &ref_type, false, error_buf,
  1399. error_buf_size)) {
  1400. goto fail;
  1401. }
  1402. type->types[param_count + i] = ref_type.ref_type;
  1403. if (need_ref_type_map) {
  1404. type->ref_type_maps[j].index = param_count + i;
  1405. if (!(type->ref_type_maps[j++].ref_type =
  1406. reftype_set_insert(module->ref_type_set, &ref_type,
  1407. error_buf, error_buf_size))) {
  1408. goto fail;
  1409. }
  1410. }
  1411. }
  1412. bh_assert(j == type->ref_type_map_count);
  1413. #if TRACE_WASM_LOADER != 0
  1414. os_printf("type %d = ", type_idx);
  1415. wasm_dump_func_type(type);
  1416. #endif
  1417. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1418. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1419. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1420. set_error_buf(error_buf, error_buf_size,
  1421. "param count or result count too large");
  1422. goto fail;
  1423. }
  1424. type->param_cell_num = (uint16)param_cell_num;
  1425. type->ret_cell_num = (uint16)ret_cell_num;
  1426. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1427. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1428. #endif
  1429. #if WASM_ENABLE_WAMR_COMPILER != 0
  1430. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1431. if (type->types[i] == VALUE_TYPE_V128)
  1432. module->is_simd_used = true;
  1433. }
  1434. #endif
  1435. *p_buf = p;
  1436. module->types[type_idx] = (WASMType *)type;
  1437. return true;
  1438. fail:
  1439. if (type)
  1440. destroy_func_type(type);
  1441. return false;
  1442. }
  1443. static bool
  1444. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1445. WASMModule *module, uint32 type_count, uint32 type_idx,
  1446. char *error_buf, uint32 error_buf_size)
  1447. {
  1448. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1449. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1450. uint32 i, j = 0, offset;
  1451. uint16 *reference_table;
  1452. uint64 total_size;
  1453. uint8 mutable;
  1454. bool need_ref_type_map;
  1455. WASMRefType ref_type;
  1456. WASMStructType *type = NULL;
  1457. /* Parse first time to resolve field count and ref type map count */
  1458. read_leb_uint32(p, p_end, field_count);
  1459. p_org = p;
  1460. for (i = 0; i < field_count; i++) {
  1461. if (!resolve_value_type(&p, p_end, module, type_count,
  1462. &need_ref_type_map, &ref_type, true, error_buf,
  1463. error_buf_size)) {
  1464. return false;
  1465. }
  1466. if (need_ref_type_map)
  1467. ref_type_map_count++;
  1468. if (wasm_is_type_reftype(ref_type.ref_type))
  1469. ref_field_count++;
  1470. CHECK_BUF(p, p_end, 1);
  1471. mutable = read_uint8(p);
  1472. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1473. return false;
  1474. }
  1475. }
  1476. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1477. type_idx, field_count, ref_type_map_count);
  1478. /* Parse second time to resolve field types and ref type map info */
  1479. p = p_org;
  1480. total_size = offsetof(WASMStructType, fields)
  1481. + sizeof(WASMStructFieldType) * (uint64)field_count
  1482. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1483. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1484. return false;
  1485. }
  1486. if (ref_type_map_count > 0) {
  1487. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1488. if (!(type->ref_type_maps =
  1489. loader_malloc(total_size, error_buf, error_buf_size))) {
  1490. goto fail;
  1491. }
  1492. }
  1493. type->reference_table = reference_table =
  1494. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1495. + sizeof(WASMStructFieldType) * field_count);
  1496. *reference_table++ = ref_field_count;
  1497. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1498. type->field_count = field_count;
  1499. type->ref_type_map_count = ref_type_map_count;
  1500. offset = (uint32)offsetof(WASMStructObject, field_data);
  1501. for (i = 0; i < field_count; i++) {
  1502. if (!resolve_value_type(&p, p_end, module, type_count,
  1503. &need_ref_type_map, &ref_type, true, error_buf,
  1504. error_buf_size)) {
  1505. goto fail;
  1506. }
  1507. type->fields[i].field_type = ref_type.ref_type;
  1508. if (need_ref_type_map) {
  1509. type->ref_type_maps[j].index = i;
  1510. if (!(type->ref_type_maps[j++].ref_type =
  1511. reftype_set_insert(module->ref_type_set, &ref_type,
  1512. error_buf, error_buf_size))) {
  1513. goto fail;
  1514. }
  1515. }
  1516. CHECK_BUF(p, p_end, 1);
  1517. type->fields[i].field_flags = read_uint8(p);
  1518. type->fields[i].field_size =
  1519. (uint8)wasm_reftype_size(ref_type.ref_type);
  1520. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1521. || defined(BUILD_TARGET_X86_32))
  1522. if (type->fields[i].field_size == 2)
  1523. offset = align_uint(offset, 2);
  1524. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1525. offset = align_uint(offset, 4);
  1526. #endif
  1527. type->fields[i].field_offset = offset;
  1528. if (wasm_is_type_reftype(ref_type.ref_type))
  1529. *reference_table++ = offset;
  1530. offset += type->fields[i].field_size;
  1531. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1532. type->fields[i].field_flags, type->fields[i].field_type);
  1533. }
  1534. type->total_size = offset;
  1535. bh_assert(j == type->ref_type_map_count);
  1536. #if TRACE_WASM_LOADER != 0
  1537. os_printf("type %d = ", type_idx);
  1538. wasm_dump_struct_type(type);
  1539. #endif
  1540. *p_buf = p;
  1541. module->types[type_idx] = (WASMType *)type;
  1542. return true;
  1543. fail:
  1544. if (type)
  1545. destroy_struct_type(type);
  1546. return false;
  1547. }
  1548. static bool
  1549. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1550. WASMModule *module, uint32 type_count, uint32 type_idx,
  1551. char *error_buf, uint32 error_buf_size)
  1552. {
  1553. const uint8 *p = *p_buf, *p_end = buf_end;
  1554. uint8 mutable;
  1555. bool need_ref_type_map;
  1556. WASMRefType ref_type;
  1557. WASMArrayType *type = NULL;
  1558. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1559. &ref_type, true, error_buf, error_buf_size)) {
  1560. return false;
  1561. }
  1562. CHECK_BUF(p, p_end, 1);
  1563. mutable = read_uint8(p);
  1564. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1565. return false;
  1566. }
  1567. LOG_VERBOSE("type %u: array", type_idx);
  1568. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1569. error_buf_size))) {
  1570. return false;
  1571. }
  1572. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1573. type->elem_flags = mutable;
  1574. type->elem_type = ref_type.ref_type;
  1575. if (need_ref_type_map) {
  1576. if (!(type->elem_ref_type =
  1577. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1578. error_buf_size))) {
  1579. goto fail;
  1580. }
  1581. }
  1582. #if TRACE_WASM_LOADER != 0
  1583. os_printf("type %d = ", type_idx);
  1584. wasm_dump_array_type(type);
  1585. #endif
  1586. *p_buf = p;
  1587. module->types[type_idx] = (WASMType *)type;
  1588. return true;
  1589. fail:
  1590. if (type)
  1591. destroy_array_type(type);
  1592. return false;
  1593. }
  1594. static bool
  1595. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1596. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1597. {
  1598. if (heap_type >= 0) {
  1599. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1600. error_buf_size)) {
  1601. return false;
  1602. }
  1603. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1604. heap_type);
  1605. }
  1606. else {
  1607. if (!wasm_is_valid_heap_type(heap_type)) {
  1608. set_error_buf(error_buf, error_buf_size, "unknown type");
  1609. return false;
  1610. }
  1611. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1612. heap_type);
  1613. if (nullable) {
  1614. /* For (ref null func/extern/any/eq/i31/data),
  1615. they are same as
  1616. funcref/externref/anyref/eqref/i31ref/dataref,
  1617. we convert the multi-byte type to one-byte
  1618. type to reduce the footprint and the
  1619. complexity of type equal/subtype checking */
  1620. ref_type->ref_type =
  1621. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1622. }
  1623. }
  1624. return true;
  1625. }
  1626. static void
  1627. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1628. WASMRefType *ref_type2)
  1629. {
  1630. /**
  1631. * The difference rt1 ∖ rt2 between two reference types is defined as
  1632. * follows:
  1633. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1634. * (ref ht2) = (ref null?1 ht1)
  1635. */
  1636. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1637. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1638. ref_type1, wasm_reftype_struct_size(ref_type1));
  1639. }
  1640. else {
  1641. ref_type_diff->ref_type = ref_type1->ref_type;
  1642. }
  1643. if (ref_type2->ref_ht_common.nullable) {
  1644. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1645. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1646. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1647. (int32)ref_type_diff->ref_type - 0x80);
  1648. }
  1649. else {
  1650. ref_type_diff->ref_ht_typeidx.nullable = false;
  1651. }
  1652. }
  1653. }
  1654. #else /* else of WASM_ENABLE_GC != 0 */
  1655. static void
  1656. destroy_wasm_type(WASMType *type)
  1657. {
  1658. if (type->ref_count > 1) {
  1659. /* The type is referenced by other types
  1660. of current wasm module */
  1661. type->ref_count--;
  1662. return;
  1663. }
  1664. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1665. && WASM_ENABLE_LAZY_JIT != 0
  1666. if (type->call_to_llvm_jit_from_fast_jit)
  1667. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1668. #endif
  1669. wasm_runtime_free(type);
  1670. }
  1671. #endif /* end of WASM_ENABLE_GC != 0 */
  1672. static bool
  1673. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1674. char *error_buf, uint32 error_buf_size)
  1675. {
  1676. const uint8 *p = buf, *p_end = buf_end;
  1677. uint32 type_count, i;
  1678. uint64 total_size;
  1679. uint8 flag;
  1680. #if WASM_ENABLE_GC != 0
  1681. uint32 processed_type_count = 0;
  1682. #endif
  1683. read_leb_uint32(p, p_end, type_count);
  1684. if (type_count) {
  1685. module->type_count = type_count;
  1686. total_size = sizeof(WASMType *) * (uint64)type_count;
  1687. if (!(module->types =
  1688. loader_malloc(total_size, error_buf, error_buf_size))) {
  1689. return false;
  1690. }
  1691. #if WASM_ENABLE_GC == 0
  1692. for (i = 0; i < type_count; i++) {
  1693. WASMFuncType *type;
  1694. const uint8 *p_org;
  1695. uint32 param_count, result_count, j;
  1696. uint32 param_cell_num, ret_cell_num;
  1697. CHECK_BUF(p, p_end, 1);
  1698. flag = read_uint8(p);
  1699. if (flag != 0x60) {
  1700. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1701. return false;
  1702. }
  1703. read_leb_uint32(p, p_end, param_count);
  1704. /* Resolve param count and result count firstly */
  1705. p_org = p;
  1706. CHECK_BUF(p, p_end, param_count);
  1707. p += param_count;
  1708. read_leb_uint32(p, p_end, result_count);
  1709. CHECK_BUF(p, p_end, result_count);
  1710. p = p_org;
  1711. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1712. set_error_buf(error_buf, error_buf_size,
  1713. "param count or result count too large");
  1714. return false;
  1715. }
  1716. total_size = offsetof(WASMFuncType, types)
  1717. + sizeof(uint8) * (uint64)(param_count + result_count);
  1718. if (!(type = module->types[i] =
  1719. loader_malloc(total_size, error_buf, error_buf_size))) {
  1720. return false;
  1721. }
  1722. /* Resolve param types and result types */
  1723. type->ref_count = 1;
  1724. type->param_count = (uint16)param_count;
  1725. type->result_count = (uint16)result_count;
  1726. for (j = 0; j < param_count; j++) {
  1727. CHECK_BUF(p, p_end, 1);
  1728. type->types[j] = read_uint8(p);
  1729. }
  1730. read_leb_uint32(p, p_end, result_count);
  1731. for (j = 0; j < result_count; j++) {
  1732. CHECK_BUF(p, p_end, 1);
  1733. type->types[param_count + j] = read_uint8(p);
  1734. }
  1735. for (j = 0; j < param_count + result_count; j++) {
  1736. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1737. set_error_buf(error_buf, error_buf_size,
  1738. "unknown value type");
  1739. return false;
  1740. }
  1741. }
  1742. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1743. ret_cell_num =
  1744. wasm_get_cell_num(type->types + param_count, result_count);
  1745. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1746. set_error_buf(error_buf, error_buf_size,
  1747. "param count or result count too large");
  1748. return false;
  1749. }
  1750. type->param_cell_num = (uint16)param_cell_num;
  1751. type->ret_cell_num = (uint16)ret_cell_num;
  1752. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1753. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1754. #endif
  1755. #if WASM_ENABLE_WAMR_COMPILER != 0
  1756. for (j = 0; j < type->param_count + type->result_count; j++) {
  1757. if (type->types[j] == VALUE_TYPE_V128)
  1758. module->is_simd_used = true;
  1759. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1760. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1761. module->is_ref_types_used = true;
  1762. }
  1763. #endif
  1764. /* If there is already a same type created, use it instead */
  1765. for (j = 0; j < i; j++) {
  1766. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1767. if (module->types[j]->ref_count == UINT16_MAX) {
  1768. set_error_buf(error_buf, error_buf_size,
  1769. "wasm type's ref count too large");
  1770. return false;
  1771. }
  1772. destroy_wasm_type(type);
  1773. module->types[i] = module->types[j];
  1774. module->types[j]->ref_count++;
  1775. break;
  1776. }
  1777. }
  1778. }
  1779. #else /* else of WASM_ENABLE_GC == 0 */
  1780. for (i = 0; i < type_count; i++) {
  1781. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1782. uint32 rec_count = 1, j;
  1783. bool is_sub_final = true;
  1784. CHECK_BUF(p, p_end, 1);
  1785. flag = read_uint8(p);
  1786. if (flag == DEFINED_TYPE_REC) {
  1787. read_leb_uint32(p, p_end, rec_count);
  1788. if (rec_count > 1) {
  1789. uint64 new_total_size;
  1790. /* integer overflow */
  1791. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1792. set_error_buf(error_buf, error_buf_size,
  1793. "recursive type count too large");
  1794. return false;
  1795. }
  1796. module->type_count += rec_count - 1;
  1797. new_total_size =
  1798. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1799. if (new_total_size > UINT32_MAX) {
  1800. set_error_buf(error_buf, error_buf_size,
  1801. "allocate memory failed");
  1802. return false;
  1803. }
  1804. MEM_REALLOC(module->types, (uint32)total_size,
  1805. (uint32)new_total_size);
  1806. total_size = new_total_size;
  1807. }
  1808. LOG_VERBOSE("Processing rec group [%d-%d]",
  1809. processed_type_count,
  1810. processed_type_count + rec_count - 1);
  1811. }
  1812. else {
  1813. p--;
  1814. }
  1815. for (j = 0; j < rec_count; j++) {
  1816. WASMType *cur_type = NULL;
  1817. CHECK_BUF(p, p_end, 1);
  1818. flag = read_uint8(p);
  1819. parent_type_idx = -1;
  1820. if (flag == DEFINED_TYPE_SUB
  1821. || flag == DEFINED_TYPE_SUB_FINAL) {
  1822. read_leb_uint32(p, p_end, super_type_count);
  1823. if (super_type_count > 1) {
  1824. set_error_buf(error_buf, error_buf_size,
  1825. "super type count too large");
  1826. return false;
  1827. }
  1828. if (super_type_count > 0) {
  1829. read_leb_uint32(p, p_end, parent_type_idx);
  1830. if (parent_type_idx >= processed_type_count + j) {
  1831. set_error_buf_v(error_buf, error_buf_size,
  1832. "unknown type %d", parent_type_idx);
  1833. return false;
  1834. }
  1835. if (module->types[parent_type_idx]->is_sub_final) {
  1836. set_error_buf(error_buf, error_buf_size,
  1837. "sub type can not inherit from "
  1838. "a final super type");
  1839. return false;
  1840. }
  1841. }
  1842. if (flag == DEFINED_TYPE_SUB)
  1843. is_sub_final = false;
  1844. CHECK_BUF(p, p_end, 1);
  1845. flag = read_uint8(p);
  1846. }
  1847. if (flag == DEFINED_TYPE_FUNC) {
  1848. if (!resolve_func_type(&p, buf_end, module,
  1849. processed_type_count + rec_count,
  1850. processed_type_count + j, error_buf,
  1851. error_buf_size)) {
  1852. return false;
  1853. }
  1854. }
  1855. else if (flag == DEFINED_TYPE_STRUCT) {
  1856. if (!resolve_struct_type(&p, buf_end, module,
  1857. processed_type_count + rec_count,
  1858. processed_type_count + j,
  1859. error_buf, error_buf_size)) {
  1860. return false;
  1861. }
  1862. }
  1863. else if (flag == DEFINED_TYPE_ARRAY) {
  1864. if (!resolve_array_type(&p, buf_end, module,
  1865. processed_type_count + rec_count,
  1866. processed_type_count + j, error_buf,
  1867. error_buf_size)) {
  1868. return false;
  1869. }
  1870. }
  1871. else {
  1872. set_error_buf(error_buf, error_buf_size,
  1873. "invalid type flag");
  1874. return false;
  1875. }
  1876. cur_type = module->types[processed_type_count + j];
  1877. cur_type->ref_count = 1;
  1878. cur_type->parent_type_idx = parent_type_idx;
  1879. cur_type->is_sub_final = is_sub_final;
  1880. cur_type->rec_count = rec_count;
  1881. cur_type->rec_idx = j;
  1882. cur_type->rec_begin_type_idx = processed_type_count;
  1883. }
  1884. /* resolve subtyping relationship in current rec group */
  1885. for (j = 0; j < rec_count; j++) {
  1886. WASMType *cur_type = module->types[processed_type_count + j];
  1887. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1888. WASMType *parent_type =
  1889. module->types[cur_type->parent_type_idx];
  1890. cur_type->parent_type = parent_type;
  1891. cur_type->root_type = parent_type->root_type;
  1892. if (parent_type->inherit_depth == UINT16_MAX) {
  1893. set_error_buf(error_buf, error_buf_size,
  1894. "parent type's inherit depth too large");
  1895. return false;
  1896. }
  1897. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1898. }
  1899. else {
  1900. cur_type->parent_type = NULL;
  1901. cur_type->root_type = cur_type;
  1902. cur_type->inherit_depth = 0;
  1903. }
  1904. }
  1905. for (j = 0; j < rec_count; j++) {
  1906. WASMType *cur_type = module->types[processed_type_count + j];
  1907. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1908. WASMType *parent_type =
  1909. module->types[cur_type->parent_type_idx];
  1910. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1911. module->types,
  1912. module->type_count)) {
  1913. set_error_buf_v(error_buf, error_buf_size,
  1914. "sub type %u does not match super type",
  1915. processed_type_count + j);
  1916. return false;
  1917. }
  1918. }
  1919. }
  1920. /* If there is already an equivalence type or a group of equivalence
  1921. recursive types created, use it or them instead */
  1922. for (j = 0; j < processed_type_count;) {
  1923. WASMType *src_type = module->types[j];
  1924. WASMType *cur_type = module->types[processed_type_count];
  1925. uint32 k, src_rec_count;
  1926. src_rec_count = src_type->rec_count;
  1927. if (src_rec_count != rec_count) {
  1928. /* no type equivalence */
  1929. j += src_rec_count;
  1930. continue;
  1931. }
  1932. for (k = 0; k < rec_count; k++) {
  1933. src_type = module->types[j + k];
  1934. cur_type = module->types[processed_type_count + k];
  1935. if (!wasm_type_equal(src_type, cur_type, module->types,
  1936. module->type_count)) {
  1937. break;
  1938. }
  1939. }
  1940. if (k < rec_count) {
  1941. /* no type equivalence */
  1942. j += src_rec_count;
  1943. continue;
  1944. }
  1945. /* type equivalence */
  1946. for (k = 0; k < rec_count; k++) {
  1947. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1948. set_error_buf(error_buf, error_buf_size,
  1949. "wasm type's ref count too large");
  1950. return false;
  1951. }
  1952. destroy_wasm_type(module->types[processed_type_count + k]);
  1953. module->types[processed_type_count + k] =
  1954. module->types[j + k];
  1955. module->types[j + k]->ref_count++;
  1956. }
  1957. break;
  1958. }
  1959. if (rec_count > 1) {
  1960. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1961. processed_type_count,
  1962. processed_type_count + rec_count - 1);
  1963. }
  1964. processed_type_count += rec_count;
  1965. }
  1966. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1967. * module->type_count,
  1968. error_buf, error_buf_size))) {
  1969. return false;
  1970. }
  1971. #endif /* end of WASM_ENABLE_GC == 0 */
  1972. }
  1973. if (p != p_end) {
  1974. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1975. return false;
  1976. }
  1977. LOG_VERBOSE("Load type section success.\n");
  1978. return true;
  1979. fail:
  1980. return false;
  1981. }
  1982. static void
  1983. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  1984. uint32 *max_size)
  1985. {
  1986. uint32 default_max_size;
  1987. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  1988. * ABI unchanged */
  1989. (void)is_table64;
  1990. if (UINT32_MAX / 2 > init_size)
  1991. default_max_size = init_size * 2;
  1992. else
  1993. default_max_size = UINT32_MAX;
  1994. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1995. default_max_size = WASM_TABLE_MAX_SIZE;
  1996. if (max_size_flag) {
  1997. /* module defines the table limitation */
  1998. bh_assert(init_size <= *max_size);
  1999. if (init_size < *max_size) {
  2000. *max_size =
  2001. *max_size < default_max_size ? *max_size : default_max_size;
  2002. }
  2003. }
  2004. else {
  2005. /* partial defined table limitation, gives a default value */
  2006. *max_size = default_max_size;
  2007. }
  2008. }
  2009. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2010. /**
  2011. * Find export item of a module with export info:
  2012. * module name, field name and export kind
  2013. */
  2014. static WASMExport *
  2015. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2016. const char *field_name, uint8 export_kind,
  2017. char *error_buf, uint32 error_buf_size)
  2018. {
  2019. WASMExport *export =
  2020. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2021. export_kind, error_buf, error_buf_size);
  2022. return export;
  2023. }
  2024. #endif
  2025. #if WASM_ENABLE_MULTI_MODULE != 0
  2026. static WASMTable *
  2027. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2028. uint32 init_size, uint32 max_size, char *error_buf,
  2029. uint32 error_buf_size)
  2030. {
  2031. WASMModuleCommon *module_reg;
  2032. WASMTable *table = NULL;
  2033. WASMExport *export = NULL;
  2034. WASMModule *module = NULL;
  2035. module_reg = wasm_runtime_find_module_registered(module_name);
  2036. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2037. LOG_DEBUG("can not find a module named %s for table", module_name);
  2038. set_error_buf(error_buf, error_buf_size, "unknown import");
  2039. return NULL;
  2040. }
  2041. module = (WASMModule *)module_reg;
  2042. export =
  2043. wasm_loader_find_export(module, module_name, table_name,
  2044. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2045. if (!export) {
  2046. return NULL;
  2047. }
  2048. /* resolve table and check the init/max size */
  2049. if (export->index < module->import_table_count) {
  2050. table =
  2051. module->import_tables[export->index].u.table.import_table_linked;
  2052. }
  2053. else {
  2054. table = &(module->tables[export->index - module->import_table_count]);
  2055. }
  2056. if (table->table_type.init_size < init_size
  2057. || table->table_type.max_size > max_size) {
  2058. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2059. module_name, table_name, table->table_type.init_size,
  2060. table->table_type.max_size, init_size, max_size);
  2061. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2062. return NULL;
  2063. }
  2064. return table;
  2065. }
  2066. static WASMMemory *
  2067. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2068. uint32 init_page_count, uint32 max_page_count,
  2069. char *error_buf, uint32 error_buf_size)
  2070. {
  2071. WASMModuleCommon *module_reg;
  2072. WASMMemory *memory = NULL;
  2073. WASMExport *export = NULL;
  2074. WASMModule *module = NULL;
  2075. module_reg = wasm_runtime_find_module_registered(module_name);
  2076. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2077. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2078. set_error_buf(error_buf, error_buf_size, "unknown import");
  2079. return NULL;
  2080. }
  2081. module = (WASMModule *)module_reg;
  2082. export =
  2083. wasm_loader_find_export(module, module_name, memory_name,
  2084. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2085. if (!export) {
  2086. return NULL;
  2087. }
  2088. /* resolve memory and check the init/max page count */
  2089. if (export->index < module->import_memory_count) {
  2090. memory = module->import_memories[export->index]
  2091. .u.memory.import_memory_linked;
  2092. }
  2093. else {
  2094. memory =
  2095. &(module->memories[export->index - module->import_memory_count]);
  2096. }
  2097. if (memory->init_page_count < init_page_count
  2098. || memory->max_page_count > max_page_count) {
  2099. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2100. module_name, memory_name, memory->init_page_count,
  2101. memory->max_page_count, init_page_count, max_page_count);
  2102. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2103. return NULL;
  2104. }
  2105. return memory;
  2106. }
  2107. static WASMGlobal *
  2108. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2109. uint8 type, bool is_mutable, char *error_buf,
  2110. uint32 error_buf_size)
  2111. {
  2112. WASMModuleCommon *module_reg;
  2113. WASMGlobal *global = NULL;
  2114. WASMExport *export = NULL;
  2115. WASMModule *module = NULL;
  2116. module_reg = wasm_runtime_find_module_registered(module_name);
  2117. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2118. LOG_DEBUG("can not find a module named %s for global", module_name);
  2119. set_error_buf(error_buf, error_buf_size, "unknown import");
  2120. return NULL;
  2121. }
  2122. module = (WASMModule *)module_reg;
  2123. export =
  2124. wasm_loader_find_export(module, module_name, global_name,
  2125. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2126. if (!export) {
  2127. return NULL;
  2128. }
  2129. /* resolve and check the global */
  2130. if (export->index < module->import_global_count) {
  2131. global =
  2132. module->import_globals[export->index].u.global.import_global_linked;
  2133. }
  2134. else {
  2135. global =
  2136. &(module->globals[export->index - module->import_global_count]);
  2137. }
  2138. if (global->type.val_type != type
  2139. || global->type.is_mutable != is_mutable) {
  2140. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2141. module_name, global_name, global->type.val_type,
  2142. global->type.is_mutable, type, is_mutable);
  2143. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2144. return NULL;
  2145. }
  2146. return global;
  2147. }
  2148. #if WASM_ENABLE_TAGS != 0
  2149. static WASMTag *
  2150. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2151. const WASMType *expected_tag_type,
  2152. uint32 *linked_tag_index, char *error_buf,
  2153. uint32 error_buf_size)
  2154. {
  2155. WASMModuleCommon *module_reg;
  2156. WASMTag *tag = NULL;
  2157. WASMExport *export = NULL;
  2158. WASMModule *module = NULL;
  2159. module_reg = wasm_runtime_find_module_registered(module_name);
  2160. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2161. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2162. tag_name);
  2163. set_error_buf(error_buf, error_buf_size, "unknown import");
  2164. return NULL;
  2165. }
  2166. module = (WASMModule *)module_reg;
  2167. export =
  2168. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2169. error_buf, error_buf_size);
  2170. if (!export) {
  2171. return NULL;
  2172. }
  2173. /* resolve tag type and tag */
  2174. if (export->index < module->import_tag_count) {
  2175. /* importing an imported tag from the submodule */
  2176. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2177. }
  2178. else {
  2179. /* importing an section tag from the submodule */
  2180. tag = module->tags[export->index - module->import_tag_count];
  2181. }
  2182. /* check function type */
  2183. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2184. module->type_count)) {
  2185. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2186. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2187. return NULL;
  2188. }
  2189. if (linked_tag_index != NULL) {
  2190. *linked_tag_index = export->index;
  2191. }
  2192. return tag;
  2193. }
  2194. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2195. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2196. static bool
  2197. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2198. const WASMModule *parent_module,
  2199. const char *sub_module_name, const char *function_name,
  2200. WASMFunctionImport *function, bool no_resolve,
  2201. char *error_buf, uint32 error_buf_size)
  2202. {
  2203. const uint8 *p = *p_buf, *p_end = buf_end;
  2204. uint32 declare_type_index = 0;
  2205. read_leb_uint32(p, p_end, declare_type_index);
  2206. *p_buf = p;
  2207. if (declare_type_index >= parent_module->type_count) {
  2208. set_error_buf(error_buf, error_buf_size, "unknown type");
  2209. return false;
  2210. }
  2211. #if WASM_ENABLE_GC != 0
  2212. function->type_idx = declare_type_index;
  2213. #endif
  2214. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2215. declare_type_index = wasm_get_smallest_type_idx(
  2216. parent_module->types, parent_module->type_count, declare_type_index);
  2217. #endif
  2218. function->func_type =
  2219. (WASMFuncType *)parent_module->types[declare_type_index];
  2220. function->module_name = (char *)sub_module_name;
  2221. function->field_name = (char *)function_name;
  2222. function->attachment = NULL;
  2223. function->signature = NULL;
  2224. function->call_conv_raw = false;
  2225. /* lookup registered native symbols first */
  2226. if (!no_resolve) {
  2227. wasm_resolve_import_func(parent_module, function);
  2228. }
  2229. return true;
  2230. fail:
  2231. return false;
  2232. }
  2233. static bool
  2234. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2235. uint32 error_buf_size)
  2236. {
  2237. if (max_size < init_size) {
  2238. set_error_buf(error_buf, error_buf_size,
  2239. "size minimum must not be greater than maximum");
  2240. return false;
  2241. }
  2242. return true;
  2243. }
  2244. static bool
  2245. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2246. WASMModule *parent_module, const char *sub_module_name,
  2247. const char *table_name, WASMTableImport *table,
  2248. char *error_buf, uint32 error_buf_size)
  2249. {
  2250. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2251. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2252. declare_max_size = 0;
  2253. #if WASM_ENABLE_MULTI_MODULE != 0
  2254. WASMModule *sub_module = NULL;
  2255. WASMTable *linked_table = NULL;
  2256. #endif
  2257. #if WASM_ENABLE_GC != 0
  2258. WASMRefType ref_type;
  2259. bool need_ref_type_map;
  2260. #endif
  2261. bool is_table64 = false;
  2262. #if WASM_ENABLE_GC == 0
  2263. CHECK_BUF(p, p_end, 1);
  2264. /* 0x70 or 0x6F */
  2265. declare_elem_type = read_uint8(p);
  2266. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2267. #if WASM_ENABLE_REF_TYPES != 0
  2268. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2269. #endif
  2270. ) {
  2271. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2272. return false;
  2273. }
  2274. #else /* else of WASM_ENABLE_GC == 0 */
  2275. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2276. &need_ref_type_map, &ref_type, false, error_buf,
  2277. error_buf_size)) {
  2278. return false;
  2279. }
  2280. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2281. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2282. return false;
  2283. }
  2284. declare_elem_type = ref_type.ref_type;
  2285. if (need_ref_type_map) {
  2286. if (!(table->table_type.elem_ref_type =
  2287. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2288. error_buf, error_buf_size))) {
  2289. return false;
  2290. }
  2291. }
  2292. #if TRACE_WASM_LOADER != 0
  2293. os_printf("import table type: ");
  2294. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2295. os_printf("\n");
  2296. #endif
  2297. #endif /* end of WASM_ENABLE_GC == 0 */
  2298. p_org = p;
  2299. read_leb_uint32(p, p_end, table_flag);
  2300. is_table64 = table_flag & TABLE64_FLAG;
  2301. if (p - p_org > 1) {
  2302. LOG_VERBOSE("integer representation too long(import table)");
  2303. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2304. return false;
  2305. }
  2306. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2307. return false;
  2308. }
  2309. read_leb_uint32(p, p_end, declare_init_size);
  2310. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2311. read_leb_uint32(p, p_end, declare_max_size);
  2312. if (!check_table_max_size(declare_init_size, declare_max_size,
  2313. error_buf, error_buf_size))
  2314. return false;
  2315. }
  2316. adjust_table_max_size(is_table64, declare_init_size,
  2317. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2318. *p_buf = p;
  2319. #if WASM_ENABLE_MULTI_MODULE != 0
  2320. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2321. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2322. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2323. error_buf_size);
  2324. if (sub_module) {
  2325. linked_table = wasm_loader_resolve_table(
  2326. sub_module_name, table_name, declare_init_size,
  2327. declare_max_size, error_buf, error_buf_size);
  2328. if (linked_table) {
  2329. /* reset with linked table limit */
  2330. declare_elem_type = linked_table->table_type.elem_type;
  2331. declare_init_size = linked_table->table_type.init_size;
  2332. declare_max_size = linked_table->table_type.max_size;
  2333. table_flag = linked_table->table_type.flags;
  2334. table->import_table_linked = linked_table;
  2335. table->import_module = sub_module;
  2336. }
  2337. }
  2338. }
  2339. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2340. /* (table (export "table") 10 20 funcref) */
  2341. /* (table (export "table64") 10 20 funcref) */
  2342. /* we need this section working in wamrc */
  2343. if (!strcmp("spectest", sub_module_name)) {
  2344. const uint32 spectest_table_init_size = 10;
  2345. const uint32 spectest_table_max_size = 20;
  2346. if (strcmp("table", table_name)
  2347. #if WASM_ENABLE_MEMORY64 != 0
  2348. && strcmp("table64", table_name)
  2349. #endif
  2350. ) {
  2351. set_error_buf(error_buf, error_buf_size,
  2352. "incompatible import type or unknown import");
  2353. return false;
  2354. }
  2355. if (declare_init_size > spectest_table_init_size
  2356. || declare_max_size < spectest_table_max_size) {
  2357. set_error_buf(error_buf, error_buf_size,
  2358. "incompatible import type");
  2359. return false;
  2360. }
  2361. declare_init_size = spectest_table_init_size;
  2362. declare_max_size = spectest_table_max_size;
  2363. }
  2364. /* now we believe all declaration are ok */
  2365. table->table_type.elem_type = declare_elem_type;
  2366. table->table_type.init_size = declare_init_size;
  2367. table->table_type.flags = table_flag;
  2368. table->table_type.max_size = declare_max_size;
  2369. #if WASM_ENABLE_WAMR_COMPILER != 0
  2370. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2371. parent_module->is_ref_types_used = true;
  2372. #endif
  2373. (void)parent_module;
  2374. return true;
  2375. fail:
  2376. return false;
  2377. }
  2378. static bool
  2379. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2380. uint32 error_buf_size)
  2381. {
  2382. uint32 default_max_size =
  2383. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2384. if (!is_memory64 && init_size > default_max_size) {
  2385. set_error_buf(error_buf, error_buf_size,
  2386. "memory size must be at most 65536 pages (4GiB)");
  2387. return false;
  2388. }
  2389. #if WASM_ENABLE_MEMORY64 != 0
  2390. else if (is_memory64 && init_size > default_max_size) {
  2391. set_error_buf(
  2392. error_buf, error_buf_size,
  2393. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2394. return false;
  2395. }
  2396. #endif
  2397. return true;
  2398. }
  2399. static bool
  2400. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2401. char *error_buf, uint32 error_buf_size)
  2402. {
  2403. uint32 default_max_size =
  2404. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2405. if (max_size < init_size) {
  2406. set_error_buf(error_buf, error_buf_size,
  2407. "size minimum must not be greater than maximum");
  2408. return false;
  2409. }
  2410. if (!is_memory64 && max_size > default_max_size) {
  2411. set_error_buf(error_buf, error_buf_size,
  2412. "memory size must be at most 65536 pages (4GiB)");
  2413. return false;
  2414. }
  2415. #if WASM_ENABLE_MEMORY64 != 0
  2416. else if (is_memory64 && max_size > default_max_size) {
  2417. set_error_buf(
  2418. error_buf, error_buf_size,
  2419. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2420. return false;
  2421. }
  2422. #endif
  2423. return true;
  2424. }
  2425. static bool
  2426. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2427. WASMModule *parent_module, const char *sub_module_name,
  2428. const char *memory_name, WASMMemoryImport *memory,
  2429. char *error_buf, uint32 error_buf_size)
  2430. {
  2431. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2432. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2433. uint32 pool_size = wasm_runtime_memory_pool_size();
  2434. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2435. / DEFAULT_NUM_BYTES_PER_PAGE;
  2436. #else
  2437. uint32 max_page_count;
  2438. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2439. uint32 mem_flag = 0;
  2440. bool is_memory64 = false;
  2441. uint32 declare_init_page_count = 0;
  2442. uint32 declare_max_page_count = 0;
  2443. #if WASM_ENABLE_MULTI_MODULE != 0
  2444. WASMModule *sub_module = NULL;
  2445. WASMMemory *linked_memory = NULL;
  2446. #endif
  2447. p_org = p;
  2448. read_leb_uint32(p, p_end, mem_flag);
  2449. is_memory64 = mem_flag & MEMORY64_FLAG;
  2450. if (p - p_org > 1) {
  2451. LOG_VERBOSE("integer representation too long(import memory)");
  2452. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2453. return false;
  2454. }
  2455. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2456. return false;
  2457. }
  2458. read_leb_uint32(p, p_end, declare_init_page_count);
  2459. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2460. error_buf_size)) {
  2461. return false;
  2462. }
  2463. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2464. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2465. #endif
  2466. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2467. read_leb_uint32(p, p_end, declare_max_page_count);
  2468. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2469. declare_max_page_count, error_buf,
  2470. error_buf_size)) {
  2471. return false;
  2472. }
  2473. if (declare_max_page_count > max_page_count) {
  2474. declare_max_page_count = max_page_count;
  2475. }
  2476. }
  2477. else {
  2478. /* Limit the maximum memory size to max_page_count */
  2479. declare_max_page_count = max_page_count;
  2480. }
  2481. #if WASM_ENABLE_MULTI_MODULE != 0
  2482. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2483. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2484. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2485. error_buf_size);
  2486. if (sub_module) {
  2487. linked_memory = wasm_loader_resolve_memory(
  2488. sub_module_name, memory_name, declare_init_page_count,
  2489. declare_max_page_count, error_buf, error_buf_size);
  2490. if (linked_memory) {
  2491. /**
  2492. * reset with linked memory limit
  2493. */
  2494. memory->import_module = sub_module;
  2495. memory->import_memory_linked = linked_memory;
  2496. declare_init_page_count = linked_memory->init_page_count;
  2497. declare_max_page_count = linked_memory->max_page_count;
  2498. }
  2499. }
  2500. }
  2501. #endif
  2502. /* (memory (export "memory") 1 2) */
  2503. if (!strcmp("spectest", sub_module_name)) {
  2504. uint32 spectest_memory_init_page = 1;
  2505. uint32 spectest_memory_max_page = 2;
  2506. if (strcmp("memory", memory_name)) {
  2507. set_error_buf(error_buf, error_buf_size,
  2508. "incompatible import type or unknown import");
  2509. return false;
  2510. }
  2511. if (declare_init_page_count > spectest_memory_init_page
  2512. || declare_max_page_count < spectest_memory_max_page) {
  2513. set_error_buf(error_buf, error_buf_size,
  2514. "incompatible import type");
  2515. return false;
  2516. }
  2517. declare_init_page_count = spectest_memory_init_page;
  2518. declare_max_page_count = spectest_memory_max_page;
  2519. }
  2520. #if WASM_ENABLE_WASI_TEST != 0
  2521. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2522. else if (!strcmp("foo", sub_module_name)) {
  2523. uint32 spectest_memory_init_page = 1;
  2524. uint32 spectest_memory_max_page = 1;
  2525. if (strcmp("bar", memory_name)) {
  2526. set_error_buf(error_buf, error_buf_size,
  2527. "incompatible import type or unknown import");
  2528. return false;
  2529. }
  2530. if (declare_init_page_count > spectest_memory_init_page
  2531. || declare_max_page_count < spectest_memory_max_page) {
  2532. set_error_buf(error_buf, error_buf_size,
  2533. "incompatible import type");
  2534. return false;
  2535. }
  2536. declare_init_page_count = spectest_memory_init_page;
  2537. declare_max_page_count = spectest_memory_max_page;
  2538. }
  2539. #endif
  2540. /* now we believe all declaration are ok */
  2541. memory->mem_type.flags = mem_flag;
  2542. memory->mem_type.init_page_count = declare_init_page_count;
  2543. memory->mem_type.max_page_count = declare_max_page_count;
  2544. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2545. *p_buf = p;
  2546. (void)parent_module;
  2547. return true;
  2548. fail:
  2549. return false;
  2550. }
  2551. #if WASM_ENABLE_TAGS != 0
  2552. static bool
  2553. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2554. const WASMModule *parent_module, /* this module ! */
  2555. const char *sub_module_name, const char *tag_name,
  2556. WASMTagImport *tag, /* structure to fill */
  2557. char *error_buf, uint32 error_buf_size)
  2558. {
  2559. /* attribute and type of the import statement */
  2560. uint8 declare_tag_attribute;
  2561. uint32 declare_type_index;
  2562. const uint8 *p = *p_buf, *p_end = buf_end;
  2563. #if WASM_ENABLE_MULTI_MODULE != 0
  2564. WASMModule *sub_module = NULL;
  2565. #endif
  2566. /* get the one byte attribute */
  2567. CHECK_BUF(p, p_end, 1);
  2568. declare_tag_attribute = read_uint8(p);
  2569. if (declare_tag_attribute != 0) {
  2570. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2571. goto fail;
  2572. }
  2573. /* get type */
  2574. read_leb_uint32(p, p_end, declare_type_index);
  2575. /* compare against module->types */
  2576. if (declare_type_index >= parent_module->type_count) {
  2577. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2578. goto fail;
  2579. }
  2580. WASMFuncType *declare_tag_type =
  2581. (WASMFuncType *)parent_module->types[declare_type_index];
  2582. /* check, that the type of the declared tag returns void */
  2583. if (declare_tag_type->result_count != 0) {
  2584. set_error_buf(error_buf, error_buf_size,
  2585. "tag type signature does not return void");
  2586. goto fail;
  2587. }
  2588. #if WASM_ENABLE_MULTI_MODULE != 0
  2589. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2590. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2591. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2592. error_buf_size);
  2593. if (sub_module) {
  2594. /* wasm_loader_resolve_tag checks, that the imported tag
  2595. * and the declared tag have the same type
  2596. */
  2597. uint32 linked_tag_index = 0;
  2598. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2599. sub_module_name, tag_name, declare_tag_type,
  2600. &linked_tag_index /* out */, error_buf, error_buf_size);
  2601. if (linked_tag) {
  2602. tag->import_module = sub_module;
  2603. tag->import_tag_linked = linked_tag;
  2604. tag->import_tag_index_linked = linked_tag_index;
  2605. }
  2606. }
  2607. }
  2608. #endif
  2609. /* store to module tag declarations */
  2610. tag->attribute = declare_tag_attribute;
  2611. tag->type = declare_type_index;
  2612. tag->module_name = (char *)sub_module_name;
  2613. tag->field_name = (char *)tag_name;
  2614. tag->tag_type = declare_tag_type;
  2615. *p_buf = p;
  2616. (void)parent_module;
  2617. LOG_VERBOSE("Load tag import success\n");
  2618. return true;
  2619. fail:
  2620. return false;
  2621. }
  2622. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2623. static bool
  2624. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2625. WASMModule *parent_module, char *sub_module_name,
  2626. char *global_name, WASMGlobalImport *global, char *error_buf,
  2627. uint32 error_buf_size)
  2628. {
  2629. const uint8 *p = *p_buf, *p_end = buf_end;
  2630. uint8 declare_type = 0;
  2631. uint8 declare_mutable = 0;
  2632. #if WASM_ENABLE_MULTI_MODULE != 0
  2633. WASMModule *sub_module = NULL;
  2634. WASMGlobal *linked_global = NULL;
  2635. #endif
  2636. #if WASM_ENABLE_GC != 0
  2637. WASMRefType ref_type;
  2638. bool need_ref_type_map;
  2639. #endif
  2640. bool ret = false;
  2641. #if WASM_ENABLE_GC == 0
  2642. CHECK_BUF(p, p_end, 2);
  2643. /* global type */
  2644. declare_type = read_uint8(p);
  2645. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2646. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2647. return false;
  2648. }
  2649. declare_mutable = read_uint8(p);
  2650. #else
  2651. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2652. &need_ref_type_map, &ref_type, false, error_buf,
  2653. error_buf_size)) {
  2654. return false;
  2655. }
  2656. declare_type = ref_type.ref_type;
  2657. if (need_ref_type_map) {
  2658. if (!(global->ref_type =
  2659. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2660. error_buf, error_buf_size))) {
  2661. return false;
  2662. }
  2663. }
  2664. #if TRACE_WASM_LOADER != 0
  2665. os_printf("import global type: ");
  2666. wasm_dump_value_type(declare_type, global->ref_type);
  2667. os_printf("\n");
  2668. #endif
  2669. CHECK_BUF(p, p_end, 1);
  2670. declare_mutable = read_uint8(p);
  2671. #endif /* end of WASM_ENABLE_GC == 0 */
  2672. *p_buf = p;
  2673. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2674. return false;
  2675. }
  2676. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2677. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2678. global);
  2679. if (ret) {
  2680. if (global->type.val_type != declare_type
  2681. || global->type.is_mutable != declare_mutable) {
  2682. set_error_buf(error_buf, error_buf_size,
  2683. "incompatible import type");
  2684. return false;
  2685. }
  2686. global->is_linked = true;
  2687. }
  2688. #endif
  2689. #if WASM_ENABLE_MULTI_MODULE != 0
  2690. if (!global->is_linked
  2691. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2692. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2693. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2694. error_buf_size);
  2695. if (sub_module) {
  2696. /* check sub modules */
  2697. linked_global = wasm_loader_resolve_global(
  2698. sub_module_name, global_name, declare_type, declare_mutable,
  2699. error_buf, error_buf_size);
  2700. if (linked_global) {
  2701. global->import_module = sub_module;
  2702. global->import_global_linked = linked_global;
  2703. global->is_linked = true;
  2704. }
  2705. }
  2706. }
  2707. #endif
  2708. global->module_name = sub_module_name;
  2709. global->field_name = global_name;
  2710. global->type.val_type = declare_type;
  2711. global->type.is_mutable = (declare_mutable == 1);
  2712. #if WASM_ENABLE_WAMR_COMPILER != 0
  2713. if (global->type.val_type == VALUE_TYPE_V128)
  2714. parent_module->is_simd_used = true;
  2715. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2716. parent_module->is_ref_types_used = true;
  2717. #endif
  2718. (void)parent_module;
  2719. (void)ret;
  2720. return true;
  2721. fail:
  2722. return false;
  2723. }
  2724. static bool
  2725. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2726. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2727. {
  2728. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2729. #if WASM_ENABLE_GC != 0
  2730. WASMRefType ref_type;
  2731. bool need_ref_type_map;
  2732. #endif
  2733. bool is_table64 = false;
  2734. #if WASM_ENABLE_GC == 0
  2735. CHECK_BUF(p, p_end, 1);
  2736. /* 0x70 or 0x6F */
  2737. table->table_type.elem_type = read_uint8(p);
  2738. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2739. #if WASM_ENABLE_REF_TYPES != 0
  2740. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2741. #endif
  2742. ) {
  2743. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2744. return false;
  2745. }
  2746. #else /* else of WASM_ENABLE_GC == 0 */
  2747. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2748. &need_ref_type_map, &ref_type, false, error_buf,
  2749. error_buf_size)) {
  2750. return false;
  2751. }
  2752. table->table_type.elem_type = ref_type.ref_type;
  2753. if (need_ref_type_map) {
  2754. if (!(table->table_type.elem_ref_type =
  2755. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2756. error_buf_size))) {
  2757. return false;
  2758. }
  2759. }
  2760. #if TRACE_WASM_LOADER != 0
  2761. os_printf("table type: ");
  2762. wasm_dump_value_type(table->table_type.elem_type,
  2763. table->table_type.elem_ref_type);
  2764. os_printf("\n");
  2765. #endif
  2766. #endif /* end of WASM_ENABLE_GC == 0 */
  2767. p_org = p;
  2768. read_leb_uint32(p, p_end, table->table_type.flags);
  2769. is_table64 = table->table_type.flags & TABLE64_FLAG;
  2770. if (p - p_org > 1) {
  2771. LOG_VERBOSE("integer representation too long(table)");
  2772. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2773. return false;
  2774. }
  2775. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  2776. error_buf_size, false)) {
  2777. return false;
  2778. }
  2779. read_leb_uint32(p, p_end, table->table_type.init_size);
  2780. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  2781. read_leb_uint32(p, p_end, table->table_type.max_size);
  2782. if (!check_table_max_size(table->table_type.init_size,
  2783. table->table_type.max_size, error_buf,
  2784. error_buf_size))
  2785. return false;
  2786. }
  2787. adjust_table_max_size(is_table64, table->table_type.init_size,
  2788. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  2789. &table->table_type.max_size);
  2790. #if WASM_ENABLE_WAMR_COMPILER != 0
  2791. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2792. module->is_ref_types_used = true;
  2793. #endif
  2794. *p_buf = p;
  2795. return true;
  2796. fail:
  2797. return false;
  2798. }
  2799. static bool
  2800. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2801. char *error_buf, uint32 error_buf_size)
  2802. {
  2803. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2804. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2805. uint32 pool_size = wasm_runtime_memory_pool_size();
  2806. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2807. / DEFAULT_NUM_BYTES_PER_PAGE;
  2808. #else
  2809. uint32 max_page_count;
  2810. #endif
  2811. bool is_memory64 = false;
  2812. p_org = p;
  2813. read_leb_uint32(p, p_end, memory->flags);
  2814. is_memory64 = memory->flags & MEMORY64_FLAG;
  2815. if (p - p_org > 1) {
  2816. LOG_VERBOSE("integer representation too long(memory)");
  2817. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2818. return false;
  2819. }
  2820. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2821. false)) {
  2822. return false;
  2823. }
  2824. read_leb_uint32(p, p_end, memory->init_page_count);
  2825. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2826. error_buf_size))
  2827. return false;
  2828. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2829. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2830. #endif
  2831. if (memory->flags & 1) {
  2832. read_leb_uint32(p, p_end, memory->max_page_count);
  2833. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2834. memory->max_page_count, error_buf,
  2835. error_buf_size))
  2836. return false;
  2837. if (memory->max_page_count > max_page_count)
  2838. memory->max_page_count = max_page_count;
  2839. }
  2840. else {
  2841. /* Limit the maximum memory size to max_page_count */
  2842. memory->max_page_count = max_page_count;
  2843. }
  2844. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2845. *p_buf = p;
  2846. return true;
  2847. fail:
  2848. return false;
  2849. }
  2850. static int
  2851. cmp_export_name(const void *a, const void *b)
  2852. {
  2853. return strcmp(*(char **)a, *(char **)b);
  2854. }
  2855. static bool
  2856. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2857. bool is_load_from_file_buf, bool no_resolve,
  2858. char *error_buf, uint32 error_buf_size)
  2859. {
  2860. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2861. uint32 import_count, name_len, type_index, i, u32, flags;
  2862. uint64 total_size;
  2863. WASMImport *import;
  2864. WASMImport *import_functions = NULL, *import_tables = NULL;
  2865. WASMImport *import_memories = NULL, *import_globals = NULL;
  2866. #if WASM_ENABLE_TAGS != 0
  2867. WASMImport *import_tags = NULL;
  2868. #endif
  2869. char *sub_module_name, *field_name;
  2870. uint8 u8, kind, global_type;
  2871. read_leb_uint32(p, p_end, import_count);
  2872. if (import_count) {
  2873. module->import_count = import_count;
  2874. total_size = sizeof(WASMImport) * (uint64)import_count;
  2875. if (!(module->imports =
  2876. loader_malloc(total_size, error_buf, error_buf_size))) {
  2877. return false;
  2878. }
  2879. p_old = p;
  2880. /* Scan firstly to get import count of each type */
  2881. for (i = 0; i < import_count; i++) {
  2882. /* module name */
  2883. read_leb_uint32(p, p_end, name_len);
  2884. CHECK_BUF(p, p_end, name_len);
  2885. p += name_len;
  2886. /* field name */
  2887. read_leb_uint32(p, p_end, name_len);
  2888. CHECK_BUF(p, p_end, name_len);
  2889. p += name_len;
  2890. CHECK_BUF(p, p_end, 1);
  2891. /* 0x00/0x01/0x02/0x03/0x04 */
  2892. kind = read_uint8(p);
  2893. switch (kind) {
  2894. case IMPORT_KIND_FUNC: /* import function */
  2895. read_leb_uint32(p, p_end, type_index);
  2896. module->import_function_count++;
  2897. break;
  2898. case IMPORT_KIND_TABLE: /* import table */
  2899. CHECK_BUF(p, p_end, 1);
  2900. /* 0x70 */
  2901. u8 = read_uint8(p);
  2902. read_leb_uint32(p, p_end, flags);
  2903. read_leb_uint32(p, p_end, u32);
  2904. if (flags & 1)
  2905. read_leb_uint32(p, p_end, u32);
  2906. module->import_table_count++;
  2907. if (module->import_table_count > 1) {
  2908. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2909. set_error_buf(error_buf, error_buf_size,
  2910. "multiple tables");
  2911. return false;
  2912. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2913. module->is_ref_types_used = true;
  2914. #endif
  2915. }
  2916. break;
  2917. case IMPORT_KIND_MEMORY: /* import memory */
  2918. read_leb_uint32(p, p_end, flags);
  2919. read_leb_uint32(p, p_end, u32);
  2920. if (flags & 1)
  2921. read_leb_uint32(p, p_end, u32);
  2922. module->import_memory_count++;
  2923. #if WASM_ENABLE_MULTI_MEMORY == 0
  2924. if (module->import_memory_count > 1) {
  2925. set_error_buf(error_buf, error_buf_size,
  2926. "multiple memories");
  2927. return false;
  2928. }
  2929. #endif
  2930. break;
  2931. #if WASM_ENABLE_TAGS != 0
  2932. case IMPORT_KIND_TAG: /* import tags */
  2933. /* it only counts the number of tags to import */
  2934. module->import_tag_count++;
  2935. CHECK_BUF(p, p_end, 1);
  2936. u8 = read_uint8(p);
  2937. read_leb_uint32(p, p_end, type_index);
  2938. break;
  2939. #endif
  2940. case IMPORT_KIND_GLOBAL: /* import global */
  2941. #if WASM_ENABLE_GC != 0
  2942. /* valtype */
  2943. CHECK_BUF(p, p_end, 1);
  2944. global_type = read_uint8(p);
  2945. if (wasm_is_type_multi_byte_type(global_type)) {
  2946. int32 heap_type;
  2947. read_leb_int32(p, p_end, heap_type);
  2948. (void)heap_type;
  2949. }
  2950. /* mutability */
  2951. CHECK_BUF(p, p_end, 1);
  2952. p += 1;
  2953. #else
  2954. CHECK_BUF(p, p_end, 2);
  2955. p += 2;
  2956. #endif
  2957. (void)global_type;
  2958. module->import_global_count++;
  2959. break;
  2960. default:
  2961. set_error_buf(error_buf, error_buf_size,
  2962. "invalid import kind");
  2963. return false;
  2964. }
  2965. }
  2966. if (module->import_function_count)
  2967. import_functions = module->import_functions = module->imports;
  2968. if (module->import_table_count)
  2969. import_tables = module->import_tables =
  2970. module->imports + module->import_function_count;
  2971. if (module->import_memory_count)
  2972. import_memories = module->import_memories =
  2973. module->imports + module->import_function_count
  2974. + module->import_table_count;
  2975. #if WASM_ENABLE_TAGS != 0
  2976. if (module->import_tag_count)
  2977. import_tags = module->import_tags =
  2978. module->imports + module->import_function_count
  2979. + module->import_table_count + module->import_memory_count;
  2980. if (module->import_global_count)
  2981. import_globals = module->import_globals =
  2982. module->imports + module->import_function_count
  2983. + module->import_table_count + module->import_memory_count
  2984. + module->import_tag_count;
  2985. #else
  2986. if (module->import_global_count)
  2987. import_globals = module->import_globals =
  2988. module->imports + module->import_function_count
  2989. + module->import_table_count + module->import_memory_count;
  2990. #endif
  2991. p = p_old;
  2992. /* Scan again to resolve the data */
  2993. for (i = 0; i < import_count; i++) {
  2994. /* load module name */
  2995. read_leb_uint32(p, p_end, name_len);
  2996. CHECK_BUF(p, p_end, name_len);
  2997. if (!(sub_module_name = wasm_const_str_list_insert(
  2998. p, name_len, module, is_load_from_file_buf, error_buf,
  2999. error_buf_size))) {
  3000. return false;
  3001. }
  3002. p += name_len;
  3003. /* load field name */
  3004. read_leb_uint32(p, p_end, name_len);
  3005. CHECK_BUF(p, p_end, name_len);
  3006. if (!(field_name = wasm_const_str_list_insert(
  3007. p, name_len, module, is_load_from_file_buf, error_buf,
  3008. error_buf_size))) {
  3009. return false;
  3010. }
  3011. p += name_len;
  3012. CHECK_BUF(p, p_end, 1);
  3013. /* 0x00/0x01/0x02/0x03/0x4 */
  3014. kind = read_uint8(p);
  3015. switch (kind) {
  3016. case IMPORT_KIND_FUNC: /* import function */
  3017. bh_assert(import_functions);
  3018. import = import_functions++;
  3019. if (!load_function_import(&p, p_end, module,
  3020. sub_module_name, field_name,
  3021. &import->u.function, no_resolve,
  3022. error_buf, error_buf_size)) {
  3023. return false;
  3024. }
  3025. break;
  3026. case IMPORT_KIND_TABLE: /* import table */
  3027. bh_assert(import_tables);
  3028. import = import_tables++;
  3029. if (!load_table_import(&p, p_end, module, sub_module_name,
  3030. field_name, &import->u.table,
  3031. error_buf, error_buf_size)) {
  3032. LOG_DEBUG("can not import such a table (%s,%s)",
  3033. sub_module_name, field_name);
  3034. return false;
  3035. }
  3036. break;
  3037. case IMPORT_KIND_MEMORY: /* import memory */
  3038. bh_assert(import_memories);
  3039. import = import_memories++;
  3040. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3041. field_name, &import->u.memory,
  3042. error_buf, error_buf_size)) {
  3043. return false;
  3044. }
  3045. break;
  3046. #if WASM_ENABLE_TAGS != 0
  3047. case IMPORT_KIND_TAG:
  3048. bh_assert(import_tags);
  3049. import = import_tags++;
  3050. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3051. field_name, &import->u.tag, error_buf,
  3052. error_buf_size)) {
  3053. return false;
  3054. }
  3055. break;
  3056. #endif
  3057. case IMPORT_KIND_GLOBAL: /* import global */
  3058. bh_assert(import_globals);
  3059. import = import_globals++;
  3060. if (!load_global_import(&p, p_end, module, sub_module_name,
  3061. field_name, &import->u.global,
  3062. error_buf, error_buf_size)) {
  3063. return false;
  3064. }
  3065. break;
  3066. default:
  3067. set_error_buf(error_buf, error_buf_size,
  3068. "invalid import kind");
  3069. return false;
  3070. }
  3071. import->kind = kind;
  3072. import->u.names.module_name = sub_module_name;
  3073. import->u.names.field_name = field_name;
  3074. }
  3075. #if WASM_ENABLE_LIBC_WASI != 0
  3076. import = module->import_functions;
  3077. for (i = 0; i < module->import_function_count; i++, import++) {
  3078. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3079. || !strcmp(import->u.names.module_name,
  3080. "wasi_snapshot_preview1")) {
  3081. module->import_wasi_api = true;
  3082. break;
  3083. }
  3084. }
  3085. #endif
  3086. }
  3087. if (p != p_end) {
  3088. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3089. return false;
  3090. }
  3091. LOG_VERBOSE("Load import section success.\n");
  3092. (void)u8;
  3093. (void)u32;
  3094. (void)type_index;
  3095. return true;
  3096. fail:
  3097. return false;
  3098. }
  3099. static bool
  3100. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3101. uint32 error_buf_size)
  3102. {
  3103. WASMFuncType *param_type = func->func_type;
  3104. uint32 param_count = param_type->param_count;
  3105. uint8 *param_types = param_type->types;
  3106. uint32 local_count = func->local_count;
  3107. uint8 *local_types = func->local_types;
  3108. uint32 i, local_offset = 0;
  3109. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3110. /*
  3111. * Only allocate memory when total_size is not 0,
  3112. * or the return value of malloc(0) might be NULL on some platforms,
  3113. * which causes wasm loader return false.
  3114. */
  3115. if (total_size > 0
  3116. && !(func->local_offsets =
  3117. loader_malloc(total_size, error_buf, error_buf_size))) {
  3118. return false;
  3119. }
  3120. for (i = 0; i < param_count; i++) {
  3121. func->local_offsets[i] = (uint16)local_offset;
  3122. local_offset += wasm_value_type_cell_num(param_types[i]);
  3123. }
  3124. for (i = 0; i < local_count; i++) {
  3125. func->local_offsets[param_count + i] = (uint16)local_offset;
  3126. local_offset += wasm_value_type_cell_num(local_types[i]);
  3127. }
  3128. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3129. return true;
  3130. }
  3131. static bool
  3132. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3133. const uint8 *buf_code, const uint8 *buf_code_end,
  3134. WASMModule *module, char *error_buf,
  3135. uint32 error_buf_size)
  3136. {
  3137. const uint8 *p = buf, *p_end = buf_end;
  3138. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3139. uint32 func_count;
  3140. uint64 total_size;
  3141. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3142. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3143. uint8 type;
  3144. WASMFunction *func;
  3145. #if WASM_ENABLE_GC != 0
  3146. bool need_ref_type_map;
  3147. WASMRefType ref_type;
  3148. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3149. #endif
  3150. read_leb_uint32(p, p_end, func_count);
  3151. if (buf_code)
  3152. read_leb_uint32(p_code, buf_code_end, code_count);
  3153. if (func_count != code_count) {
  3154. set_error_buf(error_buf, error_buf_size,
  3155. "function and code section have inconsistent lengths or "
  3156. "unexpected end");
  3157. return false;
  3158. }
  3159. if (is_indices_overflow(module->import_function_count, func_count,
  3160. error_buf, error_buf_size))
  3161. return false;
  3162. if (func_count) {
  3163. module->function_count = func_count;
  3164. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3165. if (!(module->functions =
  3166. loader_malloc(total_size, error_buf, error_buf_size))) {
  3167. return false;
  3168. }
  3169. for (i = 0; i < func_count; i++) {
  3170. /* Resolve function type */
  3171. read_leb_uint32(p, p_end, type_index);
  3172. if (type_index >= module->type_count) {
  3173. set_error_buf(error_buf, error_buf_size, "unknown type");
  3174. return false;
  3175. }
  3176. #if WASM_ENABLE_GC != 0
  3177. type_index_org = type_index;
  3178. #endif
  3179. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3180. && WASM_ENABLE_GC == 0
  3181. type_index = wasm_get_smallest_type_idx(
  3182. module->types, module->type_count, type_index);
  3183. #endif
  3184. read_leb_uint32(p_code, buf_code_end, code_size);
  3185. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3186. set_error_buf(error_buf, error_buf_size,
  3187. "invalid function code size");
  3188. return false;
  3189. }
  3190. /* Resolve local set count */
  3191. p_code_end = p_code + code_size;
  3192. local_count = 0;
  3193. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3194. p_code_save = p_code;
  3195. #if WASM_ENABLE_GC != 0
  3196. ref_type_map_count = 0;
  3197. #endif
  3198. /* Calculate total local count */
  3199. for (j = 0; j < local_set_count; j++) {
  3200. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3201. if (sub_local_count > UINT32_MAX - local_count) {
  3202. set_error_buf(error_buf, error_buf_size, "too many locals");
  3203. return false;
  3204. }
  3205. #if WASM_ENABLE_GC == 0
  3206. CHECK_BUF(p_code, buf_code_end, 1);
  3207. /* 0x7F/0x7E/0x7D/0x7C */
  3208. type = read_uint8(p_code);
  3209. local_count += sub_local_count;
  3210. #if WASM_ENABLE_WAMR_COMPILER != 0
  3211. /* If any value's type is v128, mark the module as SIMD used */
  3212. if (type == VALUE_TYPE_V128)
  3213. module->is_simd_used = true;
  3214. #endif
  3215. #else
  3216. if (!resolve_value_type(&p_code, buf_code_end, module,
  3217. module->type_count, &need_ref_type_map,
  3218. &ref_type, false, error_buf,
  3219. error_buf_size)) {
  3220. return false;
  3221. }
  3222. local_count += sub_local_count;
  3223. if (need_ref_type_map)
  3224. ref_type_map_count += sub_local_count;
  3225. #endif
  3226. }
  3227. /* Code size in code entry can't be smaller than size of vec(locals)
  3228. * + expr(at least 1 for opcode end). And expressions are encoded by
  3229. * their instruction sequence terminated with an explicit 0x0B
  3230. * opcode for end. */
  3231. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3232. set_error_buf(
  3233. error_buf, error_buf_size,
  3234. "section size mismatch: function body END opcode expected");
  3235. return false;
  3236. }
  3237. /* Alloc memory, layout: function structure + local types */
  3238. code_size = (uint32)(p_code_end - p_code);
  3239. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3240. if (!(func = module->functions[i] =
  3241. loader_malloc(total_size, error_buf, error_buf_size))) {
  3242. return false;
  3243. }
  3244. #if WASM_ENABLE_GC != 0
  3245. if (ref_type_map_count > 0) {
  3246. total_size =
  3247. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3248. if (!(func->local_ref_type_maps = loader_malloc(
  3249. total_size, error_buf, error_buf_size))) {
  3250. return false;
  3251. }
  3252. func->local_ref_type_map_count = ref_type_map_count;
  3253. }
  3254. #endif
  3255. /* Set function type, local count, code size and code body */
  3256. func->func_type = (WASMFuncType *)module->types[type_index];
  3257. func->local_count = local_count;
  3258. if (local_count > 0)
  3259. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3260. func->code_size = code_size;
  3261. /*
  3262. * we shall make a copy of code body [p_code, p_code + code_size]
  3263. * when we are worrying about inappropriate releasing behaviour.
  3264. * all code bodies are actually in a buffer which user allocates in
  3265. * his embedding environment and we don't have power on them.
  3266. * it will be like:
  3267. * code_body_cp = malloc(code_size);
  3268. * memcpy(code_body_cp, p_code, code_size);
  3269. * func->code = code_body_cp;
  3270. */
  3271. func->code = (uint8 *)p_code;
  3272. #if WASM_ENABLE_GC != 0
  3273. func->type_idx = type_index_org;
  3274. #endif
  3275. #if WASM_ENABLE_GC != 0
  3276. t = 0;
  3277. #endif
  3278. /* Load each local type */
  3279. p_code = p_code_save;
  3280. local_type_index = 0;
  3281. for (j = 0; j < local_set_count; j++) {
  3282. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3283. /* Note: sub_local_count is allowed to be 0 */
  3284. if (local_type_index > UINT32_MAX - sub_local_count
  3285. || local_type_index + sub_local_count > local_count) {
  3286. set_error_buf(error_buf, error_buf_size,
  3287. "invalid local count");
  3288. return false;
  3289. }
  3290. #if WASM_ENABLE_GC == 0
  3291. CHECK_BUF(p_code, buf_code_end, 1);
  3292. /* 0x7F/0x7E/0x7D/0x7C */
  3293. type = read_uint8(p_code);
  3294. if (!is_valid_value_type_for_interpreter(type)) {
  3295. if (type == VALUE_TYPE_V128)
  3296. set_error_buf(error_buf, error_buf_size,
  3297. "v128 value type requires simd feature");
  3298. else if (type == VALUE_TYPE_FUNCREF
  3299. || type == VALUE_TYPE_EXTERNREF)
  3300. set_error_buf(error_buf, error_buf_size,
  3301. "ref value type requires "
  3302. "reference types feature");
  3303. else
  3304. set_error_buf_v(error_buf, error_buf_size,
  3305. "invalid local type 0x%02X", type);
  3306. return false;
  3307. }
  3308. #else
  3309. if (!resolve_value_type(&p_code, buf_code_end, module,
  3310. module->type_count, &need_ref_type_map,
  3311. &ref_type, false, error_buf,
  3312. error_buf_size)) {
  3313. return false;
  3314. }
  3315. if (need_ref_type_map) {
  3316. WASMRefType *ref_type_tmp;
  3317. if (!(ref_type_tmp = reftype_set_insert(
  3318. module->ref_type_set, &ref_type, error_buf,
  3319. error_buf_size))) {
  3320. return false;
  3321. }
  3322. for (k = 0; k < sub_local_count; k++) {
  3323. func->local_ref_type_maps[t + k].ref_type =
  3324. ref_type_tmp;
  3325. func->local_ref_type_maps[t + k].index =
  3326. local_type_index + k;
  3327. }
  3328. t += sub_local_count;
  3329. }
  3330. type = ref_type.ref_type;
  3331. #endif
  3332. for (k = 0; k < sub_local_count; k++) {
  3333. func->local_types[local_type_index++] = type;
  3334. }
  3335. #if WASM_ENABLE_WAMR_COMPILER != 0
  3336. if (type == VALUE_TYPE_V128)
  3337. module->is_simd_used = true;
  3338. else if (type == VALUE_TYPE_FUNCREF
  3339. || type == VALUE_TYPE_EXTERNREF)
  3340. module->is_ref_types_used = true;
  3341. #endif
  3342. }
  3343. bh_assert(local_type_index == func->local_count);
  3344. #if WASM_ENABLE_GC != 0
  3345. bh_assert(t == func->local_ref_type_map_count);
  3346. #if TRACE_WASM_LOADER != 0
  3347. os_printf("func %u, local types: [", i);
  3348. k = 0;
  3349. for (j = 0; j < func->local_count; j++) {
  3350. WASMRefType *ref_type_tmp = NULL;
  3351. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3352. bh_assert(j == func->local_ref_type_maps[k].index);
  3353. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3354. }
  3355. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3356. if (j < func->local_count - 1)
  3357. os_printf(" ");
  3358. }
  3359. os_printf("]\n");
  3360. #endif
  3361. #endif
  3362. func->param_cell_num = func->func_type->param_cell_num;
  3363. func->ret_cell_num = func->func_type->ret_cell_num;
  3364. local_cell_num =
  3365. wasm_get_cell_num(func->local_types, func->local_count);
  3366. if (local_cell_num > UINT16_MAX) {
  3367. set_error_buf(error_buf, error_buf_size,
  3368. "local count too large");
  3369. return false;
  3370. }
  3371. func->local_cell_num = (uint16)local_cell_num;
  3372. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3373. return false;
  3374. p_code = p_code_end;
  3375. }
  3376. }
  3377. if (p != p_end) {
  3378. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3379. return false;
  3380. }
  3381. LOG_VERBOSE("Load function section success.\n");
  3382. return true;
  3383. fail:
  3384. return false;
  3385. }
  3386. static bool
  3387. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3388. char *error_buf, uint32 error_buf_size)
  3389. {
  3390. const uint8 *p = buf, *p_end = buf_end;
  3391. uint32 table_count, i;
  3392. uint64 total_size;
  3393. WASMTable *table;
  3394. read_leb_uint32(p, p_end, table_count);
  3395. if (module->import_table_count + table_count > 1) {
  3396. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3397. /* a total of one table is allowed */
  3398. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3399. return false;
  3400. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3401. module->is_ref_types_used = true;
  3402. #endif
  3403. }
  3404. if (table_count) {
  3405. module->table_count = table_count;
  3406. total_size = sizeof(WASMTable) * (uint64)table_count;
  3407. if (!(module->tables =
  3408. loader_malloc(total_size, error_buf, error_buf_size))) {
  3409. return false;
  3410. }
  3411. /* load each table */
  3412. table = module->tables;
  3413. for (i = 0; i < table_count; i++, table++) {
  3414. #if WASM_ENABLE_GC != 0
  3415. uint8 flag;
  3416. bool has_init = false;
  3417. CHECK_BUF(buf, buf_end, 1);
  3418. flag = read_uint8(p);
  3419. if (flag == TABLE_INIT_EXPR_FLAG) {
  3420. CHECK_BUF(buf, buf_end, 1);
  3421. flag = read_uint8(p);
  3422. if (flag != 0x00) {
  3423. set_error_buf(error_buf, error_buf_size,
  3424. "invalid leading bytes for table");
  3425. return false;
  3426. }
  3427. has_init = true;
  3428. }
  3429. else {
  3430. p--;
  3431. }
  3432. #endif /* end of WASM_ENABLE_GC != 0 */
  3433. if (!load_table(&p, p_end, module, table, error_buf,
  3434. error_buf_size))
  3435. return false;
  3436. #if WASM_ENABLE_GC != 0
  3437. if (has_init) {
  3438. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3439. table->table_type.elem_type,
  3440. table->table_type.elem_ref_type, error_buf,
  3441. error_buf_size))
  3442. return false;
  3443. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3444. && table->init_expr.init_expr_type
  3445. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3446. set_error_buf(
  3447. error_buf, error_buf_size,
  3448. "unsupported initializer expression for table");
  3449. return false;
  3450. }
  3451. }
  3452. else {
  3453. if (wasm_is_reftype_htref_non_nullable(
  3454. table->table_type.elem_type)) {
  3455. set_error_buf(
  3456. error_buf, error_buf_size,
  3457. "type mismatch: non-nullable table without init expr");
  3458. return false;
  3459. }
  3460. }
  3461. #endif /* end of WASM_ENABLE_GC != 0 */
  3462. #if WASM_ENABLE_WAMR_COMPILER != 0
  3463. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3464. module->is_ref_types_used = true;
  3465. #endif
  3466. }
  3467. }
  3468. if (p != p_end) {
  3469. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3470. return false;
  3471. }
  3472. LOG_VERBOSE("Load table section success.\n");
  3473. return true;
  3474. fail:
  3475. return false;
  3476. }
  3477. static bool
  3478. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3479. char *error_buf, uint32 error_buf_size)
  3480. {
  3481. const uint8 *p = buf, *p_end = buf_end;
  3482. uint32 memory_count, i;
  3483. uint64 total_size;
  3484. WASMMemory *memory;
  3485. read_leb_uint32(p, p_end, memory_count);
  3486. #if WASM_ENABLE_MULTI_MEMORY == 0
  3487. /* a total of one memory is allowed */
  3488. if (module->import_memory_count + memory_count > 1) {
  3489. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3490. return false;
  3491. }
  3492. #endif
  3493. if (memory_count) {
  3494. module->memory_count = memory_count;
  3495. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3496. if (!(module->memories =
  3497. loader_malloc(total_size, error_buf, error_buf_size))) {
  3498. return false;
  3499. }
  3500. /* load each memory */
  3501. memory = module->memories;
  3502. for (i = 0; i < memory_count; i++, memory++)
  3503. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3504. return false;
  3505. }
  3506. if (p != p_end) {
  3507. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3508. return false;
  3509. }
  3510. LOG_VERBOSE("Load memory section success.\n");
  3511. return true;
  3512. fail:
  3513. return false;
  3514. }
  3515. static bool
  3516. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3517. char *error_buf, uint32 error_buf_size)
  3518. {
  3519. const uint8 *p = buf, *p_end = buf_end;
  3520. uint32 global_count, i;
  3521. uint64 total_size;
  3522. WASMGlobal *global;
  3523. uint8 mutable;
  3524. #if WASM_ENABLE_GC != 0
  3525. bool need_ref_type_map;
  3526. WASMRefType ref_type;
  3527. #endif
  3528. read_leb_uint32(p, p_end, global_count);
  3529. if (is_indices_overflow(module->import_global_count, global_count,
  3530. error_buf, error_buf_size))
  3531. return false;
  3532. module->global_count = 0;
  3533. if (global_count) {
  3534. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3535. if (!(module->globals =
  3536. loader_malloc(total_size, error_buf, error_buf_size))) {
  3537. return false;
  3538. }
  3539. global = module->globals;
  3540. for (i = 0; i < global_count; i++, global++) {
  3541. #if WASM_ENABLE_GC == 0
  3542. CHECK_BUF(p, p_end, 2);
  3543. /* global type */
  3544. global->type.val_type = read_uint8(p);
  3545. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3546. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3547. return false;
  3548. }
  3549. mutable = read_uint8(p);
  3550. #else
  3551. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3552. &need_ref_type_map, &ref_type, false,
  3553. error_buf, error_buf_size)) {
  3554. return false;
  3555. }
  3556. global->type.val_type = ref_type.ref_type;
  3557. CHECK_BUF(p, p_end, 1);
  3558. mutable = read_uint8(p);
  3559. #endif /* end of WASM_ENABLE_GC */
  3560. #if WASM_ENABLE_WAMR_COMPILER != 0
  3561. if (global->type.val_type == VALUE_TYPE_V128)
  3562. module->is_simd_used = true;
  3563. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3564. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3565. module->is_ref_types_used = true;
  3566. #endif
  3567. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3568. return false;
  3569. }
  3570. global->type.is_mutable = mutable ? true : false;
  3571. /* initialize expression */
  3572. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3573. global->type.val_type,
  3574. #if WASM_ENABLE_GC == 0
  3575. NULL,
  3576. #else
  3577. &ref_type,
  3578. #endif
  3579. error_buf, error_buf_size))
  3580. return false;
  3581. #if WASM_ENABLE_GC != 0
  3582. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3583. uint8 global_type;
  3584. WASMRefType *global_ref_type;
  3585. uint32 global_idx = global->init_expr.u.global_index;
  3586. if (global->init_expr.u.global_index
  3587. >= module->import_global_count + i) {
  3588. set_error_buf(error_buf, error_buf_size, "unknown global");
  3589. return false;
  3590. }
  3591. if (global_idx < module->import_global_count) {
  3592. global_type = module->import_globals[global_idx]
  3593. .u.global.type.val_type;
  3594. global_ref_type =
  3595. module->import_globals[global_idx].u.global.ref_type;
  3596. }
  3597. else {
  3598. global_type =
  3599. module
  3600. ->globals[global_idx - module->import_global_count]
  3601. .type.val_type;
  3602. global_ref_type =
  3603. module
  3604. ->globals[global_idx - module->import_global_count]
  3605. .ref_type;
  3606. }
  3607. if (!wasm_reftype_is_subtype_of(
  3608. global_type, global_ref_type, global->type.val_type,
  3609. global->ref_type, module->types, module->type_count)) {
  3610. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3611. return false;
  3612. }
  3613. }
  3614. if (need_ref_type_map) {
  3615. if (!(global->ref_type =
  3616. reftype_set_insert(module->ref_type_set, &ref_type,
  3617. error_buf, error_buf_size))) {
  3618. return false;
  3619. }
  3620. }
  3621. #if TRACE_WASM_LOADER != 0
  3622. os_printf("global type: ");
  3623. wasm_dump_value_type(global->type, global->ref_type);
  3624. os_printf("\n");
  3625. #endif
  3626. #endif
  3627. module->global_count++;
  3628. }
  3629. bh_assert(module->global_count == global_count);
  3630. }
  3631. if (p != p_end) {
  3632. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3633. return false;
  3634. }
  3635. LOG_VERBOSE("Load global section success.\n");
  3636. return true;
  3637. fail:
  3638. return false;
  3639. }
  3640. static bool
  3641. check_duplicate_exports(WASMModule *module, char *error_buf,
  3642. uint32 error_buf_size)
  3643. {
  3644. uint32 i;
  3645. bool result = false;
  3646. char *names_buf[32], **names = names_buf;
  3647. if (module->export_count > 32) {
  3648. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3649. error_buf_size);
  3650. if (!names) {
  3651. return result;
  3652. }
  3653. }
  3654. for (i = 0; i < module->export_count; i++) {
  3655. names[i] = module->exports[i].name;
  3656. }
  3657. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3658. for (i = 1; i < module->export_count; i++) {
  3659. if (!strcmp(names[i], names[i - 1])) {
  3660. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3661. goto cleanup;
  3662. }
  3663. }
  3664. result = true;
  3665. cleanup:
  3666. if (module->export_count > 32) {
  3667. wasm_runtime_free(names);
  3668. }
  3669. return result;
  3670. }
  3671. static bool
  3672. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3673. bool is_load_from_file_buf, char *error_buf,
  3674. uint32 error_buf_size)
  3675. {
  3676. const uint8 *p = buf, *p_end = buf_end;
  3677. uint32 export_count, i, index;
  3678. uint64 total_size;
  3679. uint32 str_len;
  3680. WASMExport *export;
  3681. read_leb_uint32(p, p_end, export_count);
  3682. if (export_count) {
  3683. module->export_count = export_count;
  3684. total_size = sizeof(WASMExport) * (uint64)export_count;
  3685. if (!(module->exports =
  3686. loader_malloc(total_size, error_buf, error_buf_size))) {
  3687. return false;
  3688. }
  3689. export = module->exports;
  3690. for (i = 0; i < export_count; i++, export ++) {
  3691. #if WASM_ENABLE_THREAD_MGR == 0
  3692. if (p == p_end) {
  3693. /* export section with inconsistent count:
  3694. n export declared, but less than n given */
  3695. set_error_buf(error_buf, error_buf_size,
  3696. "length out of bounds");
  3697. return false;
  3698. }
  3699. #endif
  3700. read_leb_uint32(p, p_end, str_len);
  3701. CHECK_BUF(p, p_end, str_len);
  3702. if (!(export->name = wasm_const_str_list_insert(
  3703. p, str_len, module, is_load_from_file_buf, error_buf,
  3704. error_buf_size))) {
  3705. return false;
  3706. }
  3707. p += str_len;
  3708. CHECK_BUF(p, p_end, 1);
  3709. export->kind = read_uint8(p);
  3710. read_leb_uint32(p, p_end, index);
  3711. export->index = index;
  3712. switch (export->kind) {
  3713. /* function index */
  3714. case EXPORT_KIND_FUNC:
  3715. if (index >= module->function_count
  3716. + module->import_function_count) {
  3717. set_error_buf(error_buf, error_buf_size,
  3718. "unknown function");
  3719. return false;
  3720. }
  3721. #if WASM_ENABLE_SIMD != 0
  3722. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3723. /* TODO: check func type, if it has v128 param or result,
  3724. report error */
  3725. #endif
  3726. #endif
  3727. break;
  3728. /* table index */
  3729. case EXPORT_KIND_TABLE:
  3730. if (index
  3731. >= module->table_count + module->import_table_count) {
  3732. set_error_buf(error_buf, error_buf_size,
  3733. "unknown table");
  3734. return false;
  3735. }
  3736. break;
  3737. /* memory index */
  3738. case EXPORT_KIND_MEMORY:
  3739. if (index
  3740. >= module->memory_count + module->import_memory_count) {
  3741. set_error_buf(error_buf, error_buf_size,
  3742. "unknown memory");
  3743. return false;
  3744. }
  3745. break;
  3746. #if WASM_ENABLE_TAGS != 0
  3747. /* export tag */
  3748. case EXPORT_KIND_TAG:
  3749. if (index >= module->tag_count + module->import_tag_count) {
  3750. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3751. return false;
  3752. }
  3753. break;
  3754. #endif
  3755. /* global index */
  3756. case EXPORT_KIND_GLOBAL:
  3757. if (index
  3758. >= module->global_count + module->import_global_count) {
  3759. set_error_buf(error_buf, error_buf_size,
  3760. "unknown global");
  3761. return false;
  3762. }
  3763. break;
  3764. default:
  3765. set_error_buf(error_buf, error_buf_size,
  3766. "invalid export kind");
  3767. return false;
  3768. }
  3769. }
  3770. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  3771. return false;
  3772. }
  3773. }
  3774. if (p != p_end) {
  3775. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3776. return false;
  3777. }
  3778. LOG_VERBOSE("Load export section success.\n");
  3779. return true;
  3780. fail:
  3781. return false;
  3782. }
  3783. static bool
  3784. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3785. uint32 error_buf_size)
  3786. {
  3787. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3788. if (table_index != 0) {
  3789. set_error_buf(
  3790. error_buf, error_buf_size,
  3791. "zero byte expected. The module uses reference types feature "
  3792. "which is disabled in the runtime.");
  3793. return false;
  3794. }
  3795. #endif
  3796. if (table_index >= module->import_table_count + module->table_count) {
  3797. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3798. table_index);
  3799. return false;
  3800. }
  3801. return true;
  3802. }
  3803. static bool
  3804. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3805. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3806. {
  3807. const uint8 *p = *p_buf, *p_end = buf_end;
  3808. uint32 table_index;
  3809. read_leb_uint32(p, p_end, table_index);
  3810. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3811. return false;
  3812. }
  3813. *p_table_index = table_index;
  3814. *p_buf = p;
  3815. return true;
  3816. fail:
  3817. return false;
  3818. }
  3819. /* Element segments must match element type of table */
  3820. static bool
  3821. check_table_elem_type(WASMModule *module, uint32 table_index,
  3822. uint32 type_from_elem_seg, char *error_buf,
  3823. uint32 error_buf_size)
  3824. {
  3825. uint32 table_declared_elem_type;
  3826. if (table_index < module->import_table_count)
  3827. table_declared_elem_type =
  3828. module->import_tables[table_index].u.table.table_type.elem_type;
  3829. else
  3830. table_declared_elem_type =
  3831. module->tables[table_index - module->import_table_count]
  3832. .table_type.elem_type;
  3833. if (table_declared_elem_type == type_from_elem_seg)
  3834. return true;
  3835. #if WASM_ENABLE_GC != 0
  3836. /*
  3837. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3838. */
  3839. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3840. return true;
  3841. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3842. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3843. return true;
  3844. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3845. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3846. return true;
  3847. #endif
  3848. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3849. return false;
  3850. }
  3851. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3852. static bool
  3853. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3854. uint32 *p_elem_type,
  3855. #if WASM_ENABLE_GC != 0
  3856. WASMRefType **p_elem_ref_type,
  3857. #endif
  3858. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3859. {
  3860. const uint8 *p = *p_buf, *p_end = buf_end;
  3861. uint8 elem_type;
  3862. #if WASM_ENABLE_GC != 0
  3863. WASMRefType elem_ref_type;
  3864. bool need_ref_type_map;
  3865. #endif
  3866. CHECK_BUF(p, p_end, 1);
  3867. elem_type = read_uint8(p);
  3868. if (elemkind_zero) {
  3869. if (elem_type != 0) {
  3870. set_error_buf(error_buf, error_buf_size,
  3871. "invalid reference type or unknown type");
  3872. return false;
  3873. }
  3874. else {
  3875. *p_elem_type = VALUE_TYPE_FUNCREF;
  3876. *p_buf = p;
  3877. return true;
  3878. }
  3879. }
  3880. #if WASM_ENABLE_GC == 0
  3881. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3882. set_error_buf(error_buf, error_buf_size,
  3883. "invalid reference type or unknown type");
  3884. return false;
  3885. }
  3886. *p_elem_type = elem_type;
  3887. #else
  3888. p--;
  3889. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3890. module->type_count, &need_ref_type_map,
  3891. &elem_ref_type, false, error_buf, error_buf_size)) {
  3892. return false;
  3893. }
  3894. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3895. set_error_buf(error_buf, error_buf_size,
  3896. "invalid reference type or unknown type");
  3897. return false;
  3898. }
  3899. *p_elem_type = elem_ref_type.ref_type;
  3900. if (need_ref_type_map) {
  3901. if (!(*p_elem_ref_type =
  3902. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3903. error_buf, error_buf_size))) {
  3904. return false;
  3905. }
  3906. }
  3907. #endif
  3908. *p_buf = p;
  3909. return true;
  3910. fail:
  3911. return false;
  3912. }
  3913. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3914. static bool
  3915. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3916. WASMModule *module, WASMTableSeg *table_segment,
  3917. char *error_buf, uint32 error_buf_size)
  3918. {
  3919. const uint8 *p = *p_buf, *p_end = buf_end;
  3920. uint32 function_count, function_index = 0, i;
  3921. uint64 total_size;
  3922. read_leb_uint32(p, p_end, function_count);
  3923. table_segment->value_count = function_count;
  3924. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3925. if (total_size > 0
  3926. && !(table_segment->init_values =
  3927. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3928. error_buf_size))) {
  3929. return false;
  3930. }
  3931. for (i = 0; i < function_count; i++) {
  3932. InitializerExpression *init_expr = &table_segment->init_values[i];
  3933. read_leb_uint32(p, p_end, function_index);
  3934. if (!check_function_index(module, function_index, error_buf,
  3935. error_buf_size)) {
  3936. return false;
  3937. }
  3938. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3939. init_expr->u.ref_index = function_index;
  3940. }
  3941. *p_buf = p;
  3942. return true;
  3943. fail:
  3944. return false;
  3945. }
  3946. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3947. static bool
  3948. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3949. WASMModule *module, WASMTableSeg *table_segment,
  3950. char *error_buf, uint32 error_buf_size)
  3951. {
  3952. const uint8 *p = *p_buf, *p_end = buf_end;
  3953. uint32 ref_count, i;
  3954. uint64 total_size;
  3955. read_leb_uint32(p, p_end, ref_count);
  3956. table_segment->value_count = ref_count;
  3957. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3958. if (total_size > 0
  3959. && !(table_segment->init_values =
  3960. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3961. error_buf_size))) {
  3962. return false;
  3963. }
  3964. for (i = 0; i < ref_count; i++) {
  3965. InitializerExpression *init_expr = &table_segment->init_values[i];
  3966. if (!load_init_expr(module, &p, p_end, init_expr,
  3967. table_segment->elem_type,
  3968. #if WASM_ENABLE_GC == 0
  3969. NULL,
  3970. #else
  3971. table_segment->elem_ref_type,
  3972. #endif
  3973. error_buf, error_buf_size))
  3974. return false;
  3975. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3976. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3977. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3978. && init_expr->init_expr_type
  3979. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3980. }
  3981. *p_buf = p;
  3982. return true;
  3983. fail:
  3984. return false;
  3985. }
  3986. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3987. static bool
  3988. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3989. WASMModule *module, char *error_buf,
  3990. uint32 error_buf_size)
  3991. {
  3992. const uint8 *p = buf, *p_end = buf_end;
  3993. uint8 table_elem_idx_type;
  3994. uint32 table_segment_count, i;
  3995. uint64 total_size;
  3996. WASMTableSeg *table_segment;
  3997. read_leb_uint32(p, p_end, table_segment_count);
  3998. if (table_segment_count) {
  3999. module->table_seg_count = table_segment_count;
  4000. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4001. if (!(module->table_segments =
  4002. loader_malloc(total_size, error_buf, error_buf_size))) {
  4003. return false;
  4004. }
  4005. table_segment = module->table_segments;
  4006. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4007. if (p >= p_end) {
  4008. set_error_buf(error_buf, error_buf_size,
  4009. "invalid value type or "
  4010. "invalid elements segment kind");
  4011. return false;
  4012. }
  4013. table_elem_idx_type = VALUE_TYPE_I32;
  4014. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4015. read_leb_uint32(p, p_end, table_segment->mode);
  4016. /* last three bits */
  4017. table_segment->mode = table_segment->mode & 0x07;
  4018. switch (table_segment->mode) {
  4019. /* elemkind/elemtype + active */
  4020. case 0:
  4021. case 4:
  4022. {
  4023. #if WASM_ENABLE_GC != 0
  4024. if (table_segment->mode == 0) {
  4025. /* vec(funcidx), set elem type to (ref func) */
  4026. WASMRefType elem_ref_type = { 0 };
  4027. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4028. wasm_set_refheaptype_common(
  4029. &elem_ref_type.ref_ht_common, false,
  4030. HEAP_TYPE_FUNC);
  4031. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4032. module->ref_type_set, &elem_ref_type,
  4033. error_buf, error_buf_size)))
  4034. return false;
  4035. }
  4036. else {
  4037. /* vec(expr), set elem type to funcref */
  4038. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4039. }
  4040. #else
  4041. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4042. #endif
  4043. table_segment->table_index = 0;
  4044. if (!check_table_index(module, table_segment->table_index,
  4045. error_buf, error_buf_size))
  4046. return false;
  4047. #if WASM_ENABLE_MEMORY64 != 0
  4048. table_elem_idx_type =
  4049. is_table_64bit(module, table_segment->table_index)
  4050. ? VALUE_TYPE_I64
  4051. : VALUE_TYPE_I32;
  4052. #endif
  4053. if (!load_init_expr(module, &p, p_end,
  4054. &table_segment->base_offset,
  4055. table_elem_idx_type, NULL, error_buf,
  4056. error_buf_size))
  4057. return false;
  4058. if (table_segment->mode == 0) {
  4059. /* vec(funcidx) */
  4060. if (!load_func_index_vec(&p, p_end, module,
  4061. table_segment, error_buf,
  4062. error_buf_size))
  4063. return false;
  4064. }
  4065. else {
  4066. /* vec(expr) */
  4067. if (!load_init_expr_vec(&p, p_end, module,
  4068. table_segment, error_buf,
  4069. error_buf_size))
  4070. return false;
  4071. }
  4072. if (!check_table_elem_type(module,
  4073. table_segment->table_index,
  4074. table_segment->elem_type,
  4075. error_buf, error_buf_size))
  4076. return false;
  4077. break;
  4078. }
  4079. /* elemkind + passive/declarative */
  4080. case 1:
  4081. case 3:
  4082. if (!load_elem_type(module, &p, p_end,
  4083. &table_segment->elem_type,
  4084. #if WASM_ENABLE_GC != 0
  4085. &table_segment->elem_ref_type,
  4086. #endif
  4087. true, error_buf, error_buf_size))
  4088. return false;
  4089. /* vec(funcidx) */
  4090. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4091. error_buf, error_buf_size))
  4092. return false;
  4093. break;
  4094. /* elemkind/elemtype + table_idx + active */
  4095. case 2:
  4096. case 6:
  4097. if (!load_table_index(&p, p_end, module,
  4098. &table_segment->table_index,
  4099. error_buf, error_buf_size))
  4100. return false;
  4101. #if WASM_ENABLE_MEMORY64 != 0
  4102. table_elem_idx_type =
  4103. is_table_64bit(module, table_segment->table_index)
  4104. ? VALUE_TYPE_I64
  4105. : VALUE_TYPE_I32;
  4106. #endif
  4107. if (!load_init_expr(module, &p, p_end,
  4108. &table_segment->base_offset,
  4109. table_elem_idx_type, NULL, error_buf,
  4110. error_buf_size))
  4111. return false;
  4112. if (!load_elem_type(module, &p, p_end,
  4113. &table_segment->elem_type,
  4114. #if WASM_ENABLE_GC != 0
  4115. &table_segment->elem_ref_type,
  4116. #endif
  4117. table_segment->mode == 2 ? true : false,
  4118. error_buf, error_buf_size))
  4119. return false;
  4120. if (table_segment->mode == 2) {
  4121. /* vec(funcidx) */
  4122. if (!load_func_index_vec(&p, p_end, module,
  4123. table_segment, error_buf,
  4124. error_buf_size))
  4125. return false;
  4126. }
  4127. else {
  4128. /* vec(expr) */
  4129. if (!load_init_expr_vec(&p, p_end, module,
  4130. table_segment, error_buf,
  4131. error_buf_size))
  4132. return false;
  4133. }
  4134. if (!check_table_elem_type(module,
  4135. table_segment->table_index,
  4136. table_segment->elem_type,
  4137. error_buf, error_buf_size))
  4138. return false;
  4139. break;
  4140. case 5:
  4141. case 7:
  4142. if (!load_elem_type(module, &p, p_end,
  4143. &table_segment->elem_type,
  4144. #if WASM_ENABLE_GC != 0
  4145. &table_segment->elem_ref_type,
  4146. #endif
  4147. false, error_buf, error_buf_size))
  4148. return false;
  4149. /* vec(expr) */
  4150. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4151. error_buf, error_buf_size))
  4152. return false;
  4153. break;
  4154. default:
  4155. set_error_buf(error_buf, error_buf_size,
  4156. "unknown element segment kind");
  4157. return false;
  4158. }
  4159. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4160. /*
  4161. * like: 00 41 05 0b 04 00 01 00 01
  4162. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4163. */
  4164. if (!load_table_index(&p, p_end, module,
  4165. &table_segment->table_index, error_buf,
  4166. error_buf_size))
  4167. return false;
  4168. #if WASM_ENABLE_MEMORY64 != 0
  4169. table_elem_idx_type =
  4170. is_table_64bit(module, table_segment->table_index)
  4171. ? VALUE_TYPE_I64
  4172. : VALUE_TYPE_I32;
  4173. #endif
  4174. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4175. table_elem_idx_type, NULL, error_buf,
  4176. error_buf_size))
  4177. return false;
  4178. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4179. error_buf, error_buf_size))
  4180. return false;
  4181. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4182. if (!check_table_elem_type(module, table_segment->table_index,
  4183. table_segment->elem_type, error_buf,
  4184. error_buf_size))
  4185. return false;
  4186. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4187. #if WASM_ENABLE_MEMORY64 != 0
  4188. if (table_elem_idx_type == VALUE_TYPE_I64
  4189. && table_segment->base_offset.u.u64 > UINT32_MAX) {
  4190. set_error_buf(error_buf, error_buf_size,
  4191. "In table64, table base offset can't be "
  4192. "larger than UINT32_MAX");
  4193. return false;
  4194. }
  4195. #endif
  4196. #if WASM_ENABLE_WAMR_COMPILER != 0
  4197. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4198. module->is_ref_types_used = true;
  4199. #endif
  4200. }
  4201. }
  4202. if (p != p_end) {
  4203. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4204. return false;
  4205. }
  4206. LOG_VERBOSE("Load table segment section success.\n");
  4207. return true;
  4208. fail:
  4209. return false;
  4210. }
  4211. #if WASM_ENABLE_BULK_MEMORY != 0
  4212. static bool
  4213. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4214. int data_seg_len, char *error_buf,
  4215. uint32 error_buf_size)
  4216. {
  4217. if (has_datacount_section && datacount_len != data_seg_len) {
  4218. set_error_buf(error_buf, error_buf_size,
  4219. "data count and data section have inconsistent lengths");
  4220. return false;
  4221. }
  4222. return true;
  4223. }
  4224. #endif
  4225. static bool
  4226. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4227. WASMModule *module,
  4228. #if WASM_ENABLE_BULK_MEMORY != 0
  4229. bool has_datacount_section,
  4230. #endif
  4231. bool clone_data_seg, char *error_buf,
  4232. uint32 error_buf_size)
  4233. {
  4234. const uint8 *p = buf, *p_end = buf_end;
  4235. uint32 data_seg_count, i, mem_index, data_seg_len;
  4236. uint64 total_size;
  4237. WASMDataSeg *dataseg;
  4238. InitializerExpression init_expr;
  4239. #if WASM_ENABLE_BULK_MEMORY != 0
  4240. bool is_passive = false;
  4241. uint32 mem_flag;
  4242. #endif
  4243. uint8 mem_offset_type = VALUE_TYPE_I32;
  4244. read_leb_uint32(p, p_end, data_seg_count);
  4245. #if WASM_ENABLE_BULK_MEMORY != 0
  4246. if (!check_data_count_consistency(has_datacount_section,
  4247. module->data_seg_count1, data_seg_count,
  4248. error_buf, error_buf_size)) {
  4249. return false;
  4250. }
  4251. #endif
  4252. if (data_seg_count) {
  4253. module->data_seg_count = data_seg_count;
  4254. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4255. if (!(module->data_segments =
  4256. loader_malloc(total_size, error_buf, error_buf_size))) {
  4257. return false;
  4258. }
  4259. for (i = 0; i < data_seg_count; i++) {
  4260. read_leb_uint32(p, p_end, mem_index);
  4261. #if WASM_ENABLE_BULK_MEMORY != 0
  4262. is_passive = false;
  4263. mem_flag = mem_index & 0x03;
  4264. switch (mem_flag) {
  4265. case 0x01:
  4266. is_passive = true;
  4267. #if WASM_ENABLE_WAMR_COMPILER != 0
  4268. module->is_bulk_memory_used = true;
  4269. #endif
  4270. break;
  4271. case 0x00:
  4272. /* no memory index, treat index as 0 */
  4273. mem_index = 0;
  4274. goto check_mem_index;
  4275. case 0x02:
  4276. /* read following memory index */
  4277. read_leb_uint32(p, p_end, mem_index);
  4278. #if WASM_ENABLE_WAMR_COMPILER != 0
  4279. module->is_bulk_memory_used = true;
  4280. #endif
  4281. check_mem_index:
  4282. if (mem_index
  4283. >= module->import_memory_count + module->memory_count) {
  4284. set_error_buf_v(error_buf, error_buf_size,
  4285. "unknown memory %d", mem_index);
  4286. return false;
  4287. }
  4288. break;
  4289. case 0x03:
  4290. default:
  4291. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4292. return false;
  4293. break;
  4294. }
  4295. #else
  4296. if (mem_index
  4297. >= module->import_memory_count + module->memory_count) {
  4298. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4299. mem_index);
  4300. return false;
  4301. }
  4302. #endif /* WASM_ENABLE_BULK_MEMORY */
  4303. #if WASM_ENABLE_BULK_MEMORY != 0
  4304. if (!is_passive)
  4305. #endif
  4306. {
  4307. #if WASM_ENABLE_MEMORY64 != 0
  4308. /* This memory_flag is from memory instead of data segment */
  4309. uint8 memory_flag;
  4310. if (module->import_memory_count > 0) {
  4311. memory_flag = module->import_memories[mem_index]
  4312. .u.memory.mem_type.flags;
  4313. }
  4314. else {
  4315. memory_flag =
  4316. module
  4317. ->memories[mem_index - module->import_memory_count]
  4318. .flags;
  4319. }
  4320. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4321. : VALUE_TYPE_I32;
  4322. #else
  4323. mem_offset_type = VALUE_TYPE_I32;
  4324. #endif
  4325. }
  4326. #if WASM_ENABLE_BULK_MEMORY != 0
  4327. if (!is_passive)
  4328. #endif
  4329. if (!load_init_expr(module, &p, p_end, &init_expr,
  4330. mem_offset_type, NULL, error_buf,
  4331. error_buf_size))
  4332. return false;
  4333. read_leb_uint32(p, p_end, data_seg_len);
  4334. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4335. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4336. return false;
  4337. }
  4338. #if WASM_ENABLE_BULK_MEMORY != 0
  4339. dataseg->is_passive = is_passive;
  4340. if (!is_passive)
  4341. #endif
  4342. {
  4343. bh_memcpy_s(&dataseg->base_offset,
  4344. sizeof(InitializerExpression), &init_expr,
  4345. sizeof(InitializerExpression));
  4346. dataseg->memory_index = mem_index;
  4347. }
  4348. dataseg->data_length = data_seg_len;
  4349. CHECK_BUF(p, p_end, data_seg_len);
  4350. if (clone_data_seg) {
  4351. if (!(dataseg->data = loader_malloc(
  4352. dataseg->data_length, error_buf, error_buf_size))) {
  4353. return false;
  4354. }
  4355. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4356. data_seg_len);
  4357. }
  4358. else {
  4359. dataseg->data = (uint8 *)p;
  4360. }
  4361. dataseg->is_data_cloned = clone_data_seg;
  4362. p += data_seg_len;
  4363. }
  4364. }
  4365. if (p != p_end) {
  4366. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4367. return false;
  4368. }
  4369. LOG_VERBOSE("Load data segment section success.\n");
  4370. return true;
  4371. fail:
  4372. return false;
  4373. }
  4374. #if WASM_ENABLE_BULK_MEMORY != 0
  4375. static bool
  4376. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4377. WASMModule *module, char *error_buf,
  4378. uint32 error_buf_size)
  4379. {
  4380. const uint8 *p = buf, *p_end = buf_end;
  4381. uint32 data_seg_count1 = 0;
  4382. read_leb_uint32(p, p_end, data_seg_count1);
  4383. module->data_seg_count1 = data_seg_count1;
  4384. if (p != p_end) {
  4385. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4386. return false;
  4387. }
  4388. #if WASM_ENABLE_WAMR_COMPILER != 0
  4389. module->is_bulk_memory_used = true;
  4390. #endif
  4391. LOG_VERBOSE("Load datacount section success.\n");
  4392. return true;
  4393. fail:
  4394. return false;
  4395. }
  4396. #endif
  4397. #if WASM_ENABLE_TAGS != 0
  4398. static bool
  4399. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4400. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4401. uint32 error_buf_size)
  4402. {
  4403. (void)buf_code;
  4404. (void)buf_code_end;
  4405. const uint8 *p = buf, *p_end = buf_end;
  4406. size_t total_size = 0;
  4407. /* number of tags defined in the section */
  4408. uint32 section_tag_count = 0;
  4409. uint8 tag_attribute;
  4410. uint32 tag_type;
  4411. WASMTag *tag = NULL;
  4412. /* get tag count */
  4413. read_leb_uint32(p, p_end, section_tag_count);
  4414. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4415. error_buf, error_buf_size))
  4416. return false;
  4417. module->tag_count = section_tag_count;
  4418. if (section_tag_count) {
  4419. total_size = sizeof(WASMTag *) * module->tag_count;
  4420. if (!(module->tags =
  4421. loader_malloc(total_size, error_buf, error_buf_size))) {
  4422. return false;
  4423. }
  4424. /* load each tag, imported tags precede the tags */
  4425. uint32 tag_index;
  4426. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4427. /* get the one byte attribute */
  4428. CHECK_BUF(p, p_end, 1);
  4429. tag_attribute = read_uint8(p);
  4430. /* get type */
  4431. read_leb_uint32(p, p_end, tag_type);
  4432. /* compare against module->types */
  4433. if (tag_type >= module->type_count) {
  4434. set_error_buf(error_buf, error_buf_size, "unknown type");
  4435. return false;
  4436. }
  4437. /* get return type (must be 0) */
  4438. /* check, that the type of the referred tag returns void */
  4439. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4440. if (func_type->result_count != 0) {
  4441. set_error_buf(error_buf, error_buf_size,
  4442. "non-empty tag result type");
  4443. goto fail;
  4444. }
  4445. if (!(tag = module->tags[tag_index] = loader_malloc(
  4446. sizeof(WASMTag), error_buf, error_buf_size))) {
  4447. return false;
  4448. }
  4449. /* store to module tag declarations */
  4450. tag->attribute = tag_attribute;
  4451. tag->type = tag_type;
  4452. tag->tag_type = func_type;
  4453. }
  4454. }
  4455. if (p != p_end) {
  4456. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4457. return false;
  4458. }
  4459. LOG_VERBOSE("Load tag section success.\n");
  4460. return true;
  4461. fail:
  4462. return false;
  4463. }
  4464. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4465. static bool
  4466. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4467. const uint8 *buf_func_end, WASMModule *module,
  4468. char *error_buf, uint32 error_buf_size)
  4469. {
  4470. const uint8 *p = buf, *p_end = buf_end;
  4471. const uint8 *p_func = buf_func;
  4472. uint32 func_count = 0, code_count;
  4473. /* code has been loaded in function section, so pass it here, just check
  4474. * whether function and code section have inconsistent lengths */
  4475. read_leb_uint32(p, p_end, code_count);
  4476. if (buf_func)
  4477. read_leb_uint32(p_func, buf_func_end, func_count);
  4478. if (func_count != code_count) {
  4479. set_error_buf(error_buf, error_buf_size,
  4480. "function and code section have inconsistent lengths");
  4481. return false;
  4482. }
  4483. LOG_VERBOSE("Load code segment section success.\n");
  4484. (void)module;
  4485. return true;
  4486. fail:
  4487. return false;
  4488. }
  4489. static bool
  4490. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4491. char *error_buf, uint32 error_buf_size)
  4492. {
  4493. const uint8 *p = buf, *p_end = buf_end;
  4494. WASMFuncType *type;
  4495. uint32 start_function;
  4496. read_leb_uint32(p, p_end, start_function);
  4497. if (start_function
  4498. >= module->function_count + module->import_function_count) {
  4499. set_error_buf(error_buf, error_buf_size, "unknown function");
  4500. return false;
  4501. }
  4502. if (start_function < module->import_function_count)
  4503. type = module->import_functions[start_function].u.function.func_type;
  4504. else
  4505. type = module->functions[start_function - module->import_function_count]
  4506. ->func_type;
  4507. if (type->param_count != 0 || type->result_count != 0) {
  4508. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4509. return false;
  4510. }
  4511. module->start_function = start_function;
  4512. if (p != p_end) {
  4513. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4514. return false;
  4515. }
  4516. LOG_VERBOSE("Load start section success.\n");
  4517. return true;
  4518. fail:
  4519. return false;
  4520. }
  4521. #if WASM_ENABLE_STRINGREF != 0
  4522. static bool
  4523. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4524. WASMModule *module, bool is_load_from_file_buf,
  4525. char *error_buf, uint32 error_buf_size)
  4526. {
  4527. const uint8 *p = buf, *p_end = buf_end;
  4528. int32 deferred_count, immediate_count, string_length, i;
  4529. uint64 total_size;
  4530. read_leb_uint32(p, p_end, deferred_count);
  4531. read_leb_uint32(p, p_end, immediate_count);
  4532. /* proposal set deferred_count for future extension */
  4533. if (deferred_count != 0) {
  4534. goto fail;
  4535. }
  4536. if (immediate_count > 0) {
  4537. total_size = sizeof(char *) * (uint64)immediate_count;
  4538. if (!(module->string_literal_ptrs =
  4539. loader_malloc(total_size, error_buf, error_buf_size))) {
  4540. goto fail;
  4541. }
  4542. module->string_literal_count = immediate_count;
  4543. total_size = sizeof(uint32) * (uint64)immediate_count;
  4544. if (!(module->string_literal_lengths =
  4545. loader_malloc(total_size, error_buf, error_buf_size))) {
  4546. goto fail;
  4547. }
  4548. for (i = 0; i < immediate_count; i++) {
  4549. read_leb_uint32(p, p_end, string_length);
  4550. CHECK_BUF(p, p_end, string_length);
  4551. module->string_literal_ptrs[i] = p;
  4552. module->string_literal_lengths[i] = string_length;
  4553. p += string_length;
  4554. }
  4555. }
  4556. if (p != p_end) {
  4557. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4558. goto fail;
  4559. }
  4560. LOG_VERBOSE("Load stringref section success.\n");
  4561. return true;
  4562. fail:
  4563. return false;
  4564. }
  4565. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4566. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4567. static bool
  4568. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4569. bool is_load_from_file_buf, char *error_buf,
  4570. uint32 error_buf_size)
  4571. {
  4572. const uint8 *p = buf, *p_end = buf_end;
  4573. uint32 name_type, subsection_size;
  4574. uint32 previous_name_type = 0;
  4575. uint32 num_func_name;
  4576. uint32 func_index;
  4577. uint32 previous_func_index = ~0U;
  4578. uint32 func_name_len;
  4579. uint32 name_index;
  4580. int i = 0;
  4581. if (p >= p_end) {
  4582. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4583. return false;
  4584. }
  4585. while (p < p_end) {
  4586. read_leb_uint32(p, p_end, name_type);
  4587. if (i != 0) {
  4588. if (name_type == previous_name_type) {
  4589. set_error_buf(error_buf, error_buf_size,
  4590. "duplicate sub-section");
  4591. return false;
  4592. }
  4593. if (name_type < previous_name_type) {
  4594. set_error_buf(error_buf, error_buf_size,
  4595. "out-of-order sub-section");
  4596. return false;
  4597. }
  4598. }
  4599. previous_name_type = name_type;
  4600. read_leb_uint32(p, p_end, subsection_size);
  4601. CHECK_BUF(p, p_end, subsection_size);
  4602. switch (name_type) {
  4603. case SUB_SECTION_TYPE_FUNC:
  4604. if (subsection_size) {
  4605. read_leb_uint32(p, p_end, num_func_name);
  4606. for (name_index = 0; name_index < num_func_name;
  4607. name_index++) {
  4608. read_leb_uint32(p, p_end, func_index);
  4609. if (func_index == previous_func_index) {
  4610. set_error_buf(error_buf, error_buf_size,
  4611. "duplicate function name");
  4612. return false;
  4613. }
  4614. if (func_index < previous_func_index
  4615. && previous_func_index != ~0U) {
  4616. set_error_buf(error_buf, error_buf_size,
  4617. "out-of-order function index ");
  4618. return false;
  4619. }
  4620. previous_func_index = func_index;
  4621. read_leb_uint32(p, p_end, func_name_len);
  4622. CHECK_BUF(p, p_end, func_name_len);
  4623. /* Skip the import functions */
  4624. if (func_index >= module->import_function_count) {
  4625. func_index -= module->import_function_count;
  4626. if (func_index >= module->function_count) {
  4627. set_error_buf(error_buf, error_buf_size,
  4628. "out-of-range function index");
  4629. return false;
  4630. }
  4631. if (!(module->functions[func_index]->field_name =
  4632. wasm_const_str_list_insert(
  4633. p, func_name_len, module,
  4634. #if WASM_ENABLE_WAMR_COMPILER != 0
  4635. false,
  4636. #else
  4637. is_load_from_file_buf,
  4638. #endif
  4639. error_buf, error_buf_size))) {
  4640. return false;
  4641. }
  4642. }
  4643. p += func_name_len;
  4644. }
  4645. }
  4646. break;
  4647. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4648. */
  4649. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4650. default:
  4651. p = p + subsection_size;
  4652. break;
  4653. }
  4654. i++;
  4655. }
  4656. return true;
  4657. fail:
  4658. return false;
  4659. }
  4660. #endif
  4661. static bool
  4662. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4663. bool is_load_from_file_buf, char *error_buf,
  4664. uint32 error_buf_size)
  4665. {
  4666. const uint8 *p = buf, *p_end = buf_end;
  4667. char section_name[32];
  4668. uint32 name_len, buffer_len;
  4669. if (p >= p_end) {
  4670. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4671. return false;
  4672. }
  4673. read_leb_uint32(p, p_end, name_len);
  4674. if (p + name_len > p_end) {
  4675. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4676. return false;
  4677. }
  4678. if (!wasm_check_utf8_str(p, name_len)) {
  4679. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4680. return false;
  4681. }
  4682. buffer_len = sizeof(section_name);
  4683. memset(section_name, 0, buffer_len);
  4684. if (name_len < buffer_len) {
  4685. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4686. }
  4687. else {
  4688. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4689. memset(section_name + buffer_len - 4, '.', 3);
  4690. }
  4691. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4692. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4693. module->name_section_buf = buf;
  4694. module->name_section_buf_end = buf_end;
  4695. p += name_len;
  4696. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4697. error_buf, error_buf_size)) {
  4698. return false;
  4699. }
  4700. LOG_VERBOSE("Load custom name section success.");
  4701. }
  4702. #endif
  4703. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4704. {
  4705. WASMCustomSection *section =
  4706. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4707. if (!section) {
  4708. return false;
  4709. }
  4710. section->name_addr = (char *)p;
  4711. section->name_len = name_len;
  4712. section->content_addr = (uint8 *)(p + name_len);
  4713. section->content_len = (uint32)(p_end - p - name_len);
  4714. section->next = module->custom_section_list;
  4715. module->custom_section_list = section;
  4716. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4717. return true;
  4718. }
  4719. #endif
  4720. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4721. (void)is_load_from_file_buf;
  4722. (void)module;
  4723. return true;
  4724. fail:
  4725. return false;
  4726. }
  4727. static void
  4728. calculate_global_data_offset(WASMModule *module)
  4729. {
  4730. uint32 i, data_offset;
  4731. data_offset = 0;
  4732. for (i = 0; i < module->import_global_count; i++) {
  4733. WASMGlobalImport *import_global =
  4734. &((module->import_globals + i)->u.global);
  4735. #if WASM_ENABLE_FAST_JIT != 0
  4736. import_global->data_offset = data_offset;
  4737. #endif
  4738. data_offset += wasm_value_type_size(import_global->type.val_type);
  4739. }
  4740. for (i = 0; i < module->global_count; i++) {
  4741. WASMGlobal *global = module->globals + i;
  4742. #if WASM_ENABLE_FAST_JIT != 0
  4743. global->data_offset = data_offset;
  4744. #endif
  4745. data_offset += wasm_value_type_size(global->type.val_type);
  4746. }
  4747. module->global_data_size = data_offset;
  4748. }
  4749. #if WASM_ENABLE_FAST_JIT != 0
  4750. static bool
  4751. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4752. uint32 error_buf_size)
  4753. {
  4754. #if WASM_ENABLE_LAZY_JIT != 0
  4755. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4756. #endif
  4757. uint32 i;
  4758. if (!module->function_count)
  4759. return true;
  4760. if (!(module->fast_jit_func_ptrs =
  4761. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4762. error_buf_size))) {
  4763. return false;
  4764. }
  4765. #if WASM_ENABLE_LAZY_JIT != 0
  4766. for (i = 0; i < module->function_count; i++) {
  4767. module->fast_jit_func_ptrs[i] =
  4768. jit_globals->compile_fast_jit_and_then_call;
  4769. }
  4770. #endif
  4771. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4772. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4773. set_error_buf(error_buf, error_buf_size,
  4774. "init fast jit thread lock failed");
  4775. return false;
  4776. }
  4777. module->fast_jit_thread_locks_inited[i] = true;
  4778. }
  4779. return true;
  4780. }
  4781. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4782. #if WASM_ENABLE_JIT != 0
  4783. static bool
  4784. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4785. uint32 error_buf_size)
  4786. {
  4787. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4788. AOTCompOption option = { 0 };
  4789. char *aot_last_error;
  4790. uint64 size;
  4791. #if WASM_ENABLE_GC != 0
  4792. bool gc_enabled = true;
  4793. #else
  4794. bool gc_enabled = false;
  4795. #endif
  4796. if (module->function_count == 0)
  4797. return true;
  4798. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4799. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4800. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4801. return false;
  4802. }
  4803. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4804. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4805. os_mutex_destroy(&module->tierup_wait_lock);
  4806. return false;
  4807. }
  4808. module->tierup_wait_lock_inited = true;
  4809. #endif
  4810. size = sizeof(void *) * (uint64)module->function_count
  4811. + sizeof(bool) * (uint64)module->function_count;
  4812. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4813. return false;
  4814. }
  4815. module->func_ptrs_compiled =
  4816. (bool *)((uint8 *)module->func_ptrs
  4817. + sizeof(void *) * module->function_count);
  4818. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4819. if (!module->comp_data) {
  4820. aot_last_error = aot_get_last_error();
  4821. bh_assert(aot_last_error != NULL);
  4822. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4823. return false;
  4824. }
  4825. option.is_jit_mode = true;
  4826. option.opt_level = llvm_jit_options->opt_level;
  4827. option.size_level = llvm_jit_options->size_level;
  4828. option.segue_flags = llvm_jit_options->segue_flags;
  4829. option.quick_invoke_c_api_import =
  4830. llvm_jit_options->quick_invoke_c_api_import;
  4831. #if WASM_ENABLE_BULK_MEMORY != 0
  4832. option.enable_bulk_memory = true;
  4833. #endif
  4834. #if WASM_ENABLE_THREAD_MGR != 0
  4835. option.enable_thread_mgr = true;
  4836. #endif
  4837. #if WASM_ENABLE_TAIL_CALL != 0
  4838. option.enable_tail_call = true;
  4839. #endif
  4840. #if WASM_ENABLE_SIMD != 0
  4841. option.enable_simd = true;
  4842. #endif
  4843. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4844. option.enable_ref_types = true;
  4845. #elif WASM_ENABLE_GC != 0
  4846. option.enable_gc = true;
  4847. #endif
  4848. option.enable_aux_stack_check = true;
  4849. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4850. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4851. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4852. aot_call_stack_features_init_default(&option.call_stack_features);
  4853. #endif
  4854. #if WASM_ENABLE_PERF_PROFILING != 0
  4855. option.enable_perf_profiling = true;
  4856. #endif
  4857. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4858. option.enable_memory_profiling = true;
  4859. option.enable_stack_estimation = true;
  4860. #endif
  4861. #if WASM_ENABLE_SHARED_HEAP != 0
  4862. option.enable_shared_heap = true;
  4863. #endif
  4864. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4865. if (!module->comp_ctx) {
  4866. aot_last_error = aot_get_last_error();
  4867. bh_assert(aot_last_error != NULL);
  4868. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4869. return false;
  4870. }
  4871. return true;
  4872. }
  4873. static bool
  4874. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4875. uint32 error_buf_size)
  4876. {
  4877. char *aot_last_error;
  4878. uint32 i;
  4879. if (module->function_count == 0)
  4880. return true;
  4881. if (!aot_compile_wasm(module->comp_ctx)) {
  4882. aot_last_error = aot_get_last_error();
  4883. bh_assert(aot_last_error != NULL);
  4884. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4885. return false;
  4886. }
  4887. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4888. if (module->orcjit_stop_compiling)
  4889. return false;
  4890. #endif
  4891. bh_print_time("Begin to lookup llvm jit functions");
  4892. for (i = 0; i < module->function_count; i++) {
  4893. LLVMOrcJITTargetAddress func_addr = 0;
  4894. LLVMErrorRef error;
  4895. char func_name[48];
  4896. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4897. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4898. func_name);
  4899. if (error != LLVMErrorSuccess) {
  4900. char *err_msg = LLVMGetErrorMessage(error);
  4901. set_error_buf_v(error_buf, error_buf_size,
  4902. "failed to compile llvm jit function: %s", err_msg);
  4903. LLVMDisposeErrorMessage(err_msg);
  4904. return false;
  4905. }
  4906. /**
  4907. * No need to lock the func_ptr[func_idx] here as it is basic
  4908. * data type, the load/store for it can be finished by one cpu
  4909. * instruction, and there can be only one cpu instruction
  4910. * loading/storing at the same time.
  4911. */
  4912. module->func_ptrs[i] = (void *)func_addr;
  4913. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4914. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4915. if (module->orcjit_stop_compiling)
  4916. return false;
  4917. #endif
  4918. }
  4919. bh_print_time("End lookup llvm jit functions");
  4920. return true;
  4921. }
  4922. #endif /* end of WASM_ENABLE_JIT != 0 */
  4923. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4924. && WASM_ENABLE_LAZY_JIT != 0
  4925. static void *
  4926. init_llvm_jit_functions_stage2_callback(void *arg)
  4927. {
  4928. WASMModule *module = (WASMModule *)arg;
  4929. char error_buf[128];
  4930. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4931. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4932. module->orcjit_stop_compiling = true;
  4933. return NULL;
  4934. }
  4935. os_mutex_lock(&module->tierup_wait_lock);
  4936. module->llvm_jit_inited = true;
  4937. os_cond_broadcast(&module->tierup_wait_cond);
  4938. os_mutex_unlock(&module->tierup_wait_lock);
  4939. return NULL;
  4940. }
  4941. #endif
  4942. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4943. /* The callback function to compile jit functions */
  4944. static void *
  4945. orcjit_thread_callback(void *arg)
  4946. {
  4947. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4948. #if WASM_ENABLE_JIT != 0
  4949. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4950. #endif
  4951. WASMModule *module = thread_arg->module;
  4952. uint32 group_idx = thread_arg->group_idx;
  4953. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4954. uint32 func_count = module->function_count;
  4955. uint32 i;
  4956. #if WASM_ENABLE_FAST_JIT != 0
  4957. /* Compile fast jit functions of this group */
  4958. for (i = group_idx; i < func_count; i += group_stride) {
  4959. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4960. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4961. break;
  4962. }
  4963. if (module->orcjit_stop_compiling) {
  4964. return NULL;
  4965. }
  4966. }
  4967. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4968. os_mutex_lock(&module->tierup_wait_lock);
  4969. module->fast_jit_ready_groups++;
  4970. os_mutex_unlock(&module->tierup_wait_lock);
  4971. #endif
  4972. #endif
  4973. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4974. && WASM_ENABLE_LAZY_JIT != 0
  4975. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4976. for (i = group_idx; i < func_count;
  4977. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4978. uint32 j;
  4979. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4980. if (i + j * group_stride < func_count) {
  4981. if (!jit_compiler_set_call_to_fast_jit(
  4982. module,
  4983. i + j * group_stride + module->import_function_count)) {
  4984. LOG_ERROR(
  4985. "failed to compile call_to_fast_jit for func %u\n",
  4986. i + j * group_stride + module->import_function_count);
  4987. module->orcjit_stop_compiling = true;
  4988. return NULL;
  4989. }
  4990. }
  4991. if (module->orcjit_stop_compiling) {
  4992. return NULL;
  4993. }
  4994. }
  4995. }
  4996. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4997. fast jit functions are compiled */
  4998. os_mutex_lock(&module->tierup_wait_lock);
  4999. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5000. && module->fast_jit_ready_groups >= group_stride)) {
  5001. os_cond_reltimedwait(&module->tierup_wait_cond,
  5002. &module->tierup_wait_lock, 10000);
  5003. if (module->orcjit_stop_compiling) {
  5004. /* init_llvm_jit_functions_stage2 failed */
  5005. os_mutex_unlock(&module->tierup_wait_lock);
  5006. return NULL;
  5007. }
  5008. }
  5009. os_mutex_unlock(&module->tierup_wait_lock);
  5010. #endif
  5011. #if WASM_ENABLE_JIT != 0
  5012. /* Compile llvm jit functions of this group */
  5013. for (i = group_idx; i < func_count;
  5014. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5015. LLVMOrcJITTargetAddress func_addr = 0;
  5016. LLVMErrorRef error;
  5017. char func_name[48];
  5018. typedef void (*F)(void);
  5019. union {
  5020. F f;
  5021. void *v;
  5022. } u;
  5023. uint32 j;
  5024. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5025. "_wrapper");
  5026. LOG_DEBUG("compile llvm jit func %s", func_name);
  5027. error =
  5028. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5029. if (error != LLVMErrorSuccess) {
  5030. char *err_msg = LLVMGetErrorMessage(error);
  5031. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5032. LLVMDisposeErrorMessage(err_msg);
  5033. break;
  5034. }
  5035. /* Call the jit wrapper function to trigger its compilation, so as
  5036. to compile the actual jit functions, since we add the latter to
  5037. function list in the PartitionFunction callback */
  5038. u.v = (void *)func_addr;
  5039. u.f();
  5040. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5041. if (i + j * group_stride < func_count) {
  5042. module->func_ptrs_compiled[i + j * group_stride] = true;
  5043. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5044. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5045. i + j * group_stride);
  5046. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5047. func_name);
  5048. if (error != LLVMErrorSuccess) {
  5049. char *err_msg = LLVMGetErrorMessage(error);
  5050. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5051. err_msg);
  5052. LLVMDisposeErrorMessage(err_msg);
  5053. /* Ignore current llvm jit func, as its func ptr is
  5054. previous set to call_to_fast_jit, which also works */
  5055. continue;
  5056. }
  5057. jit_compiler_set_llvm_jit_func_ptr(
  5058. module,
  5059. i + j * group_stride + module->import_function_count,
  5060. (void *)func_addr);
  5061. /* Try to switch to call this llvm jit function instead of
  5062. fast jit function from fast jit jitted code */
  5063. jit_compiler_set_call_to_llvm_jit(
  5064. module,
  5065. i + j * group_stride + module->import_function_count);
  5066. #endif
  5067. }
  5068. }
  5069. if (module->orcjit_stop_compiling) {
  5070. break;
  5071. }
  5072. }
  5073. #endif
  5074. return NULL;
  5075. }
  5076. static void
  5077. orcjit_stop_compile_threads(WASMModule *module)
  5078. {
  5079. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5080. / sizeof(OrcJitThreadArg));
  5081. module->orcjit_stop_compiling = true;
  5082. for (i = 0; i < thread_num; i++) {
  5083. if (module->orcjit_threads[i])
  5084. os_thread_join(module->orcjit_threads[i], NULL);
  5085. }
  5086. }
  5087. static bool
  5088. compile_jit_functions(WASMModule *module, char *error_buf,
  5089. uint32 error_buf_size)
  5090. {
  5091. uint32 thread_num =
  5092. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5093. uint32 i, j;
  5094. bh_print_time("Begin to compile jit functions");
  5095. /* Create threads to compile the jit functions */
  5096. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5097. #if WASM_ENABLE_JIT != 0
  5098. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5099. #endif
  5100. module->orcjit_thread_args[i].module = module;
  5101. module->orcjit_thread_args[i].group_idx = i;
  5102. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5103. (void *)&module->orcjit_thread_args[i],
  5104. APP_THREAD_STACK_SIZE_DEFAULT)
  5105. != 0) {
  5106. set_error_buf(error_buf, error_buf_size,
  5107. "create orcjit compile thread failed");
  5108. /* Terminate the threads created */
  5109. module->orcjit_stop_compiling = true;
  5110. for (j = 0; j < i; j++) {
  5111. os_thread_join(module->orcjit_threads[j], NULL);
  5112. }
  5113. return false;
  5114. }
  5115. }
  5116. #if WASM_ENABLE_LAZY_JIT == 0
  5117. /* Wait until all jit functions are compiled for eager mode */
  5118. for (i = 0; i < thread_num; i++) {
  5119. if (module->orcjit_threads[i])
  5120. os_thread_join(module->orcjit_threads[i], NULL);
  5121. }
  5122. #if WASM_ENABLE_FAST_JIT != 0
  5123. /* Ensure all the fast-jit functions are compiled */
  5124. for (i = 0; i < module->function_count; i++) {
  5125. if (!jit_compiler_is_compiled(module,
  5126. i + module->import_function_count)) {
  5127. set_error_buf(error_buf, error_buf_size,
  5128. "failed to compile fast jit function");
  5129. return false;
  5130. }
  5131. }
  5132. #endif
  5133. #if WASM_ENABLE_JIT != 0
  5134. /* Ensure all the llvm-jit functions are compiled */
  5135. for (i = 0; i < module->function_count; i++) {
  5136. if (!module->func_ptrs_compiled[i]) {
  5137. set_error_buf(error_buf, error_buf_size,
  5138. "failed to compile llvm jit function");
  5139. return false;
  5140. }
  5141. }
  5142. #endif
  5143. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5144. bh_print_time("End compile jit functions");
  5145. return true;
  5146. }
  5147. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5148. static bool
  5149. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5150. uint32 cur_func_idx, char *error_buf,
  5151. uint32 error_buf_size);
  5152. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5153. void **
  5154. wasm_interp_get_handle_table(void);
  5155. static void **handle_table;
  5156. #endif
  5157. static bool
  5158. load_from_sections(WASMModule *module, WASMSection *sections,
  5159. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5160. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5161. {
  5162. WASMExport *export;
  5163. WASMSection *section = sections;
  5164. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5165. *buf_func = NULL, *buf_func_end = NULL;
  5166. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5167. WASMGlobal *aux_stack_top_global = NULL, *global;
  5168. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5169. aux_stack_top = (uint64)-1LL;
  5170. uint32 global_index, func_index, i;
  5171. uint32 aux_data_end_global_index = (uint32)-1;
  5172. uint32 aux_heap_base_global_index = (uint32)-1;
  5173. WASMFuncType *func_type;
  5174. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5175. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5176. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5177. #if WASM_ENABLE_BULK_MEMORY != 0
  5178. bool has_datacount_section = false;
  5179. #endif
  5180. /* Find code and function sections if have */
  5181. while (section) {
  5182. if (section->section_type == SECTION_TYPE_CODE) {
  5183. buf_code = section->section_body;
  5184. buf_code_end = buf_code + section->section_body_size;
  5185. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5186. module->buf_code = (uint8 *)buf_code;
  5187. module->buf_code_size = section->section_body_size;
  5188. #endif
  5189. }
  5190. else if (section->section_type == SECTION_TYPE_FUNC) {
  5191. buf_func = section->section_body;
  5192. buf_func_end = buf_func + section->section_body_size;
  5193. }
  5194. section = section->next;
  5195. }
  5196. section = sections;
  5197. while (section) {
  5198. buf = section->section_body;
  5199. buf_end = buf + section->section_body_size;
  5200. switch (section->section_type) {
  5201. case SECTION_TYPE_USER:
  5202. /* unsupported user section, ignore it. */
  5203. if (!load_user_section(buf, buf_end, module,
  5204. reuse_const_strings, error_buf,
  5205. error_buf_size))
  5206. return false;
  5207. break;
  5208. case SECTION_TYPE_TYPE:
  5209. if (!load_type_section(buf, buf_end, module, error_buf,
  5210. error_buf_size))
  5211. return false;
  5212. break;
  5213. case SECTION_TYPE_IMPORT:
  5214. if (!load_import_section(buf, buf_end, module,
  5215. reuse_const_strings, no_resolve,
  5216. error_buf, error_buf_size))
  5217. return false;
  5218. break;
  5219. case SECTION_TYPE_FUNC:
  5220. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5221. module, error_buf, error_buf_size))
  5222. return false;
  5223. break;
  5224. case SECTION_TYPE_TABLE:
  5225. if (!load_table_section(buf, buf_end, module, error_buf,
  5226. error_buf_size))
  5227. return false;
  5228. break;
  5229. case SECTION_TYPE_MEMORY:
  5230. if (!load_memory_section(buf, buf_end, module, error_buf,
  5231. error_buf_size))
  5232. return false;
  5233. break;
  5234. #if WASM_ENABLE_TAGS != 0
  5235. case SECTION_TYPE_TAG:
  5236. /* load tag declaration section */
  5237. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5238. module, error_buf, error_buf_size))
  5239. return false;
  5240. break;
  5241. #endif
  5242. case SECTION_TYPE_GLOBAL:
  5243. if (!load_global_section(buf, buf_end, module, error_buf,
  5244. error_buf_size))
  5245. return false;
  5246. break;
  5247. case SECTION_TYPE_EXPORT:
  5248. if (!load_export_section(buf, buf_end, module,
  5249. reuse_const_strings, error_buf,
  5250. error_buf_size))
  5251. return false;
  5252. break;
  5253. case SECTION_TYPE_START:
  5254. if (!load_start_section(buf, buf_end, module, error_buf,
  5255. error_buf_size))
  5256. return false;
  5257. break;
  5258. case SECTION_TYPE_ELEM:
  5259. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5260. error_buf_size))
  5261. return false;
  5262. break;
  5263. case SECTION_TYPE_CODE:
  5264. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5265. module, error_buf, error_buf_size))
  5266. return false;
  5267. break;
  5268. case SECTION_TYPE_DATA:
  5269. if (!load_data_segment_section(buf, buf_end, module,
  5270. #if WASM_ENABLE_BULK_MEMORY != 0
  5271. has_datacount_section,
  5272. #endif
  5273. clone_data_seg, error_buf,
  5274. error_buf_size))
  5275. return false;
  5276. break;
  5277. #if WASM_ENABLE_BULK_MEMORY != 0
  5278. case SECTION_TYPE_DATACOUNT:
  5279. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5280. error_buf_size))
  5281. return false;
  5282. has_datacount_section = true;
  5283. break;
  5284. #endif
  5285. #if WASM_ENABLE_STRINGREF != 0
  5286. case SECTION_TYPE_STRINGREF:
  5287. if (!load_stringref_section(buf, buf_end, module,
  5288. reuse_const_strings, error_buf,
  5289. error_buf_size))
  5290. return false;
  5291. break;
  5292. #endif
  5293. default:
  5294. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5295. return false;
  5296. }
  5297. section = section->next;
  5298. }
  5299. #if WASM_ENABLE_BULK_MEMORY != 0
  5300. if (!check_data_count_consistency(
  5301. has_datacount_section, module->data_seg_count1,
  5302. module->data_seg_count, error_buf, error_buf_size)) {
  5303. return false;
  5304. }
  5305. #endif
  5306. module->aux_data_end_global_index = (uint32)-1;
  5307. module->aux_heap_base_global_index = (uint32)-1;
  5308. module->aux_stack_top_global_index = (uint32)-1;
  5309. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5310. export = module->exports;
  5311. for (i = 0; i < module->export_count; i++, export ++) {
  5312. if (export->kind == EXPORT_KIND_GLOBAL) {
  5313. if (!strcmp(export->name, "__heap_base")) {
  5314. if (export->index < module->import_global_count) {
  5315. LOG_DEBUG("Skip the process if __heap_base is imported "
  5316. "instead of being a local global");
  5317. continue;
  5318. }
  5319. /* only process linker-generated symbols */
  5320. global_index = export->index - module->import_global_count;
  5321. global = module->globals + global_index;
  5322. if (global->type.val_type == VALUE_TYPE_I32
  5323. && !global->type.is_mutable
  5324. && global->init_expr.init_expr_type
  5325. == INIT_EXPR_TYPE_I32_CONST) {
  5326. aux_heap_base_global = global;
  5327. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5328. aux_heap_base_global_index = export->index;
  5329. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5330. aux_heap_base);
  5331. }
  5332. }
  5333. else if (!strcmp(export->name, "__data_end")) {
  5334. if (export->index < module->import_global_count) {
  5335. LOG_DEBUG("Skip the process if __data_end is imported "
  5336. "instead of being a local global");
  5337. continue;
  5338. }
  5339. /* only process linker-generated symbols */
  5340. global_index = export->index - module->import_global_count;
  5341. global = module->globals + global_index;
  5342. if (global->type.val_type == VALUE_TYPE_I32
  5343. && !global->type.is_mutable
  5344. && global->init_expr.init_expr_type
  5345. == INIT_EXPR_TYPE_I32_CONST) {
  5346. aux_data_end_global = global;
  5347. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5348. aux_data_end_global_index = export->index;
  5349. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5350. aux_data_end);
  5351. aux_data_end = align_uint64(aux_data_end, 16);
  5352. }
  5353. }
  5354. /* For module compiled with -pthread option, the global is:
  5355. [0] stack_top <-- 0
  5356. [1] tls_pointer
  5357. [2] tls_size
  5358. [3] data_end <-- 3
  5359. [4] global_base
  5360. [5] heap_base <-- 5
  5361. [6] dso_handle
  5362. For module compiled without -pthread option:
  5363. [0] stack_top <-- 0
  5364. [1] data_end <-- 1
  5365. [2] global_base
  5366. [3] heap_base <-- 3
  5367. [4] dso_handle
  5368. */
  5369. if (aux_data_end_global && aux_heap_base_global
  5370. && aux_data_end <= aux_heap_base) {
  5371. module->aux_data_end_global_index = aux_data_end_global_index;
  5372. module->aux_data_end = aux_data_end;
  5373. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5374. module->aux_heap_base = aux_heap_base;
  5375. /* Resolve aux stack top global */
  5376. for (global_index = 0; global_index < module->global_count;
  5377. global_index++) {
  5378. global = module->globals + global_index;
  5379. if (global->type.is_mutable /* heap_base and data_end is
  5380. not mutable */
  5381. && global->type.val_type == VALUE_TYPE_I32
  5382. && global->init_expr.init_expr_type
  5383. == INIT_EXPR_TYPE_I32_CONST
  5384. && (uint64)(uint32)global->init_expr.u.i32
  5385. <= aux_heap_base) {
  5386. aux_stack_top_global = global;
  5387. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5388. module->aux_stack_top_global_index =
  5389. module->import_global_count + global_index;
  5390. module->aux_stack_bottom = aux_stack_top;
  5391. module->aux_stack_size =
  5392. aux_stack_top > aux_data_end
  5393. ? (uint32)(aux_stack_top - aux_data_end)
  5394. : (uint32)aux_stack_top;
  5395. LOG_VERBOSE(
  5396. "Found aux stack top global, value: %" PRIu64 ", "
  5397. "global index: %d, stack size: %d",
  5398. aux_stack_top, global_index,
  5399. module->aux_stack_size);
  5400. break;
  5401. }
  5402. }
  5403. if (!aux_stack_top_global) {
  5404. /* Auxiliary stack global isn't found, it must be unused
  5405. in the wasm app, as if it is used, the global must be
  5406. defined. Here we set it to __heap_base global and set
  5407. its size to 0. */
  5408. aux_stack_top_global = aux_heap_base_global;
  5409. aux_stack_top = aux_heap_base;
  5410. module->aux_stack_top_global_index =
  5411. module->aux_heap_base_global_index;
  5412. module->aux_stack_bottom = aux_stack_top;
  5413. module->aux_stack_size = 0;
  5414. }
  5415. break;
  5416. }
  5417. }
  5418. }
  5419. module->malloc_function = (uint32)-1;
  5420. module->free_function = (uint32)-1;
  5421. module->retain_function = (uint32)-1;
  5422. /* Resolve malloc/free function exported by wasm module */
  5423. #if WASM_ENABLE_MEMORY64 != 0
  5424. if (has_module_memory64(module))
  5425. malloc_free_io_type = VALUE_TYPE_I64;
  5426. #endif
  5427. export = module->exports;
  5428. for (i = 0; i < module->export_count; i++, export ++) {
  5429. if (export->kind == EXPORT_KIND_FUNC) {
  5430. if (!strcmp(export->name, "malloc")
  5431. && export->index >= module->import_function_count) {
  5432. func_index = export->index - module->import_function_count;
  5433. func_type = module->functions[func_index]->func_type;
  5434. if (func_type->param_count == 1 && func_type->result_count == 1
  5435. && func_type->types[0] == malloc_free_io_type
  5436. && func_type->types[1] == malloc_free_io_type) {
  5437. bh_assert(module->malloc_function == (uint32)-1);
  5438. module->malloc_function = export->index;
  5439. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5440. export->name, export->index);
  5441. }
  5442. }
  5443. else if (!strcmp(export->name, "__new")
  5444. && export->index >= module->import_function_count) {
  5445. /* __new && __pin for AssemblyScript */
  5446. func_index = export->index - module->import_function_count;
  5447. func_type = module->functions[func_index]->func_type;
  5448. if (func_type->param_count == 2 && func_type->result_count == 1
  5449. && func_type->types[0] == malloc_free_io_type
  5450. && func_type->types[1] == VALUE_TYPE_I32
  5451. && func_type->types[2] == malloc_free_io_type) {
  5452. uint32 j;
  5453. WASMExport *export_tmp;
  5454. bh_assert(module->malloc_function == (uint32)-1);
  5455. module->malloc_function = export->index;
  5456. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5457. export->name, export->index);
  5458. /* resolve retain function.
  5459. If not found, reset malloc function index */
  5460. export_tmp = module->exports;
  5461. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5462. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5463. && (!strcmp(export_tmp->name, "__retain")
  5464. || (!strcmp(export_tmp->name, "__pin")))
  5465. && (export_tmp->index
  5466. >= module->import_function_count)) {
  5467. func_index = export_tmp->index
  5468. - module->import_function_count;
  5469. func_type =
  5470. module->functions[func_index]->func_type;
  5471. if (func_type->param_count == 1
  5472. && func_type->result_count == 1
  5473. && func_type->types[0] == malloc_free_io_type
  5474. && func_type->types[1] == malloc_free_io_type) {
  5475. bh_assert(module->retain_function
  5476. == (uint32)-1);
  5477. module->retain_function = export_tmp->index;
  5478. LOG_VERBOSE("Found retain function, name: %s, "
  5479. "index: %u",
  5480. export_tmp->name,
  5481. export_tmp->index);
  5482. break;
  5483. }
  5484. }
  5485. }
  5486. if (j == module->export_count) {
  5487. module->malloc_function = (uint32)-1;
  5488. LOG_VERBOSE("Can't find retain function,"
  5489. "reset malloc function index to -1");
  5490. }
  5491. }
  5492. }
  5493. else if (((!strcmp(export->name, "free"))
  5494. || (!strcmp(export->name, "__release"))
  5495. || (!strcmp(export->name, "__unpin")))
  5496. && export->index >= module->import_function_count) {
  5497. func_index = export->index - module->import_function_count;
  5498. func_type = module->functions[func_index]->func_type;
  5499. if (func_type->param_count == 1 && func_type->result_count == 0
  5500. && func_type->types[0] == malloc_free_io_type) {
  5501. bh_assert(module->free_function == (uint32)-1);
  5502. module->free_function = export->index;
  5503. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5504. export->name, export->index);
  5505. }
  5506. }
  5507. }
  5508. }
  5509. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5510. handle_table = wasm_interp_get_handle_table();
  5511. #endif
  5512. for (i = 0; i < module->function_count; i++) {
  5513. WASMFunction *func = module->functions[i];
  5514. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5515. error_buf_size)) {
  5516. return false;
  5517. }
  5518. if (i == module->function_count - 1
  5519. && func->code + func->code_size != buf_code_end) {
  5520. set_error_buf(error_buf, error_buf_size,
  5521. "code section size mismatch");
  5522. return false;
  5523. }
  5524. }
  5525. if (!module->possible_memory_grow) {
  5526. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5527. if (aux_data_end_global && aux_heap_base_global
  5528. && aux_stack_top_global) {
  5529. uint64 init_memory_size;
  5530. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5531. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5532. * valid range of uint32 */
  5533. if (shrunk_memory_size <= UINT32_MAX) {
  5534. if (module->import_memory_count) {
  5535. WASMMemoryImport *memory_import =
  5536. &module->import_memories[0].u.memory;
  5537. init_memory_size =
  5538. (uint64)memory_import->mem_type.num_bytes_per_page
  5539. * memory_import->mem_type.init_page_count;
  5540. if (shrunk_memory_size <= init_memory_size) {
  5541. /* Reset memory info to decrease memory usage */
  5542. memory_import->mem_type.num_bytes_per_page =
  5543. (uint32)shrunk_memory_size;
  5544. memory_import->mem_type.init_page_count = 1;
  5545. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5546. shrunk_memory_size);
  5547. }
  5548. }
  5549. if (module->memory_count) {
  5550. WASMMemory *memory = &module->memories[0];
  5551. init_memory_size = (uint64)memory->num_bytes_per_page
  5552. * memory->init_page_count;
  5553. if (shrunk_memory_size <= init_memory_size) {
  5554. /* Reset memory info to decrease memory usage */
  5555. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5556. memory->init_page_count = 1;
  5557. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5558. shrunk_memory_size);
  5559. }
  5560. }
  5561. }
  5562. }
  5563. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  5564. #if WASM_ENABLE_MULTI_MODULE == 0
  5565. if (module->import_memory_count) {
  5566. WASMMemoryImport *memory_import =
  5567. &module->import_memories[0].u.memory;
  5568. /* Only resize the memory to one big page if num_bytes_per_page is
  5569. * in valid range of uint32 */
  5570. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5571. memory_import->mem_type.num_bytes_per_page *=
  5572. memory_import->mem_type.init_page_count;
  5573. if (memory_import->mem_type.init_page_count > 0)
  5574. memory_import->mem_type.init_page_count =
  5575. memory_import->mem_type.max_page_count = 1;
  5576. else
  5577. memory_import->mem_type.init_page_count =
  5578. memory_import->mem_type.max_page_count = 0;
  5579. }
  5580. }
  5581. if (module->memory_count) {
  5582. WASMMemory *memory = &module->memories[0];
  5583. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5584. * valid range of uint32 */
  5585. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5586. memory->num_bytes_per_page *= memory->init_page_count;
  5587. if (memory->init_page_count > 0)
  5588. memory->init_page_count = memory->max_page_count = 1;
  5589. else
  5590. memory->init_page_count = memory->max_page_count = 0;
  5591. }
  5592. }
  5593. #endif
  5594. }
  5595. #if WASM_ENABLE_MEMORY64 != 0
  5596. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5597. false))
  5598. return false;
  5599. #endif
  5600. calculate_global_data_offset(module);
  5601. #if WASM_ENABLE_FAST_JIT != 0
  5602. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5603. return false;
  5604. }
  5605. #endif
  5606. #if WASM_ENABLE_JIT != 0
  5607. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5608. return false;
  5609. }
  5610. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5611. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5612. return false;
  5613. }
  5614. #else
  5615. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5616. thread fast jit execution, since applying llvm optimizations in
  5617. aot_compile_wasm may cost a lot of time.
  5618. Create thread with enough native stack to apply llvm optimizations */
  5619. if (os_thread_create(&module->llvm_jit_init_thread,
  5620. init_llvm_jit_functions_stage2_callback,
  5621. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5622. != 0) {
  5623. set_error_buf(error_buf, error_buf_size,
  5624. "create orcjit compile thread failed");
  5625. return false;
  5626. }
  5627. #endif
  5628. #endif
  5629. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5630. /* Create threads to compile the jit functions */
  5631. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5632. return false;
  5633. }
  5634. #endif
  5635. #if WASM_ENABLE_MEMORY_TRACING != 0
  5636. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5637. #endif
  5638. return true;
  5639. }
  5640. static WASMModule *
  5641. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5642. {
  5643. WASMModule *module =
  5644. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5645. bh_list_status ret;
  5646. if (!module) {
  5647. return NULL;
  5648. }
  5649. module->module_type = Wasm_Module_Bytecode;
  5650. /* Set start_function to -1, means no start function */
  5651. module->start_function = (uint32)-1;
  5652. module->name = name;
  5653. module->is_binary_freeable = false;
  5654. #if WASM_ENABLE_FAST_INTERP == 0
  5655. module->br_table_cache_list = &module->br_table_cache_list_head;
  5656. ret = bh_list_init(module->br_table_cache_list);
  5657. bh_assert(ret == BH_LIST_SUCCESS);
  5658. #endif
  5659. #if WASM_ENABLE_MULTI_MODULE != 0
  5660. module->import_module_list = &module->import_module_list_head;
  5661. ret = bh_list_init(module->import_module_list);
  5662. bh_assert(ret == BH_LIST_SUCCESS);
  5663. #endif
  5664. #if WASM_ENABLE_DEBUG_INTERP != 0
  5665. ret = bh_list_init(&module->fast_opcode_list);
  5666. bh_assert(ret == BH_LIST_SUCCESS);
  5667. #endif
  5668. #if WASM_ENABLE_GC != 0
  5669. if (!(module->ref_type_set =
  5670. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5671. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5672. goto fail1;
  5673. }
  5674. if (os_mutex_init(&module->rtt_type_lock)) {
  5675. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5676. goto fail2;
  5677. }
  5678. #endif
  5679. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5680. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5681. && WASM_ENABLE_LAZY_JIT != 0)
  5682. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5683. set_error_buf(error_buf, error_buf_size,
  5684. "init instance list lock failed");
  5685. goto fail3;
  5686. }
  5687. #endif
  5688. (void)ret;
  5689. return module;
  5690. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5691. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5692. && WASM_ENABLE_LAZY_JIT != 0)
  5693. fail3:
  5694. #endif
  5695. #if WASM_ENABLE_GC != 0
  5696. os_mutex_destroy(&module->rtt_type_lock);
  5697. fail2:
  5698. bh_hash_map_destroy(module->ref_type_set);
  5699. fail1:
  5700. #endif
  5701. wasm_runtime_free(module);
  5702. return NULL;
  5703. }
  5704. #if WASM_ENABLE_DEBUG_INTERP != 0
  5705. static bool
  5706. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5707. uint32 error_buf_size)
  5708. {
  5709. WASMFastOPCodeNode *fast_op =
  5710. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5711. if (fast_op) {
  5712. fast_op->offset = pos - module->load_addr;
  5713. fast_op->orig_op = orig_op;
  5714. bh_list_insert(&module->fast_opcode_list, fast_op);
  5715. }
  5716. return fast_op ? true : false;
  5717. }
  5718. #endif
  5719. WASMModule *
  5720. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5721. uint32 error_buf_size)
  5722. {
  5723. WASMModule *module = create_module("", error_buf, error_buf_size);
  5724. if (!module)
  5725. return NULL;
  5726. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  5727. error_buf_size)) {
  5728. wasm_loader_unload(module);
  5729. return NULL;
  5730. }
  5731. LOG_VERBOSE("Load module from sections success.\n");
  5732. return module;
  5733. }
  5734. static void
  5735. destroy_sections(WASMSection *section_list)
  5736. {
  5737. WASMSection *section = section_list, *next;
  5738. while (section) {
  5739. next = section->next;
  5740. wasm_runtime_free(section);
  5741. section = next;
  5742. }
  5743. }
  5744. /* clang-format off */
  5745. static uint8 section_ids[] = {
  5746. SECTION_TYPE_USER,
  5747. SECTION_TYPE_TYPE,
  5748. SECTION_TYPE_IMPORT,
  5749. SECTION_TYPE_FUNC,
  5750. SECTION_TYPE_TABLE,
  5751. SECTION_TYPE_MEMORY,
  5752. #if WASM_ENABLE_TAGS != 0
  5753. SECTION_TYPE_TAG,
  5754. #endif
  5755. #if WASM_ENABLE_STRINGREF != 0
  5756. /* must immediately precede the global section,
  5757. or where the global section would be */
  5758. SECTION_TYPE_STRINGREF,
  5759. #endif
  5760. SECTION_TYPE_GLOBAL,
  5761. SECTION_TYPE_EXPORT,
  5762. SECTION_TYPE_START,
  5763. SECTION_TYPE_ELEM,
  5764. #if WASM_ENABLE_BULK_MEMORY != 0
  5765. SECTION_TYPE_DATACOUNT,
  5766. #endif
  5767. SECTION_TYPE_CODE,
  5768. SECTION_TYPE_DATA
  5769. };
  5770. /* clang-format on */
  5771. static uint8
  5772. get_section_index(uint8 section_type)
  5773. {
  5774. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5775. for (uint8 i = 0; i < max_id; i++) {
  5776. if (section_type == section_ids[i])
  5777. return i;
  5778. }
  5779. return (uint8)-1;
  5780. }
  5781. static bool
  5782. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5783. char *error_buf, uint32 error_buf_size)
  5784. {
  5785. WASMSection *section_list_end = NULL, *section;
  5786. const uint8 *p = buf, *p_end = buf + size;
  5787. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5788. uint32 section_size;
  5789. bh_assert(!*p_section_list);
  5790. p += 8;
  5791. while (p < p_end) {
  5792. CHECK_BUF(p, p_end, 1);
  5793. section_type = read_uint8(p);
  5794. section_index = get_section_index(section_type);
  5795. if (section_index != (uint8)-1) {
  5796. if (section_type != SECTION_TYPE_USER) {
  5797. /* Custom sections may be inserted at any place,
  5798. while other sections must occur at most once
  5799. and in prescribed order. */
  5800. if (last_section_index != (uint8)-1
  5801. && (section_index <= last_section_index)) {
  5802. set_error_buf(error_buf, error_buf_size,
  5803. "unexpected content after last section or "
  5804. "junk after last section");
  5805. return false;
  5806. }
  5807. last_section_index = section_index;
  5808. }
  5809. read_leb_uint32(p, p_end, section_size);
  5810. CHECK_BUF1(p, p_end, section_size);
  5811. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5812. error_buf_size))) {
  5813. return false;
  5814. }
  5815. section->section_type = section_type;
  5816. section->section_body = (uint8 *)p;
  5817. section->section_body_size = section_size;
  5818. if (!section_list_end)
  5819. *p_section_list = section_list_end = section;
  5820. else {
  5821. section_list_end->next = section;
  5822. section_list_end = section;
  5823. }
  5824. p += section_size;
  5825. }
  5826. else {
  5827. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5828. return false;
  5829. }
  5830. }
  5831. return true;
  5832. fail:
  5833. return false;
  5834. }
  5835. static void
  5836. exchange32(uint8 *p_data)
  5837. {
  5838. uint8 value = *p_data;
  5839. *p_data = *(p_data + 3);
  5840. *(p_data + 3) = value;
  5841. value = *(p_data + 1);
  5842. *(p_data + 1) = *(p_data + 2);
  5843. *(p_data + 2) = value;
  5844. }
  5845. static union {
  5846. int a;
  5847. char b;
  5848. } __ue = { .a = 1 };
  5849. #define is_little_endian() (__ue.b == 1)
  5850. static bool
  5851. load(const uint8 *buf, uint32 size, WASMModule *module,
  5852. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  5853. uint32 error_buf_size)
  5854. {
  5855. const uint8 *buf_end = buf + size;
  5856. const uint8 *p = buf, *p_end = buf_end;
  5857. uint32 magic_number, version;
  5858. WASMSection *section_list = NULL;
  5859. CHECK_BUF1(p, p_end, sizeof(uint32));
  5860. magic_number = read_uint32(p);
  5861. if (!is_little_endian())
  5862. exchange32((uint8 *)&magic_number);
  5863. if (magic_number != WASM_MAGIC_NUMBER) {
  5864. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5865. return false;
  5866. }
  5867. CHECK_BUF1(p, p_end, sizeof(uint32));
  5868. version = read_uint32(p);
  5869. if (!is_little_endian())
  5870. exchange32((uint8 *)&version);
  5871. if (version != WASM_CURRENT_VERSION) {
  5872. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5873. return false;
  5874. }
  5875. module->package_version = version;
  5876. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5877. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5878. no_resolve, error_buf, error_buf_size)) {
  5879. destroy_sections(section_list);
  5880. return false;
  5881. }
  5882. destroy_sections(section_list);
  5883. return true;
  5884. fail:
  5885. return false;
  5886. }
  5887. #if WASM_ENABLE_LIBC_WASI != 0
  5888. /**
  5889. * refer to
  5890. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5891. */
  5892. static bool
  5893. check_wasi_abi_compatibility(const WASMModule *module,
  5894. #if WASM_ENABLE_MULTI_MODULE != 0
  5895. bool main_module,
  5896. #endif
  5897. char *error_buf, uint32 error_buf_size)
  5898. {
  5899. /**
  5900. * be careful with:
  5901. * wasi compatible modules(command/reactor) which don't import any wasi
  5902. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5903. * reactor can depend on nothing. At the same time, each has its own entry
  5904. * point.
  5905. *
  5906. * observations:
  5907. * - clang always injects `_start` into a command
  5908. * - clang always injects `_initialize` into a reactor
  5909. * - `iwasm -f` allows to run a function in the reactor
  5910. *
  5911. * strong assumptions:
  5912. * - no one will define either `_start` or `_initialize` on purpose
  5913. * - `_start` should always be `void _start(void)`
  5914. * - `_initialize` should always be `void _initialize(void)`
  5915. *
  5916. */
  5917. /* clang-format off */
  5918. /**
  5919. *
  5920. * | | import_wasi_api True | | import_wasi_api False | |
  5921. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5922. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5923. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5924. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5925. */
  5926. /* clang-format on */
  5927. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5928. uint32 import_function_count = module->import_function_count;
  5929. WASMFuncType *func_type;
  5930. /* (func (export "_start") (...) */
  5931. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5932. error_buf, error_buf_size);
  5933. if (start) {
  5934. if (start->index < import_function_count) {
  5935. set_error_buf(
  5936. error_buf, error_buf_size,
  5937. "the builtin _start function can not be an import function");
  5938. return false;
  5939. }
  5940. func_type =
  5941. module->functions[start->index - import_function_count]->func_type;
  5942. if (func_type->param_count || func_type->result_count) {
  5943. set_error_buf(error_buf, error_buf_size,
  5944. "the signature of builtin _start function is wrong");
  5945. return false;
  5946. }
  5947. }
  5948. else {
  5949. /* (func (export "_initialize") (...) */
  5950. initialize =
  5951. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5952. error_buf, error_buf_size);
  5953. if (initialize) {
  5954. if (initialize->index < import_function_count) {
  5955. set_error_buf(error_buf, error_buf_size,
  5956. "the builtin _initialize function can not be an "
  5957. "import function");
  5958. return false;
  5959. }
  5960. func_type =
  5961. module->functions[initialize->index - import_function_count]
  5962. ->func_type;
  5963. if (func_type->param_count || func_type->result_count) {
  5964. set_error_buf(
  5965. error_buf, error_buf_size,
  5966. "the signature of builtin _initialize function is wrong");
  5967. return false;
  5968. }
  5969. }
  5970. }
  5971. /* filter out non-wasi compatible modules */
  5972. if (!module->import_wasi_api && !start && !initialize) {
  5973. return true;
  5974. }
  5975. /* should have one at least */
  5976. if (module->import_wasi_api && !start && !initialize) {
  5977. LOG_WARNING("warning: a module with WASI apis should be either "
  5978. "a command or a reactor");
  5979. }
  5980. /*
  5981. * there is at least one of `_start` and `_initialize` in below cases.
  5982. * according to the assumption, they should be all wasi compatible
  5983. */
  5984. #if WASM_ENABLE_MULTI_MODULE != 0
  5985. /* filter out commands (with `_start`) cases */
  5986. if (start && !main_module) {
  5987. set_error_buf(
  5988. error_buf, error_buf_size,
  5989. "a command (with _start function) can not be a sub-module");
  5990. return false;
  5991. }
  5992. #endif
  5993. /*
  5994. * it is ok a reactor acts as a main module,
  5995. * so skip the check about (with `_initialize`)
  5996. */
  5997. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5998. error_buf, error_buf_size);
  5999. if (!memory
  6000. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  6001. /*
  6002. * with wasi-threads, it's still an open question if a memory
  6003. * should be exported.
  6004. *
  6005. * https://github.com/WebAssembly/wasi-threads/issues/22
  6006. * https://github.com/WebAssembly/WASI/issues/502
  6007. *
  6008. * Note: this code assumes the number of memories is at most 1.
  6009. */
  6010. && module->import_memory_count == 0
  6011. #endif
  6012. ) {
  6013. set_error_buf(error_buf, error_buf_size,
  6014. "a module with WASI apis must export memory by default");
  6015. return false;
  6016. }
  6017. return true;
  6018. }
  6019. #endif
  6020. WASMModule *
  6021. wasm_loader_load(uint8 *buf, uint32 size,
  6022. #if WASM_ENABLE_MULTI_MODULE != 0
  6023. bool main_module,
  6024. #endif
  6025. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6026. {
  6027. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6028. if (!module) {
  6029. return NULL;
  6030. }
  6031. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6032. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6033. module->load_addr = (uint8 *)buf;
  6034. module->load_size = size;
  6035. #endif
  6036. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6037. error_buf, error_buf_size)) {
  6038. goto fail;
  6039. }
  6040. #if WASM_ENABLE_LIBC_WASI != 0
  6041. /* Check the WASI application ABI */
  6042. if (!check_wasi_abi_compatibility(module,
  6043. #if WASM_ENABLE_MULTI_MODULE != 0
  6044. main_module,
  6045. #endif
  6046. error_buf, error_buf_size)) {
  6047. goto fail;
  6048. }
  6049. #endif
  6050. LOG_VERBOSE("Load module success.\n");
  6051. return module;
  6052. fail:
  6053. wasm_loader_unload(module);
  6054. return NULL;
  6055. }
  6056. void
  6057. wasm_loader_unload(WASMModule *module)
  6058. {
  6059. uint32 i;
  6060. if (!module)
  6061. return;
  6062. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6063. && WASM_ENABLE_LAZY_JIT != 0
  6064. module->orcjit_stop_compiling = true;
  6065. if (module->llvm_jit_init_thread)
  6066. os_thread_join(module->llvm_jit_init_thread, NULL);
  6067. #endif
  6068. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6069. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6070. module internal data after they were freed */
  6071. orcjit_stop_compile_threads(module);
  6072. #endif
  6073. #if WASM_ENABLE_JIT != 0
  6074. if (module->func_ptrs)
  6075. wasm_runtime_free(module->func_ptrs);
  6076. if (module->comp_ctx)
  6077. aot_destroy_comp_context(module->comp_ctx);
  6078. if (module->comp_data)
  6079. aot_destroy_comp_data(module->comp_data);
  6080. #endif
  6081. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6082. && WASM_ENABLE_LAZY_JIT != 0
  6083. if (module->tierup_wait_lock_inited) {
  6084. os_mutex_destroy(&module->tierup_wait_lock);
  6085. os_cond_destroy(&module->tierup_wait_cond);
  6086. }
  6087. #endif
  6088. if (module->imports)
  6089. wasm_runtime_free(module->imports);
  6090. if (module->functions) {
  6091. for (i = 0; i < module->function_count; i++) {
  6092. if (module->functions[i]) {
  6093. if (module->functions[i]->local_offsets)
  6094. wasm_runtime_free(module->functions[i]->local_offsets);
  6095. #if WASM_ENABLE_FAST_INTERP != 0
  6096. if (module->functions[i]->code_compiled)
  6097. wasm_runtime_free(module->functions[i]->code_compiled);
  6098. if (module->functions[i]->consts)
  6099. wasm_runtime_free(module->functions[i]->consts);
  6100. #endif
  6101. #if WASM_ENABLE_FAST_JIT != 0
  6102. if (module->functions[i]->fast_jit_jitted_code) {
  6103. jit_code_cache_free(
  6104. module->functions[i]->fast_jit_jitted_code);
  6105. }
  6106. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6107. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6108. jit_code_cache_free(
  6109. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6110. }
  6111. #endif
  6112. #endif
  6113. #if WASM_ENABLE_GC != 0
  6114. if (module->functions[i]->local_ref_type_maps) {
  6115. wasm_runtime_free(
  6116. module->functions[i]->local_ref_type_maps);
  6117. }
  6118. #endif
  6119. wasm_runtime_free(module->functions[i]);
  6120. }
  6121. }
  6122. wasm_runtime_free(module->functions);
  6123. }
  6124. if (module->tables) {
  6125. #if WASM_ENABLE_GC != 0
  6126. for (i = 0; i < module->table_count; i++) {
  6127. destroy_init_expr(module, &module->tables[i].init_expr);
  6128. }
  6129. #endif
  6130. wasm_runtime_free(module->tables);
  6131. }
  6132. if (module->memories)
  6133. wasm_runtime_free(module->memories);
  6134. if (module->globals) {
  6135. #if WASM_ENABLE_GC != 0
  6136. for (i = 0; i < module->global_count; i++) {
  6137. destroy_init_expr(module, &module->globals[i].init_expr);
  6138. }
  6139. #endif
  6140. wasm_runtime_free(module->globals);
  6141. }
  6142. #if WASM_ENABLE_TAGS != 0
  6143. if (module->tags) {
  6144. for (i = 0; i < module->tag_count; i++) {
  6145. if (module->tags[i])
  6146. wasm_runtime_free(module->tags[i]);
  6147. }
  6148. wasm_runtime_free(module->tags);
  6149. }
  6150. #endif
  6151. if (module->exports)
  6152. wasm_runtime_free(module->exports);
  6153. if (module->table_segments) {
  6154. for (i = 0; i < module->table_seg_count; i++) {
  6155. if (module->table_segments[i].init_values) {
  6156. #if WASM_ENABLE_GC != 0
  6157. uint32 j;
  6158. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6159. destroy_init_expr(
  6160. module, &module->table_segments[i].init_values[j]);
  6161. }
  6162. #endif
  6163. wasm_runtime_free(module->table_segments[i].init_values);
  6164. }
  6165. }
  6166. wasm_runtime_free(module->table_segments);
  6167. }
  6168. if (module->data_segments) {
  6169. for (i = 0; i < module->data_seg_count; i++) {
  6170. if (module->data_segments[i]) {
  6171. if (module->data_segments[i]->is_data_cloned)
  6172. wasm_runtime_free(module->data_segments[i]->data);
  6173. wasm_runtime_free(module->data_segments[i]);
  6174. }
  6175. }
  6176. wasm_runtime_free(module->data_segments);
  6177. }
  6178. if (module->types) {
  6179. for (i = 0; i < module->type_count; i++) {
  6180. if (module->types[i])
  6181. destroy_wasm_type(module->types[i]);
  6182. }
  6183. wasm_runtime_free(module->types);
  6184. }
  6185. if (module->const_str_list) {
  6186. StringNode *node = module->const_str_list, *node_next;
  6187. while (node) {
  6188. node_next = node->next;
  6189. wasm_runtime_free(node);
  6190. node = node_next;
  6191. }
  6192. }
  6193. #if WASM_ENABLE_STRINGREF != 0
  6194. if (module->string_literal_ptrs) {
  6195. wasm_runtime_free((void *)module->string_literal_ptrs);
  6196. }
  6197. if (module->string_literal_lengths) {
  6198. wasm_runtime_free(module->string_literal_lengths);
  6199. }
  6200. #endif
  6201. #if WASM_ENABLE_FAST_INTERP == 0
  6202. if (module->br_table_cache_list) {
  6203. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6204. BrTableCache *node_next;
  6205. while (node) {
  6206. node_next = bh_list_elem_next(node);
  6207. wasm_runtime_free(node);
  6208. node = node_next;
  6209. }
  6210. }
  6211. #endif
  6212. #if WASM_ENABLE_MULTI_MODULE != 0
  6213. /* just release the sub module list */
  6214. if (module->import_module_list) {
  6215. WASMRegisteredModule *node =
  6216. bh_list_first_elem(module->import_module_list);
  6217. while (node) {
  6218. WASMRegisteredModule *next = bh_list_elem_next(node);
  6219. bh_list_remove(module->import_module_list, node);
  6220. /*
  6221. * unload(sub_module) will be triggered during runtime_destroy().
  6222. * every module in the global module list will be unloaded one by
  6223. * one. so don't worry.
  6224. */
  6225. wasm_runtime_free(node);
  6226. /*
  6227. * the module file reading buffer will be released
  6228. * in runtime_destroy()
  6229. */
  6230. node = next;
  6231. }
  6232. }
  6233. #endif
  6234. #if WASM_ENABLE_DEBUG_INTERP != 0
  6235. WASMFastOPCodeNode *fast_opcode =
  6236. bh_list_first_elem(&module->fast_opcode_list);
  6237. while (fast_opcode) {
  6238. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6239. wasm_runtime_free(fast_opcode);
  6240. fast_opcode = next;
  6241. }
  6242. #endif
  6243. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6244. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6245. && WASM_ENABLE_LAZY_JIT != 0)
  6246. os_mutex_destroy(&module->instance_list_lock);
  6247. #endif
  6248. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6249. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6250. #endif
  6251. #if WASM_ENABLE_FAST_JIT != 0
  6252. if (module->fast_jit_func_ptrs) {
  6253. wasm_runtime_free(module->fast_jit_func_ptrs);
  6254. }
  6255. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6256. if (module->fast_jit_thread_locks_inited[i]) {
  6257. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6258. }
  6259. }
  6260. #endif
  6261. #if WASM_ENABLE_GC != 0
  6262. os_mutex_destroy(&module->rtt_type_lock);
  6263. bh_hash_map_destroy(module->ref_type_set);
  6264. if (module->rtt_types) {
  6265. for (i = 0; i < module->type_count; i++) {
  6266. if (module->rtt_types[i])
  6267. wasm_runtime_free(module->rtt_types[i]);
  6268. }
  6269. wasm_runtime_free(module->rtt_types);
  6270. }
  6271. #if WASM_ENABLE_STRINGREF != 0
  6272. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6273. if (module->stringref_rtts[i])
  6274. wasm_runtime_free(module->stringref_rtts[i]);
  6275. }
  6276. #endif
  6277. #endif
  6278. wasm_runtime_free(module);
  6279. }
  6280. bool
  6281. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6282. const uint8 *start_addr, const uint8 *code_end_addr,
  6283. uint8 label_type, uint8 **p_else_addr,
  6284. uint8 **p_end_addr)
  6285. {
  6286. const uint8 *p = start_addr, *p_end = code_end_addr;
  6287. uint8 *else_addr = NULL;
  6288. char error_buf[128];
  6289. uint32 block_nested_depth = 1, count, i, j, t;
  6290. uint32 error_buf_size = sizeof(error_buf);
  6291. uint8 opcode, u8;
  6292. BlockAddr block_stack[16] = { { 0 } }, *block;
  6293. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6294. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6295. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6296. if (block[j].start_addr == start_addr) {
  6297. /* Cache hit */
  6298. *p_else_addr = block[j].else_addr;
  6299. *p_end_addr = block[j].end_addr;
  6300. return true;
  6301. }
  6302. }
  6303. /* Cache unhit */
  6304. block_stack[0].start_addr = start_addr;
  6305. while (p < code_end_addr) {
  6306. opcode = *p++;
  6307. #if WASM_ENABLE_DEBUG_INTERP != 0
  6308. op_break_retry:
  6309. #endif
  6310. switch (opcode) {
  6311. case WASM_OP_UNREACHABLE:
  6312. case WASM_OP_NOP:
  6313. break;
  6314. #if WASM_ENABLE_EXCE_HANDLING != 0
  6315. case WASM_OP_TRY:
  6316. u8 = read_uint8(p);
  6317. if (block_nested_depth
  6318. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6319. block_stack[block_nested_depth].start_addr = p;
  6320. block_stack[block_nested_depth].else_addr = NULL;
  6321. }
  6322. block_nested_depth++;
  6323. break;
  6324. case EXT_OP_TRY:
  6325. skip_leb_uint32(p, p_end);
  6326. if (block_nested_depth
  6327. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6328. block_stack[block_nested_depth].start_addr = p;
  6329. block_stack[block_nested_depth].else_addr = NULL;
  6330. }
  6331. block_nested_depth++;
  6332. break;
  6333. case WASM_OP_CATCH:
  6334. if (block_nested_depth == 1) {
  6335. *p_end_addr = (uint8 *)(p - 1);
  6336. /* stop search and return the address of the catch block */
  6337. return true;
  6338. }
  6339. break;
  6340. case WASM_OP_CATCH_ALL:
  6341. if (block_nested_depth == 1) {
  6342. *p_end_addr = (uint8 *)(p - 1);
  6343. /* stop search and return the address of the catch_all block
  6344. */
  6345. return true;
  6346. }
  6347. break;
  6348. case WASM_OP_THROW:
  6349. /* skip tag_index */
  6350. skip_leb(p);
  6351. break;
  6352. case WASM_OP_RETHROW:
  6353. /* skip depth */
  6354. skip_leb(p);
  6355. break;
  6356. case WASM_OP_DELEGATE:
  6357. if (block_nested_depth == 1) {
  6358. *p_end_addr = (uint8 *)(p - 1);
  6359. return true;
  6360. }
  6361. else {
  6362. skip_leb(p);
  6363. /* the DELEGATE opcode ends the tryblock, */
  6364. block_nested_depth--;
  6365. if (block_nested_depth
  6366. < sizeof(block_stack) / sizeof(BlockAddr))
  6367. block_stack[block_nested_depth].end_addr =
  6368. (uint8 *)(p - 1);
  6369. }
  6370. break;
  6371. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6372. case WASM_OP_BLOCK:
  6373. case WASM_OP_LOOP:
  6374. case WASM_OP_IF:
  6375. {
  6376. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6377. u8 = read_uint8(p);
  6378. if (is_byte_a_type(u8)) {
  6379. #if WASM_ENABLE_GC != 0
  6380. if (wasm_is_type_multi_byte_type(u8)) {
  6381. /* the possible extra bytes of GC ref type have been
  6382. modified to OP_NOP, no need to resolve them again */
  6383. }
  6384. #endif
  6385. }
  6386. else {
  6387. p--;
  6388. /* block type */
  6389. skip_leb_int32(p, p_end);
  6390. }
  6391. if (block_nested_depth
  6392. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6393. block_stack[block_nested_depth].start_addr = p;
  6394. block_stack[block_nested_depth].else_addr = NULL;
  6395. }
  6396. block_nested_depth++;
  6397. break;
  6398. }
  6399. case EXT_OP_BLOCK:
  6400. case EXT_OP_LOOP:
  6401. case EXT_OP_IF:
  6402. /* block type */
  6403. skip_leb_int32(p, p_end);
  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. case WASM_OP_ELSE:
  6412. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6413. else_addr = (uint8 *)(p - 1);
  6414. if (block_nested_depth - 1
  6415. < sizeof(block_stack) / sizeof(BlockAddr))
  6416. block_stack[block_nested_depth - 1].else_addr =
  6417. (uint8 *)(p - 1);
  6418. break;
  6419. case WASM_OP_END:
  6420. if (block_nested_depth == 1) {
  6421. if (label_type == LABEL_TYPE_IF)
  6422. *p_else_addr = else_addr;
  6423. *p_end_addr = (uint8 *)(p - 1);
  6424. block_stack[0].end_addr = (uint8 *)(p - 1);
  6425. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6426. t++) {
  6427. start_addr = block_stack[t].start_addr;
  6428. if (start_addr) {
  6429. i = ((uintptr_t)start_addr)
  6430. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6431. block =
  6432. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6433. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6434. if (!block[j].start_addr)
  6435. break;
  6436. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6437. memmove(block + 1, block,
  6438. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6439. * sizeof(BlockAddr));
  6440. j = 0;
  6441. }
  6442. block[j].start_addr = block_stack[t].start_addr;
  6443. block[j].else_addr = block_stack[t].else_addr;
  6444. block[j].end_addr = block_stack[t].end_addr;
  6445. }
  6446. else
  6447. break;
  6448. }
  6449. return true;
  6450. }
  6451. else {
  6452. block_nested_depth--;
  6453. if (block_nested_depth
  6454. < sizeof(block_stack) / sizeof(BlockAddr))
  6455. block_stack[block_nested_depth].end_addr =
  6456. (uint8 *)(p - 1);
  6457. }
  6458. break;
  6459. case WASM_OP_BR:
  6460. case WASM_OP_BR_IF:
  6461. skip_leb_uint32(p, p_end); /* labelidx */
  6462. break;
  6463. case WASM_OP_BR_TABLE:
  6464. read_leb_uint32(p, p_end, count); /* label num */
  6465. #if WASM_ENABLE_FAST_INTERP != 0
  6466. for (i = 0; i <= count; i++) /* labelidxs */
  6467. skip_leb_uint32(p, p_end);
  6468. #else
  6469. p += count + 1;
  6470. while (*p == WASM_OP_NOP)
  6471. p++;
  6472. #endif
  6473. break;
  6474. #if WASM_ENABLE_FAST_INTERP == 0
  6475. case EXT_OP_BR_TABLE_CACHE:
  6476. read_leb_uint32(p, p_end, count); /* label num */
  6477. while (*p == WASM_OP_NOP)
  6478. p++;
  6479. break;
  6480. #endif
  6481. case WASM_OP_RETURN:
  6482. break;
  6483. case WASM_OP_CALL:
  6484. #if WASM_ENABLE_TAIL_CALL != 0
  6485. case WASM_OP_RETURN_CALL:
  6486. #endif
  6487. skip_leb_uint32(p, p_end); /* funcidx */
  6488. break;
  6489. case WASM_OP_CALL_INDIRECT:
  6490. #if WASM_ENABLE_TAIL_CALL != 0
  6491. case WASM_OP_RETURN_CALL_INDIRECT:
  6492. #endif
  6493. skip_leb_uint32(p, p_end); /* typeidx */
  6494. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6495. skip_leb_uint32(p, p_end); /* tableidx */
  6496. #else
  6497. u8 = read_uint8(p); /* 0x00 */
  6498. #endif
  6499. break;
  6500. #if WASM_ENABLE_GC != 0
  6501. case WASM_OP_CALL_REF:
  6502. case WASM_OP_RETURN_CALL_REF:
  6503. skip_leb_uint32(p, p_end); /* typeidx */
  6504. break;
  6505. #endif
  6506. case WASM_OP_DROP:
  6507. case WASM_OP_SELECT:
  6508. case WASM_OP_DROP_64:
  6509. case WASM_OP_SELECT_64:
  6510. break;
  6511. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6512. case WASM_OP_SELECT_T:
  6513. {
  6514. skip_leb_uint32(p, p_end); /* vec length */
  6515. u8 = read_uint8(p); /* typeidx */
  6516. /* the possible extra bytes of GC ref type have been
  6517. modified to OP_NOP, no need to resolve them again */
  6518. break;
  6519. }
  6520. case WASM_OP_TABLE_GET:
  6521. case WASM_OP_TABLE_SET:
  6522. skip_leb_uint32(p, p_end); /* table index */
  6523. break;
  6524. case WASM_OP_REF_NULL:
  6525. {
  6526. u8 = read_uint8(p); /* type */
  6527. if (is_byte_a_type(u8)) {
  6528. #if WASM_ENABLE_GC != 0
  6529. if (wasm_is_type_multi_byte_type(u8)) {
  6530. /* the possible extra bytes of GC ref type have been
  6531. modified to OP_NOP, no need to resolve them again */
  6532. }
  6533. #endif
  6534. }
  6535. else {
  6536. p--;
  6537. skip_leb_uint32(p, p_end);
  6538. }
  6539. break;
  6540. }
  6541. case WASM_OP_REF_IS_NULL:
  6542. break;
  6543. case WASM_OP_REF_FUNC:
  6544. skip_leb_uint32(p, p_end); /* func index */
  6545. break;
  6546. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6547. #if WASM_ENABLE_GC != 0
  6548. case WASM_OP_REF_AS_NON_NULL:
  6549. case WASM_OP_REF_EQ:
  6550. break;
  6551. case WASM_OP_BR_ON_NULL:
  6552. case WASM_OP_BR_ON_NON_NULL:
  6553. skip_leb_uint32(p, p_end); /* label index */
  6554. break;
  6555. #endif /* end of WASM_ENABLE_GC != 0 */
  6556. case WASM_OP_GET_LOCAL:
  6557. case WASM_OP_SET_LOCAL:
  6558. case WASM_OP_TEE_LOCAL:
  6559. case WASM_OP_GET_GLOBAL:
  6560. case WASM_OP_SET_GLOBAL:
  6561. case WASM_OP_GET_GLOBAL_64:
  6562. case WASM_OP_SET_GLOBAL_64:
  6563. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6564. skip_leb_uint32(p, p_end); /* local index */
  6565. break;
  6566. case EXT_OP_GET_LOCAL_FAST:
  6567. case EXT_OP_SET_LOCAL_FAST:
  6568. case EXT_OP_TEE_LOCAL_FAST:
  6569. CHECK_BUF(p, p_end, 1);
  6570. p++;
  6571. break;
  6572. case WASM_OP_I32_LOAD:
  6573. case WASM_OP_I64_LOAD:
  6574. case WASM_OP_F32_LOAD:
  6575. case WASM_OP_F64_LOAD:
  6576. case WASM_OP_I32_LOAD8_S:
  6577. case WASM_OP_I32_LOAD8_U:
  6578. case WASM_OP_I32_LOAD16_S:
  6579. case WASM_OP_I32_LOAD16_U:
  6580. case WASM_OP_I64_LOAD8_S:
  6581. case WASM_OP_I64_LOAD8_U:
  6582. case WASM_OP_I64_LOAD16_S:
  6583. case WASM_OP_I64_LOAD16_U:
  6584. case WASM_OP_I64_LOAD32_S:
  6585. case WASM_OP_I64_LOAD32_U:
  6586. case WASM_OP_I32_STORE:
  6587. case WASM_OP_I64_STORE:
  6588. case WASM_OP_F32_STORE:
  6589. case WASM_OP_F64_STORE:
  6590. case WASM_OP_I32_STORE8:
  6591. case WASM_OP_I32_STORE16:
  6592. case WASM_OP_I64_STORE8:
  6593. case WASM_OP_I64_STORE16:
  6594. case WASM_OP_I64_STORE32:
  6595. skip_leb_align(p, p_end); /* align */
  6596. skip_leb_mem_offset(p, p_end); /* offset */
  6597. break;
  6598. case WASM_OP_MEMORY_SIZE:
  6599. case WASM_OP_MEMORY_GROW:
  6600. skip_leb_memidx(p, p_end); /* memidx */
  6601. break;
  6602. case WASM_OP_I32_CONST:
  6603. skip_leb_int32(p, p_end);
  6604. break;
  6605. case WASM_OP_I64_CONST:
  6606. skip_leb_int64(p, p_end);
  6607. break;
  6608. case WASM_OP_F32_CONST:
  6609. p += sizeof(float32);
  6610. break;
  6611. case WASM_OP_F64_CONST:
  6612. p += sizeof(float64);
  6613. break;
  6614. case WASM_OP_I32_EQZ:
  6615. case WASM_OP_I32_EQ:
  6616. case WASM_OP_I32_NE:
  6617. case WASM_OP_I32_LT_S:
  6618. case WASM_OP_I32_LT_U:
  6619. case WASM_OP_I32_GT_S:
  6620. case WASM_OP_I32_GT_U:
  6621. case WASM_OP_I32_LE_S:
  6622. case WASM_OP_I32_LE_U:
  6623. case WASM_OP_I32_GE_S:
  6624. case WASM_OP_I32_GE_U:
  6625. case WASM_OP_I64_EQZ:
  6626. case WASM_OP_I64_EQ:
  6627. case WASM_OP_I64_NE:
  6628. case WASM_OP_I64_LT_S:
  6629. case WASM_OP_I64_LT_U:
  6630. case WASM_OP_I64_GT_S:
  6631. case WASM_OP_I64_GT_U:
  6632. case WASM_OP_I64_LE_S:
  6633. case WASM_OP_I64_LE_U:
  6634. case WASM_OP_I64_GE_S:
  6635. case WASM_OP_I64_GE_U:
  6636. case WASM_OP_F32_EQ:
  6637. case WASM_OP_F32_NE:
  6638. case WASM_OP_F32_LT:
  6639. case WASM_OP_F32_GT:
  6640. case WASM_OP_F32_LE:
  6641. case WASM_OP_F32_GE:
  6642. case WASM_OP_F64_EQ:
  6643. case WASM_OP_F64_NE:
  6644. case WASM_OP_F64_LT:
  6645. case WASM_OP_F64_GT:
  6646. case WASM_OP_F64_LE:
  6647. case WASM_OP_F64_GE:
  6648. case WASM_OP_I32_CLZ:
  6649. case WASM_OP_I32_CTZ:
  6650. case WASM_OP_I32_POPCNT:
  6651. case WASM_OP_I32_ADD:
  6652. case WASM_OP_I32_SUB:
  6653. case WASM_OP_I32_MUL:
  6654. case WASM_OP_I32_DIV_S:
  6655. case WASM_OP_I32_DIV_U:
  6656. case WASM_OP_I32_REM_S:
  6657. case WASM_OP_I32_REM_U:
  6658. case WASM_OP_I32_AND:
  6659. case WASM_OP_I32_OR:
  6660. case WASM_OP_I32_XOR:
  6661. case WASM_OP_I32_SHL:
  6662. case WASM_OP_I32_SHR_S:
  6663. case WASM_OP_I32_SHR_U:
  6664. case WASM_OP_I32_ROTL:
  6665. case WASM_OP_I32_ROTR:
  6666. case WASM_OP_I64_CLZ:
  6667. case WASM_OP_I64_CTZ:
  6668. case WASM_OP_I64_POPCNT:
  6669. case WASM_OP_I64_ADD:
  6670. case WASM_OP_I64_SUB:
  6671. case WASM_OP_I64_MUL:
  6672. case WASM_OP_I64_DIV_S:
  6673. case WASM_OP_I64_DIV_U:
  6674. case WASM_OP_I64_REM_S:
  6675. case WASM_OP_I64_REM_U:
  6676. case WASM_OP_I64_AND:
  6677. case WASM_OP_I64_OR:
  6678. case WASM_OP_I64_XOR:
  6679. case WASM_OP_I64_SHL:
  6680. case WASM_OP_I64_SHR_S:
  6681. case WASM_OP_I64_SHR_U:
  6682. case WASM_OP_I64_ROTL:
  6683. case WASM_OP_I64_ROTR:
  6684. case WASM_OP_F32_ABS:
  6685. case WASM_OP_F32_NEG:
  6686. case WASM_OP_F32_CEIL:
  6687. case WASM_OP_F32_FLOOR:
  6688. case WASM_OP_F32_TRUNC:
  6689. case WASM_OP_F32_NEAREST:
  6690. case WASM_OP_F32_SQRT:
  6691. case WASM_OP_F32_ADD:
  6692. case WASM_OP_F32_SUB:
  6693. case WASM_OP_F32_MUL:
  6694. case WASM_OP_F32_DIV:
  6695. case WASM_OP_F32_MIN:
  6696. case WASM_OP_F32_MAX:
  6697. case WASM_OP_F32_COPYSIGN:
  6698. case WASM_OP_F64_ABS:
  6699. case WASM_OP_F64_NEG:
  6700. case WASM_OP_F64_CEIL:
  6701. case WASM_OP_F64_FLOOR:
  6702. case WASM_OP_F64_TRUNC:
  6703. case WASM_OP_F64_NEAREST:
  6704. case WASM_OP_F64_SQRT:
  6705. case WASM_OP_F64_ADD:
  6706. case WASM_OP_F64_SUB:
  6707. case WASM_OP_F64_MUL:
  6708. case WASM_OP_F64_DIV:
  6709. case WASM_OP_F64_MIN:
  6710. case WASM_OP_F64_MAX:
  6711. case WASM_OP_F64_COPYSIGN:
  6712. case WASM_OP_I32_WRAP_I64:
  6713. case WASM_OP_I32_TRUNC_S_F32:
  6714. case WASM_OP_I32_TRUNC_U_F32:
  6715. case WASM_OP_I32_TRUNC_S_F64:
  6716. case WASM_OP_I32_TRUNC_U_F64:
  6717. case WASM_OP_I64_EXTEND_S_I32:
  6718. case WASM_OP_I64_EXTEND_U_I32:
  6719. case WASM_OP_I64_TRUNC_S_F32:
  6720. case WASM_OP_I64_TRUNC_U_F32:
  6721. case WASM_OP_I64_TRUNC_S_F64:
  6722. case WASM_OP_I64_TRUNC_U_F64:
  6723. case WASM_OP_F32_CONVERT_S_I32:
  6724. case WASM_OP_F32_CONVERT_U_I32:
  6725. case WASM_OP_F32_CONVERT_S_I64:
  6726. case WASM_OP_F32_CONVERT_U_I64:
  6727. case WASM_OP_F32_DEMOTE_F64:
  6728. case WASM_OP_F64_CONVERT_S_I32:
  6729. case WASM_OP_F64_CONVERT_U_I32:
  6730. case WASM_OP_F64_CONVERT_S_I64:
  6731. case WASM_OP_F64_CONVERT_U_I64:
  6732. case WASM_OP_F64_PROMOTE_F32:
  6733. case WASM_OP_I32_REINTERPRET_F32:
  6734. case WASM_OP_I64_REINTERPRET_F64:
  6735. case WASM_OP_F32_REINTERPRET_I32:
  6736. case WASM_OP_F64_REINTERPRET_I64:
  6737. case WASM_OP_I32_EXTEND8_S:
  6738. case WASM_OP_I32_EXTEND16_S:
  6739. case WASM_OP_I64_EXTEND8_S:
  6740. case WASM_OP_I64_EXTEND16_S:
  6741. case WASM_OP_I64_EXTEND32_S:
  6742. break;
  6743. #if WASM_ENABLE_GC != 0
  6744. case WASM_OP_GC_PREFIX:
  6745. {
  6746. uint32 opcode1;
  6747. read_leb_uint32(p, p_end, opcode1);
  6748. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6749. is no larger than UINT8_MAX */
  6750. opcode = (uint8)opcode1;
  6751. switch (opcode) {
  6752. case WASM_OP_STRUCT_NEW:
  6753. case WASM_OP_STRUCT_NEW_DEFAULT:
  6754. skip_leb_uint32(p, p_end); /* typeidx */
  6755. break;
  6756. case WASM_OP_STRUCT_GET:
  6757. case WASM_OP_STRUCT_GET_S:
  6758. case WASM_OP_STRUCT_GET_U:
  6759. case WASM_OP_STRUCT_SET:
  6760. skip_leb_uint32(p, p_end); /* typeidx */
  6761. skip_leb_uint32(p, p_end); /* fieldidx */
  6762. break;
  6763. case WASM_OP_ARRAY_NEW:
  6764. case WASM_OP_ARRAY_NEW_DEFAULT:
  6765. case WASM_OP_ARRAY_GET:
  6766. case WASM_OP_ARRAY_GET_S:
  6767. case WASM_OP_ARRAY_GET_U:
  6768. case WASM_OP_ARRAY_SET:
  6769. case WASM_OP_ARRAY_FILL:
  6770. skip_leb_uint32(p, p_end); /* typeidx */
  6771. break;
  6772. case WASM_OP_ARRAY_COPY:
  6773. skip_leb_uint32(p, p_end); /* typeidx1 */
  6774. skip_leb_uint32(p, p_end); /* typeidx2 */
  6775. break;
  6776. case WASM_OP_ARRAY_LEN:
  6777. break;
  6778. case WASM_OP_ARRAY_NEW_FIXED:
  6779. case WASM_OP_ARRAY_NEW_DATA:
  6780. case WASM_OP_ARRAY_NEW_ELEM:
  6781. skip_leb_uint32(p, p_end); /* typeidx */
  6782. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6783. break;
  6784. case WASM_OP_REF_I31:
  6785. case WASM_OP_I31_GET_S:
  6786. case WASM_OP_I31_GET_U:
  6787. break;
  6788. case WASM_OP_REF_TEST:
  6789. case WASM_OP_REF_CAST:
  6790. case WASM_OP_REF_TEST_NULLABLE:
  6791. case WASM_OP_REF_CAST_NULLABLE:
  6792. skip_leb_int32(p, p_end); /* heaptype */
  6793. break;
  6794. case WASM_OP_BR_ON_CAST:
  6795. case WASM_OP_BR_ON_CAST_FAIL:
  6796. p += sizeof(uint8); /* castflag */
  6797. skip_leb_uint32(p, p_end); /* labelidx */
  6798. skip_leb_int32(p, p_end); /* heaptype */
  6799. skip_leb_int32(p, p_end); /* heaptype2 */
  6800. break;
  6801. case WASM_OP_ANY_CONVERT_EXTERN:
  6802. case WASM_OP_EXTERN_CONVERT_ANY:
  6803. break;
  6804. #if WASM_ENABLE_STRINGREF != 0
  6805. case WASM_OP_STRING_NEW_UTF8:
  6806. case WASM_OP_STRING_NEW_WTF16:
  6807. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6808. case WASM_OP_STRING_NEW_WTF8:
  6809. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6810. break;
  6811. case WASM_OP_STRING_CONST:
  6812. skip_leb_int32(p, p_end); /* contents */
  6813. break;
  6814. case WASM_OP_STRING_MEASURE_UTF8:
  6815. case WASM_OP_STRING_MEASURE_WTF8:
  6816. case WASM_OP_STRING_MEASURE_WTF16:
  6817. break;
  6818. case WASM_OP_STRING_ENCODE_UTF8:
  6819. case WASM_OP_STRING_ENCODE_WTF16:
  6820. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6821. case WASM_OP_STRING_ENCODE_WTF8:
  6822. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6823. break;
  6824. case WASM_OP_STRING_CONCAT:
  6825. case WASM_OP_STRING_EQ:
  6826. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6827. case WASM_OP_STRING_AS_WTF8:
  6828. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6829. break;
  6830. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6831. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6832. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6833. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6834. break;
  6835. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6836. case WASM_OP_STRING_AS_WTF16:
  6837. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6838. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6839. break;
  6840. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6841. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6842. break;
  6843. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6844. case WASM_OP_STRING_AS_ITER:
  6845. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6846. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6847. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6848. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6849. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6850. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6851. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6852. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6853. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6854. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6855. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6856. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6857. break;
  6858. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6859. default:
  6860. return false;
  6861. }
  6862. break;
  6863. }
  6864. #endif /* end of WASM_ENABLE_GC != 0 */
  6865. case WASM_OP_MISC_PREFIX:
  6866. {
  6867. uint32 opcode1;
  6868. read_leb_uint32(p, p_end, opcode1);
  6869. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6870. is no larger than UINT8_MAX */
  6871. opcode = (uint8)opcode1;
  6872. switch (opcode) {
  6873. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6874. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6875. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6876. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6877. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6878. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6879. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6880. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6881. break;
  6882. #if WASM_ENABLE_BULK_MEMORY != 0
  6883. case WASM_OP_MEMORY_INIT:
  6884. skip_leb_uint32(p, p_end);
  6885. skip_leb_memidx(p, p_end);
  6886. break;
  6887. case WASM_OP_DATA_DROP:
  6888. skip_leb_uint32(p, p_end);
  6889. break;
  6890. case WASM_OP_MEMORY_COPY:
  6891. skip_leb_memidx(p, p_end);
  6892. skip_leb_memidx(p, p_end);
  6893. break;
  6894. case WASM_OP_MEMORY_FILL:
  6895. skip_leb_memidx(p, p_end);
  6896. break;
  6897. #endif /* WASM_ENABLE_BULK_MEMORY */
  6898. #if WASM_ENABLE_REF_TYPES != 0
  6899. case WASM_OP_TABLE_INIT:
  6900. case WASM_OP_TABLE_COPY:
  6901. /* tableidx */
  6902. skip_leb_uint32(p, p_end);
  6903. /* elemidx */
  6904. skip_leb_uint32(p, p_end);
  6905. break;
  6906. case WASM_OP_ELEM_DROP:
  6907. /* elemidx */
  6908. skip_leb_uint32(p, p_end);
  6909. break;
  6910. case WASM_OP_TABLE_SIZE:
  6911. case WASM_OP_TABLE_GROW:
  6912. case WASM_OP_TABLE_FILL:
  6913. skip_leb_uint32(p, p_end); /* table idx */
  6914. break;
  6915. #endif /* WASM_ENABLE_REF_TYPES */
  6916. default:
  6917. return false;
  6918. }
  6919. break;
  6920. }
  6921. #if WASM_ENABLE_SIMD != 0
  6922. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6923. case WASM_OP_SIMD_PREFIX:
  6924. {
  6925. uint32 opcode1;
  6926. read_leb_uint32(p, p_end, opcode1);
  6927. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6928. is no larger than UINT8_MAX */
  6929. opcode = (uint8)opcode1;
  6930. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6931. */
  6932. switch (opcode) {
  6933. case SIMD_v128_load:
  6934. case SIMD_v128_load8x8_s:
  6935. case SIMD_v128_load8x8_u:
  6936. case SIMD_v128_load16x4_s:
  6937. case SIMD_v128_load16x4_u:
  6938. case SIMD_v128_load32x2_s:
  6939. case SIMD_v128_load32x2_u:
  6940. case SIMD_v128_load8_splat:
  6941. case SIMD_v128_load16_splat:
  6942. case SIMD_v128_load32_splat:
  6943. case SIMD_v128_load64_splat:
  6944. case SIMD_v128_store:
  6945. /* memarg align */
  6946. skip_leb_uint32(p, p_end);
  6947. /* memarg offset */
  6948. skip_leb_mem_offset(p, p_end);
  6949. break;
  6950. case SIMD_v128_const:
  6951. case SIMD_v8x16_shuffle:
  6952. /* immByte[16] immLaneId[16] */
  6953. CHECK_BUF1(p, p_end, 16);
  6954. p += 16;
  6955. break;
  6956. case SIMD_i8x16_extract_lane_s:
  6957. case SIMD_i8x16_extract_lane_u:
  6958. case SIMD_i8x16_replace_lane:
  6959. case SIMD_i16x8_extract_lane_s:
  6960. case SIMD_i16x8_extract_lane_u:
  6961. case SIMD_i16x8_replace_lane:
  6962. case SIMD_i32x4_extract_lane:
  6963. case SIMD_i32x4_replace_lane:
  6964. case SIMD_i64x2_extract_lane:
  6965. case SIMD_i64x2_replace_lane:
  6966. case SIMD_f32x4_extract_lane:
  6967. case SIMD_f32x4_replace_lane:
  6968. case SIMD_f64x2_extract_lane:
  6969. case SIMD_f64x2_replace_lane:
  6970. /* ImmLaneId */
  6971. CHECK_BUF(p, p_end, 1);
  6972. p++;
  6973. break;
  6974. case SIMD_v128_load8_lane:
  6975. case SIMD_v128_load16_lane:
  6976. case SIMD_v128_load32_lane:
  6977. case SIMD_v128_load64_lane:
  6978. case SIMD_v128_store8_lane:
  6979. case SIMD_v128_store16_lane:
  6980. case SIMD_v128_store32_lane:
  6981. case SIMD_v128_store64_lane:
  6982. /* memarg align */
  6983. skip_leb_uint32(p, p_end);
  6984. /* memarg offset */
  6985. skip_leb_mem_offset(p, p_end);
  6986. /* ImmLaneId */
  6987. CHECK_BUF(p, p_end, 1);
  6988. p++;
  6989. break;
  6990. case SIMD_v128_load32_zero:
  6991. case SIMD_v128_load64_zero:
  6992. /* memarg align */
  6993. skip_leb_uint32(p, p_end);
  6994. /* memarg offset */
  6995. skip_leb_mem_offset(p, p_end);
  6996. break;
  6997. default:
  6998. /*
  6999. * since latest SIMD specific used almost every value
  7000. * from 0x00 to 0xff, the default branch will present
  7001. * all opcodes without imm
  7002. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  7003. */
  7004. break;
  7005. }
  7006. break;
  7007. }
  7008. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  7009. #endif /* end of WASM_ENABLE_SIMD */
  7010. #if WASM_ENABLE_SHARED_MEMORY != 0
  7011. case WASM_OP_ATOMIC_PREFIX:
  7012. {
  7013. uint32 opcode1;
  7014. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  7015. read_leb_uint32(p, p_end, opcode1);
  7016. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7017. is no larger than UINT8_MAX */
  7018. opcode = (uint8)opcode1;
  7019. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7020. skip_leb_uint32(p, p_end); /* align */
  7021. skip_leb_mem_offset(p, p_end); /* offset */
  7022. }
  7023. else {
  7024. /* atomic.fence doesn't have memarg */
  7025. p++;
  7026. }
  7027. break;
  7028. }
  7029. #endif
  7030. #if WASM_ENABLE_DEBUG_INTERP != 0
  7031. case DEBUG_OP_BREAK:
  7032. {
  7033. WASMDebugInstance *debug_instance =
  7034. wasm_exec_env_get_instance(exec_env);
  7035. char original_opcode[1];
  7036. uint64 size = 1;
  7037. WASMModuleInstance *module_inst =
  7038. (WASMModuleInstance *)exec_env->module_inst;
  7039. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7040. ? (p - 1) - module_inst->module->load_addr
  7041. : ~0;
  7042. if (debug_instance) {
  7043. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7044. original_opcode, &size)
  7045. && size == 1) {
  7046. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7047. "with %02x: ",
  7048. original_opcode[0]);
  7049. opcode = original_opcode[0];
  7050. goto op_break_retry;
  7051. }
  7052. }
  7053. break;
  7054. }
  7055. #endif
  7056. default:
  7057. return false;
  7058. }
  7059. }
  7060. (void)u8;
  7061. (void)exec_env;
  7062. return false;
  7063. fail:
  7064. return false;
  7065. }
  7066. #if WASM_ENABLE_FAST_INTERP != 0
  7067. #if WASM_DEBUG_PREPROCESSOR != 0
  7068. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7069. #else
  7070. #define LOG_OP(...) (void)0
  7071. #endif
  7072. #define PATCH_ELSE 0
  7073. #define PATCH_END 1
  7074. typedef struct BranchBlockPatch {
  7075. struct BranchBlockPatch *next;
  7076. uint8 patch_type;
  7077. uint8 *code_compiled;
  7078. } BranchBlockPatch;
  7079. #endif
  7080. typedef struct BranchBlock {
  7081. uint8 label_type;
  7082. BlockType block_type;
  7083. uint8 *start_addr;
  7084. uint8 *else_addr;
  7085. uint8 *end_addr;
  7086. uint32 stack_cell_num;
  7087. #if WASM_ENABLE_GC != 0
  7088. uint32 reftype_map_num;
  7089. /* Indicate which local is used inside current block, used to validate
  7090. * local.get with non-nullable ref types */
  7091. uint8 *local_use_mask;
  7092. uint32 local_use_mask_size;
  7093. #endif
  7094. #if WASM_ENABLE_FAST_INTERP != 0
  7095. uint16 dynamic_offset;
  7096. uint8 *code_compiled;
  7097. BranchBlockPatch *patch_list;
  7098. /* This is used to save params frame_offset of of if block */
  7099. int16 *param_frame_offsets;
  7100. /* This is used to recover the dynamic offset for else branch,
  7101. * and also to remember the start offset of dynamic space which
  7102. * stores the block arguments for loop block, so we can use it
  7103. * to copy the stack operands to the loop block's arguments in
  7104. * wasm_loader_emit_br_info for opcode br. */
  7105. uint16 start_dynamic_offset;
  7106. #endif
  7107. /* Indicate the operand stack is in polymorphic state.
  7108. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7109. * to polymorphic state until the block's 'end' opcode is processed.
  7110. * If stack is in polymorphic state and stack is empty, instruction can
  7111. * pop any type of value directly without decreasing stack top pointer
  7112. * and stack cell num. */
  7113. bool is_stack_polymorphic;
  7114. } BranchBlock;
  7115. typedef struct WASMLoaderContext {
  7116. /* frame ref stack */
  7117. uint8 *frame_ref;
  7118. uint8 *frame_ref_bottom;
  7119. uint8 *frame_ref_boundary;
  7120. uint32 frame_ref_size;
  7121. uint32 stack_cell_num;
  7122. uint32 max_stack_cell_num;
  7123. #if WASM_ENABLE_GC != 0
  7124. /* frame reftype map stack */
  7125. WASMRefTypeMap *frame_reftype_map;
  7126. WASMRefTypeMap *frame_reftype_map_bottom;
  7127. WASMRefTypeMap *frame_reftype_map_boundary;
  7128. uint32 frame_reftype_map_size;
  7129. uint32 reftype_map_num;
  7130. uint32 max_reftype_map_num;
  7131. /* Current module */
  7132. WASMModule *module;
  7133. /* Current module's ref_type_set */
  7134. HashMap *ref_type_set;
  7135. /* Always point to local variable ref_type of
  7136. wasm_loader_prepare_bytecode */
  7137. WASMRefType *ref_type_tmp;
  7138. #endif
  7139. /* frame csp stack */
  7140. BranchBlock *frame_csp;
  7141. BranchBlock *frame_csp_bottom;
  7142. BranchBlock *frame_csp_boundary;
  7143. uint32 frame_csp_size;
  7144. uint32 csp_num;
  7145. uint32 max_csp_num;
  7146. #if WASM_ENABLE_FAST_INTERP != 0
  7147. /* frame offset stack */
  7148. int16 *frame_offset;
  7149. int16 *frame_offset_bottom;
  7150. int16 *frame_offset_boundary;
  7151. uint32 frame_offset_size;
  7152. int16 dynamic_offset;
  7153. int16 start_dynamic_offset;
  7154. int16 max_dynamic_offset;
  7155. /* preserved local offset */
  7156. int16 preserved_local_offset;
  7157. /* const buffer for i64 and f64 consts, note that the raw bytes
  7158. * of i64 and f64 are the same, so we read an i64 value from an
  7159. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7160. int64 *i64_consts;
  7161. uint32 i64_const_max_num;
  7162. uint32 i64_const_num;
  7163. /* const buffer for i32 and f32 consts */
  7164. int32 *i32_consts;
  7165. uint32 i32_const_max_num;
  7166. uint32 i32_const_num;
  7167. /* processed code */
  7168. uint8 *p_code_compiled;
  7169. uint8 *p_code_compiled_end;
  7170. uint32 code_compiled_size;
  7171. /* If the last opcode will be dropped, the peak memory usage will be larger
  7172. * than the final code_compiled_size, we record the peak size to ensure
  7173. * there will not be invalid memory access during second traverse */
  7174. uint32 code_compiled_peak_size;
  7175. #endif
  7176. } WASMLoaderContext;
  7177. #define CHECK_CSP_PUSH() \
  7178. do { \
  7179. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7180. MEM_REALLOC( \
  7181. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7182. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7183. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7184. ctx->frame_csp_boundary = \
  7185. ctx->frame_csp_bottom \
  7186. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7187. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7188. } \
  7189. } while (0)
  7190. #define CHECK_CSP_POP() \
  7191. do { \
  7192. if (ctx->csp_num < 1) { \
  7193. set_error_buf(error_buf, error_buf_size, \
  7194. "type mismatch: " \
  7195. "expect data but block stack was empty"); \
  7196. goto fail; \
  7197. } \
  7198. } while (0)
  7199. #if WASM_ENABLE_FAST_INTERP != 0
  7200. static bool
  7201. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7202. uint32 error_buf_size)
  7203. {
  7204. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7205. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7206. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7207. ctx->frame_offset_size + 16);
  7208. ctx->frame_offset_size += 16;
  7209. ctx->frame_offset_boundary =
  7210. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7211. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7212. }
  7213. return true;
  7214. fail:
  7215. return false;
  7216. }
  7217. static bool
  7218. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7219. {
  7220. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7221. return false;
  7222. return true;
  7223. }
  7224. static void
  7225. free_label_patch_list(BranchBlock *frame_csp)
  7226. {
  7227. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7228. BranchBlockPatch *next;
  7229. while (label_patch != NULL) {
  7230. next = label_patch->next;
  7231. wasm_runtime_free(label_patch);
  7232. label_patch = next;
  7233. }
  7234. frame_csp->patch_list = NULL;
  7235. }
  7236. static void
  7237. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7238. {
  7239. BranchBlock *tmp_csp = frame_csp;
  7240. uint32 i;
  7241. for (i = 0; i < csp_num; i++) {
  7242. free_label_patch_list(tmp_csp);
  7243. tmp_csp++;
  7244. }
  7245. }
  7246. static void
  7247. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7248. {
  7249. BranchBlock *tmp_csp = frame_csp;
  7250. uint32 i;
  7251. for (i = 0; i < csp_num; i++) {
  7252. if (tmp_csp->param_frame_offsets)
  7253. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7254. tmp_csp++;
  7255. }
  7256. }
  7257. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7258. #if WASM_ENABLE_GC != 0
  7259. static bool
  7260. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7261. char *error_buf, uint32 error_buf_size)
  7262. {
  7263. BranchBlock *current_csp = ctx->frame_csp - 1;
  7264. uint32 local_mask_size;
  7265. if (local_count == 0) {
  7266. current_csp->local_use_mask_size = 0;
  7267. return true;
  7268. }
  7269. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7270. * else branch, we don't need to allocate memory again */
  7271. if (!current_csp->local_use_mask) {
  7272. local_mask_size = (local_count + 7) / sizeof(uint8);
  7273. if (!(current_csp->local_use_mask =
  7274. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7275. return false;
  7276. }
  7277. current_csp->local_use_mask_size = local_mask_size;
  7278. }
  7279. else {
  7280. local_mask_size = current_csp->local_use_mask_size;
  7281. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7282. }
  7283. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7284. /* For non-function blocks, inherit the use status from parent block */
  7285. BranchBlock *parent_csp = current_csp - 1;
  7286. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7287. bh_assert(parent_csp->local_use_mask);
  7288. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7289. parent_csp->local_use_mask, local_mask_size);
  7290. }
  7291. return true;
  7292. }
  7293. static void
  7294. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7295. {
  7296. BranchBlock *current_csp = ctx->frame_csp - 1;
  7297. bh_assert(current_csp->local_use_mask
  7298. || current_csp->local_use_mask_size == 0);
  7299. if (current_csp->local_use_mask) {
  7300. wasm_runtime_free(current_csp->local_use_mask);
  7301. }
  7302. current_csp->local_use_mask = NULL;
  7303. current_csp->local_use_mask_size = 0;
  7304. }
  7305. static void
  7306. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7307. {
  7308. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7309. uint32 i;
  7310. for (i = 0; i < ctx->csp_num; i++) {
  7311. if (tmp_csp->local_use_mask) {
  7312. wasm_runtime_free(tmp_csp->local_use_mask);
  7313. tmp_csp->local_use_mask = NULL;
  7314. tmp_csp->local_use_mask_size = 0;
  7315. }
  7316. tmp_csp++;
  7317. }
  7318. }
  7319. static void
  7320. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7321. {
  7322. BranchBlock *current_csp = ctx->frame_csp - 1;
  7323. uint32 byte_offset = index / sizeof(uint8);
  7324. uint32 bit_offset = index % sizeof(uint8);
  7325. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7326. bh_assert(current_csp->local_use_mask);
  7327. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7328. }
  7329. static bool
  7330. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7331. {
  7332. BranchBlock *current_csp = ctx->frame_csp - 1;
  7333. uint32 byte_offset = index / sizeof(uint8);
  7334. uint32 bit_offset = index % sizeof(uint8);
  7335. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7336. bh_assert(current_csp->local_use_mask);
  7337. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7338. ? true
  7339. : false;
  7340. }
  7341. #endif /* end of WASM_ENABLE_GC != 0 */
  7342. static void
  7343. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7344. {
  7345. if (ctx) {
  7346. if (ctx->frame_ref_bottom)
  7347. wasm_runtime_free(ctx->frame_ref_bottom);
  7348. #if WASM_ENABLE_GC != 0
  7349. if (ctx->frame_reftype_map_bottom)
  7350. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7351. #endif
  7352. if (ctx->frame_csp_bottom) {
  7353. #if WASM_ENABLE_FAST_INTERP != 0
  7354. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7355. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7356. ctx->csp_num);
  7357. #endif
  7358. #if WASM_ENABLE_GC != 0
  7359. wasm_loader_clean_all_local_use_masks(ctx);
  7360. #endif
  7361. wasm_runtime_free(ctx->frame_csp_bottom);
  7362. }
  7363. #if WASM_ENABLE_FAST_INTERP != 0
  7364. if (ctx->frame_offset_bottom)
  7365. wasm_runtime_free(ctx->frame_offset_bottom);
  7366. if (ctx->i64_consts)
  7367. wasm_runtime_free(ctx->i64_consts);
  7368. if (ctx->i32_consts)
  7369. wasm_runtime_free(ctx->i32_consts);
  7370. #endif
  7371. wasm_runtime_free(ctx);
  7372. }
  7373. }
  7374. static WASMLoaderContext *
  7375. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7376. {
  7377. WASMLoaderContext *loader_ctx =
  7378. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7379. if (!loader_ctx)
  7380. return NULL;
  7381. loader_ctx->frame_ref_size = 32;
  7382. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7383. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7384. goto fail;
  7385. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7386. #if WASM_ENABLE_GC != 0
  7387. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7388. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7389. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7390. error_buf_size)))
  7391. goto fail;
  7392. loader_ctx->frame_reftype_map_boundary =
  7393. loader_ctx->frame_reftype_map_bottom + 16;
  7394. #endif
  7395. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7396. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7397. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7398. goto fail;
  7399. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7400. #if WASM_ENABLE_EXCE_HANDLING != 0
  7401. func->exception_handler_count = 0;
  7402. #endif
  7403. #if WASM_ENABLE_FAST_INTERP != 0
  7404. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7405. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7406. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7407. error_buf_size)))
  7408. goto fail;
  7409. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7410. loader_ctx->i64_const_max_num = 8;
  7411. if (!(loader_ctx->i64_consts =
  7412. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7413. error_buf, error_buf_size)))
  7414. goto fail;
  7415. loader_ctx->i32_const_max_num = 8;
  7416. if (!(loader_ctx->i32_consts =
  7417. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7418. error_buf, error_buf_size)))
  7419. goto fail;
  7420. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7421. set_error_buf(error_buf, error_buf_size,
  7422. "fast interpreter offset overflow");
  7423. goto fail;
  7424. }
  7425. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7426. loader_ctx->max_dynamic_offset =
  7427. func->param_cell_num + func->local_cell_num;
  7428. #endif
  7429. return loader_ctx;
  7430. fail:
  7431. wasm_loader_ctx_destroy(loader_ctx);
  7432. return NULL;
  7433. }
  7434. static bool
  7435. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7436. uint32 error_buf_size)
  7437. {
  7438. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7439. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7440. /* Increase the frame ref stack */
  7441. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7442. ctx->frame_ref_size + 16);
  7443. ctx->frame_ref_size += 16;
  7444. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7445. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7446. }
  7447. #if WASM_ENABLE_GC != 0
  7448. if (wasm_is_type_multi_byte_type(type)
  7449. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7450. /* Increase the frame reftype map stack */
  7451. bh_assert(
  7452. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7453. * sizeof(WASMRefTypeMap))
  7454. == ctx->frame_reftype_map_size);
  7455. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7456. ctx->frame_reftype_map_size
  7457. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7458. ctx->frame_reftype_map =
  7459. ctx->frame_reftype_map_bottom
  7460. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7461. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7462. ctx->frame_reftype_map_boundary =
  7463. ctx->frame_reftype_map_bottom
  7464. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7465. }
  7466. #endif
  7467. return true;
  7468. fail:
  7469. return false;
  7470. }
  7471. static bool
  7472. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7473. uint32 error_buf_size)
  7474. {
  7475. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7476. uint32 i;
  7477. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7478. return false;
  7479. #if WASM_ENABLE_GC != 0
  7480. if (wasm_is_type_multi_byte_type(type)) {
  7481. WASMRefType *ref_type;
  7482. if (!(ref_type =
  7483. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7484. error_buf, error_buf_size))) {
  7485. return false;
  7486. }
  7487. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7488. /* Increase the frame reftype map stack */
  7489. bh_assert((uint32)((ctx->frame_reftype_map
  7490. - ctx->frame_reftype_map_bottom)
  7491. * sizeof(WASMRefTypeMap))
  7492. == ctx->frame_reftype_map_size);
  7493. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7494. ctx->frame_reftype_map_size,
  7495. ctx->frame_reftype_map_size
  7496. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7497. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7498. + ctx->frame_reftype_map_size
  7499. / ((uint32)sizeof(WASMRefTypeMap));
  7500. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7501. ctx->frame_reftype_map_boundary =
  7502. ctx->frame_reftype_map_bottom
  7503. + ctx->frame_reftype_map_size
  7504. / ((uint32)sizeof(WASMRefTypeMap));
  7505. }
  7506. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7507. ctx->frame_reftype_map->ref_type = ref_type;
  7508. ctx->frame_reftype_map++;
  7509. ctx->reftype_map_num++;
  7510. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7511. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7512. }
  7513. #endif
  7514. for (i = 0; i < type_cell_num; i++)
  7515. *ctx->frame_ref++ = type;
  7516. ctx->stack_cell_num += type_cell_num;
  7517. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7518. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7519. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7520. set_error_buf(error_buf, error_buf_size,
  7521. "operand stack depth limit exceeded");
  7522. return false;
  7523. }
  7524. }
  7525. return true;
  7526. #if WASM_ENABLE_GC != 0
  7527. fail:
  7528. return false;
  7529. #endif
  7530. }
  7531. static bool
  7532. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7533. int32 stack_cell_num,
  7534. #if WASM_ENABLE_GC != 0
  7535. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7536. #endif
  7537. uint8 type,
  7538. #if WASM_ENABLE_GC != 0
  7539. WASMRefType *ref_type,
  7540. #endif
  7541. char *error_buf, uint32 error_buf_size)
  7542. {
  7543. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7544. #if WASM_ENABLE_GC != 0
  7545. WASMRefType *frame_reftype = NULL;
  7546. #endif
  7547. if (stack_cell_num < type_cell_num) {
  7548. set_error_buf(error_buf, error_buf_size,
  7549. "type mismatch: expect data but stack was empty");
  7550. return false;
  7551. }
  7552. #if WASM_ENABLE_GC == 0
  7553. for (i = 0; i < type_cell_num; i++) {
  7554. if (*(frame_ref - 1 - i) != type) {
  7555. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7556. "type mismatch: expect ", type2str(type),
  7557. " but got other");
  7558. return false;
  7559. }
  7560. }
  7561. #else
  7562. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7563. bh_assert(reftype_map_num > 0);
  7564. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7565. }
  7566. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7567. ref_type, ctx->module->types,
  7568. ctx->module->type_count)) {
  7569. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7570. "type mismatch: expect ", type2str(type),
  7571. " but got other");
  7572. return false;
  7573. }
  7574. for (i = 0; i < type_cell_num - 1; i++) {
  7575. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7576. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7577. "type mismatch: expect ", type2str(type),
  7578. " but got other");
  7579. return false;
  7580. }
  7581. }
  7582. #endif
  7583. return true;
  7584. }
  7585. static bool
  7586. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7587. uint32 error_buf_size)
  7588. {
  7589. int32 block_stack_cell_num =
  7590. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7591. #if WASM_ENABLE_GC != 0
  7592. int32 reftype_map_num =
  7593. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7594. #endif
  7595. if (block_stack_cell_num > 0) {
  7596. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7597. /* the stack top is a value of any type, return success */
  7598. return true;
  7599. }
  7600. #if WASM_ENABLE_GC != 0
  7601. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7602. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7603. WASMRefType *stack_top_ref_type = NULL;
  7604. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7605. bh_assert(reftype_map_num > 0);
  7606. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7607. }
  7608. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7609. ctx->ref_type_tmp, ctx->module->types,
  7610. ctx->module->type_count)) {
  7611. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7612. uint32 ref_type_struct_size =
  7613. wasm_reftype_struct_size(stack_top_ref_type);
  7614. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7615. stack_top_ref_type, ref_type_struct_size);
  7616. }
  7617. return true;
  7618. }
  7619. }
  7620. #endif
  7621. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7622. #if WASM_ENABLE_GC != 0
  7623. ctx->frame_reftype_map, reftype_map_num,
  7624. #endif
  7625. type,
  7626. #if WASM_ENABLE_GC != 0
  7627. ctx->ref_type_tmp,
  7628. #endif
  7629. error_buf, error_buf_size)) {
  7630. return false;
  7631. }
  7632. return true;
  7633. }
  7634. static bool
  7635. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7636. uint32 error_buf_size)
  7637. {
  7638. BranchBlock *cur_block = ctx->frame_csp - 1;
  7639. int32 available_stack_cell =
  7640. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7641. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7642. /* Directly return success if current block is in stack
  7643. polymorphic state while stack is empty. */
  7644. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7645. return true;
  7646. if (type == VALUE_TYPE_VOID)
  7647. return true;
  7648. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7649. return false;
  7650. bh_assert(available_stack_cell > 0);
  7651. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7652. type = VALUE_TYPE_ANY;
  7653. cell_num_to_pop = 1;
  7654. }
  7655. ctx->frame_ref -= cell_num_to_pop;
  7656. ctx->stack_cell_num -= cell_num_to_pop;
  7657. #if WASM_ENABLE_GC != 0
  7658. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7659. ctx->frame_reftype_map--;
  7660. ctx->reftype_map_num--;
  7661. }
  7662. #endif
  7663. return true;
  7664. }
  7665. #if WASM_ENABLE_GC != 0
  7666. /* Get the stack top element of current block */
  7667. static bool
  7668. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7669. WASMRefType **p_ref_type, char *error_buf,
  7670. uint32 error_buf_size)
  7671. {
  7672. BranchBlock *cur_block = ctx->frame_csp - 1;
  7673. int32 available_stack_cell =
  7674. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7675. if (available_stack_cell <= 0) {
  7676. /* Directly return success if current block is in stack
  7677. polymorphic state while stack is empty. */
  7678. if (cur_block->is_stack_polymorphic) {
  7679. *p_type = VALUE_TYPE_ANY;
  7680. return true;
  7681. }
  7682. else {
  7683. set_error_buf(
  7684. error_buf, error_buf_size,
  7685. "type mismatch: expect data but block stack was empty");
  7686. return false;
  7687. }
  7688. }
  7689. *p_type = *(ctx->frame_ref - 1);
  7690. if (wasm_is_type_multi_byte_type(*p_type)) {
  7691. int32 available_reftype_map =
  7692. (int32)(ctx->reftype_map_num
  7693. - (ctx->frame_csp - 1)->reftype_map_num);
  7694. bh_assert(available_reftype_map > 0);
  7695. (void)available_reftype_map;
  7696. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7697. }
  7698. return true;
  7699. }
  7700. #if WASM_ENABLE_FAST_INTERP != 0
  7701. static bool
  7702. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7703. char *error_buf, uint32 error_buf_size);
  7704. #endif
  7705. /* Check whether the stack top elem is a heap object, and if yes,
  7706. pop and return it */
  7707. static bool
  7708. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7709. WASMRefType *ref_ht_ret, char *error_buf,
  7710. uint32 error_buf_size)
  7711. {
  7712. uint8 type = 0;
  7713. WASMRefType *ref_type = NULL;
  7714. /* Get stack top element */
  7715. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7716. error_buf_size)) {
  7717. return false;
  7718. }
  7719. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7720. /* stack top isn't a ref type */
  7721. && !wasm_is_type_reftype(type)) {
  7722. set_error_buf(error_buf, error_buf_size,
  7723. "type mismatch: expect heap object but got others");
  7724. return false;
  7725. }
  7726. /* POP stack top */
  7727. if (wasm_is_type_multi_byte_type(type)) {
  7728. bh_assert(ref_type);
  7729. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7730. wasm_reftype_struct_size(ref_type));
  7731. }
  7732. #if WASM_ENABLE_FAST_INTERP != 0
  7733. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7734. error_buf_size)) {
  7735. return false;
  7736. }
  7737. #else
  7738. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7739. return false;
  7740. }
  7741. #endif
  7742. if (p_type)
  7743. *p_type = type;
  7744. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7745. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7746. wasm_reftype_struct_size(ref_type));
  7747. }
  7748. return true;
  7749. }
  7750. /* Check whether the stack top elem is subtype of (ref null ht),
  7751. and if yes, pop it and return the converted (ref ht) */
  7752. static bool
  7753. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7754. WASMRefType *ref_ht_ret, char *error_buf,
  7755. uint32 error_buf_size)
  7756. {
  7757. uint8 type = 0;
  7758. WASMRefType ref_type = { 0 };
  7759. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7760. error_buf_size)) {
  7761. return false;
  7762. }
  7763. /* Convert to related (ref ht) and return */
  7764. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7765. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7766. */
  7767. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7768. HEAP_TYPE_ARRAY
  7769. + (type - REF_TYPE_ARRAYREF));
  7770. type = ref_ht_ret->ref_type;
  7771. }
  7772. else if (wasm_is_reftype_htref_nullable(type)
  7773. || wasm_is_reftype_htref_non_nullable(type)) {
  7774. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7775. wasm_reftype_struct_size(&ref_type));
  7776. /* Convert to (ref ht) */
  7777. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7778. ref_ht_ret->ref_ht_common.nullable = false;
  7779. type = ref_ht_ret->ref_type;
  7780. }
  7781. *p_type = type;
  7782. return true;
  7783. }
  7784. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7785. and if yes, pop it and return the type_idx */
  7786. static bool
  7787. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7788. uint32 *p_type_idx, char *error_buf,
  7789. uint32 error_buf_size)
  7790. {
  7791. uint8 type = 0;
  7792. int32 type_idx = -1;
  7793. WASMRefType *ref_type = NULL;
  7794. /* Get stack top element */
  7795. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7796. error_buf_size)) {
  7797. return false;
  7798. }
  7799. if (type != VALUE_TYPE_ANY) {
  7800. /* stack top isn't (ref null $t) */
  7801. if (!((wasm_is_reftype_htref_nullable(type)
  7802. || wasm_is_reftype_htref_non_nullable(type))
  7803. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7804. set_error_buf(error_buf, error_buf_size,
  7805. "type mismatch: expect (ref null $t) but got others");
  7806. return false;
  7807. }
  7808. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7809. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7810. wasm_reftype_struct_size(ref_type));
  7811. }
  7812. /* POP stack top */
  7813. #if WASM_ENABLE_FAST_INTERP != 0
  7814. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7815. error_buf_size)) {
  7816. return false;
  7817. }
  7818. #else
  7819. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7820. return false;
  7821. }
  7822. #endif
  7823. /* Convert to type_idx and return */
  7824. *p_type = type;
  7825. if (type != VALUE_TYPE_ANY)
  7826. *p_type_idx = (uint32)type_idx;
  7827. return true;
  7828. }
  7829. #endif /* WASM_ENABLE_GC != 0 */
  7830. #if WASM_ENABLE_FAST_INTERP == 0
  7831. static bool
  7832. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7833. uint8 type_push, uint8 type_pop, char *error_buf,
  7834. uint32 error_buf_size)
  7835. {
  7836. for (int i = 0; i < pop_cnt; i++) {
  7837. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7838. error_buf_size))
  7839. return false;
  7840. }
  7841. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7842. return false;
  7843. return true;
  7844. }
  7845. #endif
  7846. static bool
  7847. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7848. BlockType block_type, uint8 *start_addr,
  7849. char *error_buf, uint32 error_buf_size)
  7850. {
  7851. CHECK_CSP_PUSH();
  7852. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7853. ctx->frame_csp->label_type = label_type;
  7854. ctx->frame_csp->block_type = block_type;
  7855. ctx->frame_csp->start_addr = start_addr;
  7856. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7857. #if WASM_ENABLE_GC != 0
  7858. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7859. #endif
  7860. #if WASM_ENABLE_FAST_INTERP != 0
  7861. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7862. ctx->frame_csp->patch_list = NULL;
  7863. #endif
  7864. ctx->frame_csp++;
  7865. ctx->csp_num++;
  7866. if (ctx->csp_num > ctx->max_csp_num) {
  7867. ctx->max_csp_num = ctx->csp_num;
  7868. if (ctx->max_csp_num > UINT16_MAX) {
  7869. set_error_buf(error_buf, error_buf_size,
  7870. "label stack depth limit exceeded");
  7871. return false;
  7872. }
  7873. }
  7874. return true;
  7875. fail:
  7876. return false;
  7877. }
  7878. static bool
  7879. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7880. uint32 error_buf_size)
  7881. {
  7882. CHECK_CSP_POP();
  7883. #if WASM_ENABLE_FAST_INTERP != 0
  7884. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7885. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7886. #endif
  7887. ctx->frame_csp--;
  7888. ctx->csp_num--;
  7889. return true;
  7890. fail:
  7891. return false;
  7892. }
  7893. #if WASM_ENABLE_FAST_INTERP != 0
  7894. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7895. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7896. #define emit_label(opcode) \
  7897. do { \
  7898. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7899. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7900. } while (0)
  7901. #define skip_label() \
  7902. do { \
  7903. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7904. LOG_OP("\ndelete last op\n"); \
  7905. } while (0)
  7906. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7907. #if UINTPTR_MAX == UINT64_MAX
  7908. #define emit_label(opcode) \
  7909. do { \
  7910. int32 offset = \
  7911. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7912. /* emit int32 relative offset in 64-bit target */ \
  7913. wasm_loader_emit_uint32(loader_ctx, offset); \
  7914. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7915. } while (0)
  7916. #else
  7917. #define emit_label(opcode) \
  7918. do { \
  7919. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7920. /* emit uint32 label address in 32-bit target */ \
  7921. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7922. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7923. } while (0)
  7924. #endif
  7925. #define skip_label() \
  7926. do { \
  7927. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7928. LOG_OP("\ndelete last op\n"); \
  7929. } while (0)
  7930. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7931. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7932. #define emit_label(opcode) \
  7933. do { \
  7934. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7935. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7936. } while (0)
  7937. #define skip_label() \
  7938. do { \
  7939. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7940. LOG_OP("\ndelete last op\n"); \
  7941. } while (0)
  7942. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7943. #define emit_empty_label_addr_and_frame_ip(type) \
  7944. do { \
  7945. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7946. loader_ctx->p_code_compiled, error_buf, \
  7947. error_buf_size)) \
  7948. goto fail; \
  7949. /* label address, to be patched */ \
  7950. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7951. } while (0)
  7952. #define emit_br_info(frame_csp, is_br) \
  7953. do { \
  7954. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7955. error_buf_size)) \
  7956. goto fail; \
  7957. } while (0)
  7958. #define LAST_OP_OUTPUT_I32() \
  7959. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7960. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7961. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7962. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7963. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7964. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7965. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7966. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7967. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7968. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7969. || (last_op == EXT_OP_COPY_STACK_TOP)
  7970. #define LAST_OP_OUTPUT_I64() \
  7971. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7972. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7973. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7974. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7975. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7976. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7977. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7978. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7979. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7980. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7981. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7982. #define GET_CONST_OFFSET(type, val) \
  7983. do { \
  7984. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7985. &operand_offset, error_buf, \
  7986. error_buf_size))) \
  7987. goto fail; \
  7988. } while (0)
  7989. #define GET_CONST_F32_OFFSET(type, fval) \
  7990. do { \
  7991. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7992. &operand_offset, error_buf, \
  7993. error_buf_size))) \
  7994. goto fail; \
  7995. } while (0)
  7996. #define GET_CONST_F64_OFFSET(type, fval) \
  7997. do { \
  7998. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7999. &operand_offset, error_buf, \
  8000. error_buf_size))) \
  8001. goto fail; \
  8002. } while (0)
  8003. #define emit_operand(ctx, offset) \
  8004. do { \
  8005. wasm_loader_emit_int16(ctx, offset); \
  8006. LOG_OP("%d\t", offset); \
  8007. } while (0)
  8008. #define emit_byte(ctx, byte) \
  8009. do { \
  8010. wasm_loader_emit_uint8(ctx, byte); \
  8011. LOG_OP("%d\t", byte); \
  8012. } while (0)
  8013. #define emit_uint32(ctx, value) \
  8014. do { \
  8015. wasm_loader_emit_uint32(ctx, value); \
  8016. LOG_OP("%d\t", value); \
  8017. } while (0)
  8018. #define emit_uint64(ctx, value) \
  8019. do { \
  8020. wasm_loader_emit_const(ctx, &value, false); \
  8021. LOG_OP("%lld\t", value); \
  8022. } while (0)
  8023. #define emit_float32(ctx, value) \
  8024. do { \
  8025. wasm_loader_emit_const(ctx, &value, true); \
  8026. LOG_OP("%f\t", value); \
  8027. } while (0)
  8028. #define emit_float64(ctx, value) \
  8029. do { \
  8030. wasm_loader_emit_const(ctx, &value, false); \
  8031. LOG_OP("%f\t", value); \
  8032. } while (0)
  8033. static bool
  8034. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8035. {
  8036. if (!(ctx->p_code_compiled =
  8037. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8038. return false;
  8039. ctx->p_code_compiled_end =
  8040. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8041. /* clean up frame ref */
  8042. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8043. ctx->frame_ref = ctx->frame_ref_bottom;
  8044. ctx->stack_cell_num = 0;
  8045. #if WASM_ENABLE_GC != 0
  8046. /* clean up reftype map */
  8047. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8048. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8049. ctx->reftype_map_num = 0;
  8050. #endif
  8051. /* clean up frame csp */
  8052. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8053. ctx->frame_csp = ctx->frame_csp_bottom;
  8054. ctx->csp_num = 0;
  8055. ctx->max_csp_num = 0;
  8056. /* clean up frame offset */
  8057. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8058. ctx->frame_offset = ctx->frame_offset_bottom;
  8059. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8060. /* init preserved local offsets */
  8061. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8062. /* const buf is reserved */
  8063. return true;
  8064. }
  8065. static void
  8066. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8067. {
  8068. ctx->code_compiled_size += size;
  8069. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8070. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8071. }
  8072. }
  8073. static void
  8074. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8075. {
  8076. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8077. if (ctx->p_code_compiled) {
  8078. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8079. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8080. #endif
  8081. bh_memcpy_s(ctx->p_code_compiled,
  8082. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8083. value, size);
  8084. ctx->p_code_compiled += size;
  8085. }
  8086. else {
  8087. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8088. bh_assert((ctx->code_compiled_size & 1) == 0);
  8089. #endif
  8090. increase_compiled_code_space(ctx, size);
  8091. }
  8092. }
  8093. static void
  8094. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8095. {
  8096. if (ctx->p_code_compiled) {
  8097. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8098. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8099. #endif
  8100. STORE_U32(ctx->p_code_compiled, value);
  8101. ctx->p_code_compiled += sizeof(uint32);
  8102. }
  8103. else {
  8104. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8105. bh_assert((ctx->code_compiled_size & 1) == 0);
  8106. #endif
  8107. increase_compiled_code_space(ctx, sizeof(uint32));
  8108. }
  8109. }
  8110. static void
  8111. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8112. {
  8113. if (ctx->p_code_compiled) {
  8114. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8115. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8116. #endif
  8117. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8118. ctx->p_code_compiled += sizeof(int16);
  8119. }
  8120. else {
  8121. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8122. bh_assert((ctx->code_compiled_size & 1) == 0);
  8123. #endif
  8124. increase_compiled_code_space(ctx, sizeof(uint16));
  8125. }
  8126. }
  8127. static void
  8128. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8129. {
  8130. if (ctx->p_code_compiled) {
  8131. *(ctx->p_code_compiled) = value;
  8132. ctx->p_code_compiled += sizeof(uint8);
  8133. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8134. ctx->p_code_compiled++;
  8135. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8136. #endif
  8137. }
  8138. else {
  8139. increase_compiled_code_space(ctx, sizeof(uint8));
  8140. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8141. increase_compiled_code_space(ctx, sizeof(uint8));
  8142. bh_assert((ctx->code_compiled_size & 1) == 0);
  8143. #endif
  8144. }
  8145. }
  8146. static void
  8147. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8148. {
  8149. if (ctx->p_code_compiled) {
  8150. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8151. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8152. #endif
  8153. STORE_PTR(ctx->p_code_compiled, value);
  8154. ctx->p_code_compiled += sizeof(void *);
  8155. }
  8156. else {
  8157. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8158. bh_assert((ctx->code_compiled_size & 1) == 0);
  8159. #endif
  8160. increase_compiled_code_space(ctx, sizeof(void *));
  8161. }
  8162. }
  8163. static void
  8164. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8165. {
  8166. if (ctx->p_code_compiled) {
  8167. ctx->p_code_compiled -= size;
  8168. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8169. if (size == sizeof(uint8)) {
  8170. ctx->p_code_compiled--;
  8171. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8172. }
  8173. #endif
  8174. }
  8175. else {
  8176. ctx->code_compiled_size -= size;
  8177. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8178. if (size == sizeof(uint8)) {
  8179. ctx->code_compiled_size--;
  8180. bh_assert((ctx->code_compiled_size & 1) == 0);
  8181. }
  8182. #endif
  8183. }
  8184. }
  8185. static bool
  8186. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8187. uint32 local_index, uint32 local_type,
  8188. bool *preserved, char *error_buf,
  8189. uint32 error_buf_size)
  8190. {
  8191. uint32 i = 0;
  8192. int16 preserved_offset = (int16)local_index;
  8193. *preserved = false;
  8194. while (i < loader_ctx->stack_cell_num) {
  8195. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8196. /* move previous local into dynamic space before a set/tee_local opcode
  8197. */
  8198. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8199. if (!(*preserved)) {
  8200. *preserved = true;
  8201. skip_label();
  8202. preserved_offset = loader_ctx->preserved_local_offset;
  8203. if (loader_ctx->p_code_compiled) {
  8204. bh_assert(preserved_offset != (int16)local_index);
  8205. }
  8206. if (is_32bit_type(local_type)) {
  8207. /* Only increase preserve offset in the second traversal */
  8208. if (loader_ctx->p_code_compiled)
  8209. loader_ctx->preserved_local_offset++;
  8210. emit_label(EXT_OP_COPY_STACK_TOP);
  8211. }
  8212. else {
  8213. if (loader_ctx->p_code_compiled)
  8214. loader_ctx->preserved_local_offset += 2;
  8215. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8216. }
  8217. emit_operand(loader_ctx, local_index);
  8218. emit_operand(loader_ctx, preserved_offset);
  8219. emit_label(opcode);
  8220. }
  8221. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8222. }
  8223. if (is_32bit_type(cur_type))
  8224. i++;
  8225. else
  8226. i += 2;
  8227. }
  8228. (void)error_buf;
  8229. (void)error_buf_size;
  8230. return true;
  8231. }
  8232. static bool
  8233. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8234. char *error_buf, uint32 error_buf_size)
  8235. {
  8236. uint32 i = 0;
  8237. bool preserve_local;
  8238. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8239. blocks will not influence the value of these locals */
  8240. while (i < loader_ctx->stack_cell_num) {
  8241. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8242. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8243. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8244. && (cur_offset >= 0)) {
  8245. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8246. cur_type, &preserve_local,
  8247. error_buf, error_buf_size)))
  8248. return false;
  8249. }
  8250. if (is_32bit_type(cur_type)) {
  8251. i++;
  8252. }
  8253. else {
  8254. i += 2;
  8255. }
  8256. }
  8257. return true;
  8258. }
  8259. static bool
  8260. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8261. uint8 *p_code_compiled, char *error_buf,
  8262. uint32 error_buf_size)
  8263. {
  8264. BranchBlockPatch *patch =
  8265. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8266. if (!patch) {
  8267. return false;
  8268. }
  8269. patch->patch_type = patch_type;
  8270. patch->code_compiled = p_code_compiled;
  8271. if (!frame_csp->patch_list) {
  8272. frame_csp->patch_list = patch;
  8273. patch->next = NULL;
  8274. }
  8275. else {
  8276. patch->next = frame_csp->patch_list;
  8277. frame_csp->patch_list = patch;
  8278. }
  8279. return true;
  8280. }
  8281. static void
  8282. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8283. {
  8284. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8285. BranchBlockPatch *node = frame_csp->patch_list;
  8286. BranchBlockPatch *node_prev = NULL, *node_next;
  8287. if (!ctx->p_code_compiled)
  8288. return;
  8289. while (node) {
  8290. node_next = node->next;
  8291. if (node->patch_type == patch_type) {
  8292. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8293. if (node_prev == NULL) {
  8294. frame_csp->patch_list = node_next;
  8295. }
  8296. else {
  8297. node_prev->next = node_next;
  8298. }
  8299. wasm_runtime_free(node);
  8300. }
  8301. else {
  8302. node_prev = node;
  8303. }
  8304. node = node_next;
  8305. }
  8306. }
  8307. static bool
  8308. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8309. bool is_br, char *error_buf, uint32 error_buf_size)
  8310. {
  8311. /* br info layout:
  8312. * a) arity of target block
  8313. * b) total cell num of arity values
  8314. * c) each arity value's cell num
  8315. * d) each arity value's src frame offset
  8316. * e) each arity values's dst dynamic offset
  8317. * f) branch target address
  8318. *
  8319. * Note: b-e are omitted when arity is 0 so that
  8320. * interpreter can recover the br info quickly.
  8321. */
  8322. BlockType *block_type = &frame_csp->block_type;
  8323. uint8 *types = NULL, cell;
  8324. #if WASM_ENABLE_GC != 0
  8325. WASMRefTypeMap *reftype_maps;
  8326. uint32 reftype_map_count;
  8327. #endif
  8328. uint32 arity = 0;
  8329. int32 i;
  8330. int16 *frame_offset = ctx->frame_offset;
  8331. uint16 dynamic_offset;
  8332. /* Note: loop's arity is different from if and block. loop's arity is
  8333. * its parameter count while if and block arity is result count.
  8334. */
  8335. #if WASM_ENABLE_GC == 0
  8336. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8337. arity = block_type_get_param_types(block_type, &types);
  8338. else
  8339. arity = block_type_get_result_types(block_type, &types);
  8340. #else
  8341. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8342. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8343. &reftype_map_count);
  8344. else
  8345. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8346. &reftype_map_count);
  8347. #endif
  8348. /* Part a */
  8349. emit_uint32(ctx, arity);
  8350. if (arity) {
  8351. /* Part b */
  8352. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8353. /* Part c */
  8354. for (i = (int32)arity - 1; i >= 0; i--) {
  8355. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8356. emit_byte(ctx, cell);
  8357. }
  8358. /* Part d */
  8359. for (i = (int32)arity - 1; i >= 0; i--) {
  8360. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8361. frame_offset -= cell;
  8362. emit_operand(ctx, *(int16 *)(frame_offset));
  8363. }
  8364. /* Part e */
  8365. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8366. /* Use start_dynamic_offset which was set in
  8367. copy_params_to_dynamic_space */
  8368. dynamic_offset = frame_csp->start_dynamic_offset
  8369. + wasm_get_cell_num(types, arity);
  8370. else
  8371. dynamic_offset =
  8372. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8373. if (is_br)
  8374. ctx->dynamic_offset = dynamic_offset;
  8375. for (i = (int32)arity - 1; i >= 0; i--) {
  8376. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8377. dynamic_offset -= cell;
  8378. emit_operand(ctx, dynamic_offset);
  8379. }
  8380. }
  8381. /* Part f */
  8382. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8383. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8384. }
  8385. else {
  8386. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8387. error_buf, error_buf_size))
  8388. return false;
  8389. /* label address, to be patched */
  8390. wasm_loader_emit_ptr(ctx, NULL);
  8391. }
  8392. return true;
  8393. }
  8394. static bool
  8395. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8396. bool disable_emit, int16 operand_offset,
  8397. char *error_buf, uint32 error_buf_size)
  8398. {
  8399. uint32 cell_num_to_push, i;
  8400. if (type == VALUE_TYPE_VOID)
  8401. return true;
  8402. /* only check memory overflow in first traverse */
  8403. if (ctx->p_code_compiled == NULL) {
  8404. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8405. return false;
  8406. }
  8407. if (disable_emit)
  8408. *(ctx->frame_offset)++ = operand_offset;
  8409. else {
  8410. emit_operand(ctx, ctx->dynamic_offset);
  8411. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8412. ctx->dynamic_offset++;
  8413. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8414. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8415. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8416. goto fail;
  8417. }
  8418. }
  8419. }
  8420. if (is_32bit_type(type))
  8421. return true;
  8422. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8423. for (i = 0; i < cell_num_to_push; i++) {
  8424. if (ctx->p_code_compiled == NULL) {
  8425. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8426. return false;
  8427. }
  8428. ctx->frame_offset++;
  8429. if (!disable_emit) {
  8430. ctx->dynamic_offset++;
  8431. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8432. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8433. if (ctx->max_dynamic_offset >= INT16_MAX)
  8434. goto fail;
  8435. }
  8436. }
  8437. }
  8438. return true;
  8439. fail:
  8440. set_error_buf(error_buf, error_buf_size,
  8441. "fast interpreter offset overflow");
  8442. return false;
  8443. }
  8444. /* This function should be in front of wasm_loader_pop_frame_ref
  8445. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8446. will be modified by wasm_loader_pop_frame_ref */
  8447. static bool
  8448. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8449. char *error_buf, uint32 error_buf_size)
  8450. {
  8451. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8452. then current block is the function block */
  8453. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8454. BranchBlock *cur_block = ctx->frame_csp - depth;
  8455. int32 available_stack_cell =
  8456. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8457. uint32 cell_num_to_pop;
  8458. /* Directly return success if current block is in stack
  8459. polymorphic state while stack is empty. */
  8460. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8461. return true;
  8462. if (type == VALUE_TYPE_VOID)
  8463. return true;
  8464. /* Change type to ANY when the stack top is ANY, so as to avoid
  8465. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8466. two offsets */
  8467. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8468. type = VALUE_TYPE_ANY;
  8469. cell_num_to_pop = wasm_value_type_cell_num(type);
  8470. /* Check the offset stack bottom to ensure the frame offset
  8471. stack will not go underflow. But we don't thrown error
  8472. and return true here, because the error msg should be
  8473. given in wasm_loader_pop_frame_ref */
  8474. if (!check_offset_pop(ctx, cell_num_to_pop))
  8475. return true;
  8476. ctx->frame_offset -= cell_num_to_pop;
  8477. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8478. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8479. ctx->dynamic_offset -= cell_num_to_pop;
  8480. emit_operand(ctx, *(ctx->frame_offset));
  8481. (void)error_buf;
  8482. (void)error_buf_size;
  8483. return true;
  8484. }
  8485. static bool
  8486. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8487. bool disable_emit, int16 operand_offset,
  8488. char *error_buf, uint32 error_buf_size)
  8489. {
  8490. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8491. error_buf, error_buf_size)))
  8492. return false;
  8493. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8494. return false;
  8495. return true;
  8496. }
  8497. static bool
  8498. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8499. char *error_buf, uint32 error_buf_size)
  8500. {
  8501. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8502. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8503. return false;
  8504. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8505. return false;
  8506. return true;
  8507. }
  8508. static bool
  8509. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8510. uint8 type_push, uint8 type_pop,
  8511. bool disable_emit, int16 operand_offset,
  8512. char *error_buf, uint32 error_buf_size)
  8513. {
  8514. uint8 i;
  8515. for (i = 0; i < pop_cnt; i++) {
  8516. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8517. error_buf_size))
  8518. return false;
  8519. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8520. error_buf_size))
  8521. return false;
  8522. }
  8523. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8524. operand_offset, error_buf,
  8525. error_buf_size))
  8526. return false;
  8527. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8528. return false;
  8529. return true;
  8530. }
  8531. static int
  8532. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  8533. {
  8534. int64 i64_const1 = *(int64 *)p_i64_const1;
  8535. int64 i64_const2 = *(int64 *)p_i64_const2;
  8536. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  8537. }
  8538. static int
  8539. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  8540. {
  8541. int32 i32_const1 = *(int32 *)p_i32_const1;
  8542. int32 i32_const2 = *(int32 *)p_i32_const2;
  8543. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  8544. }
  8545. static bool
  8546. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8547. int16 *offset, char *error_buf,
  8548. uint32 error_buf_size)
  8549. {
  8550. if (!ctx->p_code_compiled) {
  8551. /* Treat i64 and f64 as the same by reading i64 value from
  8552. the raw bytes */
  8553. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8554. /* No slot left, emit const instead */
  8555. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  8556. *offset = 0;
  8557. return true;
  8558. }
  8559. /* Traverse the list if the const num is small */
  8560. if (ctx->i64_const_num < 10) {
  8561. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  8562. if (ctx->i64_consts[i] == *(int64 *)value) {
  8563. *offset = -1;
  8564. return true;
  8565. }
  8566. }
  8567. }
  8568. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  8569. MEM_REALLOC(ctx->i64_consts,
  8570. sizeof(int64) * ctx->i64_const_max_num,
  8571. sizeof(int64) * (ctx->i64_const_max_num * 2));
  8572. ctx->i64_const_max_num *= 2;
  8573. }
  8574. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  8575. }
  8576. else {
  8577. /* Treat i32 and f32 as the same by reading i32 value from
  8578. the raw bytes */
  8579. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  8580. /* No slot left, emit const instead */
  8581. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  8582. *offset = 0;
  8583. return true;
  8584. }
  8585. /* Traverse the list if the const num is small */
  8586. if (ctx->i32_const_num < 10) {
  8587. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  8588. if (ctx->i32_consts[i] == *(int32 *)value) {
  8589. *offset = -1;
  8590. return true;
  8591. }
  8592. }
  8593. }
  8594. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  8595. MEM_REALLOC(ctx->i32_consts,
  8596. sizeof(int32) * ctx->i32_const_max_num,
  8597. sizeof(int32) * (ctx->i32_const_max_num * 2));
  8598. ctx->i32_const_max_num *= 2;
  8599. }
  8600. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  8601. }
  8602. *offset = -1;
  8603. return true;
  8604. }
  8605. else {
  8606. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8607. int64 key = *(int64 *)value, *i64_const;
  8608. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  8609. sizeof(int64), cmp_i64_const);
  8610. if (!i64_const) { /* not found, emit const instead */
  8611. *offset = 0;
  8612. return true;
  8613. }
  8614. *offset = -(uint32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  8615. + (uint32)(i64_const - ctx->i64_consts) * 2;
  8616. }
  8617. else {
  8618. int32 key = *(int32 *)value, *i32_const;
  8619. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  8620. sizeof(int32), cmp_i32_const);
  8621. if (!i32_const) { /* not found, emit const instead */
  8622. *offset = 0;
  8623. return true;
  8624. }
  8625. *offset = -(uint32)(ctx->i32_const_num)
  8626. + (uint32)(i32_const - ctx->i32_consts);
  8627. }
  8628. return true;
  8629. }
  8630. fail:
  8631. return false;
  8632. }
  8633. /*
  8634. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8635. -- Mostly used for the binary / compare operation
  8636. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8637. -- Mostly used in block / control instructions
  8638. The POP will always emit the offset on the top of the frame_offset stack
  8639. PUSH can be used in two ways:
  8640. 1. directly PUSH:
  8641. PUSH_XXX();
  8642. will allocate a dynamic space and emit
  8643. 2. silent PUSH:
  8644. operand_offset = xxx; disable_emit = true;
  8645. PUSH_XXX();
  8646. only push the frame_offset stack, no emit
  8647. */
  8648. #define TEMPLATE_PUSH(Type) \
  8649. do { \
  8650. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8651. disable_emit, operand_offset, \
  8652. error_buf, error_buf_size)) \
  8653. goto fail; \
  8654. } while (0)
  8655. #define TEMPLATE_PUSH_REF(Type) \
  8656. do { \
  8657. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8658. operand_offset, error_buf, \
  8659. error_buf_size)) \
  8660. goto fail; \
  8661. } while (0)
  8662. #define TEMPLATE_POP(Type) \
  8663. do { \
  8664. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8665. error_buf, error_buf_size)) \
  8666. goto fail; \
  8667. } while (0)
  8668. #define TEMPLATE_POP_REF(Type) \
  8669. do { \
  8670. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8671. error_buf_size)) \
  8672. goto fail; \
  8673. } while (0)
  8674. #define PUSH_OFFSET_TYPE(type) \
  8675. do { \
  8676. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8677. operand_offset, error_buf, \
  8678. error_buf_size))) \
  8679. goto fail; \
  8680. } while (0)
  8681. #define POP_OFFSET_TYPE(type) \
  8682. do { \
  8683. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8684. error_buf_size))) \
  8685. goto fail; \
  8686. } while (0)
  8687. #define POP_AND_PUSH(type_pop, type_push) \
  8688. do { \
  8689. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8690. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8691. operand_offset, error_buf, error_buf_size))) \
  8692. goto fail; \
  8693. } while (0)
  8694. /* type of POPs should be the same */
  8695. #define POP2_AND_PUSH(type_pop, type_push) \
  8696. do { \
  8697. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8698. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8699. operand_offset, error_buf, error_buf_size))) \
  8700. goto fail; \
  8701. } while (0)
  8702. #else /* WASM_ENABLE_FAST_INTERP */
  8703. #define TEMPLATE_PUSH(Type) \
  8704. do { \
  8705. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8706. error_buf, error_buf_size))) \
  8707. goto fail; \
  8708. } while (0)
  8709. #define TEMPLATE_PUSH_REF(Type) \
  8710. do { \
  8711. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8712. error_buf_size))) \
  8713. goto fail; \
  8714. } while (0)
  8715. #define TEMPLATE_POP(Type) \
  8716. do { \
  8717. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8718. error_buf, error_buf_size))) \
  8719. goto fail; \
  8720. } while (0)
  8721. #define TEMPLATE_POP_REF(Type) \
  8722. do { \
  8723. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8724. error_buf_size))) \
  8725. goto fail; \
  8726. } while (0)
  8727. #define POP_AND_PUSH(type_pop, type_push) \
  8728. do { \
  8729. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8730. type_pop, error_buf, \
  8731. error_buf_size))) \
  8732. goto fail; \
  8733. } while (0)
  8734. /* type of POPs should be the same */
  8735. #define POP2_AND_PUSH(type_pop, type_push) \
  8736. do { \
  8737. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8738. type_pop, error_buf, \
  8739. error_buf_size))) \
  8740. goto fail; \
  8741. } while (0)
  8742. #endif /* WASM_ENABLE_FAST_INTERP */
  8743. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8744. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8745. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8746. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8747. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8748. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8749. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8750. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8751. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8752. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8753. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8754. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  8755. #define POP_I32() TEMPLATE_POP(I32)
  8756. #define POP_F32() TEMPLATE_POP(F32)
  8757. #define POP_I64() TEMPLATE_POP(I64)
  8758. #define POP_F64() TEMPLATE_POP(F64)
  8759. #define POP_V128() TEMPLATE_POP(V128)
  8760. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8761. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8762. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8763. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8764. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  8765. #if WASM_ENABLE_FAST_INTERP != 0
  8766. static bool
  8767. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8768. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8769. {
  8770. int16 operand_offset = 0;
  8771. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8772. : loader_ctx->frame_csp;
  8773. BlockType *block_type = &block->block_type;
  8774. uint8 *return_types = NULL;
  8775. #if WASM_ENABLE_GC != 0
  8776. WASMRefTypeMap *reftype_maps = NULL;
  8777. uint32 reftype_map_count;
  8778. #endif
  8779. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8780. int32 i = 0;
  8781. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8782. *frame_offset_org = NULL;
  8783. #if WASM_ENABLE_GC == 0
  8784. return_count = block_type_get_result_types(block_type, &return_types);
  8785. #else
  8786. return_count = block_type_get_result_types(
  8787. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8788. #endif
  8789. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8790. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8791. if (return_count == 1) {
  8792. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8793. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8794. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8795. /* insert op_copy before else opcode */
  8796. if (opcode == WASM_OP_ELSE)
  8797. skip_label();
  8798. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8799. : EXT_OP_COPY_STACK_TOP_I64);
  8800. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8801. emit_operand(loader_ctx, block->dynamic_offset);
  8802. if (opcode == WASM_OP_ELSE) {
  8803. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8804. }
  8805. else {
  8806. loader_ctx->frame_offset -= cell;
  8807. loader_ctx->dynamic_offset = block->dynamic_offset;
  8808. PUSH_OFFSET_TYPE(return_types[0]);
  8809. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8810. }
  8811. if (opcode == WASM_OP_ELSE)
  8812. emit_label(opcode);
  8813. }
  8814. return true;
  8815. }
  8816. /* Copy stack top values to block's results which are in dynamic space.
  8817. * The instruction format:
  8818. * Part a: values count
  8819. * Part b: all values total cell num
  8820. * Part c: each value's cell_num, src offset and dst offset
  8821. * Part d: each value's src offset and dst offset
  8822. * Part e: each value's dst offset
  8823. */
  8824. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8825. dynamic_offset = dynamic_offset_org =
  8826. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8827. /* First traversal to get the count of values needed to be copied. */
  8828. for (i = (int32)return_count - 1; i >= 0; i--) {
  8829. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8830. frame_offset -= cells;
  8831. dynamic_offset -= cells;
  8832. if (dynamic_offset != *frame_offset) {
  8833. value_count++;
  8834. total_cel_num += cells;
  8835. }
  8836. }
  8837. if (value_count) {
  8838. uint32 j = 0;
  8839. uint8 *emit_data = NULL, *cells = NULL;
  8840. int16 *src_offsets = NULL;
  8841. uint16 *dst_offsets = NULL;
  8842. uint64 size =
  8843. (uint64)value_count
  8844. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8845. /* Allocate memory for the emit data */
  8846. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8847. return false;
  8848. cells = emit_data;
  8849. src_offsets = (int16 *)(cells + value_count);
  8850. dst_offsets = (uint16 *)(src_offsets + value_count);
  8851. /* insert op_copy before else opcode */
  8852. if (opcode == WASM_OP_ELSE)
  8853. skip_label();
  8854. emit_label(EXT_OP_COPY_STACK_VALUES);
  8855. /* Part a) */
  8856. emit_uint32(loader_ctx, value_count);
  8857. /* Part b) */
  8858. emit_uint32(loader_ctx, total_cel_num);
  8859. /* Second traversal to get each value's cell num, src offset and dst
  8860. * offset. */
  8861. frame_offset = frame_offset_org;
  8862. dynamic_offset = dynamic_offset_org;
  8863. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8864. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8865. frame_offset -= cell;
  8866. dynamic_offset -= cell;
  8867. if (dynamic_offset != *frame_offset) {
  8868. /* cell num */
  8869. cells[j] = cell;
  8870. /* src offset */
  8871. src_offsets[j] = *frame_offset;
  8872. /* dst offset */
  8873. dst_offsets[j] = dynamic_offset;
  8874. j++;
  8875. }
  8876. if (opcode == WASM_OP_ELSE) {
  8877. *frame_offset = dynamic_offset;
  8878. }
  8879. else {
  8880. loader_ctx->frame_offset = frame_offset;
  8881. loader_ctx->dynamic_offset = dynamic_offset;
  8882. if (!(wasm_loader_push_frame_offset(
  8883. loader_ctx, return_types[i], disable_emit,
  8884. operand_offset, error_buf, error_buf_size))) {
  8885. wasm_runtime_free(emit_data);
  8886. goto fail;
  8887. }
  8888. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8889. loader_ctx->frame_offset = frame_offset_org;
  8890. loader_ctx->dynamic_offset = dynamic_offset_org;
  8891. }
  8892. }
  8893. bh_assert(j == value_count);
  8894. /* Emit the cells, src_offsets and dst_offsets */
  8895. for (j = 0; j < value_count; j++)
  8896. emit_byte(loader_ctx, cells[j]);
  8897. for (j = 0; j < value_count; j++)
  8898. emit_operand(loader_ctx, src_offsets[j]);
  8899. for (j = 0; j < value_count; j++)
  8900. emit_operand(loader_ctx, dst_offsets[j]);
  8901. if (opcode == WASM_OP_ELSE)
  8902. emit_label(opcode);
  8903. wasm_runtime_free(emit_data);
  8904. }
  8905. return true;
  8906. fail:
  8907. return false;
  8908. }
  8909. #endif /* WASM_ENABLE_FAST_INTERP */
  8910. #define PUSH_TYPE(type) \
  8911. do { \
  8912. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8913. error_buf_size))) \
  8914. goto fail; \
  8915. } while (0)
  8916. #define POP_TYPE(type) \
  8917. do { \
  8918. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8919. error_buf_size))) \
  8920. goto fail; \
  8921. } while (0)
  8922. #if WASM_ENABLE_GC == 0
  8923. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8924. do { \
  8925. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8926. _start_addr, error_buf, \
  8927. error_buf_size)) \
  8928. goto fail; \
  8929. } while (0)
  8930. #define POP_CSP() \
  8931. do { \
  8932. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8933. goto fail; \
  8934. } while (0)
  8935. #else
  8936. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8937. do { \
  8938. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8939. _start_addr, error_buf, \
  8940. error_buf_size)) \
  8941. goto fail; \
  8942. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8943. error_buf, error_buf_size)) { \
  8944. goto fail; \
  8945. } \
  8946. } while (0)
  8947. #define POP_CSP() \
  8948. do { \
  8949. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8950. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8951. goto fail; \
  8952. } while (0)
  8953. #endif /* end of WASM_ENABLE_GC == 0 */
  8954. #if WASM_ENABLE_GC == 0
  8955. #define GET_LOCAL_REFTYPE() (void)0
  8956. #else
  8957. #define GET_LOCAL_REFTYPE() \
  8958. do { \
  8959. if (wasm_is_type_multi_byte_type(local_type)) { \
  8960. WASMRefType *_ref_type; \
  8961. if (local_idx < param_count) \
  8962. _ref_type = wasm_reftype_map_find( \
  8963. param_reftype_maps, param_reftype_map_count, local_idx); \
  8964. else \
  8965. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8966. local_reftype_map_count, \
  8967. local_idx - param_count); \
  8968. bh_assert(_ref_type); \
  8969. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8970. wasm_reftype_struct_size(_ref_type)); \
  8971. } \
  8972. } while (0)
  8973. #endif /* end of WASM_ENABLE_GC == 0 */
  8974. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8975. do { \
  8976. read_leb_uint32(p, p_end, local_idx); \
  8977. if (local_idx >= param_count + local_count) { \
  8978. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8979. goto fail; \
  8980. } \
  8981. local_type = local_idx < param_count \
  8982. ? param_types[local_idx] \
  8983. : local_types[local_idx - param_count]; \
  8984. local_offset = local_offsets[local_idx]; \
  8985. GET_LOCAL_REFTYPE(); \
  8986. } while (0)
  8987. static bool
  8988. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8989. {
  8990. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8991. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8992. return false;
  8993. }
  8994. return true;
  8995. }
  8996. #define CHECK_MEMORY() \
  8997. do { \
  8998. if (!check_memory(module, error_buf, error_buf_size)) \
  8999. goto fail; \
  9000. } while (0)
  9001. static bool
  9002. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9003. uint32 error_buf_size)
  9004. {
  9005. uint8 mem_access_aligns[] = {
  9006. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9007. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9008. };
  9009. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9010. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9011. set_error_buf(error_buf, error_buf_size,
  9012. "invalid memop flags: alignment must not be larger "
  9013. "than natural");
  9014. return false;
  9015. }
  9016. return true;
  9017. }
  9018. #if WASM_ENABLE_SIMD != 0
  9019. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  9020. static bool
  9021. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9022. uint32 error_buf_size)
  9023. {
  9024. uint8 mem_access_aligns[] = {
  9025. 4, /* load */
  9026. 3, 3, 3, 3, 3, 3, /* load and extend */
  9027. 0, 1, 2, 3, /* load and splat */
  9028. 4, /* store */
  9029. };
  9030. uint8 mem_access_aligns_load_lane[] = {
  9031. 0, 1, 2, 3, /* load lane */
  9032. 0, 1, 2, 3, /* store lane */
  9033. 2, 3 /* store zero */
  9034. };
  9035. if (!((opcode <= SIMD_v128_store)
  9036. || (SIMD_v128_load8_lane <= opcode
  9037. && opcode <= SIMD_v128_load64_zero))) {
  9038. set_error_buf(error_buf, error_buf_size,
  9039. "the opcode doesn't include memarg");
  9040. return false;
  9041. }
  9042. if ((opcode <= SIMD_v128_store
  9043. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9044. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9045. && align > mem_access_aligns_load_lane[opcode
  9046. - SIMD_v128_load8_lane])) {
  9047. set_error_buf(error_buf, error_buf_size,
  9048. "invalid memop flags: alignment must not be larger "
  9049. "than natural");
  9050. return false;
  9051. }
  9052. return true;
  9053. }
  9054. static bool
  9055. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9056. uint32 error_buf_size)
  9057. {
  9058. switch (opcode) {
  9059. case SIMD_i8x16_extract_lane_s:
  9060. case SIMD_i8x16_extract_lane_u:
  9061. case SIMD_i8x16_replace_lane:
  9062. if (lane >= 16) {
  9063. goto fail;
  9064. }
  9065. break;
  9066. case SIMD_i16x8_extract_lane_s:
  9067. case SIMD_i16x8_extract_lane_u:
  9068. case SIMD_i16x8_replace_lane:
  9069. if (lane >= 8) {
  9070. goto fail;
  9071. }
  9072. break;
  9073. case SIMD_i32x4_extract_lane:
  9074. case SIMD_i32x4_replace_lane:
  9075. case SIMD_f32x4_extract_lane:
  9076. case SIMD_f32x4_replace_lane:
  9077. if (lane >= 4) {
  9078. goto fail;
  9079. }
  9080. break;
  9081. case SIMD_i64x2_extract_lane:
  9082. case SIMD_i64x2_replace_lane:
  9083. case SIMD_f64x2_extract_lane:
  9084. case SIMD_f64x2_replace_lane:
  9085. if (lane >= 2) {
  9086. goto fail;
  9087. }
  9088. break;
  9089. case SIMD_v128_load8_lane:
  9090. case SIMD_v128_load16_lane:
  9091. case SIMD_v128_load32_lane:
  9092. case SIMD_v128_load64_lane:
  9093. case SIMD_v128_store8_lane:
  9094. case SIMD_v128_store16_lane:
  9095. case SIMD_v128_store32_lane:
  9096. case SIMD_v128_store64_lane:
  9097. case SIMD_v128_load32_zero:
  9098. case SIMD_v128_load64_zero:
  9099. {
  9100. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9101. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9102. goto fail;
  9103. }
  9104. break;
  9105. }
  9106. default:
  9107. goto fail;
  9108. }
  9109. return true;
  9110. fail:
  9111. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9112. return false;
  9113. }
  9114. static bool
  9115. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9116. {
  9117. uint8 i;
  9118. for (i = 0; i != 16; ++i) {
  9119. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9120. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9121. return false;
  9122. }
  9123. }
  9124. return true;
  9125. }
  9126. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9127. #endif /* end of WASM_ENABLE_SIMD */
  9128. #if WASM_ENABLE_SHARED_MEMORY != 0
  9129. static bool
  9130. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9131. uint32 error_buf_size)
  9132. {
  9133. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9134. uint8 mem_access_aligns[] = {
  9135. 2, 3, 0, 1, 0, 1, 2,
  9136. };
  9137. uint8 expect;
  9138. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9139. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9140. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9141. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9142. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9143. }
  9144. else {
  9145. /* 7 opcodes in every group */
  9146. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9147. }
  9148. if (align != expect) {
  9149. set_error_buf(error_buf, error_buf_size,
  9150. "alignment isn't equal to natural");
  9151. return false;
  9152. }
  9153. return true;
  9154. }
  9155. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9156. static bool
  9157. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9158. char *error_buf, uint32 error_buf_size)
  9159. {
  9160. BranchBlock *target_block, *cur_block;
  9161. BlockType *target_block_type;
  9162. uint8 type, *types = NULL, *frame_ref;
  9163. uint32 arity = 0;
  9164. int32 i, available_stack_cell;
  9165. uint16 cell_num;
  9166. #if WASM_ENABLE_GC != 0
  9167. WASMRefTypeMap *frame_reftype_map;
  9168. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9169. WASMRefType *ref_type;
  9170. uint32 reftype_map_count = 0;
  9171. int32 available_reftype_map;
  9172. bool is_type_multi_byte;
  9173. #endif
  9174. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9175. uint8 *frame_ref_after_popped = NULL;
  9176. uint8 frame_ref_tmp[4] = { 0 };
  9177. uint8 *frame_ref_buf = frame_ref_tmp;
  9178. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9179. #if WASM_ENABLE_GC != 0
  9180. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9181. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9182. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9183. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9184. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9185. #endif
  9186. #if WASM_ENABLE_FAST_INTERP != 0
  9187. int16 *frame_offset_old = loader_ctx->frame_offset;
  9188. int16 *frame_offset_after_popped = NULL;
  9189. int16 frame_offset_tmp[4] = { 0 };
  9190. int16 *frame_offset_buf = frame_offset_tmp;
  9191. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9192. #endif
  9193. bool ret = false;
  9194. bh_assert(loader_ctx->csp_num > 0);
  9195. if (loader_ctx->csp_num - 1 < depth) {
  9196. set_error_buf(error_buf, error_buf_size,
  9197. "unknown label, "
  9198. "unexpected end of section or function");
  9199. return false;
  9200. }
  9201. cur_block = loader_ctx->frame_csp - 1;
  9202. target_block = loader_ctx->frame_csp - (depth + 1);
  9203. target_block_type = &target_block->block_type;
  9204. frame_ref = loader_ctx->frame_ref;
  9205. #if WASM_ENABLE_GC != 0
  9206. frame_reftype_map = loader_ctx->frame_reftype_map;
  9207. #endif
  9208. /* Note: loop's arity is different from if and block. loop's arity is
  9209. * its parameter count while if and block arity is result count.
  9210. */
  9211. #if WASM_ENABLE_GC == 0
  9212. if (target_block->label_type == LABEL_TYPE_LOOP)
  9213. arity = block_type_get_param_types(target_block_type, &types);
  9214. else
  9215. arity = block_type_get_result_types(target_block_type, &types);
  9216. #else
  9217. if (target_block->label_type == LABEL_TYPE_LOOP)
  9218. arity = block_type_get_param_types(target_block_type, &types,
  9219. &reftype_maps, &reftype_map_count);
  9220. else
  9221. arity = block_type_get_result_types(target_block_type, &types,
  9222. &reftype_maps, &reftype_map_count);
  9223. #endif
  9224. /* If the stack is in polymorphic state, just clear the stack
  9225. * and then re-push the values to make the stack top values
  9226. * match block type. */
  9227. if (cur_block->is_stack_polymorphic) {
  9228. #if WASM_ENABLE_GC != 0
  9229. int32 j = reftype_map_count - 1;
  9230. #endif
  9231. for (i = (int32)arity - 1; i >= 0; i--) {
  9232. #if WASM_ENABLE_GC != 0
  9233. if (wasm_is_type_multi_byte_type(types[i])) {
  9234. bh_assert(reftype_maps[j].index == i);
  9235. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9236. reftype_maps[j].ref_type,
  9237. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9238. j--;
  9239. }
  9240. #endif
  9241. #if WASM_ENABLE_FAST_INTERP != 0
  9242. POP_OFFSET_TYPE(types[i]);
  9243. #endif
  9244. POP_TYPE(types[i]);
  9245. }
  9246. /* Backup stack data since it may be changed in the below
  9247. push operations, and the stack data may be used when
  9248. checking other target blocks of opcode br_table */
  9249. if (opcode == WASM_OP_BR_TABLE) {
  9250. uint64 total_size;
  9251. frame_ref_after_popped = loader_ctx->frame_ref;
  9252. total_size = (uint64)sizeof(uint8)
  9253. * (frame_ref_old - frame_ref_after_popped);
  9254. if (total_size > sizeof(frame_ref_tmp)
  9255. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9256. error_buf_size))) {
  9257. goto fail;
  9258. }
  9259. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9260. frame_ref_after_popped, (uint32)total_size);
  9261. #if WASM_ENABLE_GC != 0
  9262. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9263. total_size =
  9264. (uint64)sizeof(WASMRefTypeMap)
  9265. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9266. if (total_size > sizeof(frame_reftype_map_tmp)
  9267. && !(frame_reftype_map_buf = loader_malloc(
  9268. total_size, error_buf, error_buf_size))) {
  9269. goto fail;
  9270. }
  9271. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9272. frame_reftype_map_after_popped, (uint32)total_size);
  9273. #endif
  9274. #if WASM_ENABLE_FAST_INTERP != 0
  9275. frame_offset_after_popped = loader_ctx->frame_offset;
  9276. total_size = (uint64)sizeof(int16)
  9277. * (frame_offset_old - frame_offset_after_popped);
  9278. if (total_size > sizeof(frame_offset_tmp)
  9279. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9280. error_buf_size))) {
  9281. goto fail;
  9282. }
  9283. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9284. frame_offset_after_popped, (uint32)total_size);
  9285. #endif
  9286. }
  9287. #if WASM_ENABLE_GC != 0
  9288. j = 0;
  9289. #endif
  9290. for (i = 0; i < (int32)arity; i++) {
  9291. #if WASM_ENABLE_GC != 0
  9292. if (wasm_is_type_multi_byte_type(types[i])) {
  9293. bh_assert(reftype_maps[j].index == i);
  9294. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9295. reftype_maps[j].ref_type,
  9296. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9297. j++;
  9298. }
  9299. #endif
  9300. #if WASM_ENABLE_FAST_INTERP != 0
  9301. bool disable_emit = true;
  9302. int16 operand_offset = 0;
  9303. PUSH_OFFSET_TYPE(types[i]);
  9304. #endif
  9305. PUSH_TYPE(types[i]);
  9306. }
  9307. #if WASM_ENABLE_FAST_INTERP != 0
  9308. emit_br_info(target_block, opcode == WASM_OP_BR);
  9309. #endif
  9310. /* Restore the stack data, note that frame_ref_bottom,
  9311. frame_reftype_map_bottom, frame_offset_bottom may be
  9312. re-allocated in the above push operations */
  9313. if (opcode == WASM_OP_BR_TABLE) {
  9314. uint32 total_size;
  9315. /* The stack operand num should not be smaller than before
  9316. after pop and push operations */
  9317. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9318. loader_ctx->stack_cell_num = stack_cell_num_old;
  9319. loader_ctx->frame_ref =
  9320. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9321. total_size = (uint32)(sizeof(uint8)
  9322. * (frame_ref_old - frame_ref_after_popped));
  9323. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9324. frame_ref_buf, total_size);
  9325. #if WASM_ENABLE_GC != 0
  9326. /* The stack operand num should not be smaller than before
  9327. after pop and push operations */
  9328. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9329. loader_ctx->reftype_map_num = reftype_map_num_old;
  9330. loader_ctx->frame_reftype_map =
  9331. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9332. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9333. * (frame_reftype_map_old
  9334. - frame_reftype_map_after_popped));
  9335. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9336. total_size, frame_reftype_map_buf, total_size);
  9337. #endif
  9338. #if WASM_ENABLE_FAST_INTERP != 0
  9339. loader_ctx->frame_offset =
  9340. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9341. total_size =
  9342. (uint32)(sizeof(int16)
  9343. * (frame_offset_old - frame_offset_after_popped));
  9344. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9345. total_size, frame_offset_buf, total_size);
  9346. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9347. #endif
  9348. }
  9349. ret = true;
  9350. goto cleanup_and_return;
  9351. }
  9352. available_stack_cell =
  9353. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9354. #if WASM_ENABLE_GC != 0
  9355. available_reftype_map =
  9356. (int32)(loader_ctx->reftype_map_num
  9357. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9358. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9359. #endif
  9360. /* Check stack top values match target block type */
  9361. for (i = (int32)arity - 1; i >= 0; i--) {
  9362. type = types[i];
  9363. #if WASM_ENABLE_GC != 0
  9364. ref_type = NULL;
  9365. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9366. if (is_type_multi_byte) {
  9367. bh_assert(reftype_map);
  9368. ref_type = reftype_map->ref_type;
  9369. }
  9370. #endif
  9371. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9372. break;
  9373. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9374. #if WASM_ENABLE_GC != 0
  9375. frame_reftype_map, available_reftype_map,
  9376. #endif
  9377. type,
  9378. #if WASM_ENABLE_GC != 0
  9379. ref_type,
  9380. #endif
  9381. error_buf, error_buf_size)) {
  9382. goto fail;
  9383. }
  9384. cell_num = wasm_value_type_cell_num(types[i]);
  9385. frame_ref -= cell_num;
  9386. available_stack_cell -= cell_num;
  9387. #if WASM_ENABLE_GC != 0
  9388. if (is_type_multi_byte) {
  9389. frame_reftype_map--;
  9390. available_reftype_map--;
  9391. reftype_map--;
  9392. }
  9393. #endif
  9394. }
  9395. #if WASM_ENABLE_FAST_INTERP != 0
  9396. emit_br_info(target_block, opcode == WASM_OP_BR);
  9397. #endif
  9398. ret = true;
  9399. cleanup_and_return:
  9400. fail:
  9401. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9402. wasm_runtime_free(frame_ref_buf);
  9403. #if WASM_ENABLE_GC != 0
  9404. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9405. wasm_runtime_free(frame_reftype_map_buf);
  9406. #endif
  9407. #if WASM_ENABLE_FAST_INTERP != 0
  9408. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9409. wasm_runtime_free(frame_offset_buf);
  9410. #endif
  9411. return ret;
  9412. }
  9413. static BranchBlock *
  9414. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9415. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9416. {
  9417. uint8 *p = *p_buf, *p_end = buf_end;
  9418. BranchBlock *frame_csp_tmp;
  9419. uint32 depth;
  9420. read_leb_uint32(p, p_end, depth);
  9421. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9422. error_buf_size)) {
  9423. goto fail;
  9424. }
  9425. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9426. *p_buf = p;
  9427. return frame_csp_tmp;
  9428. fail:
  9429. return NULL;
  9430. }
  9431. #if WASM_ENABLE_EXCE_HANDLING != 0
  9432. static BranchBlock *
  9433. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9434. uint8 *buf_end, char *error_buf,
  9435. uint32 error_buf_size)
  9436. {
  9437. uint8 *p = *p_buf, *p_end = buf_end;
  9438. BranchBlock *frame_csp_tmp;
  9439. uint32 depth;
  9440. read_leb_uint32(p, p_end, depth);
  9441. /*
  9442. * Note: "delegate 0" means the surrounding block, not the
  9443. * try-delegate block itself.
  9444. *
  9445. * Note: the caller hasn't popped the try-delegate frame yet.
  9446. */
  9447. bh_assert(loader_ctx->csp_num > 0);
  9448. if (loader_ctx->csp_num - 1 <= depth) {
  9449. #if WASM_ENABLE_SPEC_TEST == 0
  9450. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9451. #else
  9452. set_error_buf(error_buf, error_buf_size, "unknown label");
  9453. #endif
  9454. goto fail;
  9455. }
  9456. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9457. #if WASM_ENABLE_FAST_INTERP != 0
  9458. emit_br_info(frame_csp_tmp, false);
  9459. #endif
  9460. *p_buf = p;
  9461. return frame_csp_tmp;
  9462. fail:
  9463. return NULL;
  9464. }
  9465. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9466. static bool
  9467. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9468. char *error_buf, uint32 error_buf_size)
  9469. {
  9470. BlockType *block_type = &block->block_type;
  9471. uint8 *return_types = NULL;
  9472. uint32 return_count = 0;
  9473. int32 available_stack_cell, return_cell_num, i;
  9474. uint8 *frame_ref = NULL;
  9475. #if WASM_ENABLE_GC != 0
  9476. WASMRefTypeMap *frame_reftype_map;
  9477. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9478. WASMRefType *ref_type;
  9479. uint32 param_count, return_reftype_map_count = 0;
  9480. int32 available_reftype_map =
  9481. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9482. #endif
  9483. available_stack_cell =
  9484. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9485. #if WASM_ENABLE_GC == 0
  9486. return_count = block_type_get_result_types(block_type, &return_types);
  9487. #else
  9488. return_count = block_type_get_result_types(block_type, &return_types,
  9489. &return_reftype_maps,
  9490. &return_reftype_map_count);
  9491. param_count =
  9492. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9493. (void)param_count;
  9494. #endif
  9495. return_cell_num =
  9496. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9497. /* If the stack is in polymorphic state, just clear the stack
  9498. * and then re-push the values to make the stack top values
  9499. * match block type. */
  9500. if (block->is_stack_polymorphic) {
  9501. #if WASM_ENABLE_GC != 0
  9502. int32 j = return_reftype_map_count - 1;
  9503. #endif
  9504. for (i = (int32)return_count - 1; i >= 0; i--) {
  9505. #if WASM_ENABLE_GC != 0
  9506. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9507. bh_assert(return_reftype_maps[j].index == i + param_count);
  9508. bh_memcpy_s(
  9509. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9510. return_reftype_maps[j].ref_type,
  9511. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9512. j--;
  9513. }
  9514. #endif
  9515. #if WASM_ENABLE_FAST_INTERP != 0
  9516. POP_OFFSET_TYPE(return_types[i]);
  9517. #endif
  9518. POP_TYPE(return_types[i]);
  9519. }
  9520. /* Check stack is empty */
  9521. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9522. set_error_buf(
  9523. error_buf, error_buf_size,
  9524. "type mismatch: stack size does not match block type");
  9525. goto fail;
  9526. }
  9527. #if WASM_ENABLE_GC != 0
  9528. j = 0;
  9529. #endif
  9530. for (i = 0; i < (int32)return_count; i++) {
  9531. #if WASM_ENABLE_GC != 0
  9532. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9533. bh_assert(return_reftype_maps[j].index == i + param_count);
  9534. bh_memcpy_s(
  9535. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9536. return_reftype_maps[j].ref_type,
  9537. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9538. j++;
  9539. }
  9540. #endif
  9541. #if WASM_ENABLE_FAST_INTERP != 0
  9542. bool disable_emit = true;
  9543. int16 operand_offset = 0;
  9544. PUSH_OFFSET_TYPE(return_types[i]);
  9545. #endif
  9546. PUSH_TYPE(return_types[i]);
  9547. }
  9548. return true;
  9549. }
  9550. if (available_stack_cell != return_cell_num) {
  9551. #if WASM_ENABLE_EXCE_HANDLING != 0
  9552. /* testspec: this error message format is expected by try_catch.wast */
  9553. snprintf(
  9554. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9555. block->label_type == LABEL_TYPE_TRY
  9556. || (block->label_type == LABEL_TYPE_CATCH
  9557. && return_cell_num > 0)
  9558. ? "instruction"
  9559. : "block",
  9560. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9561. " but stack has ",
  9562. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9563. : "");
  9564. goto fail;
  9565. #else
  9566. set_error_buf(error_buf, error_buf_size,
  9567. "type mismatch: stack size does not match block type");
  9568. goto fail;
  9569. #endif
  9570. }
  9571. /* Check stack values match return types */
  9572. frame_ref = loader_ctx->frame_ref;
  9573. #if WASM_ENABLE_GC != 0
  9574. frame_reftype_map = loader_ctx->frame_reftype_map;
  9575. return_reftype_map =
  9576. return_reftype_map_count
  9577. ? return_reftype_maps + return_reftype_map_count - 1
  9578. : NULL;
  9579. #endif
  9580. for (i = (int32)return_count - 1; i >= 0; i--) {
  9581. uint8 type = return_types[i];
  9582. #if WASM_ENABLE_GC != 0
  9583. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9584. ref_type = NULL;
  9585. if (is_type_multi_byte) {
  9586. bh_assert(return_reftype_map);
  9587. ref_type = return_reftype_map->ref_type;
  9588. }
  9589. #endif
  9590. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9591. #if WASM_ENABLE_GC != 0
  9592. frame_reftype_map, available_reftype_map,
  9593. #endif
  9594. type,
  9595. #if WASM_ENABLE_GC != 0
  9596. ref_type,
  9597. #endif
  9598. error_buf, error_buf_size))
  9599. return false;
  9600. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9601. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9602. #if WASM_ENABLE_GC != 0
  9603. if (is_type_multi_byte) {
  9604. frame_reftype_map--;
  9605. available_reftype_map--;
  9606. return_reftype_map--;
  9607. }
  9608. #endif
  9609. }
  9610. return true;
  9611. fail:
  9612. return false;
  9613. }
  9614. #if WASM_ENABLE_FAST_INTERP != 0
  9615. /* Copy parameters to dynamic space.
  9616. * 1) POP original parameter out;
  9617. * 2) Push and copy original values to dynamic space.
  9618. * The copy instruction format:
  9619. * Part a: param count
  9620. * Part b: all param total cell num
  9621. * Part c: each param's cell_num, src offset and dst offset
  9622. * Part d: each param's src offset
  9623. * Part e: each param's dst offset
  9624. */
  9625. static bool
  9626. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9627. uint32 error_buf_size)
  9628. {
  9629. bool ret = false;
  9630. int16 *frame_offset = NULL;
  9631. uint8 *cells = NULL, cell;
  9632. int16 *src_offsets = NULL;
  9633. uint8 *emit_data = NULL;
  9634. uint32 i;
  9635. BranchBlock *block = loader_ctx->frame_csp - 1;
  9636. BlockType *block_type = &block->block_type;
  9637. WASMFuncType *wasm_type = block_type->u.type;
  9638. uint32 param_count = block_type->u.type->param_count;
  9639. int16 condition_offset = 0;
  9640. bool disable_emit = false;
  9641. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9642. int16 operand_offset = 0;
  9643. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9644. bh_assert(size > 0);
  9645. /* For if block, we also need copy the condition operand offset. */
  9646. if (is_if_block)
  9647. size += sizeof(*cells) + sizeof(*src_offsets);
  9648. /* Allocate memory for the emit data */
  9649. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9650. return false;
  9651. cells = emit_data;
  9652. src_offsets = (int16 *)(cells + param_count);
  9653. if (is_if_block)
  9654. condition_offset = *loader_ctx->frame_offset;
  9655. /* POP original parameter out */
  9656. for (i = 0; i < param_count; i++) {
  9657. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9658. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9659. }
  9660. frame_offset = loader_ctx->frame_offset;
  9661. /* Get each param's cell num and src offset */
  9662. for (i = 0; i < param_count; i++) {
  9663. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9664. cells[i] = cell;
  9665. src_offsets[i] = *frame_offset;
  9666. frame_offset += cell;
  9667. }
  9668. /* emit copy instruction */
  9669. emit_label(EXT_OP_COPY_STACK_VALUES);
  9670. /* Part a) */
  9671. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9672. /* Part b) */
  9673. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9674. : wasm_type->param_cell_num);
  9675. /* Part c) */
  9676. for (i = 0; i < param_count; i++)
  9677. emit_byte(loader_ctx, cells[i]);
  9678. if (is_if_block)
  9679. emit_byte(loader_ctx, 1);
  9680. /* Part d) */
  9681. for (i = 0; i < param_count; i++)
  9682. emit_operand(loader_ctx, src_offsets[i]);
  9683. if (is_if_block)
  9684. emit_operand(loader_ctx, condition_offset);
  9685. /* Since the start offset to save the block's params and
  9686. * the start offset to save the block's results may be
  9687. * different, we remember the dynamic offset for loop block
  9688. * so that we can use it to copy the stack operands to the
  9689. * loop block's params in wasm_loader_emit_br_info. */
  9690. if (block->label_type == LABEL_TYPE_LOOP)
  9691. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9692. /* Part e) */
  9693. /* Push to dynamic space. The push will emit the dst offset. */
  9694. for (i = 0; i < param_count; i++)
  9695. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9696. if (is_if_block)
  9697. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9698. ret = true;
  9699. fail:
  9700. /* Free the emit data */
  9701. wasm_runtime_free(emit_data);
  9702. return ret;
  9703. }
  9704. #endif
  9705. #if WASM_ENABLE_GC == 0
  9706. #define RESET_REFTYPE_MAP_STACK() (void)0
  9707. #else
  9708. #define RESET_REFTYPE_MAP_STACK() \
  9709. do { \
  9710. loader_ctx->reftype_map_num = \
  9711. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9712. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9713. + loader_ctx->reftype_map_num; \
  9714. } while (0)
  9715. #endif
  9716. /* reset the stack to the state of before entering the last block */
  9717. #if WASM_ENABLE_FAST_INTERP != 0
  9718. #define RESET_STACK() \
  9719. do { \
  9720. loader_ctx->stack_cell_num = \
  9721. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9722. loader_ctx->frame_ref = \
  9723. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9724. loader_ctx->frame_offset = \
  9725. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9726. RESET_REFTYPE_MAP_STACK(); \
  9727. } while (0)
  9728. #else
  9729. #define RESET_STACK() \
  9730. do { \
  9731. loader_ctx->stack_cell_num = \
  9732. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9733. loader_ctx->frame_ref = \
  9734. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9735. RESET_REFTYPE_MAP_STACK(); \
  9736. } while (0)
  9737. #endif
  9738. /* set current block's stack polymorphic state */
  9739. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9740. do { \
  9741. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9742. _cur_block->is_stack_polymorphic = flag; \
  9743. } while (0)
  9744. #define BLOCK_HAS_PARAM(block_type) \
  9745. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9746. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9747. do { \
  9748. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9749. error_buf_size))) { \
  9750. goto fail; \
  9751. } \
  9752. } while (0)
  9753. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9754. static bool
  9755. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9756. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9757. uint32 error_buf_size)
  9758. {
  9759. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9760. return false;
  9761. }
  9762. if (table_idx < module->import_table_count) {
  9763. if (p_elem_type)
  9764. *p_elem_type =
  9765. module->import_tables[table_idx].u.table.table_type.elem_type;
  9766. #if WASM_ENABLE_GC != 0
  9767. if (p_ref_type)
  9768. *((WASMRefType **)p_ref_type) =
  9769. module->import_tables[table_idx]
  9770. .u.table.table_type.elem_ref_type;
  9771. #endif
  9772. }
  9773. else {
  9774. if (p_elem_type)
  9775. *p_elem_type =
  9776. module->tables[table_idx - module->import_table_count]
  9777. .table_type.elem_type;
  9778. #if WASM_ENABLE_GC != 0
  9779. if (p_ref_type)
  9780. *((WASMRefType **)p_ref_type) =
  9781. module->tables[table_idx - module->import_table_count]
  9782. .table_type.elem_ref_type;
  9783. #endif
  9784. }
  9785. return true;
  9786. }
  9787. static bool
  9788. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9789. uint8 *p_elem_type, void **p_elem_ref_type,
  9790. char *error_buf, uint32 error_buf_size)
  9791. {
  9792. if (table_seg_idx >= module->table_seg_count) {
  9793. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9794. table_seg_idx);
  9795. return false;
  9796. }
  9797. if (p_elem_type) {
  9798. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9799. }
  9800. #if WASM_ENABLE_GC != 0
  9801. if (p_elem_ref_type)
  9802. *((WASMRefType **)p_elem_ref_type) =
  9803. module->table_segments[table_seg_idx].elem_ref_type;
  9804. #endif
  9805. return true;
  9806. }
  9807. #endif
  9808. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9809. const uint8 *
  9810. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9811. uint32 *len)
  9812. {
  9813. WASMCustomSection *section = module->custom_section_list;
  9814. while (section) {
  9815. if ((section->name_len == strlen(name))
  9816. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9817. if (len) {
  9818. *len = section->content_len;
  9819. }
  9820. return section->content_addr;
  9821. }
  9822. section = section->next;
  9823. }
  9824. return NULL;
  9825. }
  9826. #endif
  9827. #if 0
  9828. #define HANDLE_OPCODE(opcode) #opcode
  9829. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9830. #undef HANDLE_OPCODE
  9831. #endif
  9832. #if WASM_ENABLE_FAST_INTERP == 0
  9833. #define pb_read_leb_uint32 read_leb_uint32
  9834. #define pb_read_leb_int32 read_leb_int32
  9835. #define pb_read_leb_int64 read_leb_int64
  9836. #define pb_read_leb_memarg read_leb_memarg
  9837. #define pb_read_leb_mem_offset read_leb_mem_offset
  9838. #else
  9839. /* Read leb without malformed format check */
  9840. static uint64
  9841. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  9842. {
  9843. uint8 *buf = *p_buf;
  9844. uint64 result = 0, byte = 0;
  9845. uint32 shift = 0;
  9846. do {
  9847. byte = *buf++;
  9848. result |= ((byte & 0x7f) << shift);
  9849. shift += 7;
  9850. } while (byte & 0x80);
  9851. if (sign && (shift < maxbits) && (byte & 0x40)) {
  9852. /* Sign extend */
  9853. result |= (~((uint64)0)) << shift;
  9854. }
  9855. *p_buf = buf;
  9856. return result;
  9857. }
  9858. #define pb_read_leb_uint32(p, p_end, res) \
  9859. do { \
  9860. if (!loader_ctx->p_code_compiled) \
  9861. /* Enable format check in the first scan */ \
  9862. read_leb_uint32(p, p_end, res); \
  9863. else \
  9864. /* Disable format check in the second scan */ \
  9865. res = (uint32)read_leb_quick(&p, 32, false); \
  9866. } while (0)
  9867. #define pb_read_leb_int32(p, p_end, res) \
  9868. do { \
  9869. if (!loader_ctx->p_code_compiled) \
  9870. /* Enable format check in the first scan */ \
  9871. read_leb_int32(p, p_end, res); \
  9872. else \
  9873. /* Disable format check in the second scan */ \
  9874. res = (int32)read_leb_quick(&p, 32, true); \
  9875. } while (0)
  9876. #define pb_read_leb_int64(p, p_end, res) \
  9877. do { \
  9878. if (!loader_ctx->p_code_compiled) \
  9879. /* Enable format check in the first scan */ \
  9880. read_leb_int64(p, p_end, res); \
  9881. else \
  9882. /* Disable format check in the second scan */ \
  9883. res = (int64)read_leb_quick(&p, 64, true); \
  9884. } while (0)
  9885. #if WASM_ENABLE_MULTI_MEMORY != 0
  9886. #define pb_read_leb_memarg read_leb_memarg
  9887. #else
  9888. #define pb_read_leb_memarg pb_read_leb_uint32
  9889. #endif
  9890. #if WASM_ENABLE_MEMORY64 != 0
  9891. #define pb_read_leb_mem_offset read_leb_mem_offset
  9892. #else
  9893. #define pb_read_leb_mem_offset pb_read_leb_uint32
  9894. #endif
  9895. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9896. static bool
  9897. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9898. uint32 cur_func_idx, char *error_buf,
  9899. uint32 error_buf_size)
  9900. {
  9901. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9902. uint32 param_count, local_count, global_count;
  9903. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  9904. table_elem_idx_type;
  9905. BlockType func_block_type;
  9906. uint16 *local_offsets, local_offset;
  9907. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9908. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9909. mem_offset_t mem_offset;
  9910. int32 i32_const = 0;
  9911. int64 i64_const;
  9912. uint8 opcode;
  9913. bool return_value = false;
  9914. WASMLoaderContext *loader_ctx;
  9915. BranchBlock *frame_csp_tmp;
  9916. #if WASM_ENABLE_GC != 0
  9917. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9918. uint32 param_reftype_map_count, local_reftype_map_count;
  9919. int32 heap_type;
  9920. WASMRefType wasm_ref_type = { 0 };
  9921. bool need_ref_type_map;
  9922. #endif
  9923. #if WASM_ENABLE_FAST_INTERP != 0
  9924. int16 operand_offset = 0;
  9925. uint8 last_op = 0;
  9926. bool disable_emit, preserve_local = false, if_condition_available = true;
  9927. float32 f32_const;
  9928. float64 f64_const;
  9929. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9930. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9931. #endif
  9932. #if WASM_ENABLE_MEMORY64 != 0
  9933. bool is_memory64 = has_module_memory64(module);
  9934. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9935. #else
  9936. mem_offset_type = VALUE_TYPE_I32;
  9937. table_elem_idx_type = VALUE_TYPE_I32;
  9938. #endif
  9939. uint32 memidx;
  9940. global_count = module->import_global_count + module->global_count;
  9941. param_count = func->func_type->param_count;
  9942. param_types = func->func_type->types;
  9943. func_block_type.is_value_type = false;
  9944. func_block_type.u.type = func->func_type;
  9945. local_count = func->local_count;
  9946. local_types = func->local_types;
  9947. local_offsets = func->local_offsets;
  9948. #if WASM_ENABLE_GC != 0
  9949. param_reftype_maps = func->func_type->ref_type_maps;
  9950. param_reftype_map_count = func->func_type->ref_type_map_count;
  9951. local_reftype_maps = func->local_ref_type_maps;
  9952. local_reftype_map_count = func->local_ref_type_map_count;
  9953. #endif
  9954. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9955. goto fail;
  9956. }
  9957. #if WASM_ENABLE_GC != 0
  9958. loader_ctx->module = module;
  9959. loader_ctx->ref_type_set = module->ref_type_set;
  9960. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9961. #endif
  9962. #if WASM_ENABLE_FAST_INTERP != 0
  9963. /* For the first traverse, the initial value of preserved_local_offset has
  9964. * not been determined, we use the INT16_MAX to represent that a slot has
  9965. * been copied to preserve space. For second traverse, this field will be
  9966. * set to the appropriate value in wasm_loader_ctx_reinit.
  9967. * This is for Issue #1230,
  9968. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9969. * drop opcodes need to know which slots are preserved, so those slots will
  9970. * not be treated as dynamically allocated slots */
  9971. loader_ctx->preserved_local_offset = INT16_MAX;
  9972. re_scan:
  9973. if (loader_ctx->code_compiled_size > 0) {
  9974. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9975. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9976. goto fail;
  9977. }
  9978. p = func->code;
  9979. func->code_compiled = loader_ctx->p_code_compiled;
  9980. func->code_compiled_size = loader_ctx->code_compiled_size;
  9981. if (loader_ctx->i64_const_num > 0) {
  9982. int64 *i64_consts_old = loader_ctx->i64_consts;
  9983. /* Sort the i64 consts */
  9984. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  9985. cmp_i64_const);
  9986. /* Remove the duplicated i64 consts */
  9987. uint32 k = 1;
  9988. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  9989. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  9990. i64_consts_old[k++] = i64_consts_old[i];
  9991. }
  9992. }
  9993. if (k < loader_ctx->i64_const_num) {
  9994. int64 *i64_consts_new;
  9995. /* Try to reallocate memory with a smaller size */
  9996. if ((i64_consts_new =
  9997. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  9998. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  9999. i64_consts_old, (uint32)sizeof(int64) * k);
  10000. /* Free the old memory */
  10001. wasm_runtime_free(i64_consts_old);
  10002. loader_ctx->i64_consts = i64_consts_new;
  10003. loader_ctx->i64_const_max_num = k;
  10004. }
  10005. loader_ctx->i64_const_num = k;
  10006. }
  10007. }
  10008. if (loader_ctx->i32_const_num > 0) {
  10009. int32 *i32_consts_old = loader_ctx->i32_consts;
  10010. /* Sort the i32 consts */
  10011. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10012. cmp_i32_const);
  10013. /* Remove the duplicated i32 consts */
  10014. uint32 k = 1;
  10015. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10016. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10017. i32_consts_old[k++] = i32_consts_old[i];
  10018. }
  10019. }
  10020. if (k < loader_ctx->i32_const_num) {
  10021. int32 *i32_consts_new;
  10022. /* Try to reallocate memory with a smaller size */
  10023. if ((i32_consts_new =
  10024. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10025. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10026. i32_consts_old, (uint32)sizeof(int32) * k);
  10027. /* Free the old memory */
  10028. wasm_runtime_free(i32_consts_old);
  10029. loader_ctx->i32_consts = i32_consts_new;
  10030. loader_ctx->i32_const_max_num = k;
  10031. }
  10032. loader_ctx->i32_const_num = k;
  10033. }
  10034. }
  10035. }
  10036. #endif
  10037. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10038. while (p < p_end) {
  10039. opcode = *p++;
  10040. #if WASM_ENABLE_FAST_INTERP != 0
  10041. p_org = p;
  10042. disable_emit = false;
  10043. emit_label(opcode);
  10044. #endif
  10045. switch (opcode) {
  10046. case WASM_OP_UNREACHABLE:
  10047. RESET_STACK();
  10048. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10049. break;
  10050. case WASM_OP_NOP:
  10051. #if WASM_ENABLE_FAST_INTERP != 0
  10052. skip_label();
  10053. #endif
  10054. break;
  10055. case WASM_OP_IF:
  10056. {
  10057. #if WASM_ENABLE_FAST_INTERP != 0
  10058. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10059. int32 available_stack_cell =
  10060. (int32)(loader_ctx->stack_cell_num
  10061. - parent_block->stack_cell_num);
  10062. if (available_stack_cell <= 0
  10063. && parent_block->is_stack_polymorphic)
  10064. if_condition_available = false;
  10065. else
  10066. if_condition_available = true;
  10067. PRESERVE_LOCAL_FOR_BLOCK();
  10068. #endif
  10069. #if WASM_ENABLE_GC == 0
  10070. POP_I32();
  10071. #endif
  10072. goto handle_op_block_and_loop;
  10073. }
  10074. case WASM_OP_BLOCK:
  10075. case WASM_OP_LOOP:
  10076. #if WASM_ENABLE_EXCE_HANDLING != 0
  10077. case WASM_OP_TRY:
  10078. if (opcode == WASM_OP_TRY) {
  10079. /*
  10080. * keep track of exception handlers to account for
  10081. * memory allocation
  10082. */
  10083. func->exception_handler_count++;
  10084. /*
  10085. * try is a block
  10086. * do nothing special, but execution continues to
  10087. * to handle_op_block_and_loop,
  10088. * and that be pushes the csp
  10089. */
  10090. }
  10091. #endif
  10092. #if WASM_ENABLE_FAST_INTERP != 0
  10093. PRESERVE_LOCAL_FOR_BLOCK();
  10094. #endif
  10095. handle_op_block_and_loop:
  10096. {
  10097. uint8 value_type;
  10098. BlockType block_type;
  10099. #if WASM_ENABLE_FAST_INTERP != 0
  10100. uint32 available_params = 0;
  10101. #endif
  10102. CHECK_BUF(p, p_end, 1);
  10103. value_type = read_uint8(p);
  10104. if (is_byte_a_type(value_type)) {
  10105. /* If the first byte is one of these special values:
  10106. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10107. * the single return value. */
  10108. block_type.is_value_type = true;
  10109. block_type.u.value_type.type = value_type;
  10110. #if WASM_ENABLE_WAMR_COMPILER != 0
  10111. if (value_type == VALUE_TYPE_V128)
  10112. module->is_simd_used = true;
  10113. else if (value_type == VALUE_TYPE_FUNCREF
  10114. || value_type == VALUE_TYPE_EXTERNREF)
  10115. module->is_ref_types_used = true;
  10116. #endif
  10117. #if WASM_ENABLE_GC != 0
  10118. if (value_type != VALUE_TYPE_VOID) {
  10119. p_org = p;
  10120. p--;
  10121. if (!resolve_value_type((const uint8 **)&p, p_end,
  10122. module, module->type_count,
  10123. &need_ref_type_map,
  10124. &wasm_ref_type, false,
  10125. error_buf, error_buf_size)) {
  10126. goto fail;
  10127. }
  10128. if (need_ref_type_map) {
  10129. block_type.u.value_type.ref_type_map.index = 0;
  10130. if (!(block_type.u.value_type.ref_type_map
  10131. .ref_type = reftype_set_insert(
  10132. module->ref_type_set, &wasm_ref_type,
  10133. error_buf, error_buf_size))) {
  10134. goto fail;
  10135. }
  10136. }
  10137. /* Set again as the type might be changed, e.g.
  10138. (ref null any) to anyref */
  10139. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10140. #if WASM_ENABLE_FAST_INTERP == 0
  10141. while (p_org < p) {
  10142. #if WASM_ENABLE_DEBUG_INTERP != 0
  10143. if (!record_fast_op(module, p_org, *p_org,
  10144. error_buf, error_buf_size)) {
  10145. goto fail;
  10146. }
  10147. #endif
  10148. /* Ignore extra bytes for interpreter */
  10149. *p_org++ = WASM_OP_NOP;
  10150. }
  10151. #endif
  10152. }
  10153. #endif /* end of WASM_ENABLE_GC != 0 */
  10154. }
  10155. else {
  10156. int32 type_index;
  10157. /* Resolve the leb128 encoded type index as block type */
  10158. p--;
  10159. p_org = p - 1;
  10160. pb_read_leb_int32(p, p_end, type_index);
  10161. if ((uint32)type_index >= module->type_count) {
  10162. set_error_buf(error_buf, error_buf_size,
  10163. "unknown type");
  10164. goto fail;
  10165. }
  10166. block_type.is_value_type = false;
  10167. block_type.u.type =
  10168. (WASMFuncType *)module->types[type_index];
  10169. #if WASM_ENABLE_FAST_INTERP == 0
  10170. /* If block use type index as block type, change the opcode
  10171. * to new extended opcode so that interpreter can resolve
  10172. * the block quickly.
  10173. */
  10174. #if WASM_ENABLE_DEBUG_INTERP != 0
  10175. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10176. error_buf_size)) {
  10177. goto fail;
  10178. }
  10179. #endif
  10180. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10181. #endif
  10182. }
  10183. #if WASM_ENABLE_GC != 0
  10184. if (opcode == WASM_OP_IF) {
  10185. POP_I32();
  10186. }
  10187. #endif
  10188. /* Pop block parameters from stack */
  10189. if (BLOCK_HAS_PARAM(block_type)) {
  10190. WASMFuncType *wasm_type = block_type.u.type;
  10191. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10192. #if WASM_ENABLE_FAST_INTERP != 0
  10193. uint32 cell_num;
  10194. available_params = block_type.u.type->param_count;
  10195. #endif
  10196. for (i = 0; i < block_type.u.type->param_count; i++) {
  10197. int32 available_stack_cell =
  10198. (int32)(loader_ctx->stack_cell_num
  10199. - cur_block->stack_cell_num);
  10200. if (available_stack_cell <= 0
  10201. && cur_block->is_stack_polymorphic) {
  10202. #if WASM_ENABLE_FAST_INTERP != 0
  10203. available_params = i;
  10204. #endif
  10205. break;
  10206. }
  10207. POP_TYPE(
  10208. wasm_type->types[wasm_type->param_count - i - 1]);
  10209. #if WASM_ENABLE_FAST_INTERP != 0
  10210. /* decrease the frame_offset pointer accordingly to keep
  10211. * consistent with frame_ref stack */
  10212. cell_num = wasm_value_type_cell_num(
  10213. wasm_type->types[wasm_type->param_count - i - 1]);
  10214. loader_ctx->frame_offset -= cell_num;
  10215. #endif
  10216. }
  10217. }
  10218. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10219. block_type, p);
  10220. /* Pass parameters to block */
  10221. if (BLOCK_HAS_PARAM(block_type)) {
  10222. WASMFuncType *func_type = block_type.u.type;
  10223. #if WASM_ENABLE_GC != 0
  10224. WASMRefType *ref_type;
  10225. uint32 j = 0;
  10226. #endif
  10227. for (i = 0; i < func_type->param_count; i++) {
  10228. #if WASM_ENABLE_FAST_INTERP != 0
  10229. uint32 cell_num =
  10230. wasm_value_type_cell_num(func_type->types[i]);
  10231. if (i >= available_params) {
  10232. /* make sure enough space */
  10233. if (loader_ctx->p_code_compiled == NULL) {
  10234. loader_ctx->frame_offset += cell_num;
  10235. if (!check_offset_push(loader_ctx, error_buf,
  10236. error_buf_size))
  10237. goto fail;
  10238. /* for following dummy value assignemnt */
  10239. loader_ctx->frame_offset -= cell_num;
  10240. }
  10241. /* If there isn't enough data on stack, push a dummy
  10242. * offset to keep the stack consistent with
  10243. * frame_ref.
  10244. * Since the stack is already in polymorphic state,
  10245. * the opcode will not be executed, so the dummy
  10246. * offset won't cause any error */
  10247. for (uint32 n = 0; n < cell_num; n++) {
  10248. *loader_ctx->frame_offset++ = 0;
  10249. }
  10250. }
  10251. else {
  10252. loader_ctx->frame_offset += cell_num;
  10253. }
  10254. #endif
  10255. #if WASM_ENABLE_GC != 0
  10256. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10257. bh_assert(func_type->ref_type_maps[j].index == i);
  10258. ref_type = func_type->ref_type_maps[j].ref_type;
  10259. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10260. ref_type,
  10261. wasm_reftype_struct_size(ref_type));
  10262. j++;
  10263. }
  10264. #endif
  10265. PUSH_TYPE(func_type->types[i]);
  10266. }
  10267. }
  10268. #if WASM_ENABLE_FAST_INTERP != 0
  10269. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10270. skip_label();
  10271. if (BLOCK_HAS_PARAM(block_type)) {
  10272. /* Make sure params are in dynamic space */
  10273. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10274. error_buf_size))
  10275. goto fail;
  10276. }
  10277. if (opcode == WASM_OP_LOOP) {
  10278. (loader_ctx->frame_csp - 1)->code_compiled =
  10279. loader_ctx->p_code_compiled;
  10280. }
  10281. }
  10282. #if WASM_ENABLE_EXCE_HANDLING != 0
  10283. else if (opcode == WASM_OP_TRY) {
  10284. skip_label();
  10285. }
  10286. #endif
  10287. else if (opcode == WASM_OP_IF) {
  10288. BranchBlock *block = loader_ctx->frame_csp - 1;
  10289. /* If block has parameters, we should make sure they are in
  10290. * dynamic space. Otherwise, when else branch is missing,
  10291. * the later opcode may consume incorrect operand offset.
  10292. * Spec case:
  10293. * (func (export "params-id") (param i32) (result i32)
  10294. * (i32.const 1)
  10295. * (i32.const 2)
  10296. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10297. * (then)) (i32.add)
  10298. * )
  10299. *
  10300. * So we should emit a copy instruction before the if.
  10301. *
  10302. * And we also need to save the parameter offsets and
  10303. * recover them before entering else branch.
  10304. *
  10305. */
  10306. if (BLOCK_HAS_PARAM(block_type)) {
  10307. uint64 size;
  10308. /* In polymorphic state, there may be no if condition on
  10309. * the stack, so the offset may not emitted */
  10310. if (if_condition_available) {
  10311. /* skip the if condition operand offset */
  10312. wasm_loader_emit_backspace(loader_ctx,
  10313. sizeof(int16));
  10314. }
  10315. /* skip the if label */
  10316. skip_label();
  10317. /* Emit a copy instruction */
  10318. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10319. error_buf_size))
  10320. goto fail;
  10321. /* Emit the if instruction */
  10322. emit_label(opcode);
  10323. /* Emit the new condition operand offset */
  10324. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10325. /* Save top param_count values of frame_offset stack, so
  10326. * that we can recover it before executing else branch
  10327. */
  10328. size = sizeof(int16)
  10329. * (uint64)block_type.u.type->param_cell_num;
  10330. if (!(block->param_frame_offsets = loader_malloc(
  10331. size, error_buf, error_buf_size)))
  10332. goto fail;
  10333. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10334. loader_ctx->frame_offset
  10335. - size / sizeof(int16),
  10336. (uint32)size);
  10337. }
  10338. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10339. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10340. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10341. }
  10342. #endif
  10343. break;
  10344. }
  10345. #if WASM_ENABLE_EXCE_HANDLING != 0
  10346. case WASM_OP_THROW:
  10347. {
  10348. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10349. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10350. uint8 label_type = cur_block->label_type;
  10351. uint32 tag_index = 0;
  10352. pb_read_leb_int32(p, p_end, tag_index);
  10353. /* check validity of tag_index against module->tag_count */
  10354. /* check tag index is within the tag index space */
  10355. if (tag_index >= module->import_tag_count + module->tag_count) {
  10356. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10357. tag_index);
  10358. goto fail;
  10359. }
  10360. /* the tag_type is stored in either the WASMTag (section tags)
  10361. * or WASMTagImport (import tag) */
  10362. WASMFuncType *tag_type = NULL;
  10363. if (tag_index < module->import_tag_count) {
  10364. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10365. }
  10366. else {
  10367. tag_type =
  10368. module->tags[tag_index - module->import_tag_count]
  10369. ->tag_type;
  10370. }
  10371. if (tag_type->result_count != 0) {
  10372. set_error_buf(error_buf, error_buf_size,
  10373. "tag type signature does not return void");
  10374. goto fail;
  10375. }
  10376. int32 available_stack_cell =
  10377. (int32)(loader_ctx->stack_cell_num
  10378. - cur_block->stack_cell_num);
  10379. int32 tti;
  10380. /* Check stack values match return types by comparing tag param
  10381. * types with stack cells */
  10382. uint8 *frame_ref = loader_ctx->frame_ref;
  10383. #if WASM_ENABLE_GC != 0
  10384. WASMRefTypeMap *frame_reftype_map =
  10385. loader_ctx->frame_reftype_map;
  10386. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10387. /* Temporarily set these values since they may be used in
  10388. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10389. param_reftype_maps = tag_type->ref_type_maps;
  10390. /* For tag_type function, it shouldn't have result_count = 0 */
  10391. param_reftype_map_count = tag_type->ref_type_map_count;
  10392. param_count = tag_type->param_count;
  10393. #endif
  10394. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10395. #if WASM_ENABLE_GC != 0
  10396. local_type = tag_type->types[tti];
  10397. local_idx = tti;
  10398. /* Get the wasm_ref_type if the local_type is multibyte
  10399. type */
  10400. GET_LOCAL_REFTYPE();
  10401. #endif
  10402. if (!check_stack_top_values(
  10403. loader_ctx, frame_ref, available_stack_cell,
  10404. #if WASM_ENABLE_GC != 0
  10405. frame_reftype_map, frame_reftype_map_num,
  10406. #endif
  10407. tag_type->types[tti],
  10408. #if WASM_ENABLE_GC != 0
  10409. &wasm_ref_type,
  10410. #endif
  10411. error_buf, error_buf_size)) {
  10412. snprintf(error_buf, error_buf_size,
  10413. "type mismatch: instruction requires [%s] but "
  10414. "stack has [%s]",
  10415. tag_type->param_count > 0
  10416. ? type2str(tag_type->types[tti])
  10417. : "",
  10418. available_stack_cell > 0
  10419. ? type2str(*(loader_ctx->frame_ref - 1))
  10420. : "");
  10421. goto fail;
  10422. }
  10423. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10424. available_stack_cell -=
  10425. wasm_value_type_cell_num(tag_type->types[tti]);
  10426. }
  10427. #if WASM_ENABLE_GC != 0
  10428. /* Restore the values */
  10429. param_reftype_maps = func->func_type->ref_type_maps;
  10430. param_reftype_map_count = func->func_type->ref_type_map_count;
  10431. param_count = func->func_type->param_count;
  10432. #endif
  10433. /* throw is stack polymorphic */
  10434. (void)label_type;
  10435. RESET_STACK();
  10436. break;
  10437. }
  10438. case WASM_OP_RETHROW:
  10439. {
  10440. /* must be done before checking branch block */
  10441. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10442. /* check the target catching block: LABEL_TYPE_CATCH */
  10443. if (!(frame_csp_tmp =
  10444. check_branch_block(loader_ctx, &p, p_end, opcode,
  10445. error_buf, error_buf_size)))
  10446. goto fail;
  10447. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10448. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10449. /* trap according to spectest (rethrow.wast) */
  10450. set_error_buf(error_buf, error_buf_size,
  10451. "invalid rethrow label");
  10452. goto fail;
  10453. }
  10454. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10455. uint8 label_type = cur_block->label_type;
  10456. (void)label_type;
  10457. /* rethrow is stack polymorphic */
  10458. RESET_STACK();
  10459. break;
  10460. }
  10461. case WASM_OP_DELEGATE:
  10462. {
  10463. /* check target block is valid */
  10464. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10465. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10466. goto fail;
  10467. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10468. uint8 label_type = cur_block->label_type;
  10469. (void)label_type;
  10470. /* DELEGATE ends the block */
  10471. POP_CSP();
  10472. break;
  10473. }
  10474. case WASM_OP_CATCH:
  10475. {
  10476. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10477. uint8 label_type = cur_block->label_type;
  10478. uint32 tag_index = 0;
  10479. pb_read_leb_int32(p, p_end, tag_index);
  10480. /* check validity of tag_index against module->tag_count */
  10481. /* check tag index is within the tag index space */
  10482. if (tag_index >= module->import_tag_count + module->tag_count) {
  10483. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10484. __FUNCTION__);
  10485. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10486. goto fail;
  10487. }
  10488. /* the tag_type is stored in either the WASMTag (section tags)
  10489. * or WASMTagImport (import tag) */
  10490. WASMFuncType *func_type = NULL;
  10491. if (tag_index < module->import_tag_count) {
  10492. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10493. }
  10494. else {
  10495. func_type =
  10496. module->tags[tag_index - module->import_tag_count]
  10497. ->tag_type;
  10498. }
  10499. if (func_type->result_count != 0) {
  10500. set_error_buf(error_buf, error_buf_size,
  10501. "tag type signature does not return void");
  10502. goto fail;
  10503. }
  10504. /* check validity of current label (expect LABEL_TYPE_TRY or
  10505. * LABEL_TYPE_CATCH) */
  10506. if ((LABEL_TYPE_CATCH != label_type)
  10507. && (LABEL_TYPE_TRY != label_type)) {
  10508. set_error_buf(error_buf, error_buf_size,
  10509. "Unexpected block sequence encountered.");
  10510. goto fail;
  10511. }
  10512. /*
  10513. * replace frame_csp by LABEL_TYPE_CATCH
  10514. */
  10515. cur_block->label_type = LABEL_TYPE_CATCH;
  10516. /* RESET_STACK removes the values pushed in TRY or pervious
  10517. * CATCH Blocks */
  10518. RESET_STACK();
  10519. #if WASM_ENABLE_GC != 0
  10520. WASMRefType *ref_type;
  10521. uint32 j = 0;
  10522. #endif
  10523. /* push types on the stack according to caught type */
  10524. for (i = 0; i < func_type->param_count; i++) {
  10525. #if WASM_ENABLE_GC != 0
  10526. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10527. bh_assert(func_type->ref_type_maps[j].index == i);
  10528. ref_type = func_type->ref_type_maps[j].ref_type;
  10529. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10530. ref_type,
  10531. wasm_reftype_struct_size(ref_type));
  10532. j++;
  10533. }
  10534. #endif
  10535. PUSH_TYPE(func_type->types[i]);
  10536. }
  10537. break;
  10538. }
  10539. case WASM_OP_CATCH_ALL:
  10540. {
  10541. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10542. /* expecting a TRY or CATCH, anything else will be considered an
  10543. * error */
  10544. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10545. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10546. set_error_buf(error_buf, error_buf_size,
  10547. "Unexpected block sequence encountered.");
  10548. goto fail;
  10549. }
  10550. /* no immediates */
  10551. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10552. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10553. /* RESET_STACK removes the values pushed in TRY or pervious
  10554. * CATCH Blocks */
  10555. RESET_STACK();
  10556. /* catch_all has no tagtype and therefore no parameters */
  10557. break;
  10558. }
  10559. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10560. case WASM_OP_ELSE:
  10561. handle_op_else:
  10562. {
  10563. BranchBlock *block = NULL;
  10564. BlockType block_type;
  10565. if (loader_ctx->csp_num < 2
  10566. /* the matched if isn't found */
  10567. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10568. /* duplicated else is found */
  10569. || (loader_ctx->frame_csp - 1)->else_addr) {
  10570. set_error_buf(
  10571. error_buf, error_buf_size,
  10572. "opcode else found without matched opcode if");
  10573. goto fail;
  10574. }
  10575. block = loader_ctx->frame_csp - 1;
  10576. /* check whether if branch's stack matches its result type */
  10577. if (!check_block_stack(loader_ctx, block, error_buf,
  10578. error_buf_size))
  10579. goto fail;
  10580. block->else_addr = p - 1;
  10581. block_type = block->block_type;
  10582. #if WASM_ENABLE_GC != 0
  10583. if (!wasm_loader_init_local_use_masks(
  10584. loader_ctx, local_count, error_buf, error_buf_size)) {
  10585. goto fail;
  10586. }
  10587. #endif
  10588. #if WASM_ENABLE_FAST_INTERP != 0
  10589. /* if the result of if branch is in local or const area, add a
  10590. * copy op */
  10591. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10592. error_buf, error_buf_size)) {
  10593. goto fail;
  10594. }
  10595. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10596. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10597. #endif
  10598. RESET_STACK();
  10599. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10600. /* Pass parameters to if-false branch */
  10601. if (BLOCK_HAS_PARAM(block_type)) {
  10602. for (i = 0; i < block_type.u.type->param_count; i++)
  10603. PUSH_TYPE(block_type.u.type->types[i]);
  10604. }
  10605. #if WASM_ENABLE_FAST_INTERP != 0
  10606. /* Recover top param_count values of frame_offset stack */
  10607. if (BLOCK_HAS_PARAM((block_type))) {
  10608. uint32 size;
  10609. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10610. bh_memcpy_s(loader_ctx->frame_offset, size,
  10611. block->param_frame_offsets, size);
  10612. loader_ctx->frame_offset += (size / sizeof(int16));
  10613. }
  10614. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10615. #endif
  10616. break;
  10617. }
  10618. case WASM_OP_END:
  10619. {
  10620. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10621. /* check whether block stack matches its result type */
  10622. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10623. error_buf_size))
  10624. goto fail;
  10625. /* if there is no else branch, make a virtual else opcode for
  10626. easier integrity check and to copy the correct results to
  10627. the block return address for fast-interp mode:
  10628. change if block from `if ... end` to `if ... else end` */
  10629. if (cur_block->label_type == LABEL_TYPE_IF
  10630. && !cur_block->else_addr) {
  10631. opcode = WASM_OP_ELSE;
  10632. p--;
  10633. #if WASM_ENABLE_FAST_INTERP != 0
  10634. p_org = p;
  10635. skip_label();
  10636. disable_emit = false;
  10637. emit_label(opcode);
  10638. #endif
  10639. goto handle_op_else;
  10640. }
  10641. POP_CSP();
  10642. #if WASM_ENABLE_FAST_INTERP != 0
  10643. skip_label();
  10644. /* copy the result to the block return address */
  10645. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10646. error_buf, error_buf_size)) {
  10647. /* it could be tmp frame_csp allocated from opcode like
  10648. * OP_BR and not counted in loader_ctx->csp_num, it won't
  10649. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  10650. * to free the loader_ctx->frame_csp if fails */
  10651. free_label_patch_list(loader_ctx->frame_csp);
  10652. goto fail;
  10653. }
  10654. apply_label_patch(loader_ctx, 0, PATCH_END);
  10655. free_label_patch_list(loader_ctx->frame_csp);
  10656. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10657. int32 idx;
  10658. uint8 ret_type;
  10659. emit_label(WASM_OP_RETURN);
  10660. for (idx = (int32)func->func_type->result_count - 1;
  10661. idx >= 0; idx--) {
  10662. ret_type = *(func->func_type->types
  10663. + func->func_type->param_count + idx);
  10664. POP_OFFSET_TYPE(ret_type);
  10665. }
  10666. }
  10667. #endif
  10668. if (loader_ctx->csp_num > 0) {
  10669. loader_ctx->frame_csp->end_addr = p - 1;
  10670. }
  10671. else {
  10672. /* end of function block, function will return */
  10673. if (p < p_end) {
  10674. set_error_buf(error_buf, error_buf_size,
  10675. "section size mismatch");
  10676. goto fail;
  10677. }
  10678. }
  10679. break;
  10680. }
  10681. case WASM_OP_BR:
  10682. {
  10683. if (!(frame_csp_tmp =
  10684. check_branch_block(loader_ctx, &p, p_end, opcode,
  10685. error_buf, error_buf_size)))
  10686. goto fail;
  10687. RESET_STACK();
  10688. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10689. break;
  10690. }
  10691. case WASM_OP_BR_IF:
  10692. {
  10693. POP_I32();
  10694. if (!(frame_csp_tmp =
  10695. check_branch_block(loader_ctx, &p, p_end, opcode,
  10696. error_buf, error_buf_size)))
  10697. goto fail;
  10698. break;
  10699. }
  10700. case WASM_OP_BR_TABLE:
  10701. {
  10702. uint32 depth = 0, default_arity, arity = 0;
  10703. BranchBlock *target_block;
  10704. BlockType *target_block_type;
  10705. #if WASM_ENABLE_FAST_INTERP == 0
  10706. BrTableCache *br_table_cache = NULL;
  10707. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10708. uint32 j;
  10709. #endif
  10710. pb_read_leb_uint32(p, p_end, count);
  10711. #if WASM_ENABLE_FAST_INTERP != 0
  10712. emit_uint32(loader_ctx, count);
  10713. #endif
  10714. POP_I32();
  10715. /* Get each depth and check it */
  10716. p_org = p;
  10717. for (i = 0; i <= count; i++) {
  10718. pb_read_leb_uint32(p, p_end, depth);
  10719. bh_assert(loader_ctx->csp_num > 0);
  10720. if (loader_ctx->csp_num - 1 < depth) {
  10721. set_error_buf(error_buf, error_buf_size,
  10722. "unknown label, "
  10723. "unexpected end of section or function");
  10724. goto fail;
  10725. }
  10726. }
  10727. p = p_org;
  10728. /* Get the default block's arity */
  10729. target_block = loader_ctx->frame_csp - (depth + 1);
  10730. target_block_type = &target_block->block_type;
  10731. default_arity = block_type_get_arity(target_block_type,
  10732. target_block->label_type);
  10733. #if WASM_ENABLE_FAST_INTERP == 0
  10734. p_depth_begin = p_depth = p;
  10735. #endif
  10736. for (i = 0; i <= count; i++) {
  10737. p_org = p;
  10738. pb_read_leb_uint32(p, p_end, depth);
  10739. p = p_org;
  10740. /* Get the target block's arity and check it */
  10741. target_block = loader_ctx->frame_csp - (depth + 1);
  10742. target_block_type = &target_block->block_type;
  10743. arity = block_type_get_arity(target_block_type,
  10744. target_block->label_type);
  10745. if (arity != default_arity) {
  10746. set_error_buf(error_buf, error_buf_size,
  10747. "type mismatch: br_table targets must "
  10748. "all use same result type");
  10749. goto fail;
  10750. }
  10751. if (!(frame_csp_tmp =
  10752. check_branch_block(loader_ctx, &p, p_end, opcode,
  10753. error_buf, error_buf_size))) {
  10754. goto fail;
  10755. }
  10756. #if WASM_ENABLE_FAST_INTERP == 0
  10757. if (br_table_cache) {
  10758. br_table_cache->br_depths[i] = depth;
  10759. }
  10760. else {
  10761. if (depth > 255) {
  10762. /* The depth cannot be stored in one byte,
  10763. create br_table cache to store each depth */
  10764. #if WASM_ENABLE_DEBUG_INTERP != 0
  10765. if (!record_fast_op(module, p_opcode, *p_opcode,
  10766. error_buf, error_buf_size)) {
  10767. goto fail;
  10768. }
  10769. #endif
  10770. if (!(br_table_cache = loader_malloc(
  10771. offsetof(BrTableCache, br_depths)
  10772. + sizeof(uint32)
  10773. * (uint64)(count + 1),
  10774. error_buf, error_buf_size))) {
  10775. goto fail;
  10776. }
  10777. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10778. br_table_cache->br_table_op_addr = p_opcode;
  10779. br_table_cache->br_count = count;
  10780. /* Copy previous depths which are one byte */
  10781. for (j = 0; j < i; j++) {
  10782. br_table_cache->br_depths[j] = p_depth_begin[j];
  10783. }
  10784. br_table_cache->br_depths[i] = depth;
  10785. bh_list_insert(module->br_table_cache_list,
  10786. br_table_cache);
  10787. }
  10788. else {
  10789. /* The depth can be stored in one byte, use the
  10790. byte of the leb to store it */
  10791. *p_depth++ = (uint8)depth;
  10792. }
  10793. }
  10794. #endif
  10795. }
  10796. #if WASM_ENABLE_FAST_INTERP == 0
  10797. /* Set the tailing bytes to nop */
  10798. if (br_table_cache)
  10799. p_depth = p_depth_begin;
  10800. while (p_depth < p)
  10801. *p_depth++ = WASM_OP_NOP;
  10802. #endif
  10803. RESET_STACK();
  10804. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10805. break;
  10806. }
  10807. case WASM_OP_RETURN:
  10808. {
  10809. WASMFuncType *func_type = func->func_type;
  10810. int32 idx;
  10811. uint8 ret_type;
  10812. #if WASM_ENABLE_GC != 0
  10813. uint32 j = func_type->ref_type_map_count - 1;
  10814. #endif
  10815. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10816. idx--) {
  10817. ret_type =
  10818. *(func_type->types + func_type->param_count + idx);
  10819. #if WASM_ENABLE_GC != 0
  10820. if (wasm_is_type_multi_byte_type(ret_type)) {
  10821. WASMRefType *ref_type =
  10822. func_type->ref_type_maps[j].ref_type;
  10823. bh_assert(func_type->ref_type_maps[j].index
  10824. == func_type->param_count + idx);
  10825. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10826. ref_type,
  10827. wasm_reftype_struct_size(ref_type));
  10828. j--;
  10829. }
  10830. #endif
  10831. #if WASM_ENABLE_FAST_INTERP != 0
  10832. /* emit the offset after return opcode */
  10833. POP_OFFSET_TYPE(ret_type);
  10834. #endif
  10835. POP_TYPE(ret_type);
  10836. }
  10837. RESET_STACK();
  10838. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10839. break;
  10840. }
  10841. case WASM_OP_CALL:
  10842. #if WASM_ENABLE_TAIL_CALL != 0
  10843. case WASM_OP_RETURN_CALL:
  10844. #endif
  10845. #if WASM_ENABLE_GC != 0
  10846. case WASM_OP_CALL_REF:
  10847. case WASM_OP_RETURN_CALL_REF:
  10848. #endif
  10849. {
  10850. WASMFuncType *func_type;
  10851. uint8 type;
  10852. int32 idx;
  10853. #if WASM_ENABLE_GC != 0
  10854. WASMRefType *ref_type;
  10855. uint32 type_idx1;
  10856. int32 j;
  10857. #endif
  10858. #if WASM_ENABLE_GC != 0
  10859. if (opcode == WASM_OP_CALL_REF
  10860. || opcode == WASM_OP_RETURN_CALL_REF) {
  10861. pb_read_leb_uint32(p, p_end, type_idx1);
  10862. if (!check_type_index(module, module->type_count, type_idx1,
  10863. error_buf, error_buf_size)) {
  10864. goto fail;
  10865. }
  10866. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10867. set_error_buf(error_buf, error_buf_size,
  10868. "unkown function type");
  10869. goto fail;
  10870. }
  10871. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10872. &type_idx, error_buf,
  10873. error_buf_size)) {
  10874. goto fail;
  10875. }
  10876. if (type == VALUE_TYPE_ANY) {
  10877. type_idx = type_idx1;
  10878. }
  10879. if (!check_type_index(module, module->type_count, type_idx,
  10880. error_buf, error_buf_size)) {
  10881. goto fail;
  10882. }
  10883. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10884. set_error_buf(error_buf, error_buf_size,
  10885. "unkown function type");
  10886. goto fail;
  10887. }
  10888. if (!wasm_func_type_is_super_of(
  10889. (WASMFuncType *)module->types[type_idx1],
  10890. (WASMFuncType *)module->types[type_idx])) {
  10891. set_error_buf(error_buf, error_buf_size,
  10892. "function type mismatch");
  10893. goto fail;
  10894. }
  10895. func_type = (WASMFuncType *)module->types[type_idx];
  10896. }
  10897. else
  10898. #endif
  10899. {
  10900. pb_read_leb_uint32(p, p_end, func_idx);
  10901. #if WASM_ENABLE_FAST_INTERP != 0
  10902. /* we need to emit func_idx before arguments */
  10903. emit_uint32(loader_ctx, func_idx);
  10904. #endif
  10905. if (!check_function_index(module, func_idx, error_buf,
  10906. error_buf_size)) {
  10907. goto fail;
  10908. }
  10909. if (func_idx < module->import_function_count)
  10910. func_type = module->import_functions[func_idx]
  10911. .u.function.func_type;
  10912. else
  10913. func_type =
  10914. module
  10915. ->functions[func_idx
  10916. - module->import_function_count]
  10917. ->func_type;
  10918. }
  10919. if (func_type->param_count > 0) {
  10920. #if WASM_ENABLE_GC != 0
  10921. j = (int32)(func_type->result_ref_type_maps
  10922. - func_type->ref_type_maps - 1);
  10923. #endif
  10924. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10925. idx--) {
  10926. #if WASM_ENABLE_GC != 0
  10927. if (wasm_is_type_multi_byte_type(
  10928. func_type->types[idx])) {
  10929. ref_type = func_type->ref_type_maps[j].ref_type;
  10930. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10931. ref_type,
  10932. wasm_reftype_struct_size(ref_type));
  10933. j--;
  10934. }
  10935. #endif
  10936. #if WASM_ENABLE_FAST_INTERP != 0
  10937. POP_OFFSET_TYPE(func_type->types[idx]);
  10938. #endif
  10939. POP_TYPE(func_type->types[idx]);
  10940. }
  10941. }
  10942. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10943. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10944. #endif
  10945. #if WASM_ENABLE_GC != 0
  10946. j = (int32)(func_type->result_ref_type_maps
  10947. - func_type->ref_type_maps);
  10948. #endif
  10949. for (i = 0; i < func_type->result_count; i++) {
  10950. #if WASM_ENABLE_GC != 0
  10951. if (wasm_is_type_multi_byte_type(
  10952. func_type->types[func_type->param_count + i])) {
  10953. ref_type = func_type->ref_type_maps[j].ref_type;
  10954. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10955. ref_type,
  10956. wasm_reftype_struct_size(ref_type));
  10957. j++;
  10958. }
  10959. #endif
  10960. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10961. #if WASM_ENABLE_FAST_INTERP != 0
  10962. /* Here we emit each return value's dynamic_offset. But
  10963. * in fact these offsets are continuous, so interpreter
  10964. * only need to get the first return value's offset.
  10965. */
  10966. PUSH_OFFSET_TYPE(
  10967. func_type->types[func_type->param_count + i]);
  10968. #endif
  10969. }
  10970. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10971. }
  10972. else {
  10973. #if WASM_ENABLE_GC == 0
  10974. if (func_type->result_count
  10975. != func->func_type->result_count) {
  10976. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10977. "type mismatch: expect ",
  10978. func->func_type->result_count,
  10979. " return values but got other");
  10980. goto fail;
  10981. }
  10982. for (i = 0; i < func_type->result_count; i++) {
  10983. type = func->func_type
  10984. ->types[func->func_type->param_count + i];
  10985. if (func_type->types[func_type->param_count + i]
  10986. != type) {
  10987. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10988. "type mismatch: expect ",
  10989. type2str(type), " but got other");
  10990. goto fail;
  10991. }
  10992. }
  10993. #else
  10994. if (!wasm_func_type_result_is_subtype_of(
  10995. func_type, func->func_type, module->types,
  10996. module->type_count)) {
  10997. set_error_buf(
  10998. error_buf, error_buf_size,
  10999. "type mismatch: invalid func result types");
  11000. goto fail;
  11001. }
  11002. #endif
  11003. RESET_STACK();
  11004. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11005. }
  11006. #endif
  11007. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11008. || WASM_ENABLE_WAMR_COMPILER != 0
  11009. func->has_op_func_call = true;
  11010. #endif
  11011. (void)type;
  11012. break;
  11013. }
  11014. /*
  11015. * if disable reference type: call_indirect typeidx, 0x00
  11016. * if enable reference type: call_indirect typeidx, tableidx
  11017. */
  11018. case WASM_OP_CALL_INDIRECT:
  11019. #if WASM_ENABLE_TAIL_CALL != 0
  11020. case WASM_OP_RETURN_CALL_INDIRECT:
  11021. #endif
  11022. {
  11023. int32 idx;
  11024. WASMFuncType *func_type;
  11025. uint32 tbl_elem_type;
  11026. #if WASM_ENABLE_GC != 0
  11027. WASMRefType *elem_ref_type = NULL;
  11028. #endif
  11029. pb_read_leb_uint32(p, p_end, type_idx);
  11030. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11031. #if WASM_ENABLE_WAMR_COMPILER != 0
  11032. if (p + 1 < p_end && *p != 0x00) {
  11033. /*
  11034. * Any non-0x00 byte requires the ref types proposal.
  11035. * This is different from checking the table_idx value
  11036. * since `0x80 0x00` etc. are all valid encodings of zero.
  11037. */
  11038. module->is_ref_types_used = true;
  11039. }
  11040. #endif
  11041. pb_read_leb_uint32(p, p_end, table_idx);
  11042. #else
  11043. CHECK_BUF(p, p_end, 1);
  11044. table_idx = read_uint8(p);
  11045. #endif
  11046. if (!check_table_index(module, table_idx, error_buf,
  11047. error_buf_size)) {
  11048. goto fail;
  11049. }
  11050. tbl_elem_type =
  11051. table_idx < module->import_table_count
  11052. ? module->import_tables[table_idx]
  11053. .u.table.table_type.elem_type
  11054. : module->tables[table_idx - module->import_table_count]
  11055. .table_type.elem_type;
  11056. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11057. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11058. set_error_buf_v(error_buf, error_buf_size,
  11059. "type mismatch: instruction requires table "
  11060. "of functions but table %u has externref",
  11061. table_idx);
  11062. goto fail;
  11063. }
  11064. #elif WASM_ENABLE_GC != 0
  11065. /* Table element must match type ref null func */
  11066. elem_ref_type =
  11067. table_idx < module->import_table_count
  11068. ? module->import_tables[table_idx]
  11069. .u.table.table_type.elem_ref_type
  11070. : module->tables[table_idx - module->import_table_count]
  11071. .table_type.elem_ref_type;
  11072. if (!wasm_reftype_is_subtype_of(
  11073. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11074. module->types, module->type_count)) {
  11075. set_error_buf_v(error_buf, error_buf_size,
  11076. "type mismatch: instruction requires "
  11077. "reference type t match type ref null func"
  11078. "in table %u",
  11079. table_idx);
  11080. goto fail;
  11081. }
  11082. #else
  11083. (void)tbl_elem_type;
  11084. #endif
  11085. #if WASM_ENABLE_FAST_INTERP != 0
  11086. /* we need to emit before arguments */
  11087. #if WASM_ENABLE_TAIL_CALL != 0
  11088. emit_byte(loader_ctx, opcode);
  11089. #endif
  11090. emit_uint32(loader_ctx, type_idx);
  11091. emit_uint32(loader_ctx, table_idx);
  11092. #endif
  11093. #if WASM_ENABLE_MEMORY64 != 0
  11094. table_elem_idx_type = is_table_64bit(module, table_idx)
  11095. ? VALUE_TYPE_I64
  11096. : VALUE_TYPE_I32;
  11097. #endif
  11098. /* skip elem idx */
  11099. POP_TBL_ELEM_IDX();
  11100. if (type_idx >= module->type_count) {
  11101. set_error_buf(error_buf, error_buf_size, "unknown type");
  11102. goto fail;
  11103. }
  11104. func_type = (WASMFuncType *)module->types[type_idx];
  11105. if (func_type->param_count > 0) {
  11106. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11107. idx--) {
  11108. #if WASM_ENABLE_FAST_INTERP != 0
  11109. POP_OFFSET_TYPE(func_type->types[idx]);
  11110. #endif
  11111. POP_TYPE(func_type->types[idx]);
  11112. }
  11113. }
  11114. #if WASM_ENABLE_TAIL_CALL != 0
  11115. if (opcode == WASM_OP_CALL_INDIRECT) {
  11116. #endif
  11117. for (i = 0; i < func_type->result_count; i++) {
  11118. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11119. #if WASM_ENABLE_FAST_INTERP != 0
  11120. PUSH_OFFSET_TYPE(
  11121. func_type->types[func_type->param_count + i]);
  11122. #endif
  11123. }
  11124. #if WASM_ENABLE_TAIL_CALL != 0
  11125. }
  11126. else {
  11127. uint8 type;
  11128. if (func_type->result_count
  11129. != func->func_type->result_count) {
  11130. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11131. "type mismatch: expect ",
  11132. func->func_type->result_count,
  11133. " return values but got other");
  11134. goto fail;
  11135. }
  11136. for (i = 0; i < func_type->result_count; i++) {
  11137. type = func->func_type
  11138. ->types[func->func_type->param_count + i];
  11139. if (func_type->types[func_type->param_count + i]
  11140. != type) {
  11141. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11142. "type mismatch: expect ",
  11143. type2str(type), " but got other");
  11144. goto fail;
  11145. }
  11146. }
  11147. RESET_STACK();
  11148. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11149. }
  11150. #endif
  11151. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11152. || WASM_ENABLE_WAMR_COMPILER != 0
  11153. func->has_op_func_call = true;
  11154. #endif
  11155. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11156. func->has_op_call_indirect = true;
  11157. #endif
  11158. break;
  11159. }
  11160. case WASM_OP_DROP:
  11161. {
  11162. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11163. int32 available_stack_cell =
  11164. (int32)(loader_ctx->stack_cell_num
  11165. - cur_block->stack_cell_num);
  11166. if (available_stack_cell <= 0
  11167. && !cur_block->is_stack_polymorphic) {
  11168. set_error_buf(error_buf, error_buf_size,
  11169. "type mismatch, opcode drop was found "
  11170. "but stack was empty");
  11171. goto fail;
  11172. }
  11173. if (available_stack_cell > 0) {
  11174. #if WASM_ENABLE_GC != 0
  11175. if (wasm_is_type_multi_byte_type(
  11176. *(loader_ctx->frame_ref - 1))) {
  11177. bh_assert((int32)(loader_ctx->reftype_map_num
  11178. - cur_block->reftype_map_num)
  11179. > 0);
  11180. loader_ctx->frame_reftype_map--;
  11181. loader_ctx->reftype_map_num--;
  11182. }
  11183. #endif
  11184. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11185. loader_ctx->frame_ref--;
  11186. loader_ctx->stack_cell_num--;
  11187. #if WASM_ENABLE_FAST_INTERP != 0
  11188. skip_label();
  11189. loader_ctx->frame_offset--;
  11190. if ((*(loader_ctx->frame_offset)
  11191. > loader_ctx->start_dynamic_offset)
  11192. && (*(loader_ctx->frame_offset)
  11193. < loader_ctx->max_dynamic_offset))
  11194. loader_ctx->dynamic_offset--;
  11195. #endif
  11196. }
  11197. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11198. loader_ctx->frame_ref -= 2;
  11199. loader_ctx->stack_cell_num -= 2;
  11200. #if WASM_ENABLE_FAST_INTERP == 0
  11201. *(p - 1) = WASM_OP_DROP_64;
  11202. #endif
  11203. #if WASM_ENABLE_FAST_INTERP != 0
  11204. skip_label();
  11205. loader_ctx->frame_offset -= 2;
  11206. if ((*(loader_ctx->frame_offset)
  11207. > loader_ctx->start_dynamic_offset)
  11208. && (*(loader_ctx->frame_offset)
  11209. < loader_ctx->max_dynamic_offset))
  11210. loader_ctx->dynamic_offset -= 2;
  11211. #endif
  11212. }
  11213. #if WASM_ENABLE_SIMD != 0
  11214. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11215. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11216. loader_ctx->frame_ref -= 4;
  11217. loader_ctx->stack_cell_num -= 4;
  11218. }
  11219. #endif
  11220. #endif
  11221. else {
  11222. set_error_buf(error_buf, error_buf_size,
  11223. "type mismatch");
  11224. goto fail;
  11225. }
  11226. }
  11227. else {
  11228. #if WASM_ENABLE_FAST_INTERP != 0
  11229. skip_label();
  11230. #endif
  11231. }
  11232. break;
  11233. }
  11234. case WASM_OP_SELECT:
  11235. {
  11236. uint8 ref_type;
  11237. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11238. int32 available_stack_cell;
  11239. #if WASM_ENABLE_FAST_INTERP != 0
  11240. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11241. #endif
  11242. POP_I32();
  11243. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11244. - cur_block->stack_cell_num);
  11245. if (available_stack_cell <= 0
  11246. && !cur_block->is_stack_polymorphic) {
  11247. set_error_buf(error_buf, error_buf_size,
  11248. "type mismatch or invalid result arity, "
  11249. "opcode select was found "
  11250. "but stack was empty");
  11251. goto fail;
  11252. }
  11253. if (available_stack_cell > 0) {
  11254. switch (*(loader_ctx->frame_ref - 1)) {
  11255. case VALUE_TYPE_I32:
  11256. case VALUE_TYPE_F32:
  11257. case VALUE_TYPE_ANY:
  11258. break;
  11259. case VALUE_TYPE_I64:
  11260. case VALUE_TYPE_F64:
  11261. #if WASM_ENABLE_FAST_INTERP == 0
  11262. *(p - 1) = WASM_OP_SELECT_64;
  11263. #endif
  11264. #if WASM_ENABLE_FAST_INTERP != 0
  11265. if (loader_ctx->p_code_compiled) {
  11266. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11267. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11268. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11269. *(void **)(p_code_compiled_tmp
  11270. - sizeof(void *)) =
  11271. handle_table[opcode_tmp];
  11272. #else
  11273. #if UINTPTR_MAX == UINT64_MAX
  11274. /* emit int32 relative offset in 64-bit target
  11275. */
  11276. int32 offset =
  11277. (int32)((uint8 *)handle_table[opcode_tmp]
  11278. - (uint8 *)handle_table[0]);
  11279. *(int32 *)(p_code_compiled_tmp
  11280. - sizeof(int32)) = offset;
  11281. #else
  11282. /* emit uint32 label address in 32-bit target */
  11283. *(uint32 *)(p_code_compiled_tmp
  11284. - sizeof(uint32)) =
  11285. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11286. #endif
  11287. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11288. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11289. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11290. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11291. #else
  11292. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11293. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11294. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11295. }
  11296. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11297. break;
  11298. #if WASM_ENABLE_SIMD != 0
  11299. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11300. case VALUE_TYPE_V128:
  11301. break;
  11302. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11303. #endif /* WASM_ENABLE_SIMD != 0 */
  11304. default:
  11305. {
  11306. set_error_buf(error_buf, error_buf_size,
  11307. "type mismatch");
  11308. goto fail;
  11309. }
  11310. }
  11311. ref_type = *(loader_ctx->frame_ref - 1);
  11312. #if WASM_ENABLE_FAST_INTERP != 0
  11313. POP_OFFSET_TYPE(ref_type);
  11314. POP_TYPE(ref_type);
  11315. POP_OFFSET_TYPE(ref_type);
  11316. POP_TYPE(ref_type);
  11317. PUSH_OFFSET_TYPE(ref_type);
  11318. PUSH_TYPE(ref_type);
  11319. #else
  11320. POP2_AND_PUSH(ref_type, ref_type);
  11321. #endif
  11322. }
  11323. else {
  11324. #if WASM_ENABLE_FAST_INTERP != 0
  11325. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11326. #endif
  11327. PUSH_TYPE(VALUE_TYPE_ANY);
  11328. }
  11329. break;
  11330. }
  11331. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11332. case WASM_OP_SELECT_T:
  11333. {
  11334. uint8 vec_len, type;
  11335. #if WASM_ENABLE_GC != 0
  11336. WASMRefType *ref_type = NULL;
  11337. #endif
  11338. #if WASM_ENABLE_FAST_INTERP != 0
  11339. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11340. #endif
  11341. pb_read_leb_uint32(p, p_end, vec_len);
  11342. if (vec_len != 1) {
  11343. /* typed select must have exactly one result */
  11344. set_error_buf(error_buf, error_buf_size,
  11345. "invalid result arity");
  11346. goto fail;
  11347. }
  11348. #if WASM_ENABLE_GC == 0
  11349. CHECK_BUF(p, p_end, 1);
  11350. type = read_uint8(p);
  11351. if (!is_valid_value_type_for_interpreter(type)) {
  11352. set_error_buf(error_buf, error_buf_size,
  11353. "unknown value type");
  11354. goto fail;
  11355. }
  11356. #else
  11357. p_org = p + 1;
  11358. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11359. module->type_count, &need_ref_type_map,
  11360. &wasm_ref_type, false, error_buf,
  11361. error_buf_size)) {
  11362. goto fail;
  11363. }
  11364. type = wasm_ref_type.ref_type;
  11365. if (need_ref_type_map) {
  11366. if (!(ref_type = reftype_set_insert(
  11367. module->ref_type_set, &wasm_ref_type, error_buf,
  11368. error_buf_size))) {
  11369. goto fail;
  11370. }
  11371. }
  11372. #if WASM_ENABLE_FAST_INTERP == 0
  11373. while (p_org < p) {
  11374. #if WASM_ENABLE_DEBUG_INTERP != 0
  11375. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11376. error_buf_size)) {
  11377. goto fail;
  11378. }
  11379. #endif
  11380. /* Ignore extra bytes for interpreter */
  11381. *p_org++ = WASM_OP_NOP;
  11382. }
  11383. #endif
  11384. #endif /* end of WASM_ENABLE_GC == 0 */
  11385. POP_I32();
  11386. #if WASM_ENABLE_FAST_INTERP != 0
  11387. if (loader_ctx->p_code_compiled) {
  11388. uint8 opcode_tmp = WASM_OP_SELECT;
  11389. if (type == VALUE_TYPE_V128) {
  11390. #if (WASM_ENABLE_SIMD == 0) \
  11391. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11392. set_error_buf(error_buf, error_buf_size,
  11393. "SIMD v128 type isn't supported");
  11394. goto fail;
  11395. #endif
  11396. }
  11397. else {
  11398. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11399. opcode_tmp = WASM_OP_SELECT_64;
  11400. #if WASM_ENABLE_GC != 0
  11401. if (wasm_is_type_reftype(type))
  11402. opcode_tmp = WASM_OP_SELECT_T;
  11403. #endif
  11404. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11405. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11406. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11407. handle_table[opcode_tmp];
  11408. #else
  11409. #if UINTPTR_MAX == UINT64_MAX
  11410. /* emit int32 relative offset in 64-bit target */
  11411. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11412. - (uint8 *)handle_table[0]);
  11413. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11414. offset;
  11415. #else
  11416. /* emit uint32 label address in 32-bit target */
  11417. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11418. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11419. #endif
  11420. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11421. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11422. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11423. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11424. #else
  11425. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11426. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11427. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11428. }
  11429. }
  11430. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11431. POP_REF(type);
  11432. #if WASM_ENABLE_GC != 0
  11433. if (need_ref_type_map) {
  11434. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11435. wasm_reftype_struct_size(ref_type));
  11436. }
  11437. #endif
  11438. POP_REF(type);
  11439. #if WASM_ENABLE_GC != 0
  11440. if (need_ref_type_map) {
  11441. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11442. wasm_reftype_struct_size(ref_type));
  11443. }
  11444. #endif
  11445. PUSH_REF(type);
  11446. #if WASM_ENABLE_WAMR_COMPILER != 0
  11447. module->is_ref_types_used = true;
  11448. #endif
  11449. (void)vec_len;
  11450. break;
  11451. }
  11452. /* table.get x. tables[x]. [it] -> [t] */
  11453. /* table.set x. tables[x]. [it t] -> [] */
  11454. case WASM_OP_TABLE_GET:
  11455. case WASM_OP_TABLE_SET:
  11456. {
  11457. uint8 decl_ref_type;
  11458. #if WASM_ENABLE_GC != 0
  11459. WASMRefType *ref_type;
  11460. #endif
  11461. pb_read_leb_uint32(p, p_end, table_idx);
  11462. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11463. #if WASM_ENABLE_GC != 0
  11464. (void **)&ref_type,
  11465. #else
  11466. NULL,
  11467. #endif
  11468. error_buf, error_buf_size))
  11469. goto fail;
  11470. #if WASM_ENABLE_GC != 0
  11471. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11472. bh_assert(ref_type);
  11473. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11474. ref_type, wasm_reftype_struct_size(ref_type));
  11475. }
  11476. #endif
  11477. #if WASM_ENABLE_FAST_INTERP != 0
  11478. emit_uint32(loader_ctx, table_idx);
  11479. #endif
  11480. #if WASM_ENABLE_MEMORY64 != 0
  11481. table_elem_idx_type = is_table_64bit(module, table_idx)
  11482. ? VALUE_TYPE_I64
  11483. : VALUE_TYPE_I32;
  11484. #endif
  11485. if (opcode == WASM_OP_TABLE_GET) {
  11486. POP_TBL_ELEM_IDX();
  11487. #if WASM_ENABLE_FAST_INTERP != 0
  11488. PUSH_OFFSET_TYPE(decl_ref_type);
  11489. #endif
  11490. PUSH_TYPE(decl_ref_type);
  11491. }
  11492. else {
  11493. #if WASM_ENABLE_FAST_INTERP != 0
  11494. POP_OFFSET_TYPE(decl_ref_type);
  11495. #endif
  11496. POP_TYPE(decl_ref_type);
  11497. POP_TBL_ELEM_IDX();
  11498. }
  11499. #if WASM_ENABLE_WAMR_COMPILER != 0
  11500. module->is_ref_types_used = true;
  11501. #endif
  11502. break;
  11503. }
  11504. case WASM_OP_REF_NULL:
  11505. {
  11506. uint8 ref_type;
  11507. #if WASM_ENABLE_GC == 0
  11508. CHECK_BUF(p, p_end, 1);
  11509. ref_type = read_uint8(p);
  11510. if (ref_type != VALUE_TYPE_FUNCREF
  11511. && ref_type != VALUE_TYPE_EXTERNREF) {
  11512. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11513. goto fail;
  11514. }
  11515. #else
  11516. pb_read_leb_int32(p, p_end, heap_type);
  11517. if (heap_type >= 0) {
  11518. if (!check_type_index(module, module->type_count, heap_type,
  11519. error_buf, error_buf_size)) {
  11520. goto fail;
  11521. }
  11522. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11523. true, heap_type);
  11524. ref_type = wasm_ref_type.ref_type;
  11525. }
  11526. else {
  11527. if (!wasm_is_valid_heap_type(heap_type)) {
  11528. set_error_buf(error_buf, error_buf_size,
  11529. "unknown type");
  11530. goto fail;
  11531. }
  11532. ref_type = (uint8)((int32)0x80 + heap_type);
  11533. }
  11534. #endif /* end of WASM_ENABLE_GC == 0 */
  11535. #if WASM_ENABLE_FAST_INTERP != 0
  11536. PUSH_OFFSET_TYPE(ref_type);
  11537. #endif
  11538. PUSH_TYPE(ref_type);
  11539. #if WASM_ENABLE_WAMR_COMPILER != 0
  11540. module->is_ref_types_used = true;
  11541. #endif
  11542. break;
  11543. }
  11544. case WASM_OP_REF_IS_NULL:
  11545. {
  11546. #if WASM_ENABLE_GC == 0
  11547. #if WASM_ENABLE_FAST_INTERP != 0
  11548. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11549. int32 block_stack_cell_num =
  11550. (int32)(loader_ctx->stack_cell_num
  11551. - cur_block->stack_cell_num);
  11552. if (block_stack_cell_num <= 0) {
  11553. if (!cur_block->is_stack_polymorphic) {
  11554. set_error_buf(
  11555. error_buf, error_buf_size,
  11556. "type mismatch: expect data but stack was empty");
  11557. goto fail;
  11558. }
  11559. }
  11560. else {
  11561. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11562. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11563. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11564. if (!wasm_loader_pop_frame_ref_offset(
  11565. loader_ctx, *(loader_ctx->frame_ref - 1),
  11566. error_buf, error_buf_size)) {
  11567. goto fail;
  11568. }
  11569. }
  11570. else {
  11571. set_error_buf(error_buf, error_buf_size,
  11572. "type mismatch");
  11573. goto fail;
  11574. }
  11575. }
  11576. #else
  11577. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11578. error_buf, error_buf_size)
  11579. && !wasm_loader_pop_frame_ref(loader_ctx,
  11580. VALUE_TYPE_EXTERNREF,
  11581. error_buf, error_buf_size)) {
  11582. goto fail;
  11583. }
  11584. #endif
  11585. #else /* else of WASM_ENABLE_GC == 0 */
  11586. uint8 type;
  11587. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11588. error_buf, error_buf_size)) {
  11589. goto fail;
  11590. }
  11591. #endif
  11592. PUSH_I32();
  11593. #if WASM_ENABLE_WAMR_COMPILER != 0
  11594. module->is_ref_types_used = true;
  11595. #endif
  11596. break;
  11597. }
  11598. case WASM_OP_REF_FUNC:
  11599. {
  11600. pb_read_leb_uint32(p, p_end, func_idx);
  11601. if (!check_function_index(module, func_idx, error_buf,
  11602. error_buf_size)) {
  11603. goto fail;
  11604. }
  11605. /* Refer to a forward-declared function:
  11606. the function must be an import, exported, or present in
  11607. a table elem segment or global initializer to be used as
  11608. the operand to ref.func */
  11609. if (func_idx >= module->import_function_count) {
  11610. WASMTableSeg *table_seg = module->table_segments;
  11611. bool func_declared = false;
  11612. uint32 j;
  11613. for (i = 0; i < module->global_count; i++) {
  11614. if (module->globals[i].type.val_type
  11615. == VALUE_TYPE_FUNCREF
  11616. && module->globals[i].init_expr.init_expr_type
  11617. == INIT_EXPR_TYPE_FUNCREF_CONST
  11618. && module->globals[i].init_expr.u.u32 == func_idx) {
  11619. func_declared = true;
  11620. break;
  11621. }
  11622. }
  11623. if (!func_declared) {
  11624. /* Check whether the function is declared in table segs,
  11625. note that it doesn't matter whether the table seg's
  11626. mode is passive, active or declarative. */
  11627. for (i = 0; i < module->table_seg_count;
  11628. i++, table_seg++) {
  11629. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11630. #if WASM_ENABLE_GC != 0
  11631. /* elem type is (ref null? func) or
  11632. (ref null? $t) */
  11633. || ((table_seg->elem_type
  11634. == REF_TYPE_HT_NON_NULLABLE
  11635. || table_seg->elem_type
  11636. == REF_TYPE_HT_NULLABLE)
  11637. && (table_seg->elem_ref_type->ref_ht_common
  11638. .heap_type
  11639. == HEAP_TYPE_FUNC
  11640. || table_seg->elem_ref_type
  11641. ->ref_ht_common.heap_type
  11642. > 0))
  11643. #endif
  11644. ) {
  11645. for (j = 0; j < table_seg->value_count; j++) {
  11646. if (table_seg->init_values[j].u.ref_index
  11647. == func_idx) {
  11648. func_declared = true;
  11649. break;
  11650. }
  11651. }
  11652. }
  11653. }
  11654. }
  11655. if (!func_declared) {
  11656. /* Check whether the function is exported */
  11657. for (i = 0; i < module->export_count; i++) {
  11658. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11659. && module->exports[i].index == func_idx) {
  11660. func_declared = true;
  11661. break;
  11662. }
  11663. }
  11664. }
  11665. if (!func_declared) {
  11666. set_error_buf(error_buf, error_buf_size,
  11667. "undeclared function reference");
  11668. goto fail;
  11669. }
  11670. }
  11671. #if WASM_ENABLE_FAST_INTERP != 0
  11672. emit_uint32(loader_ctx, func_idx);
  11673. #endif
  11674. #if WASM_ENABLE_GC == 0
  11675. PUSH_FUNCREF();
  11676. #else
  11677. if (func_idx < module->import_function_count)
  11678. type_idx =
  11679. module->import_functions[func_idx].u.function.type_idx;
  11680. else
  11681. type_idx = module
  11682. ->functions[func_idx
  11683. - module->import_function_count]
  11684. ->type_idx;
  11685. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11686. false, type_idx);
  11687. PUSH_REF(wasm_ref_type.ref_type);
  11688. #endif
  11689. #if WASM_ENABLE_WAMR_COMPILER != 0
  11690. module->is_ref_types_used = true;
  11691. #endif
  11692. break;
  11693. }
  11694. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11695. #if WASM_ENABLE_GC != 0
  11696. case WASM_OP_REF_AS_NON_NULL:
  11697. case WASM_OP_BR_ON_NULL:
  11698. {
  11699. uint8 type;
  11700. WASMRefType ref_type;
  11701. /* POP (ref null ht) and get the converted (ref ht) */
  11702. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11703. error_buf, error_buf_size)) {
  11704. goto fail;
  11705. }
  11706. if (opcode == WASM_OP_BR_ON_NULL) {
  11707. if (!(frame_csp_tmp =
  11708. check_branch_block(loader_ctx, &p, p_end, opcode,
  11709. error_buf, error_buf_size))) {
  11710. goto fail;
  11711. }
  11712. #if WASM_ENABLE_FAST_INTERP != 0
  11713. disable_emit = true;
  11714. #endif
  11715. }
  11716. /* PUSH the converted (ref ht) */
  11717. if (type != VALUE_TYPE_ANY) {
  11718. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11719. sizeof(WASMRefType));
  11720. }
  11721. PUSH_REF(type);
  11722. break;
  11723. }
  11724. case WASM_OP_BR_ON_NON_NULL:
  11725. {
  11726. uint8 type;
  11727. WASMRefType ref_type;
  11728. uint32 available_stack_cell =
  11729. loader_ctx->stack_cell_num
  11730. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11731. /* POP (ref null ht) and get the converted (ref ht) */
  11732. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11733. error_buf, error_buf_size)) {
  11734. goto fail;
  11735. }
  11736. #if WASM_ENABLE_FAST_INTERP != 0
  11737. disable_emit = true;
  11738. #endif
  11739. /* Temporarily PUSH back (ref ht), check brach block and
  11740. then POP it */
  11741. if (available_stack_cell
  11742. > 0) { /* stack isn't in polymorphic state */
  11743. if (type != VALUE_TYPE_ANY) {
  11744. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11745. &ref_type, sizeof(WASMRefType));
  11746. }
  11747. PUSH_REF(type);
  11748. }
  11749. if (!(frame_csp_tmp =
  11750. check_branch_block(loader_ctx, &p, p_end, opcode,
  11751. error_buf, error_buf_size))) {
  11752. goto fail;
  11753. }
  11754. if (available_stack_cell
  11755. > 0) { /* stack isn't in polymorphic state */
  11756. POP_REF(type);
  11757. #if WASM_ENABLE_FAST_INTERP != 0
  11758. /* Erase the opnd offset emitted by POP_REF() */
  11759. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11760. #endif
  11761. }
  11762. break;
  11763. }
  11764. case WASM_OP_REF_EQ:
  11765. POP_REF(REF_TYPE_EQREF);
  11766. POP_REF(REF_TYPE_EQREF);
  11767. PUSH_I32();
  11768. break;
  11769. #endif /* end of WASM_ENABLE_GC != 0 */
  11770. case WASM_OP_GET_LOCAL:
  11771. {
  11772. p_org = p - 1;
  11773. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11774. PUSH_TYPE(local_type);
  11775. #if WASM_ENABLE_GC != 0
  11776. /* Cannot get a non-nullable and unset local */
  11777. if (local_idx >= param_count
  11778. && wasm_is_reftype_htref_non_nullable(local_type)
  11779. && !wasm_loader_get_local_status(loader_ctx,
  11780. local_idx - param_count)) {
  11781. set_error_buf(error_buf, error_buf_size,
  11782. "uninitialized local");
  11783. goto fail;
  11784. }
  11785. #endif
  11786. #if WASM_ENABLE_FAST_INTERP != 0
  11787. /* Get Local is optimized out */
  11788. skip_label();
  11789. disable_emit = true;
  11790. operand_offset = local_offset;
  11791. PUSH_OFFSET_TYPE(local_type);
  11792. #else
  11793. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11794. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11795. if (local_offset < 0x80
  11796. #if WASM_ENABLE_GC != 0
  11797. && !wasm_is_type_reftype(local_type)
  11798. #endif
  11799. ) {
  11800. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11801. if (is_32bit_type(local_type)) {
  11802. *p_org++ = (uint8)local_offset;
  11803. }
  11804. else {
  11805. *p_org++ = (uint8)(local_offset | 0x80);
  11806. }
  11807. while (p_org < p) {
  11808. *p_org++ = WASM_OP_NOP;
  11809. }
  11810. }
  11811. #endif
  11812. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11813. break;
  11814. }
  11815. case WASM_OP_SET_LOCAL:
  11816. {
  11817. p_org = p - 1;
  11818. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11819. #if WASM_ENABLE_FAST_INTERP != 0
  11820. if (!(preserve_referenced_local(
  11821. loader_ctx, opcode, local_offset, local_type,
  11822. &preserve_local, error_buf, error_buf_size)))
  11823. goto fail;
  11824. if (local_offset < 256
  11825. #if WASM_ENABLE_GC != 0
  11826. && !wasm_is_type_reftype(local_type)
  11827. #endif
  11828. ) {
  11829. skip_label();
  11830. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11831. if (loader_ctx->p_code_compiled)
  11832. STORE_U16(loader_ctx->p_code_compiled - 2,
  11833. local_offset);
  11834. loader_ctx->frame_offset--;
  11835. loader_ctx->dynamic_offset--;
  11836. }
  11837. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11838. if (loader_ctx->p_code_compiled)
  11839. STORE_U16(loader_ctx->p_code_compiled - 2,
  11840. local_offset);
  11841. loader_ctx->frame_offset -= 2;
  11842. loader_ctx->dynamic_offset -= 2;
  11843. }
  11844. else {
  11845. if (is_32bit_type(local_type)) {
  11846. emit_label(EXT_OP_SET_LOCAL_FAST);
  11847. emit_byte(loader_ctx, (uint8)local_offset);
  11848. }
  11849. else {
  11850. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11851. emit_byte(loader_ctx, (uint8)local_offset);
  11852. }
  11853. POP_OFFSET_TYPE(local_type);
  11854. }
  11855. }
  11856. else { /* local index larger than 255, reserve leb */
  11857. emit_uint32(loader_ctx, local_idx);
  11858. POP_OFFSET_TYPE(local_type);
  11859. }
  11860. #else
  11861. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11862. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11863. if (local_offset < 0x80
  11864. #if WASM_ENABLE_GC != 0
  11865. && !wasm_is_type_reftype(local_type)
  11866. #endif
  11867. ) {
  11868. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11869. if (is_32bit_type(local_type)) {
  11870. *p_org++ = (uint8)local_offset;
  11871. }
  11872. else {
  11873. *p_org++ = (uint8)(local_offset | 0x80);
  11874. }
  11875. while (p_org < p) {
  11876. *p_org++ = WASM_OP_NOP;
  11877. }
  11878. }
  11879. #endif
  11880. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11881. #if WASM_ENABLE_GC != 0
  11882. if (local_idx >= param_count) {
  11883. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11884. }
  11885. #endif
  11886. POP_TYPE(local_type);
  11887. break;
  11888. }
  11889. case WASM_OP_TEE_LOCAL:
  11890. {
  11891. p_org = p - 1;
  11892. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11893. #if WASM_ENABLE_FAST_INTERP != 0
  11894. /* If the stack is in polymorphic state, do fake pop and push on
  11895. offset stack to keep the depth of offset stack to be the
  11896. same with ref stack */
  11897. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11898. if (cur_block->is_stack_polymorphic) {
  11899. POP_OFFSET_TYPE(local_type);
  11900. PUSH_OFFSET_TYPE(local_type);
  11901. }
  11902. #endif
  11903. POP_TYPE(local_type);
  11904. PUSH_TYPE(local_type);
  11905. #if WASM_ENABLE_FAST_INTERP != 0
  11906. if (!(preserve_referenced_local(
  11907. loader_ctx, opcode, local_offset, local_type,
  11908. &preserve_local, error_buf, error_buf_size)))
  11909. goto fail;
  11910. if (local_offset < 256
  11911. #if WASM_ENABLE_GC != 0
  11912. && !wasm_is_type_reftype(local_type)
  11913. #endif
  11914. ) {
  11915. skip_label();
  11916. if (is_32bit_type(local_type)) {
  11917. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11918. emit_byte(loader_ctx, (uint8)local_offset);
  11919. }
  11920. else {
  11921. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11922. emit_byte(loader_ctx, (uint8)local_offset);
  11923. }
  11924. }
  11925. else { /* local index larger than 255, reserve leb */
  11926. emit_uint32(loader_ctx, local_idx);
  11927. }
  11928. emit_operand(loader_ctx,
  11929. *(loader_ctx->frame_offset
  11930. - wasm_value_type_cell_num(local_type)));
  11931. #else
  11932. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11933. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11934. if (local_offset < 0x80
  11935. #if WASM_ENABLE_GC != 0
  11936. && !wasm_is_type_reftype(local_type)
  11937. #endif
  11938. ) {
  11939. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11940. if (is_32bit_type(local_type)) {
  11941. *p_org++ = (uint8)local_offset;
  11942. }
  11943. else {
  11944. *p_org++ = (uint8)(local_offset | 0x80);
  11945. }
  11946. while (p_org < p) {
  11947. *p_org++ = WASM_OP_NOP;
  11948. }
  11949. }
  11950. #endif
  11951. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11952. #if WASM_ENABLE_GC != 0
  11953. if (local_idx >= param_count) {
  11954. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11955. }
  11956. #endif
  11957. break;
  11958. }
  11959. case WASM_OP_GET_GLOBAL:
  11960. {
  11961. #if WASM_ENABLE_GC != 0
  11962. WASMRefType *ref_type;
  11963. #endif
  11964. p_org = p - 1;
  11965. pb_read_leb_uint32(p, p_end, global_idx);
  11966. if (global_idx >= global_count) {
  11967. set_error_buf(error_buf, error_buf_size, "unknown global");
  11968. goto fail;
  11969. }
  11970. global_type = global_idx < module->import_global_count
  11971. ? module->import_globals[global_idx]
  11972. .u.global.type.val_type
  11973. : module
  11974. ->globals[global_idx
  11975. - module->import_global_count]
  11976. .type.val_type;
  11977. #if WASM_ENABLE_GC != 0
  11978. ref_type =
  11979. global_idx < module->import_global_count
  11980. ? module->import_globals[global_idx].u.global.ref_type
  11981. : module
  11982. ->globals[global_idx
  11983. - module->import_global_count]
  11984. .ref_type;
  11985. if (wasm_is_type_multi_byte_type(global_type)) {
  11986. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11987. wasm_reftype_struct_size(ref_type));
  11988. }
  11989. #endif
  11990. PUSH_TYPE(global_type);
  11991. #if WASM_ENABLE_FAST_INTERP == 0
  11992. if (global_type == VALUE_TYPE_I64
  11993. || global_type == VALUE_TYPE_F64) {
  11994. #if WASM_ENABLE_DEBUG_INTERP != 0
  11995. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11996. error_buf_size)) {
  11997. goto fail;
  11998. }
  11999. #endif
  12000. *p_org = WASM_OP_GET_GLOBAL_64;
  12001. }
  12002. #else /* else of WASM_ENABLE_FAST_INTERP */
  12003. if (global_type == VALUE_TYPE_I64
  12004. || global_type == VALUE_TYPE_F64) {
  12005. skip_label();
  12006. emit_label(WASM_OP_GET_GLOBAL_64);
  12007. }
  12008. emit_uint32(loader_ctx, global_idx);
  12009. PUSH_OFFSET_TYPE(global_type);
  12010. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12011. break;
  12012. }
  12013. case WASM_OP_SET_GLOBAL:
  12014. {
  12015. bool is_mutable = false;
  12016. #if WASM_ENABLE_GC != 0
  12017. WASMRefType *ref_type;
  12018. #endif
  12019. p_org = p - 1;
  12020. pb_read_leb_uint32(p, p_end, global_idx);
  12021. if (global_idx >= global_count) {
  12022. set_error_buf(error_buf, error_buf_size, "unknown global");
  12023. goto fail;
  12024. }
  12025. is_mutable = global_idx < module->import_global_count
  12026. ? module->import_globals[global_idx]
  12027. .u.global.type.is_mutable
  12028. : module
  12029. ->globals[global_idx
  12030. - module->import_global_count]
  12031. .type.is_mutable;
  12032. if (!is_mutable) {
  12033. #if WASM_ENABLE_GC == 0
  12034. set_error_buf(error_buf, error_buf_size,
  12035. "global is immutable");
  12036. #else
  12037. set_error_buf(error_buf, error_buf_size,
  12038. "immutable global");
  12039. #endif
  12040. goto fail;
  12041. }
  12042. global_type = global_idx < module->import_global_count
  12043. ? module->import_globals[global_idx]
  12044. .u.global.type.val_type
  12045. : module
  12046. ->globals[global_idx
  12047. - module->import_global_count]
  12048. .type.val_type;
  12049. #if WASM_ENABLE_GC != 0
  12050. ref_type =
  12051. global_idx < module->import_global_count
  12052. ? module->import_globals[global_idx].u.global.ref_type
  12053. : module
  12054. ->globals[global_idx
  12055. - module->import_global_count]
  12056. .ref_type;
  12057. if (wasm_is_type_multi_byte_type(global_type)) {
  12058. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12059. wasm_reftype_struct_size(ref_type));
  12060. }
  12061. #endif
  12062. #if WASM_ENABLE_FAST_INTERP == 0
  12063. if (global_type == VALUE_TYPE_I64
  12064. || global_type == VALUE_TYPE_F64) {
  12065. #if WASM_ENABLE_DEBUG_INTERP != 0
  12066. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12067. error_buf_size)) {
  12068. goto fail;
  12069. }
  12070. #endif
  12071. *p_org = WASM_OP_SET_GLOBAL_64;
  12072. }
  12073. else if (module->aux_stack_size > 0
  12074. && global_idx == module->aux_stack_top_global_index) {
  12075. #if WASM_ENABLE_DEBUG_INTERP != 0
  12076. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12077. error_buf_size)) {
  12078. goto fail;
  12079. }
  12080. #endif
  12081. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12082. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12083. func->has_op_set_global_aux_stack = true;
  12084. #endif
  12085. }
  12086. #else /* else of WASM_ENABLE_FAST_INTERP */
  12087. if (global_type == VALUE_TYPE_I64
  12088. || global_type == VALUE_TYPE_F64) {
  12089. skip_label();
  12090. emit_label(WASM_OP_SET_GLOBAL_64);
  12091. }
  12092. else if (module->aux_stack_size > 0
  12093. && global_idx == module->aux_stack_top_global_index) {
  12094. skip_label();
  12095. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12096. }
  12097. emit_uint32(loader_ctx, global_idx);
  12098. POP_OFFSET_TYPE(global_type);
  12099. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12100. POP_TYPE(global_type);
  12101. break;
  12102. }
  12103. /* load */
  12104. case WASM_OP_I32_LOAD:
  12105. case WASM_OP_I32_LOAD8_S:
  12106. case WASM_OP_I32_LOAD8_U:
  12107. case WASM_OP_I32_LOAD16_S:
  12108. case WASM_OP_I32_LOAD16_U:
  12109. case WASM_OP_I64_LOAD:
  12110. case WASM_OP_I64_LOAD8_S:
  12111. case WASM_OP_I64_LOAD8_U:
  12112. case WASM_OP_I64_LOAD16_S:
  12113. case WASM_OP_I64_LOAD16_U:
  12114. case WASM_OP_I64_LOAD32_S:
  12115. case WASM_OP_I64_LOAD32_U:
  12116. case WASM_OP_F32_LOAD:
  12117. case WASM_OP_F64_LOAD:
  12118. /* store */
  12119. case WASM_OP_I32_STORE:
  12120. case WASM_OP_I32_STORE8:
  12121. case WASM_OP_I32_STORE16:
  12122. case WASM_OP_I64_STORE:
  12123. case WASM_OP_I64_STORE8:
  12124. case WASM_OP_I64_STORE16:
  12125. case WASM_OP_I64_STORE32:
  12126. case WASM_OP_F32_STORE:
  12127. case WASM_OP_F64_STORE:
  12128. {
  12129. #if WASM_ENABLE_FAST_INTERP != 0
  12130. /* change F32/F64 into I32/I64 */
  12131. if (opcode == WASM_OP_F32_LOAD) {
  12132. skip_label();
  12133. emit_label(WASM_OP_I32_LOAD);
  12134. }
  12135. else if (opcode == WASM_OP_F64_LOAD) {
  12136. skip_label();
  12137. emit_label(WASM_OP_I64_LOAD);
  12138. }
  12139. else if (opcode == WASM_OP_F32_STORE) {
  12140. skip_label();
  12141. emit_label(WASM_OP_I32_STORE);
  12142. }
  12143. else if (opcode == WASM_OP_F64_STORE) {
  12144. skip_label();
  12145. emit_label(WASM_OP_I64_STORE);
  12146. }
  12147. #endif
  12148. CHECK_MEMORY();
  12149. pb_read_leb_memarg(p, p_end, align); /* align */
  12150. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12151. if (!check_memory_access_align(opcode, align, error_buf,
  12152. error_buf_size)) {
  12153. goto fail;
  12154. }
  12155. #if WASM_ENABLE_FAST_INTERP != 0
  12156. emit_uint32(loader_ctx, mem_offset);
  12157. #endif
  12158. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12159. func->has_memory_operations = true;
  12160. #endif
  12161. switch (opcode) {
  12162. /* load */
  12163. case WASM_OP_I32_LOAD:
  12164. case WASM_OP_I32_LOAD8_S:
  12165. case WASM_OP_I32_LOAD8_U:
  12166. case WASM_OP_I32_LOAD16_S:
  12167. case WASM_OP_I32_LOAD16_U:
  12168. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12169. break;
  12170. case WASM_OP_I64_LOAD:
  12171. case WASM_OP_I64_LOAD8_S:
  12172. case WASM_OP_I64_LOAD8_U:
  12173. case WASM_OP_I64_LOAD16_S:
  12174. case WASM_OP_I64_LOAD16_U:
  12175. case WASM_OP_I64_LOAD32_S:
  12176. case WASM_OP_I64_LOAD32_U:
  12177. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12178. break;
  12179. case WASM_OP_F32_LOAD:
  12180. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12181. break;
  12182. case WASM_OP_F64_LOAD:
  12183. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12184. break;
  12185. /* store */
  12186. case WASM_OP_I32_STORE:
  12187. case WASM_OP_I32_STORE8:
  12188. case WASM_OP_I32_STORE16:
  12189. POP_I32();
  12190. POP_MEM_OFFSET();
  12191. break;
  12192. case WASM_OP_I64_STORE:
  12193. case WASM_OP_I64_STORE8:
  12194. case WASM_OP_I64_STORE16:
  12195. case WASM_OP_I64_STORE32:
  12196. POP_I64();
  12197. POP_MEM_OFFSET();
  12198. break;
  12199. case WASM_OP_F32_STORE:
  12200. POP_F32();
  12201. POP_MEM_OFFSET();
  12202. break;
  12203. case WASM_OP_F64_STORE:
  12204. POP_F64();
  12205. POP_MEM_OFFSET();
  12206. break;
  12207. default:
  12208. break;
  12209. }
  12210. break;
  12211. }
  12212. case WASM_OP_MEMORY_SIZE:
  12213. CHECK_MEMORY();
  12214. pb_read_leb_uint32(p, p_end, memidx);
  12215. check_memidx(module, memidx);
  12216. PUSH_PAGE_COUNT();
  12217. module->possible_memory_grow = true;
  12218. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12219. func->has_memory_operations = true;
  12220. #endif
  12221. break;
  12222. case WASM_OP_MEMORY_GROW:
  12223. CHECK_MEMORY();
  12224. pb_read_leb_uint32(p, p_end, memidx);
  12225. check_memidx(module, memidx);
  12226. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12227. module->possible_memory_grow = true;
  12228. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12229. || WASM_ENABLE_WAMR_COMPILER != 0
  12230. func->has_op_memory_grow = true;
  12231. #endif
  12232. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12233. func->has_memory_operations = true;
  12234. #endif
  12235. break;
  12236. case WASM_OP_I32_CONST:
  12237. pb_read_leb_int32(p, p_end, i32_const);
  12238. #if WASM_ENABLE_FAST_INTERP != 0
  12239. skip_label();
  12240. disable_emit = true;
  12241. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12242. if (operand_offset == 0) {
  12243. disable_emit = false;
  12244. emit_label(WASM_OP_I32_CONST);
  12245. emit_uint32(loader_ctx, i32_const);
  12246. }
  12247. #else
  12248. (void)i32_const;
  12249. #endif
  12250. PUSH_I32();
  12251. break;
  12252. case WASM_OP_I64_CONST:
  12253. pb_read_leb_int64(p, p_end, i64_const);
  12254. #if WASM_ENABLE_FAST_INTERP != 0
  12255. skip_label();
  12256. disable_emit = true;
  12257. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12258. if (operand_offset == 0) {
  12259. disable_emit = false;
  12260. emit_label(WASM_OP_I64_CONST);
  12261. emit_uint64(loader_ctx, i64_const);
  12262. }
  12263. #endif
  12264. PUSH_I64();
  12265. break;
  12266. case WASM_OP_F32_CONST:
  12267. CHECK_BUF(p, p_end, sizeof(float32));
  12268. p += sizeof(float32);
  12269. #if WASM_ENABLE_FAST_INTERP != 0
  12270. skip_label();
  12271. disable_emit = true;
  12272. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12273. sizeof(float32));
  12274. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12275. if (operand_offset == 0) {
  12276. disable_emit = false;
  12277. emit_label(WASM_OP_F32_CONST);
  12278. emit_float32(loader_ctx, f32_const);
  12279. }
  12280. #endif
  12281. PUSH_F32();
  12282. break;
  12283. case WASM_OP_F64_CONST:
  12284. CHECK_BUF(p, p_end, sizeof(float64));
  12285. p += sizeof(float64);
  12286. #if WASM_ENABLE_FAST_INTERP != 0
  12287. skip_label();
  12288. disable_emit = true;
  12289. /* Some MCU may require 8-byte align */
  12290. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12291. sizeof(float64));
  12292. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12293. if (operand_offset == 0) {
  12294. disable_emit = false;
  12295. emit_label(WASM_OP_F64_CONST);
  12296. emit_float64(loader_ctx, f64_const);
  12297. }
  12298. #endif
  12299. PUSH_F64();
  12300. break;
  12301. case WASM_OP_I32_EQZ:
  12302. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12303. break;
  12304. case WASM_OP_I32_EQ:
  12305. case WASM_OP_I32_NE:
  12306. case WASM_OP_I32_LT_S:
  12307. case WASM_OP_I32_LT_U:
  12308. case WASM_OP_I32_GT_S:
  12309. case WASM_OP_I32_GT_U:
  12310. case WASM_OP_I32_LE_S:
  12311. case WASM_OP_I32_LE_U:
  12312. case WASM_OP_I32_GE_S:
  12313. case WASM_OP_I32_GE_U:
  12314. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12315. break;
  12316. case WASM_OP_I64_EQZ:
  12317. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12318. break;
  12319. case WASM_OP_I64_EQ:
  12320. case WASM_OP_I64_NE:
  12321. case WASM_OP_I64_LT_S:
  12322. case WASM_OP_I64_LT_U:
  12323. case WASM_OP_I64_GT_S:
  12324. case WASM_OP_I64_GT_U:
  12325. case WASM_OP_I64_LE_S:
  12326. case WASM_OP_I64_LE_U:
  12327. case WASM_OP_I64_GE_S:
  12328. case WASM_OP_I64_GE_U:
  12329. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12330. break;
  12331. case WASM_OP_F32_EQ:
  12332. case WASM_OP_F32_NE:
  12333. case WASM_OP_F32_LT:
  12334. case WASM_OP_F32_GT:
  12335. case WASM_OP_F32_LE:
  12336. case WASM_OP_F32_GE:
  12337. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12338. break;
  12339. case WASM_OP_F64_EQ:
  12340. case WASM_OP_F64_NE:
  12341. case WASM_OP_F64_LT:
  12342. case WASM_OP_F64_GT:
  12343. case WASM_OP_F64_LE:
  12344. case WASM_OP_F64_GE:
  12345. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12346. break;
  12347. case WASM_OP_I32_CLZ:
  12348. case WASM_OP_I32_CTZ:
  12349. case WASM_OP_I32_POPCNT:
  12350. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12351. break;
  12352. case WASM_OP_I32_ADD:
  12353. case WASM_OP_I32_SUB:
  12354. case WASM_OP_I32_MUL:
  12355. case WASM_OP_I32_DIV_S:
  12356. case WASM_OP_I32_DIV_U:
  12357. case WASM_OP_I32_REM_S:
  12358. case WASM_OP_I32_REM_U:
  12359. case WASM_OP_I32_AND:
  12360. case WASM_OP_I32_OR:
  12361. case WASM_OP_I32_XOR:
  12362. case WASM_OP_I32_SHL:
  12363. case WASM_OP_I32_SHR_S:
  12364. case WASM_OP_I32_SHR_U:
  12365. case WASM_OP_I32_ROTL:
  12366. case WASM_OP_I32_ROTR:
  12367. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12368. break;
  12369. case WASM_OP_I64_CLZ:
  12370. case WASM_OP_I64_CTZ:
  12371. case WASM_OP_I64_POPCNT:
  12372. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12373. break;
  12374. case WASM_OP_I64_ADD:
  12375. case WASM_OP_I64_SUB:
  12376. case WASM_OP_I64_MUL:
  12377. case WASM_OP_I64_DIV_S:
  12378. case WASM_OP_I64_DIV_U:
  12379. case WASM_OP_I64_REM_S:
  12380. case WASM_OP_I64_REM_U:
  12381. case WASM_OP_I64_AND:
  12382. case WASM_OP_I64_OR:
  12383. case WASM_OP_I64_XOR:
  12384. case WASM_OP_I64_SHL:
  12385. case WASM_OP_I64_SHR_S:
  12386. case WASM_OP_I64_SHR_U:
  12387. case WASM_OP_I64_ROTL:
  12388. case WASM_OP_I64_ROTR:
  12389. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12390. break;
  12391. case WASM_OP_F32_ABS:
  12392. case WASM_OP_F32_NEG:
  12393. case WASM_OP_F32_CEIL:
  12394. case WASM_OP_F32_FLOOR:
  12395. case WASM_OP_F32_TRUNC:
  12396. case WASM_OP_F32_NEAREST:
  12397. case WASM_OP_F32_SQRT:
  12398. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12399. break;
  12400. case WASM_OP_F32_ADD:
  12401. case WASM_OP_F32_SUB:
  12402. case WASM_OP_F32_MUL:
  12403. case WASM_OP_F32_DIV:
  12404. case WASM_OP_F32_MIN:
  12405. case WASM_OP_F32_MAX:
  12406. case WASM_OP_F32_COPYSIGN:
  12407. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12408. break;
  12409. case WASM_OP_F64_ABS:
  12410. case WASM_OP_F64_NEG:
  12411. case WASM_OP_F64_CEIL:
  12412. case WASM_OP_F64_FLOOR:
  12413. case WASM_OP_F64_TRUNC:
  12414. case WASM_OP_F64_NEAREST:
  12415. case WASM_OP_F64_SQRT:
  12416. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12417. break;
  12418. case WASM_OP_F64_ADD:
  12419. case WASM_OP_F64_SUB:
  12420. case WASM_OP_F64_MUL:
  12421. case WASM_OP_F64_DIV:
  12422. case WASM_OP_F64_MIN:
  12423. case WASM_OP_F64_MAX:
  12424. case WASM_OP_F64_COPYSIGN:
  12425. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12426. break;
  12427. case WASM_OP_I32_WRAP_I64:
  12428. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12429. break;
  12430. case WASM_OP_I32_TRUNC_S_F32:
  12431. case WASM_OP_I32_TRUNC_U_F32:
  12432. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12433. break;
  12434. case WASM_OP_I32_TRUNC_S_F64:
  12435. case WASM_OP_I32_TRUNC_U_F64:
  12436. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12437. break;
  12438. case WASM_OP_I64_EXTEND_S_I32:
  12439. case WASM_OP_I64_EXTEND_U_I32:
  12440. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12441. break;
  12442. case WASM_OP_I64_TRUNC_S_F32:
  12443. case WASM_OP_I64_TRUNC_U_F32:
  12444. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12445. break;
  12446. case WASM_OP_I64_TRUNC_S_F64:
  12447. case WASM_OP_I64_TRUNC_U_F64:
  12448. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12449. break;
  12450. case WASM_OP_F32_CONVERT_S_I32:
  12451. case WASM_OP_F32_CONVERT_U_I32:
  12452. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12453. break;
  12454. case WASM_OP_F32_CONVERT_S_I64:
  12455. case WASM_OP_F32_CONVERT_U_I64:
  12456. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12457. break;
  12458. case WASM_OP_F32_DEMOTE_F64:
  12459. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12460. break;
  12461. case WASM_OP_F64_CONVERT_S_I32:
  12462. case WASM_OP_F64_CONVERT_U_I32:
  12463. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12464. break;
  12465. case WASM_OP_F64_CONVERT_S_I64:
  12466. case WASM_OP_F64_CONVERT_U_I64:
  12467. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12468. break;
  12469. case WASM_OP_F64_PROMOTE_F32:
  12470. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12471. break;
  12472. case WASM_OP_I32_REINTERPRET_F32:
  12473. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12474. break;
  12475. case WASM_OP_I64_REINTERPRET_F64:
  12476. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12477. break;
  12478. case WASM_OP_F32_REINTERPRET_I32:
  12479. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12480. break;
  12481. case WASM_OP_F64_REINTERPRET_I64:
  12482. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12483. break;
  12484. case WASM_OP_I32_EXTEND8_S:
  12485. case WASM_OP_I32_EXTEND16_S:
  12486. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12487. break;
  12488. case WASM_OP_I64_EXTEND8_S:
  12489. case WASM_OP_I64_EXTEND16_S:
  12490. case WASM_OP_I64_EXTEND32_S:
  12491. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12492. break;
  12493. #if WASM_ENABLE_GC != 0
  12494. case WASM_OP_GC_PREFIX:
  12495. {
  12496. uint32 opcode1;
  12497. pb_read_leb_uint32(p, p_end, opcode1);
  12498. #if WASM_ENABLE_FAST_INTERP != 0
  12499. emit_byte(loader_ctx, ((uint8)opcode1));
  12500. #endif
  12501. switch (opcode1) {
  12502. case WASM_OP_STRUCT_NEW:
  12503. case WASM_OP_STRUCT_NEW_DEFAULT:
  12504. {
  12505. pb_read_leb_uint32(p, p_end, type_idx);
  12506. #if WASM_ENABLE_FAST_INTERP != 0
  12507. emit_uint32(loader_ctx, type_idx);
  12508. #endif
  12509. if (!check_type_index(module, module->type_count,
  12510. type_idx, error_buf,
  12511. error_buf_size)) {
  12512. goto fail;
  12513. }
  12514. if (module->types[type_idx]->type_flag
  12515. != WASM_TYPE_STRUCT) {
  12516. set_error_buf(error_buf, error_buf_size,
  12517. "unkown struct type");
  12518. goto fail;
  12519. }
  12520. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12521. int32 j, k;
  12522. uint8 value_type;
  12523. uint32 ref_type_struct_size;
  12524. WASMStructType *struct_type =
  12525. (WASMStructType *)module->types[type_idx];
  12526. k = struct_type->ref_type_map_count - 1;
  12527. for (j = struct_type->field_count - 1; j >= 0;
  12528. j--) {
  12529. value_type = struct_type->fields[j].field_type;
  12530. if (wasm_is_type_reftype(value_type)) {
  12531. if (wasm_is_type_multi_byte_type(
  12532. value_type)) {
  12533. ref_type_struct_size =
  12534. wasm_reftype_struct_size(
  12535. struct_type->ref_type_maps[k]
  12536. .ref_type);
  12537. bh_memcpy_s(
  12538. &wasm_ref_type,
  12539. (uint32)sizeof(WASMRefType),
  12540. struct_type->ref_type_maps[k]
  12541. .ref_type,
  12542. ref_type_struct_size);
  12543. k--;
  12544. }
  12545. POP_REF(value_type);
  12546. }
  12547. else {
  12548. switch (value_type) {
  12549. case VALUE_TYPE_I32:
  12550. case PACKED_TYPE_I8:
  12551. case PACKED_TYPE_I16:
  12552. POP_I32();
  12553. break;
  12554. case VALUE_TYPE_I64:
  12555. POP_I64();
  12556. break;
  12557. case VALUE_TYPE_F32:
  12558. POP_F32();
  12559. break;
  12560. case VALUE_TYPE_F64:
  12561. POP_F64();
  12562. break;
  12563. default:
  12564. set_error_buf(error_buf,
  12565. error_buf_size,
  12566. "unknown type");
  12567. goto fail;
  12568. }
  12569. }
  12570. }
  12571. }
  12572. /* PUSH struct obj, (ref $t) */
  12573. wasm_set_refheaptype_typeidx(
  12574. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12575. PUSH_REF(wasm_ref_type.ref_type);
  12576. break;
  12577. }
  12578. case WASM_OP_STRUCT_GET:
  12579. case WASM_OP_STRUCT_GET_S:
  12580. case WASM_OP_STRUCT_GET_U:
  12581. case WASM_OP_STRUCT_SET:
  12582. {
  12583. WASMStructType *struct_type;
  12584. WASMRefType *ref_type = NULL;
  12585. uint32 field_idx;
  12586. uint8 field_type;
  12587. pb_read_leb_uint32(p, p_end, type_idx);
  12588. #if WASM_ENABLE_FAST_INTERP != 0
  12589. emit_uint32(loader_ctx, type_idx);
  12590. #endif
  12591. if (!check_type_index(module, module->type_count,
  12592. type_idx, error_buf,
  12593. error_buf_size)) {
  12594. goto fail;
  12595. }
  12596. if (module->types[type_idx]->type_flag
  12597. != WASM_TYPE_STRUCT) {
  12598. set_error_buf(error_buf, error_buf_size,
  12599. "unknown struct type");
  12600. goto fail;
  12601. }
  12602. struct_type = (WASMStructType *)module->types[type_idx];
  12603. pb_read_leb_uint32(p, p_end, field_idx);
  12604. #if WASM_ENABLE_FAST_INTERP != 0
  12605. emit_uint32(loader_ctx, field_idx);
  12606. #endif
  12607. if (field_idx >= struct_type->field_count) {
  12608. set_error_buf(error_buf, error_buf_size,
  12609. "unknown struct field");
  12610. goto fail;
  12611. }
  12612. if (opcode1 == WASM_OP_STRUCT_SET
  12613. && !(struct_type->fields[field_idx].field_flags
  12614. & 1)) {
  12615. set_error_buf(error_buf, error_buf_size,
  12616. "field is immutable");
  12617. goto fail;
  12618. }
  12619. field_type = struct_type->fields[field_idx].field_type;
  12620. if (is_packed_type(field_type)) {
  12621. if (opcode1 == WASM_OP_STRUCT_GET) {
  12622. set_error_buf(error_buf, error_buf_size,
  12623. "type mismatch");
  12624. goto fail;
  12625. }
  12626. else {
  12627. field_type = VALUE_TYPE_I32;
  12628. }
  12629. }
  12630. if (wasm_is_type_multi_byte_type(field_type)) {
  12631. ref_type = wasm_reftype_map_find(
  12632. struct_type->ref_type_maps,
  12633. struct_type->ref_type_map_count, field_idx);
  12634. bh_assert(ref_type);
  12635. }
  12636. if (opcode1 == WASM_OP_STRUCT_SET) {
  12637. /* POP field */
  12638. if (wasm_is_type_multi_byte_type(field_type)) {
  12639. bh_memcpy_s(&wasm_ref_type,
  12640. (uint32)sizeof(WASMRefType),
  12641. ref_type,
  12642. wasm_reftype_struct_size(ref_type));
  12643. }
  12644. POP_REF(field_type);
  12645. /* POP struct obj, (ref null $t) */
  12646. wasm_set_refheaptype_typeidx(
  12647. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12648. POP_REF(wasm_ref_type.ref_type);
  12649. }
  12650. else {
  12651. /* POP struct obj, (ref null $t) */
  12652. wasm_set_refheaptype_typeidx(
  12653. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12654. POP_REF(wasm_ref_type.ref_type);
  12655. /* PUSH field */
  12656. if (wasm_is_type_multi_byte_type(field_type)) {
  12657. bh_memcpy_s(&wasm_ref_type,
  12658. (uint32)sizeof(WASMRefType),
  12659. ref_type,
  12660. wasm_reftype_struct_size(ref_type));
  12661. }
  12662. PUSH_REF(field_type);
  12663. }
  12664. break;
  12665. }
  12666. case WASM_OP_ARRAY_NEW:
  12667. case WASM_OP_ARRAY_NEW_DEFAULT:
  12668. case WASM_OP_ARRAY_NEW_FIXED:
  12669. case WASM_OP_ARRAY_NEW_DATA:
  12670. case WASM_OP_ARRAY_NEW_ELEM:
  12671. {
  12672. WASMArrayType *array_type;
  12673. uint8 elem_type;
  12674. uint32 u32 = 0;
  12675. pb_read_leb_uint32(p, p_end, type_idx);
  12676. #if WASM_ENABLE_FAST_INTERP != 0
  12677. emit_uint32(loader_ctx, type_idx);
  12678. #endif
  12679. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12680. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12681. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12682. pb_read_leb_uint32(p, p_end, u32);
  12683. #if WASM_ENABLE_FAST_INTERP != 0
  12684. emit_uint32(loader_ctx, u32);
  12685. #endif
  12686. }
  12687. if (!check_array_type(module, type_idx, error_buf,
  12688. error_buf_size)) {
  12689. goto fail;
  12690. }
  12691. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12692. /* length */
  12693. POP_I32();
  12694. }
  12695. array_type = (WASMArrayType *)module->types[type_idx];
  12696. elem_type = array_type->elem_type;
  12697. if (opcode1 == WASM_OP_ARRAY_NEW
  12698. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12699. if (wasm_is_type_multi_byte_type(elem_type)) {
  12700. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12701. array_type->elem_ref_type,
  12702. wasm_reftype_struct_size(
  12703. array_type->elem_ref_type));
  12704. }
  12705. if (is_packed_type(elem_type)) {
  12706. elem_type = VALUE_TYPE_I32;
  12707. }
  12708. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12709. uint32 N = u32;
  12710. for (i = 0; i < N; i++) {
  12711. if (wasm_is_type_multi_byte_type(
  12712. elem_type)) {
  12713. bh_memcpy_s(
  12714. &wasm_ref_type, sizeof(WASMRefType),
  12715. array_type->elem_ref_type,
  12716. wasm_reftype_struct_size(
  12717. array_type->elem_ref_type));
  12718. }
  12719. POP_REF(elem_type);
  12720. }
  12721. }
  12722. else
  12723. POP_REF(elem_type);
  12724. }
  12725. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12726. /* offset of data segment */
  12727. POP_I32();
  12728. if (u32 >= module->data_seg_count) {
  12729. set_error_buf(error_buf, error_buf_size,
  12730. "unknown data segment");
  12731. goto fail;
  12732. }
  12733. if (wasm_is_type_reftype(elem_type)) {
  12734. set_error_buf(error_buf, error_buf_size,
  12735. "array elem type mismatch");
  12736. goto fail;
  12737. }
  12738. }
  12739. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12740. WASMTableSeg *table_seg =
  12741. module->table_segments + u32;
  12742. /* offset of element segment */
  12743. POP_I32();
  12744. if (u32 >= module->table_seg_count) {
  12745. set_error_buf(error_buf, error_buf_size,
  12746. "unknown element segment");
  12747. goto fail;
  12748. }
  12749. if (!wasm_reftype_is_subtype_of(
  12750. table_seg->elem_type,
  12751. table_seg->elem_ref_type, elem_type,
  12752. array_type->elem_ref_type, module->types,
  12753. module->type_count)) {
  12754. set_error_buf(error_buf, error_buf_size,
  12755. "array elem type mismatch");
  12756. goto fail;
  12757. }
  12758. }
  12759. /* PUSH array obj, (ref $t) */
  12760. wasm_set_refheaptype_typeidx(
  12761. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12762. PUSH_REF(wasm_ref_type.ref_type);
  12763. break;
  12764. }
  12765. case WASM_OP_ARRAY_GET:
  12766. case WASM_OP_ARRAY_GET_S:
  12767. case WASM_OP_ARRAY_GET_U:
  12768. case WASM_OP_ARRAY_SET:
  12769. {
  12770. uint8 elem_type;
  12771. WASMArrayType *array_type;
  12772. WASMRefType *ref_type = NULL;
  12773. pb_read_leb_uint32(p, p_end, type_idx);
  12774. #if WASM_ENABLE_FAST_INTERP != 0
  12775. emit_uint32(loader_ctx, type_idx);
  12776. #endif
  12777. if (!check_array_type(module, type_idx, error_buf,
  12778. error_buf_size)) {
  12779. goto fail;
  12780. }
  12781. array_type = (WASMArrayType *)module->types[type_idx];
  12782. if (opcode1 == WASM_OP_ARRAY_SET
  12783. && !(array_type->elem_flags & 1)) {
  12784. set_error_buf(error_buf, error_buf_size,
  12785. "array is immutable");
  12786. goto fail;
  12787. }
  12788. elem_type = array_type->elem_type;
  12789. if (is_packed_type(elem_type)) {
  12790. if (opcode1 != WASM_OP_ARRAY_GET_S
  12791. && opcode1 != WASM_OP_ARRAY_GET_U
  12792. && opcode1 != WASM_OP_ARRAY_SET) {
  12793. set_error_buf(error_buf, error_buf_size,
  12794. "type mismatch");
  12795. goto fail;
  12796. }
  12797. else {
  12798. elem_type = VALUE_TYPE_I32;
  12799. }
  12800. }
  12801. ref_type = array_type->elem_ref_type;
  12802. if (opcode1 == WASM_OP_ARRAY_SET) {
  12803. /* POP elem to set */
  12804. if (wasm_is_type_multi_byte_type(elem_type)) {
  12805. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12806. ref_type,
  12807. wasm_reftype_struct_size(ref_type));
  12808. }
  12809. POP_REF(elem_type);
  12810. }
  12811. /* elem idx */
  12812. POP_I32();
  12813. /* POP array obj, (ref null $t) */
  12814. wasm_set_refheaptype_typeidx(
  12815. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12816. POP_REF(wasm_ref_type.ref_type);
  12817. if (opcode1 != WASM_OP_ARRAY_SET) {
  12818. /* PUSH elem */
  12819. if (wasm_is_type_multi_byte_type(elem_type)) {
  12820. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12821. ref_type,
  12822. wasm_reftype_struct_size(ref_type));
  12823. }
  12824. PUSH_REF(elem_type);
  12825. }
  12826. break;
  12827. }
  12828. case WASM_OP_ARRAY_LEN:
  12829. {
  12830. POP_REF(REF_TYPE_ARRAYREF);
  12831. /* length */
  12832. PUSH_I32();
  12833. break;
  12834. }
  12835. case WASM_OP_ARRAY_FILL:
  12836. {
  12837. WASMArrayType *array_type;
  12838. uint8 elem_type;
  12839. /* typeidx */
  12840. pb_read_leb_uint32(p, p_end, type_idx);
  12841. #if WASM_ENABLE_FAST_INTERP != 0
  12842. emit_uint32(loader_ctx, type_idx);
  12843. #endif
  12844. if (!check_array_type(module, type_idx, error_buf,
  12845. error_buf_size)) {
  12846. goto fail;
  12847. }
  12848. array_type = (WASMArrayType *)module->types[type_idx];
  12849. if (!(array_type->elem_flags & 1)) {
  12850. set_error_buf(error_buf, error_buf_size,
  12851. "array is immutable");
  12852. goto fail;
  12853. }
  12854. elem_type = array_type->elem_type;
  12855. if (is_packed_type(elem_type)) {
  12856. elem_type = VALUE_TYPE_I32;
  12857. }
  12858. POP_I32(); /* length */
  12859. #if WASM_ENABLE_FAST_INTERP != 0
  12860. POP_OFFSET_TYPE(elem_type);
  12861. #endif
  12862. POP_TYPE(elem_type);
  12863. POP_I32(); /* start */
  12864. /* POP array obj, (ref null $t) */
  12865. wasm_set_refheaptype_typeidx(
  12866. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12867. POP_REF(wasm_ref_type.ref_type);
  12868. break;
  12869. }
  12870. case WASM_OP_ARRAY_COPY:
  12871. {
  12872. uint32 src_type_idx;
  12873. uint8 src_elem_type, dst_elem_type;
  12874. WASMRefType src_ref_type = { 0 },
  12875. *src_elem_ref_type = NULL;
  12876. WASMRefType dst_ref_type = { 0 },
  12877. *dst_elem_ref_type = NULL;
  12878. WASMArrayType *array_type;
  12879. /* typeidx1 */
  12880. pb_read_leb_uint32(p, p_end, type_idx);
  12881. #if WASM_ENABLE_FAST_INTERP != 0
  12882. emit_uint32(loader_ctx, type_idx);
  12883. #endif
  12884. /* typeidx2 */
  12885. pb_read_leb_uint32(p, p_end, src_type_idx);
  12886. #if WASM_ENABLE_FAST_INTERP != 0
  12887. emit_uint32(loader_ctx, src_type_idx);
  12888. #endif
  12889. if (!check_array_type(module, type_idx, error_buf,
  12890. error_buf_size)) {
  12891. goto fail;
  12892. }
  12893. if (!check_array_type(module, src_type_idx, error_buf,
  12894. error_buf_size)) {
  12895. goto fail;
  12896. }
  12897. POP_I32();
  12898. POP_I32();
  12899. /* POP array obj, (ref null $t) */
  12900. wasm_set_refheaptype_typeidx(
  12901. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12902. POP_REF(wasm_ref_type.ref_type);
  12903. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12904. &wasm_ref_type,
  12905. wasm_reftype_struct_size(&wasm_ref_type));
  12906. POP_I32();
  12907. /* POP array obj, (ref null $t) */
  12908. wasm_set_refheaptype_typeidx(
  12909. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12910. POP_REF(wasm_ref_type.ref_type);
  12911. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12912. &wasm_ref_type,
  12913. wasm_reftype_struct_size(&wasm_ref_type));
  12914. array_type = (WASMArrayType *)module->types[type_idx];
  12915. if (!(array_type->elem_flags & 1)) {
  12916. set_error_buf(error_buf, error_buf_size,
  12917. "destination array is immutable");
  12918. goto fail;
  12919. }
  12920. dst_elem_type = array_type->elem_type;
  12921. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12922. dst_elem_ref_type = array_type->elem_ref_type;
  12923. }
  12924. array_type =
  12925. (WASMArrayType *)module->types[src_type_idx];
  12926. src_elem_type = array_type->elem_type;
  12927. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12928. src_elem_ref_type = array_type->elem_ref_type;
  12929. }
  12930. if (!wasm_reftype_is_subtype_of(
  12931. src_elem_type, src_elem_ref_type, dst_elem_type,
  12932. dst_elem_ref_type, module->types,
  12933. module->type_count)) {
  12934. set_error_buf(error_buf, error_buf_size,
  12935. "array types do not match");
  12936. goto fail;
  12937. }
  12938. break;
  12939. }
  12940. case WASM_OP_REF_I31:
  12941. {
  12942. POP_I32();
  12943. wasm_set_refheaptype_common(
  12944. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12945. PUSH_REF(wasm_ref_type.ref_type);
  12946. break;
  12947. }
  12948. case WASM_OP_I31_GET_S:
  12949. case WASM_OP_I31_GET_U:
  12950. {
  12951. POP_REF(REF_TYPE_I31REF);
  12952. PUSH_I32();
  12953. break;
  12954. }
  12955. case WASM_OP_REF_TEST:
  12956. case WASM_OP_REF_CAST:
  12957. case WASM_OP_REF_TEST_NULLABLE:
  12958. case WASM_OP_REF_CAST_NULLABLE:
  12959. {
  12960. uint8 type;
  12961. pb_read_leb_int32(p, p_end, heap_type);
  12962. #if WASM_ENABLE_FAST_INTERP != 0
  12963. emit_uint32(loader_ctx, (uint32)heap_type);
  12964. #endif
  12965. if (heap_type >= 0) {
  12966. if (!check_type_index(module, module->type_count,
  12967. heap_type, error_buf,
  12968. error_buf_size)) {
  12969. goto fail;
  12970. }
  12971. }
  12972. else {
  12973. if (!wasm_is_valid_heap_type(heap_type)) {
  12974. set_error_buf(error_buf, error_buf_size,
  12975. "unknown type");
  12976. goto fail;
  12977. }
  12978. }
  12979. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12980. &wasm_ref_type, error_buf,
  12981. error_buf_size)) {
  12982. goto fail;
  12983. }
  12984. if (opcode1 == WASM_OP_REF_TEST
  12985. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12986. PUSH_I32();
  12987. else {
  12988. bool nullable =
  12989. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12990. : false;
  12991. if (heap_type >= 0 || !nullable) {
  12992. wasm_set_refheaptype_typeidx(
  12993. &wasm_ref_type.ref_ht_typeidx, nullable,
  12994. heap_type);
  12995. PUSH_REF(wasm_ref_type.ref_type);
  12996. }
  12997. else {
  12998. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12999. }
  13000. }
  13001. break;
  13002. }
  13003. case WASM_OP_BR_ON_CAST:
  13004. case WASM_OP_BR_ON_CAST_FAIL:
  13005. {
  13006. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13007. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13008. uint8 type_tmp, castflags;
  13009. uint32 depth;
  13010. int32 heap_type_dst;
  13011. bool src_nullable, dst_nullable;
  13012. CHECK_BUF(p, p_end, 1);
  13013. castflags = read_uint8(p);
  13014. #if WASM_ENABLE_FAST_INTERP != 0
  13015. /* Emit heap_type firstly */
  13016. emit_byte(loader_ctx, castflags);
  13017. #endif
  13018. p_org = p;
  13019. pb_read_leb_uint32(p, p_end, depth);
  13020. pb_read_leb_int32(p, p_end, heap_type);
  13021. #if WASM_ENABLE_FAST_INTERP != 0
  13022. /* Emit heap_type firstly */
  13023. emit_uint32(loader_ctx, (uint32)heap_type);
  13024. #endif
  13025. pb_read_leb_int32(p, p_end, heap_type_dst);
  13026. #if WASM_ENABLE_FAST_INTERP != 0
  13027. /* Emit heap_type firstly */
  13028. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13029. #endif
  13030. (void)depth;
  13031. /*
  13032. * castflags should be 0~3:
  13033. * 0: (non-null, non-null)
  13034. * 1: (null, non-null)
  13035. * 2: (non-null, null)
  13036. * 3: (null, null)
  13037. */
  13038. if (castflags > 3) {
  13039. set_error_buf(error_buf, error_buf_size,
  13040. "invalid castflags");
  13041. break;
  13042. }
  13043. src_nullable =
  13044. (castflags == 1) || (castflags == 3) ? true : false;
  13045. dst_nullable =
  13046. (castflags == 2) || (castflags == 3) ? true : false;
  13047. /* Pop and backup the stack top's ref type */
  13048. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13049. &ref_type_tmp, error_buf,
  13050. error_buf_size)) {
  13051. goto fail;
  13052. }
  13053. /* The reference type rt1 must be valid */
  13054. if (!init_ref_type(module, &ref_type1, src_nullable,
  13055. heap_type, error_buf,
  13056. error_buf_size)) {
  13057. goto fail;
  13058. }
  13059. /* The reference type rt2 must be valid. */
  13060. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13061. heap_type_dst, error_buf,
  13062. error_buf_size)) {
  13063. goto fail;
  13064. }
  13065. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13066. &ref_type2);
  13067. /* The reference type rt2 must match rt1. */
  13068. if (!wasm_reftype_is_subtype_of(
  13069. ref_type2.ref_type, &ref_type2,
  13070. ref_type1.ref_type, &ref_type1, module->types,
  13071. module->type_count)) {
  13072. set_error_buf(error_buf, error_buf_size,
  13073. "type mismatch");
  13074. goto fail;
  13075. }
  13076. p = p_org;
  13077. /* Push ref type casted for branch block check */
  13078. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13079. /* The reference type rt2 must match rt′. */
  13080. type_tmp = ref_type2.ref_type;
  13081. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13082. bh_memcpy_s(
  13083. &wasm_ref_type,
  13084. wasm_reftype_struct_size(&ref_type2),
  13085. &ref_type2,
  13086. wasm_reftype_struct_size(&ref_type2));
  13087. }
  13088. }
  13089. else {
  13090. /* The reference type rt′1 must match rt′. */
  13091. type_tmp = ref_type_diff.ref_type;
  13092. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13093. bh_memcpy_s(
  13094. &wasm_ref_type,
  13095. wasm_reftype_struct_size(&ref_type_diff),
  13096. &ref_type_diff,
  13097. wasm_reftype_struct_size(&ref_type_diff));
  13098. }
  13099. }
  13100. PUSH_REF(type_tmp);
  13101. if (!(frame_csp_tmp = check_branch_block(
  13102. loader_ctx, &p, p_end, opcode, error_buf,
  13103. error_buf_size))) {
  13104. goto fail;
  13105. }
  13106. /* Ignore heap_types */
  13107. skip_leb_uint32(p, p_end);
  13108. skip_leb_uint32(p, p_end);
  13109. /* Restore the original stack top's ref type */
  13110. POP_REF(type_tmp);
  13111. #if WASM_ENABLE_FAST_INTERP != 0
  13112. /* Erase the opnd offset emitted by POP_REF() */
  13113. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13114. #endif
  13115. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13116. type_tmp = ref_type_diff.ref_type;
  13117. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13118. bh_memcpy_s(
  13119. &wasm_ref_type,
  13120. wasm_reftype_struct_size(&ref_type_diff),
  13121. &ref_type_diff,
  13122. wasm_reftype_struct_size(&ref_type_diff));
  13123. }
  13124. }
  13125. else {
  13126. type_tmp = ref_type2.ref_type;
  13127. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13128. bh_memcpy_s(
  13129. &wasm_ref_type,
  13130. wasm_reftype_struct_size(&ref_type2),
  13131. &ref_type2,
  13132. wasm_reftype_struct_size(&ref_type2));
  13133. }
  13134. }
  13135. PUSH_REF(type_tmp);
  13136. #if WASM_ENABLE_FAST_INTERP != 0
  13137. /* Erase the opnd offset emitted by PUSH_REF() */
  13138. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13139. #endif
  13140. break;
  13141. }
  13142. case WASM_OP_ANY_CONVERT_EXTERN:
  13143. {
  13144. uint8 type;
  13145. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13146. &wasm_ref_type, error_buf,
  13147. error_buf_size)) {
  13148. goto fail;
  13149. }
  13150. if (!(type == REF_TYPE_EXTERNREF
  13151. || (type == REF_TYPE_HT_NON_NULLABLE
  13152. && wasm_ref_type.ref_ht_common.heap_type
  13153. == HEAP_TYPE_EXTERN)
  13154. || type == VALUE_TYPE_ANY)) {
  13155. set_error_buf(error_buf, error_buf_size,
  13156. "type mismatch");
  13157. goto fail;
  13158. }
  13159. if (type == REF_TYPE_EXTERNREF)
  13160. type = REF_TYPE_ANYREF;
  13161. else {
  13162. wasm_ref_type.ref_ht_common.heap_type =
  13163. HEAP_TYPE_ANY;
  13164. }
  13165. PUSH_REF(type);
  13166. break;
  13167. }
  13168. case WASM_OP_EXTERN_CONVERT_ANY:
  13169. {
  13170. uint8 type;
  13171. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13172. &wasm_ref_type, error_buf,
  13173. error_buf_size)) {
  13174. goto fail;
  13175. }
  13176. if (type == REF_TYPE_EXTERNREF
  13177. || ((type == REF_TYPE_HT_NULLABLE
  13178. || type == REF_TYPE_HT_NON_NULLABLE)
  13179. && wasm_ref_type.ref_ht_common.heap_type
  13180. == HEAP_TYPE_EXTERN)) {
  13181. set_error_buf(error_buf, error_buf_size,
  13182. "type mismatch");
  13183. goto fail;
  13184. }
  13185. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13186. /* push (ref null extern) */
  13187. type = REF_TYPE_EXTERNREF;
  13188. }
  13189. else {
  13190. /* push (ref extern) */
  13191. type = REF_TYPE_HT_NON_NULLABLE;
  13192. wasm_set_refheaptype_common(
  13193. &wasm_ref_type.ref_ht_common, false,
  13194. HEAP_TYPE_EXTERN);
  13195. }
  13196. PUSH_REF(type);
  13197. break;
  13198. }
  13199. #if WASM_ENABLE_STRINGREF != 0
  13200. case WASM_OP_STRING_NEW_UTF8:
  13201. case WASM_OP_STRING_NEW_WTF16:
  13202. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13203. case WASM_OP_STRING_NEW_WTF8:
  13204. {
  13205. uint32 memidx;
  13206. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13207. func->has_memory_operations = true;
  13208. #endif
  13209. pb_read_leb_uint32(p, p_end, memidx);
  13210. #if WASM_ENABLE_FAST_INTERP != 0
  13211. emit_uint32(loader_ctx, (uint32)memidx);
  13212. #endif
  13213. POP_I32();
  13214. POP_I32();
  13215. PUSH_REF(REF_TYPE_STRINGREF);
  13216. (void)memidx;
  13217. break;
  13218. }
  13219. case WASM_OP_STRING_CONST:
  13220. {
  13221. uint32 contents;
  13222. pb_read_leb_uint32(p, p_end, contents);
  13223. #if WASM_ENABLE_FAST_INTERP != 0
  13224. emit_uint32(loader_ctx, (uint32)contents);
  13225. #endif
  13226. PUSH_REF(REF_TYPE_STRINGREF);
  13227. (void)contents;
  13228. break;
  13229. }
  13230. case WASM_OP_STRING_MEASURE_UTF8:
  13231. case WASM_OP_STRING_MEASURE_WTF8:
  13232. case WASM_OP_STRING_MEASURE_WTF16:
  13233. {
  13234. POP_STRINGREF();
  13235. PUSH_I32();
  13236. break;
  13237. }
  13238. case WASM_OP_STRING_ENCODE_UTF8:
  13239. case WASM_OP_STRING_ENCODE_WTF16:
  13240. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13241. case WASM_OP_STRING_ENCODE_WTF8:
  13242. {
  13243. uint32 memidx;
  13244. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13245. func->has_memory_operations = true;
  13246. #endif
  13247. pb_read_leb_uint32(p, p_end, memidx);
  13248. #if WASM_ENABLE_FAST_INTERP != 0
  13249. emit_uint32(loader_ctx, (uint32)memidx);
  13250. #endif
  13251. POP_I32();
  13252. POP_STRINGREF();
  13253. PUSH_I32();
  13254. (void)memidx;
  13255. break;
  13256. }
  13257. case WASM_OP_STRING_CONCAT:
  13258. {
  13259. POP_STRINGREF();
  13260. POP_STRINGREF();
  13261. PUSH_REF(REF_TYPE_STRINGREF);
  13262. break;
  13263. }
  13264. case WASM_OP_STRING_EQ:
  13265. {
  13266. POP_STRINGREF();
  13267. POP_STRINGREF();
  13268. PUSH_I32();
  13269. break;
  13270. }
  13271. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13272. {
  13273. POP_STRINGREF();
  13274. PUSH_I32();
  13275. break;
  13276. }
  13277. case WASM_OP_STRING_AS_WTF8:
  13278. {
  13279. POP_STRINGREF();
  13280. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13281. break;
  13282. }
  13283. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13284. {
  13285. POP_I32();
  13286. POP_I32();
  13287. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13288. PUSH_I32();
  13289. break;
  13290. }
  13291. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13292. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13293. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13294. {
  13295. uint32 memidx;
  13296. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13297. func->has_memory_operations = true;
  13298. #endif
  13299. pb_read_leb_uint32(p, p_end, memidx);
  13300. #if WASM_ENABLE_FAST_INTERP != 0
  13301. emit_uint32(loader_ctx, (uint32)memidx);
  13302. #endif
  13303. POP_I32();
  13304. POP_I32();
  13305. POP_I32();
  13306. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13307. PUSH_I32();
  13308. PUSH_I32();
  13309. (void)memidx;
  13310. break;
  13311. }
  13312. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13313. {
  13314. POP_I32();
  13315. POP_I32();
  13316. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13317. PUSH_REF(REF_TYPE_STRINGREF);
  13318. break;
  13319. }
  13320. case WASM_OP_STRING_AS_WTF16:
  13321. {
  13322. POP_STRINGREF();
  13323. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13324. break;
  13325. }
  13326. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13327. {
  13328. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13329. PUSH_I32();
  13330. break;
  13331. }
  13332. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13333. {
  13334. POP_I32();
  13335. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13336. PUSH_I32();
  13337. break;
  13338. }
  13339. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13340. {
  13341. uint32 memidx;
  13342. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13343. func->has_memory_operations = true;
  13344. #endif
  13345. pb_read_leb_uint32(p, p_end, memidx);
  13346. #if WASM_ENABLE_FAST_INTERP != 0
  13347. emit_uint32(loader_ctx, (uint32)memidx);
  13348. #endif
  13349. POP_I32();
  13350. POP_I32();
  13351. POP_I32();
  13352. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13353. PUSH_I32();
  13354. (void)memidx;
  13355. break;
  13356. }
  13357. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13358. {
  13359. POP_I32();
  13360. POP_I32();
  13361. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13362. PUSH_REF(REF_TYPE_STRINGREF);
  13363. break;
  13364. }
  13365. case WASM_OP_STRING_AS_ITER:
  13366. {
  13367. POP_STRINGREF();
  13368. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13369. break;
  13370. }
  13371. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13372. {
  13373. POP_REF(REF_TYPE_STRINGVIEWITER);
  13374. PUSH_I32();
  13375. break;
  13376. }
  13377. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13378. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13379. {
  13380. POP_I32();
  13381. POP_REF(REF_TYPE_STRINGVIEWITER);
  13382. PUSH_I32();
  13383. break;
  13384. }
  13385. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13386. {
  13387. POP_I32();
  13388. POP_REF(REF_TYPE_STRINGVIEWITER);
  13389. PUSH_REF(REF_TYPE_STRINGREF);
  13390. break;
  13391. }
  13392. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13393. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13394. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13395. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13396. {
  13397. POP_I32();
  13398. POP_I32();
  13399. POP_REF(REF_TYPE_ARRAYREF);
  13400. PUSH_REF(REF_TYPE_STRINGREF);
  13401. break;
  13402. }
  13403. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13404. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13405. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13406. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13407. {
  13408. POP_I32();
  13409. POP_REF(REF_TYPE_ARRAYREF);
  13410. POP_STRINGREF();
  13411. PUSH_I32();
  13412. break;
  13413. }
  13414. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13415. default:
  13416. set_error_buf_v(error_buf, error_buf_size,
  13417. "%s %02x %02x", "unsupported opcode",
  13418. 0xfb, opcode1);
  13419. goto fail;
  13420. }
  13421. break;
  13422. }
  13423. #endif /* end of WASM_ENABLE_GC != 0 */
  13424. case WASM_OP_MISC_PREFIX:
  13425. {
  13426. uint32 opcode1;
  13427. pb_read_leb_uint32(p, p_end, opcode1);
  13428. #if WASM_ENABLE_FAST_INTERP != 0
  13429. emit_byte(loader_ctx, ((uint8)opcode1));
  13430. #endif
  13431. switch (opcode1) {
  13432. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13433. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13434. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13435. break;
  13436. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13437. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13438. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13439. break;
  13440. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13441. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13442. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13443. break;
  13444. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13445. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13446. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13447. break;
  13448. #if WASM_ENABLE_BULK_MEMORY != 0
  13449. case WASM_OP_MEMORY_INIT:
  13450. {
  13451. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13452. #if WASM_ENABLE_FAST_INTERP != 0
  13453. emit_uint32(loader_ctx, data_seg_idx);
  13454. #endif
  13455. if (module->import_memory_count == 0
  13456. && module->memory_count == 0)
  13457. goto fail_unknown_memory;
  13458. pb_read_leb_uint32(p, p_end, memidx);
  13459. check_memidx(module, memidx);
  13460. if (data_seg_idx >= module->data_seg_count) {
  13461. set_error_buf_v(error_buf, error_buf_size,
  13462. "unknown data segment %d",
  13463. data_seg_idx);
  13464. goto fail;
  13465. }
  13466. if (module->data_seg_count1 == 0)
  13467. goto fail_data_cnt_sec_require;
  13468. POP_I32();
  13469. POP_I32();
  13470. POP_MEM_OFFSET();
  13471. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13472. func->has_memory_operations = true;
  13473. #endif
  13474. #if WASM_ENABLE_WAMR_COMPILER != 0
  13475. module->is_bulk_memory_used = true;
  13476. #endif
  13477. break;
  13478. }
  13479. case WASM_OP_DATA_DROP:
  13480. {
  13481. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13482. #if WASM_ENABLE_FAST_INTERP != 0
  13483. emit_uint32(loader_ctx, data_seg_idx);
  13484. #endif
  13485. if (data_seg_idx >= module->data_seg_count) {
  13486. set_error_buf(error_buf, error_buf_size,
  13487. "unknown data segment");
  13488. goto fail;
  13489. }
  13490. if (module->data_seg_count1 == 0)
  13491. goto fail_data_cnt_sec_require;
  13492. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13493. func->has_memory_operations = true;
  13494. #endif
  13495. #if WASM_ENABLE_WAMR_COMPILER != 0
  13496. module->is_bulk_memory_used = true;
  13497. #endif
  13498. break;
  13499. }
  13500. case WASM_OP_MEMORY_COPY:
  13501. {
  13502. CHECK_BUF(p, p_end, sizeof(int16));
  13503. /* check both src and dst memory index */
  13504. pb_read_leb_uint32(p, p_end, memidx);
  13505. check_memidx(module, memidx);
  13506. pb_read_leb_uint32(p, p_end, memidx);
  13507. check_memidx(module, memidx);
  13508. if (module->import_memory_count == 0
  13509. && module->memory_count == 0)
  13510. goto fail_unknown_memory;
  13511. POP_MEM_OFFSET();
  13512. POP_MEM_OFFSET();
  13513. POP_MEM_OFFSET();
  13514. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13515. func->has_memory_operations = true;
  13516. #endif
  13517. #if WASM_ENABLE_WAMR_COMPILER != 0
  13518. module->is_bulk_memory_used = true;
  13519. #endif
  13520. break;
  13521. }
  13522. case WASM_OP_MEMORY_FILL:
  13523. {
  13524. pb_read_leb_uint32(p, p_end, memidx);
  13525. check_memidx(module, memidx);
  13526. if (module->import_memory_count == 0
  13527. && module->memory_count == 0) {
  13528. goto fail_unknown_memory;
  13529. }
  13530. POP_MEM_OFFSET();
  13531. POP_I32();
  13532. POP_MEM_OFFSET();
  13533. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13534. func->has_memory_operations = true;
  13535. #endif
  13536. #if WASM_ENABLE_WAMR_COMPILER != 0
  13537. module->is_bulk_memory_used = true;
  13538. #endif
  13539. break;
  13540. }
  13541. fail_unknown_memory:
  13542. set_error_buf(error_buf, error_buf_size,
  13543. "unknown memory 0");
  13544. goto fail;
  13545. fail_data_cnt_sec_require:
  13546. set_error_buf(error_buf, error_buf_size,
  13547. "data count section required");
  13548. goto fail;
  13549. #endif /* WASM_ENABLE_BULK_MEMORY */
  13550. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13551. case WASM_OP_TABLE_INIT:
  13552. {
  13553. uint8 seg_type = 0, tbl_type = 0;
  13554. #if WASM_ENABLE_GC != 0
  13555. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13556. #endif
  13557. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13558. pb_read_leb_uint32(p, p_end, table_idx);
  13559. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13560. #if WASM_ENABLE_GC != 0
  13561. (void **)&tbl_ref_type,
  13562. #else
  13563. NULL,
  13564. #endif
  13565. error_buf, error_buf_size))
  13566. goto fail;
  13567. if (!get_table_seg_elem_type(module, table_seg_idx,
  13568. &seg_type,
  13569. #if WASM_ENABLE_GC != 0
  13570. (void **)&seg_ref_type,
  13571. #else
  13572. NULL,
  13573. #endif
  13574. error_buf, error_buf_size))
  13575. goto fail;
  13576. #if WASM_ENABLE_GC == 0
  13577. if (seg_type != tbl_type) {
  13578. set_error_buf(error_buf, error_buf_size,
  13579. "type mismatch");
  13580. goto fail;
  13581. }
  13582. #else
  13583. if (!wasm_reftype_is_subtype_of(
  13584. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13585. module->types, module->type_count)) {
  13586. set_error_buf(error_buf, error_buf_size,
  13587. "type mismatch");
  13588. goto fail;
  13589. }
  13590. #endif
  13591. #if WASM_ENABLE_FAST_INTERP != 0
  13592. emit_uint32(loader_ctx, table_seg_idx);
  13593. emit_uint32(loader_ctx, table_idx);
  13594. #endif
  13595. POP_I32();
  13596. POP_I32();
  13597. #if WASM_ENABLE_MEMORY64 != 0
  13598. table_elem_idx_type = is_table_64bit(module, table_idx)
  13599. ? VALUE_TYPE_I64
  13600. : VALUE_TYPE_I32;
  13601. #endif
  13602. POP_TBL_ELEM_IDX();
  13603. #if WASM_ENABLE_WAMR_COMPILER != 0
  13604. module->is_ref_types_used = true;
  13605. #endif
  13606. break;
  13607. }
  13608. case WASM_OP_ELEM_DROP:
  13609. {
  13610. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13611. if (!get_table_seg_elem_type(module, table_seg_idx,
  13612. NULL, NULL, error_buf,
  13613. error_buf_size))
  13614. goto fail;
  13615. #if WASM_ENABLE_FAST_INTERP != 0
  13616. emit_uint32(loader_ctx, table_seg_idx);
  13617. #endif
  13618. #if WASM_ENABLE_WAMR_COMPILER != 0
  13619. module->is_ref_types_used = true;
  13620. #endif
  13621. break;
  13622. }
  13623. case WASM_OP_TABLE_COPY:
  13624. {
  13625. uint8 src_type, dst_type, src_tbl_idx_type,
  13626. dst_tbl_idx_type, min_tbl_idx_type;
  13627. #if WASM_ENABLE_GC != 0
  13628. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13629. #endif
  13630. uint32 src_tbl_idx, dst_tbl_idx;
  13631. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  13632. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13633. #if WASM_ENABLE_GC != 0
  13634. (void **)&dst_ref_type,
  13635. #else
  13636. NULL,
  13637. #endif
  13638. error_buf, error_buf_size))
  13639. goto fail;
  13640. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  13641. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13642. #if WASM_ENABLE_GC != 0
  13643. (void **)&src_ref_type,
  13644. #else
  13645. NULL,
  13646. #endif
  13647. error_buf, error_buf_size))
  13648. goto fail;
  13649. #if WASM_ENABLE_GC == 0
  13650. if (src_type != dst_type) {
  13651. set_error_buf(error_buf, error_buf_size,
  13652. "type mismatch");
  13653. goto fail;
  13654. }
  13655. #else
  13656. if (!wasm_reftype_is_subtype_of(
  13657. src_type, src_ref_type, dst_type, dst_ref_type,
  13658. module->types, module->type_count)) {
  13659. set_error_buf(error_buf, error_buf_size,
  13660. "type mismatch");
  13661. goto fail;
  13662. }
  13663. #endif
  13664. #if WASM_ENABLE_FAST_INTERP != 0
  13665. emit_uint32(loader_ctx, dst_tbl_idx);
  13666. emit_uint32(loader_ctx, src_tbl_idx);
  13667. #endif
  13668. #if WASM_ENABLE_MEMORY64 != 0
  13669. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  13670. ? VALUE_TYPE_I64
  13671. : VALUE_TYPE_I32;
  13672. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  13673. ? VALUE_TYPE_I64
  13674. : VALUE_TYPE_I32;
  13675. min_tbl_idx_type =
  13676. (src_tbl_idx_type == VALUE_TYPE_I32
  13677. || dst_tbl_idx_type == VALUE_TYPE_I32)
  13678. ? VALUE_TYPE_I32
  13679. : VALUE_TYPE_I64;
  13680. #else
  13681. src_tbl_idx_type = VALUE_TYPE_I32;
  13682. dst_tbl_idx_type = VALUE_TYPE_I32;
  13683. min_tbl_idx_type = VALUE_TYPE_I32;
  13684. #endif
  13685. table_elem_idx_type = min_tbl_idx_type;
  13686. POP_TBL_ELEM_IDX();
  13687. table_elem_idx_type = src_tbl_idx_type;
  13688. POP_TBL_ELEM_IDX();
  13689. table_elem_idx_type = dst_tbl_idx_type;
  13690. POP_TBL_ELEM_IDX();
  13691. #if WASM_ENABLE_WAMR_COMPILER != 0
  13692. module->is_ref_types_used = true;
  13693. #endif
  13694. break;
  13695. }
  13696. case WASM_OP_TABLE_SIZE:
  13697. {
  13698. pb_read_leb_uint32(p, p_end, table_idx);
  13699. /* TODO: shall we create a new function to check
  13700. table idx instead of using below function? */
  13701. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13702. error_buf, error_buf_size))
  13703. goto fail;
  13704. #if WASM_ENABLE_FAST_INTERP != 0
  13705. emit_uint32(loader_ctx, table_idx);
  13706. #endif
  13707. #if WASM_ENABLE_MEMORY64 != 0
  13708. table_elem_idx_type = is_table_64bit(module, table_idx)
  13709. ? VALUE_TYPE_I64
  13710. : VALUE_TYPE_I32;
  13711. #endif
  13712. PUSH_TBL_ELEM_IDX();
  13713. #if WASM_ENABLE_WAMR_COMPILER != 0
  13714. module->is_ref_types_used = true;
  13715. #endif
  13716. break;
  13717. }
  13718. case WASM_OP_TABLE_GROW:
  13719. case WASM_OP_TABLE_FILL:
  13720. {
  13721. uint8 decl_type;
  13722. #if WASM_ENABLE_GC != 0
  13723. WASMRefType *ref_type = NULL;
  13724. #endif
  13725. pb_read_leb_uint32(p, p_end, table_idx);
  13726. if (!get_table_elem_type(module, table_idx, &decl_type,
  13727. #if WASM_ENABLE_GC != 0
  13728. (void **)&ref_type,
  13729. #else
  13730. NULL,
  13731. #endif
  13732. error_buf, error_buf_size))
  13733. goto fail;
  13734. #if WASM_ENABLE_GC != 0
  13735. if (wasm_is_type_multi_byte_type(decl_type)) {
  13736. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13737. ref_type,
  13738. wasm_reftype_struct_size(ref_type));
  13739. }
  13740. #endif
  13741. if (opcode1 == WASM_OP_TABLE_GROW) {
  13742. if (table_idx < module->import_table_count) {
  13743. module->import_tables[table_idx]
  13744. .u.table.table_type.possible_grow = true;
  13745. }
  13746. else {
  13747. module
  13748. ->tables[table_idx
  13749. - module->import_table_count]
  13750. .table_type.possible_grow = true;
  13751. }
  13752. }
  13753. #if WASM_ENABLE_FAST_INTERP != 0
  13754. emit_uint32(loader_ctx, table_idx);
  13755. #endif
  13756. #if WASM_ENABLE_MEMORY64 != 0
  13757. table_elem_idx_type = is_table_64bit(module, table_idx)
  13758. ? VALUE_TYPE_I64
  13759. : VALUE_TYPE_I32;
  13760. #endif
  13761. POP_TBL_ELEM_IDX();
  13762. #if WASM_ENABLE_FAST_INTERP != 0
  13763. POP_OFFSET_TYPE(decl_type);
  13764. #endif
  13765. POP_TYPE(decl_type);
  13766. if (opcode1 == WASM_OP_TABLE_GROW)
  13767. PUSH_TBL_ELEM_IDX();
  13768. else
  13769. POP_TBL_ELEM_IDX();
  13770. #if WASM_ENABLE_WAMR_COMPILER != 0
  13771. module->is_ref_types_used = true;
  13772. #endif
  13773. break;
  13774. }
  13775. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13776. default:
  13777. set_error_buf_v(error_buf, error_buf_size,
  13778. "%s %02x %02x", "unsupported opcode",
  13779. 0xfc, opcode1);
  13780. goto fail;
  13781. }
  13782. break;
  13783. }
  13784. #if WASM_ENABLE_SIMD != 0
  13785. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13786. case WASM_OP_SIMD_PREFIX:
  13787. {
  13788. uint32 opcode1;
  13789. #if WASM_ENABLE_WAMR_COMPILER != 0
  13790. /* Mark the SIMD instruction is used in this module */
  13791. module->is_simd_used = true;
  13792. #endif
  13793. pb_read_leb_uint32(p, p_end, opcode1);
  13794. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13795. */
  13796. switch (opcode1) {
  13797. /* memory instruction */
  13798. case SIMD_v128_load:
  13799. case SIMD_v128_load8x8_s:
  13800. case SIMD_v128_load8x8_u:
  13801. case SIMD_v128_load16x4_s:
  13802. case SIMD_v128_load16x4_u:
  13803. case SIMD_v128_load32x2_s:
  13804. case SIMD_v128_load32x2_u:
  13805. case SIMD_v128_load8_splat:
  13806. case SIMD_v128_load16_splat:
  13807. case SIMD_v128_load32_splat:
  13808. case SIMD_v128_load64_splat:
  13809. {
  13810. CHECK_MEMORY();
  13811. pb_read_leb_uint32(p, p_end, align); /* align */
  13812. if (!check_simd_memory_access_align(
  13813. opcode1, align, error_buf, error_buf_size)) {
  13814. goto fail;
  13815. }
  13816. pb_read_leb_mem_offset(p, p_end,
  13817. mem_offset); /* offset */
  13818. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13819. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13820. func->has_memory_operations = true;
  13821. #endif
  13822. break;
  13823. }
  13824. case SIMD_v128_store:
  13825. {
  13826. CHECK_MEMORY();
  13827. pb_read_leb_uint32(p, p_end, align); /* align */
  13828. if (!check_simd_memory_access_align(
  13829. opcode1, align, error_buf, error_buf_size)) {
  13830. goto fail;
  13831. }
  13832. pb_read_leb_mem_offset(p, p_end,
  13833. mem_offset); /* offset */
  13834. POP_V128();
  13835. POP_MEM_OFFSET();
  13836. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13837. func->has_memory_operations = true;
  13838. #endif
  13839. break;
  13840. }
  13841. /* basic operation */
  13842. case SIMD_v128_const:
  13843. {
  13844. CHECK_BUF1(p, p_end, 16);
  13845. p += 16;
  13846. PUSH_V128();
  13847. break;
  13848. }
  13849. case SIMD_v8x16_shuffle:
  13850. {
  13851. V128 mask;
  13852. CHECK_BUF1(p, p_end, 16);
  13853. mask = read_i8x16(p, error_buf, error_buf_size);
  13854. p += 16;
  13855. if (!check_simd_shuffle_mask(mask, error_buf,
  13856. error_buf_size)) {
  13857. goto fail;
  13858. }
  13859. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13860. break;
  13861. }
  13862. case SIMD_v8x16_swizzle:
  13863. {
  13864. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13865. break;
  13866. }
  13867. /* splat operation */
  13868. case SIMD_i8x16_splat:
  13869. case SIMD_i16x8_splat:
  13870. case SIMD_i32x4_splat:
  13871. case SIMD_i64x2_splat:
  13872. case SIMD_f32x4_splat:
  13873. case SIMD_f64x2_splat:
  13874. {
  13875. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13876. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13877. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13878. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13879. VALUE_TYPE_V128);
  13880. break;
  13881. }
  13882. /* lane operation */
  13883. case SIMD_i8x16_extract_lane_s:
  13884. case SIMD_i8x16_extract_lane_u:
  13885. case SIMD_i8x16_replace_lane:
  13886. case SIMD_i16x8_extract_lane_s:
  13887. case SIMD_i16x8_extract_lane_u:
  13888. case SIMD_i16x8_replace_lane:
  13889. case SIMD_i32x4_extract_lane:
  13890. case SIMD_i32x4_replace_lane:
  13891. case SIMD_i64x2_extract_lane:
  13892. case SIMD_i64x2_replace_lane:
  13893. case SIMD_f32x4_extract_lane:
  13894. case SIMD_f32x4_replace_lane:
  13895. case SIMD_f64x2_extract_lane:
  13896. case SIMD_f64x2_replace_lane:
  13897. {
  13898. uint8 lane;
  13899. /* clang-format off */
  13900. uint8 replace[] = {
  13901. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13902. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13903. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13904. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13905. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13906. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13907. };
  13908. uint8 push_type[] = {
  13909. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13910. VALUE_TYPE_V128,
  13911. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13912. VALUE_TYPE_V128,
  13913. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13914. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13915. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13916. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13917. };
  13918. /* clang-format on */
  13919. CHECK_BUF(p, p_end, 1);
  13920. lane = read_uint8(p);
  13921. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13922. error_buf_size)) {
  13923. goto fail;
  13924. }
  13925. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13926. if (!(wasm_loader_pop_frame_ref(
  13927. loader_ctx,
  13928. replace[opcode1
  13929. - SIMD_i8x16_extract_lane_s],
  13930. error_buf, error_buf_size)))
  13931. goto fail;
  13932. }
  13933. POP_AND_PUSH(
  13934. VALUE_TYPE_V128,
  13935. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13936. break;
  13937. }
  13938. /* i8x16 compare operation */
  13939. case SIMD_i8x16_eq:
  13940. case SIMD_i8x16_ne:
  13941. case SIMD_i8x16_lt_s:
  13942. case SIMD_i8x16_lt_u:
  13943. case SIMD_i8x16_gt_s:
  13944. case SIMD_i8x16_gt_u:
  13945. case SIMD_i8x16_le_s:
  13946. case SIMD_i8x16_le_u:
  13947. case SIMD_i8x16_ge_s:
  13948. case SIMD_i8x16_ge_u:
  13949. /* i16x8 compare operation */
  13950. case SIMD_i16x8_eq:
  13951. case SIMD_i16x8_ne:
  13952. case SIMD_i16x8_lt_s:
  13953. case SIMD_i16x8_lt_u:
  13954. case SIMD_i16x8_gt_s:
  13955. case SIMD_i16x8_gt_u:
  13956. case SIMD_i16x8_le_s:
  13957. case SIMD_i16x8_le_u:
  13958. case SIMD_i16x8_ge_s:
  13959. case SIMD_i16x8_ge_u:
  13960. /* i32x4 compare operation */
  13961. case SIMD_i32x4_eq:
  13962. case SIMD_i32x4_ne:
  13963. case SIMD_i32x4_lt_s:
  13964. case SIMD_i32x4_lt_u:
  13965. case SIMD_i32x4_gt_s:
  13966. case SIMD_i32x4_gt_u:
  13967. case SIMD_i32x4_le_s:
  13968. case SIMD_i32x4_le_u:
  13969. case SIMD_i32x4_ge_s:
  13970. case SIMD_i32x4_ge_u:
  13971. /* f32x4 compare operation */
  13972. case SIMD_f32x4_eq:
  13973. case SIMD_f32x4_ne:
  13974. case SIMD_f32x4_lt:
  13975. case SIMD_f32x4_gt:
  13976. case SIMD_f32x4_le:
  13977. case SIMD_f32x4_ge:
  13978. /* f64x2 compare operation */
  13979. case SIMD_f64x2_eq:
  13980. case SIMD_f64x2_ne:
  13981. case SIMD_f64x2_lt:
  13982. case SIMD_f64x2_gt:
  13983. case SIMD_f64x2_le:
  13984. case SIMD_f64x2_ge:
  13985. {
  13986. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13987. break;
  13988. }
  13989. /* v128 operation */
  13990. case SIMD_v128_not:
  13991. {
  13992. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13993. break;
  13994. }
  13995. case SIMD_v128_and:
  13996. case SIMD_v128_andnot:
  13997. case SIMD_v128_or:
  13998. case SIMD_v128_xor:
  13999. {
  14000. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14001. break;
  14002. }
  14003. case SIMD_v128_bitselect:
  14004. {
  14005. POP_V128();
  14006. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14007. break;
  14008. }
  14009. case SIMD_v128_any_true:
  14010. {
  14011. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14012. break;
  14013. }
  14014. /* Load Lane Operation */
  14015. case SIMD_v128_load8_lane:
  14016. case SIMD_v128_load16_lane:
  14017. case SIMD_v128_load32_lane:
  14018. case SIMD_v128_load64_lane:
  14019. case SIMD_v128_store8_lane:
  14020. case SIMD_v128_store16_lane:
  14021. case SIMD_v128_store32_lane:
  14022. case SIMD_v128_store64_lane:
  14023. {
  14024. uint8 lane;
  14025. CHECK_MEMORY();
  14026. pb_read_leb_uint32(p, p_end, align); /* align */
  14027. if (!check_simd_memory_access_align(
  14028. opcode1, align, error_buf, error_buf_size)) {
  14029. goto fail;
  14030. }
  14031. pb_read_leb_mem_offset(p, p_end,
  14032. mem_offset); /* offset */
  14033. CHECK_BUF(p, p_end, 1);
  14034. lane = read_uint8(p);
  14035. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14036. error_buf_size)) {
  14037. goto fail;
  14038. }
  14039. POP_V128();
  14040. POP_MEM_OFFSET();
  14041. if (opcode1 < SIMD_v128_store8_lane) {
  14042. PUSH_V128();
  14043. }
  14044. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14045. func->has_memory_operations = true;
  14046. #endif
  14047. break;
  14048. }
  14049. case SIMD_v128_load32_zero:
  14050. case SIMD_v128_load64_zero:
  14051. {
  14052. CHECK_MEMORY();
  14053. pb_read_leb_uint32(p, p_end, align); /* align */
  14054. if (!check_simd_memory_access_align(
  14055. opcode1, align, error_buf, error_buf_size)) {
  14056. goto fail;
  14057. }
  14058. pb_read_leb_mem_offset(p, p_end,
  14059. mem_offset); /* offset */
  14060. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14061. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14062. func->has_memory_operations = true;
  14063. #endif
  14064. break;
  14065. }
  14066. /* Float conversion */
  14067. case SIMD_f32x4_demote_f64x2_zero:
  14068. case SIMD_f64x2_promote_low_f32x4_zero:
  14069. {
  14070. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14071. break;
  14072. }
  14073. /* i8x16 Operation */
  14074. case SIMD_i8x16_abs:
  14075. case SIMD_i8x16_neg:
  14076. case SIMD_i8x16_popcnt:
  14077. {
  14078. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14079. break;
  14080. }
  14081. case SIMD_i8x16_all_true:
  14082. case SIMD_i8x16_bitmask:
  14083. {
  14084. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14085. break;
  14086. }
  14087. case SIMD_i8x16_narrow_i16x8_s:
  14088. case SIMD_i8x16_narrow_i16x8_u:
  14089. {
  14090. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14091. break;
  14092. }
  14093. case SIMD_f32x4_ceil:
  14094. case SIMD_f32x4_floor:
  14095. case SIMD_f32x4_trunc:
  14096. case SIMD_f32x4_nearest:
  14097. {
  14098. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14099. break;
  14100. }
  14101. case SIMD_i8x16_shl:
  14102. case SIMD_i8x16_shr_s:
  14103. case SIMD_i8x16_shr_u:
  14104. {
  14105. POP_I32();
  14106. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14107. break;
  14108. }
  14109. case SIMD_i8x16_add:
  14110. case SIMD_i8x16_add_sat_s:
  14111. case SIMD_i8x16_add_sat_u:
  14112. case SIMD_i8x16_sub:
  14113. case SIMD_i8x16_sub_sat_s:
  14114. case SIMD_i8x16_sub_sat_u:
  14115. {
  14116. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14117. break;
  14118. }
  14119. case SIMD_f64x2_ceil:
  14120. case SIMD_f64x2_floor:
  14121. {
  14122. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14123. break;
  14124. }
  14125. case SIMD_i8x16_min_s:
  14126. case SIMD_i8x16_min_u:
  14127. case SIMD_i8x16_max_s:
  14128. case SIMD_i8x16_max_u:
  14129. {
  14130. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14131. break;
  14132. }
  14133. case SIMD_f64x2_trunc:
  14134. {
  14135. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14136. break;
  14137. }
  14138. case SIMD_i8x16_avgr_u:
  14139. {
  14140. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14141. break;
  14142. }
  14143. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14144. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14145. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14146. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14147. /* i16x8 operation */
  14148. case SIMD_i16x8_abs:
  14149. case SIMD_i16x8_neg:
  14150. {
  14151. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14152. break;
  14153. }
  14154. case SIMD_i16x8_q15mulr_sat_s:
  14155. {
  14156. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14157. break;
  14158. }
  14159. case SIMD_i16x8_all_true:
  14160. case SIMD_i16x8_bitmask:
  14161. {
  14162. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14163. break;
  14164. }
  14165. case SIMD_i16x8_narrow_i32x4_s:
  14166. case SIMD_i16x8_narrow_i32x4_u:
  14167. {
  14168. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14169. break;
  14170. }
  14171. case SIMD_i16x8_extend_low_i8x16_s:
  14172. case SIMD_i16x8_extend_high_i8x16_s:
  14173. case SIMD_i16x8_extend_low_i8x16_u:
  14174. case SIMD_i16x8_extend_high_i8x16_u:
  14175. {
  14176. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14177. break;
  14178. }
  14179. case SIMD_i16x8_shl:
  14180. case SIMD_i16x8_shr_s:
  14181. case SIMD_i16x8_shr_u:
  14182. {
  14183. POP_I32();
  14184. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14185. break;
  14186. }
  14187. case SIMD_i16x8_add:
  14188. case SIMD_i16x8_add_sat_s:
  14189. case SIMD_i16x8_add_sat_u:
  14190. case SIMD_i16x8_sub:
  14191. case SIMD_i16x8_sub_sat_s:
  14192. case SIMD_i16x8_sub_sat_u:
  14193. {
  14194. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14195. break;
  14196. }
  14197. case SIMD_f64x2_nearest:
  14198. {
  14199. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14200. break;
  14201. }
  14202. case SIMD_i16x8_mul:
  14203. case SIMD_i16x8_min_s:
  14204. case SIMD_i16x8_min_u:
  14205. case SIMD_i16x8_max_s:
  14206. case SIMD_i16x8_max_u:
  14207. case SIMD_i16x8_avgr_u:
  14208. case SIMD_i16x8_extmul_low_i8x16_s:
  14209. case SIMD_i16x8_extmul_high_i8x16_s:
  14210. case SIMD_i16x8_extmul_low_i8x16_u:
  14211. case SIMD_i16x8_extmul_high_i8x16_u:
  14212. {
  14213. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14214. break;
  14215. }
  14216. /* i32x4 operation */
  14217. case SIMD_i32x4_abs:
  14218. case SIMD_i32x4_neg:
  14219. {
  14220. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14221. break;
  14222. }
  14223. case SIMD_i32x4_all_true:
  14224. case SIMD_i32x4_bitmask:
  14225. {
  14226. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14227. break;
  14228. }
  14229. case SIMD_i32x4_extend_low_i16x8_s:
  14230. case SIMD_i32x4_extend_high_i16x8_s:
  14231. case SIMD_i32x4_extend_low_i16x8_u:
  14232. case SIMD_i32x4_extend_high_i16x8_u:
  14233. {
  14234. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14235. break;
  14236. }
  14237. case SIMD_i32x4_shl:
  14238. case SIMD_i32x4_shr_s:
  14239. case SIMD_i32x4_shr_u:
  14240. {
  14241. POP_I32();
  14242. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14243. break;
  14244. }
  14245. case SIMD_i32x4_add:
  14246. case SIMD_i32x4_sub:
  14247. case SIMD_i32x4_mul:
  14248. case SIMD_i32x4_min_s:
  14249. case SIMD_i32x4_min_u:
  14250. case SIMD_i32x4_max_s:
  14251. case SIMD_i32x4_max_u:
  14252. case SIMD_i32x4_dot_i16x8_s:
  14253. case SIMD_i32x4_extmul_low_i16x8_s:
  14254. case SIMD_i32x4_extmul_high_i16x8_s:
  14255. case SIMD_i32x4_extmul_low_i16x8_u:
  14256. case SIMD_i32x4_extmul_high_i16x8_u:
  14257. {
  14258. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14259. break;
  14260. }
  14261. /* i64x2 operation */
  14262. case SIMD_i64x2_abs:
  14263. case SIMD_i64x2_neg:
  14264. {
  14265. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14266. break;
  14267. }
  14268. case SIMD_i64x2_all_true:
  14269. case SIMD_i64x2_bitmask:
  14270. {
  14271. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14272. break;
  14273. }
  14274. case SIMD_i64x2_extend_low_i32x4_s:
  14275. case SIMD_i64x2_extend_high_i32x4_s:
  14276. case SIMD_i64x2_extend_low_i32x4_u:
  14277. case SIMD_i64x2_extend_high_i32x4_u:
  14278. {
  14279. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14280. break;
  14281. }
  14282. case SIMD_i64x2_shl:
  14283. case SIMD_i64x2_shr_s:
  14284. case SIMD_i64x2_shr_u:
  14285. {
  14286. POP_I32();
  14287. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14288. break;
  14289. }
  14290. case SIMD_i64x2_add:
  14291. case SIMD_i64x2_sub:
  14292. case SIMD_i64x2_mul:
  14293. case SIMD_i64x2_eq:
  14294. case SIMD_i64x2_ne:
  14295. case SIMD_i64x2_lt_s:
  14296. case SIMD_i64x2_gt_s:
  14297. case SIMD_i64x2_le_s:
  14298. case SIMD_i64x2_ge_s:
  14299. case SIMD_i64x2_extmul_low_i32x4_s:
  14300. case SIMD_i64x2_extmul_high_i32x4_s:
  14301. case SIMD_i64x2_extmul_low_i32x4_u:
  14302. case SIMD_i64x2_extmul_high_i32x4_u:
  14303. {
  14304. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14305. break;
  14306. }
  14307. /* f32x4 operation */
  14308. case SIMD_f32x4_abs:
  14309. case SIMD_f32x4_neg:
  14310. case SIMD_f32x4_sqrt:
  14311. {
  14312. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14313. break;
  14314. }
  14315. case SIMD_f32x4_add:
  14316. case SIMD_f32x4_sub:
  14317. case SIMD_f32x4_mul:
  14318. case SIMD_f32x4_div:
  14319. case SIMD_f32x4_min:
  14320. case SIMD_f32x4_max:
  14321. case SIMD_f32x4_pmin:
  14322. case SIMD_f32x4_pmax:
  14323. {
  14324. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14325. break;
  14326. }
  14327. /* f64x2 operation */
  14328. case SIMD_f64x2_abs:
  14329. case SIMD_f64x2_neg:
  14330. case SIMD_f64x2_sqrt:
  14331. {
  14332. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14333. break;
  14334. }
  14335. case SIMD_f64x2_add:
  14336. case SIMD_f64x2_sub:
  14337. case SIMD_f64x2_mul:
  14338. case SIMD_f64x2_div:
  14339. case SIMD_f64x2_min:
  14340. case SIMD_f64x2_max:
  14341. case SIMD_f64x2_pmin:
  14342. case SIMD_f64x2_pmax:
  14343. {
  14344. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14345. break;
  14346. }
  14347. case SIMD_i32x4_trunc_sat_f32x4_s:
  14348. case SIMD_i32x4_trunc_sat_f32x4_u:
  14349. case SIMD_f32x4_convert_i32x4_s:
  14350. case SIMD_f32x4_convert_i32x4_u:
  14351. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14352. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14353. case SIMD_f64x2_convert_low_i32x4_s:
  14354. case SIMD_f64x2_convert_low_i32x4_u:
  14355. {
  14356. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14357. break;
  14358. }
  14359. default:
  14360. {
  14361. if (error_buf != NULL) {
  14362. snprintf(error_buf, error_buf_size,
  14363. "WASM module load failed: "
  14364. "invalid opcode 0xfd %02x.",
  14365. opcode1);
  14366. }
  14367. goto fail;
  14368. }
  14369. }
  14370. break;
  14371. }
  14372. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14373. #endif /* end of WASM_ENABLE_SIMD */
  14374. #if WASM_ENABLE_SHARED_MEMORY != 0
  14375. case WASM_OP_ATOMIC_PREFIX:
  14376. {
  14377. uint32 opcode1;
  14378. pb_read_leb_uint32(p, p_end, opcode1);
  14379. #if WASM_ENABLE_FAST_INTERP != 0
  14380. emit_byte(loader_ctx, opcode1);
  14381. #endif
  14382. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14383. CHECK_MEMORY();
  14384. pb_read_leb_uint32(p, p_end, align); /* align */
  14385. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14386. if (!check_memory_align_equal(opcode1, align, error_buf,
  14387. error_buf_size)) {
  14388. goto fail;
  14389. }
  14390. #if WASM_ENABLE_FAST_INTERP != 0
  14391. emit_uint32(loader_ctx, mem_offset);
  14392. #endif
  14393. }
  14394. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14395. func->has_memory_operations = true;
  14396. #endif
  14397. switch (opcode1) {
  14398. case WASM_OP_ATOMIC_NOTIFY:
  14399. POP_I32();
  14400. POP_MEM_OFFSET();
  14401. PUSH_I32();
  14402. break;
  14403. case WASM_OP_ATOMIC_WAIT32:
  14404. POP_I64();
  14405. POP_I32();
  14406. POP_MEM_OFFSET();
  14407. PUSH_I32();
  14408. break;
  14409. case WASM_OP_ATOMIC_WAIT64:
  14410. POP_I64();
  14411. POP_I64();
  14412. POP_MEM_OFFSET();
  14413. PUSH_I32();
  14414. break;
  14415. case WASM_OP_ATOMIC_FENCE:
  14416. /* reserved byte 0x00 */
  14417. if (*p++ != 0x00) {
  14418. set_error_buf(error_buf, error_buf_size,
  14419. "zero byte expected");
  14420. goto fail;
  14421. }
  14422. break;
  14423. case WASM_OP_ATOMIC_I32_LOAD:
  14424. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14425. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14426. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14427. break;
  14428. case WASM_OP_ATOMIC_I32_STORE:
  14429. case WASM_OP_ATOMIC_I32_STORE8:
  14430. case WASM_OP_ATOMIC_I32_STORE16:
  14431. POP_I32();
  14432. POP_MEM_OFFSET();
  14433. break;
  14434. case WASM_OP_ATOMIC_I64_LOAD:
  14435. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14436. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14437. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14438. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14439. break;
  14440. case WASM_OP_ATOMIC_I64_STORE:
  14441. case WASM_OP_ATOMIC_I64_STORE8:
  14442. case WASM_OP_ATOMIC_I64_STORE16:
  14443. case WASM_OP_ATOMIC_I64_STORE32:
  14444. POP_I64();
  14445. POP_MEM_OFFSET();
  14446. break;
  14447. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14448. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14449. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14450. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14451. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14452. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14453. case WASM_OP_ATOMIC_RMW_I32_AND:
  14454. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14455. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14456. case WASM_OP_ATOMIC_RMW_I32_OR:
  14457. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14458. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14459. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14460. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14461. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14462. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14463. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14464. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14465. POP_I32();
  14466. POP_MEM_OFFSET();
  14467. PUSH_I32();
  14468. break;
  14469. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14470. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14471. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14472. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14473. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14474. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14475. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14476. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14477. case WASM_OP_ATOMIC_RMW_I64_AND:
  14478. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14479. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14480. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14481. case WASM_OP_ATOMIC_RMW_I64_OR:
  14482. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14483. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14484. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14485. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14486. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14487. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14488. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14489. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14490. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14491. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14492. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14493. POP_I64();
  14494. POP_MEM_OFFSET();
  14495. PUSH_I64();
  14496. break;
  14497. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14498. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14499. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14500. POP_I32();
  14501. POP_I32();
  14502. POP_MEM_OFFSET();
  14503. PUSH_I32();
  14504. break;
  14505. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14506. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14507. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14508. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14509. POP_I64();
  14510. POP_I64();
  14511. POP_MEM_OFFSET();
  14512. PUSH_I64();
  14513. break;
  14514. default:
  14515. set_error_buf_v(error_buf, error_buf_size,
  14516. "%s %02x %02x", "unsupported opcode",
  14517. 0xfe, opcode1);
  14518. goto fail;
  14519. }
  14520. break;
  14521. }
  14522. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14523. default:
  14524. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14525. "unsupported opcode", opcode);
  14526. goto fail;
  14527. }
  14528. #if WASM_ENABLE_FAST_INTERP != 0
  14529. last_op = opcode;
  14530. #endif
  14531. }
  14532. if (loader_ctx->csp_num > 0) {
  14533. /* unmatched end opcodes result from unbalanced control flow structures,
  14534. * for example, br_table with inconsistent target count (1 declared, 2
  14535. * given), or simply superfluous end opcodes */
  14536. set_error_buf(
  14537. error_buf, error_buf_size,
  14538. "unexpected end opcodes from unbalanced control flow structures");
  14539. goto fail;
  14540. }
  14541. #if WASM_ENABLE_FAST_INTERP != 0
  14542. if (loader_ctx->p_code_compiled == NULL)
  14543. goto re_scan;
  14544. func->const_cell_num =
  14545. loader_ctx->i64_const_num * 2 + loader_ctx->i32_const_num;
  14546. if (func->const_cell_num > 0) {
  14547. if (!(func->consts =
  14548. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  14549. error_buf, error_buf_size)))
  14550. goto fail;
  14551. if (loader_ctx->i64_const_num > 0) {
  14552. bh_memcpy_s(func->consts,
  14553. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  14554. loader_ctx->i64_consts,
  14555. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  14556. }
  14557. if (loader_ctx->i32_const_num > 0) {
  14558. bh_memcpy_s(func->consts
  14559. + sizeof(int64) * loader_ctx->i64_const_num,
  14560. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  14561. loader_ctx->i32_consts,
  14562. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  14563. }
  14564. }
  14565. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14566. - loader_ctx->start_dynamic_offset + 1;
  14567. #else
  14568. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14569. #endif
  14570. func->max_block_num = loader_ctx->max_csp_num;
  14571. return_value = true;
  14572. fail:
  14573. wasm_loader_ctx_destroy(loader_ctx);
  14574. (void)table_idx;
  14575. (void)table_seg_idx;
  14576. (void)data_seg_idx;
  14577. (void)i64_const;
  14578. (void)local_offset;
  14579. (void)p_org;
  14580. (void)mem_offset;
  14581. (void)align;
  14582. return return_value;
  14583. }