wasm_loader.c 568 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517
  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. #if WASM_ENABLE_MEMORY64 != 0
  44. static void
  45. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  46. {
  47. if (error_buf != NULL) {
  48. snprintf(error_buf, error_buf_size, "offset out of range");
  49. }
  50. }
  51. #endif
  52. static void
  53. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  54. {
  55. va_list args;
  56. char buf[128];
  57. if (error_buf != NULL) {
  58. va_start(args, format);
  59. vsnprintf(buf, sizeof(buf), format, args);
  60. va_end(args);
  61. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  62. }
  63. }
  64. static bool
  65. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  66. char *error_buf, uint32 error_buf_size)
  67. {
  68. if ((uintptr_t)buf + length < (uintptr_t)buf
  69. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  70. set_error_buf(error_buf, error_buf_size,
  71. "unexpected end of section or function");
  72. return false;
  73. }
  74. return true;
  75. }
  76. static bool
  77. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  78. char *error_buf, uint32 error_buf_size)
  79. {
  80. if ((uintptr_t)buf + length < (uintptr_t)buf
  81. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  82. set_error_buf(error_buf, error_buf_size, "unexpected end");
  83. return false;
  84. }
  85. return true;
  86. }
  87. #define CHECK_BUF(buf, buf_end, length) \
  88. do { \
  89. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  90. goto fail; \
  91. } \
  92. } while (0)
  93. #define CHECK_BUF1(buf, buf_end, length) \
  94. do { \
  95. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  96. goto fail; \
  97. } \
  98. } while (0)
  99. #define skip_leb(p) while (*p++ & 0x80)
  100. #define skip_leb_int64(p, p_end) skip_leb(p)
  101. #define skip_leb_uint32(p, p_end) skip_leb(p)
  102. #define skip_leb_int32(p, p_end) skip_leb(p)
  103. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  104. static bool
  105. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  106. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  107. {
  108. const uint8 *buf = *p_buf;
  109. uint64 result = 0;
  110. uint32 shift = 0;
  111. uint32 offset = 0, bcnt = 0;
  112. uint64 byte;
  113. while (true) {
  114. /* uN or SN must not exceed ceil(N/7) bytes */
  115. if (bcnt + 1 > (maxbits + 6) / 7) {
  116. set_error_buf(error_buf, error_buf_size,
  117. "integer representation too long");
  118. return false;
  119. }
  120. CHECK_BUF(buf, buf_end, offset + 1);
  121. byte = buf[offset];
  122. offset += 1;
  123. result |= ((byte & 0x7f) << shift);
  124. shift += 7;
  125. bcnt += 1;
  126. if ((byte & 0x80) == 0) {
  127. break;
  128. }
  129. }
  130. if (!sign && maxbits == 32 && shift >= maxbits) {
  131. /* The top bits set represent values > 32 bits */
  132. if (((uint8)byte) & 0xf0)
  133. goto fail_integer_too_large;
  134. }
  135. else if (sign && maxbits == 32) {
  136. if (shift < maxbits) {
  137. /* Sign extend, second-highest bit is the sign bit */
  138. if ((uint8)byte & 0x40)
  139. result |= (~((uint64)0)) << shift;
  140. }
  141. else {
  142. /* The top bits should be a sign-extension of the sign bit */
  143. bool sign_bit_set = ((uint8)byte) & 0x8;
  144. int top_bits = ((uint8)byte) & 0xf0;
  145. if ((sign_bit_set && top_bits != 0x70)
  146. || (!sign_bit_set && top_bits != 0))
  147. goto fail_integer_too_large;
  148. }
  149. }
  150. else if (sign && maxbits == 64) {
  151. if (shift < maxbits) {
  152. /* Sign extend, second-highest bit is the sign bit */
  153. if ((uint8)byte & 0x40)
  154. result |= (~((uint64)0)) << shift;
  155. }
  156. else {
  157. /* The top bits should be a sign-extension of the sign bit */
  158. bool sign_bit_set = ((uint8)byte) & 0x1;
  159. int top_bits = ((uint8)byte) & 0xfe;
  160. if ((sign_bit_set && top_bits != 0x7e)
  161. || (!sign_bit_set && top_bits != 0))
  162. goto fail_integer_too_large;
  163. }
  164. }
  165. *p_buf += offset;
  166. *p_result = result;
  167. return true;
  168. fail_integer_too_large:
  169. set_error_buf(error_buf, error_buf_size, "integer too large");
  170. fail:
  171. return false;
  172. }
  173. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  174. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  175. #define read_leb_int64(p, p_end, res) \
  176. do { \
  177. uint64 res64; \
  178. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  179. error_buf_size)) \
  180. goto fail; \
  181. res = (int64)res64; \
  182. } while (0)
  183. #if WASM_ENABLE_MEMORY64 != 0
  184. #define read_leb_mem_offset(p, p_end, res) \
  185. do { \
  186. uint64 res64; \
  187. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  188. &res64, error_buf, error_buf_size)) { \
  189. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  190. goto fail; \
  191. } \
  192. res = (mem_offset_t)res64; \
  193. } while (0)
  194. #else
  195. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  196. #endif
  197. #define read_leb_uint32(p, p_end, res) \
  198. do { \
  199. uint64 res64; \
  200. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  201. error_buf_size)) \
  202. goto fail; \
  203. res = (uint32)res64; \
  204. } while (0)
  205. #define read_leb_int32(p, p_end, res) \
  206. do { \
  207. uint64 res64; \
  208. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  209. error_buf_size)) \
  210. goto fail; \
  211. res = (int32)res64; \
  212. } while (0)
  213. static char *
  214. type2str(uint8 type)
  215. {
  216. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  217. #if WASM_ENABLE_GC != 0
  218. char *type_str_ref[] = { "stringview_iter",
  219. "stringview_wtf16",
  220. "(ref null ht)",
  221. "(ref ht)",
  222. "", /* reserved */
  223. "stringview_wtf8",
  224. "stringref",
  225. "", /* reserved */
  226. "", /* reserved */
  227. "arrayref",
  228. "structref",
  229. "i32ref",
  230. "eqref",
  231. "anyref",
  232. "externref",
  233. "funcref",
  234. "nullref",
  235. "nullexternref",
  236. "nullfuncref" };
  237. #endif
  238. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  239. return type_str[type - VALUE_TYPE_V128];
  240. #if WASM_ENABLE_GC != 0
  241. else if (wasm_is_type_reftype(type))
  242. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  243. #endif
  244. else if (type == VALUE_TYPE_FUNCREF)
  245. return "funcref";
  246. else if (type == VALUE_TYPE_EXTERNREF)
  247. return "externref";
  248. else
  249. return "unknown type";
  250. }
  251. static bool
  252. is_32bit_type(uint8 type)
  253. {
  254. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  255. #if WASM_ENABLE_GC != 0
  256. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  257. #elif WASM_ENABLE_REF_TYPES != 0
  258. /* For reference types, we use uint32 index to represent
  259. the funcref and externref */
  260. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  261. #endif
  262. )
  263. return true;
  264. return false;
  265. }
  266. static bool
  267. is_64bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  270. #if WASM_ENABLE_GC != 0
  271. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  272. #endif
  273. )
  274. return true;
  275. return false;
  276. }
  277. static bool
  278. is_value_type(uint8 type)
  279. {
  280. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  281. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  282. #if WASM_ENABLE_GC != 0
  283. /* reference types, 0x65 to 0x70 */
  284. || wasm_is_type_reftype(type)
  285. #elif WASM_ENABLE_REF_TYPES != 0
  286. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  287. #endif
  288. #if WASM_ENABLE_SIMD != 0
  289. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  290. || type == VALUE_TYPE_V128 /* 0x7B */
  291. #endif
  292. #endif
  293. )
  294. return true;
  295. return false;
  296. }
  297. #if WASM_ENABLE_GC != 0
  298. static bool
  299. is_packed_type(uint8 type)
  300. {
  301. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  302. }
  303. #endif
  304. static bool
  305. is_byte_a_type(uint8 type)
  306. {
  307. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  308. }
  309. #if WASM_ENABLE_SIMD != 0
  310. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  311. static V128
  312. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  313. {
  314. V128 result;
  315. uint8 i;
  316. for (i = 0; i != 16; ++i) {
  317. result.i8x16[i] = read_uint8(p_buf);
  318. }
  319. return result;
  320. }
  321. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  322. #endif /* end of WASM_ENABLE_SIMD */
  323. static void *
  324. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  325. {
  326. void *mem;
  327. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  328. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  329. return NULL;
  330. }
  331. memset(mem, 0, (uint32)size);
  332. return mem;
  333. }
  334. static void *
  335. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  336. uint32 error_buf_size)
  337. {
  338. uint8 *mem_new;
  339. bh_assert(size_new > size_old);
  340. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  341. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  342. memset(mem_new + size_old, 0, size_new - size_old);
  343. wasm_runtime_free(mem_old);
  344. }
  345. return mem_new;
  346. }
  347. #define MEM_REALLOC(mem, size_old, size_new) \
  348. do { \
  349. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  350. error_buf_size); \
  351. if (!mem_new) \
  352. goto fail; \
  353. mem = mem_new; \
  354. } while (0)
  355. #if WASM_ENABLE_GC != 0
  356. static bool
  357. check_type_index(const WASMModule *module, uint32 type_index, char *error_buf,
  358. uint32 error_buf_size)
  359. {
  360. if (type_index >= module->type_count) {
  361. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  362. type_index);
  363. return false;
  364. }
  365. return true;
  366. }
  367. static bool
  368. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  369. uint32 error_buf_size)
  370. {
  371. if (!check_type_index(module, type_index, error_buf, error_buf_size)) {
  372. return false;
  373. }
  374. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  375. set_error_buf(error_buf, error_buf_size, "unkown array type");
  376. return false;
  377. }
  378. return true;
  379. }
  380. #endif
  381. static bool
  382. check_function_index(const WASMModule *module, uint32 function_index,
  383. char *error_buf, uint32 error_buf_size)
  384. {
  385. if (function_index
  386. >= module->import_function_count + module->function_count) {
  387. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  388. function_index);
  389. return false;
  390. }
  391. return true;
  392. }
  393. typedef struct InitValue {
  394. uint8 type;
  395. uint8 flag;
  396. #if WASM_ENABLE_GC != 0
  397. uint8 gc_opcode;
  398. WASMRefType ref_type;
  399. #endif
  400. WASMValue value;
  401. } InitValue;
  402. typedef struct ConstExprContext {
  403. uint32 sp;
  404. uint32 size;
  405. WASMModule *module;
  406. InitValue *stack;
  407. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  408. } ConstExprContext;
  409. static void
  410. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  411. {
  412. ctx->sp = 0;
  413. ctx->module = module;
  414. ctx->stack = ctx->data;
  415. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  416. }
  417. static bool
  418. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  419. #if WASM_ENABLE_GC != 0
  420. WASMRefType *ref_type, uint8 gc_opcode,
  421. #endif
  422. WASMValue *value, char *error_buf, uint32 error_buf_size)
  423. {
  424. InitValue *cur_value;
  425. if (ctx->sp >= ctx->size) {
  426. if (ctx->stack != ctx->data) {
  427. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  428. (ctx->size + 4) * sizeof(InitValue));
  429. }
  430. else {
  431. if (!(ctx->stack =
  432. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  433. error_buf, error_buf_size))) {
  434. goto fail;
  435. }
  436. }
  437. ctx->size += 4;
  438. }
  439. cur_value = &ctx->stack[ctx->sp++];
  440. cur_value->type = type;
  441. cur_value->flag = flag;
  442. cur_value->value = *value;
  443. #if WASM_ENABLE_GC != 0
  444. cur_value->gc_opcode = gc_opcode;
  445. if (wasm_is_type_multi_byte_type(type)) {
  446. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  447. ref_type, wasm_reftype_struct_size(ref_type));
  448. }
  449. #endif
  450. return true;
  451. fail:
  452. return false;
  453. }
  454. static bool
  455. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  456. #if WASM_ENABLE_GC != 0
  457. WASMRefType *ref_type, uint8 *p_gc_opcode,
  458. #endif
  459. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  460. {
  461. InitValue *cur_value;
  462. if (ctx->sp == 0) {
  463. set_error_buf(error_buf, error_buf_size,
  464. "type mismatch: const expr stack underflow");
  465. return false;
  466. }
  467. cur_value = &ctx->stack[--ctx->sp];
  468. #if WASM_ENABLE_GC == 0
  469. if (cur_value->type != type) {
  470. set_error_buf(error_buf, error_buf_size, "type mismatch");
  471. return false;
  472. }
  473. #else
  474. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  475. ref_type, ctx->module->types,
  476. ctx->module->type_count)) {
  477. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  478. "type mismatch: expect ", type2str(type),
  479. " but got other");
  480. goto fail;
  481. }
  482. if ((ctx->sp != 0) && (cur_value->flag == WASM_OP_GC_PREFIX)
  483. && (cur_value->gc_opcode != WASM_OP_REF_I31)) {
  484. /* To reduce complexity, we don't allow initialize struct fields/array
  485. * element with references, so struct/array must be at the bottom of the
  486. * init value stack */
  487. set_error_buf(
  488. error_buf, error_buf_size,
  489. "struct or array as field is not supported in constant expr");
  490. goto fail;
  491. }
  492. #endif
  493. if (p_flag)
  494. *p_flag = cur_value->flag;
  495. if (p_value)
  496. *p_value = cur_value->value;
  497. #if WASM_ENABLE_GC != 0
  498. if (p_gc_opcode)
  499. *p_gc_opcode = cur_value->gc_opcode;
  500. #endif
  501. return true;
  502. #if WASM_ENABLE_GC != 0
  503. fail:
  504. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  505. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  506. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  507. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  508. wasm_runtime_free(cur_value->value.data);
  509. }
  510. return false;
  511. #endif
  512. }
  513. static void
  514. destroy_const_expr_stack(ConstExprContext *ctx)
  515. {
  516. #if WASM_ENABLE_GC != 0
  517. uint32 i;
  518. for (i = 0; i < ctx->sp; i++) {
  519. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  520. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  521. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  522. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  523. wasm_runtime_free(ctx->stack[i].value.data);
  524. }
  525. }
  526. #endif
  527. if (ctx->stack != ctx->data) {
  528. wasm_runtime_free(ctx->stack);
  529. }
  530. }
  531. #if WASM_ENABLE_GC != 0
  532. static void
  533. destroy_init_expr(InitializerExpression *expr)
  534. {
  535. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  536. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  537. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  538. wasm_runtime_free(expr->u.data);
  539. }
  540. }
  541. #endif /* end of WASM_ENABLE_GC != 0 */
  542. static bool
  543. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  544. InitializerExpression *init_expr, uint8 type, void *ref_type,
  545. char *error_buf, uint32 error_buf_size)
  546. {
  547. const uint8 *p = *p_buf, *p_end = buf_end;
  548. uint8 flag, *p_float;
  549. uint32 i;
  550. ConstExprContext const_expr_ctx = { 0 };
  551. WASMValue cur_value;
  552. #if WASM_ENABLE_GC != 0
  553. uint32 opcode1, type_idx;
  554. uint8 opcode;
  555. WASMRefType cur_ref_type = { 0 };
  556. #endif
  557. init_const_expr_stack(&const_expr_ctx, module);
  558. CHECK_BUF(p, p_end, 1);
  559. flag = read_uint8(p);
  560. while (flag != WASM_OP_END) {
  561. switch (flag) {
  562. /* i32.const */
  563. case INIT_EXPR_TYPE_I32_CONST:
  564. read_leb_int32(p, p_end, cur_value.i32);
  565. if (!push_const_expr_stack(
  566. &const_expr_ctx, flag, VALUE_TYPE_I32,
  567. #if WASM_ENABLE_GC != 0
  568. NULL, 0,
  569. #endif
  570. &cur_value, error_buf, error_buf_size))
  571. goto fail;
  572. break;
  573. /* i64.const */
  574. case INIT_EXPR_TYPE_I64_CONST:
  575. read_leb_int64(p, p_end, cur_value.i64);
  576. if (!push_const_expr_stack(
  577. &const_expr_ctx, flag, VALUE_TYPE_I64,
  578. #if WASM_ENABLE_GC != 0
  579. NULL, 0,
  580. #endif
  581. &cur_value, error_buf, error_buf_size))
  582. goto fail;
  583. break;
  584. /* f32.const */
  585. case INIT_EXPR_TYPE_F32_CONST:
  586. CHECK_BUF(p, p_end, 4);
  587. p_float = (uint8 *)&cur_value.f32;
  588. for (i = 0; i < sizeof(float32); i++)
  589. *p_float++ = *p++;
  590. if (!push_const_expr_stack(
  591. &const_expr_ctx, flag, VALUE_TYPE_F32,
  592. #if WASM_ENABLE_GC != 0
  593. NULL, 0,
  594. #endif
  595. &cur_value, error_buf, error_buf_size))
  596. goto fail;
  597. break;
  598. /* f64.const */
  599. case INIT_EXPR_TYPE_F64_CONST:
  600. CHECK_BUF(p, p_end, 8);
  601. p_float = (uint8 *)&cur_value.f64;
  602. for (i = 0; i < sizeof(float64); i++)
  603. *p_float++ = *p++;
  604. if (!push_const_expr_stack(
  605. &const_expr_ctx, flag, VALUE_TYPE_F64,
  606. #if WASM_ENABLE_GC != 0
  607. NULL, 0,
  608. #endif
  609. &cur_value, error_buf, error_buf_size))
  610. goto fail;
  611. break;
  612. #if WASM_ENABLE_SIMD != 0
  613. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  614. /* v128.const */
  615. case INIT_EXPR_TYPE_V128_CONST:
  616. {
  617. uint64 high, low;
  618. CHECK_BUF(p, p_end, 1);
  619. (void)read_uint8(p);
  620. CHECK_BUF(p, p_end, 16);
  621. wasm_runtime_read_v128(p, &high, &low);
  622. p += 16;
  623. cur_value.v128.i64x2[0] = high;
  624. cur_value.v128.i64x2[1] = low;
  625. if (!push_const_expr_stack(
  626. &const_expr_ctx, flag, VALUE_TYPE_V128,
  627. #if WASM_ENABLE_GC != 0
  628. NULL, 0,
  629. #endif
  630. &cur_value, error_buf, error_buf_size))
  631. goto fail;
  632. #if WASM_ENABLE_WAMR_COMPILER != 0
  633. /* If any init_expr is v128.const, mark SIMD used */
  634. module->is_simd_used = true;
  635. #endif
  636. break;
  637. }
  638. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  639. #endif /* end of WASM_ENABLE_SIMD */
  640. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  641. /* ref.func */
  642. case INIT_EXPR_TYPE_FUNCREF_CONST:
  643. {
  644. uint32 func_idx;
  645. read_leb_uint32(p, p_end, func_idx);
  646. cur_value.ref_index = func_idx;
  647. if (!check_function_index(module, func_idx, error_buf,
  648. error_buf_size)) {
  649. goto fail;
  650. }
  651. #if WASM_ENABLE_GC == 0
  652. if (!push_const_expr_stack(&const_expr_ctx, flag,
  653. VALUE_TYPE_FUNCREF, &cur_value,
  654. error_buf, error_buf_size))
  655. goto fail;
  656. #else
  657. if (func_idx < module->import_function_count) {
  658. type_idx =
  659. module->import_functions[func_idx].u.function.type_idx;
  660. }
  661. else {
  662. type_idx = module
  663. ->functions[func_idx
  664. - module->import_function_count]
  665. ->type_idx;
  666. }
  667. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  668. false, type_idx);
  669. if (!push_const_expr_stack(&const_expr_ctx, flag,
  670. cur_ref_type.ref_type, &cur_ref_type,
  671. 0, &cur_value, error_buf,
  672. error_buf_size))
  673. goto fail;
  674. #endif
  675. #if WASM_ENABLE_WAMR_COMPILER != 0
  676. module->is_ref_types_used = true;
  677. #endif
  678. break;
  679. }
  680. /* ref.null */
  681. case INIT_EXPR_TYPE_REFNULL_CONST:
  682. {
  683. uint8 type1;
  684. CHECK_BUF(p, p_end, 1);
  685. type1 = read_uint8(p);
  686. #if WASM_ENABLE_GC == 0
  687. cur_value.ref_index = NULL_REF;
  688. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  689. &cur_value, error_buf,
  690. error_buf_size))
  691. goto fail;
  692. #else
  693. cur_value.gc_obj = NULL_REF;
  694. if (!is_byte_a_type(type1)) {
  695. p--;
  696. read_leb_uint32(p, p_end, type_idx);
  697. if (!check_type_index(module, type_idx, error_buf,
  698. error_buf_size))
  699. goto fail;
  700. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  701. true, type_idx);
  702. if (!push_const_expr_stack(&const_expr_ctx, flag,
  703. cur_ref_type.ref_type,
  704. &cur_ref_type, 0, &cur_value,
  705. error_buf, error_buf_size))
  706. goto fail;
  707. }
  708. else {
  709. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  710. NULL, 0, &cur_value, error_buf,
  711. error_buf_size))
  712. goto fail;
  713. }
  714. #endif
  715. #if WASM_ENABLE_WAMR_COMPILER != 0
  716. module->is_ref_types_used = true;
  717. #endif
  718. break;
  719. }
  720. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  721. /* get_global */
  722. case INIT_EXPR_TYPE_GET_GLOBAL:
  723. {
  724. uint32 global_idx;
  725. uint8 global_type;
  726. read_leb_uint32(p, p_end, cur_value.global_index);
  727. global_idx = cur_value.global_index;
  728. #if WASM_ENABLE_GC == 0
  729. if (global_idx >= module->import_global_count) {
  730. /**
  731. * Currently, constant expressions occurring as initializers
  732. * of globals are further constrained in that contained
  733. * global.get instructions are
  734. * only allowed to refer to imported globals.
  735. */
  736. set_error_buf_v(error_buf, error_buf_size,
  737. "unknown global %u", global_idx);
  738. goto fail;
  739. }
  740. if (module->import_globals[global_idx].u.global.is_mutable) {
  741. set_error_buf_v(error_buf, error_buf_size,
  742. "constant expression required");
  743. goto fail;
  744. }
  745. #else
  746. if (global_idx
  747. >= module->import_global_count + module->global_count) {
  748. set_error_buf_v(error_buf, error_buf_size,
  749. "unknown global %u", global_idx);
  750. goto fail;
  751. }
  752. if (global_idx < module->import_global_count
  753. && module->import_globals[global_idx].u.global.is_mutable) {
  754. set_error_buf_v(error_buf, error_buf_size,
  755. "constant expression required");
  756. goto fail;
  757. }
  758. #endif
  759. if (global_idx < module->import_global_count) {
  760. global_type =
  761. module->import_globals[global_idx].u.global.type;
  762. #if WASM_ENABLE_GC != 0
  763. if (wasm_is_type_multi_byte_type(global_type)) {
  764. WASMRefType *global_ref_type =
  765. module->import_globals[global_idx]
  766. .u.global.ref_type;
  767. bh_memcpy_s(&cur_ref_type,
  768. wasm_reftype_struct_size(global_ref_type),
  769. global_ref_type,
  770. wasm_reftype_struct_size(global_ref_type));
  771. }
  772. #endif
  773. }
  774. else {
  775. global_type =
  776. module
  777. ->globals[global_idx - module->import_global_count]
  778. .type;
  779. #if WASM_ENABLE_GC != 0
  780. if (wasm_is_type_multi_byte_type(global_type)) {
  781. WASMRefType *global_ref_type =
  782. module
  783. ->globals[global_idx
  784. - module->import_global_count]
  785. .ref_type;
  786. bh_memcpy_s(&cur_ref_type,
  787. wasm_reftype_struct_size(global_ref_type),
  788. global_ref_type,
  789. wasm_reftype_struct_size(global_ref_type));
  790. }
  791. #endif
  792. }
  793. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  794. #if WASM_ENABLE_GC != 0
  795. &cur_ref_type, 0,
  796. #endif
  797. &cur_value, error_buf,
  798. error_buf_size))
  799. goto fail;
  800. break;
  801. }
  802. #if WASM_ENABLE_GC != 0
  803. /* struct.new and array.new */
  804. case WASM_OP_GC_PREFIX:
  805. {
  806. read_leb_uint32(p, p_end, opcode1);
  807. switch (opcode1) {
  808. case WASM_OP_STRUCT_NEW:
  809. {
  810. WASMStructType *struct_type;
  811. WASMStructNewInitValues *struct_init_values = NULL;
  812. uint32 field_count;
  813. read_leb_uint32(p, p_end, type_idx);
  814. if (!check_type_index(module, type_idx, error_buf,
  815. error_buf_size)) {
  816. goto fail;
  817. }
  818. struct_type = (WASMStructType *)module->types[type_idx];
  819. if (struct_type->base_type.type_flag
  820. != WASM_TYPE_STRUCT) {
  821. set_error_buf(error_buf, error_buf_size,
  822. "unkown struct type");
  823. goto fail;
  824. }
  825. field_count = struct_type->field_count;
  826. if (!(struct_init_values = loader_malloc(
  827. offsetof(WASMStructNewInitValues, fields)
  828. + (uint64)field_count * sizeof(WASMValue),
  829. error_buf, error_buf_size))) {
  830. goto fail;
  831. }
  832. struct_init_values->count = field_count;
  833. for (i = field_count; i > 0; i--) {
  834. WASMRefType *field_ref_type = NULL;
  835. uint32 field_idx = i - 1;
  836. uint8 field_type =
  837. struct_type->fields[field_idx].field_type;
  838. if (wasm_is_type_multi_byte_type(field_type)) {
  839. field_ref_type = wasm_reftype_map_find(
  840. struct_type->ref_type_maps,
  841. struct_type->ref_type_map_count, field_idx);
  842. }
  843. if (is_packed_type(field_type)) {
  844. field_type = VALUE_TYPE_I32;
  845. }
  846. if (!pop_const_expr_stack(
  847. &const_expr_ctx, NULL, field_type,
  848. field_ref_type, NULL,
  849. &struct_init_values->fields[field_idx],
  850. error_buf, error_buf_size)) {
  851. wasm_runtime_free(struct_init_values);
  852. goto fail;
  853. }
  854. }
  855. cur_value.data = struct_init_values;
  856. wasm_set_refheaptype_typeidx(
  857. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  858. if (!push_const_expr_stack(
  859. &const_expr_ctx, flag, cur_ref_type.ref_type,
  860. &cur_ref_type, (uint8)opcode1, &cur_value,
  861. error_buf, error_buf_size)) {
  862. wasm_runtime_free(struct_init_values);
  863. goto fail;
  864. }
  865. break;
  866. }
  867. case WASM_OP_STRUCT_NEW_DEFAULT:
  868. {
  869. read_leb_uint32(p, p_end, cur_value.type_index);
  870. type_idx = cur_value.type_index;
  871. if (!check_type_index(module, type_idx, error_buf,
  872. error_buf_size)) {
  873. goto fail;
  874. }
  875. if (module->types[type_idx]->type_flag
  876. != WASM_TYPE_STRUCT) {
  877. set_error_buf(error_buf, error_buf_size,
  878. "unkown struct type");
  879. goto fail;
  880. }
  881. cur_value.type_index = type_idx;
  882. wasm_set_refheaptype_typeidx(
  883. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  884. if (!push_const_expr_stack(
  885. &const_expr_ctx, flag, cur_ref_type.ref_type,
  886. &cur_ref_type, (uint8)opcode1, &cur_value,
  887. error_buf, error_buf_size)) {
  888. goto fail;
  889. }
  890. break;
  891. }
  892. case WASM_OP_ARRAY_NEW:
  893. case WASM_OP_ARRAY_NEW_DEFAULT:
  894. case WASM_OP_ARRAY_NEW_FIXED:
  895. {
  896. WASMArrayNewInitValues *array_init_values = NULL;
  897. WASMArrayType *array_type = NULL;
  898. WASMRefType *elem_ref_type = NULL;
  899. uint64 total_size;
  900. uint8 elem_type;
  901. read_leb_uint32(p, p_end, cur_value.type_index);
  902. type_idx = cur_value.type_index;
  903. if (!check_type_index(module, type_idx, error_buf,
  904. error_buf_size)) {
  905. goto fail;
  906. }
  907. array_type = (WASMArrayType *)module->types[type_idx];
  908. if (array_type->base_type.type_flag
  909. != WASM_TYPE_ARRAY) {
  910. set_error_buf(error_buf, error_buf_size,
  911. "unkown array type");
  912. goto fail;
  913. }
  914. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  915. elem_type = array_type->elem_type;
  916. if (wasm_is_type_multi_byte_type(elem_type)) {
  917. elem_ref_type = array_type->elem_ref_type;
  918. }
  919. if (is_packed_type(elem_type)) {
  920. elem_type = VALUE_TYPE_I32;
  921. }
  922. if (opcode1 == WASM_OP_ARRAY_NEW) {
  923. WASMValue len_val;
  924. if (!(array_init_values = loader_malloc(
  925. sizeof(WASMArrayNewInitValues),
  926. error_buf, error_buf_size))) {
  927. goto fail;
  928. }
  929. array_init_values->type_idx = type_idx;
  930. if (!pop_const_expr_stack(
  931. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  932. NULL, NULL, &len_val, error_buf,
  933. error_buf_size)) {
  934. wasm_runtime_free(array_init_values);
  935. goto fail;
  936. }
  937. array_init_values->length = len_val.i32;
  938. if (!pop_const_expr_stack(
  939. &const_expr_ctx, NULL, elem_type,
  940. elem_ref_type, NULL,
  941. &array_init_values->elem_data[0],
  942. error_buf, error_buf_size)) {
  943. wasm_runtime_free(array_init_values);
  944. goto fail;
  945. }
  946. cur_value.data = array_init_values;
  947. }
  948. else {
  949. /* WASM_OP_ARRAY_NEW_FIXED */
  950. uint32 len;
  951. read_leb_uint32(p, p_end, len);
  952. total_size =
  953. (uint64)offsetof(WASMArrayNewInitValues,
  954. elem_data)
  955. + (uint64)sizeof(WASMValue) * len;
  956. if (!(array_init_values =
  957. loader_malloc(total_size, error_buf,
  958. error_buf_size))) {
  959. goto fail;
  960. }
  961. array_init_values->type_idx = type_idx;
  962. array_init_values->length = len;
  963. for (i = len; i > 0; i--) {
  964. if (!pop_const_expr_stack(
  965. &const_expr_ctx, NULL, elem_type,
  966. elem_ref_type, NULL,
  967. &array_init_values
  968. ->elem_data[i - 1],
  969. error_buf, error_buf_size)) {
  970. wasm_runtime_free(array_init_values);
  971. goto fail;
  972. }
  973. }
  974. cur_value.data = array_init_values;
  975. }
  976. }
  977. else {
  978. /* WASM_OP_ARRAY_NEW_DEFAULT */
  979. WASMValue len_val;
  980. uint32 len;
  981. /* POP(i32) */
  982. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  983. VALUE_TYPE_I32, NULL,
  984. NULL, &len_val, error_buf,
  985. error_buf_size)) {
  986. goto fail;
  987. }
  988. len = len_val.i32;
  989. cur_value.array_new_default.type_index = type_idx;
  990. cur_value.array_new_default.length = len;
  991. }
  992. wasm_set_refheaptype_typeidx(
  993. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  994. if (!push_const_expr_stack(
  995. &const_expr_ctx, flag, cur_ref_type.ref_type,
  996. &cur_ref_type, (uint8)opcode1, &cur_value,
  997. error_buf, error_buf_size)) {
  998. if (array_init_values) {
  999. wasm_runtime_free(array_init_values);
  1000. }
  1001. goto fail;
  1002. }
  1003. break;
  1004. }
  1005. case WASM_OP_ANY_CONVERT_EXTERN:
  1006. {
  1007. set_error_buf(error_buf, error_buf_size,
  1008. "unsuppoted constant expression of "
  1009. "extern.internalize");
  1010. goto fail;
  1011. }
  1012. case WASM_OP_EXTERN_CONVERT_ANY:
  1013. {
  1014. set_error_buf(error_buf, error_buf_size,
  1015. "unsuppoted constant expression of "
  1016. "extern.externalize");
  1017. goto fail;
  1018. }
  1019. case WASM_OP_REF_I31:
  1020. {
  1021. /* POP(i32) */
  1022. if (!pop_const_expr_stack(
  1023. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1024. NULL, &cur_value, error_buf, error_buf_size)) {
  1025. goto fail;
  1026. }
  1027. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1028. false, HEAP_TYPE_I31);
  1029. if (!push_const_expr_stack(
  1030. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1031. &cur_ref_type, (uint8)opcode1, &cur_value,
  1032. error_buf, error_buf_size)) {
  1033. goto fail;
  1034. }
  1035. break;
  1036. }
  1037. default:
  1038. set_error_buf(
  1039. error_buf, error_buf_size,
  1040. "type mismatch or constant expression required");
  1041. goto fail;
  1042. }
  1043. break;
  1044. }
  1045. #endif /* end of WASM_ENABLE_GC != 0 */
  1046. default:
  1047. {
  1048. set_error_buf(error_buf, error_buf_size,
  1049. "illegal opcode "
  1050. "or constant expression required "
  1051. "or type mismatch");
  1052. goto fail;
  1053. }
  1054. }
  1055. CHECK_BUF(p, p_end, 1);
  1056. flag = read_uint8(p);
  1057. }
  1058. /* There should be only one value left on the init value stack */
  1059. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1060. #if WASM_ENABLE_GC != 0
  1061. ref_type, &opcode,
  1062. #endif
  1063. &cur_value, error_buf, error_buf_size)) {
  1064. goto fail;
  1065. }
  1066. if (const_expr_ctx.sp != 0) {
  1067. set_error_buf(error_buf, error_buf_size,
  1068. "type mismatch: illegal constant opcode sequence");
  1069. goto fail;
  1070. }
  1071. init_expr->init_expr_type = flag;
  1072. init_expr->u = cur_value;
  1073. #if WASM_ENABLE_GC != 0
  1074. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1075. switch (opcode) {
  1076. case WASM_OP_STRUCT_NEW:
  1077. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1078. break;
  1079. case WASM_OP_STRUCT_NEW_DEFAULT:
  1080. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1081. break;
  1082. case WASM_OP_ARRAY_NEW:
  1083. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1084. break;
  1085. case WASM_OP_ARRAY_NEW_DEFAULT:
  1086. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1087. break;
  1088. case WASM_OP_ARRAY_NEW_FIXED:
  1089. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1090. break;
  1091. case WASM_OP_REF_I31:
  1092. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1093. break;
  1094. default:
  1095. bh_assert(0);
  1096. break;
  1097. }
  1098. }
  1099. #endif /* end of WASM_ENABLE_GC != 0 */
  1100. *p_buf = p;
  1101. destroy_const_expr_stack(&const_expr_ctx);
  1102. return true;
  1103. fail:
  1104. destroy_const_expr_stack(&const_expr_ctx);
  1105. return false;
  1106. }
  1107. static bool
  1108. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1109. {
  1110. if (mutable >= 2) {
  1111. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1112. return false;
  1113. }
  1114. return true;
  1115. }
  1116. #if WASM_ENABLE_GC != 0
  1117. static void
  1118. destroy_func_type(WASMFuncType *type)
  1119. {
  1120. /* Destroy the reference type hash set */
  1121. if (type->ref_type_maps)
  1122. wasm_runtime_free(type->ref_type_maps);
  1123. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1124. && WASM_ENABLE_LAZY_JIT != 0
  1125. if (type->call_to_llvm_jit_from_fast_jit)
  1126. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1127. #endif
  1128. /* Free the type */
  1129. wasm_runtime_free(type);
  1130. }
  1131. static void
  1132. destroy_struct_type(WASMStructType *type)
  1133. {
  1134. if (type->ref_type_maps)
  1135. wasm_runtime_free(type->ref_type_maps);
  1136. wasm_runtime_free(type);
  1137. }
  1138. static void
  1139. destroy_array_type(WASMArrayType *type)
  1140. {
  1141. wasm_runtime_free(type);
  1142. }
  1143. static void
  1144. destroy_wasm_type(WASMType *type)
  1145. {
  1146. if (type->type_flag == WASM_TYPE_FUNC)
  1147. destroy_func_type((WASMFuncType *)type);
  1148. else if (type->type_flag == WASM_TYPE_STRUCT)
  1149. destroy_struct_type((WASMStructType *)type);
  1150. else if (type->type_flag == WASM_TYPE_ARRAY)
  1151. destroy_array_type((WASMArrayType *)type);
  1152. else {
  1153. bh_assert(0);
  1154. }
  1155. }
  1156. /* Resolve (ref null ht) or (ref ht) */
  1157. static bool
  1158. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1159. WASMModule *module, bool nullable, WASMRefType *ref_type,
  1160. char *error_buf, uint32 error_buf_size)
  1161. {
  1162. const uint8 *p = *p_buf, *p_end = buf_end;
  1163. ref_type->ref_type =
  1164. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1165. ref_type->ref_ht_common.nullable = nullable;
  1166. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1167. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1168. /* heap type is (type i), i : typeidx, >= 0 */
  1169. if (!check_type_index(module, ref_type->ref_ht_typeidx.type_idx,
  1170. error_buf, error_buf_size)) {
  1171. return false;
  1172. }
  1173. }
  1174. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1175. /* heap type is func, extern, any, eq, i31 or data */
  1176. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1177. return false;
  1178. }
  1179. *p_buf = p;
  1180. return true;
  1181. fail:
  1182. return false;
  1183. }
  1184. static bool
  1185. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1186. WASMModule *module, bool *p_need_ref_type_map,
  1187. WASMRefType *ref_type, bool allow_packed_type,
  1188. char *error_buf, uint32 error_buf_size)
  1189. {
  1190. const uint8 *p = *p_buf, *p_end = buf_end;
  1191. uint8 type;
  1192. memset(ref_type, 0, sizeof(WASMRefType));
  1193. CHECK_BUF(p, p_end, 1);
  1194. type = read_uint8(p);
  1195. if (wasm_is_reftype_htref_nullable(type)) {
  1196. /* (ref null ht) */
  1197. if (!resolve_reftype_htref(&p, p_end, module, true, ref_type, error_buf,
  1198. error_buf_size))
  1199. return false;
  1200. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1201. *p_need_ref_type_map = true;
  1202. else {
  1203. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1204. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1205. multi-byte type to one-byte type to reduce the footprint and
  1206. the complexity of type equal/subtype checking */
  1207. ref_type->ref_type =
  1208. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1209. *p_need_ref_type_map = false;
  1210. }
  1211. }
  1212. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1213. /* (ref ht) */
  1214. if (!resolve_reftype_htref(&p, p_end, module, false, ref_type,
  1215. error_buf, error_buf_size))
  1216. return false;
  1217. *p_need_ref_type_map = true;
  1218. #if WASM_ENABLE_STRINGREF != 0
  1219. /* covert (ref string) to stringref */
  1220. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1221. ref_type->ref_type =
  1222. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1223. *p_need_ref_type_map = false;
  1224. }
  1225. #endif
  1226. }
  1227. else {
  1228. /* type which can be represented by one byte */
  1229. if (!is_value_type(type)
  1230. && !(allow_packed_type && is_packed_type(type))) {
  1231. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1232. return false;
  1233. }
  1234. ref_type->ref_type = type;
  1235. *p_need_ref_type_map = false;
  1236. #if WASM_ENABLE_WAMR_COMPILER != 0
  1237. /* If any value's type is v128, mark the module as SIMD used */
  1238. if (type == VALUE_TYPE_V128)
  1239. module->is_simd_used = true;
  1240. #endif
  1241. }
  1242. *p_buf = p;
  1243. return true;
  1244. fail:
  1245. return false;
  1246. }
  1247. static WASMRefType *
  1248. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1249. char *error_buf, uint32 error_buf_size)
  1250. {
  1251. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1252. if (!ret) {
  1253. set_error_buf(error_buf, error_buf_size,
  1254. "insert ref type to hash set failed");
  1255. }
  1256. return ret;
  1257. }
  1258. static bool
  1259. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1260. uint32 type_idx, char *error_buf, uint32 error_buf_size)
  1261. {
  1262. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1263. uint32 param_count, result_count, i, j = 0;
  1264. uint32 param_cell_num, ret_cell_num;
  1265. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1266. uint64 total_size;
  1267. bool need_ref_type_map;
  1268. WASMRefType ref_type;
  1269. WASMFuncType *type = NULL;
  1270. /* Parse first time to resolve param count, result count and
  1271. ref type map count */
  1272. read_leb_uint32(p, p_end, param_count);
  1273. p_org = p;
  1274. for (i = 0; i < param_count; i++) {
  1275. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1276. &ref_type, false, error_buf, error_buf_size)) {
  1277. return false;
  1278. }
  1279. if (need_ref_type_map)
  1280. ref_type_map_count++;
  1281. }
  1282. read_leb_uint32(p, p_end, result_count);
  1283. for (i = 0; i < result_count; i++) {
  1284. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1285. &ref_type, false, error_buf, error_buf_size)) {
  1286. return false;
  1287. }
  1288. if (need_ref_type_map) {
  1289. ref_type_map_count++;
  1290. result_ref_type_map_count++;
  1291. }
  1292. }
  1293. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1294. "ref type map count: %d",
  1295. type_idx, param_count, result_count, ref_type_map_count);
  1296. /* Parse second time to resolve param types, result types and
  1297. ref type map info */
  1298. p = p_org;
  1299. total_size = offsetof(WASMFuncType, types)
  1300. + sizeof(uint8) * (uint64)(param_count + result_count);
  1301. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1302. return false;
  1303. }
  1304. if (ref_type_map_count > 0) {
  1305. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1306. if (!(type->ref_type_maps =
  1307. loader_malloc(total_size, error_buf, error_buf_size))) {
  1308. goto fail;
  1309. }
  1310. }
  1311. type->base_type.type_flag = WASM_TYPE_FUNC;
  1312. type->param_count = param_count;
  1313. type->result_count = result_count;
  1314. type->ref_type_map_count = ref_type_map_count;
  1315. if (ref_type_map_count > 0) {
  1316. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1317. - result_ref_type_map_count;
  1318. }
  1319. for (i = 0; i < param_count; i++) {
  1320. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1321. &ref_type, false, error_buf, error_buf_size)) {
  1322. goto fail;
  1323. }
  1324. type->types[i] = ref_type.ref_type;
  1325. if (need_ref_type_map) {
  1326. type->ref_type_maps[j].index = i;
  1327. if (!(type->ref_type_maps[j++].ref_type =
  1328. reftype_set_insert(module->ref_type_set, &ref_type,
  1329. error_buf, error_buf_size))) {
  1330. goto fail;
  1331. }
  1332. }
  1333. }
  1334. read_leb_uint32(p, p_end, result_count);
  1335. for (i = 0; i < result_count; i++) {
  1336. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1337. &ref_type, false, error_buf, error_buf_size)) {
  1338. goto fail;
  1339. }
  1340. type->types[param_count + i] = ref_type.ref_type;
  1341. if (need_ref_type_map) {
  1342. type->ref_type_maps[j].index = param_count + i;
  1343. if (!(type->ref_type_maps[j++].ref_type =
  1344. reftype_set_insert(module->ref_type_set, &ref_type,
  1345. error_buf, error_buf_size))) {
  1346. goto fail;
  1347. }
  1348. }
  1349. }
  1350. bh_assert(j == type->ref_type_map_count);
  1351. #if TRACE_WASM_LOADER != 0
  1352. os_printf("type %d = ", type_idx);
  1353. wasm_dump_func_type(type);
  1354. #endif
  1355. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1356. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1357. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1358. set_error_buf(error_buf, error_buf_size,
  1359. "param count or result count too large");
  1360. goto fail;
  1361. }
  1362. type->param_cell_num = (uint16)param_cell_num;
  1363. type->ret_cell_num = (uint16)ret_cell_num;
  1364. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1365. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1366. #endif
  1367. #if WASM_ENABLE_WAMR_COMPILER != 0
  1368. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1369. if (type->types[i] == VALUE_TYPE_V128)
  1370. module->is_simd_used = true;
  1371. }
  1372. #endif
  1373. /* Calculate the minimal type index of the type equal to this type */
  1374. type->min_type_idx_normalized = type_idx;
  1375. for (i = 0; i < type_idx; i++) {
  1376. WASMFuncType *func_type = (WASMFuncType *)module->types[i];
  1377. if (func_type->base_type.type_flag == WASM_TYPE_FUNC
  1378. && wasm_func_type_equal(type, func_type, module->types,
  1379. type_idx + 1)) {
  1380. type->min_type_idx_normalized = i;
  1381. break;
  1382. }
  1383. }
  1384. *p_buf = p;
  1385. module->types[type_idx] = (WASMType *)type;
  1386. return true;
  1387. fail:
  1388. if (type)
  1389. destroy_func_type(type);
  1390. return false;
  1391. }
  1392. static bool
  1393. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1394. WASMModule *module, uint32 type_idx, char *error_buf,
  1395. uint32 error_buf_size)
  1396. {
  1397. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1398. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1399. uint32 i, j = 0, offset;
  1400. uint16 *reference_table;
  1401. uint64 total_size;
  1402. uint8 mutable;
  1403. bool need_ref_type_map;
  1404. WASMRefType ref_type;
  1405. WASMStructType *type = NULL;
  1406. /* Parse first time to resolve field count and ref type map count */
  1407. read_leb_uint32(p, p_end, field_count);
  1408. p_org = p;
  1409. for (i = 0; i < field_count; i++) {
  1410. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1411. &ref_type, true, error_buf, error_buf_size)) {
  1412. return false;
  1413. }
  1414. if (need_ref_type_map)
  1415. ref_type_map_count++;
  1416. if (wasm_is_type_reftype(ref_type.ref_type))
  1417. ref_field_count++;
  1418. CHECK_BUF(p, p_end, 1);
  1419. mutable = read_uint8(p);
  1420. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1421. return false;
  1422. }
  1423. }
  1424. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1425. type_idx, field_count, ref_type_map_count);
  1426. /* Parse second time to resolve field types and ref type map info */
  1427. p = p_org;
  1428. total_size = offsetof(WASMStructType, fields)
  1429. + sizeof(WASMStructFieldType) * (uint64)field_count
  1430. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1431. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1432. return false;
  1433. }
  1434. if (ref_type_map_count > 0) {
  1435. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1436. if (!(type->ref_type_maps =
  1437. loader_malloc(total_size, error_buf, error_buf_size))) {
  1438. goto fail;
  1439. }
  1440. }
  1441. type->reference_table = reference_table =
  1442. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1443. + sizeof(WASMStructFieldType) * field_count);
  1444. *reference_table++ = ref_field_count;
  1445. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1446. type->field_count = field_count;
  1447. type->ref_type_map_count = ref_type_map_count;
  1448. offset = (uint32)offsetof(WASMStructObject, field_data);
  1449. for (i = 0; i < field_count; i++) {
  1450. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1451. &ref_type, true, error_buf, error_buf_size)) {
  1452. goto fail;
  1453. }
  1454. type->fields[i].field_type = ref_type.ref_type;
  1455. if (need_ref_type_map) {
  1456. type->ref_type_maps[j].index = i;
  1457. if (!(type->ref_type_maps[j++].ref_type =
  1458. reftype_set_insert(module->ref_type_set, &ref_type,
  1459. error_buf, error_buf_size))) {
  1460. goto fail;
  1461. }
  1462. }
  1463. CHECK_BUF(p, p_end, 1);
  1464. type->fields[i].field_flags = read_uint8(p);
  1465. type->fields[i].field_size =
  1466. (uint8)wasm_reftype_size(ref_type.ref_type);
  1467. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1468. || defined(BUILD_TARGET_X86_32))
  1469. if (type->fields[i].field_size == 2)
  1470. offset = align_uint(offset, 2);
  1471. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1472. offset = align_uint(offset, 4);
  1473. #endif
  1474. type->fields[i].field_offset = offset;
  1475. if (wasm_is_type_reftype(ref_type.ref_type))
  1476. *reference_table++ = offset;
  1477. offset += type->fields[i].field_size;
  1478. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1479. type->fields[i].field_flags, type->fields[i].field_type);
  1480. }
  1481. type->total_size = offset;
  1482. bh_assert(j == type->ref_type_map_count);
  1483. #if TRACE_WASM_LOADER != 0
  1484. os_printf("type %d = ", type_idx);
  1485. wasm_dump_struct_type(type);
  1486. #endif
  1487. *p_buf = p;
  1488. module->types[type_idx] = (WASMType *)type;
  1489. return true;
  1490. fail:
  1491. if (type)
  1492. destroy_struct_type(type);
  1493. return false;
  1494. }
  1495. static bool
  1496. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1497. WASMModule *module, uint32 type_idx, char *error_buf,
  1498. uint32 error_buf_size)
  1499. {
  1500. const uint8 *p = *p_buf, *p_end = buf_end;
  1501. uint8 mutable;
  1502. bool need_ref_type_map;
  1503. WASMRefType ref_type;
  1504. WASMArrayType *type = NULL;
  1505. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  1506. true, error_buf, error_buf_size)) {
  1507. return false;
  1508. }
  1509. CHECK_BUF(p, p_end, 1);
  1510. mutable = read_uint8(p);
  1511. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1512. return false;
  1513. }
  1514. LOG_VERBOSE("type %u: array", type_idx);
  1515. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1516. error_buf_size))) {
  1517. return false;
  1518. }
  1519. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1520. type->elem_flags = mutable;
  1521. type->elem_type = ref_type.ref_type;
  1522. if (need_ref_type_map) {
  1523. if (!(type->elem_ref_type =
  1524. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1525. error_buf_size))) {
  1526. goto fail;
  1527. }
  1528. }
  1529. #if TRACE_WASM_LOADER != 0
  1530. os_printf("type %d = ", type_idx);
  1531. wasm_dump_array_type(type);
  1532. #endif
  1533. *p_buf = p;
  1534. module->types[type_idx] = (WASMType *)type;
  1535. return true;
  1536. fail:
  1537. if (type)
  1538. destroy_array_type(type);
  1539. return false;
  1540. }
  1541. static bool
  1542. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1543. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1544. {
  1545. if (heap_type >= 0) {
  1546. if (!check_type_index(module, heap_type, error_buf, error_buf_size)) {
  1547. return false;
  1548. }
  1549. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1550. heap_type);
  1551. }
  1552. else {
  1553. if (!wasm_is_valid_heap_type(heap_type)) {
  1554. set_error_buf(error_buf, error_buf_size, "unknown type");
  1555. return false;
  1556. }
  1557. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1558. heap_type);
  1559. if (nullable) {
  1560. /* For (ref null func/extern/any/eq/i31/data),
  1561. they are same as
  1562. funcref/externref/anyref/eqref/i31ref/dataref,
  1563. we convert the multi-byte type to one-byte
  1564. type to reduce the footprint and the
  1565. complexity of type equal/subtype checking */
  1566. ref_type->ref_type =
  1567. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1568. }
  1569. }
  1570. return true;
  1571. }
  1572. static void
  1573. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1574. WASMRefType *ref_type2)
  1575. {
  1576. /**
  1577. * The difference rt1 ∖ rt2 between two reference types is defined as
  1578. * follows:
  1579. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1580. * (ref ht2) = (ref null?1 ht1)
  1581. */
  1582. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1583. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1584. ref_type1, wasm_reftype_struct_size(ref_type1));
  1585. }
  1586. else {
  1587. ref_type_diff->ref_type = ref_type1->ref_type;
  1588. }
  1589. if (ref_type2->ref_ht_common.nullable) {
  1590. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1591. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1592. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1593. (int32)ref_type_diff->ref_type - 0x80);
  1594. }
  1595. else {
  1596. ref_type_diff->ref_ht_typeidx.nullable = false;
  1597. }
  1598. }
  1599. }
  1600. #else /* else of WASM_ENABLE_GC != 0 */
  1601. static void
  1602. destroy_wasm_type(WASMType *type)
  1603. {
  1604. if (type->ref_count > 1) {
  1605. /* The type is referenced by other types
  1606. of current wasm module */
  1607. type->ref_count--;
  1608. return;
  1609. }
  1610. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1611. && WASM_ENABLE_LAZY_JIT != 0
  1612. if (type->call_to_llvm_jit_from_fast_jit)
  1613. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1614. #endif
  1615. wasm_runtime_free(type);
  1616. }
  1617. #endif /* end of WASM_ENABLE_GC != 0 */
  1618. static bool
  1619. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1620. char *error_buf, uint32 error_buf_size)
  1621. {
  1622. const uint8 *p = buf, *p_end = buf_end;
  1623. uint32 type_count, i;
  1624. uint64 total_size;
  1625. uint8 flag;
  1626. #if WASM_ENABLE_GC != 0
  1627. uint32 processed_type_count = 0;
  1628. #endif
  1629. read_leb_uint32(p, p_end, type_count);
  1630. if (type_count) {
  1631. module->type_count = type_count;
  1632. total_size = sizeof(WASMType *) * (uint64)type_count;
  1633. if (!(module->types =
  1634. loader_malloc(total_size, error_buf, error_buf_size))) {
  1635. return false;
  1636. }
  1637. #if WASM_ENABLE_GC == 0
  1638. for (i = 0; i < type_count; i++) {
  1639. WASMFuncType *type;
  1640. const uint8 *p_org;
  1641. uint32 param_count, result_count, j;
  1642. uint32 param_cell_num, ret_cell_num;
  1643. CHECK_BUF(p, p_end, 1);
  1644. flag = read_uint8(p);
  1645. if (flag != 0x60) {
  1646. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1647. return false;
  1648. }
  1649. read_leb_uint32(p, p_end, param_count);
  1650. /* Resolve param count and result count firstly */
  1651. p_org = p;
  1652. CHECK_BUF(p, p_end, param_count);
  1653. p += param_count;
  1654. read_leb_uint32(p, p_end, result_count);
  1655. CHECK_BUF(p, p_end, result_count);
  1656. p = p_org;
  1657. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1658. set_error_buf(error_buf, error_buf_size,
  1659. "param count or result count too large");
  1660. return false;
  1661. }
  1662. total_size = offsetof(WASMFuncType, types)
  1663. + sizeof(uint8) * (uint64)(param_count + result_count);
  1664. if (!(type = module->types[i] =
  1665. loader_malloc(total_size, error_buf, error_buf_size))) {
  1666. return false;
  1667. }
  1668. /* Resolve param types and result types */
  1669. type->ref_count = 1;
  1670. type->param_count = (uint16)param_count;
  1671. type->result_count = (uint16)result_count;
  1672. for (j = 0; j < param_count; j++) {
  1673. CHECK_BUF(p, p_end, 1);
  1674. type->types[j] = read_uint8(p);
  1675. }
  1676. read_leb_uint32(p, p_end, result_count);
  1677. for (j = 0; j < result_count; j++) {
  1678. CHECK_BUF(p, p_end, 1);
  1679. type->types[param_count + j] = read_uint8(p);
  1680. }
  1681. for (j = 0; j < param_count + result_count; j++) {
  1682. if (!is_value_type(type->types[j])) {
  1683. set_error_buf(error_buf, error_buf_size,
  1684. "unknown value type");
  1685. return false;
  1686. }
  1687. }
  1688. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1689. ret_cell_num =
  1690. wasm_get_cell_num(type->types + param_count, result_count);
  1691. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1692. set_error_buf(error_buf, error_buf_size,
  1693. "param count or result count too large");
  1694. return false;
  1695. }
  1696. type->param_cell_num = (uint16)param_cell_num;
  1697. type->ret_cell_num = (uint16)ret_cell_num;
  1698. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1699. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1700. #endif
  1701. #if WASM_ENABLE_WAMR_COMPILER != 0
  1702. for (j = 0; j < type->param_count + type->result_count; j++) {
  1703. if (type->types[j] == VALUE_TYPE_V128)
  1704. module->is_simd_used = true;
  1705. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1706. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1707. module->is_ref_types_used = true;
  1708. }
  1709. #endif
  1710. /* If there is already a same type created, use it instead */
  1711. for (j = 0; j < i; j++) {
  1712. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1713. if (module->types[j]->ref_count == UINT16_MAX) {
  1714. set_error_buf(error_buf, error_buf_size,
  1715. "wasm type's ref count too large");
  1716. return false;
  1717. }
  1718. destroy_wasm_type(type);
  1719. module->types[i] = module->types[j];
  1720. module->types[j]->ref_count++;
  1721. break;
  1722. }
  1723. }
  1724. }
  1725. #else /* else of WASM_ENABLE_GC == 0 */
  1726. for (i = 0; i < type_count; i++) {
  1727. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1728. uint32 rec_count = 1, j;
  1729. bool is_sub_final = true;
  1730. CHECK_BUF(p, p_end, 1);
  1731. flag = read_uint8(p);
  1732. if (flag == DEFINED_TYPE_REC) {
  1733. read_leb_uint32(p, p_end, rec_count);
  1734. if (rec_count > 1) {
  1735. uint64 new_total_size;
  1736. /* integer overflow */
  1737. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1738. set_error_buf(error_buf, error_buf_size,
  1739. "recursive type count too large");
  1740. return false;
  1741. }
  1742. module->type_count += rec_count - 1;
  1743. new_total_size =
  1744. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1745. if (new_total_size > UINT32_MAX) {
  1746. set_error_buf(error_buf, error_buf_size,
  1747. "allocate memory failed");
  1748. return false;
  1749. }
  1750. MEM_REALLOC(module->types, (uint32)total_size,
  1751. (uint32)new_total_size);
  1752. total_size = new_total_size;
  1753. }
  1754. LOG_VERBOSE("Processing rec group [%d-%d]",
  1755. processed_type_count,
  1756. processed_type_count + rec_count - 1);
  1757. }
  1758. else {
  1759. p--;
  1760. }
  1761. for (j = 0; j < rec_count; j++) {
  1762. WASMType *cur_type = NULL;
  1763. CHECK_BUF(p, p_end, 1);
  1764. flag = read_uint8(p);
  1765. parent_type_idx = -1;
  1766. if (flag == DEFINED_TYPE_SUB
  1767. || flag == DEFINED_TYPE_SUB_FINAL) {
  1768. read_leb_uint32(p, p_end, super_type_count);
  1769. if (super_type_count > 1) {
  1770. set_error_buf(error_buf, error_buf_size,
  1771. "super type count too large");
  1772. return false;
  1773. }
  1774. if (super_type_count > 0) {
  1775. read_leb_uint32(p, p_end, parent_type_idx);
  1776. if (parent_type_idx >= processed_type_count + j) {
  1777. set_error_buf_v(error_buf, error_buf_size,
  1778. "unknown type %d", parent_type_idx);
  1779. return false;
  1780. }
  1781. if (module->types[parent_type_idx]->is_sub_final) {
  1782. set_error_buf(error_buf, error_buf_size,
  1783. "sub type can not inherit from "
  1784. "a final super type");
  1785. return false;
  1786. }
  1787. }
  1788. if (flag == DEFINED_TYPE_SUB)
  1789. is_sub_final = false;
  1790. CHECK_BUF(p, p_end, 1);
  1791. flag = read_uint8(p);
  1792. }
  1793. if (flag == DEFINED_TYPE_FUNC) {
  1794. if (!resolve_func_type(&p, buf_end, module,
  1795. processed_type_count + j, error_buf,
  1796. error_buf_size)) {
  1797. return false;
  1798. }
  1799. }
  1800. else if (flag == DEFINED_TYPE_STRUCT) {
  1801. if (!resolve_struct_type(&p, buf_end, module,
  1802. processed_type_count + j,
  1803. error_buf, error_buf_size)) {
  1804. return false;
  1805. }
  1806. }
  1807. else if (flag == DEFINED_TYPE_ARRAY) {
  1808. if (!resolve_array_type(&p, buf_end, module,
  1809. processed_type_count + j, error_buf,
  1810. error_buf_size)) {
  1811. return false;
  1812. }
  1813. }
  1814. else {
  1815. set_error_buf(error_buf, error_buf_size,
  1816. "invalid type flag");
  1817. return false;
  1818. }
  1819. cur_type = module->types[processed_type_count + j];
  1820. cur_type->parent_type_idx = parent_type_idx;
  1821. cur_type->is_sub_final = is_sub_final;
  1822. if (rec_count > 1) {
  1823. cur_type->rec_count = rec_count;
  1824. cur_type->rec_idx = j;
  1825. }
  1826. }
  1827. /* resolve subtyping relationship in current rec group */
  1828. for (j = 0; j < rec_count; j++) {
  1829. WASMType *cur_type = module->types[processed_type_count + j];
  1830. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1831. WASMType *parent_type =
  1832. module->types[cur_type->parent_type_idx];
  1833. cur_type->parent_type = parent_type;
  1834. cur_type->root_type = parent_type->root_type;
  1835. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1836. }
  1837. else {
  1838. cur_type->parent_type = NULL;
  1839. cur_type->root_type = cur_type;
  1840. cur_type->inherit_depth = 0;
  1841. }
  1842. }
  1843. for (j = 0; j < rec_count; j++) {
  1844. WASMType *cur_type = module->types[processed_type_count + j];
  1845. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1846. WASMType *parent_type =
  1847. module->types[cur_type->parent_type_idx];
  1848. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1849. module->types,
  1850. module->type_count)) {
  1851. set_error_buf(error_buf, error_buf_size,
  1852. "sub type does not match super type");
  1853. return false;
  1854. }
  1855. }
  1856. }
  1857. if (rec_count > 1) {
  1858. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1859. processed_type_count,
  1860. processed_type_count + rec_count - 1);
  1861. }
  1862. processed_type_count += rec_count;
  1863. }
  1864. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1865. * module->type_count,
  1866. error_buf, error_buf_size))) {
  1867. return false;
  1868. }
  1869. #endif /* end of WASM_ENABLE_GC == 0 */
  1870. }
  1871. if (p != p_end) {
  1872. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1873. return false;
  1874. }
  1875. LOG_VERBOSE("Load type section success.\n");
  1876. return true;
  1877. fail:
  1878. return false;
  1879. }
  1880. static void
  1881. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1882. {
  1883. uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
  1884. ? init_size * 2
  1885. : WASM_TABLE_MAX_SIZE;
  1886. if (max_size_flag) {
  1887. /* module defines the table limitation */
  1888. bh_assert(init_size <= *max_size);
  1889. if (init_size < *max_size) {
  1890. *max_size =
  1891. *max_size < default_max_size ? *max_size : default_max_size;
  1892. }
  1893. }
  1894. else {
  1895. /* partial defined table limitation, gives a default value */
  1896. *max_size = default_max_size;
  1897. }
  1898. }
  1899. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1900. /**
  1901. * Find export item of a module with export info:
  1902. * module name, field name and export kind
  1903. */
  1904. static WASMExport *
  1905. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1906. const char *field_name, uint8 export_kind,
  1907. char *error_buf, uint32 error_buf_size)
  1908. {
  1909. WASMExport *export =
  1910. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  1911. export_kind, error_buf, error_buf_size);
  1912. return export;
  1913. }
  1914. #endif
  1915. #if WASM_ENABLE_MULTI_MODULE != 0
  1916. static WASMFunction *
  1917. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  1918. const WASMFuncType *expected_function_type,
  1919. char *error_buf, uint32 error_buf_size)
  1920. {
  1921. WASMModuleCommon *module_reg;
  1922. WASMFunction *function = NULL;
  1923. WASMExport *export = NULL;
  1924. WASMModule *module = NULL;
  1925. WASMFuncType *target_function_type = NULL;
  1926. module_reg = wasm_runtime_find_module_registered(module_name);
  1927. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1928. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  1929. function_name);
  1930. set_error_buf(error_buf, error_buf_size, "unknown import");
  1931. return NULL;
  1932. }
  1933. module = (WASMModule *)module_reg;
  1934. export =
  1935. wasm_loader_find_export(module, module_name, function_name,
  1936. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  1937. if (!export) {
  1938. return NULL;
  1939. }
  1940. /* resolve function type and function */
  1941. if (export->index < module->import_function_count) {
  1942. target_function_type =
  1943. module->import_functions[export->index].u.function.func_type;
  1944. function = module->import_functions[export->index]
  1945. .u.function.import_func_linked;
  1946. }
  1947. else {
  1948. target_function_type =
  1949. module->functions[export->index - module->import_function_count]
  1950. ->func_type;
  1951. function =
  1952. module->functions[export->index - module->import_function_count];
  1953. }
  1954. /* check function type */
  1955. if (!wasm_type_equal((WASMType *)expected_function_type,
  1956. (WASMType *)target_function_type, module->types,
  1957. module->type_count)) {
  1958. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  1959. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1960. return NULL;
  1961. }
  1962. return function;
  1963. }
  1964. static WASMTable *
  1965. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  1966. uint32 init_size, uint32 max_size, char *error_buf,
  1967. uint32 error_buf_size)
  1968. {
  1969. WASMModuleCommon *module_reg;
  1970. WASMTable *table = NULL;
  1971. WASMExport *export = NULL;
  1972. WASMModule *module = NULL;
  1973. module_reg = wasm_runtime_find_module_registered(module_name);
  1974. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1975. LOG_DEBUG("can not find a module named %s for table", module_name);
  1976. set_error_buf(error_buf, error_buf_size, "unknown import");
  1977. return NULL;
  1978. }
  1979. module = (WASMModule *)module_reg;
  1980. export =
  1981. wasm_loader_find_export(module, module_name, table_name,
  1982. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  1983. if (!export) {
  1984. return NULL;
  1985. }
  1986. /* resolve table and check the init/max size */
  1987. if (export->index < module->import_table_count) {
  1988. table =
  1989. module->import_tables[export->index].u.table.import_table_linked;
  1990. }
  1991. else {
  1992. table = &(module->tables[export->index - module->import_table_count]);
  1993. }
  1994. if (table->init_size < init_size || table->max_size > max_size) {
  1995. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  1996. module_name, table_name, table->init_size, table->max_size,
  1997. init_size, max_size);
  1998. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1999. return NULL;
  2000. }
  2001. return table;
  2002. }
  2003. static WASMMemory *
  2004. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2005. uint32 init_page_count, uint32 max_page_count,
  2006. char *error_buf, uint32 error_buf_size)
  2007. {
  2008. WASMModuleCommon *module_reg;
  2009. WASMMemory *memory = NULL;
  2010. WASMExport *export = NULL;
  2011. WASMModule *module = NULL;
  2012. module_reg = wasm_runtime_find_module_registered(module_name);
  2013. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2014. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2015. set_error_buf(error_buf, error_buf_size, "unknown import");
  2016. return NULL;
  2017. }
  2018. module = (WASMModule *)module_reg;
  2019. export =
  2020. wasm_loader_find_export(module, module_name, memory_name,
  2021. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2022. if (!export) {
  2023. return NULL;
  2024. }
  2025. /* resolve memory and check the init/max page count */
  2026. if (export->index < module->import_memory_count) {
  2027. memory = module->import_memories[export->index]
  2028. .u.memory.import_memory_linked;
  2029. }
  2030. else {
  2031. memory =
  2032. &(module->memories[export->index - module->import_memory_count]);
  2033. }
  2034. if (memory->init_page_count < init_page_count
  2035. || memory->max_page_count > max_page_count) {
  2036. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2037. module_name, memory_name, memory->init_page_count,
  2038. memory->max_page_count, init_page_count, max_page_count);
  2039. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2040. return NULL;
  2041. }
  2042. return memory;
  2043. }
  2044. static WASMGlobal *
  2045. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2046. uint8 type, bool is_mutable, char *error_buf,
  2047. uint32 error_buf_size)
  2048. {
  2049. WASMModuleCommon *module_reg;
  2050. WASMGlobal *global = NULL;
  2051. WASMExport *export = NULL;
  2052. WASMModule *module = NULL;
  2053. module_reg = wasm_runtime_find_module_registered(module_name);
  2054. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2055. LOG_DEBUG("can not find a module named %s for global", module_name);
  2056. set_error_buf(error_buf, error_buf_size, "unknown import");
  2057. return NULL;
  2058. }
  2059. module = (WASMModule *)module_reg;
  2060. export =
  2061. wasm_loader_find_export(module, module_name, global_name,
  2062. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2063. if (!export) {
  2064. return NULL;
  2065. }
  2066. /* resolve and check the global */
  2067. if (export->index < module->import_global_count) {
  2068. global =
  2069. module->import_globals[export->index].u.global.import_global_linked;
  2070. }
  2071. else {
  2072. global =
  2073. &(module->globals[export->index - module->import_global_count]);
  2074. }
  2075. if (global->type != type || global->is_mutable != is_mutable) {
  2076. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2077. module_name, global_name, global->type, global->is_mutable,
  2078. type, is_mutable);
  2079. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2080. return NULL;
  2081. }
  2082. return global;
  2083. }
  2084. #if WASM_ENABLE_TAGS != 0
  2085. static WASMTag *
  2086. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2087. const WASMType *expected_tag_type,
  2088. uint32 *linked_tag_index, char *error_buf,
  2089. uint32 error_buf_size)
  2090. {
  2091. WASMModuleCommon *module_reg;
  2092. WASMTag *tag = NULL;
  2093. WASMExport *export = NULL;
  2094. WASMModule *module = NULL;
  2095. module_reg = wasm_runtime_find_module_registered(module_name);
  2096. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2097. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2098. tag_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, tag_name, EXPORT_KIND_TAG,
  2105. error_buf, error_buf_size);
  2106. if (!export) {
  2107. return NULL;
  2108. }
  2109. /* resolve tag type and tag */
  2110. if (export->index < module->import_tag_count) {
  2111. /* importing an imported tag from the submodule */
  2112. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2113. }
  2114. else {
  2115. /* importing an section tag from the submodule */
  2116. tag = module->tags[export->index - module->import_tag_count];
  2117. }
  2118. /* check function type */
  2119. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2120. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2121. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2122. return NULL;
  2123. }
  2124. if (linked_tag_index != NULL) {
  2125. *linked_tag_index = export->index;
  2126. }
  2127. return tag;
  2128. }
  2129. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2130. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2131. static bool
  2132. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2133. const WASMModule *parent_module,
  2134. const char *sub_module_name, const char *function_name,
  2135. WASMFunctionImport *function, char *error_buf,
  2136. uint32 error_buf_size)
  2137. {
  2138. const uint8 *p = *p_buf, *p_end = buf_end;
  2139. uint32 declare_type_index = 0;
  2140. WASMFuncType *declare_func_type = NULL;
  2141. WASMFunction *linked_func = NULL;
  2142. #if WASM_ENABLE_MULTI_MODULE != 0
  2143. WASMModule *sub_module = NULL;
  2144. #endif
  2145. const char *linked_signature = NULL;
  2146. void *linked_attachment = NULL;
  2147. bool linked_call_conv_raw = false;
  2148. bool is_native_symbol = false;
  2149. read_leb_uint32(p, p_end, declare_type_index);
  2150. *p_buf = p;
  2151. if (declare_type_index >= parent_module->type_count) {
  2152. set_error_buf(error_buf, error_buf_size, "unknown type");
  2153. return false;
  2154. }
  2155. #if WASM_ENABLE_GC != 0
  2156. function->type_idx = declare_type_index;
  2157. #endif
  2158. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2159. declare_type_index = wasm_get_smallest_type_idx(
  2160. parent_module->types, parent_module->type_count, declare_type_index);
  2161. #endif
  2162. declare_func_type =
  2163. (WASMFuncType *)parent_module->types[declare_type_index];
  2164. /* lookup registered native symbols first */
  2165. linked_func = wasm_native_resolve_symbol(
  2166. sub_module_name, function_name, declare_func_type, &linked_signature,
  2167. &linked_attachment, &linked_call_conv_raw);
  2168. if (linked_func) {
  2169. is_native_symbol = true;
  2170. }
  2171. #if WASM_ENABLE_MULTI_MODULE != 0
  2172. else {
  2173. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2174. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2175. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2176. error_buf_size);
  2177. if (!sub_module) {
  2178. return false;
  2179. }
  2180. }
  2181. linked_func = wasm_loader_resolve_function(
  2182. sub_module_name, function_name, declare_func_type, error_buf,
  2183. error_buf_size);
  2184. }
  2185. #endif
  2186. function->module_name = (char *)sub_module_name;
  2187. function->field_name = (char *)function_name;
  2188. function->func_type = declare_func_type;
  2189. /* func_ptr_linked is for native registered symbol */
  2190. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2191. function->signature = linked_signature;
  2192. function->attachment = linked_attachment;
  2193. function->call_conv_raw = linked_call_conv_raw;
  2194. #if WASM_ENABLE_MULTI_MODULE != 0
  2195. function->import_module = is_native_symbol ? NULL : sub_module;
  2196. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2197. #endif
  2198. return true;
  2199. fail:
  2200. return false;
  2201. }
  2202. static bool
  2203. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2204. uint32 error_buf_size)
  2205. {
  2206. if (max_size < init_size) {
  2207. set_error_buf(error_buf, error_buf_size,
  2208. "size minimum must not be greater than maximum");
  2209. return false;
  2210. }
  2211. return true;
  2212. }
  2213. static bool
  2214. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2215. WASMModule *parent_module, const char *sub_module_name,
  2216. const char *table_name, WASMTableImport *table,
  2217. char *error_buf, uint32 error_buf_size)
  2218. {
  2219. const uint8 *p = *p_buf, *p_end = buf_end;
  2220. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2221. declare_init_size = 0, declare_max_size = 0;
  2222. #if WASM_ENABLE_MULTI_MODULE != 0
  2223. WASMModule *sub_module = NULL;
  2224. WASMTable *linked_table = NULL;
  2225. #endif
  2226. #if WASM_ENABLE_GC != 0
  2227. WASMRefType ref_type;
  2228. bool need_ref_type_map;
  2229. #endif
  2230. #if WASM_ENABLE_GC == 0
  2231. CHECK_BUF(p, p_end, 1);
  2232. /* 0x70 or 0x6F */
  2233. declare_elem_type = read_uint8(p);
  2234. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2235. #if WASM_ENABLE_REF_TYPES != 0
  2236. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2237. #endif
  2238. ) {
  2239. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2240. return false;
  2241. }
  2242. #else /* else of WASM_ENABLE_GC == 0 */
  2243. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2244. &ref_type, false, error_buf, error_buf_size)) {
  2245. return false;
  2246. }
  2247. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2248. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2249. return false;
  2250. }
  2251. declare_elem_type = ref_type.ref_type;
  2252. if (need_ref_type_map) {
  2253. if (!(table->elem_ref_type =
  2254. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2255. error_buf, error_buf_size))) {
  2256. return false;
  2257. }
  2258. }
  2259. #if TRACE_WASM_LOADER != 0
  2260. os_printf("import table type: ");
  2261. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  2262. os_printf("\n");
  2263. #endif
  2264. #endif /* end of WASM_ENABLE_GC == 0 */
  2265. read_leb_uint32(p, p_end, declare_max_size_flag);
  2266. if (declare_max_size_flag > 1) {
  2267. set_error_buf(error_buf, error_buf_size, "integer too large");
  2268. return false;
  2269. }
  2270. read_leb_uint32(p, p_end, declare_init_size);
  2271. if (declare_max_size_flag) {
  2272. read_leb_uint32(p, p_end, declare_max_size);
  2273. if (!check_table_max_size(declare_init_size, declare_max_size,
  2274. error_buf, error_buf_size))
  2275. return false;
  2276. }
  2277. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2278. &declare_max_size);
  2279. *p_buf = p;
  2280. #if WASM_ENABLE_MULTI_MODULE != 0
  2281. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2282. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2283. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2284. error_buf_size);
  2285. if (!sub_module) {
  2286. return false;
  2287. }
  2288. linked_table = wasm_loader_resolve_table(
  2289. sub_module_name, table_name, declare_init_size, declare_max_size,
  2290. error_buf, error_buf_size);
  2291. if (!linked_table) {
  2292. return false;
  2293. }
  2294. /* reset with linked table limit */
  2295. declare_elem_type = linked_table->elem_type;
  2296. declare_init_size = linked_table->init_size;
  2297. declare_max_size = linked_table->max_size;
  2298. declare_max_size_flag = linked_table->flags;
  2299. table->import_table_linked = linked_table;
  2300. table->import_module = sub_module;
  2301. }
  2302. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2303. /* (table (export "table") 10 20 funcref) */
  2304. /* we need this section working in wamrc */
  2305. if (!strcmp("spectest", sub_module_name)) {
  2306. const uint32 spectest_table_init_size = 10;
  2307. const uint32 spectest_table_max_size = 20;
  2308. if (strcmp("table", table_name)) {
  2309. set_error_buf(error_buf, error_buf_size,
  2310. "incompatible import type or unknown import");
  2311. return false;
  2312. }
  2313. if (declare_init_size > spectest_table_init_size
  2314. || declare_max_size < spectest_table_max_size) {
  2315. set_error_buf(error_buf, error_buf_size,
  2316. "incompatible import type");
  2317. return false;
  2318. }
  2319. declare_init_size = spectest_table_init_size;
  2320. declare_max_size = spectest_table_max_size;
  2321. }
  2322. /* now we believe all declaration are ok */
  2323. table->elem_type = declare_elem_type;
  2324. table->init_size = declare_init_size;
  2325. table->flags = declare_max_size_flag;
  2326. table->max_size = declare_max_size;
  2327. #if WASM_ENABLE_WAMR_COMPILER != 0
  2328. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2329. parent_module->is_ref_types_used = true;
  2330. #endif
  2331. (void)parent_module;
  2332. return true;
  2333. fail:
  2334. return false;
  2335. }
  2336. static bool
  2337. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2338. uint32 error_buf_size)
  2339. {
  2340. uint32 default_max_size =
  2341. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2342. if (!is_memory64 && init_size > default_max_size) {
  2343. set_error_buf(error_buf, error_buf_size,
  2344. "memory size must be at most 65536 pages (4GiB)");
  2345. return false;
  2346. }
  2347. #if WASM_ENABLE_MEMORY64 != 0
  2348. else if (is_memory64 && init_size > default_max_size) {
  2349. set_error_buf(
  2350. error_buf, error_buf_size,
  2351. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2352. return false;
  2353. }
  2354. #endif
  2355. return true;
  2356. }
  2357. static bool
  2358. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2359. char *error_buf, uint32 error_buf_size)
  2360. {
  2361. uint32 default_max_size =
  2362. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2363. if (max_size < init_size) {
  2364. set_error_buf(error_buf, error_buf_size,
  2365. "size minimum must not be greater than maximum");
  2366. return false;
  2367. }
  2368. if (!is_memory64 && max_size > default_max_size) {
  2369. set_error_buf(error_buf, error_buf_size,
  2370. "memory size must be at most 65536 pages (4GiB)");
  2371. return false;
  2372. }
  2373. #if WASM_ENABLE_MEMORY64 != 0
  2374. else if (is_memory64 && max_size > default_max_size) {
  2375. set_error_buf(
  2376. error_buf, error_buf_size,
  2377. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2378. return false;
  2379. }
  2380. #endif
  2381. return true;
  2382. }
  2383. static bool
  2384. check_memory_flag(const uint8 mem_flag, char *error_buf, uint32 error_buf_size)
  2385. {
  2386. /* Check whether certain features indicated by mem_flag are enabled in
  2387. * runtime */
  2388. if (mem_flag > MAX_PAGE_COUNT_FLAG) {
  2389. #if WASM_ENABLE_SHARED_MEMORY == 0
  2390. if (mem_flag & SHARED_MEMORY_FLAG) {
  2391. LOG_VERBOSE("shared memory flag was found, please enable shared "
  2392. "memory, lib-pthread or lib-wasi-threads");
  2393. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2394. return false;
  2395. }
  2396. #endif
  2397. #if WASM_ENABLE_MEMORY64 == 0
  2398. if (mem_flag & MEMORY64_FLAG) {
  2399. LOG_VERBOSE("memory64 flag was found, please enable memory64");
  2400. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2401. return false;
  2402. }
  2403. #endif
  2404. }
  2405. if (mem_flag > MAX_PAGE_COUNT_FLAG + SHARED_MEMORY_FLAG + MEMORY64_FLAG) {
  2406. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2407. return false;
  2408. }
  2409. else if ((mem_flag & SHARED_MEMORY_FLAG)
  2410. && !(mem_flag & MAX_PAGE_COUNT_FLAG)) {
  2411. set_error_buf(error_buf, error_buf_size,
  2412. "shared memory must have maximum");
  2413. return false;
  2414. }
  2415. return true;
  2416. }
  2417. static bool
  2418. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2419. WASMModule *parent_module, const char *sub_module_name,
  2420. const char *memory_name, WASMMemoryImport *memory,
  2421. char *error_buf, uint32 error_buf_size)
  2422. {
  2423. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2424. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2425. uint32 pool_size = wasm_runtime_memory_pool_size();
  2426. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2427. / DEFAULT_NUM_BYTES_PER_PAGE;
  2428. #else
  2429. uint32 max_page_count;
  2430. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2431. uint32 mem_flag = 0;
  2432. bool is_memory64 = false;
  2433. uint32 declare_init_page_count = 0;
  2434. uint32 declare_max_page_count = 0;
  2435. #if WASM_ENABLE_MULTI_MODULE != 0
  2436. WASMModule *sub_module = NULL;
  2437. WASMMemory *linked_memory = NULL;
  2438. #endif
  2439. p_org = p;
  2440. read_leb_uint32(p, p_end, mem_flag);
  2441. is_memory64 = mem_flag & MEMORY64_FLAG;
  2442. if (p - p_org > 1) {
  2443. LOG_VERBOSE("integer representation too long");
  2444. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2445. return false;
  2446. }
  2447. if (!check_memory_flag(mem_flag, error_buf, error_buf_size)) {
  2448. return false;
  2449. }
  2450. read_leb_uint32(p, p_end, declare_init_page_count);
  2451. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2452. error_buf_size)) {
  2453. return false;
  2454. }
  2455. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2456. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2457. #endif
  2458. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2459. read_leb_uint32(p, p_end, declare_max_page_count);
  2460. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2461. declare_max_page_count, error_buf,
  2462. error_buf_size)) {
  2463. return false;
  2464. }
  2465. if (declare_max_page_count > max_page_count) {
  2466. declare_max_page_count = max_page_count;
  2467. }
  2468. }
  2469. else {
  2470. /* Limit the maximum memory size to max_page_count */
  2471. declare_max_page_count = max_page_count;
  2472. }
  2473. #if WASM_ENABLE_MULTI_MODULE != 0
  2474. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2475. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2476. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2477. error_buf_size);
  2478. if (!sub_module) {
  2479. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  2480. /* Avoid memory import failure when wasi-threads is enabled
  2481. and the memory is shared */
  2482. if (!(mem_flag & SHARED_MEMORY_FLAG))
  2483. return false;
  2484. #else
  2485. return false;
  2486. #endif /* WASM_ENABLE_LIB_WASI_THREADS */
  2487. }
  2488. else {
  2489. linked_memory = wasm_loader_resolve_memory(
  2490. sub_module_name, memory_name, declare_init_page_count,
  2491. declare_max_page_count, error_buf, error_buf_size);
  2492. if (!linked_memory) {
  2493. return false;
  2494. }
  2495. /**
  2496. * reset with linked memory limit
  2497. */
  2498. memory->import_module = sub_module;
  2499. memory->import_memory_linked = linked_memory;
  2500. declare_init_page_count = linked_memory->init_page_count;
  2501. declare_max_page_count = linked_memory->max_page_count;
  2502. }
  2503. }
  2504. #endif
  2505. /* (memory (export "memory") 1 2) */
  2506. if (!strcmp("spectest", sub_module_name)) {
  2507. uint32 spectest_memory_init_page = 1;
  2508. uint32 spectest_memory_max_page = 2;
  2509. if (strcmp("memory", memory_name)) {
  2510. set_error_buf(error_buf, error_buf_size,
  2511. "incompatible import type or unknown import");
  2512. return false;
  2513. }
  2514. if (declare_init_page_count > spectest_memory_init_page
  2515. || declare_max_page_count < spectest_memory_max_page) {
  2516. set_error_buf(error_buf, error_buf_size,
  2517. "incompatible import type");
  2518. return false;
  2519. }
  2520. declare_init_page_count = spectest_memory_init_page;
  2521. declare_max_page_count = spectest_memory_max_page;
  2522. }
  2523. /* now we believe all declaration are ok */
  2524. memory->flags = mem_flag;
  2525. memory->init_page_count = declare_init_page_count;
  2526. memory->max_page_count = declare_max_page_count;
  2527. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2528. *p_buf = p;
  2529. (void)parent_module;
  2530. return true;
  2531. fail:
  2532. return false;
  2533. }
  2534. #if WASM_ENABLE_TAGS != 0
  2535. static bool
  2536. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2537. const WASMModule *parent_module, /* this module ! */
  2538. const char *sub_module_name, const char *tag_name,
  2539. WASMTagImport *tag, /* structure to fill */
  2540. char *error_buf, uint32 error_buf_size)
  2541. {
  2542. /* attribute and type of the import statement */
  2543. uint8 declare_tag_attribute;
  2544. uint32 declare_type_index;
  2545. const uint8 *p = *p_buf, *p_end = buf_end;
  2546. #if WASM_ENABLE_MULTI_MODULE != 0
  2547. WASMModule *sub_module = NULL;
  2548. #endif
  2549. /* get the one byte attribute */
  2550. CHECK_BUF(p, p_end, 1);
  2551. declare_tag_attribute = read_uint8(p);
  2552. if (declare_tag_attribute != 0) {
  2553. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2554. goto fail;
  2555. }
  2556. /* get type */
  2557. read_leb_uint32(p, p_end, declare_type_index);
  2558. /* compare against module->types */
  2559. if (declare_type_index >= parent_module->type_count) {
  2560. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2561. goto fail;
  2562. }
  2563. WASMType *declare_tag_type = parent_module->types[declare_type_index];
  2564. /* check, that the type of the declared tag returns void */
  2565. if (declare_tag_type->result_count != 0) {
  2566. set_error_buf(error_buf, error_buf_size,
  2567. "tag type signature does not return void");
  2568. goto fail;
  2569. }
  2570. #if WASM_ENABLE_MULTI_MODULE != 0
  2571. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2572. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2573. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2574. error_buf_size);
  2575. if (!sub_module) {
  2576. return false;
  2577. }
  2578. /* wasm_loader_resolve_tag checks, that the imported tag
  2579. * and the declared tag have the same type
  2580. */
  2581. uint32 linked_tag_index = 0;
  2582. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2583. sub_module_name, tag_name, declare_tag_type,
  2584. &linked_tag_index /* out */, error_buf, error_buf_size);
  2585. if (linked_tag) {
  2586. tag->import_module = sub_module;
  2587. tag->import_tag_linked = linked_tag;
  2588. tag->import_tag_index_linked = linked_tag_index;
  2589. }
  2590. }
  2591. #endif
  2592. /* store to module tag declarations */
  2593. tag->attribute = declare_tag_attribute;
  2594. tag->type = declare_type_index;
  2595. tag->module_name = (char *)sub_module_name;
  2596. tag->field_name = (char *)tag_name;
  2597. tag->tag_type = declare_tag_type;
  2598. *p_buf = p;
  2599. (void)parent_module;
  2600. LOG_VERBOSE("Load tag import success\n");
  2601. return true;
  2602. fail:
  2603. return false;
  2604. }
  2605. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2606. static bool
  2607. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2608. WASMModule *parent_module, char *sub_module_name,
  2609. char *global_name, WASMGlobalImport *global, char *error_buf,
  2610. uint32 error_buf_size)
  2611. {
  2612. const uint8 *p = *p_buf, *p_end = buf_end;
  2613. uint8 declare_type = 0;
  2614. uint8 declare_mutable = 0;
  2615. #if WASM_ENABLE_MULTI_MODULE != 0
  2616. WASMModule *sub_module = NULL;
  2617. WASMGlobal *linked_global = NULL;
  2618. #endif
  2619. #if WASM_ENABLE_GC != 0
  2620. WASMRefType ref_type;
  2621. bool need_ref_type_map;
  2622. #endif
  2623. bool ret = false;
  2624. #if WASM_ENABLE_GC == 0
  2625. CHECK_BUF(p, p_end, 2);
  2626. declare_type = read_uint8(p);
  2627. declare_mutable = read_uint8(p);
  2628. #else
  2629. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2630. &ref_type, false, error_buf, error_buf_size)) {
  2631. return false;
  2632. }
  2633. declare_type = ref_type.ref_type;
  2634. if (need_ref_type_map) {
  2635. if (!(global->ref_type =
  2636. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2637. error_buf, error_buf_size))) {
  2638. return false;
  2639. }
  2640. }
  2641. #if TRACE_WASM_LOADER != 0
  2642. os_printf("import global type: ");
  2643. wasm_dump_value_type(declare_type, global->ref_type);
  2644. os_printf("\n");
  2645. #endif
  2646. CHECK_BUF(p, p_end, 1);
  2647. declare_mutable = read_uint8(p);
  2648. #endif /* end of WASM_ENABLE_GC == 0 */
  2649. *p_buf = p;
  2650. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2651. return false;
  2652. }
  2653. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2654. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2655. global);
  2656. if (ret) {
  2657. if (global->type != declare_type
  2658. || global->is_mutable != declare_mutable) {
  2659. set_error_buf(error_buf, error_buf_size,
  2660. "incompatible import type");
  2661. return false;
  2662. }
  2663. global->is_linked = true;
  2664. }
  2665. #endif
  2666. #if WASM_ENABLE_MULTI_MODULE != 0
  2667. if (!global->is_linked
  2668. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2669. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2670. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2671. error_buf_size);
  2672. if (!sub_module) {
  2673. return false;
  2674. }
  2675. /* check sub modules */
  2676. linked_global = wasm_loader_resolve_global(
  2677. sub_module_name, global_name, declare_type, declare_mutable,
  2678. error_buf, error_buf_size);
  2679. if (linked_global) {
  2680. global->import_module = sub_module;
  2681. global->import_global_linked = linked_global;
  2682. global->is_linked = true;
  2683. }
  2684. }
  2685. #endif
  2686. global->module_name = sub_module_name;
  2687. global->field_name = global_name;
  2688. global->type = declare_type;
  2689. global->is_mutable = (declare_mutable == 1);
  2690. #if WASM_ENABLE_WAMR_COMPILER != 0
  2691. if (global->type == VALUE_TYPE_V128)
  2692. parent_module->is_simd_used = true;
  2693. else if (global->type == VALUE_TYPE_EXTERNREF)
  2694. parent_module->is_ref_types_used = true;
  2695. #endif
  2696. (void)parent_module;
  2697. (void)ret;
  2698. return true;
  2699. fail:
  2700. return false;
  2701. }
  2702. static bool
  2703. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2704. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2705. {
  2706. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2707. #if WASM_ENABLE_GC != 0
  2708. WASMRefType ref_type;
  2709. bool need_ref_type_map;
  2710. #endif
  2711. #if WASM_ENABLE_GC == 0
  2712. CHECK_BUF(p, p_end, 1);
  2713. /* 0x70 or 0x6F */
  2714. table->elem_type = read_uint8(p);
  2715. if (VALUE_TYPE_FUNCREF != table->elem_type
  2716. #if WASM_ENABLE_REF_TYPES != 0
  2717. && VALUE_TYPE_EXTERNREF != table->elem_type
  2718. #endif
  2719. ) {
  2720. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2721. return false;
  2722. }
  2723. #else /* else of WASM_ENABLE_GC == 0 */
  2724. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  2725. false, error_buf, error_buf_size)) {
  2726. return false;
  2727. }
  2728. table->elem_type = ref_type.ref_type;
  2729. if (need_ref_type_map) {
  2730. if (!(table->elem_ref_type =
  2731. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2732. error_buf_size))) {
  2733. return false;
  2734. }
  2735. }
  2736. #if TRACE_WASM_LOADER != 0
  2737. os_printf("table type: ");
  2738. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2739. os_printf("\n");
  2740. #endif
  2741. #endif /* end of WASM_ENABLE_GC == 0 */
  2742. p_org = p;
  2743. read_leb_uint32(p, p_end, table->flags);
  2744. #if WASM_ENABLE_SHARED_MEMORY == 0
  2745. if (p - p_org > 1) {
  2746. set_error_buf(error_buf, error_buf_size,
  2747. "integer representation too long");
  2748. return false;
  2749. }
  2750. if (table->flags > 1) {
  2751. set_error_buf(error_buf, error_buf_size, "integer too large");
  2752. return false;
  2753. }
  2754. #else
  2755. if (p - p_org > 1) {
  2756. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2757. return false;
  2758. }
  2759. if (table->flags == 2) {
  2760. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2761. return false;
  2762. }
  2763. if (table->flags > 1) {
  2764. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2765. return false;
  2766. }
  2767. #endif
  2768. read_leb_uint32(p, p_end, table->init_size);
  2769. if (table->flags) {
  2770. read_leb_uint32(p, p_end, table->max_size);
  2771. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2772. error_buf_size))
  2773. return false;
  2774. }
  2775. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2776. #if WASM_ENABLE_WAMR_COMPILER != 0
  2777. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2778. module->is_ref_types_used = true;
  2779. #endif
  2780. *p_buf = p;
  2781. return true;
  2782. fail:
  2783. return false;
  2784. }
  2785. static bool
  2786. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2787. char *error_buf, uint32 error_buf_size)
  2788. {
  2789. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2790. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2791. uint32 pool_size = wasm_runtime_memory_pool_size();
  2792. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2793. / DEFAULT_NUM_BYTES_PER_PAGE;
  2794. #else
  2795. uint32 max_page_count;
  2796. #endif
  2797. bool is_memory64 = false;
  2798. p_org = p;
  2799. read_leb_uint32(p, p_end, memory->flags);
  2800. is_memory64 = memory->flags & MEMORY64_FLAG;
  2801. if (p - p_org > 1) {
  2802. LOG_VERBOSE("integer representation too long");
  2803. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2804. return false;
  2805. }
  2806. if (!check_memory_flag(memory->flags, error_buf, error_buf_size)) {
  2807. return false;
  2808. }
  2809. read_leb_uint32(p, p_end, memory->init_page_count);
  2810. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2811. error_buf_size))
  2812. return false;
  2813. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2814. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2815. #endif
  2816. if (memory->flags & 1) {
  2817. read_leb_uint32(p, p_end, memory->max_page_count);
  2818. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2819. memory->max_page_count, error_buf,
  2820. error_buf_size))
  2821. return false;
  2822. if (memory->max_page_count > max_page_count)
  2823. memory->max_page_count = max_page_count;
  2824. }
  2825. else {
  2826. /* Limit the maximum memory size to max_page_count */
  2827. memory->max_page_count = max_page_count;
  2828. }
  2829. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2830. *p_buf = p;
  2831. return true;
  2832. fail:
  2833. return false;
  2834. }
  2835. static bool
  2836. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2837. bool is_load_from_file_buf, char *error_buf,
  2838. uint32 error_buf_size)
  2839. {
  2840. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2841. uint32 import_count, name_len, type_index, i, u32, flags;
  2842. uint64 total_size;
  2843. WASMImport *import;
  2844. WASMImport *import_functions = NULL, *import_tables = NULL;
  2845. WASMImport *import_memories = NULL, *import_globals = NULL;
  2846. #if WASM_ENABLE_TAGS != 0
  2847. WASMImport *import_tags = NULL;
  2848. #endif
  2849. char *sub_module_name, *field_name;
  2850. uint8 u8, kind, global_type;
  2851. read_leb_uint32(p, p_end, import_count);
  2852. if (import_count) {
  2853. module->import_count = import_count;
  2854. total_size = sizeof(WASMImport) * (uint64)import_count;
  2855. if (!(module->imports =
  2856. loader_malloc(total_size, error_buf, error_buf_size))) {
  2857. return false;
  2858. }
  2859. p_old = p;
  2860. /* Scan firstly to get import count of each type */
  2861. for (i = 0; i < import_count; i++) {
  2862. /* module name */
  2863. read_leb_uint32(p, p_end, name_len);
  2864. CHECK_BUF(p, p_end, name_len);
  2865. p += name_len;
  2866. /* field name */
  2867. read_leb_uint32(p, p_end, name_len);
  2868. CHECK_BUF(p, p_end, name_len);
  2869. p += name_len;
  2870. CHECK_BUF(p, p_end, 1);
  2871. /* 0x00/0x01/0x02/0x03/0x04 */
  2872. kind = read_uint8(p);
  2873. switch (kind) {
  2874. case IMPORT_KIND_FUNC: /* import function */
  2875. read_leb_uint32(p, p_end, type_index);
  2876. module->import_function_count++;
  2877. break;
  2878. case IMPORT_KIND_TABLE: /* import table */
  2879. CHECK_BUF(p, p_end, 1);
  2880. /* 0x70 */
  2881. u8 = read_uint8(p);
  2882. read_leb_uint32(p, p_end, flags);
  2883. read_leb_uint32(p, p_end, u32);
  2884. if (flags & 1)
  2885. read_leb_uint32(p, p_end, u32);
  2886. module->import_table_count++;
  2887. if (module->import_table_count > 1) {
  2888. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2889. set_error_buf(error_buf, error_buf_size,
  2890. "multiple tables");
  2891. return false;
  2892. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2893. module->is_ref_types_used = true;
  2894. #endif
  2895. }
  2896. break;
  2897. case IMPORT_KIND_MEMORY: /* import memory */
  2898. read_leb_uint32(p, p_end, flags);
  2899. read_leb_uint32(p, p_end, u32);
  2900. if (flags & 1)
  2901. read_leb_uint32(p, p_end, u32);
  2902. module->import_memory_count++;
  2903. if (module->import_memory_count > 1) {
  2904. set_error_buf(error_buf, error_buf_size,
  2905. "multiple memories");
  2906. return false;
  2907. }
  2908. break;
  2909. #if WASM_ENABLE_TAGS != 0
  2910. case IMPORT_KIND_TAG: /* import tags */
  2911. /* it only counts the number of tags to import */
  2912. module->import_tag_count++;
  2913. CHECK_BUF(p, p_end, 1);
  2914. u8 = read_uint8(p);
  2915. read_leb_uint32(p, p_end, type_index);
  2916. break;
  2917. #endif
  2918. case IMPORT_KIND_GLOBAL: /* import global */
  2919. #if WASM_ENABLE_GC != 0
  2920. /* valtype */
  2921. CHECK_BUF(p, p_end, 1);
  2922. global_type = read_uint8(p);
  2923. if (wasm_is_type_multi_byte_type(global_type)) {
  2924. int32 heap_type;
  2925. read_leb_int32(p, p_end, heap_type);
  2926. (void)heap_type;
  2927. }
  2928. /* mutability */
  2929. CHECK_BUF(p, p_end, 1);
  2930. p += 1;
  2931. #else
  2932. CHECK_BUF(p, p_end, 2);
  2933. p += 2;
  2934. #endif
  2935. (void)global_type;
  2936. module->import_global_count++;
  2937. break;
  2938. default:
  2939. set_error_buf(error_buf, error_buf_size,
  2940. "invalid import kind");
  2941. return false;
  2942. }
  2943. }
  2944. if (module->import_function_count)
  2945. import_functions = module->import_functions = module->imports;
  2946. if (module->import_table_count)
  2947. import_tables = module->import_tables =
  2948. module->imports + module->import_function_count;
  2949. if (module->import_memory_count)
  2950. import_memories = module->import_memories =
  2951. module->imports + module->import_function_count
  2952. + module->import_table_count;
  2953. #if WASM_ENABLE_TAGS != 0
  2954. if (module->import_tag_count)
  2955. import_tags = module->import_tags =
  2956. module->imports + module->import_function_count
  2957. + module->import_table_count + module->import_memory_count;
  2958. if (module->import_global_count)
  2959. import_globals = module->import_globals =
  2960. module->imports + module->import_function_count
  2961. + module->import_table_count + module->import_memory_count
  2962. + module->import_tag_count;
  2963. #else
  2964. if (module->import_global_count)
  2965. import_globals = module->import_globals =
  2966. module->imports + module->import_function_count
  2967. + module->import_table_count + module->import_memory_count;
  2968. #endif
  2969. p = p_old;
  2970. /* Scan again to resolve the data */
  2971. for (i = 0; i < import_count; i++) {
  2972. /* load module name */
  2973. read_leb_uint32(p, p_end, name_len);
  2974. CHECK_BUF(p, p_end, name_len);
  2975. if (!(sub_module_name = wasm_const_str_list_insert(
  2976. p, name_len, module, is_load_from_file_buf, error_buf,
  2977. error_buf_size))) {
  2978. return false;
  2979. }
  2980. p += name_len;
  2981. /* load field name */
  2982. read_leb_uint32(p, p_end, name_len);
  2983. CHECK_BUF(p, p_end, name_len);
  2984. if (!(field_name = wasm_const_str_list_insert(
  2985. p, name_len, module, is_load_from_file_buf, error_buf,
  2986. error_buf_size))) {
  2987. return false;
  2988. }
  2989. p += name_len;
  2990. CHECK_BUF(p, p_end, 1);
  2991. /* 0x00/0x01/0x02/0x03/0x4 */
  2992. kind = read_uint8(p);
  2993. switch (kind) {
  2994. case IMPORT_KIND_FUNC: /* import function */
  2995. bh_assert(import_functions);
  2996. import = import_functions++;
  2997. if (!load_function_import(
  2998. &p, p_end, module, sub_module_name, field_name,
  2999. &import->u.function, error_buf, error_buf_size)) {
  3000. return false;
  3001. }
  3002. break;
  3003. case IMPORT_KIND_TABLE: /* import table */
  3004. bh_assert(import_tables);
  3005. import = import_tables++;
  3006. if (!load_table_import(&p, p_end, module, sub_module_name,
  3007. field_name, &import->u.table,
  3008. error_buf, error_buf_size)) {
  3009. LOG_DEBUG("can not import such a table (%s,%s)",
  3010. sub_module_name, field_name);
  3011. return false;
  3012. }
  3013. break;
  3014. case IMPORT_KIND_MEMORY: /* import memory */
  3015. bh_assert(import_memories);
  3016. import = import_memories++;
  3017. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3018. field_name, &import->u.memory,
  3019. error_buf, error_buf_size)) {
  3020. return false;
  3021. }
  3022. break;
  3023. #if WASM_ENABLE_TAGS != 0
  3024. case IMPORT_KIND_TAG:
  3025. bh_assert(import_tags);
  3026. import = import_tags++;
  3027. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3028. field_name, &import->u.tag, error_buf,
  3029. error_buf_size)) {
  3030. return false;
  3031. }
  3032. break;
  3033. #endif
  3034. case IMPORT_KIND_GLOBAL: /* import global */
  3035. bh_assert(import_globals);
  3036. import = import_globals++;
  3037. if (!load_global_import(&p, p_end, module, sub_module_name,
  3038. field_name, &import->u.global,
  3039. error_buf, error_buf_size)) {
  3040. return false;
  3041. }
  3042. break;
  3043. default:
  3044. set_error_buf(error_buf, error_buf_size,
  3045. "invalid import kind");
  3046. return false;
  3047. }
  3048. import->kind = kind;
  3049. import->u.names.module_name = sub_module_name;
  3050. import->u.names.field_name = field_name;
  3051. }
  3052. #if WASM_ENABLE_LIBC_WASI != 0
  3053. import = module->import_functions;
  3054. for (i = 0; i < module->import_function_count; i++, import++) {
  3055. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3056. || !strcmp(import->u.names.module_name,
  3057. "wasi_snapshot_preview1")) {
  3058. module->import_wasi_api = true;
  3059. break;
  3060. }
  3061. }
  3062. #endif
  3063. }
  3064. if (p != p_end) {
  3065. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3066. return false;
  3067. }
  3068. LOG_VERBOSE("Load import section success.\n");
  3069. (void)u8;
  3070. (void)u32;
  3071. (void)type_index;
  3072. return true;
  3073. fail:
  3074. return false;
  3075. }
  3076. static bool
  3077. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3078. uint32 error_buf_size)
  3079. {
  3080. WASMFuncType *param_type = func->func_type;
  3081. uint32 param_count = param_type->param_count;
  3082. uint8 *param_types = param_type->types;
  3083. uint32 local_count = func->local_count;
  3084. uint8 *local_types = func->local_types;
  3085. uint32 i, local_offset = 0;
  3086. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3087. /*
  3088. * Only allocate memory when total_size is not 0,
  3089. * or the return value of malloc(0) might be NULL on some platforms,
  3090. * which causes wasm loader return false.
  3091. */
  3092. if (total_size > 0
  3093. && !(func->local_offsets =
  3094. loader_malloc(total_size, error_buf, error_buf_size))) {
  3095. return false;
  3096. }
  3097. for (i = 0; i < param_count; i++) {
  3098. func->local_offsets[i] = (uint16)local_offset;
  3099. local_offset += wasm_value_type_cell_num(param_types[i]);
  3100. }
  3101. for (i = 0; i < local_count; i++) {
  3102. func->local_offsets[param_count + i] = (uint16)local_offset;
  3103. local_offset += wasm_value_type_cell_num(local_types[i]);
  3104. }
  3105. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3106. return true;
  3107. }
  3108. static bool
  3109. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3110. const uint8 *buf_code, const uint8 *buf_code_end,
  3111. WASMModule *module, char *error_buf,
  3112. uint32 error_buf_size)
  3113. {
  3114. const uint8 *p = buf, *p_end = buf_end;
  3115. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3116. uint32 func_count;
  3117. uint64 total_size;
  3118. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3119. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3120. uint8 type;
  3121. WASMFunction *func;
  3122. #if WASM_ENABLE_GC != 0
  3123. bool need_ref_type_map;
  3124. WASMRefType ref_type;
  3125. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3126. #endif
  3127. read_leb_uint32(p, p_end, func_count);
  3128. if (buf_code)
  3129. read_leb_uint32(p_code, buf_code_end, code_count);
  3130. if (func_count != code_count) {
  3131. set_error_buf(error_buf, error_buf_size,
  3132. "function and code section have inconsistent lengths or "
  3133. "unexpected end");
  3134. return false;
  3135. }
  3136. if (func_count) {
  3137. module->function_count = func_count;
  3138. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3139. if (!(module->functions =
  3140. loader_malloc(total_size, error_buf, error_buf_size))) {
  3141. return false;
  3142. }
  3143. for (i = 0; i < func_count; i++) {
  3144. /* Resolve function type */
  3145. read_leb_uint32(p, p_end, type_index);
  3146. if (type_index >= module->type_count) {
  3147. set_error_buf(error_buf, error_buf_size, "unknown type");
  3148. return false;
  3149. }
  3150. #if WASM_ENABLE_GC != 0
  3151. type_index_org = type_index;
  3152. #endif
  3153. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3154. type_index = wasm_get_smallest_type_idx(
  3155. module->types, module->type_count, type_index);
  3156. #endif
  3157. read_leb_uint32(p_code, buf_code_end, code_size);
  3158. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3159. set_error_buf(error_buf, error_buf_size,
  3160. "invalid function code size");
  3161. return false;
  3162. }
  3163. /* Resolve local set count */
  3164. p_code_end = p_code + code_size;
  3165. local_count = 0;
  3166. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3167. p_code_save = p_code;
  3168. #if WASM_ENABLE_GC != 0
  3169. ref_type_map_count = 0;
  3170. #endif
  3171. /* Calculate total local count */
  3172. for (j = 0; j < local_set_count; j++) {
  3173. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3174. if (sub_local_count > UINT32_MAX - local_count) {
  3175. set_error_buf(error_buf, error_buf_size, "too many locals");
  3176. return false;
  3177. }
  3178. #if WASM_ENABLE_GC == 0
  3179. CHECK_BUF(p_code, buf_code_end, 1);
  3180. /* 0x7F/0x7E/0x7D/0x7C */
  3181. type = read_uint8(p_code);
  3182. local_count += sub_local_count;
  3183. #if WASM_ENABLE_WAMR_COMPILER != 0
  3184. /* If any value's type is v128, mark the module as SIMD used */
  3185. if (type == VALUE_TYPE_V128)
  3186. module->is_simd_used = true;
  3187. #endif
  3188. #else
  3189. if (!resolve_value_type(&p_code, buf_code_end, module,
  3190. &need_ref_type_map, &ref_type, false,
  3191. error_buf, error_buf_size)) {
  3192. return false;
  3193. }
  3194. local_count += sub_local_count;
  3195. if (need_ref_type_map)
  3196. ref_type_map_count += sub_local_count;
  3197. #endif
  3198. }
  3199. /* Alloc memory, layout: function structure + local types */
  3200. code_size = (uint32)(p_code_end - p_code);
  3201. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3202. if (!(func = module->functions[i] =
  3203. loader_malloc(total_size, error_buf, error_buf_size))) {
  3204. return false;
  3205. }
  3206. #if WASM_ENABLE_GC != 0
  3207. if (ref_type_map_count > 0) {
  3208. total_size =
  3209. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3210. if (!(func->local_ref_type_maps = loader_malloc(
  3211. total_size, error_buf, error_buf_size))) {
  3212. return false;
  3213. }
  3214. func->local_ref_type_map_count = ref_type_map_count;
  3215. }
  3216. #endif
  3217. /* Set function type, local count, code size and code body */
  3218. func->func_type = (WASMFuncType *)module->types[type_index];
  3219. func->local_count = local_count;
  3220. if (local_count > 0)
  3221. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3222. func->code_size = code_size;
  3223. /*
  3224. * we shall make a copy of code body [p_code, p_code + code_size]
  3225. * when we are worrying about inappropriate releasing behaviour.
  3226. * all code bodies are actually in a buffer which user allocates in
  3227. * his embedding environment and we don't have power on them.
  3228. * it will be like:
  3229. * code_body_cp = malloc(code_size);
  3230. * memcpy(code_body_cp, p_code, code_size);
  3231. * func->code = code_body_cp;
  3232. */
  3233. func->code = (uint8 *)p_code;
  3234. #if WASM_ENABLE_GC != 0
  3235. func->type_idx = type_index_org;
  3236. #endif
  3237. #if WASM_ENABLE_GC != 0
  3238. t = 0;
  3239. #endif
  3240. /* Load each local type */
  3241. p_code = p_code_save;
  3242. local_type_index = 0;
  3243. for (j = 0; j < local_set_count; j++) {
  3244. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3245. /* Note: sub_local_count is allowed to be 0 */
  3246. if (local_type_index > UINT32_MAX - sub_local_count
  3247. || local_type_index + sub_local_count > local_count) {
  3248. set_error_buf(error_buf, error_buf_size,
  3249. "invalid local count");
  3250. return false;
  3251. }
  3252. #if WASM_ENABLE_GC == 0
  3253. CHECK_BUF(p_code, buf_code_end, 1);
  3254. /* 0x7F/0x7E/0x7D/0x7C */
  3255. type = read_uint8(p_code);
  3256. if (!is_value_type(type)) {
  3257. if (type == VALUE_TYPE_V128)
  3258. set_error_buf(error_buf, error_buf_size,
  3259. "v128 value type requires simd feature");
  3260. else if (type == VALUE_TYPE_FUNCREF
  3261. || type == VALUE_TYPE_EXTERNREF)
  3262. set_error_buf(error_buf, error_buf_size,
  3263. "ref value type requires "
  3264. "reference types feature");
  3265. else
  3266. set_error_buf_v(error_buf, error_buf_size,
  3267. "invalid local type 0x%02X", type);
  3268. return false;
  3269. }
  3270. #else
  3271. if (!resolve_value_type(&p_code, buf_code_end, module,
  3272. &need_ref_type_map, &ref_type, false,
  3273. error_buf, error_buf_size)) {
  3274. return false;
  3275. }
  3276. if (need_ref_type_map) {
  3277. WASMRefType *ref_type_tmp;
  3278. if (!(ref_type_tmp = reftype_set_insert(
  3279. module->ref_type_set, &ref_type, error_buf,
  3280. error_buf_size))) {
  3281. return false;
  3282. }
  3283. for (k = 0; k < sub_local_count; k++) {
  3284. func->local_ref_type_maps[t + k].ref_type =
  3285. ref_type_tmp;
  3286. func->local_ref_type_maps[t + k].index =
  3287. local_type_index + k;
  3288. }
  3289. t += sub_local_count;
  3290. }
  3291. type = ref_type.ref_type;
  3292. #endif
  3293. for (k = 0; k < sub_local_count; k++) {
  3294. func->local_types[local_type_index++] = type;
  3295. }
  3296. #if WASM_ENABLE_WAMR_COMPILER != 0
  3297. if (type == VALUE_TYPE_V128)
  3298. module->is_simd_used = true;
  3299. else if (type == VALUE_TYPE_FUNCREF
  3300. || type == VALUE_TYPE_EXTERNREF)
  3301. module->is_ref_types_used = true;
  3302. #endif
  3303. }
  3304. bh_assert(local_type_index == func->local_count);
  3305. #if WASM_ENABLE_GC != 0
  3306. bh_assert(t == func->local_ref_type_map_count);
  3307. #if TRACE_WASM_LOADER != 0
  3308. os_printf("func %u, local types: [", i);
  3309. k = 0;
  3310. for (j = 0; j < func->local_count; j++) {
  3311. WASMRefType *ref_type_tmp = NULL;
  3312. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3313. bh_assert(j == func->local_ref_type_maps[k].index);
  3314. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3315. }
  3316. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3317. if (j < func->local_count - 1)
  3318. os_printf(" ");
  3319. }
  3320. os_printf("]\n");
  3321. #endif
  3322. #endif
  3323. func->param_cell_num = func->func_type->param_cell_num;
  3324. func->ret_cell_num = func->func_type->ret_cell_num;
  3325. local_cell_num =
  3326. wasm_get_cell_num(func->local_types, func->local_count);
  3327. if (local_cell_num > UINT16_MAX) {
  3328. set_error_buf(error_buf, error_buf_size,
  3329. "local count too large");
  3330. return false;
  3331. }
  3332. func->local_cell_num = (uint16)local_cell_num;
  3333. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3334. return false;
  3335. p_code = p_code_end;
  3336. }
  3337. }
  3338. if (p != p_end) {
  3339. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3340. return false;
  3341. }
  3342. LOG_VERBOSE("Load function section success.\n");
  3343. return true;
  3344. fail:
  3345. return false;
  3346. }
  3347. static bool
  3348. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3349. char *error_buf, uint32 error_buf_size)
  3350. {
  3351. const uint8 *p = buf, *p_end = buf_end;
  3352. uint32 table_count, i;
  3353. uint64 total_size;
  3354. WASMTable *table;
  3355. read_leb_uint32(p, p_end, table_count);
  3356. if (module->import_table_count + table_count > 1) {
  3357. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3358. /* a total of one table is allowed */
  3359. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3360. return false;
  3361. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3362. module->is_ref_types_used = true;
  3363. #endif
  3364. }
  3365. if (table_count) {
  3366. module->table_count = table_count;
  3367. total_size = sizeof(WASMTable) * (uint64)table_count;
  3368. if (!(module->tables =
  3369. loader_malloc(total_size, error_buf, error_buf_size))) {
  3370. return false;
  3371. }
  3372. /* load each table */
  3373. table = module->tables;
  3374. for (i = 0; i < table_count; i++, table++) {
  3375. #if WASM_ENABLE_GC != 0
  3376. uint8 flag;
  3377. bool has_init = false;
  3378. CHECK_BUF(buf, buf_end, 1);
  3379. flag = read_uint8(p);
  3380. if (flag == TABLE_INIT_EXPR_FLAG) {
  3381. CHECK_BUF(buf, buf_end, 1);
  3382. flag = read_uint8(p);
  3383. if (flag != 0x00) {
  3384. set_error_buf(error_buf, error_buf_size,
  3385. "invalid leading bytes for table");
  3386. return false;
  3387. }
  3388. has_init = true;
  3389. }
  3390. else {
  3391. p--;
  3392. }
  3393. #endif /* end of WASM_ENABLE_GC != 0 */
  3394. if (!load_table(&p, p_end, module, table, error_buf,
  3395. error_buf_size))
  3396. return false;
  3397. #if WASM_ENABLE_GC != 0
  3398. if (has_init) {
  3399. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3400. table->elem_type, table->elem_ref_type,
  3401. error_buf, error_buf_size))
  3402. return false;
  3403. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3404. && table->init_expr.init_expr_type
  3405. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3406. set_error_buf(
  3407. error_buf, error_buf_size,
  3408. "unsupported initializer expression for table");
  3409. return false;
  3410. }
  3411. }
  3412. else {
  3413. if (wasm_is_reftype_htref_non_nullable(table->elem_type)) {
  3414. set_error_buf(
  3415. error_buf, error_buf_size,
  3416. "type mismatch: non-nullable table without init expr");
  3417. return false;
  3418. }
  3419. }
  3420. #endif /* end of WASM_ENABLE_GC != 0 */
  3421. #if WASM_ENABLE_WAMR_COMPILER != 0
  3422. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  3423. module->is_ref_types_used = true;
  3424. #endif
  3425. }
  3426. }
  3427. if (p != p_end) {
  3428. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3429. return false;
  3430. }
  3431. LOG_VERBOSE("Load table section success.\n");
  3432. return true;
  3433. fail:
  3434. return false;
  3435. }
  3436. static bool
  3437. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3438. char *error_buf, uint32 error_buf_size)
  3439. {
  3440. const uint8 *p = buf, *p_end = buf_end;
  3441. uint32 memory_count, i;
  3442. uint64 total_size;
  3443. WASMMemory *memory;
  3444. read_leb_uint32(p, p_end, memory_count);
  3445. /* a total of one memory is allowed */
  3446. if (module->import_memory_count + memory_count > 1) {
  3447. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3448. return false;
  3449. }
  3450. if (memory_count) {
  3451. module->memory_count = memory_count;
  3452. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3453. if (!(module->memories =
  3454. loader_malloc(total_size, error_buf, error_buf_size))) {
  3455. return false;
  3456. }
  3457. /* load each memory */
  3458. memory = module->memories;
  3459. for (i = 0; i < memory_count; i++, memory++)
  3460. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3461. return false;
  3462. }
  3463. if (p != p_end) {
  3464. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3465. return false;
  3466. }
  3467. LOG_VERBOSE("Load memory section success.\n");
  3468. return true;
  3469. fail:
  3470. return false;
  3471. }
  3472. static bool
  3473. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3474. char *error_buf, uint32 error_buf_size)
  3475. {
  3476. const uint8 *p = buf, *p_end = buf_end;
  3477. uint32 global_count, i;
  3478. uint64 total_size;
  3479. WASMGlobal *global;
  3480. uint8 mutable;
  3481. #if WASM_ENABLE_GC != 0
  3482. bool need_ref_type_map;
  3483. WASMRefType ref_type;
  3484. #endif
  3485. read_leb_uint32(p, p_end, global_count);
  3486. module->global_count = 0;
  3487. if (global_count) {
  3488. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3489. if (!(module->globals =
  3490. loader_malloc(total_size, error_buf, error_buf_size))) {
  3491. return false;
  3492. }
  3493. global = module->globals;
  3494. for (i = 0; i < global_count; i++, global++) {
  3495. #if WASM_ENABLE_GC == 0
  3496. CHECK_BUF(p, p_end, 2);
  3497. global->type = read_uint8(p);
  3498. mutable = read_uint8(p);
  3499. #else
  3500. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  3501. &ref_type, false, error_buf,
  3502. error_buf_size)) {
  3503. return false;
  3504. }
  3505. global->type = ref_type.ref_type;
  3506. CHECK_BUF(p, p_end, 1);
  3507. mutable = read_uint8(p);
  3508. #endif /* end of WASM_ENABLE_GC */
  3509. #if WASM_ENABLE_WAMR_COMPILER != 0
  3510. if (global->type == VALUE_TYPE_V128)
  3511. module->is_simd_used = true;
  3512. else if (global->type == VALUE_TYPE_FUNCREF
  3513. || global->type == VALUE_TYPE_EXTERNREF)
  3514. module->is_ref_types_used = true;
  3515. #endif
  3516. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3517. return false;
  3518. }
  3519. global->is_mutable = mutable ? true : false;
  3520. /* initialize expression */
  3521. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3522. global->type,
  3523. #if WASM_ENABLE_GC == 0
  3524. NULL,
  3525. #else
  3526. &ref_type,
  3527. #endif
  3528. error_buf, error_buf_size))
  3529. return false;
  3530. #if WASM_ENABLE_GC != 0
  3531. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3532. uint8 global_type;
  3533. WASMRefType *global_ref_type;
  3534. uint32 global_idx = global->init_expr.u.global_index;
  3535. if (global->init_expr.u.global_index
  3536. >= module->import_global_count + i) {
  3537. set_error_buf(error_buf, error_buf_size, "unknown global");
  3538. return false;
  3539. }
  3540. if (global_idx < module->import_global_count) {
  3541. global_type =
  3542. module->import_globals[global_idx].u.global.type;
  3543. global_ref_type =
  3544. module->import_globals[global_idx].u.global.ref_type;
  3545. }
  3546. else {
  3547. global_type =
  3548. module
  3549. ->globals[global_idx - module->import_global_count]
  3550. .type;
  3551. global_ref_type =
  3552. module
  3553. ->globals[global_idx - module->import_global_count]
  3554. .ref_type;
  3555. }
  3556. if (!wasm_reftype_is_subtype_of(
  3557. global_type, global_ref_type, global->type,
  3558. global->ref_type, module->types, module->type_count)) {
  3559. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3560. return false;
  3561. }
  3562. }
  3563. if (need_ref_type_map) {
  3564. if (!(global->ref_type =
  3565. reftype_set_insert(module->ref_type_set, &ref_type,
  3566. error_buf, error_buf_size))) {
  3567. return false;
  3568. }
  3569. }
  3570. #if TRACE_WASM_LOADER != 0
  3571. os_printf("global type: ");
  3572. wasm_dump_value_type(global->type, global->ref_type);
  3573. os_printf("\n");
  3574. #endif
  3575. #endif
  3576. module->global_count++;
  3577. }
  3578. bh_assert(module->global_count == global_count);
  3579. }
  3580. if (p != p_end) {
  3581. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3582. return false;
  3583. }
  3584. LOG_VERBOSE("Load global section success.\n");
  3585. return true;
  3586. fail:
  3587. return false;
  3588. }
  3589. static bool
  3590. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3591. bool is_load_from_file_buf, char *error_buf,
  3592. uint32 error_buf_size)
  3593. {
  3594. const uint8 *p = buf, *p_end = buf_end;
  3595. uint32 export_count, i, j, index;
  3596. uint64 total_size;
  3597. uint32 str_len;
  3598. WASMExport *export;
  3599. const char *name;
  3600. read_leb_uint32(p, p_end, export_count);
  3601. if (export_count) {
  3602. module->export_count = export_count;
  3603. total_size = sizeof(WASMExport) * (uint64)export_count;
  3604. if (!(module->exports =
  3605. loader_malloc(total_size, error_buf, error_buf_size))) {
  3606. return false;
  3607. }
  3608. export = module->exports;
  3609. for (i = 0; i < export_count; i++, export ++) {
  3610. #if WASM_ENABLE_THREAD_MGR == 0
  3611. if (p == p_end) {
  3612. /* export section with inconsistent count:
  3613. n export declared, but less than n given */
  3614. set_error_buf(error_buf, error_buf_size,
  3615. "length out of bounds");
  3616. return false;
  3617. }
  3618. #endif
  3619. read_leb_uint32(p, p_end, str_len);
  3620. CHECK_BUF(p, p_end, str_len);
  3621. for (j = 0; j < i; j++) {
  3622. name = module->exports[j].name;
  3623. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3624. set_error_buf(error_buf, error_buf_size,
  3625. "duplicate export name");
  3626. return false;
  3627. }
  3628. }
  3629. if (!(export->name = wasm_const_str_list_insert(
  3630. p, str_len, module, is_load_from_file_buf, error_buf,
  3631. error_buf_size))) {
  3632. return false;
  3633. }
  3634. p += str_len;
  3635. CHECK_BUF(p, p_end, 1);
  3636. export->kind = read_uint8(p);
  3637. read_leb_uint32(p, p_end, index);
  3638. export->index = index;
  3639. switch (export->kind) {
  3640. /* function index */
  3641. case EXPORT_KIND_FUNC:
  3642. if (index >= module->function_count
  3643. + module->import_function_count) {
  3644. set_error_buf(error_buf, error_buf_size,
  3645. "unknown function");
  3646. return false;
  3647. }
  3648. #if WASM_ENABLE_SIMD != 0
  3649. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3650. /* TODO: check func type, if it has v128 param or result,
  3651. report error */
  3652. #endif
  3653. #endif
  3654. break;
  3655. /* table index */
  3656. case EXPORT_KIND_TABLE:
  3657. if (index
  3658. >= module->table_count + module->import_table_count) {
  3659. set_error_buf(error_buf, error_buf_size,
  3660. "unknown table");
  3661. return false;
  3662. }
  3663. break;
  3664. /* memory index */
  3665. case EXPORT_KIND_MEMORY:
  3666. if (index
  3667. >= module->memory_count + module->import_memory_count) {
  3668. set_error_buf(error_buf, error_buf_size,
  3669. "unknown memory");
  3670. return false;
  3671. }
  3672. break;
  3673. #if WASM_ENABLE_TAGS != 0
  3674. /* export tag */
  3675. case EXPORT_KIND_TAG:
  3676. if (index >= module->tag_count + module->import_tag_count) {
  3677. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3678. return false;
  3679. }
  3680. break;
  3681. #endif
  3682. /* global index */
  3683. case EXPORT_KIND_GLOBAL:
  3684. if (index
  3685. >= module->global_count + module->import_global_count) {
  3686. set_error_buf(error_buf, error_buf_size,
  3687. "unknown global");
  3688. return false;
  3689. }
  3690. break;
  3691. default:
  3692. set_error_buf(error_buf, error_buf_size,
  3693. "invalid export kind");
  3694. return false;
  3695. }
  3696. }
  3697. }
  3698. if (p != p_end) {
  3699. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3700. return false;
  3701. }
  3702. LOG_VERBOSE("Load export section success.\n");
  3703. return true;
  3704. fail:
  3705. return false;
  3706. }
  3707. static bool
  3708. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3709. uint32 error_buf_size)
  3710. {
  3711. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3712. if (table_index != 0) {
  3713. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3714. return false;
  3715. }
  3716. #endif
  3717. if (table_index >= module->import_table_count + module->table_count) {
  3718. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3719. table_index);
  3720. return false;
  3721. }
  3722. return true;
  3723. }
  3724. static bool
  3725. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3726. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3727. {
  3728. const uint8 *p = *p_buf, *p_end = buf_end;
  3729. uint32 table_index;
  3730. read_leb_uint32(p, p_end, table_index);
  3731. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3732. return false;
  3733. }
  3734. *p_table_index = table_index;
  3735. *p_buf = p;
  3736. return true;
  3737. fail:
  3738. return false;
  3739. }
  3740. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3741. static bool
  3742. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3743. uint32 *p_elem_type,
  3744. #if WASM_ENABLE_GC != 0
  3745. WASMRefType **p_elem_ref_type,
  3746. #endif
  3747. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3748. {
  3749. const uint8 *p = *p_buf, *p_end = buf_end;
  3750. uint8 elem_type;
  3751. #if WASM_ENABLE_GC != 0
  3752. WASMRefType elem_ref_type;
  3753. bool need_ref_type_map;
  3754. #endif
  3755. CHECK_BUF(p, p_end, 1);
  3756. elem_type = read_uint8(p);
  3757. if (elemkind_zero) {
  3758. if (elem_type != 0) {
  3759. set_error_buf(error_buf, error_buf_size,
  3760. "invalid reference type or unknown type");
  3761. return false;
  3762. }
  3763. else {
  3764. *p_elem_type = VALUE_TYPE_FUNCREF;
  3765. *p_buf = p;
  3766. return true;
  3767. }
  3768. }
  3769. #if WASM_ENABLE_GC == 0
  3770. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3771. set_error_buf(error_buf, error_buf_size,
  3772. "invalid reference type or unknown type");
  3773. return false;
  3774. }
  3775. *p_elem_type = elem_type;
  3776. #else
  3777. p--;
  3778. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3779. &need_ref_type_map, &elem_ref_type, false,
  3780. error_buf, error_buf_size)) {
  3781. return false;
  3782. }
  3783. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3784. set_error_buf(error_buf, error_buf_size,
  3785. "invalid reference type or unknown type");
  3786. return false;
  3787. }
  3788. *p_elem_type = elem_ref_type.ref_type;
  3789. if (need_ref_type_map) {
  3790. if (!(*p_elem_ref_type =
  3791. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3792. error_buf, error_buf_size))) {
  3793. return false;
  3794. }
  3795. }
  3796. #endif
  3797. *p_buf = p;
  3798. return true;
  3799. fail:
  3800. return false;
  3801. }
  3802. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3803. static bool
  3804. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3805. WASMModule *module, WASMTableSeg *table_segment,
  3806. char *error_buf, uint32 error_buf_size)
  3807. {
  3808. const uint8 *p = *p_buf, *p_end = buf_end;
  3809. uint32 function_count, function_index = 0, i;
  3810. uint64 total_size;
  3811. read_leb_uint32(p, p_end, function_count);
  3812. table_segment->value_count = function_count;
  3813. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3814. if (total_size > 0
  3815. && !(table_segment->init_values =
  3816. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3817. error_buf_size))) {
  3818. return false;
  3819. }
  3820. for (i = 0; i < function_count; i++) {
  3821. InitializerExpression *init_expr = &table_segment->init_values[i];
  3822. read_leb_uint32(p, p_end, function_index);
  3823. if (!check_function_index(module, function_index, error_buf,
  3824. error_buf_size)) {
  3825. return false;
  3826. }
  3827. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3828. init_expr->u.ref_index = function_index;
  3829. }
  3830. *p_buf = p;
  3831. return true;
  3832. fail:
  3833. return false;
  3834. }
  3835. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3836. static bool
  3837. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3838. WASMModule *module, WASMTableSeg *table_segment,
  3839. char *error_buf, uint32 error_buf_size)
  3840. {
  3841. const uint8 *p = *p_buf, *p_end = buf_end;
  3842. uint32 ref_count, i;
  3843. uint64 total_size;
  3844. read_leb_uint32(p, p_end, ref_count);
  3845. table_segment->value_count = ref_count;
  3846. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3847. if (total_size > 0
  3848. && !(table_segment->init_values =
  3849. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3850. error_buf_size))) {
  3851. return false;
  3852. }
  3853. for (i = 0; i < ref_count; i++) {
  3854. InitializerExpression *init_expr = &table_segment->init_values[i];
  3855. if (!load_init_expr(module, &p, p_end, init_expr,
  3856. table_segment->elem_type,
  3857. #if WASM_ENABLE_GC == 0
  3858. NULL,
  3859. #else
  3860. table_segment->elem_ref_type,
  3861. #endif
  3862. error_buf, error_buf_size))
  3863. return false;
  3864. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3865. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3866. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3867. && init_expr->init_expr_type
  3868. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3869. }
  3870. *p_buf = p;
  3871. return true;
  3872. fail:
  3873. return false;
  3874. }
  3875. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3876. static bool
  3877. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3878. WASMModule *module, char *error_buf,
  3879. uint32 error_buf_size)
  3880. {
  3881. const uint8 *p = buf, *p_end = buf_end;
  3882. uint32 table_segment_count, i;
  3883. uint64 total_size;
  3884. WASMTableSeg *table_segment;
  3885. read_leb_uint32(p, p_end, table_segment_count);
  3886. if (table_segment_count) {
  3887. module->table_seg_count = table_segment_count;
  3888. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3889. if (!(module->table_segments =
  3890. loader_malloc(total_size, error_buf, error_buf_size))) {
  3891. return false;
  3892. }
  3893. table_segment = module->table_segments;
  3894. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3895. if (p >= p_end) {
  3896. set_error_buf(error_buf, error_buf_size,
  3897. "invalid value type or "
  3898. "invalid elements segment kind");
  3899. return false;
  3900. }
  3901. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3902. read_leb_uint32(p, p_end, table_segment->mode);
  3903. /* last three bits */
  3904. table_segment->mode = table_segment->mode & 0x07;
  3905. switch (table_segment->mode) {
  3906. /* elemkind/elemtype + active */
  3907. case 0:
  3908. case 4:
  3909. {
  3910. #if WASM_ENABLE_GC != 0
  3911. if (table_segment->mode == 0) {
  3912. /* vec(funcidx), set elem type to (ref func) */
  3913. WASMRefType elem_ref_type = { 0 };
  3914. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  3915. wasm_set_refheaptype_common(
  3916. &elem_ref_type.ref_ht_common, false,
  3917. HEAP_TYPE_FUNC);
  3918. if (!(table_segment->elem_ref_type = reftype_set_insert(
  3919. module->ref_type_set, &elem_ref_type,
  3920. error_buf, error_buf_size)))
  3921. return false;
  3922. }
  3923. else {
  3924. /* vec(expr), set elem type to funcref */
  3925. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3926. }
  3927. #else
  3928. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3929. #endif
  3930. table_segment->table_index = 0;
  3931. if (!check_table_index(module, table_segment->table_index,
  3932. error_buf, error_buf_size))
  3933. return false;
  3934. if (!load_init_expr(
  3935. module, &p, p_end, &table_segment->base_offset,
  3936. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3937. return false;
  3938. if (table_segment->mode == 0) {
  3939. /* vec(funcidx) */
  3940. if (!load_func_index_vec(&p, p_end, module,
  3941. table_segment, error_buf,
  3942. error_buf_size))
  3943. return false;
  3944. }
  3945. else {
  3946. /* vec(expr) */
  3947. if (!load_init_expr_vec(&p, p_end, module,
  3948. table_segment, error_buf,
  3949. error_buf_size))
  3950. return false;
  3951. }
  3952. break;
  3953. }
  3954. /* elemkind + passive/declarative */
  3955. case 1:
  3956. case 3:
  3957. if (!load_elem_type(module, &p, p_end,
  3958. &table_segment->elem_type,
  3959. #if WASM_ENABLE_GC != 0
  3960. &table_segment->elem_ref_type,
  3961. #endif
  3962. true, error_buf, error_buf_size))
  3963. return false;
  3964. /* vec(funcidx) */
  3965. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3966. error_buf, error_buf_size))
  3967. return false;
  3968. break;
  3969. /* elemkind/elemtype + table_idx + active */
  3970. case 2:
  3971. case 6:
  3972. if (!load_table_index(&p, p_end, module,
  3973. &table_segment->table_index,
  3974. error_buf, error_buf_size))
  3975. return false;
  3976. if (!load_init_expr(
  3977. module, &p, p_end, &table_segment->base_offset,
  3978. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3979. return false;
  3980. if (!load_elem_type(module, &p, p_end,
  3981. &table_segment->elem_type,
  3982. #if WASM_ENABLE_GC != 0
  3983. &table_segment->elem_ref_type,
  3984. #endif
  3985. table_segment->mode == 2 ? true : false,
  3986. error_buf, error_buf_size))
  3987. return false;
  3988. if (table_segment->mode == 2) {
  3989. /* vec(funcidx) */
  3990. if (!load_func_index_vec(&p, p_end, module,
  3991. table_segment, error_buf,
  3992. error_buf_size))
  3993. return false;
  3994. }
  3995. else {
  3996. /* vec(expr) */
  3997. if (!load_init_expr_vec(&p, p_end, module,
  3998. table_segment, error_buf,
  3999. error_buf_size))
  4000. return false;
  4001. }
  4002. break;
  4003. case 5:
  4004. case 7:
  4005. if (!load_elem_type(module, &p, p_end,
  4006. &table_segment->elem_type,
  4007. #if WASM_ENABLE_GC != 0
  4008. &table_segment->elem_ref_type,
  4009. #endif
  4010. false, error_buf, error_buf_size))
  4011. return false;
  4012. /* vec(expr) */
  4013. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4014. error_buf, error_buf_size))
  4015. return false;
  4016. break;
  4017. default:
  4018. set_error_buf(error_buf, error_buf_size,
  4019. "unknown element segment kind");
  4020. return false;
  4021. }
  4022. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4023. /*
  4024. * like: 00 41 05 0b 04 00 01 00 01
  4025. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4026. */
  4027. if (!load_table_index(&p, p_end, module,
  4028. &table_segment->table_index, error_buf,
  4029. error_buf_size))
  4030. return false;
  4031. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4032. VALUE_TYPE_I32, NULL, error_buf,
  4033. error_buf_size))
  4034. return false;
  4035. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4036. error_buf, error_buf_size))
  4037. return false;
  4038. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4039. #if WASM_ENABLE_WAMR_COMPILER != 0
  4040. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4041. module->is_ref_types_used = true;
  4042. #endif
  4043. }
  4044. }
  4045. if (p != p_end) {
  4046. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4047. return false;
  4048. }
  4049. LOG_VERBOSE("Load table segment section success.\n");
  4050. return true;
  4051. fail:
  4052. return false;
  4053. }
  4054. static bool
  4055. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4056. WASMModule *module, char *error_buf,
  4057. uint32 error_buf_size)
  4058. {
  4059. const uint8 *p = buf, *p_end = buf_end;
  4060. uint32 data_seg_count, i, mem_index, data_seg_len;
  4061. uint64 total_size;
  4062. WASMDataSeg *dataseg;
  4063. InitializerExpression init_expr;
  4064. #if WASM_ENABLE_BULK_MEMORY != 0
  4065. bool is_passive = false;
  4066. uint32 mem_flag;
  4067. #endif
  4068. uint8 mem_offset_type;
  4069. read_leb_uint32(p, p_end, data_seg_count);
  4070. #if WASM_ENABLE_BULK_MEMORY != 0
  4071. if ((module->data_seg_count1 != 0)
  4072. && (data_seg_count != module->data_seg_count1)) {
  4073. set_error_buf(error_buf, error_buf_size,
  4074. "data count and data section have inconsistent lengths");
  4075. return false;
  4076. }
  4077. #endif
  4078. if (data_seg_count) {
  4079. module->data_seg_count = data_seg_count;
  4080. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4081. if (!(module->data_segments =
  4082. loader_malloc(total_size, error_buf, error_buf_size))) {
  4083. return false;
  4084. }
  4085. for (i = 0; i < data_seg_count; i++) {
  4086. read_leb_uint32(p, p_end, mem_index);
  4087. #if WASM_ENABLE_BULK_MEMORY != 0
  4088. is_passive = false;
  4089. mem_flag = mem_index & 0x03;
  4090. switch (mem_flag) {
  4091. case 0x01:
  4092. is_passive = true;
  4093. #if WASM_ENABLE_WAMR_COMPILER != 0
  4094. module->is_bulk_memory_used = true;
  4095. #endif
  4096. break;
  4097. case 0x00:
  4098. /* no memory index, treat index as 0 */
  4099. mem_index = 0;
  4100. goto check_mem_index;
  4101. case 0x02:
  4102. /* read following memory index */
  4103. read_leb_uint32(p, p_end, mem_index);
  4104. #if WASM_ENABLE_WAMR_COMPILER != 0
  4105. module->is_bulk_memory_used = true;
  4106. #endif
  4107. check_mem_index:
  4108. if (mem_index
  4109. >= module->import_memory_count + module->memory_count) {
  4110. set_error_buf_v(error_buf, error_buf_size,
  4111. "unknown memory %d", mem_index);
  4112. return false;
  4113. }
  4114. break;
  4115. case 0x03:
  4116. default:
  4117. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4118. return false;
  4119. break;
  4120. }
  4121. #else
  4122. if (mem_index
  4123. >= module->import_memory_count + module->memory_count) {
  4124. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4125. mem_index);
  4126. return false;
  4127. }
  4128. #endif /* WASM_ENABLE_BULK_MEMORY */
  4129. #if WASM_ENABLE_BULK_MEMORY != 0
  4130. if (!is_passive)
  4131. #endif
  4132. {
  4133. #if WASM_ENABLE_MEMORY64 != 0
  4134. /* This memory_flag is from memory instead of data segment */
  4135. uint8 memory_flag;
  4136. if (module->import_memory_count > 0) {
  4137. memory_flag =
  4138. module->import_memories[mem_index].u.memory.flags;
  4139. }
  4140. else {
  4141. memory_flag =
  4142. module
  4143. ->memories[mem_index - module->import_memory_count]
  4144. .flags;
  4145. }
  4146. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4147. : VALUE_TYPE_I32;
  4148. #else
  4149. mem_offset_type = VALUE_TYPE_I32;
  4150. #endif
  4151. }
  4152. #if WASM_ENABLE_BULK_MEMORY != 0
  4153. if (!is_passive)
  4154. #endif
  4155. if (!load_init_expr(module, &p, p_end, &init_expr,
  4156. mem_offset_type, NULL, error_buf,
  4157. error_buf_size))
  4158. return false;
  4159. read_leb_uint32(p, p_end, data_seg_len);
  4160. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4161. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4162. return false;
  4163. }
  4164. #if WASM_ENABLE_BULK_MEMORY != 0
  4165. dataseg->is_passive = is_passive;
  4166. if (!is_passive)
  4167. #endif
  4168. {
  4169. bh_memcpy_s(&dataseg->base_offset,
  4170. sizeof(InitializerExpression), &init_expr,
  4171. sizeof(InitializerExpression));
  4172. dataseg->memory_index = mem_index;
  4173. }
  4174. dataseg->data_length = data_seg_len;
  4175. CHECK_BUF(p, p_end, data_seg_len);
  4176. dataseg->data = (uint8 *)p;
  4177. p += data_seg_len;
  4178. }
  4179. }
  4180. if (p != p_end) {
  4181. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4182. return false;
  4183. }
  4184. LOG_VERBOSE("Load data segment section success.\n");
  4185. return true;
  4186. fail:
  4187. return false;
  4188. }
  4189. #if WASM_ENABLE_BULK_MEMORY != 0
  4190. static bool
  4191. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4192. WASMModule *module, char *error_buf,
  4193. uint32 error_buf_size)
  4194. {
  4195. const uint8 *p = buf, *p_end = buf_end;
  4196. uint32 data_seg_count1 = 0;
  4197. read_leb_uint32(p, p_end, data_seg_count1);
  4198. module->data_seg_count1 = data_seg_count1;
  4199. if (p != p_end) {
  4200. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4201. return false;
  4202. }
  4203. #if WASM_ENABLE_WAMR_COMPILER != 0
  4204. module->is_bulk_memory_used = true;
  4205. #endif
  4206. LOG_VERBOSE("Load datacount section success.\n");
  4207. return true;
  4208. fail:
  4209. return false;
  4210. }
  4211. #endif
  4212. #if WASM_ENABLE_TAGS != 0
  4213. static bool
  4214. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4215. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4216. uint32 error_buf_size)
  4217. {
  4218. (void)buf_code;
  4219. (void)buf_code_end;
  4220. const uint8 *p = buf, *p_end = buf_end;
  4221. size_t total_size = 0;
  4222. /* number of tags defined in the section */
  4223. uint32 section_tag_count = 0;
  4224. uint8 tag_attribute;
  4225. uint32 tag_type;
  4226. WASMTag *tag = NULL;
  4227. /* get tag count */
  4228. read_leb_uint32(p, p_end, section_tag_count);
  4229. module->tag_count = section_tag_count;
  4230. if (section_tag_count) {
  4231. total_size = sizeof(WASMTag *) * module->tag_count;
  4232. if (!(module->tags =
  4233. loader_malloc(total_size, error_buf, error_buf_size))) {
  4234. return false;
  4235. }
  4236. /* load each tag, imported tags precede the tags */
  4237. uint32 tag_index;
  4238. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4239. /* get the one byte attribute */
  4240. CHECK_BUF(p, p_end, 1);
  4241. tag_attribute = read_uint8(p);
  4242. /* get type */
  4243. read_leb_uint32(p, p_end, tag_type);
  4244. /* compare against module->types */
  4245. if (tag_type >= module->type_count) {
  4246. set_error_buf(error_buf, error_buf_size, "unknown type");
  4247. return false;
  4248. }
  4249. /* get return type (must be 0) */
  4250. /* check, that the type of the referred tag returns void */
  4251. WASMType *func_type = (WASMType *)module->types[tag_type];
  4252. if (func_type->result_count != 0) {
  4253. set_error_buf(error_buf, error_buf_size,
  4254. "non-empty tag result type");
  4255. goto fail;
  4256. }
  4257. if (!(tag = module->tags[tag_index] = loader_malloc(
  4258. sizeof(WASMTag), error_buf, error_buf_size))) {
  4259. return false;
  4260. }
  4261. /* store to module tag declarations */
  4262. tag->attribute = tag_attribute;
  4263. tag->type = tag_type;
  4264. tag->tag_type = func_type;
  4265. }
  4266. }
  4267. if (p != p_end) {
  4268. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4269. return false;
  4270. }
  4271. LOG_VERBOSE("Load tag section success.\n");
  4272. return true;
  4273. fail:
  4274. return false;
  4275. }
  4276. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4277. static bool
  4278. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4279. const uint8 *buf_func_end, WASMModule *module,
  4280. char *error_buf, uint32 error_buf_size)
  4281. {
  4282. const uint8 *p = buf, *p_end = buf_end;
  4283. const uint8 *p_func = buf_func;
  4284. uint32 func_count = 0, code_count;
  4285. /* code has been loaded in function section, so pass it here, just check
  4286. * whether function and code section have inconsistent lengths */
  4287. read_leb_uint32(p, p_end, code_count);
  4288. if (buf_func)
  4289. read_leb_uint32(p_func, buf_func_end, func_count);
  4290. if (func_count != code_count) {
  4291. set_error_buf(error_buf, error_buf_size,
  4292. "function and code section have inconsistent lengths");
  4293. return false;
  4294. }
  4295. LOG_VERBOSE("Load code segment section success.\n");
  4296. (void)module;
  4297. return true;
  4298. fail:
  4299. return false;
  4300. }
  4301. static bool
  4302. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4303. char *error_buf, uint32 error_buf_size)
  4304. {
  4305. const uint8 *p = buf, *p_end = buf_end;
  4306. WASMFuncType *type;
  4307. uint32 start_function;
  4308. read_leb_uint32(p, p_end, start_function);
  4309. if (start_function
  4310. >= module->function_count + module->import_function_count) {
  4311. set_error_buf(error_buf, error_buf_size, "unknown function");
  4312. return false;
  4313. }
  4314. if (start_function < module->import_function_count)
  4315. type = module->import_functions[start_function].u.function.func_type;
  4316. else
  4317. type = module->functions[start_function - module->import_function_count]
  4318. ->func_type;
  4319. if (type->param_count != 0 || type->result_count != 0) {
  4320. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4321. return false;
  4322. }
  4323. module->start_function = start_function;
  4324. if (p != p_end) {
  4325. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4326. return false;
  4327. }
  4328. LOG_VERBOSE("Load start section success.\n");
  4329. return true;
  4330. fail:
  4331. return false;
  4332. }
  4333. #if WASM_ENABLE_STRINGREF != 0
  4334. static bool
  4335. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4336. WASMModule *module, bool is_load_from_file_buf,
  4337. char *error_buf, uint32 error_buf_size)
  4338. {
  4339. const uint8 *p = buf, *p_end = buf_end;
  4340. int32 deferred_count, immediate_count, string_length, i;
  4341. uint64 total_size;
  4342. read_leb_uint32(p, p_end, deferred_count);
  4343. read_leb_uint32(p, p_end, immediate_count);
  4344. /* proposal set deferred_count for future extension */
  4345. if (deferred_count != 0) {
  4346. goto fail;
  4347. }
  4348. if (immediate_count > 0) {
  4349. total_size = sizeof(char *) * (uint64)immediate_count;
  4350. if (!(module->string_literal_ptrs =
  4351. loader_malloc(total_size, error_buf, error_buf_size))) {
  4352. goto fail;
  4353. }
  4354. module->string_literal_count = immediate_count;
  4355. total_size = sizeof(uint32) * (uint64)immediate_count;
  4356. if (!(module->string_literal_lengths =
  4357. loader_malloc(total_size, error_buf, error_buf_size))) {
  4358. goto fail;
  4359. }
  4360. for (i = 0; i < immediate_count; i++) {
  4361. read_leb_uint32(p, p_end, string_length);
  4362. CHECK_BUF(p, p_end, string_length);
  4363. module->string_literal_ptrs[i] = p;
  4364. module->string_literal_lengths[i] = string_length;
  4365. p += string_length;
  4366. }
  4367. }
  4368. if (p != p_end) {
  4369. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4370. goto fail;
  4371. }
  4372. LOG_VERBOSE("Load stringref section success.\n");
  4373. return true;
  4374. fail:
  4375. return false;
  4376. }
  4377. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4378. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4379. static bool
  4380. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4381. bool is_load_from_file_buf, char *error_buf,
  4382. uint32 error_buf_size)
  4383. {
  4384. const uint8 *p = buf, *p_end = buf_end;
  4385. uint32 name_type, subsection_size;
  4386. uint32 previous_name_type = 0;
  4387. uint32 num_func_name;
  4388. uint32 func_index;
  4389. uint32 previous_func_index = ~0U;
  4390. uint32 func_name_len;
  4391. uint32 name_index;
  4392. int i = 0;
  4393. if (p >= p_end) {
  4394. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4395. return false;
  4396. }
  4397. while (p < p_end) {
  4398. read_leb_uint32(p, p_end, name_type);
  4399. if (i != 0) {
  4400. if (name_type == previous_name_type) {
  4401. set_error_buf(error_buf, error_buf_size,
  4402. "duplicate sub-section");
  4403. return false;
  4404. }
  4405. if (name_type < previous_name_type) {
  4406. set_error_buf(error_buf, error_buf_size,
  4407. "out-of-order sub-section");
  4408. return false;
  4409. }
  4410. }
  4411. previous_name_type = name_type;
  4412. read_leb_uint32(p, p_end, subsection_size);
  4413. CHECK_BUF(p, p_end, subsection_size);
  4414. switch (name_type) {
  4415. case SUB_SECTION_TYPE_FUNC:
  4416. if (subsection_size) {
  4417. read_leb_uint32(p, p_end, num_func_name);
  4418. for (name_index = 0; name_index < num_func_name;
  4419. name_index++) {
  4420. read_leb_uint32(p, p_end, func_index);
  4421. if (func_index == previous_func_index) {
  4422. set_error_buf(error_buf, error_buf_size,
  4423. "duplicate function name");
  4424. return false;
  4425. }
  4426. if (func_index < previous_func_index
  4427. && previous_func_index != ~0U) {
  4428. set_error_buf(error_buf, error_buf_size,
  4429. "out-of-order function index ");
  4430. return false;
  4431. }
  4432. previous_func_index = func_index;
  4433. read_leb_uint32(p, p_end, func_name_len);
  4434. CHECK_BUF(p, p_end, func_name_len);
  4435. /* Skip the import functions */
  4436. if (func_index >= module->import_function_count) {
  4437. func_index -= module->import_function_count;
  4438. if (func_index >= module->function_count) {
  4439. set_error_buf(error_buf, error_buf_size,
  4440. "out-of-range function index");
  4441. return false;
  4442. }
  4443. if (!(module->functions[func_index]->field_name =
  4444. wasm_const_str_list_insert(
  4445. p, func_name_len, module,
  4446. #if WASM_ENABLE_WAMR_COMPILER != 0
  4447. false,
  4448. #else
  4449. is_load_from_file_buf,
  4450. #endif
  4451. error_buf, error_buf_size))) {
  4452. return false;
  4453. }
  4454. }
  4455. p += func_name_len;
  4456. }
  4457. }
  4458. break;
  4459. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4460. */
  4461. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4462. default:
  4463. p = p + subsection_size;
  4464. break;
  4465. }
  4466. i++;
  4467. }
  4468. return true;
  4469. fail:
  4470. return false;
  4471. }
  4472. #endif
  4473. static bool
  4474. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4475. bool is_load_from_file_buf, char *error_buf,
  4476. uint32 error_buf_size)
  4477. {
  4478. const uint8 *p = buf, *p_end = buf_end;
  4479. char section_name[32];
  4480. uint32 name_len, buffer_len;
  4481. if (p >= p_end) {
  4482. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4483. return false;
  4484. }
  4485. read_leb_uint32(p, p_end, name_len);
  4486. if (p + name_len > p_end) {
  4487. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4488. return false;
  4489. }
  4490. if (!wasm_check_utf8_str(p, name_len)) {
  4491. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4492. return false;
  4493. }
  4494. buffer_len = sizeof(section_name);
  4495. memset(section_name, 0, buffer_len);
  4496. if (name_len < buffer_len) {
  4497. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4498. }
  4499. else {
  4500. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4501. memset(section_name + buffer_len - 4, '.', 3);
  4502. }
  4503. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4504. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4505. module->name_section_buf = buf;
  4506. module->name_section_buf_end = buf_end;
  4507. p += name_len;
  4508. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  4509. error_buf_size);
  4510. LOG_VERBOSE("Load custom name section success.");
  4511. return true;
  4512. }
  4513. #endif
  4514. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4515. {
  4516. WASMCustomSection *section =
  4517. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4518. if (!section) {
  4519. return false;
  4520. }
  4521. section->name_addr = (char *)p;
  4522. section->name_len = name_len;
  4523. section->content_addr = (uint8 *)(p + name_len);
  4524. section->content_len = (uint32)(p_end - p - name_len);
  4525. section->next = module->custom_section_list;
  4526. module->custom_section_list = section;
  4527. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4528. return true;
  4529. }
  4530. #endif
  4531. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4532. (void)is_load_from_file_buf;
  4533. (void)module;
  4534. return true;
  4535. fail:
  4536. return false;
  4537. }
  4538. static void
  4539. calculate_global_data_offset(WASMModule *module)
  4540. {
  4541. uint32 i, data_offset;
  4542. data_offset = 0;
  4543. for (i = 0; i < module->import_global_count; i++) {
  4544. WASMGlobalImport *import_global =
  4545. &((module->import_globals + i)->u.global);
  4546. #if WASM_ENABLE_FAST_JIT != 0
  4547. import_global->data_offset = data_offset;
  4548. #endif
  4549. data_offset += wasm_value_type_size(import_global->type);
  4550. }
  4551. for (i = 0; i < module->global_count; i++) {
  4552. WASMGlobal *global = module->globals + i;
  4553. #if WASM_ENABLE_FAST_JIT != 0
  4554. global->data_offset = data_offset;
  4555. #endif
  4556. data_offset += wasm_value_type_size(global->type);
  4557. }
  4558. module->global_data_size = data_offset;
  4559. }
  4560. #if WASM_ENABLE_FAST_JIT != 0
  4561. static bool
  4562. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4563. uint32 error_buf_size)
  4564. {
  4565. #if WASM_ENABLE_LAZY_JIT != 0
  4566. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4567. #endif
  4568. uint32 i;
  4569. if (!module->function_count)
  4570. return true;
  4571. if (!(module->fast_jit_func_ptrs =
  4572. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4573. error_buf_size))) {
  4574. return false;
  4575. }
  4576. #if WASM_ENABLE_LAZY_JIT != 0
  4577. for (i = 0; i < module->function_count; i++) {
  4578. module->fast_jit_func_ptrs[i] =
  4579. jit_globals->compile_fast_jit_and_then_call;
  4580. }
  4581. #endif
  4582. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4583. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4584. set_error_buf(error_buf, error_buf_size,
  4585. "init fast jit thread lock failed");
  4586. return false;
  4587. }
  4588. module->fast_jit_thread_locks_inited[i] = true;
  4589. }
  4590. return true;
  4591. }
  4592. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4593. #if WASM_ENABLE_JIT != 0
  4594. static bool
  4595. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4596. uint32 error_buf_size)
  4597. {
  4598. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4599. AOTCompOption option = { 0 };
  4600. char *aot_last_error;
  4601. uint64 size;
  4602. #if WASM_ENABLE_GC != 0
  4603. bool gc_enabled = true;
  4604. #else
  4605. bool gc_enabled = false;
  4606. #endif
  4607. if (module->function_count == 0)
  4608. return true;
  4609. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4610. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4611. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4612. return false;
  4613. }
  4614. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4615. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4616. os_mutex_destroy(&module->tierup_wait_lock);
  4617. return false;
  4618. }
  4619. module->tierup_wait_lock_inited = true;
  4620. #endif
  4621. size = sizeof(void *) * (uint64)module->function_count
  4622. + sizeof(bool) * (uint64)module->function_count;
  4623. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4624. return false;
  4625. }
  4626. module->func_ptrs_compiled =
  4627. (bool *)((uint8 *)module->func_ptrs
  4628. + sizeof(void *) * module->function_count);
  4629. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4630. if (!module->comp_data) {
  4631. aot_last_error = aot_get_last_error();
  4632. bh_assert(aot_last_error != NULL);
  4633. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4634. return false;
  4635. }
  4636. option.is_jit_mode = true;
  4637. option.opt_level = llvm_jit_options->opt_level;
  4638. option.size_level = llvm_jit_options->size_level;
  4639. option.segue_flags = llvm_jit_options->segue_flags;
  4640. option.quick_invoke_c_api_import =
  4641. llvm_jit_options->quick_invoke_c_api_import;
  4642. #if WASM_ENABLE_BULK_MEMORY != 0
  4643. option.enable_bulk_memory = true;
  4644. #endif
  4645. #if WASM_ENABLE_THREAD_MGR != 0
  4646. option.enable_thread_mgr = true;
  4647. #endif
  4648. #if WASM_ENABLE_TAIL_CALL != 0
  4649. option.enable_tail_call = true;
  4650. #endif
  4651. #if WASM_ENABLE_SIMD != 0
  4652. option.enable_simd = true;
  4653. #endif
  4654. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4655. option.enable_ref_types = true;
  4656. #elif WASM_ENABLE_GC != 0
  4657. option.enable_gc = true;
  4658. #endif
  4659. option.enable_aux_stack_check = true;
  4660. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4661. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4662. option.enable_aux_stack_frame = true;
  4663. #endif
  4664. #if WASM_ENABLE_PERF_PROFILING != 0
  4665. option.enable_perf_profiling = true;
  4666. #endif
  4667. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4668. option.enable_memory_profiling = true;
  4669. option.enable_stack_estimation = true;
  4670. #endif
  4671. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4672. if (!module->comp_ctx) {
  4673. aot_last_error = aot_get_last_error();
  4674. bh_assert(aot_last_error != NULL);
  4675. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4676. return false;
  4677. }
  4678. return true;
  4679. }
  4680. static bool
  4681. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4682. uint32 error_buf_size)
  4683. {
  4684. char *aot_last_error;
  4685. uint32 i;
  4686. if (module->function_count == 0)
  4687. return true;
  4688. if (!aot_compile_wasm(module->comp_ctx)) {
  4689. aot_last_error = aot_get_last_error();
  4690. bh_assert(aot_last_error != NULL);
  4691. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4692. return false;
  4693. }
  4694. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4695. if (module->orcjit_stop_compiling)
  4696. return false;
  4697. #endif
  4698. bh_print_time("Begin to lookup llvm jit functions");
  4699. for (i = 0; i < module->function_count; i++) {
  4700. LLVMOrcJITTargetAddress func_addr = 0;
  4701. LLVMErrorRef error;
  4702. char func_name[48];
  4703. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4704. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4705. func_name);
  4706. if (error != LLVMErrorSuccess) {
  4707. char *err_msg = LLVMGetErrorMessage(error);
  4708. set_error_buf_v(error_buf, error_buf_size,
  4709. "failed to compile llvm jit function: %s", err_msg);
  4710. LLVMDisposeErrorMessage(err_msg);
  4711. return false;
  4712. }
  4713. /**
  4714. * No need to lock the func_ptr[func_idx] here as it is basic
  4715. * data type, the load/store for it can be finished by one cpu
  4716. * instruction, and there can be only one cpu instruction
  4717. * loading/storing at the same time.
  4718. */
  4719. module->func_ptrs[i] = (void *)func_addr;
  4720. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4721. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4722. if (module->orcjit_stop_compiling)
  4723. return false;
  4724. #endif
  4725. }
  4726. bh_print_time("End lookup llvm jit functions");
  4727. return true;
  4728. }
  4729. #endif /* end of WASM_ENABLE_JIT != 0 */
  4730. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4731. && WASM_ENABLE_LAZY_JIT != 0
  4732. static void *
  4733. init_llvm_jit_functions_stage2_callback(void *arg)
  4734. {
  4735. WASMModule *module = (WASMModule *)arg;
  4736. char error_buf[128];
  4737. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4738. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4739. module->orcjit_stop_compiling = true;
  4740. return NULL;
  4741. }
  4742. os_mutex_lock(&module->tierup_wait_lock);
  4743. module->llvm_jit_inited = true;
  4744. os_cond_broadcast(&module->tierup_wait_cond);
  4745. os_mutex_unlock(&module->tierup_wait_lock);
  4746. return NULL;
  4747. }
  4748. #endif
  4749. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4750. /* The callback function to compile jit functions */
  4751. static void *
  4752. orcjit_thread_callback(void *arg)
  4753. {
  4754. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4755. #if WASM_ENABLE_JIT != 0
  4756. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4757. #endif
  4758. WASMModule *module = thread_arg->module;
  4759. uint32 group_idx = thread_arg->group_idx;
  4760. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4761. uint32 func_count = module->function_count;
  4762. uint32 i;
  4763. #if WASM_ENABLE_FAST_JIT != 0
  4764. /* Compile fast jit funcitons of this group */
  4765. for (i = group_idx; i < func_count; i += group_stride) {
  4766. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4767. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4768. break;
  4769. }
  4770. if (module->orcjit_stop_compiling) {
  4771. return NULL;
  4772. }
  4773. }
  4774. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4775. os_mutex_lock(&module->tierup_wait_lock);
  4776. module->fast_jit_ready_groups++;
  4777. os_mutex_unlock(&module->tierup_wait_lock);
  4778. #endif
  4779. #endif
  4780. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4781. && WASM_ENABLE_LAZY_JIT != 0
  4782. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4783. for (i = group_idx; i < func_count;
  4784. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4785. uint32 j;
  4786. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4787. if (i + j * group_stride < func_count) {
  4788. if (!jit_compiler_set_call_to_fast_jit(
  4789. module,
  4790. i + j * group_stride + module->import_function_count)) {
  4791. LOG_ERROR(
  4792. "failed to compile call_to_fast_jit for func %u\n",
  4793. i + j * group_stride + module->import_function_count);
  4794. module->orcjit_stop_compiling = true;
  4795. return NULL;
  4796. }
  4797. }
  4798. if (module->orcjit_stop_compiling) {
  4799. return NULL;
  4800. }
  4801. }
  4802. }
  4803. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4804. fast jit functions are compiled */
  4805. os_mutex_lock(&module->tierup_wait_lock);
  4806. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4807. && module->fast_jit_ready_groups >= group_stride)) {
  4808. os_cond_reltimedwait(&module->tierup_wait_cond,
  4809. &module->tierup_wait_lock, 10000);
  4810. if (module->orcjit_stop_compiling) {
  4811. /* init_llvm_jit_functions_stage2 failed */
  4812. os_mutex_unlock(&module->tierup_wait_lock);
  4813. return NULL;
  4814. }
  4815. }
  4816. os_mutex_unlock(&module->tierup_wait_lock);
  4817. #endif
  4818. #if WASM_ENABLE_JIT != 0
  4819. /* Compile llvm jit functions of this group */
  4820. for (i = group_idx; i < func_count;
  4821. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4822. LLVMOrcJITTargetAddress func_addr = 0;
  4823. LLVMErrorRef error;
  4824. char func_name[48];
  4825. typedef void (*F)(void);
  4826. union {
  4827. F f;
  4828. void *v;
  4829. } u;
  4830. uint32 j;
  4831. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4832. "_wrapper");
  4833. LOG_DEBUG("compile llvm jit func %s", func_name);
  4834. error =
  4835. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4836. if (error != LLVMErrorSuccess) {
  4837. char *err_msg = LLVMGetErrorMessage(error);
  4838. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4839. LLVMDisposeErrorMessage(err_msg);
  4840. break;
  4841. }
  4842. /* Call the jit wrapper function to trigger its compilation, so as
  4843. to compile the actual jit functions, since we add the latter to
  4844. function list in the PartitionFunction callback */
  4845. u.v = (void *)func_addr;
  4846. u.f();
  4847. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4848. if (i + j * group_stride < func_count) {
  4849. module->func_ptrs_compiled[i + j * group_stride] = true;
  4850. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4851. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  4852. i + j * group_stride);
  4853. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  4854. func_name);
  4855. if (error != LLVMErrorSuccess) {
  4856. char *err_msg = LLVMGetErrorMessage(error);
  4857. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  4858. err_msg);
  4859. LLVMDisposeErrorMessage(err_msg);
  4860. /* Ignore current llvm jit func, as its func ptr is
  4861. previous set to call_to_fast_jit, which also works */
  4862. continue;
  4863. }
  4864. jit_compiler_set_llvm_jit_func_ptr(
  4865. module,
  4866. i + j * group_stride + module->import_function_count,
  4867. (void *)func_addr);
  4868. /* Try to switch to call this llvm jit funtion instead of
  4869. fast jit function from fast jit jitted code */
  4870. jit_compiler_set_call_to_llvm_jit(
  4871. module,
  4872. i + j * group_stride + module->import_function_count);
  4873. #endif
  4874. }
  4875. }
  4876. if (module->orcjit_stop_compiling) {
  4877. break;
  4878. }
  4879. }
  4880. #endif
  4881. return NULL;
  4882. }
  4883. static void
  4884. orcjit_stop_compile_threads(WASMModule *module)
  4885. {
  4886. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  4887. / sizeof(OrcJitThreadArg));
  4888. module->orcjit_stop_compiling = true;
  4889. for (i = 0; i < thread_num; i++) {
  4890. if (module->orcjit_threads[i])
  4891. os_thread_join(module->orcjit_threads[i], NULL);
  4892. }
  4893. }
  4894. static bool
  4895. compile_jit_functions(WASMModule *module, char *error_buf,
  4896. uint32 error_buf_size)
  4897. {
  4898. uint32 thread_num =
  4899. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  4900. uint32 i, j;
  4901. bh_print_time("Begin to compile jit functions");
  4902. /* Create threads to compile the jit functions */
  4903. for (i = 0; i < thread_num && i < module->function_count; i++) {
  4904. #if WASM_ENABLE_JIT != 0
  4905. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  4906. #endif
  4907. module->orcjit_thread_args[i].module = module;
  4908. module->orcjit_thread_args[i].group_idx = i;
  4909. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  4910. (void *)&module->orcjit_thread_args[i],
  4911. APP_THREAD_STACK_SIZE_DEFAULT)
  4912. != 0) {
  4913. set_error_buf(error_buf, error_buf_size,
  4914. "create orcjit compile thread failed");
  4915. /* Terminate the threads created */
  4916. module->orcjit_stop_compiling = true;
  4917. for (j = 0; j < i; j++) {
  4918. os_thread_join(module->orcjit_threads[j], NULL);
  4919. }
  4920. return false;
  4921. }
  4922. }
  4923. #if WASM_ENABLE_LAZY_JIT == 0
  4924. /* Wait until all jit functions are compiled for eager mode */
  4925. for (i = 0; i < thread_num; i++) {
  4926. if (module->orcjit_threads[i])
  4927. os_thread_join(module->orcjit_threads[i], NULL);
  4928. }
  4929. #if WASM_ENABLE_FAST_JIT != 0
  4930. /* Ensure all the fast-jit functions are compiled */
  4931. for (i = 0; i < module->function_count; i++) {
  4932. if (!jit_compiler_is_compiled(module,
  4933. i + module->import_function_count)) {
  4934. set_error_buf(error_buf, error_buf_size,
  4935. "failed to compile fast jit function");
  4936. return false;
  4937. }
  4938. }
  4939. #endif
  4940. #if WASM_ENABLE_JIT != 0
  4941. /* Ensure all the llvm-jit functions are compiled */
  4942. for (i = 0; i < module->function_count; i++) {
  4943. if (!module->func_ptrs_compiled[i]) {
  4944. set_error_buf(error_buf, error_buf_size,
  4945. "failed to compile llvm jit function");
  4946. return false;
  4947. }
  4948. }
  4949. #endif
  4950. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  4951. bh_print_time("End compile jit functions");
  4952. return true;
  4953. }
  4954. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  4955. static bool
  4956. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4957. uint32 cur_func_idx, char *error_buf,
  4958. uint32 error_buf_size);
  4959. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  4960. void **
  4961. wasm_interp_get_handle_table();
  4962. static void **handle_table;
  4963. #endif
  4964. static bool
  4965. load_from_sections(WASMModule *module, WASMSection *sections,
  4966. bool is_load_from_file_buf, char *error_buf,
  4967. uint32 error_buf_size)
  4968. {
  4969. WASMExport *export;
  4970. WASMSection *section = sections;
  4971. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  4972. *buf_func = NULL, *buf_func_end = NULL;
  4973. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  4974. WASMGlobal *aux_stack_top_global = NULL, *global;
  4975. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  4976. aux_stack_top = (uint64)-1LL;
  4977. uint32 global_index, func_index, i;
  4978. uint32 aux_data_end_global_index = (uint32)-1;
  4979. uint32 aux_heap_base_global_index = (uint32)-1;
  4980. WASMFuncType *func_type;
  4981. /* Find code and function sections if have */
  4982. while (section) {
  4983. if (section->section_type == SECTION_TYPE_CODE) {
  4984. buf_code = section->section_body;
  4985. buf_code_end = buf_code + section->section_body_size;
  4986. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  4987. module->buf_code = (uint8 *)buf_code;
  4988. module->buf_code_size = section->section_body_size;
  4989. #endif
  4990. }
  4991. else if (section->section_type == SECTION_TYPE_FUNC) {
  4992. buf_func = section->section_body;
  4993. buf_func_end = buf_func + section->section_body_size;
  4994. }
  4995. section = section->next;
  4996. }
  4997. section = sections;
  4998. while (section) {
  4999. buf = section->section_body;
  5000. buf_end = buf + section->section_body_size;
  5001. switch (section->section_type) {
  5002. case SECTION_TYPE_USER:
  5003. /* unsupported user section, ignore it. */
  5004. if (!load_user_section(buf, buf_end, module,
  5005. is_load_from_file_buf, error_buf,
  5006. error_buf_size))
  5007. return false;
  5008. break;
  5009. case SECTION_TYPE_TYPE:
  5010. if (!load_type_section(buf, buf_end, module, error_buf,
  5011. error_buf_size))
  5012. return false;
  5013. break;
  5014. case SECTION_TYPE_IMPORT:
  5015. if (!load_import_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_FUNC:
  5021. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5022. module, error_buf, error_buf_size))
  5023. return false;
  5024. break;
  5025. case SECTION_TYPE_TABLE:
  5026. if (!load_table_section(buf, buf_end, module, error_buf,
  5027. error_buf_size))
  5028. return false;
  5029. break;
  5030. case SECTION_TYPE_MEMORY:
  5031. if (!load_memory_section(buf, buf_end, module, error_buf,
  5032. error_buf_size))
  5033. return false;
  5034. break;
  5035. #if WASM_ENABLE_TAGS != 0
  5036. case SECTION_TYPE_TAG:
  5037. /* load tag declaration section */
  5038. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5039. module, error_buf, error_buf_size))
  5040. return false;
  5041. break;
  5042. #endif
  5043. case SECTION_TYPE_GLOBAL:
  5044. if (!load_global_section(buf, buf_end, module, error_buf,
  5045. error_buf_size))
  5046. return false;
  5047. break;
  5048. case SECTION_TYPE_EXPORT:
  5049. if (!load_export_section(buf, buf_end, module,
  5050. is_load_from_file_buf, error_buf,
  5051. error_buf_size))
  5052. return false;
  5053. break;
  5054. case SECTION_TYPE_START:
  5055. if (!load_start_section(buf, buf_end, module, error_buf,
  5056. error_buf_size))
  5057. return false;
  5058. break;
  5059. case SECTION_TYPE_ELEM:
  5060. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5061. error_buf_size))
  5062. return false;
  5063. break;
  5064. case SECTION_TYPE_CODE:
  5065. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5066. module, error_buf, error_buf_size))
  5067. return false;
  5068. break;
  5069. case SECTION_TYPE_DATA:
  5070. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  5071. error_buf_size))
  5072. return false;
  5073. break;
  5074. #if WASM_ENABLE_BULK_MEMORY != 0
  5075. case SECTION_TYPE_DATACOUNT:
  5076. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5077. error_buf_size))
  5078. return false;
  5079. break;
  5080. #endif
  5081. #if WASM_ENABLE_STRINGREF != 0
  5082. case SECTION_TYPE_STRINGREF:
  5083. if (!load_stringref_section(buf, buf_end, module,
  5084. is_load_from_file_buf, error_buf,
  5085. error_buf_size))
  5086. return false;
  5087. break;
  5088. #endif
  5089. default:
  5090. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5091. return false;
  5092. }
  5093. section = section->next;
  5094. }
  5095. module->aux_data_end_global_index = (uint32)-1;
  5096. module->aux_heap_base_global_index = (uint32)-1;
  5097. module->aux_stack_top_global_index = (uint32)-1;
  5098. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5099. export = module->exports;
  5100. for (i = 0; i < module->export_count; i++, export ++) {
  5101. if (export->kind == EXPORT_KIND_GLOBAL) {
  5102. if (!strcmp(export->name, "__heap_base")) {
  5103. global_index = export->index - module->import_global_count;
  5104. global = module->globals + global_index;
  5105. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5106. && global->init_expr.init_expr_type
  5107. == INIT_EXPR_TYPE_I32_CONST) {
  5108. aux_heap_base_global = global;
  5109. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5110. aux_heap_base_global_index = export->index;
  5111. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5112. aux_heap_base);
  5113. }
  5114. }
  5115. else if (!strcmp(export->name, "__data_end")) {
  5116. global_index = export->index - module->import_global_count;
  5117. global = module->globals + global_index;
  5118. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5119. && global->init_expr.init_expr_type
  5120. == INIT_EXPR_TYPE_I32_CONST) {
  5121. aux_data_end_global = global;
  5122. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5123. aux_data_end_global_index = export->index;
  5124. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5125. aux_data_end);
  5126. aux_data_end = align_uint64(aux_data_end, 16);
  5127. }
  5128. }
  5129. /* For module compiled with -pthread option, the global is:
  5130. [0] stack_top <-- 0
  5131. [1] tls_pointer
  5132. [2] tls_size
  5133. [3] data_end <-- 3
  5134. [4] global_base
  5135. [5] heap_base <-- 5
  5136. [6] dso_handle
  5137. For module compiled without -pthread option:
  5138. [0] stack_top <-- 0
  5139. [1] data_end <-- 1
  5140. [2] global_base
  5141. [3] heap_base <-- 3
  5142. [4] dso_handle
  5143. */
  5144. if (aux_data_end_global && aux_heap_base_global
  5145. && aux_data_end <= aux_heap_base) {
  5146. module->aux_data_end_global_index = aux_data_end_global_index;
  5147. module->aux_data_end = aux_data_end;
  5148. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5149. module->aux_heap_base = aux_heap_base;
  5150. /* Resolve aux stack top global */
  5151. for (global_index = 0; global_index < module->global_count;
  5152. global_index++) {
  5153. global = module->globals + global_index;
  5154. if (global->is_mutable /* heap_base and data_end is
  5155. not mutable */
  5156. && global->type == VALUE_TYPE_I32
  5157. && global->init_expr.init_expr_type
  5158. == INIT_EXPR_TYPE_I32_CONST
  5159. && (uint64)(uint32)global->init_expr.u.i32
  5160. <= aux_heap_base) {
  5161. aux_stack_top_global = global;
  5162. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5163. module->aux_stack_top_global_index =
  5164. module->import_global_count + global_index;
  5165. module->aux_stack_bottom = aux_stack_top;
  5166. module->aux_stack_size =
  5167. aux_stack_top > aux_data_end
  5168. ? (uint32)(aux_stack_top - aux_data_end)
  5169. : (uint32)aux_stack_top;
  5170. LOG_VERBOSE(
  5171. "Found aux stack top global, value: %" PRIu64 ", "
  5172. "global index: %d, stack size: %d",
  5173. aux_stack_top, global_index,
  5174. module->aux_stack_size);
  5175. break;
  5176. }
  5177. }
  5178. if (!aux_stack_top_global) {
  5179. /* Auxiliary stack global isn't found, it must be unused
  5180. in the wasm app, as if it is used, the global must be
  5181. defined. Here we set it to __heap_base global and set
  5182. its size to 0. */
  5183. aux_stack_top_global = aux_heap_base_global;
  5184. aux_stack_top = aux_heap_base;
  5185. module->aux_stack_top_global_index =
  5186. module->aux_heap_base_global_index;
  5187. module->aux_stack_bottom = aux_stack_top;
  5188. module->aux_stack_size = 0;
  5189. }
  5190. break;
  5191. }
  5192. }
  5193. }
  5194. module->malloc_function = (uint32)-1;
  5195. module->free_function = (uint32)-1;
  5196. module->retain_function = (uint32)-1;
  5197. /* Resolve malloc/free function exported by wasm module */
  5198. export = module->exports;
  5199. for (i = 0; i < module->export_count; i++, export ++) {
  5200. if (export->kind == EXPORT_KIND_FUNC) {
  5201. if (!strcmp(export->name, "malloc")
  5202. && export->index >= module->import_function_count) {
  5203. func_index = export->index - module->import_function_count;
  5204. func_type = module->functions[func_index]->func_type;
  5205. if (func_type->param_count == 1 && func_type->result_count == 1
  5206. && func_type->types[0] == VALUE_TYPE_I32
  5207. && func_type->types[1] == VALUE_TYPE_I32) {
  5208. bh_assert(module->malloc_function == (uint32)-1);
  5209. module->malloc_function = export->index;
  5210. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5211. export->name, export->index);
  5212. }
  5213. }
  5214. else if (!strcmp(export->name, "__new")
  5215. && export->index >= module->import_function_count) {
  5216. /* __new && __pin for AssemblyScript */
  5217. func_index = export->index - module->import_function_count;
  5218. func_type = module->functions[func_index]->func_type;
  5219. if (func_type->param_count == 2 && func_type->result_count == 1
  5220. && func_type->types[0] == VALUE_TYPE_I32
  5221. && func_type->types[1] == VALUE_TYPE_I32
  5222. && func_type->types[2] == VALUE_TYPE_I32) {
  5223. uint32 j;
  5224. WASMExport *export_tmp;
  5225. bh_assert(module->malloc_function == (uint32)-1);
  5226. module->malloc_function = export->index;
  5227. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5228. export->name, export->index);
  5229. /* resolve retain function.
  5230. If not found, reset malloc function index */
  5231. export_tmp = module->exports;
  5232. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5233. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5234. && (!strcmp(export_tmp->name, "__retain")
  5235. || (!strcmp(export_tmp->name, "__pin")))
  5236. && (export_tmp->index
  5237. >= module->import_function_count)) {
  5238. func_index = export_tmp->index
  5239. - module->import_function_count;
  5240. func_type =
  5241. module->functions[func_index]->func_type;
  5242. if (func_type->param_count == 1
  5243. && func_type->result_count == 1
  5244. && func_type->types[0] == VALUE_TYPE_I32
  5245. && func_type->types[1] == VALUE_TYPE_I32) {
  5246. bh_assert(module->retain_function
  5247. == (uint32)-1);
  5248. module->retain_function = export_tmp->index;
  5249. LOG_VERBOSE("Found retain function, name: %s, "
  5250. "index: %u",
  5251. export_tmp->name,
  5252. export_tmp->index);
  5253. break;
  5254. }
  5255. }
  5256. }
  5257. if (j == module->export_count) {
  5258. module->malloc_function = (uint32)-1;
  5259. LOG_VERBOSE("Can't find retain function,"
  5260. "reset malloc function index to -1");
  5261. }
  5262. }
  5263. }
  5264. else if (((!strcmp(export->name, "free"))
  5265. || (!strcmp(export->name, "__release"))
  5266. || (!strcmp(export->name, "__unpin")))
  5267. && export->index >= module->import_function_count) {
  5268. func_index = export->index - module->import_function_count;
  5269. func_type = module->functions[func_index]->func_type;
  5270. if (func_type->param_count == 1 && func_type->result_count == 0
  5271. && func_type->types[0] == VALUE_TYPE_I32) {
  5272. bh_assert(module->free_function == (uint32)-1);
  5273. module->free_function = export->index;
  5274. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5275. export->name, export->index);
  5276. }
  5277. }
  5278. }
  5279. }
  5280. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5281. handle_table = wasm_interp_get_handle_table();
  5282. #endif
  5283. for (i = 0; i < module->function_count; i++) {
  5284. WASMFunction *func = module->functions[i];
  5285. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5286. error_buf_size)) {
  5287. return false;
  5288. }
  5289. if (i == module->function_count - 1
  5290. && func->code + func->code_size != buf_code_end) {
  5291. set_error_buf(error_buf, error_buf_size,
  5292. "code section size mismatch");
  5293. return false;
  5294. }
  5295. }
  5296. if (!module->possible_memory_grow) {
  5297. WASMMemoryImport *memory_import;
  5298. WASMMemory *memory;
  5299. if (aux_data_end_global && aux_heap_base_global
  5300. && aux_stack_top_global) {
  5301. uint64 init_memory_size;
  5302. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5303. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5304. * valid range of uint32 */
  5305. if (shrunk_memory_size <= UINT32_MAX) {
  5306. if (module->import_memory_count) {
  5307. memory_import = &module->import_memories[0].u.memory;
  5308. init_memory_size = (uint64)memory_import->num_bytes_per_page
  5309. * memory_import->init_page_count;
  5310. if (shrunk_memory_size <= init_memory_size) {
  5311. /* Reset memory info to decrease memory usage */
  5312. memory_import->num_bytes_per_page =
  5313. (uint32)shrunk_memory_size;
  5314. memory_import->init_page_count = 1;
  5315. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5316. shrunk_memory_size);
  5317. }
  5318. }
  5319. if (module->memory_count) {
  5320. memory = &module->memories[0];
  5321. init_memory_size = (uint64)memory->num_bytes_per_page
  5322. * memory->init_page_count;
  5323. if (shrunk_memory_size <= init_memory_size) {
  5324. /* Reset memory info to decrease memory usage */
  5325. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5326. memory->init_page_count = 1;
  5327. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5328. shrunk_memory_size);
  5329. }
  5330. }
  5331. }
  5332. }
  5333. #if WASM_ENABLE_MULTI_MODULE == 0
  5334. if (module->import_memory_count) {
  5335. memory_import = &module->import_memories[0].u.memory;
  5336. /* Only resize the memory to one big page if num_bytes_per_page is
  5337. * in valid range of uint32 */
  5338. if (memory_import->init_page_count < DEFAULT_MAX_PAGES) {
  5339. memory_import->num_bytes_per_page *=
  5340. memory_import->init_page_count;
  5341. if (memory_import->init_page_count > 0)
  5342. memory_import->init_page_count =
  5343. memory_import->max_page_count = 1;
  5344. else
  5345. memory_import->init_page_count =
  5346. memory_import->max_page_count = 0;
  5347. }
  5348. }
  5349. if (module->memory_count) {
  5350. memory = &module->memories[0];
  5351. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5352. * valid range of uint32 */
  5353. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5354. memory->num_bytes_per_page *= memory->init_page_count;
  5355. if (memory->init_page_count > 0)
  5356. memory->init_page_count = memory->max_page_count = 1;
  5357. else
  5358. memory->init_page_count = memory->max_page_count = 0;
  5359. }
  5360. }
  5361. #endif
  5362. }
  5363. calculate_global_data_offset(module);
  5364. #if WASM_ENABLE_FAST_JIT != 0
  5365. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5366. return false;
  5367. }
  5368. #endif
  5369. #if WASM_ENABLE_JIT != 0
  5370. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5371. return false;
  5372. }
  5373. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5374. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5375. return false;
  5376. }
  5377. #else
  5378. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5379. thread fast jit execution, since applying llvm optimizations in
  5380. aot_compile_wasm may cost a lot of time.
  5381. Create thread with enough native stack to apply llvm optimizations */
  5382. if (os_thread_create(&module->llvm_jit_init_thread,
  5383. init_llvm_jit_functions_stage2_callback,
  5384. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5385. != 0) {
  5386. set_error_buf(error_buf, error_buf_size,
  5387. "create orcjit compile thread failed");
  5388. return false;
  5389. }
  5390. #endif
  5391. #endif
  5392. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5393. /* Create threads to compile the jit functions */
  5394. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5395. return false;
  5396. }
  5397. #endif
  5398. #if WASM_ENABLE_MEMORY_TRACING != 0
  5399. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5400. #endif
  5401. return true;
  5402. }
  5403. static WASMModule *
  5404. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5405. {
  5406. WASMModule *module =
  5407. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5408. bh_list_status ret;
  5409. if (!module) {
  5410. return NULL;
  5411. }
  5412. module->module_type = Wasm_Module_Bytecode;
  5413. /* Set start_function to -1, means no start function */
  5414. module->start_function = (uint32)-1;
  5415. module->name = name;
  5416. #if WASM_ENABLE_FAST_INTERP == 0
  5417. module->br_table_cache_list = &module->br_table_cache_list_head;
  5418. ret = bh_list_init(module->br_table_cache_list);
  5419. bh_assert(ret == BH_LIST_SUCCESS);
  5420. #endif
  5421. #if WASM_ENABLE_MULTI_MODULE != 0
  5422. module->import_module_list = &module->import_module_list_head;
  5423. ret = bh_list_init(module->import_module_list);
  5424. bh_assert(ret == BH_LIST_SUCCESS);
  5425. #endif
  5426. #if WASM_ENABLE_DEBUG_INTERP != 0
  5427. ret = bh_list_init(&module->fast_opcode_list);
  5428. bh_assert(ret == BH_LIST_SUCCESS);
  5429. #endif
  5430. #if WASM_ENABLE_GC != 0
  5431. if (!(module->ref_type_set =
  5432. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5433. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5434. goto fail1;
  5435. }
  5436. if (os_mutex_init(&module->rtt_type_lock)) {
  5437. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5438. goto fail2;
  5439. }
  5440. #endif
  5441. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5442. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5443. && WASM_ENABLE_LAZY_JIT != 0)
  5444. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5445. set_error_buf(error_buf, error_buf_size,
  5446. "init instance list lock failed");
  5447. goto fail3;
  5448. }
  5449. #endif
  5450. (void)ret;
  5451. return module;
  5452. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5453. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5454. && WASM_ENABLE_LAZY_JIT != 0)
  5455. fail3:
  5456. #endif
  5457. #if WASM_ENABLE_GC != 0
  5458. os_mutex_destroy(&module->rtt_type_lock);
  5459. fail2:
  5460. bh_hash_map_destroy(module->ref_type_set);
  5461. fail1:
  5462. #endif
  5463. wasm_runtime_free(module);
  5464. return NULL;
  5465. }
  5466. #if WASM_ENABLE_DEBUG_INTERP != 0
  5467. static bool
  5468. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5469. uint32 error_buf_size)
  5470. {
  5471. WASMFastOPCodeNode *fast_op =
  5472. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5473. if (fast_op) {
  5474. fast_op->offset = pos - module->load_addr;
  5475. fast_op->orig_op = orig_op;
  5476. bh_list_insert(&module->fast_opcode_list, fast_op);
  5477. }
  5478. return fast_op ? true : false;
  5479. }
  5480. #endif
  5481. WASMModule *
  5482. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5483. uint32 error_buf_size)
  5484. {
  5485. WASMModule *module = create_module("", error_buf, error_buf_size);
  5486. if (!module)
  5487. return NULL;
  5488. if (!load_from_sections(module, section_list, false, error_buf,
  5489. error_buf_size)) {
  5490. wasm_loader_unload(module);
  5491. return NULL;
  5492. }
  5493. LOG_VERBOSE("Load module from sections success.\n");
  5494. return module;
  5495. }
  5496. static void
  5497. destroy_sections(WASMSection *section_list)
  5498. {
  5499. WASMSection *section = section_list, *next;
  5500. while (section) {
  5501. next = section->next;
  5502. wasm_runtime_free(section);
  5503. section = next;
  5504. }
  5505. }
  5506. /* clang-format off */
  5507. static uint8 section_ids[] = {
  5508. SECTION_TYPE_USER,
  5509. SECTION_TYPE_TYPE,
  5510. SECTION_TYPE_IMPORT,
  5511. SECTION_TYPE_FUNC,
  5512. SECTION_TYPE_TABLE,
  5513. SECTION_TYPE_MEMORY,
  5514. #if WASM_ENABLE_TAGS != 0
  5515. SECTION_TYPE_TAG,
  5516. #endif
  5517. #if WASM_ENABLE_STRINGREF != 0
  5518. /* must immediately precede the global section,
  5519. or where the global section would be */
  5520. SECTION_TYPE_STRINGREF,
  5521. #endif
  5522. SECTION_TYPE_GLOBAL,
  5523. SECTION_TYPE_EXPORT,
  5524. SECTION_TYPE_START,
  5525. SECTION_TYPE_ELEM,
  5526. #if WASM_ENABLE_BULK_MEMORY != 0
  5527. SECTION_TYPE_DATACOUNT,
  5528. #endif
  5529. SECTION_TYPE_CODE,
  5530. SECTION_TYPE_DATA
  5531. };
  5532. /* clang-format on */
  5533. static uint8
  5534. get_section_index(uint8 section_type)
  5535. {
  5536. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5537. for (uint8 i = 0; i < max_id; i++) {
  5538. if (section_type == section_ids[i])
  5539. return i;
  5540. }
  5541. return (uint8)-1;
  5542. }
  5543. static bool
  5544. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5545. char *error_buf, uint32 error_buf_size)
  5546. {
  5547. WASMSection *section_list_end = NULL, *section;
  5548. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  5549. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5550. uint32 section_size;
  5551. bh_assert(!*p_section_list);
  5552. p += 8;
  5553. while (p < p_end) {
  5554. CHECK_BUF(p, p_end, 1);
  5555. section_type = read_uint8(p);
  5556. section_index = get_section_index(section_type);
  5557. if (section_index != (uint8)-1) {
  5558. if (section_type != SECTION_TYPE_USER) {
  5559. /* Custom sections may be inserted at any place,
  5560. while other sections must occur at most once
  5561. and in prescribed order. */
  5562. if (last_section_index != (uint8)-1
  5563. && (section_index <= last_section_index)) {
  5564. set_error_buf(error_buf, error_buf_size,
  5565. "unexpected content after last section or "
  5566. "junk after last section");
  5567. return false;
  5568. }
  5569. last_section_index = section_index;
  5570. }
  5571. read_leb_uint32(p, p_end, section_size);
  5572. CHECK_BUF1(p, p_end, section_size);
  5573. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5574. error_buf_size))) {
  5575. return false;
  5576. }
  5577. section->section_type = section_type;
  5578. section->section_body = (uint8 *)p;
  5579. section->section_body_size = section_size;
  5580. if (!section_list_end)
  5581. *p_section_list = section_list_end = section;
  5582. else {
  5583. section_list_end->next = section;
  5584. section_list_end = section;
  5585. }
  5586. p += section_size;
  5587. }
  5588. else {
  5589. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5590. return false;
  5591. }
  5592. }
  5593. return true;
  5594. fail:
  5595. return false;
  5596. }
  5597. static void
  5598. exchange32(uint8 *p_data)
  5599. {
  5600. uint8 value = *p_data;
  5601. *p_data = *(p_data + 3);
  5602. *(p_data + 3) = value;
  5603. value = *(p_data + 1);
  5604. *(p_data + 1) = *(p_data + 2);
  5605. *(p_data + 2) = value;
  5606. }
  5607. static union {
  5608. int a;
  5609. char b;
  5610. } __ue = { .a = 1 };
  5611. #define is_little_endian() (__ue.b == 1)
  5612. static bool
  5613. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  5614. uint32 error_buf_size)
  5615. {
  5616. const uint8 *buf_end = buf + size;
  5617. const uint8 *p = buf, *p_end = buf_end;
  5618. uint32 magic_number, version;
  5619. WASMSection *section_list = NULL;
  5620. CHECK_BUF1(p, p_end, sizeof(uint32));
  5621. magic_number = read_uint32(p);
  5622. if (!is_little_endian())
  5623. exchange32((uint8 *)&magic_number);
  5624. if (magic_number != WASM_MAGIC_NUMBER) {
  5625. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5626. return false;
  5627. }
  5628. CHECK_BUF1(p, p_end, sizeof(uint32));
  5629. version = read_uint32(p);
  5630. if (!is_little_endian())
  5631. exchange32((uint8 *)&version);
  5632. if (version != WASM_CURRENT_VERSION) {
  5633. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5634. return false;
  5635. }
  5636. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5637. || !load_from_sections(module, section_list, true, error_buf,
  5638. error_buf_size)) {
  5639. destroy_sections(section_list);
  5640. return false;
  5641. }
  5642. destroy_sections(section_list);
  5643. return true;
  5644. fail:
  5645. return false;
  5646. }
  5647. #if WASM_ENABLE_LIBC_WASI != 0
  5648. /**
  5649. * refer to
  5650. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5651. */
  5652. static bool
  5653. check_wasi_abi_compatibility(const WASMModule *module,
  5654. #if WASM_ENABLE_MULTI_MODULE != 0
  5655. bool main_module,
  5656. #endif
  5657. char *error_buf, uint32 error_buf_size)
  5658. {
  5659. /**
  5660. * be careful with:
  5661. * wasi compatiable modules(command/reactor) which don't import any wasi
  5662. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5663. * reactor can depend on nothing. At the same time, each has its own entry
  5664. * point.
  5665. *
  5666. * observations:
  5667. * - clang always injects `_start` into a command
  5668. * - clang always injects `_initialize` into a reactor
  5669. * - `iwasm -f` allows to run a function in the reactor
  5670. *
  5671. * strong assumptions:
  5672. * - no one will define either `_start` or `_initialize` on purpose
  5673. * - `_start` should always be `void _start(void)`
  5674. * - `_initialize` should always be `void _initialize(void)`
  5675. *
  5676. */
  5677. /* clang-format off */
  5678. /**
  5679. *
  5680. * | | import_wasi_api True | | import_wasi_api False | |
  5681. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5682. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5683. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5684. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5685. */
  5686. /* clang-format on */
  5687. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5688. uint32 import_function_count = module->import_function_count;
  5689. WASMFuncType *func_type;
  5690. /* (func (export "_start") (...) */
  5691. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5692. error_buf, error_buf_size);
  5693. if (start) {
  5694. if (start->index < import_function_count) {
  5695. set_error_buf(
  5696. error_buf, error_buf_size,
  5697. "the builtin _start function can not be an import function");
  5698. return false;
  5699. }
  5700. func_type =
  5701. module->functions[start->index - import_function_count]->func_type;
  5702. if (func_type->param_count || func_type->result_count) {
  5703. set_error_buf(error_buf, error_buf_size,
  5704. "the signature of builtin _start function is wrong");
  5705. return false;
  5706. }
  5707. }
  5708. else {
  5709. /* (func (export "_initialize") (...) */
  5710. initialize =
  5711. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5712. error_buf, error_buf_size);
  5713. if (initialize) {
  5714. if (initialize->index < import_function_count) {
  5715. set_error_buf(error_buf, error_buf_size,
  5716. "the builtin _initialize function can not be an "
  5717. "import function");
  5718. return false;
  5719. }
  5720. func_type =
  5721. module->functions[initialize->index - import_function_count]
  5722. ->func_type;
  5723. if (func_type->param_count || func_type->result_count) {
  5724. set_error_buf(
  5725. error_buf, error_buf_size,
  5726. "the signature of builtin _initialize function is wrong");
  5727. return false;
  5728. }
  5729. }
  5730. }
  5731. /* filter out non-wasi compatiable modules */
  5732. if (!module->import_wasi_api && !start && !initialize) {
  5733. return true;
  5734. }
  5735. /* should have one at least */
  5736. if (module->import_wasi_api && !start && !initialize) {
  5737. LOG_WARNING("warning: a module with WASI apis should be either "
  5738. "a command or a reactor");
  5739. }
  5740. /*
  5741. * there is at least one of `_start` and `_initialize` in below cases.
  5742. * according to the assumption, they should be all wasi compatiable
  5743. */
  5744. #if WASM_ENABLE_MULTI_MODULE != 0
  5745. /* filter out commands (with `_start`) cases */
  5746. if (start && !main_module) {
  5747. set_error_buf(
  5748. error_buf, error_buf_size,
  5749. "a command (with _start function) can not be a sub-module");
  5750. return false;
  5751. }
  5752. #endif
  5753. /*
  5754. * it is ok a reactor acts as a main module,
  5755. * so skip the check about (with `_initialize`)
  5756. */
  5757. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5758. error_buf, error_buf_size);
  5759. if (!memory
  5760. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5761. /*
  5762. * with wasi-threads, it's still an open question if a memory
  5763. * should be exported.
  5764. *
  5765. * https://github.com/WebAssembly/wasi-threads/issues/22
  5766. * https://github.com/WebAssembly/WASI/issues/502
  5767. *
  5768. * Note: this code assumes the number of memories is at most 1.
  5769. */
  5770. && module->import_memory_count == 0
  5771. #endif
  5772. ) {
  5773. set_error_buf(error_buf, error_buf_size,
  5774. "a module with WASI apis must export memory by default");
  5775. return false;
  5776. }
  5777. return true;
  5778. }
  5779. #endif
  5780. WASMModule *
  5781. wasm_loader_load(uint8 *buf, uint32 size,
  5782. #if WASM_ENABLE_MULTI_MODULE != 0
  5783. bool main_module,
  5784. #endif
  5785. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5786. {
  5787. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5788. if (!module) {
  5789. return NULL;
  5790. }
  5791. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5792. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5793. module->load_addr = (uint8 *)buf;
  5794. module->load_size = size;
  5795. #endif
  5796. if (!load(buf, size, module, error_buf, error_buf_size)) {
  5797. goto fail;
  5798. }
  5799. #if WASM_ENABLE_LIBC_WASI != 0
  5800. /* Check the WASI application ABI */
  5801. if (!check_wasi_abi_compatibility(module,
  5802. #if WASM_ENABLE_MULTI_MODULE != 0
  5803. main_module,
  5804. #endif
  5805. error_buf, error_buf_size)) {
  5806. goto fail;
  5807. }
  5808. #endif
  5809. LOG_VERBOSE("Load module success.\n");
  5810. return module;
  5811. fail:
  5812. wasm_loader_unload(module);
  5813. return NULL;
  5814. }
  5815. void
  5816. wasm_loader_unload(WASMModule *module)
  5817. {
  5818. uint32 i;
  5819. if (!module)
  5820. return;
  5821. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5822. && WASM_ENABLE_LAZY_JIT != 0
  5823. module->orcjit_stop_compiling = true;
  5824. if (module->llvm_jit_init_thread)
  5825. os_thread_join(module->llvm_jit_init_thread, NULL);
  5826. #endif
  5827. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5828. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  5829. module internal data after they were freed */
  5830. orcjit_stop_compile_threads(module);
  5831. #endif
  5832. #if WASM_ENABLE_JIT != 0
  5833. if (module->func_ptrs)
  5834. wasm_runtime_free(module->func_ptrs);
  5835. if (module->comp_ctx)
  5836. aot_destroy_comp_context(module->comp_ctx);
  5837. if (module->comp_data)
  5838. aot_destroy_comp_data(module->comp_data);
  5839. #endif
  5840. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5841. && WASM_ENABLE_LAZY_JIT != 0
  5842. if (module->tierup_wait_lock_inited) {
  5843. os_mutex_destroy(&module->tierup_wait_lock);
  5844. os_cond_destroy(&module->tierup_wait_cond);
  5845. }
  5846. #endif
  5847. if (module->types) {
  5848. for (i = 0; i < module->type_count; i++) {
  5849. if (module->types[i])
  5850. destroy_wasm_type(module->types[i]);
  5851. }
  5852. wasm_runtime_free(module->types);
  5853. }
  5854. if (module->imports)
  5855. wasm_runtime_free(module->imports);
  5856. if (module->functions) {
  5857. for (i = 0; i < module->function_count; i++) {
  5858. if (module->functions[i]) {
  5859. if (module->functions[i]->local_offsets)
  5860. wasm_runtime_free(module->functions[i]->local_offsets);
  5861. #if WASM_ENABLE_FAST_INTERP != 0
  5862. if (module->functions[i]->code_compiled)
  5863. wasm_runtime_free(module->functions[i]->code_compiled);
  5864. if (module->functions[i]->consts)
  5865. wasm_runtime_free(module->functions[i]->consts);
  5866. #endif
  5867. #if WASM_ENABLE_FAST_JIT != 0
  5868. if (module->functions[i]->fast_jit_jitted_code) {
  5869. jit_code_cache_free(
  5870. module->functions[i]->fast_jit_jitted_code);
  5871. }
  5872. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5873. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  5874. jit_code_cache_free(
  5875. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  5876. }
  5877. #endif
  5878. #endif
  5879. #if WASM_ENABLE_GC != 0
  5880. if (module->functions[i]->local_ref_type_maps) {
  5881. wasm_runtime_free(
  5882. module->functions[i]->local_ref_type_maps);
  5883. }
  5884. #endif
  5885. wasm_runtime_free(module->functions[i]);
  5886. }
  5887. }
  5888. wasm_runtime_free(module->functions);
  5889. }
  5890. if (module->tables) {
  5891. #if WASM_ENABLE_GC != 0
  5892. for (i = 0; i < module->table_count; i++) {
  5893. destroy_init_expr(&module->tables[i].init_expr);
  5894. }
  5895. #endif
  5896. wasm_runtime_free(module->tables);
  5897. }
  5898. if (module->memories)
  5899. wasm_runtime_free(module->memories);
  5900. if (module->globals) {
  5901. #if WASM_ENABLE_GC != 0
  5902. for (i = 0; i < module->global_count; i++) {
  5903. destroy_init_expr(&module->globals[i].init_expr);
  5904. }
  5905. #endif
  5906. wasm_runtime_free(module->globals);
  5907. }
  5908. #if WASM_ENABLE_TAGS != 0
  5909. if (module->tags) {
  5910. for (i = 0; i < module->tag_count; i++) {
  5911. if (module->tags[i])
  5912. wasm_runtime_free(module->tags[i]);
  5913. }
  5914. wasm_runtime_free(module->tags);
  5915. }
  5916. #endif
  5917. if (module->exports)
  5918. wasm_runtime_free(module->exports);
  5919. if (module->table_segments) {
  5920. for (i = 0; i < module->table_seg_count; i++) {
  5921. if (module->table_segments[i].init_values) {
  5922. #if WASM_ENABLE_GC != 0
  5923. uint32 j;
  5924. for (j = 0; j < module->table_segments[i].value_count; j++) {
  5925. destroy_init_expr(
  5926. &module->table_segments[i].init_values[j]);
  5927. }
  5928. #endif
  5929. wasm_runtime_free(module->table_segments[i].init_values);
  5930. }
  5931. }
  5932. wasm_runtime_free(module->table_segments);
  5933. }
  5934. if (module->data_segments) {
  5935. for (i = 0; i < module->data_seg_count; i++) {
  5936. if (module->data_segments[i])
  5937. wasm_runtime_free(module->data_segments[i]);
  5938. }
  5939. wasm_runtime_free(module->data_segments);
  5940. }
  5941. if (module->const_str_list) {
  5942. StringNode *node = module->const_str_list, *node_next;
  5943. while (node) {
  5944. node_next = node->next;
  5945. wasm_runtime_free(node);
  5946. node = node_next;
  5947. }
  5948. }
  5949. #if WASM_ENABLE_STRINGREF != 0
  5950. if (module->string_literal_ptrs) {
  5951. wasm_runtime_free((void *)module->string_literal_ptrs);
  5952. }
  5953. if (module->string_literal_lengths) {
  5954. wasm_runtime_free(module->string_literal_lengths);
  5955. }
  5956. #endif
  5957. #if WASM_ENABLE_FAST_INTERP == 0
  5958. if (module->br_table_cache_list) {
  5959. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  5960. BrTableCache *node_next;
  5961. while (node) {
  5962. node_next = bh_list_elem_next(node);
  5963. wasm_runtime_free(node);
  5964. node = node_next;
  5965. }
  5966. }
  5967. #endif
  5968. #if WASM_ENABLE_MULTI_MODULE != 0
  5969. /* just release the sub module list */
  5970. if (module->import_module_list) {
  5971. WASMRegisteredModule *node =
  5972. bh_list_first_elem(module->import_module_list);
  5973. while (node) {
  5974. WASMRegisteredModule *next = bh_list_elem_next(node);
  5975. bh_list_remove(module->import_module_list, node);
  5976. /*
  5977. * unload(sub_module) will be trigged during runtime_destroy().
  5978. * every module in the global module list will be unloaded one by
  5979. * one. so don't worry.
  5980. */
  5981. wasm_runtime_free(node);
  5982. /*
  5983. * the module file reading buffer will be released
  5984. * in runtime_destroy()
  5985. */
  5986. node = next;
  5987. }
  5988. }
  5989. #endif
  5990. #if WASM_ENABLE_DEBUG_INTERP != 0
  5991. WASMFastOPCodeNode *fast_opcode =
  5992. bh_list_first_elem(&module->fast_opcode_list);
  5993. while (fast_opcode) {
  5994. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  5995. wasm_runtime_free(fast_opcode);
  5996. fast_opcode = next;
  5997. }
  5998. #endif
  5999. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6000. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6001. && WASM_ENABLE_LAZY_JIT != 0)
  6002. os_mutex_destroy(&module->instance_list_lock);
  6003. #endif
  6004. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6005. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6006. #endif
  6007. #if WASM_ENABLE_FAST_JIT != 0
  6008. if (module->fast_jit_func_ptrs) {
  6009. wasm_runtime_free(module->fast_jit_func_ptrs);
  6010. }
  6011. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6012. if (module->fast_jit_thread_locks_inited[i]) {
  6013. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6014. }
  6015. }
  6016. #endif
  6017. #if WASM_ENABLE_GC != 0
  6018. os_mutex_destroy(&module->rtt_type_lock);
  6019. bh_hash_map_destroy(module->ref_type_set);
  6020. if (module->rtt_types) {
  6021. for (i = 0; i < module->type_count; i++) {
  6022. if (module->rtt_types[i])
  6023. wasm_runtime_free(module->rtt_types[i]);
  6024. }
  6025. wasm_runtime_free(module->rtt_types);
  6026. }
  6027. #if WASM_ENABLE_STRINGREF != 0
  6028. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6029. if (module->stringref_rtts[i])
  6030. wasm_runtime_free(module->stringref_rtts[i]);
  6031. }
  6032. #endif
  6033. #endif
  6034. wasm_runtime_free(module);
  6035. }
  6036. bool
  6037. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6038. const uint8 *start_addr, const uint8 *code_end_addr,
  6039. uint8 label_type, uint8 **p_else_addr,
  6040. uint8 **p_end_addr)
  6041. {
  6042. const uint8 *p = start_addr, *p_end = code_end_addr;
  6043. uint8 *else_addr = NULL;
  6044. char error_buf[128];
  6045. uint32 block_nested_depth = 1, count, i, j, t;
  6046. uint32 error_buf_size = sizeof(error_buf);
  6047. uint8 opcode, u8;
  6048. BlockAddr block_stack[16] = { { 0 } }, *block;
  6049. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6050. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6051. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6052. if (block[j].start_addr == start_addr) {
  6053. /* Cache hit */
  6054. *p_else_addr = block[j].else_addr;
  6055. *p_end_addr = block[j].end_addr;
  6056. return true;
  6057. }
  6058. }
  6059. /* Cache unhit */
  6060. block_stack[0].start_addr = start_addr;
  6061. while (p < code_end_addr) {
  6062. opcode = *p++;
  6063. #if WASM_ENABLE_DEBUG_INTERP != 0
  6064. op_break_retry:
  6065. #endif
  6066. switch (opcode) {
  6067. case WASM_OP_UNREACHABLE:
  6068. case WASM_OP_NOP:
  6069. break;
  6070. #if WASM_ENABLE_EXCE_HANDLING != 0
  6071. case WASM_OP_TRY:
  6072. u8 = read_uint8(p);
  6073. if (block_nested_depth
  6074. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6075. block_stack[block_nested_depth].start_addr = p;
  6076. block_stack[block_nested_depth].else_addr = NULL;
  6077. }
  6078. block_nested_depth++;
  6079. break;
  6080. case EXT_OP_TRY:
  6081. skip_leb_uint32(p, p_end);
  6082. if (block_nested_depth
  6083. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6084. block_stack[block_nested_depth].start_addr = p;
  6085. block_stack[block_nested_depth].else_addr = NULL;
  6086. }
  6087. block_nested_depth++;
  6088. break;
  6089. case WASM_OP_CATCH:
  6090. if (block_nested_depth == 1) {
  6091. *p_end_addr = (uint8 *)(p - 1);
  6092. /* stop search and return the address of the catch block */
  6093. return true;
  6094. }
  6095. break;
  6096. case WASM_OP_CATCH_ALL:
  6097. if (block_nested_depth == 1) {
  6098. *p_end_addr = (uint8 *)(p - 1);
  6099. /* stop search and return the address of the catch_all block
  6100. */
  6101. return true;
  6102. }
  6103. break;
  6104. case WASM_OP_THROW:
  6105. /* skip tag_index */
  6106. skip_leb(p);
  6107. break;
  6108. case WASM_OP_RETHROW:
  6109. /* skip depth */
  6110. skip_leb(p);
  6111. break;
  6112. case WASM_OP_DELEGATE:
  6113. if (block_nested_depth == 1) {
  6114. *p_end_addr = (uint8 *)(p - 1);
  6115. return true;
  6116. }
  6117. else {
  6118. skip_leb(p);
  6119. /* the DELEGATE opcode ends the tryblock, */
  6120. block_nested_depth--;
  6121. if (block_nested_depth
  6122. < sizeof(block_stack) / sizeof(BlockAddr))
  6123. block_stack[block_nested_depth].end_addr =
  6124. (uint8 *)(p - 1);
  6125. }
  6126. break;
  6127. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6128. case WASM_OP_BLOCK:
  6129. case WASM_OP_LOOP:
  6130. case WASM_OP_IF:
  6131. {
  6132. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6133. u8 = read_uint8(p);
  6134. if (is_byte_a_type(u8)) {
  6135. #if WASM_ENABLE_GC != 0
  6136. if (wasm_is_type_multi_byte_type(u8)) {
  6137. /* the possible extra bytes of GC ref type have been
  6138. modified to OP_NOP, no need to resolve them again */
  6139. }
  6140. #endif
  6141. }
  6142. else {
  6143. p--;
  6144. skip_leb_uint32(p, p_end);
  6145. }
  6146. if (block_nested_depth
  6147. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6148. block_stack[block_nested_depth].start_addr = p;
  6149. block_stack[block_nested_depth].else_addr = NULL;
  6150. }
  6151. block_nested_depth++;
  6152. break;
  6153. }
  6154. case EXT_OP_BLOCK:
  6155. case EXT_OP_LOOP:
  6156. case EXT_OP_IF:
  6157. /* block type */
  6158. skip_leb_uint32(p, p_end);
  6159. if (block_nested_depth
  6160. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6161. block_stack[block_nested_depth].start_addr = p;
  6162. block_stack[block_nested_depth].else_addr = NULL;
  6163. }
  6164. block_nested_depth++;
  6165. break;
  6166. case WASM_OP_ELSE:
  6167. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6168. else_addr = (uint8 *)(p - 1);
  6169. if (block_nested_depth - 1
  6170. < sizeof(block_stack) / sizeof(BlockAddr))
  6171. block_stack[block_nested_depth - 1].else_addr =
  6172. (uint8 *)(p - 1);
  6173. break;
  6174. case WASM_OP_END:
  6175. if (block_nested_depth == 1) {
  6176. if (label_type == LABEL_TYPE_IF)
  6177. *p_else_addr = else_addr;
  6178. *p_end_addr = (uint8 *)(p - 1);
  6179. block_stack[0].end_addr = (uint8 *)(p - 1);
  6180. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6181. t++) {
  6182. start_addr = block_stack[t].start_addr;
  6183. if (start_addr) {
  6184. i = ((uintptr_t)start_addr)
  6185. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6186. block =
  6187. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6188. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6189. if (!block[j].start_addr)
  6190. break;
  6191. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6192. memmove(block + 1, block,
  6193. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6194. * sizeof(BlockAddr));
  6195. j = 0;
  6196. }
  6197. block[j].start_addr = block_stack[t].start_addr;
  6198. block[j].else_addr = block_stack[t].else_addr;
  6199. block[j].end_addr = block_stack[t].end_addr;
  6200. }
  6201. else
  6202. break;
  6203. }
  6204. return true;
  6205. }
  6206. else {
  6207. block_nested_depth--;
  6208. if (block_nested_depth
  6209. < sizeof(block_stack) / sizeof(BlockAddr))
  6210. block_stack[block_nested_depth].end_addr =
  6211. (uint8 *)(p - 1);
  6212. }
  6213. break;
  6214. case WASM_OP_BR:
  6215. case WASM_OP_BR_IF:
  6216. skip_leb_uint32(p, p_end); /* labelidx */
  6217. break;
  6218. case WASM_OP_BR_TABLE:
  6219. read_leb_uint32(p, p_end, count); /* lable num */
  6220. #if WASM_ENABLE_FAST_INTERP != 0
  6221. for (i = 0; i <= count; i++) /* lableidxs */
  6222. skip_leb_uint32(p, p_end);
  6223. #else
  6224. p += count + 1;
  6225. while (*p == WASM_OP_NOP)
  6226. p++;
  6227. #endif
  6228. break;
  6229. #if WASM_ENABLE_FAST_INTERP == 0
  6230. case EXT_OP_BR_TABLE_CACHE:
  6231. read_leb_uint32(p, p_end, count); /* lable num */
  6232. while (*p == WASM_OP_NOP)
  6233. p++;
  6234. break;
  6235. #endif
  6236. case WASM_OP_RETURN:
  6237. break;
  6238. case WASM_OP_CALL:
  6239. #if WASM_ENABLE_TAIL_CALL != 0
  6240. case WASM_OP_RETURN_CALL:
  6241. #endif
  6242. skip_leb_uint32(p, p_end); /* funcidx */
  6243. break;
  6244. case WASM_OP_CALL_INDIRECT:
  6245. #if WASM_ENABLE_TAIL_CALL != 0
  6246. case WASM_OP_RETURN_CALL_INDIRECT:
  6247. #endif
  6248. skip_leb_uint32(p, p_end); /* typeidx */
  6249. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6250. u8 = read_uint8(p); /* 0x00 */
  6251. #else
  6252. skip_leb_uint32(p, p_end); /* talbeidx */
  6253. #endif
  6254. break;
  6255. #if WASM_ENABLE_GC != 0
  6256. case WASM_OP_CALL_REF:
  6257. case WASM_OP_RETURN_CALL_REF:
  6258. skip_leb_uint32(p, p_end); /* typeidx */
  6259. break;
  6260. #endif
  6261. case WASM_OP_DROP:
  6262. case WASM_OP_SELECT:
  6263. case WASM_OP_DROP_64:
  6264. case WASM_OP_SELECT_64:
  6265. break;
  6266. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6267. case WASM_OP_SELECT_T:
  6268. {
  6269. skip_leb_uint32(p, p_end); /* vec length */
  6270. u8 = read_uint8(p); /* typeidx */
  6271. /* the possible extra bytes of GC ref type have been
  6272. modified to OP_NOP, no need to resolve them again */
  6273. break;
  6274. }
  6275. case WASM_OP_TABLE_GET:
  6276. case WASM_OP_TABLE_SET:
  6277. skip_leb_uint32(p, p_end); /* table index */
  6278. break;
  6279. case WASM_OP_REF_NULL:
  6280. {
  6281. u8 = read_uint8(p); /* type */
  6282. if (is_byte_a_type(u8)) {
  6283. #if WASM_ENABLE_GC != 0
  6284. if (wasm_is_type_multi_byte_type(u8)) {
  6285. /* the possible extra bytes of GC ref type have been
  6286. modified to OP_NOP, no need to resolve them again */
  6287. }
  6288. #endif
  6289. }
  6290. else {
  6291. p--;
  6292. skip_leb_uint32(p, p_end);
  6293. }
  6294. break;
  6295. }
  6296. case WASM_OP_REF_IS_NULL:
  6297. break;
  6298. case WASM_OP_REF_FUNC:
  6299. skip_leb_uint32(p, p_end); /* func index */
  6300. break;
  6301. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6302. #if WASM_ENABLE_GC != 0
  6303. case WASM_OP_REF_AS_NON_NULL:
  6304. case WASM_OP_REF_EQ:
  6305. break;
  6306. case WASM_OP_BR_ON_NULL:
  6307. case WASM_OP_BR_ON_NON_NULL:
  6308. skip_leb_uint32(p, p_end); /* label index */
  6309. break;
  6310. #endif /* end of WASM_ENABLE_GC != 0 */
  6311. case WASM_OP_GET_LOCAL:
  6312. case WASM_OP_SET_LOCAL:
  6313. case WASM_OP_TEE_LOCAL:
  6314. case WASM_OP_GET_GLOBAL:
  6315. case WASM_OP_SET_GLOBAL:
  6316. case WASM_OP_GET_GLOBAL_64:
  6317. case WASM_OP_SET_GLOBAL_64:
  6318. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6319. skip_leb_uint32(p, p_end); /* local index */
  6320. break;
  6321. case EXT_OP_GET_LOCAL_FAST:
  6322. case EXT_OP_SET_LOCAL_FAST:
  6323. case EXT_OP_TEE_LOCAL_FAST:
  6324. CHECK_BUF(p, p_end, 1);
  6325. p++;
  6326. break;
  6327. case WASM_OP_I32_LOAD:
  6328. case WASM_OP_I64_LOAD:
  6329. case WASM_OP_F32_LOAD:
  6330. case WASM_OP_F64_LOAD:
  6331. case WASM_OP_I32_LOAD8_S:
  6332. case WASM_OP_I32_LOAD8_U:
  6333. case WASM_OP_I32_LOAD16_S:
  6334. case WASM_OP_I32_LOAD16_U:
  6335. case WASM_OP_I64_LOAD8_S:
  6336. case WASM_OP_I64_LOAD8_U:
  6337. case WASM_OP_I64_LOAD16_S:
  6338. case WASM_OP_I64_LOAD16_U:
  6339. case WASM_OP_I64_LOAD32_S:
  6340. case WASM_OP_I64_LOAD32_U:
  6341. case WASM_OP_I32_STORE:
  6342. case WASM_OP_I64_STORE:
  6343. case WASM_OP_F32_STORE:
  6344. case WASM_OP_F64_STORE:
  6345. case WASM_OP_I32_STORE8:
  6346. case WASM_OP_I32_STORE16:
  6347. case WASM_OP_I64_STORE8:
  6348. case WASM_OP_I64_STORE16:
  6349. case WASM_OP_I64_STORE32:
  6350. skip_leb_uint32(p, p_end); /* align */
  6351. skip_leb_mem_offset(p, p_end); /* offset */
  6352. break;
  6353. case WASM_OP_MEMORY_SIZE:
  6354. case WASM_OP_MEMORY_GROW:
  6355. skip_leb_uint32(p, p_end); /* 0x00 */
  6356. break;
  6357. case WASM_OP_I32_CONST:
  6358. skip_leb_int32(p, p_end);
  6359. break;
  6360. case WASM_OP_I64_CONST:
  6361. skip_leb_int64(p, p_end);
  6362. break;
  6363. case WASM_OP_F32_CONST:
  6364. p += sizeof(float32);
  6365. break;
  6366. case WASM_OP_F64_CONST:
  6367. p += sizeof(float64);
  6368. break;
  6369. case WASM_OP_I32_EQZ:
  6370. case WASM_OP_I32_EQ:
  6371. case WASM_OP_I32_NE:
  6372. case WASM_OP_I32_LT_S:
  6373. case WASM_OP_I32_LT_U:
  6374. case WASM_OP_I32_GT_S:
  6375. case WASM_OP_I32_GT_U:
  6376. case WASM_OP_I32_LE_S:
  6377. case WASM_OP_I32_LE_U:
  6378. case WASM_OP_I32_GE_S:
  6379. case WASM_OP_I32_GE_U:
  6380. case WASM_OP_I64_EQZ:
  6381. case WASM_OP_I64_EQ:
  6382. case WASM_OP_I64_NE:
  6383. case WASM_OP_I64_LT_S:
  6384. case WASM_OP_I64_LT_U:
  6385. case WASM_OP_I64_GT_S:
  6386. case WASM_OP_I64_GT_U:
  6387. case WASM_OP_I64_LE_S:
  6388. case WASM_OP_I64_LE_U:
  6389. case WASM_OP_I64_GE_S:
  6390. case WASM_OP_I64_GE_U:
  6391. case WASM_OP_F32_EQ:
  6392. case WASM_OP_F32_NE:
  6393. case WASM_OP_F32_LT:
  6394. case WASM_OP_F32_GT:
  6395. case WASM_OP_F32_LE:
  6396. case WASM_OP_F32_GE:
  6397. case WASM_OP_F64_EQ:
  6398. case WASM_OP_F64_NE:
  6399. case WASM_OP_F64_LT:
  6400. case WASM_OP_F64_GT:
  6401. case WASM_OP_F64_LE:
  6402. case WASM_OP_F64_GE:
  6403. case WASM_OP_I32_CLZ:
  6404. case WASM_OP_I32_CTZ:
  6405. case WASM_OP_I32_POPCNT:
  6406. case WASM_OP_I32_ADD:
  6407. case WASM_OP_I32_SUB:
  6408. case WASM_OP_I32_MUL:
  6409. case WASM_OP_I32_DIV_S:
  6410. case WASM_OP_I32_DIV_U:
  6411. case WASM_OP_I32_REM_S:
  6412. case WASM_OP_I32_REM_U:
  6413. case WASM_OP_I32_AND:
  6414. case WASM_OP_I32_OR:
  6415. case WASM_OP_I32_XOR:
  6416. case WASM_OP_I32_SHL:
  6417. case WASM_OP_I32_SHR_S:
  6418. case WASM_OP_I32_SHR_U:
  6419. case WASM_OP_I32_ROTL:
  6420. case WASM_OP_I32_ROTR:
  6421. case WASM_OP_I64_CLZ:
  6422. case WASM_OP_I64_CTZ:
  6423. case WASM_OP_I64_POPCNT:
  6424. case WASM_OP_I64_ADD:
  6425. case WASM_OP_I64_SUB:
  6426. case WASM_OP_I64_MUL:
  6427. case WASM_OP_I64_DIV_S:
  6428. case WASM_OP_I64_DIV_U:
  6429. case WASM_OP_I64_REM_S:
  6430. case WASM_OP_I64_REM_U:
  6431. case WASM_OP_I64_AND:
  6432. case WASM_OP_I64_OR:
  6433. case WASM_OP_I64_XOR:
  6434. case WASM_OP_I64_SHL:
  6435. case WASM_OP_I64_SHR_S:
  6436. case WASM_OP_I64_SHR_U:
  6437. case WASM_OP_I64_ROTL:
  6438. case WASM_OP_I64_ROTR:
  6439. case WASM_OP_F32_ABS:
  6440. case WASM_OP_F32_NEG:
  6441. case WASM_OP_F32_CEIL:
  6442. case WASM_OP_F32_FLOOR:
  6443. case WASM_OP_F32_TRUNC:
  6444. case WASM_OP_F32_NEAREST:
  6445. case WASM_OP_F32_SQRT:
  6446. case WASM_OP_F32_ADD:
  6447. case WASM_OP_F32_SUB:
  6448. case WASM_OP_F32_MUL:
  6449. case WASM_OP_F32_DIV:
  6450. case WASM_OP_F32_MIN:
  6451. case WASM_OP_F32_MAX:
  6452. case WASM_OP_F32_COPYSIGN:
  6453. case WASM_OP_F64_ABS:
  6454. case WASM_OP_F64_NEG:
  6455. case WASM_OP_F64_CEIL:
  6456. case WASM_OP_F64_FLOOR:
  6457. case WASM_OP_F64_TRUNC:
  6458. case WASM_OP_F64_NEAREST:
  6459. case WASM_OP_F64_SQRT:
  6460. case WASM_OP_F64_ADD:
  6461. case WASM_OP_F64_SUB:
  6462. case WASM_OP_F64_MUL:
  6463. case WASM_OP_F64_DIV:
  6464. case WASM_OP_F64_MIN:
  6465. case WASM_OP_F64_MAX:
  6466. case WASM_OP_F64_COPYSIGN:
  6467. case WASM_OP_I32_WRAP_I64:
  6468. case WASM_OP_I32_TRUNC_S_F32:
  6469. case WASM_OP_I32_TRUNC_U_F32:
  6470. case WASM_OP_I32_TRUNC_S_F64:
  6471. case WASM_OP_I32_TRUNC_U_F64:
  6472. case WASM_OP_I64_EXTEND_S_I32:
  6473. case WASM_OP_I64_EXTEND_U_I32:
  6474. case WASM_OP_I64_TRUNC_S_F32:
  6475. case WASM_OP_I64_TRUNC_U_F32:
  6476. case WASM_OP_I64_TRUNC_S_F64:
  6477. case WASM_OP_I64_TRUNC_U_F64:
  6478. case WASM_OP_F32_CONVERT_S_I32:
  6479. case WASM_OP_F32_CONVERT_U_I32:
  6480. case WASM_OP_F32_CONVERT_S_I64:
  6481. case WASM_OP_F32_CONVERT_U_I64:
  6482. case WASM_OP_F32_DEMOTE_F64:
  6483. case WASM_OP_F64_CONVERT_S_I32:
  6484. case WASM_OP_F64_CONVERT_U_I32:
  6485. case WASM_OP_F64_CONVERT_S_I64:
  6486. case WASM_OP_F64_CONVERT_U_I64:
  6487. case WASM_OP_F64_PROMOTE_F32:
  6488. case WASM_OP_I32_REINTERPRET_F32:
  6489. case WASM_OP_I64_REINTERPRET_F64:
  6490. case WASM_OP_F32_REINTERPRET_I32:
  6491. case WASM_OP_F64_REINTERPRET_I64:
  6492. case WASM_OP_I32_EXTEND8_S:
  6493. case WASM_OP_I32_EXTEND16_S:
  6494. case WASM_OP_I64_EXTEND8_S:
  6495. case WASM_OP_I64_EXTEND16_S:
  6496. case WASM_OP_I64_EXTEND32_S:
  6497. break;
  6498. #if WASM_ENABLE_GC != 0
  6499. case WASM_OP_GC_PREFIX:
  6500. {
  6501. uint32 opcode1;
  6502. read_leb_uint32(p, p_end, opcode1);
  6503. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6504. is no larger than UINT8_MAX */
  6505. opcode = (uint8)opcode1;
  6506. switch (opcode) {
  6507. case WASM_OP_STRUCT_NEW:
  6508. case WASM_OP_STRUCT_NEW_DEFAULT:
  6509. skip_leb_uint32(p, p_end); /* typeidx */
  6510. break;
  6511. case WASM_OP_STRUCT_GET:
  6512. case WASM_OP_STRUCT_GET_S:
  6513. case WASM_OP_STRUCT_GET_U:
  6514. case WASM_OP_STRUCT_SET:
  6515. skip_leb_uint32(p, p_end); /* typeidx */
  6516. skip_leb_uint32(p, p_end); /* fieldidx */
  6517. break;
  6518. case WASM_OP_ARRAY_NEW:
  6519. case WASM_OP_ARRAY_NEW_DEFAULT:
  6520. case WASM_OP_ARRAY_GET:
  6521. case WASM_OP_ARRAY_GET_S:
  6522. case WASM_OP_ARRAY_GET_U:
  6523. case WASM_OP_ARRAY_SET:
  6524. case WASM_OP_ARRAY_FILL:
  6525. skip_leb_uint32(p, p_end); /* typeidx */
  6526. break;
  6527. case WASM_OP_ARRAY_COPY:
  6528. skip_leb_uint32(p, p_end); /* typeidx1 */
  6529. skip_leb_uint32(p, p_end); /* typeidx2 */
  6530. break;
  6531. case WASM_OP_ARRAY_LEN:
  6532. break;
  6533. case WASM_OP_ARRAY_NEW_FIXED:
  6534. case WASM_OP_ARRAY_NEW_DATA:
  6535. case WASM_OP_ARRAY_NEW_ELEM:
  6536. skip_leb_uint32(p, p_end); /* typeidx */
  6537. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6538. break;
  6539. case WASM_OP_REF_I31:
  6540. case WASM_OP_I31_GET_S:
  6541. case WASM_OP_I31_GET_U:
  6542. break;
  6543. case WASM_OP_REF_TEST:
  6544. case WASM_OP_REF_CAST:
  6545. case WASM_OP_REF_TEST_NULLABLE:
  6546. case WASM_OP_REF_CAST_NULLABLE:
  6547. skip_leb_int32(p, p_end); /* heaptype */
  6548. break;
  6549. case WASM_OP_BR_ON_CAST:
  6550. case WASM_OP_BR_ON_CAST_FAIL:
  6551. p += sizeof(uint8); /* castflag */
  6552. skip_leb_uint32(p, p_end); /* labelidx */
  6553. skip_leb_int32(p, p_end); /* heaptype */
  6554. skip_leb_int32(p, p_end); /* heaptype2 */
  6555. break;
  6556. case WASM_OP_ANY_CONVERT_EXTERN:
  6557. case WASM_OP_EXTERN_CONVERT_ANY:
  6558. break;
  6559. #if WASM_ENABLE_STRINGREF != 0
  6560. case WASM_OP_STRING_NEW_UTF8:
  6561. case WASM_OP_STRING_NEW_WTF16:
  6562. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6563. case WASM_OP_STRING_NEW_WTF8:
  6564. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6565. break;
  6566. case WASM_OP_STRING_CONST:
  6567. skip_leb_int32(p, p_end); /* contents */
  6568. break;
  6569. case WASM_OP_STRING_MEASURE_UTF8:
  6570. case WASM_OP_STRING_MEASURE_WTF8:
  6571. case WASM_OP_STRING_MEASURE_WTF16:
  6572. break;
  6573. case WASM_OP_STRING_ENCODE_UTF8:
  6574. case WASM_OP_STRING_ENCODE_WTF16:
  6575. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6576. case WASM_OP_STRING_ENCODE_WTF8:
  6577. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6578. break;
  6579. case WASM_OP_STRING_CONCAT:
  6580. case WASM_OP_STRING_EQ:
  6581. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6582. case WASM_OP_STRING_AS_WTF8:
  6583. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6584. break;
  6585. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6586. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6587. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6588. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6589. break;
  6590. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6591. case WASM_OP_STRING_AS_WTF16:
  6592. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6593. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6594. break;
  6595. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6596. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6597. break;
  6598. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6599. case WASM_OP_STRING_AS_ITER:
  6600. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6601. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6602. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6603. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6604. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6605. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6606. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6607. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6608. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6609. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6610. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6611. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6612. break;
  6613. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6614. default:
  6615. return false;
  6616. }
  6617. break;
  6618. }
  6619. #endif /* end of WASM_ENABLE_GC != 0 */
  6620. case WASM_OP_MISC_PREFIX:
  6621. {
  6622. uint32 opcode1;
  6623. read_leb_uint32(p, p_end, opcode1);
  6624. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6625. is no larger than UINT8_MAX */
  6626. opcode = (uint8)opcode1;
  6627. switch (opcode) {
  6628. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6629. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6630. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6631. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6632. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6633. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6634. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6635. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6636. break;
  6637. #if WASM_ENABLE_BULK_MEMORY != 0
  6638. case WASM_OP_MEMORY_INIT:
  6639. skip_leb_uint32(p, p_end);
  6640. /* skip memory idx */
  6641. p++;
  6642. break;
  6643. case WASM_OP_DATA_DROP:
  6644. skip_leb_uint32(p, p_end);
  6645. break;
  6646. case WASM_OP_MEMORY_COPY:
  6647. /* skip two memory idx */
  6648. p += 2;
  6649. break;
  6650. case WASM_OP_MEMORY_FILL:
  6651. /* skip memory idx */
  6652. p++;
  6653. break;
  6654. #endif /* WASM_ENABLE_BULK_MEMORY */
  6655. #if WASM_ENABLE_REF_TYPES != 0
  6656. case WASM_OP_TABLE_INIT:
  6657. case WASM_OP_TABLE_COPY:
  6658. /* tableidx */
  6659. skip_leb_uint32(p, p_end);
  6660. /* elemidx */
  6661. skip_leb_uint32(p, p_end);
  6662. break;
  6663. case WASM_OP_ELEM_DROP:
  6664. /* elemidx */
  6665. skip_leb_uint32(p, p_end);
  6666. break;
  6667. case WASM_OP_TABLE_SIZE:
  6668. case WASM_OP_TABLE_GROW:
  6669. case WASM_OP_TABLE_FILL:
  6670. skip_leb_uint32(p, p_end); /* table idx */
  6671. break;
  6672. #endif /* WASM_ENABLE_REF_TYPES */
  6673. default:
  6674. return false;
  6675. }
  6676. break;
  6677. }
  6678. #if WASM_ENABLE_SIMD != 0
  6679. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6680. case WASM_OP_SIMD_PREFIX:
  6681. {
  6682. /* TODO: memory64 offset type changes */
  6683. uint32 opcode1;
  6684. read_leb_uint32(p, p_end, opcode1);
  6685. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6686. is no larger than UINT8_MAX */
  6687. opcode = (uint8)opcode1;
  6688. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6689. */
  6690. switch (opcode) {
  6691. case SIMD_v128_load:
  6692. case SIMD_v128_load8x8_s:
  6693. case SIMD_v128_load8x8_u:
  6694. case SIMD_v128_load16x4_s:
  6695. case SIMD_v128_load16x4_u:
  6696. case SIMD_v128_load32x2_s:
  6697. case SIMD_v128_load32x2_u:
  6698. case SIMD_v128_load8_splat:
  6699. case SIMD_v128_load16_splat:
  6700. case SIMD_v128_load32_splat:
  6701. case SIMD_v128_load64_splat:
  6702. case SIMD_v128_store:
  6703. /* memarg align */
  6704. skip_leb_uint32(p, p_end);
  6705. /* memarg offset*/
  6706. skip_leb_uint32(p, p_end);
  6707. break;
  6708. case SIMD_v128_const:
  6709. case SIMD_v8x16_shuffle:
  6710. /* immByte[16] immLaneId[16] */
  6711. CHECK_BUF1(p, p_end, 16);
  6712. p += 16;
  6713. break;
  6714. case SIMD_i8x16_extract_lane_s:
  6715. case SIMD_i8x16_extract_lane_u:
  6716. case SIMD_i8x16_replace_lane:
  6717. case SIMD_i16x8_extract_lane_s:
  6718. case SIMD_i16x8_extract_lane_u:
  6719. case SIMD_i16x8_replace_lane:
  6720. case SIMD_i32x4_extract_lane:
  6721. case SIMD_i32x4_replace_lane:
  6722. case SIMD_i64x2_extract_lane:
  6723. case SIMD_i64x2_replace_lane:
  6724. case SIMD_f32x4_extract_lane:
  6725. case SIMD_f32x4_replace_lane:
  6726. case SIMD_f64x2_extract_lane:
  6727. case SIMD_f64x2_replace_lane:
  6728. /* ImmLaneId */
  6729. CHECK_BUF(p, p_end, 1);
  6730. p++;
  6731. break;
  6732. case SIMD_v128_load8_lane:
  6733. case SIMD_v128_load16_lane:
  6734. case SIMD_v128_load32_lane:
  6735. case SIMD_v128_load64_lane:
  6736. case SIMD_v128_store8_lane:
  6737. case SIMD_v128_store16_lane:
  6738. case SIMD_v128_store32_lane:
  6739. case SIMD_v128_store64_lane:
  6740. /* memarg align */
  6741. skip_leb_uint32(p, p_end);
  6742. /* memarg offset*/
  6743. skip_leb_uint32(p, p_end);
  6744. /* ImmLaneId */
  6745. CHECK_BUF(p, p_end, 1);
  6746. p++;
  6747. break;
  6748. case SIMD_v128_load32_zero:
  6749. case SIMD_v128_load64_zero:
  6750. /* memarg align */
  6751. skip_leb_uint32(p, p_end);
  6752. /* memarg offset*/
  6753. skip_leb_uint32(p, p_end);
  6754. break;
  6755. default:
  6756. /*
  6757. * since latest SIMD specific used almost every value
  6758. * from 0x00 to 0xff, the default branch will present
  6759. * all opcodes without imm
  6760. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6761. */
  6762. break;
  6763. }
  6764. break;
  6765. }
  6766. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6767. #endif /* end of WASM_ENABLE_SIMD */
  6768. #if WASM_ENABLE_SHARED_MEMORY != 0
  6769. case WASM_OP_ATOMIC_PREFIX:
  6770. {
  6771. /* TODO: memory64 offset type changes */
  6772. uint32 opcode1;
  6773. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6774. read_leb_uint32(p, p_end, opcode1);
  6775. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6776. is no larger than UINT8_MAX */
  6777. opcode = (uint8)opcode1;
  6778. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6779. skip_leb_uint32(p, p_end); /* align */
  6780. skip_leb_mem_offset(p, p_end); /* offset */
  6781. }
  6782. else {
  6783. /* atomic.fence doesn't have memarg */
  6784. p++;
  6785. }
  6786. break;
  6787. }
  6788. #endif
  6789. #if WASM_ENABLE_DEBUG_INTERP != 0
  6790. case DEBUG_OP_BREAK:
  6791. {
  6792. WASMDebugInstance *debug_instance =
  6793. wasm_exec_env_get_instance(exec_env);
  6794. char orignal_opcode[1];
  6795. uint64 size = 1;
  6796. WASMModuleInstance *module_inst =
  6797. (WASMModuleInstance *)exec_env->module_inst;
  6798. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6799. ? (p - 1) - module_inst->module->load_addr
  6800. : ~0;
  6801. if (debug_instance) {
  6802. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6803. orignal_opcode, &size)
  6804. && size == 1) {
  6805. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6806. "with %02x: ",
  6807. orignal_opcode[0]);
  6808. opcode = orignal_opcode[0];
  6809. goto op_break_retry;
  6810. }
  6811. }
  6812. break;
  6813. }
  6814. #endif
  6815. default:
  6816. return false;
  6817. }
  6818. }
  6819. (void)u8;
  6820. (void)exec_env;
  6821. return false;
  6822. fail:
  6823. return false;
  6824. }
  6825. #if WASM_ENABLE_FAST_INTERP != 0
  6826. #if WASM_DEBUG_PREPROCESSOR != 0
  6827. #define LOG_OP(...) os_printf(__VA_ARGS__)
  6828. #else
  6829. #define LOG_OP(...) (void)0
  6830. #endif
  6831. #define PATCH_ELSE 0
  6832. #define PATCH_END 1
  6833. typedef struct BranchBlockPatch {
  6834. struct BranchBlockPatch *next;
  6835. uint8 patch_type;
  6836. uint8 *code_compiled;
  6837. } BranchBlockPatch;
  6838. #endif
  6839. typedef struct BranchBlock {
  6840. uint8 label_type;
  6841. BlockType block_type;
  6842. uint8 *start_addr;
  6843. uint8 *else_addr;
  6844. uint8 *end_addr;
  6845. uint32 stack_cell_num;
  6846. #if WASM_ENABLE_GC != 0
  6847. uint32 reftype_map_num;
  6848. /* Indicate which local is used inside current block, used to validate
  6849. * local.get with non-nullable ref types */
  6850. uint8 *local_use_mask;
  6851. uint32 local_use_mask_size;
  6852. #endif
  6853. #if WASM_ENABLE_FAST_INTERP != 0
  6854. uint16 dynamic_offset;
  6855. uint8 *code_compiled;
  6856. BranchBlockPatch *patch_list;
  6857. /* This is used to save params frame_offset of of if block */
  6858. int16 *param_frame_offsets;
  6859. /* This is used to recover dynamic offset for else branch */
  6860. uint16 start_dynamic_offset;
  6861. #endif
  6862. /* Indicate the operand stack is in polymorphic state.
  6863. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  6864. * to polymorphic state until the block's 'end' opcode is processed.
  6865. * If stack is in polymorphic state and stack is empty, instruction can
  6866. * pop any type of value directly without decreasing stack top pointer
  6867. * and stack cell num. */
  6868. bool is_stack_polymorphic;
  6869. } BranchBlock;
  6870. typedef struct WASMLoaderContext {
  6871. /* frame ref stack */
  6872. uint8 *frame_ref;
  6873. uint8 *frame_ref_bottom;
  6874. uint8 *frame_ref_boundary;
  6875. uint32 frame_ref_size;
  6876. uint32 stack_cell_num;
  6877. uint32 max_stack_cell_num;
  6878. #if WASM_ENABLE_GC != 0
  6879. /* frame reftype map stack */
  6880. WASMRefTypeMap *frame_reftype_map;
  6881. WASMRefTypeMap *frame_reftype_map_bottom;
  6882. WASMRefTypeMap *frame_reftype_map_boundary;
  6883. uint32 frame_reftype_map_size;
  6884. uint32 reftype_map_num;
  6885. uint32 max_reftype_map_num;
  6886. /* Current module */
  6887. WASMModule *module;
  6888. /* Current module's ref_type_set */
  6889. HashMap *ref_type_set;
  6890. /* Always point to local variable ref_type of
  6891. wasm_loader_prepare_bytecode */
  6892. WASMRefType *ref_type_tmp;
  6893. #endif
  6894. /* frame csp stack */
  6895. BranchBlock *frame_csp;
  6896. BranchBlock *frame_csp_bottom;
  6897. BranchBlock *frame_csp_boundary;
  6898. uint32 frame_csp_size;
  6899. uint32 csp_num;
  6900. uint32 max_csp_num;
  6901. #if WASM_ENABLE_FAST_INTERP != 0
  6902. /* frame offset stack */
  6903. int16 *frame_offset;
  6904. int16 *frame_offset_bottom;
  6905. int16 *frame_offset_boundary;
  6906. uint32 frame_offset_size;
  6907. int16 dynamic_offset;
  6908. int16 start_dynamic_offset;
  6909. int16 max_dynamic_offset;
  6910. /* preserved local offset */
  6911. int16 preserved_local_offset;
  6912. /* const buffer */
  6913. uint8 *const_buf;
  6914. uint16 num_const;
  6915. uint16 const_cell_num;
  6916. uint32 const_buf_size;
  6917. /* processed code */
  6918. uint8 *p_code_compiled;
  6919. uint8 *p_code_compiled_end;
  6920. uint32 code_compiled_size;
  6921. /* If the last opcode will be dropped, the peak memory usage will be larger
  6922. * than the final code_compiled_size, we record the peak size to ensure
  6923. * there will not be invalid memory access during second traverse */
  6924. uint32 code_compiled_peak_size;
  6925. #endif
  6926. } WASMLoaderContext;
  6927. typedef struct Const {
  6928. WASMValue value;
  6929. uint16 slot_index;
  6930. uint8 value_type;
  6931. } Const;
  6932. #define CHECK_CSP_PUSH() \
  6933. do { \
  6934. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  6935. MEM_REALLOC( \
  6936. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  6937. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  6938. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  6939. ctx->frame_csp_boundary = \
  6940. ctx->frame_csp_bottom \
  6941. + ctx->frame_csp_size / sizeof(BranchBlock); \
  6942. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  6943. } \
  6944. } while (0)
  6945. #define CHECK_CSP_POP() \
  6946. do { \
  6947. if (ctx->csp_num < 1) { \
  6948. set_error_buf(error_buf, error_buf_size, \
  6949. "type mismatch: " \
  6950. "expect data but block stack was empty"); \
  6951. goto fail; \
  6952. } \
  6953. } while (0)
  6954. #if WASM_ENABLE_FAST_INTERP != 0
  6955. static bool
  6956. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  6957. uint32 error_buf_size)
  6958. {
  6959. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  6960. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  6961. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  6962. ctx->frame_offset_size + 16);
  6963. ctx->frame_offset_size += 16;
  6964. ctx->frame_offset_boundary =
  6965. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  6966. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  6967. }
  6968. return true;
  6969. fail:
  6970. return false;
  6971. }
  6972. static bool
  6973. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  6974. {
  6975. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  6976. return false;
  6977. return true;
  6978. }
  6979. static void
  6980. free_label_patch_list(BranchBlock *frame_csp)
  6981. {
  6982. BranchBlockPatch *label_patch = frame_csp->patch_list;
  6983. BranchBlockPatch *next;
  6984. while (label_patch != NULL) {
  6985. next = label_patch->next;
  6986. wasm_runtime_free(label_patch);
  6987. label_patch = next;
  6988. }
  6989. frame_csp->patch_list = NULL;
  6990. }
  6991. static void
  6992. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  6993. {
  6994. BranchBlock *tmp_csp = frame_csp;
  6995. for (uint32 i = 0; i < csp_num; i++) {
  6996. free_label_patch_list(tmp_csp);
  6997. tmp_csp++;
  6998. }
  6999. }
  7000. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7001. #if WASM_ENABLE_GC != 0
  7002. static bool
  7003. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7004. char *error_buf, uint32 error_buf_size)
  7005. {
  7006. BranchBlock *current_csp = ctx->frame_csp - 1;
  7007. uint32 local_mask_size;
  7008. if (local_count == 0) {
  7009. current_csp->local_use_mask_size = 0;
  7010. return true;
  7011. }
  7012. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7013. * else branch, we don't need to allocate memory again */
  7014. if (!current_csp->local_use_mask) {
  7015. local_mask_size = (local_count + 7) / sizeof(uint8);
  7016. if (!(current_csp->local_use_mask =
  7017. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7018. return false;
  7019. }
  7020. current_csp->local_use_mask_size = local_mask_size;
  7021. }
  7022. else {
  7023. local_mask_size = current_csp->local_use_mask_size;
  7024. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7025. }
  7026. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7027. /* For non-function blocks, inherit the use status from parent block */
  7028. BranchBlock *parent_csp = current_csp - 1;
  7029. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7030. bh_assert(parent_csp->local_use_mask);
  7031. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7032. parent_csp->local_use_mask, local_mask_size);
  7033. }
  7034. return true;
  7035. }
  7036. static void
  7037. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7038. {
  7039. BranchBlock *current_csp = ctx->frame_csp - 1;
  7040. bh_assert(current_csp->local_use_mask
  7041. || current_csp->local_use_mask_size == 0);
  7042. if (current_csp->local_use_mask) {
  7043. wasm_runtime_free(current_csp->local_use_mask);
  7044. }
  7045. current_csp->local_use_mask = NULL;
  7046. current_csp->local_use_mask_size = 0;
  7047. }
  7048. static void
  7049. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7050. {
  7051. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7052. uint32 i;
  7053. for (i = 0; i < ctx->csp_num; i++) {
  7054. if (tmp_csp->local_use_mask) {
  7055. wasm_runtime_free(tmp_csp->local_use_mask);
  7056. tmp_csp->local_use_mask = NULL;
  7057. tmp_csp->local_use_mask_size = 0;
  7058. }
  7059. tmp_csp++;
  7060. }
  7061. }
  7062. static void
  7063. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7064. {
  7065. BranchBlock *current_csp = ctx->frame_csp - 1;
  7066. uint32 byte_offset = index / sizeof(uint8);
  7067. uint32 bit_offset = index % sizeof(uint8);
  7068. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7069. bh_assert(current_csp->local_use_mask);
  7070. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7071. }
  7072. static bool
  7073. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7074. {
  7075. BranchBlock *current_csp = ctx->frame_csp - 1;
  7076. uint32 byte_offset = index / sizeof(uint8);
  7077. uint32 bit_offset = index % sizeof(uint8);
  7078. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7079. bh_assert(current_csp->local_use_mask);
  7080. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7081. ? true
  7082. : false;
  7083. }
  7084. #endif /* end of WASM_ENABLE_GC != 0 */
  7085. static void
  7086. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7087. {
  7088. if (ctx) {
  7089. if (ctx->frame_ref_bottom)
  7090. wasm_runtime_free(ctx->frame_ref_bottom);
  7091. #if WASM_ENABLE_GC != 0
  7092. if (ctx->frame_reftype_map_bottom)
  7093. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7094. #endif
  7095. if (ctx->frame_csp_bottom) {
  7096. #if WASM_ENABLE_FAST_INTERP != 0
  7097. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7098. #endif
  7099. #if WASM_ENABLE_GC != 0
  7100. wasm_loader_clean_all_local_use_masks(ctx);
  7101. #endif
  7102. wasm_runtime_free(ctx->frame_csp_bottom);
  7103. }
  7104. #if WASM_ENABLE_FAST_INTERP != 0
  7105. if (ctx->frame_offset_bottom)
  7106. wasm_runtime_free(ctx->frame_offset_bottom);
  7107. if (ctx->const_buf)
  7108. wasm_runtime_free(ctx->const_buf);
  7109. #endif
  7110. wasm_runtime_free(ctx);
  7111. }
  7112. }
  7113. static WASMLoaderContext *
  7114. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7115. {
  7116. WASMLoaderContext *loader_ctx =
  7117. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7118. if (!loader_ctx)
  7119. return NULL;
  7120. loader_ctx->frame_ref_size = 32;
  7121. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7122. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7123. goto fail;
  7124. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7125. #if WASM_ENABLE_GC != 0
  7126. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7127. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7128. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7129. error_buf_size)))
  7130. goto fail;
  7131. loader_ctx->frame_reftype_map_boundary =
  7132. loader_ctx->frame_reftype_map_bottom + 16;
  7133. #endif
  7134. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7135. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7136. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7137. goto fail;
  7138. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7139. #if WASM_ENABLE_EXCE_HANDLING != 0
  7140. func->exception_handler_count = 0;
  7141. #endif
  7142. #if WASM_ENABLE_FAST_INTERP != 0
  7143. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7144. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7145. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7146. error_buf_size)))
  7147. goto fail;
  7148. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7149. loader_ctx->num_const = 0;
  7150. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7151. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7152. error_buf, error_buf_size)))
  7153. goto fail;
  7154. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7155. set_error_buf(error_buf, error_buf_size,
  7156. "fast interpreter offset overflow");
  7157. goto fail;
  7158. }
  7159. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7160. loader_ctx->max_dynamic_offset =
  7161. func->param_cell_num + func->local_cell_num;
  7162. #endif
  7163. return loader_ctx;
  7164. fail:
  7165. wasm_loader_ctx_destroy(loader_ctx);
  7166. return NULL;
  7167. }
  7168. static bool
  7169. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7170. uint32 error_buf_size)
  7171. {
  7172. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7173. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7174. /* Increase the frame ref stack */
  7175. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7176. ctx->frame_ref_size + 16);
  7177. ctx->frame_ref_size += 16;
  7178. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7179. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7180. }
  7181. #if WASM_ENABLE_GC != 0
  7182. if (wasm_is_type_multi_byte_type(type)
  7183. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7184. /* Increase the frame reftype map stack */
  7185. bh_assert(
  7186. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7187. * sizeof(WASMRefTypeMap))
  7188. == ctx->frame_reftype_map_size);
  7189. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7190. ctx->frame_reftype_map_size
  7191. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7192. ctx->frame_reftype_map =
  7193. ctx->frame_reftype_map_bottom
  7194. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7195. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7196. ctx->frame_reftype_map_boundary =
  7197. ctx->frame_reftype_map_bottom
  7198. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7199. }
  7200. #endif
  7201. return true;
  7202. fail:
  7203. return false;
  7204. }
  7205. static bool
  7206. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7207. uint32 error_buf_size)
  7208. {
  7209. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7210. uint32 i;
  7211. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7212. return false;
  7213. #if WASM_ENABLE_GC != 0
  7214. if (wasm_is_type_multi_byte_type(type)) {
  7215. WASMRefType *ref_type;
  7216. if (!(ref_type =
  7217. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7218. error_buf, error_buf_size))) {
  7219. return false;
  7220. }
  7221. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7222. /* Increase the frame reftype map stack */
  7223. bh_assert((uint32)((ctx->frame_reftype_map
  7224. - ctx->frame_reftype_map_bottom)
  7225. * sizeof(WASMRefTypeMap))
  7226. == ctx->frame_reftype_map_size);
  7227. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7228. ctx->frame_reftype_map_size,
  7229. ctx->frame_reftype_map_size
  7230. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7231. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7232. + ctx->frame_reftype_map_size
  7233. / ((uint32)sizeof(WASMRefTypeMap));
  7234. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7235. ctx->frame_reftype_map_boundary =
  7236. ctx->frame_reftype_map_bottom
  7237. + ctx->frame_reftype_map_size
  7238. / ((uint32)sizeof(WASMRefTypeMap));
  7239. }
  7240. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7241. ctx->frame_reftype_map->ref_type = ref_type;
  7242. ctx->frame_reftype_map++;
  7243. ctx->reftype_map_num++;
  7244. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7245. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7246. }
  7247. #endif
  7248. for (i = 0; i < type_cell_num; i++)
  7249. *ctx->frame_ref++ = type;
  7250. ctx->stack_cell_num += type_cell_num;
  7251. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7252. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7253. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7254. set_error_buf(error_buf, error_buf_size,
  7255. "operand stack depth limit exceeded");
  7256. return false;
  7257. }
  7258. }
  7259. return true;
  7260. #if WASM_ENABLE_GC != 0
  7261. fail:
  7262. return false;
  7263. #endif
  7264. }
  7265. static bool
  7266. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7267. int32 stack_cell_num,
  7268. #if WASM_ENABLE_GC != 0
  7269. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7270. #endif
  7271. uint8 type,
  7272. #if WASM_ENABLE_GC != 0
  7273. WASMRefType *ref_type,
  7274. #endif
  7275. char *error_buf, uint32 error_buf_size)
  7276. {
  7277. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7278. #if WASM_ENABLE_GC != 0
  7279. WASMRefType *frame_reftype = NULL;
  7280. #endif
  7281. if (stack_cell_num < type_cell_num) {
  7282. set_error_buf(error_buf, error_buf_size,
  7283. "type mismatch: expect data but stack was empty");
  7284. return false;
  7285. }
  7286. #if WASM_ENABLE_GC == 0
  7287. for (i = 0; i < type_cell_num; i++) {
  7288. if (*(frame_ref - 1 - i) != type) {
  7289. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7290. "type mismatch: expect ", type2str(type),
  7291. " but got other");
  7292. return false;
  7293. }
  7294. }
  7295. #else
  7296. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7297. bh_assert(reftype_map_num > 0);
  7298. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7299. }
  7300. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7301. ref_type, ctx->module->types,
  7302. ctx->module->type_count)) {
  7303. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7304. "type mismatch: expect ", type2str(type),
  7305. " but got other");
  7306. return false;
  7307. }
  7308. for (i = 0; i < type_cell_num - 1; i++) {
  7309. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7310. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7311. "type mismatch: expect ", type2str(type),
  7312. " but got other");
  7313. return false;
  7314. }
  7315. }
  7316. #endif
  7317. return true;
  7318. }
  7319. static bool
  7320. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7321. uint32 error_buf_size)
  7322. {
  7323. int32 block_stack_cell_num =
  7324. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7325. #if WASM_ENABLE_GC != 0
  7326. int32 reftype_map_num =
  7327. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7328. #endif
  7329. if (block_stack_cell_num > 0) {
  7330. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7331. /* the stack top is a value of any type, return success */
  7332. return true;
  7333. }
  7334. #if WASM_ENABLE_GC != 0
  7335. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7336. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7337. WASMRefType *stack_top_ref_type = NULL;
  7338. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7339. bh_assert(reftype_map_num > 0);
  7340. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7341. }
  7342. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7343. ctx->ref_type_tmp, ctx->module->types,
  7344. ctx->module->type_count)) {
  7345. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7346. uint32 ref_type_struct_size =
  7347. wasm_reftype_struct_size(stack_top_ref_type);
  7348. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7349. stack_top_ref_type, ref_type_struct_size);
  7350. }
  7351. return true;
  7352. }
  7353. }
  7354. #endif
  7355. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7356. #if WASM_ENABLE_GC != 0
  7357. ctx->frame_reftype_map, reftype_map_num,
  7358. #endif
  7359. type,
  7360. #if WASM_ENABLE_GC != 0
  7361. ctx->ref_type_tmp,
  7362. #endif
  7363. error_buf, error_buf_size)) {
  7364. return false;
  7365. }
  7366. return true;
  7367. }
  7368. static bool
  7369. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7370. uint32 error_buf_size)
  7371. {
  7372. BranchBlock *cur_block = ctx->frame_csp - 1;
  7373. int32 available_stack_cell =
  7374. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7375. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7376. /* Directly return success if current block is in stack
  7377. polymorphic state while stack is empty. */
  7378. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7379. return true;
  7380. if (type == VALUE_TYPE_VOID)
  7381. return true;
  7382. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7383. return false;
  7384. bh_assert(available_stack_cell > 0);
  7385. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7386. type = VALUE_TYPE_ANY;
  7387. cell_num_to_pop = 1;
  7388. }
  7389. ctx->frame_ref -= cell_num_to_pop;
  7390. ctx->stack_cell_num -= cell_num_to_pop;
  7391. #if WASM_ENABLE_GC != 0
  7392. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7393. ctx->frame_reftype_map--;
  7394. ctx->reftype_map_num--;
  7395. }
  7396. #endif
  7397. return true;
  7398. }
  7399. #if WASM_ENABLE_GC != 0
  7400. /* Get the stack top element of current block */
  7401. static bool
  7402. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7403. WASMRefType **p_ref_type, char *error_buf,
  7404. uint32 error_buf_size)
  7405. {
  7406. BranchBlock *cur_block = ctx->frame_csp - 1;
  7407. int32 available_stack_cell =
  7408. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7409. if (available_stack_cell <= 0) {
  7410. /* Directly return success if current block is in stack
  7411. polymorphic state while stack is empty. */
  7412. if (cur_block->is_stack_polymorphic) {
  7413. *p_type = VALUE_TYPE_ANY;
  7414. return true;
  7415. }
  7416. else {
  7417. set_error_buf(
  7418. error_buf, error_buf_size,
  7419. "type mismatch: expect data but block stack was empty");
  7420. return false;
  7421. }
  7422. }
  7423. *p_type = *(ctx->frame_ref - 1);
  7424. if (wasm_is_type_multi_byte_type(*p_type)) {
  7425. int32 available_reftype_map =
  7426. (int32)(ctx->reftype_map_num
  7427. - (ctx->frame_csp - 1)->reftype_map_num);
  7428. bh_assert(available_reftype_map > 0);
  7429. (void)available_reftype_map;
  7430. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7431. }
  7432. return true;
  7433. }
  7434. #if WASM_ENABLE_FAST_INTERP != 0
  7435. static bool
  7436. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7437. char *error_buf, uint32 error_buf_size);
  7438. #endif
  7439. /* Check whether the stack top elem is a heap object, and if yes,
  7440. pop and return it */
  7441. static bool
  7442. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7443. WASMRefType *ref_ht_ret, char *error_buf,
  7444. uint32 error_buf_size)
  7445. {
  7446. uint8 type = 0;
  7447. WASMRefType *ref_type = NULL;
  7448. /* Get stack top element */
  7449. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7450. error_buf_size)) {
  7451. return false;
  7452. }
  7453. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7454. /* stack top isn't a ref type */
  7455. && !wasm_is_type_reftype(type)) {
  7456. set_error_buf(error_buf, error_buf_size,
  7457. "type mismatch: expect heap object but got others");
  7458. return false;
  7459. }
  7460. /* POP stack top */
  7461. if (wasm_is_type_multi_byte_type(type)) {
  7462. bh_assert(ref_type);
  7463. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7464. wasm_reftype_struct_size(ref_type));
  7465. }
  7466. #if WASM_ENABLE_FAST_INTERP != 0
  7467. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7468. error_buf_size)) {
  7469. return false;
  7470. }
  7471. #else
  7472. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7473. return false;
  7474. }
  7475. #endif
  7476. if (p_type)
  7477. *p_type = type;
  7478. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7479. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7480. wasm_reftype_struct_size(ref_type));
  7481. }
  7482. return true;
  7483. }
  7484. /* Check whether the stack top elem is subtype of (ref null ht),
  7485. and if yes, pop it and return the converted (ref ht) */
  7486. static bool
  7487. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7488. WASMRefType *ref_ht_ret, char *error_buf,
  7489. uint32 error_buf_size)
  7490. {
  7491. uint8 type = 0;
  7492. WASMRefType ref_type = { 0 };
  7493. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7494. error_buf_size)) {
  7495. return false;
  7496. }
  7497. /* Convert to related (ref ht) and return */
  7498. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7499. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7500. */
  7501. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7502. HEAP_TYPE_ARRAY
  7503. + (type - REF_TYPE_ARRAYREF));
  7504. type = ref_ht_ret->ref_type;
  7505. }
  7506. else if (wasm_is_reftype_htref_nullable(type)
  7507. || wasm_is_reftype_htref_non_nullable(type)) {
  7508. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7509. wasm_reftype_struct_size(&ref_type));
  7510. /* Convert to (ref ht) */
  7511. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7512. ref_ht_ret->ref_ht_common.nullable = false;
  7513. type = ref_ht_ret->ref_type;
  7514. }
  7515. *p_type = type;
  7516. return true;
  7517. }
  7518. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7519. and if yes, pop it and return the type_idx */
  7520. static bool
  7521. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7522. uint32 *p_type_idx, char *error_buf,
  7523. uint32 error_buf_size)
  7524. {
  7525. uint8 type = 0;
  7526. int32 type_idx = -1;
  7527. WASMRefType *ref_type = NULL;
  7528. /* Get stack top element */
  7529. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7530. error_buf_size)) {
  7531. return false;
  7532. }
  7533. if (type != VALUE_TYPE_ANY) {
  7534. /* stack top isn't (ref null $t) */
  7535. if (!((wasm_is_reftype_htref_nullable(type)
  7536. || wasm_is_reftype_htref_non_nullable(type))
  7537. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7538. set_error_buf(error_buf, error_buf_size,
  7539. "type mismatch: expect (ref null $t) but got others");
  7540. return false;
  7541. }
  7542. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7543. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7544. wasm_reftype_struct_size(ref_type));
  7545. }
  7546. /* POP stack top */
  7547. #if WASM_ENABLE_FAST_INTERP != 0
  7548. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7549. error_buf_size)) {
  7550. return false;
  7551. }
  7552. #else
  7553. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7554. return false;
  7555. }
  7556. #endif
  7557. /* Convert to type_idx and return */
  7558. *p_type = type;
  7559. if (type != VALUE_TYPE_ANY)
  7560. *p_type_idx = (uint32)type_idx;
  7561. return true;
  7562. }
  7563. #endif /* WASM_ENABLE_GC != 0 */
  7564. #if WASM_ENABLE_FAST_INTERP == 0
  7565. static bool
  7566. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7567. uint8 type_push, uint8 type_pop, char *error_buf,
  7568. uint32 error_buf_size)
  7569. {
  7570. for (int i = 0; i < pop_cnt; i++) {
  7571. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7572. error_buf_size))
  7573. return false;
  7574. }
  7575. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7576. return false;
  7577. return true;
  7578. }
  7579. #endif
  7580. static bool
  7581. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7582. BlockType block_type, uint8 *start_addr,
  7583. char *error_buf, uint32 error_buf_size)
  7584. {
  7585. CHECK_CSP_PUSH();
  7586. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7587. ctx->frame_csp->label_type = label_type;
  7588. ctx->frame_csp->block_type = block_type;
  7589. ctx->frame_csp->start_addr = start_addr;
  7590. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7591. #if WASM_ENABLE_GC != 0
  7592. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7593. #endif
  7594. #if WASM_ENABLE_FAST_INTERP != 0
  7595. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7596. ctx->frame_csp->patch_list = NULL;
  7597. #endif
  7598. ctx->frame_csp++;
  7599. ctx->csp_num++;
  7600. if (ctx->csp_num > ctx->max_csp_num) {
  7601. ctx->max_csp_num = ctx->csp_num;
  7602. if (ctx->max_csp_num > UINT16_MAX) {
  7603. set_error_buf(error_buf, error_buf_size,
  7604. "label stack depth limit exceeded");
  7605. return false;
  7606. }
  7607. }
  7608. return true;
  7609. fail:
  7610. return false;
  7611. }
  7612. static bool
  7613. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7614. uint32 error_buf_size)
  7615. {
  7616. CHECK_CSP_POP();
  7617. #if WASM_ENABLE_FAST_INTERP != 0
  7618. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7619. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7620. #endif
  7621. ctx->frame_csp--;
  7622. ctx->csp_num--;
  7623. return true;
  7624. fail:
  7625. return false;
  7626. }
  7627. #if WASM_ENABLE_FAST_INTERP != 0
  7628. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7629. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7630. #define emit_label(opcode) \
  7631. do { \
  7632. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7633. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7634. } while (0)
  7635. #define skip_label() \
  7636. do { \
  7637. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7638. LOG_OP("\ndelete last op\n"); \
  7639. } while (0)
  7640. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7641. #if UINTPTR_MAX == UINT64_MAX
  7642. #define emit_label(opcode) \
  7643. do { \
  7644. int32 offset = \
  7645. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7646. /* emit int32 relative offset in 64-bit target */ \
  7647. wasm_loader_emit_uint32(loader_ctx, offset); \
  7648. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7649. } while (0)
  7650. #else
  7651. #define emit_label(opcode) \
  7652. do { \
  7653. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7654. /* emit uint32 label address in 32-bit target */ \
  7655. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7656. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7657. } while (0)
  7658. #endif
  7659. #define skip_label() \
  7660. do { \
  7661. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7662. LOG_OP("\ndelete last op\n"); \
  7663. } while (0)
  7664. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7665. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7666. #define emit_label(opcode) \
  7667. do { \
  7668. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7669. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7670. } while (0)
  7671. #define skip_label() \
  7672. do { \
  7673. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7674. LOG_OP("\ndelete last op\n"); \
  7675. } while (0)
  7676. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7677. #define emit_empty_label_addr_and_frame_ip(type) \
  7678. do { \
  7679. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7680. loader_ctx->p_code_compiled, error_buf, \
  7681. error_buf_size)) \
  7682. goto fail; \
  7683. /* label address, to be patched */ \
  7684. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7685. } while (0)
  7686. #define emit_br_info(frame_csp, is_br) \
  7687. do { \
  7688. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7689. error_buf_size)) \
  7690. goto fail; \
  7691. } while (0)
  7692. #define LAST_OP_OUTPUT_I32() \
  7693. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7694. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7695. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7696. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7697. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7698. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7699. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7700. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7701. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7702. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7703. || (last_op == EXT_OP_COPY_STACK_TOP)
  7704. #define LAST_OP_OUTPUT_I64() \
  7705. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7706. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7707. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7708. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7709. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7710. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7711. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7712. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7713. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7714. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7715. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7716. #define GET_CONST_OFFSET(type, val) \
  7717. do { \
  7718. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7719. &operand_offset, error_buf, \
  7720. error_buf_size))) \
  7721. goto fail; \
  7722. } while (0)
  7723. #define GET_CONST_F32_OFFSET(type, fval) \
  7724. do { \
  7725. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7726. &operand_offset, error_buf, \
  7727. error_buf_size))) \
  7728. goto fail; \
  7729. } while (0)
  7730. #define GET_CONST_F64_OFFSET(type, fval) \
  7731. do { \
  7732. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7733. &operand_offset, error_buf, \
  7734. error_buf_size))) \
  7735. goto fail; \
  7736. } while (0)
  7737. #define emit_operand(ctx, offset) \
  7738. do { \
  7739. wasm_loader_emit_int16(ctx, offset); \
  7740. LOG_OP("%d\t", offset); \
  7741. } while (0)
  7742. #define emit_byte(ctx, byte) \
  7743. do { \
  7744. wasm_loader_emit_uint8(ctx, byte); \
  7745. LOG_OP("%d\t", byte); \
  7746. } while (0)
  7747. #define emit_uint32(ctx, value) \
  7748. do { \
  7749. wasm_loader_emit_uint32(ctx, value); \
  7750. LOG_OP("%d\t", value); \
  7751. } while (0)
  7752. #define emit_uint64(ctx, value) \
  7753. do { \
  7754. wasm_loader_emit_const(ctx, &value, false); \
  7755. LOG_OP("%lld\t", value); \
  7756. } while (0)
  7757. #define emit_float32(ctx, value) \
  7758. do { \
  7759. wasm_loader_emit_const(ctx, &value, true); \
  7760. LOG_OP("%f\t", value); \
  7761. } while (0)
  7762. #define emit_float64(ctx, value) \
  7763. do { \
  7764. wasm_loader_emit_const(ctx, &value, false); \
  7765. LOG_OP("%f\t", value); \
  7766. } while (0)
  7767. static bool
  7768. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7769. {
  7770. if (!(ctx->p_code_compiled =
  7771. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7772. return false;
  7773. ctx->p_code_compiled_end =
  7774. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7775. /* clean up frame ref */
  7776. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7777. ctx->frame_ref = ctx->frame_ref_bottom;
  7778. ctx->stack_cell_num = 0;
  7779. #if WASM_ENABLE_GC != 0
  7780. /* clean up reftype map */
  7781. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7782. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7783. ctx->reftype_map_num = 0;
  7784. #endif
  7785. /* clean up frame csp */
  7786. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7787. ctx->frame_csp = ctx->frame_csp_bottom;
  7788. ctx->csp_num = 0;
  7789. ctx->max_csp_num = 0;
  7790. /* clean up frame offset */
  7791. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7792. ctx->frame_offset = ctx->frame_offset_bottom;
  7793. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7794. /* init preserved local offsets */
  7795. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7796. /* const buf is reserved */
  7797. return true;
  7798. }
  7799. static void
  7800. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  7801. {
  7802. ctx->code_compiled_size += size;
  7803. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  7804. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  7805. }
  7806. }
  7807. static void
  7808. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  7809. {
  7810. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  7811. if (ctx->p_code_compiled) {
  7812. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7813. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7814. #endif
  7815. bh_memcpy_s(ctx->p_code_compiled,
  7816. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  7817. value, size);
  7818. ctx->p_code_compiled += size;
  7819. }
  7820. else {
  7821. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7822. bh_assert((ctx->code_compiled_size & 1) == 0);
  7823. #endif
  7824. increase_compiled_code_space(ctx, size);
  7825. }
  7826. }
  7827. static void
  7828. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  7829. {
  7830. if (ctx->p_code_compiled) {
  7831. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7832. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7833. #endif
  7834. STORE_U32(ctx->p_code_compiled, value);
  7835. ctx->p_code_compiled += sizeof(uint32);
  7836. }
  7837. else {
  7838. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7839. bh_assert((ctx->code_compiled_size & 1) == 0);
  7840. #endif
  7841. increase_compiled_code_space(ctx, sizeof(uint32));
  7842. }
  7843. }
  7844. static void
  7845. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  7846. {
  7847. if (ctx->p_code_compiled) {
  7848. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7849. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7850. #endif
  7851. STORE_U16(ctx->p_code_compiled, (uint16)value);
  7852. ctx->p_code_compiled += sizeof(int16);
  7853. }
  7854. else {
  7855. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7856. bh_assert((ctx->code_compiled_size & 1) == 0);
  7857. #endif
  7858. increase_compiled_code_space(ctx, sizeof(uint16));
  7859. }
  7860. }
  7861. static void
  7862. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  7863. {
  7864. if (ctx->p_code_compiled) {
  7865. *(ctx->p_code_compiled) = value;
  7866. ctx->p_code_compiled += sizeof(uint8);
  7867. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7868. ctx->p_code_compiled++;
  7869. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7870. #endif
  7871. }
  7872. else {
  7873. increase_compiled_code_space(ctx, sizeof(uint8));
  7874. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7875. increase_compiled_code_space(ctx, sizeof(uint8));
  7876. bh_assert((ctx->code_compiled_size & 1) == 0);
  7877. #endif
  7878. }
  7879. }
  7880. static void
  7881. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  7882. {
  7883. if (ctx->p_code_compiled) {
  7884. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7885. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7886. #endif
  7887. STORE_PTR(ctx->p_code_compiled, value);
  7888. ctx->p_code_compiled += sizeof(void *);
  7889. }
  7890. else {
  7891. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7892. bh_assert((ctx->code_compiled_size & 1) == 0);
  7893. #endif
  7894. increase_compiled_code_space(ctx, sizeof(void *));
  7895. }
  7896. }
  7897. static void
  7898. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  7899. {
  7900. if (ctx->p_code_compiled) {
  7901. ctx->p_code_compiled -= size;
  7902. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7903. if (size == sizeof(uint8)) {
  7904. ctx->p_code_compiled--;
  7905. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7906. }
  7907. #endif
  7908. }
  7909. else {
  7910. ctx->code_compiled_size -= size;
  7911. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7912. if (size == sizeof(uint8)) {
  7913. ctx->code_compiled_size--;
  7914. bh_assert((ctx->code_compiled_size & 1) == 0);
  7915. }
  7916. #endif
  7917. }
  7918. }
  7919. static bool
  7920. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  7921. uint32 local_index, uint32 local_type,
  7922. bool *preserved, char *error_buf,
  7923. uint32 error_buf_size)
  7924. {
  7925. uint32 i = 0;
  7926. int16 preserved_offset = (int16)local_index;
  7927. *preserved = false;
  7928. while (i < loader_ctx->stack_cell_num) {
  7929. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  7930. /* move previous local into dynamic space before a set/tee_local opcode
  7931. */
  7932. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  7933. if (!(*preserved)) {
  7934. *preserved = true;
  7935. skip_label();
  7936. preserved_offset = loader_ctx->preserved_local_offset;
  7937. if (loader_ctx->p_code_compiled) {
  7938. bh_assert(preserved_offset != (int16)local_index);
  7939. }
  7940. if (is_32bit_type(local_type)) {
  7941. /* Only increase preserve offset in the second traversal */
  7942. if (loader_ctx->p_code_compiled)
  7943. loader_ctx->preserved_local_offset++;
  7944. emit_label(EXT_OP_COPY_STACK_TOP);
  7945. }
  7946. else {
  7947. if (loader_ctx->p_code_compiled)
  7948. loader_ctx->preserved_local_offset += 2;
  7949. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  7950. }
  7951. emit_operand(loader_ctx, local_index);
  7952. emit_operand(loader_ctx, preserved_offset);
  7953. emit_label(opcode);
  7954. }
  7955. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  7956. }
  7957. if (is_32bit_type(cur_type))
  7958. i++;
  7959. else
  7960. i += 2;
  7961. }
  7962. (void)error_buf;
  7963. (void)error_buf_size;
  7964. return true;
  7965. }
  7966. static bool
  7967. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  7968. char *error_buf, uint32 error_buf_size)
  7969. {
  7970. uint32 i = 0;
  7971. bool preserve_local;
  7972. /* preserve locals before blocks to ensure that "tee/set_local" inside
  7973. blocks will not influence the value of these locals */
  7974. while (i < loader_ctx->stack_cell_num) {
  7975. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  7976. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  7977. if ((cur_offset < loader_ctx->start_dynamic_offset)
  7978. && (cur_offset >= 0)) {
  7979. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  7980. cur_type, &preserve_local,
  7981. error_buf, error_buf_size)))
  7982. return false;
  7983. }
  7984. if (is_32bit_type(cur_type)) {
  7985. i++;
  7986. }
  7987. else {
  7988. i += 2;
  7989. }
  7990. }
  7991. return true;
  7992. }
  7993. static bool
  7994. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  7995. uint8 *p_code_compiled, char *error_buf,
  7996. uint32 error_buf_size)
  7997. {
  7998. BranchBlockPatch *patch =
  7999. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8000. if (!patch) {
  8001. return false;
  8002. }
  8003. patch->patch_type = patch_type;
  8004. patch->code_compiled = p_code_compiled;
  8005. if (!frame_csp->patch_list) {
  8006. frame_csp->patch_list = patch;
  8007. patch->next = NULL;
  8008. }
  8009. else {
  8010. patch->next = frame_csp->patch_list;
  8011. frame_csp->patch_list = patch;
  8012. }
  8013. return true;
  8014. }
  8015. static void
  8016. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8017. {
  8018. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8019. BranchBlockPatch *node = frame_csp->patch_list;
  8020. BranchBlockPatch *node_prev = NULL, *node_next;
  8021. if (!ctx->p_code_compiled)
  8022. return;
  8023. while (node) {
  8024. node_next = node->next;
  8025. if (node->patch_type == patch_type) {
  8026. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8027. if (node_prev == NULL) {
  8028. frame_csp->patch_list = node_next;
  8029. }
  8030. else {
  8031. node_prev->next = node_next;
  8032. }
  8033. wasm_runtime_free(node);
  8034. }
  8035. else {
  8036. node_prev = node;
  8037. }
  8038. node = node_next;
  8039. }
  8040. }
  8041. static bool
  8042. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8043. bool is_br, char *error_buf, uint32 error_buf_size)
  8044. {
  8045. /* br info layout:
  8046. * a) arity of target block
  8047. * b) total cell num of arity values
  8048. * c) each arity value's cell num
  8049. * d) each arity value's src frame offset
  8050. * e) each arity values's dst dynamic offset
  8051. * f) branch target address
  8052. *
  8053. * Note: b-e are omitted when arity is 0 so that
  8054. * interpreter can recover the br info quickly.
  8055. */
  8056. BlockType *block_type = &frame_csp->block_type;
  8057. uint8 *types = NULL, cell;
  8058. #if WASM_ENABLE_GC != 0
  8059. WASMRefTypeMap *reftype_maps;
  8060. uint32 reftype_map_count;
  8061. #endif
  8062. uint32 arity = 0;
  8063. int32 i;
  8064. int16 *frame_offset = ctx->frame_offset;
  8065. uint16 dynamic_offset;
  8066. /* Note: loop's arity is different from if and block. loop's arity is
  8067. * its parameter count while if and block arity is result count.
  8068. */
  8069. #if WASM_ENABLE_GC == 0
  8070. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8071. arity = block_type_get_param_types(block_type, &types);
  8072. else
  8073. arity = block_type_get_result_types(block_type, &types);
  8074. #else
  8075. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8076. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8077. &reftype_map_count);
  8078. else
  8079. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8080. &reftype_map_count);
  8081. #endif
  8082. /* Part a */
  8083. emit_uint32(ctx, arity);
  8084. if (arity) {
  8085. /* Part b */
  8086. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8087. /* Part c */
  8088. for (i = (int32)arity - 1; i >= 0; i--) {
  8089. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8090. emit_byte(ctx, cell);
  8091. }
  8092. /* Part d */
  8093. for (i = (int32)arity - 1; i >= 0; i--) {
  8094. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8095. frame_offset -= cell;
  8096. emit_operand(ctx, *(int16 *)(frame_offset));
  8097. }
  8098. /* Part e */
  8099. dynamic_offset =
  8100. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8101. if (is_br)
  8102. ctx->dynamic_offset = dynamic_offset;
  8103. for (i = (int32)arity - 1; i >= 0; i--) {
  8104. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8105. dynamic_offset -= cell;
  8106. emit_operand(ctx, dynamic_offset);
  8107. }
  8108. }
  8109. /* Part f */
  8110. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8111. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8112. }
  8113. else {
  8114. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8115. error_buf, error_buf_size))
  8116. return false;
  8117. /* label address, to be patched */
  8118. wasm_loader_emit_ptr(ctx, NULL);
  8119. }
  8120. return true;
  8121. }
  8122. static bool
  8123. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8124. bool disable_emit, int16 operand_offset,
  8125. char *error_buf, uint32 error_buf_size)
  8126. {
  8127. if (type == VALUE_TYPE_VOID)
  8128. return true;
  8129. /* only check memory overflow in first traverse */
  8130. if (ctx->p_code_compiled == NULL) {
  8131. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8132. return false;
  8133. }
  8134. if (disable_emit)
  8135. *(ctx->frame_offset)++ = operand_offset;
  8136. else {
  8137. emit_operand(ctx, ctx->dynamic_offset);
  8138. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8139. ctx->dynamic_offset++;
  8140. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8141. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8142. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8143. goto fail;
  8144. }
  8145. }
  8146. }
  8147. if (is_32bit_type(type))
  8148. return true;
  8149. if (ctx->p_code_compiled == NULL) {
  8150. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8151. return false;
  8152. }
  8153. ctx->frame_offset++;
  8154. if (!disable_emit) {
  8155. ctx->dynamic_offset++;
  8156. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8157. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8158. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8159. goto fail;
  8160. }
  8161. }
  8162. }
  8163. return true;
  8164. fail:
  8165. set_error_buf(error_buf, error_buf_size,
  8166. "fast interpreter offset overflow");
  8167. return false;
  8168. }
  8169. /* This function should be in front of wasm_loader_pop_frame_ref
  8170. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8171. will be modified by wasm_loader_pop_frame_ref */
  8172. static bool
  8173. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8174. char *error_buf, uint32 error_buf_size)
  8175. {
  8176. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8177. then current block is the function block */
  8178. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8179. BranchBlock *cur_block = ctx->frame_csp - depth;
  8180. int32 available_stack_cell =
  8181. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8182. /* Directly return success if current block is in stack
  8183. * polymorphic state while stack is empty. */
  8184. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8185. return true;
  8186. if (type == VALUE_TYPE_VOID)
  8187. return true;
  8188. if (is_32bit_type(type)) {
  8189. /* Check the offset stack bottom to ensure the frame offset
  8190. stack will not go underflow. But we don't thrown error
  8191. and return true here, because the error msg should be
  8192. given in wasm_loader_pop_frame_ref */
  8193. if (!check_offset_pop(ctx, 1))
  8194. return true;
  8195. ctx->frame_offset -= 1;
  8196. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8197. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8198. ctx->dynamic_offset -= 1;
  8199. }
  8200. else {
  8201. if (!check_offset_pop(ctx, 2))
  8202. return true;
  8203. ctx->frame_offset -= 2;
  8204. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8205. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8206. ctx->dynamic_offset -= 2;
  8207. }
  8208. emit_operand(ctx, *(ctx->frame_offset));
  8209. (void)error_buf;
  8210. (void)error_buf_size;
  8211. return true;
  8212. }
  8213. static bool
  8214. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8215. bool disable_emit, int16 operand_offset,
  8216. char *error_buf, uint32 error_buf_size)
  8217. {
  8218. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8219. error_buf, error_buf_size)))
  8220. return false;
  8221. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8222. return false;
  8223. return true;
  8224. }
  8225. static bool
  8226. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8227. char *error_buf, uint32 error_buf_size)
  8228. {
  8229. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8230. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8231. return false;
  8232. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8233. return false;
  8234. return true;
  8235. }
  8236. static bool
  8237. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8238. uint8 type_push, uint8 type_pop,
  8239. bool disable_emit, int16 operand_offset,
  8240. char *error_buf, uint32 error_buf_size)
  8241. {
  8242. uint8 i;
  8243. for (i = 0; i < pop_cnt; i++) {
  8244. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8245. error_buf_size))
  8246. return false;
  8247. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8248. error_buf_size))
  8249. return false;
  8250. }
  8251. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8252. operand_offset, error_buf,
  8253. error_buf_size))
  8254. return false;
  8255. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8256. return false;
  8257. return true;
  8258. }
  8259. static bool
  8260. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8261. int16 *offset, char *error_buf,
  8262. uint32 error_buf_size)
  8263. {
  8264. int8 bytes_to_increase;
  8265. int16 operand_offset = 0;
  8266. Const *c;
  8267. /* Search existing constant */
  8268. for (c = (Const *)ctx->const_buf;
  8269. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8270. /* TODO: handle v128 type? */
  8271. if ((type == c->value_type)
  8272. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8273. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8274. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8275. || (type == VALUE_TYPE_FUNCREF
  8276. && *(int32 *)value == c->value.i32)
  8277. || (type == VALUE_TYPE_EXTERNREF
  8278. && *(int32 *)value == c->value.i32)
  8279. #endif
  8280. || (type == VALUE_TYPE_F64
  8281. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8282. || (type == VALUE_TYPE_F32
  8283. && (0
  8284. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8285. operand_offset = c->slot_index;
  8286. break;
  8287. }
  8288. if (is_32bit_type(c->value_type))
  8289. operand_offset += 1;
  8290. else
  8291. operand_offset += 2;
  8292. }
  8293. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8294. /* New constant, append to the const buffer */
  8295. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8296. bytes_to_increase = 2;
  8297. }
  8298. else {
  8299. bytes_to_increase = 1;
  8300. }
  8301. /* The max cell num of const buffer is 32768 since the valid index range
  8302. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8303. * full */
  8304. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8305. *offset = 0;
  8306. return true;
  8307. }
  8308. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8309. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8310. ctx->const_buf_size + 4 * sizeof(Const));
  8311. ctx->const_buf_size += 4 * sizeof(Const);
  8312. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8313. }
  8314. c->value_type = type;
  8315. switch (type) {
  8316. case VALUE_TYPE_F64:
  8317. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8318. sizeof(float64));
  8319. ctx->const_cell_num += 2;
  8320. /* The const buf will be reversed, we use the second cell */
  8321. /* of the i64/f64 const so the finnal offset is corrent */
  8322. operand_offset++;
  8323. break;
  8324. case VALUE_TYPE_I64:
  8325. c->value.i64 = *(int64 *)value;
  8326. ctx->const_cell_num += 2;
  8327. operand_offset++;
  8328. break;
  8329. case VALUE_TYPE_F32:
  8330. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8331. sizeof(float32));
  8332. ctx->const_cell_num++;
  8333. break;
  8334. case VALUE_TYPE_I32:
  8335. c->value.i32 = *(int32 *)value;
  8336. ctx->const_cell_num++;
  8337. break;
  8338. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8339. case VALUE_TYPE_EXTERNREF:
  8340. case VALUE_TYPE_FUNCREF:
  8341. c->value.i32 = *(int32 *)value;
  8342. ctx->const_cell_num++;
  8343. break;
  8344. #endif
  8345. default:
  8346. break;
  8347. }
  8348. c->slot_index = operand_offset;
  8349. ctx->num_const++;
  8350. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8351. (int64)c->value.i64);
  8352. }
  8353. /* use negetive index for const */
  8354. operand_offset = -(operand_offset + 1);
  8355. *offset = operand_offset;
  8356. return true;
  8357. fail:
  8358. return false;
  8359. }
  8360. /*
  8361. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8362. -- Mostly used for the binary / compare operation
  8363. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8364. -- Mostly used in block / control instructions
  8365. The POP will always emit the offset on the top of the frame_offset stack
  8366. PUSH can be used in two ways:
  8367. 1. directly PUSH:
  8368. PUSH_XXX();
  8369. will allocate a dynamic space and emit
  8370. 2. silent PUSH:
  8371. operand_offset = xxx; disable_emit = true;
  8372. PUSH_XXX();
  8373. only push the frame_offset stack, no emit
  8374. */
  8375. #define TEMPLATE_PUSH(Type) \
  8376. do { \
  8377. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8378. disable_emit, operand_offset, \
  8379. error_buf, error_buf_size)) \
  8380. goto fail; \
  8381. } while (0)
  8382. #define TEMPLATE_PUSH_REF(Type) \
  8383. do { \
  8384. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8385. operand_offset, error_buf, \
  8386. error_buf_size)) \
  8387. goto fail; \
  8388. } while (0)
  8389. #define TEMPLATE_POP(Type) \
  8390. do { \
  8391. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8392. error_buf, error_buf_size)) \
  8393. goto fail; \
  8394. } while (0)
  8395. #define TEMPLATE_POP_REF(Type) \
  8396. do { \
  8397. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8398. error_buf_size)) \
  8399. goto fail; \
  8400. } while (0)
  8401. #define PUSH_OFFSET_TYPE(type) \
  8402. do { \
  8403. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8404. operand_offset, error_buf, \
  8405. error_buf_size))) \
  8406. goto fail; \
  8407. } while (0)
  8408. #define POP_OFFSET_TYPE(type) \
  8409. do { \
  8410. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8411. error_buf_size))) \
  8412. goto fail; \
  8413. } while (0)
  8414. #define POP_AND_PUSH(type_pop, type_push) \
  8415. do { \
  8416. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8417. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8418. operand_offset, error_buf, error_buf_size))) \
  8419. goto fail; \
  8420. } while (0)
  8421. /* type of POPs should be the same */
  8422. #define POP2_AND_PUSH(type_pop, type_push) \
  8423. do { \
  8424. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8425. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8426. operand_offset, error_buf, error_buf_size))) \
  8427. goto fail; \
  8428. } while (0)
  8429. #else /* WASM_ENABLE_FAST_INTERP */
  8430. #define TEMPLATE_PUSH(Type) \
  8431. do { \
  8432. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8433. error_buf, error_buf_size))) \
  8434. goto fail; \
  8435. } while (0)
  8436. #define TEMPLATE_PUSH_REF(Type) \
  8437. do { \
  8438. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8439. error_buf_size))) \
  8440. goto fail; \
  8441. } while (0)
  8442. #define TEMPLATE_POP(Type) \
  8443. do { \
  8444. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8445. error_buf, error_buf_size))) \
  8446. goto fail; \
  8447. } while (0)
  8448. #define TEMPLATE_POP_REF(Type) \
  8449. do { \
  8450. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8451. error_buf_size))) \
  8452. goto fail; \
  8453. } while (0)
  8454. #define POP_AND_PUSH(type_pop, type_push) \
  8455. do { \
  8456. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8457. type_pop, error_buf, \
  8458. error_buf_size))) \
  8459. goto fail; \
  8460. } while (0)
  8461. /* type of POPs should be the same */
  8462. #define POP2_AND_PUSH(type_pop, type_push) \
  8463. do { \
  8464. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8465. type_pop, error_buf, \
  8466. error_buf_size))) \
  8467. goto fail; \
  8468. } while (0)
  8469. #endif /* WASM_ENABLE_FAST_INTERP */
  8470. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8471. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8472. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8473. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8474. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8475. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8476. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8477. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8478. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8479. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8480. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8481. #define POP_I32() TEMPLATE_POP(I32)
  8482. #define POP_F32() TEMPLATE_POP(F32)
  8483. #define POP_I64() TEMPLATE_POP(I64)
  8484. #define POP_F64() TEMPLATE_POP(F64)
  8485. #define POP_V128() TEMPLATE_POP(V128)
  8486. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8487. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8488. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8489. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8490. #if WASM_ENABLE_FAST_INTERP != 0
  8491. static bool
  8492. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8493. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8494. {
  8495. int16 operand_offset = 0;
  8496. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8497. : loader_ctx->frame_csp;
  8498. BlockType *block_type = &block->block_type;
  8499. uint8 *return_types = NULL;
  8500. #if WASM_ENABLE_GC != 0
  8501. WASMRefTypeMap *reftype_maps = NULL;
  8502. uint32 reftype_map_count;
  8503. #endif
  8504. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8505. int32 i = 0;
  8506. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8507. *frame_offset_org = NULL;
  8508. #if WASM_ENABLE_GC == 0
  8509. return_count = block_type_get_result_types(block_type, &return_types);
  8510. #else
  8511. return_count = block_type_get_result_types(
  8512. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8513. #endif
  8514. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8515. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8516. if (return_count == 1) {
  8517. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8518. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8519. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8520. /* insert op_copy before else opcode */
  8521. if (opcode == WASM_OP_ELSE)
  8522. skip_label();
  8523. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8524. : EXT_OP_COPY_STACK_TOP_I64);
  8525. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8526. emit_operand(loader_ctx, block->dynamic_offset);
  8527. if (opcode == WASM_OP_ELSE) {
  8528. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8529. }
  8530. else {
  8531. loader_ctx->frame_offset -= cell;
  8532. loader_ctx->dynamic_offset = block->dynamic_offset;
  8533. PUSH_OFFSET_TYPE(return_types[0]);
  8534. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8535. }
  8536. if (opcode == WASM_OP_ELSE)
  8537. emit_label(opcode);
  8538. }
  8539. return true;
  8540. }
  8541. /* Copy stack top values to block's results which are in dynamic space.
  8542. * The instruction format:
  8543. * Part a: values count
  8544. * Part b: all values total cell num
  8545. * Part c: each value's cell_num, src offset and dst offset
  8546. * Part d: each value's src offset and dst offset
  8547. * Part e: each value's dst offset
  8548. */
  8549. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8550. dynamic_offset = dynamic_offset_org =
  8551. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8552. /* First traversal to get the count of values needed to be copied. */
  8553. for (i = (int32)return_count - 1; i >= 0; i--) {
  8554. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8555. frame_offset -= cells;
  8556. dynamic_offset -= cells;
  8557. if (dynamic_offset != *frame_offset) {
  8558. value_count++;
  8559. total_cel_num += cells;
  8560. }
  8561. }
  8562. if (value_count) {
  8563. uint32 j = 0;
  8564. uint8 *emit_data = NULL, *cells = NULL;
  8565. int16 *src_offsets = NULL;
  8566. uint16 *dst_offsets = NULL;
  8567. uint64 size =
  8568. (uint64)value_count
  8569. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8570. /* Allocate memory for the emit data */
  8571. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8572. return false;
  8573. cells = emit_data;
  8574. src_offsets = (int16 *)(cells + value_count);
  8575. dst_offsets = (uint16 *)(src_offsets + value_count);
  8576. /* insert op_copy before else opcode */
  8577. if (opcode == WASM_OP_ELSE)
  8578. skip_label();
  8579. emit_label(EXT_OP_COPY_STACK_VALUES);
  8580. /* Part a) */
  8581. emit_uint32(loader_ctx, value_count);
  8582. /* Part b) */
  8583. emit_uint32(loader_ctx, total_cel_num);
  8584. /* Second traversal to get each value's cell num, src offset and dst
  8585. * offset. */
  8586. frame_offset = frame_offset_org;
  8587. dynamic_offset = dynamic_offset_org;
  8588. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8589. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8590. frame_offset -= cell;
  8591. dynamic_offset -= cell;
  8592. if (dynamic_offset != *frame_offset) {
  8593. /* cell num */
  8594. cells[j] = cell;
  8595. /* src offset */
  8596. src_offsets[j] = *frame_offset;
  8597. /* dst offset */
  8598. dst_offsets[j] = dynamic_offset;
  8599. j++;
  8600. }
  8601. if (opcode == WASM_OP_ELSE) {
  8602. *frame_offset = dynamic_offset;
  8603. }
  8604. else {
  8605. loader_ctx->frame_offset = frame_offset;
  8606. loader_ctx->dynamic_offset = dynamic_offset;
  8607. PUSH_OFFSET_TYPE(return_types[i]);
  8608. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8609. loader_ctx->frame_offset = frame_offset_org;
  8610. loader_ctx->dynamic_offset = dynamic_offset_org;
  8611. }
  8612. }
  8613. bh_assert(j == value_count);
  8614. /* Emit the cells, src_offsets and dst_offsets */
  8615. for (j = 0; j < value_count; j++)
  8616. emit_byte(loader_ctx, cells[j]);
  8617. for (j = 0; j < value_count; j++)
  8618. emit_operand(loader_ctx, src_offsets[j]);
  8619. for (j = 0; j < value_count; j++)
  8620. emit_operand(loader_ctx, dst_offsets[j]);
  8621. if (opcode == WASM_OP_ELSE)
  8622. emit_label(opcode);
  8623. wasm_runtime_free(emit_data);
  8624. }
  8625. return true;
  8626. fail:
  8627. return false;
  8628. }
  8629. #endif /* WASM_ENABLE_FAST_INTERP */
  8630. #define RESERVE_BLOCK_RET() \
  8631. do { \
  8632. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8633. error_buf_size)) \
  8634. goto fail; \
  8635. } while (0)
  8636. #define PUSH_TYPE(type) \
  8637. do { \
  8638. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8639. error_buf_size))) \
  8640. goto fail; \
  8641. } while (0)
  8642. #define POP_TYPE(type) \
  8643. do { \
  8644. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8645. error_buf_size))) \
  8646. goto fail; \
  8647. } while (0)
  8648. #if WASM_ENABLE_GC == 0
  8649. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8650. do { \
  8651. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8652. _start_addr, error_buf, \
  8653. error_buf_size)) \
  8654. goto fail; \
  8655. } while (0)
  8656. #define POP_CSP() \
  8657. do { \
  8658. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8659. goto fail; \
  8660. } while (0)
  8661. #else
  8662. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8663. do { \
  8664. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8665. _start_addr, error_buf, \
  8666. error_buf_size)) \
  8667. goto fail; \
  8668. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8669. error_buf, error_buf_size)) { \
  8670. goto fail; \
  8671. } \
  8672. } while (0)
  8673. #define POP_CSP() \
  8674. do { \
  8675. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8676. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8677. goto fail; \
  8678. } while (0)
  8679. #endif /* end of WASM_ENABLE_GC == 0 */
  8680. #if WASM_ENABLE_GC == 0
  8681. #define GET_LOCAL_REFTYPE() (void)0
  8682. #else
  8683. #define GET_LOCAL_REFTYPE() \
  8684. do { \
  8685. if (wasm_is_type_multi_byte_type(local_type)) { \
  8686. WASMRefType *_ref_type; \
  8687. if (local_idx < param_count) \
  8688. _ref_type = wasm_reftype_map_find( \
  8689. param_reftype_maps, param_reftype_map_count, local_idx); \
  8690. else \
  8691. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8692. local_reftype_map_count, \
  8693. local_idx - param_count); \
  8694. bh_assert(_ref_type); \
  8695. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8696. wasm_reftype_struct_size(_ref_type)); \
  8697. } \
  8698. } while (0)
  8699. #endif /* end of WASM_ENABLE_GC == 0 */
  8700. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8701. do { \
  8702. read_leb_uint32(p, p_end, local_idx); \
  8703. if (local_idx >= param_count + local_count) { \
  8704. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8705. goto fail; \
  8706. } \
  8707. local_type = local_idx < param_count \
  8708. ? param_types[local_idx] \
  8709. : local_types[local_idx - param_count]; \
  8710. local_offset = local_offsets[local_idx]; \
  8711. GET_LOCAL_REFTYPE(); \
  8712. } while (0)
  8713. static bool
  8714. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8715. {
  8716. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8717. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8718. return false;
  8719. }
  8720. return true;
  8721. }
  8722. #define CHECK_MEMORY() \
  8723. do { \
  8724. if (!check_memory(module, error_buf, error_buf_size)) \
  8725. goto fail; \
  8726. } while (0)
  8727. static bool
  8728. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8729. uint32 error_buf_size)
  8730. {
  8731. uint8 mem_access_aligns[] = {
  8732. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8733. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8734. };
  8735. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8736. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8737. set_error_buf(error_buf, error_buf_size,
  8738. "alignment must not be larger than natural");
  8739. return false;
  8740. }
  8741. return true;
  8742. }
  8743. #if WASM_ENABLE_SIMD != 0
  8744. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8745. static bool
  8746. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8747. uint32 error_buf_size)
  8748. {
  8749. uint8 mem_access_aligns[] = {
  8750. 4, /* load */
  8751. 3, 3, 3, 3, 3, 3, /* load and extend */
  8752. 0, 1, 2, 3, /* load and splat */
  8753. 4, /* store */
  8754. };
  8755. uint8 mem_access_aligns_load_lane[] = {
  8756. 0, 1, 2, 3, /* load lane */
  8757. 0, 1, 2, 3, /* store lane */
  8758. 2, 3 /* store zero */
  8759. };
  8760. if (!((opcode <= SIMD_v128_store)
  8761. || (SIMD_v128_load8_lane <= opcode
  8762. && opcode <= SIMD_v128_load64_zero))) {
  8763. set_error_buf(error_buf, error_buf_size,
  8764. "the opcode doesn't include memarg");
  8765. return false;
  8766. }
  8767. if ((opcode <= SIMD_v128_store
  8768. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8769. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8770. && align > mem_access_aligns_load_lane[opcode
  8771. - SIMD_v128_load8_lane])) {
  8772. set_error_buf(error_buf, error_buf_size,
  8773. "alignment must not be larger than natural");
  8774. return false;
  8775. }
  8776. return true;
  8777. }
  8778. static bool
  8779. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8780. uint32 error_buf_size)
  8781. {
  8782. switch (opcode) {
  8783. case SIMD_i8x16_extract_lane_s:
  8784. case SIMD_i8x16_extract_lane_u:
  8785. case SIMD_i8x16_replace_lane:
  8786. if (lane >= 16) {
  8787. goto fail;
  8788. }
  8789. break;
  8790. case SIMD_i16x8_extract_lane_s:
  8791. case SIMD_i16x8_extract_lane_u:
  8792. case SIMD_i16x8_replace_lane:
  8793. if (lane >= 8) {
  8794. goto fail;
  8795. }
  8796. break;
  8797. case SIMD_i32x4_extract_lane:
  8798. case SIMD_i32x4_replace_lane:
  8799. case SIMD_f32x4_extract_lane:
  8800. case SIMD_f32x4_replace_lane:
  8801. if (lane >= 4) {
  8802. goto fail;
  8803. }
  8804. break;
  8805. case SIMD_i64x2_extract_lane:
  8806. case SIMD_i64x2_replace_lane:
  8807. case SIMD_f64x2_extract_lane:
  8808. case SIMD_f64x2_replace_lane:
  8809. if (lane >= 2) {
  8810. goto fail;
  8811. }
  8812. break;
  8813. case SIMD_v128_load8_lane:
  8814. case SIMD_v128_load16_lane:
  8815. case SIMD_v128_load32_lane:
  8816. case SIMD_v128_load64_lane:
  8817. case SIMD_v128_store8_lane:
  8818. case SIMD_v128_store16_lane:
  8819. case SIMD_v128_store32_lane:
  8820. case SIMD_v128_store64_lane:
  8821. case SIMD_v128_load32_zero:
  8822. case SIMD_v128_load64_zero:
  8823. {
  8824. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  8825. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  8826. goto fail;
  8827. }
  8828. break;
  8829. }
  8830. default:
  8831. goto fail;
  8832. }
  8833. return true;
  8834. fail:
  8835. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8836. return false;
  8837. }
  8838. static bool
  8839. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  8840. {
  8841. uint8 i;
  8842. for (i = 0; i != 16; ++i) {
  8843. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  8844. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8845. return false;
  8846. }
  8847. }
  8848. return true;
  8849. }
  8850. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8851. #endif /* end of WASM_ENABLE_SIMD */
  8852. #if WASM_ENABLE_SHARED_MEMORY != 0
  8853. static bool
  8854. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  8855. uint32 error_buf_size)
  8856. {
  8857. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  8858. uint8 mem_access_aligns[] = {
  8859. 2, 3, 0, 1, 0, 1, 2,
  8860. };
  8861. uint8 expect;
  8862. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  8863. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  8864. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  8865. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  8866. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  8867. }
  8868. else {
  8869. /* 7 opcodes in every group */
  8870. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  8871. }
  8872. if (align != expect) {
  8873. set_error_buf(error_buf, error_buf_size,
  8874. "alignment isn't equal to natural");
  8875. return false;
  8876. }
  8877. return true;
  8878. }
  8879. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  8880. static bool
  8881. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  8882. char *error_buf, uint32 error_buf_size)
  8883. {
  8884. BranchBlock *target_block, *cur_block;
  8885. BlockType *target_block_type;
  8886. uint8 type, *types = NULL, *frame_ref;
  8887. uint32 arity = 0;
  8888. int32 i, available_stack_cell;
  8889. uint16 cell_num;
  8890. #if WASM_ENABLE_GC != 0
  8891. WASMRefTypeMap *frame_reftype_map;
  8892. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  8893. WASMRefType *ref_type;
  8894. uint32 reftype_map_count = 0;
  8895. int32 available_reftype_map;
  8896. bool is_type_multi_byte;
  8897. #endif
  8898. uint8 *frame_ref_old = loader_ctx->frame_ref;
  8899. uint8 *frame_ref_after_popped = NULL;
  8900. uint8 frame_ref_tmp[4] = { 0 };
  8901. uint8 *frame_ref_buf = frame_ref_tmp;
  8902. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  8903. #if WASM_ENABLE_GC != 0
  8904. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  8905. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  8906. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  8907. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  8908. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  8909. #endif
  8910. #if WASM_ENABLE_FAST_INTERP != 0
  8911. int16 *frame_offset_old = loader_ctx->frame_offset;
  8912. int16 *frame_offset_after_popped = NULL;
  8913. int16 frame_offset_tmp[4] = { 0 };
  8914. int16 *frame_offset_buf = frame_offset_tmp;
  8915. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  8916. #endif
  8917. bool ret = false;
  8918. bh_assert(loader_ctx->csp_num > 0);
  8919. if (loader_ctx->csp_num - 1 < depth) {
  8920. set_error_buf(error_buf, error_buf_size,
  8921. "unknown label, "
  8922. "unexpected end of section or function");
  8923. return false;
  8924. }
  8925. cur_block = loader_ctx->frame_csp - 1;
  8926. target_block = loader_ctx->frame_csp - (depth + 1);
  8927. target_block_type = &target_block->block_type;
  8928. frame_ref = loader_ctx->frame_ref;
  8929. #if WASM_ENABLE_GC != 0
  8930. frame_reftype_map = loader_ctx->frame_reftype_map;
  8931. #endif
  8932. /* Note: loop's arity is different from if and block. loop's arity is
  8933. * its parameter count while if and block arity is result count.
  8934. */
  8935. #if WASM_ENABLE_GC == 0
  8936. if (target_block->label_type == LABEL_TYPE_LOOP)
  8937. arity = block_type_get_param_types(target_block_type, &types);
  8938. else
  8939. arity = block_type_get_result_types(target_block_type, &types);
  8940. #else
  8941. if (target_block->label_type == LABEL_TYPE_LOOP)
  8942. arity = block_type_get_param_types(target_block_type, &types,
  8943. &reftype_maps, &reftype_map_count);
  8944. else
  8945. arity = block_type_get_result_types(target_block_type, &types,
  8946. &reftype_maps, &reftype_map_count);
  8947. #endif
  8948. /* If the stack is in polymorphic state, just clear the stack
  8949. * and then re-push the values to make the stack top values
  8950. * match block type. */
  8951. if (cur_block->is_stack_polymorphic) {
  8952. #if WASM_ENABLE_GC != 0
  8953. int32 j = reftype_map_count - 1;
  8954. #endif
  8955. for (i = (int32)arity - 1; i >= 0; i--) {
  8956. #if WASM_ENABLE_GC != 0
  8957. if (wasm_is_type_multi_byte_type(types[i])) {
  8958. bh_assert(reftype_maps[j].index == i);
  8959. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  8960. reftype_maps[j].ref_type,
  8961. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  8962. j--;
  8963. }
  8964. #endif
  8965. #if WASM_ENABLE_FAST_INTERP != 0
  8966. POP_OFFSET_TYPE(types[i]);
  8967. #endif
  8968. POP_TYPE(types[i]);
  8969. }
  8970. /* Backup stack data since it may be changed in the below
  8971. push operations, and the stack data may be used when
  8972. checking other target blocks of opcode br_table */
  8973. if (opcode == WASM_OP_BR_TABLE) {
  8974. uint64 total_size;
  8975. frame_ref_after_popped = loader_ctx->frame_ref;
  8976. total_size = (uint64)sizeof(uint8)
  8977. * (frame_ref_old - frame_ref_after_popped);
  8978. if (total_size > sizeof(frame_ref_tmp)
  8979. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  8980. error_buf_size))) {
  8981. goto fail;
  8982. }
  8983. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  8984. frame_ref_after_popped, (uint32)total_size);
  8985. #if WASM_ENABLE_GC != 0
  8986. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  8987. total_size =
  8988. (uint64)sizeof(WASMRefTypeMap)
  8989. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  8990. if (total_size > sizeof(frame_reftype_map_tmp)
  8991. && !(frame_reftype_map_buf = loader_malloc(
  8992. total_size, error_buf, error_buf_size))) {
  8993. goto fail;
  8994. }
  8995. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  8996. frame_reftype_map_after_popped, (uint32)total_size);
  8997. #endif
  8998. #if WASM_ENABLE_FAST_INTERP != 0
  8999. frame_offset_after_popped = loader_ctx->frame_offset;
  9000. total_size = (uint64)sizeof(int16)
  9001. * (frame_offset_old - frame_offset_after_popped);
  9002. if (total_size > sizeof(frame_offset_tmp)
  9003. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9004. error_buf_size))) {
  9005. goto fail;
  9006. }
  9007. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9008. frame_offset_after_popped, (uint32)total_size);
  9009. #endif
  9010. }
  9011. #if WASM_ENABLE_GC != 0
  9012. j = 0;
  9013. #endif
  9014. for (i = 0; i < (int32)arity; i++) {
  9015. #if WASM_ENABLE_GC != 0
  9016. if (wasm_is_type_multi_byte_type(types[i])) {
  9017. bh_assert(reftype_maps[j].index == i);
  9018. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9019. reftype_maps[j].ref_type,
  9020. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9021. j++;
  9022. }
  9023. #endif
  9024. #if WASM_ENABLE_FAST_INTERP != 0
  9025. bool disable_emit = true;
  9026. int16 operand_offset = 0;
  9027. PUSH_OFFSET_TYPE(types[i]);
  9028. #endif
  9029. PUSH_TYPE(types[i]);
  9030. }
  9031. #if WASM_ENABLE_FAST_INTERP != 0
  9032. emit_br_info(target_block, opcode == WASM_OP_BR);
  9033. #endif
  9034. /* Restore the stack data, note that frame_ref_bottom,
  9035. frame_reftype_map_bottom, frame_offset_bottom may be
  9036. re-allocated in the above push operations */
  9037. if (opcode == WASM_OP_BR_TABLE) {
  9038. uint32 total_size;
  9039. /* The stack operand num should not be smaller than before
  9040. after pop and push operations */
  9041. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9042. loader_ctx->stack_cell_num = stack_cell_num_old;
  9043. loader_ctx->frame_ref =
  9044. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9045. total_size = (uint32)(sizeof(uint8)
  9046. * (frame_ref_old - frame_ref_after_popped));
  9047. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9048. frame_ref_buf, total_size);
  9049. #if WASM_ENABLE_GC != 0
  9050. /* The stack operand num should not be smaller than before
  9051. after pop and push operations */
  9052. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9053. loader_ctx->reftype_map_num = reftype_map_num_old;
  9054. loader_ctx->frame_reftype_map =
  9055. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9056. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9057. * (frame_reftype_map_old
  9058. - frame_reftype_map_after_popped));
  9059. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9060. total_size, frame_reftype_map_buf, total_size);
  9061. #endif
  9062. #if WASM_ENABLE_FAST_INTERP != 0
  9063. loader_ctx->frame_offset =
  9064. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9065. total_size =
  9066. (uint32)(sizeof(int16)
  9067. * (frame_offset_old - frame_offset_after_popped));
  9068. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9069. total_size, frame_offset_buf, total_size);
  9070. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9071. #endif
  9072. }
  9073. ret = true;
  9074. goto cleanup_and_return;
  9075. }
  9076. available_stack_cell =
  9077. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9078. #if WASM_ENABLE_GC != 0
  9079. available_reftype_map =
  9080. (int32)(loader_ctx->reftype_map_num
  9081. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9082. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9083. #endif
  9084. /* Check stack top values match target block type */
  9085. for (i = (int32)arity - 1; i >= 0; i--) {
  9086. type = types[i];
  9087. #if WASM_ENABLE_GC != 0
  9088. ref_type = NULL;
  9089. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9090. if (is_type_multi_byte) {
  9091. bh_assert(reftype_map);
  9092. ref_type = reftype_map->ref_type;
  9093. }
  9094. #endif
  9095. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9096. break;
  9097. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9098. #if WASM_ENABLE_GC != 0
  9099. frame_reftype_map, available_reftype_map,
  9100. #endif
  9101. type,
  9102. #if WASM_ENABLE_GC != 0
  9103. ref_type,
  9104. #endif
  9105. error_buf, error_buf_size)) {
  9106. goto fail;
  9107. }
  9108. cell_num = wasm_value_type_cell_num(types[i]);
  9109. frame_ref -= cell_num;
  9110. available_stack_cell -= cell_num;
  9111. #if WASM_ENABLE_GC != 0
  9112. if (is_type_multi_byte) {
  9113. frame_reftype_map--;
  9114. available_reftype_map--;
  9115. reftype_map--;
  9116. }
  9117. #endif
  9118. }
  9119. #if WASM_ENABLE_FAST_INTERP != 0
  9120. emit_br_info(target_block, opcode == WASM_OP_BR);
  9121. #endif
  9122. ret = true;
  9123. cleanup_and_return:
  9124. fail:
  9125. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9126. wasm_runtime_free(frame_ref_buf);
  9127. #if WASM_ENABLE_GC != 0
  9128. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9129. wasm_runtime_free(frame_reftype_map_buf);
  9130. #endif
  9131. #if WASM_ENABLE_FAST_INTERP != 0
  9132. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9133. wasm_runtime_free(frame_offset_buf);
  9134. #endif
  9135. return ret;
  9136. }
  9137. static BranchBlock *
  9138. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9139. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9140. {
  9141. uint8 *p = *p_buf, *p_end = buf_end;
  9142. BranchBlock *frame_csp_tmp;
  9143. uint32 depth;
  9144. read_leb_uint32(p, p_end, depth);
  9145. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9146. error_buf_size)) {
  9147. goto fail;
  9148. }
  9149. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9150. *p_buf = p;
  9151. return frame_csp_tmp;
  9152. fail:
  9153. return NULL;
  9154. }
  9155. #if WASM_ENABLE_EXCE_HANDLING != 0
  9156. static BranchBlock *
  9157. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9158. uint8 *buf_end, char *error_buf,
  9159. uint32 error_buf_size)
  9160. {
  9161. uint8 *p = *p_buf, *p_end = buf_end;
  9162. BranchBlock *frame_csp_tmp;
  9163. uint32 depth;
  9164. read_leb_uint32(p, p_end, depth);
  9165. /*
  9166. * Note: "delegate 0" means the surrounding block, not the
  9167. * try-delegate block itself.
  9168. *
  9169. * Note: the caller hasn't popped the try-delegate frame yet.
  9170. */
  9171. bh_assert(loader_ctx->csp_num > 0);
  9172. if (loader_ctx->csp_num - 1 <= depth) {
  9173. #if WASM_ENABLE_SPEC_TEST == 0
  9174. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9175. #else
  9176. set_error_buf(error_buf, error_buf_size, "unknown label");
  9177. #endif
  9178. goto fail;
  9179. }
  9180. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9181. #if WASM_ENABLE_FAST_INTERP != 0
  9182. emit_br_info(frame_csp_tmp, false);
  9183. #endif
  9184. *p_buf = p;
  9185. return frame_csp_tmp;
  9186. fail:
  9187. return NULL;
  9188. }
  9189. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9190. static bool
  9191. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9192. char *error_buf, uint32 error_buf_size)
  9193. {
  9194. BlockType *block_type = &block->block_type;
  9195. uint8 *return_types = NULL;
  9196. uint32 return_count = 0;
  9197. int32 available_stack_cell, return_cell_num, i;
  9198. uint8 *frame_ref = NULL;
  9199. #if WASM_ENABLE_GC != 0
  9200. WASMRefTypeMap *frame_reftype_map;
  9201. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9202. WASMRefType *ref_type;
  9203. uint32 param_count, return_reftype_map_count = 0;
  9204. int32 available_reftype_map =
  9205. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9206. #endif
  9207. available_stack_cell =
  9208. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9209. #if WASM_ENABLE_GC == 0
  9210. return_count = block_type_get_result_types(block_type, &return_types);
  9211. #else
  9212. return_count = block_type_get_result_types(block_type, &return_types,
  9213. &return_reftype_maps,
  9214. &return_reftype_map_count);
  9215. param_count =
  9216. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9217. (void)param_count;
  9218. #endif
  9219. return_cell_num =
  9220. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9221. /* If the stack is in polymorphic state, just clear the stack
  9222. * and then re-push the values to make the stack top values
  9223. * match block type. */
  9224. if (block->is_stack_polymorphic) {
  9225. #if WASM_ENABLE_GC != 0
  9226. int32 j = return_reftype_map_count - 1;
  9227. #endif
  9228. for (i = (int32)return_count - 1; i >= 0; i--) {
  9229. #if WASM_ENABLE_GC != 0
  9230. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9231. bh_assert(return_reftype_maps[j].index == i + param_count);
  9232. bh_memcpy_s(
  9233. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9234. return_reftype_maps[j].ref_type,
  9235. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9236. j--;
  9237. }
  9238. #endif
  9239. #if WASM_ENABLE_FAST_INTERP != 0
  9240. POP_OFFSET_TYPE(return_types[i]);
  9241. #endif
  9242. POP_TYPE(return_types[i]);
  9243. }
  9244. /* Check stack is empty */
  9245. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9246. set_error_buf(
  9247. error_buf, error_buf_size,
  9248. "type mismatch: stack size does not match block type");
  9249. goto fail;
  9250. }
  9251. #if WASM_ENABLE_GC != 0
  9252. j = 0;
  9253. #endif
  9254. for (i = 0; i < (int32)return_count; i++) {
  9255. #if WASM_ENABLE_GC != 0
  9256. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9257. bh_assert(return_reftype_maps[j].index == i + param_count);
  9258. bh_memcpy_s(
  9259. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9260. return_reftype_maps[j].ref_type,
  9261. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9262. j++;
  9263. }
  9264. #endif
  9265. #if WASM_ENABLE_FAST_INTERP != 0
  9266. bool disable_emit = true;
  9267. int16 operand_offset = 0;
  9268. PUSH_OFFSET_TYPE(return_types[i]);
  9269. #endif
  9270. PUSH_TYPE(return_types[i]);
  9271. }
  9272. return true;
  9273. }
  9274. if (available_stack_cell != return_cell_num) {
  9275. #if WASM_ENABLE_EXCE_HANDLING != 0
  9276. /* testspec: this error message format is expected by try_catch.wast */
  9277. snprintf(
  9278. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9279. block->label_type == LABEL_TYPE_TRY
  9280. || (block->label_type == LABEL_TYPE_CATCH
  9281. && return_cell_num > 0)
  9282. ? "instruction"
  9283. : "block",
  9284. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9285. " but stack has ",
  9286. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9287. : "");
  9288. goto fail;
  9289. #else
  9290. set_error_buf(error_buf, error_buf_size,
  9291. "type mismatch: stack size does not match block type");
  9292. goto fail;
  9293. #endif
  9294. }
  9295. /* Check stack values match return types */
  9296. frame_ref = loader_ctx->frame_ref;
  9297. #if WASM_ENABLE_GC != 0
  9298. frame_reftype_map = loader_ctx->frame_reftype_map;
  9299. return_reftype_map =
  9300. return_reftype_map_count
  9301. ? return_reftype_maps + return_reftype_map_count - 1
  9302. : NULL;
  9303. #endif
  9304. for (i = (int32)return_count - 1; i >= 0; i--) {
  9305. uint8 type = return_types[i];
  9306. #if WASM_ENABLE_GC != 0
  9307. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9308. ref_type = NULL;
  9309. if (is_type_multi_byte) {
  9310. bh_assert(return_reftype_map);
  9311. ref_type = return_reftype_map->ref_type;
  9312. }
  9313. #endif
  9314. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9315. #if WASM_ENABLE_GC != 0
  9316. frame_reftype_map, available_reftype_map,
  9317. #endif
  9318. type,
  9319. #if WASM_ENABLE_GC != 0
  9320. ref_type,
  9321. #endif
  9322. error_buf, error_buf_size))
  9323. return false;
  9324. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9325. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9326. #if WASM_ENABLE_GC != 0
  9327. if (is_type_multi_byte) {
  9328. frame_reftype_map--;
  9329. available_reftype_map--;
  9330. return_reftype_map--;
  9331. }
  9332. #endif
  9333. }
  9334. return true;
  9335. fail:
  9336. return false;
  9337. }
  9338. #if WASM_ENABLE_FAST_INTERP != 0
  9339. /* Copy parameters to dynamic space.
  9340. * 1) POP original parameter out;
  9341. * 2) Push and copy original values to dynamic space.
  9342. * The copy instruction format:
  9343. * Part a: param count
  9344. * Part b: all param total cell num
  9345. * Part c: each param's cell_num, src offset and dst offset
  9346. * Part d: each param's src offset
  9347. * Part e: each param's dst offset
  9348. */
  9349. static bool
  9350. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  9351. char *error_buf, uint32 error_buf_size)
  9352. {
  9353. bool ret = false;
  9354. int16 *frame_offset = NULL;
  9355. uint8 *cells = NULL, cell;
  9356. int16 *src_offsets = NULL;
  9357. uint8 *emit_data = NULL;
  9358. uint32 i;
  9359. BranchBlock *block = loader_ctx->frame_csp - 1;
  9360. BlockType *block_type = &block->block_type;
  9361. WASMFuncType *wasm_type = block_type->u.type;
  9362. uint32 param_count = block_type->u.type->param_count;
  9363. int16 condition_offset = 0;
  9364. bool disable_emit = false;
  9365. int16 operand_offset = 0;
  9366. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9367. bh_assert(size > 0);
  9368. /* For if block, we also need copy the condition operand offset. */
  9369. if (is_if_block)
  9370. size += sizeof(*cells) + sizeof(*src_offsets);
  9371. /* Allocate memory for the emit data */
  9372. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9373. return false;
  9374. cells = emit_data;
  9375. src_offsets = (int16 *)(cells + param_count);
  9376. if (is_if_block)
  9377. condition_offset = *loader_ctx->frame_offset;
  9378. /* POP original parameter out */
  9379. for (i = 0; i < param_count; i++) {
  9380. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9381. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9382. }
  9383. frame_offset = loader_ctx->frame_offset;
  9384. /* Get each param's cell num and src offset */
  9385. for (i = 0; i < param_count; i++) {
  9386. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9387. cells[i] = cell;
  9388. src_offsets[i] = *frame_offset;
  9389. frame_offset += cell;
  9390. }
  9391. /* emit copy instruction */
  9392. emit_label(EXT_OP_COPY_STACK_VALUES);
  9393. /* Part a) */
  9394. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9395. /* Part b) */
  9396. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9397. : wasm_type->param_cell_num);
  9398. /* Part c) */
  9399. for (i = 0; i < param_count; i++)
  9400. emit_byte(loader_ctx, cells[i]);
  9401. if (is_if_block)
  9402. emit_byte(loader_ctx, 1);
  9403. /* Part d) */
  9404. for (i = 0; i < param_count; i++)
  9405. emit_operand(loader_ctx, src_offsets[i]);
  9406. if (is_if_block)
  9407. emit_operand(loader_ctx, condition_offset);
  9408. /* Part e) */
  9409. /* Push to dynamic space. The push will emit the dst offset. */
  9410. for (i = 0; i < param_count; i++)
  9411. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9412. if (is_if_block)
  9413. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9414. ret = true;
  9415. fail:
  9416. /* Free the emit data */
  9417. wasm_runtime_free(emit_data);
  9418. return ret;
  9419. }
  9420. #endif
  9421. #if WASM_ENABLE_GC == 0
  9422. #define RESET_REFTYPE_MAP_STACK() (void)0
  9423. #else
  9424. #define RESET_REFTYPE_MAP_STACK() \
  9425. do { \
  9426. loader_ctx->reftype_map_num = \
  9427. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9428. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9429. + loader_ctx->reftype_map_num; \
  9430. } while (0)
  9431. #endif
  9432. /* reset the stack to the state of before entering the last block */
  9433. #if WASM_ENABLE_FAST_INTERP != 0
  9434. #define RESET_STACK() \
  9435. do { \
  9436. loader_ctx->stack_cell_num = \
  9437. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9438. loader_ctx->frame_ref = \
  9439. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9440. loader_ctx->frame_offset = \
  9441. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9442. RESET_REFTYPE_MAP_STACK(); \
  9443. } while (0)
  9444. #else
  9445. #define RESET_STACK() \
  9446. do { \
  9447. loader_ctx->stack_cell_num = \
  9448. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9449. loader_ctx->frame_ref = \
  9450. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9451. RESET_REFTYPE_MAP_STACK(); \
  9452. } while (0)
  9453. #endif
  9454. /* set current block's stack polymorphic state */
  9455. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9456. do { \
  9457. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9458. _cur_block->is_stack_polymorphic = flag; \
  9459. } while (0)
  9460. #define BLOCK_HAS_PARAM(block_type) \
  9461. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9462. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9463. do { \
  9464. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9465. error_buf_size))) { \
  9466. goto fail; \
  9467. } \
  9468. } while (0)
  9469. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9470. static bool
  9471. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9472. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9473. uint32 error_buf_size)
  9474. {
  9475. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9476. return false;
  9477. }
  9478. if (table_idx < module->import_table_count) {
  9479. if (p_elem_type)
  9480. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  9481. #if WASM_ENABLE_GC != 0
  9482. if (p_ref_type)
  9483. *((WASMRefType **)p_ref_type) =
  9484. module->import_tables[table_idx].u.table.elem_ref_type;
  9485. #endif
  9486. }
  9487. else {
  9488. if (p_elem_type)
  9489. *p_elem_type =
  9490. module->tables[module->import_table_count + table_idx]
  9491. .elem_type;
  9492. #if WASM_ENABLE_GC != 0
  9493. if (p_ref_type)
  9494. *((WASMRefType **)p_ref_type) =
  9495. module->tables[module->import_table_count + table_idx]
  9496. .elem_ref_type;
  9497. #endif
  9498. }
  9499. return true;
  9500. }
  9501. static bool
  9502. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9503. uint8 *p_elem_type, void **p_elem_ref_type,
  9504. char *error_buf, uint32 error_buf_size)
  9505. {
  9506. if (table_seg_idx >= module->table_seg_count) {
  9507. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9508. table_seg_idx);
  9509. return false;
  9510. }
  9511. if (p_elem_type) {
  9512. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9513. }
  9514. #if WASM_ENABLE_GC != 0
  9515. if (p_elem_ref_type)
  9516. *((WASMRefType **)p_elem_ref_type) =
  9517. module->table_segments[table_seg_idx].elem_ref_type;
  9518. #endif
  9519. return true;
  9520. }
  9521. #endif
  9522. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9523. const uint8 *
  9524. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9525. uint32 *len)
  9526. {
  9527. WASMCustomSection *section = module->custom_section_list;
  9528. while (section) {
  9529. if ((section->name_len == strlen(name))
  9530. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9531. if (len) {
  9532. *len = section->content_len;
  9533. }
  9534. return section->content_addr;
  9535. }
  9536. section = section->next;
  9537. }
  9538. return NULL;
  9539. }
  9540. #endif
  9541. static bool
  9542. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9543. uint32 cur_func_idx, char *error_buf,
  9544. uint32 error_buf_size)
  9545. {
  9546. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9547. uint32 param_count, local_count, global_count;
  9548. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9549. BlockType func_block_type;
  9550. uint16 *local_offsets, local_offset;
  9551. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9552. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9553. mem_offset_t mem_offset;
  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. #if WASM_ENABLE_MEMORY64 != 0
  9578. bool is_memory64 = false;
  9579. /* TODO: multi-memories for now assuming the memory idx type is consistent
  9580. * across multi-memories */
  9581. if (module->import_memory_count > 0)
  9582. is_memory64 = module->import_memories[0].u.memory.flags & MEMORY64_FLAG;
  9583. else if (module->memory_count > 0)
  9584. is_memory64 = module->memories[0].flags & MEMORY64_FLAG;
  9585. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9586. #else
  9587. mem_offset_type = VALUE_TYPE_I32;
  9588. #endif
  9589. global_count = module->import_global_count + module->global_count;
  9590. param_count = func->func_type->param_count;
  9591. param_types = func->func_type->types;
  9592. func_block_type.is_value_type = false;
  9593. func_block_type.u.type = func->func_type;
  9594. local_count = func->local_count;
  9595. local_types = func->local_types;
  9596. local_offsets = func->local_offsets;
  9597. #if WASM_ENABLE_GC != 0
  9598. param_reftype_maps = func->func_type->ref_type_maps;
  9599. param_reftype_map_count = func->func_type->ref_type_map_count;
  9600. local_reftype_maps = func->local_ref_type_maps;
  9601. local_reftype_map_count = func->local_ref_type_map_count;
  9602. #endif
  9603. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9604. goto fail;
  9605. }
  9606. #if WASM_ENABLE_GC != 0
  9607. loader_ctx->module = module;
  9608. loader_ctx->ref_type_set = module->ref_type_set;
  9609. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9610. #endif
  9611. #if WASM_ENABLE_FAST_INTERP != 0
  9612. /* For the first traverse, the initial value of preserved_local_offset has
  9613. * not been determined, we use the INT16_MAX to represent that a slot has
  9614. * been copied to preserve space. For second traverse, this field will be
  9615. * set to the appropriate value in wasm_loader_ctx_reinit.
  9616. * This is for Issue #1230,
  9617. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9618. * drop opcodes need to know which slots are preserved, so those slots will
  9619. * not be treated as dynamically allocated slots */
  9620. loader_ctx->preserved_local_offset = INT16_MAX;
  9621. re_scan:
  9622. if (loader_ctx->code_compiled_size > 0) {
  9623. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9624. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9625. goto fail;
  9626. }
  9627. p = func->code;
  9628. func->code_compiled = loader_ctx->p_code_compiled;
  9629. func->code_compiled_size = loader_ctx->code_compiled_size;
  9630. }
  9631. #endif
  9632. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9633. while (p < p_end) {
  9634. opcode = *p++;
  9635. #if WASM_ENABLE_FAST_INTERP != 0
  9636. p_org = p;
  9637. disable_emit = false;
  9638. emit_label(opcode);
  9639. #endif
  9640. switch (opcode) {
  9641. case WASM_OP_UNREACHABLE:
  9642. RESET_STACK();
  9643. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9644. break;
  9645. case WASM_OP_NOP:
  9646. #if WASM_ENABLE_FAST_INTERP != 0
  9647. skip_label();
  9648. #endif
  9649. break;
  9650. case WASM_OP_IF:
  9651. {
  9652. #if WASM_ENABLE_FAST_INTERP != 0
  9653. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9654. int32 available_stack_cell =
  9655. (int32)(loader_ctx->stack_cell_num
  9656. - parent_block->stack_cell_num);
  9657. if (available_stack_cell <= 0
  9658. && parent_block->is_stack_polymorphic)
  9659. if_condition_available = false;
  9660. else
  9661. if_condition_available = true;
  9662. PRESERVE_LOCAL_FOR_BLOCK();
  9663. #endif
  9664. #if WASM_ENABLE_GC == 0
  9665. POP_I32();
  9666. #endif
  9667. goto handle_op_block_and_loop;
  9668. }
  9669. case WASM_OP_BLOCK:
  9670. case WASM_OP_LOOP:
  9671. #if WASM_ENABLE_EXCE_HANDLING != 0
  9672. case WASM_OP_TRY:
  9673. if (opcode == WASM_OP_TRY) {
  9674. /*
  9675. * keep track of exception handlers to account for
  9676. * memory allocation
  9677. */
  9678. func->exception_handler_count++;
  9679. /*
  9680. * try is a block
  9681. * do nothing special, but execution continues to
  9682. * to handle_op_block_and_loop,
  9683. * and that be pushes the csp
  9684. */
  9685. }
  9686. #endif
  9687. #if WASM_ENABLE_FAST_INTERP != 0
  9688. PRESERVE_LOCAL_FOR_BLOCK();
  9689. #endif
  9690. handle_op_block_and_loop:
  9691. {
  9692. uint8 value_type;
  9693. BlockType block_type;
  9694. #if WASM_ENABLE_FAST_INTERP != 0
  9695. uint32 available_params = 0;
  9696. #endif
  9697. CHECK_BUF(p, p_end, 1);
  9698. value_type = read_uint8(p);
  9699. if (is_byte_a_type(value_type)) {
  9700. /* If the first byte is one of these special values:
  9701. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9702. * the single return value. */
  9703. block_type.is_value_type = true;
  9704. block_type.u.value_type.type = value_type;
  9705. #if WASM_ENABLE_WAMR_COMPILER != 0
  9706. if (value_type == VALUE_TYPE_V128)
  9707. module->is_simd_used = true;
  9708. else if (value_type == VALUE_TYPE_FUNCREF
  9709. || value_type == VALUE_TYPE_EXTERNREF)
  9710. module->is_ref_types_used = true;
  9711. #endif
  9712. #if WASM_ENABLE_GC != 0
  9713. if (value_type != VALUE_TYPE_VOID) {
  9714. p_org = p;
  9715. p--;
  9716. if (!resolve_value_type((const uint8 **)&p, p_end,
  9717. module, &need_ref_type_map,
  9718. &wasm_ref_type, false,
  9719. error_buf, error_buf_size)) {
  9720. goto fail;
  9721. }
  9722. if (need_ref_type_map) {
  9723. block_type.u.value_type.ref_type_map.index = 0;
  9724. if (!(block_type.u.value_type.ref_type_map
  9725. .ref_type = reftype_set_insert(
  9726. module->ref_type_set, &wasm_ref_type,
  9727. error_buf, error_buf_size))) {
  9728. goto fail;
  9729. }
  9730. }
  9731. /* Set again as the type might be changed, e.g.
  9732. (ref null any) to anyref */
  9733. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9734. #if WASM_ENABLE_FAST_INTERP == 0
  9735. while (p_org < p) {
  9736. #if WASM_ENABLE_DEBUG_INTERP != 0
  9737. if (!record_fast_op(module, p_org, *p_org,
  9738. error_buf, error_buf_size)) {
  9739. goto fail;
  9740. }
  9741. #endif
  9742. /* Ignore extra bytes for interpreter */
  9743. *p_org++ = WASM_OP_NOP;
  9744. }
  9745. #endif
  9746. }
  9747. #endif /* end of WASM_ENABLE_GC != 0 */
  9748. }
  9749. else {
  9750. uint32 type_index;
  9751. /* Resolve the leb128 encoded type index as block type */
  9752. p--;
  9753. p_org = p - 1;
  9754. read_leb_uint32(p, p_end, type_index);
  9755. if (type_index >= module->type_count) {
  9756. set_error_buf(error_buf, error_buf_size,
  9757. "unknown type");
  9758. goto fail;
  9759. }
  9760. block_type.is_value_type = false;
  9761. block_type.u.type =
  9762. (WASMFuncType *)module->types[type_index];
  9763. #if WASM_ENABLE_FAST_INTERP == 0
  9764. /* If block use type index as block type, change the opcode
  9765. * to new extended opcode so that interpreter can resolve
  9766. * the block quickly.
  9767. */
  9768. #if WASM_ENABLE_DEBUG_INTERP != 0
  9769. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9770. error_buf_size)) {
  9771. goto fail;
  9772. }
  9773. #endif
  9774. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9775. #endif
  9776. }
  9777. #if WASM_ENABLE_GC != 0
  9778. if (opcode == WASM_OP_IF) {
  9779. POP_I32();
  9780. }
  9781. #endif
  9782. /* Pop block parameters from stack */
  9783. if (BLOCK_HAS_PARAM(block_type)) {
  9784. WASMFuncType *wasm_type = block_type.u.type;
  9785. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9786. #if WASM_ENABLE_FAST_INTERP != 0
  9787. uint32 cell_num;
  9788. available_params = block_type.u.type->param_count;
  9789. #endif
  9790. for (i = 0; i < block_type.u.type->param_count; i++) {
  9791. int32 available_stack_cell =
  9792. (int32)(loader_ctx->stack_cell_num
  9793. - cur_block->stack_cell_num);
  9794. if (available_stack_cell <= 0
  9795. && cur_block->is_stack_polymorphic) {
  9796. #if WASM_ENABLE_FAST_INTERP != 0
  9797. available_params = i;
  9798. #endif
  9799. break;
  9800. }
  9801. POP_TYPE(
  9802. wasm_type->types[wasm_type->param_count - i - 1]);
  9803. #if WASM_ENABLE_FAST_INTERP != 0
  9804. /* decrease the frame_offset pointer accordingly to keep
  9805. * consistent with frame_ref stack */
  9806. cell_num = wasm_value_type_cell_num(
  9807. wasm_type->types[wasm_type->param_count - i - 1]);
  9808. loader_ctx->frame_offset -= cell_num;
  9809. #endif
  9810. }
  9811. }
  9812. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  9813. block_type, p);
  9814. /* Pass parameters to block */
  9815. if (BLOCK_HAS_PARAM(block_type)) {
  9816. for (i = 0; i < block_type.u.type->param_count; i++) {
  9817. #if WASM_ENABLE_FAST_INTERP != 0
  9818. uint32 cell_num = wasm_value_type_cell_num(
  9819. block_type.u.type->types[i]);
  9820. if (i >= available_params) {
  9821. /* If there isn't enough data on stack, push a dummy
  9822. * offset to keep the stack consistent with
  9823. * frame_ref.
  9824. * Since the stack is already in polymorphic state,
  9825. * the opcode will not be executed, so the dummy
  9826. * offset won't cause any error */
  9827. uint32 n;
  9828. for (n = 0; n < cell_num; n++) {
  9829. if (loader_ctx->p_code_compiled == NULL) {
  9830. if (!check_offset_push(loader_ctx,
  9831. error_buf,
  9832. error_buf_size))
  9833. goto fail;
  9834. }
  9835. *loader_ctx->frame_offset++ = 0;
  9836. }
  9837. }
  9838. else {
  9839. loader_ctx->frame_offset += cell_num;
  9840. }
  9841. #endif
  9842. PUSH_TYPE(block_type.u.type->types[i]);
  9843. }
  9844. }
  9845. #if WASM_ENABLE_FAST_INTERP != 0
  9846. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  9847. skip_label();
  9848. if (BLOCK_HAS_PARAM(block_type)) {
  9849. /* Make sure params are in dynamic space */
  9850. if (!copy_params_to_dynamic_space(
  9851. loader_ctx, false, error_buf, error_buf_size))
  9852. goto fail;
  9853. }
  9854. if (opcode == WASM_OP_LOOP) {
  9855. (loader_ctx->frame_csp - 1)->code_compiled =
  9856. loader_ctx->p_code_compiled;
  9857. }
  9858. }
  9859. #if WASM_ENABLE_EXCE_HANDLING != 0
  9860. else if (opcode == WASM_OP_TRY) {
  9861. skip_label();
  9862. }
  9863. #endif
  9864. else if (opcode == WASM_OP_IF) {
  9865. BranchBlock *block = loader_ctx->frame_csp - 1;
  9866. /* If block has parameters, we should make sure they are in
  9867. * dynamic space. Otherwise, when else branch is missing,
  9868. * the later opcode may consume incorrect operand offset.
  9869. * Spec case:
  9870. * (func (export "params-id") (param i32) (result i32)
  9871. * (i32.const 1)
  9872. * (i32.const 2)
  9873. * (if (param i32 i32) (result i32 i32) (local.get 0)
  9874. * (then)) (i32.add)
  9875. * )
  9876. *
  9877. * So we should emit a copy instruction before the if.
  9878. *
  9879. * And we also need to save the parameter offsets and
  9880. * recover them before entering else branch.
  9881. *
  9882. */
  9883. if (BLOCK_HAS_PARAM(block_type)) {
  9884. uint64 size;
  9885. /* In polymorphic state, there may be no if condition on
  9886. * the stack, so the offset may not emitted */
  9887. if (if_condition_available) {
  9888. /* skip the if condition operand offset */
  9889. wasm_loader_emit_backspace(loader_ctx,
  9890. sizeof(int16));
  9891. }
  9892. /* skip the if label */
  9893. skip_label();
  9894. /* Emit a copy instruction */
  9895. if (!copy_params_to_dynamic_space(
  9896. loader_ctx, true, error_buf, error_buf_size))
  9897. goto fail;
  9898. /* Emit the if instruction */
  9899. emit_label(opcode);
  9900. /* Emit the new condition operand offset */
  9901. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  9902. /* Save top param_count values of frame_offset stack, so
  9903. * that we can recover it before executing else branch
  9904. */
  9905. size = sizeof(int16)
  9906. * (uint64)block_type.u.type->param_cell_num;
  9907. if (!(block->param_frame_offsets = loader_malloc(
  9908. size, error_buf, error_buf_size)))
  9909. goto fail;
  9910. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  9911. loader_ctx->frame_offset
  9912. - size / sizeof(int16),
  9913. (uint32)size);
  9914. }
  9915. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9916. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  9917. emit_empty_label_addr_and_frame_ip(PATCH_END);
  9918. }
  9919. #endif
  9920. break;
  9921. }
  9922. #if WASM_ENABLE_EXCE_HANDLING != 0
  9923. case WASM_OP_THROW:
  9924. {
  9925. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9926. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9927. uint8 label_type = cur_block->label_type;
  9928. uint32 tag_index = 0;
  9929. read_leb_int32(p, p_end, tag_index);
  9930. /* check validity of tag_index against module->tag_count */
  9931. /* check tag index is within the tag index space */
  9932. if (tag_index >= module->import_tag_count + module->tag_count) {
  9933. snprintf(error_buf, error_buf_size, "unknown tag %d",
  9934. tag_index);
  9935. goto fail;
  9936. }
  9937. /* the tag_type is stored in either the WASMTag (section tags)
  9938. * or WASMTagImport (import tag) */
  9939. WASMType *tag_type = NULL;
  9940. if (tag_index < module->import_tag_count) {
  9941. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  9942. }
  9943. else {
  9944. tag_type =
  9945. module->tags[tag_index - module->import_tag_count]
  9946. ->tag_type;
  9947. }
  9948. if (tag_type->result_count != 0) {
  9949. set_error_buf(error_buf, error_buf_size,
  9950. "tag type signature does not return void");
  9951. goto fail;
  9952. }
  9953. int32 available_stack_cell =
  9954. (int32)(loader_ctx->stack_cell_num
  9955. - cur_block->stack_cell_num);
  9956. /* Check stack values match return types by comparing tag param
  9957. * types with stack cells */
  9958. uint8 *frame_ref = loader_ctx->frame_ref;
  9959. for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
  9960. tti--) {
  9961. if (!check_stack_top_values(
  9962. loader_ctx, frame_ref, available_stack_cell,
  9963. tag_type->types[tti], error_buf, error_buf_size)) {
  9964. snprintf(error_buf, error_buf_size,
  9965. "type mismatch: instruction requires [%s] but "
  9966. "stack has [%s]",
  9967. tag_type->param_count > 0
  9968. ? type2str(tag_type->types[tti])
  9969. : "",
  9970. available_stack_cell > 0
  9971. ? type2str(*(loader_ctx->frame_ref - 1))
  9972. : "");
  9973. goto fail;
  9974. }
  9975. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  9976. available_stack_cell -=
  9977. wasm_value_type_cell_num(tag_type->types[tti]);
  9978. }
  9979. /* throw is stack polymorphic */
  9980. (void)label_type;
  9981. RESET_STACK();
  9982. break;
  9983. }
  9984. case WASM_OP_RETHROW:
  9985. {
  9986. /* must be done before checking branch block */
  9987. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9988. /* check the target catching block: LABEL_TYPE_CATCH */
  9989. if (!(frame_csp_tmp =
  9990. check_branch_block(loader_ctx, &p, p_end, opcode,
  9991. error_buf, error_buf_size)))
  9992. goto fail;
  9993. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  9994. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  9995. /* trap according to spectest (rethrow.wast) */
  9996. set_error_buf(error_buf, error_buf_size,
  9997. "invalid rethrow label");
  9998. goto fail;
  9999. }
  10000. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10001. uint8 label_type = cur_block->label_type;
  10002. (void)label_type;
  10003. /* rethrow is stack polymorphic */
  10004. RESET_STACK();
  10005. break;
  10006. }
  10007. case WASM_OP_DELEGATE:
  10008. {
  10009. /* check target block is valid */
  10010. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10011. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10012. goto fail;
  10013. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10014. uint8 label_type = cur_block->label_type;
  10015. (void)label_type;
  10016. /* DELEGATE ends the block */
  10017. POP_CSP();
  10018. break;
  10019. }
  10020. case WASM_OP_CATCH:
  10021. {
  10022. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10023. uint8 label_type = cur_block->label_type;
  10024. uint32 tag_index = 0;
  10025. read_leb_int32(p, p_end, tag_index);
  10026. /* check validity of tag_index against module->tag_count */
  10027. /* check tag index is within the tag index space */
  10028. if (tag_index >= module->import_tag_count + module->tag_count) {
  10029. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10030. __FUNCTION__);
  10031. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10032. goto fail;
  10033. }
  10034. /* the tag_type is stored in either the WASMTag (section tags)
  10035. * or WASMTagImport (import tag) */
  10036. WASMType *func_type = NULL;
  10037. if (tag_index < module->import_tag_count) {
  10038. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10039. }
  10040. else {
  10041. func_type =
  10042. module->tags[tag_index - module->import_tag_count]
  10043. ->tag_type;
  10044. }
  10045. if (func_type->result_count != 0) {
  10046. set_error_buf(error_buf, error_buf_size,
  10047. "tag type signature does not return void");
  10048. goto fail;
  10049. }
  10050. /* check validity of current label (expect LABEL_TYPE_TRY or
  10051. * LABEL_TYPE_CATCH) */
  10052. if ((LABEL_TYPE_CATCH != label_type)
  10053. && (LABEL_TYPE_TRY != label_type)) {
  10054. set_error_buf(error_buf, error_buf_size,
  10055. "Unexpected block sequence encountered.");
  10056. goto fail;
  10057. }
  10058. BlockType new_block_type;
  10059. new_block_type.is_value_type = false;
  10060. new_block_type.u.type = func_type;
  10061. /*
  10062. * replace frame_csp by LABEL_TYPE_CATCH
  10063. */
  10064. cur_block->label_type = LABEL_TYPE_CATCH;
  10065. /* RESET_STACK removes the values pushed in TRY or pervious
  10066. * CATCH Blocks */
  10067. RESET_STACK();
  10068. /* push types on the stack according to catched type */
  10069. if (BLOCK_HAS_PARAM(new_block_type)) {
  10070. for (i = 0; i < new_block_type.u.type->param_count; i++)
  10071. PUSH_TYPE(new_block_type.u.type->types[i]);
  10072. }
  10073. break;
  10074. }
  10075. case WASM_OP_CATCH_ALL:
  10076. {
  10077. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10078. /* expecting a TRY or CATCH, anything else will be considered an
  10079. * error */
  10080. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10081. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10082. set_error_buf(error_buf, error_buf_size,
  10083. "Unexpected block sequence encountered.");
  10084. goto fail;
  10085. }
  10086. /* no immediates */
  10087. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10088. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10089. /* RESET_STACK removes the values pushed in TRY or pervious
  10090. * CATCH Blocks */
  10091. RESET_STACK();
  10092. /* catch_all has no tagtype and therefore no parameters */
  10093. break;
  10094. }
  10095. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10096. case WASM_OP_ELSE:
  10097. {
  10098. BranchBlock *block = NULL;
  10099. BlockType block_type;
  10100. if (loader_ctx->csp_num < 2
  10101. || (loader_ctx->frame_csp - 1)->label_type
  10102. != LABEL_TYPE_IF) {
  10103. set_error_buf(
  10104. error_buf, error_buf_size,
  10105. "opcode else found without matched opcode if");
  10106. goto fail;
  10107. }
  10108. block = loader_ctx->frame_csp - 1;
  10109. /* check whether if branch's stack matches its result type */
  10110. if (!check_block_stack(loader_ctx, block, error_buf,
  10111. error_buf_size))
  10112. goto fail;
  10113. block->else_addr = p - 1;
  10114. block_type = block->block_type;
  10115. #if WASM_ENABLE_GC != 0
  10116. if (!wasm_loader_init_local_use_masks(
  10117. loader_ctx, local_count, error_buf, error_buf_size)) {
  10118. goto fail;
  10119. }
  10120. #endif
  10121. #if WASM_ENABLE_FAST_INTERP != 0
  10122. /* if the result of if branch is in local or const area, add a
  10123. * copy op */
  10124. RESERVE_BLOCK_RET();
  10125. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10126. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10127. #endif
  10128. RESET_STACK();
  10129. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10130. /* Pass parameters to if-false branch */
  10131. if (BLOCK_HAS_PARAM(block_type)) {
  10132. for (i = 0; i < block_type.u.type->param_count; i++)
  10133. PUSH_TYPE(block_type.u.type->types[i]);
  10134. }
  10135. #if WASM_ENABLE_FAST_INTERP != 0
  10136. /* Recover top param_count values of frame_offset stack */
  10137. if (BLOCK_HAS_PARAM((block_type))) {
  10138. uint32 size;
  10139. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10140. bh_memcpy_s(loader_ctx->frame_offset, size,
  10141. block->param_frame_offsets, size);
  10142. loader_ctx->frame_offset += (size / sizeof(int16));
  10143. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10144. }
  10145. #endif
  10146. break;
  10147. }
  10148. case WASM_OP_END:
  10149. {
  10150. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10151. /* check whether block stack matches its result type */
  10152. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10153. error_buf_size))
  10154. goto fail;
  10155. /* if no else branch, and return types do not match param types,
  10156. report failure */
  10157. if (cur_block->label_type == LABEL_TYPE_IF
  10158. && !cur_block->else_addr) {
  10159. uint32 block_param_count = 0, block_ret_count = 0;
  10160. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  10161. BlockType *cur_block_type = &cur_block->block_type;
  10162. #if WASM_ENABLE_GC != 0
  10163. uint32 block_param_reftype_map_count;
  10164. uint32 block_ret_reftype_map_count;
  10165. WASMRefTypeMap *block_param_reftype_maps;
  10166. WASMRefTypeMap *block_ret_reftype_maps;
  10167. #endif
  10168. block_param_count = block_type_get_param_types(
  10169. cur_block_type, &block_param_types
  10170. #if WASM_ENABLE_GC != 0
  10171. ,
  10172. &block_param_reftype_maps,
  10173. &block_param_reftype_map_count
  10174. #endif
  10175. );
  10176. block_ret_count = block_type_get_result_types(
  10177. cur_block_type, &block_ret_types
  10178. #if WASM_ENABLE_GC != 0
  10179. ,
  10180. &block_ret_reftype_maps, &block_ret_reftype_map_count
  10181. #endif
  10182. );
  10183. if (block_param_count != block_ret_count
  10184. || (block_param_count
  10185. && memcmp(block_param_types, block_ret_types,
  10186. block_param_count))) {
  10187. set_error_buf(error_buf, error_buf_size,
  10188. "type mismatch: else branch missing");
  10189. goto fail;
  10190. }
  10191. #if WASM_ENABLE_GC != 0
  10192. if (block_param_reftype_map_count
  10193. != block_ret_reftype_map_count
  10194. || (block_param_reftype_map_count
  10195. && memcmp(block_param_reftype_maps,
  10196. block_ret_reftype_maps,
  10197. sizeof(WASMRefTypeMap)
  10198. * block_param_reftype_map_count))) {
  10199. set_error_buf(error_buf, error_buf_size,
  10200. "type mismatch: else branch missing");
  10201. goto fail;
  10202. }
  10203. #endif
  10204. }
  10205. POP_CSP();
  10206. #if WASM_ENABLE_FAST_INTERP != 0
  10207. skip_label();
  10208. /* copy the result to the block return address */
  10209. RESERVE_BLOCK_RET();
  10210. apply_label_patch(loader_ctx, 0, PATCH_END);
  10211. free_label_patch_list(loader_ctx->frame_csp);
  10212. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10213. int32 idx;
  10214. uint8 ret_type;
  10215. emit_label(WASM_OP_RETURN);
  10216. for (idx = (int32)func->func_type->result_count - 1;
  10217. idx >= 0; idx--) {
  10218. ret_type = *(func->func_type->types
  10219. + func->func_type->param_count + idx);
  10220. POP_OFFSET_TYPE(ret_type);
  10221. }
  10222. }
  10223. #endif
  10224. if (loader_ctx->csp_num > 0) {
  10225. loader_ctx->frame_csp->end_addr = p - 1;
  10226. }
  10227. else {
  10228. /* end of function block, function will return */
  10229. if (p < p_end) {
  10230. set_error_buf(error_buf, error_buf_size,
  10231. "section size mismatch");
  10232. goto fail;
  10233. }
  10234. }
  10235. break;
  10236. }
  10237. case WASM_OP_BR:
  10238. {
  10239. if (!(frame_csp_tmp =
  10240. check_branch_block(loader_ctx, &p, p_end, opcode,
  10241. error_buf, error_buf_size)))
  10242. goto fail;
  10243. RESET_STACK();
  10244. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10245. break;
  10246. }
  10247. case WASM_OP_BR_IF:
  10248. {
  10249. POP_I32();
  10250. if (!(frame_csp_tmp =
  10251. check_branch_block(loader_ctx, &p, p_end, opcode,
  10252. error_buf, error_buf_size)))
  10253. goto fail;
  10254. break;
  10255. }
  10256. case WASM_OP_BR_TABLE:
  10257. {
  10258. uint32 depth = 0, default_arity, arity = 0;
  10259. BranchBlock *target_block;
  10260. BlockType *target_block_type;
  10261. #if WASM_ENABLE_FAST_INTERP == 0
  10262. BrTableCache *br_table_cache = NULL;
  10263. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10264. uint32 j;
  10265. #endif
  10266. read_leb_uint32(p, p_end, count);
  10267. #if WASM_ENABLE_FAST_INTERP != 0
  10268. emit_uint32(loader_ctx, count);
  10269. #endif
  10270. POP_I32();
  10271. /* Get the default depth and check it */
  10272. p_org = p;
  10273. for (i = 0; i <= count; i++) {
  10274. read_leb_uint32(p, p_end, depth);
  10275. }
  10276. if (loader_ctx->csp_num < depth + 1) {
  10277. set_error_buf(error_buf, error_buf_size,
  10278. "unknown label, "
  10279. "unexpected end of section or function");
  10280. goto fail;
  10281. }
  10282. p = p_org;
  10283. /* Get the default block's arity */
  10284. target_block = loader_ctx->frame_csp - (depth + 1);
  10285. target_block_type = &target_block->block_type;
  10286. default_arity = block_type_get_arity(target_block_type,
  10287. target_block->label_type);
  10288. #if WASM_ENABLE_FAST_INTERP == 0
  10289. p_depth_begin = p_depth = p;
  10290. #endif
  10291. for (i = 0; i <= count; i++) {
  10292. p_org = p;
  10293. read_leb_uint32(p, p_end, depth);
  10294. if (loader_ctx->csp_num < depth + 1) {
  10295. set_error_buf(error_buf, error_buf_size,
  10296. "unknown label, "
  10297. "unexpected end of section or function");
  10298. goto fail;
  10299. }
  10300. p = p_org;
  10301. /* Get the target block's arity and check it */
  10302. target_block = loader_ctx->frame_csp - (depth + 1);
  10303. target_block_type = &target_block->block_type;
  10304. arity = block_type_get_arity(target_block_type,
  10305. target_block->label_type);
  10306. if (arity != default_arity) {
  10307. set_error_buf(error_buf, error_buf_size,
  10308. "type mismatch: br_table targets must "
  10309. "all use same result type");
  10310. goto fail;
  10311. }
  10312. if (!(frame_csp_tmp =
  10313. check_branch_block(loader_ctx, &p, p_end, opcode,
  10314. error_buf, error_buf_size))) {
  10315. goto fail;
  10316. }
  10317. #if WASM_ENABLE_FAST_INTERP == 0
  10318. if (br_table_cache) {
  10319. br_table_cache->br_depths[i] = depth;
  10320. }
  10321. else {
  10322. if (depth > 255) {
  10323. /* The depth cannot be stored in one byte,
  10324. create br_table cache to store each depth */
  10325. #if WASM_ENABLE_DEBUG_INTERP != 0
  10326. if (!record_fast_op(module, p_opcode, *p_opcode,
  10327. error_buf, error_buf_size)) {
  10328. goto fail;
  10329. }
  10330. #endif
  10331. if (!(br_table_cache = loader_malloc(
  10332. offsetof(BrTableCache, br_depths)
  10333. + sizeof(uint32)
  10334. * (uint64)(count + 1),
  10335. error_buf, error_buf_size))) {
  10336. goto fail;
  10337. }
  10338. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10339. br_table_cache->br_table_op_addr = p_opcode;
  10340. br_table_cache->br_count = count;
  10341. /* Copy previous depths which are one byte */
  10342. for (j = 0; j < i; j++) {
  10343. br_table_cache->br_depths[j] = p_depth_begin[j];
  10344. }
  10345. br_table_cache->br_depths[i] = depth;
  10346. bh_list_insert(module->br_table_cache_list,
  10347. br_table_cache);
  10348. }
  10349. else {
  10350. /* The depth can be stored in one byte, use the
  10351. byte of the leb to store it */
  10352. *p_depth++ = (uint8)depth;
  10353. }
  10354. }
  10355. #endif
  10356. }
  10357. #if WASM_ENABLE_FAST_INTERP == 0
  10358. /* Set the tailing bytes to nop */
  10359. if (br_table_cache)
  10360. p_depth = p_depth_begin;
  10361. while (p_depth < p)
  10362. *p_depth++ = WASM_OP_NOP;
  10363. #endif
  10364. RESET_STACK();
  10365. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10366. break;
  10367. }
  10368. case WASM_OP_RETURN:
  10369. {
  10370. int32 idx;
  10371. uint8 ret_type;
  10372. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  10373. idx--) {
  10374. ret_type = *(func->func_type->types
  10375. + func->func_type->param_count + idx);
  10376. #if WASM_ENABLE_FAST_INTERP != 0
  10377. /* emit the offset after return opcode */
  10378. POP_OFFSET_TYPE(ret_type);
  10379. #endif
  10380. POP_TYPE(ret_type);
  10381. }
  10382. RESET_STACK();
  10383. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10384. break;
  10385. }
  10386. case WASM_OP_CALL:
  10387. #if WASM_ENABLE_TAIL_CALL != 0
  10388. case WASM_OP_RETURN_CALL:
  10389. #endif
  10390. #if WASM_ENABLE_GC != 0
  10391. case WASM_OP_CALL_REF:
  10392. case WASM_OP_RETURN_CALL_REF:
  10393. #endif
  10394. {
  10395. WASMFuncType *func_type;
  10396. uint8 type;
  10397. int32 idx;
  10398. #if WASM_ENABLE_GC != 0
  10399. WASMRefType *ref_type;
  10400. uint32 type_idx1;
  10401. int32 j;
  10402. #endif
  10403. #if WASM_ENABLE_GC != 0
  10404. if (opcode == WASM_OP_CALL_REF
  10405. || opcode == WASM_OP_RETURN_CALL_REF) {
  10406. read_leb_uint32(p, p_end, type_idx1);
  10407. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10408. &type_idx, error_buf,
  10409. error_buf_size)) {
  10410. goto fail;
  10411. }
  10412. if (type == VALUE_TYPE_ANY) {
  10413. type_idx = type_idx1;
  10414. }
  10415. if (!check_type_index(module, type_idx, error_buf,
  10416. error_buf_size)) {
  10417. goto fail;
  10418. }
  10419. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10420. set_error_buf(error_buf, error_buf_size,
  10421. "unkown function type");
  10422. goto fail;
  10423. }
  10424. if (type_idx != type_idx1) {
  10425. set_error_buf(error_buf, error_buf_size,
  10426. "function type mismatch");
  10427. goto fail;
  10428. }
  10429. func_type = (WASMFuncType *)module->types[type_idx];
  10430. }
  10431. else
  10432. #endif
  10433. {
  10434. read_leb_uint32(p, p_end, func_idx);
  10435. #if WASM_ENABLE_FAST_INTERP != 0
  10436. /* we need to emit func_idx before arguments */
  10437. emit_uint32(loader_ctx, func_idx);
  10438. #endif
  10439. if (!check_function_index(module, func_idx, error_buf,
  10440. error_buf_size)) {
  10441. goto fail;
  10442. }
  10443. if (func_idx < module->import_function_count)
  10444. func_type = module->import_functions[func_idx]
  10445. .u.function.func_type;
  10446. else
  10447. func_type =
  10448. module
  10449. ->functions[func_idx
  10450. - module->import_function_count]
  10451. ->func_type;
  10452. }
  10453. if (func_type->param_count > 0) {
  10454. #if WASM_ENABLE_GC != 0
  10455. j = (int32)(func_type->result_ref_type_maps
  10456. - func_type->ref_type_maps - 1);
  10457. #endif
  10458. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10459. idx--) {
  10460. #if WASM_ENABLE_GC != 0
  10461. if (wasm_is_type_multi_byte_type(
  10462. func_type->types[idx])) {
  10463. ref_type = func_type->ref_type_maps[j].ref_type;
  10464. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10465. ref_type,
  10466. wasm_reftype_struct_size(ref_type));
  10467. j--;
  10468. }
  10469. #endif
  10470. #if WASM_ENABLE_FAST_INTERP != 0
  10471. POP_OFFSET_TYPE(func_type->types[idx]);
  10472. #endif
  10473. POP_TYPE(func_type->types[idx]);
  10474. }
  10475. }
  10476. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10477. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10478. #endif
  10479. #if WASM_ENABLE_GC != 0
  10480. j = (int32)(func_type->result_ref_type_maps
  10481. - func_type->ref_type_maps);
  10482. #endif
  10483. for (i = 0; i < func_type->result_count; i++) {
  10484. #if WASM_ENABLE_GC != 0
  10485. if (wasm_is_type_multi_byte_type(
  10486. func_type->types[func_type->param_count + i])) {
  10487. ref_type = func_type->ref_type_maps[j].ref_type;
  10488. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10489. ref_type,
  10490. wasm_reftype_struct_size(ref_type));
  10491. j++;
  10492. }
  10493. #endif
  10494. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10495. #if WASM_ENABLE_FAST_INTERP != 0
  10496. /* Here we emit each return value's dynamic_offset. But
  10497. * in fact these offsets are continuous, so interpreter
  10498. * only need to get the first return value's offset.
  10499. */
  10500. PUSH_OFFSET_TYPE(
  10501. func_type->types[func_type->param_count + i]);
  10502. #endif
  10503. }
  10504. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10505. }
  10506. else {
  10507. #if WASM_ENABLE_GC == 0
  10508. if (func_type->result_count
  10509. != func->func_type->result_count) {
  10510. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10511. "type mismatch: expect ",
  10512. func->func_type->result_count,
  10513. " return values but got other");
  10514. goto fail;
  10515. }
  10516. for (i = 0; i < func_type->result_count; i++) {
  10517. type = func->func_type
  10518. ->types[func->func_type->param_count + i];
  10519. if (func_type->types[func_type->param_count + i]
  10520. != type) {
  10521. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10522. "type mismatch: expect ",
  10523. type2str(type), " but got other");
  10524. goto fail;
  10525. }
  10526. }
  10527. #else
  10528. if (!wasm_func_type_result_is_subtype_of(
  10529. func_type, func->func_type, module->types,
  10530. module->type_count)) {
  10531. set_error_buf(
  10532. error_buf, error_buf_size,
  10533. "type mismatch: invalid func result types");
  10534. goto fail;
  10535. }
  10536. #endif
  10537. RESET_STACK();
  10538. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10539. }
  10540. #endif
  10541. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10542. || WASM_ENABLE_WAMR_COMPILER != 0
  10543. func->has_op_func_call = true;
  10544. #endif
  10545. (void)type;
  10546. break;
  10547. }
  10548. /*
  10549. * if disable reference type: call_indirect typeidx, 0x00
  10550. * if enable reference type: call_indirect typeidx, tableidx
  10551. */
  10552. case WASM_OP_CALL_INDIRECT:
  10553. #if WASM_ENABLE_TAIL_CALL != 0
  10554. case WASM_OP_RETURN_CALL_INDIRECT:
  10555. #endif
  10556. {
  10557. int32 idx;
  10558. WASMFuncType *func_type;
  10559. read_leb_uint32(p, p_end, type_idx);
  10560. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10561. read_leb_uint32(p, p_end, table_idx);
  10562. #else
  10563. CHECK_BUF(p, p_end, 1);
  10564. table_idx = read_uint8(p);
  10565. #endif
  10566. if (!check_table_index(module, table_idx, error_buf,
  10567. error_buf_size)) {
  10568. goto fail;
  10569. }
  10570. #if WASM_ENABLE_FAST_INTERP != 0
  10571. /* we need to emit before arguments */
  10572. #if WASM_ENABLE_TAIL_CALL != 0
  10573. emit_byte(loader_ctx, opcode);
  10574. #endif
  10575. emit_uint32(loader_ctx, type_idx);
  10576. emit_uint32(loader_ctx, table_idx);
  10577. #endif
  10578. /* skip elem idx */
  10579. POP_I32();
  10580. if (type_idx >= module->type_count) {
  10581. set_error_buf(error_buf, error_buf_size, "unknown type");
  10582. goto fail;
  10583. }
  10584. func_type = (WASMFuncType *)module->types[type_idx];
  10585. if (func_type->param_count > 0) {
  10586. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10587. idx--) {
  10588. #if WASM_ENABLE_FAST_INTERP != 0
  10589. POP_OFFSET_TYPE(func_type->types[idx]);
  10590. #endif
  10591. POP_TYPE(func_type->types[idx]);
  10592. }
  10593. }
  10594. #if WASM_ENABLE_TAIL_CALL != 0
  10595. if (opcode == WASM_OP_CALL_INDIRECT) {
  10596. #endif
  10597. for (i = 0; i < func_type->result_count; i++) {
  10598. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10599. #if WASM_ENABLE_FAST_INTERP != 0
  10600. PUSH_OFFSET_TYPE(
  10601. func_type->types[func_type->param_count + i]);
  10602. #endif
  10603. }
  10604. #if WASM_ENABLE_TAIL_CALL != 0
  10605. }
  10606. else {
  10607. uint8 type;
  10608. if (func_type->result_count
  10609. != func->func_type->result_count) {
  10610. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10611. "type mismatch: expect ",
  10612. func->func_type->result_count,
  10613. " return values but got other");
  10614. goto fail;
  10615. }
  10616. for (i = 0; i < func_type->result_count; i++) {
  10617. type = func->func_type
  10618. ->types[func->func_type->param_count + i];
  10619. if (func_type->types[func_type->param_count + i]
  10620. != type) {
  10621. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10622. "type mismatch: expect ",
  10623. type2str(type), " but got other");
  10624. goto fail;
  10625. }
  10626. }
  10627. RESET_STACK();
  10628. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10629. }
  10630. #endif
  10631. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10632. || WASM_ENABLE_WAMR_COMPILER != 0
  10633. func->has_op_func_call = true;
  10634. #endif
  10635. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10636. func->has_op_call_indirect = true;
  10637. #endif
  10638. break;
  10639. }
  10640. case WASM_OP_DROP:
  10641. {
  10642. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10643. int32 available_stack_cell =
  10644. (int32)(loader_ctx->stack_cell_num
  10645. - cur_block->stack_cell_num);
  10646. if (available_stack_cell <= 0
  10647. && !cur_block->is_stack_polymorphic) {
  10648. set_error_buf(error_buf, error_buf_size,
  10649. "type mismatch, opcode drop was found "
  10650. "but stack was empty");
  10651. goto fail;
  10652. }
  10653. if (available_stack_cell > 0) {
  10654. #if WASM_ENABLE_GC != 0
  10655. if (wasm_is_type_multi_byte_type(
  10656. *(loader_ctx->frame_ref - 1))) {
  10657. bh_assert((int32)(loader_ctx->reftype_map_num
  10658. - cur_block->reftype_map_num)
  10659. > 0);
  10660. loader_ctx->frame_reftype_map--;
  10661. loader_ctx->reftype_map_num--;
  10662. }
  10663. #endif
  10664. if (is_32bit_type(*(loader_ctx->frame_ref - 1))
  10665. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  10666. loader_ctx->frame_ref--;
  10667. loader_ctx->stack_cell_num--;
  10668. #if WASM_ENABLE_FAST_INTERP != 0
  10669. skip_label();
  10670. loader_ctx->frame_offset--;
  10671. if ((*(loader_ctx->frame_offset)
  10672. > loader_ctx->start_dynamic_offset)
  10673. && (*(loader_ctx->frame_offset)
  10674. < loader_ctx->max_dynamic_offset))
  10675. loader_ctx->dynamic_offset--;
  10676. #endif
  10677. }
  10678. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10679. loader_ctx->frame_ref -= 2;
  10680. loader_ctx->stack_cell_num -= 2;
  10681. #if WASM_ENABLE_FAST_INTERP == 0
  10682. *(p - 1) = WASM_OP_DROP_64;
  10683. #endif
  10684. #if WASM_ENABLE_FAST_INTERP != 0
  10685. skip_label();
  10686. loader_ctx->frame_offset -= 2;
  10687. if ((*(loader_ctx->frame_offset)
  10688. > loader_ctx->start_dynamic_offset)
  10689. && (*(loader_ctx->frame_offset)
  10690. < loader_ctx->max_dynamic_offset))
  10691. loader_ctx->dynamic_offset -= 2;
  10692. #endif
  10693. }
  10694. #if WASM_ENABLE_SIMD != 0
  10695. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10696. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10697. loader_ctx->frame_ref -= 4;
  10698. loader_ctx->stack_cell_num -= 4;
  10699. }
  10700. #endif
  10701. #endif
  10702. else {
  10703. set_error_buf(error_buf, error_buf_size,
  10704. "type mismatch");
  10705. goto fail;
  10706. }
  10707. }
  10708. else {
  10709. #if WASM_ENABLE_FAST_INTERP != 0
  10710. skip_label();
  10711. #endif
  10712. }
  10713. break;
  10714. }
  10715. case WASM_OP_SELECT:
  10716. {
  10717. uint8 ref_type;
  10718. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10719. int32 available_stack_cell;
  10720. #if WASM_ENABLE_FAST_INTERP != 0
  10721. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10722. #endif
  10723. POP_I32();
  10724. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10725. - cur_block->stack_cell_num);
  10726. if (available_stack_cell <= 0
  10727. && !cur_block->is_stack_polymorphic) {
  10728. set_error_buf(error_buf, error_buf_size,
  10729. "type mismatch or invalid result arity, "
  10730. "opcode select was found "
  10731. "but stack was empty");
  10732. goto fail;
  10733. }
  10734. if (available_stack_cell > 0) {
  10735. switch (*(loader_ctx->frame_ref - 1)) {
  10736. case VALUE_TYPE_I32:
  10737. case VALUE_TYPE_F32:
  10738. break;
  10739. case VALUE_TYPE_I64:
  10740. case VALUE_TYPE_F64:
  10741. #if WASM_ENABLE_FAST_INTERP == 0
  10742. *(p - 1) = WASM_OP_SELECT_64;
  10743. #endif
  10744. #if WASM_ENABLE_FAST_INTERP != 0
  10745. if (loader_ctx->p_code_compiled) {
  10746. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10747. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10748. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10749. *(void **)(p_code_compiled_tmp
  10750. - sizeof(void *)) =
  10751. handle_table[opcode_tmp];
  10752. #else
  10753. #if UINTPTR_MAX == UINT64_MAX
  10754. /* emit int32 relative offset in 64-bit target
  10755. */
  10756. int32 offset =
  10757. (int32)((uint8 *)handle_table[opcode_tmp]
  10758. - (uint8 *)handle_table[0]);
  10759. *(int32 *)(p_code_compiled_tmp
  10760. - sizeof(int32)) = offset;
  10761. #else
  10762. /* emit uint32 label address in 32-bit target */
  10763. *(uint32 *)(p_code_compiled_tmp
  10764. - sizeof(uint32)) =
  10765. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10766. #endif
  10767. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10768. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10769. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10770. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10771. #else
  10772. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10773. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10774. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10775. }
  10776. #endif /* end of WASM_ENABLE_FAST_INTERP */
  10777. break;
  10778. #if WASM_ENABLE_SIMD != 0
  10779. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10780. case VALUE_TYPE_V128:
  10781. break;
  10782. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  10783. #endif /* WASM_ENABLE_SIMD != 0 */
  10784. default:
  10785. {
  10786. set_error_buf(error_buf, error_buf_size,
  10787. "type mismatch");
  10788. goto fail;
  10789. }
  10790. }
  10791. ref_type = *(loader_ctx->frame_ref - 1);
  10792. #if WASM_ENABLE_FAST_INTERP != 0
  10793. POP_OFFSET_TYPE(ref_type);
  10794. POP_TYPE(ref_type);
  10795. POP_OFFSET_TYPE(ref_type);
  10796. POP_TYPE(ref_type);
  10797. PUSH_OFFSET_TYPE(ref_type);
  10798. PUSH_TYPE(ref_type);
  10799. #else
  10800. POP2_AND_PUSH(ref_type, ref_type);
  10801. #endif
  10802. }
  10803. else {
  10804. #if WASM_ENABLE_FAST_INTERP != 0
  10805. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  10806. #endif
  10807. PUSH_TYPE(VALUE_TYPE_ANY);
  10808. }
  10809. break;
  10810. }
  10811. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10812. case WASM_OP_SELECT_T:
  10813. {
  10814. uint8 vec_len, type;
  10815. #if WASM_ENABLE_GC != 0
  10816. WASMRefType *ref_type = NULL;
  10817. #endif
  10818. #if WASM_ENABLE_FAST_INTERP != 0
  10819. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10820. #endif
  10821. read_leb_uint32(p, p_end, vec_len);
  10822. if (vec_len != 1) {
  10823. /* typed select must have exactly one result */
  10824. set_error_buf(error_buf, error_buf_size,
  10825. "invalid result arity");
  10826. goto fail;
  10827. }
  10828. #if WASM_ENABLE_GC == 0
  10829. CHECK_BUF(p, p_end, 1);
  10830. type = read_uint8(p);
  10831. if (!is_value_type(type)) {
  10832. set_error_buf(error_buf, error_buf_size,
  10833. "unknown value type");
  10834. goto fail;
  10835. }
  10836. #else
  10837. p_org = p + 1;
  10838. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  10839. &need_ref_type_map, &wasm_ref_type,
  10840. false, error_buf, error_buf_size)) {
  10841. goto fail;
  10842. }
  10843. type = wasm_ref_type.ref_type;
  10844. if (need_ref_type_map) {
  10845. if (!(ref_type = reftype_set_insert(
  10846. module->ref_type_set, &wasm_ref_type, error_buf,
  10847. error_buf_size))) {
  10848. goto fail;
  10849. }
  10850. }
  10851. #if WASM_ENABLE_FAST_INTERP == 0
  10852. while (p_org < p) {
  10853. #if WASM_ENABLE_DEBUG_INTERP != 0
  10854. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10855. error_buf_size)) {
  10856. goto fail;
  10857. }
  10858. #endif
  10859. /* Ignore extra bytes for interpreter */
  10860. *p_org++ = WASM_OP_NOP;
  10861. }
  10862. #endif
  10863. #endif /* end of WASM_ENABLE_GC == 0 */
  10864. POP_I32();
  10865. #if WASM_ENABLE_FAST_INTERP != 0
  10866. if (loader_ctx->p_code_compiled) {
  10867. uint8 opcode_tmp = WASM_OP_SELECT;
  10868. if (type == VALUE_TYPE_V128) {
  10869. #if (WASM_ENABLE_SIMD == 0) \
  10870. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  10871. set_error_buf(error_buf, error_buf_size,
  10872. "SIMD v128 type isn't supported");
  10873. goto fail;
  10874. #endif
  10875. }
  10876. else {
  10877. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  10878. opcode_tmp = WASM_OP_SELECT_64;
  10879. #if WASM_ENABLE_GC != 0
  10880. if (wasm_is_type_reftype(type))
  10881. opcode_tmp = WASM_OP_SELECT_T;
  10882. #endif
  10883. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10884. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10885. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  10886. handle_table[opcode_tmp];
  10887. #else
  10888. #if UINTPTR_MAX == UINT64_MAX
  10889. /* emit int32 relative offset in 64-bit target */
  10890. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  10891. - (uint8 *)handle_table[0]);
  10892. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  10893. offset;
  10894. #else
  10895. /* emit uint32 label address in 32-bit target */
  10896. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  10897. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10898. #endif
  10899. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10900. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10901. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10902. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10903. #else
  10904. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10905. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10906. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10907. }
  10908. }
  10909. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  10910. POP_REF(type);
  10911. #if WASM_ENABLE_GC != 0
  10912. if (need_ref_type_map) {
  10913. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  10914. wasm_reftype_struct_size(ref_type));
  10915. }
  10916. #endif
  10917. POP_REF(type);
  10918. #if WASM_ENABLE_GC != 0
  10919. if (need_ref_type_map) {
  10920. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  10921. wasm_reftype_struct_size(ref_type));
  10922. }
  10923. #endif
  10924. PUSH_REF(type);
  10925. #if WASM_ENABLE_WAMR_COMPILER != 0
  10926. module->is_ref_types_used = true;
  10927. #endif
  10928. (void)vec_len;
  10929. break;
  10930. }
  10931. /* table.get x. tables[x]. [i32] -> [t] */
  10932. /* table.set x. tables[x]. [i32 t] -> [] */
  10933. case WASM_OP_TABLE_GET:
  10934. case WASM_OP_TABLE_SET:
  10935. {
  10936. uint8 decl_ref_type;
  10937. #if WASM_ENABLE_GC != 0
  10938. WASMRefType *ref_type;
  10939. #endif
  10940. read_leb_uint32(p, p_end, table_idx);
  10941. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  10942. #if WASM_ENABLE_GC != 0
  10943. (void **)&ref_type,
  10944. #else
  10945. NULL,
  10946. #endif
  10947. error_buf, error_buf_size))
  10948. goto fail;
  10949. #if WASM_ENABLE_GC != 0
  10950. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  10951. bh_assert(ref_type);
  10952. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  10953. ref_type, wasm_reftype_struct_size(ref_type));
  10954. }
  10955. #endif
  10956. #if WASM_ENABLE_FAST_INTERP != 0
  10957. emit_uint32(loader_ctx, table_idx);
  10958. #endif
  10959. if (opcode == WASM_OP_TABLE_GET) {
  10960. POP_I32();
  10961. #if WASM_ENABLE_FAST_INTERP != 0
  10962. PUSH_OFFSET_TYPE(decl_ref_type);
  10963. #endif
  10964. PUSH_TYPE(decl_ref_type);
  10965. }
  10966. else {
  10967. #if WASM_ENABLE_FAST_INTERP != 0
  10968. POP_OFFSET_TYPE(decl_ref_type);
  10969. #endif
  10970. POP_TYPE(decl_ref_type);
  10971. POP_I32();
  10972. }
  10973. #if WASM_ENABLE_WAMR_COMPILER != 0
  10974. module->is_ref_types_used = true;
  10975. #endif
  10976. break;
  10977. }
  10978. case WASM_OP_REF_NULL:
  10979. {
  10980. uint8 ref_type;
  10981. #if WASM_ENABLE_GC == 0
  10982. CHECK_BUF(p, p_end, 1);
  10983. ref_type = read_uint8(p);
  10984. if (ref_type != VALUE_TYPE_FUNCREF
  10985. && ref_type != VALUE_TYPE_EXTERNREF) {
  10986. set_error_buf(error_buf, error_buf_size, "type mismatch");
  10987. goto fail;
  10988. }
  10989. #else
  10990. read_leb_int32(p, p_end, heap_type);
  10991. if (heap_type >= 0) {
  10992. if (!check_type_index(module, heap_type, error_buf,
  10993. error_buf_size)) {
  10994. goto fail;
  10995. }
  10996. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  10997. true, heap_type);
  10998. ref_type = wasm_ref_type.ref_type;
  10999. }
  11000. else {
  11001. if (!wasm_is_valid_heap_type(heap_type)) {
  11002. set_error_buf(error_buf, error_buf_size,
  11003. "unknown type");
  11004. goto fail;
  11005. }
  11006. ref_type = (uint8)((int32)0x80 + heap_type);
  11007. }
  11008. #endif /* end of WASM_ENABLE_GC == 0 */
  11009. #if WASM_ENABLE_FAST_INTERP != 0
  11010. PUSH_OFFSET_TYPE(ref_type);
  11011. #endif
  11012. PUSH_TYPE(ref_type);
  11013. #if WASM_ENABLE_WAMR_COMPILER != 0
  11014. module->is_ref_types_used = true;
  11015. #endif
  11016. break;
  11017. }
  11018. case WASM_OP_REF_IS_NULL:
  11019. {
  11020. #if WASM_ENABLE_GC == 0
  11021. #if WASM_ENABLE_FAST_INTERP != 0
  11022. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11023. int32 block_stack_cell_num =
  11024. (int32)(loader_ctx->stack_cell_num
  11025. - cur_block->stack_cell_num);
  11026. if (block_stack_cell_num <= 0) {
  11027. if (!cur_block->is_stack_polymorphic) {
  11028. set_error_buf(
  11029. error_buf, error_buf_size,
  11030. "type mismatch: expect data but stack was empty");
  11031. goto fail;
  11032. }
  11033. }
  11034. else {
  11035. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11036. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11037. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11038. if (!wasm_loader_pop_frame_ref_offset(
  11039. loader_ctx, *(loader_ctx->frame_ref - 1),
  11040. error_buf, error_buf_size)) {
  11041. goto fail;
  11042. }
  11043. }
  11044. else {
  11045. set_error_buf(error_buf, error_buf_size,
  11046. "type mismatch");
  11047. goto fail;
  11048. }
  11049. }
  11050. #else
  11051. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11052. error_buf, error_buf_size)
  11053. && !wasm_loader_pop_frame_ref(loader_ctx,
  11054. VALUE_TYPE_EXTERNREF,
  11055. error_buf, error_buf_size)) {
  11056. goto fail;
  11057. }
  11058. #endif
  11059. #else /* else of WASM_ENABLE_GC == 0 */
  11060. uint8 type;
  11061. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11062. error_buf, error_buf_size)) {
  11063. goto fail;
  11064. }
  11065. #endif
  11066. PUSH_I32();
  11067. #if WASM_ENABLE_WAMR_COMPILER != 0
  11068. module->is_ref_types_used = true;
  11069. #endif
  11070. break;
  11071. }
  11072. case WASM_OP_REF_FUNC:
  11073. {
  11074. read_leb_uint32(p, p_end, func_idx);
  11075. if (!check_function_index(module, func_idx, error_buf,
  11076. error_buf_size)) {
  11077. goto fail;
  11078. }
  11079. /* Refer to a forward-declared function */
  11080. if (func_idx >= cur_func_idx + module->import_function_count) {
  11081. WASMTableSeg *table_seg = module->table_segments;
  11082. bool func_declared = false;
  11083. uint32 j;
  11084. /* Check whether the function is declared in table segs,
  11085. note that it doesn't matter whether the table seg's mode
  11086. is passive, active or declarative. */
  11087. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  11088. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11089. #if WASM_ENABLE_GC != 0
  11090. || (table_seg->elem_type == REF_TYPE_HT_NON_NULLABLE
  11091. && table_seg->elem_ref_type->ref_ht_common
  11092. .heap_type
  11093. == HEAP_TYPE_FUNC)
  11094. #endif
  11095. ) {
  11096. for (j = 0; j < table_seg->value_count; j++) {
  11097. if (table_seg->init_values[j].u.ref_index
  11098. == func_idx) {
  11099. func_declared = true;
  11100. break;
  11101. }
  11102. }
  11103. }
  11104. }
  11105. if (!func_declared) {
  11106. /* Check whether the function is exported */
  11107. for (i = 0; i < module->export_count; i++) {
  11108. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11109. && module->exports[i].index == func_idx) {
  11110. func_declared = true;
  11111. break;
  11112. }
  11113. }
  11114. }
  11115. if (!func_declared) {
  11116. set_error_buf(error_buf, error_buf_size,
  11117. "undeclared function reference");
  11118. goto fail;
  11119. }
  11120. }
  11121. #if WASM_ENABLE_FAST_INTERP != 0
  11122. emit_uint32(loader_ctx, func_idx);
  11123. #endif
  11124. #if WASM_ENABLE_GC == 0
  11125. PUSH_FUNCREF();
  11126. #else
  11127. if (func_idx < module->import_function_count)
  11128. type_idx =
  11129. module->import_functions[func_idx].u.function.type_idx;
  11130. else
  11131. type_idx = module
  11132. ->functions[func_idx
  11133. - module->import_function_count]
  11134. ->type_idx;
  11135. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11136. false, type_idx);
  11137. PUSH_REF(wasm_ref_type.ref_type);
  11138. #endif
  11139. #if WASM_ENABLE_WAMR_COMPILER != 0
  11140. module->is_ref_types_used = true;
  11141. #endif
  11142. break;
  11143. }
  11144. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11145. #if WASM_ENABLE_GC != 0
  11146. case WASM_OP_REF_AS_NON_NULL:
  11147. case WASM_OP_BR_ON_NULL:
  11148. {
  11149. uint8 type;
  11150. WASMRefType ref_type;
  11151. /* POP (ref null ht) and get the converted (ref ht) */
  11152. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11153. error_buf, error_buf_size)) {
  11154. goto fail;
  11155. }
  11156. if (opcode == WASM_OP_BR_ON_NULL) {
  11157. if (!(frame_csp_tmp =
  11158. check_branch_block(loader_ctx, &p, p_end, opcode,
  11159. error_buf, error_buf_size))) {
  11160. goto fail;
  11161. }
  11162. #if WASM_ENABLE_FAST_INTERP != 0
  11163. disable_emit = true;
  11164. #endif
  11165. }
  11166. /* PUSH the converted (ref ht) */
  11167. if (type != VALUE_TYPE_ANY) {
  11168. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11169. sizeof(WASMRefType));
  11170. }
  11171. PUSH_REF(type);
  11172. break;
  11173. }
  11174. case WASM_OP_BR_ON_NON_NULL:
  11175. {
  11176. uint8 type;
  11177. WASMRefType ref_type;
  11178. uint32 available_stack_cell =
  11179. loader_ctx->stack_cell_num
  11180. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11181. /* POP (ref null ht) and get the converted (ref ht) */
  11182. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11183. error_buf, error_buf_size)) {
  11184. goto fail;
  11185. }
  11186. #if WASM_ENABLE_FAST_INTERP != 0
  11187. disable_emit = true;
  11188. #endif
  11189. /* Temporarily PUSH back (ref ht), check brach block and
  11190. then POP it */
  11191. if (available_stack_cell
  11192. > 0) { /* stack isn't in polymorphic state */
  11193. if (type != VALUE_TYPE_ANY) {
  11194. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11195. &ref_type, sizeof(WASMRefType));
  11196. }
  11197. PUSH_REF(type);
  11198. }
  11199. if (!(frame_csp_tmp =
  11200. check_branch_block(loader_ctx, &p, p_end, opcode,
  11201. error_buf, error_buf_size))) {
  11202. goto fail;
  11203. }
  11204. if (available_stack_cell
  11205. > 0) { /* stack isn't in polymorphic state */
  11206. POP_REF(type);
  11207. #if WASM_ENABLE_FAST_INTERP != 0
  11208. /* Erase the opnd offset emitted by POP_REF() */
  11209. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11210. #endif
  11211. }
  11212. break;
  11213. }
  11214. case WASM_OP_REF_EQ:
  11215. POP_REF(REF_TYPE_EQREF);
  11216. POP_REF(REF_TYPE_EQREF);
  11217. PUSH_I32();
  11218. break;
  11219. #endif /* end of WASM_ENABLE_GC != 0 */
  11220. case WASM_OP_GET_LOCAL:
  11221. {
  11222. p_org = p - 1;
  11223. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11224. PUSH_TYPE(local_type);
  11225. #if WASM_ENABLE_GC != 0
  11226. /* Cannot get a non-nullable and unset local */
  11227. if (local_idx >= param_count
  11228. && wasm_is_reftype_htref_non_nullable(local_type)
  11229. && !wasm_loader_get_local_status(loader_ctx,
  11230. local_idx - param_count)) {
  11231. set_error_buf(error_buf, error_buf_size,
  11232. "uninitialized local");
  11233. goto fail;
  11234. }
  11235. #endif
  11236. #if WASM_ENABLE_FAST_INTERP != 0
  11237. /* Get Local is optimized out */
  11238. skip_label();
  11239. disable_emit = true;
  11240. operand_offset = local_offset;
  11241. PUSH_OFFSET_TYPE(local_type);
  11242. #else
  11243. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11244. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11245. if (local_offset < 0x80
  11246. #if WASM_ENABLE_GC != 0
  11247. && !wasm_is_type_reftype(local_type)
  11248. #endif
  11249. ) {
  11250. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11251. if (is_32bit_type(local_type)) {
  11252. *p_org++ = (uint8)local_offset;
  11253. }
  11254. else {
  11255. *p_org++ = (uint8)(local_offset | 0x80);
  11256. }
  11257. while (p_org < p) {
  11258. *p_org++ = WASM_OP_NOP;
  11259. }
  11260. }
  11261. #endif
  11262. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11263. break;
  11264. }
  11265. case WASM_OP_SET_LOCAL:
  11266. {
  11267. p_org = p - 1;
  11268. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11269. #if WASM_ENABLE_FAST_INTERP != 0
  11270. if (!(preserve_referenced_local(
  11271. loader_ctx, opcode, local_offset, local_type,
  11272. &preserve_local, error_buf, error_buf_size)))
  11273. goto fail;
  11274. if (local_offset < 256
  11275. #if WASM_ENABLE_GC != 0
  11276. && !wasm_is_type_reftype(local_type)
  11277. #endif
  11278. ) {
  11279. skip_label();
  11280. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11281. if (loader_ctx->p_code_compiled)
  11282. STORE_U16(loader_ctx->p_code_compiled - 2,
  11283. local_offset);
  11284. loader_ctx->frame_offset--;
  11285. loader_ctx->dynamic_offset--;
  11286. }
  11287. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11288. if (loader_ctx->p_code_compiled)
  11289. STORE_U16(loader_ctx->p_code_compiled - 2,
  11290. local_offset);
  11291. loader_ctx->frame_offset -= 2;
  11292. loader_ctx->dynamic_offset -= 2;
  11293. }
  11294. else {
  11295. if (is_32bit_type(local_type)) {
  11296. emit_label(EXT_OP_SET_LOCAL_FAST);
  11297. emit_byte(loader_ctx, (uint8)local_offset);
  11298. }
  11299. else {
  11300. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11301. emit_byte(loader_ctx, (uint8)local_offset);
  11302. }
  11303. POP_OFFSET_TYPE(local_type);
  11304. }
  11305. }
  11306. else { /* local index larger than 255, reserve leb */
  11307. emit_uint32(loader_ctx, local_idx);
  11308. POP_OFFSET_TYPE(local_type);
  11309. }
  11310. #else
  11311. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11312. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11313. if (local_offset < 0x80
  11314. #if WASM_ENABLE_GC != 0
  11315. && !wasm_is_type_reftype(local_type)
  11316. #endif
  11317. ) {
  11318. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11319. if (is_32bit_type(local_type)) {
  11320. *p_org++ = (uint8)local_offset;
  11321. }
  11322. else {
  11323. *p_org++ = (uint8)(local_offset | 0x80);
  11324. }
  11325. while (p_org < p) {
  11326. *p_org++ = WASM_OP_NOP;
  11327. }
  11328. }
  11329. #endif
  11330. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11331. #if WASM_ENABLE_GC != 0
  11332. if (local_idx >= param_count) {
  11333. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11334. }
  11335. #endif
  11336. POP_TYPE(local_type);
  11337. break;
  11338. }
  11339. case WASM_OP_TEE_LOCAL:
  11340. {
  11341. p_org = p - 1;
  11342. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11343. #if WASM_ENABLE_FAST_INTERP != 0
  11344. /* If the stack is in polymorphic state, do fake pop and push on
  11345. offset stack to keep the depth of offset stack to be the
  11346. same with ref stack */
  11347. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11348. if (cur_block->is_stack_polymorphic) {
  11349. POP_OFFSET_TYPE(local_type);
  11350. PUSH_OFFSET_TYPE(local_type);
  11351. }
  11352. #endif
  11353. POP_TYPE(local_type);
  11354. PUSH_TYPE(local_type);
  11355. #if WASM_ENABLE_FAST_INTERP != 0
  11356. if (!(preserve_referenced_local(
  11357. loader_ctx, opcode, local_offset, local_type,
  11358. &preserve_local, error_buf, error_buf_size)))
  11359. goto fail;
  11360. if (local_offset < 256
  11361. #if WASM_ENABLE_GC != 0
  11362. && !wasm_is_type_reftype(local_type)
  11363. #endif
  11364. ) {
  11365. skip_label();
  11366. if (is_32bit_type(local_type)) {
  11367. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11368. emit_byte(loader_ctx, (uint8)local_offset);
  11369. }
  11370. else {
  11371. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11372. emit_byte(loader_ctx, (uint8)local_offset);
  11373. }
  11374. }
  11375. else { /* local index larger than 255, reserve leb */
  11376. emit_uint32(loader_ctx, local_idx);
  11377. }
  11378. emit_operand(loader_ctx,
  11379. *(loader_ctx->frame_offset
  11380. - wasm_value_type_cell_num(local_type)));
  11381. #else
  11382. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11383. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11384. if (local_offset < 0x80
  11385. #if WASM_ENABLE_GC != 0
  11386. && !wasm_is_type_reftype(local_type)
  11387. #endif
  11388. ) {
  11389. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11390. if (is_32bit_type(local_type)) {
  11391. *p_org++ = (uint8)local_offset;
  11392. }
  11393. else {
  11394. *p_org++ = (uint8)(local_offset | 0x80);
  11395. }
  11396. while (p_org < p) {
  11397. *p_org++ = WASM_OP_NOP;
  11398. }
  11399. }
  11400. #endif
  11401. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11402. #if WASM_ENABLE_GC != 0
  11403. if (local_idx >= param_count) {
  11404. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11405. }
  11406. #endif
  11407. break;
  11408. }
  11409. case WASM_OP_GET_GLOBAL:
  11410. {
  11411. #if WASM_ENABLE_GC != 0
  11412. WASMRefType *ref_type;
  11413. #endif
  11414. p_org = p - 1;
  11415. read_leb_uint32(p, p_end, global_idx);
  11416. if (global_idx >= global_count) {
  11417. set_error_buf(error_buf, error_buf_size, "unknown global");
  11418. goto fail;
  11419. }
  11420. global_type =
  11421. global_idx < module->import_global_count
  11422. ? module->import_globals[global_idx].u.global.type
  11423. : module
  11424. ->globals[global_idx
  11425. - module->import_global_count]
  11426. .type;
  11427. #if WASM_ENABLE_GC != 0
  11428. ref_type =
  11429. global_idx < module->import_global_count
  11430. ? module->import_globals[global_idx].u.global.ref_type
  11431. : module
  11432. ->globals[global_idx
  11433. - module->import_global_count]
  11434. .ref_type;
  11435. if (wasm_is_type_multi_byte_type(global_type)) {
  11436. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11437. wasm_reftype_struct_size(ref_type));
  11438. }
  11439. #endif
  11440. PUSH_TYPE(global_type);
  11441. #if WASM_ENABLE_FAST_INTERP == 0
  11442. if (global_type == VALUE_TYPE_I64
  11443. || global_type == VALUE_TYPE_F64) {
  11444. #if WASM_ENABLE_DEBUG_INTERP != 0
  11445. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11446. error_buf_size)) {
  11447. goto fail;
  11448. }
  11449. #endif
  11450. *p_org = WASM_OP_GET_GLOBAL_64;
  11451. }
  11452. #else /* else of WASM_ENABLE_FAST_INTERP */
  11453. if (global_type == VALUE_TYPE_I64
  11454. || global_type == VALUE_TYPE_F64) {
  11455. skip_label();
  11456. emit_label(WASM_OP_GET_GLOBAL_64);
  11457. }
  11458. emit_uint32(loader_ctx, global_idx);
  11459. PUSH_OFFSET_TYPE(global_type);
  11460. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11461. break;
  11462. }
  11463. case WASM_OP_SET_GLOBAL:
  11464. {
  11465. bool is_mutable = false;
  11466. #if WASM_ENABLE_GC != 0
  11467. WASMRefType *ref_type;
  11468. #endif
  11469. p_org = p - 1;
  11470. read_leb_uint32(p, p_end, global_idx);
  11471. if (global_idx >= global_count) {
  11472. set_error_buf(error_buf, error_buf_size, "unknown global");
  11473. goto fail;
  11474. }
  11475. is_mutable =
  11476. global_idx < module->import_global_count
  11477. ? module->import_globals[global_idx].u.global.is_mutable
  11478. : module
  11479. ->globals[global_idx
  11480. - module->import_global_count]
  11481. .is_mutable;
  11482. if (!is_mutable) {
  11483. #if WASM_ENABLE_GC == 0
  11484. set_error_buf(error_buf, error_buf_size,
  11485. "global is immutable");
  11486. #else
  11487. set_error_buf(error_buf, error_buf_size,
  11488. "immutable global");
  11489. #endif
  11490. goto fail;
  11491. }
  11492. global_type =
  11493. global_idx < module->import_global_count
  11494. ? module->import_globals[global_idx].u.global.type
  11495. : module
  11496. ->globals[global_idx
  11497. - module->import_global_count]
  11498. .type;
  11499. #if WASM_ENABLE_GC != 0
  11500. ref_type =
  11501. global_idx < module->import_global_count
  11502. ? module->import_globals[global_idx].u.global.ref_type
  11503. : module
  11504. ->globals[global_idx
  11505. - module->import_global_count]
  11506. .ref_type;
  11507. if (wasm_is_type_multi_byte_type(global_type)) {
  11508. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11509. wasm_reftype_struct_size(ref_type));
  11510. }
  11511. #endif
  11512. #if WASM_ENABLE_FAST_INTERP == 0
  11513. if (global_type == VALUE_TYPE_I64
  11514. || global_type == VALUE_TYPE_F64) {
  11515. #if WASM_ENABLE_DEBUG_INTERP != 0
  11516. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11517. error_buf_size)) {
  11518. goto fail;
  11519. }
  11520. #endif
  11521. *p_org = WASM_OP_SET_GLOBAL_64;
  11522. }
  11523. else if (module->aux_stack_size > 0
  11524. && global_idx == module->aux_stack_top_global_index) {
  11525. #if WASM_ENABLE_DEBUG_INTERP != 0
  11526. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11527. error_buf_size)) {
  11528. goto fail;
  11529. }
  11530. #endif
  11531. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11532. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11533. func->has_op_set_global_aux_stack = true;
  11534. #endif
  11535. }
  11536. #else /* else of WASM_ENABLE_FAST_INTERP */
  11537. if (global_type == VALUE_TYPE_I64
  11538. || global_type == VALUE_TYPE_F64) {
  11539. skip_label();
  11540. emit_label(WASM_OP_SET_GLOBAL_64);
  11541. }
  11542. else if (module->aux_stack_size > 0
  11543. && global_idx == module->aux_stack_top_global_index) {
  11544. skip_label();
  11545. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11546. }
  11547. emit_uint32(loader_ctx, global_idx);
  11548. POP_OFFSET_TYPE(global_type);
  11549. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11550. POP_TYPE(global_type);
  11551. break;
  11552. }
  11553. /* load */
  11554. case WASM_OP_I32_LOAD:
  11555. case WASM_OP_I32_LOAD8_S:
  11556. case WASM_OP_I32_LOAD8_U:
  11557. case WASM_OP_I32_LOAD16_S:
  11558. case WASM_OP_I32_LOAD16_U:
  11559. case WASM_OP_I64_LOAD:
  11560. case WASM_OP_I64_LOAD8_S:
  11561. case WASM_OP_I64_LOAD8_U:
  11562. case WASM_OP_I64_LOAD16_S:
  11563. case WASM_OP_I64_LOAD16_U:
  11564. case WASM_OP_I64_LOAD32_S:
  11565. case WASM_OP_I64_LOAD32_U:
  11566. case WASM_OP_F32_LOAD:
  11567. case WASM_OP_F64_LOAD:
  11568. /* store */
  11569. case WASM_OP_I32_STORE:
  11570. case WASM_OP_I32_STORE8:
  11571. case WASM_OP_I32_STORE16:
  11572. case WASM_OP_I64_STORE:
  11573. case WASM_OP_I64_STORE8:
  11574. case WASM_OP_I64_STORE16:
  11575. case WASM_OP_I64_STORE32:
  11576. case WASM_OP_F32_STORE:
  11577. case WASM_OP_F64_STORE:
  11578. {
  11579. #if WASM_ENABLE_FAST_INTERP != 0
  11580. /* change F32/F64 into I32/I64 */
  11581. if (opcode == WASM_OP_F32_LOAD) {
  11582. skip_label();
  11583. emit_label(WASM_OP_I32_LOAD);
  11584. }
  11585. else if (opcode == WASM_OP_F64_LOAD) {
  11586. skip_label();
  11587. emit_label(WASM_OP_I64_LOAD);
  11588. }
  11589. else if (opcode == WASM_OP_F32_STORE) {
  11590. skip_label();
  11591. emit_label(WASM_OP_I32_STORE);
  11592. }
  11593. else if (opcode == WASM_OP_F64_STORE) {
  11594. skip_label();
  11595. emit_label(WASM_OP_I64_STORE);
  11596. }
  11597. #endif
  11598. CHECK_MEMORY();
  11599. read_leb_uint32(p, p_end, align); /* align */
  11600. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11601. if (!check_memory_access_align(opcode, align, error_buf,
  11602. error_buf_size)) {
  11603. goto fail;
  11604. }
  11605. #if WASM_ENABLE_FAST_INTERP != 0
  11606. emit_uint32(loader_ctx, mem_offset);
  11607. #endif
  11608. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11609. func->has_memory_operations = true;
  11610. #endif
  11611. switch (opcode) {
  11612. /* load */
  11613. case WASM_OP_I32_LOAD:
  11614. case WASM_OP_I32_LOAD8_S:
  11615. case WASM_OP_I32_LOAD8_U:
  11616. case WASM_OP_I32_LOAD16_S:
  11617. case WASM_OP_I32_LOAD16_U:
  11618. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11619. break;
  11620. case WASM_OP_I64_LOAD:
  11621. case WASM_OP_I64_LOAD8_S:
  11622. case WASM_OP_I64_LOAD8_U:
  11623. case WASM_OP_I64_LOAD16_S:
  11624. case WASM_OP_I64_LOAD16_U:
  11625. case WASM_OP_I64_LOAD32_S:
  11626. case WASM_OP_I64_LOAD32_U:
  11627. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11628. break;
  11629. case WASM_OP_F32_LOAD:
  11630. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11631. break;
  11632. case WASM_OP_F64_LOAD:
  11633. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11634. break;
  11635. /* store */
  11636. case WASM_OP_I32_STORE:
  11637. case WASM_OP_I32_STORE8:
  11638. case WASM_OP_I32_STORE16:
  11639. POP_I32();
  11640. POP_MEM_OFFSET();
  11641. break;
  11642. case WASM_OP_I64_STORE:
  11643. case WASM_OP_I64_STORE8:
  11644. case WASM_OP_I64_STORE16:
  11645. case WASM_OP_I64_STORE32:
  11646. POP_I64();
  11647. POP_MEM_OFFSET();
  11648. break;
  11649. case WASM_OP_F32_STORE:
  11650. POP_F32();
  11651. POP_MEM_OFFSET();
  11652. break;
  11653. case WASM_OP_F64_STORE:
  11654. POP_F64();
  11655. POP_MEM_OFFSET();
  11656. break;
  11657. default:
  11658. break;
  11659. }
  11660. break;
  11661. }
  11662. case WASM_OP_MEMORY_SIZE:
  11663. CHECK_MEMORY();
  11664. /* reserved byte 0x00 */
  11665. if (*p++ != 0x00) {
  11666. set_error_buf(error_buf, error_buf_size,
  11667. "zero byte expected");
  11668. goto fail;
  11669. }
  11670. PUSH_PAGE_COUNT();
  11671. module->possible_memory_grow = true;
  11672. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11673. func->has_memory_operations = true;
  11674. #endif
  11675. break;
  11676. case WASM_OP_MEMORY_GROW:
  11677. CHECK_MEMORY();
  11678. /* reserved byte 0x00 */
  11679. if (*p++ != 0x00) {
  11680. set_error_buf(error_buf, error_buf_size,
  11681. "zero byte expected");
  11682. goto fail;
  11683. }
  11684. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11685. module->possible_memory_grow = true;
  11686. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11687. || WASM_ENABLE_WAMR_COMPILER != 0
  11688. func->has_op_memory_grow = true;
  11689. #endif
  11690. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11691. func->has_memory_operations = true;
  11692. #endif
  11693. break;
  11694. case WASM_OP_I32_CONST:
  11695. read_leb_int32(p, p_end, i32_const);
  11696. #if WASM_ENABLE_FAST_INTERP != 0
  11697. skip_label();
  11698. disable_emit = true;
  11699. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11700. if (operand_offset == 0) {
  11701. disable_emit = false;
  11702. emit_label(WASM_OP_I32_CONST);
  11703. emit_uint32(loader_ctx, i32_const);
  11704. }
  11705. #else
  11706. (void)i32_const;
  11707. #endif
  11708. PUSH_I32();
  11709. break;
  11710. case WASM_OP_I64_CONST:
  11711. read_leb_int64(p, p_end, i64_const);
  11712. #if WASM_ENABLE_FAST_INTERP != 0
  11713. skip_label();
  11714. disable_emit = true;
  11715. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11716. if (operand_offset == 0) {
  11717. disable_emit = false;
  11718. emit_label(WASM_OP_I64_CONST);
  11719. emit_uint64(loader_ctx, i64_const);
  11720. }
  11721. #endif
  11722. PUSH_I64();
  11723. break;
  11724. case WASM_OP_F32_CONST:
  11725. p += sizeof(float32);
  11726. #if WASM_ENABLE_FAST_INTERP != 0
  11727. skip_label();
  11728. disable_emit = true;
  11729. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11730. sizeof(float32));
  11731. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11732. if (operand_offset == 0) {
  11733. disable_emit = false;
  11734. emit_label(WASM_OP_F32_CONST);
  11735. emit_float32(loader_ctx, f32_const);
  11736. }
  11737. #endif
  11738. PUSH_F32();
  11739. break;
  11740. case WASM_OP_F64_CONST:
  11741. p += sizeof(float64);
  11742. #if WASM_ENABLE_FAST_INTERP != 0
  11743. skip_label();
  11744. disable_emit = true;
  11745. /* Some MCU may require 8-byte align */
  11746. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  11747. sizeof(float64));
  11748. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  11749. if (operand_offset == 0) {
  11750. disable_emit = false;
  11751. emit_label(WASM_OP_F64_CONST);
  11752. emit_float64(loader_ctx, f64_const);
  11753. }
  11754. #endif
  11755. PUSH_F64();
  11756. break;
  11757. case WASM_OP_I32_EQZ:
  11758. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11759. break;
  11760. case WASM_OP_I32_EQ:
  11761. case WASM_OP_I32_NE:
  11762. case WASM_OP_I32_LT_S:
  11763. case WASM_OP_I32_LT_U:
  11764. case WASM_OP_I32_GT_S:
  11765. case WASM_OP_I32_GT_U:
  11766. case WASM_OP_I32_LE_S:
  11767. case WASM_OP_I32_LE_U:
  11768. case WASM_OP_I32_GE_S:
  11769. case WASM_OP_I32_GE_U:
  11770. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11771. break;
  11772. case WASM_OP_I64_EQZ:
  11773. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11774. break;
  11775. case WASM_OP_I64_EQ:
  11776. case WASM_OP_I64_NE:
  11777. case WASM_OP_I64_LT_S:
  11778. case WASM_OP_I64_LT_U:
  11779. case WASM_OP_I64_GT_S:
  11780. case WASM_OP_I64_GT_U:
  11781. case WASM_OP_I64_LE_S:
  11782. case WASM_OP_I64_LE_U:
  11783. case WASM_OP_I64_GE_S:
  11784. case WASM_OP_I64_GE_U:
  11785. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11786. break;
  11787. case WASM_OP_F32_EQ:
  11788. case WASM_OP_F32_NE:
  11789. case WASM_OP_F32_LT:
  11790. case WASM_OP_F32_GT:
  11791. case WASM_OP_F32_LE:
  11792. case WASM_OP_F32_GE:
  11793. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11794. break;
  11795. case WASM_OP_F64_EQ:
  11796. case WASM_OP_F64_NE:
  11797. case WASM_OP_F64_LT:
  11798. case WASM_OP_F64_GT:
  11799. case WASM_OP_F64_LE:
  11800. case WASM_OP_F64_GE:
  11801. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11802. break;
  11803. case WASM_OP_I32_CLZ:
  11804. case WASM_OP_I32_CTZ:
  11805. case WASM_OP_I32_POPCNT:
  11806. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11807. break;
  11808. case WASM_OP_I32_ADD:
  11809. case WASM_OP_I32_SUB:
  11810. case WASM_OP_I32_MUL:
  11811. case WASM_OP_I32_DIV_S:
  11812. case WASM_OP_I32_DIV_U:
  11813. case WASM_OP_I32_REM_S:
  11814. case WASM_OP_I32_REM_U:
  11815. case WASM_OP_I32_AND:
  11816. case WASM_OP_I32_OR:
  11817. case WASM_OP_I32_XOR:
  11818. case WASM_OP_I32_SHL:
  11819. case WASM_OP_I32_SHR_S:
  11820. case WASM_OP_I32_SHR_U:
  11821. case WASM_OP_I32_ROTL:
  11822. case WASM_OP_I32_ROTR:
  11823. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11824. break;
  11825. case WASM_OP_I64_CLZ:
  11826. case WASM_OP_I64_CTZ:
  11827. case WASM_OP_I64_POPCNT:
  11828. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11829. break;
  11830. case WASM_OP_I64_ADD:
  11831. case WASM_OP_I64_SUB:
  11832. case WASM_OP_I64_MUL:
  11833. case WASM_OP_I64_DIV_S:
  11834. case WASM_OP_I64_DIV_U:
  11835. case WASM_OP_I64_REM_S:
  11836. case WASM_OP_I64_REM_U:
  11837. case WASM_OP_I64_AND:
  11838. case WASM_OP_I64_OR:
  11839. case WASM_OP_I64_XOR:
  11840. case WASM_OP_I64_SHL:
  11841. case WASM_OP_I64_SHR_S:
  11842. case WASM_OP_I64_SHR_U:
  11843. case WASM_OP_I64_ROTL:
  11844. case WASM_OP_I64_ROTR:
  11845. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11846. break;
  11847. case WASM_OP_F32_ABS:
  11848. case WASM_OP_F32_NEG:
  11849. case WASM_OP_F32_CEIL:
  11850. case WASM_OP_F32_FLOOR:
  11851. case WASM_OP_F32_TRUNC:
  11852. case WASM_OP_F32_NEAREST:
  11853. case WASM_OP_F32_SQRT:
  11854. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11855. break;
  11856. case WASM_OP_F32_ADD:
  11857. case WASM_OP_F32_SUB:
  11858. case WASM_OP_F32_MUL:
  11859. case WASM_OP_F32_DIV:
  11860. case WASM_OP_F32_MIN:
  11861. case WASM_OP_F32_MAX:
  11862. case WASM_OP_F32_COPYSIGN:
  11863. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11864. break;
  11865. case WASM_OP_F64_ABS:
  11866. case WASM_OP_F64_NEG:
  11867. case WASM_OP_F64_CEIL:
  11868. case WASM_OP_F64_FLOOR:
  11869. case WASM_OP_F64_TRUNC:
  11870. case WASM_OP_F64_NEAREST:
  11871. case WASM_OP_F64_SQRT:
  11872. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11873. break;
  11874. case WASM_OP_F64_ADD:
  11875. case WASM_OP_F64_SUB:
  11876. case WASM_OP_F64_MUL:
  11877. case WASM_OP_F64_DIV:
  11878. case WASM_OP_F64_MIN:
  11879. case WASM_OP_F64_MAX:
  11880. case WASM_OP_F64_COPYSIGN:
  11881. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11882. break;
  11883. case WASM_OP_I32_WRAP_I64:
  11884. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11885. break;
  11886. case WASM_OP_I32_TRUNC_S_F32:
  11887. case WASM_OP_I32_TRUNC_U_F32:
  11888. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11889. break;
  11890. case WASM_OP_I32_TRUNC_S_F64:
  11891. case WASM_OP_I32_TRUNC_U_F64:
  11892. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11893. break;
  11894. case WASM_OP_I64_EXTEND_S_I32:
  11895. case WASM_OP_I64_EXTEND_U_I32:
  11896. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11897. break;
  11898. case WASM_OP_I64_TRUNC_S_F32:
  11899. case WASM_OP_I64_TRUNC_U_F32:
  11900. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  11901. break;
  11902. case WASM_OP_I64_TRUNC_S_F64:
  11903. case WASM_OP_I64_TRUNC_U_F64:
  11904. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  11905. break;
  11906. case WASM_OP_F32_CONVERT_S_I32:
  11907. case WASM_OP_F32_CONVERT_U_I32:
  11908. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11909. break;
  11910. case WASM_OP_F32_CONVERT_S_I64:
  11911. case WASM_OP_F32_CONVERT_U_I64:
  11912. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  11913. break;
  11914. case WASM_OP_F32_DEMOTE_F64:
  11915. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  11916. break;
  11917. case WASM_OP_F64_CONVERT_S_I32:
  11918. case WASM_OP_F64_CONVERT_U_I32:
  11919. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  11920. break;
  11921. case WASM_OP_F64_CONVERT_S_I64:
  11922. case WASM_OP_F64_CONVERT_U_I64:
  11923. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  11924. break;
  11925. case WASM_OP_F64_PROMOTE_F32:
  11926. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  11927. break;
  11928. case WASM_OP_I32_REINTERPRET_F32:
  11929. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11930. break;
  11931. case WASM_OP_I64_REINTERPRET_F64:
  11932. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  11933. break;
  11934. case WASM_OP_F32_REINTERPRET_I32:
  11935. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11936. break;
  11937. case WASM_OP_F64_REINTERPRET_I64:
  11938. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  11939. break;
  11940. case WASM_OP_I32_EXTEND8_S:
  11941. case WASM_OP_I32_EXTEND16_S:
  11942. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11943. break;
  11944. case WASM_OP_I64_EXTEND8_S:
  11945. case WASM_OP_I64_EXTEND16_S:
  11946. case WASM_OP_I64_EXTEND32_S:
  11947. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11948. break;
  11949. #if WASM_ENABLE_GC != 0
  11950. case WASM_OP_GC_PREFIX:
  11951. {
  11952. uint32 opcode1;
  11953. read_leb_uint32(p, p_end, opcode1);
  11954. #if WASM_ENABLE_FAST_INTERP != 0
  11955. emit_byte(loader_ctx, ((uint8)opcode1));
  11956. #endif
  11957. switch (opcode1) {
  11958. case WASM_OP_STRUCT_NEW:
  11959. case WASM_OP_STRUCT_NEW_DEFAULT:
  11960. {
  11961. read_leb_uint32(p, p_end, type_idx);
  11962. #if WASM_ENABLE_FAST_INTERP != 0
  11963. emit_uint32(loader_ctx, type_idx);
  11964. #endif
  11965. if (!check_type_index(module, type_idx, error_buf,
  11966. error_buf_size)) {
  11967. goto fail;
  11968. }
  11969. if (module->types[type_idx]->type_flag
  11970. != WASM_TYPE_STRUCT) {
  11971. set_error_buf(error_buf, error_buf_size,
  11972. "unkown struct type");
  11973. goto fail;
  11974. }
  11975. if (opcode1 == WASM_OP_STRUCT_NEW) {
  11976. int32 j, k;
  11977. uint8 value_type;
  11978. uint32 ref_type_struct_size;
  11979. WASMStructType *struct_type =
  11980. (WASMStructType *)module->types[type_idx];
  11981. k = struct_type->ref_type_map_count - 1;
  11982. for (j = struct_type->field_count - 1; j >= 0;
  11983. j--) {
  11984. value_type = struct_type->fields[j].field_type;
  11985. if (wasm_is_type_reftype(value_type)) {
  11986. if (wasm_is_type_multi_byte_type(
  11987. value_type)) {
  11988. ref_type_struct_size =
  11989. wasm_reftype_struct_size(
  11990. struct_type->ref_type_maps[k]
  11991. .ref_type);
  11992. bh_memcpy_s(
  11993. &wasm_ref_type,
  11994. (uint32)sizeof(WASMRefType),
  11995. struct_type->ref_type_maps[k]
  11996. .ref_type,
  11997. ref_type_struct_size);
  11998. k--;
  11999. }
  12000. POP_REF(value_type);
  12001. }
  12002. else {
  12003. switch (value_type) {
  12004. case VALUE_TYPE_I32:
  12005. case PACKED_TYPE_I8:
  12006. case PACKED_TYPE_I16:
  12007. POP_I32();
  12008. break;
  12009. case VALUE_TYPE_I64:
  12010. POP_I64();
  12011. break;
  12012. case VALUE_TYPE_F32:
  12013. POP_F32();
  12014. break;
  12015. case VALUE_TYPE_F64:
  12016. POP_F64();
  12017. break;
  12018. default:
  12019. set_error_buf(error_buf,
  12020. error_buf_size,
  12021. "unknown type");
  12022. goto fail;
  12023. }
  12024. }
  12025. }
  12026. }
  12027. /* PUSH struct obj, (ref $t) */
  12028. wasm_set_refheaptype_typeidx(
  12029. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12030. PUSH_REF(wasm_ref_type.ref_type);
  12031. break;
  12032. }
  12033. case WASM_OP_STRUCT_GET:
  12034. case WASM_OP_STRUCT_GET_S:
  12035. case WASM_OP_STRUCT_GET_U:
  12036. case WASM_OP_STRUCT_SET:
  12037. {
  12038. WASMStructType *struct_type;
  12039. WASMRefType *ref_type = NULL;
  12040. uint32 field_idx;
  12041. uint8 field_type;
  12042. read_leb_uint32(p, p_end, type_idx);
  12043. #if WASM_ENABLE_FAST_INTERP != 0
  12044. emit_uint32(loader_ctx, type_idx);
  12045. #endif
  12046. if (!check_type_index(module, type_idx, error_buf,
  12047. error_buf_size)) {
  12048. goto fail;
  12049. }
  12050. if (module->types[type_idx]->type_flag
  12051. != WASM_TYPE_STRUCT) {
  12052. set_error_buf(error_buf, error_buf_size,
  12053. "unknown struct type");
  12054. goto fail;
  12055. }
  12056. struct_type = (WASMStructType *)module->types[type_idx];
  12057. read_leb_uint32(p, p_end, field_idx);
  12058. #if WASM_ENABLE_FAST_INTERP != 0
  12059. emit_uint32(loader_ctx, field_idx);
  12060. #endif
  12061. if (field_idx >= struct_type->field_count) {
  12062. set_error_buf(error_buf, error_buf_size,
  12063. "unknown struct field");
  12064. goto fail;
  12065. }
  12066. if (opcode1 == WASM_OP_STRUCT_SET
  12067. && !(struct_type->fields[field_idx].field_flags
  12068. & 1)) {
  12069. set_error_buf(error_buf, error_buf_size,
  12070. "field is immutable");
  12071. goto fail;
  12072. }
  12073. field_type = struct_type->fields[field_idx].field_type;
  12074. if (is_packed_type(field_type)) {
  12075. if (opcode1 == WASM_OP_STRUCT_GET) {
  12076. set_error_buf(error_buf, error_buf_size,
  12077. "type mismatch");
  12078. goto fail;
  12079. }
  12080. else {
  12081. field_type = VALUE_TYPE_I32;
  12082. }
  12083. }
  12084. if (wasm_is_type_multi_byte_type(field_type)) {
  12085. ref_type = wasm_reftype_map_find(
  12086. struct_type->ref_type_maps,
  12087. struct_type->ref_type_map_count, field_idx);
  12088. bh_assert(ref_type);
  12089. }
  12090. if (opcode1 == WASM_OP_STRUCT_SET) {
  12091. /* POP field */
  12092. if (wasm_is_type_multi_byte_type(field_type)) {
  12093. bh_memcpy_s(&wasm_ref_type,
  12094. (uint32)sizeof(WASMRefType),
  12095. ref_type,
  12096. wasm_reftype_struct_size(ref_type));
  12097. }
  12098. POP_REF(field_type);
  12099. /* POP struct obj, (ref null $t) */
  12100. wasm_set_refheaptype_typeidx(
  12101. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12102. POP_REF(wasm_ref_type.ref_type);
  12103. }
  12104. else {
  12105. /* POP struct obj, (ref null $t) */
  12106. wasm_set_refheaptype_typeidx(
  12107. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12108. POP_REF(wasm_ref_type.ref_type);
  12109. /* PUSH field */
  12110. if (wasm_is_type_multi_byte_type(field_type)) {
  12111. bh_memcpy_s(&wasm_ref_type,
  12112. (uint32)sizeof(WASMRefType),
  12113. ref_type,
  12114. wasm_reftype_struct_size(ref_type));
  12115. }
  12116. PUSH_REF(field_type);
  12117. }
  12118. break;
  12119. }
  12120. case WASM_OP_ARRAY_NEW:
  12121. case WASM_OP_ARRAY_NEW_DEFAULT:
  12122. case WASM_OP_ARRAY_NEW_FIXED:
  12123. case WASM_OP_ARRAY_NEW_DATA:
  12124. case WASM_OP_ARRAY_NEW_ELEM:
  12125. {
  12126. WASMArrayType *array_type;
  12127. uint8 elem_type;
  12128. uint32 u32 = 0;
  12129. read_leb_uint32(p, p_end, type_idx);
  12130. #if WASM_ENABLE_FAST_INTERP != 0
  12131. emit_uint32(loader_ctx, type_idx);
  12132. #endif
  12133. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12134. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12135. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12136. read_leb_uint32(p, p_end, u32);
  12137. #if WASM_ENABLE_FAST_INTERP != 0
  12138. emit_uint32(loader_ctx, u32);
  12139. #endif
  12140. }
  12141. if (!check_array_type(module, type_idx, error_buf,
  12142. error_buf_size)) {
  12143. goto fail;
  12144. }
  12145. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12146. /* length */
  12147. POP_I32();
  12148. }
  12149. array_type = (WASMArrayType *)module->types[type_idx];
  12150. elem_type = array_type->elem_type;
  12151. if (opcode1 == WASM_OP_ARRAY_NEW
  12152. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12153. if (wasm_is_type_multi_byte_type(elem_type)) {
  12154. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12155. array_type->elem_ref_type,
  12156. wasm_reftype_struct_size(
  12157. array_type->elem_ref_type));
  12158. }
  12159. if (is_packed_type(elem_type)) {
  12160. elem_type = VALUE_TYPE_I32;
  12161. }
  12162. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12163. uint32 N = u32;
  12164. for (i = 0; i < N; i++) {
  12165. if (wasm_is_type_multi_byte_type(
  12166. elem_type)) {
  12167. bh_memcpy_s(
  12168. &wasm_ref_type, sizeof(WASMRefType),
  12169. array_type->elem_ref_type,
  12170. wasm_reftype_struct_size(
  12171. array_type->elem_ref_type));
  12172. }
  12173. POP_REF(elem_type);
  12174. }
  12175. }
  12176. else
  12177. POP_REF(elem_type);
  12178. }
  12179. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12180. /* offset of data segment */
  12181. POP_I32();
  12182. if (u32 >= module->data_seg_count) {
  12183. set_error_buf(error_buf, error_buf_size,
  12184. "unknown data segement");
  12185. goto fail;
  12186. }
  12187. if (wasm_is_type_reftype(elem_type)) {
  12188. set_error_buf(error_buf, error_buf_size,
  12189. "array elem type mismatch");
  12190. goto fail;
  12191. }
  12192. }
  12193. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12194. WASMTableSeg *table_seg =
  12195. module->table_segments + u32;
  12196. /* offset of element segment */
  12197. POP_I32();
  12198. if (u32 >= module->table_seg_count) {
  12199. set_error_buf(error_buf, error_buf_size,
  12200. "unknown element segement");
  12201. goto fail;
  12202. }
  12203. if (!wasm_reftype_is_subtype_of(
  12204. table_seg->elem_type,
  12205. table_seg->elem_ref_type, elem_type,
  12206. array_type->elem_ref_type, module->types,
  12207. module->type_count)) {
  12208. set_error_buf(error_buf, error_buf_size,
  12209. "array elem type mismatch");
  12210. goto fail;
  12211. }
  12212. }
  12213. /* PUSH array obj, (ref $t) */
  12214. wasm_set_refheaptype_typeidx(
  12215. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12216. PUSH_REF(wasm_ref_type.ref_type);
  12217. break;
  12218. }
  12219. case WASM_OP_ARRAY_GET:
  12220. case WASM_OP_ARRAY_GET_S:
  12221. case WASM_OP_ARRAY_GET_U:
  12222. case WASM_OP_ARRAY_SET:
  12223. {
  12224. uint8 elem_type;
  12225. WASMArrayType *array_type;
  12226. WASMRefType *ref_type = NULL;
  12227. read_leb_uint32(p, p_end, type_idx);
  12228. #if WASM_ENABLE_FAST_INTERP != 0
  12229. emit_uint32(loader_ctx, type_idx);
  12230. #endif
  12231. if (!check_array_type(module, type_idx, error_buf,
  12232. error_buf_size)) {
  12233. goto fail;
  12234. }
  12235. array_type = (WASMArrayType *)module->types[type_idx];
  12236. if (opcode1 == WASM_OP_ARRAY_SET
  12237. && !(array_type->elem_flags & 1)) {
  12238. set_error_buf(error_buf, error_buf_size,
  12239. "array is immutable");
  12240. goto fail;
  12241. }
  12242. elem_type = array_type->elem_type;
  12243. if (is_packed_type(elem_type)) {
  12244. if (opcode1 != WASM_OP_ARRAY_GET_S
  12245. && opcode1 != WASM_OP_ARRAY_GET_U
  12246. && opcode1 != WASM_OP_ARRAY_SET) {
  12247. set_error_buf(error_buf, error_buf_size,
  12248. "type mismatch");
  12249. goto fail;
  12250. }
  12251. else {
  12252. elem_type = VALUE_TYPE_I32;
  12253. }
  12254. }
  12255. ref_type = array_type->elem_ref_type;
  12256. if (opcode1 == WASM_OP_ARRAY_SET) {
  12257. /* POP elem to set */
  12258. if (wasm_is_type_multi_byte_type(elem_type)) {
  12259. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12260. ref_type,
  12261. wasm_reftype_struct_size(ref_type));
  12262. }
  12263. POP_REF(elem_type);
  12264. }
  12265. /* elem idx */
  12266. POP_I32();
  12267. /* POP array obj, (ref null $t) */
  12268. wasm_set_refheaptype_typeidx(
  12269. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12270. POP_REF(wasm_ref_type.ref_type);
  12271. if (opcode1 != WASM_OP_ARRAY_SET) {
  12272. /* PUSH elem */
  12273. if (wasm_is_type_multi_byte_type(elem_type)) {
  12274. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12275. ref_type,
  12276. wasm_reftype_struct_size(ref_type));
  12277. }
  12278. PUSH_REF(elem_type);
  12279. }
  12280. break;
  12281. }
  12282. case WASM_OP_ARRAY_LEN:
  12283. {
  12284. POP_REF(REF_TYPE_ARRAYREF);
  12285. /* length */
  12286. PUSH_I32();
  12287. break;
  12288. }
  12289. case WASM_OP_ARRAY_FILL:
  12290. {
  12291. WASMArrayType *array_type;
  12292. uint8 elem_type;
  12293. /* typeidx */
  12294. read_leb_uint32(p, p_end, type_idx);
  12295. #if WASM_ENABLE_FAST_INTERP != 0
  12296. emit_uint32(loader_ctx, type_idx);
  12297. #endif
  12298. if (!check_array_type(module, type_idx, error_buf,
  12299. error_buf_size)) {
  12300. goto fail;
  12301. }
  12302. array_type = (WASMArrayType *)module->types[type_idx];
  12303. if (!(array_type->elem_flags & 1)) {
  12304. set_error_buf(error_buf, error_buf_size,
  12305. "array is immutable");
  12306. goto fail;
  12307. }
  12308. elem_type = array_type->elem_type;
  12309. if (is_packed_type(elem_type)) {
  12310. elem_type = VALUE_TYPE_I32;
  12311. }
  12312. POP_I32(); /* length */
  12313. #if WASM_ENABLE_FAST_INTERP != 0
  12314. POP_OFFSET_TYPE(elem_type);
  12315. #endif
  12316. POP_TYPE(elem_type);
  12317. POP_I32(); /* start */
  12318. /* POP array obj, (ref null $t) */
  12319. wasm_set_refheaptype_typeidx(
  12320. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12321. POP_REF(wasm_ref_type.ref_type);
  12322. break;
  12323. }
  12324. case WASM_OP_ARRAY_COPY:
  12325. {
  12326. uint32 src_type_idx;
  12327. uint8 src_elem_type, dst_elem_type;
  12328. WASMRefType src_ref_type = { 0 },
  12329. *src_elem_ref_type = NULL;
  12330. WASMRefType dst_ref_type = { 0 },
  12331. *dst_elem_ref_type = NULL;
  12332. WASMArrayType *array_type;
  12333. /* typeidx1 */
  12334. read_leb_uint32(p, p_end, type_idx);
  12335. #if WASM_ENABLE_FAST_INTERP != 0
  12336. emit_uint32(loader_ctx, type_idx);
  12337. #endif
  12338. /* typeidx2 */
  12339. read_leb_uint32(p, p_end, src_type_idx);
  12340. #if WASM_ENABLE_FAST_INTERP != 0
  12341. emit_uint32(loader_ctx, src_type_idx);
  12342. #endif
  12343. if (!check_array_type(module, type_idx, error_buf,
  12344. error_buf_size)) {
  12345. goto fail;
  12346. }
  12347. if (!check_array_type(module, src_type_idx, error_buf,
  12348. error_buf_size)) {
  12349. goto fail;
  12350. }
  12351. POP_I32();
  12352. POP_I32();
  12353. /* POP array obj, (ref null $t) */
  12354. wasm_set_refheaptype_typeidx(
  12355. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12356. POP_REF(wasm_ref_type.ref_type);
  12357. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12358. &wasm_ref_type,
  12359. wasm_reftype_struct_size(&wasm_ref_type));
  12360. POP_I32();
  12361. /* POP array obj, (ref null $t) */
  12362. wasm_set_refheaptype_typeidx(
  12363. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12364. POP_REF(wasm_ref_type.ref_type);
  12365. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12366. &wasm_ref_type,
  12367. wasm_reftype_struct_size(&wasm_ref_type));
  12368. array_type = (WASMArrayType *)module->types[type_idx];
  12369. if (!(array_type->elem_flags & 1)) {
  12370. set_error_buf(error_buf, error_buf_size,
  12371. "destination array is immutable");
  12372. goto fail;
  12373. }
  12374. dst_elem_type = array_type->elem_type;
  12375. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12376. dst_elem_ref_type = array_type->elem_ref_type;
  12377. }
  12378. array_type =
  12379. (WASMArrayType *)module->types[src_type_idx];
  12380. src_elem_type = array_type->elem_type;
  12381. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12382. src_elem_ref_type = array_type->elem_ref_type;
  12383. }
  12384. if (!wasm_reftype_is_subtype_of(
  12385. src_elem_type, src_elem_ref_type, dst_elem_type,
  12386. dst_elem_ref_type, module->types,
  12387. module->type_count)) {
  12388. set_error_buf(error_buf, error_buf_size,
  12389. "array types do not match");
  12390. goto fail;
  12391. }
  12392. break;
  12393. }
  12394. case WASM_OP_REF_I31:
  12395. {
  12396. POP_I32();
  12397. wasm_set_refheaptype_common(
  12398. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12399. PUSH_REF(wasm_ref_type.ref_type);
  12400. break;
  12401. }
  12402. case WASM_OP_I31_GET_S:
  12403. case WASM_OP_I31_GET_U:
  12404. {
  12405. POP_REF(REF_TYPE_I31REF);
  12406. PUSH_I32();
  12407. break;
  12408. }
  12409. case WASM_OP_REF_TEST:
  12410. case WASM_OP_REF_CAST:
  12411. case WASM_OP_REF_TEST_NULLABLE:
  12412. case WASM_OP_REF_CAST_NULLABLE:
  12413. {
  12414. uint8 type;
  12415. read_leb_int32(p, p_end, heap_type);
  12416. #if WASM_ENABLE_FAST_INTERP != 0
  12417. emit_uint32(loader_ctx, (uint32)heap_type);
  12418. #endif
  12419. if (heap_type >= 0) {
  12420. if (!check_type_index(module, heap_type, error_buf,
  12421. error_buf_size)) {
  12422. goto fail;
  12423. }
  12424. }
  12425. else {
  12426. if (!wasm_is_valid_heap_type(heap_type)) {
  12427. set_error_buf(error_buf, error_buf_size,
  12428. "unknown type");
  12429. goto fail;
  12430. }
  12431. }
  12432. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12433. &wasm_ref_type, error_buf,
  12434. error_buf_size)) {
  12435. goto fail;
  12436. }
  12437. if (opcode1 == WASM_OP_REF_TEST
  12438. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12439. PUSH_I32();
  12440. else {
  12441. bool nullable =
  12442. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12443. : false;
  12444. if (heap_type >= 0 || !nullable) {
  12445. wasm_set_refheaptype_typeidx(
  12446. &wasm_ref_type.ref_ht_typeidx, nullable,
  12447. heap_type);
  12448. PUSH_REF(wasm_ref_type.ref_type);
  12449. }
  12450. else {
  12451. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12452. }
  12453. }
  12454. break;
  12455. }
  12456. case WASM_OP_BR_ON_CAST:
  12457. case WASM_OP_BR_ON_CAST_FAIL:
  12458. {
  12459. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12460. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12461. uint8 type_tmp, castflags;
  12462. uint32 depth;
  12463. int32 heap_type_dst;
  12464. bool src_nullable, dst_nullable;
  12465. CHECK_BUF(p, p_end, 1);
  12466. castflags = read_uint8(p);
  12467. #if WASM_ENABLE_FAST_INTERP != 0
  12468. /* Emit heap_type firstly */
  12469. emit_byte(loader_ctx, castflags);
  12470. #endif
  12471. p_org = p;
  12472. read_leb_uint32(p, p_end, depth);
  12473. read_leb_int32(p, p_end, heap_type);
  12474. #if WASM_ENABLE_FAST_INTERP != 0
  12475. /* Emit heap_type firstly */
  12476. emit_uint32(loader_ctx, (uint32)heap_type);
  12477. #endif
  12478. read_leb_int32(p, p_end, heap_type_dst);
  12479. #if WASM_ENABLE_FAST_INTERP != 0
  12480. /* Emit heap_type firstly */
  12481. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12482. #endif
  12483. (void)depth;
  12484. /*
  12485. * castflags should be 0~3:
  12486. * 0: (non-null, non-null)
  12487. * 1: (null, non-null)
  12488. * 2: (non-null, null)
  12489. * 3: (null, null)
  12490. */
  12491. if (castflags > 3) {
  12492. set_error_buf(error_buf, error_buf_size,
  12493. "invalid castflags");
  12494. break;
  12495. }
  12496. src_nullable =
  12497. (castflags == 1) || (castflags == 3) ? true : false;
  12498. dst_nullable =
  12499. (castflags == 2) || (castflags == 3) ? true : false;
  12500. /* Pop and backup the stack top's ref type */
  12501. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12502. &ref_type_tmp, error_buf,
  12503. error_buf_size)) {
  12504. goto fail;
  12505. }
  12506. /* The reference type rt1 must be valid */
  12507. if (!init_ref_type(module, &ref_type1, src_nullable,
  12508. heap_type, error_buf,
  12509. error_buf_size)) {
  12510. goto fail;
  12511. }
  12512. /* The reference type rt2 must be valid. */
  12513. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12514. heap_type_dst, error_buf,
  12515. error_buf_size)) {
  12516. goto fail;
  12517. }
  12518. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12519. &ref_type2);
  12520. /* The reference type rt2 must match rt1. */
  12521. if (!wasm_reftype_is_subtype_of(
  12522. ref_type2.ref_type, &ref_type2,
  12523. ref_type1.ref_type, &ref_type1, module->types,
  12524. module->type_count)) {
  12525. set_error_buf(error_buf, error_buf_size,
  12526. "type mismatch");
  12527. goto fail;
  12528. }
  12529. p = p_org;
  12530. /* Push ref type casted for branch block check */
  12531. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12532. /* The reference type rt2 must match rt′. */
  12533. type_tmp = ref_type2.ref_type;
  12534. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12535. bh_memcpy_s(
  12536. &wasm_ref_type,
  12537. wasm_reftype_struct_size(&ref_type2),
  12538. &ref_type2,
  12539. wasm_reftype_struct_size(&ref_type2));
  12540. }
  12541. }
  12542. else {
  12543. /* The reference type rt′1 must match rt′. */
  12544. type_tmp = ref_type_diff.ref_type;
  12545. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12546. bh_memcpy_s(
  12547. &wasm_ref_type,
  12548. wasm_reftype_struct_size(&ref_type_diff),
  12549. &ref_type_diff,
  12550. wasm_reftype_struct_size(&ref_type_diff));
  12551. }
  12552. }
  12553. PUSH_REF(type_tmp);
  12554. if (!(frame_csp_tmp = check_branch_block(
  12555. loader_ctx, &p, p_end, opcode, error_buf,
  12556. error_buf_size))) {
  12557. goto fail;
  12558. }
  12559. /* Ignore heap_types */
  12560. skip_leb_uint32(p, p_end);
  12561. skip_leb_uint32(p, p_end);
  12562. /* Restore the original stack top's ref type */
  12563. POP_REF(type_tmp);
  12564. #if WASM_ENABLE_FAST_INTERP != 0
  12565. /* Erase the opnd offset emitted by POP_REF() */
  12566. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12567. #endif
  12568. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12569. type_tmp = ref_type_diff.ref_type;
  12570. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12571. bh_memcpy_s(
  12572. &wasm_ref_type,
  12573. wasm_reftype_struct_size(&ref_type_diff),
  12574. &ref_type_diff,
  12575. wasm_reftype_struct_size(&ref_type_diff));
  12576. }
  12577. }
  12578. else {
  12579. type_tmp = ref_type2.ref_type;
  12580. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12581. bh_memcpy_s(
  12582. &wasm_ref_type,
  12583. wasm_reftype_struct_size(&ref_type2),
  12584. &ref_type2,
  12585. wasm_reftype_struct_size(&ref_type2));
  12586. }
  12587. }
  12588. PUSH_REF(type_tmp);
  12589. #if WASM_ENABLE_FAST_INTERP != 0
  12590. /* Erase the opnd offset emitted by PUSH_REF() */
  12591. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12592. #endif
  12593. break;
  12594. }
  12595. case WASM_OP_ANY_CONVERT_EXTERN:
  12596. {
  12597. uint8 type;
  12598. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12599. &wasm_ref_type, error_buf,
  12600. error_buf_size)) {
  12601. goto fail;
  12602. }
  12603. if (!(type == REF_TYPE_EXTERNREF
  12604. || (type == REF_TYPE_HT_NON_NULLABLE
  12605. && wasm_ref_type.ref_ht_common.heap_type
  12606. == HEAP_TYPE_EXTERN)
  12607. || type == VALUE_TYPE_ANY)) {
  12608. set_error_buf(error_buf, error_buf_size,
  12609. "type mismatch");
  12610. goto fail;
  12611. }
  12612. if (type == REF_TYPE_EXTERNREF)
  12613. type = REF_TYPE_ANYREF;
  12614. else {
  12615. wasm_ref_type.ref_ht_common.heap_type =
  12616. HEAP_TYPE_ANY;
  12617. }
  12618. PUSH_REF(type);
  12619. break;
  12620. }
  12621. case WASM_OP_EXTERN_CONVERT_ANY:
  12622. {
  12623. uint8 type;
  12624. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12625. &wasm_ref_type, error_buf,
  12626. error_buf_size)) {
  12627. goto fail;
  12628. }
  12629. if (type == REF_TYPE_EXTERNREF
  12630. || ((type == REF_TYPE_HT_NULLABLE
  12631. || type == REF_TYPE_HT_NON_NULLABLE)
  12632. && wasm_ref_type.ref_ht_common.heap_type
  12633. == HEAP_TYPE_EXTERN)) {
  12634. set_error_buf(error_buf, error_buf_size,
  12635. "type mismatch");
  12636. goto fail;
  12637. }
  12638. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12639. /* push (ref null extern) */
  12640. type = REF_TYPE_EXTERNREF;
  12641. }
  12642. else {
  12643. /* push (ref extern) */
  12644. type = REF_TYPE_HT_NON_NULLABLE;
  12645. wasm_set_refheaptype_common(
  12646. &wasm_ref_type.ref_ht_common, false,
  12647. HEAP_TYPE_EXTERN);
  12648. }
  12649. PUSH_REF(type);
  12650. break;
  12651. }
  12652. #if WASM_ENABLE_STRINGREF != 0
  12653. case WASM_OP_STRING_NEW_UTF8:
  12654. case WASM_OP_STRING_NEW_WTF16:
  12655. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12656. case WASM_OP_STRING_NEW_WTF8:
  12657. {
  12658. uint32 memidx;
  12659. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12660. func->has_memory_operations = true;
  12661. #endif
  12662. read_leb_uint32(p, p_end, memidx);
  12663. #if WASM_ENABLE_FAST_INTERP != 0
  12664. emit_uint32(loader_ctx, (uint32)memidx);
  12665. #endif
  12666. POP_I32();
  12667. POP_I32();
  12668. PUSH_REF(REF_TYPE_STRINGREF);
  12669. (void)memidx;
  12670. break;
  12671. }
  12672. case WASM_OP_STRING_CONST:
  12673. {
  12674. uint32 contents;
  12675. read_leb_uint32(p, p_end, contents);
  12676. #if WASM_ENABLE_FAST_INTERP != 0
  12677. emit_uint32(loader_ctx, (uint32)contents);
  12678. #endif
  12679. PUSH_REF(REF_TYPE_STRINGREF);
  12680. (void)contents;
  12681. break;
  12682. }
  12683. case WASM_OP_STRING_MEASURE_UTF8:
  12684. case WASM_OP_STRING_MEASURE_WTF8:
  12685. case WASM_OP_STRING_MEASURE_WTF16:
  12686. {
  12687. POP_STRINGREF();
  12688. PUSH_I32();
  12689. break;
  12690. }
  12691. case WASM_OP_STRING_ENCODE_UTF8:
  12692. case WASM_OP_STRING_ENCODE_WTF16:
  12693. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12694. case WASM_OP_STRING_ENCODE_WTF8:
  12695. {
  12696. uint32 memidx;
  12697. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12698. func->has_memory_operations = true;
  12699. #endif
  12700. read_leb_uint32(p, p_end, memidx);
  12701. #if WASM_ENABLE_FAST_INTERP != 0
  12702. emit_uint32(loader_ctx, (uint32)memidx);
  12703. #endif
  12704. POP_I32();
  12705. POP_STRINGREF();
  12706. PUSH_I32();
  12707. (void)memidx;
  12708. break;
  12709. }
  12710. case WASM_OP_STRING_CONCAT:
  12711. {
  12712. POP_STRINGREF();
  12713. POP_STRINGREF();
  12714. PUSH_REF(REF_TYPE_STRINGREF);
  12715. break;
  12716. }
  12717. case WASM_OP_STRING_EQ:
  12718. {
  12719. POP_STRINGREF();
  12720. POP_STRINGREF();
  12721. PUSH_I32();
  12722. break;
  12723. }
  12724. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12725. {
  12726. POP_STRINGREF();
  12727. PUSH_I32();
  12728. break;
  12729. }
  12730. case WASM_OP_STRING_AS_WTF8:
  12731. {
  12732. POP_STRINGREF();
  12733. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  12734. break;
  12735. }
  12736. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  12737. {
  12738. POP_I32();
  12739. POP_I32();
  12740. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12741. PUSH_I32();
  12742. break;
  12743. }
  12744. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  12745. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  12746. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  12747. {
  12748. uint32 memidx;
  12749. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12750. func->has_memory_operations = true;
  12751. #endif
  12752. read_leb_uint32(p, p_end, memidx);
  12753. #if WASM_ENABLE_FAST_INTERP != 0
  12754. emit_uint32(loader_ctx, (uint32)memidx);
  12755. #endif
  12756. POP_I32();
  12757. POP_I32();
  12758. POP_I32();
  12759. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12760. PUSH_I32();
  12761. PUSH_I32();
  12762. (void)memidx;
  12763. break;
  12764. }
  12765. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  12766. {
  12767. POP_I32();
  12768. POP_I32();
  12769. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12770. PUSH_REF(REF_TYPE_STRINGREF);
  12771. break;
  12772. }
  12773. case WASM_OP_STRING_AS_WTF16:
  12774. {
  12775. POP_STRINGREF();
  12776. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  12777. break;
  12778. }
  12779. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  12780. {
  12781. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12782. PUSH_I32();
  12783. break;
  12784. }
  12785. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  12786. {
  12787. POP_I32();
  12788. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12789. PUSH_I32();
  12790. break;
  12791. }
  12792. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  12793. {
  12794. uint32 memidx;
  12795. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12796. func->has_memory_operations = true;
  12797. #endif
  12798. read_leb_uint32(p, p_end, memidx);
  12799. #if WASM_ENABLE_FAST_INTERP != 0
  12800. emit_uint32(loader_ctx, (uint32)memidx);
  12801. #endif
  12802. POP_I32();
  12803. POP_I32();
  12804. POP_I32();
  12805. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12806. PUSH_I32();
  12807. (void)memidx;
  12808. break;
  12809. }
  12810. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  12811. {
  12812. POP_I32();
  12813. POP_I32();
  12814. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12815. PUSH_REF(REF_TYPE_STRINGREF);
  12816. break;
  12817. }
  12818. case WASM_OP_STRING_AS_ITER:
  12819. {
  12820. POP_STRINGREF();
  12821. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  12822. break;
  12823. }
  12824. case WASM_OP_STRINGVIEW_ITER_NEXT:
  12825. {
  12826. POP_REF(REF_TYPE_STRINGVIEWITER);
  12827. PUSH_I32();
  12828. break;
  12829. }
  12830. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  12831. case WASM_OP_STRINGVIEW_ITER_REWIND:
  12832. {
  12833. POP_I32();
  12834. POP_REF(REF_TYPE_STRINGVIEWITER);
  12835. PUSH_I32();
  12836. break;
  12837. }
  12838. case WASM_OP_STRINGVIEW_ITER_SLICE:
  12839. {
  12840. POP_I32();
  12841. POP_REF(REF_TYPE_STRINGVIEWITER);
  12842. PUSH_REF(REF_TYPE_STRINGREF);
  12843. break;
  12844. }
  12845. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  12846. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  12847. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  12848. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  12849. {
  12850. POP_I32();
  12851. POP_I32();
  12852. POP_REF(REF_TYPE_ARRAYREF);
  12853. PUSH_REF(REF_TYPE_STRINGREF);
  12854. break;
  12855. }
  12856. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  12857. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  12858. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  12859. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  12860. {
  12861. POP_I32();
  12862. POP_REF(REF_TYPE_ARRAYREF);
  12863. POP_STRINGREF();
  12864. PUSH_I32();
  12865. break;
  12866. }
  12867. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  12868. default:
  12869. set_error_buf_v(error_buf, error_buf_size,
  12870. "%s %02x %02x", "unsupported opcode",
  12871. 0xfb, opcode1);
  12872. goto fail;
  12873. }
  12874. break;
  12875. }
  12876. #endif /* end of WASM_ENABLE_GC != 0 */
  12877. case WASM_OP_MISC_PREFIX:
  12878. {
  12879. uint32 opcode1;
  12880. read_leb_uint32(p, p_end, opcode1);
  12881. #if WASM_ENABLE_FAST_INTERP != 0
  12882. emit_byte(loader_ctx, ((uint8)opcode1));
  12883. #endif
  12884. switch (opcode1) {
  12885. case WASM_OP_I32_TRUNC_SAT_S_F32:
  12886. case WASM_OP_I32_TRUNC_SAT_U_F32:
  12887. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12888. break;
  12889. case WASM_OP_I32_TRUNC_SAT_S_F64:
  12890. case WASM_OP_I32_TRUNC_SAT_U_F64:
  12891. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12892. break;
  12893. case WASM_OP_I64_TRUNC_SAT_S_F32:
  12894. case WASM_OP_I64_TRUNC_SAT_U_F32:
  12895. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12896. break;
  12897. case WASM_OP_I64_TRUNC_SAT_S_F64:
  12898. case WASM_OP_I64_TRUNC_SAT_U_F64:
  12899. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12900. break;
  12901. #if WASM_ENABLE_BULK_MEMORY != 0
  12902. case WASM_OP_MEMORY_INIT:
  12903. {
  12904. read_leb_uint32(p, p_end, data_seg_idx);
  12905. #if WASM_ENABLE_FAST_INTERP != 0
  12906. emit_uint32(loader_ctx, data_seg_idx);
  12907. #endif
  12908. if (module->import_memory_count == 0
  12909. && module->memory_count == 0)
  12910. goto fail_unknown_memory;
  12911. if (*p++ != 0x00)
  12912. goto fail_zero_byte_expected;
  12913. if (data_seg_idx >= module->data_seg_count) {
  12914. set_error_buf_v(error_buf, error_buf_size,
  12915. "unknown data segment %d",
  12916. data_seg_idx);
  12917. goto fail;
  12918. }
  12919. if (module->data_seg_count1 == 0)
  12920. goto fail_data_cnt_sec_require;
  12921. POP_I32();
  12922. POP_I32();
  12923. POP_MEM_OFFSET();
  12924. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12925. func->has_memory_operations = true;
  12926. #endif
  12927. #if WASM_ENABLE_WAMR_COMPILER != 0
  12928. module->is_bulk_memory_used = true;
  12929. #endif
  12930. break;
  12931. }
  12932. case WASM_OP_DATA_DROP:
  12933. {
  12934. read_leb_uint32(p, p_end, data_seg_idx);
  12935. #if WASM_ENABLE_FAST_INTERP != 0
  12936. emit_uint32(loader_ctx, data_seg_idx);
  12937. #endif
  12938. if (data_seg_idx >= module->data_seg_count) {
  12939. set_error_buf(error_buf, error_buf_size,
  12940. "unknown data segment");
  12941. goto fail;
  12942. }
  12943. if (module->data_seg_count1 == 0)
  12944. goto fail_data_cnt_sec_require;
  12945. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12946. func->has_memory_operations = true;
  12947. #endif
  12948. #if WASM_ENABLE_WAMR_COMPILER != 0
  12949. module->is_bulk_memory_used = true;
  12950. #endif
  12951. break;
  12952. }
  12953. case WASM_OP_MEMORY_COPY:
  12954. {
  12955. /* both src and dst memory index should be 0 */
  12956. if (*(int16 *)p != 0x0000)
  12957. goto fail_zero_byte_expected;
  12958. p += 2;
  12959. if (module->import_memory_count == 0
  12960. && module->memory_count == 0)
  12961. goto fail_unknown_memory;
  12962. POP_MEM_OFFSET();
  12963. POP_MEM_OFFSET();
  12964. POP_MEM_OFFSET();
  12965. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12966. func->has_memory_operations = true;
  12967. #endif
  12968. #if WASM_ENABLE_WAMR_COMPILER != 0
  12969. module->is_bulk_memory_used = true;
  12970. #endif
  12971. break;
  12972. }
  12973. case WASM_OP_MEMORY_FILL:
  12974. {
  12975. if (*p++ != 0x00) {
  12976. goto fail_zero_byte_expected;
  12977. }
  12978. if (module->import_memory_count == 0
  12979. && module->memory_count == 0) {
  12980. goto fail_unknown_memory;
  12981. }
  12982. POP_MEM_OFFSET();
  12983. POP_I32();
  12984. POP_MEM_OFFSET();
  12985. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12986. func->has_memory_operations = true;
  12987. #endif
  12988. #if WASM_ENABLE_WAMR_COMPILER != 0
  12989. module->is_bulk_memory_used = true;
  12990. #endif
  12991. break;
  12992. }
  12993. fail_zero_byte_expected:
  12994. set_error_buf(error_buf, error_buf_size,
  12995. "zero byte expected");
  12996. goto fail;
  12997. fail_unknown_memory:
  12998. set_error_buf(error_buf, error_buf_size,
  12999. "unknown memory 0");
  13000. goto fail;
  13001. fail_data_cnt_sec_require:
  13002. set_error_buf(error_buf, error_buf_size,
  13003. "data count section required");
  13004. goto fail;
  13005. #endif /* WASM_ENABLE_BULK_MEMORY */
  13006. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13007. case WASM_OP_TABLE_INIT:
  13008. {
  13009. uint8 seg_type = 0, tbl_type = 0;
  13010. #if WASM_ENABLE_GC != 0
  13011. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13012. #endif
  13013. read_leb_uint32(p, p_end, table_seg_idx);
  13014. read_leb_uint32(p, p_end, table_idx);
  13015. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13016. #if WASM_ENABLE_GC != 0
  13017. (void **)&tbl_ref_type,
  13018. #else
  13019. NULL,
  13020. #endif
  13021. error_buf, error_buf_size))
  13022. goto fail;
  13023. if (!get_table_seg_elem_type(module, table_seg_idx,
  13024. &seg_type,
  13025. #if WASM_ENABLE_GC != 0
  13026. (void **)&seg_ref_type,
  13027. #else
  13028. NULL,
  13029. #endif
  13030. error_buf, error_buf_size))
  13031. goto fail;
  13032. #if WASM_ENABLE_GC == 0
  13033. if (seg_type != tbl_type) {
  13034. set_error_buf(error_buf, error_buf_size,
  13035. "type mismatch");
  13036. goto fail;
  13037. }
  13038. #else
  13039. if (!wasm_reftype_is_subtype_of(
  13040. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13041. module->types, module->type_count)) {
  13042. set_error_buf(error_buf, error_buf_size,
  13043. "type mismatch");
  13044. goto fail;
  13045. }
  13046. #endif
  13047. #if WASM_ENABLE_FAST_INTERP != 0
  13048. emit_uint32(loader_ctx, table_seg_idx);
  13049. emit_uint32(loader_ctx, table_idx);
  13050. #endif
  13051. POP_I32();
  13052. POP_I32();
  13053. POP_I32();
  13054. #if WASM_ENABLE_WAMR_COMPILER != 0
  13055. module->is_ref_types_used = true;
  13056. #endif
  13057. break;
  13058. }
  13059. case WASM_OP_ELEM_DROP:
  13060. {
  13061. read_leb_uint32(p, p_end, table_seg_idx);
  13062. if (!get_table_seg_elem_type(module, table_seg_idx,
  13063. NULL, NULL, error_buf,
  13064. error_buf_size))
  13065. goto fail;
  13066. #if WASM_ENABLE_FAST_INTERP != 0
  13067. emit_uint32(loader_ctx, table_seg_idx);
  13068. #endif
  13069. #if WASM_ENABLE_WAMR_COMPILER != 0
  13070. module->is_ref_types_used = true;
  13071. #endif
  13072. break;
  13073. }
  13074. case WASM_OP_TABLE_COPY:
  13075. {
  13076. uint8 src_type, dst_type;
  13077. #if WASM_ENABLE_GC != 0
  13078. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13079. #endif
  13080. uint32 src_tbl_idx, dst_tbl_idx;
  13081. read_leb_uint32(p, p_end, dst_tbl_idx);
  13082. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13083. #if WASM_ENABLE_GC != 0
  13084. (void **)&dst_ref_type,
  13085. #else
  13086. NULL,
  13087. #endif
  13088. error_buf, error_buf_size))
  13089. goto fail;
  13090. read_leb_uint32(p, p_end, src_tbl_idx);
  13091. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13092. #if WASM_ENABLE_GC != 0
  13093. (void **)&src_ref_type,
  13094. #else
  13095. NULL,
  13096. #endif
  13097. error_buf, error_buf_size))
  13098. goto fail;
  13099. #if WASM_ENABLE_GC == 0
  13100. if (src_type != dst_type) {
  13101. set_error_buf(error_buf, error_buf_size,
  13102. "type mismatch");
  13103. goto fail;
  13104. }
  13105. #else
  13106. if (!wasm_reftype_is_subtype_of(
  13107. src_type, src_ref_type, dst_type, dst_ref_type,
  13108. module->types, module->type_count)) {
  13109. set_error_buf(error_buf, error_buf_size,
  13110. "type mismatch");
  13111. goto fail;
  13112. }
  13113. #endif
  13114. #if WASM_ENABLE_FAST_INTERP != 0
  13115. emit_uint32(loader_ctx, dst_tbl_idx);
  13116. emit_uint32(loader_ctx, src_tbl_idx);
  13117. #endif
  13118. POP_I32();
  13119. POP_I32();
  13120. POP_I32();
  13121. #if WASM_ENABLE_WAMR_COMPILER != 0
  13122. module->is_ref_types_used = true;
  13123. #endif
  13124. break;
  13125. }
  13126. case WASM_OP_TABLE_SIZE:
  13127. {
  13128. read_leb_uint32(p, p_end, table_idx);
  13129. /* TODO: shall we create a new function to check
  13130. table idx instead of using below function? */
  13131. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13132. error_buf, error_buf_size))
  13133. goto fail;
  13134. #if WASM_ENABLE_FAST_INTERP != 0
  13135. emit_uint32(loader_ctx, table_idx);
  13136. #endif
  13137. PUSH_I32();
  13138. #if WASM_ENABLE_WAMR_COMPILER != 0
  13139. module->is_ref_types_used = true;
  13140. #endif
  13141. break;
  13142. }
  13143. case WASM_OP_TABLE_GROW:
  13144. case WASM_OP_TABLE_FILL:
  13145. {
  13146. uint8 decl_type;
  13147. #if WASM_ENABLE_GC != 0
  13148. WASMRefType *ref_type = NULL;
  13149. #endif
  13150. read_leb_uint32(p, p_end, table_idx);
  13151. if (!get_table_elem_type(module, table_idx, &decl_type,
  13152. #if WASM_ENABLE_GC != 0
  13153. (void **)&ref_type,
  13154. #else
  13155. NULL,
  13156. #endif
  13157. error_buf, error_buf_size))
  13158. goto fail;
  13159. #if WASM_ENABLE_GC != 0
  13160. if (wasm_is_type_multi_byte_type(decl_type)) {
  13161. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13162. ref_type,
  13163. wasm_reftype_struct_size(ref_type));
  13164. }
  13165. #endif
  13166. if (opcode1 == WASM_OP_TABLE_GROW) {
  13167. if (table_idx < module->import_table_count) {
  13168. module->import_tables[table_idx]
  13169. .u.table.possible_grow = true;
  13170. }
  13171. else {
  13172. module
  13173. ->tables[table_idx
  13174. - module->import_table_count]
  13175. .possible_grow = true;
  13176. }
  13177. }
  13178. #if WASM_ENABLE_FAST_INTERP != 0
  13179. emit_uint32(loader_ctx, table_idx);
  13180. #endif
  13181. POP_I32();
  13182. #if WASM_ENABLE_FAST_INTERP != 0
  13183. POP_OFFSET_TYPE(decl_type);
  13184. #endif
  13185. POP_TYPE(decl_type);
  13186. if (opcode1 == WASM_OP_TABLE_GROW)
  13187. PUSH_I32();
  13188. else
  13189. POP_I32();
  13190. #if WASM_ENABLE_WAMR_COMPILER != 0
  13191. module->is_ref_types_used = true;
  13192. #endif
  13193. break;
  13194. }
  13195. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13196. default:
  13197. set_error_buf_v(error_buf, error_buf_size,
  13198. "%s %02x %02x", "unsupported opcode",
  13199. 0xfc, opcode1);
  13200. goto fail;
  13201. }
  13202. break;
  13203. }
  13204. #if WASM_ENABLE_SIMD != 0
  13205. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13206. case WASM_OP_SIMD_PREFIX:
  13207. {
  13208. /* TODO: memory64 offset type changes */
  13209. uint32 opcode1;
  13210. #if WASM_ENABLE_WAMR_COMPILER != 0
  13211. /* Mark the SIMD instruction is used in this module */
  13212. module->is_simd_used = true;
  13213. #endif
  13214. read_leb_uint32(p, p_end, opcode1);
  13215. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13216. */
  13217. switch (opcode1) {
  13218. /* memory instruction */
  13219. case SIMD_v128_load:
  13220. case SIMD_v128_load8x8_s:
  13221. case SIMD_v128_load8x8_u:
  13222. case SIMD_v128_load16x4_s:
  13223. case SIMD_v128_load16x4_u:
  13224. case SIMD_v128_load32x2_s:
  13225. case SIMD_v128_load32x2_u:
  13226. case SIMD_v128_load8_splat:
  13227. case SIMD_v128_load16_splat:
  13228. case SIMD_v128_load32_splat:
  13229. case SIMD_v128_load64_splat:
  13230. {
  13231. CHECK_MEMORY();
  13232. read_leb_uint32(p, p_end, align); /* align */
  13233. if (!check_simd_memory_access_align(
  13234. opcode1, align, error_buf, error_buf_size)) {
  13235. goto fail;
  13236. }
  13237. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13238. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13239. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13240. func->has_memory_operations = true;
  13241. #endif
  13242. break;
  13243. }
  13244. case SIMD_v128_store:
  13245. {
  13246. CHECK_MEMORY();
  13247. read_leb_uint32(p, p_end, align); /* align */
  13248. if (!check_simd_memory_access_align(
  13249. opcode1, align, error_buf, error_buf_size)) {
  13250. goto fail;
  13251. }
  13252. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13253. POP_V128();
  13254. POP_I32();
  13255. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13256. func->has_memory_operations = true;
  13257. #endif
  13258. break;
  13259. }
  13260. /* basic operation */
  13261. case SIMD_v128_const:
  13262. {
  13263. CHECK_BUF1(p, p_end, 16);
  13264. p += 16;
  13265. PUSH_V128();
  13266. break;
  13267. }
  13268. case SIMD_v8x16_shuffle:
  13269. {
  13270. V128 mask;
  13271. CHECK_BUF1(p, p_end, 16);
  13272. mask = read_i8x16(p, error_buf, error_buf_size);
  13273. p += 16;
  13274. if (!check_simd_shuffle_mask(mask, error_buf,
  13275. error_buf_size)) {
  13276. goto fail;
  13277. }
  13278. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13279. break;
  13280. }
  13281. case SIMD_v8x16_swizzle:
  13282. {
  13283. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13284. break;
  13285. }
  13286. /* splat operation */
  13287. case SIMD_i8x16_splat:
  13288. case SIMD_i16x8_splat:
  13289. case SIMD_i32x4_splat:
  13290. case SIMD_i64x2_splat:
  13291. case SIMD_f32x4_splat:
  13292. case SIMD_f64x2_splat:
  13293. {
  13294. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13295. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13296. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13297. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13298. VALUE_TYPE_V128);
  13299. break;
  13300. }
  13301. /* lane operation */
  13302. case SIMD_i8x16_extract_lane_s:
  13303. case SIMD_i8x16_extract_lane_u:
  13304. case SIMD_i8x16_replace_lane:
  13305. case SIMD_i16x8_extract_lane_s:
  13306. case SIMD_i16x8_extract_lane_u:
  13307. case SIMD_i16x8_replace_lane:
  13308. case SIMD_i32x4_extract_lane:
  13309. case SIMD_i32x4_replace_lane:
  13310. case SIMD_i64x2_extract_lane:
  13311. case SIMD_i64x2_replace_lane:
  13312. case SIMD_f32x4_extract_lane:
  13313. case SIMD_f32x4_replace_lane:
  13314. case SIMD_f64x2_extract_lane:
  13315. case SIMD_f64x2_replace_lane:
  13316. {
  13317. uint8 lane;
  13318. /* clang-format off */
  13319. uint8 replace[] = {
  13320. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13321. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13322. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13323. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13324. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13325. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13326. };
  13327. uint8 push_type[] = {
  13328. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13329. VALUE_TYPE_V128,
  13330. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13331. VALUE_TYPE_V128,
  13332. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13333. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13334. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13335. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13336. };
  13337. /* clang-format on */
  13338. CHECK_BUF(p, p_end, 1);
  13339. lane = read_uint8(p);
  13340. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13341. error_buf_size)) {
  13342. goto fail;
  13343. }
  13344. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13345. if (!(wasm_loader_pop_frame_ref(
  13346. loader_ctx,
  13347. replace[opcode1
  13348. - SIMD_i8x16_extract_lane_s],
  13349. error_buf, error_buf_size)))
  13350. goto fail;
  13351. }
  13352. POP_AND_PUSH(
  13353. VALUE_TYPE_V128,
  13354. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13355. break;
  13356. }
  13357. /* i8x16 compare operation */
  13358. case SIMD_i8x16_eq:
  13359. case SIMD_i8x16_ne:
  13360. case SIMD_i8x16_lt_s:
  13361. case SIMD_i8x16_lt_u:
  13362. case SIMD_i8x16_gt_s:
  13363. case SIMD_i8x16_gt_u:
  13364. case SIMD_i8x16_le_s:
  13365. case SIMD_i8x16_le_u:
  13366. case SIMD_i8x16_ge_s:
  13367. case SIMD_i8x16_ge_u:
  13368. /* i16x8 compare operation */
  13369. case SIMD_i16x8_eq:
  13370. case SIMD_i16x8_ne:
  13371. case SIMD_i16x8_lt_s:
  13372. case SIMD_i16x8_lt_u:
  13373. case SIMD_i16x8_gt_s:
  13374. case SIMD_i16x8_gt_u:
  13375. case SIMD_i16x8_le_s:
  13376. case SIMD_i16x8_le_u:
  13377. case SIMD_i16x8_ge_s:
  13378. case SIMD_i16x8_ge_u:
  13379. /* i32x4 compare operation */
  13380. case SIMD_i32x4_eq:
  13381. case SIMD_i32x4_ne:
  13382. case SIMD_i32x4_lt_s:
  13383. case SIMD_i32x4_lt_u:
  13384. case SIMD_i32x4_gt_s:
  13385. case SIMD_i32x4_gt_u:
  13386. case SIMD_i32x4_le_s:
  13387. case SIMD_i32x4_le_u:
  13388. case SIMD_i32x4_ge_s:
  13389. case SIMD_i32x4_ge_u:
  13390. /* f32x4 compare operation */
  13391. case SIMD_f32x4_eq:
  13392. case SIMD_f32x4_ne:
  13393. case SIMD_f32x4_lt:
  13394. case SIMD_f32x4_gt:
  13395. case SIMD_f32x4_le:
  13396. case SIMD_f32x4_ge:
  13397. /* f64x2 compare operation */
  13398. case SIMD_f64x2_eq:
  13399. case SIMD_f64x2_ne:
  13400. case SIMD_f64x2_lt:
  13401. case SIMD_f64x2_gt:
  13402. case SIMD_f64x2_le:
  13403. case SIMD_f64x2_ge:
  13404. {
  13405. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13406. break;
  13407. }
  13408. /* v128 operation */
  13409. case SIMD_v128_not:
  13410. {
  13411. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13412. break;
  13413. }
  13414. case SIMD_v128_and:
  13415. case SIMD_v128_andnot:
  13416. case SIMD_v128_or:
  13417. case SIMD_v128_xor:
  13418. {
  13419. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13420. break;
  13421. }
  13422. case SIMD_v128_bitselect:
  13423. {
  13424. POP_V128();
  13425. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13426. break;
  13427. }
  13428. case SIMD_v128_any_true:
  13429. {
  13430. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13431. break;
  13432. }
  13433. /* Load Lane Operation */
  13434. case SIMD_v128_load8_lane:
  13435. case SIMD_v128_load16_lane:
  13436. case SIMD_v128_load32_lane:
  13437. case SIMD_v128_load64_lane:
  13438. case SIMD_v128_store8_lane:
  13439. case SIMD_v128_store16_lane:
  13440. case SIMD_v128_store32_lane:
  13441. case SIMD_v128_store64_lane:
  13442. {
  13443. uint8 lane;
  13444. CHECK_MEMORY();
  13445. read_leb_uint32(p, p_end, align); /* align */
  13446. if (!check_simd_memory_access_align(
  13447. opcode1, align, error_buf, error_buf_size)) {
  13448. goto fail;
  13449. }
  13450. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13451. CHECK_BUF(p, p_end, 1);
  13452. lane = read_uint8(p);
  13453. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13454. error_buf_size)) {
  13455. goto fail;
  13456. }
  13457. POP_V128();
  13458. POP_I32();
  13459. if (opcode1 < SIMD_v128_store8_lane) {
  13460. PUSH_V128();
  13461. }
  13462. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13463. func->has_memory_operations = true;
  13464. #endif
  13465. break;
  13466. }
  13467. case SIMD_v128_load32_zero:
  13468. case SIMD_v128_load64_zero:
  13469. {
  13470. CHECK_MEMORY();
  13471. read_leb_uint32(p, p_end, align); /* align */
  13472. if (!check_simd_memory_access_align(
  13473. opcode1, align, error_buf, error_buf_size)) {
  13474. goto fail;
  13475. }
  13476. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13477. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13478. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13479. func->has_memory_operations = true;
  13480. #endif
  13481. break;
  13482. }
  13483. /* Float conversion */
  13484. case SIMD_f32x4_demote_f64x2_zero:
  13485. case SIMD_f64x2_promote_low_f32x4_zero:
  13486. {
  13487. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13488. break;
  13489. }
  13490. /* i8x16 Operation */
  13491. case SIMD_i8x16_abs:
  13492. case SIMD_i8x16_neg:
  13493. case SIMD_i8x16_popcnt:
  13494. {
  13495. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13496. break;
  13497. }
  13498. case SIMD_i8x16_all_true:
  13499. case SIMD_i8x16_bitmask:
  13500. {
  13501. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13502. break;
  13503. }
  13504. case SIMD_i8x16_narrow_i16x8_s:
  13505. case SIMD_i8x16_narrow_i16x8_u:
  13506. {
  13507. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13508. break;
  13509. }
  13510. case SIMD_f32x4_ceil:
  13511. case SIMD_f32x4_floor:
  13512. case SIMD_f32x4_trunc:
  13513. case SIMD_f32x4_nearest:
  13514. {
  13515. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13516. break;
  13517. }
  13518. case SIMD_i8x16_shl:
  13519. case SIMD_i8x16_shr_s:
  13520. case SIMD_i8x16_shr_u:
  13521. {
  13522. POP_I32();
  13523. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13524. break;
  13525. }
  13526. case SIMD_i8x16_add:
  13527. case SIMD_i8x16_add_sat_s:
  13528. case SIMD_i8x16_add_sat_u:
  13529. case SIMD_i8x16_sub:
  13530. case SIMD_i8x16_sub_sat_s:
  13531. case SIMD_i8x16_sub_sat_u:
  13532. {
  13533. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13534. break;
  13535. }
  13536. case SIMD_f64x2_ceil:
  13537. case SIMD_f64x2_floor:
  13538. {
  13539. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13540. break;
  13541. }
  13542. case SIMD_i8x16_min_s:
  13543. case SIMD_i8x16_min_u:
  13544. case SIMD_i8x16_max_s:
  13545. case SIMD_i8x16_max_u:
  13546. {
  13547. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13548. break;
  13549. }
  13550. case SIMD_f64x2_trunc:
  13551. {
  13552. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13553. break;
  13554. }
  13555. case SIMD_i8x16_avgr_u:
  13556. {
  13557. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13558. break;
  13559. }
  13560. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13561. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13562. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13563. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13564. /* i16x8 operation */
  13565. case SIMD_i16x8_abs:
  13566. case SIMD_i16x8_neg:
  13567. {
  13568. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13569. break;
  13570. }
  13571. case SIMD_i16x8_q15mulr_sat_s:
  13572. {
  13573. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13574. break;
  13575. }
  13576. case SIMD_i16x8_all_true:
  13577. case SIMD_i16x8_bitmask:
  13578. {
  13579. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13580. break;
  13581. }
  13582. case SIMD_i16x8_narrow_i32x4_s:
  13583. case SIMD_i16x8_narrow_i32x4_u:
  13584. {
  13585. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13586. break;
  13587. }
  13588. case SIMD_i16x8_extend_low_i8x16_s:
  13589. case SIMD_i16x8_extend_high_i8x16_s:
  13590. case SIMD_i16x8_extend_low_i8x16_u:
  13591. case SIMD_i16x8_extend_high_i8x16_u:
  13592. {
  13593. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13594. break;
  13595. }
  13596. case SIMD_i16x8_shl:
  13597. case SIMD_i16x8_shr_s:
  13598. case SIMD_i16x8_shr_u:
  13599. {
  13600. POP_I32();
  13601. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13602. break;
  13603. }
  13604. case SIMD_i16x8_add:
  13605. case SIMD_i16x8_add_sat_s:
  13606. case SIMD_i16x8_add_sat_u:
  13607. case SIMD_i16x8_sub:
  13608. case SIMD_i16x8_sub_sat_s:
  13609. case SIMD_i16x8_sub_sat_u:
  13610. {
  13611. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13612. break;
  13613. }
  13614. case SIMD_f64x2_nearest:
  13615. {
  13616. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13617. break;
  13618. }
  13619. case SIMD_i16x8_mul:
  13620. case SIMD_i16x8_min_s:
  13621. case SIMD_i16x8_min_u:
  13622. case SIMD_i16x8_max_s:
  13623. case SIMD_i16x8_max_u:
  13624. case SIMD_i16x8_avgr_u:
  13625. case SIMD_i16x8_extmul_low_i8x16_s:
  13626. case SIMD_i16x8_extmul_high_i8x16_s:
  13627. case SIMD_i16x8_extmul_low_i8x16_u:
  13628. case SIMD_i16x8_extmul_high_i8x16_u:
  13629. {
  13630. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13631. break;
  13632. }
  13633. /* i32x4 operation */
  13634. case SIMD_i32x4_abs:
  13635. case SIMD_i32x4_neg:
  13636. {
  13637. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13638. break;
  13639. }
  13640. case SIMD_i32x4_all_true:
  13641. case SIMD_i32x4_bitmask:
  13642. {
  13643. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13644. break;
  13645. }
  13646. case SIMD_i32x4_extend_low_i16x8_s:
  13647. case SIMD_i32x4_extend_high_i16x8_s:
  13648. case SIMD_i32x4_extend_low_i16x8_u:
  13649. case SIMD_i32x4_extend_high_i16x8_u:
  13650. {
  13651. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13652. break;
  13653. }
  13654. case SIMD_i32x4_shl:
  13655. case SIMD_i32x4_shr_s:
  13656. case SIMD_i32x4_shr_u:
  13657. {
  13658. POP_I32();
  13659. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13660. break;
  13661. }
  13662. case SIMD_i32x4_add:
  13663. case SIMD_i32x4_sub:
  13664. case SIMD_i32x4_mul:
  13665. case SIMD_i32x4_min_s:
  13666. case SIMD_i32x4_min_u:
  13667. case SIMD_i32x4_max_s:
  13668. case SIMD_i32x4_max_u:
  13669. case SIMD_i32x4_dot_i16x8_s:
  13670. case SIMD_i32x4_extmul_low_i16x8_s:
  13671. case SIMD_i32x4_extmul_high_i16x8_s:
  13672. case SIMD_i32x4_extmul_low_i16x8_u:
  13673. case SIMD_i32x4_extmul_high_i16x8_u:
  13674. {
  13675. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13676. break;
  13677. }
  13678. /* i64x2 operation */
  13679. case SIMD_i64x2_abs:
  13680. case SIMD_i64x2_neg:
  13681. {
  13682. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13683. break;
  13684. }
  13685. case SIMD_i64x2_all_true:
  13686. case SIMD_i64x2_bitmask:
  13687. {
  13688. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13689. break;
  13690. }
  13691. case SIMD_i64x2_extend_low_i32x4_s:
  13692. case SIMD_i64x2_extend_high_i32x4_s:
  13693. case SIMD_i64x2_extend_low_i32x4_u:
  13694. case SIMD_i64x2_extend_high_i32x4_u:
  13695. {
  13696. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13697. break;
  13698. }
  13699. case SIMD_i64x2_shl:
  13700. case SIMD_i64x2_shr_s:
  13701. case SIMD_i64x2_shr_u:
  13702. {
  13703. POP_I32();
  13704. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13705. break;
  13706. }
  13707. case SIMD_i64x2_add:
  13708. case SIMD_i64x2_sub:
  13709. case SIMD_i64x2_mul:
  13710. case SIMD_i64x2_eq:
  13711. case SIMD_i64x2_ne:
  13712. case SIMD_i64x2_lt_s:
  13713. case SIMD_i64x2_gt_s:
  13714. case SIMD_i64x2_le_s:
  13715. case SIMD_i64x2_ge_s:
  13716. case SIMD_i64x2_extmul_low_i32x4_s:
  13717. case SIMD_i64x2_extmul_high_i32x4_s:
  13718. case SIMD_i64x2_extmul_low_i32x4_u:
  13719. case SIMD_i64x2_extmul_high_i32x4_u:
  13720. {
  13721. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13722. break;
  13723. }
  13724. /* f32x4 operation */
  13725. case SIMD_f32x4_abs:
  13726. case SIMD_f32x4_neg:
  13727. case SIMD_f32x4_sqrt:
  13728. {
  13729. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13730. break;
  13731. }
  13732. case SIMD_f32x4_add:
  13733. case SIMD_f32x4_sub:
  13734. case SIMD_f32x4_mul:
  13735. case SIMD_f32x4_div:
  13736. case SIMD_f32x4_min:
  13737. case SIMD_f32x4_max:
  13738. case SIMD_f32x4_pmin:
  13739. case SIMD_f32x4_pmax:
  13740. {
  13741. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13742. break;
  13743. }
  13744. /* f64x2 operation */
  13745. case SIMD_f64x2_abs:
  13746. case SIMD_f64x2_neg:
  13747. case SIMD_f64x2_sqrt:
  13748. {
  13749. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13750. break;
  13751. }
  13752. case SIMD_f64x2_add:
  13753. case SIMD_f64x2_sub:
  13754. case SIMD_f64x2_mul:
  13755. case SIMD_f64x2_div:
  13756. case SIMD_f64x2_min:
  13757. case SIMD_f64x2_max:
  13758. case SIMD_f64x2_pmin:
  13759. case SIMD_f64x2_pmax:
  13760. {
  13761. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13762. break;
  13763. }
  13764. case SIMD_i32x4_trunc_sat_f32x4_s:
  13765. case SIMD_i32x4_trunc_sat_f32x4_u:
  13766. case SIMD_f32x4_convert_i32x4_s:
  13767. case SIMD_f32x4_convert_i32x4_u:
  13768. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  13769. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  13770. case SIMD_f64x2_convert_low_i32x4_s:
  13771. case SIMD_f64x2_convert_low_i32x4_u:
  13772. {
  13773. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13774. break;
  13775. }
  13776. default:
  13777. {
  13778. if (error_buf != NULL) {
  13779. snprintf(error_buf, error_buf_size,
  13780. "WASM module load failed: "
  13781. "invalid opcode 0xfd %02x.",
  13782. opcode1);
  13783. }
  13784. goto fail;
  13785. }
  13786. }
  13787. break;
  13788. }
  13789. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  13790. #endif /* end of WASM_ENABLE_SIMD */
  13791. #if WASM_ENABLE_SHARED_MEMORY != 0
  13792. case WASM_OP_ATOMIC_PREFIX:
  13793. {
  13794. uint32 opcode1;
  13795. read_leb_uint32(p, p_end, opcode1);
  13796. #if WASM_ENABLE_FAST_INTERP != 0
  13797. emit_byte(loader_ctx, opcode1);
  13798. #endif
  13799. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  13800. CHECK_MEMORY();
  13801. read_leb_uint32(p, p_end, align); /* align */
  13802. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13803. if (!check_memory_align_equal(opcode1, align, error_buf,
  13804. error_buf_size)) {
  13805. goto fail;
  13806. }
  13807. #if WASM_ENABLE_FAST_INTERP != 0
  13808. emit_uint32(loader_ctx, mem_offset);
  13809. #endif
  13810. }
  13811. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13812. func->has_memory_operations = true;
  13813. #endif
  13814. switch (opcode1) {
  13815. case WASM_OP_ATOMIC_NOTIFY:
  13816. POP_I32();
  13817. POP_MEM_OFFSET();
  13818. PUSH_I32();
  13819. break;
  13820. case WASM_OP_ATOMIC_WAIT32:
  13821. POP_I64();
  13822. POP_I32();
  13823. POP_MEM_OFFSET();
  13824. PUSH_I32();
  13825. break;
  13826. case WASM_OP_ATOMIC_WAIT64:
  13827. POP_I64();
  13828. POP_I64();
  13829. POP_MEM_OFFSET();
  13830. PUSH_I32();
  13831. break;
  13832. case WASM_OP_ATOMIC_FENCE:
  13833. /* reserved byte 0x00 */
  13834. if (*p++ != 0x00) {
  13835. set_error_buf(error_buf, error_buf_size,
  13836. "zero byte expected");
  13837. goto fail;
  13838. }
  13839. break;
  13840. case WASM_OP_ATOMIC_I32_LOAD:
  13841. case WASM_OP_ATOMIC_I32_LOAD8_U:
  13842. case WASM_OP_ATOMIC_I32_LOAD16_U:
  13843. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  13844. break;
  13845. case WASM_OP_ATOMIC_I32_STORE:
  13846. case WASM_OP_ATOMIC_I32_STORE8:
  13847. case WASM_OP_ATOMIC_I32_STORE16:
  13848. POP_I32();
  13849. POP_MEM_OFFSET();
  13850. break;
  13851. case WASM_OP_ATOMIC_I64_LOAD:
  13852. case WASM_OP_ATOMIC_I64_LOAD8_U:
  13853. case WASM_OP_ATOMIC_I64_LOAD16_U:
  13854. case WASM_OP_ATOMIC_I64_LOAD32_U:
  13855. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  13856. break;
  13857. case WASM_OP_ATOMIC_I64_STORE:
  13858. case WASM_OP_ATOMIC_I64_STORE8:
  13859. case WASM_OP_ATOMIC_I64_STORE16:
  13860. case WASM_OP_ATOMIC_I64_STORE32:
  13861. POP_I64();
  13862. POP_MEM_OFFSET();
  13863. break;
  13864. case WASM_OP_ATOMIC_RMW_I32_ADD:
  13865. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  13866. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  13867. case WASM_OP_ATOMIC_RMW_I32_SUB:
  13868. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  13869. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  13870. case WASM_OP_ATOMIC_RMW_I32_AND:
  13871. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  13872. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  13873. case WASM_OP_ATOMIC_RMW_I32_OR:
  13874. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  13875. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  13876. case WASM_OP_ATOMIC_RMW_I32_XOR:
  13877. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  13878. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  13879. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  13880. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  13881. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  13882. POP_I32();
  13883. POP_MEM_OFFSET();
  13884. PUSH_I32();
  13885. break;
  13886. case WASM_OP_ATOMIC_RMW_I64_ADD:
  13887. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  13888. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  13889. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  13890. case WASM_OP_ATOMIC_RMW_I64_SUB:
  13891. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  13892. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  13893. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  13894. case WASM_OP_ATOMIC_RMW_I64_AND:
  13895. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  13896. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  13897. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  13898. case WASM_OP_ATOMIC_RMW_I64_OR:
  13899. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  13900. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  13901. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  13902. case WASM_OP_ATOMIC_RMW_I64_XOR:
  13903. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  13904. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  13905. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  13906. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  13907. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  13908. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  13909. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  13910. POP_I64();
  13911. POP_MEM_OFFSET();
  13912. PUSH_I64();
  13913. break;
  13914. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  13915. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  13916. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  13917. POP_I32();
  13918. POP_I32();
  13919. POP_MEM_OFFSET();
  13920. PUSH_I32();
  13921. break;
  13922. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  13923. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  13924. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  13925. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  13926. POP_I64();
  13927. POP_I64();
  13928. POP_MEM_OFFSET();
  13929. PUSH_I64();
  13930. break;
  13931. default:
  13932. set_error_buf_v(error_buf, error_buf_size,
  13933. "%s %02x %02x", "unsupported opcode",
  13934. 0xfe, opcode1);
  13935. goto fail;
  13936. }
  13937. break;
  13938. }
  13939. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  13940. default:
  13941. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  13942. "unsupported opcode", opcode);
  13943. goto fail;
  13944. }
  13945. #if WASM_ENABLE_FAST_INTERP != 0
  13946. last_op = opcode;
  13947. #endif
  13948. }
  13949. if (loader_ctx->csp_num > 0) {
  13950. if (cur_func_idx < module->function_count - 1)
  13951. /* Function with missing end marker (between two functions) */
  13952. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  13953. else
  13954. /* Function with missing end marker
  13955. (at EOF or end of code sections) */
  13956. set_error_buf(error_buf, error_buf_size,
  13957. "unexpected end of section or function, "
  13958. "or section size mismatch");
  13959. goto fail;
  13960. }
  13961. #if WASM_ENABLE_FAST_INTERP != 0
  13962. if (loader_ctx->p_code_compiled == NULL)
  13963. goto re_scan;
  13964. func->const_cell_num = loader_ctx->const_cell_num;
  13965. if (func->const_cell_num > 0) {
  13966. int32 j;
  13967. if (!(func->consts = func_const = loader_malloc(
  13968. func->const_cell_num * 4, error_buf, error_buf_size)))
  13969. goto fail;
  13970. func_const_end = func->consts + func->const_cell_num * 4;
  13971. /* reverse the const buf */
  13972. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  13973. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  13974. if (c->value_type == VALUE_TYPE_F64
  13975. || c->value_type == VALUE_TYPE_I64) {
  13976. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  13977. &(c->value.f64), (uint32)sizeof(int64));
  13978. func_const += sizeof(int64);
  13979. }
  13980. else {
  13981. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  13982. &(c->value.f32), (uint32)sizeof(int32));
  13983. func_const += sizeof(int32);
  13984. }
  13985. }
  13986. }
  13987. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  13988. - loader_ctx->start_dynamic_offset + 1;
  13989. #else
  13990. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  13991. #endif
  13992. func->max_block_num = loader_ctx->max_csp_num;
  13993. return_value = true;
  13994. fail:
  13995. wasm_loader_ctx_destroy(loader_ctx);
  13996. (void)table_idx;
  13997. (void)table_seg_idx;
  13998. (void)data_seg_idx;
  13999. (void)i64_const;
  14000. (void)local_offset;
  14001. (void)p_org;
  14002. (void)mem_offset;
  14003. (void)align;
  14004. return return_value;
  14005. }