wasm_loader.c 566 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509
  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_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.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. static void
  36. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  37. {
  38. if (error_buf != NULL) {
  39. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  40. string);
  41. }
  42. }
  43. static void
  44. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  45. {
  46. va_list args;
  47. char buf[128];
  48. if (error_buf != NULL) {
  49. va_start(args, format);
  50. vsnprintf(buf, sizeof(buf), format, args);
  51. va_end(args);
  52. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  53. }
  54. }
  55. static bool
  56. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  57. char *error_buf, uint32 error_buf_size)
  58. {
  59. if ((uintptr_t)buf + length < (uintptr_t)buf
  60. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  61. set_error_buf(error_buf, error_buf_size,
  62. "unexpected end of section or function");
  63. return false;
  64. }
  65. return true;
  66. }
  67. static bool
  68. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  69. char *error_buf, uint32 error_buf_size)
  70. {
  71. if ((uintptr_t)buf + length < (uintptr_t)buf
  72. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  73. set_error_buf(error_buf, error_buf_size, "unexpected end");
  74. return false;
  75. }
  76. return true;
  77. }
  78. #define CHECK_BUF(buf, buf_end, length) \
  79. do { \
  80. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  81. goto fail; \
  82. } \
  83. } while (0)
  84. #define CHECK_BUF1(buf, buf_end, length) \
  85. do { \
  86. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  87. goto fail; \
  88. } \
  89. } while (0)
  90. #define skip_leb(p) while (*p++ & 0x80)
  91. #define skip_leb_int64(p, p_end) skip_leb(p)
  92. #define skip_leb_uint32(p, p_end) skip_leb(p)
  93. #define skip_leb_int32(p, p_end) skip_leb(p)
  94. static bool
  95. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  96. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  97. {
  98. const uint8 *buf = *p_buf;
  99. uint64 result = 0;
  100. uint32 shift = 0;
  101. uint32 offset = 0, bcnt = 0;
  102. uint64 byte;
  103. while (true) {
  104. /* uN or SN must not exceed ceil(N/7) bytes */
  105. if (bcnt + 1 > (maxbits + 6) / 7) {
  106. set_error_buf(error_buf, error_buf_size,
  107. "integer representation too long");
  108. return false;
  109. }
  110. CHECK_BUF(buf, buf_end, offset + 1);
  111. byte = buf[offset];
  112. offset += 1;
  113. result |= ((byte & 0x7f) << shift);
  114. shift += 7;
  115. bcnt += 1;
  116. if ((byte & 0x80) == 0) {
  117. break;
  118. }
  119. }
  120. if (!sign && maxbits == 32 && shift >= maxbits) {
  121. /* The top bits set represent values > 32 bits */
  122. if (((uint8)byte) & 0xf0)
  123. goto fail_integer_too_large;
  124. }
  125. else if (sign && maxbits == 32) {
  126. if (shift < maxbits) {
  127. /* Sign extend, second highest bit is the sign bit */
  128. if ((uint8)byte & 0x40)
  129. result |= (~((uint64)0)) << shift;
  130. }
  131. else {
  132. /* The top bits should be a sign-extension of the sign bit */
  133. bool sign_bit_set = ((uint8)byte) & 0x8;
  134. int top_bits = ((uint8)byte) & 0xf0;
  135. if ((sign_bit_set && top_bits != 0x70)
  136. || (!sign_bit_set && top_bits != 0))
  137. goto fail_integer_too_large;
  138. }
  139. }
  140. else if (sign && maxbits == 64) {
  141. if (shift < maxbits) {
  142. /* Sign extend, second highest bit is the sign bit */
  143. if ((uint8)byte & 0x40)
  144. result |= (~((uint64)0)) << shift;
  145. }
  146. else {
  147. /* The top bits should be a sign-extension of the sign bit */
  148. bool sign_bit_set = ((uint8)byte) & 0x1;
  149. int top_bits = ((uint8)byte) & 0xfe;
  150. if ((sign_bit_set && top_bits != 0x7e)
  151. || (!sign_bit_set && top_bits != 0))
  152. goto fail_integer_too_large;
  153. }
  154. }
  155. *p_buf += offset;
  156. *p_result = result;
  157. return true;
  158. fail_integer_too_large:
  159. set_error_buf(error_buf, error_buf_size, "integer too large");
  160. fail:
  161. return false;
  162. }
  163. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  164. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  165. #define read_leb_int64(p, p_end, res) \
  166. do { \
  167. uint64 res64; \
  168. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  169. error_buf_size)) \
  170. goto fail; \
  171. res = (int64)res64; \
  172. } while (0)
  173. #define read_leb_uint32(p, p_end, res) \
  174. do { \
  175. uint64 res64; \
  176. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  177. error_buf_size)) \
  178. goto fail; \
  179. res = (uint32)res64; \
  180. } while (0)
  181. #define read_leb_int32(p, p_end, res) \
  182. do { \
  183. uint64 res64; \
  184. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  185. error_buf_size)) \
  186. goto fail; \
  187. res = (int32)res64; \
  188. } while (0)
  189. static char *
  190. type2str(uint8 type)
  191. {
  192. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  193. #if WASM_ENABLE_GC != 0
  194. char *type_str_ref[] = { "stringview_iter",
  195. "stringview_wtf16",
  196. "(ref null ht)",
  197. "(ref ht)",
  198. "", /* reserved */
  199. "stringview_wtf8",
  200. "stringref",
  201. "", /* reserved */
  202. "", /* reserved */
  203. "arrayref",
  204. "structref",
  205. "i32ref",
  206. "eqref",
  207. "anyref",
  208. "externref",
  209. "funcref",
  210. "nullref",
  211. "nullexternref",
  212. "nullfuncref" };
  213. #endif
  214. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  215. return type_str[type - VALUE_TYPE_V128];
  216. #if WASM_ENABLE_GC != 0
  217. else if (wasm_is_type_reftype(type))
  218. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  219. #endif
  220. else if (type == VALUE_TYPE_FUNCREF)
  221. return "funcref";
  222. else if (type == VALUE_TYPE_EXTERNREF)
  223. return "externref";
  224. else
  225. return "unknown type";
  226. }
  227. static bool
  228. is_32bit_type(uint8 type)
  229. {
  230. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  231. #if WASM_ENABLE_GC != 0
  232. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  233. #elif WASM_ENABLE_REF_TYPES != 0
  234. /* For reference types, we use uint32 index to represent
  235. the funcref and externref */
  236. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  237. #endif
  238. )
  239. return true;
  240. return false;
  241. }
  242. static bool
  243. is_64bit_type(uint8 type)
  244. {
  245. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  246. #if WASM_ENABLE_GC != 0
  247. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  248. #endif
  249. )
  250. return true;
  251. return false;
  252. }
  253. static bool
  254. is_value_type(uint8 type)
  255. {
  256. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  257. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  258. #if WASM_ENABLE_GC != 0
  259. /* reference types, 0x65 to 0x70 */
  260. || wasm_is_type_reftype(type)
  261. #elif WASM_ENABLE_REF_TYPES != 0
  262. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  263. #endif
  264. #if WASM_ENABLE_SIMD != 0
  265. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  266. || type == VALUE_TYPE_V128 /* 0x7B */
  267. #endif
  268. #endif
  269. )
  270. return true;
  271. return false;
  272. }
  273. #if WASM_ENABLE_GC != 0
  274. static bool
  275. is_packed_type(uint8 type)
  276. {
  277. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  278. }
  279. #endif
  280. static bool
  281. is_byte_a_type(uint8 type)
  282. {
  283. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  284. }
  285. #if WASM_ENABLE_SIMD != 0
  286. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  287. static V128
  288. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  289. {
  290. V128 result;
  291. uint8 i;
  292. for (i = 0; i != 16; ++i) {
  293. result.i8x16[i] = read_uint8(p_buf);
  294. }
  295. return result;
  296. }
  297. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  298. #endif /* end of WASM_ENABLE_SIMD */
  299. static void *
  300. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  301. {
  302. void *mem;
  303. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  304. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  305. return NULL;
  306. }
  307. memset(mem, 0, (uint32)size);
  308. return mem;
  309. }
  310. static void *
  311. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  312. uint32 error_buf_size)
  313. {
  314. uint8 *mem_new;
  315. bh_assert(size_new > size_old);
  316. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  317. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  318. memset(mem_new + size_old, 0, size_new - size_old);
  319. wasm_runtime_free(mem_old);
  320. }
  321. return mem_new;
  322. }
  323. #define MEM_REALLOC(mem, size_old, size_new) \
  324. do { \
  325. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  326. error_buf_size); \
  327. if (!mem_new) \
  328. goto fail; \
  329. mem = mem_new; \
  330. } while (0)
  331. static bool
  332. check_utf8_str(const uint8 *str, uint32 len)
  333. {
  334. /* The valid ranges are taken from page 125, below link
  335. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  336. const uint8 *p = str, *p_end = str + len;
  337. uint8 chr;
  338. while (p < p_end) {
  339. chr = *p;
  340. if (chr == 0) {
  341. LOG_WARNING(
  342. "LIMITATION: a string which contains '\\00' is unsupported");
  343. return false;
  344. }
  345. else if (chr < 0x80) {
  346. p++;
  347. }
  348. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  349. if (p[1] < 0x80 || p[1] > 0xBF) {
  350. return false;
  351. }
  352. p += 2;
  353. }
  354. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  355. if (chr == 0xE0) {
  356. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  357. return false;
  358. }
  359. }
  360. else if (chr == 0xED) {
  361. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  362. return false;
  363. }
  364. }
  365. else { /* chr >= 0xE1 && chr <= 0xEF */
  366. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  367. return false;
  368. }
  369. }
  370. p += 3;
  371. }
  372. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  373. if (chr == 0xF0) {
  374. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  375. || p[3] < 0x80 || p[3] > 0xBF) {
  376. return false;
  377. }
  378. }
  379. else if (chr <= 0xF3) { /* and also chr >= 0xF1 */
  380. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  381. || p[3] < 0x80 || p[3] > 0xBF) {
  382. return false;
  383. }
  384. }
  385. else { /* chr == 0xF4 */
  386. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  387. || p[3] < 0x80 || p[3] > 0xBF) {
  388. return false;
  389. }
  390. }
  391. p += 4;
  392. }
  393. else {
  394. return false;
  395. }
  396. }
  397. return (p == p_end);
  398. }
  399. static char *
  400. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  401. bool is_load_from_file_buf, char *error_buf,
  402. uint32 error_buf_size)
  403. {
  404. StringNode *node, *node_next;
  405. if (!check_utf8_str(str, len)) {
  406. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  407. return NULL;
  408. }
  409. if (len == 0) {
  410. return "";
  411. }
  412. else if (is_load_from_file_buf) {
  413. /* As the file buffer can be referred to after loading, we use
  414. the previous byte of leb encoded size to adjust the string:
  415. move string 1 byte backward and then append '\0' */
  416. char *c_str = (char *)str - 1;
  417. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  418. c_str[len] = '\0';
  419. return c_str;
  420. }
  421. /* Search const str list */
  422. node = module->const_str_list;
  423. while (node) {
  424. node_next = node->next;
  425. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  426. break;
  427. node = node_next;
  428. }
  429. if (node) {
  430. return node->str;
  431. }
  432. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  433. error_buf_size))) {
  434. return NULL;
  435. }
  436. node->str = ((char *)node) + sizeof(StringNode);
  437. bh_memcpy_s(node->str, len + 1, str, len);
  438. node->str[len] = '\0';
  439. if (!module->const_str_list) {
  440. /* set as head */
  441. module->const_str_list = node;
  442. node->next = NULL;
  443. }
  444. else {
  445. /* insert it */
  446. node->next = module->const_str_list;
  447. module->const_str_list = node;
  448. }
  449. return node->str;
  450. }
  451. #if WASM_ENABLE_GC != 0
  452. static bool
  453. check_type_index(const WASMModule *module, uint32 type_index, char *error_buf,
  454. uint32 error_buf_size)
  455. {
  456. if (type_index >= module->type_count) {
  457. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  458. type_index);
  459. return false;
  460. }
  461. return true;
  462. }
  463. static bool
  464. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  465. uint32 error_buf_size)
  466. {
  467. if (!check_type_index(module, type_index, error_buf, error_buf_size)) {
  468. return false;
  469. }
  470. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  471. set_error_buf(error_buf, error_buf_size, "unkown array type");
  472. return false;
  473. }
  474. return true;
  475. }
  476. #endif
  477. static bool
  478. check_function_index(const WASMModule *module, uint32 function_index,
  479. char *error_buf, uint32 error_buf_size)
  480. {
  481. if (function_index
  482. >= module->import_function_count + module->function_count) {
  483. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  484. function_index);
  485. return false;
  486. }
  487. return true;
  488. }
  489. typedef struct InitValue {
  490. uint8 type;
  491. uint8 flag;
  492. #if WASM_ENABLE_GC != 0
  493. uint8 gc_opcode;
  494. WASMRefType ref_type;
  495. #endif
  496. WASMValue value;
  497. } InitValue;
  498. typedef struct ConstExprContext {
  499. uint32 sp;
  500. uint32 size;
  501. WASMModule *module;
  502. InitValue *stack;
  503. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  504. } ConstExprContext;
  505. static void
  506. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  507. {
  508. ctx->sp = 0;
  509. ctx->module = module;
  510. ctx->stack = ctx->data;
  511. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  512. }
  513. static bool
  514. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  515. #if WASM_ENABLE_GC != 0
  516. WASMRefType *ref_type, uint8 gc_opcode,
  517. #endif
  518. WASMValue *value, char *error_buf, uint32 error_buf_size)
  519. {
  520. InitValue *cur_value;
  521. if (ctx->sp >= ctx->size) {
  522. if (ctx->stack != ctx->data) {
  523. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  524. (ctx->size + 4) * sizeof(InitValue));
  525. }
  526. else {
  527. if (!(ctx->stack =
  528. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  529. error_buf, error_buf_size))) {
  530. goto fail;
  531. }
  532. }
  533. ctx->size += 4;
  534. }
  535. cur_value = &ctx->stack[ctx->sp++];
  536. cur_value->type = type;
  537. cur_value->flag = flag;
  538. cur_value->value = *value;
  539. #if WASM_ENABLE_GC != 0
  540. cur_value->gc_opcode = gc_opcode;
  541. if (wasm_is_type_multi_byte_type(type)) {
  542. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  543. ref_type, wasm_reftype_struct_size(ref_type));
  544. }
  545. #endif
  546. return true;
  547. fail:
  548. return false;
  549. }
  550. static bool
  551. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  552. #if WASM_ENABLE_GC != 0
  553. WASMRefType *ref_type, uint8 *p_gc_opcode,
  554. #endif
  555. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  556. {
  557. InitValue *cur_value;
  558. if (ctx->sp == 0) {
  559. set_error_buf(error_buf, error_buf_size,
  560. "type mismatch: const expr stack underflow");
  561. return false;
  562. }
  563. cur_value = &ctx->stack[--ctx->sp];
  564. #if WASM_ENABLE_GC == 0
  565. if (cur_value->type != type) {
  566. set_error_buf(error_buf, error_buf_size, "type mismatch");
  567. return false;
  568. }
  569. #else
  570. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  571. ref_type, ctx->module->types,
  572. ctx->module->type_count)) {
  573. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  574. "type mismatch: expect ", type2str(type),
  575. " but got other");
  576. goto fail;
  577. }
  578. if ((ctx->sp != 0) && (cur_value->flag == WASM_OP_GC_PREFIX)
  579. && (cur_value->gc_opcode != WASM_OP_REF_I31)) {
  580. /* To reduce complexity, we don't allow initialize struct fields/array
  581. * element with references, so struct/array must be at the bottom of the
  582. * init value stack */
  583. set_error_buf(
  584. error_buf, error_buf_size,
  585. "struct or array as field is not supported in constant expr");
  586. goto fail;
  587. }
  588. #endif
  589. if (p_flag)
  590. *p_flag = cur_value->flag;
  591. if (p_value)
  592. *p_value = cur_value->value;
  593. #if WASM_ENABLE_GC != 0
  594. if (p_gc_opcode)
  595. *p_gc_opcode = cur_value->gc_opcode;
  596. #endif
  597. return true;
  598. #if WASM_ENABLE_GC != 0
  599. fail:
  600. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  601. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  602. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  603. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  604. wasm_runtime_free(cur_value->value.data);
  605. }
  606. return false;
  607. #endif
  608. }
  609. static void
  610. destroy_const_expr_stack(ConstExprContext *ctx)
  611. {
  612. #if WASM_ENABLE_GC != 0
  613. uint32 i;
  614. for (i = 0; i < ctx->sp; i++) {
  615. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  616. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  617. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  618. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  619. wasm_runtime_free(ctx->stack[i].value.data);
  620. }
  621. }
  622. #endif
  623. if (ctx->stack != ctx->data) {
  624. wasm_runtime_free(ctx->stack);
  625. }
  626. }
  627. #if WASM_ENABLE_GC != 0
  628. static void
  629. destroy_init_expr(InitializerExpression *expr)
  630. {
  631. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  632. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  633. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  634. wasm_runtime_free(expr->u.data);
  635. }
  636. }
  637. #endif /* end of WASM_ENABLE_GC != 0 */
  638. static bool
  639. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  640. InitializerExpression *init_expr, uint8 type, void *ref_type,
  641. char *error_buf, uint32 error_buf_size)
  642. {
  643. const uint8 *p = *p_buf, *p_end = buf_end;
  644. uint8 flag, *p_float;
  645. uint32 i;
  646. ConstExprContext const_expr_ctx = { 0 };
  647. WASMValue cur_value;
  648. #if WASM_ENABLE_GC != 0
  649. uint32 opcode1, type_idx;
  650. uint8 opcode;
  651. WASMRefType cur_ref_type = { 0 };
  652. #endif
  653. init_const_expr_stack(&const_expr_ctx, module);
  654. CHECK_BUF(p, p_end, 1);
  655. flag = read_uint8(p);
  656. while (flag != WASM_OP_END) {
  657. switch (flag) {
  658. /* i32.const */
  659. case INIT_EXPR_TYPE_I32_CONST:
  660. read_leb_int32(p, p_end, cur_value.i32);
  661. if (!push_const_expr_stack(
  662. &const_expr_ctx, flag, VALUE_TYPE_I32,
  663. #if WASM_ENABLE_GC != 0
  664. NULL, 0,
  665. #endif
  666. &cur_value, error_buf, error_buf_size))
  667. goto fail;
  668. break;
  669. /* i64.const */
  670. case INIT_EXPR_TYPE_I64_CONST:
  671. read_leb_int64(p, p_end, cur_value.i64);
  672. if (!push_const_expr_stack(
  673. &const_expr_ctx, flag, VALUE_TYPE_I64,
  674. #if WASM_ENABLE_GC != 0
  675. NULL, 0,
  676. #endif
  677. &cur_value, error_buf, error_buf_size))
  678. goto fail;
  679. break;
  680. /* f32.const */
  681. case INIT_EXPR_TYPE_F32_CONST:
  682. CHECK_BUF(p, p_end, 4);
  683. p_float = (uint8 *)&cur_value.f32;
  684. for (i = 0; i < sizeof(float32); i++)
  685. *p_float++ = *p++;
  686. if (!push_const_expr_stack(
  687. &const_expr_ctx, flag, VALUE_TYPE_F32,
  688. #if WASM_ENABLE_GC != 0
  689. NULL, 0,
  690. #endif
  691. &cur_value, error_buf, error_buf_size))
  692. goto fail;
  693. break;
  694. /* f64.const */
  695. case INIT_EXPR_TYPE_F64_CONST:
  696. CHECK_BUF(p, p_end, 8);
  697. p_float = (uint8 *)&cur_value.f64;
  698. for (i = 0; i < sizeof(float64); i++)
  699. *p_float++ = *p++;
  700. if (!push_const_expr_stack(
  701. &const_expr_ctx, flag, VALUE_TYPE_F64,
  702. #if WASM_ENABLE_GC != 0
  703. NULL, 0,
  704. #endif
  705. &cur_value, error_buf, error_buf_size))
  706. goto fail;
  707. break;
  708. #if WASM_ENABLE_SIMD != 0
  709. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  710. /* v128.const */
  711. case INIT_EXPR_TYPE_V128_CONST:
  712. {
  713. uint64 high, low;
  714. CHECK_BUF(p, p_end, 1);
  715. (void)read_uint8(p);
  716. CHECK_BUF(p, p_end, 16);
  717. wasm_runtime_read_v128(p, &high, &low);
  718. p += 16;
  719. cur_value.v128.i64x2[0] = high;
  720. cur_value.v128.i64x2[1] = low;
  721. if (!push_const_expr_stack(
  722. &const_expr_ctx, flag, VALUE_TYPE_V128,
  723. #if WASM_ENABLE_GC != 0
  724. NULL, 0,
  725. #endif
  726. &cur_value, error_buf, error_buf_size))
  727. goto fail;
  728. #if WASM_ENABLE_WAMR_COMPILER != 0
  729. /* If any init_expr is v128.const, mark SIMD used */
  730. module->is_simd_used = true;
  731. #endif
  732. break;
  733. }
  734. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  735. #endif /* end of WASM_ENABLE_SIMD */
  736. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  737. /* ref.func */
  738. case INIT_EXPR_TYPE_FUNCREF_CONST:
  739. {
  740. uint32 func_idx;
  741. read_leb_uint32(p, p_end, func_idx);
  742. cur_value.ref_index = func_idx;
  743. if (!check_function_index(module, func_idx, error_buf,
  744. error_buf_size)) {
  745. goto fail;
  746. }
  747. #if WASM_ENABLE_GC == 0
  748. if (!push_const_expr_stack(&const_expr_ctx, flag,
  749. VALUE_TYPE_FUNCREF, &cur_value,
  750. error_buf, error_buf_size))
  751. goto fail;
  752. #else
  753. if (func_idx < module->import_function_count) {
  754. type_idx =
  755. module->import_functions[func_idx].u.function.type_idx;
  756. }
  757. else {
  758. type_idx = module
  759. ->functions[func_idx
  760. - module->import_function_count]
  761. ->type_idx;
  762. }
  763. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  764. false, type_idx);
  765. if (!push_const_expr_stack(&const_expr_ctx, flag,
  766. cur_ref_type.ref_type, &cur_ref_type,
  767. 0, &cur_value, error_buf,
  768. error_buf_size))
  769. goto fail;
  770. #endif
  771. #if WASM_ENABLE_WAMR_COMPILER != 0
  772. module->is_ref_types_used = true;
  773. #endif
  774. break;
  775. }
  776. /* ref.null */
  777. case INIT_EXPR_TYPE_REFNULL_CONST:
  778. {
  779. uint8 type1;
  780. CHECK_BUF(p, p_end, 1);
  781. type1 = read_uint8(p);
  782. #if WASM_ENABLE_GC == 0
  783. cur_value.ref_index = NULL_REF;
  784. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  785. &cur_value, error_buf,
  786. error_buf_size))
  787. goto fail;
  788. #else
  789. cur_value.gc_obj = NULL_REF;
  790. if (!is_byte_a_type(type1)) {
  791. p--;
  792. read_leb_uint32(p, p_end, type_idx);
  793. if (!check_type_index(module, type_idx, error_buf,
  794. error_buf_size))
  795. goto fail;
  796. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  797. true, type_idx);
  798. if (!push_const_expr_stack(&const_expr_ctx, flag,
  799. cur_ref_type.ref_type,
  800. &cur_ref_type, 0, &cur_value,
  801. error_buf, error_buf_size))
  802. goto fail;
  803. }
  804. else {
  805. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  806. NULL, 0, &cur_value, error_buf,
  807. error_buf_size))
  808. goto fail;
  809. }
  810. #endif
  811. #if WASM_ENABLE_WAMR_COMPILER != 0
  812. module->is_ref_types_used = true;
  813. #endif
  814. break;
  815. }
  816. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  817. /* get_global */
  818. case INIT_EXPR_TYPE_GET_GLOBAL:
  819. {
  820. uint32 global_idx;
  821. uint8 global_type;
  822. read_leb_uint32(p, p_end, cur_value.global_index);
  823. global_idx = cur_value.global_index;
  824. #if WASM_ENABLE_GC == 0
  825. if (global_idx >= module->import_global_count) {
  826. /**
  827. * Currently, constant expressions occurring as initializers
  828. * of globals are further constrained in that contained
  829. * global.get instructions are
  830. * only allowed to refer to imported globals.
  831. */
  832. set_error_buf_v(error_buf, error_buf_size,
  833. "unknown global %u", global_idx);
  834. goto fail;
  835. }
  836. if (module->import_globals[global_idx].u.global.is_mutable) {
  837. set_error_buf_v(error_buf, error_buf_size,
  838. "constant expression required");
  839. goto fail;
  840. }
  841. #else
  842. if (global_idx
  843. >= module->import_global_count + module->global_count) {
  844. set_error_buf_v(error_buf, error_buf_size,
  845. "unknown global %u", global_idx);
  846. goto fail;
  847. }
  848. if (global_idx < module->import_global_count
  849. && module->import_globals[global_idx].u.global.is_mutable) {
  850. set_error_buf_v(error_buf, error_buf_size,
  851. "constant expression required");
  852. goto fail;
  853. }
  854. #endif
  855. if (global_idx < module->import_global_count) {
  856. global_type =
  857. module->import_globals[global_idx].u.global.type;
  858. #if WASM_ENABLE_GC != 0
  859. if (wasm_is_type_multi_byte_type(global_type)) {
  860. WASMRefType *global_ref_type =
  861. module->import_globals[global_idx]
  862. .u.global.ref_type;
  863. bh_memcpy_s(&cur_ref_type,
  864. wasm_reftype_struct_size(global_ref_type),
  865. global_ref_type,
  866. wasm_reftype_struct_size(global_ref_type));
  867. }
  868. #endif
  869. }
  870. else {
  871. global_type =
  872. module
  873. ->globals[global_idx - module->import_global_count]
  874. .type;
  875. #if WASM_ENABLE_GC != 0
  876. if (wasm_is_type_multi_byte_type(global_type)) {
  877. WASMRefType *global_ref_type =
  878. module
  879. ->globals[global_idx
  880. - module->import_global_count]
  881. .ref_type;
  882. bh_memcpy_s(&cur_ref_type,
  883. wasm_reftype_struct_size(global_ref_type),
  884. global_ref_type,
  885. wasm_reftype_struct_size(global_ref_type));
  886. }
  887. #endif
  888. }
  889. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  890. #if WASM_ENABLE_GC != 0
  891. &cur_ref_type, 0,
  892. #endif
  893. &cur_value, error_buf,
  894. error_buf_size))
  895. goto fail;
  896. break;
  897. }
  898. #if WASM_ENABLE_GC != 0
  899. /* struct.new and array.new */
  900. case WASM_OP_GC_PREFIX:
  901. {
  902. read_leb_uint32(p, p_end, opcode1);
  903. switch (opcode1) {
  904. case WASM_OP_STRUCT_NEW:
  905. {
  906. WASMStructType *struct_type;
  907. WASMStructNewInitValues *struct_init_values = NULL;
  908. uint32 field_count;
  909. read_leb_uint32(p, p_end, type_idx);
  910. if (!check_type_index(module, type_idx, error_buf,
  911. error_buf_size)) {
  912. goto fail;
  913. }
  914. struct_type = (WASMStructType *)module->types[type_idx];
  915. if (struct_type->base_type.type_flag
  916. != WASM_TYPE_STRUCT) {
  917. set_error_buf(error_buf, error_buf_size,
  918. "unkown struct type");
  919. goto fail;
  920. }
  921. field_count = struct_type->field_count;
  922. if (!(struct_init_values = loader_malloc(
  923. offsetof(WASMStructNewInitValues, fields)
  924. + (uint64)field_count * sizeof(WASMValue),
  925. error_buf, error_buf_size))) {
  926. goto fail;
  927. }
  928. struct_init_values->count = field_count;
  929. for (i = field_count; i > 0; i--) {
  930. WASMRefType *field_ref_type = NULL;
  931. uint32 field_idx = i - 1;
  932. uint8 field_type =
  933. struct_type->fields[field_idx].field_type;
  934. if (wasm_is_type_multi_byte_type(field_type)) {
  935. field_ref_type = wasm_reftype_map_find(
  936. struct_type->ref_type_maps,
  937. struct_type->ref_type_map_count, field_idx);
  938. }
  939. if (is_packed_type(field_type)) {
  940. field_type = VALUE_TYPE_I32;
  941. }
  942. if (!pop_const_expr_stack(
  943. &const_expr_ctx, NULL, field_type,
  944. field_ref_type, NULL,
  945. &struct_init_values->fields[field_idx],
  946. error_buf, error_buf_size)) {
  947. wasm_runtime_free(struct_init_values);
  948. goto fail;
  949. }
  950. }
  951. cur_value.data = struct_init_values;
  952. wasm_set_refheaptype_typeidx(
  953. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  954. if (!push_const_expr_stack(
  955. &const_expr_ctx, flag, cur_ref_type.ref_type,
  956. &cur_ref_type, (uint8)opcode1, &cur_value,
  957. error_buf, error_buf_size)) {
  958. wasm_runtime_free(struct_init_values);
  959. goto fail;
  960. }
  961. break;
  962. }
  963. case WASM_OP_STRUCT_NEW_DEFAULT:
  964. {
  965. read_leb_uint32(p, p_end, cur_value.type_index);
  966. type_idx = cur_value.type_index;
  967. if (!check_type_index(module, type_idx, error_buf,
  968. error_buf_size)) {
  969. goto fail;
  970. }
  971. if (module->types[type_idx]->type_flag
  972. != WASM_TYPE_STRUCT) {
  973. set_error_buf(error_buf, error_buf_size,
  974. "unkown struct type");
  975. goto fail;
  976. }
  977. cur_value.type_index = type_idx;
  978. wasm_set_refheaptype_typeidx(
  979. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  980. if (!push_const_expr_stack(
  981. &const_expr_ctx, flag, cur_ref_type.ref_type,
  982. &cur_ref_type, (uint8)opcode1, &cur_value,
  983. error_buf, error_buf_size)) {
  984. goto fail;
  985. }
  986. break;
  987. }
  988. case WASM_OP_ARRAY_NEW:
  989. case WASM_OP_ARRAY_NEW_DEFAULT:
  990. case WASM_OP_ARRAY_NEW_FIXED:
  991. {
  992. WASMArrayNewInitValues *array_init_values = NULL;
  993. WASMArrayType *array_type = NULL;
  994. WASMRefType *elem_ref_type = NULL;
  995. uint64 total_size;
  996. uint8 elem_type;
  997. read_leb_uint32(p, p_end, cur_value.type_index);
  998. type_idx = cur_value.type_index;
  999. if (!check_type_index(module, type_idx, error_buf,
  1000. error_buf_size)) {
  1001. goto fail;
  1002. }
  1003. array_type = (WASMArrayType *)module->types[type_idx];
  1004. if (array_type->base_type.type_flag
  1005. != WASM_TYPE_ARRAY) {
  1006. set_error_buf(error_buf, error_buf_size,
  1007. "unkown array type");
  1008. goto fail;
  1009. }
  1010. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  1011. elem_type = array_type->elem_type;
  1012. if (wasm_is_type_multi_byte_type(elem_type)) {
  1013. elem_ref_type = array_type->elem_ref_type;
  1014. }
  1015. if (is_packed_type(elem_type)) {
  1016. elem_type = VALUE_TYPE_I32;
  1017. }
  1018. if (opcode1 == WASM_OP_ARRAY_NEW) {
  1019. WASMValue len_val;
  1020. if (!(array_init_values = loader_malloc(
  1021. sizeof(WASMArrayNewInitValues),
  1022. error_buf, error_buf_size))) {
  1023. goto fail;
  1024. }
  1025. array_init_values->type_idx = type_idx;
  1026. if (!pop_const_expr_stack(
  1027. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  1028. NULL, NULL, &len_val, error_buf,
  1029. error_buf_size)) {
  1030. wasm_runtime_free(array_init_values);
  1031. goto fail;
  1032. }
  1033. array_init_values->length = len_val.i32;
  1034. if (!pop_const_expr_stack(
  1035. &const_expr_ctx, NULL, elem_type,
  1036. elem_ref_type, NULL,
  1037. &array_init_values->elem_data[0],
  1038. error_buf, error_buf_size)) {
  1039. wasm_runtime_free(array_init_values);
  1040. goto fail;
  1041. }
  1042. cur_value.data = array_init_values;
  1043. }
  1044. else {
  1045. /* WASM_OP_ARRAY_NEW_FIXED */
  1046. uint32 len;
  1047. read_leb_uint32(p, p_end, len);
  1048. total_size =
  1049. (uint64)offsetof(WASMArrayNewInitValues,
  1050. elem_data)
  1051. + (uint64)sizeof(WASMValue) * len;
  1052. if (!(array_init_values =
  1053. loader_malloc(total_size, error_buf,
  1054. error_buf_size))) {
  1055. goto fail;
  1056. }
  1057. array_init_values->type_idx = type_idx;
  1058. array_init_values->length = len;
  1059. for (i = len; i > 0; i--) {
  1060. if (!pop_const_expr_stack(
  1061. &const_expr_ctx, NULL, elem_type,
  1062. elem_ref_type, NULL,
  1063. &array_init_values
  1064. ->elem_data[i - 1],
  1065. error_buf, error_buf_size)) {
  1066. wasm_runtime_free(array_init_values);
  1067. goto fail;
  1068. }
  1069. }
  1070. cur_value.data = array_init_values;
  1071. }
  1072. }
  1073. else {
  1074. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1075. WASMValue len_val;
  1076. uint32 len;
  1077. /* POP(i32) */
  1078. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1079. VALUE_TYPE_I32, NULL,
  1080. NULL, &len_val, error_buf,
  1081. error_buf_size)) {
  1082. goto fail;
  1083. }
  1084. len = len_val.i32;
  1085. cur_value.array_new_default.type_index = type_idx;
  1086. cur_value.array_new_default.length = len;
  1087. }
  1088. wasm_set_refheaptype_typeidx(
  1089. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1090. if (!push_const_expr_stack(
  1091. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1092. &cur_ref_type, (uint8)opcode1, &cur_value,
  1093. error_buf, error_buf_size)) {
  1094. if (array_init_values) {
  1095. wasm_runtime_free(array_init_values);
  1096. }
  1097. goto fail;
  1098. }
  1099. break;
  1100. }
  1101. case WASM_OP_ANY_CONVERT_EXTERN:
  1102. {
  1103. set_error_buf(error_buf, error_buf_size,
  1104. "unsuppoted constant expression of "
  1105. "extern.internalize");
  1106. goto fail;
  1107. }
  1108. case WASM_OP_EXTERN_CONVERT_ANY:
  1109. {
  1110. set_error_buf(error_buf, error_buf_size,
  1111. "unsuppoted constant expression of "
  1112. "extern.externalize");
  1113. goto fail;
  1114. }
  1115. case WASM_OP_REF_I31:
  1116. {
  1117. /* POP(i32) */
  1118. if (!pop_const_expr_stack(
  1119. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1120. NULL, &cur_value, error_buf, error_buf_size)) {
  1121. goto fail;
  1122. }
  1123. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1124. false, HEAP_TYPE_I31);
  1125. if (!push_const_expr_stack(
  1126. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1127. &cur_ref_type, (uint8)opcode1, &cur_value,
  1128. error_buf, error_buf_size)) {
  1129. goto fail;
  1130. }
  1131. break;
  1132. }
  1133. default:
  1134. set_error_buf(
  1135. error_buf, error_buf_size,
  1136. "type mismatch or constant expression required");
  1137. goto fail;
  1138. }
  1139. break;
  1140. }
  1141. #endif /* end of WASM_ENABLE_GC != 0 */
  1142. default:
  1143. {
  1144. set_error_buf(error_buf, error_buf_size,
  1145. "illegal opcode "
  1146. "or constant expression required "
  1147. "or type mismatch");
  1148. goto fail;
  1149. }
  1150. }
  1151. CHECK_BUF(p, p_end, 1);
  1152. flag = read_uint8(p);
  1153. }
  1154. /* There should be only one value left on the init value stack */
  1155. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1156. #if WASM_ENABLE_GC != 0
  1157. ref_type, &opcode,
  1158. #endif
  1159. &cur_value, error_buf, error_buf_size)) {
  1160. goto fail;
  1161. }
  1162. if (const_expr_ctx.sp != 0) {
  1163. set_error_buf(error_buf, error_buf_size,
  1164. "type mismatch: illegal constant opcode sequence");
  1165. goto fail;
  1166. }
  1167. init_expr->init_expr_type = flag;
  1168. init_expr->u = cur_value;
  1169. #if WASM_ENABLE_GC != 0
  1170. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1171. switch (opcode) {
  1172. case WASM_OP_STRUCT_NEW:
  1173. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1174. break;
  1175. case WASM_OP_STRUCT_NEW_DEFAULT:
  1176. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1177. break;
  1178. case WASM_OP_ARRAY_NEW:
  1179. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1180. break;
  1181. case WASM_OP_ARRAY_NEW_DEFAULT:
  1182. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1183. break;
  1184. case WASM_OP_ARRAY_NEW_FIXED:
  1185. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1186. break;
  1187. case WASM_OP_REF_I31:
  1188. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1189. break;
  1190. default:
  1191. bh_assert(0);
  1192. break;
  1193. }
  1194. }
  1195. #endif /* end of WASM_ENABLE_GC != 0 */
  1196. *p_buf = p;
  1197. destroy_const_expr_stack(&const_expr_ctx);
  1198. return true;
  1199. fail:
  1200. destroy_const_expr_stack(&const_expr_ctx);
  1201. return false;
  1202. }
  1203. static bool
  1204. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1205. {
  1206. if (mutable >= 2) {
  1207. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1208. return false;
  1209. }
  1210. return true;
  1211. }
  1212. #if WASM_ENABLE_GC != 0
  1213. static void
  1214. destroy_func_type(WASMFuncType *type)
  1215. {
  1216. /* Destroy the reference type hash set */
  1217. if (type->ref_type_maps)
  1218. wasm_runtime_free(type->ref_type_maps);
  1219. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1220. && WASM_ENABLE_LAZY_JIT != 0
  1221. if (type->call_to_llvm_jit_from_fast_jit)
  1222. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1223. #endif
  1224. /* Free the type */
  1225. wasm_runtime_free(type);
  1226. }
  1227. static void
  1228. destroy_struct_type(WASMStructType *type)
  1229. {
  1230. if (type->ref_type_maps)
  1231. wasm_runtime_free(type->ref_type_maps);
  1232. wasm_runtime_free(type);
  1233. }
  1234. static void
  1235. destroy_array_type(WASMArrayType *type)
  1236. {
  1237. wasm_runtime_free(type);
  1238. }
  1239. static void
  1240. destroy_wasm_type(WASMType *type)
  1241. {
  1242. if (type->type_flag == WASM_TYPE_FUNC)
  1243. destroy_func_type((WASMFuncType *)type);
  1244. else if (type->type_flag == WASM_TYPE_STRUCT)
  1245. destroy_struct_type((WASMStructType *)type);
  1246. else if (type->type_flag == WASM_TYPE_ARRAY)
  1247. destroy_array_type((WASMArrayType *)type);
  1248. else {
  1249. bh_assert(0);
  1250. }
  1251. }
  1252. /* Resolve (ref null ht) or (ref ht) */
  1253. static bool
  1254. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1255. WASMModule *module, bool nullable, WASMRefType *ref_type,
  1256. char *error_buf, uint32 error_buf_size)
  1257. {
  1258. const uint8 *p = *p_buf, *p_end = buf_end;
  1259. ref_type->ref_type =
  1260. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1261. ref_type->ref_ht_common.nullable = nullable;
  1262. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1263. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1264. /* heap type is (type i), i : typeidx, >= 0 */
  1265. if (!check_type_index(module, ref_type->ref_ht_typeidx.type_idx,
  1266. error_buf, error_buf_size)) {
  1267. return false;
  1268. }
  1269. }
  1270. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1271. /* heap type is func, extern, any, eq, i31 or data */
  1272. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1273. return false;
  1274. }
  1275. *p_buf = p;
  1276. return true;
  1277. fail:
  1278. return false;
  1279. }
  1280. static bool
  1281. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1282. WASMModule *module, bool *p_need_ref_type_map,
  1283. WASMRefType *ref_type, bool allow_packed_type,
  1284. char *error_buf, uint32 error_buf_size)
  1285. {
  1286. const uint8 *p = *p_buf, *p_end = buf_end;
  1287. uint8 type;
  1288. memset(ref_type, 0, sizeof(WASMRefType));
  1289. CHECK_BUF(p, p_end, 1);
  1290. type = read_uint8(p);
  1291. if (wasm_is_reftype_htref_nullable(type)) {
  1292. /* (ref null ht) */
  1293. if (!resolve_reftype_htref(&p, p_end, module, true, ref_type, error_buf,
  1294. error_buf_size))
  1295. return false;
  1296. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1297. *p_need_ref_type_map = true;
  1298. else {
  1299. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1300. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1301. multi-byte type to one-byte type to reduce the footprint and
  1302. the complexity of type equal/subtype checking */
  1303. ref_type->ref_type =
  1304. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1305. *p_need_ref_type_map = false;
  1306. }
  1307. }
  1308. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1309. /* (ref ht) */
  1310. if (!resolve_reftype_htref(&p, p_end, module, false, ref_type,
  1311. error_buf, error_buf_size))
  1312. return false;
  1313. *p_need_ref_type_map = true;
  1314. #if WASM_ENABLE_STRINGREF != 0
  1315. /* covert (ref string) to stringref */
  1316. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1317. ref_type->ref_type =
  1318. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1319. *p_need_ref_type_map = false;
  1320. }
  1321. #endif
  1322. }
  1323. else {
  1324. /* type which can be represented by one byte */
  1325. if (!is_value_type(type)
  1326. && !(allow_packed_type && is_packed_type(type))) {
  1327. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1328. return false;
  1329. }
  1330. ref_type->ref_type = type;
  1331. *p_need_ref_type_map = false;
  1332. #if WASM_ENABLE_WAMR_COMPILER != 0
  1333. /* If any value's type is v128, mark the module as SIMD used */
  1334. if (type == VALUE_TYPE_V128)
  1335. module->is_simd_used = true;
  1336. #endif
  1337. }
  1338. *p_buf = p;
  1339. return true;
  1340. fail:
  1341. return false;
  1342. }
  1343. static WASMRefType *
  1344. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1345. char *error_buf, uint32 error_buf_size)
  1346. {
  1347. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1348. if (!ret) {
  1349. set_error_buf(error_buf, error_buf_size,
  1350. "insert ref type to hash set failed");
  1351. }
  1352. return ret;
  1353. }
  1354. static bool
  1355. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1356. uint32 type_idx, char *error_buf, uint32 error_buf_size)
  1357. {
  1358. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1359. uint32 param_count, result_count, i, j = 0;
  1360. uint32 param_cell_num, ret_cell_num;
  1361. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1362. uint64 total_size;
  1363. bool need_ref_type_map;
  1364. WASMRefType ref_type;
  1365. WASMFuncType *type = NULL;
  1366. /* Parse first time to resolve param count, result count and
  1367. ref type map count */
  1368. read_leb_uint32(p, p_end, param_count);
  1369. p_org = p;
  1370. for (i = 0; i < param_count; i++) {
  1371. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1372. &ref_type, false, error_buf, error_buf_size)) {
  1373. return false;
  1374. }
  1375. if (need_ref_type_map)
  1376. ref_type_map_count++;
  1377. }
  1378. read_leb_uint32(p, p_end, result_count);
  1379. for (i = 0; i < result_count; i++) {
  1380. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1381. &ref_type, false, error_buf, error_buf_size)) {
  1382. return false;
  1383. }
  1384. if (need_ref_type_map) {
  1385. ref_type_map_count++;
  1386. result_ref_type_map_count++;
  1387. }
  1388. }
  1389. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1390. "ref type map count: %d",
  1391. type_idx, param_count, result_count, ref_type_map_count);
  1392. /* Parse second time to resolve param types, result types and
  1393. ref type map info */
  1394. p = p_org;
  1395. total_size = offsetof(WASMFuncType, types)
  1396. + sizeof(uint8) * (uint64)(param_count + result_count);
  1397. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1398. return false;
  1399. }
  1400. if (ref_type_map_count > 0) {
  1401. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1402. if (!(type->ref_type_maps =
  1403. loader_malloc(total_size, error_buf, error_buf_size))) {
  1404. goto fail;
  1405. }
  1406. }
  1407. type->base_type.type_flag = WASM_TYPE_FUNC;
  1408. type->param_count = param_count;
  1409. type->result_count = result_count;
  1410. type->ref_type_map_count = ref_type_map_count;
  1411. if (ref_type_map_count > 0) {
  1412. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1413. - result_ref_type_map_count;
  1414. }
  1415. for (i = 0; i < param_count; i++) {
  1416. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1417. &ref_type, false, error_buf, error_buf_size)) {
  1418. goto fail;
  1419. }
  1420. type->types[i] = ref_type.ref_type;
  1421. if (need_ref_type_map) {
  1422. type->ref_type_maps[j].index = i;
  1423. if (!(type->ref_type_maps[j++].ref_type =
  1424. reftype_set_insert(module->ref_type_set, &ref_type,
  1425. error_buf, error_buf_size))) {
  1426. goto fail;
  1427. }
  1428. }
  1429. }
  1430. read_leb_uint32(p, p_end, result_count);
  1431. for (i = 0; i < result_count; i++) {
  1432. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1433. &ref_type, false, error_buf, error_buf_size)) {
  1434. goto fail;
  1435. }
  1436. type->types[param_count + i] = ref_type.ref_type;
  1437. if (need_ref_type_map) {
  1438. type->ref_type_maps[j].index = param_count + i;
  1439. if (!(type->ref_type_maps[j++].ref_type =
  1440. reftype_set_insert(module->ref_type_set, &ref_type,
  1441. error_buf, error_buf_size))) {
  1442. goto fail;
  1443. }
  1444. }
  1445. }
  1446. bh_assert(j == type->ref_type_map_count);
  1447. #if TRACE_WASM_LOADER != 0
  1448. os_printf("type %d = ", type_idx);
  1449. wasm_dump_func_type(type);
  1450. #endif
  1451. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1452. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1453. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1454. set_error_buf(error_buf, error_buf_size,
  1455. "param count or result count too large");
  1456. goto fail;
  1457. }
  1458. type->param_cell_num = (uint16)param_cell_num;
  1459. type->ret_cell_num = (uint16)ret_cell_num;
  1460. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1461. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1462. #endif
  1463. #if WASM_ENABLE_WAMR_COMPILER != 0
  1464. for (i = 0; i < type->param_count + type->result_count; i++) {
  1465. if (type->types[i] == VALUE_TYPE_V128)
  1466. module->is_simd_used = true;
  1467. }
  1468. #endif
  1469. /* Calculate the minimal type index of the type equal to this type */
  1470. type->min_type_idx_normalized = type_idx;
  1471. for (i = 0; i < type_idx; i++) {
  1472. WASMFuncType *func_type = (WASMFuncType *)module->types[i];
  1473. if (func_type->base_type.type_flag == WASM_TYPE_FUNC
  1474. && wasm_func_type_equal(type, func_type, module->types,
  1475. type_idx + 1)) {
  1476. type->min_type_idx_normalized = i;
  1477. break;
  1478. }
  1479. }
  1480. *p_buf = p;
  1481. module->types[type_idx] = (WASMType *)type;
  1482. return true;
  1483. fail:
  1484. if (type)
  1485. destroy_func_type(type);
  1486. return false;
  1487. }
  1488. static bool
  1489. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1490. WASMModule *module, uint32 type_idx, char *error_buf,
  1491. uint32 error_buf_size)
  1492. {
  1493. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1494. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1495. uint32 i, j = 0, offset;
  1496. uint16 *reference_table;
  1497. uint64 total_size;
  1498. uint8 mutable;
  1499. bool need_ref_type_map;
  1500. WASMRefType ref_type;
  1501. WASMStructType *type = NULL;
  1502. /* Parse first time to resolve field count and ref type map count */
  1503. read_leb_uint32(p, p_end, field_count);
  1504. p_org = p;
  1505. for (i = 0; i < field_count; i++) {
  1506. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1507. &ref_type, true, error_buf, error_buf_size)) {
  1508. return false;
  1509. }
  1510. if (need_ref_type_map)
  1511. ref_type_map_count++;
  1512. if (wasm_is_type_reftype(ref_type.ref_type))
  1513. ref_field_count++;
  1514. CHECK_BUF(p, p_end, 1);
  1515. mutable = read_uint8(p);
  1516. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1517. return false;
  1518. }
  1519. }
  1520. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1521. type_idx, field_count, ref_type_map_count);
  1522. /* Parse second time to resolve field types and ref type map info */
  1523. p = p_org;
  1524. total_size = offsetof(WASMStructType, fields)
  1525. + sizeof(WASMStructFieldType) * (uint64)field_count
  1526. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1527. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1528. return false;
  1529. }
  1530. if (ref_type_map_count > 0) {
  1531. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1532. if (!(type->ref_type_maps =
  1533. loader_malloc(total_size, error_buf, error_buf_size))) {
  1534. goto fail;
  1535. }
  1536. }
  1537. type->reference_table = reference_table =
  1538. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1539. + sizeof(WASMStructFieldType) * field_count);
  1540. *reference_table++ = ref_field_count;
  1541. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1542. type->field_count = field_count;
  1543. type->ref_type_map_count = ref_type_map_count;
  1544. offset = (uint32)offsetof(WASMStructObject, field_data);
  1545. for (i = 0; i < field_count; i++) {
  1546. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1547. &ref_type, true, error_buf, error_buf_size)) {
  1548. goto fail;
  1549. }
  1550. type->fields[i].field_type = ref_type.ref_type;
  1551. if (need_ref_type_map) {
  1552. type->ref_type_maps[j].index = i;
  1553. if (!(type->ref_type_maps[j++].ref_type =
  1554. reftype_set_insert(module->ref_type_set, &ref_type,
  1555. error_buf, error_buf_size))) {
  1556. goto fail;
  1557. }
  1558. }
  1559. CHECK_BUF(p, p_end, 1);
  1560. type->fields[i].field_flags = read_uint8(p);
  1561. type->fields[i].field_size =
  1562. (uint8)wasm_reftype_size(ref_type.ref_type);
  1563. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1564. || defined(BUILD_TARGET_X86_32))
  1565. if (type->fields[i].field_size == 2)
  1566. offset = align_uint(offset, 2);
  1567. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1568. offset = align_uint(offset, 4);
  1569. #endif
  1570. type->fields[i].field_offset = offset;
  1571. if (wasm_is_type_reftype(ref_type.ref_type))
  1572. *reference_table++ = offset;
  1573. offset += type->fields[i].field_size;
  1574. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1575. type->fields[i].field_flags, type->fields[i].field_type);
  1576. }
  1577. type->total_size = offset;
  1578. bh_assert(j == type->ref_type_map_count);
  1579. #if TRACE_WASM_LOADER != 0
  1580. os_printf("type %d = ", type_idx);
  1581. wasm_dump_struct_type(type);
  1582. #endif
  1583. *p_buf = p;
  1584. module->types[type_idx] = (WASMType *)type;
  1585. return true;
  1586. fail:
  1587. if (type)
  1588. destroy_struct_type(type);
  1589. return false;
  1590. }
  1591. static bool
  1592. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1593. WASMModule *module, uint32 type_idx, char *error_buf,
  1594. uint32 error_buf_size)
  1595. {
  1596. const uint8 *p = *p_buf, *p_end = buf_end;
  1597. uint8 mutable;
  1598. bool need_ref_type_map;
  1599. WASMRefType ref_type;
  1600. WASMArrayType *type = NULL;
  1601. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  1602. true, error_buf, error_buf_size)) {
  1603. return false;
  1604. }
  1605. CHECK_BUF(p, p_end, 1);
  1606. mutable = read_uint8(p);
  1607. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1608. return false;
  1609. }
  1610. LOG_VERBOSE("type %u: array", type_idx);
  1611. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1612. error_buf_size))) {
  1613. return false;
  1614. }
  1615. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1616. type->elem_flags = mutable;
  1617. type->elem_type = ref_type.ref_type;
  1618. if (need_ref_type_map) {
  1619. if (!(type->elem_ref_type =
  1620. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1621. error_buf_size))) {
  1622. goto fail;
  1623. }
  1624. }
  1625. #if TRACE_WASM_LOADER != 0
  1626. os_printf("type %d = ", type_idx);
  1627. wasm_dump_array_type(type);
  1628. #endif
  1629. *p_buf = p;
  1630. module->types[type_idx] = (WASMType *)type;
  1631. return true;
  1632. fail:
  1633. if (type)
  1634. destroy_array_type(type);
  1635. return false;
  1636. }
  1637. static bool
  1638. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1639. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1640. {
  1641. if (heap_type >= 0) {
  1642. if (!check_type_index(module, heap_type, error_buf, error_buf_size)) {
  1643. return false;
  1644. }
  1645. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1646. heap_type);
  1647. }
  1648. else {
  1649. if (!wasm_is_valid_heap_type(heap_type)) {
  1650. set_error_buf(error_buf, error_buf_size, "unknown type");
  1651. return false;
  1652. }
  1653. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1654. heap_type);
  1655. if (nullable) {
  1656. /* For (ref null func/extern/any/eq/i31/data),
  1657. they are same as
  1658. funcref/externref/anyref/eqref/i31ref/dataref,
  1659. we convert the multi-byte type to one-byte
  1660. type to reduce the footprint and the
  1661. complexity of type equal/subtype checking */
  1662. ref_type->ref_type =
  1663. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1664. }
  1665. }
  1666. return true;
  1667. }
  1668. static void
  1669. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1670. WASMRefType *ref_type2)
  1671. {
  1672. /**
  1673. * The difference rt1 ∖ rt2 between two reference types is defined as
  1674. * follows:
  1675. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1676. * (ref ht2) = (ref null?1 ht1)
  1677. */
  1678. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1679. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1680. ref_type1, wasm_reftype_struct_size(ref_type1));
  1681. }
  1682. else {
  1683. ref_type_diff->ref_type = ref_type1->ref_type;
  1684. }
  1685. if (ref_type2->ref_ht_common.nullable) {
  1686. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1687. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1688. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1689. (int32)ref_type_diff->ref_type - 0x80);
  1690. }
  1691. else {
  1692. ref_type_diff->ref_ht_typeidx.nullable = false;
  1693. }
  1694. }
  1695. }
  1696. #else /* else of WASM_ENABLE_GC != 0 */
  1697. static void
  1698. destroy_wasm_type(WASMType *type)
  1699. {
  1700. if (type->ref_count > 1) {
  1701. /* The type is referenced by other types
  1702. of current wasm module */
  1703. type->ref_count--;
  1704. return;
  1705. }
  1706. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1707. && WASM_ENABLE_LAZY_JIT != 0
  1708. if (type->call_to_llvm_jit_from_fast_jit)
  1709. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1710. #endif
  1711. wasm_runtime_free(type);
  1712. }
  1713. #endif /* end of WASM_ENABLE_GC != 0 */
  1714. static bool
  1715. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1716. char *error_buf, uint32 error_buf_size)
  1717. {
  1718. const uint8 *p = buf, *p_end = buf_end;
  1719. uint32 type_count, i;
  1720. uint64 total_size;
  1721. uint8 flag;
  1722. #if WASM_ENABLE_GC != 0
  1723. uint32 processed_type_count = 0;
  1724. #endif
  1725. read_leb_uint32(p, p_end, type_count);
  1726. if (type_count) {
  1727. module->type_count = type_count;
  1728. total_size = sizeof(WASMType *) * (uint64)type_count;
  1729. if (!(module->types =
  1730. loader_malloc(total_size, error_buf, error_buf_size))) {
  1731. return false;
  1732. }
  1733. #if WASM_ENABLE_GC == 0
  1734. for (i = 0; i < type_count; i++) {
  1735. WASMFuncType *type;
  1736. const uint8 *p_org;
  1737. uint32 param_count, result_count, j;
  1738. uint32 param_cell_num, ret_cell_num;
  1739. CHECK_BUF(p, p_end, 1);
  1740. flag = read_uint8(p);
  1741. if (flag != 0x60) {
  1742. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1743. return false;
  1744. }
  1745. read_leb_uint32(p, p_end, param_count);
  1746. /* Resolve param count and result count firstly */
  1747. p_org = p;
  1748. CHECK_BUF(p, p_end, param_count);
  1749. p += param_count;
  1750. read_leb_uint32(p, p_end, result_count);
  1751. CHECK_BUF(p, p_end, result_count);
  1752. p = p_org;
  1753. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1754. set_error_buf(error_buf, error_buf_size,
  1755. "param count or result count too large");
  1756. return false;
  1757. }
  1758. total_size = offsetof(WASMFuncType, types)
  1759. + sizeof(uint8) * (uint64)(param_count + result_count);
  1760. if (!(type = module->types[i] =
  1761. loader_malloc(total_size, error_buf, error_buf_size))) {
  1762. return false;
  1763. }
  1764. /* Resolve param types and result types */
  1765. type->ref_count = 1;
  1766. type->param_count = (uint16)param_count;
  1767. type->result_count = (uint16)result_count;
  1768. for (j = 0; j < param_count; j++) {
  1769. CHECK_BUF(p, p_end, 1);
  1770. type->types[j] = read_uint8(p);
  1771. }
  1772. read_leb_uint32(p, p_end, result_count);
  1773. for (j = 0; j < result_count; j++) {
  1774. CHECK_BUF(p, p_end, 1);
  1775. type->types[param_count + j] = read_uint8(p);
  1776. }
  1777. for (j = 0; j < param_count + result_count; j++) {
  1778. if (!is_value_type(type->types[j])) {
  1779. set_error_buf(error_buf, error_buf_size,
  1780. "unknown value type");
  1781. return false;
  1782. }
  1783. }
  1784. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1785. ret_cell_num =
  1786. wasm_get_cell_num(type->types + param_count, result_count);
  1787. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1788. set_error_buf(error_buf, error_buf_size,
  1789. "param count or result count too large");
  1790. return false;
  1791. }
  1792. type->param_cell_num = (uint16)param_cell_num;
  1793. type->ret_cell_num = (uint16)ret_cell_num;
  1794. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1795. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1796. #endif
  1797. #if WASM_ENABLE_WAMR_COMPILER != 0
  1798. for (j = 0; j < type->param_count + type->result_count; j++) {
  1799. if (type->types[j] == VALUE_TYPE_V128)
  1800. module->is_simd_used = true;
  1801. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1802. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1803. module->is_ref_types_used = true;
  1804. }
  1805. #endif
  1806. /* If there is already a same type created, use it instead */
  1807. for (j = 0; j < i; j++) {
  1808. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1809. if (module->types[j]->ref_count == UINT16_MAX) {
  1810. set_error_buf(error_buf, error_buf_size,
  1811. "wasm type's ref count too large");
  1812. return false;
  1813. }
  1814. destroy_wasm_type(type);
  1815. module->types[i] = module->types[j];
  1816. module->types[j]->ref_count++;
  1817. break;
  1818. }
  1819. }
  1820. }
  1821. #else /* else of WASM_ENABLE_GC == 0 */
  1822. for (i = 0; i < type_count; i++) {
  1823. uint32 super_type_count = 0, parent_type_idx = (uint32)-1,
  1824. rec_count = 1, j;
  1825. bool is_sub_final = true;
  1826. CHECK_BUF(p, p_end, 1);
  1827. flag = read_uint8(p);
  1828. if (flag == DEFINED_TYPE_REC) {
  1829. read_leb_uint32(p, p_end, rec_count);
  1830. if (rec_count > 1) {
  1831. uint64 new_total_size;
  1832. module->type_count += rec_count - 1;
  1833. new_total_size =
  1834. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1835. MEM_REALLOC(module->types, total_size, new_total_size);
  1836. total_size = new_total_size;
  1837. }
  1838. LOG_VERBOSE("Processing rec group [%d-%d]",
  1839. processed_type_count,
  1840. processed_type_count + rec_count - 1);
  1841. }
  1842. else {
  1843. p--;
  1844. }
  1845. for (j = 0; j < rec_count; j++) {
  1846. WASMType *cur_type = NULL;
  1847. CHECK_BUF(p, p_end, 1);
  1848. flag = read_uint8(p);
  1849. parent_type_idx = -1;
  1850. if (flag == DEFINED_TYPE_SUB
  1851. || flag == DEFINED_TYPE_SUB_FINAL) {
  1852. read_leb_uint32(p, p_end, super_type_count);
  1853. if (super_type_count > 1) {
  1854. set_error_buf(error_buf, error_buf_size,
  1855. "super type count too large");
  1856. return false;
  1857. }
  1858. if (super_type_count > 0) {
  1859. read_leb_uint32(p, p_end, parent_type_idx);
  1860. if (parent_type_idx >= processed_type_count + j) {
  1861. set_error_buf_v(error_buf, error_buf_size,
  1862. "unknown type %d", parent_type_idx);
  1863. return false;
  1864. }
  1865. if (module->types[parent_type_idx]->is_sub_final) {
  1866. set_error_buf(error_buf, error_buf_size,
  1867. "sub type can not inherit from "
  1868. "a final super type");
  1869. return false;
  1870. }
  1871. }
  1872. if (flag == DEFINED_TYPE_SUB)
  1873. is_sub_final = false;
  1874. CHECK_BUF(p, p_end, 1);
  1875. flag = read_uint8(p);
  1876. }
  1877. if (flag == DEFINED_TYPE_FUNC) {
  1878. if (!resolve_func_type(&p, buf_end, module,
  1879. processed_type_count + j, error_buf,
  1880. error_buf_size)) {
  1881. return false;
  1882. }
  1883. }
  1884. else if (flag == DEFINED_TYPE_STRUCT) {
  1885. if (!resolve_struct_type(&p, buf_end, module,
  1886. processed_type_count + j,
  1887. error_buf, error_buf_size)) {
  1888. return false;
  1889. }
  1890. }
  1891. else if (flag == DEFINED_TYPE_ARRAY) {
  1892. if (!resolve_array_type(&p, buf_end, module,
  1893. processed_type_count + j, error_buf,
  1894. error_buf_size)) {
  1895. return false;
  1896. }
  1897. }
  1898. else {
  1899. set_error_buf(error_buf, error_buf_size,
  1900. "invalid type flag");
  1901. return false;
  1902. }
  1903. cur_type = module->types[processed_type_count + j];
  1904. cur_type->parent_type_idx = parent_type_idx;
  1905. cur_type->is_sub_final = is_sub_final;
  1906. if (rec_count > 1) {
  1907. cur_type->rec_count = rec_count;
  1908. cur_type->rec_idx = j;
  1909. }
  1910. }
  1911. /* resolve subtyping relationship in current rec group */
  1912. for (j = 0; j < rec_count; j++) {
  1913. WASMType *cur_type = module->types[processed_type_count + j];
  1914. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1915. WASMType *parent_type =
  1916. module->types[cur_type->parent_type_idx];
  1917. cur_type->parent_type = parent_type;
  1918. cur_type->root_type = parent_type->root_type;
  1919. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1920. }
  1921. else {
  1922. cur_type->parent_type = NULL;
  1923. cur_type->root_type = cur_type;
  1924. cur_type->inherit_depth = 0;
  1925. }
  1926. }
  1927. for (j = 0; j < rec_count; j++) {
  1928. WASMType *cur_type = module->types[processed_type_count + j];
  1929. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1930. WASMType *parent_type =
  1931. module->types[cur_type->parent_type_idx];
  1932. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1933. module->types,
  1934. module->type_count)) {
  1935. set_error_buf(error_buf, error_buf_size,
  1936. "sub type does not match super type");
  1937. return false;
  1938. }
  1939. }
  1940. }
  1941. if (rec_count > 1) {
  1942. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1943. processed_type_count,
  1944. processed_type_count + rec_count - 1);
  1945. }
  1946. processed_type_count += rec_count;
  1947. }
  1948. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1949. * module->type_count,
  1950. error_buf, error_buf_size))) {
  1951. return false;
  1952. }
  1953. #endif /* end of WASM_ENABLE_GC == 0 */
  1954. }
  1955. if (p != p_end) {
  1956. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1957. return false;
  1958. }
  1959. LOG_VERBOSE("Load type section success.\n");
  1960. return true;
  1961. fail:
  1962. return false;
  1963. }
  1964. static void
  1965. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1966. {
  1967. uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
  1968. ? init_size * 2
  1969. : WASM_TABLE_MAX_SIZE;
  1970. if (max_size_flag) {
  1971. /* module defines the table limitation */
  1972. bh_assert(init_size <= *max_size);
  1973. if (init_size < *max_size) {
  1974. *max_size =
  1975. *max_size < default_max_size ? *max_size : default_max_size;
  1976. }
  1977. }
  1978. else {
  1979. /* partial defined table limitation, gives a default value */
  1980. *max_size = default_max_size;
  1981. }
  1982. }
  1983. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1984. /**
  1985. * Find export item of a module with export info:
  1986. * module name, field name and export kind
  1987. */
  1988. static WASMExport *
  1989. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1990. const char *field_name, uint8 export_kind,
  1991. char *error_buf, uint32 error_buf_size)
  1992. {
  1993. WASMExport *export =
  1994. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  1995. export_kind, error_buf, error_buf_size);
  1996. return export;
  1997. }
  1998. #endif
  1999. #if WASM_ENABLE_MULTI_MODULE != 0
  2000. static WASMFunction *
  2001. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  2002. const WASMFuncType *expected_function_type,
  2003. char *error_buf, uint32 error_buf_size)
  2004. {
  2005. WASMModuleCommon *module_reg;
  2006. WASMFunction *function = NULL;
  2007. WASMExport *export = NULL;
  2008. WASMModule *module = NULL;
  2009. WASMFuncType *target_function_type = NULL;
  2010. module_reg = wasm_runtime_find_module_registered(module_name);
  2011. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2012. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2013. function_name);
  2014. set_error_buf(error_buf, error_buf_size, "unknown import");
  2015. return NULL;
  2016. }
  2017. module = (WASMModule *)module_reg;
  2018. export =
  2019. wasm_loader_find_export(module, module_name, function_name,
  2020. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2021. if (!export) {
  2022. return NULL;
  2023. }
  2024. /* resolve function type and function */
  2025. if (export->index < module->import_function_count) {
  2026. target_function_type =
  2027. module->import_functions[export->index].u.function.func_type;
  2028. function = module->import_functions[export->index]
  2029. .u.function.import_func_linked;
  2030. }
  2031. else {
  2032. target_function_type =
  2033. module->functions[export->index - module->import_function_count]
  2034. ->func_type;
  2035. function =
  2036. module->functions[export->index - module->import_function_count];
  2037. }
  2038. /* check function type */
  2039. if (!wasm_type_equal((WASMType *)expected_function_type,
  2040. (WASMType *)target_function_type, module->types,
  2041. module->type_count)) {
  2042. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2043. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2044. return NULL;
  2045. }
  2046. return function;
  2047. }
  2048. static WASMTable *
  2049. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2050. uint32 init_size, uint32 max_size, char *error_buf,
  2051. uint32 error_buf_size)
  2052. {
  2053. WASMModuleCommon *module_reg;
  2054. WASMTable *table = NULL;
  2055. WASMExport *export = NULL;
  2056. WASMModule *module = NULL;
  2057. module_reg = wasm_runtime_find_module_registered(module_name);
  2058. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2059. LOG_DEBUG("can not find a module named %s for table", module_name);
  2060. set_error_buf(error_buf, error_buf_size, "unknown import");
  2061. return NULL;
  2062. }
  2063. module = (WASMModule *)module_reg;
  2064. export =
  2065. wasm_loader_find_export(module, module_name, table_name,
  2066. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2067. if (!export) {
  2068. return NULL;
  2069. }
  2070. /* resolve table and check the init/max size */
  2071. if (export->index < module->import_table_count) {
  2072. table =
  2073. module->import_tables[export->index].u.table.import_table_linked;
  2074. }
  2075. else {
  2076. table = &(module->tables[export->index - module->import_table_count]);
  2077. }
  2078. if (table->init_size < init_size || table->max_size > max_size) {
  2079. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2080. module_name, table_name, table->init_size, table->max_size,
  2081. init_size, max_size);
  2082. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2083. return NULL;
  2084. }
  2085. return table;
  2086. }
  2087. static WASMMemory *
  2088. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2089. uint32 init_page_count, uint32 max_page_count,
  2090. char *error_buf, uint32 error_buf_size)
  2091. {
  2092. WASMModuleCommon *module_reg;
  2093. WASMMemory *memory = NULL;
  2094. WASMExport *export = NULL;
  2095. WASMModule *module = NULL;
  2096. module_reg = wasm_runtime_find_module_registered(module_name);
  2097. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2098. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2099. set_error_buf(error_buf, error_buf_size, "unknown import");
  2100. return NULL;
  2101. }
  2102. module = (WASMModule *)module_reg;
  2103. export =
  2104. wasm_loader_find_export(module, module_name, memory_name,
  2105. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2106. if (!export) {
  2107. return NULL;
  2108. }
  2109. /* resolve memory and check the init/max page count */
  2110. if (export->index < module->import_memory_count) {
  2111. memory = module->import_memories[export->index]
  2112. .u.memory.import_memory_linked;
  2113. }
  2114. else {
  2115. memory =
  2116. &(module->memories[export->index - module->import_memory_count]);
  2117. }
  2118. if (memory->init_page_count < init_page_count
  2119. || memory->max_page_count > max_page_count) {
  2120. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2121. module_name, memory_name, memory->init_page_count,
  2122. memory->max_page_count, init_page_count, max_page_count);
  2123. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2124. return NULL;
  2125. }
  2126. return memory;
  2127. }
  2128. static WASMGlobal *
  2129. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2130. uint8 type, bool is_mutable, char *error_buf,
  2131. uint32 error_buf_size)
  2132. {
  2133. WASMModuleCommon *module_reg;
  2134. WASMGlobal *global = NULL;
  2135. WASMExport *export = NULL;
  2136. WASMModule *module = NULL;
  2137. module_reg = wasm_runtime_find_module_registered(module_name);
  2138. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2139. LOG_DEBUG("can not find a module named %s for global", module_name);
  2140. set_error_buf(error_buf, error_buf_size, "unknown import");
  2141. return NULL;
  2142. }
  2143. module = (WASMModule *)module_reg;
  2144. export =
  2145. wasm_loader_find_export(module, module_name, global_name,
  2146. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2147. if (!export) {
  2148. return NULL;
  2149. }
  2150. /* resolve and check the global */
  2151. if (export->index < module->import_global_count) {
  2152. global =
  2153. module->import_globals[export->index].u.global.import_global_linked;
  2154. }
  2155. else {
  2156. global =
  2157. &(module->globals[export->index - module->import_global_count]);
  2158. }
  2159. if (global->type != type || global->is_mutable != is_mutable) {
  2160. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2161. module_name, global_name, global->type, global->is_mutable,
  2162. type, is_mutable);
  2163. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2164. return NULL;
  2165. }
  2166. return global;
  2167. }
  2168. #if WASM_ENABLE_TAGS != 0
  2169. static WASMTag *
  2170. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2171. const WASMType *expected_tag_type,
  2172. uint32 *linked_tag_index, char *error_buf,
  2173. uint32 error_buf_size)
  2174. {
  2175. WASMModuleCommon *module_reg;
  2176. WASMTag *tag = NULL;
  2177. WASMExport *export = NULL;
  2178. WASMModule *module = NULL;
  2179. module_reg = wasm_runtime_find_module_registered(module_name);
  2180. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2181. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2182. tag_name);
  2183. set_error_buf(error_buf, error_buf_size, "unknown import");
  2184. return NULL;
  2185. }
  2186. module = (WASMModule *)module_reg;
  2187. export =
  2188. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2189. error_buf, error_buf_size);
  2190. if (!export) {
  2191. return NULL;
  2192. }
  2193. /* resolve tag type and tag */
  2194. if (export->index < module->import_tag_count) {
  2195. /* importing an imported tag from the submodule */
  2196. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2197. }
  2198. else {
  2199. /* importing an section tag from the submodule */
  2200. tag = module->tags[export->index - module->import_tag_count];
  2201. }
  2202. /* check function type */
  2203. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2204. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2205. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2206. return NULL;
  2207. }
  2208. if (linked_tag_index != NULL) {
  2209. *linked_tag_index = export->index;
  2210. }
  2211. return tag;
  2212. }
  2213. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2214. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2215. static bool
  2216. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2217. const WASMModule *parent_module,
  2218. const char *sub_module_name, const char *function_name,
  2219. WASMFunctionImport *function, char *error_buf,
  2220. uint32 error_buf_size)
  2221. {
  2222. const uint8 *p = *p_buf, *p_end = buf_end;
  2223. uint32 declare_type_index = 0;
  2224. WASMFuncType *declare_func_type = NULL;
  2225. WASMFunction *linked_func = NULL;
  2226. #if WASM_ENABLE_MULTI_MODULE != 0
  2227. WASMModule *sub_module = NULL;
  2228. #endif
  2229. const char *linked_signature = NULL;
  2230. void *linked_attachment = NULL;
  2231. bool linked_call_conv_raw = false;
  2232. bool is_native_symbol = false;
  2233. read_leb_uint32(p, p_end, declare_type_index);
  2234. *p_buf = p;
  2235. if (declare_type_index >= parent_module->type_count) {
  2236. set_error_buf(error_buf, error_buf_size, "unknown type");
  2237. return false;
  2238. }
  2239. #if WASM_ENABLE_GC != 0
  2240. function->type_idx = declare_type_index;
  2241. #endif
  2242. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2243. declare_type_index = wasm_get_smallest_type_idx(
  2244. parent_module->types, parent_module->type_count, declare_type_index);
  2245. #endif
  2246. declare_func_type =
  2247. (WASMFuncType *)parent_module->types[declare_type_index];
  2248. /* lookup registered native symbols first */
  2249. linked_func = wasm_native_resolve_symbol(
  2250. sub_module_name, function_name, declare_func_type, &linked_signature,
  2251. &linked_attachment, &linked_call_conv_raw);
  2252. if (linked_func) {
  2253. is_native_symbol = true;
  2254. }
  2255. #if WASM_ENABLE_MULTI_MODULE != 0
  2256. else {
  2257. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2258. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2259. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2260. error_buf_size);
  2261. if (!sub_module) {
  2262. return false;
  2263. }
  2264. }
  2265. linked_func = wasm_loader_resolve_function(
  2266. sub_module_name, function_name, declare_func_type, error_buf,
  2267. error_buf_size);
  2268. }
  2269. #endif
  2270. function->module_name = (char *)sub_module_name;
  2271. function->field_name = (char *)function_name;
  2272. function->func_type = declare_func_type;
  2273. /* func_ptr_linked is for native registered symbol */
  2274. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2275. function->signature = linked_signature;
  2276. function->attachment = linked_attachment;
  2277. function->call_conv_raw = linked_call_conv_raw;
  2278. #if WASM_ENABLE_MULTI_MODULE != 0
  2279. function->import_module = is_native_symbol ? NULL : sub_module;
  2280. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2281. #endif
  2282. return true;
  2283. fail:
  2284. return false;
  2285. }
  2286. static bool
  2287. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2288. uint32 error_buf_size)
  2289. {
  2290. if (max_size < init_size) {
  2291. set_error_buf(error_buf, error_buf_size,
  2292. "size minimum must not be greater than maximum");
  2293. return false;
  2294. }
  2295. return true;
  2296. }
  2297. static bool
  2298. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2299. WASMModule *parent_module, const char *sub_module_name,
  2300. const char *table_name, WASMTableImport *table,
  2301. char *error_buf, uint32 error_buf_size)
  2302. {
  2303. const uint8 *p = *p_buf, *p_end = buf_end;
  2304. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2305. declare_init_size = 0, declare_max_size = 0;
  2306. #if WASM_ENABLE_MULTI_MODULE != 0
  2307. WASMModule *sub_module = NULL;
  2308. WASMTable *linked_table = NULL;
  2309. #endif
  2310. #if WASM_ENABLE_GC != 0
  2311. WASMRefType ref_type;
  2312. bool need_ref_type_map;
  2313. #endif
  2314. #if WASM_ENABLE_GC == 0
  2315. CHECK_BUF(p, p_end, 1);
  2316. /* 0x70 or 0x6F */
  2317. declare_elem_type = read_uint8(p);
  2318. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2319. #if WASM_ENABLE_REF_TYPES != 0
  2320. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2321. #endif
  2322. ) {
  2323. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2324. return false;
  2325. }
  2326. #else /* else of WASM_ENABLE_GC == 0 */
  2327. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2328. &ref_type, false, error_buf, error_buf_size)) {
  2329. return false;
  2330. }
  2331. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2332. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2333. return false;
  2334. }
  2335. declare_elem_type = ref_type.ref_type;
  2336. if (need_ref_type_map) {
  2337. if (!(table->elem_ref_type =
  2338. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2339. error_buf, error_buf_size))) {
  2340. return false;
  2341. }
  2342. }
  2343. #if TRACE_WASM_LOADER != 0
  2344. os_printf("import table type: ");
  2345. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  2346. os_printf("\n");
  2347. #endif
  2348. #endif /* end of WASM_ENABLE_GC == 0 */
  2349. read_leb_uint32(p, p_end, declare_max_size_flag);
  2350. if (declare_max_size_flag > 1) {
  2351. set_error_buf(error_buf, error_buf_size, "integer too large");
  2352. return false;
  2353. }
  2354. read_leb_uint32(p, p_end, declare_init_size);
  2355. if (declare_max_size_flag) {
  2356. read_leb_uint32(p, p_end, declare_max_size);
  2357. if (!check_table_max_size(declare_init_size, declare_max_size,
  2358. error_buf, error_buf_size))
  2359. return false;
  2360. }
  2361. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2362. &declare_max_size);
  2363. *p_buf = p;
  2364. #if WASM_ENABLE_MULTI_MODULE != 0
  2365. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2366. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2367. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2368. error_buf_size);
  2369. if (!sub_module) {
  2370. return false;
  2371. }
  2372. linked_table = wasm_loader_resolve_table(
  2373. sub_module_name, table_name, declare_init_size, declare_max_size,
  2374. error_buf, error_buf_size);
  2375. if (!linked_table) {
  2376. return false;
  2377. }
  2378. /* reset with linked table limit */
  2379. declare_elem_type = linked_table->elem_type;
  2380. declare_init_size = linked_table->init_size;
  2381. declare_max_size = linked_table->max_size;
  2382. declare_max_size_flag = linked_table->flags;
  2383. table->import_table_linked = linked_table;
  2384. table->import_module = sub_module;
  2385. }
  2386. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2387. /* (table (export "table") 10 20 funcref) */
  2388. /* we need this section working in wamrc */
  2389. if (!strcmp("spectest", sub_module_name)) {
  2390. const uint32 spectest_table_init_size = 10;
  2391. const uint32 spectest_table_max_size = 20;
  2392. if (strcmp("table", table_name)) {
  2393. set_error_buf(error_buf, error_buf_size,
  2394. "incompatible import type or unknown import");
  2395. return false;
  2396. }
  2397. if (declare_init_size > spectest_table_init_size
  2398. || declare_max_size < spectest_table_max_size) {
  2399. set_error_buf(error_buf, error_buf_size,
  2400. "incompatible import type");
  2401. return false;
  2402. }
  2403. declare_init_size = spectest_table_init_size;
  2404. declare_max_size = spectest_table_max_size;
  2405. }
  2406. /* now we believe all declaration are ok */
  2407. table->elem_type = declare_elem_type;
  2408. table->init_size = declare_init_size;
  2409. table->flags = declare_max_size_flag;
  2410. table->max_size = declare_max_size;
  2411. #if WASM_ENABLE_WAMR_COMPILER != 0
  2412. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2413. parent_module->is_ref_types_used = true;
  2414. #endif
  2415. (void)parent_module;
  2416. return true;
  2417. fail:
  2418. return false;
  2419. }
  2420. static bool
  2421. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  2422. {
  2423. if (init_size > DEFAULT_MAX_PAGES) {
  2424. set_error_buf(error_buf, error_buf_size,
  2425. "memory size must be at most 65536 pages (4GiB)");
  2426. return false;
  2427. }
  2428. return true;
  2429. }
  2430. static bool
  2431. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2432. uint32 error_buf_size)
  2433. {
  2434. if (max_size < init_size) {
  2435. set_error_buf(error_buf, error_buf_size,
  2436. "size minimum must not be greater than maximum");
  2437. return false;
  2438. }
  2439. if (max_size > DEFAULT_MAX_PAGES) {
  2440. set_error_buf(error_buf, error_buf_size,
  2441. "memory size must be at most 65536 pages (4GiB)");
  2442. return false;
  2443. }
  2444. return true;
  2445. }
  2446. static bool
  2447. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2448. WASMModule *parent_module, const char *sub_module_name,
  2449. const char *memory_name, WASMMemoryImport *memory,
  2450. char *error_buf, uint32 error_buf_size)
  2451. {
  2452. const uint8 *p = *p_buf, *p_end = buf_end;
  2453. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2454. uint32 pool_size = wasm_runtime_memory_pool_size();
  2455. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2456. / DEFAULT_NUM_BYTES_PER_PAGE;
  2457. #else
  2458. uint32 max_page_count = DEFAULT_MAX_PAGES;
  2459. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2460. uint32 declare_max_page_count_flag = 0;
  2461. uint32 declare_init_page_count = 0;
  2462. uint32 declare_max_page_count = 0;
  2463. #if WASM_ENABLE_MULTI_MODULE != 0
  2464. WASMModule *sub_module = NULL;
  2465. WASMMemory *linked_memory = NULL;
  2466. #endif
  2467. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  2468. read_leb_uint32(p, p_end, declare_init_page_count);
  2469. if (!check_memory_init_size(declare_init_page_count, error_buf,
  2470. error_buf_size)) {
  2471. return false;
  2472. }
  2473. if (declare_max_page_count_flag & 1) {
  2474. read_leb_uint32(p, p_end, declare_max_page_count);
  2475. if (!check_memory_max_size(declare_init_page_count,
  2476. declare_max_page_count, error_buf,
  2477. error_buf_size)) {
  2478. return false;
  2479. }
  2480. if (declare_max_page_count > max_page_count) {
  2481. declare_max_page_count = max_page_count;
  2482. }
  2483. }
  2484. else {
  2485. /* Limit the maximum memory size to max_page_count */
  2486. declare_max_page_count = max_page_count;
  2487. }
  2488. #if WASM_ENABLE_MULTI_MODULE != 0
  2489. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2490. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2491. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2492. error_buf_size);
  2493. if (!sub_module) {
  2494. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  2495. /* Avoid memory import failure when wasi-threads is enabled
  2496. and the memory is shared */
  2497. if (!(declare_max_page_count_flag & 2))
  2498. return false;
  2499. #else
  2500. return false;
  2501. #endif /* WASM_ENABLE_LIB_WASI_THREADS */
  2502. }
  2503. else {
  2504. linked_memory = wasm_loader_resolve_memory(
  2505. sub_module_name, memory_name, declare_init_page_count,
  2506. declare_max_page_count, error_buf, error_buf_size);
  2507. if (!linked_memory) {
  2508. return false;
  2509. }
  2510. /**
  2511. * reset with linked memory limit
  2512. */
  2513. memory->import_module = sub_module;
  2514. memory->import_memory_linked = linked_memory;
  2515. declare_init_page_count = linked_memory->init_page_count;
  2516. declare_max_page_count = linked_memory->max_page_count;
  2517. }
  2518. }
  2519. #endif
  2520. /* (memory (export "memory") 1 2) */
  2521. if (!strcmp("spectest", sub_module_name)) {
  2522. uint32 spectest_memory_init_page = 1;
  2523. uint32 spectest_memory_max_page = 2;
  2524. if (strcmp("memory", memory_name)) {
  2525. set_error_buf(error_buf, error_buf_size,
  2526. "incompatible import type or unknown import");
  2527. return false;
  2528. }
  2529. if (declare_init_page_count > spectest_memory_init_page
  2530. || declare_max_page_count < spectest_memory_max_page) {
  2531. set_error_buf(error_buf, error_buf_size,
  2532. "incompatible import type");
  2533. return false;
  2534. }
  2535. declare_init_page_count = spectest_memory_init_page;
  2536. declare_max_page_count = spectest_memory_max_page;
  2537. }
  2538. /* now we believe all declaration are ok */
  2539. memory->flags = declare_max_page_count_flag;
  2540. memory->init_page_count = declare_init_page_count;
  2541. memory->max_page_count = declare_max_page_count;
  2542. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2543. *p_buf = p;
  2544. (void)parent_module;
  2545. return true;
  2546. fail:
  2547. return false;
  2548. }
  2549. #if WASM_ENABLE_TAGS != 0
  2550. static bool
  2551. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2552. const WASMModule *parent_module, /* this module ! */
  2553. const char *sub_module_name, const char *tag_name,
  2554. WASMTagImport *tag, /* structure to fill */
  2555. char *error_buf, uint32 error_buf_size)
  2556. {
  2557. /* attribute and type of the import statement */
  2558. uint8 declare_tag_attribute;
  2559. uint32 declare_type_index;
  2560. const uint8 *p = *p_buf, *p_end = buf_end;
  2561. #if WASM_ENABLE_MULTI_MODULE != 0
  2562. WASMModule *sub_module = NULL;
  2563. #endif
  2564. /* get the one byte attribute */
  2565. CHECK_BUF(p, p_end, 1);
  2566. declare_tag_attribute = read_uint8(p);
  2567. if (declare_tag_attribute != 0) {
  2568. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2569. goto fail;
  2570. }
  2571. /* get type */
  2572. read_leb_uint32(p, p_end, declare_type_index);
  2573. /* compare against module->types */
  2574. if (declare_type_index >= parent_module->type_count) {
  2575. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2576. goto fail;
  2577. }
  2578. WASMType *declare_tag_type = parent_module->types[declare_type_index];
  2579. /* check, that the type of the declared tag returns void */
  2580. if (declare_tag_type->result_count != 0) {
  2581. set_error_buf(error_buf, error_buf_size,
  2582. "tag type signature does not return void");
  2583. goto fail;
  2584. }
  2585. #if WASM_ENABLE_MULTI_MODULE != 0
  2586. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2587. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2588. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2589. error_buf_size);
  2590. if (!sub_module) {
  2591. return false;
  2592. }
  2593. /* wasm_loader_resolve_tag checks, that the imported tag
  2594. * and the declared tag have the same type
  2595. */
  2596. uint32 linked_tag_index = 0;
  2597. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2598. sub_module_name, tag_name, declare_tag_type,
  2599. &linked_tag_index /* out */, error_buf, error_buf_size);
  2600. if (linked_tag) {
  2601. tag->import_module = sub_module;
  2602. tag->import_tag_linked = linked_tag;
  2603. tag->import_tag_index_linked = linked_tag_index;
  2604. }
  2605. }
  2606. #endif
  2607. /* store to module tag declarations */
  2608. tag->attribute = declare_tag_attribute;
  2609. tag->type = declare_type_index;
  2610. tag->module_name = (char *)sub_module_name;
  2611. tag->field_name = (char *)tag_name;
  2612. tag->tag_type = declare_tag_type;
  2613. *p_buf = p;
  2614. (void)parent_module;
  2615. LOG_VERBOSE("Load tag import success\n");
  2616. return true;
  2617. fail:
  2618. return false;
  2619. }
  2620. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2621. static bool
  2622. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2623. WASMModule *parent_module, char *sub_module_name,
  2624. char *global_name, WASMGlobalImport *global, char *error_buf,
  2625. uint32 error_buf_size)
  2626. {
  2627. const uint8 *p = *p_buf, *p_end = buf_end;
  2628. uint8 declare_type = 0;
  2629. uint8 declare_mutable = 0;
  2630. #if WASM_ENABLE_MULTI_MODULE != 0
  2631. WASMModule *sub_module = NULL;
  2632. WASMGlobal *linked_global = NULL;
  2633. #endif
  2634. #if WASM_ENABLE_GC != 0
  2635. WASMRefType ref_type;
  2636. bool need_ref_type_map;
  2637. #endif
  2638. bool ret = false;
  2639. #if WASM_ENABLE_GC == 0
  2640. CHECK_BUF(p, p_end, 2);
  2641. declare_type = read_uint8(p);
  2642. declare_mutable = read_uint8(p);
  2643. #else
  2644. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2645. &ref_type, false, error_buf, error_buf_size)) {
  2646. return false;
  2647. }
  2648. declare_type = ref_type.ref_type;
  2649. if (need_ref_type_map) {
  2650. if (!(global->ref_type =
  2651. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2652. error_buf, error_buf_size))) {
  2653. return false;
  2654. }
  2655. }
  2656. #if TRACE_WASM_LOADER != 0
  2657. os_printf("import global type: ");
  2658. wasm_dump_value_type(declare_type, global->ref_type);
  2659. os_printf("\n");
  2660. #endif
  2661. CHECK_BUF(p, p_end, 1);
  2662. declare_mutable = read_uint8(p);
  2663. #endif /* end of WASM_ENABLE_GC == 0 */
  2664. *p_buf = p;
  2665. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2666. return false;
  2667. }
  2668. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2669. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2670. global);
  2671. if (ret) {
  2672. if (global->type != declare_type
  2673. || global->is_mutable != declare_mutable) {
  2674. set_error_buf(error_buf, error_buf_size,
  2675. "incompatible import type");
  2676. return false;
  2677. }
  2678. global->is_linked = true;
  2679. }
  2680. #endif
  2681. #if WASM_ENABLE_MULTI_MODULE != 0
  2682. if (!global->is_linked
  2683. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2684. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2685. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2686. error_buf_size);
  2687. if (!sub_module) {
  2688. return false;
  2689. }
  2690. /* check sub modules */
  2691. linked_global = wasm_loader_resolve_global(
  2692. sub_module_name, global_name, declare_type, declare_mutable,
  2693. error_buf, error_buf_size);
  2694. if (linked_global) {
  2695. global->import_module = sub_module;
  2696. global->import_global_linked = linked_global;
  2697. global->is_linked = true;
  2698. }
  2699. }
  2700. #endif
  2701. global->module_name = sub_module_name;
  2702. global->field_name = global_name;
  2703. global->type = declare_type;
  2704. global->is_mutable = (declare_mutable == 1);
  2705. #if WASM_ENABLE_WAMR_COMPILER != 0
  2706. if (global->type == VALUE_TYPE_V128)
  2707. parent_module->is_simd_used = true;
  2708. else if (global->type == VALUE_TYPE_EXTERNREF)
  2709. parent_module->is_ref_types_used = true;
  2710. #endif
  2711. (void)parent_module;
  2712. (void)ret;
  2713. return true;
  2714. fail:
  2715. return false;
  2716. }
  2717. static bool
  2718. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2719. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2720. {
  2721. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2722. #if WASM_ENABLE_GC != 0
  2723. WASMRefType ref_type;
  2724. bool need_ref_type_map;
  2725. #endif
  2726. #if WASM_ENABLE_GC == 0
  2727. CHECK_BUF(p, p_end, 1);
  2728. /* 0x70 or 0x6F */
  2729. table->elem_type = read_uint8(p);
  2730. if (VALUE_TYPE_FUNCREF != table->elem_type
  2731. #if WASM_ENABLE_REF_TYPES != 0
  2732. && VALUE_TYPE_EXTERNREF != table->elem_type
  2733. #endif
  2734. ) {
  2735. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2736. return false;
  2737. }
  2738. #else /* else of WASM_ENABLE_GC == 0 */
  2739. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  2740. false, error_buf, error_buf_size)) {
  2741. return false;
  2742. }
  2743. table->elem_type = ref_type.ref_type;
  2744. if (need_ref_type_map) {
  2745. if (!(table->elem_ref_type =
  2746. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2747. error_buf_size))) {
  2748. return false;
  2749. }
  2750. }
  2751. #if TRACE_WASM_LOADER != 0
  2752. os_printf("table type: ");
  2753. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2754. os_printf("\n");
  2755. #endif
  2756. #endif /* end of WASM_ENABLE_GC == 0 */
  2757. p_org = p;
  2758. read_leb_uint32(p, p_end, table->flags);
  2759. #if WASM_ENABLE_SHARED_MEMORY == 0
  2760. if (p - p_org > 1) {
  2761. set_error_buf(error_buf, error_buf_size,
  2762. "integer representation too long");
  2763. return false;
  2764. }
  2765. if (table->flags > 1) {
  2766. set_error_buf(error_buf, error_buf_size, "integer too large");
  2767. return false;
  2768. }
  2769. #else
  2770. if (p - p_org > 1) {
  2771. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2772. return false;
  2773. }
  2774. if (table->flags == 2) {
  2775. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2776. return false;
  2777. }
  2778. if (table->flags > 1) {
  2779. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2780. return false;
  2781. }
  2782. #endif
  2783. read_leb_uint32(p, p_end, table->init_size);
  2784. if (table->flags) {
  2785. read_leb_uint32(p, p_end, table->max_size);
  2786. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2787. error_buf_size))
  2788. return false;
  2789. }
  2790. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2791. #if WASM_ENABLE_WAMR_COMPILER != 0
  2792. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2793. module->is_ref_types_used = true;
  2794. #endif
  2795. *p_buf = p;
  2796. return true;
  2797. fail:
  2798. return false;
  2799. }
  2800. static bool
  2801. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2802. char *error_buf, uint32 error_buf_size)
  2803. {
  2804. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2805. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2806. uint32 pool_size = wasm_runtime_memory_pool_size();
  2807. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2808. / DEFAULT_NUM_BYTES_PER_PAGE;
  2809. #else
  2810. uint32 max_page_count = DEFAULT_MAX_PAGES;
  2811. #endif
  2812. p_org = p;
  2813. read_leb_uint32(p, p_end, memory->flags);
  2814. #if WASM_ENABLE_SHARED_MEMORY == 0
  2815. if (p - p_org > 1) {
  2816. set_error_buf(error_buf, error_buf_size,
  2817. "integer representation too long");
  2818. return false;
  2819. }
  2820. if (memory->flags > 1) {
  2821. if (memory->flags & 2) {
  2822. set_error_buf(error_buf, error_buf_size,
  2823. "shared memory flag was found, "
  2824. "please enable shared memory, lib-pthread "
  2825. "or lib-wasi-threads");
  2826. }
  2827. else {
  2828. set_error_buf(error_buf, error_buf_size, "invalid memory flags");
  2829. }
  2830. return false;
  2831. }
  2832. #else
  2833. if (p - p_org > 1) {
  2834. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2835. return false;
  2836. }
  2837. if (memory->flags > 3) {
  2838. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2839. return false;
  2840. }
  2841. else if (memory->flags == 2) {
  2842. set_error_buf(error_buf, error_buf_size,
  2843. "shared memory must have maximum");
  2844. return false;
  2845. }
  2846. #endif
  2847. read_leb_uint32(p, p_end, memory->init_page_count);
  2848. if (!check_memory_init_size(memory->init_page_count, error_buf,
  2849. error_buf_size))
  2850. return false;
  2851. if (memory->flags & 1) {
  2852. read_leb_uint32(p, p_end, memory->max_page_count);
  2853. if (!check_memory_max_size(memory->init_page_count,
  2854. memory->max_page_count, error_buf,
  2855. error_buf_size))
  2856. return false;
  2857. if (memory->max_page_count > max_page_count)
  2858. memory->max_page_count = max_page_count;
  2859. }
  2860. else {
  2861. /* Limit the maximum memory size to max_page_count */
  2862. memory->max_page_count = max_page_count;
  2863. }
  2864. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2865. *p_buf = p;
  2866. return true;
  2867. fail:
  2868. return false;
  2869. }
  2870. static bool
  2871. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2872. bool is_load_from_file_buf, char *error_buf,
  2873. uint32 error_buf_size)
  2874. {
  2875. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2876. uint32 import_count, name_len, type_index, i, u32, flags;
  2877. uint64 total_size;
  2878. WASMImport *import;
  2879. WASMImport *import_functions = NULL, *import_tables = NULL;
  2880. WASMImport *import_memories = NULL, *import_globals = NULL;
  2881. #if WASM_ENABLE_TAGS != 0
  2882. WASMImport *import_tags = NULL;
  2883. #endif
  2884. char *sub_module_name, *field_name;
  2885. uint8 u8, kind, global_type;
  2886. read_leb_uint32(p, p_end, import_count);
  2887. if (import_count) {
  2888. module->import_count = import_count;
  2889. total_size = sizeof(WASMImport) * (uint64)import_count;
  2890. if (!(module->imports =
  2891. loader_malloc(total_size, error_buf, error_buf_size))) {
  2892. return false;
  2893. }
  2894. p_old = p;
  2895. /* Scan firstly to get import count of each type */
  2896. for (i = 0; i < import_count; i++) {
  2897. /* module name */
  2898. read_leb_uint32(p, p_end, name_len);
  2899. CHECK_BUF(p, p_end, name_len);
  2900. p += name_len;
  2901. /* field name */
  2902. read_leb_uint32(p, p_end, name_len);
  2903. CHECK_BUF(p, p_end, name_len);
  2904. p += name_len;
  2905. CHECK_BUF(p, p_end, 1);
  2906. /* 0x00/0x01/0x02/0x03/0x04 */
  2907. kind = read_uint8(p);
  2908. switch (kind) {
  2909. case IMPORT_KIND_FUNC: /* import function */
  2910. read_leb_uint32(p, p_end, type_index);
  2911. module->import_function_count++;
  2912. break;
  2913. case IMPORT_KIND_TABLE: /* import table */
  2914. CHECK_BUF(p, p_end, 1);
  2915. /* 0x70 */
  2916. u8 = read_uint8(p);
  2917. read_leb_uint32(p, p_end, flags);
  2918. read_leb_uint32(p, p_end, u32);
  2919. if (flags & 1)
  2920. read_leb_uint32(p, p_end, u32);
  2921. module->import_table_count++;
  2922. if (module->import_table_count > 1) {
  2923. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2924. set_error_buf(error_buf, error_buf_size,
  2925. "multiple tables");
  2926. return false;
  2927. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2928. module->is_ref_types_used = true;
  2929. #endif
  2930. }
  2931. break;
  2932. case IMPORT_KIND_MEMORY: /* import memory */
  2933. read_leb_uint32(p, p_end, flags);
  2934. read_leb_uint32(p, p_end, u32);
  2935. if (flags & 1)
  2936. read_leb_uint32(p, p_end, u32);
  2937. module->import_memory_count++;
  2938. if (module->import_memory_count > 1) {
  2939. set_error_buf(error_buf, error_buf_size,
  2940. "multiple memories");
  2941. return false;
  2942. }
  2943. break;
  2944. #if WASM_ENABLE_TAGS != 0
  2945. case IMPORT_KIND_TAG: /* import tags */
  2946. /* it only counts the number of tags to import */
  2947. module->import_tag_count++;
  2948. CHECK_BUF(p, p_end, 1);
  2949. u8 = read_uint8(p);
  2950. read_leb_uint32(p, p_end, type_index);
  2951. break;
  2952. #endif
  2953. case IMPORT_KIND_GLOBAL: /* import global */
  2954. #if WASM_ENABLE_GC != 0
  2955. /* valtype */
  2956. CHECK_BUF(p, p_end, 1);
  2957. global_type = read_uint8(p);
  2958. if (wasm_is_type_multi_byte_type(global_type)) {
  2959. int32 heap_type;
  2960. read_leb_int32(p, p_end, heap_type);
  2961. (void)heap_type;
  2962. }
  2963. /* mutability */
  2964. CHECK_BUF(p, p_end, 1);
  2965. p += 1;
  2966. #else
  2967. CHECK_BUF(p, p_end, 2);
  2968. p += 2;
  2969. #endif
  2970. (void)global_type;
  2971. module->import_global_count++;
  2972. break;
  2973. default:
  2974. set_error_buf(error_buf, error_buf_size,
  2975. "invalid import kind");
  2976. return false;
  2977. }
  2978. }
  2979. if (module->import_function_count)
  2980. import_functions = module->import_functions = module->imports;
  2981. if (module->import_table_count)
  2982. import_tables = module->import_tables =
  2983. module->imports + module->import_function_count;
  2984. if (module->import_memory_count)
  2985. import_memories = module->import_memories =
  2986. module->imports + module->import_function_count
  2987. + module->import_table_count;
  2988. #if WASM_ENABLE_TAGS != 0
  2989. if (module->import_tag_count)
  2990. import_tags = module->import_tags =
  2991. module->imports + module->import_function_count
  2992. + module->import_table_count + module->import_memory_count;
  2993. if (module->import_global_count)
  2994. import_globals = module->import_globals =
  2995. module->imports + module->import_function_count
  2996. + module->import_table_count + module->import_memory_count
  2997. + module->import_tag_count;
  2998. #else
  2999. if (module->import_global_count)
  3000. import_globals = module->import_globals =
  3001. module->imports + module->import_function_count
  3002. + module->import_table_count + module->import_memory_count;
  3003. #endif
  3004. p = p_old;
  3005. /* Scan again to resolve the data */
  3006. for (i = 0; i < import_count; i++) {
  3007. /* load module name */
  3008. read_leb_uint32(p, p_end, name_len);
  3009. CHECK_BUF(p, p_end, name_len);
  3010. if (!(sub_module_name = const_str_list_insert(
  3011. p, name_len, module, is_load_from_file_buf, error_buf,
  3012. error_buf_size))) {
  3013. return false;
  3014. }
  3015. p += name_len;
  3016. /* load field name */
  3017. read_leb_uint32(p, p_end, name_len);
  3018. CHECK_BUF(p, p_end, name_len);
  3019. if (!(field_name = const_str_list_insert(
  3020. p, name_len, module, is_load_from_file_buf, error_buf,
  3021. error_buf_size))) {
  3022. return false;
  3023. }
  3024. p += name_len;
  3025. CHECK_BUF(p, p_end, 1);
  3026. /* 0x00/0x01/0x02/0x03/0x4 */
  3027. kind = read_uint8(p);
  3028. switch (kind) {
  3029. case IMPORT_KIND_FUNC: /* import function */
  3030. bh_assert(import_functions);
  3031. import = import_functions++;
  3032. if (!load_function_import(
  3033. &p, p_end, module, sub_module_name, field_name,
  3034. &import->u.function, error_buf, error_buf_size)) {
  3035. return false;
  3036. }
  3037. break;
  3038. case IMPORT_KIND_TABLE: /* import table */
  3039. bh_assert(import_tables);
  3040. import = import_tables++;
  3041. if (!load_table_import(&p, p_end, module, sub_module_name,
  3042. field_name, &import->u.table,
  3043. error_buf, error_buf_size)) {
  3044. LOG_DEBUG("can not import such a table (%s,%s)",
  3045. sub_module_name, field_name);
  3046. return false;
  3047. }
  3048. break;
  3049. case IMPORT_KIND_MEMORY: /* import memory */
  3050. bh_assert(import_memories);
  3051. import = import_memories++;
  3052. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3053. field_name, &import->u.memory,
  3054. error_buf, error_buf_size)) {
  3055. return false;
  3056. }
  3057. break;
  3058. #if WASM_ENABLE_TAGS != 0
  3059. case IMPORT_KIND_TAG:
  3060. bh_assert(import_tags);
  3061. import = import_tags++;
  3062. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3063. field_name, &import->u.tag, error_buf,
  3064. error_buf_size)) {
  3065. return false;
  3066. }
  3067. break;
  3068. #endif
  3069. case IMPORT_KIND_GLOBAL: /* import global */
  3070. bh_assert(import_globals);
  3071. import = import_globals++;
  3072. if (!load_global_import(&p, p_end, module, sub_module_name,
  3073. field_name, &import->u.global,
  3074. error_buf, error_buf_size)) {
  3075. return false;
  3076. }
  3077. break;
  3078. default:
  3079. set_error_buf(error_buf, error_buf_size,
  3080. "invalid import kind");
  3081. return false;
  3082. }
  3083. import->kind = kind;
  3084. import->u.names.module_name = sub_module_name;
  3085. import->u.names.field_name = field_name;
  3086. }
  3087. #if WASM_ENABLE_LIBC_WASI != 0
  3088. import = module->import_functions;
  3089. for (i = 0; i < module->import_function_count; i++, import++) {
  3090. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3091. || !strcmp(import->u.names.module_name,
  3092. "wasi_snapshot_preview1")) {
  3093. module->import_wasi_api = true;
  3094. break;
  3095. }
  3096. }
  3097. #endif
  3098. }
  3099. if (p != p_end) {
  3100. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3101. return false;
  3102. }
  3103. LOG_VERBOSE("Load import section success.\n");
  3104. (void)u8;
  3105. (void)u32;
  3106. (void)type_index;
  3107. return true;
  3108. fail:
  3109. return false;
  3110. }
  3111. static bool
  3112. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3113. uint32 error_buf_size)
  3114. {
  3115. WASMFuncType *param_type = func->func_type;
  3116. uint32 param_count = param_type->param_count;
  3117. uint8 *param_types = param_type->types;
  3118. uint32 local_count = func->local_count;
  3119. uint8 *local_types = func->local_types;
  3120. uint32 i, local_offset = 0;
  3121. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3122. /*
  3123. * Only allocate memory when total_size is not 0,
  3124. * or the return value of malloc(0) might be NULL on some platforms,
  3125. * which causes wasm loader return false.
  3126. */
  3127. if (total_size > 0
  3128. && !(func->local_offsets =
  3129. loader_malloc(total_size, error_buf, error_buf_size))) {
  3130. return false;
  3131. }
  3132. for (i = 0; i < param_count; i++) {
  3133. func->local_offsets[i] = (uint16)local_offset;
  3134. local_offset += wasm_value_type_cell_num(param_types[i]);
  3135. }
  3136. for (i = 0; i < local_count; i++) {
  3137. func->local_offsets[param_count + i] = (uint16)local_offset;
  3138. local_offset += wasm_value_type_cell_num(local_types[i]);
  3139. }
  3140. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3141. return true;
  3142. }
  3143. static bool
  3144. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3145. const uint8 *buf_code, const uint8 *buf_code_end,
  3146. WASMModule *module, char *error_buf,
  3147. uint32 error_buf_size)
  3148. {
  3149. const uint8 *p = buf, *p_end = buf_end;
  3150. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3151. uint32 func_count;
  3152. uint64 total_size;
  3153. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3154. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3155. uint8 type;
  3156. WASMFunction *func;
  3157. #if WASM_ENABLE_GC != 0
  3158. bool need_ref_type_map;
  3159. WASMRefType ref_type;
  3160. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3161. #endif
  3162. read_leb_uint32(p, p_end, func_count);
  3163. if (buf_code)
  3164. read_leb_uint32(p_code, buf_code_end, code_count);
  3165. if (func_count != code_count) {
  3166. set_error_buf(error_buf, error_buf_size,
  3167. "function and code section have inconsistent lengths or "
  3168. "unexpected end");
  3169. return false;
  3170. }
  3171. if (func_count) {
  3172. module->function_count = func_count;
  3173. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3174. if (!(module->functions =
  3175. loader_malloc(total_size, error_buf, error_buf_size))) {
  3176. return false;
  3177. }
  3178. for (i = 0; i < func_count; i++) {
  3179. /* Resolve function type */
  3180. read_leb_uint32(p, p_end, type_index);
  3181. if (type_index >= module->type_count) {
  3182. set_error_buf(error_buf, error_buf_size, "unknown type");
  3183. return false;
  3184. }
  3185. #if WASM_ENABLE_GC != 0
  3186. type_index_org = type_index;
  3187. #endif
  3188. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3189. type_index = wasm_get_smallest_type_idx(
  3190. module->types, module->type_count, type_index);
  3191. #endif
  3192. read_leb_uint32(p_code, buf_code_end, code_size);
  3193. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3194. set_error_buf(error_buf, error_buf_size,
  3195. "invalid function code size");
  3196. return false;
  3197. }
  3198. /* Resolve local set count */
  3199. p_code_end = p_code + code_size;
  3200. local_count = 0;
  3201. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3202. p_code_save = p_code;
  3203. #if WASM_ENABLE_GC != 0
  3204. ref_type_map_count = 0;
  3205. #endif
  3206. /* Calculate total local count */
  3207. for (j = 0; j < local_set_count; j++) {
  3208. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3209. if (sub_local_count > UINT32_MAX - local_count) {
  3210. set_error_buf(error_buf, error_buf_size, "too many locals");
  3211. return false;
  3212. }
  3213. #if WASM_ENABLE_GC == 0
  3214. CHECK_BUF(p_code, buf_code_end, 1);
  3215. /* 0x7F/0x7E/0x7D/0x7C */
  3216. type = read_uint8(p_code);
  3217. local_count += sub_local_count;
  3218. #if WASM_ENABLE_WAMR_COMPILER != 0
  3219. /* If any value's type is v128, mark the module as SIMD used */
  3220. if (type == VALUE_TYPE_V128)
  3221. module->is_simd_used = true;
  3222. #endif
  3223. #else
  3224. if (!resolve_value_type(&p_code, buf_code_end, module,
  3225. &need_ref_type_map, &ref_type, false,
  3226. error_buf, error_buf_size)) {
  3227. return false;
  3228. }
  3229. local_count += sub_local_count;
  3230. if (need_ref_type_map)
  3231. ref_type_map_count += sub_local_count;
  3232. #endif
  3233. }
  3234. /* Alloc memory, layout: function structure + local types */
  3235. code_size = (uint32)(p_code_end - p_code);
  3236. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3237. if (!(func = module->functions[i] =
  3238. loader_malloc(total_size, error_buf, error_buf_size))) {
  3239. return false;
  3240. }
  3241. #if WASM_ENABLE_GC != 0
  3242. if (ref_type_map_count > 0) {
  3243. total_size =
  3244. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3245. if (!(func->local_ref_type_maps = loader_malloc(
  3246. total_size, error_buf, error_buf_size))) {
  3247. return false;
  3248. }
  3249. func->local_ref_type_map_count = ref_type_map_count;
  3250. }
  3251. #endif
  3252. /* Set function type, local count, code size and code body */
  3253. func->func_type = (WASMFuncType *)module->types[type_index];
  3254. func->local_count = local_count;
  3255. if (local_count > 0)
  3256. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3257. func->code_size = code_size;
  3258. /*
  3259. * we shall make a copy of code body [p_code, p_code + code_size]
  3260. * when we are worrying about inappropriate releasing behaviour.
  3261. * all code bodies are actually in a buffer which user allocates in
  3262. * his embedding environment and we don't have power on them.
  3263. * it will be like:
  3264. * code_body_cp = malloc(code_size);
  3265. * memcpy(code_body_cp, p_code, code_size);
  3266. * func->code = code_body_cp;
  3267. */
  3268. func->code = (uint8 *)p_code;
  3269. #if WASM_ENABLE_GC != 0
  3270. func->type_idx = type_index_org;
  3271. #endif
  3272. #if WASM_ENABLE_GC != 0
  3273. t = 0;
  3274. #endif
  3275. /* Load each local type */
  3276. p_code = p_code_save;
  3277. local_type_index = 0;
  3278. for (j = 0; j < local_set_count; j++) {
  3279. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3280. /* Note: sub_local_count is allowed to be 0 */
  3281. if (local_type_index > UINT32_MAX - sub_local_count
  3282. || local_type_index + sub_local_count > local_count) {
  3283. set_error_buf(error_buf, error_buf_size,
  3284. "invalid local count");
  3285. return false;
  3286. }
  3287. #if WASM_ENABLE_GC == 0
  3288. CHECK_BUF(p_code, buf_code_end, 1);
  3289. /* 0x7F/0x7E/0x7D/0x7C */
  3290. type = read_uint8(p_code);
  3291. if (!is_value_type(type)) {
  3292. if (type == VALUE_TYPE_V128)
  3293. set_error_buf(error_buf, error_buf_size,
  3294. "v128 value type requires simd feature");
  3295. else if (type == VALUE_TYPE_FUNCREF
  3296. || type == VALUE_TYPE_EXTERNREF)
  3297. set_error_buf(error_buf, error_buf_size,
  3298. "ref value type requires "
  3299. "reference types feature");
  3300. else
  3301. set_error_buf_v(error_buf, error_buf_size,
  3302. "invalid local type 0x%02X", type);
  3303. return false;
  3304. }
  3305. #else
  3306. if (!resolve_value_type(&p_code, buf_code_end, module,
  3307. &need_ref_type_map, &ref_type, false,
  3308. error_buf, error_buf_size)) {
  3309. return false;
  3310. }
  3311. if (need_ref_type_map) {
  3312. WASMRefType *ref_type_tmp;
  3313. if (!(ref_type_tmp = reftype_set_insert(
  3314. module->ref_type_set, &ref_type, error_buf,
  3315. error_buf_size))) {
  3316. return false;
  3317. }
  3318. for (k = 0; k < sub_local_count; k++) {
  3319. func->local_ref_type_maps[t + k].ref_type =
  3320. ref_type_tmp;
  3321. func->local_ref_type_maps[t + k].index =
  3322. local_type_index + k;
  3323. }
  3324. t += sub_local_count;
  3325. }
  3326. type = ref_type.ref_type;
  3327. #endif
  3328. for (k = 0; k < sub_local_count; k++) {
  3329. func->local_types[local_type_index++] = type;
  3330. }
  3331. #if WASM_ENABLE_WAMR_COMPILER != 0
  3332. if (type == VALUE_TYPE_V128)
  3333. module->is_simd_used = true;
  3334. else if (type == VALUE_TYPE_FUNCREF
  3335. || type == VALUE_TYPE_EXTERNREF)
  3336. module->is_ref_types_used = true;
  3337. #endif
  3338. }
  3339. bh_assert(local_type_index == func->local_count);
  3340. #if WASM_ENABLE_GC != 0
  3341. bh_assert(t == func->local_ref_type_map_count);
  3342. #if TRACE_WASM_LOADER != 0
  3343. os_printf("func %u, local types: [", i);
  3344. k = 0;
  3345. for (j = 0; j < func->local_count; j++) {
  3346. WASMRefType *ref_type_tmp = NULL;
  3347. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3348. bh_assert(j == func->local_ref_type_maps[k].index);
  3349. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3350. }
  3351. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3352. if (j < func->local_count - 1)
  3353. os_printf(" ");
  3354. }
  3355. os_printf("]\n");
  3356. #endif
  3357. #endif
  3358. func->param_cell_num = func->func_type->param_cell_num;
  3359. func->ret_cell_num = func->func_type->ret_cell_num;
  3360. local_cell_num =
  3361. wasm_get_cell_num(func->local_types, func->local_count);
  3362. if (local_cell_num > UINT16_MAX) {
  3363. set_error_buf(error_buf, error_buf_size,
  3364. "local count too large");
  3365. return false;
  3366. }
  3367. func->local_cell_num = (uint16)local_cell_num;
  3368. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3369. return false;
  3370. p_code = p_code_end;
  3371. }
  3372. }
  3373. if (p != p_end) {
  3374. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3375. return false;
  3376. }
  3377. LOG_VERBOSE("Load function section success.\n");
  3378. return true;
  3379. fail:
  3380. return false;
  3381. }
  3382. static bool
  3383. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3384. char *error_buf, uint32 error_buf_size)
  3385. {
  3386. const uint8 *p = buf, *p_end = buf_end;
  3387. uint32 table_count, i;
  3388. uint64 total_size;
  3389. WASMTable *table;
  3390. read_leb_uint32(p, p_end, table_count);
  3391. if (module->import_table_count + table_count > 1) {
  3392. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3393. /* a total of one table is allowed */
  3394. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3395. return false;
  3396. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3397. module->is_ref_types_used = true;
  3398. #endif
  3399. }
  3400. if (table_count) {
  3401. module->table_count = table_count;
  3402. total_size = sizeof(WASMTable) * (uint64)table_count;
  3403. if (!(module->tables =
  3404. loader_malloc(total_size, error_buf, error_buf_size))) {
  3405. return false;
  3406. }
  3407. /* load each table */
  3408. table = module->tables;
  3409. for (i = 0; i < table_count; i++, table++) {
  3410. #if WASM_ENABLE_GC != 0
  3411. uint8 flag;
  3412. bool has_init = false;
  3413. CHECK_BUF(buf, buf_end, 1);
  3414. flag = read_uint8(p);
  3415. if (flag == TABLE_INIT_EXPR_FLAG) {
  3416. CHECK_BUF(buf, buf_end, 1);
  3417. flag = read_uint8(p);
  3418. if (flag != 0x00) {
  3419. set_error_buf(error_buf, error_buf_size,
  3420. "invalid leading bytes for table");
  3421. return false;
  3422. }
  3423. has_init = true;
  3424. }
  3425. else {
  3426. p--;
  3427. }
  3428. #endif /* end of WASM_ENABLE_GC != 0 */
  3429. if (!load_table(&p, p_end, module, table, error_buf,
  3430. error_buf_size))
  3431. return false;
  3432. #if WASM_ENABLE_GC != 0
  3433. if (has_init) {
  3434. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3435. table->elem_type, table->elem_ref_type,
  3436. error_buf, error_buf_size))
  3437. return false;
  3438. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3439. && table->init_expr.init_expr_type
  3440. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3441. set_error_buf(
  3442. error_buf, error_buf_size,
  3443. "unsupported initializer expression for table");
  3444. return false;
  3445. }
  3446. }
  3447. else {
  3448. if (wasm_is_reftype_htref_non_nullable(table->elem_type)) {
  3449. set_error_buf(
  3450. error_buf, error_buf_size,
  3451. "type mismatch: non-nullable table without init expr");
  3452. return false;
  3453. }
  3454. }
  3455. #endif /* end of WASM_ENABLE_GC != 0 */
  3456. #if WASM_ENABLE_WAMR_COMPILER != 0
  3457. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  3458. module->is_ref_types_used = true;
  3459. #endif
  3460. }
  3461. }
  3462. if (p != p_end) {
  3463. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3464. return false;
  3465. }
  3466. LOG_VERBOSE("Load table section success.\n");
  3467. return true;
  3468. fail:
  3469. return false;
  3470. }
  3471. static bool
  3472. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3473. char *error_buf, uint32 error_buf_size)
  3474. {
  3475. const uint8 *p = buf, *p_end = buf_end;
  3476. uint32 memory_count, i;
  3477. uint64 total_size;
  3478. WASMMemory *memory;
  3479. read_leb_uint32(p, p_end, memory_count);
  3480. /* a total of one memory is allowed */
  3481. if (module->import_memory_count + memory_count > 1) {
  3482. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3483. return false;
  3484. }
  3485. if (memory_count) {
  3486. module->memory_count = memory_count;
  3487. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3488. if (!(module->memories =
  3489. loader_malloc(total_size, error_buf, error_buf_size))) {
  3490. return false;
  3491. }
  3492. /* load each memory */
  3493. memory = module->memories;
  3494. for (i = 0; i < memory_count; i++, memory++)
  3495. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3496. return false;
  3497. }
  3498. if (p != p_end) {
  3499. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3500. return false;
  3501. }
  3502. LOG_VERBOSE("Load memory section success.\n");
  3503. return true;
  3504. fail:
  3505. return false;
  3506. }
  3507. static bool
  3508. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3509. char *error_buf, uint32 error_buf_size)
  3510. {
  3511. const uint8 *p = buf, *p_end = buf_end;
  3512. uint32 global_count, i;
  3513. uint64 total_size;
  3514. WASMGlobal *global;
  3515. uint8 mutable;
  3516. #if WASM_ENABLE_GC != 0
  3517. bool need_ref_type_map;
  3518. WASMRefType ref_type;
  3519. #endif
  3520. read_leb_uint32(p, p_end, global_count);
  3521. module->global_count = 0;
  3522. if (global_count) {
  3523. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3524. if (!(module->globals =
  3525. loader_malloc(total_size, error_buf, error_buf_size))) {
  3526. return false;
  3527. }
  3528. global = module->globals;
  3529. for (i = 0; i < global_count; i++, global++) {
  3530. #if WASM_ENABLE_GC == 0
  3531. CHECK_BUF(p, p_end, 2);
  3532. global->type = read_uint8(p);
  3533. mutable = read_uint8(p);
  3534. #else
  3535. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  3536. &ref_type, false, error_buf,
  3537. error_buf_size)) {
  3538. return false;
  3539. }
  3540. global->type = ref_type.ref_type;
  3541. CHECK_BUF(p, p_end, 1);
  3542. mutable = read_uint8(p);
  3543. #endif /* end of WASM_ENABLE_GC */
  3544. #if WASM_ENABLE_WAMR_COMPILER != 0
  3545. if (global->type == VALUE_TYPE_V128)
  3546. module->is_simd_used = true;
  3547. else if (global->type == VALUE_TYPE_FUNCREF
  3548. || global->type == VALUE_TYPE_EXTERNREF)
  3549. module->is_ref_types_used = true;
  3550. #endif
  3551. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3552. return false;
  3553. }
  3554. global->is_mutable = mutable ? true : false;
  3555. /* initialize expression */
  3556. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3557. global->type,
  3558. #if WASM_ENABLE_GC == 0
  3559. NULL,
  3560. #else
  3561. &ref_type,
  3562. #endif
  3563. error_buf, error_buf_size))
  3564. return false;
  3565. #if WASM_ENABLE_GC != 0
  3566. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3567. uint8 global_type;
  3568. WASMRefType *global_ref_type;
  3569. uint32 global_idx = global->init_expr.u.global_index;
  3570. if (global->init_expr.u.global_index
  3571. >= module->import_global_count + i) {
  3572. set_error_buf(error_buf, error_buf_size, "unknown global");
  3573. return false;
  3574. }
  3575. if (global_idx < module->import_global_count) {
  3576. global_type =
  3577. module->import_globals[global_idx].u.global.type;
  3578. global_ref_type =
  3579. module->import_globals[global_idx].u.global.ref_type;
  3580. }
  3581. else {
  3582. global_type =
  3583. module
  3584. ->globals[global_idx - module->import_global_count]
  3585. .type;
  3586. global_ref_type =
  3587. module
  3588. ->globals[global_idx - module->import_global_count]
  3589. .ref_type;
  3590. }
  3591. if (!wasm_reftype_is_subtype_of(
  3592. global_type, global_ref_type, global->type,
  3593. global->ref_type, module->types, module->type_count)) {
  3594. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3595. return false;
  3596. }
  3597. }
  3598. if (need_ref_type_map) {
  3599. if (!(global->ref_type =
  3600. reftype_set_insert(module->ref_type_set, &ref_type,
  3601. error_buf, error_buf_size))) {
  3602. return false;
  3603. }
  3604. }
  3605. #if TRACE_WASM_LOADER != 0
  3606. os_printf("global type: ");
  3607. wasm_dump_value_type(global->type, global->ref_type);
  3608. os_printf("\n");
  3609. #endif
  3610. #endif
  3611. module->global_count++;
  3612. }
  3613. bh_assert(module->global_count == global_count);
  3614. }
  3615. if (p != p_end) {
  3616. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3617. return false;
  3618. }
  3619. LOG_VERBOSE("Load global section success.\n");
  3620. return true;
  3621. fail:
  3622. return false;
  3623. }
  3624. static bool
  3625. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3626. bool is_load_from_file_buf, char *error_buf,
  3627. uint32 error_buf_size)
  3628. {
  3629. const uint8 *p = buf, *p_end = buf_end;
  3630. uint32 export_count, i, j, index;
  3631. uint64 total_size;
  3632. uint32 str_len;
  3633. WASMExport *export;
  3634. const char *name;
  3635. read_leb_uint32(p, p_end, export_count);
  3636. if (export_count) {
  3637. module->export_count = export_count;
  3638. total_size = sizeof(WASMExport) * (uint64)export_count;
  3639. if (!(module->exports =
  3640. loader_malloc(total_size, error_buf, error_buf_size))) {
  3641. return false;
  3642. }
  3643. export = module->exports;
  3644. for (i = 0; i < export_count; i++, export ++) {
  3645. #if WASM_ENABLE_THREAD_MGR == 0
  3646. if (p == p_end) {
  3647. /* export section with inconsistent count:
  3648. n export declared, but less than n given */
  3649. set_error_buf(error_buf, error_buf_size,
  3650. "length out of bounds");
  3651. return false;
  3652. }
  3653. #endif
  3654. read_leb_uint32(p, p_end, str_len);
  3655. CHECK_BUF(p, p_end, str_len);
  3656. for (j = 0; j < i; j++) {
  3657. name = module->exports[j].name;
  3658. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3659. set_error_buf(error_buf, error_buf_size,
  3660. "duplicate export name");
  3661. return false;
  3662. }
  3663. }
  3664. if (!(export->name = const_str_list_insert(
  3665. p, str_len, module, is_load_from_file_buf, error_buf,
  3666. error_buf_size))) {
  3667. return false;
  3668. }
  3669. p += str_len;
  3670. CHECK_BUF(p, p_end, 1);
  3671. export->kind = read_uint8(p);
  3672. read_leb_uint32(p, p_end, index);
  3673. export->index = index;
  3674. switch (export->kind) {
  3675. /* function index */
  3676. case EXPORT_KIND_FUNC:
  3677. if (index >= module->function_count
  3678. + module->import_function_count) {
  3679. set_error_buf(error_buf, error_buf_size,
  3680. "unknown function");
  3681. return false;
  3682. }
  3683. #if WASM_ENABLE_SIMD != 0
  3684. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3685. /* TODO: check func type, if it has v128 param or result,
  3686. report error */
  3687. #endif
  3688. #endif
  3689. break;
  3690. /* table index */
  3691. case EXPORT_KIND_TABLE:
  3692. if (index
  3693. >= module->table_count + module->import_table_count) {
  3694. set_error_buf(error_buf, error_buf_size,
  3695. "unknown table");
  3696. return false;
  3697. }
  3698. break;
  3699. /* memory index */
  3700. case EXPORT_KIND_MEMORY:
  3701. if (index
  3702. >= module->memory_count + module->import_memory_count) {
  3703. set_error_buf(error_buf, error_buf_size,
  3704. "unknown memory");
  3705. return false;
  3706. }
  3707. break;
  3708. #if WASM_ENABLE_TAGS != 0
  3709. /* export tag */
  3710. case EXPORT_KIND_TAG:
  3711. if (index >= module->tag_count + module->import_tag_count) {
  3712. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3713. return false;
  3714. }
  3715. break;
  3716. #endif
  3717. /* global index */
  3718. case EXPORT_KIND_GLOBAL:
  3719. if (index
  3720. >= module->global_count + module->import_global_count) {
  3721. set_error_buf(error_buf, error_buf_size,
  3722. "unknown global");
  3723. return false;
  3724. }
  3725. break;
  3726. default:
  3727. set_error_buf(error_buf, error_buf_size,
  3728. "invalid export kind");
  3729. return false;
  3730. }
  3731. }
  3732. }
  3733. if (p != p_end) {
  3734. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3735. return false;
  3736. }
  3737. LOG_VERBOSE("Load export section success.\n");
  3738. return true;
  3739. fail:
  3740. return false;
  3741. }
  3742. static bool
  3743. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3744. uint32 error_buf_size)
  3745. {
  3746. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3747. if (table_index != 0) {
  3748. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3749. return false;
  3750. }
  3751. #endif
  3752. if (table_index >= module->import_table_count + module->table_count) {
  3753. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3754. table_index);
  3755. return false;
  3756. }
  3757. return true;
  3758. }
  3759. static bool
  3760. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3761. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3762. {
  3763. const uint8 *p = *p_buf, *p_end = buf_end;
  3764. uint32 table_index;
  3765. read_leb_uint32(p, p_end, table_index);
  3766. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3767. return false;
  3768. }
  3769. *p_table_index = table_index;
  3770. *p_buf = p;
  3771. return true;
  3772. fail:
  3773. return false;
  3774. }
  3775. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3776. static bool
  3777. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3778. uint32 *p_elem_type,
  3779. #if WASM_ENABLE_GC != 0
  3780. WASMRefType **p_elem_ref_type,
  3781. #endif
  3782. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3783. {
  3784. const uint8 *p = *p_buf, *p_end = buf_end;
  3785. uint8 elem_type;
  3786. #if WASM_ENABLE_GC != 0
  3787. WASMRefType elem_ref_type;
  3788. bool need_ref_type_map;
  3789. #endif
  3790. CHECK_BUF(p, p_end, 1);
  3791. elem_type = read_uint8(p);
  3792. if (elemkind_zero) {
  3793. if (elem_type != 0) {
  3794. set_error_buf(error_buf, error_buf_size,
  3795. "invalid reference type or unknown type");
  3796. return false;
  3797. }
  3798. else {
  3799. *p_elem_type = VALUE_TYPE_FUNCREF;
  3800. *p_buf = p;
  3801. return true;
  3802. }
  3803. }
  3804. #if WASM_ENABLE_GC == 0
  3805. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3806. set_error_buf(error_buf, error_buf_size,
  3807. "invalid reference type or unknown type");
  3808. return false;
  3809. }
  3810. *p_elem_type = elem_type;
  3811. #else
  3812. p--;
  3813. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3814. &need_ref_type_map, &elem_ref_type, false,
  3815. error_buf, error_buf_size)) {
  3816. return false;
  3817. }
  3818. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3819. set_error_buf(error_buf, error_buf_size,
  3820. "invalid reference type or unknown type");
  3821. return false;
  3822. }
  3823. *p_elem_type = elem_ref_type.ref_type;
  3824. if (need_ref_type_map) {
  3825. if (!(*p_elem_ref_type =
  3826. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3827. error_buf, error_buf_size))) {
  3828. return false;
  3829. }
  3830. }
  3831. #endif
  3832. *p_buf = p;
  3833. return true;
  3834. fail:
  3835. return false;
  3836. }
  3837. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3838. static bool
  3839. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3840. WASMModule *module, WASMTableSeg *table_segment,
  3841. char *error_buf, uint32 error_buf_size)
  3842. {
  3843. const uint8 *p = *p_buf, *p_end = buf_end;
  3844. uint32 function_count, function_index = 0, i;
  3845. uint64 total_size;
  3846. read_leb_uint32(p, p_end, function_count);
  3847. table_segment->value_count = function_count;
  3848. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3849. if (total_size > 0
  3850. && !(table_segment->init_values =
  3851. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3852. error_buf_size))) {
  3853. return false;
  3854. }
  3855. for (i = 0; i < function_count; i++) {
  3856. InitializerExpression *init_expr = &table_segment->init_values[i];
  3857. read_leb_uint32(p, p_end, function_index);
  3858. if (!check_function_index(module, function_index, error_buf,
  3859. error_buf_size)) {
  3860. return false;
  3861. }
  3862. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3863. init_expr->u.ref_index = function_index;
  3864. }
  3865. *p_buf = p;
  3866. return true;
  3867. fail:
  3868. return false;
  3869. }
  3870. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3871. static bool
  3872. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3873. WASMModule *module, WASMTableSeg *table_segment,
  3874. char *error_buf, uint32 error_buf_size)
  3875. {
  3876. const uint8 *p = *p_buf, *p_end = buf_end;
  3877. uint32 ref_count, i;
  3878. uint64 total_size;
  3879. read_leb_uint32(p, p_end, ref_count);
  3880. table_segment->value_count = ref_count;
  3881. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3882. if (total_size > 0
  3883. && !(table_segment->init_values =
  3884. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3885. error_buf_size))) {
  3886. return false;
  3887. }
  3888. for (i = 0; i < ref_count; i++) {
  3889. InitializerExpression *init_expr = &table_segment->init_values[i];
  3890. if (!load_init_expr(module, &p, p_end, init_expr,
  3891. table_segment->elem_type,
  3892. #if WASM_ENABLE_GC == 0
  3893. NULL,
  3894. #else
  3895. table_segment->elem_ref_type,
  3896. #endif
  3897. error_buf, error_buf_size))
  3898. return false;
  3899. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3900. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3901. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3902. && init_expr->init_expr_type
  3903. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3904. }
  3905. *p_buf = p;
  3906. return true;
  3907. fail:
  3908. return false;
  3909. }
  3910. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3911. static bool
  3912. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3913. WASMModule *module, char *error_buf,
  3914. uint32 error_buf_size)
  3915. {
  3916. const uint8 *p = buf, *p_end = buf_end;
  3917. uint32 table_segment_count, i;
  3918. uint64 total_size;
  3919. WASMTableSeg *table_segment;
  3920. read_leb_uint32(p, p_end, table_segment_count);
  3921. if (table_segment_count) {
  3922. module->table_seg_count = table_segment_count;
  3923. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3924. if (!(module->table_segments =
  3925. loader_malloc(total_size, error_buf, error_buf_size))) {
  3926. return false;
  3927. }
  3928. table_segment = module->table_segments;
  3929. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3930. if (p >= p_end) {
  3931. set_error_buf(error_buf, error_buf_size,
  3932. "invalid value type or "
  3933. "invalid elements segment kind");
  3934. return false;
  3935. }
  3936. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3937. read_leb_uint32(p, p_end, table_segment->mode);
  3938. /* last three bits */
  3939. table_segment->mode = table_segment->mode & 0x07;
  3940. switch (table_segment->mode) {
  3941. /* elemkind/elemtype + active */
  3942. case 0:
  3943. case 4:
  3944. {
  3945. #if WASM_ENABLE_GC != 0
  3946. if (table_segment->mode == 0) {
  3947. /* vec(funcidx), set elem type to (ref func) */
  3948. WASMRefType elem_ref_type = { 0 };
  3949. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  3950. wasm_set_refheaptype_common(
  3951. &elem_ref_type.ref_ht_common, false,
  3952. HEAP_TYPE_FUNC);
  3953. if (!(table_segment->elem_ref_type = reftype_set_insert(
  3954. module->ref_type_set, &elem_ref_type,
  3955. error_buf, error_buf_size)))
  3956. return false;
  3957. }
  3958. else {
  3959. /* vec(expr), set elem type to funcref */
  3960. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3961. }
  3962. #else
  3963. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3964. #endif
  3965. table_segment->table_index = 0;
  3966. if (!check_table_index(module, table_segment->table_index,
  3967. error_buf, error_buf_size))
  3968. return false;
  3969. if (!load_init_expr(
  3970. module, &p, p_end, &table_segment->base_offset,
  3971. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3972. return false;
  3973. if (table_segment->mode == 0) {
  3974. /* vec(funcidx) */
  3975. if (!load_func_index_vec(&p, p_end, module,
  3976. table_segment, error_buf,
  3977. error_buf_size))
  3978. return false;
  3979. }
  3980. else {
  3981. /* vec(expr) */
  3982. if (!load_init_expr_vec(&p, p_end, module,
  3983. table_segment, error_buf,
  3984. error_buf_size))
  3985. return false;
  3986. }
  3987. break;
  3988. }
  3989. /* elemkind + passive/declarative */
  3990. case 1:
  3991. case 3:
  3992. if (!load_elem_type(module, &p, p_end,
  3993. &table_segment->elem_type,
  3994. #if WASM_ENABLE_GC != 0
  3995. &table_segment->elem_ref_type,
  3996. #endif
  3997. true, error_buf, error_buf_size))
  3998. return false;
  3999. /* vec(funcidx) */
  4000. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4001. error_buf, error_buf_size))
  4002. return false;
  4003. break;
  4004. /* elemkind/elemtype + table_idx + active */
  4005. case 2:
  4006. case 6:
  4007. if (!load_table_index(&p, p_end, module,
  4008. &table_segment->table_index,
  4009. error_buf, error_buf_size))
  4010. return false;
  4011. if (!load_init_expr(
  4012. module, &p, p_end, &table_segment->base_offset,
  4013. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4014. return false;
  4015. if (!load_elem_type(module, &p, p_end,
  4016. &table_segment->elem_type,
  4017. #if WASM_ENABLE_GC != 0
  4018. &table_segment->elem_ref_type,
  4019. #endif
  4020. table_segment->mode == 2 ? true : false,
  4021. error_buf, error_buf_size))
  4022. return false;
  4023. if (table_segment->mode == 2) {
  4024. /* vec(funcidx) */
  4025. if (!load_func_index_vec(&p, p_end, module,
  4026. table_segment, error_buf,
  4027. error_buf_size))
  4028. return false;
  4029. }
  4030. else {
  4031. /* vec(expr) */
  4032. if (!load_init_expr_vec(&p, p_end, module,
  4033. table_segment, error_buf,
  4034. error_buf_size))
  4035. return false;
  4036. }
  4037. break;
  4038. case 5:
  4039. case 7:
  4040. if (!load_elem_type(module, &p, p_end,
  4041. &table_segment->elem_type,
  4042. #if WASM_ENABLE_GC != 0
  4043. &table_segment->elem_ref_type,
  4044. #endif
  4045. false, error_buf, error_buf_size))
  4046. return false;
  4047. /* vec(expr) */
  4048. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4049. error_buf, error_buf_size))
  4050. return false;
  4051. break;
  4052. default:
  4053. set_error_buf(error_buf, error_buf_size,
  4054. "unknown element segment kind");
  4055. return false;
  4056. }
  4057. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4058. /*
  4059. * like: 00 41 05 0b 04 00 01 00 01
  4060. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4061. */
  4062. if (!load_table_index(&p, p_end, module,
  4063. &table_segment->table_index, error_buf,
  4064. error_buf_size))
  4065. return false;
  4066. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4067. VALUE_TYPE_I32, NULL, error_buf,
  4068. error_buf_size))
  4069. return false;
  4070. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4071. error_buf, error_buf_size))
  4072. return false;
  4073. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4074. #if WASM_ENABLE_WAMR_COMPILER != 0
  4075. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4076. module->is_ref_types_used = true;
  4077. #endif
  4078. }
  4079. }
  4080. if (p != p_end) {
  4081. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4082. return false;
  4083. }
  4084. LOG_VERBOSE("Load table segment section success.\n");
  4085. return true;
  4086. fail:
  4087. return false;
  4088. }
  4089. static bool
  4090. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4091. WASMModule *module, char *error_buf,
  4092. uint32 error_buf_size)
  4093. {
  4094. const uint8 *p = buf, *p_end = buf_end;
  4095. uint32 data_seg_count, i, mem_index, data_seg_len;
  4096. uint64 total_size;
  4097. WASMDataSeg *dataseg;
  4098. InitializerExpression init_expr;
  4099. #if WASM_ENABLE_BULK_MEMORY != 0
  4100. bool is_passive = false;
  4101. uint32 mem_flag;
  4102. #endif
  4103. read_leb_uint32(p, p_end, data_seg_count);
  4104. #if WASM_ENABLE_BULK_MEMORY != 0
  4105. if ((module->data_seg_count1 != 0)
  4106. && (data_seg_count != module->data_seg_count1)) {
  4107. set_error_buf(error_buf, error_buf_size,
  4108. "data count and data section have inconsistent lengths");
  4109. return false;
  4110. }
  4111. #endif
  4112. if (data_seg_count) {
  4113. module->data_seg_count = data_seg_count;
  4114. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4115. if (!(module->data_segments =
  4116. loader_malloc(total_size, error_buf, error_buf_size))) {
  4117. return false;
  4118. }
  4119. for (i = 0; i < data_seg_count; i++) {
  4120. read_leb_uint32(p, p_end, mem_index);
  4121. #if WASM_ENABLE_BULK_MEMORY != 0
  4122. is_passive = false;
  4123. mem_flag = mem_index & 0x03;
  4124. switch (mem_flag) {
  4125. case 0x01:
  4126. is_passive = true;
  4127. #if WASM_ENABLE_WAMR_COMPILER != 0
  4128. module->is_bulk_memory_used = true;
  4129. #endif
  4130. break;
  4131. case 0x00:
  4132. /* no memory index, treat index as 0 */
  4133. mem_index = 0;
  4134. goto check_mem_index;
  4135. case 0x02:
  4136. /* read following memory index */
  4137. read_leb_uint32(p, p_end, mem_index);
  4138. #if WASM_ENABLE_WAMR_COMPILER != 0
  4139. module->is_bulk_memory_used = true;
  4140. #endif
  4141. check_mem_index:
  4142. if (mem_index
  4143. >= module->import_memory_count + module->memory_count) {
  4144. set_error_buf_v(error_buf, error_buf_size,
  4145. "unknown memory %d", mem_index);
  4146. return false;
  4147. }
  4148. break;
  4149. case 0x03:
  4150. default:
  4151. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4152. return false;
  4153. break;
  4154. }
  4155. #else
  4156. if (mem_index
  4157. >= module->import_memory_count + module->memory_count) {
  4158. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4159. mem_index);
  4160. return false;
  4161. }
  4162. #endif /* WASM_ENABLE_BULK_MEMORY */
  4163. #if WASM_ENABLE_BULK_MEMORY != 0
  4164. if (!is_passive)
  4165. #endif
  4166. if (!load_init_expr(module, &p, p_end, &init_expr,
  4167. VALUE_TYPE_I32, NULL, error_buf,
  4168. error_buf_size))
  4169. return false;
  4170. read_leb_uint32(p, p_end, data_seg_len);
  4171. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4172. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4173. return false;
  4174. }
  4175. #if WASM_ENABLE_BULK_MEMORY != 0
  4176. dataseg->is_passive = is_passive;
  4177. if (!is_passive)
  4178. #endif
  4179. {
  4180. bh_memcpy_s(&dataseg->base_offset,
  4181. sizeof(InitializerExpression), &init_expr,
  4182. sizeof(InitializerExpression));
  4183. dataseg->memory_index = mem_index;
  4184. }
  4185. dataseg->data_length = data_seg_len;
  4186. CHECK_BUF(p, p_end, data_seg_len);
  4187. dataseg->data = (uint8 *)p;
  4188. p += data_seg_len;
  4189. }
  4190. }
  4191. if (p != p_end) {
  4192. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4193. return false;
  4194. }
  4195. LOG_VERBOSE("Load data segment section success.\n");
  4196. return true;
  4197. fail:
  4198. return false;
  4199. }
  4200. #if WASM_ENABLE_BULK_MEMORY != 0
  4201. static bool
  4202. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4203. WASMModule *module, char *error_buf,
  4204. uint32 error_buf_size)
  4205. {
  4206. const uint8 *p = buf, *p_end = buf_end;
  4207. uint32 data_seg_count1 = 0;
  4208. read_leb_uint32(p, p_end, data_seg_count1);
  4209. module->data_seg_count1 = data_seg_count1;
  4210. if (p != p_end) {
  4211. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4212. return false;
  4213. }
  4214. #if WASM_ENABLE_WAMR_COMPILER != 0
  4215. module->is_bulk_memory_used = true;
  4216. #endif
  4217. LOG_VERBOSE("Load datacount section success.\n");
  4218. return true;
  4219. fail:
  4220. return false;
  4221. }
  4222. #endif
  4223. #if WASM_ENABLE_TAGS != 0
  4224. static bool
  4225. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4226. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4227. uint32 error_buf_size)
  4228. {
  4229. (void)buf_code;
  4230. (void)buf_code_end;
  4231. const uint8 *p = buf, *p_end = buf_end;
  4232. size_t total_size = 0;
  4233. /* number of tags defined in the section */
  4234. uint32 section_tag_count = 0;
  4235. uint8 tag_attribute;
  4236. uint32 tag_type;
  4237. WASMTag *tag = NULL;
  4238. /* get tag count */
  4239. read_leb_uint32(p, p_end, section_tag_count);
  4240. module->tag_count = section_tag_count;
  4241. if (section_tag_count) {
  4242. total_size = sizeof(WASMTag *) * module->tag_count;
  4243. if (!(module->tags =
  4244. loader_malloc(total_size, error_buf, error_buf_size))) {
  4245. return false;
  4246. }
  4247. /* load each tag, imported tags precede the tags */
  4248. uint32 tag_index;
  4249. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4250. /* get the one byte attribute */
  4251. CHECK_BUF(p, p_end, 1);
  4252. tag_attribute = read_uint8(p);
  4253. /* get type */
  4254. read_leb_uint32(p, p_end, tag_type);
  4255. /* compare against module->types */
  4256. if (tag_type >= module->type_count) {
  4257. set_error_buf(error_buf, error_buf_size, "unknown type");
  4258. return false;
  4259. }
  4260. /* get return type (must be 0) */
  4261. /* check, that the type of the referred tag returns void */
  4262. WASMType *func_type = (WASMType *)module->types[tag_type];
  4263. if (func_type->result_count != 0) {
  4264. set_error_buf(error_buf, error_buf_size,
  4265. "non-empty tag result type");
  4266. goto fail;
  4267. }
  4268. if (!(tag = module->tags[tag_index] = loader_malloc(
  4269. sizeof(WASMTag), error_buf, error_buf_size))) {
  4270. return false;
  4271. }
  4272. /* store to module tag declarations */
  4273. tag->attribute = tag_attribute;
  4274. tag->type = tag_type;
  4275. tag->tag_type = func_type;
  4276. }
  4277. }
  4278. if (p != p_end) {
  4279. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4280. return false;
  4281. }
  4282. LOG_VERBOSE("Load tag section success.\n");
  4283. return true;
  4284. fail:
  4285. return false;
  4286. }
  4287. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4288. static bool
  4289. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4290. const uint8 *buf_func_end, WASMModule *module,
  4291. char *error_buf, uint32 error_buf_size)
  4292. {
  4293. const uint8 *p = buf, *p_end = buf_end;
  4294. const uint8 *p_func = buf_func;
  4295. uint32 func_count = 0, code_count;
  4296. /* code has been loaded in function section, so pass it here, just check
  4297. * whether function and code section have inconsistent lengths */
  4298. read_leb_uint32(p, p_end, code_count);
  4299. if (buf_func)
  4300. read_leb_uint32(p_func, buf_func_end, func_count);
  4301. if (func_count != code_count) {
  4302. set_error_buf(error_buf, error_buf_size,
  4303. "function and code section have inconsistent lengths");
  4304. return false;
  4305. }
  4306. LOG_VERBOSE("Load code segment section success.\n");
  4307. (void)module;
  4308. return true;
  4309. fail:
  4310. return false;
  4311. }
  4312. static bool
  4313. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4314. char *error_buf, uint32 error_buf_size)
  4315. {
  4316. const uint8 *p = buf, *p_end = buf_end;
  4317. WASMFuncType *type;
  4318. uint32 start_function;
  4319. read_leb_uint32(p, p_end, start_function);
  4320. if (start_function
  4321. >= module->function_count + module->import_function_count) {
  4322. set_error_buf(error_buf, error_buf_size, "unknown function");
  4323. return false;
  4324. }
  4325. if (start_function < module->import_function_count)
  4326. type = module->import_functions[start_function].u.function.func_type;
  4327. else
  4328. type = module->functions[start_function - module->import_function_count]
  4329. ->func_type;
  4330. if (type->param_count != 0 || type->result_count != 0) {
  4331. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4332. return false;
  4333. }
  4334. module->start_function = start_function;
  4335. if (p != p_end) {
  4336. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4337. return false;
  4338. }
  4339. LOG_VERBOSE("Load start section success.\n");
  4340. return true;
  4341. fail:
  4342. return false;
  4343. }
  4344. #if WASM_ENABLE_STRINGREF != 0
  4345. static bool
  4346. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4347. WASMModule *module, bool is_load_from_file_buf,
  4348. char *error_buf, uint32 error_buf_size)
  4349. {
  4350. const uint8 *p = buf, *p_end = buf_end;
  4351. int32 deferred_count, immediate_count, string_length, i;
  4352. uint64 total_size;
  4353. read_leb_uint32(p, p_end, deferred_count);
  4354. read_leb_uint32(p, p_end, immediate_count);
  4355. /* proposal set deferred_count for future extension */
  4356. if (deferred_count != 0) {
  4357. goto fail;
  4358. }
  4359. if (immediate_count > 0) {
  4360. total_size = sizeof(char *) * (uint64)immediate_count;
  4361. if (!(module->string_literal_ptrs =
  4362. loader_malloc(total_size, error_buf, error_buf_size))) {
  4363. goto fail;
  4364. }
  4365. module->string_literal_count = immediate_count;
  4366. total_size = sizeof(uint32) * (uint64)immediate_count;
  4367. if (!(module->string_literal_lengths =
  4368. loader_malloc(total_size, error_buf, error_buf_size))) {
  4369. goto fail;
  4370. }
  4371. for (i = 0; i < immediate_count; i++) {
  4372. read_leb_uint32(p, p_end, string_length);
  4373. CHECK_BUF(p, p_end, string_length);
  4374. module->string_literal_ptrs[i] = p;
  4375. module->string_literal_lengths[i] = string_length;
  4376. p += string_length;
  4377. }
  4378. }
  4379. if (p != p_end) {
  4380. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4381. goto fail;
  4382. }
  4383. LOG_VERBOSE("Load stringref section success.\n");
  4384. return true;
  4385. fail:
  4386. return false;
  4387. }
  4388. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4389. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4390. static bool
  4391. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4392. bool is_load_from_file_buf, char *error_buf,
  4393. uint32 error_buf_size)
  4394. {
  4395. const uint8 *p = buf, *p_end = buf_end;
  4396. uint32 name_type, subsection_size;
  4397. uint32 previous_name_type = 0;
  4398. uint32 num_func_name;
  4399. uint32 func_index;
  4400. uint32 previous_func_index = ~0U;
  4401. uint32 func_name_len;
  4402. uint32 name_index;
  4403. int i = 0;
  4404. if (p >= p_end) {
  4405. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4406. return false;
  4407. }
  4408. while (p < p_end) {
  4409. read_leb_uint32(p, p_end, name_type);
  4410. if (i != 0) {
  4411. if (name_type == previous_name_type) {
  4412. set_error_buf(error_buf, error_buf_size,
  4413. "duplicate sub-section");
  4414. return false;
  4415. }
  4416. if (name_type < previous_name_type) {
  4417. set_error_buf(error_buf, error_buf_size,
  4418. "out-of-order sub-section");
  4419. return false;
  4420. }
  4421. }
  4422. previous_name_type = name_type;
  4423. read_leb_uint32(p, p_end, subsection_size);
  4424. CHECK_BUF(p, p_end, subsection_size);
  4425. switch (name_type) {
  4426. case SUB_SECTION_TYPE_FUNC:
  4427. if (subsection_size) {
  4428. read_leb_uint32(p, p_end, num_func_name);
  4429. for (name_index = 0; name_index < num_func_name;
  4430. name_index++) {
  4431. read_leb_uint32(p, p_end, func_index);
  4432. if (func_index == previous_func_index) {
  4433. set_error_buf(error_buf, error_buf_size,
  4434. "duplicate function name");
  4435. return false;
  4436. }
  4437. if (func_index < previous_func_index
  4438. && previous_func_index != ~0U) {
  4439. set_error_buf(error_buf, error_buf_size,
  4440. "out-of-order function index ");
  4441. return false;
  4442. }
  4443. previous_func_index = func_index;
  4444. read_leb_uint32(p, p_end, func_name_len);
  4445. CHECK_BUF(p, p_end, func_name_len);
  4446. /* Skip the import functions */
  4447. if (func_index >= module->import_function_count) {
  4448. func_index -= module->import_function_count;
  4449. if (func_index >= module->function_count) {
  4450. set_error_buf(error_buf, error_buf_size,
  4451. "out-of-range function index");
  4452. return false;
  4453. }
  4454. if (!(module->functions[func_index]->field_name =
  4455. const_str_list_insert(
  4456. p, func_name_len, module,
  4457. #if WASM_ENABLE_WAMR_COMPILER != 0
  4458. false,
  4459. #else
  4460. is_load_from_file_buf,
  4461. #endif
  4462. error_buf, error_buf_size))) {
  4463. return false;
  4464. }
  4465. }
  4466. p += func_name_len;
  4467. }
  4468. }
  4469. break;
  4470. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4471. */
  4472. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4473. default:
  4474. p = p + subsection_size;
  4475. break;
  4476. }
  4477. i++;
  4478. }
  4479. return true;
  4480. fail:
  4481. return false;
  4482. }
  4483. #endif
  4484. static bool
  4485. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4486. bool is_load_from_file_buf, char *error_buf,
  4487. uint32 error_buf_size)
  4488. {
  4489. const uint8 *p = buf, *p_end = buf_end;
  4490. char section_name[32];
  4491. uint32 name_len, buffer_len;
  4492. if (p >= p_end) {
  4493. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4494. return false;
  4495. }
  4496. read_leb_uint32(p, p_end, name_len);
  4497. if (p + name_len > p_end) {
  4498. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4499. return false;
  4500. }
  4501. if (!check_utf8_str(p, name_len)) {
  4502. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4503. return false;
  4504. }
  4505. buffer_len = sizeof(section_name);
  4506. memset(section_name, 0, buffer_len);
  4507. if (name_len < buffer_len) {
  4508. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4509. }
  4510. else {
  4511. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4512. memset(section_name + buffer_len - 4, '.', 3);
  4513. }
  4514. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4515. if (memcmp(p, "name", 4) == 0) {
  4516. module->name_section_buf = buf;
  4517. module->name_section_buf_end = buf_end;
  4518. p += name_len;
  4519. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  4520. error_buf_size);
  4521. LOG_VERBOSE("Load custom name section success.");
  4522. return true;
  4523. }
  4524. #endif
  4525. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4526. {
  4527. WASMCustomSection *section =
  4528. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4529. if (!section) {
  4530. return false;
  4531. }
  4532. section->name_addr = (char *)p;
  4533. section->name_len = name_len;
  4534. section->content_addr = (uint8 *)(p + name_len);
  4535. section->content_len = (uint32)(p_end - p - name_len);
  4536. section->next = module->custom_section_list;
  4537. module->custom_section_list = section;
  4538. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4539. return true;
  4540. }
  4541. #endif
  4542. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4543. (void)is_load_from_file_buf;
  4544. (void)module;
  4545. return true;
  4546. fail:
  4547. return false;
  4548. }
  4549. static void
  4550. calculate_global_data_offset(WASMModule *module)
  4551. {
  4552. uint32 i, data_offset;
  4553. data_offset = 0;
  4554. for (i = 0; i < module->import_global_count; i++) {
  4555. WASMGlobalImport *import_global =
  4556. &((module->import_globals + i)->u.global);
  4557. #if WASM_ENABLE_FAST_JIT != 0
  4558. import_global->data_offset = data_offset;
  4559. #endif
  4560. data_offset += wasm_value_type_size(import_global->type);
  4561. }
  4562. for (i = 0; i < module->global_count; i++) {
  4563. WASMGlobal *global = module->globals + i;
  4564. #if WASM_ENABLE_FAST_JIT != 0
  4565. global->data_offset = data_offset;
  4566. #endif
  4567. data_offset += wasm_value_type_size(global->type);
  4568. }
  4569. module->global_data_size = data_offset;
  4570. }
  4571. #if WASM_ENABLE_FAST_JIT != 0
  4572. static bool
  4573. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4574. uint32 error_buf_size)
  4575. {
  4576. #if WASM_ENABLE_LAZY_JIT != 0
  4577. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4578. #endif
  4579. uint32 i;
  4580. if (!module->function_count)
  4581. return true;
  4582. if (!(module->fast_jit_func_ptrs =
  4583. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4584. error_buf_size))) {
  4585. return false;
  4586. }
  4587. #if WASM_ENABLE_LAZY_JIT != 0
  4588. for (i = 0; i < module->function_count; i++) {
  4589. module->fast_jit_func_ptrs[i] =
  4590. jit_globals->compile_fast_jit_and_then_call;
  4591. }
  4592. #endif
  4593. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4594. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4595. set_error_buf(error_buf, error_buf_size,
  4596. "init fast jit thread lock failed");
  4597. return false;
  4598. }
  4599. module->fast_jit_thread_locks_inited[i] = true;
  4600. }
  4601. return true;
  4602. }
  4603. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4604. #if WASM_ENABLE_JIT != 0
  4605. static bool
  4606. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4607. uint32 error_buf_size)
  4608. {
  4609. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4610. AOTCompOption option = { 0 };
  4611. char *aot_last_error;
  4612. uint64 size;
  4613. #if WASM_ENABLE_GC != 0
  4614. bool gc_enabled = true;
  4615. #else
  4616. bool gc_enabled = false;
  4617. #endif
  4618. if (module->function_count == 0)
  4619. return true;
  4620. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4621. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4622. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4623. return false;
  4624. }
  4625. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4626. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4627. os_mutex_destroy(&module->tierup_wait_lock);
  4628. return false;
  4629. }
  4630. module->tierup_wait_lock_inited = true;
  4631. #endif
  4632. size = sizeof(void *) * (uint64)module->function_count
  4633. + sizeof(bool) * (uint64)module->function_count;
  4634. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4635. return false;
  4636. }
  4637. module->func_ptrs_compiled =
  4638. (bool *)((uint8 *)module->func_ptrs
  4639. + sizeof(void *) * module->function_count);
  4640. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4641. if (!module->comp_data) {
  4642. aot_last_error = aot_get_last_error();
  4643. bh_assert(aot_last_error != NULL);
  4644. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4645. return false;
  4646. }
  4647. option.is_jit_mode = true;
  4648. option.opt_level = llvm_jit_options->opt_level;
  4649. option.size_level = llvm_jit_options->size_level;
  4650. option.segue_flags = llvm_jit_options->segue_flags;
  4651. option.quick_invoke_c_api_import =
  4652. llvm_jit_options->quick_invoke_c_api_import;
  4653. #if WASM_ENABLE_BULK_MEMORY != 0
  4654. option.enable_bulk_memory = true;
  4655. #endif
  4656. #if WASM_ENABLE_THREAD_MGR != 0
  4657. option.enable_thread_mgr = true;
  4658. #endif
  4659. #if WASM_ENABLE_TAIL_CALL != 0
  4660. option.enable_tail_call = true;
  4661. #endif
  4662. #if WASM_ENABLE_SIMD != 0
  4663. option.enable_simd = true;
  4664. #endif
  4665. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4666. option.enable_ref_types = true;
  4667. #elif WASM_ENABLE_GC != 0
  4668. option.enable_gc = true;
  4669. #endif
  4670. option.enable_aux_stack_check = true;
  4671. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4672. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4673. option.enable_aux_stack_frame = true;
  4674. #endif
  4675. #if WASM_ENABLE_PERF_PROFILING != 0
  4676. option.enable_perf_profiling = true;
  4677. #endif
  4678. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4679. option.enable_memory_profiling = true;
  4680. option.enable_stack_estimation = true;
  4681. #endif
  4682. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4683. if (!module->comp_ctx) {
  4684. aot_last_error = aot_get_last_error();
  4685. bh_assert(aot_last_error != NULL);
  4686. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4687. return false;
  4688. }
  4689. return true;
  4690. }
  4691. static bool
  4692. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4693. uint32 error_buf_size)
  4694. {
  4695. char *aot_last_error;
  4696. uint32 i;
  4697. if (module->function_count == 0)
  4698. return true;
  4699. if (!aot_compile_wasm(module->comp_ctx)) {
  4700. aot_last_error = aot_get_last_error();
  4701. bh_assert(aot_last_error != NULL);
  4702. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4703. return false;
  4704. }
  4705. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4706. if (module->orcjit_stop_compiling)
  4707. return false;
  4708. #endif
  4709. bh_print_time("Begin to lookup llvm jit functions");
  4710. for (i = 0; i < module->function_count; i++) {
  4711. LLVMOrcJITTargetAddress func_addr = 0;
  4712. LLVMErrorRef error;
  4713. char func_name[48];
  4714. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4715. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4716. func_name);
  4717. if (error != LLVMErrorSuccess) {
  4718. char *err_msg = LLVMGetErrorMessage(error);
  4719. set_error_buf_v(error_buf, error_buf_size,
  4720. "failed to compile llvm jit function: %s", err_msg);
  4721. LLVMDisposeErrorMessage(err_msg);
  4722. return false;
  4723. }
  4724. /**
  4725. * No need to lock the func_ptr[func_idx] here as it is basic
  4726. * data type, the load/store for it can be finished by one cpu
  4727. * instruction, and there can be only one cpu instruction
  4728. * loading/storing at the same time.
  4729. */
  4730. module->func_ptrs[i] = (void *)func_addr;
  4731. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4732. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4733. if (module->orcjit_stop_compiling)
  4734. return false;
  4735. #endif
  4736. }
  4737. bh_print_time("End lookup llvm jit functions");
  4738. return true;
  4739. }
  4740. #endif /* end of WASM_ENABLE_JIT != 0 */
  4741. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4742. && WASM_ENABLE_LAZY_JIT != 0
  4743. static void *
  4744. init_llvm_jit_functions_stage2_callback(void *arg)
  4745. {
  4746. WASMModule *module = (WASMModule *)arg;
  4747. char error_buf[128];
  4748. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4749. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4750. module->orcjit_stop_compiling = true;
  4751. return NULL;
  4752. }
  4753. os_mutex_lock(&module->tierup_wait_lock);
  4754. module->llvm_jit_inited = true;
  4755. os_cond_broadcast(&module->tierup_wait_cond);
  4756. os_mutex_unlock(&module->tierup_wait_lock);
  4757. return NULL;
  4758. }
  4759. #endif
  4760. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4761. /* The callback function to compile jit functions */
  4762. static void *
  4763. orcjit_thread_callback(void *arg)
  4764. {
  4765. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4766. #if WASM_ENABLE_JIT != 0
  4767. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4768. #endif
  4769. WASMModule *module = thread_arg->module;
  4770. uint32 group_idx = thread_arg->group_idx;
  4771. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4772. uint32 func_count = module->function_count;
  4773. uint32 i;
  4774. #if WASM_ENABLE_FAST_JIT != 0
  4775. /* Compile fast jit funcitons of this group */
  4776. for (i = group_idx; i < func_count; i += group_stride) {
  4777. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4778. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4779. break;
  4780. }
  4781. if (module->orcjit_stop_compiling) {
  4782. return NULL;
  4783. }
  4784. }
  4785. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4786. os_mutex_lock(&module->tierup_wait_lock);
  4787. module->fast_jit_ready_groups++;
  4788. os_mutex_unlock(&module->tierup_wait_lock);
  4789. #endif
  4790. #endif
  4791. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4792. && WASM_ENABLE_LAZY_JIT != 0
  4793. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4794. for (i = group_idx; i < func_count;
  4795. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4796. uint32 j;
  4797. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4798. if (i + j * group_stride < func_count) {
  4799. if (!jit_compiler_set_call_to_fast_jit(
  4800. module,
  4801. i + j * group_stride + module->import_function_count)) {
  4802. LOG_ERROR(
  4803. "failed to compile call_to_fast_jit for func %u\n",
  4804. i + j * group_stride + module->import_function_count);
  4805. module->orcjit_stop_compiling = true;
  4806. return NULL;
  4807. }
  4808. }
  4809. if (module->orcjit_stop_compiling) {
  4810. return NULL;
  4811. }
  4812. }
  4813. }
  4814. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4815. fast jit functions are compiled */
  4816. os_mutex_lock(&module->tierup_wait_lock);
  4817. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4818. && module->fast_jit_ready_groups >= group_stride)) {
  4819. os_cond_reltimedwait(&module->tierup_wait_cond,
  4820. &module->tierup_wait_lock, 10000);
  4821. if (module->orcjit_stop_compiling) {
  4822. /* init_llvm_jit_functions_stage2 failed */
  4823. os_mutex_unlock(&module->tierup_wait_lock);
  4824. return NULL;
  4825. }
  4826. }
  4827. os_mutex_unlock(&module->tierup_wait_lock);
  4828. #endif
  4829. #if WASM_ENABLE_JIT != 0
  4830. /* Compile llvm jit functions of this group */
  4831. for (i = group_idx; i < func_count;
  4832. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4833. LLVMOrcJITTargetAddress func_addr = 0;
  4834. LLVMErrorRef error;
  4835. char func_name[48];
  4836. typedef void (*F)(void);
  4837. union {
  4838. F f;
  4839. void *v;
  4840. } u;
  4841. uint32 j;
  4842. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4843. "_wrapper");
  4844. LOG_DEBUG("compile llvm jit func %s", func_name);
  4845. error =
  4846. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4847. if (error != LLVMErrorSuccess) {
  4848. char *err_msg = LLVMGetErrorMessage(error);
  4849. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4850. LLVMDisposeErrorMessage(err_msg);
  4851. break;
  4852. }
  4853. /* Call the jit wrapper function to trigger its compilation, so as
  4854. to compile the actual jit functions, since we add the latter to
  4855. function list in the PartitionFunction callback */
  4856. u.v = (void *)func_addr;
  4857. u.f();
  4858. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4859. if (i + j * group_stride < func_count) {
  4860. module->func_ptrs_compiled[i + j * group_stride] = true;
  4861. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4862. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  4863. i + j * group_stride);
  4864. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  4865. func_name);
  4866. if (error != LLVMErrorSuccess) {
  4867. char *err_msg = LLVMGetErrorMessage(error);
  4868. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  4869. err_msg);
  4870. LLVMDisposeErrorMessage(err_msg);
  4871. /* Ignore current llvm jit func, as its func ptr is
  4872. previous set to call_to_fast_jit, which also works */
  4873. continue;
  4874. }
  4875. jit_compiler_set_llvm_jit_func_ptr(
  4876. module,
  4877. i + j * group_stride + module->import_function_count,
  4878. (void *)func_addr);
  4879. /* Try to switch to call this llvm jit funtion instead of
  4880. fast jit function from fast jit jitted code */
  4881. jit_compiler_set_call_to_llvm_jit(
  4882. module,
  4883. i + j * group_stride + module->import_function_count);
  4884. #endif
  4885. }
  4886. }
  4887. if (module->orcjit_stop_compiling) {
  4888. break;
  4889. }
  4890. }
  4891. #endif
  4892. return NULL;
  4893. }
  4894. static void
  4895. orcjit_stop_compile_threads(WASMModule *module)
  4896. {
  4897. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  4898. / sizeof(OrcJitThreadArg));
  4899. module->orcjit_stop_compiling = true;
  4900. for (i = 0; i < thread_num; i++) {
  4901. if (module->orcjit_threads[i])
  4902. os_thread_join(module->orcjit_threads[i], NULL);
  4903. }
  4904. }
  4905. static bool
  4906. compile_jit_functions(WASMModule *module, char *error_buf,
  4907. uint32 error_buf_size)
  4908. {
  4909. uint32 thread_num =
  4910. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  4911. uint32 i, j;
  4912. bh_print_time("Begin to compile jit functions");
  4913. /* Create threads to compile the jit functions */
  4914. for (i = 0; i < thread_num && i < module->function_count; i++) {
  4915. #if WASM_ENABLE_JIT != 0
  4916. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  4917. #endif
  4918. module->orcjit_thread_args[i].module = module;
  4919. module->orcjit_thread_args[i].group_idx = i;
  4920. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  4921. (void *)&module->orcjit_thread_args[i],
  4922. APP_THREAD_STACK_SIZE_DEFAULT)
  4923. != 0) {
  4924. set_error_buf(error_buf, error_buf_size,
  4925. "create orcjit compile thread failed");
  4926. /* Terminate the threads created */
  4927. module->orcjit_stop_compiling = true;
  4928. for (j = 0; j < i; j++) {
  4929. os_thread_join(module->orcjit_threads[j], NULL);
  4930. }
  4931. return false;
  4932. }
  4933. }
  4934. #if WASM_ENABLE_LAZY_JIT == 0
  4935. /* Wait until all jit functions are compiled for eager mode */
  4936. for (i = 0; i < thread_num; i++) {
  4937. if (module->orcjit_threads[i])
  4938. os_thread_join(module->orcjit_threads[i], NULL);
  4939. }
  4940. #if WASM_ENABLE_FAST_JIT != 0
  4941. /* Ensure all the fast-jit functions are compiled */
  4942. for (i = 0; i < module->function_count; i++) {
  4943. if (!jit_compiler_is_compiled(module,
  4944. i + module->import_function_count)) {
  4945. set_error_buf(error_buf, error_buf_size,
  4946. "failed to compile fast jit function");
  4947. return false;
  4948. }
  4949. }
  4950. #endif
  4951. #if WASM_ENABLE_JIT != 0
  4952. /* Ensure all the llvm-jit functions are compiled */
  4953. for (i = 0; i < module->function_count; i++) {
  4954. if (!module->func_ptrs_compiled[i]) {
  4955. set_error_buf(error_buf, error_buf_size,
  4956. "failed to compile llvm jit function");
  4957. return false;
  4958. }
  4959. }
  4960. #endif
  4961. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  4962. bh_print_time("End compile jit functions");
  4963. return true;
  4964. }
  4965. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  4966. static bool
  4967. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4968. uint32 cur_func_idx, char *error_buf,
  4969. uint32 error_buf_size);
  4970. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  4971. void **
  4972. wasm_interp_get_handle_table();
  4973. static void **handle_table;
  4974. #endif
  4975. static bool
  4976. load_from_sections(WASMModule *module, WASMSection *sections,
  4977. bool is_load_from_file_buf, char *error_buf,
  4978. uint32 error_buf_size)
  4979. {
  4980. WASMExport *export;
  4981. WASMSection *section = sections;
  4982. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  4983. *buf_func = NULL, *buf_func_end = NULL;
  4984. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  4985. WASMGlobal *aux_stack_top_global = NULL, *global;
  4986. uint64 aux_data_end = (uint64)-1, aux_heap_base = (uint64)-1,
  4987. aux_stack_top = (uint64)-1;
  4988. uint32 global_index, func_index, i;
  4989. uint32 aux_data_end_global_index = (uint32)-1;
  4990. uint32 aux_heap_base_global_index = (uint32)-1;
  4991. WASMFuncType *func_type;
  4992. /* Find code and function sections if have */
  4993. while (section) {
  4994. if (section->section_type == SECTION_TYPE_CODE) {
  4995. buf_code = section->section_body;
  4996. buf_code_end = buf_code + section->section_body_size;
  4997. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  4998. module->buf_code = (uint8 *)buf_code;
  4999. module->buf_code_size = section->section_body_size;
  5000. #endif
  5001. }
  5002. else if (section->section_type == SECTION_TYPE_FUNC) {
  5003. buf_func = section->section_body;
  5004. buf_func_end = buf_func + section->section_body_size;
  5005. }
  5006. section = section->next;
  5007. }
  5008. section = sections;
  5009. while (section) {
  5010. buf = section->section_body;
  5011. buf_end = buf + section->section_body_size;
  5012. switch (section->section_type) {
  5013. case SECTION_TYPE_USER:
  5014. /* unsupported user section, ignore it. */
  5015. if (!load_user_section(buf, buf_end, module,
  5016. is_load_from_file_buf, error_buf,
  5017. error_buf_size))
  5018. return false;
  5019. break;
  5020. case SECTION_TYPE_TYPE:
  5021. if (!load_type_section(buf, buf_end, module, error_buf,
  5022. error_buf_size))
  5023. return false;
  5024. break;
  5025. case SECTION_TYPE_IMPORT:
  5026. if (!load_import_section(buf, buf_end, module,
  5027. is_load_from_file_buf, error_buf,
  5028. error_buf_size))
  5029. return false;
  5030. break;
  5031. case SECTION_TYPE_FUNC:
  5032. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5033. module, error_buf, error_buf_size))
  5034. return false;
  5035. break;
  5036. case SECTION_TYPE_TABLE:
  5037. if (!load_table_section(buf, buf_end, module, error_buf,
  5038. error_buf_size))
  5039. return false;
  5040. break;
  5041. case SECTION_TYPE_MEMORY:
  5042. if (!load_memory_section(buf, buf_end, module, error_buf,
  5043. error_buf_size))
  5044. return false;
  5045. break;
  5046. #if WASM_ENABLE_TAGS != 0
  5047. case SECTION_TYPE_TAG:
  5048. /* load tag declaration section */
  5049. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5050. module, error_buf, error_buf_size))
  5051. return false;
  5052. break;
  5053. #endif
  5054. case SECTION_TYPE_GLOBAL:
  5055. if (!load_global_section(buf, buf_end, module, error_buf,
  5056. error_buf_size))
  5057. return false;
  5058. break;
  5059. case SECTION_TYPE_EXPORT:
  5060. if (!load_export_section(buf, buf_end, module,
  5061. is_load_from_file_buf, error_buf,
  5062. error_buf_size))
  5063. return false;
  5064. break;
  5065. case SECTION_TYPE_START:
  5066. if (!load_start_section(buf, buf_end, module, error_buf,
  5067. error_buf_size))
  5068. return false;
  5069. break;
  5070. case SECTION_TYPE_ELEM:
  5071. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5072. error_buf_size))
  5073. return false;
  5074. break;
  5075. case SECTION_TYPE_CODE:
  5076. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5077. module, error_buf, error_buf_size))
  5078. return false;
  5079. break;
  5080. case SECTION_TYPE_DATA:
  5081. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  5082. error_buf_size))
  5083. return false;
  5084. break;
  5085. #if WASM_ENABLE_BULK_MEMORY != 0
  5086. case SECTION_TYPE_DATACOUNT:
  5087. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5088. error_buf_size))
  5089. return false;
  5090. break;
  5091. #endif
  5092. #if WASM_ENABLE_STRINGREF != 0
  5093. case SECTION_TYPE_STRINGREF:
  5094. if (!load_stringref_section(buf, buf_end, module,
  5095. is_load_from_file_buf, error_buf,
  5096. error_buf_size))
  5097. return false;
  5098. break;
  5099. #endif
  5100. default:
  5101. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5102. return false;
  5103. }
  5104. section = section->next;
  5105. }
  5106. module->aux_data_end_global_index = (uint32)-1;
  5107. module->aux_heap_base_global_index = (uint32)-1;
  5108. module->aux_stack_top_global_index = (uint32)-1;
  5109. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5110. export = module->exports;
  5111. for (i = 0; i < module->export_count; i++, export ++) {
  5112. if (export->kind == EXPORT_KIND_GLOBAL) {
  5113. if (!strcmp(export->name, "__heap_base")) {
  5114. global_index = export->index - module->import_global_count;
  5115. global = module->globals + global_index;
  5116. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5117. && global->init_expr.init_expr_type
  5118. == INIT_EXPR_TYPE_I32_CONST) {
  5119. aux_heap_base_global = global;
  5120. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5121. aux_heap_base_global_index = export->index;
  5122. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  5123. aux_heap_base);
  5124. }
  5125. }
  5126. else if (!strcmp(export->name, "__data_end")) {
  5127. global_index = export->index - module->import_global_count;
  5128. global = module->globals + global_index;
  5129. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5130. && global->init_expr.init_expr_type
  5131. == INIT_EXPR_TYPE_I32_CONST) {
  5132. aux_data_end_global = global;
  5133. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5134. aux_data_end_global_index = export->index;
  5135. LOG_VERBOSE("Found aux __data_end global, value: %d",
  5136. aux_data_end);
  5137. aux_data_end = align_uint64(aux_data_end, 16);
  5138. }
  5139. }
  5140. /* For module compiled with -pthread option, the global is:
  5141. [0] stack_top <-- 0
  5142. [1] tls_pointer
  5143. [2] tls_size
  5144. [3] data_end <-- 3
  5145. [4] global_base
  5146. [5] heap_base <-- 5
  5147. [6] dso_handle
  5148. For module compiled without -pthread option:
  5149. [0] stack_top <-- 0
  5150. [1] data_end <-- 1
  5151. [2] global_base
  5152. [3] heap_base <-- 3
  5153. [4] dso_handle
  5154. */
  5155. if (aux_data_end_global && aux_heap_base_global
  5156. && aux_data_end <= aux_heap_base) {
  5157. module->aux_data_end_global_index = aux_data_end_global_index;
  5158. module->aux_data_end = aux_data_end;
  5159. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5160. module->aux_heap_base = aux_heap_base;
  5161. /* Resolve aux stack top global */
  5162. for (global_index = 0; global_index < module->global_count;
  5163. global_index++) {
  5164. global = module->globals + global_index;
  5165. if (global->is_mutable /* heap_base and data_end is
  5166. not mutable */
  5167. && global->type == VALUE_TYPE_I32
  5168. && global->init_expr.init_expr_type
  5169. == INIT_EXPR_TYPE_I32_CONST
  5170. && (uint64)(uint32)global->init_expr.u.i32
  5171. <= aux_heap_base) {
  5172. aux_stack_top_global = global;
  5173. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5174. module->aux_stack_top_global_index =
  5175. module->import_global_count + global_index;
  5176. module->aux_stack_bottom = aux_stack_top;
  5177. module->aux_stack_size =
  5178. aux_stack_top > aux_data_end
  5179. ? (uint32)(aux_stack_top - aux_data_end)
  5180. : (uint32)aux_stack_top;
  5181. LOG_VERBOSE("Found aux stack top global, value: %d, "
  5182. "global index: %d, stack size: %d",
  5183. aux_stack_top, global_index,
  5184. module->aux_stack_size);
  5185. break;
  5186. }
  5187. }
  5188. if (!aux_stack_top_global) {
  5189. /* Auxiliary stack global isn't found, it must be unused
  5190. in the wasm app, as if it is used, the global must be
  5191. defined. Here we set it to __heap_base global and set
  5192. its size to 0. */
  5193. aux_stack_top_global = aux_heap_base_global;
  5194. aux_stack_top = aux_heap_base;
  5195. module->aux_stack_top_global_index =
  5196. module->aux_heap_base_global_index;
  5197. module->aux_stack_bottom = aux_stack_top;
  5198. module->aux_stack_size = 0;
  5199. }
  5200. break;
  5201. }
  5202. }
  5203. }
  5204. module->malloc_function = (uint32)-1;
  5205. module->free_function = (uint32)-1;
  5206. module->retain_function = (uint32)-1;
  5207. /* Resolve malloc/free function exported by wasm module */
  5208. export = module->exports;
  5209. for (i = 0; i < module->export_count; i++, export ++) {
  5210. if (export->kind == EXPORT_KIND_FUNC) {
  5211. if (!strcmp(export->name, "malloc")
  5212. && export->index >= module->import_function_count) {
  5213. func_index = export->index - module->import_function_count;
  5214. func_type = module->functions[func_index]->func_type;
  5215. if (func_type->param_count == 1 && func_type->result_count == 1
  5216. && func_type->types[0] == VALUE_TYPE_I32
  5217. && func_type->types[1] == VALUE_TYPE_I32) {
  5218. bh_assert(module->malloc_function == (uint32)-1);
  5219. module->malloc_function = export->index;
  5220. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5221. export->name, export->index);
  5222. }
  5223. }
  5224. else if (!strcmp(export->name, "__new")
  5225. && export->index >= module->import_function_count) {
  5226. /* __new && __pin for AssemblyScript */
  5227. func_index = export->index - module->import_function_count;
  5228. func_type = module->functions[func_index]->func_type;
  5229. if (func_type->param_count == 2 && func_type->result_count == 1
  5230. && func_type->types[0] == VALUE_TYPE_I32
  5231. && func_type->types[1] == VALUE_TYPE_I32
  5232. && func_type->types[2] == VALUE_TYPE_I32) {
  5233. uint32 j;
  5234. WASMExport *export_tmp;
  5235. bh_assert(module->malloc_function == (uint32)-1);
  5236. module->malloc_function = export->index;
  5237. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5238. export->name, export->index);
  5239. /* resolve retain function.
  5240. If not found, reset malloc function index */
  5241. export_tmp = module->exports;
  5242. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5243. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5244. && (!strcmp(export_tmp->name, "__retain")
  5245. || (!strcmp(export_tmp->name, "__pin")))
  5246. && (export_tmp->index
  5247. >= module->import_function_count)) {
  5248. func_index = export_tmp->index
  5249. - module->import_function_count;
  5250. func_type =
  5251. module->functions[func_index]->func_type;
  5252. if (func_type->param_count == 1
  5253. && func_type->result_count == 1
  5254. && func_type->types[0] == VALUE_TYPE_I32
  5255. && func_type->types[1] == VALUE_TYPE_I32) {
  5256. bh_assert(module->retain_function
  5257. == (uint32)-1);
  5258. module->retain_function = export_tmp->index;
  5259. LOG_VERBOSE("Found retain function, name: %s, "
  5260. "index: %u",
  5261. export_tmp->name,
  5262. export_tmp->index);
  5263. break;
  5264. }
  5265. }
  5266. }
  5267. if (j == module->export_count) {
  5268. module->malloc_function = (uint32)-1;
  5269. LOG_VERBOSE("Can't find retain function,"
  5270. "reset malloc function index to -1");
  5271. }
  5272. }
  5273. }
  5274. else if (((!strcmp(export->name, "free"))
  5275. || (!strcmp(export->name, "__release"))
  5276. || (!strcmp(export->name, "__unpin")))
  5277. && export->index >= module->import_function_count) {
  5278. func_index = export->index - module->import_function_count;
  5279. func_type = module->functions[func_index]->func_type;
  5280. if (func_type->param_count == 1 && func_type->result_count == 0
  5281. && func_type->types[0] == VALUE_TYPE_I32) {
  5282. bh_assert(module->free_function == (uint32)-1);
  5283. module->free_function = export->index;
  5284. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5285. export->name, export->index);
  5286. }
  5287. }
  5288. }
  5289. }
  5290. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5291. handle_table = wasm_interp_get_handle_table();
  5292. #endif
  5293. for (i = 0; i < module->function_count; i++) {
  5294. WASMFunction *func = module->functions[i];
  5295. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5296. error_buf_size)) {
  5297. return false;
  5298. }
  5299. if (i == module->function_count - 1
  5300. && func->code + func->code_size != buf_code_end) {
  5301. set_error_buf(error_buf, error_buf_size,
  5302. "code section size mismatch");
  5303. return false;
  5304. }
  5305. }
  5306. if (!module->possible_memory_grow) {
  5307. WASMMemoryImport *memory_import;
  5308. WASMMemory *memory;
  5309. if (aux_data_end_global && aux_heap_base_global
  5310. && aux_stack_top_global) {
  5311. uint64 init_memory_size;
  5312. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5313. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5314. * valid range of uint32 */
  5315. if (shrunk_memory_size <= UINT32_MAX) {
  5316. if (module->import_memory_count) {
  5317. memory_import = &module->import_memories[0].u.memory;
  5318. init_memory_size = (uint64)memory_import->num_bytes_per_page
  5319. * memory_import->init_page_count;
  5320. if (shrunk_memory_size <= init_memory_size) {
  5321. /* Reset memory info to decrease memory usage */
  5322. memory_import->num_bytes_per_page = shrunk_memory_size;
  5323. memory_import->init_page_count = 1;
  5324. LOG_VERBOSE("Shrink import memory size to %d",
  5325. shrunk_memory_size);
  5326. }
  5327. }
  5328. if (module->memory_count) {
  5329. memory = &module->memories[0];
  5330. init_memory_size = (uint64)memory->num_bytes_per_page
  5331. * memory->init_page_count;
  5332. if (shrunk_memory_size <= init_memory_size) {
  5333. /* Reset memory info to decrease memory usage */
  5334. memory->num_bytes_per_page = shrunk_memory_size;
  5335. memory->init_page_count = 1;
  5336. LOG_VERBOSE("Shrink memory size to %d",
  5337. shrunk_memory_size);
  5338. }
  5339. }
  5340. }
  5341. }
  5342. #if WASM_ENABLE_MULTI_MODULE == 0
  5343. if (module->import_memory_count) {
  5344. memory_import = &module->import_memories[0].u.memory;
  5345. /* Only resize the memory to one big page if num_bytes_per_page is
  5346. * in valid range of uint32 */
  5347. if (memory_import->init_page_count < DEFAULT_MAX_PAGES) {
  5348. memory_import->num_bytes_per_page *=
  5349. memory_import->init_page_count;
  5350. if (memory_import->init_page_count > 0)
  5351. memory_import->init_page_count =
  5352. memory_import->max_page_count = 1;
  5353. else
  5354. memory_import->init_page_count =
  5355. memory_import->max_page_count = 0;
  5356. }
  5357. }
  5358. if (module->memory_count) {
  5359. memory = &module->memories[0];
  5360. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5361. * valid range of uint32 */
  5362. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5363. memory->num_bytes_per_page *= memory->init_page_count;
  5364. if (memory->init_page_count > 0)
  5365. memory->init_page_count = memory->max_page_count = 1;
  5366. else
  5367. memory->init_page_count = memory->max_page_count = 0;
  5368. }
  5369. }
  5370. #endif
  5371. }
  5372. calculate_global_data_offset(module);
  5373. #if WASM_ENABLE_FAST_JIT != 0
  5374. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5375. return false;
  5376. }
  5377. #endif
  5378. #if WASM_ENABLE_JIT != 0
  5379. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5380. return false;
  5381. }
  5382. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5383. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5384. return false;
  5385. }
  5386. #else
  5387. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5388. thread fast jit execution, since applying llvm optimizations in
  5389. aot_compile_wasm may cost a lot of time.
  5390. Create thread with enough native stack to apply llvm optimizations */
  5391. if (os_thread_create(&module->llvm_jit_init_thread,
  5392. init_llvm_jit_functions_stage2_callback,
  5393. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5394. != 0) {
  5395. set_error_buf(error_buf, error_buf_size,
  5396. "create orcjit compile thread failed");
  5397. return false;
  5398. }
  5399. #endif
  5400. #endif
  5401. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5402. /* Create threads to compile the jit functions */
  5403. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5404. return false;
  5405. }
  5406. #endif
  5407. #if WASM_ENABLE_MEMORY_TRACING != 0
  5408. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5409. #endif
  5410. return true;
  5411. }
  5412. static WASMModule *
  5413. create_module(char *error_buf, uint32 error_buf_size)
  5414. {
  5415. WASMModule *module =
  5416. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5417. bh_list_status ret;
  5418. if (!module) {
  5419. return NULL;
  5420. }
  5421. module->module_type = Wasm_Module_Bytecode;
  5422. /* Set start_function to -1, means no start function */
  5423. module->start_function = (uint32)-1;
  5424. #if WASM_ENABLE_FAST_INTERP == 0
  5425. module->br_table_cache_list = &module->br_table_cache_list_head;
  5426. ret = bh_list_init(module->br_table_cache_list);
  5427. bh_assert(ret == BH_LIST_SUCCESS);
  5428. #endif
  5429. #if WASM_ENABLE_MULTI_MODULE != 0
  5430. module->import_module_list = &module->import_module_list_head;
  5431. ret = bh_list_init(module->import_module_list);
  5432. bh_assert(ret == BH_LIST_SUCCESS);
  5433. #endif
  5434. #if WASM_ENABLE_DEBUG_INTERP != 0
  5435. ret = bh_list_init(&module->fast_opcode_list);
  5436. bh_assert(ret == BH_LIST_SUCCESS);
  5437. #endif
  5438. #if WASM_ENABLE_GC != 0
  5439. if (!(module->ref_type_set =
  5440. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5441. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5442. goto fail1;
  5443. }
  5444. if (os_mutex_init(&module->rtt_type_lock)) {
  5445. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5446. goto fail2;
  5447. }
  5448. #endif
  5449. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5450. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5451. && WASM_ENABLE_LAZY_JIT != 0)
  5452. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5453. set_error_buf(error_buf, error_buf_size,
  5454. "init instance list lock failed");
  5455. goto fail3;
  5456. }
  5457. #endif
  5458. (void)ret;
  5459. return module;
  5460. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5461. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5462. && WASM_ENABLE_LAZY_JIT != 0)
  5463. fail3:
  5464. #endif
  5465. #if WASM_ENABLE_GC != 0
  5466. os_mutex_destroy(&module->rtt_type_lock);
  5467. fail2:
  5468. bh_hash_map_destroy(module->ref_type_set);
  5469. fail1:
  5470. #endif
  5471. wasm_runtime_free(module);
  5472. return NULL;
  5473. }
  5474. #if WASM_ENABLE_DEBUG_INTERP != 0
  5475. static bool
  5476. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5477. uint32 error_buf_size)
  5478. {
  5479. WASMFastOPCodeNode *fast_op =
  5480. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5481. if (fast_op) {
  5482. fast_op->offset = pos - module->load_addr;
  5483. fast_op->orig_op = orig_op;
  5484. bh_list_insert(&module->fast_opcode_list, fast_op);
  5485. }
  5486. return fast_op ? true : false;
  5487. }
  5488. #endif
  5489. WASMModule *
  5490. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5491. uint32 error_buf_size)
  5492. {
  5493. WASMModule *module = create_module(error_buf, error_buf_size);
  5494. if (!module)
  5495. return NULL;
  5496. if (!load_from_sections(module, section_list, false, error_buf,
  5497. error_buf_size)) {
  5498. wasm_loader_unload(module);
  5499. return NULL;
  5500. }
  5501. LOG_VERBOSE("Load module from sections success.\n");
  5502. return module;
  5503. }
  5504. static void
  5505. destroy_sections(WASMSection *section_list)
  5506. {
  5507. WASMSection *section = section_list, *next;
  5508. while (section) {
  5509. next = section->next;
  5510. wasm_runtime_free(section);
  5511. section = next;
  5512. }
  5513. }
  5514. /* clang-format off */
  5515. static uint8 section_ids[] = {
  5516. SECTION_TYPE_USER,
  5517. SECTION_TYPE_TYPE,
  5518. SECTION_TYPE_IMPORT,
  5519. SECTION_TYPE_FUNC,
  5520. SECTION_TYPE_TABLE,
  5521. SECTION_TYPE_MEMORY,
  5522. #if WASM_ENABLE_TAGS != 0
  5523. SECTION_TYPE_TAG,
  5524. #endif
  5525. #if WASM_ENABLE_STRINGREF != 0
  5526. /* must immediately precede the global section,
  5527. or where the global section would be */
  5528. SECTION_TYPE_STRINGREF,
  5529. #endif
  5530. SECTION_TYPE_GLOBAL,
  5531. SECTION_TYPE_EXPORT,
  5532. SECTION_TYPE_START,
  5533. SECTION_TYPE_ELEM,
  5534. #if WASM_ENABLE_BULK_MEMORY != 0
  5535. SECTION_TYPE_DATACOUNT,
  5536. #endif
  5537. SECTION_TYPE_CODE,
  5538. SECTION_TYPE_DATA
  5539. };
  5540. /* clang-format on */
  5541. static uint8
  5542. get_section_index(uint8 section_type)
  5543. {
  5544. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5545. for (uint8 i = 0; i < max_id; i++) {
  5546. if (section_type == section_ids[i])
  5547. return i;
  5548. }
  5549. return (uint8)-1;
  5550. }
  5551. static bool
  5552. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5553. char *error_buf, uint32 error_buf_size)
  5554. {
  5555. WASMSection *section_list_end = NULL, *section;
  5556. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  5557. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5558. uint32 section_size;
  5559. bh_assert(!*p_section_list);
  5560. p += 8;
  5561. while (p < p_end) {
  5562. CHECK_BUF(p, p_end, 1);
  5563. section_type = read_uint8(p);
  5564. section_index = get_section_index(section_type);
  5565. if (section_index != (uint8)-1) {
  5566. if (section_type != SECTION_TYPE_USER) {
  5567. /* Custom sections may be inserted at any place,
  5568. while other sections must occur at most once
  5569. and in prescribed order. */
  5570. if (last_section_index != (uint8)-1
  5571. && (section_index <= last_section_index)) {
  5572. set_error_buf(error_buf, error_buf_size,
  5573. "unexpected content after last section or "
  5574. "junk after last section");
  5575. return false;
  5576. }
  5577. last_section_index = section_index;
  5578. }
  5579. read_leb_uint32(p, p_end, section_size);
  5580. CHECK_BUF1(p, p_end, section_size);
  5581. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5582. error_buf_size))) {
  5583. return false;
  5584. }
  5585. section->section_type = section_type;
  5586. section->section_body = (uint8 *)p;
  5587. section->section_body_size = section_size;
  5588. if (!section_list_end)
  5589. *p_section_list = section_list_end = section;
  5590. else {
  5591. section_list_end->next = section;
  5592. section_list_end = section;
  5593. }
  5594. p += section_size;
  5595. }
  5596. else {
  5597. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5598. return false;
  5599. }
  5600. }
  5601. return true;
  5602. fail:
  5603. return false;
  5604. }
  5605. static void
  5606. exchange32(uint8 *p_data)
  5607. {
  5608. uint8 value = *p_data;
  5609. *p_data = *(p_data + 3);
  5610. *(p_data + 3) = value;
  5611. value = *(p_data + 1);
  5612. *(p_data + 1) = *(p_data + 2);
  5613. *(p_data + 2) = value;
  5614. }
  5615. static union {
  5616. int a;
  5617. char b;
  5618. } __ue = { .a = 1 };
  5619. #define is_little_endian() (__ue.b == 1)
  5620. static bool
  5621. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  5622. uint32 error_buf_size)
  5623. {
  5624. const uint8 *buf_end = buf + size;
  5625. const uint8 *p = buf, *p_end = buf_end;
  5626. uint32 magic_number, version;
  5627. WASMSection *section_list = NULL;
  5628. CHECK_BUF1(p, p_end, sizeof(uint32));
  5629. magic_number = read_uint32(p);
  5630. if (!is_little_endian())
  5631. exchange32((uint8 *)&magic_number);
  5632. if (magic_number != WASM_MAGIC_NUMBER) {
  5633. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5634. return false;
  5635. }
  5636. CHECK_BUF1(p, p_end, sizeof(uint32));
  5637. version = read_uint32(p);
  5638. if (!is_little_endian())
  5639. exchange32((uint8 *)&version);
  5640. if (version != WASM_CURRENT_VERSION) {
  5641. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5642. return false;
  5643. }
  5644. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5645. || !load_from_sections(module, section_list, true, error_buf,
  5646. error_buf_size)) {
  5647. destroy_sections(section_list);
  5648. return false;
  5649. }
  5650. destroy_sections(section_list);
  5651. return true;
  5652. fail:
  5653. return false;
  5654. }
  5655. #if WASM_ENABLE_LIBC_WASI != 0
  5656. /**
  5657. * refer to
  5658. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5659. */
  5660. static bool
  5661. check_wasi_abi_compatibility(const WASMModule *module,
  5662. #if WASM_ENABLE_MULTI_MODULE != 0
  5663. bool main_module,
  5664. #endif
  5665. char *error_buf, uint32 error_buf_size)
  5666. {
  5667. /**
  5668. * be careful with:
  5669. * wasi compatiable modules(command/reactor) which don't import any wasi
  5670. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5671. * reactor can depend on nothing. At the same time, each has its own entry
  5672. * point.
  5673. *
  5674. * observations:
  5675. * - clang always injects `_start` into a command
  5676. * - clang always injects `_initialize` into a reactor
  5677. * - `iwasm -f` allows to run a function in the reactor
  5678. *
  5679. * strong assumptions:
  5680. * - no one will define either `_start` or `_initialize` on purpose
  5681. * - `_start` should always be `void _start(void)`
  5682. * - `_initialize` should always be `void _initialize(void)`
  5683. *
  5684. */
  5685. /* clang-format off */
  5686. /**
  5687. *
  5688. * | | import_wasi_api True | | import_wasi_api False | |
  5689. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5690. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5691. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5692. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5693. */
  5694. /* clang-format on */
  5695. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5696. uint32 import_function_count = module->import_function_count;
  5697. WASMFuncType *func_type;
  5698. /* (func (export "_start") (...) */
  5699. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5700. error_buf, error_buf_size);
  5701. if (start) {
  5702. if (start->index < import_function_count) {
  5703. set_error_buf(
  5704. error_buf, error_buf_size,
  5705. "the builtin _start function can not be an import function");
  5706. return false;
  5707. }
  5708. func_type =
  5709. module->functions[start->index - import_function_count]->func_type;
  5710. if (func_type->param_count || func_type->result_count) {
  5711. set_error_buf(error_buf, error_buf_size,
  5712. "the signature of builtin _start function is wrong");
  5713. return false;
  5714. }
  5715. }
  5716. else {
  5717. /* (func (export "_initialize") (...) */
  5718. initialize =
  5719. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5720. error_buf, error_buf_size);
  5721. if (initialize) {
  5722. if (initialize->index < import_function_count) {
  5723. set_error_buf(error_buf, error_buf_size,
  5724. "the builtin _initialize function can not be an "
  5725. "import function");
  5726. return false;
  5727. }
  5728. func_type =
  5729. module->functions[initialize->index - import_function_count]
  5730. ->func_type;
  5731. if (func_type->param_count || func_type->result_count) {
  5732. set_error_buf(
  5733. error_buf, error_buf_size,
  5734. "the signature of builtin _initialize function is wrong");
  5735. return false;
  5736. }
  5737. }
  5738. }
  5739. /* filter out non-wasi compatiable modules */
  5740. if (!module->import_wasi_api && !start && !initialize) {
  5741. return true;
  5742. }
  5743. /* should have one at least */
  5744. if (module->import_wasi_api && !start && !initialize) {
  5745. LOG_WARNING("warning: a module with WASI apis should be either "
  5746. "a command or a reactor");
  5747. }
  5748. /*
  5749. * there is at least one of `_start` and `_initialize` in below cases.
  5750. * according to the assumption, they should be all wasi compatiable
  5751. */
  5752. #if WASM_ENABLE_MULTI_MODULE != 0
  5753. /* filter out commands (with `_start`) cases */
  5754. if (start && !main_module) {
  5755. set_error_buf(
  5756. error_buf, error_buf_size,
  5757. "a command (with _start function) can not be a sub-module");
  5758. return false;
  5759. }
  5760. #endif
  5761. /*
  5762. * it is ok a reactor acts as a main module,
  5763. * so skip the check about (with `_initialize`)
  5764. */
  5765. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5766. error_buf, error_buf_size);
  5767. if (!memory
  5768. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5769. /*
  5770. * with wasi-threads, it's still an open question if a memory
  5771. * should be exported.
  5772. *
  5773. * https://github.com/WebAssembly/wasi-threads/issues/22
  5774. * https://github.com/WebAssembly/WASI/issues/502
  5775. *
  5776. * Note: this code assumes the number of memories is at most 1.
  5777. */
  5778. && module->import_memory_count == 0
  5779. #endif
  5780. ) {
  5781. set_error_buf(error_buf, error_buf_size,
  5782. "a module with WASI apis must export memory by default");
  5783. return false;
  5784. }
  5785. return true;
  5786. }
  5787. #endif
  5788. WASMModule *
  5789. wasm_loader_load(uint8 *buf, uint32 size,
  5790. #if WASM_ENABLE_MULTI_MODULE != 0
  5791. bool main_module,
  5792. #endif
  5793. char *error_buf, uint32 error_buf_size)
  5794. {
  5795. WASMModule *module = create_module(error_buf, error_buf_size);
  5796. if (!module) {
  5797. return NULL;
  5798. }
  5799. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5800. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5801. module->load_addr = (uint8 *)buf;
  5802. module->load_size = size;
  5803. #endif
  5804. if (!load(buf, size, module, error_buf, error_buf_size)) {
  5805. goto fail;
  5806. }
  5807. #if WASM_ENABLE_LIBC_WASI != 0
  5808. /* Check the WASI application ABI */
  5809. if (!check_wasi_abi_compatibility(module,
  5810. #if WASM_ENABLE_MULTI_MODULE != 0
  5811. main_module,
  5812. #endif
  5813. error_buf, error_buf_size)) {
  5814. goto fail;
  5815. }
  5816. #endif
  5817. LOG_VERBOSE("Load module success.\n");
  5818. return module;
  5819. fail:
  5820. wasm_loader_unload(module);
  5821. return NULL;
  5822. }
  5823. void
  5824. wasm_loader_unload(WASMModule *module)
  5825. {
  5826. uint32 i;
  5827. if (!module)
  5828. return;
  5829. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5830. && WASM_ENABLE_LAZY_JIT != 0
  5831. module->orcjit_stop_compiling = true;
  5832. if (module->llvm_jit_init_thread)
  5833. os_thread_join(module->llvm_jit_init_thread, NULL);
  5834. #endif
  5835. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5836. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  5837. module internal data after they were freed */
  5838. orcjit_stop_compile_threads(module);
  5839. #endif
  5840. #if WASM_ENABLE_JIT != 0
  5841. if (module->func_ptrs)
  5842. wasm_runtime_free(module->func_ptrs);
  5843. if (module->comp_ctx)
  5844. aot_destroy_comp_context(module->comp_ctx);
  5845. if (module->comp_data)
  5846. aot_destroy_comp_data(module->comp_data);
  5847. #endif
  5848. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5849. && WASM_ENABLE_LAZY_JIT != 0
  5850. if (module->tierup_wait_lock_inited) {
  5851. os_mutex_destroy(&module->tierup_wait_lock);
  5852. os_cond_destroy(&module->tierup_wait_cond);
  5853. }
  5854. #endif
  5855. if (module->types) {
  5856. for (i = 0; i < module->type_count; i++) {
  5857. if (module->types[i])
  5858. destroy_wasm_type(module->types[i]);
  5859. }
  5860. wasm_runtime_free(module->types);
  5861. }
  5862. if (module->imports)
  5863. wasm_runtime_free(module->imports);
  5864. if (module->functions) {
  5865. for (i = 0; i < module->function_count; i++) {
  5866. if (module->functions[i]) {
  5867. if (module->functions[i]->local_offsets)
  5868. wasm_runtime_free(module->functions[i]->local_offsets);
  5869. #if WASM_ENABLE_FAST_INTERP != 0
  5870. if (module->functions[i]->code_compiled)
  5871. wasm_runtime_free(module->functions[i]->code_compiled);
  5872. if (module->functions[i]->consts)
  5873. wasm_runtime_free(module->functions[i]->consts);
  5874. #endif
  5875. #if WASM_ENABLE_FAST_JIT != 0
  5876. if (module->functions[i]->fast_jit_jitted_code) {
  5877. jit_code_cache_free(
  5878. module->functions[i]->fast_jit_jitted_code);
  5879. }
  5880. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5881. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  5882. jit_code_cache_free(
  5883. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  5884. }
  5885. #endif
  5886. #endif
  5887. #if WASM_ENABLE_GC != 0
  5888. if (module->functions[i]->local_ref_type_maps) {
  5889. wasm_runtime_free(
  5890. module->functions[i]->local_ref_type_maps);
  5891. }
  5892. #endif
  5893. wasm_runtime_free(module->functions[i]);
  5894. }
  5895. }
  5896. wasm_runtime_free(module->functions);
  5897. }
  5898. if (module->tables) {
  5899. #if WASM_ENABLE_GC != 0
  5900. for (i = 0; i < module->table_count; i++) {
  5901. destroy_init_expr(&module->tables[i].init_expr);
  5902. }
  5903. #endif
  5904. wasm_runtime_free(module->tables);
  5905. }
  5906. if (module->memories)
  5907. wasm_runtime_free(module->memories);
  5908. if (module->globals) {
  5909. #if WASM_ENABLE_GC != 0
  5910. for (i = 0; i < module->global_count; i++) {
  5911. destroy_init_expr(&module->globals[i].init_expr);
  5912. }
  5913. #endif
  5914. wasm_runtime_free(module->globals);
  5915. }
  5916. #if WASM_ENABLE_TAGS != 0
  5917. if (module->tags) {
  5918. for (i = 0; i < module->tag_count; i++) {
  5919. if (module->tags[i])
  5920. wasm_runtime_free(module->tags[i]);
  5921. }
  5922. wasm_runtime_free(module->tags);
  5923. }
  5924. #endif
  5925. if (module->exports)
  5926. wasm_runtime_free(module->exports);
  5927. if (module->table_segments) {
  5928. for (i = 0; i < module->table_seg_count; i++) {
  5929. if (module->table_segments[i].init_values) {
  5930. #if WASM_ENABLE_GC != 0
  5931. uint32 j;
  5932. for (j = 0; j < module->table_segments[i].value_count; j++) {
  5933. destroy_init_expr(
  5934. &module->table_segments[i].init_values[j]);
  5935. }
  5936. #endif
  5937. wasm_runtime_free(module->table_segments[i].init_values);
  5938. }
  5939. }
  5940. wasm_runtime_free(module->table_segments);
  5941. }
  5942. if (module->data_segments) {
  5943. for (i = 0; i < module->data_seg_count; i++) {
  5944. if (module->data_segments[i])
  5945. wasm_runtime_free(module->data_segments[i]);
  5946. }
  5947. wasm_runtime_free(module->data_segments);
  5948. }
  5949. if (module->const_str_list) {
  5950. StringNode *node = module->const_str_list, *node_next;
  5951. while (node) {
  5952. node_next = node->next;
  5953. wasm_runtime_free(node);
  5954. node = node_next;
  5955. }
  5956. }
  5957. #if WASM_ENABLE_STRINGREF != 0
  5958. if (module->string_literal_ptrs) {
  5959. wasm_runtime_free(module->string_literal_ptrs);
  5960. }
  5961. if (module->string_literal_lengths) {
  5962. wasm_runtime_free(module->string_literal_lengths);
  5963. }
  5964. #endif
  5965. #if WASM_ENABLE_FAST_INTERP == 0
  5966. if (module->br_table_cache_list) {
  5967. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  5968. BrTableCache *node_next;
  5969. while (node) {
  5970. node_next = bh_list_elem_next(node);
  5971. wasm_runtime_free(node);
  5972. node = node_next;
  5973. }
  5974. }
  5975. #endif
  5976. #if WASM_ENABLE_MULTI_MODULE != 0
  5977. /* just release the sub module list */
  5978. if (module->import_module_list) {
  5979. WASMRegisteredModule *node =
  5980. bh_list_first_elem(module->import_module_list);
  5981. while (node) {
  5982. WASMRegisteredModule *next = bh_list_elem_next(node);
  5983. bh_list_remove(module->import_module_list, node);
  5984. /*
  5985. * unload(sub_module) will be trigged during runtime_destroy().
  5986. * every module in the global module list will be unloaded one by
  5987. * one. so don't worry.
  5988. */
  5989. wasm_runtime_free(node);
  5990. /*
  5991. * the module file reading buffer will be released
  5992. * in runtime_destroy()
  5993. */
  5994. node = next;
  5995. }
  5996. }
  5997. #endif
  5998. #if WASM_ENABLE_DEBUG_INTERP != 0
  5999. WASMFastOPCodeNode *fast_opcode =
  6000. bh_list_first_elem(&module->fast_opcode_list);
  6001. while (fast_opcode) {
  6002. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6003. wasm_runtime_free(fast_opcode);
  6004. fast_opcode = next;
  6005. }
  6006. #endif
  6007. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6008. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6009. && WASM_ENABLE_LAZY_JIT != 0)
  6010. os_mutex_destroy(&module->instance_list_lock);
  6011. #endif
  6012. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6013. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6014. #endif
  6015. #if WASM_ENABLE_FAST_JIT != 0
  6016. if (module->fast_jit_func_ptrs) {
  6017. wasm_runtime_free(module->fast_jit_func_ptrs);
  6018. }
  6019. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6020. if (module->fast_jit_thread_locks_inited[i]) {
  6021. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6022. }
  6023. }
  6024. #endif
  6025. #if WASM_ENABLE_GC != 0
  6026. os_mutex_destroy(&module->rtt_type_lock);
  6027. bh_hash_map_destroy(module->ref_type_set);
  6028. if (module->rtt_types) {
  6029. for (i = 0; i < module->type_count; i++) {
  6030. if (module->rtt_types[i])
  6031. wasm_runtime_free(module->rtt_types[i]);
  6032. }
  6033. wasm_runtime_free(module->rtt_types);
  6034. }
  6035. #if WASM_ENABLE_STRINGREF != 0
  6036. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6037. if (module->stringref_rtts[i])
  6038. wasm_runtime_free(module->stringref_rtts[i]);
  6039. }
  6040. #endif
  6041. #endif
  6042. wasm_runtime_free(module);
  6043. }
  6044. bool
  6045. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6046. const uint8 *start_addr, const uint8 *code_end_addr,
  6047. uint8 label_type, uint8 **p_else_addr,
  6048. uint8 **p_end_addr)
  6049. {
  6050. const uint8 *p = start_addr, *p_end = code_end_addr;
  6051. uint8 *else_addr = NULL;
  6052. char error_buf[128];
  6053. uint32 block_nested_depth = 1, count, i, j, t;
  6054. uint32 error_buf_size = sizeof(error_buf);
  6055. uint8 opcode, u8;
  6056. BlockAddr block_stack[16] = { { 0 } }, *block;
  6057. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6058. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6059. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6060. if (block[j].start_addr == start_addr) {
  6061. /* Cache hit */
  6062. *p_else_addr = block[j].else_addr;
  6063. *p_end_addr = block[j].end_addr;
  6064. return true;
  6065. }
  6066. }
  6067. /* Cache unhit */
  6068. block_stack[0].start_addr = start_addr;
  6069. while (p < code_end_addr) {
  6070. opcode = *p++;
  6071. #if WASM_ENABLE_DEBUG_INTERP != 0
  6072. op_break_retry:
  6073. #endif
  6074. switch (opcode) {
  6075. case WASM_OP_UNREACHABLE:
  6076. case WASM_OP_NOP:
  6077. break;
  6078. #if WASM_ENABLE_EXCE_HANDLING != 0
  6079. case WASM_OP_TRY:
  6080. u8 = read_uint8(p);
  6081. if (block_nested_depth
  6082. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6083. block_stack[block_nested_depth].start_addr = p;
  6084. block_stack[block_nested_depth].else_addr = NULL;
  6085. }
  6086. block_nested_depth++;
  6087. break;
  6088. case EXT_OP_TRY:
  6089. skip_leb_uint32(p, p_end);
  6090. if (block_nested_depth
  6091. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6092. block_stack[block_nested_depth].start_addr = p;
  6093. block_stack[block_nested_depth].else_addr = NULL;
  6094. }
  6095. block_nested_depth++;
  6096. break;
  6097. case WASM_OP_CATCH:
  6098. if (block_nested_depth == 1) {
  6099. *p_end_addr = (uint8 *)(p - 1);
  6100. /* stop search and return the address of the catch block */
  6101. return true;
  6102. }
  6103. break;
  6104. case WASM_OP_CATCH_ALL:
  6105. if (block_nested_depth == 1) {
  6106. *p_end_addr = (uint8 *)(p - 1);
  6107. /* stop search and return the address of the catch_all block
  6108. */
  6109. return true;
  6110. }
  6111. break;
  6112. case WASM_OP_THROW:
  6113. /* skip tag_index */
  6114. skip_leb(p);
  6115. break;
  6116. case WASM_OP_RETHROW:
  6117. /* skip depth */
  6118. skip_leb(p);
  6119. break;
  6120. case WASM_OP_DELEGATE:
  6121. if (block_nested_depth == 1) {
  6122. *p_end_addr = (uint8 *)(p - 1);
  6123. return true;
  6124. }
  6125. else {
  6126. skip_leb(p);
  6127. /* the DELEGATE opcode ends the tryblock, */
  6128. block_nested_depth--;
  6129. if (block_nested_depth
  6130. < sizeof(block_stack) / sizeof(BlockAddr))
  6131. block_stack[block_nested_depth].end_addr =
  6132. (uint8 *)(p - 1);
  6133. }
  6134. break;
  6135. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6136. case WASM_OP_BLOCK:
  6137. case WASM_OP_LOOP:
  6138. case WASM_OP_IF:
  6139. {
  6140. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6141. u8 = read_uint8(p);
  6142. if (is_byte_a_type(u8)) {
  6143. #if WASM_ENABLE_GC != 0
  6144. if (wasm_is_type_multi_byte_type(u8)) {
  6145. /* the possible extra bytes of GC ref type have been
  6146. modified to OP_NOP, no need to resolve them again */
  6147. }
  6148. #endif
  6149. }
  6150. else {
  6151. p--;
  6152. skip_leb_uint32(p, p_end);
  6153. }
  6154. if (block_nested_depth
  6155. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6156. block_stack[block_nested_depth].start_addr = p;
  6157. block_stack[block_nested_depth].else_addr = NULL;
  6158. }
  6159. block_nested_depth++;
  6160. break;
  6161. }
  6162. case EXT_OP_BLOCK:
  6163. case EXT_OP_LOOP:
  6164. case EXT_OP_IF:
  6165. /* block type */
  6166. skip_leb_uint32(p, p_end);
  6167. if (block_nested_depth
  6168. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6169. block_stack[block_nested_depth].start_addr = p;
  6170. block_stack[block_nested_depth].else_addr = NULL;
  6171. }
  6172. block_nested_depth++;
  6173. break;
  6174. case WASM_OP_ELSE:
  6175. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6176. else_addr = (uint8 *)(p - 1);
  6177. if (block_nested_depth - 1
  6178. < sizeof(block_stack) / sizeof(BlockAddr))
  6179. block_stack[block_nested_depth - 1].else_addr =
  6180. (uint8 *)(p - 1);
  6181. break;
  6182. case WASM_OP_END:
  6183. if (block_nested_depth == 1) {
  6184. if (label_type == LABEL_TYPE_IF)
  6185. *p_else_addr = else_addr;
  6186. *p_end_addr = (uint8 *)(p - 1);
  6187. block_stack[0].end_addr = (uint8 *)(p - 1);
  6188. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6189. t++) {
  6190. start_addr = block_stack[t].start_addr;
  6191. if (start_addr) {
  6192. i = ((uintptr_t)start_addr)
  6193. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6194. block =
  6195. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6196. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6197. if (!block[j].start_addr)
  6198. break;
  6199. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6200. memmove(block + 1, block,
  6201. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6202. * sizeof(BlockAddr));
  6203. j = 0;
  6204. }
  6205. block[j].start_addr = block_stack[t].start_addr;
  6206. block[j].else_addr = block_stack[t].else_addr;
  6207. block[j].end_addr = block_stack[t].end_addr;
  6208. }
  6209. else
  6210. break;
  6211. }
  6212. return true;
  6213. }
  6214. else {
  6215. block_nested_depth--;
  6216. if (block_nested_depth
  6217. < sizeof(block_stack) / sizeof(BlockAddr))
  6218. block_stack[block_nested_depth].end_addr =
  6219. (uint8 *)(p - 1);
  6220. }
  6221. break;
  6222. case WASM_OP_BR:
  6223. case WASM_OP_BR_IF:
  6224. skip_leb_uint32(p, p_end); /* labelidx */
  6225. break;
  6226. case WASM_OP_BR_TABLE:
  6227. read_leb_uint32(p, p_end, count); /* lable num */
  6228. #if WASM_ENABLE_FAST_INTERP != 0
  6229. for (i = 0; i <= count; i++) /* lableidxs */
  6230. skip_leb_uint32(p, p_end);
  6231. #else
  6232. p += count + 1;
  6233. while (*p == WASM_OP_NOP)
  6234. p++;
  6235. #endif
  6236. break;
  6237. #if WASM_ENABLE_FAST_INTERP == 0
  6238. case EXT_OP_BR_TABLE_CACHE:
  6239. read_leb_uint32(p, p_end, count); /* lable num */
  6240. while (*p == WASM_OP_NOP)
  6241. p++;
  6242. break;
  6243. #endif
  6244. case WASM_OP_RETURN:
  6245. break;
  6246. case WASM_OP_CALL:
  6247. #if WASM_ENABLE_TAIL_CALL != 0
  6248. case WASM_OP_RETURN_CALL:
  6249. #endif
  6250. skip_leb_uint32(p, p_end); /* funcidx */
  6251. break;
  6252. case WASM_OP_CALL_INDIRECT:
  6253. #if WASM_ENABLE_TAIL_CALL != 0
  6254. case WASM_OP_RETURN_CALL_INDIRECT:
  6255. #endif
  6256. skip_leb_uint32(p, p_end); /* typeidx */
  6257. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6258. u8 = read_uint8(p); /* 0x00 */
  6259. #else
  6260. skip_leb_uint32(p, p_end); /* talbeidx */
  6261. #endif
  6262. break;
  6263. #if WASM_ENABLE_GC != 0
  6264. case WASM_OP_CALL_REF:
  6265. case WASM_OP_RETURN_CALL_REF:
  6266. skip_leb_uint32(p, p_end); /* typeidx */
  6267. break;
  6268. #endif
  6269. case WASM_OP_DROP:
  6270. case WASM_OP_SELECT:
  6271. case WASM_OP_DROP_64:
  6272. case WASM_OP_SELECT_64:
  6273. break;
  6274. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6275. case WASM_OP_SELECT_T:
  6276. {
  6277. skip_leb_uint32(p, p_end); /* vec length */
  6278. u8 = read_uint8(p); /* typeidx */
  6279. /* the possible extra bytes of GC ref type have been
  6280. modified to OP_NOP, no need to resolve them again */
  6281. break;
  6282. }
  6283. case WASM_OP_TABLE_GET:
  6284. case WASM_OP_TABLE_SET:
  6285. skip_leb_uint32(p, p_end); /* table index */
  6286. break;
  6287. case WASM_OP_REF_NULL:
  6288. {
  6289. u8 = read_uint8(p); /* type */
  6290. if (is_byte_a_type(u8)) {
  6291. #if WASM_ENABLE_GC != 0
  6292. if (wasm_is_type_multi_byte_type(u8)) {
  6293. /* the possible extra bytes of GC ref type have been
  6294. modified to OP_NOP, no need to resolve them again */
  6295. }
  6296. #endif
  6297. }
  6298. else {
  6299. p--;
  6300. skip_leb_uint32(p, p_end);
  6301. }
  6302. break;
  6303. }
  6304. case WASM_OP_REF_IS_NULL:
  6305. break;
  6306. case WASM_OP_REF_FUNC:
  6307. skip_leb_uint32(p, p_end); /* func index */
  6308. break;
  6309. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6310. #if WASM_ENABLE_GC != 0
  6311. case WASM_OP_REF_AS_NON_NULL:
  6312. case WASM_OP_REF_EQ:
  6313. break;
  6314. case WASM_OP_BR_ON_NULL:
  6315. case WASM_OP_BR_ON_NON_NULL:
  6316. skip_leb_uint32(p, p_end); /* label index */
  6317. break;
  6318. #endif /* end of WASM_ENABLE_GC != 0 */
  6319. case WASM_OP_GET_LOCAL:
  6320. case WASM_OP_SET_LOCAL:
  6321. case WASM_OP_TEE_LOCAL:
  6322. case WASM_OP_GET_GLOBAL:
  6323. case WASM_OP_SET_GLOBAL:
  6324. case WASM_OP_GET_GLOBAL_64:
  6325. case WASM_OP_SET_GLOBAL_64:
  6326. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6327. skip_leb_uint32(p, p_end); /* local index */
  6328. break;
  6329. case EXT_OP_GET_LOCAL_FAST:
  6330. case EXT_OP_SET_LOCAL_FAST:
  6331. case EXT_OP_TEE_LOCAL_FAST:
  6332. CHECK_BUF(p, p_end, 1);
  6333. p++;
  6334. break;
  6335. case WASM_OP_I32_LOAD:
  6336. case WASM_OP_I64_LOAD:
  6337. case WASM_OP_F32_LOAD:
  6338. case WASM_OP_F64_LOAD:
  6339. case WASM_OP_I32_LOAD8_S:
  6340. case WASM_OP_I32_LOAD8_U:
  6341. case WASM_OP_I32_LOAD16_S:
  6342. case WASM_OP_I32_LOAD16_U:
  6343. case WASM_OP_I64_LOAD8_S:
  6344. case WASM_OP_I64_LOAD8_U:
  6345. case WASM_OP_I64_LOAD16_S:
  6346. case WASM_OP_I64_LOAD16_U:
  6347. case WASM_OP_I64_LOAD32_S:
  6348. case WASM_OP_I64_LOAD32_U:
  6349. case WASM_OP_I32_STORE:
  6350. case WASM_OP_I64_STORE:
  6351. case WASM_OP_F32_STORE:
  6352. case WASM_OP_F64_STORE:
  6353. case WASM_OP_I32_STORE8:
  6354. case WASM_OP_I32_STORE16:
  6355. case WASM_OP_I64_STORE8:
  6356. case WASM_OP_I64_STORE16:
  6357. case WASM_OP_I64_STORE32:
  6358. skip_leb_uint32(p, p_end); /* align */
  6359. skip_leb_uint32(p, p_end); /* offset */
  6360. break;
  6361. case WASM_OP_MEMORY_SIZE:
  6362. case WASM_OP_MEMORY_GROW:
  6363. skip_leb_uint32(p, p_end); /* 0x00 */
  6364. break;
  6365. case WASM_OP_I32_CONST:
  6366. skip_leb_int32(p, p_end);
  6367. break;
  6368. case WASM_OP_I64_CONST:
  6369. skip_leb_int64(p, p_end);
  6370. break;
  6371. case WASM_OP_F32_CONST:
  6372. p += sizeof(float32);
  6373. break;
  6374. case WASM_OP_F64_CONST:
  6375. p += sizeof(float64);
  6376. break;
  6377. case WASM_OP_I32_EQZ:
  6378. case WASM_OP_I32_EQ:
  6379. case WASM_OP_I32_NE:
  6380. case WASM_OP_I32_LT_S:
  6381. case WASM_OP_I32_LT_U:
  6382. case WASM_OP_I32_GT_S:
  6383. case WASM_OP_I32_GT_U:
  6384. case WASM_OP_I32_LE_S:
  6385. case WASM_OP_I32_LE_U:
  6386. case WASM_OP_I32_GE_S:
  6387. case WASM_OP_I32_GE_U:
  6388. case WASM_OP_I64_EQZ:
  6389. case WASM_OP_I64_EQ:
  6390. case WASM_OP_I64_NE:
  6391. case WASM_OP_I64_LT_S:
  6392. case WASM_OP_I64_LT_U:
  6393. case WASM_OP_I64_GT_S:
  6394. case WASM_OP_I64_GT_U:
  6395. case WASM_OP_I64_LE_S:
  6396. case WASM_OP_I64_LE_U:
  6397. case WASM_OP_I64_GE_S:
  6398. case WASM_OP_I64_GE_U:
  6399. case WASM_OP_F32_EQ:
  6400. case WASM_OP_F32_NE:
  6401. case WASM_OP_F32_LT:
  6402. case WASM_OP_F32_GT:
  6403. case WASM_OP_F32_LE:
  6404. case WASM_OP_F32_GE:
  6405. case WASM_OP_F64_EQ:
  6406. case WASM_OP_F64_NE:
  6407. case WASM_OP_F64_LT:
  6408. case WASM_OP_F64_GT:
  6409. case WASM_OP_F64_LE:
  6410. case WASM_OP_F64_GE:
  6411. case WASM_OP_I32_CLZ:
  6412. case WASM_OP_I32_CTZ:
  6413. case WASM_OP_I32_POPCNT:
  6414. case WASM_OP_I32_ADD:
  6415. case WASM_OP_I32_SUB:
  6416. case WASM_OP_I32_MUL:
  6417. case WASM_OP_I32_DIV_S:
  6418. case WASM_OP_I32_DIV_U:
  6419. case WASM_OP_I32_REM_S:
  6420. case WASM_OP_I32_REM_U:
  6421. case WASM_OP_I32_AND:
  6422. case WASM_OP_I32_OR:
  6423. case WASM_OP_I32_XOR:
  6424. case WASM_OP_I32_SHL:
  6425. case WASM_OP_I32_SHR_S:
  6426. case WASM_OP_I32_SHR_U:
  6427. case WASM_OP_I32_ROTL:
  6428. case WASM_OP_I32_ROTR:
  6429. case WASM_OP_I64_CLZ:
  6430. case WASM_OP_I64_CTZ:
  6431. case WASM_OP_I64_POPCNT:
  6432. case WASM_OP_I64_ADD:
  6433. case WASM_OP_I64_SUB:
  6434. case WASM_OP_I64_MUL:
  6435. case WASM_OP_I64_DIV_S:
  6436. case WASM_OP_I64_DIV_U:
  6437. case WASM_OP_I64_REM_S:
  6438. case WASM_OP_I64_REM_U:
  6439. case WASM_OP_I64_AND:
  6440. case WASM_OP_I64_OR:
  6441. case WASM_OP_I64_XOR:
  6442. case WASM_OP_I64_SHL:
  6443. case WASM_OP_I64_SHR_S:
  6444. case WASM_OP_I64_SHR_U:
  6445. case WASM_OP_I64_ROTL:
  6446. case WASM_OP_I64_ROTR:
  6447. case WASM_OP_F32_ABS:
  6448. case WASM_OP_F32_NEG:
  6449. case WASM_OP_F32_CEIL:
  6450. case WASM_OP_F32_FLOOR:
  6451. case WASM_OP_F32_TRUNC:
  6452. case WASM_OP_F32_NEAREST:
  6453. case WASM_OP_F32_SQRT:
  6454. case WASM_OP_F32_ADD:
  6455. case WASM_OP_F32_SUB:
  6456. case WASM_OP_F32_MUL:
  6457. case WASM_OP_F32_DIV:
  6458. case WASM_OP_F32_MIN:
  6459. case WASM_OP_F32_MAX:
  6460. case WASM_OP_F32_COPYSIGN:
  6461. case WASM_OP_F64_ABS:
  6462. case WASM_OP_F64_NEG:
  6463. case WASM_OP_F64_CEIL:
  6464. case WASM_OP_F64_FLOOR:
  6465. case WASM_OP_F64_TRUNC:
  6466. case WASM_OP_F64_NEAREST:
  6467. case WASM_OP_F64_SQRT:
  6468. case WASM_OP_F64_ADD:
  6469. case WASM_OP_F64_SUB:
  6470. case WASM_OP_F64_MUL:
  6471. case WASM_OP_F64_DIV:
  6472. case WASM_OP_F64_MIN:
  6473. case WASM_OP_F64_MAX:
  6474. case WASM_OP_F64_COPYSIGN:
  6475. case WASM_OP_I32_WRAP_I64:
  6476. case WASM_OP_I32_TRUNC_S_F32:
  6477. case WASM_OP_I32_TRUNC_U_F32:
  6478. case WASM_OP_I32_TRUNC_S_F64:
  6479. case WASM_OP_I32_TRUNC_U_F64:
  6480. case WASM_OP_I64_EXTEND_S_I32:
  6481. case WASM_OP_I64_EXTEND_U_I32:
  6482. case WASM_OP_I64_TRUNC_S_F32:
  6483. case WASM_OP_I64_TRUNC_U_F32:
  6484. case WASM_OP_I64_TRUNC_S_F64:
  6485. case WASM_OP_I64_TRUNC_U_F64:
  6486. case WASM_OP_F32_CONVERT_S_I32:
  6487. case WASM_OP_F32_CONVERT_U_I32:
  6488. case WASM_OP_F32_CONVERT_S_I64:
  6489. case WASM_OP_F32_CONVERT_U_I64:
  6490. case WASM_OP_F32_DEMOTE_F64:
  6491. case WASM_OP_F64_CONVERT_S_I32:
  6492. case WASM_OP_F64_CONVERT_U_I32:
  6493. case WASM_OP_F64_CONVERT_S_I64:
  6494. case WASM_OP_F64_CONVERT_U_I64:
  6495. case WASM_OP_F64_PROMOTE_F32:
  6496. case WASM_OP_I32_REINTERPRET_F32:
  6497. case WASM_OP_I64_REINTERPRET_F64:
  6498. case WASM_OP_F32_REINTERPRET_I32:
  6499. case WASM_OP_F64_REINTERPRET_I64:
  6500. case WASM_OP_I32_EXTEND8_S:
  6501. case WASM_OP_I32_EXTEND16_S:
  6502. case WASM_OP_I64_EXTEND8_S:
  6503. case WASM_OP_I64_EXTEND16_S:
  6504. case WASM_OP_I64_EXTEND32_S:
  6505. break;
  6506. #if WASM_ENABLE_GC != 0
  6507. case WASM_OP_GC_PREFIX:
  6508. {
  6509. uint32 opcode1;
  6510. read_leb_uint32(p, p_end, opcode1);
  6511. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6512. is no larger than UINT8_MAX */
  6513. opcode = (uint8)opcode1;
  6514. switch (opcode) {
  6515. case WASM_OP_STRUCT_NEW:
  6516. case WASM_OP_STRUCT_NEW_DEFAULT:
  6517. skip_leb_uint32(p, p_end); /* typeidx */
  6518. break;
  6519. case WASM_OP_STRUCT_GET:
  6520. case WASM_OP_STRUCT_GET_S:
  6521. case WASM_OP_STRUCT_GET_U:
  6522. case WASM_OP_STRUCT_SET:
  6523. skip_leb_uint32(p, p_end); /* typeidx */
  6524. skip_leb_uint32(p, p_end); /* fieldidx */
  6525. break;
  6526. case WASM_OP_ARRAY_NEW:
  6527. case WASM_OP_ARRAY_NEW_DEFAULT:
  6528. case WASM_OP_ARRAY_GET:
  6529. case WASM_OP_ARRAY_GET_S:
  6530. case WASM_OP_ARRAY_GET_U:
  6531. case WASM_OP_ARRAY_SET:
  6532. case WASM_OP_ARRAY_FILL:
  6533. skip_leb_uint32(p, p_end); /* typeidx */
  6534. break;
  6535. case WASM_OP_ARRAY_COPY:
  6536. skip_leb_uint32(p, p_end); /* typeidx1 */
  6537. skip_leb_uint32(p, p_end); /* typeidx2 */
  6538. break;
  6539. case WASM_OP_ARRAY_LEN:
  6540. break;
  6541. case WASM_OP_ARRAY_NEW_FIXED:
  6542. case WASM_OP_ARRAY_NEW_DATA:
  6543. case WASM_OP_ARRAY_NEW_ELEM:
  6544. skip_leb_uint32(p, p_end); /* typeidx */
  6545. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6546. break;
  6547. case WASM_OP_REF_I31:
  6548. case WASM_OP_I31_GET_S:
  6549. case WASM_OP_I31_GET_U:
  6550. break;
  6551. case WASM_OP_REF_TEST:
  6552. case WASM_OP_REF_CAST:
  6553. case WASM_OP_REF_TEST_NULLABLE:
  6554. case WASM_OP_REF_CAST_NULLABLE:
  6555. skip_leb_int32(p, p_end); /* heaptype */
  6556. break;
  6557. case WASM_OP_BR_ON_CAST:
  6558. case WASM_OP_BR_ON_CAST_FAIL:
  6559. p += sizeof(uint8); /* castflag */
  6560. skip_leb_uint32(p, p_end); /* labelidx */
  6561. skip_leb_int32(p, p_end); /* heaptype */
  6562. skip_leb_int32(p, p_end); /* heaptype2 */
  6563. break;
  6564. case WASM_OP_ANY_CONVERT_EXTERN:
  6565. case WASM_OP_EXTERN_CONVERT_ANY:
  6566. break;
  6567. #if WASM_ENABLE_STRINGREF != 0
  6568. case WASM_OP_STRING_NEW_UTF8:
  6569. case WASM_OP_STRING_NEW_WTF16:
  6570. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6571. case WASM_OP_STRING_NEW_WTF8:
  6572. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6573. break;
  6574. case WASM_OP_STRING_CONST:
  6575. skip_leb_int32(p, p_end); /* contents */
  6576. break;
  6577. case WASM_OP_STRING_MEASURE_UTF8:
  6578. case WASM_OP_STRING_MEASURE_WTF8:
  6579. case WASM_OP_STRING_MEASURE_WTF16:
  6580. break;
  6581. case WASM_OP_STRING_ENCODE_UTF8:
  6582. case WASM_OP_STRING_ENCODE_WTF16:
  6583. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6584. case WASM_OP_STRING_ENCODE_WTF8:
  6585. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6586. break;
  6587. case WASM_OP_STRING_CONCAT:
  6588. case WASM_OP_STRING_EQ:
  6589. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6590. case WASM_OP_STRING_AS_WTF8:
  6591. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6592. break;
  6593. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6594. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6595. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6596. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6597. break;
  6598. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6599. case WASM_OP_STRING_AS_WTF16:
  6600. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6601. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6602. break;
  6603. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6604. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6605. break;
  6606. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6607. case WASM_OP_STRING_AS_ITER:
  6608. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6609. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6610. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6611. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6612. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6613. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6614. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6615. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6616. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6617. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6618. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6619. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6620. break;
  6621. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6622. default:
  6623. return false;
  6624. }
  6625. break;
  6626. }
  6627. #endif /* end of WASM_ENABLE_GC != 0 */
  6628. case WASM_OP_MISC_PREFIX:
  6629. {
  6630. uint32 opcode1;
  6631. read_leb_uint32(p, p_end, opcode1);
  6632. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6633. is no larger than UINT8_MAX */
  6634. opcode = (uint8)opcode1;
  6635. switch (opcode) {
  6636. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6637. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6638. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6639. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6640. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6641. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6642. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6643. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6644. break;
  6645. #if WASM_ENABLE_BULK_MEMORY != 0
  6646. case WASM_OP_MEMORY_INIT:
  6647. skip_leb_uint32(p, p_end);
  6648. /* skip memory idx */
  6649. p++;
  6650. break;
  6651. case WASM_OP_DATA_DROP:
  6652. skip_leb_uint32(p, p_end);
  6653. break;
  6654. case WASM_OP_MEMORY_COPY:
  6655. /* skip two memory idx */
  6656. p += 2;
  6657. break;
  6658. case WASM_OP_MEMORY_FILL:
  6659. /* skip memory idx */
  6660. p++;
  6661. break;
  6662. #endif /* WASM_ENABLE_BULK_MEMORY */
  6663. #if WASM_ENABLE_REF_TYPES != 0
  6664. case WASM_OP_TABLE_INIT:
  6665. case WASM_OP_TABLE_COPY:
  6666. /* tableidx */
  6667. skip_leb_uint32(p, p_end);
  6668. /* elemidx */
  6669. skip_leb_uint32(p, p_end);
  6670. break;
  6671. case WASM_OP_ELEM_DROP:
  6672. /* elemidx */
  6673. skip_leb_uint32(p, p_end);
  6674. break;
  6675. case WASM_OP_TABLE_SIZE:
  6676. case WASM_OP_TABLE_GROW:
  6677. case WASM_OP_TABLE_FILL:
  6678. skip_leb_uint32(p, p_end); /* table idx */
  6679. break;
  6680. #endif /* WASM_ENABLE_REF_TYPES */
  6681. default:
  6682. return false;
  6683. }
  6684. break;
  6685. }
  6686. #if WASM_ENABLE_SIMD != 0
  6687. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6688. case WASM_OP_SIMD_PREFIX:
  6689. {
  6690. uint32 opcode1;
  6691. read_leb_uint32(p, p_end, opcode1);
  6692. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6693. is no larger than UINT8_MAX */
  6694. opcode = (uint8)opcode1;
  6695. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6696. */
  6697. switch (opcode) {
  6698. case SIMD_v128_load:
  6699. case SIMD_v128_load8x8_s:
  6700. case SIMD_v128_load8x8_u:
  6701. case SIMD_v128_load16x4_s:
  6702. case SIMD_v128_load16x4_u:
  6703. case SIMD_v128_load32x2_s:
  6704. case SIMD_v128_load32x2_u:
  6705. case SIMD_v128_load8_splat:
  6706. case SIMD_v128_load16_splat:
  6707. case SIMD_v128_load32_splat:
  6708. case SIMD_v128_load64_splat:
  6709. case SIMD_v128_store:
  6710. /* memarg align */
  6711. skip_leb_uint32(p, p_end);
  6712. /* memarg offset*/
  6713. skip_leb_uint32(p, p_end);
  6714. break;
  6715. case SIMD_v128_const:
  6716. case SIMD_v8x16_shuffle:
  6717. /* immByte[16] immLaneId[16] */
  6718. CHECK_BUF1(p, p_end, 16);
  6719. p += 16;
  6720. break;
  6721. case SIMD_i8x16_extract_lane_s:
  6722. case SIMD_i8x16_extract_lane_u:
  6723. case SIMD_i8x16_replace_lane:
  6724. case SIMD_i16x8_extract_lane_s:
  6725. case SIMD_i16x8_extract_lane_u:
  6726. case SIMD_i16x8_replace_lane:
  6727. case SIMD_i32x4_extract_lane:
  6728. case SIMD_i32x4_replace_lane:
  6729. case SIMD_i64x2_extract_lane:
  6730. case SIMD_i64x2_replace_lane:
  6731. case SIMD_f32x4_extract_lane:
  6732. case SIMD_f32x4_replace_lane:
  6733. case SIMD_f64x2_extract_lane:
  6734. case SIMD_f64x2_replace_lane:
  6735. /* ImmLaneId */
  6736. CHECK_BUF(p, p_end, 1);
  6737. p++;
  6738. break;
  6739. case SIMD_v128_load8_lane:
  6740. case SIMD_v128_load16_lane:
  6741. case SIMD_v128_load32_lane:
  6742. case SIMD_v128_load64_lane:
  6743. case SIMD_v128_store8_lane:
  6744. case SIMD_v128_store16_lane:
  6745. case SIMD_v128_store32_lane:
  6746. case SIMD_v128_store64_lane:
  6747. /* memarg align */
  6748. skip_leb_uint32(p, p_end);
  6749. /* memarg offset*/
  6750. skip_leb_uint32(p, p_end);
  6751. /* ImmLaneId */
  6752. CHECK_BUF(p, p_end, 1);
  6753. p++;
  6754. break;
  6755. case SIMD_v128_load32_zero:
  6756. case SIMD_v128_load64_zero:
  6757. /* memarg align */
  6758. skip_leb_uint32(p, p_end);
  6759. /* memarg offset*/
  6760. skip_leb_uint32(p, p_end);
  6761. break;
  6762. default:
  6763. /*
  6764. * since latest SIMD specific used almost every value
  6765. * from 0x00 to 0xff, the default branch will present
  6766. * all opcodes without imm
  6767. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6768. */
  6769. break;
  6770. }
  6771. break;
  6772. }
  6773. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6774. #endif /* end of WASM_ENABLE_SIMD */
  6775. #if WASM_ENABLE_SHARED_MEMORY != 0
  6776. case WASM_OP_ATOMIC_PREFIX:
  6777. {
  6778. uint32 opcode1;
  6779. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6780. read_leb_uint32(p, p_end, opcode1);
  6781. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6782. is no larger than UINT8_MAX */
  6783. opcode = (uint8)opcode1;
  6784. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6785. skip_leb_uint32(p, p_end); /* align */
  6786. skip_leb_uint32(p, p_end); /* offset */
  6787. }
  6788. else {
  6789. /* atomic.fence doesn't have memarg */
  6790. p++;
  6791. }
  6792. break;
  6793. }
  6794. #endif
  6795. #if WASM_ENABLE_DEBUG_INTERP != 0
  6796. case DEBUG_OP_BREAK:
  6797. {
  6798. WASMDebugInstance *debug_instance =
  6799. wasm_exec_env_get_instance(exec_env);
  6800. char orignal_opcode[1];
  6801. uint64 size = 1;
  6802. WASMModuleInstance *module_inst =
  6803. (WASMModuleInstance *)exec_env->module_inst;
  6804. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6805. ? (p - 1) - module_inst->module->load_addr
  6806. : ~0;
  6807. if (debug_instance) {
  6808. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6809. orignal_opcode, &size)
  6810. && size == 1) {
  6811. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6812. "with %02x: ",
  6813. orignal_opcode[0]);
  6814. opcode = orignal_opcode[0];
  6815. goto op_break_retry;
  6816. }
  6817. }
  6818. break;
  6819. }
  6820. #endif
  6821. default:
  6822. return false;
  6823. }
  6824. }
  6825. (void)u8;
  6826. (void)exec_env;
  6827. return false;
  6828. fail:
  6829. return false;
  6830. }
  6831. #if WASM_ENABLE_FAST_INTERP != 0
  6832. #if WASM_DEBUG_PREPROCESSOR != 0
  6833. #define LOG_OP(...) os_printf(__VA_ARGS__)
  6834. #else
  6835. #define LOG_OP(...) (void)0
  6836. #endif
  6837. #define PATCH_ELSE 0
  6838. #define PATCH_END 1
  6839. typedef struct BranchBlockPatch {
  6840. struct BranchBlockPatch *next;
  6841. uint8 patch_type;
  6842. uint8 *code_compiled;
  6843. } BranchBlockPatch;
  6844. #endif
  6845. typedef struct BranchBlock {
  6846. uint8 label_type;
  6847. BlockType block_type;
  6848. uint8 *start_addr;
  6849. uint8 *else_addr;
  6850. uint8 *end_addr;
  6851. uint32 stack_cell_num;
  6852. #if WASM_ENABLE_GC != 0
  6853. uint32 reftype_map_num;
  6854. /* Indicate which local is used inside current block, used to validate
  6855. * local.get with non-nullable ref types */
  6856. uint8 *local_use_mask;
  6857. uint32 local_use_mask_size;
  6858. #endif
  6859. #if WASM_ENABLE_FAST_INTERP != 0
  6860. uint16 dynamic_offset;
  6861. uint8 *code_compiled;
  6862. BranchBlockPatch *patch_list;
  6863. /* This is used to save params frame_offset of of if block */
  6864. int16 *param_frame_offsets;
  6865. /* This is used to recover dynamic offset for else branch */
  6866. uint16 start_dynamic_offset;
  6867. #endif
  6868. /* Indicate the operand stack is in polymorphic state.
  6869. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  6870. * to polymorphic state until the block's 'end' opcode is processed.
  6871. * If stack is in polymorphic state and stack is empty, instruction can
  6872. * pop any type of value directly without decreasing stack top pointer
  6873. * and stack cell num. */
  6874. bool is_stack_polymorphic;
  6875. } BranchBlock;
  6876. typedef struct WASMLoaderContext {
  6877. /* frame ref stack */
  6878. uint8 *frame_ref;
  6879. uint8 *frame_ref_bottom;
  6880. uint8 *frame_ref_boundary;
  6881. uint32 frame_ref_size;
  6882. uint32 stack_cell_num;
  6883. uint32 max_stack_cell_num;
  6884. #if WASM_ENABLE_GC != 0
  6885. /* frame reftype map stack */
  6886. WASMRefTypeMap *frame_reftype_map;
  6887. WASMRefTypeMap *frame_reftype_map_bottom;
  6888. WASMRefTypeMap *frame_reftype_map_boundary;
  6889. uint32 frame_reftype_map_size;
  6890. uint32 reftype_map_num;
  6891. uint32 max_reftype_map_num;
  6892. /* Current module */
  6893. WASMModule *module;
  6894. /* Current module's ref_type_set */
  6895. HashMap *ref_type_set;
  6896. /* Always point to local variable ref_type of
  6897. wasm_loader_prepare_bytecode */
  6898. WASMRefType *ref_type_tmp;
  6899. #endif
  6900. /* frame csp stack */
  6901. BranchBlock *frame_csp;
  6902. BranchBlock *frame_csp_bottom;
  6903. BranchBlock *frame_csp_boundary;
  6904. uint32 frame_csp_size;
  6905. uint32 csp_num;
  6906. uint32 max_csp_num;
  6907. #if WASM_ENABLE_FAST_INTERP != 0
  6908. /* frame offset stack */
  6909. int16 *frame_offset;
  6910. int16 *frame_offset_bottom;
  6911. int16 *frame_offset_boundary;
  6912. uint32 frame_offset_size;
  6913. int16 dynamic_offset;
  6914. int16 start_dynamic_offset;
  6915. int16 max_dynamic_offset;
  6916. /* preserved local offset */
  6917. int16 preserved_local_offset;
  6918. /* const buffer */
  6919. uint8 *const_buf;
  6920. uint16 num_const;
  6921. uint16 const_cell_num;
  6922. uint32 const_buf_size;
  6923. /* processed code */
  6924. uint8 *p_code_compiled;
  6925. uint8 *p_code_compiled_end;
  6926. uint32 code_compiled_size;
  6927. /* If the last opcode will be dropped, the peak memory usage will be larger
  6928. * than the final code_compiled_size, we record the peak size to ensure
  6929. * there will not be invalid memory access during second traverse */
  6930. uint32 code_compiled_peak_size;
  6931. #endif
  6932. } WASMLoaderContext;
  6933. typedef struct Const {
  6934. WASMValue value;
  6935. uint16 slot_index;
  6936. uint8 value_type;
  6937. } Const;
  6938. #define CHECK_CSP_PUSH() \
  6939. do { \
  6940. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  6941. MEM_REALLOC( \
  6942. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  6943. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  6944. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  6945. ctx->frame_csp_boundary = \
  6946. ctx->frame_csp_bottom \
  6947. + ctx->frame_csp_size / sizeof(BranchBlock); \
  6948. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  6949. } \
  6950. } while (0)
  6951. #define CHECK_CSP_POP() \
  6952. do { \
  6953. if (ctx->csp_num < 1) { \
  6954. set_error_buf(error_buf, error_buf_size, \
  6955. "type mismatch: " \
  6956. "expect data but block stack was empty"); \
  6957. goto fail; \
  6958. } \
  6959. } while (0)
  6960. #if WASM_ENABLE_FAST_INTERP != 0
  6961. static bool
  6962. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  6963. uint32 error_buf_size)
  6964. {
  6965. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  6966. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  6967. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  6968. ctx->frame_offset_size + 16);
  6969. ctx->frame_offset_size += 16;
  6970. ctx->frame_offset_boundary =
  6971. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  6972. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  6973. }
  6974. return true;
  6975. fail:
  6976. return false;
  6977. }
  6978. static bool
  6979. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  6980. {
  6981. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  6982. return false;
  6983. return true;
  6984. }
  6985. static void
  6986. free_label_patch_list(BranchBlock *frame_csp)
  6987. {
  6988. BranchBlockPatch *label_patch = frame_csp->patch_list;
  6989. BranchBlockPatch *next;
  6990. while (label_patch != NULL) {
  6991. next = label_patch->next;
  6992. wasm_runtime_free(label_patch);
  6993. label_patch = next;
  6994. }
  6995. frame_csp->patch_list = NULL;
  6996. }
  6997. static void
  6998. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  6999. {
  7000. BranchBlock *tmp_csp = frame_csp;
  7001. for (uint32 i = 0; i < csp_num; i++) {
  7002. free_label_patch_list(tmp_csp);
  7003. tmp_csp++;
  7004. }
  7005. }
  7006. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7007. #if WASM_ENABLE_GC != 0
  7008. static bool
  7009. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7010. char *error_buf, uint32 error_buf_size)
  7011. {
  7012. BranchBlock *current_csp = ctx->frame_csp - 1;
  7013. uint32 local_mask_size;
  7014. if (local_count == 0) {
  7015. current_csp->local_use_mask_size = 0;
  7016. return true;
  7017. }
  7018. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7019. * else branch, we don't need to allocate memory again */
  7020. if (!current_csp->local_use_mask) {
  7021. local_mask_size = (local_count + 7) / sizeof(uint8);
  7022. if (!(current_csp->local_use_mask =
  7023. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7024. return false;
  7025. }
  7026. current_csp->local_use_mask_size = local_mask_size;
  7027. }
  7028. else {
  7029. local_mask_size = current_csp->local_use_mask_size;
  7030. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7031. }
  7032. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7033. /* For non-function blocks, inherit the use status from parent block */
  7034. BranchBlock *parent_csp = current_csp - 1;
  7035. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7036. bh_assert(parent_csp->local_use_mask);
  7037. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7038. parent_csp->local_use_mask, local_mask_size);
  7039. }
  7040. return true;
  7041. }
  7042. static void
  7043. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7044. {
  7045. BranchBlock *current_csp = ctx->frame_csp - 1;
  7046. bh_assert(current_csp->local_use_mask
  7047. || current_csp->local_use_mask_size == 0);
  7048. if (current_csp->local_use_mask) {
  7049. wasm_runtime_free(current_csp->local_use_mask);
  7050. }
  7051. current_csp->local_use_mask = NULL;
  7052. current_csp->local_use_mask_size = 0;
  7053. }
  7054. static void
  7055. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7056. {
  7057. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7058. uint32 i;
  7059. for (i = 0; i < ctx->csp_num; i++) {
  7060. if (tmp_csp->local_use_mask) {
  7061. wasm_runtime_free(tmp_csp->local_use_mask);
  7062. tmp_csp->local_use_mask = NULL;
  7063. tmp_csp->local_use_mask_size = 0;
  7064. }
  7065. tmp_csp++;
  7066. }
  7067. }
  7068. static void
  7069. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7070. {
  7071. BranchBlock *current_csp = ctx->frame_csp - 1;
  7072. uint32 byte_offset = index / sizeof(uint8);
  7073. uint32 bit_offset = index % sizeof(uint8);
  7074. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7075. bh_assert(current_csp->local_use_mask);
  7076. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7077. }
  7078. static bool
  7079. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7080. {
  7081. BranchBlock *current_csp = ctx->frame_csp - 1;
  7082. uint32 byte_offset = index / sizeof(uint8);
  7083. uint32 bit_offset = index % sizeof(uint8);
  7084. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7085. bh_assert(current_csp->local_use_mask);
  7086. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7087. ? true
  7088. : false;
  7089. }
  7090. #endif /* end of WASM_ENABLE_GC != 0 */
  7091. static void
  7092. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7093. {
  7094. if (ctx) {
  7095. if (ctx->frame_ref_bottom)
  7096. wasm_runtime_free(ctx->frame_ref_bottom);
  7097. #if WASM_ENABLE_GC != 0
  7098. if (ctx->frame_reftype_map_bottom)
  7099. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7100. #endif
  7101. if (ctx->frame_csp_bottom) {
  7102. #if WASM_ENABLE_FAST_INTERP != 0
  7103. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7104. #endif
  7105. #if WASM_ENABLE_GC != 0
  7106. wasm_loader_clean_all_local_use_masks(ctx);
  7107. #endif
  7108. wasm_runtime_free(ctx->frame_csp_bottom);
  7109. }
  7110. #if WASM_ENABLE_FAST_INTERP != 0
  7111. if (ctx->frame_offset_bottom)
  7112. wasm_runtime_free(ctx->frame_offset_bottom);
  7113. if (ctx->const_buf)
  7114. wasm_runtime_free(ctx->const_buf);
  7115. #endif
  7116. wasm_runtime_free(ctx);
  7117. }
  7118. }
  7119. static WASMLoaderContext *
  7120. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7121. {
  7122. WASMLoaderContext *loader_ctx =
  7123. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7124. if (!loader_ctx)
  7125. return NULL;
  7126. loader_ctx->frame_ref_size = 32;
  7127. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7128. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7129. goto fail;
  7130. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7131. #if WASM_ENABLE_GC != 0
  7132. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7133. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7134. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7135. error_buf_size)))
  7136. goto fail;
  7137. loader_ctx->frame_reftype_map_boundary =
  7138. loader_ctx->frame_reftype_map_bottom + 16;
  7139. #endif
  7140. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7141. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7142. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7143. goto fail;
  7144. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7145. #if WASM_ENABLE_EXCE_HANDLING != 0
  7146. func->exception_handler_count = 0;
  7147. #endif
  7148. #if WASM_ENABLE_FAST_INTERP != 0
  7149. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7150. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7151. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7152. error_buf_size)))
  7153. goto fail;
  7154. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7155. loader_ctx->num_const = 0;
  7156. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7157. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7158. error_buf, error_buf_size)))
  7159. goto fail;
  7160. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7161. set_error_buf(error_buf, error_buf_size,
  7162. "fast interpreter offset overflow");
  7163. goto fail;
  7164. }
  7165. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7166. loader_ctx->max_dynamic_offset =
  7167. func->param_cell_num + func->local_cell_num;
  7168. #endif
  7169. return loader_ctx;
  7170. fail:
  7171. wasm_loader_ctx_destroy(loader_ctx);
  7172. return NULL;
  7173. }
  7174. static bool
  7175. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7176. uint32 error_buf_size)
  7177. {
  7178. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7179. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7180. /* Increase the frame ref stack */
  7181. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7182. ctx->frame_ref_size + 16);
  7183. ctx->frame_ref_size += 16;
  7184. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7185. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7186. }
  7187. #if WASM_ENABLE_GC != 0
  7188. if (wasm_is_type_multi_byte_type(type)
  7189. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7190. /* Increase the frame reftype map stack */
  7191. bh_assert(
  7192. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7193. * sizeof(WASMRefTypeMap))
  7194. == ctx->frame_reftype_map_size);
  7195. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7196. ctx->frame_reftype_map_size
  7197. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7198. ctx->frame_reftype_map =
  7199. ctx->frame_reftype_map_bottom
  7200. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7201. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7202. ctx->frame_reftype_map_boundary =
  7203. ctx->frame_reftype_map_bottom
  7204. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7205. }
  7206. #endif
  7207. return true;
  7208. fail:
  7209. return false;
  7210. }
  7211. static bool
  7212. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7213. uint32 error_buf_size)
  7214. {
  7215. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7216. uint32 i;
  7217. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7218. return false;
  7219. #if WASM_ENABLE_GC != 0
  7220. if (wasm_is_type_multi_byte_type(type)) {
  7221. WASMRefType *ref_type;
  7222. if (!(ref_type =
  7223. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7224. error_buf, error_buf_size))) {
  7225. return false;
  7226. }
  7227. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7228. /* Increase the frame reftype map stack */
  7229. bh_assert((uint32)((ctx->frame_reftype_map
  7230. - ctx->frame_reftype_map_bottom)
  7231. * sizeof(WASMRefTypeMap))
  7232. == ctx->frame_reftype_map_size);
  7233. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7234. ctx->frame_reftype_map_size,
  7235. ctx->frame_reftype_map_size
  7236. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7237. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7238. + ctx->frame_reftype_map_size
  7239. / ((uint32)sizeof(WASMRefTypeMap));
  7240. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7241. ctx->frame_reftype_map_boundary =
  7242. ctx->frame_reftype_map_bottom
  7243. + ctx->frame_reftype_map_size
  7244. / ((uint32)sizeof(WASMRefTypeMap));
  7245. }
  7246. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7247. ctx->frame_reftype_map->ref_type = ref_type;
  7248. ctx->frame_reftype_map++;
  7249. ctx->reftype_map_num++;
  7250. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7251. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7252. }
  7253. #endif
  7254. for (i = 0; i < type_cell_num; i++)
  7255. *ctx->frame_ref++ = type;
  7256. ctx->stack_cell_num += type_cell_num;
  7257. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7258. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7259. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7260. set_error_buf(error_buf, error_buf_size,
  7261. "operand stack depth limit exceeded");
  7262. return false;
  7263. }
  7264. }
  7265. return true;
  7266. #if WASM_ENABLE_GC != 0
  7267. fail:
  7268. return false;
  7269. #endif
  7270. }
  7271. static bool
  7272. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7273. int32 stack_cell_num,
  7274. #if WASM_ENABLE_GC != 0
  7275. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7276. #endif
  7277. uint8 type,
  7278. #if WASM_ENABLE_GC != 0
  7279. WASMRefType *ref_type,
  7280. #endif
  7281. char *error_buf, uint32 error_buf_size)
  7282. {
  7283. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7284. #if WASM_ENABLE_GC != 0
  7285. WASMRefType *frame_reftype = NULL;
  7286. #endif
  7287. if (stack_cell_num < type_cell_num) {
  7288. set_error_buf(error_buf, error_buf_size,
  7289. "type mismatch: expect data but stack was empty");
  7290. return false;
  7291. }
  7292. #if WASM_ENABLE_GC == 0
  7293. for (i = 0; i < type_cell_num; i++) {
  7294. if (*(frame_ref - 1 - i) != type) {
  7295. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7296. "type mismatch: expect ", type2str(type),
  7297. " but got other");
  7298. return false;
  7299. }
  7300. }
  7301. #else
  7302. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7303. bh_assert(reftype_map_num > 0);
  7304. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7305. }
  7306. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7307. ref_type, ctx->module->types,
  7308. ctx->module->type_count)) {
  7309. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7310. "type mismatch: expect ", type2str(type),
  7311. " but got other");
  7312. return false;
  7313. }
  7314. for (i = 0; i < type_cell_num - 1; i++) {
  7315. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7316. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7317. "type mismatch: expect ", type2str(type),
  7318. " but got other");
  7319. return false;
  7320. }
  7321. }
  7322. #endif
  7323. return true;
  7324. }
  7325. static bool
  7326. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7327. uint32 error_buf_size)
  7328. {
  7329. int32 block_stack_cell_num =
  7330. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7331. #if WASM_ENABLE_GC != 0
  7332. int32 reftype_map_num =
  7333. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7334. #endif
  7335. if (block_stack_cell_num > 0) {
  7336. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7337. /* the stack top is a value of any type, return success */
  7338. return true;
  7339. }
  7340. #if WASM_ENABLE_GC != 0
  7341. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7342. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7343. WASMRefType *stack_top_ref_type = NULL;
  7344. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7345. bh_assert(reftype_map_num > 0);
  7346. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7347. }
  7348. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7349. ctx->ref_type_tmp, ctx->module->types,
  7350. ctx->module->type_count)) {
  7351. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7352. uint32 ref_type_struct_size =
  7353. wasm_reftype_struct_size(stack_top_ref_type);
  7354. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7355. stack_top_ref_type, ref_type_struct_size);
  7356. }
  7357. return true;
  7358. }
  7359. }
  7360. #endif
  7361. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7362. #if WASM_ENABLE_GC != 0
  7363. ctx->frame_reftype_map, reftype_map_num,
  7364. #endif
  7365. type,
  7366. #if WASM_ENABLE_GC != 0
  7367. ctx->ref_type_tmp,
  7368. #endif
  7369. error_buf, error_buf_size)) {
  7370. return false;
  7371. }
  7372. return true;
  7373. }
  7374. static bool
  7375. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7376. uint32 error_buf_size)
  7377. {
  7378. BranchBlock *cur_block = ctx->frame_csp - 1;
  7379. int32 available_stack_cell =
  7380. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7381. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7382. /* Directly return success if current block is in stack
  7383. polymorphic state while stack is empty. */
  7384. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7385. return true;
  7386. if (type == VALUE_TYPE_VOID)
  7387. return true;
  7388. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7389. return false;
  7390. bh_assert(available_stack_cell > 0);
  7391. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7392. type = VALUE_TYPE_ANY;
  7393. cell_num_to_pop = 1;
  7394. }
  7395. ctx->frame_ref -= cell_num_to_pop;
  7396. ctx->stack_cell_num -= cell_num_to_pop;
  7397. #if WASM_ENABLE_GC != 0
  7398. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7399. ctx->frame_reftype_map--;
  7400. ctx->reftype_map_num--;
  7401. }
  7402. #endif
  7403. return true;
  7404. }
  7405. #if WASM_ENABLE_GC != 0
  7406. /* Get the stack top element of current block */
  7407. static bool
  7408. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7409. WASMRefType **p_ref_type, char *error_buf,
  7410. uint32 error_buf_size)
  7411. {
  7412. BranchBlock *cur_block = ctx->frame_csp - 1;
  7413. int32 available_stack_cell =
  7414. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7415. if (available_stack_cell <= 0) {
  7416. /* Directly return success if current block is in stack
  7417. polymorphic state while stack is empty. */
  7418. if (cur_block->is_stack_polymorphic) {
  7419. *p_type = VALUE_TYPE_ANY;
  7420. return true;
  7421. }
  7422. else {
  7423. set_error_buf(
  7424. error_buf, error_buf_size,
  7425. "type mismatch: expect data but block stack was empty");
  7426. return false;
  7427. }
  7428. }
  7429. *p_type = *(ctx->frame_ref - 1);
  7430. if (wasm_is_type_multi_byte_type(*p_type)) {
  7431. int32 available_reftype_map =
  7432. (int32)(ctx->reftype_map_num
  7433. - (ctx->frame_csp - 1)->reftype_map_num);
  7434. bh_assert(available_reftype_map > 0);
  7435. (void)available_reftype_map;
  7436. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7437. }
  7438. return true;
  7439. }
  7440. #if WASM_ENABLE_FAST_INTERP != 0
  7441. static bool
  7442. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7443. char *error_buf, uint32 error_buf_size);
  7444. #endif
  7445. /* Check whether the stack top elem is a heap object, and if yes,
  7446. pop and return it */
  7447. static bool
  7448. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7449. WASMRefType *ref_ht_ret, char *error_buf,
  7450. uint32 error_buf_size)
  7451. {
  7452. uint8 type = 0;
  7453. WASMRefType *ref_type = NULL;
  7454. /* Get stack top element */
  7455. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7456. error_buf_size)) {
  7457. return false;
  7458. }
  7459. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7460. /* stack top isn't a ref type */
  7461. && !wasm_is_type_reftype(type)) {
  7462. set_error_buf(error_buf, error_buf_size,
  7463. "type mismatch: expect heap object but got others");
  7464. return false;
  7465. }
  7466. /* POP stack top */
  7467. if (wasm_is_type_multi_byte_type(type)) {
  7468. bh_assert(ref_type);
  7469. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7470. wasm_reftype_struct_size(ref_type));
  7471. }
  7472. #if WASM_ENABLE_FAST_INTERP != 0
  7473. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7474. error_buf_size)) {
  7475. return false;
  7476. }
  7477. #else
  7478. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7479. return false;
  7480. }
  7481. #endif
  7482. if (p_type)
  7483. *p_type = type;
  7484. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7485. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7486. wasm_reftype_struct_size(ref_type));
  7487. }
  7488. return true;
  7489. }
  7490. /* Check whether the stack top elem is subtype of (ref null ht),
  7491. and if yes, pop it and return the converted (ref ht) */
  7492. static bool
  7493. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7494. WASMRefType *ref_ht_ret, char *error_buf,
  7495. uint32 error_buf_size)
  7496. {
  7497. uint8 type = 0;
  7498. WASMRefType ref_type = { 0 };
  7499. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7500. error_buf_size)) {
  7501. return false;
  7502. }
  7503. /* Convert to related (ref ht) and return */
  7504. if ((type >= REF_TYPE_EQREF && type <= REF_TYPE_FUNCREF)
  7505. || (type >= REF_TYPE_NULLREF && type <= REF_TYPE_I31REF)) {
  7506. /* Return (ref func/extern/any/eq/i31/nofunc/noextern/struct/array/none)
  7507. */
  7508. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7509. HEAP_TYPE_FUNC + (type - REF_TYPE_FUNCREF));
  7510. type = ref_ht_ret->ref_type;
  7511. }
  7512. else if (wasm_is_reftype_htref_nullable(type)
  7513. || wasm_is_reftype_htref_non_nullable(type)) {
  7514. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7515. wasm_reftype_struct_size(&ref_type));
  7516. /* Convert to (ref ht) */
  7517. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7518. ref_ht_ret->ref_ht_common.nullable = false;
  7519. type = ref_ht_ret->ref_type;
  7520. }
  7521. *p_type = type;
  7522. return true;
  7523. }
  7524. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7525. and if yes, pop it and return the type_idx */
  7526. static bool
  7527. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7528. uint32 *p_type_idx, char *error_buf,
  7529. uint32 error_buf_size)
  7530. {
  7531. uint8 type = 0;
  7532. int32 type_idx = -1;
  7533. WASMRefType *ref_type = NULL;
  7534. /* Get stack top element */
  7535. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7536. error_buf_size)) {
  7537. return false;
  7538. }
  7539. if (type != VALUE_TYPE_ANY) {
  7540. /* stack top isn't (ref null $t) */
  7541. if (!((wasm_is_reftype_htref_nullable(type)
  7542. || wasm_is_reftype_htref_non_nullable(type))
  7543. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7544. set_error_buf(error_buf, error_buf_size,
  7545. "type mismatch: expect (ref null $t) but got others");
  7546. return false;
  7547. }
  7548. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7549. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7550. wasm_reftype_struct_size(ref_type));
  7551. }
  7552. /* POP stack top */
  7553. #if WASM_ENABLE_FAST_INTERP != 0
  7554. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7555. error_buf_size)) {
  7556. return false;
  7557. }
  7558. #else
  7559. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7560. return false;
  7561. }
  7562. #endif
  7563. /* Convert to type_idx and return */
  7564. *p_type = type;
  7565. if (type != VALUE_TYPE_ANY)
  7566. *p_type_idx = (uint32)type_idx;
  7567. return true;
  7568. }
  7569. #endif /* WASM_ENABLE_GC != 0 */
  7570. #if WASM_ENABLE_FAST_INTERP == 0
  7571. static bool
  7572. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7573. uint8 type_push, uint8 type_pop, char *error_buf,
  7574. uint32 error_buf_size)
  7575. {
  7576. for (int i = 0; i < pop_cnt; i++) {
  7577. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7578. error_buf_size))
  7579. return false;
  7580. }
  7581. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7582. return false;
  7583. return true;
  7584. }
  7585. #endif
  7586. static bool
  7587. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7588. BlockType block_type, uint8 *start_addr,
  7589. char *error_buf, uint32 error_buf_size)
  7590. {
  7591. CHECK_CSP_PUSH();
  7592. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7593. ctx->frame_csp->label_type = label_type;
  7594. ctx->frame_csp->block_type = block_type;
  7595. ctx->frame_csp->start_addr = start_addr;
  7596. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7597. #if WASM_ENABLE_GC != 0
  7598. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7599. #endif
  7600. #if WASM_ENABLE_FAST_INTERP != 0
  7601. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7602. ctx->frame_csp->patch_list = NULL;
  7603. #endif
  7604. ctx->frame_csp++;
  7605. ctx->csp_num++;
  7606. if (ctx->csp_num > ctx->max_csp_num) {
  7607. ctx->max_csp_num = ctx->csp_num;
  7608. if (ctx->max_csp_num > UINT16_MAX) {
  7609. set_error_buf(error_buf, error_buf_size,
  7610. "label stack depth limit exceeded");
  7611. return false;
  7612. }
  7613. }
  7614. return true;
  7615. fail:
  7616. return false;
  7617. }
  7618. static bool
  7619. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7620. uint32 error_buf_size)
  7621. {
  7622. CHECK_CSP_POP();
  7623. #if WASM_ENABLE_FAST_INTERP != 0
  7624. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7625. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7626. #endif
  7627. ctx->frame_csp--;
  7628. ctx->csp_num--;
  7629. return true;
  7630. fail:
  7631. return false;
  7632. }
  7633. #if WASM_ENABLE_FAST_INTERP != 0
  7634. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7635. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7636. #define emit_label(opcode) \
  7637. do { \
  7638. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7639. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7640. } while (0)
  7641. #define skip_label() \
  7642. do { \
  7643. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7644. LOG_OP("\ndelete last op\n"); \
  7645. } while (0)
  7646. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7647. #if UINTPTR_MAX == UINT64_MAX
  7648. #define emit_label(opcode) \
  7649. do { \
  7650. int32 offset = \
  7651. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7652. /* emit int32 relative offset in 64-bit target */ \
  7653. wasm_loader_emit_uint32(loader_ctx, offset); \
  7654. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7655. } while (0)
  7656. #else
  7657. #define emit_label(opcode) \
  7658. do { \
  7659. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7660. /* emit uint32 label address in 32-bit target */ \
  7661. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7662. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7663. } while (0)
  7664. #endif
  7665. #define skip_label() \
  7666. do { \
  7667. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7668. LOG_OP("\ndelete last op\n"); \
  7669. } while (0)
  7670. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7671. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7672. #define emit_label(opcode) \
  7673. do { \
  7674. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7675. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7676. } while (0)
  7677. #define skip_label() \
  7678. do { \
  7679. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7680. LOG_OP("\ndelete last op\n"); \
  7681. } while (0)
  7682. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7683. #define emit_empty_label_addr_and_frame_ip(type) \
  7684. do { \
  7685. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7686. loader_ctx->p_code_compiled, error_buf, \
  7687. error_buf_size)) \
  7688. goto fail; \
  7689. /* label address, to be patched */ \
  7690. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7691. } while (0)
  7692. #define emit_br_info(frame_csp) \
  7693. do { \
  7694. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  7695. error_buf_size)) \
  7696. goto fail; \
  7697. } while (0)
  7698. #define LAST_OP_OUTPUT_I32() \
  7699. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7700. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7701. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7702. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7703. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7704. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7705. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7706. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7707. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7708. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7709. || (last_op == EXT_OP_COPY_STACK_TOP)
  7710. #define LAST_OP_OUTPUT_I64() \
  7711. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7712. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7713. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7714. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7715. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7716. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7717. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7718. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7719. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7720. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7721. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7722. #define GET_CONST_OFFSET(type, val) \
  7723. do { \
  7724. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7725. &operand_offset, error_buf, \
  7726. error_buf_size))) \
  7727. goto fail; \
  7728. } while (0)
  7729. #define GET_CONST_F32_OFFSET(type, fval) \
  7730. do { \
  7731. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7732. &operand_offset, error_buf, \
  7733. error_buf_size))) \
  7734. goto fail; \
  7735. } while (0)
  7736. #define GET_CONST_F64_OFFSET(type, fval) \
  7737. do { \
  7738. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7739. &operand_offset, error_buf, \
  7740. error_buf_size))) \
  7741. goto fail; \
  7742. } while (0)
  7743. #define emit_operand(ctx, offset) \
  7744. do { \
  7745. wasm_loader_emit_int16(ctx, offset); \
  7746. LOG_OP("%d\t", offset); \
  7747. } while (0)
  7748. #define emit_byte(ctx, byte) \
  7749. do { \
  7750. wasm_loader_emit_uint8(ctx, byte); \
  7751. LOG_OP("%d\t", byte); \
  7752. } while (0)
  7753. #define emit_uint32(ctx, value) \
  7754. do { \
  7755. wasm_loader_emit_uint32(ctx, value); \
  7756. LOG_OP("%d\t", value); \
  7757. } while (0)
  7758. #define emit_uint64(ctx, value) \
  7759. do { \
  7760. wasm_loader_emit_const(ctx, &value, false); \
  7761. LOG_OP("%lld\t", value); \
  7762. } while (0)
  7763. #define emit_float32(ctx, value) \
  7764. do { \
  7765. wasm_loader_emit_const(ctx, &value, true); \
  7766. LOG_OP("%f\t", value); \
  7767. } while (0)
  7768. #define emit_float64(ctx, value) \
  7769. do { \
  7770. wasm_loader_emit_const(ctx, &value, false); \
  7771. LOG_OP("%f\t", value); \
  7772. } while (0)
  7773. static bool
  7774. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7775. {
  7776. if (!(ctx->p_code_compiled =
  7777. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7778. return false;
  7779. ctx->p_code_compiled_end =
  7780. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7781. /* clean up frame ref */
  7782. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7783. ctx->frame_ref = ctx->frame_ref_bottom;
  7784. ctx->stack_cell_num = 0;
  7785. #if WASM_ENABLE_GC != 0
  7786. /* clean up reftype map */
  7787. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7788. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7789. ctx->reftype_map_num = 0;
  7790. #endif
  7791. /* clean up frame csp */
  7792. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7793. ctx->frame_csp = ctx->frame_csp_bottom;
  7794. ctx->csp_num = 0;
  7795. ctx->max_csp_num = 0;
  7796. /* clean up frame offset */
  7797. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7798. ctx->frame_offset = ctx->frame_offset_bottom;
  7799. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7800. /* init preserved local offsets */
  7801. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7802. /* const buf is reserved */
  7803. return true;
  7804. }
  7805. static void
  7806. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  7807. {
  7808. ctx->code_compiled_size += size;
  7809. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  7810. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  7811. }
  7812. }
  7813. static void
  7814. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  7815. {
  7816. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  7817. if (ctx->p_code_compiled) {
  7818. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7819. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7820. #endif
  7821. bh_memcpy_s(ctx->p_code_compiled,
  7822. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  7823. value, size);
  7824. ctx->p_code_compiled += size;
  7825. }
  7826. else {
  7827. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7828. bh_assert((ctx->code_compiled_size & 1) == 0);
  7829. #endif
  7830. increase_compiled_code_space(ctx, size);
  7831. }
  7832. }
  7833. static void
  7834. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  7835. {
  7836. if (ctx->p_code_compiled) {
  7837. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7838. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7839. #endif
  7840. STORE_U32(ctx->p_code_compiled, value);
  7841. ctx->p_code_compiled += sizeof(uint32);
  7842. }
  7843. else {
  7844. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7845. bh_assert((ctx->code_compiled_size & 1) == 0);
  7846. #endif
  7847. increase_compiled_code_space(ctx, sizeof(uint32));
  7848. }
  7849. }
  7850. static void
  7851. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  7852. {
  7853. if (ctx->p_code_compiled) {
  7854. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7855. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7856. #endif
  7857. STORE_U16(ctx->p_code_compiled, (uint16)value);
  7858. ctx->p_code_compiled += sizeof(int16);
  7859. }
  7860. else {
  7861. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7862. bh_assert((ctx->code_compiled_size & 1) == 0);
  7863. #endif
  7864. increase_compiled_code_space(ctx, sizeof(uint16));
  7865. }
  7866. }
  7867. static void
  7868. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  7869. {
  7870. if (ctx->p_code_compiled) {
  7871. *(ctx->p_code_compiled) = value;
  7872. ctx->p_code_compiled += sizeof(uint8);
  7873. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7874. ctx->p_code_compiled++;
  7875. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7876. #endif
  7877. }
  7878. else {
  7879. increase_compiled_code_space(ctx, sizeof(uint8));
  7880. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7881. increase_compiled_code_space(ctx, sizeof(uint8));
  7882. bh_assert((ctx->code_compiled_size & 1) == 0);
  7883. #endif
  7884. }
  7885. }
  7886. static void
  7887. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  7888. {
  7889. if (ctx->p_code_compiled) {
  7890. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7891. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7892. #endif
  7893. STORE_PTR(ctx->p_code_compiled, value);
  7894. ctx->p_code_compiled += sizeof(void *);
  7895. }
  7896. else {
  7897. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7898. bh_assert((ctx->code_compiled_size & 1) == 0);
  7899. #endif
  7900. increase_compiled_code_space(ctx, sizeof(void *));
  7901. }
  7902. }
  7903. static void
  7904. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  7905. {
  7906. if (ctx->p_code_compiled) {
  7907. ctx->p_code_compiled -= size;
  7908. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7909. if (size == sizeof(uint8)) {
  7910. ctx->p_code_compiled--;
  7911. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7912. }
  7913. #endif
  7914. }
  7915. else {
  7916. ctx->code_compiled_size -= size;
  7917. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7918. if (size == sizeof(uint8)) {
  7919. ctx->code_compiled_size--;
  7920. bh_assert((ctx->code_compiled_size & 1) == 0);
  7921. }
  7922. #endif
  7923. }
  7924. }
  7925. static bool
  7926. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  7927. uint32 local_index, uint32 local_type,
  7928. bool *preserved, char *error_buf,
  7929. uint32 error_buf_size)
  7930. {
  7931. uint32 i = 0;
  7932. int16 preserved_offset = (int16)local_index;
  7933. *preserved = false;
  7934. while (i < loader_ctx->stack_cell_num) {
  7935. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  7936. /* move previous local into dynamic space before a set/tee_local opcode
  7937. */
  7938. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  7939. if (!(*preserved)) {
  7940. *preserved = true;
  7941. skip_label();
  7942. preserved_offset = loader_ctx->preserved_local_offset;
  7943. if (loader_ctx->p_code_compiled) {
  7944. bh_assert(preserved_offset != (int16)local_index);
  7945. }
  7946. if (is_32bit_type(local_type)) {
  7947. /* Only increase preserve offset in the second traversal */
  7948. if (loader_ctx->p_code_compiled)
  7949. loader_ctx->preserved_local_offset++;
  7950. emit_label(EXT_OP_COPY_STACK_TOP);
  7951. }
  7952. else {
  7953. if (loader_ctx->p_code_compiled)
  7954. loader_ctx->preserved_local_offset += 2;
  7955. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  7956. }
  7957. emit_operand(loader_ctx, local_index);
  7958. emit_operand(loader_ctx, preserved_offset);
  7959. emit_label(opcode);
  7960. }
  7961. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  7962. }
  7963. if (is_32bit_type(cur_type))
  7964. i++;
  7965. else
  7966. i += 2;
  7967. }
  7968. (void)error_buf;
  7969. (void)error_buf_size;
  7970. return true;
  7971. }
  7972. static bool
  7973. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  7974. char *error_buf, uint32 error_buf_size)
  7975. {
  7976. uint32 i = 0;
  7977. bool preserve_local;
  7978. /* preserve locals before blocks to ensure that "tee/set_local" inside
  7979. blocks will not influence the value of these locals */
  7980. while (i < loader_ctx->stack_cell_num) {
  7981. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  7982. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  7983. if ((cur_offset < loader_ctx->start_dynamic_offset)
  7984. && (cur_offset >= 0)) {
  7985. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  7986. cur_type, &preserve_local,
  7987. error_buf, error_buf_size)))
  7988. return false;
  7989. }
  7990. if (is_32bit_type(cur_type)) {
  7991. i++;
  7992. }
  7993. else {
  7994. i += 2;
  7995. }
  7996. }
  7997. return true;
  7998. }
  7999. static bool
  8000. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8001. uint8 *p_code_compiled, char *error_buf,
  8002. uint32 error_buf_size)
  8003. {
  8004. BranchBlockPatch *patch =
  8005. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8006. if (!patch) {
  8007. return false;
  8008. }
  8009. patch->patch_type = patch_type;
  8010. patch->code_compiled = p_code_compiled;
  8011. if (!frame_csp->patch_list) {
  8012. frame_csp->patch_list = patch;
  8013. patch->next = NULL;
  8014. }
  8015. else {
  8016. patch->next = frame_csp->patch_list;
  8017. frame_csp->patch_list = patch;
  8018. }
  8019. return true;
  8020. }
  8021. static void
  8022. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8023. {
  8024. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8025. BranchBlockPatch *node = frame_csp->patch_list;
  8026. BranchBlockPatch *node_prev = NULL, *node_next;
  8027. if (!ctx->p_code_compiled)
  8028. return;
  8029. while (node) {
  8030. node_next = node->next;
  8031. if (node->patch_type == patch_type) {
  8032. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8033. if (node_prev == NULL) {
  8034. frame_csp->patch_list = node_next;
  8035. }
  8036. else {
  8037. node_prev->next = node_next;
  8038. }
  8039. wasm_runtime_free(node);
  8040. }
  8041. else {
  8042. node_prev = node;
  8043. }
  8044. node = node_next;
  8045. }
  8046. }
  8047. static bool
  8048. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8049. char *error_buf, uint32 error_buf_size)
  8050. {
  8051. /* br info layout:
  8052. * a) arity of target block
  8053. * b) total cell num of arity values
  8054. * c) each arity value's cell num
  8055. * d) each arity value's src frame offset
  8056. * e) each arity values's dst dynamic offset
  8057. * f) branch target address
  8058. *
  8059. * Note: b-e are omitted when arity is 0 so that
  8060. * interpreter can recover the br info quickly.
  8061. */
  8062. BlockType *block_type = &frame_csp->block_type;
  8063. uint8 *types = NULL, cell;
  8064. #if WASM_ENABLE_GC != 0
  8065. WASMRefTypeMap *reftype_maps;
  8066. uint32 reftype_map_count;
  8067. #endif
  8068. uint32 arity = 0;
  8069. int32 i;
  8070. int16 *frame_offset = ctx->frame_offset;
  8071. uint16 dynamic_offset;
  8072. /* Note: loop's arity is different from if and block. loop's arity is
  8073. * its parameter count while if and block arity is result count.
  8074. */
  8075. #if WASM_ENABLE_GC == 0
  8076. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8077. arity = block_type_get_param_types(block_type, &types);
  8078. else
  8079. arity = block_type_get_result_types(block_type, &types);
  8080. #else
  8081. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8082. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8083. &reftype_map_count);
  8084. else
  8085. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8086. &reftype_map_count);
  8087. #endif
  8088. /* Part a */
  8089. emit_uint32(ctx, arity);
  8090. if (arity) {
  8091. /* Part b */
  8092. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8093. /* Part c */
  8094. for (i = (int32)arity - 1; i >= 0; i--) {
  8095. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8096. emit_byte(ctx, cell);
  8097. }
  8098. /* Part d */
  8099. for (i = (int32)arity - 1; i >= 0; i--) {
  8100. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8101. frame_offset -= cell;
  8102. emit_operand(ctx, *(int16 *)(frame_offset));
  8103. }
  8104. /* Part e */
  8105. dynamic_offset =
  8106. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8107. ctx->dynamic_offset = dynamic_offset;
  8108. for (i = (int32)arity - 1; i >= 0; i--) {
  8109. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8110. dynamic_offset -= cell;
  8111. emit_operand(ctx, dynamic_offset);
  8112. }
  8113. }
  8114. /* Part f */
  8115. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8116. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8117. }
  8118. else {
  8119. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8120. error_buf, error_buf_size))
  8121. return false;
  8122. /* label address, to be patched */
  8123. wasm_loader_emit_ptr(ctx, NULL);
  8124. }
  8125. return true;
  8126. }
  8127. static bool
  8128. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8129. bool disable_emit, int16 operand_offset,
  8130. char *error_buf, uint32 error_buf_size)
  8131. {
  8132. if (type == VALUE_TYPE_VOID)
  8133. return true;
  8134. /* only check memory overflow in first traverse */
  8135. if (ctx->p_code_compiled == NULL) {
  8136. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8137. return false;
  8138. }
  8139. if (disable_emit)
  8140. *(ctx->frame_offset)++ = operand_offset;
  8141. else {
  8142. emit_operand(ctx, ctx->dynamic_offset);
  8143. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8144. ctx->dynamic_offset++;
  8145. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8146. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8147. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8148. goto fail;
  8149. }
  8150. }
  8151. }
  8152. if (is_32bit_type(type))
  8153. return true;
  8154. if (ctx->p_code_compiled == NULL) {
  8155. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8156. return false;
  8157. }
  8158. ctx->frame_offset++;
  8159. if (!disable_emit) {
  8160. ctx->dynamic_offset++;
  8161. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8162. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8163. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8164. goto fail;
  8165. }
  8166. }
  8167. }
  8168. return true;
  8169. fail:
  8170. set_error_buf(error_buf, error_buf_size,
  8171. "fast interpreter offset overflow");
  8172. return false;
  8173. }
  8174. /* This function should be in front of wasm_loader_pop_frame_ref
  8175. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8176. will be modified by wasm_loader_pop_frame_ref */
  8177. static bool
  8178. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8179. char *error_buf, uint32 error_buf_size)
  8180. {
  8181. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8182. then current block is the function block */
  8183. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8184. BranchBlock *cur_block = ctx->frame_csp - depth;
  8185. int32 available_stack_cell =
  8186. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8187. /* Directly return success if current block is in stack
  8188. * polymorphic state while stack is empty. */
  8189. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8190. return true;
  8191. if (type == VALUE_TYPE_VOID)
  8192. return true;
  8193. if (is_32bit_type(type)) {
  8194. /* Check the offset stack bottom to ensure the frame offset
  8195. stack will not go underflow. But we don't thrown error
  8196. and return true here, because the error msg should be
  8197. given in wasm_loader_pop_frame_ref */
  8198. if (!check_offset_pop(ctx, 1))
  8199. return true;
  8200. ctx->frame_offset -= 1;
  8201. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8202. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8203. ctx->dynamic_offset -= 1;
  8204. }
  8205. else {
  8206. if (!check_offset_pop(ctx, 2))
  8207. return true;
  8208. ctx->frame_offset -= 2;
  8209. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8210. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8211. ctx->dynamic_offset -= 2;
  8212. }
  8213. emit_operand(ctx, *(ctx->frame_offset));
  8214. (void)error_buf;
  8215. (void)error_buf_size;
  8216. return true;
  8217. }
  8218. static bool
  8219. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8220. bool disable_emit, int16 operand_offset,
  8221. char *error_buf, uint32 error_buf_size)
  8222. {
  8223. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8224. error_buf, error_buf_size)))
  8225. return false;
  8226. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8227. return false;
  8228. return true;
  8229. }
  8230. static bool
  8231. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8232. char *error_buf, uint32 error_buf_size)
  8233. {
  8234. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8235. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8236. return false;
  8237. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8238. return false;
  8239. return true;
  8240. }
  8241. static bool
  8242. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8243. uint8 type_push, uint8 type_pop,
  8244. bool disable_emit, int16 operand_offset,
  8245. char *error_buf, uint32 error_buf_size)
  8246. {
  8247. uint8 i;
  8248. for (i = 0; i < pop_cnt; i++) {
  8249. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8250. error_buf_size))
  8251. return false;
  8252. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8253. error_buf_size))
  8254. return false;
  8255. }
  8256. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8257. operand_offset, error_buf,
  8258. error_buf_size))
  8259. return false;
  8260. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8261. return false;
  8262. return true;
  8263. }
  8264. static bool
  8265. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8266. int16 *offset, char *error_buf,
  8267. uint32 error_buf_size)
  8268. {
  8269. int8 bytes_to_increase;
  8270. int16 operand_offset = 0;
  8271. Const *c;
  8272. /* Search existing constant */
  8273. for (c = (Const *)ctx->const_buf;
  8274. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8275. /* TODO: handle v128 type? */
  8276. if ((type == c->value_type)
  8277. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8278. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8279. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8280. || (type == VALUE_TYPE_FUNCREF
  8281. && *(int32 *)value == c->value.i32)
  8282. || (type == VALUE_TYPE_EXTERNREF
  8283. && *(int32 *)value == c->value.i32)
  8284. #endif
  8285. || (type == VALUE_TYPE_F64
  8286. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8287. || (type == VALUE_TYPE_F32
  8288. && (0
  8289. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8290. operand_offset = c->slot_index;
  8291. break;
  8292. }
  8293. if (is_32bit_type(c->value_type))
  8294. operand_offset += 1;
  8295. else
  8296. operand_offset += 2;
  8297. }
  8298. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8299. /* New constant, append to the const buffer */
  8300. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8301. bytes_to_increase = 2;
  8302. }
  8303. else {
  8304. bytes_to_increase = 1;
  8305. }
  8306. /* The max cell num of const buffer is 32768 since the valid index range
  8307. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8308. * full */
  8309. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8310. *offset = 0;
  8311. return true;
  8312. }
  8313. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8314. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8315. ctx->const_buf_size + 4 * sizeof(Const));
  8316. ctx->const_buf_size += 4 * sizeof(Const);
  8317. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8318. }
  8319. c->value_type = type;
  8320. switch (type) {
  8321. case VALUE_TYPE_F64:
  8322. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8323. sizeof(float64));
  8324. ctx->const_cell_num += 2;
  8325. /* The const buf will be reversed, we use the second cell */
  8326. /* of the i64/f64 const so the finnal offset is corrent */
  8327. operand_offset++;
  8328. break;
  8329. case VALUE_TYPE_I64:
  8330. c->value.i64 = *(int64 *)value;
  8331. ctx->const_cell_num += 2;
  8332. operand_offset++;
  8333. break;
  8334. case VALUE_TYPE_F32:
  8335. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8336. sizeof(float32));
  8337. ctx->const_cell_num++;
  8338. break;
  8339. case VALUE_TYPE_I32:
  8340. c->value.i32 = *(int32 *)value;
  8341. ctx->const_cell_num++;
  8342. break;
  8343. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8344. case VALUE_TYPE_EXTERNREF:
  8345. case VALUE_TYPE_FUNCREF:
  8346. c->value.i32 = *(int32 *)value;
  8347. ctx->const_cell_num++;
  8348. break;
  8349. #endif
  8350. default:
  8351. break;
  8352. }
  8353. c->slot_index = operand_offset;
  8354. ctx->num_const++;
  8355. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8356. (int64)c->value.i64);
  8357. }
  8358. /* use negetive index for const */
  8359. operand_offset = -(operand_offset + 1);
  8360. *offset = operand_offset;
  8361. return true;
  8362. fail:
  8363. return false;
  8364. }
  8365. /*
  8366. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8367. -- Mostly used for the binary / compare operation
  8368. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8369. -- Mostly used in block / control instructions
  8370. The POP will always emit the offset on the top of the frame_offset stack
  8371. PUSH can be used in two ways:
  8372. 1. directly PUSH:
  8373. PUSH_XXX();
  8374. will allocate a dynamic space and emit
  8375. 2. silent PUSH:
  8376. operand_offset = xxx; disable_emit = true;
  8377. PUSH_XXX();
  8378. only push the frame_offset stack, no emit
  8379. */
  8380. #define TEMPLATE_PUSH(Type) \
  8381. do { \
  8382. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8383. disable_emit, operand_offset, \
  8384. error_buf, error_buf_size)) \
  8385. goto fail; \
  8386. } while (0)
  8387. #define TEMPLATE_PUSH_REF(Type) \
  8388. do { \
  8389. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8390. operand_offset, error_buf, \
  8391. error_buf_size)) \
  8392. goto fail; \
  8393. } while (0)
  8394. #define TEMPLATE_POP(Type) \
  8395. do { \
  8396. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8397. error_buf, error_buf_size)) \
  8398. goto fail; \
  8399. } while (0)
  8400. #define TEMPLATE_POP_REF(Type) \
  8401. do { \
  8402. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8403. error_buf_size)) \
  8404. goto fail; \
  8405. } while (0)
  8406. #define PUSH_OFFSET_TYPE(type) \
  8407. do { \
  8408. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8409. operand_offset, error_buf, \
  8410. error_buf_size))) \
  8411. goto fail; \
  8412. } while (0)
  8413. #define POP_OFFSET_TYPE(type) \
  8414. do { \
  8415. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8416. error_buf_size))) \
  8417. goto fail; \
  8418. } while (0)
  8419. #define POP_AND_PUSH(type_pop, type_push) \
  8420. do { \
  8421. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8422. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8423. operand_offset, error_buf, error_buf_size))) \
  8424. goto fail; \
  8425. } while (0)
  8426. /* type of POPs should be the same */
  8427. #define POP2_AND_PUSH(type_pop, type_push) \
  8428. do { \
  8429. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8430. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8431. operand_offset, error_buf, error_buf_size))) \
  8432. goto fail; \
  8433. } while (0)
  8434. #else /* WASM_ENABLE_FAST_INTERP */
  8435. #define TEMPLATE_PUSH(Type) \
  8436. do { \
  8437. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8438. error_buf, error_buf_size))) \
  8439. goto fail; \
  8440. } while (0)
  8441. #define TEMPLATE_PUSH_REF(Type) \
  8442. do { \
  8443. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8444. error_buf_size))) \
  8445. goto fail; \
  8446. } while (0)
  8447. #define TEMPLATE_POP(Type) \
  8448. do { \
  8449. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8450. error_buf, error_buf_size))) \
  8451. goto fail; \
  8452. } while (0)
  8453. #define TEMPLATE_POP_REF(Type) \
  8454. do { \
  8455. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8456. error_buf_size))) \
  8457. goto fail; \
  8458. } while (0)
  8459. #define POP_AND_PUSH(type_pop, type_push) \
  8460. do { \
  8461. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8462. type_pop, error_buf, \
  8463. error_buf_size))) \
  8464. goto fail; \
  8465. } while (0)
  8466. /* type of POPs should be the same */
  8467. #define POP2_AND_PUSH(type_pop, type_push) \
  8468. do { \
  8469. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8470. type_pop, error_buf, \
  8471. error_buf_size))) \
  8472. goto fail; \
  8473. } while (0)
  8474. #endif /* WASM_ENABLE_FAST_INTERP */
  8475. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8476. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8477. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8478. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8479. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8480. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8481. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8482. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8483. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8484. #define POP_I32() TEMPLATE_POP(I32)
  8485. #define POP_F32() TEMPLATE_POP(F32)
  8486. #define POP_I64() TEMPLATE_POP(I64)
  8487. #define POP_F64() TEMPLATE_POP(F64)
  8488. #define POP_V128() TEMPLATE_POP(V128)
  8489. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8490. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8491. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8492. #if WASM_ENABLE_FAST_INTERP != 0
  8493. static bool
  8494. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8495. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8496. {
  8497. int16 operand_offset = 0;
  8498. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8499. : loader_ctx->frame_csp;
  8500. BlockType *block_type = &block->block_type;
  8501. uint8 *return_types = NULL;
  8502. #if WASM_ENABLE_GC != 0
  8503. WASMRefTypeMap *reftype_maps = NULL;
  8504. uint32 reftype_map_count;
  8505. #endif
  8506. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8507. int32 i = 0;
  8508. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8509. *frame_offset_org = NULL;
  8510. #if WASM_ENABLE_GC == 0
  8511. return_count = block_type_get_result_types(block_type, &return_types);
  8512. #else
  8513. return_count = block_type_get_result_types(
  8514. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8515. #endif
  8516. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8517. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8518. if (return_count == 1) {
  8519. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8520. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8521. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8522. /* insert op_copy before else opcode */
  8523. if (opcode == WASM_OP_ELSE)
  8524. skip_label();
  8525. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8526. : EXT_OP_COPY_STACK_TOP_I64);
  8527. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8528. emit_operand(loader_ctx, block->dynamic_offset);
  8529. if (opcode == WASM_OP_ELSE) {
  8530. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8531. }
  8532. else {
  8533. loader_ctx->frame_offset -= cell;
  8534. loader_ctx->dynamic_offset = block->dynamic_offset;
  8535. PUSH_OFFSET_TYPE(return_types[0]);
  8536. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8537. }
  8538. if (opcode == WASM_OP_ELSE)
  8539. emit_label(opcode);
  8540. }
  8541. return true;
  8542. }
  8543. /* Copy stack top values to block's results which are in dynamic space.
  8544. * The instruction format:
  8545. * Part a: values count
  8546. * Part b: all values total cell num
  8547. * Part c: each value's cell_num, src offset and dst offset
  8548. * Part d: each value's src offset and dst offset
  8549. * Part e: each value's dst offset
  8550. */
  8551. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8552. dynamic_offset = dynamic_offset_org =
  8553. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8554. /* First traversal to get the count of values needed to be copied. */
  8555. for (i = (int32)return_count - 1; i >= 0; i--) {
  8556. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8557. frame_offset -= cells;
  8558. dynamic_offset -= cells;
  8559. if (dynamic_offset != *frame_offset) {
  8560. value_count++;
  8561. total_cel_num += cells;
  8562. }
  8563. }
  8564. if (value_count) {
  8565. uint32 j = 0;
  8566. uint8 *emit_data = NULL, *cells = NULL;
  8567. int16 *src_offsets = NULL;
  8568. uint16 *dst_offsets = NULL;
  8569. uint64 size =
  8570. (uint64)value_count
  8571. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8572. /* Allocate memory for the emit data */
  8573. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8574. return false;
  8575. cells = emit_data;
  8576. src_offsets = (int16 *)(cells + value_count);
  8577. dst_offsets = (uint16 *)(src_offsets + value_count);
  8578. /* insert op_copy before else opcode */
  8579. if (opcode == WASM_OP_ELSE)
  8580. skip_label();
  8581. emit_label(EXT_OP_COPY_STACK_VALUES);
  8582. /* Part a) */
  8583. emit_uint32(loader_ctx, value_count);
  8584. /* Part b) */
  8585. emit_uint32(loader_ctx, total_cel_num);
  8586. /* Second traversal to get each value's cell num, src offset and dst
  8587. * offset. */
  8588. frame_offset = frame_offset_org;
  8589. dynamic_offset = dynamic_offset_org;
  8590. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8591. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8592. frame_offset -= cell;
  8593. dynamic_offset -= cell;
  8594. if (dynamic_offset != *frame_offset) {
  8595. /* cell num */
  8596. cells[j] = cell;
  8597. /* src offset */
  8598. src_offsets[j] = *frame_offset;
  8599. /* dst offset */
  8600. dst_offsets[j] = dynamic_offset;
  8601. j++;
  8602. }
  8603. if (opcode == WASM_OP_ELSE) {
  8604. *frame_offset = dynamic_offset;
  8605. }
  8606. else {
  8607. loader_ctx->frame_offset = frame_offset;
  8608. loader_ctx->dynamic_offset = dynamic_offset;
  8609. PUSH_OFFSET_TYPE(return_types[i]);
  8610. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8611. loader_ctx->frame_offset = frame_offset_org;
  8612. loader_ctx->dynamic_offset = dynamic_offset_org;
  8613. }
  8614. }
  8615. bh_assert(j == value_count);
  8616. /* Emit the cells, src_offsets and dst_offsets */
  8617. for (j = 0; j < value_count; j++)
  8618. emit_byte(loader_ctx, cells[j]);
  8619. for (j = 0; j < value_count; j++)
  8620. emit_operand(loader_ctx, src_offsets[j]);
  8621. for (j = 0; j < value_count; j++)
  8622. emit_operand(loader_ctx, dst_offsets[j]);
  8623. if (opcode == WASM_OP_ELSE)
  8624. emit_label(opcode);
  8625. wasm_runtime_free(emit_data);
  8626. }
  8627. return true;
  8628. fail:
  8629. return false;
  8630. }
  8631. #endif /* WASM_ENABLE_FAST_INTERP */
  8632. #define RESERVE_BLOCK_RET() \
  8633. do { \
  8634. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8635. error_buf_size)) \
  8636. goto fail; \
  8637. } while (0)
  8638. #define PUSH_TYPE(type) \
  8639. do { \
  8640. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8641. error_buf_size))) \
  8642. goto fail; \
  8643. } while (0)
  8644. #define POP_TYPE(type) \
  8645. do { \
  8646. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8647. error_buf_size))) \
  8648. goto fail; \
  8649. } while (0)
  8650. #if WASM_ENABLE_GC == 0
  8651. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8652. do { \
  8653. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8654. _start_addr, error_buf, \
  8655. error_buf_size)) \
  8656. goto fail; \
  8657. } while (0)
  8658. #define POP_CSP() \
  8659. do { \
  8660. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8661. goto fail; \
  8662. } while (0)
  8663. #else
  8664. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8665. do { \
  8666. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8667. _start_addr, error_buf, \
  8668. error_buf_size)) \
  8669. goto fail; \
  8670. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8671. error_buf, error_buf_size)) { \
  8672. goto fail; \
  8673. } \
  8674. } while (0)
  8675. #define POP_CSP() \
  8676. do { \
  8677. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8678. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8679. goto fail; \
  8680. } while (0)
  8681. #endif /* end of WASM_ENABLE_GC == 0 */
  8682. #if WASM_ENABLE_GC == 0
  8683. #define GET_LOCAL_REFTYPE() (void)0
  8684. #else
  8685. #define GET_LOCAL_REFTYPE() \
  8686. do { \
  8687. if (wasm_is_type_multi_byte_type(local_type)) { \
  8688. WASMRefType *_ref_type; \
  8689. if (local_idx < param_count) \
  8690. _ref_type = wasm_reftype_map_find( \
  8691. param_reftype_maps, param_reftype_map_count, local_idx); \
  8692. else \
  8693. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8694. local_reftype_map_count, \
  8695. local_idx - param_count); \
  8696. bh_assert(_ref_type); \
  8697. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8698. wasm_reftype_struct_size(_ref_type)); \
  8699. } \
  8700. } while (0)
  8701. #endif /* end of WASM_ENABLE_GC == 0 */
  8702. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8703. do { \
  8704. read_leb_uint32(p, p_end, local_idx); \
  8705. if (local_idx >= param_count + local_count) { \
  8706. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8707. goto fail; \
  8708. } \
  8709. local_type = local_idx < param_count \
  8710. ? param_types[local_idx] \
  8711. : local_types[local_idx - param_count]; \
  8712. local_offset = local_offsets[local_idx]; \
  8713. GET_LOCAL_REFTYPE(); \
  8714. } while (0)
  8715. static bool
  8716. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8717. {
  8718. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8719. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8720. return false;
  8721. }
  8722. return true;
  8723. }
  8724. #define CHECK_MEMORY() \
  8725. do { \
  8726. if (!check_memory(module, error_buf, error_buf_size)) \
  8727. goto fail; \
  8728. } while (0)
  8729. static bool
  8730. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8731. uint32 error_buf_size)
  8732. {
  8733. uint8 mem_access_aligns[] = {
  8734. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8735. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8736. };
  8737. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8738. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8739. set_error_buf(error_buf, error_buf_size,
  8740. "alignment must not be larger than natural");
  8741. return false;
  8742. }
  8743. return true;
  8744. }
  8745. #if WASM_ENABLE_SIMD != 0
  8746. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8747. static bool
  8748. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8749. uint32 error_buf_size)
  8750. {
  8751. uint8 mem_access_aligns[] = {
  8752. 4, /* load */
  8753. 3, 3, 3, 3, 3, 3, /* load and extend */
  8754. 0, 1, 2, 3, /* load and splat */
  8755. 4, /* store */
  8756. };
  8757. uint8 mem_access_aligns_load_lane[] = {
  8758. 0, 1, 2, 3, /* load lane */
  8759. 0, 1, 2, 3, /* store lane */
  8760. 2, 3 /* store zero */
  8761. };
  8762. if (!((opcode <= SIMD_v128_store)
  8763. || (SIMD_v128_load8_lane <= opcode
  8764. && opcode <= SIMD_v128_load64_zero))) {
  8765. set_error_buf(error_buf, error_buf_size,
  8766. "the opcode doesn't include memarg");
  8767. return false;
  8768. }
  8769. if ((opcode <= SIMD_v128_store
  8770. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8771. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8772. && align > mem_access_aligns_load_lane[opcode
  8773. - SIMD_v128_load8_lane])) {
  8774. set_error_buf(error_buf, error_buf_size,
  8775. "alignment must not be larger than natural");
  8776. return false;
  8777. }
  8778. return true;
  8779. }
  8780. static bool
  8781. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8782. uint32 error_buf_size)
  8783. {
  8784. switch (opcode) {
  8785. case SIMD_i8x16_extract_lane_s:
  8786. case SIMD_i8x16_extract_lane_u:
  8787. case SIMD_i8x16_replace_lane:
  8788. if (lane >= 16) {
  8789. goto fail;
  8790. }
  8791. break;
  8792. case SIMD_i16x8_extract_lane_s:
  8793. case SIMD_i16x8_extract_lane_u:
  8794. case SIMD_i16x8_replace_lane:
  8795. if (lane >= 8) {
  8796. goto fail;
  8797. }
  8798. break;
  8799. case SIMD_i32x4_extract_lane:
  8800. case SIMD_i32x4_replace_lane:
  8801. case SIMD_f32x4_extract_lane:
  8802. case SIMD_f32x4_replace_lane:
  8803. if (lane >= 4) {
  8804. goto fail;
  8805. }
  8806. break;
  8807. case SIMD_i64x2_extract_lane:
  8808. case SIMD_i64x2_replace_lane:
  8809. case SIMD_f64x2_extract_lane:
  8810. case SIMD_f64x2_replace_lane:
  8811. if (lane >= 2) {
  8812. goto fail;
  8813. }
  8814. break;
  8815. case SIMD_v128_load8_lane:
  8816. case SIMD_v128_load16_lane:
  8817. case SIMD_v128_load32_lane:
  8818. case SIMD_v128_load64_lane:
  8819. case SIMD_v128_store8_lane:
  8820. case SIMD_v128_store16_lane:
  8821. case SIMD_v128_store32_lane:
  8822. case SIMD_v128_store64_lane:
  8823. case SIMD_v128_load32_zero:
  8824. case SIMD_v128_load64_zero:
  8825. {
  8826. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  8827. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  8828. goto fail;
  8829. }
  8830. break;
  8831. }
  8832. default:
  8833. goto fail;
  8834. }
  8835. return true;
  8836. fail:
  8837. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8838. return false;
  8839. }
  8840. static bool
  8841. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  8842. {
  8843. uint8 i;
  8844. for (i = 0; i != 16; ++i) {
  8845. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  8846. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8847. return false;
  8848. }
  8849. }
  8850. return true;
  8851. }
  8852. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8853. #endif /* end of WASM_ENABLE_SIMD */
  8854. #if WASM_ENABLE_SHARED_MEMORY != 0
  8855. static bool
  8856. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  8857. uint32 error_buf_size)
  8858. {
  8859. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  8860. uint8 mem_access_aligns[] = {
  8861. 2, 3, 0, 1, 0, 1, 2,
  8862. };
  8863. uint8 expect;
  8864. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  8865. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  8866. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  8867. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  8868. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  8869. }
  8870. else {
  8871. /* 7 opcodes in every group */
  8872. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  8873. }
  8874. if (align != expect) {
  8875. set_error_buf(error_buf, error_buf_size,
  8876. "alignment isn't equal to natural");
  8877. return false;
  8878. }
  8879. return true;
  8880. }
  8881. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  8882. static bool
  8883. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  8884. bool is_br_table, char *error_buf, uint32 error_buf_size)
  8885. {
  8886. BranchBlock *target_block, *cur_block;
  8887. BlockType *target_block_type;
  8888. uint8 type, *types = NULL, *frame_ref;
  8889. uint32 arity = 0;
  8890. int32 i, available_stack_cell;
  8891. uint16 cell_num;
  8892. #if WASM_ENABLE_GC != 0
  8893. WASMRefTypeMap *frame_reftype_map;
  8894. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  8895. WASMRefType *ref_type;
  8896. uint32 reftype_map_count = 0;
  8897. int32 available_reftype_map;
  8898. bool is_type_multi_byte;
  8899. #endif
  8900. uint8 *frame_ref_old = loader_ctx->frame_ref;
  8901. uint8 *frame_ref_after_popped = NULL;
  8902. uint8 frame_ref_tmp[4] = { 0 };
  8903. uint8 *frame_ref_buf = frame_ref_tmp;
  8904. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  8905. #if WASM_ENABLE_GC != 0
  8906. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  8907. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  8908. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  8909. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  8910. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  8911. #endif
  8912. #if WASM_ENABLE_FAST_INTERP != 0
  8913. int16 *frame_offset_old = loader_ctx->frame_offset;
  8914. int16 *frame_offset_after_popped = NULL;
  8915. int16 frame_offset_tmp[4] = { 0 };
  8916. int16 *frame_offset_buf = frame_offset_tmp;
  8917. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  8918. #endif
  8919. bool ret = false;
  8920. bh_assert(loader_ctx->csp_num > 0);
  8921. if (loader_ctx->csp_num - 1 < depth) {
  8922. set_error_buf(error_buf, error_buf_size,
  8923. "unknown label, "
  8924. "unexpected end of section or function");
  8925. return false;
  8926. }
  8927. cur_block = loader_ctx->frame_csp - 1;
  8928. target_block = loader_ctx->frame_csp - (depth + 1);
  8929. target_block_type = &target_block->block_type;
  8930. frame_ref = loader_ctx->frame_ref;
  8931. #if WASM_ENABLE_GC != 0
  8932. frame_reftype_map = loader_ctx->frame_reftype_map;
  8933. #endif
  8934. /* Note: loop's arity is different from if and block. loop's arity is
  8935. * its parameter count while if and block arity is result count.
  8936. */
  8937. #if WASM_ENABLE_GC == 0
  8938. if (target_block->label_type == LABEL_TYPE_LOOP)
  8939. arity = block_type_get_param_types(target_block_type, &types);
  8940. else
  8941. arity = block_type_get_result_types(target_block_type, &types);
  8942. #else
  8943. if (target_block->label_type == LABEL_TYPE_LOOP)
  8944. arity = block_type_get_param_types(target_block_type, &types,
  8945. &reftype_maps, &reftype_map_count);
  8946. else
  8947. arity = block_type_get_result_types(target_block_type, &types,
  8948. &reftype_maps, &reftype_map_count);
  8949. #endif
  8950. /* If the stack is in polymorphic state, just clear the stack
  8951. * and then re-push the values to make the stack top values
  8952. * match block type. */
  8953. if (cur_block->is_stack_polymorphic) {
  8954. #if WASM_ENABLE_GC != 0
  8955. int32 j = reftype_map_count - 1;
  8956. #endif
  8957. for (i = (int32)arity - 1; i >= 0; i--) {
  8958. #if WASM_ENABLE_GC != 0
  8959. if (wasm_is_type_multi_byte_type(types[i])) {
  8960. bh_assert(reftype_maps[j].index == i);
  8961. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  8962. reftype_maps[j].ref_type,
  8963. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  8964. j--;
  8965. }
  8966. #endif
  8967. #if WASM_ENABLE_FAST_INTERP != 0
  8968. POP_OFFSET_TYPE(types[i]);
  8969. #endif
  8970. POP_TYPE(types[i]);
  8971. }
  8972. /* Backup stack data since it may be changed in the below
  8973. push operations, and the stack data may be used when
  8974. checking other target blocks of opcode br_table */
  8975. if (is_br_table) {
  8976. uint64 total_size;
  8977. frame_ref_after_popped = loader_ctx->frame_ref;
  8978. total_size = (uint64)sizeof(uint8)
  8979. * (frame_ref_old - frame_ref_after_popped);
  8980. if (total_size > sizeof(frame_ref_tmp)
  8981. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  8982. error_buf_size))) {
  8983. goto fail;
  8984. }
  8985. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  8986. frame_ref_after_popped, (uint32)total_size);
  8987. #if WASM_ENABLE_GC != 0
  8988. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  8989. total_size =
  8990. (uint64)sizeof(WASMRefTypeMap)
  8991. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  8992. if (total_size > sizeof(frame_reftype_map_tmp)
  8993. && !(frame_reftype_map_buf = loader_malloc(
  8994. total_size, error_buf, error_buf_size))) {
  8995. goto fail;
  8996. }
  8997. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  8998. frame_reftype_map_after_popped, (uint32)total_size);
  8999. #endif
  9000. #if WASM_ENABLE_FAST_INTERP != 0
  9001. frame_offset_after_popped = loader_ctx->frame_offset;
  9002. total_size = (uint64)sizeof(int16)
  9003. * (frame_offset_old - frame_offset_after_popped);
  9004. if (total_size > sizeof(frame_offset_tmp)
  9005. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9006. error_buf_size))) {
  9007. goto fail;
  9008. }
  9009. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9010. frame_offset_after_popped, (uint32)total_size);
  9011. #endif
  9012. }
  9013. #if WASM_ENABLE_GC != 0
  9014. j = 0;
  9015. #endif
  9016. for (i = 0; i < (int32)arity; i++) {
  9017. #if WASM_ENABLE_GC != 0
  9018. if (wasm_is_type_multi_byte_type(types[i])) {
  9019. bh_assert(reftype_maps[j].index == i);
  9020. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9021. reftype_maps[j].ref_type,
  9022. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9023. j++;
  9024. }
  9025. #endif
  9026. #if WASM_ENABLE_FAST_INTERP != 0
  9027. bool disable_emit = true;
  9028. int16 operand_offset = 0;
  9029. PUSH_OFFSET_TYPE(types[i]);
  9030. #endif
  9031. PUSH_TYPE(types[i]);
  9032. }
  9033. #if WASM_ENABLE_FAST_INTERP != 0
  9034. emit_br_info(target_block);
  9035. #endif
  9036. /* Restore the stack data, note that frame_ref_bottom,
  9037. frame_reftype_map_bottom, frame_offset_bottom may be
  9038. re-allocated in the above push operations */
  9039. if (is_br_table) {
  9040. uint32 total_size;
  9041. /* The stack operand num should not be smaller than before
  9042. after pop and push operations */
  9043. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9044. loader_ctx->stack_cell_num = stack_cell_num_old;
  9045. loader_ctx->frame_ref =
  9046. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9047. total_size = (uint32)sizeof(uint8)
  9048. * (frame_ref_old - frame_ref_after_popped);
  9049. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9050. frame_ref_buf, total_size);
  9051. #if WASM_ENABLE_GC != 0
  9052. /* The stack operand num should not be smaller than before
  9053. after pop and push operations */
  9054. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9055. loader_ctx->reftype_map_num = reftype_map_num_old;
  9056. loader_ctx->frame_reftype_map =
  9057. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9058. total_size =
  9059. (uint32)sizeof(WASMRefTypeMap)
  9060. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9061. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9062. total_size, frame_reftype_map_buf, total_size);
  9063. #endif
  9064. #if WASM_ENABLE_FAST_INTERP != 0
  9065. loader_ctx->frame_offset =
  9066. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9067. total_size = (uint32)sizeof(int16)
  9068. * (frame_offset_old - frame_offset_after_popped);
  9069. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9070. total_size, frame_offset_buf, total_size);
  9071. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9072. #endif
  9073. }
  9074. ret = true;
  9075. goto cleanup_and_return;
  9076. }
  9077. available_stack_cell =
  9078. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9079. #if WASM_ENABLE_GC != 0
  9080. available_reftype_map =
  9081. (int32)(loader_ctx->reftype_map_num
  9082. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9083. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9084. #endif
  9085. /* Check stack top values match target block type */
  9086. for (i = (int32)arity - 1; i >= 0; i--) {
  9087. type = types[i];
  9088. #if WASM_ENABLE_GC != 0
  9089. ref_type = NULL;
  9090. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9091. if (is_type_multi_byte) {
  9092. bh_assert(reftype_map);
  9093. ref_type = reftype_map->ref_type;
  9094. }
  9095. #endif
  9096. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9097. break;
  9098. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9099. #if WASM_ENABLE_GC != 0
  9100. frame_reftype_map, available_reftype_map,
  9101. #endif
  9102. type,
  9103. #if WASM_ENABLE_GC != 0
  9104. ref_type,
  9105. #endif
  9106. error_buf, error_buf_size)) {
  9107. goto fail;
  9108. }
  9109. cell_num = wasm_value_type_cell_num(types[i]);
  9110. frame_ref -= cell_num;
  9111. available_stack_cell -= cell_num;
  9112. #if WASM_ENABLE_GC != 0
  9113. if (is_type_multi_byte) {
  9114. frame_reftype_map--;
  9115. available_reftype_map--;
  9116. reftype_map--;
  9117. }
  9118. #endif
  9119. }
  9120. #if WASM_ENABLE_FAST_INTERP != 0
  9121. emit_br_info(target_block);
  9122. #endif
  9123. ret = true;
  9124. cleanup_and_return:
  9125. fail:
  9126. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9127. wasm_runtime_free(frame_ref_buf);
  9128. #if WASM_ENABLE_GC != 0
  9129. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9130. wasm_runtime_free(frame_reftype_map_buf);
  9131. #endif
  9132. #if WASM_ENABLE_FAST_INTERP != 0
  9133. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9134. wasm_runtime_free(frame_offset_tmp);
  9135. #endif
  9136. return ret;
  9137. }
  9138. static BranchBlock *
  9139. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9140. bool is_br_table, char *error_buf, uint32 error_buf_size)
  9141. {
  9142. uint8 *p = *p_buf, *p_end = buf_end;
  9143. BranchBlock *frame_csp_tmp;
  9144. uint32 depth;
  9145. read_leb_uint32(p, p_end, depth);
  9146. if (!wasm_loader_check_br(loader_ctx, depth, is_br_table, error_buf,
  9147. error_buf_size)) {
  9148. goto fail;
  9149. }
  9150. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9151. *p_buf = p;
  9152. return frame_csp_tmp;
  9153. fail:
  9154. return NULL;
  9155. }
  9156. #if WASM_ENABLE_EXCE_HANDLING != 0
  9157. static BranchBlock *
  9158. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9159. uint8 *buf_end, char *error_buf,
  9160. uint32 error_buf_size)
  9161. {
  9162. uint8 *p = *p_buf, *p_end = buf_end;
  9163. BranchBlock *frame_csp_tmp;
  9164. uint32 depth;
  9165. read_leb_uint32(p, p_end, depth);
  9166. /*
  9167. * Note: "delegate 0" means the surrounding block, not the
  9168. * try-delegate block itself.
  9169. *
  9170. * Note: the caller hasn't popped the try-delegate frame yet.
  9171. */
  9172. bh_assert(loader_ctx->csp_num > 0);
  9173. if (loader_ctx->csp_num - 1 <= depth) {
  9174. #if WASM_ENABLE_SPEC_TEST == 0
  9175. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9176. #else
  9177. set_error_buf(error_buf, error_buf_size, "unknown label");
  9178. #endif
  9179. goto fail;
  9180. }
  9181. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9182. #if WASM_ENABLE_FAST_INTERP != 0
  9183. emit_br_info(frame_csp_tmp);
  9184. #endif
  9185. *p_buf = p;
  9186. return frame_csp_tmp;
  9187. fail:
  9188. return NULL;
  9189. }
  9190. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9191. static bool
  9192. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9193. char *error_buf, uint32 error_buf_size)
  9194. {
  9195. BlockType *block_type = &block->block_type;
  9196. uint8 *return_types = NULL;
  9197. uint32 return_count = 0;
  9198. int32 available_stack_cell, return_cell_num, i;
  9199. uint8 *frame_ref = NULL;
  9200. #if WASM_ENABLE_GC != 0
  9201. WASMRefTypeMap *frame_reftype_map;
  9202. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9203. WASMRefType *ref_type;
  9204. uint32 param_count, return_reftype_map_count = 0;
  9205. int32 available_reftype_map =
  9206. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9207. #endif
  9208. available_stack_cell =
  9209. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9210. #if WASM_ENABLE_GC == 0
  9211. return_count = block_type_get_result_types(block_type, &return_types);
  9212. #else
  9213. return_count = block_type_get_result_types(block_type, &return_types,
  9214. &return_reftype_maps,
  9215. &return_reftype_map_count);
  9216. param_count =
  9217. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9218. (void)param_count;
  9219. #endif
  9220. return_cell_num =
  9221. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9222. /* If the stack is in polymorphic state, just clear the stack
  9223. * and then re-push the values to make the stack top values
  9224. * match block type. */
  9225. if (block->is_stack_polymorphic) {
  9226. #if WASM_ENABLE_GC != 0
  9227. int32 j = return_reftype_map_count - 1;
  9228. #endif
  9229. for (i = (int32)return_count - 1; i >= 0; i--) {
  9230. #if WASM_ENABLE_GC != 0
  9231. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9232. bh_assert(return_reftype_maps[j].index == i + param_count);
  9233. bh_memcpy_s(
  9234. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9235. return_reftype_maps[j].ref_type,
  9236. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9237. j--;
  9238. }
  9239. #endif
  9240. #if WASM_ENABLE_FAST_INTERP != 0
  9241. POP_OFFSET_TYPE(return_types[i]);
  9242. #endif
  9243. POP_TYPE(return_types[i]);
  9244. }
  9245. /* Check stack is empty */
  9246. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9247. set_error_buf(
  9248. error_buf, error_buf_size,
  9249. "type mismatch: stack size does not match block type");
  9250. goto fail;
  9251. }
  9252. #if WASM_ENABLE_GC != 0
  9253. j = 0;
  9254. #endif
  9255. for (i = 0; i < (int32)return_count; i++) {
  9256. #if WASM_ENABLE_GC != 0
  9257. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9258. bh_assert(return_reftype_maps[j].index == i + param_count);
  9259. bh_memcpy_s(
  9260. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9261. return_reftype_maps[j].ref_type,
  9262. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9263. j++;
  9264. }
  9265. #endif
  9266. #if WASM_ENABLE_FAST_INTERP != 0
  9267. bool disable_emit = true;
  9268. int16 operand_offset = 0;
  9269. PUSH_OFFSET_TYPE(return_types[i]);
  9270. #endif
  9271. PUSH_TYPE(return_types[i]);
  9272. }
  9273. return true;
  9274. }
  9275. if (available_stack_cell != return_cell_num) {
  9276. #if WASM_ENABLE_EXCE_HANDLING != 0
  9277. /* testspec: this error message format is expected by try_catch.wast */
  9278. snprintf(
  9279. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9280. block->label_type == LABEL_TYPE_TRY
  9281. || (block->label_type == LABEL_TYPE_CATCH
  9282. && return_cell_num > 0)
  9283. ? "instruction"
  9284. : "block",
  9285. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9286. " but stack has ",
  9287. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9288. : "");
  9289. goto fail;
  9290. #else
  9291. set_error_buf(error_buf, error_buf_size,
  9292. "type mismatch: stack size does not match block type");
  9293. goto fail;
  9294. #endif
  9295. }
  9296. /* Check stack values match return types */
  9297. frame_ref = loader_ctx->frame_ref;
  9298. #if WASM_ENABLE_GC != 0
  9299. frame_reftype_map = loader_ctx->frame_reftype_map;
  9300. return_reftype_map =
  9301. return_reftype_map_count
  9302. ? return_reftype_maps + return_reftype_map_count - 1
  9303. : NULL;
  9304. #endif
  9305. for (i = (int32)return_count - 1; i >= 0; i--) {
  9306. uint8 type = return_types[i];
  9307. #if WASM_ENABLE_GC != 0
  9308. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9309. ref_type = NULL;
  9310. if (is_type_multi_byte) {
  9311. bh_assert(return_reftype_map);
  9312. ref_type = return_reftype_map->ref_type;
  9313. }
  9314. #endif
  9315. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9316. #if WASM_ENABLE_GC != 0
  9317. frame_reftype_map, available_reftype_map,
  9318. #endif
  9319. type,
  9320. #if WASM_ENABLE_GC != 0
  9321. ref_type,
  9322. #endif
  9323. error_buf, error_buf_size))
  9324. return false;
  9325. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9326. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9327. #if WASM_ENABLE_GC != 0
  9328. if (is_type_multi_byte) {
  9329. frame_reftype_map--;
  9330. available_reftype_map--;
  9331. return_reftype_map--;
  9332. }
  9333. #endif
  9334. }
  9335. return true;
  9336. fail:
  9337. return false;
  9338. }
  9339. #if WASM_ENABLE_FAST_INTERP != 0
  9340. /* Copy parameters to dynamic space.
  9341. * 1) POP original parameter out;
  9342. * 2) Push and copy original values to dynamic space.
  9343. * The copy instruction format:
  9344. * Part a: param count
  9345. * Part b: all param total cell num
  9346. * Part c: each param's cell_num, src offset and dst offset
  9347. * Part d: each param's src offset
  9348. * Part e: each param's dst offset
  9349. */
  9350. static bool
  9351. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  9352. char *error_buf, uint32 error_buf_size)
  9353. {
  9354. bool ret = false;
  9355. int16 *frame_offset = NULL;
  9356. uint8 *cells = NULL, cell;
  9357. int16 *src_offsets = NULL;
  9358. uint8 *emit_data = NULL;
  9359. uint32 i;
  9360. BranchBlock *block = loader_ctx->frame_csp - 1;
  9361. BlockType *block_type = &block->block_type;
  9362. WASMFuncType *wasm_type = block_type->u.type;
  9363. uint32 param_count = block_type->u.type->param_count;
  9364. int16 condition_offset = 0;
  9365. bool disable_emit = false;
  9366. int16 operand_offset = 0;
  9367. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9368. bh_assert(size > 0);
  9369. /* For if block, we also need copy the condition operand offset. */
  9370. if (is_if_block)
  9371. size += sizeof(*cells) + sizeof(*src_offsets);
  9372. /* Allocate memory for the emit data */
  9373. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9374. return false;
  9375. cells = emit_data;
  9376. src_offsets = (int16 *)(cells + param_count);
  9377. if (is_if_block)
  9378. condition_offset = *loader_ctx->frame_offset;
  9379. /* POP original parameter out */
  9380. for (i = 0; i < param_count; i++) {
  9381. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9382. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9383. }
  9384. frame_offset = loader_ctx->frame_offset;
  9385. /* Get each param's cell num and src offset */
  9386. for (i = 0; i < param_count; i++) {
  9387. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9388. cells[i] = cell;
  9389. src_offsets[i] = *frame_offset;
  9390. frame_offset += cell;
  9391. }
  9392. /* emit copy instruction */
  9393. emit_label(EXT_OP_COPY_STACK_VALUES);
  9394. /* Part a) */
  9395. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9396. /* Part b) */
  9397. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9398. : wasm_type->param_cell_num);
  9399. /* Part c) */
  9400. for (i = 0; i < param_count; i++)
  9401. emit_byte(loader_ctx, cells[i]);
  9402. if (is_if_block)
  9403. emit_byte(loader_ctx, 1);
  9404. /* Part d) */
  9405. for (i = 0; i < param_count; i++)
  9406. emit_operand(loader_ctx, src_offsets[i]);
  9407. if (is_if_block)
  9408. emit_operand(loader_ctx, condition_offset);
  9409. /* Part e) */
  9410. /* Push to dynamic space. The push will emit the dst offset. */
  9411. for (i = 0; i < param_count; i++)
  9412. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9413. if (is_if_block)
  9414. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9415. ret = true;
  9416. fail:
  9417. /* Free the emit data */
  9418. wasm_runtime_free(emit_data);
  9419. return ret;
  9420. }
  9421. #endif
  9422. #if WASM_ENABLE_GC == 0
  9423. #define RESET_REFTYPE_MAP_STACK() (void)0
  9424. #else
  9425. #define RESET_REFTYPE_MAP_STACK() \
  9426. do { \
  9427. loader_ctx->reftype_map_num = \
  9428. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9429. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9430. + loader_ctx->reftype_map_num; \
  9431. } while (0)
  9432. #endif
  9433. /* reset the stack to the state of before entering the last block */
  9434. #if WASM_ENABLE_FAST_INTERP != 0
  9435. #define RESET_STACK() \
  9436. do { \
  9437. loader_ctx->stack_cell_num = \
  9438. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9439. loader_ctx->frame_ref = \
  9440. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9441. loader_ctx->frame_offset = \
  9442. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9443. RESET_REFTYPE_MAP_STACK(); \
  9444. } while (0)
  9445. #else
  9446. #define RESET_STACK() \
  9447. do { \
  9448. loader_ctx->stack_cell_num = \
  9449. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9450. loader_ctx->frame_ref = \
  9451. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9452. RESET_REFTYPE_MAP_STACK(); \
  9453. } while (0)
  9454. #endif
  9455. /* set current block's stack polymorphic state */
  9456. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9457. do { \
  9458. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9459. _cur_block->is_stack_polymorphic = flag; \
  9460. } while (0)
  9461. #define BLOCK_HAS_PARAM(block_type) \
  9462. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9463. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9464. do { \
  9465. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9466. error_buf_size))) { \
  9467. goto fail; \
  9468. } \
  9469. } while (0)
  9470. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9471. static bool
  9472. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9473. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9474. uint32 error_buf_size)
  9475. {
  9476. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9477. return false;
  9478. }
  9479. if (table_idx < module->import_table_count) {
  9480. if (p_elem_type)
  9481. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  9482. #if WASM_ENABLE_GC != 0
  9483. if (p_ref_type)
  9484. *((WASMRefType **)p_ref_type) =
  9485. module->import_tables[table_idx].u.table.elem_ref_type;
  9486. #endif
  9487. }
  9488. else {
  9489. if (p_elem_type)
  9490. *p_elem_type =
  9491. module->tables[module->import_table_count + table_idx]
  9492. .elem_type;
  9493. #if WASM_ENABLE_GC != 0
  9494. if (p_ref_type)
  9495. *((WASMRefType **)p_ref_type) =
  9496. module->tables[module->import_table_count + table_idx]
  9497. .elem_ref_type;
  9498. #endif
  9499. }
  9500. return true;
  9501. }
  9502. static bool
  9503. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9504. uint8 *p_elem_type, void **p_elem_ref_type,
  9505. char *error_buf, uint32 error_buf_size)
  9506. {
  9507. if (table_seg_idx >= module->table_seg_count) {
  9508. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9509. table_seg_idx);
  9510. return false;
  9511. }
  9512. if (p_elem_type) {
  9513. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9514. }
  9515. #if WASM_ENABLE_GC != 0
  9516. if (p_elem_ref_type)
  9517. *((WASMRefType **)p_elem_ref_type) =
  9518. module->table_segments[table_seg_idx].elem_ref_type;
  9519. #endif
  9520. return true;
  9521. }
  9522. #endif
  9523. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9524. const uint8 *
  9525. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9526. uint32 *len)
  9527. {
  9528. WASMCustomSection *section = module->custom_section_list;
  9529. while (section) {
  9530. if ((section->name_len == strlen(name))
  9531. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9532. if (len) {
  9533. *len = section->content_len;
  9534. }
  9535. return section->content_addr;
  9536. }
  9537. section = section->next;
  9538. }
  9539. return NULL;
  9540. }
  9541. #endif
  9542. static bool
  9543. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9544. uint32 cur_func_idx, char *error_buf,
  9545. uint32 error_buf_size)
  9546. {
  9547. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9548. uint32 param_count, local_count, global_count;
  9549. uint8 *param_types, *local_types, local_type, global_type;
  9550. BlockType func_block_type;
  9551. uint16 *local_offsets, local_offset;
  9552. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9553. uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
  9554. int32 i32_const = 0;
  9555. int64 i64_const;
  9556. uint8 opcode;
  9557. bool return_value = false;
  9558. WASMLoaderContext *loader_ctx;
  9559. BranchBlock *frame_csp_tmp;
  9560. #if WASM_ENABLE_GC != 0
  9561. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9562. uint32 param_reftype_map_count, local_reftype_map_count;
  9563. int32 heap_type;
  9564. WASMRefType wasm_ref_type = { 0 };
  9565. bool need_ref_type_map;
  9566. #endif
  9567. #if WASM_ENABLE_FAST_INTERP != 0
  9568. uint8 *func_const_end, *func_const = NULL;
  9569. int16 operand_offset = 0;
  9570. uint8 last_op = 0;
  9571. bool disable_emit, preserve_local = false, if_condition_available = true;
  9572. float32 f32_const;
  9573. float64 f64_const;
  9574. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9575. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9576. #endif
  9577. global_count = module->import_global_count + module->global_count;
  9578. param_count = func->func_type->param_count;
  9579. param_types = func->func_type->types;
  9580. func_block_type.is_value_type = false;
  9581. func_block_type.u.type = func->func_type;
  9582. local_count = func->local_count;
  9583. local_types = func->local_types;
  9584. local_offsets = func->local_offsets;
  9585. #if WASM_ENABLE_GC != 0
  9586. param_reftype_maps = func->func_type->ref_type_maps;
  9587. param_reftype_map_count = func->func_type->ref_type_map_count;
  9588. local_reftype_maps = func->local_ref_type_maps;
  9589. local_reftype_map_count = func->local_ref_type_map_count;
  9590. #endif
  9591. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9592. goto fail;
  9593. }
  9594. #if WASM_ENABLE_GC != 0
  9595. loader_ctx->module = module;
  9596. loader_ctx->ref_type_set = module->ref_type_set;
  9597. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9598. #endif
  9599. #if WASM_ENABLE_FAST_INTERP != 0
  9600. /* For the first traverse, the initial value of preserved_local_offset has
  9601. * not been determined, we use the INT16_MAX to represent that a slot has
  9602. * been copied to preserve space. For second traverse, this field will be
  9603. * set to the appropriate value in wasm_loader_ctx_reinit.
  9604. * This is for Issue #1230,
  9605. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9606. * drop opcodes need to know which slots are preserved, so those slots will
  9607. * not be treated as dynamically allocated slots */
  9608. loader_ctx->preserved_local_offset = INT16_MAX;
  9609. re_scan:
  9610. if (loader_ctx->code_compiled_size > 0) {
  9611. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9612. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9613. goto fail;
  9614. }
  9615. p = func->code;
  9616. func->code_compiled = loader_ctx->p_code_compiled;
  9617. func->code_compiled_size = loader_ctx->code_compiled_size;
  9618. }
  9619. #endif
  9620. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9621. while (p < p_end) {
  9622. opcode = *p++;
  9623. #if WASM_ENABLE_FAST_INTERP != 0
  9624. p_org = p;
  9625. disable_emit = false;
  9626. emit_label(opcode);
  9627. #endif
  9628. switch (opcode) {
  9629. case WASM_OP_UNREACHABLE:
  9630. RESET_STACK();
  9631. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9632. break;
  9633. case WASM_OP_NOP:
  9634. #if WASM_ENABLE_FAST_INTERP != 0
  9635. skip_label();
  9636. #endif
  9637. break;
  9638. case WASM_OP_IF:
  9639. {
  9640. #if WASM_ENABLE_FAST_INTERP != 0
  9641. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9642. int32 available_stack_cell =
  9643. (int32)(loader_ctx->stack_cell_num
  9644. - parent_block->stack_cell_num);
  9645. if (available_stack_cell <= 0
  9646. && parent_block->is_stack_polymorphic)
  9647. if_condition_available = false;
  9648. else
  9649. if_condition_available = true;
  9650. PRESERVE_LOCAL_FOR_BLOCK();
  9651. #endif
  9652. #if WASM_ENABLE_GC == 0
  9653. POP_I32();
  9654. #endif
  9655. goto handle_op_block_and_loop;
  9656. }
  9657. case WASM_OP_BLOCK:
  9658. case WASM_OP_LOOP:
  9659. #if WASM_ENABLE_EXCE_HANDLING != 0
  9660. case WASM_OP_TRY:
  9661. if (opcode == WASM_OP_TRY) {
  9662. /*
  9663. * keep track of exception handlers to account for
  9664. * memory allocation
  9665. */
  9666. func->exception_handler_count++;
  9667. /*
  9668. * try is a block
  9669. * do nothing special, but execution continues to
  9670. * to handle_op_block_and_loop,
  9671. * and that be pushes the csp
  9672. */
  9673. }
  9674. #endif
  9675. #if WASM_ENABLE_FAST_INTERP != 0
  9676. PRESERVE_LOCAL_FOR_BLOCK();
  9677. #endif
  9678. handle_op_block_and_loop:
  9679. {
  9680. uint8 value_type;
  9681. BlockType block_type;
  9682. #if WASM_ENABLE_FAST_INTERP != 0
  9683. uint32 available_params = 0;
  9684. #endif
  9685. CHECK_BUF(p, p_end, 1);
  9686. value_type = read_uint8(p);
  9687. if (is_byte_a_type(value_type)) {
  9688. /* If the first byte is one of these special values:
  9689. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9690. * the single return value. */
  9691. block_type.is_value_type = true;
  9692. block_type.u.value_type.type = value_type;
  9693. #if WASM_ENABLE_WAMR_COMPILER != 0
  9694. if (value_type == VALUE_TYPE_V128)
  9695. module->is_simd_used = true;
  9696. else if (value_type == VALUE_TYPE_FUNCREF
  9697. || value_type == VALUE_TYPE_EXTERNREF)
  9698. module->is_ref_types_used = true;
  9699. #endif
  9700. #if WASM_ENABLE_GC != 0
  9701. if (value_type != VALUE_TYPE_VOID) {
  9702. p_org = p;
  9703. p--;
  9704. if (!resolve_value_type((const uint8 **)&p, p_end,
  9705. module, &need_ref_type_map,
  9706. &wasm_ref_type, false,
  9707. error_buf, error_buf_size)) {
  9708. goto fail;
  9709. }
  9710. if (need_ref_type_map) {
  9711. block_type.u.value_type.ref_type_map.index = 0;
  9712. if (!(block_type.u.value_type.ref_type_map
  9713. .ref_type = reftype_set_insert(
  9714. module->ref_type_set, &wasm_ref_type,
  9715. error_buf, error_buf_size))) {
  9716. goto fail;
  9717. }
  9718. }
  9719. /* Set again as the type might be changed, e.g.
  9720. (ref null any) to anyref */
  9721. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9722. #if WASM_ENABLE_FAST_INTERP == 0
  9723. while (p_org < p) {
  9724. #if WASM_ENABLE_DEBUG_INTERP != 0
  9725. if (!record_fast_op(module, p_org, *p_org,
  9726. error_buf, error_buf_size)) {
  9727. goto fail;
  9728. }
  9729. #endif
  9730. /* Ignore extra bytes for interpreter */
  9731. *p_org++ = WASM_OP_NOP;
  9732. }
  9733. #endif
  9734. }
  9735. #endif /* end of WASM_ENABLE_GC != 0 */
  9736. }
  9737. else {
  9738. uint32 type_index;
  9739. /* Resolve the leb128 encoded type index as block type */
  9740. p--;
  9741. p_org = p - 1;
  9742. read_leb_uint32(p, p_end, type_index);
  9743. if (type_index >= module->type_count) {
  9744. set_error_buf(error_buf, error_buf_size,
  9745. "unknown type");
  9746. goto fail;
  9747. }
  9748. block_type.is_value_type = false;
  9749. block_type.u.type =
  9750. (WASMFuncType *)module->types[type_index];
  9751. #if WASM_ENABLE_FAST_INTERP == 0
  9752. /* If block use type index as block type, change the opcode
  9753. * to new extended opcode so that interpreter can resolve
  9754. * the block quickly.
  9755. */
  9756. #if WASM_ENABLE_DEBUG_INTERP != 0
  9757. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9758. error_buf_size)) {
  9759. goto fail;
  9760. }
  9761. #endif
  9762. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9763. #endif
  9764. }
  9765. #if WASM_ENABLE_GC != 0
  9766. if (opcode == WASM_OP_IF) {
  9767. POP_I32();
  9768. }
  9769. #endif
  9770. /* Pop block parameters from stack */
  9771. if (BLOCK_HAS_PARAM(block_type)) {
  9772. WASMFuncType *wasm_type = block_type.u.type;
  9773. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9774. #if WASM_ENABLE_FAST_INTERP != 0
  9775. uint32 cell_num;
  9776. available_params = block_type.u.type->param_count;
  9777. #endif
  9778. for (i = 0; i < block_type.u.type->param_count; i++) {
  9779. int32 available_stack_cell =
  9780. (int32)(loader_ctx->stack_cell_num
  9781. - cur_block->stack_cell_num);
  9782. if (available_stack_cell <= 0
  9783. && cur_block->is_stack_polymorphic) {
  9784. #if WASM_ENABLE_FAST_INTERP != 0
  9785. available_params = i;
  9786. #endif
  9787. break;
  9788. }
  9789. POP_TYPE(
  9790. wasm_type->types[wasm_type->param_count - i - 1]);
  9791. #if WASM_ENABLE_FAST_INTERP != 0
  9792. /* decrease the frame_offset pointer accordingly to keep
  9793. * consistent with frame_ref stack */
  9794. cell_num = wasm_value_type_cell_num(
  9795. wasm_type->types[wasm_type->param_count - i - 1]);
  9796. loader_ctx->frame_offset -= cell_num;
  9797. #endif
  9798. }
  9799. }
  9800. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  9801. block_type, p);
  9802. /* Pass parameters to block */
  9803. if (BLOCK_HAS_PARAM(block_type)) {
  9804. for (i = 0; i < block_type.u.type->param_count; i++) {
  9805. #if WASM_ENABLE_FAST_INTERP != 0
  9806. uint32 cell_num = wasm_value_type_cell_num(
  9807. block_type.u.type->types[i]);
  9808. if (i >= available_params) {
  9809. /* If there isn't enough data on stack, push a dummy
  9810. * offset to keep the stack consistent with
  9811. * frame_ref.
  9812. * Since the stack is already in polymorphic state,
  9813. * the opcode will not be executed, so the dummy
  9814. * offset won't cause any error */
  9815. uint32 n;
  9816. for (n = 0; n < cell_num; n++) {
  9817. if (loader_ctx->p_code_compiled == NULL) {
  9818. if (!check_offset_push(loader_ctx,
  9819. error_buf,
  9820. error_buf_size))
  9821. goto fail;
  9822. }
  9823. *loader_ctx->frame_offset++ = 0;
  9824. }
  9825. }
  9826. else {
  9827. loader_ctx->frame_offset += cell_num;
  9828. }
  9829. #endif
  9830. PUSH_TYPE(block_type.u.type->types[i]);
  9831. }
  9832. }
  9833. #if WASM_ENABLE_FAST_INTERP != 0
  9834. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  9835. skip_label();
  9836. if (BLOCK_HAS_PARAM(block_type)) {
  9837. /* Make sure params are in dynamic space */
  9838. if (!copy_params_to_dynamic_space(
  9839. loader_ctx, false, error_buf, error_buf_size))
  9840. goto fail;
  9841. }
  9842. if (opcode == WASM_OP_LOOP) {
  9843. (loader_ctx->frame_csp - 1)->code_compiled =
  9844. loader_ctx->p_code_compiled;
  9845. }
  9846. }
  9847. #if WASM_ENABLE_EXCE_HANDLING != 0
  9848. else if (opcode == WASM_OP_TRY) {
  9849. skip_label();
  9850. }
  9851. #endif
  9852. else if (opcode == WASM_OP_IF) {
  9853. BranchBlock *block = loader_ctx->frame_csp - 1;
  9854. /* If block has parameters, we should make sure they are in
  9855. * dynamic space. Otherwise, when else branch is missing,
  9856. * the later opcode may consume incorrect operand offset.
  9857. * Spec case:
  9858. * (func (export "params-id") (param i32) (result i32)
  9859. * (i32.const 1)
  9860. * (i32.const 2)
  9861. * (if (param i32 i32) (result i32 i32) (local.get 0)
  9862. * (then)) (i32.add)
  9863. * )
  9864. *
  9865. * So we should emit a copy instruction before the if.
  9866. *
  9867. * And we also need to save the parameter offsets and
  9868. * recover them before entering else branch.
  9869. *
  9870. */
  9871. if (BLOCK_HAS_PARAM(block_type)) {
  9872. uint64 size;
  9873. /* In polymorphic state, there may be no if condition on
  9874. * the stack, so the offset may not emitted */
  9875. if (if_condition_available) {
  9876. /* skip the if condition operand offset */
  9877. wasm_loader_emit_backspace(loader_ctx,
  9878. sizeof(int16));
  9879. }
  9880. /* skip the if label */
  9881. skip_label();
  9882. /* Emit a copy instruction */
  9883. if (!copy_params_to_dynamic_space(
  9884. loader_ctx, true, error_buf, error_buf_size))
  9885. goto fail;
  9886. /* Emit the if instruction */
  9887. emit_label(opcode);
  9888. /* Emit the new condition operand offset */
  9889. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  9890. /* Save top param_count values of frame_offset stack, so
  9891. * that we can recover it before executing else branch
  9892. */
  9893. size = sizeof(int16)
  9894. * (uint64)block_type.u.type->param_cell_num;
  9895. if (!(block->param_frame_offsets = loader_malloc(
  9896. size, error_buf, error_buf_size)))
  9897. goto fail;
  9898. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  9899. loader_ctx->frame_offset
  9900. - size / sizeof(int16),
  9901. (uint32)size);
  9902. }
  9903. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9904. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  9905. emit_empty_label_addr_and_frame_ip(PATCH_END);
  9906. }
  9907. #endif
  9908. break;
  9909. }
  9910. #if WASM_ENABLE_EXCE_HANDLING != 0
  9911. case WASM_OP_THROW:
  9912. {
  9913. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9914. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9915. uint8 label_type = cur_block->label_type;
  9916. uint32 tag_index = 0;
  9917. read_leb_int32(p, p_end, tag_index);
  9918. /* check validity of tag_index against module->tag_count */
  9919. /* check tag index is within the tag index space */
  9920. if (tag_index >= module->import_tag_count + module->tag_count) {
  9921. snprintf(error_buf, error_buf_size, "unknown tag %d",
  9922. tag_index);
  9923. goto fail;
  9924. }
  9925. /* the tag_type is stored in either the WASMTag (section tags)
  9926. * or WASMTagImport (import tag) */
  9927. WASMType *tag_type = NULL;
  9928. if (tag_index < module->import_tag_count) {
  9929. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  9930. }
  9931. else {
  9932. tag_type =
  9933. module->tags[tag_index - module->import_tag_count]
  9934. ->tag_type;
  9935. }
  9936. if (tag_type->result_count != 0) {
  9937. set_error_buf(error_buf, error_buf_size,
  9938. "tag type signature does not return void");
  9939. goto fail;
  9940. }
  9941. int32 available_stack_cell =
  9942. (int32)(loader_ctx->stack_cell_num
  9943. - cur_block->stack_cell_num);
  9944. /* Check stack values match return types by comparing tag param
  9945. * types with stack cells */
  9946. uint8 *frame_ref = loader_ctx->frame_ref;
  9947. for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
  9948. tti--) {
  9949. if (!check_stack_top_values(
  9950. loader_ctx, frame_ref, available_stack_cell,
  9951. tag_type->types[tti], error_buf, error_buf_size)) {
  9952. snprintf(error_buf, error_buf_size,
  9953. "type mismatch: instruction requires [%s] but "
  9954. "stack has [%s]",
  9955. tag_type->param_count > 0
  9956. ? type2str(tag_type->types[tti])
  9957. : "",
  9958. available_stack_cell > 0
  9959. ? type2str(*(loader_ctx->frame_ref - 1))
  9960. : "");
  9961. goto fail;
  9962. }
  9963. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  9964. available_stack_cell -=
  9965. wasm_value_type_cell_num(tag_type->types[tti]);
  9966. }
  9967. /* throw is stack polymorphic */
  9968. (void)label_type;
  9969. RESET_STACK();
  9970. break;
  9971. }
  9972. case WASM_OP_RETHROW:
  9973. {
  9974. /* must be done before checking branch block */
  9975. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9976. /* check the target catching block: LABEL_TYPE_CATCH */
  9977. if (!(frame_csp_tmp =
  9978. check_branch_block(loader_ctx, &p, p_end, false,
  9979. error_buf, error_buf_size)))
  9980. goto fail;
  9981. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  9982. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  9983. /* trap according to spectest (rethrow.wast) */
  9984. set_error_buf(error_buf, error_buf_size,
  9985. "invalid rethrow label");
  9986. goto fail;
  9987. }
  9988. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9989. uint8 label_type = cur_block->label_type;
  9990. (void)label_type;
  9991. /* rethrow is stack polymorphic */
  9992. RESET_STACK();
  9993. break;
  9994. }
  9995. case WASM_OP_DELEGATE:
  9996. {
  9997. /* check target block is valid */
  9998. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  9999. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10000. goto fail;
  10001. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10002. uint8 label_type = cur_block->label_type;
  10003. (void)label_type;
  10004. /* DELEGATE ends the block */
  10005. POP_CSP();
  10006. break;
  10007. }
  10008. case WASM_OP_CATCH:
  10009. {
  10010. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10011. uint8 label_type = cur_block->label_type;
  10012. uint32 tag_index = 0;
  10013. read_leb_int32(p, p_end, tag_index);
  10014. /* check validity of tag_index against module->tag_count */
  10015. /* check tag index is within the tag index space */
  10016. if (tag_index >= module->import_tag_count + module->tag_count) {
  10017. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10018. __FUNCTION__);
  10019. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10020. goto fail;
  10021. }
  10022. /* the tag_type is stored in either the WASMTag (section tags)
  10023. * or WASMTagImport (import tag) */
  10024. WASMType *func_type = NULL;
  10025. if (tag_index < module->import_tag_count) {
  10026. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10027. }
  10028. else {
  10029. func_type =
  10030. module->tags[tag_index - module->import_tag_count]
  10031. ->tag_type;
  10032. }
  10033. if (func_type->result_count != 0) {
  10034. set_error_buf(error_buf, error_buf_size,
  10035. "tag type signature does not return void");
  10036. goto fail;
  10037. }
  10038. /* check validity of current label (expect LABEL_TYPE_TRY or
  10039. * LABEL_TYPE_CATCH) */
  10040. if ((LABEL_TYPE_CATCH != label_type)
  10041. && (LABEL_TYPE_TRY != label_type)) {
  10042. set_error_buf(error_buf, error_buf_size,
  10043. "Unexpected block sequence encountered.");
  10044. goto fail;
  10045. }
  10046. BlockType new_block_type;
  10047. new_block_type.is_value_type = false;
  10048. new_block_type.u.type = func_type;
  10049. /*
  10050. * replace frame_csp by LABEL_TYPE_CATCH
  10051. */
  10052. cur_block->label_type = LABEL_TYPE_CATCH;
  10053. /* RESET_STACK removes the values pushed in TRY or pervious
  10054. * CATCH Blocks */
  10055. RESET_STACK();
  10056. /* push types on the stack according to catched type */
  10057. if (BLOCK_HAS_PARAM(new_block_type)) {
  10058. for (i = 0; i < new_block_type.u.type->param_count; i++)
  10059. PUSH_TYPE(new_block_type.u.type->types[i]);
  10060. }
  10061. break;
  10062. }
  10063. case WASM_OP_CATCH_ALL:
  10064. {
  10065. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10066. /* expecting a TRY or CATCH, anything else will be considered an
  10067. * error */
  10068. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10069. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10070. set_error_buf(error_buf, error_buf_size,
  10071. "Unexpected block sequence encountered.");
  10072. goto fail;
  10073. }
  10074. /* no immediates */
  10075. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10076. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10077. /* RESET_STACK removes the values pushed in TRY or pervious
  10078. * CATCH Blocks */
  10079. RESET_STACK();
  10080. /* catch_all has no tagtype and therefore no parameters */
  10081. break;
  10082. }
  10083. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10084. case WASM_OP_ELSE:
  10085. {
  10086. BranchBlock *block = NULL;
  10087. BlockType block_type;
  10088. if (loader_ctx->csp_num < 2
  10089. || (loader_ctx->frame_csp - 1)->label_type
  10090. != LABEL_TYPE_IF) {
  10091. set_error_buf(
  10092. error_buf, error_buf_size,
  10093. "opcode else found without matched opcode if");
  10094. goto fail;
  10095. }
  10096. block = loader_ctx->frame_csp - 1;
  10097. /* check whether if branch's stack matches its result type */
  10098. if (!check_block_stack(loader_ctx, block, error_buf,
  10099. error_buf_size))
  10100. goto fail;
  10101. block->else_addr = p - 1;
  10102. block_type = block->block_type;
  10103. #if WASM_ENABLE_GC != 0
  10104. if (!wasm_loader_init_local_use_masks(
  10105. loader_ctx, local_count, error_buf, error_buf_size)) {
  10106. goto fail;
  10107. }
  10108. #endif
  10109. #if WASM_ENABLE_FAST_INTERP != 0
  10110. /* if the result of if branch is in local or const area, add a
  10111. * copy op */
  10112. RESERVE_BLOCK_RET();
  10113. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10114. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10115. #endif
  10116. RESET_STACK();
  10117. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10118. /* Pass parameters to if-false branch */
  10119. if (BLOCK_HAS_PARAM(block_type)) {
  10120. for (i = 0; i < block_type.u.type->param_count; i++)
  10121. PUSH_TYPE(block_type.u.type->types[i]);
  10122. }
  10123. #if WASM_ENABLE_FAST_INTERP != 0
  10124. /* Recover top param_count values of frame_offset stack */
  10125. if (BLOCK_HAS_PARAM((block_type))) {
  10126. uint32 size;
  10127. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10128. bh_memcpy_s(loader_ctx->frame_offset, size,
  10129. block->param_frame_offsets, size);
  10130. loader_ctx->frame_offset += (size / sizeof(int16));
  10131. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10132. }
  10133. #endif
  10134. break;
  10135. }
  10136. case WASM_OP_END:
  10137. {
  10138. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10139. /* check whether block stack matches its result type */
  10140. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10141. error_buf_size))
  10142. goto fail;
  10143. /* if no else branch, and return types do not match param types,
  10144. report failure */
  10145. if (cur_block->label_type == LABEL_TYPE_IF
  10146. && !cur_block->else_addr) {
  10147. uint32 block_param_count = 0, block_ret_count = 0;
  10148. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  10149. BlockType *cur_block_type = &cur_block->block_type;
  10150. #if WASM_ENABLE_GC != 0
  10151. uint32 block_param_reftype_map_count;
  10152. uint32 block_ret_reftype_map_count;
  10153. WASMRefTypeMap *block_param_reftype_maps;
  10154. WASMRefTypeMap *block_ret_reftype_maps;
  10155. #endif
  10156. block_param_count = block_type_get_param_types(
  10157. cur_block_type, &block_param_types
  10158. #if WASM_ENABLE_GC != 0
  10159. ,
  10160. &block_param_reftype_maps,
  10161. &block_param_reftype_map_count
  10162. #endif
  10163. );
  10164. block_ret_count = block_type_get_result_types(
  10165. cur_block_type, &block_ret_types
  10166. #if WASM_ENABLE_GC != 0
  10167. ,
  10168. &block_ret_reftype_maps, &block_ret_reftype_map_count
  10169. #endif
  10170. );
  10171. if (block_param_count != block_ret_count
  10172. || (block_param_count
  10173. && memcmp(block_param_types, block_ret_types,
  10174. block_param_count))) {
  10175. set_error_buf(error_buf, error_buf_size,
  10176. "type mismatch: else branch missing");
  10177. goto fail;
  10178. }
  10179. #if WASM_ENABLE_GC != 0
  10180. if (block_param_reftype_map_count
  10181. != block_ret_reftype_map_count
  10182. || (block_param_reftype_map_count
  10183. && memcmp(block_param_reftype_maps,
  10184. block_ret_reftype_maps,
  10185. sizeof(WASMRefTypeMap)
  10186. * block_param_reftype_map_count))) {
  10187. set_error_buf(error_buf, error_buf_size,
  10188. "type mismatch: else branch missing");
  10189. goto fail;
  10190. }
  10191. #endif
  10192. }
  10193. POP_CSP();
  10194. #if WASM_ENABLE_FAST_INTERP != 0
  10195. skip_label();
  10196. /* copy the result to the block return address */
  10197. RESERVE_BLOCK_RET();
  10198. apply_label_patch(loader_ctx, 0, PATCH_END);
  10199. free_label_patch_list(loader_ctx->frame_csp);
  10200. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10201. int32 idx;
  10202. uint8 ret_type;
  10203. emit_label(WASM_OP_RETURN);
  10204. for (idx = (int32)func->func_type->result_count - 1;
  10205. idx >= 0; idx--) {
  10206. ret_type = *(func->func_type->types
  10207. + func->func_type->param_count + idx);
  10208. POP_OFFSET_TYPE(ret_type);
  10209. }
  10210. }
  10211. #endif
  10212. if (loader_ctx->csp_num > 0) {
  10213. loader_ctx->frame_csp->end_addr = p - 1;
  10214. }
  10215. else {
  10216. /* end of function block, function will return */
  10217. if (p < p_end) {
  10218. set_error_buf(error_buf, error_buf_size,
  10219. "section size mismatch");
  10220. goto fail;
  10221. }
  10222. }
  10223. break;
  10224. }
  10225. case WASM_OP_BR:
  10226. {
  10227. if (!(frame_csp_tmp =
  10228. check_branch_block(loader_ctx, &p, p_end, false,
  10229. error_buf, error_buf_size)))
  10230. goto fail;
  10231. RESET_STACK();
  10232. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10233. break;
  10234. }
  10235. case WASM_OP_BR_IF:
  10236. {
  10237. POP_I32();
  10238. if (!(frame_csp_tmp =
  10239. check_branch_block(loader_ctx, &p, p_end, false,
  10240. error_buf, error_buf_size)))
  10241. goto fail;
  10242. break;
  10243. }
  10244. case WASM_OP_BR_TABLE:
  10245. {
  10246. uint32 depth, default_arity, arity = 0;
  10247. BranchBlock *target_block;
  10248. BlockType *target_block_type;
  10249. #if WASM_ENABLE_FAST_INTERP == 0
  10250. BrTableCache *br_table_cache = NULL;
  10251. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10252. uint32 j;
  10253. #endif
  10254. read_leb_uint32(p, p_end, count);
  10255. #if WASM_ENABLE_FAST_INTERP != 0
  10256. emit_uint32(loader_ctx, count);
  10257. #endif
  10258. POP_I32();
  10259. /* Get the default depth and check it */
  10260. p_org = p;
  10261. for (i = 0; i <= count; i++) {
  10262. read_leb_uint32(p, p_end, depth);
  10263. }
  10264. if (loader_ctx->csp_num < depth + 1) {
  10265. set_error_buf(error_buf, error_buf_size,
  10266. "unknown label, "
  10267. "unexpected end of section or function");
  10268. goto fail;
  10269. }
  10270. p = p_org;
  10271. /* Get the default block's arity */
  10272. target_block = loader_ctx->frame_csp - (depth + 1);
  10273. target_block_type = &target_block->block_type;
  10274. default_arity = block_type_get_arity(target_block_type,
  10275. target_block->label_type);
  10276. #if WASM_ENABLE_FAST_INTERP == 0
  10277. p_depth_begin = p_depth = p;
  10278. #endif
  10279. for (i = 0; i <= count; i++) {
  10280. p_org = p;
  10281. read_leb_uint32(p, p_end, depth);
  10282. if (loader_ctx->csp_num < depth + 1) {
  10283. set_error_buf(error_buf, error_buf_size,
  10284. "unknown label, "
  10285. "unexpected end of section or function");
  10286. goto fail;
  10287. }
  10288. p = p_org;
  10289. /* Get the target block's arity and check it */
  10290. target_block = loader_ctx->frame_csp - (depth + 1);
  10291. target_block_type = &target_block->block_type;
  10292. arity = block_type_get_arity(target_block_type,
  10293. target_block->label_type);
  10294. if (arity != default_arity) {
  10295. set_error_buf(error_buf, error_buf_size,
  10296. "type mismatch: br_table targets must "
  10297. "all use same result type");
  10298. goto fail;
  10299. }
  10300. if (!(frame_csp_tmp =
  10301. check_branch_block(loader_ctx, &p, p_end, true,
  10302. error_buf, error_buf_size))) {
  10303. goto fail;
  10304. }
  10305. #if WASM_ENABLE_FAST_INTERP == 0
  10306. if (br_table_cache) {
  10307. br_table_cache->br_depths[i] = depth;
  10308. }
  10309. else {
  10310. if (depth > 255) {
  10311. /* The depth cannot be stored in one byte,
  10312. create br_table cache to store each depth */
  10313. #if WASM_ENABLE_DEBUG_INTERP != 0
  10314. if (!record_fast_op(module, p_opcode, *p_opcode,
  10315. error_buf, error_buf_size)) {
  10316. goto fail;
  10317. }
  10318. #endif
  10319. if (!(br_table_cache = loader_malloc(
  10320. offsetof(BrTableCache, br_depths)
  10321. + sizeof(uint32)
  10322. * (uint64)(count + 1),
  10323. error_buf, error_buf_size))) {
  10324. goto fail;
  10325. }
  10326. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10327. br_table_cache->br_table_op_addr = p_opcode;
  10328. br_table_cache->br_count = count;
  10329. /* Copy previous depths which are one byte */
  10330. for (j = 0; j < i; j++) {
  10331. br_table_cache->br_depths[j] = p_depth_begin[j];
  10332. }
  10333. br_table_cache->br_depths[i] = depth;
  10334. bh_list_insert(module->br_table_cache_list,
  10335. br_table_cache);
  10336. }
  10337. else {
  10338. /* The depth can be stored in one byte, use the
  10339. byte of the leb to store it */
  10340. *p_depth++ = (uint8)depth;
  10341. }
  10342. }
  10343. #endif
  10344. }
  10345. #if WASM_ENABLE_FAST_INTERP == 0
  10346. /* Set the tailing bytes to nop */
  10347. if (br_table_cache)
  10348. p_depth = p_depth_begin;
  10349. while (p_depth < p)
  10350. *p_depth++ = WASM_OP_NOP;
  10351. #endif
  10352. RESET_STACK();
  10353. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10354. break;
  10355. }
  10356. case WASM_OP_RETURN:
  10357. {
  10358. int32 idx;
  10359. uint8 ret_type;
  10360. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  10361. idx--) {
  10362. ret_type = *(func->func_type->types
  10363. + func->func_type->param_count + idx);
  10364. #if WASM_ENABLE_FAST_INTERP != 0
  10365. /* emit the offset after return opcode */
  10366. POP_OFFSET_TYPE(ret_type);
  10367. #endif
  10368. POP_TYPE(ret_type);
  10369. }
  10370. RESET_STACK();
  10371. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10372. break;
  10373. }
  10374. case WASM_OP_CALL:
  10375. #if WASM_ENABLE_TAIL_CALL != 0
  10376. case WASM_OP_RETURN_CALL:
  10377. #endif
  10378. #if WASM_ENABLE_GC != 0
  10379. case WASM_OP_CALL_REF:
  10380. case WASM_OP_RETURN_CALL_REF:
  10381. #endif
  10382. {
  10383. WASMFuncType *func_type;
  10384. uint8 type;
  10385. int32 idx;
  10386. #if WASM_ENABLE_GC != 0
  10387. WASMRefType *ref_type;
  10388. uint32 type_idx1;
  10389. int32 j;
  10390. #endif
  10391. #if WASM_ENABLE_GC != 0
  10392. if (opcode == WASM_OP_CALL_REF
  10393. || opcode == WASM_OP_RETURN_CALL_REF) {
  10394. read_leb_uint32(p, p_end, type_idx1);
  10395. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10396. &type_idx, error_buf,
  10397. error_buf_size)) {
  10398. goto fail;
  10399. }
  10400. if (type == VALUE_TYPE_ANY) {
  10401. type_idx = type_idx1;
  10402. }
  10403. if (!check_type_index(module, type_idx, error_buf,
  10404. error_buf_size)) {
  10405. goto fail;
  10406. }
  10407. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10408. set_error_buf(error_buf, error_buf_size,
  10409. "unkown function type");
  10410. goto fail;
  10411. }
  10412. if (type_idx != type_idx1) {
  10413. set_error_buf(error_buf, error_buf_size,
  10414. "function type mismatch");
  10415. goto fail;
  10416. }
  10417. func_type = (WASMFuncType *)module->types[type_idx];
  10418. }
  10419. else
  10420. #endif
  10421. {
  10422. read_leb_uint32(p, p_end, func_idx);
  10423. #if WASM_ENABLE_FAST_INTERP != 0
  10424. /* we need to emit func_idx before arguments */
  10425. emit_uint32(loader_ctx, func_idx);
  10426. #endif
  10427. if (!check_function_index(module, func_idx, error_buf,
  10428. error_buf_size)) {
  10429. goto fail;
  10430. }
  10431. if (func_idx < module->import_function_count)
  10432. func_type = module->import_functions[func_idx]
  10433. .u.function.func_type;
  10434. else
  10435. func_type =
  10436. module
  10437. ->functions[func_idx
  10438. - module->import_function_count]
  10439. ->func_type;
  10440. }
  10441. if (func_type->param_count > 0) {
  10442. #if WASM_ENABLE_GC != 0
  10443. j = (int32)(func_type->result_ref_type_maps
  10444. - func_type->ref_type_maps - 1);
  10445. #endif
  10446. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10447. idx--) {
  10448. #if WASM_ENABLE_GC != 0
  10449. if (wasm_is_type_multi_byte_type(
  10450. func_type->types[idx])) {
  10451. ref_type = func_type->ref_type_maps[j].ref_type;
  10452. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10453. ref_type,
  10454. wasm_reftype_struct_size(ref_type));
  10455. j--;
  10456. }
  10457. #endif
  10458. #if WASM_ENABLE_FAST_INTERP != 0
  10459. POP_OFFSET_TYPE(func_type->types[idx]);
  10460. #endif
  10461. POP_TYPE(func_type->types[idx]);
  10462. }
  10463. }
  10464. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10465. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10466. #endif
  10467. #if WASM_ENABLE_GC != 0
  10468. j = (int32)(func_type->result_ref_type_maps
  10469. - func_type->ref_type_maps);
  10470. #endif
  10471. for (i = 0; i < func_type->result_count; i++) {
  10472. #if WASM_ENABLE_GC != 0
  10473. if (wasm_is_type_multi_byte_type(
  10474. func_type->types[func_type->param_count + i])) {
  10475. ref_type = func_type->ref_type_maps[j].ref_type;
  10476. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10477. ref_type,
  10478. wasm_reftype_struct_size(ref_type));
  10479. j++;
  10480. }
  10481. #endif
  10482. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10483. #if WASM_ENABLE_FAST_INTERP != 0
  10484. /* Here we emit each return value's dynamic_offset. But
  10485. * in fact these offsets are continuous, so interpreter
  10486. * only need to get the first return value's offset.
  10487. */
  10488. PUSH_OFFSET_TYPE(
  10489. func_type->types[func_type->param_count + i]);
  10490. #endif
  10491. }
  10492. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10493. }
  10494. else {
  10495. #if WASM_ENABLE_GC == 0
  10496. if (func_type->result_count
  10497. != func->func_type->result_count) {
  10498. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10499. "type mismatch: expect ",
  10500. func->func_type->result_count,
  10501. " return values but got other");
  10502. goto fail;
  10503. }
  10504. for (i = 0; i < func_type->result_count; i++) {
  10505. type = func->func_type
  10506. ->types[func->func_type->param_count + i];
  10507. if (func_type->types[func_type->param_count + i]
  10508. != type) {
  10509. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10510. "type mismatch: expect ",
  10511. type2str(type), " but got other");
  10512. goto fail;
  10513. }
  10514. }
  10515. #else
  10516. if (!wasm_func_type_result_is_subtype_of(
  10517. func_type, func->func_type, module->types,
  10518. module->type_count)) {
  10519. set_error_buf(
  10520. error_buf, error_buf_size,
  10521. "type mismatch: invalid func result types");
  10522. goto fail;
  10523. }
  10524. #endif
  10525. RESET_STACK();
  10526. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10527. }
  10528. #endif
  10529. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10530. || WASM_ENABLE_WAMR_COMPILER != 0
  10531. func->has_op_func_call = true;
  10532. #endif
  10533. (void)type;
  10534. break;
  10535. }
  10536. /*
  10537. * if disable reference type: call_indirect typeidx, 0x00
  10538. * if enable reference type: call_indirect typeidx, tableidx
  10539. */
  10540. case WASM_OP_CALL_INDIRECT:
  10541. #if WASM_ENABLE_TAIL_CALL != 0
  10542. case WASM_OP_RETURN_CALL_INDIRECT:
  10543. #endif
  10544. {
  10545. int32 idx;
  10546. WASMFuncType *func_type;
  10547. read_leb_uint32(p, p_end, type_idx);
  10548. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10549. read_leb_uint32(p, p_end, table_idx);
  10550. #else
  10551. CHECK_BUF(p, p_end, 1);
  10552. table_idx = read_uint8(p);
  10553. #endif
  10554. if (!check_table_index(module, table_idx, error_buf,
  10555. error_buf_size)) {
  10556. goto fail;
  10557. }
  10558. #if WASM_ENABLE_FAST_INTERP != 0
  10559. /* we need to emit before arguments */
  10560. #if WASM_ENABLE_TAIL_CALL != 0
  10561. emit_byte(loader_ctx, opcode);
  10562. #endif
  10563. emit_uint32(loader_ctx, type_idx);
  10564. emit_uint32(loader_ctx, table_idx);
  10565. #endif
  10566. /* skip elem idx */
  10567. POP_I32();
  10568. if (type_idx >= module->type_count) {
  10569. set_error_buf(error_buf, error_buf_size, "unknown type");
  10570. goto fail;
  10571. }
  10572. func_type = (WASMFuncType *)module->types[type_idx];
  10573. if (func_type->param_count > 0) {
  10574. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10575. idx--) {
  10576. #if WASM_ENABLE_FAST_INTERP != 0
  10577. POP_OFFSET_TYPE(func_type->types[idx]);
  10578. #endif
  10579. POP_TYPE(func_type->types[idx]);
  10580. }
  10581. }
  10582. #if WASM_ENABLE_TAIL_CALL != 0
  10583. if (opcode == WASM_OP_CALL_INDIRECT) {
  10584. #endif
  10585. for (i = 0; i < func_type->result_count; i++) {
  10586. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10587. #if WASM_ENABLE_FAST_INTERP != 0
  10588. PUSH_OFFSET_TYPE(
  10589. func_type->types[func_type->param_count + i]);
  10590. #endif
  10591. }
  10592. #if WASM_ENABLE_TAIL_CALL != 0
  10593. }
  10594. else {
  10595. uint8 type;
  10596. if (func_type->result_count
  10597. != func->func_type->result_count) {
  10598. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10599. "type mismatch: expect ",
  10600. func->func_type->result_count,
  10601. " return values but got other");
  10602. goto fail;
  10603. }
  10604. for (i = 0; i < func_type->result_count; i++) {
  10605. type = func->func_type
  10606. ->types[func->func_type->param_count + i];
  10607. if (func_type->types[func_type->param_count + i]
  10608. != type) {
  10609. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10610. "type mismatch: expect ",
  10611. type2str(type), " but got other");
  10612. goto fail;
  10613. }
  10614. }
  10615. RESET_STACK();
  10616. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10617. }
  10618. #endif
  10619. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10620. || WASM_ENABLE_WAMR_COMPILER != 0
  10621. func->has_op_func_call = true;
  10622. #endif
  10623. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10624. func->has_op_call_indirect = true;
  10625. #endif
  10626. break;
  10627. }
  10628. case WASM_OP_DROP:
  10629. {
  10630. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10631. int32 available_stack_cell =
  10632. (int32)(loader_ctx->stack_cell_num
  10633. - cur_block->stack_cell_num);
  10634. if (available_stack_cell <= 0
  10635. && !cur_block->is_stack_polymorphic) {
  10636. set_error_buf(error_buf, error_buf_size,
  10637. "type mismatch, opcode drop was found "
  10638. "but stack was empty");
  10639. goto fail;
  10640. }
  10641. if (available_stack_cell > 0) {
  10642. #if WASM_ENABLE_GC != 0
  10643. if (wasm_is_type_multi_byte_type(
  10644. *(loader_ctx->frame_ref - 1))) {
  10645. bh_assert((int32)(loader_ctx->reftype_map_num
  10646. - cur_block->reftype_map_num)
  10647. > 0);
  10648. loader_ctx->frame_reftype_map--;
  10649. loader_ctx->reftype_map_num--;
  10650. }
  10651. #endif
  10652. if (is_32bit_type(*(loader_ctx->frame_ref - 1))
  10653. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  10654. loader_ctx->frame_ref--;
  10655. loader_ctx->stack_cell_num--;
  10656. #if WASM_ENABLE_FAST_INTERP != 0
  10657. skip_label();
  10658. loader_ctx->frame_offset--;
  10659. if ((*(loader_ctx->frame_offset)
  10660. > loader_ctx->start_dynamic_offset)
  10661. && (*(loader_ctx->frame_offset)
  10662. < loader_ctx->max_dynamic_offset))
  10663. loader_ctx->dynamic_offset--;
  10664. #endif
  10665. }
  10666. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10667. loader_ctx->frame_ref -= 2;
  10668. loader_ctx->stack_cell_num -= 2;
  10669. #if WASM_ENABLE_FAST_INTERP == 0
  10670. *(p - 1) = WASM_OP_DROP_64;
  10671. #endif
  10672. #if WASM_ENABLE_FAST_INTERP != 0
  10673. skip_label();
  10674. loader_ctx->frame_offset -= 2;
  10675. if ((*(loader_ctx->frame_offset)
  10676. > loader_ctx->start_dynamic_offset)
  10677. && (*(loader_ctx->frame_offset)
  10678. < loader_ctx->max_dynamic_offset))
  10679. loader_ctx->dynamic_offset -= 2;
  10680. #endif
  10681. }
  10682. #if WASM_ENABLE_SIMD != 0
  10683. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10684. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10685. loader_ctx->frame_ref -= 4;
  10686. loader_ctx->stack_cell_num -= 4;
  10687. }
  10688. #endif
  10689. #endif
  10690. else {
  10691. set_error_buf(error_buf, error_buf_size,
  10692. "type mismatch");
  10693. goto fail;
  10694. }
  10695. }
  10696. else {
  10697. #if WASM_ENABLE_FAST_INTERP != 0
  10698. skip_label();
  10699. #endif
  10700. }
  10701. break;
  10702. }
  10703. case WASM_OP_SELECT:
  10704. {
  10705. uint8 ref_type;
  10706. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10707. int32 available_stack_cell;
  10708. #if WASM_ENABLE_FAST_INTERP != 0
  10709. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10710. #endif
  10711. POP_I32();
  10712. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10713. - cur_block->stack_cell_num);
  10714. if (available_stack_cell <= 0
  10715. && !cur_block->is_stack_polymorphic) {
  10716. set_error_buf(error_buf, error_buf_size,
  10717. "type mismatch or invalid result arity, "
  10718. "opcode select was found "
  10719. "but stack was empty");
  10720. goto fail;
  10721. }
  10722. if (available_stack_cell > 0) {
  10723. switch (*(loader_ctx->frame_ref - 1)) {
  10724. case VALUE_TYPE_I32:
  10725. case VALUE_TYPE_F32:
  10726. break;
  10727. case VALUE_TYPE_I64:
  10728. case VALUE_TYPE_F64:
  10729. #if WASM_ENABLE_FAST_INTERP == 0
  10730. *(p - 1) = WASM_OP_SELECT_64;
  10731. #endif
  10732. #if WASM_ENABLE_FAST_INTERP != 0
  10733. if (loader_ctx->p_code_compiled) {
  10734. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10735. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10736. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10737. *(void **)(p_code_compiled_tmp
  10738. - sizeof(void *)) =
  10739. handle_table[opcode_tmp];
  10740. #else
  10741. #if UINTPTR_MAX == UINT64_MAX
  10742. /* emit int32 relative offset in 64-bit target
  10743. */
  10744. int32 offset =
  10745. (int32)((uint8 *)handle_table[opcode_tmp]
  10746. - (uint8 *)handle_table[0]);
  10747. *(int32 *)(p_code_compiled_tmp
  10748. - sizeof(int32)) = offset;
  10749. #else
  10750. /* emit uint32 label address in 32-bit target */
  10751. *(uint32 *)(p_code_compiled_tmp
  10752. - sizeof(uint32)) =
  10753. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10754. #endif
  10755. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10756. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10757. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10758. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10759. #else
  10760. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10761. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10762. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10763. }
  10764. #endif /* end of WASM_ENABLE_FAST_INTERP */
  10765. break;
  10766. #if WASM_ENABLE_SIMD != 0
  10767. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10768. case VALUE_TYPE_V128:
  10769. break;
  10770. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  10771. #endif /* WASM_ENABLE_SIMD != 0 */
  10772. default:
  10773. {
  10774. set_error_buf(error_buf, error_buf_size,
  10775. "type mismatch");
  10776. goto fail;
  10777. }
  10778. }
  10779. ref_type = *(loader_ctx->frame_ref - 1);
  10780. #if WASM_ENABLE_FAST_INTERP != 0
  10781. POP_OFFSET_TYPE(ref_type);
  10782. POP_TYPE(ref_type);
  10783. POP_OFFSET_TYPE(ref_type);
  10784. POP_TYPE(ref_type);
  10785. PUSH_OFFSET_TYPE(ref_type);
  10786. PUSH_TYPE(ref_type);
  10787. #else
  10788. POP2_AND_PUSH(ref_type, ref_type);
  10789. #endif
  10790. }
  10791. else {
  10792. #if WASM_ENABLE_FAST_INTERP != 0
  10793. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  10794. #endif
  10795. PUSH_TYPE(VALUE_TYPE_ANY);
  10796. }
  10797. break;
  10798. }
  10799. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10800. case WASM_OP_SELECT_T:
  10801. {
  10802. uint8 vec_len, type;
  10803. #if WASM_ENABLE_GC != 0
  10804. WASMRefType *ref_type = NULL;
  10805. #endif
  10806. #if WASM_ENABLE_FAST_INTERP != 0
  10807. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10808. #endif
  10809. read_leb_uint32(p, p_end, vec_len);
  10810. if (vec_len != 1) {
  10811. /* typed select must have exactly one result */
  10812. set_error_buf(error_buf, error_buf_size,
  10813. "invalid result arity");
  10814. goto fail;
  10815. }
  10816. #if WASM_ENABLE_GC == 0
  10817. CHECK_BUF(p, p_end, 1);
  10818. type = read_uint8(p);
  10819. if (!is_value_type(type)) {
  10820. set_error_buf(error_buf, error_buf_size,
  10821. "unknown value type");
  10822. goto fail;
  10823. }
  10824. #else
  10825. p_org = p + 1;
  10826. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  10827. &need_ref_type_map, &wasm_ref_type,
  10828. false, error_buf, error_buf_size)) {
  10829. goto fail;
  10830. }
  10831. type = wasm_ref_type.ref_type;
  10832. if (need_ref_type_map) {
  10833. if (!(ref_type = reftype_set_insert(
  10834. module->ref_type_set, &wasm_ref_type, error_buf,
  10835. error_buf_size))) {
  10836. goto fail;
  10837. }
  10838. }
  10839. #if WASM_ENABLE_FAST_INTERP == 0
  10840. while (p_org < p) {
  10841. #if WASM_ENABLE_DEBUG_INTERP != 0
  10842. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10843. error_buf_size)) {
  10844. goto fail;
  10845. }
  10846. #endif
  10847. /* Ignore extra bytes for interpreter */
  10848. *p_org++ = WASM_OP_NOP;
  10849. }
  10850. #endif
  10851. #endif /* end of WASM_ENABLE_GC == 0 */
  10852. POP_I32();
  10853. #if WASM_ENABLE_FAST_INTERP != 0
  10854. if (loader_ctx->p_code_compiled) {
  10855. uint8 opcode_tmp = WASM_OP_SELECT;
  10856. if (type == VALUE_TYPE_V128) {
  10857. #if (WASM_ENABLE_SIMD == 0) \
  10858. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  10859. set_error_buf(error_buf, error_buf_size,
  10860. "SIMD v128 type isn't supported");
  10861. goto fail;
  10862. #endif
  10863. }
  10864. else {
  10865. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  10866. opcode_tmp = WASM_OP_SELECT_64;
  10867. #if WASM_ENABLE_GC != 0
  10868. if (wasm_is_type_reftype(type))
  10869. opcode_tmp = WASM_OP_SELECT_T;
  10870. #endif
  10871. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10872. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10873. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  10874. handle_table[opcode_tmp];
  10875. #else
  10876. #if UINTPTR_MAX == UINT64_MAX
  10877. /* emit int32 relative offset in 64-bit target */
  10878. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  10879. - (uint8 *)handle_table[0]);
  10880. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  10881. offset;
  10882. #else
  10883. /* emit uint32 label address in 32-bit target */
  10884. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  10885. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10886. #endif
  10887. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10888. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10889. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10890. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10891. #else
  10892. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10893. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10894. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10895. }
  10896. }
  10897. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  10898. POP_REF(type);
  10899. #if WASM_ENABLE_GC != 0
  10900. if (need_ref_type_map) {
  10901. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  10902. wasm_reftype_struct_size(ref_type));
  10903. }
  10904. #endif
  10905. POP_REF(type);
  10906. #if WASM_ENABLE_GC != 0
  10907. if (need_ref_type_map) {
  10908. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  10909. wasm_reftype_struct_size(ref_type));
  10910. }
  10911. #endif
  10912. PUSH_REF(type);
  10913. #if WASM_ENABLE_WAMR_COMPILER != 0
  10914. module->is_ref_types_used = true;
  10915. #endif
  10916. (void)vec_len;
  10917. break;
  10918. }
  10919. /* table.get x. tables[x]. [i32] -> [t] */
  10920. /* table.set x. tables[x]. [i32 t] -> [] */
  10921. case WASM_OP_TABLE_GET:
  10922. case WASM_OP_TABLE_SET:
  10923. {
  10924. uint8 decl_ref_type;
  10925. #if WASM_ENABLE_GC != 0
  10926. WASMRefType *ref_type;
  10927. #endif
  10928. read_leb_uint32(p, p_end, table_idx);
  10929. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  10930. #if WASM_ENABLE_GC != 0
  10931. (void **)&ref_type,
  10932. #else
  10933. NULL,
  10934. #endif
  10935. error_buf, error_buf_size))
  10936. goto fail;
  10937. #if WASM_ENABLE_GC != 0
  10938. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  10939. bh_assert(ref_type);
  10940. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  10941. ref_type, wasm_reftype_struct_size(ref_type));
  10942. }
  10943. #endif
  10944. #if WASM_ENABLE_FAST_INTERP != 0
  10945. emit_uint32(loader_ctx, table_idx);
  10946. #endif
  10947. if (opcode == WASM_OP_TABLE_GET) {
  10948. POP_I32();
  10949. #if WASM_ENABLE_FAST_INTERP != 0
  10950. PUSH_OFFSET_TYPE(decl_ref_type);
  10951. #endif
  10952. PUSH_TYPE(decl_ref_type);
  10953. }
  10954. else {
  10955. #if WASM_ENABLE_FAST_INTERP != 0
  10956. POP_OFFSET_TYPE(decl_ref_type);
  10957. #endif
  10958. POP_TYPE(decl_ref_type);
  10959. POP_I32();
  10960. }
  10961. #if WASM_ENABLE_WAMR_COMPILER != 0
  10962. module->is_ref_types_used = true;
  10963. #endif
  10964. break;
  10965. }
  10966. case WASM_OP_REF_NULL:
  10967. {
  10968. uint8 ref_type;
  10969. #if WASM_ENABLE_GC == 0
  10970. CHECK_BUF(p, p_end, 1);
  10971. ref_type = read_uint8(p);
  10972. if (ref_type != VALUE_TYPE_FUNCREF
  10973. && ref_type != VALUE_TYPE_EXTERNREF) {
  10974. set_error_buf(error_buf, error_buf_size, "type mismatch");
  10975. goto fail;
  10976. }
  10977. #else
  10978. read_leb_int32(p, p_end, heap_type);
  10979. if (heap_type >= 0) {
  10980. if (!check_type_index(module, heap_type, error_buf,
  10981. error_buf_size)) {
  10982. goto fail;
  10983. }
  10984. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  10985. true, heap_type);
  10986. ref_type = wasm_ref_type.ref_type;
  10987. }
  10988. else {
  10989. if (!wasm_is_valid_heap_type(heap_type)) {
  10990. set_error_buf(error_buf, error_buf_size,
  10991. "unknown type");
  10992. goto fail;
  10993. }
  10994. ref_type = (uint8)((int32)0x80 + heap_type);
  10995. }
  10996. #endif /* end of WASM_ENABLE_GC == 0 */
  10997. #if WASM_ENABLE_FAST_INTERP != 0
  10998. PUSH_OFFSET_TYPE(ref_type);
  10999. #endif
  11000. PUSH_TYPE(ref_type);
  11001. #if WASM_ENABLE_WAMR_COMPILER != 0
  11002. module->is_ref_types_used = true;
  11003. #endif
  11004. break;
  11005. }
  11006. case WASM_OP_REF_IS_NULL:
  11007. {
  11008. #if WASM_ENABLE_GC == 0
  11009. #if WASM_ENABLE_FAST_INTERP != 0
  11010. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11011. int32 block_stack_cell_num =
  11012. (int32)(loader_ctx->stack_cell_num
  11013. - cur_block->stack_cell_num);
  11014. if (block_stack_cell_num <= 0) {
  11015. if (!cur_block->is_stack_polymorphic) {
  11016. set_error_buf(
  11017. error_buf, error_buf_size,
  11018. "type mismatch: expect data but stack was empty");
  11019. goto fail;
  11020. }
  11021. }
  11022. else {
  11023. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11024. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11025. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11026. if (!wasm_loader_pop_frame_ref_offset(
  11027. loader_ctx, *(loader_ctx->frame_ref - 1),
  11028. error_buf, error_buf_size)) {
  11029. goto fail;
  11030. }
  11031. }
  11032. else {
  11033. set_error_buf(error_buf, error_buf_size,
  11034. "type mismatch");
  11035. goto fail;
  11036. }
  11037. }
  11038. #else
  11039. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11040. error_buf, error_buf_size)
  11041. && !wasm_loader_pop_frame_ref(loader_ctx,
  11042. VALUE_TYPE_EXTERNREF,
  11043. error_buf, error_buf_size)) {
  11044. goto fail;
  11045. }
  11046. #endif
  11047. #else /* else of WASM_ENABLE_GC == 0 */
  11048. uint8 type;
  11049. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11050. error_buf, error_buf_size)) {
  11051. goto fail;
  11052. }
  11053. #endif
  11054. PUSH_I32();
  11055. #if WASM_ENABLE_WAMR_COMPILER != 0
  11056. module->is_ref_types_used = true;
  11057. #endif
  11058. break;
  11059. }
  11060. case WASM_OP_REF_FUNC:
  11061. {
  11062. read_leb_uint32(p, p_end, func_idx);
  11063. if (!check_function_index(module, func_idx, error_buf,
  11064. error_buf_size)) {
  11065. goto fail;
  11066. }
  11067. /* Refer to a forward-declared function */
  11068. if (func_idx >= cur_func_idx + module->import_function_count) {
  11069. WASMTableSeg *table_seg = module->table_segments;
  11070. bool func_declared = false;
  11071. uint32 j;
  11072. /* Check whether the function is declared in table segs,
  11073. note that it doesn't matter whether the table seg's mode
  11074. is passive, active or declarative. */
  11075. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  11076. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11077. #if WASM_ENABLE_GC != 0
  11078. || (table_seg->elem_type == REF_TYPE_HT_NON_NULLABLE
  11079. && table_seg->elem_ref_type->ref_ht_common
  11080. .heap_type
  11081. == HEAP_TYPE_FUNC)
  11082. #endif
  11083. ) {
  11084. for (j = 0; j < table_seg->value_count; j++) {
  11085. if (table_seg->init_values[j].u.ref_index
  11086. == func_idx) {
  11087. func_declared = true;
  11088. break;
  11089. }
  11090. }
  11091. }
  11092. }
  11093. if (!func_declared) {
  11094. /* Check whether the function is exported */
  11095. for (i = 0; i < module->export_count; i++) {
  11096. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11097. && module->exports[i].index == func_idx) {
  11098. func_declared = true;
  11099. break;
  11100. }
  11101. }
  11102. }
  11103. if (!func_declared) {
  11104. set_error_buf(error_buf, error_buf_size,
  11105. "undeclared function reference");
  11106. goto fail;
  11107. }
  11108. }
  11109. #if WASM_ENABLE_FAST_INTERP != 0
  11110. emit_uint32(loader_ctx, func_idx);
  11111. #endif
  11112. #if WASM_ENABLE_GC == 0
  11113. PUSH_FUNCREF();
  11114. #else
  11115. if (func_idx < module->import_function_count)
  11116. type_idx =
  11117. module->import_functions[func_idx].u.function.type_idx;
  11118. else
  11119. type_idx = module
  11120. ->functions[func_idx
  11121. - module->import_function_count]
  11122. ->type_idx;
  11123. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11124. false, type_idx);
  11125. PUSH_REF(wasm_ref_type.ref_type);
  11126. #endif
  11127. #if WASM_ENABLE_WAMR_COMPILER != 0
  11128. module->is_ref_types_used = true;
  11129. #endif
  11130. break;
  11131. }
  11132. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11133. #if WASM_ENABLE_GC != 0
  11134. case WASM_OP_REF_AS_NON_NULL:
  11135. case WASM_OP_BR_ON_NULL:
  11136. {
  11137. uint8 type;
  11138. WASMRefType ref_type;
  11139. /* POP (ref null ht) and get the converted (ref ht) */
  11140. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11141. error_buf, error_buf_size)) {
  11142. goto fail;
  11143. }
  11144. if (opcode == WASM_OP_BR_ON_NULL) {
  11145. if (!(frame_csp_tmp =
  11146. check_branch_block(loader_ctx, &p, p_end, false,
  11147. error_buf, error_buf_size))) {
  11148. goto fail;
  11149. }
  11150. #if WASM_ENABLE_FAST_INTERP != 0
  11151. disable_emit = true;
  11152. #endif
  11153. }
  11154. /* PUSH the converted (ref ht) */
  11155. if (type != VALUE_TYPE_ANY) {
  11156. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11157. sizeof(WASMRefType));
  11158. }
  11159. PUSH_REF(type);
  11160. break;
  11161. }
  11162. case WASM_OP_BR_ON_NON_NULL:
  11163. {
  11164. uint8 type;
  11165. WASMRefType ref_type;
  11166. uint32 available_stack_cell =
  11167. loader_ctx->stack_cell_num
  11168. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11169. /* POP (ref null ht) and get the converted (ref ht) */
  11170. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11171. error_buf, error_buf_size)) {
  11172. goto fail;
  11173. }
  11174. #if WASM_ENABLE_FAST_INTERP != 0
  11175. disable_emit = true;
  11176. #endif
  11177. /* Temporarily PUSH back (ref ht), check brach block and
  11178. then POP it */
  11179. if (available_stack_cell
  11180. > 0) { /* stack isn't in polymorphic state */
  11181. if (type != VALUE_TYPE_ANY) {
  11182. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11183. &ref_type, sizeof(WASMRefType));
  11184. }
  11185. PUSH_REF(type);
  11186. }
  11187. if (!(frame_csp_tmp =
  11188. check_branch_block(loader_ctx, &p, p_end, false,
  11189. error_buf, error_buf_size))) {
  11190. goto fail;
  11191. }
  11192. if (available_stack_cell
  11193. > 0) { /* stack isn't in polymorphic state */
  11194. POP_REF(type);
  11195. #if WASM_ENABLE_FAST_INTERP != 0
  11196. /* Erase the opnd offset emitted by POP_REF() */
  11197. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11198. #endif
  11199. }
  11200. break;
  11201. }
  11202. case WASM_OP_REF_EQ:
  11203. POP_REF(REF_TYPE_EQREF);
  11204. POP_REF(REF_TYPE_EQREF);
  11205. PUSH_I32();
  11206. break;
  11207. #endif /* end of WASM_ENABLE_GC != 0 */
  11208. case WASM_OP_GET_LOCAL:
  11209. {
  11210. p_org = p - 1;
  11211. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11212. PUSH_TYPE(local_type);
  11213. #if WASM_ENABLE_GC != 0
  11214. /* Cannot get a non-nullable and unset local */
  11215. if (local_idx >= param_count
  11216. && wasm_is_reftype_htref_non_nullable(local_type)
  11217. && !wasm_loader_get_local_status(loader_ctx,
  11218. local_idx - param_count)) {
  11219. set_error_buf(error_buf, error_buf_size,
  11220. "uninitialized local");
  11221. goto fail;
  11222. }
  11223. #endif
  11224. #if WASM_ENABLE_FAST_INTERP != 0
  11225. /* Get Local is optimized out */
  11226. skip_label();
  11227. disable_emit = true;
  11228. operand_offset = local_offset;
  11229. PUSH_OFFSET_TYPE(local_type);
  11230. #else
  11231. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11232. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11233. if (local_offset < 0x80
  11234. #if WASM_ENABLE_GC != 0
  11235. && !wasm_is_type_reftype(local_type)
  11236. #endif
  11237. ) {
  11238. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11239. if (is_32bit_type(local_type)) {
  11240. *p_org++ = (uint8)local_offset;
  11241. }
  11242. else {
  11243. *p_org++ = (uint8)(local_offset | 0x80);
  11244. }
  11245. while (p_org < p) {
  11246. *p_org++ = WASM_OP_NOP;
  11247. }
  11248. }
  11249. #endif
  11250. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11251. break;
  11252. }
  11253. case WASM_OP_SET_LOCAL:
  11254. {
  11255. p_org = p - 1;
  11256. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11257. #if WASM_ENABLE_FAST_INTERP != 0
  11258. if (!(preserve_referenced_local(
  11259. loader_ctx, opcode, local_offset, local_type,
  11260. &preserve_local, error_buf, error_buf_size)))
  11261. goto fail;
  11262. if (local_offset < 256
  11263. #if WASM_ENABLE_GC != 0
  11264. && !wasm_is_type_reftype(local_type)
  11265. #endif
  11266. ) {
  11267. skip_label();
  11268. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11269. if (loader_ctx->p_code_compiled)
  11270. STORE_U16(loader_ctx->p_code_compiled - 2,
  11271. local_offset);
  11272. loader_ctx->frame_offset--;
  11273. loader_ctx->dynamic_offset--;
  11274. }
  11275. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11276. if (loader_ctx->p_code_compiled)
  11277. STORE_U16(loader_ctx->p_code_compiled - 2,
  11278. local_offset);
  11279. loader_ctx->frame_offset -= 2;
  11280. loader_ctx->dynamic_offset -= 2;
  11281. }
  11282. else {
  11283. if (is_32bit_type(local_type)) {
  11284. emit_label(EXT_OP_SET_LOCAL_FAST);
  11285. emit_byte(loader_ctx, (uint8)local_offset);
  11286. }
  11287. else {
  11288. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11289. emit_byte(loader_ctx, (uint8)local_offset);
  11290. }
  11291. POP_OFFSET_TYPE(local_type);
  11292. }
  11293. }
  11294. else { /* local index larger than 255, reserve leb */
  11295. emit_uint32(loader_ctx, local_idx);
  11296. POP_OFFSET_TYPE(local_type);
  11297. }
  11298. #else
  11299. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11300. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11301. if (local_offset < 0x80
  11302. #if WASM_ENABLE_GC != 0
  11303. && !wasm_is_type_reftype(local_type)
  11304. #endif
  11305. ) {
  11306. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11307. if (is_32bit_type(local_type)) {
  11308. *p_org++ = (uint8)local_offset;
  11309. }
  11310. else {
  11311. *p_org++ = (uint8)(local_offset | 0x80);
  11312. }
  11313. while (p_org < p) {
  11314. *p_org++ = WASM_OP_NOP;
  11315. }
  11316. }
  11317. #endif
  11318. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11319. #if WASM_ENABLE_GC != 0
  11320. if (local_idx >= param_count) {
  11321. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11322. }
  11323. #endif
  11324. POP_TYPE(local_type);
  11325. break;
  11326. }
  11327. case WASM_OP_TEE_LOCAL:
  11328. {
  11329. p_org = p - 1;
  11330. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11331. #if WASM_ENABLE_FAST_INTERP != 0
  11332. /* If the stack is in polymorphic state, do fake pop and push on
  11333. offset stack to keep the depth of offset stack to be the
  11334. same with ref stack */
  11335. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11336. if (cur_block->is_stack_polymorphic) {
  11337. POP_OFFSET_TYPE(local_type);
  11338. PUSH_OFFSET_TYPE(local_type);
  11339. }
  11340. #endif
  11341. POP_TYPE(local_type);
  11342. PUSH_TYPE(local_type);
  11343. #if WASM_ENABLE_FAST_INTERP != 0
  11344. if (!(preserve_referenced_local(
  11345. loader_ctx, opcode, local_offset, local_type,
  11346. &preserve_local, error_buf, error_buf_size)))
  11347. goto fail;
  11348. if (local_offset < 256
  11349. #if WASM_ENABLE_GC != 0
  11350. && !wasm_is_type_reftype(local_type)
  11351. #endif
  11352. ) {
  11353. skip_label();
  11354. if (is_32bit_type(local_type)) {
  11355. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11356. emit_byte(loader_ctx, (uint8)local_offset);
  11357. }
  11358. else {
  11359. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11360. emit_byte(loader_ctx, (uint8)local_offset);
  11361. }
  11362. }
  11363. else { /* local index larger than 255, reserve leb */
  11364. emit_uint32(loader_ctx, local_idx);
  11365. }
  11366. emit_operand(loader_ctx,
  11367. *(loader_ctx->frame_offset
  11368. - wasm_value_type_cell_num(local_type)));
  11369. #else
  11370. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11371. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11372. if (local_offset < 0x80
  11373. #if WASM_ENABLE_GC != 0
  11374. && !wasm_is_type_reftype(local_type)
  11375. #endif
  11376. ) {
  11377. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11378. if (is_32bit_type(local_type)) {
  11379. *p_org++ = (uint8)local_offset;
  11380. }
  11381. else {
  11382. *p_org++ = (uint8)(local_offset | 0x80);
  11383. }
  11384. while (p_org < p) {
  11385. *p_org++ = WASM_OP_NOP;
  11386. }
  11387. }
  11388. #endif
  11389. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11390. #if WASM_ENABLE_GC != 0
  11391. if (local_idx >= param_count) {
  11392. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11393. }
  11394. #endif
  11395. break;
  11396. }
  11397. case WASM_OP_GET_GLOBAL:
  11398. {
  11399. #if WASM_ENABLE_GC != 0
  11400. WASMRefType *ref_type;
  11401. #endif
  11402. p_org = p - 1;
  11403. read_leb_uint32(p, p_end, global_idx);
  11404. if (global_idx >= global_count) {
  11405. set_error_buf(error_buf, error_buf_size, "unknown global");
  11406. goto fail;
  11407. }
  11408. global_type =
  11409. global_idx < module->import_global_count
  11410. ? module->import_globals[global_idx].u.global.type
  11411. : module
  11412. ->globals[global_idx
  11413. - module->import_global_count]
  11414. .type;
  11415. #if WASM_ENABLE_GC != 0
  11416. ref_type =
  11417. global_idx < module->import_global_count
  11418. ? module->import_globals[global_idx].u.global.ref_type
  11419. : module
  11420. ->globals[global_idx
  11421. - module->import_global_count]
  11422. .ref_type;
  11423. if (wasm_is_type_multi_byte_type(global_type)) {
  11424. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11425. wasm_reftype_struct_size(ref_type));
  11426. }
  11427. #endif
  11428. PUSH_TYPE(global_type);
  11429. #if WASM_ENABLE_FAST_INTERP == 0
  11430. if (global_type == VALUE_TYPE_I64
  11431. || global_type == VALUE_TYPE_F64) {
  11432. #if WASM_ENABLE_DEBUG_INTERP != 0
  11433. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11434. error_buf_size)) {
  11435. goto fail;
  11436. }
  11437. #endif
  11438. *p_org = WASM_OP_GET_GLOBAL_64;
  11439. }
  11440. #else /* else of WASM_ENABLE_FAST_INTERP */
  11441. if (global_type == VALUE_TYPE_I64
  11442. || global_type == VALUE_TYPE_F64) {
  11443. skip_label();
  11444. emit_label(WASM_OP_GET_GLOBAL_64);
  11445. }
  11446. emit_uint32(loader_ctx, global_idx);
  11447. PUSH_OFFSET_TYPE(global_type);
  11448. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11449. break;
  11450. }
  11451. case WASM_OP_SET_GLOBAL:
  11452. {
  11453. bool is_mutable = false;
  11454. #if WASM_ENABLE_GC != 0
  11455. WASMRefType *ref_type;
  11456. #endif
  11457. p_org = p - 1;
  11458. read_leb_uint32(p, p_end, global_idx);
  11459. if (global_idx >= global_count) {
  11460. set_error_buf(error_buf, error_buf_size, "unknown global");
  11461. goto fail;
  11462. }
  11463. is_mutable =
  11464. global_idx < module->import_global_count
  11465. ? module->import_globals[global_idx].u.global.is_mutable
  11466. : module
  11467. ->globals[global_idx
  11468. - module->import_global_count]
  11469. .is_mutable;
  11470. if (!is_mutable) {
  11471. #if WASM_ENABLE_GC == 0
  11472. set_error_buf(error_buf, error_buf_size,
  11473. "global is immutable");
  11474. #else
  11475. set_error_buf(error_buf, error_buf_size,
  11476. "immutable global");
  11477. #endif
  11478. goto fail;
  11479. }
  11480. global_type =
  11481. global_idx < module->import_global_count
  11482. ? module->import_globals[global_idx].u.global.type
  11483. : module
  11484. ->globals[global_idx
  11485. - module->import_global_count]
  11486. .type;
  11487. #if WASM_ENABLE_GC != 0
  11488. ref_type =
  11489. global_idx < module->import_global_count
  11490. ? module->import_globals[global_idx].u.global.ref_type
  11491. : module
  11492. ->globals[global_idx
  11493. - module->import_global_count]
  11494. .ref_type;
  11495. if (wasm_is_type_multi_byte_type(global_type)) {
  11496. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11497. wasm_reftype_struct_size(ref_type));
  11498. }
  11499. #endif
  11500. #if WASM_ENABLE_FAST_INTERP == 0
  11501. if (global_type == VALUE_TYPE_I64
  11502. || global_type == VALUE_TYPE_F64) {
  11503. #if WASM_ENABLE_DEBUG_INTERP != 0
  11504. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11505. error_buf_size)) {
  11506. goto fail;
  11507. }
  11508. #endif
  11509. *p_org = WASM_OP_SET_GLOBAL_64;
  11510. }
  11511. else if (module->aux_stack_size > 0
  11512. && global_idx == module->aux_stack_top_global_index) {
  11513. #if WASM_ENABLE_DEBUG_INTERP != 0
  11514. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11515. error_buf_size)) {
  11516. goto fail;
  11517. }
  11518. #endif
  11519. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11520. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11521. func->has_op_set_global_aux_stack = true;
  11522. #endif
  11523. }
  11524. #else /* else of WASM_ENABLE_FAST_INTERP */
  11525. if (global_type == VALUE_TYPE_I64
  11526. || global_type == VALUE_TYPE_F64) {
  11527. skip_label();
  11528. emit_label(WASM_OP_SET_GLOBAL_64);
  11529. }
  11530. else if (module->aux_stack_size > 0
  11531. && global_idx == module->aux_stack_top_global_index) {
  11532. skip_label();
  11533. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11534. }
  11535. emit_uint32(loader_ctx, global_idx);
  11536. POP_OFFSET_TYPE(global_type);
  11537. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11538. POP_TYPE(global_type);
  11539. break;
  11540. }
  11541. /* load */
  11542. case WASM_OP_I32_LOAD:
  11543. case WASM_OP_I32_LOAD8_S:
  11544. case WASM_OP_I32_LOAD8_U:
  11545. case WASM_OP_I32_LOAD16_S:
  11546. case WASM_OP_I32_LOAD16_U:
  11547. case WASM_OP_I64_LOAD:
  11548. case WASM_OP_I64_LOAD8_S:
  11549. case WASM_OP_I64_LOAD8_U:
  11550. case WASM_OP_I64_LOAD16_S:
  11551. case WASM_OP_I64_LOAD16_U:
  11552. case WASM_OP_I64_LOAD32_S:
  11553. case WASM_OP_I64_LOAD32_U:
  11554. case WASM_OP_F32_LOAD:
  11555. case WASM_OP_F64_LOAD:
  11556. /* store */
  11557. case WASM_OP_I32_STORE:
  11558. case WASM_OP_I32_STORE8:
  11559. case WASM_OP_I32_STORE16:
  11560. case WASM_OP_I64_STORE:
  11561. case WASM_OP_I64_STORE8:
  11562. case WASM_OP_I64_STORE16:
  11563. case WASM_OP_I64_STORE32:
  11564. case WASM_OP_F32_STORE:
  11565. case WASM_OP_F64_STORE:
  11566. {
  11567. #if WASM_ENABLE_FAST_INTERP != 0
  11568. /* change F32/F64 into I32/I64 */
  11569. if (opcode == WASM_OP_F32_LOAD) {
  11570. skip_label();
  11571. emit_label(WASM_OP_I32_LOAD);
  11572. }
  11573. else if (opcode == WASM_OP_F64_LOAD) {
  11574. skip_label();
  11575. emit_label(WASM_OP_I64_LOAD);
  11576. }
  11577. else if (opcode == WASM_OP_F32_STORE) {
  11578. skip_label();
  11579. emit_label(WASM_OP_I32_STORE);
  11580. }
  11581. else if (opcode == WASM_OP_F64_STORE) {
  11582. skip_label();
  11583. emit_label(WASM_OP_I64_STORE);
  11584. }
  11585. #endif
  11586. CHECK_MEMORY();
  11587. read_leb_uint32(p, p_end, align); /* align */
  11588. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11589. if (!check_memory_access_align(opcode, align, error_buf,
  11590. error_buf_size)) {
  11591. goto fail;
  11592. }
  11593. #if WASM_ENABLE_FAST_INTERP != 0
  11594. emit_uint32(loader_ctx, mem_offset);
  11595. #endif
  11596. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11597. func->has_memory_operations = true;
  11598. #endif
  11599. switch (opcode) {
  11600. /* load */
  11601. case WASM_OP_I32_LOAD:
  11602. case WASM_OP_I32_LOAD8_S:
  11603. case WASM_OP_I32_LOAD8_U:
  11604. case WASM_OP_I32_LOAD16_S:
  11605. case WASM_OP_I32_LOAD16_U:
  11606. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11607. break;
  11608. case WASM_OP_I64_LOAD:
  11609. case WASM_OP_I64_LOAD8_S:
  11610. case WASM_OP_I64_LOAD8_U:
  11611. case WASM_OP_I64_LOAD16_S:
  11612. case WASM_OP_I64_LOAD16_U:
  11613. case WASM_OP_I64_LOAD32_S:
  11614. case WASM_OP_I64_LOAD32_U:
  11615. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11616. break;
  11617. case WASM_OP_F32_LOAD:
  11618. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11619. break;
  11620. case WASM_OP_F64_LOAD:
  11621. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  11622. break;
  11623. /* store */
  11624. case WASM_OP_I32_STORE:
  11625. case WASM_OP_I32_STORE8:
  11626. case WASM_OP_I32_STORE16:
  11627. POP_I32();
  11628. POP_I32();
  11629. break;
  11630. case WASM_OP_I64_STORE:
  11631. case WASM_OP_I64_STORE8:
  11632. case WASM_OP_I64_STORE16:
  11633. case WASM_OP_I64_STORE32:
  11634. POP_I64();
  11635. POP_I32();
  11636. break;
  11637. case WASM_OP_F32_STORE:
  11638. POP_F32();
  11639. POP_I32();
  11640. break;
  11641. case WASM_OP_F64_STORE:
  11642. POP_F64();
  11643. POP_I32();
  11644. break;
  11645. default:
  11646. break;
  11647. }
  11648. break;
  11649. }
  11650. case WASM_OP_MEMORY_SIZE:
  11651. CHECK_MEMORY();
  11652. /* reserved byte 0x00 */
  11653. if (*p++ != 0x00) {
  11654. set_error_buf(error_buf, error_buf_size,
  11655. "zero byte expected");
  11656. goto fail;
  11657. }
  11658. PUSH_I32();
  11659. module->possible_memory_grow = true;
  11660. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11661. func->has_memory_operations = true;
  11662. #endif
  11663. break;
  11664. case WASM_OP_MEMORY_GROW:
  11665. CHECK_MEMORY();
  11666. /* reserved byte 0x00 */
  11667. if (*p++ != 0x00) {
  11668. set_error_buf(error_buf, error_buf_size,
  11669. "zero byte expected");
  11670. goto fail;
  11671. }
  11672. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11673. module->possible_memory_grow = true;
  11674. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11675. || WASM_ENABLE_WAMR_COMPILER != 0
  11676. func->has_op_memory_grow = true;
  11677. #endif
  11678. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11679. func->has_memory_operations = true;
  11680. #endif
  11681. break;
  11682. case WASM_OP_I32_CONST:
  11683. read_leb_int32(p, p_end, i32_const);
  11684. #if WASM_ENABLE_FAST_INTERP != 0
  11685. skip_label();
  11686. disable_emit = true;
  11687. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11688. if (operand_offset == 0) {
  11689. disable_emit = false;
  11690. emit_label(WASM_OP_I32_CONST);
  11691. emit_uint32(loader_ctx, i32_const);
  11692. }
  11693. #else
  11694. (void)i32_const;
  11695. #endif
  11696. PUSH_I32();
  11697. break;
  11698. case WASM_OP_I64_CONST:
  11699. read_leb_int64(p, p_end, i64_const);
  11700. #if WASM_ENABLE_FAST_INTERP != 0
  11701. skip_label();
  11702. disable_emit = true;
  11703. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11704. if (operand_offset == 0) {
  11705. disable_emit = false;
  11706. emit_label(WASM_OP_I64_CONST);
  11707. emit_uint64(loader_ctx, i64_const);
  11708. }
  11709. #endif
  11710. PUSH_I64();
  11711. break;
  11712. case WASM_OP_F32_CONST:
  11713. p += sizeof(float32);
  11714. #if WASM_ENABLE_FAST_INTERP != 0
  11715. skip_label();
  11716. disable_emit = true;
  11717. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11718. sizeof(float32));
  11719. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11720. if (operand_offset == 0) {
  11721. disable_emit = false;
  11722. emit_label(WASM_OP_F32_CONST);
  11723. emit_float32(loader_ctx, f32_const);
  11724. }
  11725. #endif
  11726. PUSH_F32();
  11727. break;
  11728. case WASM_OP_F64_CONST:
  11729. p += sizeof(float64);
  11730. #if WASM_ENABLE_FAST_INTERP != 0
  11731. skip_label();
  11732. disable_emit = true;
  11733. /* Some MCU may require 8-byte align */
  11734. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  11735. sizeof(float64));
  11736. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  11737. if (operand_offset == 0) {
  11738. disable_emit = false;
  11739. emit_label(WASM_OP_F64_CONST);
  11740. emit_float64(loader_ctx, f64_const);
  11741. }
  11742. #endif
  11743. PUSH_F64();
  11744. break;
  11745. case WASM_OP_I32_EQZ:
  11746. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11747. break;
  11748. case WASM_OP_I32_EQ:
  11749. case WASM_OP_I32_NE:
  11750. case WASM_OP_I32_LT_S:
  11751. case WASM_OP_I32_LT_U:
  11752. case WASM_OP_I32_GT_S:
  11753. case WASM_OP_I32_GT_U:
  11754. case WASM_OP_I32_LE_S:
  11755. case WASM_OP_I32_LE_U:
  11756. case WASM_OP_I32_GE_S:
  11757. case WASM_OP_I32_GE_U:
  11758. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11759. break;
  11760. case WASM_OP_I64_EQZ:
  11761. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11762. break;
  11763. case WASM_OP_I64_EQ:
  11764. case WASM_OP_I64_NE:
  11765. case WASM_OP_I64_LT_S:
  11766. case WASM_OP_I64_LT_U:
  11767. case WASM_OP_I64_GT_S:
  11768. case WASM_OP_I64_GT_U:
  11769. case WASM_OP_I64_LE_S:
  11770. case WASM_OP_I64_LE_U:
  11771. case WASM_OP_I64_GE_S:
  11772. case WASM_OP_I64_GE_U:
  11773. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11774. break;
  11775. case WASM_OP_F32_EQ:
  11776. case WASM_OP_F32_NE:
  11777. case WASM_OP_F32_LT:
  11778. case WASM_OP_F32_GT:
  11779. case WASM_OP_F32_LE:
  11780. case WASM_OP_F32_GE:
  11781. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11782. break;
  11783. case WASM_OP_F64_EQ:
  11784. case WASM_OP_F64_NE:
  11785. case WASM_OP_F64_LT:
  11786. case WASM_OP_F64_GT:
  11787. case WASM_OP_F64_LE:
  11788. case WASM_OP_F64_GE:
  11789. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11790. break;
  11791. case WASM_OP_I32_CLZ:
  11792. case WASM_OP_I32_CTZ:
  11793. case WASM_OP_I32_POPCNT:
  11794. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11795. break;
  11796. case WASM_OP_I32_ADD:
  11797. case WASM_OP_I32_SUB:
  11798. case WASM_OP_I32_MUL:
  11799. case WASM_OP_I32_DIV_S:
  11800. case WASM_OP_I32_DIV_U:
  11801. case WASM_OP_I32_REM_S:
  11802. case WASM_OP_I32_REM_U:
  11803. case WASM_OP_I32_AND:
  11804. case WASM_OP_I32_OR:
  11805. case WASM_OP_I32_XOR:
  11806. case WASM_OP_I32_SHL:
  11807. case WASM_OP_I32_SHR_S:
  11808. case WASM_OP_I32_SHR_U:
  11809. case WASM_OP_I32_ROTL:
  11810. case WASM_OP_I32_ROTR:
  11811. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11812. break;
  11813. case WASM_OP_I64_CLZ:
  11814. case WASM_OP_I64_CTZ:
  11815. case WASM_OP_I64_POPCNT:
  11816. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11817. break;
  11818. case WASM_OP_I64_ADD:
  11819. case WASM_OP_I64_SUB:
  11820. case WASM_OP_I64_MUL:
  11821. case WASM_OP_I64_DIV_S:
  11822. case WASM_OP_I64_DIV_U:
  11823. case WASM_OP_I64_REM_S:
  11824. case WASM_OP_I64_REM_U:
  11825. case WASM_OP_I64_AND:
  11826. case WASM_OP_I64_OR:
  11827. case WASM_OP_I64_XOR:
  11828. case WASM_OP_I64_SHL:
  11829. case WASM_OP_I64_SHR_S:
  11830. case WASM_OP_I64_SHR_U:
  11831. case WASM_OP_I64_ROTL:
  11832. case WASM_OP_I64_ROTR:
  11833. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11834. break;
  11835. case WASM_OP_F32_ABS:
  11836. case WASM_OP_F32_NEG:
  11837. case WASM_OP_F32_CEIL:
  11838. case WASM_OP_F32_FLOOR:
  11839. case WASM_OP_F32_TRUNC:
  11840. case WASM_OP_F32_NEAREST:
  11841. case WASM_OP_F32_SQRT:
  11842. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11843. break;
  11844. case WASM_OP_F32_ADD:
  11845. case WASM_OP_F32_SUB:
  11846. case WASM_OP_F32_MUL:
  11847. case WASM_OP_F32_DIV:
  11848. case WASM_OP_F32_MIN:
  11849. case WASM_OP_F32_MAX:
  11850. case WASM_OP_F32_COPYSIGN:
  11851. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11852. break;
  11853. case WASM_OP_F64_ABS:
  11854. case WASM_OP_F64_NEG:
  11855. case WASM_OP_F64_CEIL:
  11856. case WASM_OP_F64_FLOOR:
  11857. case WASM_OP_F64_TRUNC:
  11858. case WASM_OP_F64_NEAREST:
  11859. case WASM_OP_F64_SQRT:
  11860. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11861. break;
  11862. case WASM_OP_F64_ADD:
  11863. case WASM_OP_F64_SUB:
  11864. case WASM_OP_F64_MUL:
  11865. case WASM_OP_F64_DIV:
  11866. case WASM_OP_F64_MIN:
  11867. case WASM_OP_F64_MAX:
  11868. case WASM_OP_F64_COPYSIGN:
  11869. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11870. break;
  11871. case WASM_OP_I32_WRAP_I64:
  11872. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11873. break;
  11874. case WASM_OP_I32_TRUNC_S_F32:
  11875. case WASM_OP_I32_TRUNC_U_F32:
  11876. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11877. break;
  11878. case WASM_OP_I32_TRUNC_S_F64:
  11879. case WASM_OP_I32_TRUNC_U_F64:
  11880. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11881. break;
  11882. case WASM_OP_I64_EXTEND_S_I32:
  11883. case WASM_OP_I64_EXTEND_U_I32:
  11884. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11885. break;
  11886. case WASM_OP_I64_TRUNC_S_F32:
  11887. case WASM_OP_I64_TRUNC_U_F32:
  11888. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  11889. break;
  11890. case WASM_OP_I64_TRUNC_S_F64:
  11891. case WASM_OP_I64_TRUNC_U_F64:
  11892. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  11893. break;
  11894. case WASM_OP_F32_CONVERT_S_I32:
  11895. case WASM_OP_F32_CONVERT_U_I32:
  11896. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11897. break;
  11898. case WASM_OP_F32_CONVERT_S_I64:
  11899. case WASM_OP_F32_CONVERT_U_I64:
  11900. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  11901. break;
  11902. case WASM_OP_F32_DEMOTE_F64:
  11903. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  11904. break;
  11905. case WASM_OP_F64_CONVERT_S_I32:
  11906. case WASM_OP_F64_CONVERT_U_I32:
  11907. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  11908. break;
  11909. case WASM_OP_F64_CONVERT_S_I64:
  11910. case WASM_OP_F64_CONVERT_U_I64:
  11911. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  11912. break;
  11913. case WASM_OP_F64_PROMOTE_F32:
  11914. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  11915. break;
  11916. case WASM_OP_I32_REINTERPRET_F32:
  11917. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11918. break;
  11919. case WASM_OP_I64_REINTERPRET_F64:
  11920. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  11921. break;
  11922. case WASM_OP_F32_REINTERPRET_I32:
  11923. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11924. break;
  11925. case WASM_OP_F64_REINTERPRET_I64:
  11926. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  11927. break;
  11928. case WASM_OP_I32_EXTEND8_S:
  11929. case WASM_OP_I32_EXTEND16_S:
  11930. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11931. break;
  11932. case WASM_OP_I64_EXTEND8_S:
  11933. case WASM_OP_I64_EXTEND16_S:
  11934. case WASM_OP_I64_EXTEND32_S:
  11935. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11936. break;
  11937. #if WASM_ENABLE_GC != 0
  11938. case WASM_OP_GC_PREFIX:
  11939. {
  11940. uint32 opcode1;
  11941. read_leb_uint32(p, p_end, opcode1);
  11942. #if WASM_ENABLE_FAST_INTERP != 0
  11943. emit_byte(loader_ctx, ((uint8)opcode1));
  11944. #endif
  11945. switch (opcode1) {
  11946. case WASM_OP_STRUCT_NEW:
  11947. case WASM_OP_STRUCT_NEW_DEFAULT:
  11948. {
  11949. read_leb_uint32(p, p_end, type_idx);
  11950. #if WASM_ENABLE_FAST_INTERP != 0
  11951. emit_uint32(loader_ctx, type_idx);
  11952. #endif
  11953. if (!check_type_index(module, type_idx, error_buf,
  11954. error_buf_size)) {
  11955. goto fail;
  11956. }
  11957. if (module->types[type_idx]->type_flag
  11958. != WASM_TYPE_STRUCT) {
  11959. set_error_buf(error_buf, error_buf_size,
  11960. "unkown struct type");
  11961. goto fail;
  11962. }
  11963. if (opcode1 == WASM_OP_STRUCT_NEW) {
  11964. int32 j, k;
  11965. uint8 value_type;
  11966. uint32 ref_type_struct_size;
  11967. WASMStructType *struct_type =
  11968. (WASMStructType *)module->types[type_idx];
  11969. k = struct_type->ref_type_map_count - 1;
  11970. for (j = struct_type->field_count - 1; j >= 0;
  11971. j--) {
  11972. value_type = struct_type->fields[j].field_type;
  11973. if (wasm_is_type_reftype(value_type)) {
  11974. if (wasm_is_type_multi_byte_type(
  11975. value_type)) {
  11976. ref_type_struct_size =
  11977. wasm_reftype_struct_size(
  11978. struct_type->ref_type_maps[k]
  11979. .ref_type);
  11980. bh_memcpy_s(
  11981. &wasm_ref_type,
  11982. (uint32)sizeof(WASMRefType),
  11983. struct_type->ref_type_maps[k]
  11984. .ref_type,
  11985. ref_type_struct_size);
  11986. k--;
  11987. }
  11988. POP_REF(value_type);
  11989. }
  11990. else {
  11991. switch (value_type) {
  11992. case VALUE_TYPE_I32:
  11993. case PACKED_TYPE_I8:
  11994. case PACKED_TYPE_I16:
  11995. POP_I32();
  11996. break;
  11997. case VALUE_TYPE_I64:
  11998. POP_I64();
  11999. break;
  12000. case VALUE_TYPE_F32:
  12001. POP_F32();
  12002. break;
  12003. case VALUE_TYPE_F64:
  12004. POP_F64();
  12005. break;
  12006. default:
  12007. set_error_buf(error_buf,
  12008. error_buf_size,
  12009. "unknown type");
  12010. goto fail;
  12011. }
  12012. }
  12013. }
  12014. }
  12015. /* PUSH struct obj, (ref $t) */
  12016. wasm_set_refheaptype_typeidx(
  12017. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12018. PUSH_REF(wasm_ref_type.ref_type);
  12019. break;
  12020. }
  12021. case WASM_OP_STRUCT_GET:
  12022. case WASM_OP_STRUCT_GET_S:
  12023. case WASM_OP_STRUCT_GET_U:
  12024. case WASM_OP_STRUCT_SET:
  12025. {
  12026. WASMStructType *struct_type;
  12027. WASMRefType *ref_type = NULL;
  12028. uint32 field_idx;
  12029. uint8 field_type;
  12030. read_leb_uint32(p, p_end, type_idx);
  12031. #if WASM_ENABLE_FAST_INTERP != 0
  12032. emit_uint32(loader_ctx, type_idx);
  12033. #endif
  12034. if (!check_type_index(module, type_idx, error_buf,
  12035. error_buf_size)) {
  12036. goto fail;
  12037. }
  12038. if (module->types[type_idx]->type_flag
  12039. != WASM_TYPE_STRUCT) {
  12040. set_error_buf(error_buf, error_buf_size,
  12041. "unknown struct type");
  12042. goto fail;
  12043. }
  12044. struct_type = (WASMStructType *)module->types[type_idx];
  12045. read_leb_uint32(p, p_end, field_idx);
  12046. #if WASM_ENABLE_FAST_INTERP != 0
  12047. emit_uint32(loader_ctx, field_idx);
  12048. #endif
  12049. if (field_idx >= struct_type->field_count) {
  12050. set_error_buf(error_buf, error_buf_size,
  12051. "unknown struct field");
  12052. goto fail;
  12053. }
  12054. if (opcode1 == WASM_OP_STRUCT_SET
  12055. && !(struct_type->fields[field_idx].field_flags
  12056. & 1)) {
  12057. set_error_buf(error_buf, error_buf_size,
  12058. "field is immutable");
  12059. goto fail;
  12060. }
  12061. field_type = struct_type->fields[field_idx].field_type;
  12062. if (is_packed_type(field_type)) {
  12063. if (opcode1 == WASM_OP_STRUCT_GET) {
  12064. set_error_buf(error_buf, error_buf_size,
  12065. "type mismatch");
  12066. goto fail;
  12067. }
  12068. else {
  12069. field_type = VALUE_TYPE_I32;
  12070. }
  12071. }
  12072. if (wasm_is_type_multi_byte_type(field_type)) {
  12073. ref_type = wasm_reftype_map_find(
  12074. struct_type->ref_type_maps,
  12075. struct_type->ref_type_map_count, field_idx);
  12076. bh_assert(ref_type);
  12077. }
  12078. if (opcode1 == WASM_OP_STRUCT_SET) {
  12079. /* POP field */
  12080. if (wasm_is_type_multi_byte_type(field_type)) {
  12081. bh_memcpy_s(&wasm_ref_type,
  12082. (uint32)sizeof(WASMRefType),
  12083. ref_type,
  12084. wasm_reftype_struct_size(ref_type));
  12085. }
  12086. POP_REF(field_type);
  12087. /* POP struct obj, (ref null $t) */
  12088. wasm_set_refheaptype_typeidx(
  12089. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12090. POP_REF(wasm_ref_type.ref_type);
  12091. }
  12092. else {
  12093. /* POP struct obj, (ref null $t) */
  12094. wasm_set_refheaptype_typeidx(
  12095. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12096. POP_REF(wasm_ref_type.ref_type);
  12097. /* PUSH field */
  12098. if (wasm_is_type_multi_byte_type(field_type)) {
  12099. bh_memcpy_s(&wasm_ref_type,
  12100. (uint32)sizeof(WASMRefType),
  12101. ref_type,
  12102. wasm_reftype_struct_size(ref_type));
  12103. }
  12104. PUSH_REF(field_type);
  12105. }
  12106. break;
  12107. }
  12108. case WASM_OP_ARRAY_NEW:
  12109. case WASM_OP_ARRAY_NEW_DEFAULT:
  12110. case WASM_OP_ARRAY_NEW_FIXED:
  12111. case WASM_OP_ARRAY_NEW_DATA:
  12112. case WASM_OP_ARRAY_NEW_ELEM:
  12113. {
  12114. WASMArrayType *array_type;
  12115. uint8 elem_type;
  12116. uint32 u32 = 0;
  12117. read_leb_uint32(p, p_end, type_idx);
  12118. #if WASM_ENABLE_FAST_INTERP != 0
  12119. emit_uint32(loader_ctx, type_idx);
  12120. #endif
  12121. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12122. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12123. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12124. read_leb_uint32(p, p_end, u32);
  12125. #if WASM_ENABLE_FAST_INTERP != 0
  12126. emit_uint32(loader_ctx, u32);
  12127. #endif
  12128. }
  12129. if (!check_array_type(module, type_idx, error_buf,
  12130. error_buf_size)) {
  12131. goto fail;
  12132. }
  12133. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12134. /* length */
  12135. POP_I32();
  12136. }
  12137. array_type = (WASMArrayType *)module->types[type_idx];
  12138. elem_type = array_type->elem_type;
  12139. if (opcode1 == WASM_OP_ARRAY_NEW
  12140. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12141. if (wasm_is_type_multi_byte_type(elem_type)) {
  12142. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12143. array_type->elem_ref_type,
  12144. wasm_reftype_struct_size(
  12145. array_type->elem_ref_type));
  12146. }
  12147. if (is_packed_type(elem_type)) {
  12148. elem_type = VALUE_TYPE_I32;
  12149. }
  12150. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12151. uint32 N = u32;
  12152. for (i = 0; i < N; i++) {
  12153. if (wasm_is_type_multi_byte_type(
  12154. elem_type)) {
  12155. bh_memcpy_s(
  12156. &wasm_ref_type, sizeof(WASMRefType),
  12157. array_type->elem_ref_type,
  12158. wasm_reftype_struct_size(
  12159. array_type->elem_ref_type));
  12160. }
  12161. POP_REF(elem_type);
  12162. }
  12163. }
  12164. else
  12165. POP_REF(elem_type);
  12166. }
  12167. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12168. /* offset of data segment */
  12169. POP_I32();
  12170. if (u32 >= module->data_seg_count) {
  12171. set_error_buf(error_buf, error_buf_size,
  12172. "unknown data segement");
  12173. goto fail;
  12174. }
  12175. if (wasm_is_type_reftype(elem_type)) {
  12176. set_error_buf(error_buf, error_buf_size,
  12177. "array elem type mismatch");
  12178. goto fail;
  12179. }
  12180. }
  12181. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12182. WASMTableSeg *table_seg =
  12183. module->table_segments + u32;
  12184. /* offset of element segment */
  12185. POP_I32();
  12186. if (u32 >= module->table_seg_count) {
  12187. set_error_buf(error_buf, error_buf_size,
  12188. "unknown element segement");
  12189. goto fail;
  12190. }
  12191. if (!wasm_reftype_is_subtype_of(
  12192. table_seg->elem_type,
  12193. table_seg->elem_ref_type, elem_type,
  12194. array_type->elem_ref_type, module->types,
  12195. module->type_count)) {
  12196. set_error_buf(error_buf, error_buf_size,
  12197. "array elem type mismatch");
  12198. goto fail;
  12199. }
  12200. }
  12201. /* PUSH array obj, (ref $t) */
  12202. wasm_set_refheaptype_typeidx(
  12203. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12204. PUSH_REF(wasm_ref_type.ref_type);
  12205. break;
  12206. }
  12207. case WASM_OP_ARRAY_GET:
  12208. case WASM_OP_ARRAY_GET_S:
  12209. case WASM_OP_ARRAY_GET_U:
  12210. case WASM_OP_ARRAY_SET:
  12211. {
  12212. uint8 elem_type;
  12213. WASMArrayType *array_type;
  12214. WASMRefType *ref_type = NULL;
  12215. read_leb_uint32(p, p_end, type_idx);
  12216. #if WASM_ENABLE_FAST_INTERP != 0
  12217. emit_uint32(loader_ctx, type_idx);
  12218. #endif
  12219. if (!check_array_type(module, type_idx, error_buf,
  12220. error_buf_size)) {
  12221. goto fail;
  12222. }
  12223. array_type = (WASMArrayType *)module->types[type_idx];
  12224. if (opcode1 == WASM_OP_ARRAY_SET
  12225. && !(array_type->elem_flags & 1)) {
  12226. set_error_buf(error_buf, error_buf_size,
  12227. "array is immutable");
  12228. goto fail;
  12229. }
  12230. elem_type = array_type->elem_type;
  12231. if (is_packed_type(elem_type)) {
  12232. if (opcode1 != WASM_OP_ARRAY_GET_S
  12233. && opcode1 != WASM_OP_ARRAY_GET_U
  12234. && opcode1 != WASM_OP_ARRAY_SET) {
  12235. set_error_buf(error_buf, error_buf_size,
  12236. "type mismatch");
  12237. goto fail;
  12238. }
  12239. else {
  12240. elem_type = VALUE_TYPE_I32;
  12241. }
  12242. }
  12243. ref_type = array_type->elem_ref_type;
  12244. if (opcode1 == WASM_OP_ARRAY_SET) {
  12245. /* POP elem to set */
  12246. if (wasm_is_type_multi_byte_type(elem_type)) {
  12247. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12248. ref_type,
  12249. wasm_reftype_struct_size(ref_type));
  12250. }
  12251. POP_REF(elem_type);
  12252. }
  12253. /* elem idx */
  12254. POP_I32();
  12255. /* POP array obj, (ref null $t) */
  12256. wasm_set_refheaptype_typeidx(
  12257. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12258. POP_REF(wasm_ref_type.ref_type);
  12259. if (opcode1 != WASM_OP_ARRAY_SET) {
  12260. /* PUSH elem */
  12261. if (wasm_is_type_multi_byte_type(elem_type)) {
  12262. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12263. ref_type,
  12264. wasm_reftype_struct_size(ref_type));
  12265. }
  12266. PUSH_REF(elem_type);
  12267. }
  12268. break;
  12269. }
  12270. case WASM_OP_ARRAY_LEN:
  12271. {
  12272. POP_REF(REF_TYPE_ARRAYREF);
  12273. /* length */
  12274. PUSH_I32();
  12275. break;
  12276. }
  12277. case WASM_OP_ARRAY_FILL:
  12278. {
  12279. WASMArrayType *array_type;
  12280. uint8 elem_type;
  12281. /* typeidx */
  12282. read_leb_uint32(p, p_end, type_idx);
  12283. #if WASM_ENABLE_FAST_INTERP != 0
  12284. emit_uint32(loader_ctx, type_idx);
  12285. #endif
  12286. if (!check_array_type(module, type_idx, error_buf,
  12287. error_buf_size)) {
  12288. goto fail;
  12289. }
  12290. array_type = (WASMArrayType *)module->types[type_idx];
  12291. if (!(array_type->elem_flags & 1)) {
  12292. set_error_buf(error_buf, error_buf_size,
  12293. "array is immutable");
  12294. goto fail;
  12295. }
  12296. elem_type = array_type->elem_type;
  12297. if (is_packed_type(elem_type)) {
  12298. elem_type = VALUE_TYPE_I32;
  12299. }
  12300. POP_I32(); /* length */
  12301. #if WASM_ENABLE_FAST_INTERP != 0
  12302. POP_OFFSET_TYPE(elem_type);
  12303. #endif
  12304. POP_TYPE(elem_type);
  12305. POP_I32(); /* start */
  12306. /* POP array obj, (ref null $t) */
  12307. wasm_set_refheaptype_typeidx(
  12308. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12309. POP_REF(wasm_ref_type.ref_type);
  12310. break;
  12311. }
  12312. case WASM_OP_ARRAY_COPY:
  12313. {
  12314. uint32 src_type_idx;
  12315. uint8 src_elem_type, dst_elem_type;
  12316. WASMRefType src_ref_type = { 0 },
  12317. *src_elem_ref_type = NULL;
  12318. WASMRefType dst_ref_type = { 0 },
  12319. *dst_elem_ref_type = NULL;
  12320. WASMArrayType *array_type;
  12321. /* typeidx1 */
  12322. read_leb_uint32(p, p_end, type_idx);
  12323. #if WASM_ENABLE_FAST_INTERP != 0
  12324. emit_uint32(loader_ctx, type_idx);
  12325. #endif
  12326. /* typeidx2 */
  12327. read_leb_uint32(p, p_end, src_type_idx);
  12328. #if WASM_ENABLE_FAST_INTERP != 0
  12329. emit_uint32(loader_ctx, src_type_idx);
  12330. #endif
  12331. if (!check_array_type(module, type_idx, error_buf,
  12332. error_buf_size)) {
  12333. goto fail;
  12334. }
  12335. if (!check_array_type(module, src_type_idx, error_buf,
  12336. error_buf_size)) {
  12337. goto fail;
  12338. }
  12339. POP_I32();
  12340. POP_I32();
  12341. /* POP array obj, (ref null $t) */
  12342. wasm_set_refheaptype_typeidx(
  12343. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12344. POP_REF(wasm_ref_type.ref_type);
  12345. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12346. &wasm_ref_type,
  12347. wasm_reftype_struct_size(&wasm_ref_type));
  12348. POP_I32();
  12349. /* POP array obj, (ref null $t) */
  12350. wasm_set_refheaptype_typeidx(
  12351. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12352. POP_REF(wasm_ref_type.ref_type);
  12353. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12354. &wasm_ref_type,
  12355. wasm_reftype_struct_size(&wasm_ref_type));
  12356. array_type = (WASMArrayType *)module->types[type_idx];
  12357. if (!(array_type->elem_flags & 1)) {
  12358. set_error_buf(error_buf, error_buf_size,
  12359. "destination array is immutable");
  12360. goto fail;
  12361. }
  12362. dst_elem_type = array_type->elem_type;
  12363. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12364. dst_elem_ref_type = array_type->elem_ref_type;
  12365. }
  12366. array_type =
  12367. (WASMArrayType *)module->types[src_type_idx];
  12368. src_elem_type = array_type->elem_type;
  12369. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12370. src_elem_ref_type = array_type->elem_ref_type;
  12371. }
  12372. if (!wasm_reftype_is_subtype_of(
  12373. src_elem_type, src_elem_ref_type, dst_elem_type,
  12374. dst_elem_ref_type, module->types,
  12375. module->type_count)) {
  12376. set_error_buf(error_buf, error_buf_size,
  12377. "array types do not match");
  12378. goto fail;
  12379. }
  12380. break;
  12381. }
  12382. case WASM_OP_REF_I31:
  12383. {
  12384. POP_I32();
  12385. wasm_set_refheaptype_common(
  12386. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12387. PUSH_REF(wasm_ref_type.ref_type);
  12388. break;
  12389. }
  12390. case WASM_OP_I31_GET_S:
  12391. case WASM_OP_I31_GET_U:
  12392. {
  12393. POP_REF(REF_TYPE_I31REF);
  12394. PUSH_I32();
  12395. break;
  12396. }
  12397. case WASM_OP_REF_TEST:
  12398. case WASM_OP_REF_CAST:
  12399. case WASM_OP_REF_TEST_NULLABLE:
  12400. case WASM_OP_REF_CAST_NULLABLE:
  12401. {
  12402. uint8 type;
  12403. read_leb_int32(p, p_end, heap_type);
  12404. #if WASM_ENABLE_FAST_INTERP != 0
  12405. emit_uint32(loader_ctx, (uint32)heap_type);
  12406. #endif
  12407. if (heap_type >= 0) {
  12408. if (!check_type_index(module, heap_type, error_buf,
  12409. error_buf_size)) {
  12410. goto fail;
  12411. }
  12412. }
  12413. else {
  12414. if (!wasm_is_valid_heap_type(heap_type)) {
  12415. set_error_buf(error_buf, error_buf_size,
  12416. "unknown type");
  12417. goto fail;
  12418. }
  12419. }
  12420. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12421. &wasm_ref_type, error_buf,
  12422. error_buf_size)) {
  12423. goto fail;
  12424. }
  12425. if (opcode1 == WASM_OP_REF_TEST
  12426. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12427. PUSH_I32();
  12428. else {
  12429. bool nullable =
  12430. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12431. : false;
  12432. if (heap_type >= 0 || !nullable) {
  12433. wasm_set_refheaptype_typeidx(
  12434. &wasm_ref_type.ref_ht_typeidx, nullable,
  12435. heap_type);
  12436. PUSH_REF(wasm_ref_type.ref_type);
  12437. }
  12438. else {
  12439. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12440. }
  12441. }
  12442. break;
  12443. }
  12444. case WASM_OP_BR_ON_CAST:
  12445. case WASM_OP_BR_ON_CAST_FAIL:
  12446. {
  12447. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12448. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12449. uint8 type_tmp, castflags;
  12450. uint32 depth;
  12451. int32 heap_type_dst;
  12452. bool src_nullable, dst_nullable;
  12453. CHECK_BUF(p, p_end, 1);
  12454. castflags = read_uint8(p);
  12455. #if WASM_ENABLE_FAST_INTERP != 0
  12456. /* Emit heap_type firstly */
  12457. emit_byte(loader_ctx, castflags);
  12458. #endif
  12459. p_org = p;
  12460. read_leb_uint32(p, p_end, depth);
  12461. read_leb_int32(p, p_end, heap_type);
  12462. #if WASM_ENABLE_FAST_INTERP != 0
  12463. /* Emit heap_type firstly */
  12464. emit_uint32(loader_ctx, (uint32)heap_type);
  12465. #endif
  12466. read_leb_int32(p, p_end, heap_type_dst);
  12467. #if WASM_ENABLE_FAST_INTERP != 0
  12468. /* Emit heap_type firstly */
  12469. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12470. #endif
  12471. (void)depth;
  12472. /*
  12473. * castflags should be 0~3:
  12474. * 0: (non-null, non-null)
  12475. * 1: (null, non-null)
  12476. * 2: (non-null, null)
  12477. * 3: (null, null)
  12478. */
  12479. if (castflags > 3) {
  12480. set_error_buf(error_buf, error_buf_size,
  12481. "invalid castflags");
  12482. break;
  12483. }
  12484. src_nullable =
  12485. (castflags == 1) || (castflags == 3) ? true : false;
  12486. dst_nullable =
  12487. (castflags == 2) || (castflags == 3) ? true : false;
  12488. /* Pop and backup the stack top's ref type */
  12489. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12490. &ref_type_tmp, error_buf,
  12491. error_buf_size)) {
  12492. goto fail;
  12493. }
  12494. /* The reference type rt1 must be valid */
  12495. if (!init_ref_type(module, &ref_type1, src_nullable,
  12496. heap_type, error_buf,
  12497. error_buf_size)) {
  12498. goto fail;
  12499. }
  12500. /* The reference type rt2 must be valid. */
  12501. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12502. heap_type_dst, error_buf,
  12503. error_buf_size)) {
  12504. goto fail;
  12505. }
  12506. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12507. &ref_type2);
  12508. /* The reference type rt2 must match rt1. */
  12509. if (!wasm_reftype_is_subtype_of(
  12510. ref_type2.ref_type, &ref_type2,
  12511. ref_type1.ref_type, &ref_type1, module->types,
  12512. module->type_count)) {
  12513. set_error_buf(error_buf, error_buf_size,
  12514. "type mismatch");
  12515. goto fail;
  12516. }
  12517. p = p_org;
  12518. /* Push ref type casted for branch block check */
  12519. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12520. /* The reference type rt2 must match rt′. */
  12521. type_tmp = ref_type2.ref_type;
  12522. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12523. bh_memcpy_s(
  12524. &wasm_ref_type,
  12525. wasm_reftype_struct_size(&ref_type2),
  12526. &ref_type2,
  12527. wasm_reftype_struct_size(&ref_type2));
  12528. }
  12529. }
  12530. else {
  12531. /* The reference type rt′1 must match rt′. */
  12532. type_tmp = ref_type_diff.ref_type;
  12533. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12534. bh_memcpy_s(
  12535. &wasm_ref_type,
  12536. wasm_reftype_struct_size(&ref_type_diff),
  12537. &ref_type_diff,
  12538. wasm_reftype_struct_size(&ref_type_diff));
  12539. }
  12540. }
  12541. PUSH_REF(type_tmp);
  12542. if (!(frame_csp_tmp = check_branch_block(
  12543. loader_ctx, &p, p_end, false, error_buf,
  12544. error_buf_size))) {
  12545. goto fail;
  12546. }
  12547. /* Ignore heap_types */
  12548. skip_leb_uint32(p, p_end);
  12549. skip_leb_uint32(p, p_end);
  12550. /* Restore the original stack top's ref type */
  12551. POP_REF(type_tmp);
  12552. #if WASM_ENABLE_FAST_INTERP != 0
  12553. /* Erase the opnd offset emitted by POP_REF() */
  12554. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12555. #endif
  12556. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12557. type_tmp = ref_type_diff.ref_type;
  12558. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12559. bh_memcpy_s(
  12560. &wasm_ref_type,
  12561. wasm_reftype_struct_size(&ref_type_diff),
  12562. &ref_type_diff,
  12563. wasm_reftype_struct_size(&ref_type_diff));
  12564. }
  12565. }
  12566. else {
  12567. type_tmp = ref_type2.ref_type;
  12568. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12569. bh_memcpy_s(
  12570. &wasm_ref_type,
  12571. wasm_reftype_struct_size(&ref_type2),
  12572. &ref_type2,
  12573. wasm_reftype_struct_size(&ref_type2));
  12574. }
  12575. }
  12576. PUSH_REF(type_tmp);
  12577. #if WASM_ENABLE_FAST_INTERP != 0
  12578. /* Erase the opnd offset emitted by PUSH_REF() */
  12579. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12580. #endif
  12581. break;
  12582. }
  12583. case WASM_OP_ANY_CONVERT_EXTERN:
  12584. {
  12585. uint8 type;
  12586. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12587. &wasm_ref_type, error_buf,
  12588. error_buf_size)) {
  12589. goto fail;
  12590. }
  12591. if (!(type == REF_TYPE_EXTERNREF
  12592. || (type == REF_TYPE_HT_NON_NULLABLE
  12593. && wasm_ref_type.ref_ht_common.heap_type
  12594. == HEAP_TYPE_EXTERN)
  12595. || type == VALUE_TYPE_ANY)) {
  12596. set_error_buf(error_buf, error_buf_size,
  12597. "type mismatch");
  12598. goto fail;
  12599. }
  12600. if (type == REF_TYPE_EXTERNREF)
  12601. type = REF_TYPE_ANYREF;
  12602. else {
  12603. wasm_ref_type.ref_ht_common.heap_type =
  12604. HEAP_TYPE_ANY;
  12605. }
  12606. PUSH_REF(type);
  12607. break;
  12608. }
  12609. case WASM_OP_EXTERN_CONVERT_ANY:
  12610. {
  12611. uint8 type;
  12612. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12613. &wasm_ref_type, error_buf,
  12614. error_buf_size)) {
  12615. goto fail;
  12616. }
  12617. if (type == REF_TYPE_EXTERNREF
  12618. || ((type == REF_TYPE_HT_NULLABLE
  12619. || type == REF_TYPE_HT_NON_NULLABLE)
  12620. && wasm_ref_type.ref_ht_common.heap_type
  12621. == HEAP_TYPE_EXTERN)) {
  12622. set_error_buf(error_buf, error_buf_size,
  12623. "type mismatch");
  12624. goto fail;
  12625. }
  12626. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12627. /* push (ref null extern) */
  12628. type = REF_TYPE_EXTERNREF;
  12629. }
  12630. else {
  12631. /* push (ref extern) */
  12632. type = REF_TYPE_HT_NON_NULLABLE;
  12633. wasm_set_refheaptype_common(
  12634. &wasm_ref_type.ref_ht_common, false,
  12635. HEAP_TYPE_EXTERN);
  12636. }
  12637. PUSH_REF(type);
  12638. break;
  12639. }
  12640. #if WASM_ENABLE_STRINGREF != 0
  12641. case WASM_OP_STRING_NEW_UTF8:
  12642. case WASM_OP_STRING_NEW_WTF16:
  12643. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12644. case WASM_OP_STRING_NEW_WTF8:
  12645. {
  12646. uint32 memidx;
  12647. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12648. func->has_memory_operations = true;
  12649. #endif
  12650. read_leb_uint32(p, p_end, memidx);
  12651. #if WASM_ENABLE_FAST_INTERP != 0
  12652. emit_uint32(loader_ctx, (uint32)memidx);
  12653. #endif
  12654. POP_I32();
  12655. POP_I32();
  12656. PUSH_REF(REF_TYPE_STRINGREF);
  12657. (void)memidx;
  12658. break;
  12659. }
  12660. case WASM_OP_STRING_CONST:
  12661. {
  12662. uint32 contents;
  12663. read_leb_uint32(p, p_end, contents);
  12664. #if WASM_ENABLE_FAST_INTERP != 0
  12665. emit_uint32(loader_ctx, (uint32)contents);
  12666. #endif
  12667. PUSH_REF(REF_TYPE_STRINGREF);
  12668. (void)contents;
  12669. break;
  12670. }
  12671. case WASM_OP_STRING_MEASURE_UTF8:
  12672. case WASM_OP_STRING_MEASURE_WTF8:
  12673. case WASM_OP_STRING_MEASURE_WTF16:
  12674. {
  12675. POP_STRINGREF();
  12676. PUSH_I32();
  12677. break;
  12678. }
  12679. case WASM_OP_STRING_ENCODE_UTF8:
  12680. case WASM_OP_STRING_ENCODE_WTF16:
  12681. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12682. case WASM_OP_STRING_ENCODE_WTF8:
  12683. {
  12684. uint32 memidx;
  12685. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12686. func->has_memory_operations = true;
  12687. #endif
  12688. read_leb_uint32(p, p_end, memidx);
  12689. #if WASM_ENABLE_FAST_INTERP != 0
  12690. emit_uint32(loader_ctx, (uint32)memidx);
  12691. #endif
  12692. POP_I32();
  12693. POP_STRINGREF();
  12694. PUSH_I32();
  12695. (void)memidx;
  12696. break;
  12697. }
  12698. case WASM_OP_STRING_CONCAT:
  12699. {
  12700. POP_STRINGREF();
  12701. POP_STRINGREF();
  12702. PUSH_REF(REF_TYPE_STRINGREF);
  12703. break;
  12704. }
  12705. case WASM_OP_STRING_EQ:
  12706. {
  12707. POP_STRINGREF();
  12708. POP_STRINGREF();
  12709. PUSH_I32();
  12710. break;
  12711. }
  12712. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12713. {
  12714. POP_STRINGREF();
  12715. PUSH_I32();
  12716. break;
  12717. }
  12718. case WASM_OP_STRING_AS_WTF8:
  12719. {
  12720. POP_STRINGREF();
  12721. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  12722. break;
  12723. }
  12724. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  12725. {
  12726. POP_I32();
  12727. POP_I32();
  12728. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12729. PUSH_I32();
  12730. break;
  12731. }
  12732. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  12733. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  12734. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  12735. {
  12736. uint32 memidx;
  12737. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12738. func->has_memory_operations = true;
  12739. #endif
  12740. read_leb_uint32(p, p_end, memidx);
  12741. #if WASM_ENABLE_FAST_INTERP != 0
  12742. emit_uint32(loader_ctx, (uint32)memidx);
  12743. #endif
  12744. POP_I32();
  12745. POP_I32();
  12746. POP_I32();
  12747. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12748. PUSH_I32();
  12749. PUSH_I32();
  12750. (void)memidx;
  12751. break;
  12752. }
  12753. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  12754. {
  12755. POP_I32();
  12756. POP_I32();
  12757. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12758. PUSH_REF(REF_TYPE_STRINGREF);
  12759. break;
  12760. }
  12761. case WASM_OP_STRING_AS_WTF16:
  12762. {
  12763. POP_STRINGREF();
  12764. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  12765. break;
  12766. }
  12767. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  12768. {
  12769. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12770. PUSH_I32();
  12771. break;
  12772. }
  12773. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  12774. {
  12775. POP_I32();
  12776. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12777. PUSH_I32();
  12778. break;
  12779. }
  12780. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  12781. {
  12782. uint32 memidx;
  12783. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12784. func->has_memory_operations = true;
  12785. #endif
  12786. read_leb_uint32(p, p_end, memidx);
  12787. #if WASM_ENABLE_FAST_INTERP != 0
  12788. emit_uint32(loader_ctx, (uint32)memidx);
  12789. #endif
  12790. POP_I32();
  12791. POP_I32();
  12792. POP_I32();
  12793. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12794. PUSH_I32();
  12795. (void)memidx;
  12796. break;
  12797. }
  12798. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  12799. {
  12800. POP_I32();
  12801. POP_I32();
  12802. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12803. PUSH_REF(REF_TYPE_STRINGREF);
  12804. break;
  12805. }
  12806. case WASM_OP_STRING_AS_ITER:
  12807. {
  12808. POP_STRINGREF();
  12809. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  12810. break;
  12811. }
  12812. case WASM_OP_STRINGVIEW_ITER_NEXT:
  12813. {
  12814. POP_REF(REF_TYPE_STRINGVIEWITER);
  12815. PUSH_I32();
  12816. break;
  12817. }
  12818. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  12819. case WASM_OP_STRINGVIEW_ITER_REWIND:
  12820. {
  12821. POP_I32();
  12822. POP_REF(REF_TYPE_STRINGVIEWITER);
  12823. PUSH_I32();
  12824. break;
  12825. }
  12826. case WASM_OP_STRINGVIEW_ITER_SLICE:
  12827. {
  12828. POP_I32();
  12829. POP_REF(REF_TYPE_STRINGVIEWITER);
  12830. PUSH_REF(REF_TYPE_STRINGREF);
  12831. break;
  12832. }
  12833. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  12834. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  12835. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  12836. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  12837. {
  12838. POP_I32();
  12839. POP_I32();
  12840. POP_REF(REF_TYPE_ARRAYREF);
  12841. PUSH_REF(REF_TYPE_STRINGREF);
  12842. break;
  12843. }
  12844. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  12845. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  12846. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  12847. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  12848. {
  12849. POP_I32();
  12850. POP_REF(REF_TYPE_ARRAYREF);
  12851. POP_STRINGREF();
  12852. PUSH_I32();
  12853. break;
  12854. }
  12855. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  12856. default:
  12857. set_error_buf_v(error_buf, error_buf_size,
  12858. "%s %02x %02x", "unsupported opcode",
  12859. 0xfb, opcode1);
  12860. goto fail;
  12861. }
  12862. break;
  12863. }
  12864. #endif /* end of WASM_ENABLE_GC != 0 */
  12865. case WASM_OP_MISC_PREFIX:
  12866. {
  12867. uint32 opcode1;
  12868. read_leb_uint32(p, p_end, opcode1);
  12869. #if WASM_ENABLE_FAST_INTERP != 0
  12870. emit_byte(loader_ctx, ((uint8)opcode1));
  12871. #endif
  12872. switch (opcode1) {
  12873. case WASM_OP_I32_TRUNC_SAT_S_F32:
  12874. case WASM_OP_I32_TRUNC_SAT_U_F32:
  12875. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12876. break;
  12877. case WASM_OP_I32_TRUNC_SAT_S_F64:
  12878. case WASM_OP_I32_TRUNC_SAT_U_F64:
  12879. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12880. break;
  12881. case WASM_OP_I64_TRUNC_SAT_S_F32:
  12882. case WASM_OP_I64_TRUNC_SAT_U_F32:
  12883. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12884. break;
  12885. case WASM_OP_I64_TRUNC_SAT_S_F64:
  12886. case WASM_OP_I64_TRUNC_SAT_U_F64:
  12887. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12888. break;
  12889. #if WASM_ENABLE_BULK_MEMORY != 0
  12890. case WASM_OP_MEMORY_INIT:
  12891. {
  12892. read_leb_uint32(p, p_end, data_seg_idx);
  12893. #if WASM_ENABLE_FAST_INTERP != 0
  12894. emit_uint32(loader_ctx, data_seg_idx);
  12895. #endif
  12896. if (module->import_memory_count == 0
  12897. && module->memory_count == 0)
  12898. goto fail_unknown_memory;
  12899. if (*p++ != 0x00)
  12900. goto fail_zero_byte_expected;
  12901. if (data_seg_idx >= module->data_seg_count) {
  12902. set_error_buf_v(error_buf, error_buf_size,
  12903. "unknown data segment %d",
  12904. data_seg_idx);
  12905. goto fail;
  12906. }
  12907. if (module->data_seg_count1 == 0)
  12908. goto fail_data_cnt_sec_require;
  12909. POP_I32();
  12910. POP_I32();
  12911. POP_I32();
  12912. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12913. func->has_memory_operations = true;
  12914. #endif
  12915. #if WASM_ENABLE_WAMR_COMPILER != 0
  12916. module->is_bulk_memory_used = true;
  12917. #endif
  12918. break;
  12919. }
  12920. case WASM_OP_DATA_DROP:
  12921. {
  12922. read_leb_uint32(p, p_end, data_seg_idx);
  12923. #if WASM_ENABLE_FAST_INTERP != 0
  12924. emit_uint32(loader_ctx, data_seg_idx);
  12925. #endif
  12926. if (data_seg_idx >= module->data_seg_count) {
  12927. set_error_buf(error_buf, error_buf_size,
  12928. "unknown data segment");
  12929. goto fail;
  12930. }
  12931. if (module->data_seg_count1 == 0)
  12932. goto fail_data_cnt_sec_require;
  12933. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12934. func->has_memory_operations = true;
  12935. #endif
  12936. #if WASM_ENABLE_WAMR_COMPILER != 0
  12937. module->is_bulk_memory_used = true;
  12938. #endif
  12939. break;
  12940. }
  12941. case WASM_OP_MEMORY_COPY:
  12942. {
  12943. /* both src and dst memory index should be 0 */
  12944. if (*(int16 *)p != 0x0000)
  12945. goto fail_zero_byte_expected;
  12946. p += 2;
  12947. if (module->import_memory_count == 0
  12948. && module->memory_count == 0)
  12949. goto fail_unknown_memory;
  12950. POP_I32();
  12951. POP_I32();
  12952. POP_I32();
  12953. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12954. func->has_memory_operations = true;
  12955. #endif
  12956. #if WASM_ENABLE_WAMR_COMPILER != 0
  12957. module->is_bulk_memory_used = true;
  12958. #endif
  12959. break;
  12960. }
  12961. case WASM_OP_MEMORY_FILL:
  12962. {
  12963. if (*p++ != 0x00) {
  12964. goto fail_zero_byte_expected;
  12965. }
  12966. if (module->import_memory_count == 0
  12967. && module->memory_count == 0) {
  12968. goto fail_unknown_memory;
  12969. }
  12970. POP_I32();
  12971. POP_I32();
  12972. POP_I32();
  12973. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12974. func->has_memory_operations = true;
  12975. #endif
  12976. #if WASM_ENABLE_WAMR_COMPILER != 0
  12977. module->is_bulk_memory_used = true;
  12978. #endif
  12979. break;
  12980. }
  12981. fail_zero_byte_expected:
  12982. set_error_buf(error_buf, error_buf_size,
  12983. "zero byte expected");
  12984. goto fail;
  12985. fail_unknown_memory:
  12986. set_error_buf(error_buf, error_buf_size,
  12987. "unknown memory 0");
  12988. goto fail;
  12989. fail_data_cnt_sec_require:
  12990. set_error_buf(error_buf, error_buf_size,
  12991. "data count section required");
  12992. goto fail;
  12993. #endif /* WASM_ENABLE_BULK_MEMORY */
  12994. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  12995. case WASM_OP_TABLE_INIT:
  12996. {
  12997. uint8 seg_type = 0, tbl_type = 0;
  12998. #if WASM_ENABLE_GC != 0
  12999. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13000. #endif
  13001. read_leb_uint32(p, p_end, table_seg_idx);
  13002. read_leb_uint32(p, p_end, table_idx);
  13003. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13004. #if WASM_ENABLE_GC != 0
  13005. (void **)&tbl_ref_type,
  13006. #else
  13007. NULL,
  13008. #endif
  13009. error_buf, error_buf_size))
  13010. goto fail;
  13011. if (!get_table_seg_elem_type(module, table_seg_idx,
  13012. &seg_type,
  13013. #if WASM_ENABLE_GC != 0
  13014. (void **)&seg_ref_type,
  13015. #else
  13016. NULL,
  13017. #endif
  13018. error_buf, error_buf_size))
  13019. goto fail;
  13020. #if WASM_ENABLE_GC == 0
  13021. if (seg_type != tbl_type) {
  13022. set_error_buf(error_buf, error_buf_size,
  13023. "type mismatch");
  13024. goto fail;
  13025. }
  13026. #else
  13027. if (!wasm_reftype_is_subtype_of(
  13028. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13029. module->types, module->type_count)) {
  13030. set_error_buf(error_buf, error_buf_size,
  13031. "type mismatch");
  13032. goto fail;
  13033. }
  13034. #endif
  13035. #if WASM_ENABLE_FAST_INTERP != 0
  13036. emit_uint32(loader_ctx, table_seg_idx);
  13037. emit_uint32(loader_ctx, table_idx);
  13038. #endif
  13039. POP_I32();
  13040. POP_I32();
  13041. POP_I32();
  13042. #if WASM_ENABLE_WAMR_COMPILER != 0
  13043. module->is_ref_types_used = true;
  13044. #endif
  13045. break;
  13046. }
  13047. case WASM_OP_ELEM_DROP:
  13048. {
  13049. read_leb_uint32(p, p_end, table_seg_idx);
  13050. if (!get_table_seg_elem_type(module, table_seg_idx,
  13051. NULL, NULL, error_buf,
  13052. error_buf_size))
  13053. goto fail;
  13054. #if WASM_ENABLE_FAST_INTERP != 0
  13055. emit_uint32(loader_ctx, table_seg_idx);
  13056. #endif
  13057. #if WASM_ENABLE_WAMR_COMPILER != 0
  13058. module->is_ref_types_used = true;
  13059. #endif
  13060. break;
  13061. }
  13062. case WASM_OP_TABLE_COPY:
  13063. {
  13064. uint8 src_type, dst_type;
  13065. #if WASM_ENABLE_GC != 0
  13066. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13067. #endif
  13068. uint32 src_tbl_idx, dst_tbl_idx;
  13069. read_leb_uint32(p, p_end, dst_tbl_idx);
  13070. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13071. #if WASM_ENABLE_GC != 0
  13072. (void **)&dst_ref_type,
  13073. #else
  13074. NULL,
  13075. #endif
  13076. error_buf, error_buf_size))
  13077. goto fail;
  13078. read_leb_uint32(p, p_end, src_tbl_idx);
  13079. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13080. #if WASM_ENABLE_GC != 0
  13081. (void **)&src_ref_type,
  13082. #else
  13083. NULL,
  13084. #endif
  13085. error_buf, error_buf_size))
  13086. goto fail;
  13087. #if WASM_ENABLE_GC == 0
  13088. if (src_type != dst_type) {
  13089. set_error_buf(error_buf, error_buf_size,
  13090. "type mismatch");
  13091. goto fail;
  13092. }
  13093. #else
  13094. if (!wasm_reftype_is_subtype_of(
  13095. src_type, src_ref_type, dst_type, dst_ref_type,
  13096. module->types, module->type_count)) {
  13097. set_error_buf(error_buf, error_buf_size,
  13098. "type mismatch");
  13099. goto fail;
  13100. }
  13101. #endif
  13102. #if WASM_ENABLE_FAST_INTERP != 0
  13103. emit_uint32(loader_ctx, dst_tbl_idx);
  13104. emit_uint32(loader_ctx, src_tbl_idx);
  13105. #endif
  13106. POP_I32();
  13107. POP_I32();
  13108. POP_I32();
  13109. #if WASM_ENABLE_WAMR_COMPILER != 0
  13110. module->is_ref_types_used = true;
  13111. #endif
  13112. break;
  13113. }
  13114. case WASM_OP_TABLE_SIZE:
  13115. {
  13116. read_leb_uint32(p, p_end, table_idx);
  13117. /* TODO: shall we create a new function to check
  13118. table idx instead of using below function? */
  13119. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13120. error_buf, error_buf_size))
  13121. goto fail;
  13122. #if WASM_ENABLE_FAST_INTERP != 0
  13123. emit_uint32(loader_ctx, table_idx);
  13124. #endif
  13125. PUSH_I32();
  13126. #if WASM_ENABLE_WAMR_COMPILER != 0
  13127. module->is_ref_types_used = true;
  13128. #endif
  13129. break;
  13130. }
  13131. case WASM_OP_TABLE_GROW:
  13132. case WASM_OP_TABLE_FILL:
  13133. {
  13134. uint8 decl_type;
  13135. #if WASM_ENABLE_GC != 0
  13136. WASMRefType *ref_type = NULL;
  13137. #endif
  13138. read_leb_uint32(p, p_end, table_idx);
  13139. if (!get_table_elem_type(module, table_idx, &decl_type,
  13140. #if WASM_ENABLE_GC != 0
  13141. (void **)&ref_type,
  13142. #else
  13143. NULL,
  13144. #endif
  13145. error_buf, error_buf_size))
  13146. goto fail;
  13147. #if WASM_ENABLE_GC != 0
  13148. if (wasm_is_type_multi_byte_type(decl_type)) {
  13149. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13150. ref_type,
  13151. wasm_reftype_struct_size(ref_type));
  13152. }
  13153. #endif
  13154. if (opcode1 == WASM_OP_TABLE_GROW) {
  13155. if (table_idx < module->import_table_count) {
  13156. module->import_tables[table_idx]
  13157. .u.table.possible_grow = true;
  13158. }
  13159. else {
  13160. module
  13161. ->tables[table_idx
  13162. - module->import_table_count]
  13163. .possible_grow = true;
  13164. }
  13165. }
  13166. #if WASM_ENABLE_FAST_INTERP != 0
  13167. emit_uint32(loader_ctx, table_idx);
  13168. #endif
  13169. POP_I32();
  13170. #if WASM_ENABLE_FAST_INTERP != 0
  13171. POP_OFFSET_TYPE(decl_type);
  13172. #endif
  13173. POP_TYPE(decl_type);
  13174. if (opcode1 == WASM_OP_TABLE_GROW)
  13175. PUSH_I32();
  13176. else
  13177. POP_I32();
  13178. #if WASM_ENABLE_WAMR_COMPILER != 0
  13179. module->is_ref_types_used = true;
  13180. #endif
  13181. break;
  13182. }
  13183. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13184. default:
  13185. set_error_buf_v(error_buf, error_buf_size,
  13186. "%s %02x %02x", "unsupported opcode",
  13187. 0xfc, opcode1);
  13188. goto fail;
  13189. }
  13190. break;
  13191. }
  13192. #if WASM_ENABLE_SIMD != 0
  13193. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13194. case WASM_OP_SIMD_PREFIX:
  13195. {
  13196. uint32 opcode1;
  13197. #if WASM_ENABLE_WAMR_COMPILER != 0
  13198. /* Mark the SIMD instruction is used in this module */
  13199. module->is_simd_used = true;
  13200. #endif
  13201. read_leb_uint32(p, p_end, opcode1);
  13202. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13203. */
  13204. switch (opcode1) {
  13205. /* memory instruction */
  13206. case SIMD_v128_load:
  13207. case SIMD_v128_load8x8_s:
  13208. case SIMD_v128_load8x8_u:
  13209. case SIMD_v128_load16x4_s:
  13210. case SIMD_v128_load16x4_u:
  13211. case SIMD_v128_load32x2_s:
  13212. case SIMD_v128_load32x2_u:
  13213. case SIMD_v128_load8_splat:
  13214. case SIMD_v128_load16_splat:
  13215. case SIMD_v128_load32_splat:
  13216. case SIMD_v128_load64_splat:
  13217. {
  13218. CHECK_MEMORY();
  13219. read_leb_uint32(p, p_end, align); /* align */
  13220. if (!check_simd_memory_access_align(
  13221. opcode1, align, error_buf, error_buf_size)) {
  13222. goto fail;
  13223. }
  13224. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13225. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13226. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13227. func->has_memory_operations = true;
  13228. #endif
  13229. break;
  13230. }
  13231. case SIMD_v128_store:
  13232. {
  13233. CHECK_MEMORY();
  13234. read_leb_uint32(p, p_end, align); /* align */
  13235. if (!check_simd_memory_access_align(
  13236. opcode1, align, error_buf, error_buf_size)) {
  13237. goto fail;
  13238. }
  13239. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13240. POP_V128();
  13241. POP_I32();
  13242. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13243. func->has_memory_operations = true;
  13244. #endif
  13245. break;
  13246. }
  13247. /* basic operation */
  13248. case SIMD_v128_const:
  13249. {
  13250. CHECK_BUF1(p, p_end, 16);
  13251. p += 16;
  13252. PUSH_V128();
  13253. break;
  13254. }
  13255. case SIMD_v8x16_shuffle:
  13256. {
  13257. V128 mask;
  13258. CHECK_BUF1(p, p_end, 16);
  13259. mask = read_i8x16(p, error_buf, error_buf_size);
  13260. p += 16;
  13261. if (!check_simd_shuffle_mask(mask, error_buf,
  13262. error_buf_size)) {
  13263. goto fail;
  13264. }
  13265. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13266. break;
  13267. }
  13268. case SIMD_v8x16_swizzle:
  13269. {
  13270. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13271. break;
  13272. }
  13273. /* splat operation */
  13274. case SIMD_i8x16_splat:
  13275. case SIMD_i16x8_splat:
  13276. case SIMD_i32x4_splat:
  13277. case SIMD_i64x2_splat:
  13278. case SIMD_f32x4_splat:
  13279. case SIMD_f64x2_splat:
  13280. {
  13281. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13282. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13283. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13284. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13285. VALUE_TYPE_V128);
  13286. break;
  13287. }
  13288. /* lane operation */
  13289. case SIMD_i8x16_extract_lane_s:
  13290. case SIMD_i8x16_extract_lane_u:
  13291. case SIMD_i8x16_replace_lane:
  13292. case SIMD_i16x8_extract_lane_s:
  13293. case SIMD_i16x8_extract_lane_u:
  13294. case SIMD_i16x8_replace_lane:
  13295. case SIMD_i32x4_extract_lane:
  13296. case SIMD_i32x4_replace_lane:
  13297. case SIMD_i64x2_extract_lane:
  13298. case SIMD_i64x2_replace_lane:
  13299. case SIMD_f32x4_extract_lane:
  13300. case SIMD_f32x4_replace_lane:
  13301. case SIMD_f64x2_extract_lane:
  13302. case SIMD_f64x2_replace_lane:
  13303. {
  13304. uint8 lane;
  13305. /* clang-format off */
  13306. uint8 replace[] = {
  13307. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13308. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13309. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13310. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13311. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13312. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13313. };
  13314. uint8 push_type[] = {
  13315. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13316. VALUE_TYPE_V128,
  13317. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13318. VALUE_TYPE_V128,
  13319. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13320. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13321. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13322. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13323. };
  13324. /* clang-format on */
  13325. CHECK_BUF(p, p_end, 1);
  13326. lane = read_uint8(p);
  13327. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13328. error_buf_size)) {
  13329. goto fail;
  13330. }
  13331. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13332. if (!(wasm_loader_pop_frame_ref(
  13333. loader_ctx,
  13334. replace[opcode1
  13335. - SIMD_i8x16_extract_lane_s],
  13336. error_buf, error_buf_size)))
  13337. goto fail;
  13338. }
  13339. POP_AND_PUSH(
  13340. VALUE_TYPE_V128,
  13341. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13342. break;
  13343. }
  13344. /* i8x16 compare operation */
  13345. case SIMD_i8x16_eq:
  13346. case SIMD_i8x16_ne:
  13347. case SIMD_i8x16_lt_s:
  13348. case SIMD_i8x16_lt_u:
  13349. case SIMD_i8x16_gt_s:
  13350. case SIMD_i8x16_gt_u:
  13351. case SIMD_i8x16_le_s:
  13352. case SIMD_i8x16_le_u:
  13353. case SIMD_i8x16_ge_s:
  13354. case SIMD_i8x16_ge_u:
  13355. /* i16x8 compare operation */
  13356. case SIMD_i16x8_eq:
  13357. case SIMD_i16x8_ne:
  13358. case SIMD_i16x8_lt_s:
  13359. case SIMD_i16x8_lt_u:
  13360. case SIMD_i16x8_gt_s:
  13361. case SIMD_i16x8_gt_u:
  13362. case SIMD_i16x8_le_s:
  13363. case SIMD_i16x8_le_u:
  13364. case SIMD_i16x8_ge_s:
  13365. case SIMD_i16x8_ge_u:
  13366. /* i32x4 compare operation */
  13367. case SIMD_i32x4_eq:
  13368. case SIMD_i32x4_ne:
  13369. case SIMD_i32x4_lt_s:
  13370. case SIMD_i32x4_lt_u:
  13371. case SIMD_i32x4_gt_s:
  13372. case SIMD_i32x4_gt_u:
  13373. case SIMD_i32x4_le_s:
  13374. case SIMD_i32x4_le_u:
  13375. case SIMD_i32x4_ge_s:
  13376. case SIMD_i32x4_ge_u:
  13377. /* f32x4 compare operation */
  13378. case SIMD_f32x4_eq:
  13379. case SIMD_f32x4_ne:
  13380. case SIMD_f32x4_lt:
  13381. case SIMD_f32x4_gt:
  13382. case SIMD_f32x4_le:
  13383. case SIMD_f32x4_ge:
  13384. /* f64x2 compare operation */
  13385. case SIMD_f64x2_eq:
  13386. case SIMD_f64x2_ne:
  13387. case SIMD_f64x2_lt:
  13388. case SIMD_f64x2_gt:
  13389. case SIMD_f64x2_le:
  13390. case SIMD_f64x2_ge:
  13391. {
  13392. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13393. break;
  13394. }
  13395. /* v128 operation */
  13396. case SIMD_v128_not:
  13397. {
  13398. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13399. break;
  13400. }
  13401. case SIMD_v128_and:
  13402. case SIMD_v128_andnot:
  13403. case SIMD_v128_or:
  13404. case SIMD_v128_xor:
  13405. {
  13406. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13407. break;
  13408. }
  13409. case SIMD_v128_bitselect:
  13410. {
  13411. POP_V128();
  13412. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13413. break;
  13414. }
  13415. case SIMD_v128_any_true:
  13416. {
  13417. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13418. break;
  13419. }
  13420. /* Load Lane Operation */
  13421. case SIMD_v128_load8_lane:
  13422. case SIMD_v128_load16_lane:
  13423. case SIMD_v128_load32_lane:
  13424. case SIMD_v128_load64_lane:
  13425. case SIMD_v128_store8_lane:
  13426. case SIMD_v128_store16_lane:
  13427. case SIMD_v128_store32_lane:
  13428. case SIMD_v128_store64_lane:
  13429. {
  13430. uint8 lane;
  13431. CHECK_MEMORY();
  13432. read_leb_uint32(p, p_end, align); /* align */
  13433. if (!check_simd_memory_access_align(
  13434. opcode1, align, error_buf, error_buf_size)) {
  13435. goto fail;
  13436. }
  13437. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13438. CHECK_BUF(p, p_end, 1);
  13439. lane = read_uint8(p);
  13440. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13441. error_buf_size)) {
  13442. goto fail;
  13443. }
  13444. POP_V128();
  13445. POP_I32();
  13446. if (opcode1 < SIMD_v128_store8_lane) {
  13447. PUSH_V128();
  13448. }
  13449. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13450. func->has_memory_operations = true;
  13451. #endif
  13452. break;
  13453. }
  13454. case SIMD_v128_load32_zero:
  13455. case SIMD_v128_load64_zero:
  13456. {
  13457. CHECK_MEMORY();
  13458. read_leb_uint32(p, p_end, align); /* align */
  13459. if (!check_simd_memory_access_align(
  13460. opcode1, align, error_buf, error_buf_size)) {
  13461. goto fail;
  13462. }
  13463. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13464. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13465. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13466. func->has_memory_operations = true;
  13467. #endif
  13468. break;
  13469. }
  13470. /* Float conversion */
  13471. case SIMD_f32x4_demote_f64x2_zero:
  13472. case SIMD_f64x2_promote_low_f32x4_zero:
  13473. {
  13474. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13475. break;
  13476. }
  13477. /* i8x16 Operation */
  13478. case SIMD_i8x16_abs:
  13479. case SIMD_i8x16_neg:
  13480. case SIMD_i8x16_popcnt:
  13481. {
  13482. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13483. break;
  13484. }
  13485. case SIMD_i8x16_all_true:
  13486. case SIMD_i8x16_bitmask:
  13487. {
  13488. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13489. break;
  13490. }
  13491. case SIMD_i8x16_narrow_i16x8_s:
  13492. case SIMD_i8x16_narrow_i16x8_u:
  13493. {
  13494. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13495. break;
  13496. }
  13497. case SIMD_f32x4_ceil:
  13498. case SIMD_f32x4_floor:
  13499. case SIMD_f32x4_trunc:
  13500. case SIMD_f32x4_nearest:
  13501. {
  13502. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13503. break;
  13504. }
  13505. case SIMD_i8x16_shl:
  13506. case SIMD_i8x16_shr_s:
  13507. case SIMD_i8x16_shr_u:
  13508. {
  13509. POP_I32();
  13510. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13511. break;
  13512. }
  13513. case SIMD_i8x16_add:
  13514. case SIMD_i8x16_add_sat_s:
  13515. case SIMD_i8x16_add_sat_u:
  13516. case SIMD_i8x16_sub:
  13517. case SIMD_i8x16_sub_sat_s:
  13518. case SIMD_i8x16_sub_sat_u:
  13519. {
  13520. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13521. break;
  13522. }
  13523. case SIMD_f64x2_ceil:
  13524. case SIMD_f64x2_floor:
  13525. {
  13526. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13527. break;
  13528. }
  13529. case SIMD_i8x16_min_s:
  13530. case SIMD_i8x16_min_u:
  13531. case SIMD_i8x16_max_s:
  13532. case SIMD_i8x16_max_u:
  13533. {
  13534. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13535. break;
  13536. }
  13537. case SIMD_f64x2_trunc:
  13538. {
  13539. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13540. break;
  13541. }
  13542. case SIMD_i8x16_avgr_u:
  13543. {
  13544. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13545. break;
  13546. }
  13547. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13548. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13549. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13550. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13551. /* i16x8 operation */
  13552. case SIMD_i16x8_abs:
  13553. case SIMD_i16x8_neg:
  13554. {
  13555. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13556. break;
  13557. }
  13558. case SIMD_i16x8_q15mulr_sat_s:
  13559. {
  13560. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13561. break;
  13562. }
  13563. case SIMD_i16x8_all_true:
  13564. case SIMD_i16x8_bitmask:
  13565. {
  13566. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13567. break;
  13568. }
  13569. case SIMD_i16x8_narrow_i32x4_s:
  13570. case SIMD_i16x8_narrow_i32x4_u:
  13571. {
  13572. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13573. break;
  13574. }
  13575. case SIMD_i16x8_extend_low_i8x16_s:
  13576. case SIMD_i16x8_extend_high_i8x16_s:
  13577. case SIMD_i16x8_extend_low_i8x16_u:
  13578. case SIMD_i16x8_extend_high_i8x16_u:
  13579. {
  13580. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13581. break;
  13582. }
  13583. case SIMD_i16x8_shl:
  13584. case SIMD_i16x8_shr_s:
  13585. case SIMD_i16x8_shr_u:
  13586. {
  13587. POP_I32();
  13588. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13589. break;
  13590. }
  13591. case SIMD_i16x8_add:
  13592. case SIMD_i16x8_add_sat_s:
  13593. case SIMD_i16x8_add_sat_u:
  13594. case SIMD_i16x8_sub:
  13595. case SIMD_i16x8_sub_sat_s:
  13596. case SIMD_i16x8_sub_sat_u:
  13597. {
  13598. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13599. break;
  13600. }
  13601. case SIMD_f64x2_nearest:
  13602. {
  13603. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13604. break;
  13605. }
  13606. case SIMD_i16x8_mul:
  13607. case SIMD_i16x8_min_s:
  13608. case SIMD_i16x8_min_u:
  13609. case SIMD_i16x8_max_s:
  13610. case SIMD_i16x8_max_u:
  13611. case SIMD_i16x8_avgr_u:
  13612. case SIMD_i16x8_extmul_low_i8x16_s:
  13613. case SIMD_i16x8_extmul_high_i8x16_s:
  13614. case SIMD_i16x8_extmul_low_i8x16_u:
  13615. case SIMD_i16x8_extmul_high_i8x16_u:
  13616. {
  13617. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13618. break;
  13619. }
  13620. /* i32x4 operation */
  13621. case SIMD_i32x4_abs:
  13622. case SIMD_i32x4_neg:
  13623. {
  13624. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13625. break;
  13626. }
  13627. case SIMD_i32x4_all_true:
  13628. case SIMD_i32x4_bitmask:
  13629. {
  13630. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13631. break;
  13632. }
  13633. case SIMD_i32x4_narrow_i64x2_s:
  13634. case SIMD_i32x4_narrow_i64x2_u:
  13635. {
  13636. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13637. break;
  13638. }
  13639. case SIMD_i32x4_extend_low_i16x8_s:
  13640. case SIMD_i32x4_extend_high_i16x8_s:
  13641. case SIMD_i32x4_extend_low_i16x8_u:
  13642. case SIMD_i32x4_extend_high_i16x8_u:
  13643. {
  13644. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13645. break;
  13646. }
  13647. case SIMD_i32x4_shl:
  13648. case SIMD_i32x4_shr_s:
  13649. case SIMD_i32x4_shr_u:
  13650. {
  13651. POP_I32();
  13652. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13653. break;
  13654. }
  13655. case SIMD_i32x4_add:
  13656. case SIMD_i32x4_sub:
  13657. case SIMD_i32x4_mul:
  13658. case SIMD_i32x4_min_s:
  13659. case SIMD_i32x4_min_u:
  13660. case SIMD_i32x4_max_s:
  13661. case SIMD_i32x4_max_u:
  13662. case SIMD_i32x4_dot_i16x8_s:
  13663. case SIMD_i32x4_avgr_u:
  13664. case SIMD_i32x4_extmul_low_i16x8_s:
  13665. case SIMD_i32x4_extmul_high_i16x8_s:
  13666. case SIMD_i32x4_extmul_low_i16x8_u:
  13667. case SIMD_i32x4_extmul_high_i16x8_u:
  13668. {
  13669. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13670. break;
  13671. }
  13672. /* i64x2 operation */
  13673. case SIMD_i64x2_abs:
  13674. case SIMD_i64x2_neg:
  13675. {
  13676. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13677. break;
  13678. }
  13679. case SIMD_i64x2_all_true:
  13680. case SIMD_i64x2_bitmask:
  13681. {
  13682. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13683. break;
  13684. }
  13685. case SIMD_i64x2_extend_low_i32x4_s:
  13686. case SIMD_i64x2_extend_high_i32x4_s:
  13687. case SIMD_i64x2_extend_low_i32x4_u:
  13688. case SIMD_i64x2_extend_high_i32x4_u:
  13689. {
  13690. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13691. break;
  13692. }
  13693. case SIMD_i64x2_shl:
  13694. case SIMD_i64x2_shr_s:
  13695. case SIMD_i64x2_shr_u:
  13696. {
  13697. POP_I32();
  13698. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13699. break;
  13700. }
  13701. case SIMD_i64x2_add:
  13702. case SIMD_i64x2_sub:
  13703. case SIMD_i64x2_mul:
  13704. case SIMD_i64x2_eq:
  13705. case SIMD_i64x2_ne:
  13706. case SIMD_i64x2_lt_s:
  13707. case SIMD_i64x2_gt_s:
  13708. case SIMD_i64x2_le_s:
  13709. case SIMD_i64x2_ge_s:
  13710. case SIMD_i64x2_extmul_low_i32x4_s:
  13711. case SIMD_i64x2_extmul_high_i32x4_s:
  13712. case SIMD_i64x2_extmul_low_i32x4_u:
  13713. case SIMD_i64x2_extmul_high_i32x4_u:
  13714. {
  13715. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13716. break;
  13717. }
  13718. /* f32x4 operation */
  13719. case SIMD_f32x4_abs:
  13720. case SIMD_f32x4_neg:
  13721. case SIMD_f32x4_round:
  13722. case SIMD_f32x4_sqrt:
  13723. {
  13724. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13725. break;
  13726. }
  13727. case SIMD_f32x4_add:
  13728. case SIMD_f32x4_sub:
  13729. case SIMD_f32x4_mul:
  13730. case SIMD_f32x4_div:
  13731. case SIMD_f32x4_min:
  13732. case SIMD_f32x4_max:
  13733. case SIMD_f32x4_pmin:
  13734. case SIMD_f32x4_pmax:
  13735. {
  13736. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13737. break;
  13738. }
  13739. /* f64x2 operation */
  13740. case SIMD_f64x2_abs:
  13741. case SIMD_f64x2_neg:
  13742. case SIMD_f64x2_round:
  13743. case SIMD_f64x2_sqrt:
  13744. {
  13745. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13746. break;
  13747. }
  13748. case SIMD_f64x2_add:
  13749. case SIMD_f64x2_sub:
  13750. case SIMD_f64x2_mul:
  13751. case SIMD_f64x2_div:
  13752. case SIMD_f64x2_min:
  13753. case SIMD_f64x2_max:
  13754. case SIMD_f64x2_pmin:
  13755. case SIMD_f64x2_pmax:
  13756. {
  13757. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13758. break;
  13759. }
  13760. case SIMD_i32x4_trunc_sat_f32x4_s:
  13761. case SIMD_i32x4_trunc_sat_f32x4_u:
  13762. case SIMD_f32x4_convert_i32x4_s:
  13763. case SIMD_f32x4_convert_i32x4_u:
  13764. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  13765. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  13766. case SIMD_f64x2_convert_low_i32x4_s:
  13767. case SIMD_f64x2_convert_low_i32x4_u:
  13768. {
  13769. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13770. break;
  13771. }
  13772. default:
  13773. {
  13774. if (error_buf != NULL) {
  13775. snprintf(error_buf, error_buf_size,
  13776. "WASM module load failed: "
  13777. "invalid opcode 0xfd %02x.",
  13778. opcode1);
  13779. }
  13780. goto fail;
  13781. }
  13782. }
  13783. break;
  13784. }
  13785. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  13786. #endif /* end of WASM_ENABLE_SIMD */
  13787. #if WASM_ENABLE_SHARED_MEMORY != 0
  13788. case WASM_OP_ATOMIC_PREFIX:
  13789. {
  13790. uint32 opcode1;
  13791. read_leb_uint32(p, p_end, opcode1);
  13792. #if WASM_ENABLE_FAST_INTERP != 0
  13793. emit_byte(loader_ctx, opcode1);
  13794. #endif
  13795. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  13796. CHECK_MEMORY();
  13797. read_leb_uint32(p, p_end, align); /* align */
  13798. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13799. if (!check_memory_align_equal(opcode1, align, error_buf,
  13800. error_buf_size)) {
  13801. goto fail;
  13802. }
  13803. #if WASM_ENABLE_FAST_INTERP != 0
  13804. emit_uint32(loader_ctx, mem_offset);
  13805. #endif
  13806. }
  13807. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13808. func->has_memory_operations = true;
  13809. #endif
  13810. switch (opcode1) {
  13811. case WASM_OP_ATOMIC_NOTIFY:
  13812. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13813. break;
  13814. case WASM_OP_ATOMIC_WAIT32:
  13815. POP_I64();
  13816. POP_I32();
  13817. POP_I32();
  13818. PUSH_I32();
  13819. break;
  13820. case WASM_OP_ATOMIC_WAIT64:
  13821. POP_I64();
  13822. POP_I64();
  13823. POP_I32();
  13824. PUSH_I32();
  13825. break;
  13826. case WASM_OP_ATOMIC_FENCE:
  13827. /* reserved byte 0x00 */
  13828. if (*p++ != 0x00) {
  13829. set_error_buf(error_buf, error_buf_size,
  13830. "zero byte expected");
  13831. goto fail;
  13832. }
  13833. break;
  13834. case WASM_OP_ATOMIC_I32_LOAD:
  13835. case WASM_OP_ATOMIC_I32_LOAD8_U:
  13836. case WASM_OP_ATOMIC_I32_LOAD16_U:
  13837. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13838. break;
  13839. case WASM_OP_ATOMIC_I32_STORE:
  13840. case WASM_OP_ATOMIC_I32_STORE8:
  13841. case WASM_OP_ATOMIC_I32_STORE16:
  13842. POP_I32();
  13843. POP_I32();
  13844. break;
  13845. case WASM_OP_ATOMIC_I64_LOAD:
  13846. case WASM_OP_ATOMIC_I64_LOAD8_U:
  13847. case WASM_OP_ATOMIC_I64_LOAD16_U:
  13848. case WASM_OP_ATOMIC_I64_LOAD32_U:
  13849. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  13850. break;
  13851. case WASM_OP_ATOMIC_I64_STORE:
  13852. case WASM_OP_ATOMIC_I64_STORE8:
  13853. case WASM_OP_ATOMIC_I64_STORE16:
  13854. case WASM_OP_ATOMIC_I64_STORE32:
  13855. POP_I64();
  13856. POP_I32();
  13857. break;
  13858. case WASM_OP_ATOMIC_RMW_I32_ADD:
  13859. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  13860. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  13861. case WASM_OP_ATOMIC_RMW_I32_SUB:
  13862. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  13863. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  13864. case WASM_OP_ATOMIC_RMW_I32_AND:
  13865. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  13866. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  13867. case WASM_OP_ATOMIC_RMW_I32_OR:
  13868. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  13869. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  13870. case WASM_OP_ATOMIC_RMW_I32_XOR:
  13871. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  13872. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  13873. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  13874. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  13875. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  13876. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13877. break;
  13878. case WASM_OP_ATOMIC_RMW_I64_ADD:
  13879. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  13880. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  13881. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  13882. case WASM_OP_ATOMIC_RMW_I64_SUB:
  13883. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  13884. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  13885. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  13886. case WASM_OP_ATOMIC_RMW_I64_AND:
  13887. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  13888. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  13889. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  13890. case WASM_OP_ATOMIC_RMW_I64_OR:
  13891. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  13892. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  13893. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  13894. case WASM_OP_ATOMIC_RMW_I64_XOR:
  13895. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  13896. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  13897. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  13898. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  13899. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  13900. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  13901. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  13902. POP_I64();
  13903. POP_I32();
  13904. PUSH_I64();
  13905. break;
  13906. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  13907. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  13908. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  13909. POP_I32();
  13910. POP_I32();
  13911. POP_I32();
  13912. PUSH_I32();
  13913. break;
  13914. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  13915. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  13916. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  13917. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  13918. POP_I64();
  13919. POP_I64();
  13920. POP_I32();
  13921. PUSH_I64();
  13922. break;
  13923. default:
  13924. set_error_buf_v(error_buf, error_buf_size,
  13925. "%s %02x %02x", "unsupported opcode",
  13926. 0xfe, opcode1);
  13927. goto fail;
  13928. }
  13929. break;
  13930. }
  13931. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  13932. default:
  13933. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  13934. "unsupported opcode", opcode);
  13935. goto fail;
  13936. }
  13937. #if WASM_ENABLE_FAST_INTERP != 0
  13938. last_op = opcode;
  13939. #endif
  13940. }
  13941. if (loader_ctx->csp_num > 0) {
  13942. if (cur_func_idx < module->function_count - 1)
  13943. /* Function with missing end marker (between two functions) */
  13944. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  13945. else
  13946. /* Function with missing end marker
  13947. (at EOF or end of code sections) */
  13948. set_error_buf(error_buf, error_buf_size,
  13949. "unexpected end of section or function, "
  13950. "or section size mismatch");
  13951. goto fail;
  13952. }
  13953. #if WASM_ENABLE_FAST_INTERP != 0
  13954. if (loader_ctx->p_code_compiled == NULL)
  13955. goto re_scan;
  13956. func->const_cell_num = loader_ctx->const_cell_num;
  13957. if (func->const_cell_num > 0) {
  13958. int32 j;
  13959. if (!(func->consts = func_const = loader_malloc(
  13960. func->const_cell_num * 4, error_buf, error_buf_size)))
  13961. goto fail;
  13962. func_const_end = func->consts + func->const_cell_num * 4;
  13963. /* reverse the const buf */
  13964. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  13965. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  13966. if (c->value_type == VALUE_TYPE_F64
  13967. || c->value_type == VALUE_TYPE_I64) {
  13968. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  13969. &(c->value.f64), (uint32)sizeof(int64));
  13970. func_const += sizeof(int64);
  13971. }
  13972. else {
  13973. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  13974. &(c->value.f32), (uint32)sizeof(int32));
  13975. func_const += sizeof(int32);
  13976. }
  13977. }
  13978. }
  13979. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  13980. - loader_ctx->start_dynamic_offset + 1;
  13981. #else
  13982. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  13983. #endif
  13984. func->max_block_num = loader_ctx->max_csp_num;
  13985. return_value = true;
  13986. fail:
  13987. wasm_loader_ctx_destroy(loader_ctx);
  13988. (void)table_idx;
  13989. (void)table_seg_idx;
  13990. (void)data_seg_idx;
  13991. (void)i64_const;
  13992. (void)local_offset;
  13993. (void)p_org;
  13994. (void)mem_offset;
  13995. (void)align;
  13996. return return_value;
  13997. }