wasm_loader.c 562 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. static void
  36. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  37. {
  38. if (error_buf != NULL) {
  39. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  40. string);
  41. }
  42. }
  43. static void
  44. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  45. {
  46. va_list args;
  47. char buf[128];
  48. if (error_buf != NULL) {
  49. va_start(args, format);
  50. vsnprintf(buf, sizeof(buf), format, args);
  51. va_end(args);
  52. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  53. }
  54. }
  55. static bool
  56. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  57. char *error_buf, uint32 error_buf_size)
  58. {
  59. if ((uintptr_t)buf + length < (uintptr_t)buf
  60. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  61. set_error_buf(error_buf, error_buf_size,
  62. "unexpected end of section or function");
  63. return false;
  64. }
  65. return true;
  66. }
  67. static bool
  68. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  69. char *error_buf, uint32 error_buf_size)
  70. {
  71. if ((uintptr_t)buf + length < (uintptr_t)buf
  72. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  73. set_error_buf(error_buf, error_buf_size, "unexpected end");
  74. return false;
  75. }
  76. return true;
  77. }
  78. #define CHECK_BUF(buf, buf_end, length) \
  79. do { \
  80. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  81. goto fail; \
  82. } \
  83. } while (0)
  84. #define CHECK_BUF1(buf, buf_end, length) \
  85. do { \
  86. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  87. goto fail; \
  88. } \
  89. } while (0)
  90. #define skip_leb(p) while (*p++ & 0x80)
  91. #define skip_leb_int64(p, p_end) skip_leb(p)
  92. #define skip_leb_uint32(p, p_end) skip_leb(p)
  93. #define skip_leb_int32(p, p_end) skip_leb(p)
  94. static bool
  95. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  96. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  97. {
  98. const uint8 *buf = *p_buf;
  99. uint64 result = 0;
  100. uint32 shift = 0;
  101. uint32 offset = 0, bcnt = 0;
  102. uint64 byte;
  103. while (true) {
  104. /* uN or SN must not exceed ceil(N/7) bytes */
  105. if (bcnt + 1 > (maxbits + 6) / 7) {
  106. set_error_buf(error_buf, error_buf_size,
  107. "integer representation too long");
  108. return false;
  109. }
  110. CHECK_BUF(buf, buf_end, offset + 1);
  111. byte = buf[offset];
  112. offset += 1;
  113. result |= ((byte & 0x7f) << shift);
  114. shift += 7;
  115. bcnt += 1;
  116. if ((byte & 0x80) == 0) {
  117. break;
  118. }
  119. }
  120. if (!sign && maxbits == 32 && shift >= maxbits) {
  121. /* The top bits set represent values > 32 bits */
  122. if (((uint8)byte) & 0xf0)
  123. goto fail_integer_too_large;
  124. }
  125. else if (sign && maxbits == 32) {
  126. if (shift < maxbits) {
  127. /* Sign extend, second highest bit is the sign bit */
  128. if ((uint8)byte & 0x40)
  129. result |= (~((uint64)0)) << shift;
  130. }
  131. else {
  132. /* The top bits should be a sign-extension of the sign bit */
  133. bool sign_bit_set = ((uint8)byte) & 0x8;
  134. int top_bits = ((uint8)byte) & 0xf0;
  135. if ((sign_bit_set && top_bits != 0x70)
  136. || (!sign_bit_set && top_bits != 0))
  137. goto fail_integer_too_large;
  138. }
  139. }
  140. else if (sign && maxbits == 64) {
  141. if (shift < maxbits) {
  142. /* Sign extend, second highest bit is the sign bit */
  143. if ((uint8)byte & 0x40)
  144. result |= (~((uint64)0)) << shift;
  145. }
  146. else {
  147. /* The top bits should be a sign-extension of the sign bit */
  148. bool sign_bit_set = ((uint8)byte) & 0x1;
  149. int top_bits = ((uint8)byte) & 0xfe;
  150. if ((sign_bit_set && top_bits != 0x7e)
  151. || (!sign_bit_set && top_bits != 0))
  152. goto fail_integer_too_large;
  153. }
  154. }
  155. *p_buf += offset;
  156. *p_result = result;
  157. return true;
  158. fail_integer_too_large:
  159. set_error_buf(error_buf, error_buf_size, "integer too large");
  160. fail:
  161. return false;
  162. }
  163. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  164. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  165. #define read_leb_int64(p, p_end, res) \
  166. do { \
  167. uint64 res64; \
  168. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  169. error_buf_size)) \
  170. goto fail; \
  171. res = (int64)res64; \
  172. } while (0)
  173. #define read_leb_uint32(p, p_end, res) \
  174. do { \
  175. uint64 res64; \
  176. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  177. error_buf_size)) \
  178. goto fail; \
  179. res = (uint32)res64; \
  180. } while (0)
  181. #define read_leb_int32(p, p_end, res) \
  182. do { \
  183. uint64 res64; \
  184. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  185. error_buf_size)) \
  186. goto fail; \
  187. res = (int32)res64; \
  188. } while (0)
  189. static char *
  190. type2str(uint8 type)
  191. {
  192. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  193. #if WASM_ENABLE_GC != 0
  194. char *type_str_ref[] = { "stringview_iter",
  195. "stringview_wtf16",
  196. "(ref null ht)",
  197. "(ref ht)",
  198. "", /* reserved */
  199. "stringview_wtf8",
  200. "stringref",
  201. "", /* reserved */
  202. "", /* reserved */
  203. "arrayref",
  204. "structref",
  205. "i32ref",
  206. "eqref",
  207. "anyref",
  208. "externref",
  209. "funcref",
  210. "nullref",
  211. "nullexternref",
  212. "nullfuncref" };
  213. #endif
  214. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  215. return type_str[type - VALUE_TYPE_V128];
  216. #if WASM_ENABLE_GC != 0
  217. else if (wasm_is_type_reftype(type))
  218. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  219. #endif
  220. else if (type == VALUE_TYPE_FUNCREF)
  221. return "funcref";
  222. else if (type == VALUE_TYPE_EXTERNREF)
  223. return "externref";
  224. else
  225. return "unknown type";
  226. }
  227. static bool
  228. is_32bit_type(uint8 type)
  229. {
  230. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  231. #if WASM_ENABLE_GC != 0
  232. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  233. #elif WASM_ENABLE_REF_TYPES != 0
  234. /* For reference types, we use uint32 index to represent
  235. the funcref and externref */
  236. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  237. #endif
  238. )
  239. return true;
  240. return false;
  241. }
  242. static bool
  243. is_64bit_type(uint8 type)
  244. {
  245. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  246. #if WASM_ENABLE_GC != 0
  247. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  248. #endif
  249. )
  250. return true;
  251. return false;
  252. }
  253. static bool
  254. is_value_type(uint8 type)
  255. {
  256. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  257. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  258. #if WASM_ENABLE_GC != 0
  259. /* reference types, 0x65 to 0x70 */
  260. || wasm_is_type_reftype(type)
  261. #elif WASM_ENABLE_REF_TYPES != 0
  262. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  263. #endif
  264. #if WASM_ENABLE_SIMD != 0
  265. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  266. || type == VALUE_TYPE_V128 /* 0x7B */
  267. #endif
  268. #endif
  269. )
  270. return true;
  271. return false;
  272. }
  273. #if WASM_ENABLE_GC != 0
  274. static bool
  275. is_packed_type(uint8 type)
  276. {
  277. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  278. }
  279. #endif
  280. static bool
  281. is_byte_a_type(uint8 type)
  282. {
  283. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  284. }
  285. #if WASM_ENABLE_SIMD != 0
  286. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  287. static V128
  288. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  289. {
  290. V128 result;
  291. uint8 i;
  292. for (i = 0; i != 16; ++i) {
  293. result.i8x16[i] = read_uint8(p_buf);
  294. }
  295. return result;
  296. }
  297. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  298. #endif /* end of WASM_ENABLE_SIMD */
  299. static void *
  300. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  301. {
  302. void *mem;
  303. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  304. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  305. return NULL;
  306. }
  307. memset(mem, 0, (uint32)size);
  308. return mem;
  309. }
  310. static void *
  311. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  312. uint32 error_buf_size)
  313. {
  314. uint8 *mem_new;
  315. bh_assert(size_new > size_old);
  316. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  317. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  318. memset(mem_new + size_old, 0, size_new - size_old);
  319. wasm_runtime_free(mem_old);
  320. }
  321. return mem_new;
  322. }
  323. #define MEM_REALLOC(mem, size_old, size_new) \
  324. do { \
  325. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  326. error_buf_size); \
  327. if (!mem_new) \
  328. goto fail; \
  329. mem = mem_new; \
  330. } while (0)
  331. #if WASM_ENABLE_GC != 0
  332. static bool
  333. check_type_index(const WASMModule *module, uint32 type_index, char *error_buf,
  334. uint32 error_buf_size)
  335. {
  336. if (type_index >= module->type_count) {
  337. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  338. type_index);
  339. return false;
  340. }
  341. return true;
  342. }
  343. static bool
  344. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  345. uint32 error_buf_size)
  346. {
  347. if (!check_type_index(module, type_index, error_buf, error_buf_size)) {
  348. return false;
  349. }
  350. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  351. set_error_buf(error_buf, error_buf_size, "unkown array type");
  352. return false;
  353. }
  354. return true;
  355. }
  356. #endif
  357. static bool
  358. check_function_index(const WASMModule *module, uint32 function_index,
  359. char *error_buf, uint32 error_buf_size)
  360. {
  361. if (function_index
  362. >= module->import_function_count + module->function_count) {
  363. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  364. function_index);
  365. return false;
  366. }
  367. return true;
  368. }
  369. typedef struct InitValue {
  370. uint8 type;
  371. uint8 flag;
  372. #if WASM_ENABLE_GC != 0
  373. uint8 gc_opcode;
  374. WASMRefType ref_type;
  375. #endif
  376. WASMValue value;
  377. } InitValue;
  378. typedef struct ConstExprContext {
  379. uint32 sp;
  380. uint32 size;
  381. WASMModule *module;
  382. InitValue *stack;
  383. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  384. } ConstExprContext;
  385. static void
  386. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  387. {
  388. ctx->sp = 0;
  389. ctx->module = module;
  390. ctx->stack = ctx->data;
  391. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  392. }
  393. static bool
  394. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  395. #if WASM_ENABLE_GC != 0
  396. WASMRefType *ref_type, uint8 gc_opcode,
  397. #endif
  398. WASMValue *value, char *error_buf, uint32 error_buf_size)
  399. {
  400. InitValue *cur_value;
  401. if (ctx->sp >= ctx->size) {
  402. if (ctx->stack != ctx->data) {
  403. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  404. (ctx->size + 4) * sizeof(InitValue));
  405. }
  406. else {
  407. if (!(ctx->stack =
  408. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  409. error_buf, error_buf_size))) {
  410. goto fail;
  411. }
  412. }
  413. ctx->size += 4;
  414. }
  415. cur_value = &ctx->stack[ctx->sp++];
  416. cur_value->type = type;
  417. cur_value->flag = flag;
  418. cur_value->value = *value;
  419. #if WASM_ENABLE_GC != 0
  420. cur_value->gc_opcode = gc_opcode;
  421. if (wasm_is_type_multi_byte_type(type)) {
  422. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  423. ref_type, wasm_reftype_struct_size(ref_type));
  424. }
  425. #endif
  426. return true;
  427. fail:
  428. return false;
  429. }
  430. static bool
  431. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  432. #if WASM_ENABLE_GC != 0
  433. WASMRefType *ref_type, uint8 *p_gc_opcode,
  434. #endif
  435. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  436. {
  437. InitValue *cur_value;
  438. if (ctx->sp == 0) {
  439. set_error_buf(error_buf, error_buf_size,
  440. "type mismatch: const expr stack underflow");
  441. return false;
  442. }
  443. cur_value = &ctx->stack[--ctx->sp];
  444. #if WASM_ENABLE_GC == 0
  445. if (cur_value->type != type) {
  446. set_error_buf(error_buf, error_buf_size, "type mismatch");
  447. return false;
  448. }
  449. #else
  450. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  451. ref_type, ctx->module->types,
  452. ctx->module->type_count)) {
  453. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  454. "type mismatch: expect ", type2str(type),
  455. " but got other");
  456. goto fail;
  457. }
  458. if ((ctx->sp != 0) && (cur_value->flag == WASM_OP_GC_PREFIX)
  459. && (cur_value->gc_opcode != WASM_OP_REF_I31)) {
  460. /* To reduce complexity, we don't allow initialize struct fields/array
  461. * element with references, so struct/array must be at the bottom of the
  462. * init value stack */
  463. set_error_buf(
  464. error_buf, error_buf_size,
  465. "struct or array as field is not supported in constant expr");
  466. goto fail;
  467. }
  468. #endif
  469. if (p_flag)
  470. *p_flag = cur_value->flag;
  471. if (p_value)
  472. *p_value = cur_value->value;
  473. #if WASM_ENABLE_GC != 0
  474. if (p_gc_opcode)
  475. *p_gc_opcode = cur_value->gc_opcode;
  476. #endif
  477. return true;
  478. #if WASM_ENABLE_GC != 0
  479. fail:
  480. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  481. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  482. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  483. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  484. wasm_runtime_free(cur_value->value.data);
  485. }
  486. return false;
  487. #endif
  488. }
  489. static void
  490. destroy_const_expr_stack(ConstExprContext *ctx)
  491. {
  492. #if WASM_ENABLE_GC != 0
  493. uint32 i;
  494. for (i = 0; i < ctx->sp; i++) {
  495. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  496. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  497. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  498. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  499. wasm_runtime_free(ctx->stack[i].value.data);
  500. }
  501. }
  502. #endif
  503. if (ctx->stack != ctx->data) {
  504. wasm_runtime_free(ctx->stack);
  505. }
  506. }
  507. #if WASM_ENABLE_GC != 0
  508. static void
  509. destroy_init_expr(InitializerExpression *expr)
  510. {
  511. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  512. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  513. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  514. wasm_runtime_free(expr->u.data);
  515. }
  516. }
  517. #endif /* end of WASM_ENABLE_GC != 0 */
  518. static bool
  519. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  520. InitializerExpression *init_expr, uint8 type, void *ref_type,
  521. char *error_buf, uint32 error_buf_size)
  522. {
  523. const uint8 *p = *p_buf, *p_end = buf_end;
  524. uint8 flag, *p_float;
  525. uint32 i;
  526. ConstExprContext const_expr_ctx = { 0 };
  527. WASMValue cur_value;
  528. #if WASM_ENABLE_GC != 0
  529. uint32 opcode1, type_idx;
  530. uint8 opcode;
  531. WASMRefType cur_ref_type = { 0 };
  532. #endif
  533. init_const_expr_stack(&const_expr_ctx, module);
  534. CHECK_BUF(p, p_end, 1);
  535. flag = read_uint8(p);
  536. while (flag != WASM_OP_END) {
  537. switch (flag) {
  538. /* i32.const */
  539. case INIT_EXPR_TYPE_I32_CONST:
  540. read_leb_int32(p, p_end, cur_value.i32);
  541. if (!push_const_expr_stack(
  542. &const_expr_ctx, flag, VALUE_TYPE_I32,
  543. #if WASM_ENABLE_GC != 0
  544. NULL, 0,
  545. #endif
  546. &cur_value, error_buf, error_buf_size))
  547. goto fail;
  548. break;
  549. /* i64.const */
  550. case INIT_EXPR_TYPE_I64_CONST:
  551. read_leb_int64(p, p_end, cur_value.i64);
  552. if (!push_const_expr_stack(
  553. &const_expr_ctx, flag, VALUE_TYPE_I64,
  554. #if WASM_ENABLE_GC != 0
  555. NULL, 0,
  556. #endif
  557. &cur_value, error_buf, error_buf_size))
  558. goto fail;
  559. break;
  560. /* f32.const */
  561. case INIT_EXPR_TYPE_F32_CONST:
  562. CHECK_BUF(p, p_end, 4);
  563. p_float = (uint8 *)&cur_value.f32;
  564. for (i = 0; i < sizeof(float32); i++)
  565. *p_float++ = *p++;
  566. if (!push_const_expr_stack(
  567. &const_expr_ctx, flag, VALUE_TYPE_F32,
  568. #if WASM_ENABLE_GC != 0
  569. NULL, 0,
  570. #endif
  571. &cur_value, error_buf, error_buf_size))
  572. goto fail;
  573. break;
  574. /* f64.const */
  575. case INIT_EXPR_TYPE_F64_CONST:
  576. CHECK_BUF(p, p_end, 8);
  577. p_float = (uint8 *)&cur_value.f64;
  578. for (i = 0; i < sizeof(float64); i++)
  579. *p_float++ = *p++;
  580. if (!push_const_expr_stack(
  581. &const_expr_ctx, flag, VALUE_TYPE_F64,
  582. #if WASM_ENABLE_GC != 0
  583. NULL, 0,
  584. #endif
  585. &cur_value, error_buf, error_buf_size))
  586. goto fail;
  587. break;
  588. #if WASM_ENABLE_SIMD != 0
  589. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  590. /* v128.const */
  591. case INIT_EXPR_TYPE_V128_CONST:
  592. {
  593. uint64 high, low;
  594. CHECK_BUF(p, p_end, 1);
  595. (void)read_uint8(p);
  596. CHECK_BUF(p, p_end, 16);
  597. wasm_runtime_read_v128(p, &high, &low);
  598. p += 16;
  599. cur_value.v128.i64x2[0] = high;
  600. cur_value.v128.i64x2[1] = low;
  601. if (!push_const_expr_stack(
  602. &const_expr_ctx, flag, VALUE_TYPE_V128,
  603. #if WASM_ENABLE_GC != 0
  604. NULL, 0,
  605. #endif
  606. &cur_value, error_buf, error_buf_size))
  607. goto fail;
  608. #if WASM_ENABLE_WAMR_COMPILER != 0
  609. /* If any init_expr is v128.const, mark SIMD used */
  610. module->is_simd_used = true;
  611. #endif
  612. break;
  613. }
  614. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  615. #endif /* end of WASM_ENABLE_SIMD */
  616. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  617. /* ref.func */
  618. case INIT_EXPR_TYPE_FUNCREF_CONST:
  619. {
  620. uint32 func_idx;
  621. read_leb_uint32(p, p_end, func_idx);
  622. cur_value.ref_index = func_idx;
  623. if (!check_function_index(module, func_idx, error_buf,
  624. error_buf_size)) {
  625. goto fail;
  626. }
  627. #if WASM_ENABLE_GC == 0
  628. if (!push_const_expr_stack(&const_expr_ctx, flag,
  629. VALUE_TYPE_FUNCREF, &cur_value,
  630. error_buf, error_buf_size))
  631. goto fail;
  632. #else
  633. if (func_idx < module->import_function_count) {
  634. type_idx =
  635. module->import_functions[func_idx].u.function.type_idx;
  636. }
  637. else {
  638. type_idx = module
  639. ->functions[func_idx
  640. - module->import_function_count]
  641. ->type_idx;
  642. }
  643. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  644. false, type_idx);
  645. if (!push_const_expr_stack(&const_expr_ctx, flag,
  646. cur_ref_type.ref_type, &cur_ref_type,
  647. 0, &cur_value, error_buf,
  648. error_buf_size))
  649. goto fail;
  650. #endif
  651. #if WASM_ENABLE_WAMR_COMPILER != 0
  652. module->is_ref_types_used = true;
  653. #endif
  654. break;
  655. }
  656. /* ref.null */
  657. case INIT_EXPR_TYPE_REFNULL_CONST:
  658. {
  659. uint8 type1;
  660. CHECK_BUF(p, p_end, 1);
  661. type1 = read_uint8(p);
  662. #if WASM_ENABLE_GC == 0
  663. cur_value.ref_index = NULL_REF;
  664. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  665. &cur_value, error_buf,
  666. error_buf_size))
  667. goto fail;
  668. #else
  669. cur_value.gc_obj = NULL_REF;
  670. if (!is_byte_a_type(type1)) {
  671. p--;
  672. read_leb_uint32(p, p_end, type_idx);
  673. if (!check_type_index(module, type_idx, error_buf,
  674. error_buf_size))
  675. goto fail;
  676. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  677. true, type_idx);
  678. if (!push_const_expr_stack(&const_expr_ctx, flag,
  679. cur_ref_type.ref_type,
  680. &cur_ref_type, 0, &cur_value,
  681. error_buf, error_buf_size))
  682. goto fail;
  683. }
  684. else {
  685. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  686. NULL, 0, &cur_value, error_buf,
  687. error_buf_size))
  688. goto fail;
  689. }
  690. #endif
  691. #if WASM_ENABLE_WAMR_COMPILER != 0
  692. module->is_ref_types_used = true;
  693. #endif
  694. break;
  695. }
  696. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  697. /* get_global */
  698. case INIT_EXPR_TYPE_GET_GLOBAL:
  699. {
  700. uint32 global_idx;
  701. uint8 global_type;
  702. read_leb_uint32(p, p_end, cur_value.global_index);
  703. global_idx = cur_value.global_index;
  704. #if WASM_ENABLE_GC == 0
  705. if (global_idx >= module->import_global_count) {
  706. /**
  707. * Currently, constant expressions occurring as initializers
  708. * of globals are further constrained in that contained
  709. * global.get instructions are
  710. * only allowed to refer to imported globals.
  711. */
  712. set_error_buf_v(error_buf, error_buf_size,
  713. "unknown global %u", global_idx);
  714. goto fail;
  715. }
  716. if (module->import_globals[global_idx].u.global.is_mutable) {
  717. set_error_buf_v(error_buf, error_buf_size,
  718. "constant expression required");
  719. goto fail;
  720. }
  721. #else
  722. if (global_idx
  723. >= module->import_global_count + module->global_count) {
  724. set_error_buf_v(error_buf, error_buf_size,
  725. "unknown global %u", global_idx);
  726. goto fail;
  727. }
  728. if (global_idx < module->import_global_count
  729. && module->import_globals[global_idx].u.global.is_mutable) {
  730. set_error_buf_v(error_buf, error_buf_size,
  731. "constant expression required");
  732. goto fail;
  733. }
  734. #endif
  735. if (global_idx < module->import_global_count) {
  736. global_type =
  737. module->import_globals[global_idx].u.global.type;
  738. #if WASM_ENABLE_GC != 0
  739. if (wasm_is_type_multi_byte_type(global_type)) {
  740. WASMRefType *global_ref_type =
  741. module->import_globals[global_idx]
  742. .u.global.ref_type;
  743. bh_memcpy_s(&cur_ref_type,
  744. wasm_reftype_struct_size(global_ref_type),
  745. global_ref_type,
  746. wasm_reftype_struct_size(global_ref_type));
  747. }
  748. #endif
  749. }
  750. else {
  751. global_type =
  752. module
  753. ->globals[global_idx - module->import_global_count]
  754. .type;
  755. #if WASM_ENABLE_GC != 0
  756. if (wasm_is_type_multi_byte_type(global_type)) {
  757. WASMRefType *global_ref_type =
  758. module
  759. ->globals[global_idx
  760. - module->import_global_count]
  761. .ref_type;
  762. bh_memcpy_s(&cur_ref_type,
  763. wasm_reftype_struct_size(global_ref_type),
  764. global_ref_type,
  765. wasm_reftype_struct_size(global_ref_type));
  766. }
  767. #endif
  768. }
  769. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  770. #if WASM_ENABLE_GC != 0
  771. &cur_ref_type, 0,
  772. #endif
  773. &cur_value, error_buf,
  774. error_buf_size))
  775. goto fail;
  776. break;
  777. }
  778. #if WASM_ENABLE_GC != 0
  779. /* struct.new and array.new */
  780. case WASM_OP_GC_PREFIX:
  781. {
  782. read_leb_uint32(p, p_end, opcode1);
  783. switch (opcode1) {
  784. case WASM_OP_STRUCT_NEW:
  785. {
  786. WASMStructType *struct_type;
  787. WASMStructNewInitValues *struct_init_values = NULL;
  788. uint32 field_count;
  789. read_leb_uint32(p, p_end, type_idx);
  790. if (!check_type_index(module, type_idx, error_buf,
  791. error_buf_size)) {
  792. goto fail;
  793. }
  794. struct_type = (WASMStructType *)module->types[type_idx];
  795. if (struct_type->base_type.type_flag
  796. != WASM_TYPE_STRUCT) {
  797. set_error_buf(error_buf, error_buf_size,
  798. "unkown struct type");
  799. goto fail;
  800. }
  801. field_count = struct_type->field_count;
  802. if (!(struct_init_values = loader_malloc(
  803. offsetof(WASMStructNewInitValues, fields)
  804. + (uint64)field_count * sizeof(WASMValue),
  805. error_buf, error_buf_size))) {
  806. goto fail;
  807. }
  808. struct_init_values->count = field_count;
  809. for (i = field_count; i > 0; i--) {
  810. WASMRefType *field_ref_type = NULL;
  811. uint32 field_idx = i - 1;
  812. uint8 field_type =
  813. struct_type->fields[field_idx].field_type;
  814. if (wasm_is_type_multi_byte_type(field_type)) {
  815. field_ref_type = wasm_reftype_map_find(
  816. struct_type->ref_type_maps,
  817. struct_type->ref_type_map_count, field_idx);
  818. }
  819. if (is_packed_type(field_type)) {
  820. field_type = VALUE_TYPE_I32;
  821. }
  822. if (!pop_const_expr_stack(
  823. &const_expr_ctx, NULL, field_type,
  824. field_ref_type, NULL,
  825. &struct_init_values->fields[field_idx],
  826. error_buf, error_buf_size)) {
  827. wasm_runtime_free(struct_init_values);
  828. goto fail;
  829. }
  830. }
  831. cur_value.data = struct_init_values;
  832. wasm_set_refheaptype_typeidx(
  833. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  834. if (!push_const_expr_stack(
  835. &const_expr_ctx, flag, cur_ref_type.ref_type,
  836. &cur_ref_type, (uint8)opcode1, &cur_value,
  837. error_buf, error_buf_size)) {
  838. wasm_runtime_free(struct_init_values);
  839. goto fail;
  840. }
  841. break;
  842. }
  843. case WASM_OP_STRUCT_NEW_DEFAULT:
  844. {
  845. read_leb_uint32(p, p_end, cur_value.type_index);
  846. type_idx = cur_value.type_index;
  847. if (!check_type_index(module, type_idx, error_buf,
  848. error_buf_size)) {
  849. goto fail;
  850. }
  851. if (module->types[type_idx]->type_flag
  852. != WASM_TYPE_STRUCT) {
  853. set_error_buf(error_buf, error_buf_size,
  854. "unkown struct type");
  855. goto fail;
  856. }
  857. cur_value.type_index = type_idx;
  858. wasm_set_refheaptype_typeidx(
  859. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  860. if (!push_const_expr_stack(
  861. &const_expr_ctx, flag, cur_ref_type.ref_type,
  862. &cur_ref_type, (uint8)opcode1, &cur_value,
  863. error_buf, error_buf_size)) {
  864. goto fail;
  865. }
  866. break;
  867. }
  868. case WASM_OP_ARRAY_NEW:
  869. case WASM_OP_ARRAY_NEW_DEFAULT:
  870. case WASM_OP_ARRAY_NEW_FIXED:
  871. {
  872. WASMArrayNewInitValues *array_init_values = NULL;
  873. WASMArrayType *array_type = NULL;
  874. WASMRefType *elem_ref_type = NULL;
  875. uint64 total_size;
  876. uint8 elem_type;
  877. read_leb_uint32(p, p_end, cur_value.type_index);
  878. type_idx = cur_value.type_index;
  879. if (!check_type_index(module, type_idx, error_buf,
  880. error_buf_size)) {
  881. goto fail;
  882. }
  883. array_type = (WASMArrayType *)module->types[type_idx];
  884. if (array_type->base_type.type_flag
  885. != WASM_TYPE_ARRAY) {
  886. set_error_buf(error_buf, error_buf_size,
  887. "unkown array type");
  888. goto fail;
  889. }
  890. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  891. elem_type = array_type->elem_type;
  892. if (wasm_is_type_multi_byte_type(elem_type)) {
  893. elem_ref_type = array_type->elem_ref_type;
  894. }
  895. if (is_packed_type(elem_type)) {
  896. elem_type = VALUE_TYPE_I32;
  897. }
  898. if (opcode1 == WASM_OP_ARRAY_NEW) {
  899. WASMValue len_val;
  900. if (!(array_init_values = loader_malloc(
  901. sizeof(WASMArrayNewInitValues),
  902. error_buf, error_buf_size))) {
  903. goto fail;
  904. }
  905. array_init_values->type_idx = type_idx;
  906. if (!pop_const_expr_stack(
  907. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  908. NULL, NULL, &len_val, error_buf,
  909. error_buf_size)) {
  910. wasm_runtime_free(array_init_values);
  911. goto fail;
  912. }
  913. array_init_values->length = len_val.i32;
  914. if (!pop_const_expr_stack(
  915. &const_expr_ctx, NULL, elem_type,
  916. elem_ref_type, NULL,
  917. &array_init_values->elem_data[0],
  918. error_buf, error_buf_size)) {
  919. wasm_runtime_free(array_init_values);
  920. goto fail;
  921. }
  922. cur_value.data = array_init_values;
  923. }
  924. else {
  925. /* WASM_OP_ARRAY_NEW_FIXED */
  926. uint32 len;
  927. read_leb_uint32(p, p_end, len);
  928. total_size =
  929. (uint64)offsetof(WASMArrayNewInitValues,
  930. elem_data)
  931. + (uint64)sizeof(WASMValue) * len;
  932. if (!(array_init_values =
  933. loader_malloc(total_size, error_buf,
  934. error_buf_size))) {
  935. goto fail;
  936. }
  937. array_init_values->type_idx = type_idx;
  938. array_init_values->length = len;
  939. for (i = len; i > 0; i--) {
  940. if (!pop_const_expr_stack(
  941. &const_expr_ctx, NULL, elem_type,
  942. elem_ref_type, NULL,
  943. &array_init_values
  944. ->elem_data[i - 1],
  945. error_buf, error_buf_size)) {
  946. wasm_runtime_free(array_init_values);
  947. goto fail;
  948. }
  949. }
  950. cur_value.data = array_init_values;
  951. }
  952. }
  953. else {
  954. /* WASM_OP_ARRAY_NEW_DEFAULT */
  955. WASMValue len_val;
  956. uint32 len;
  957. /* POP(i32) */
  958. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  959. VALUE_TYPE_I32, NULL,
  960. NULL, &len_val, error_buf,
  961. error_buf_size)) {
  962. goto fail;
  963. }
  964. len = len_val.i32;
  965. cur_value.array_new_default.type_index = type_idx;
  966. cur_value.array_new_default.length = len;
  967. }
  968. wasm_set_refheaptype_typeidx(
  969. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  970. if (!push_const_expr_stack(
  971. &const_expr_ctx, flag, cur_ref_type.ref_type,
  972. &cur_ref_type, (uint8)opcode1, &cur_value,
  973. error_buf, error_buf_size)) {
  974. if (array_init_values) {
  975. wasm_runtime_free(array_init_values);
  976. }
  977. goto fail;
  978. }
  979. break;
  980. }
  981. case WASM_OP_ANY_CONVERT_EXTERN:
  982. {
  983. set_error_buf(error_buf, error_buf_size,
  984. "unsuppoted constant expression of "
  985. "extern.internalize");
  986. goto fail;
  987. }
  988. case WASM_OP_EXTERN_CONVERT_ANY:
  989. {
  990. set_error_buf(error_buf, error_buf_size,
  991. "unsuppoted constant expression of "
  992. "extern.externalize");
  993. goto fail;
  994. }
  995. case WASM_OP_REF_I31:
  996. {
  997. /* POP(i32) */
  998. if (!pop_const_expr_stack(
  999. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1000. NULL, &cur_value, error_buf, error_buf_size)) {
  1001. goto fail;
  1002. }
  1003. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1004. false, HEAP_TYPE_I31);
  1005. if (!push_const_expr_stack(
  1006. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1007. &cur_ref_type, (uint8)opcode1, &cur_value,
  1008. error_buf, error_buf_size)) {
  1009. goto fail;
  1010. }
  1011. break;
  1012. }
  1013. default:
  1014. set_error_buf(
  1015. error_buf, error_buf_size,
  1016. "type mismatch or constant expression required");
  1017. goto fail;
  1018. }
  1019. break;
  1020. }
  1021. #endif /* end of WASM_ENABLE_GC != 0 */
  1022. default:
  1023. {
  1024. set_error_buf(error_buf, error_buf_size,
  1025. "illegal opcode "
  1026. "or constant expression required "
  1027. "or type mismatch");
  1028. goto fail;
  1029. }
  1030. }
  1031. CHECK_BUF(p, p_end, 1);
  1032. flag = read_uint8(p);
  1033. }
  1034. /* There should be only one value left on the init value stack */
  1035. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1036. #if WASM_ENABLE_GC != 0
  1037. ref_type, &opcode,
  1038. #endif
  1039. &cur_value, error_buf, error_buf_size)) {
  1040. goto fail;
  1041. }
  1042. if (const_expr_ctx.sp != 0) {
  1043. set_error_buf(error_buf, error_buf_size,
  1044. "type mismatch: illegal constant opcode sequence");
  1045. goto fail;
  1046. }
  1047. init_expr->init_expr_type = flag;
  1048. init_expr->u = cur_value;
  1049. #if WASM_ENABLE_GC != 0
  1050. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1051. switch (opcode) {
  1052. case WASM_OP_STRUCT_NEW:
  1053. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1054. break;
  1055. case WASM_OP_STRUCT_NEW_DEFAULT:
  1056. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1057. break;
  1058. case WASM_OP_ARRAY_NEW:
  1059. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1060. break;
  1061. case WASM_OP_ARRAY_NEW_DEFAULT:
  1062. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1063. break;
  1064. case WASM_OP_ARRAY_NEW_FIXED:
  1065. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1066. break;
  1067. case WASM_OP_REF_I31:
  1068. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1069. break;
  1070. default:
  1071. bh_assert(0);
  1072. break;
  1073. }
  1074. }
  1075. #endif /* end of WASM_ENABLE_GC != 0 */
  1076. *p_buf = p;
  1077. destroy_const_expr_stack(&const_expr_ctx);
  1078. return true;
  1079. fail:
  1080. destroy_const_expr_stack(&const_expr_ctx);
  1081. return false;
  1082. }
  1083. static bool
  1084. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1085. {
  1086. if (mutable >= 2) {
  1087. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1088. return false;
  1089. }
  1090. return true;
  1091. }
  1092. #if WASM_ENABLE_GC != 0
  1093. static void
  1094. destroy_func_type(WASMFuncType *type)
  1095. {
  1096. /* Destroy the reference type hash set */
  1097. if (type->ref_type_maps)
  1098. wasm_runtime_free(type->ref_type_maps);
  1099. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1100. && WASM_ENABLE_LAZY_JIT != 0
  1101. if (type->call_to_llvm_jit_from_fast_jit)
  1102. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1103. #endif
  1104. /* Free the type */
  1105. wasm_runtime_free(type);
  1106. }
  1107. static void
  1108. destroy_struct_type(WASMStructType *type)
  1109. {
  1110. if (type->ref_type_maps)
  1111. wasm_runtime_free(type->ref_type_maps);
  1112. wasm_runtime_free(type);
  1113. }
  1114. static void
  1115. destroy_array_type(WASMArrayType *type)
  1116. {
  1117. wasm_runtime_free(type);
  1118. }
  1119. static void
  1120. destroy_wasm_type(WASMType *type)
  1121. {
  1122. if (type->type_flag == WASM_TYPE_FUNC)
  1123. destroy_func_type((WASMFuncType *)type);
  1124. else if (type->type_flag == WASM_TYPE_STRUCT)
  1125. destroy_struct_type((WASMStructType *)type);
  1126. else if (type->type_flag == WASM_TYPE_ARRAY)
  1127. destroy_array_type((WASMArrayType *)type);
  1128. else {
  1129. bh_assert(0);
  1130. }
  1131. }
  1132. /* Resolve (ref null ht) or (ref ht) */
  1133. static bool
  1134. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1135. WASMModule *module, bool nullable, WASMRefType *ref_type,
  1136. char *error_buf, uint32 error_buf_size)
  1137. {
  1138. const uint8 *p = *p_buf, *p_end = buf_end;
  1139. ref_type->ref_type =
  1140. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1141. ref_type->ref_ht_common.nullable = nullable;
  1142. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1143. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1144. /* heap type is (type i), i : typeidx, >= 0 */
  1145. if (!check_type_index(module, ref_type->ref_ht_typeidx.type_idx,
  1146. error_buf, error_buf_size)) {
  1147. return false;
  1148. }
  1149. }
  1150. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1151. /* heap type is func, extern, any, eq, i31 or data */
  1152. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1153. return false;
  1154. }
  1155. *p_buf = p;
  1156. return true;
  1157. fail:
  1158. return false;
  1159. }
  1160. static bool
  1161. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1162. WASMModule *module, bool *p_need_ref_type_map,
  1163. WASMRefType *ref_type, bool allow_packed_type,
  1164. char *error_buf, uint32 error_buf_size)
  1165. {
  1166. const uint8 *p = *p_buf, *p_end = buf_end;
  1167. uint8 type;
  1168. memset(ref_type, 0, sizeof(WASMRefType));
  1169. CHECK_BUF(p, p_end, 1);
  1170. type = read_uint8(p);
  1171. if (wasm_is_reftype_htref_nullable(type)) {
  1172. /* (ref null ht) */
  1173. if (!resolve_reftype_htref(&p, p_end, module, true, ref_type, error_buf,
  1174. error_buf_size))
  1175. return false;
  1176. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1177. *p_need_ref_type_map = true;
  1178. else {
  1179. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1180. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1181. multi-byte type to one-byte type to reduce the footprint and
  1182. the complexity of type equal/subtype checking */
  1183. ref_type->ref_type =
  1184. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1185. *p_need_ref_type_map = false;
  1186. }
  1187. }
  1188. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1189. /* (ref ht) */
  1190. if (!resolve_reftype_htref(&p, p_end, module, false, ref_type,
  1191. error_buf, error_buf_size))
  1192. return false;
  1193. *p_need_ref_type_map = true;
  1194. #if WASM_ENABLE_STRINGREF != 0
  1195. /* covert (ref string) to stringref */
  1196. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1197. ref_type->ref_type =
  1198. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1199. *p_need_ref_type_map = false;
  1200. }
  1201. #endif
  1202. }
  1203. else {
  1204. /* type which can be represented by one byte */
  1205. if (!is_value_type(type)
  1206. && !(allow_packed_type && is_packed_type(type))) {
  1207. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1208. return false;
  1209. }
  1210. ref_type->ref_type = type;
  1211. *p_need_ref_type_map = false;
  1212. #if WASM_ENABLE_WAMR_COMPILER != 0
  1213. /* If any value's type is v128, mark the module as SIMD used */
  1214. if (type == VALUE_TYPE_V128)
  1215. module->is_simd_used = true;
  1216. #endif
  1217. }
  1218. *p_buf = p;
  1219. return true;
  1220. fail:
  1221. return false;
  1222. }
  1223. static WASMRefType *
  1224. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1225. char *error_buf, uint32 error_buf_size)
  1226. {
  1227. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1228. if (!ret) {
  1229. set_error_buf(error_buf, error_buf_size,
  1230. "insert ref type to hash set failed");
  1231. }
  1232. return ret;
  1233. }
  1234. static bool
  1235. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1236. uint32 type_idx, char *error_buf, uint32 error_buf_size)
  1237. {
  1238. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1239. uint32 param_count, result_count, i, j = 0;
  1240. uint32 param_cell_num, ret_cell_num;
  1241. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1242. uint64 total_size;
  1243. bool need_ref_type_map;
  1244. WASMRefType ref_type;
  1245. WASMFuncType *type = NULL;
  1246. /* Parse first time to resolve param count, result count and
  1247. ref type map count */
  1248. read_leb_uint32(p, p_end, param_count);
  1249. p_org = p;
  1250. for (i = 0; i < param_count; i++) {
  1251. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1252. &ref_type, false, error_buf, error_buf_size)) {
  1253. return false;
  1254. }
  1255. if (need_ref_type_map)
  1256. ref_type_map_count++;
  1257. }
  1258. read_leb_uint32(p, p_end, result_count);
  1259. for (i = 0; i < result_count; i++) {
  1260. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1261. &ref_type, false, error_buf, error_buf_size)) {
  1262. return false;
  1263. }
  1264. if (need_ref_type_map) {
  1265. ref_type_map_count++;
  1266. result_ref_type_map_count++;
  1267. }
  1268. }
  1269. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1270. "ref type map count: %d",
  1271. type_idx, param_count, result_count, ref_type_map_count);
  1272. /* Parse second time to resolve param types, result types and
  1273. ref type map info */
  1274. p = p_org;
  1275. total_size = offsetof(WASMFuncType, types)
  1276. + sizeof(uint8) * (uint64)(param_count + result_count);
  1277. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1278. return false;
  1279. }
  1280. if (ref_type_map_count > 0) {
  1281. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1282. if (!(type->ref_type_maps =
  1283. loader_malloc(total_size, error_buf, error_buf_size))) {
  1284. goto fail;
  1285. }
  1286. }
  1287. type->base_type.type_flag = WASM_TYPE_FUNC;
  1288. type->param_count = param_count;
  1289. type->result_count = result_count;
  1290. type->ref_type_map_count = ref_type_map_count;
  1291. if (ref_type_map_count > 0) {
  1292. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1293. - result_ref_type_map_count;
  1294. }
  1295. for (i = 0; i < param_count; i++) {
  1296. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1297. &ref_type, false, error_buf, error_buf_size)) {
  1298. goto fail;
  1299. }
  1300. type->types[i] = ref_type.ref_type;
  1301. if (need_ref_type_map) {
  1302. type->ref_type_maps[j].index = i;
  1303. if (!(type->ref_type_maps[j++].ref_type =
  1304. reftype_set_insert(module->ref_type_set, &ref_type,
  1305. error_buf, error_buf_size))) {
  1306. goto fail;
  1307. }
  1308. }
  1309. }
  1310. read_leb_uint32(p, p_end, result_count);
  1311. for (i = 0; i < result_count; i++) {
  1312. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1313. &ref_type, false, error_buf, error_buf_size)) {
  1314. goto fail;
  1315. }
  1316. type->types[param_count + i] = ref_type.ref_type;
  1317. if (need_ref_type_map) {
  1318. type->ref_type_maps[j].index = param_count + i;
  1319. if (!(type->ref_type_maps[j++].ref_type =
  1320. reftype_set_insert(module->ref_type_set, &ref_type,
  1321. error_buf, error_buf_size))) {
  1322. goto fail;
  1323. }
  1324. }
  1325. }
  1326. bh_assert(j == type->ref_type_map_count);
  1327. #if TRACE_WASM_LOADER != 0
  1328. os_printf("type %d = ", type_idx);
  1329. wasm_dump_func_type(type);
  1330. #endif
  1331. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1332. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1333. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1334. set_error_buf(error_buf, error_buf_size,
  1335. "param count or result count too large");
  1336. goto fail;
  1337. }
  1338. type->param_cell_num = (uint16)param_cell_num;
  1339. type->ret_cell_num = (uint16)ret_cell_num;
  1340. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1341. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1342. #endif
  1343. #if WASM_ENABLE_WAMR_COMPILER != 0
  1344. for (i = 0; i < type->param_count + type->result_count; i++) {
  1345. if (type->types[i] == VALUE_TYPE_V128)
  1346. module->is_simd_used = true;
  1347. }
  1348. #endif
  1349. /* Calculate the minimal type index of the type equal to this type */
  1350. type->min_type_idx_normalized = type_idx;
  1351. for (i = 0; i < type_idx; i++) {
  1352. WASMFuncType *func_type = (WASMFuncType *)module->types[i];
  1353. if (func_type->base_type.type_flag == WASM_TYPE_FUNC
  1354. && wasm_func_type_equal(type, func_type, module->types,
  1355. type_idx + 1)) {
  1356. type->min_type_idx_normalized = i;
  1357. break;
  1358. }
  1359. }
  1360. *p_buf = p;
  1361. module->types[type_idx] = (WASMType *)type;
  1362. return true;
  1363. fail:
  1364. if (type)
  1365. destroy_func_type(type);
  1366. return false;
  1367. }
  1368. static bool
  1369. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1370. WASMModule *module, uint32 type_idx, char *error_buf,
  1371. uint32 error_buf_size)
  1372. {
  1373. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1374. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1375. uint32 i, j = 0, offset;
  1376. uint16 *reference_table;
  1377. uint64 total_size;
  1378. uint8 mutable;
  1379. bool need_ref_type_map;
  1380. WASMRefType ref_type;
  1381. WASMStructType *type = NULL;
  1382. /* Parse first time to resolve field count and ref type map count */
  1383. read_leb_uint32(p, p_end, field_count);
  1384. p_org = p;
  1385. for (i = 0; i < field_count; i++) {
  1386. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1387. &ref_type, true, error_buf, error_buf_size)) {
  1388. return false;
  1389. }
  1390. if (need_ref_type_map)
  1391. ref_type_map_count++;
  1392. if (wasm_is_type_reftype(ref_type.ref_type))
  1393. ref_field_count++;
  1394. CHECK_BUF(p, p_end, 1);
  1395. mutable = read_uint8(p);
  1396. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1397. return false;
  1398. }
  1399. }
  1400. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1401. type_idx, field_count, ref_type_map_count);
  1402. /* Parse second time to resolve field types and ref type map info */
  1403. p = p_org;
  1404. total_size = offsetof(WASMStructType, fields)
  1405. + sizeof(WASMStructFieldType) * (uint64)field_count
  1406. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1407. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1408. return false;
  1409. }
  1410. if (ref_type_map_count > 0) {
  1411. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1412. if (!(type->ref_type_maps =
  1413. loader_malloc(total_size, error_buf, error_buf_size))) {
  1414. goto fail;
  1415. }
  1416. }
  1417. type->reference_table = reference_table =
  1418. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1419. + sizeof(WASMStructFieldType) * field_count);
  1420. *reference_table++ = ref_field_count;
  1421. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1422. type->field_count = field_count;
  1423. type->ref_type_map_count = ref_type_map_count;
  1424. offset = (uint32)offsetof(WASMStructObject, field_data);
  1425. for (i = 0; i < field_count; i++) {
  1426. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1427. &ref_type, true, error_buf, error_buf_size)) {
  1428. goto fail;
  1429. }
  1430. type->fields[i].field_type = ref_type.ref_type;
  1431. if (need_ref_type_map) {
  1432. type->ref_type_maps[j].index = i;
  1433. if (!(type->ref_type_maps[j++].ref_type =
  1434. reftype_set_insert(module->ref_type_set, &ref_type,
  1435. error_buf, error_buf_size))) {
  1436. goto fail;
  1437. }
  1438. }
  1439. CHECK_BUF(p, p_end, 1);
  1440. type->fields[i].field_flags = read_uint8(p);
  1441. type->fields[i].field_size =
  1442. (uint8)wasm_reftype_size(ref_type.ref_type);
  1443. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1444. || defined(BUILD_TARGET_X86_32))
  1445. if (type->fields[i].field_size == 2)
  1446. offset = align_uint(offset, 2);
  1447. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1448. offset = align_uint(offset, 4);
  1449. #endif
  1450. type->fields[i].field_offset = offset;
  1451. if (wasm_is_type_reftype(ref_type.ref_type))
  1452. *reference_table++ = offset;
  1453. offset += type->fields[i].field_size;
  1454. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1455. type->fields[i].field_flags, type->fields[i].field_type);
  1456. }
  1457. type->total_size = offset;
  1458. bh_assert(j == type->ref_type_map_count);
  1459. #if TRACE_WASM_LOADER != 0
  1460. os_printf("type %d = ", type_idx);
  1461. wasm_dump_struct_type(type);
  1462. #endif
  1463. *p_buf = p;
  1464. module->types[type_idx] = (WASMType *)type;
  1465. return true;
  1466. fail:
  1467. if (type)
  1468. destroy_struct_type(type);
  1469. return false;
  1470. }
  1471. static bool
  1472. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1473. WASMModule *module, uint32 type_idx, char *error_buf,
  1474. uint32 error_buf_size)
  1475. {
  1476. const uint8 *p = *p_buf, *p_end = buf_end;
  1477. uint8 mutable;
  1478. bool need_ref_type_map;
  1479. WASMRefType ref_type;
  1480. WASMArrayType *type = NULL;
  1481. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  1482. true, error_buf, error_buf_size)) {
  1483. return false;
  1484. }
  1485. CHECK_BUF(p, p_end, 1);
  1486. mutable = read_uint8(p);
  1487. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1488. return false;
  1489. }
  1490. LOG_VERBOSE("type %u: array", type_idx);
  1491. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1492. error_buf_size))) {
  1493. return false;
  1494. }
  1495. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1496. type->elem_flags = mutable;
  1497. type->elem_type = ref_type.ref_type;
  1498. if (need_ref_type_map) {
  1499. if (!(type->elem_ref_type =
  1500. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1501. error_buf_size))) {
  1502. goto fail;
  1503. }
  1504. }
  1505. #if TRACE_WASM_LOADER != 0
  1506. os_printf("type %d = ", type_idx);
  1507. wasm_dump_array_type(type);
  1508. #endif
  1509. *p_buf = p;
  1510. module->types[type_idx] = (WASMType *)type;
  1511. return true;
  1512. fail:
  1513. if (type)
  1514. destroy_array_type(type);
  1515. return false;
  1516. }
  1517. static bool
  1518. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1519. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1520. {
  1521. if (heap_type >= 0) {
  1522. if (!check_type_index(module, heap_type, error_buf, error_buf_size)) {
  1523. return false;
  1524. }
  1525. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1526. heap_type);
  1527. }
  1528. else {
  1529. if (!wasm_is_valid_heap_type(heap_type)) {
  1530. set_error_buf(error_buf, error_buf_size, "unknown type");
  1531. return false;
  1532. }
  1533. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1534. heap_type);
  1535. if (nullable) {
  1536. /* For (ref null func/extern/any/eq/i31/data),
  1537. they are same as
  1538. funcref/externref/anyref/eqref/i31ref/dataref,
  1539. we convert the multi-byte type to one-byte
  1540. type to reduce the footprint and the
  1541. complexity of type equal/subtype checking */
  1542. ref_type->ref_type =
  1543. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1544. }
  1545. }
  1546. return true;
  1547. }
  1548. static void
  1549. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1550. WASMRefType *ref_type2)
  1551. {
  1552. /**
  1553. * The difference rt1 ∖ rt2 between two reference types is defined as
  1554. * follows:
  1555. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1556. * (ref ht2) = (ref null?1 ht1)
  1557. */
  1558. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1559. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1560. ref_type1, wasm_reftype_struct_size(ref_type1));
  1561. }
  1562. else {
  1563. ref_type_diff->ref_type = ref_type1->ref_type;
  1564. }
  1565. if (ref_type2->ref_ht_common.nullable) {
  1566. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1567. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1568. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1569. (int32)ref_type_diff->ref_type - 0x80);
  1570. }
  1571. else {
  1572. ref_type_diff->ref_ht_typeidx.nullable = false;
  1573. }
  1574. }
  1575. }
  1576. #else /* else of WASM_ENABLE_GC != 0 */
  1577. static void
  1578. destroy_wasm_type(WASMType *type)
  1579. {
  1580. if (type->ref_count > 1) {
  1581. /* The type is referenced by other types
  1582. of current wasm module */
  1583. type->ref_count--;
  1584. return;
  1585. }
  1586. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1587. && WASM_ENABLE_LAZY_JIT != 0
  1588. if (type->call_to_llvm_jit_from_fast_jit)
  1589. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1590. #endif
  1591. wasm_runtime_free(type);
  1592. }
  1593. #endif /* end of WASM_ENABLE_GC != 0 */
  1594. static bool
  1595. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1596. char *error_buf, uint32 error_buf_size)
  1597. {
  1598. const uint8 *p = buf, *p_end = buf_end;
  1599. uint32 type_count, i;
  1600. uint64 total_size;
  1601. uint8 flag;
  1602. #if WASM_ENABLE_GC != 0
  1603. uint32 processed_type_count = 0;
  1604. #endif
  1605. read_leb_uint32(p, p_end, type_count);
  1606. if (type_count) {
  1607. module->type_count = type_count;
  1608. total_size = sizeof(WASMType *) * (uint64)type_count;
  1609. if (!(module->types =
  1610. loader_malloc(total_size, error_buf, error_buf_size))) {
  1611. return false;
  1612. }
  1613. #if WASM_ENABLE_GC == 0
  1614. for (i = 0; i < type_count; i++) {
  1615. WASMFuncType *type;
  1616. const uint8 *p_org;
  1617. uint32 param_count, result_count, j;
  1618. uint32 param_cell_num, ret_cell_num;
  1619. CHECK_BUF(p, p_end, 1);
  1620. flag = read_uint8(p);
  1621. if (flag != 0x60) {
  1622. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1623. return false;
  1624. }
  1625. read_leb_uint32(p, p_end, param_count);
  1626. /* Resolve param count and result count firstly */
  1627. p_org = p;
  1628. CHECK_BUF(p, p_end, param_count);
  1629. p += param_count;
  1630. read_leb_uint32(p, p_end, result_count);
  1631. CHECK_BUF(p, p_end, result_count);
  1632. p = p_org;
  1633. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1634. set_error_buf(error_buf, error_buf_size,
  1635. "param count or result count too large");
  1636. return false;
  1637. }
  1638. total_size = offsetof(WASMFuncType, types)
  1639. + sizeof(uint8) * (uint64)(param_count + result_count);
  1640. if (!(type = module->types[i] =
  1641. loader_malloc(total_size, error_buf, error_buf_size))) {
  1642. return false;
  1643. }
  1644. /* Resolve param types and result types */
  1645. type->ref_count = 1;
  1646. type->param_count = (uint16)param_count;
  1647. type->result_count = (uint16)result_count;
  1648. for (j = 0; j < param_count; j++) {
  1649. CHECK_BUF(p, p_end, 1);
  1650. type->types[j] = read_uint8(p);
  1651. }
  1652. read_leb_uint32(p, p_end, result_count);
  1653. for (j = 0; j < result_count; j++) {
  1654. CHECK_BUF(p, p_end, 1);
  1655. type->types[param_count + j] = read_uint8(p);
  1656. }
  1657. for (j = 0; j < param_count + result_count; j++) {
  1658. if (!is_value_type(type->types[j])) {
  1659. set_error_buf(error_buf, error_buf_size,
  1660. "unknown value type");
  1661. return false;
  1662. }
  1663. }
  1664. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1665. ret_cell_num =
  1666. wasm_get_cell_num(type->types + param_count, result_count);
  1667. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1668. set_error_buf(error_buf, error_buf_size,
  1669. "param count or result count too large");
  1670. return false;
  1671. }
  1672. type->param_cell_num = (uint16)param_cell_num;
  1673. type->ret_cell_num = (uint16)ret_cell_num;
  1674. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1675. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1676. #endif
  1677. #if WASM_ENABLE_WAMR_COMPILER != 0
  1678. for (j = 0; j < type->param_count + type->result_count; j++) {
  1679. if (type->types[j] == VALUE_TYPE_V128)
  1680. module->is_simd_used = true;
  1681. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1682. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1683. module->is_ref_types_used = true;
  1684. }
  1685. #endif
  1686. /* If there is already a same type created, use it instead */
  1687. for (j = 0; j < i; j++) {
  1688. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1689. if (module->types[j]->ref_count == UINT16_MAX) {
  1690. set_error_buf(error_buf, error_buf_size,
  1691. "wasm type's ref count too large");
  1692. return false;
  1693. }
  1694. destroy_wasm_type(type);
  1695. module->types[i] = module->types[j];
  1696. module->types[j]->ref_count++;
  1697. break;
  1698. }
  1699. }
  1700. }
  1701. #else /* else of WASM_ENABLE_GC == 0 */
  1702. for (i = 0; i < type_count; i++) {
  1703. uint32 super_type_count = 0, parent_type_idx = (uint32)-1,
  1704. rec_count = 1, j;
  1705. bool is_sub_final = true;
  1706. CHECK_BUF(p, p_end, 1);
  1707. flag = read_uint8(p);
  1708. if (flag == DEFINED_TYPE_REC) {
  1709. read_leb_uint32(p, p_end, rec_count);
  1710. if (rec_count > 1) {
  1711. uint64 new_total_size;
  1712. module->type_count += rec_count - 1;
  1713. new_total_size =
  1714. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1715. MEM_REALLOC(module->types, total_size, new_total_size);
  1716. total_size = new_total_size;
  1717. }
  1718. LOG_VERBOSE("Processing rec group [%d-%d]",
  1719. processed_type_count,
  1720. processed_type_count + rec_count - 1);
  1721. }
  1722. else {
  1723. p--;
  1724. }
  1725. for (j = 0; j < rec_count; j++) {
  1726. WASMType *cur_type = NULL;
  1727. CHECK_BUF(p, p_end, 1);
  1728. flag = read_uint8(p);
  1729. parent_type_idx = -1;
  1730. if (flag == DEFINED_TYPE_SUB
  1731. || flag == DEFINED_TYPE_SUB_FINAL) {
  1732. read_leb_uint32(p, p_end, super_type_count);
  1733. if (super_type_count > 1) {
  1734. set_error_buf(error_buf, error_buf_size,
  1735. "super type count too large");
  1736. return false;
  1737. }
  1738. if (super_type_count > 0) {
  1739. read_leb_uint32(p, p_end, parent_type_idx);
  1740. if (parent_type_idx >= processed_type_count + j) {
  1741. set_error_buf_v(error_buf, error_buf_size,
  1742. "unknown type %d", parent_type_idx);
  1743. return false;
  1744. }
  1745. if (module->types[parent_type_idx]->is_sub_final) {
  1746. set_error_buf(error_buf, error_buf_size,
  1747. "sub type can not inherit from "
  1748. "a final super type");
  1749. return false;
  1750. }
  1751. }
  1752. if (flag == DEFINED_TYPE_SUB)
  1753. is_sub_final = false;
  1754. CHECK_BUF(p, p_end, 1);
  1755. flag = read_uint8(p);
  1756. }
  1757. if (flag == DEFINED_TYPE_FUNC) {
  1758. if (!resolve_func_type(&p, buf_end, module,
  1759. processed_type_count + j, error_buf,
  1760. error_buf_size)) {
  1761. return false;
  1762. }
  1763. }
  1764. else if (flag == DEFINED_TYPE_STRUCT) {
  1765. if (!resolve_struct_type(&p, buf_end, module,
  1766. processed_type_count + j,
  1767. error_buf, error_buf_size)) {
  1768. return false;
  1769. }
  1770. }
  1771. else if (flag == DEFINED_TYPE_ARRAY) {
  1772. if (!resolve_array_type(&p, buf_end, module,
  1773. processed_type_count + j, error_buf,
  1774. error_buf_size)) {
  1775. return false;
  1776. }
  1777. }
  1778. else {
  1779. set_error_buf(error_buf, error_buf_size,
  1780. "invalid type flag");
  1781. return false;
  1782. }
  1783. cur_type = module->types[processed_type_count + j];
  1784. cur_type->parent_type_idx = parent_type_idx;
  1785. cur_type->is_sub_final = is_sub_final;
  1786. if (rec_count > 1) {
  1787. cur_type->rec_count = rec_count;
  1788. cur_type->rec_idx = j;
  1789. }
  1790. }
  1791. /* resolve subtyping relationship in current rec group */
  1792. for (j = 0; j < rec_count; j++) {
  1793. WASMType *cur_type = module->types[processed_type_count + j];
  1794. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1795. WASMType *parent_type =
  1796. module->types[cur_type->parent_type_idx];
  1797. cur_type->parent_type = parent_type;
  1798. cur_type->root_type = parent_type->root_type;
  1799. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1800. }
  1801. else {
  1802. cur_type->parent_type = NULL;
  1803. cur_type->root_type = cur_type;
  1804. cur_type->inherit_depth = 0;
  1805. }
  1806. }
  1807. for (j = 0; j < rec_count; j++) {
  1808. WASMType *cur_type = module->types[processed_type_count + j];
  1809. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1810. WASMType *parent_type =
  1811. module->types[cur_type->parent_type_idx];
  1812. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1813. module->types,
  1814. module->type_count)) {
  1815. set_error_buf(error_buf, error_buf_size,
  1816. "sub type does not match super type");
  1817. return false;
  1818. }
  1819. }
  1820. }
  1821. if (rec_count > 1) {
  1822. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1823. processed_type_count,
  1824. processed_type_count + rec_count - 1);
  1825. }
  1826. processed_type_count += rec_count;
  1827. }
  1828. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1829. * module->type_count,
  1830. error_buf, error_buf_size))) {
  1831. return false;
  1832. }
  1833. #endif /* end of WASM_ENABLE_GC == 0 */
  1834. }
  1835. if (p != p_end) {
  1836. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1837. return false;
  1838. }
  1839. LOG_VERBOSE("Load type section success.\n");
  1840. return true;
  1841. fail:
  1842. return false;
  1843. }
  1844. static void
  1845. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1846. {
  1847. uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
  1848. ? init_size * 2
  1849. : WASM_TABLE_MAX_SIZE;
  1850. if (max_size_flag) {
  1851. /* module defines the table limitation */
  1852. bh_assert(init_size <= *max_size);
  1853. if (init_size < *max_size) {
  1854. *max_size =
  1855. *max_size < default_max_size ? *max_size : default_max_size;
  1856. }
  1857. }
  1858. else {
  1859. /* partial defined table limitation, gives a default value */
  1860. *max_size = default_max_size;
  1861. }
  1862. }
  1863. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1864. /**
  1865. * Find export item of a module with export info:
  1866. * module name, field name and export kind
  1867. */
  1868. static WASMExport *
  1869. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1870. const char *field_name, uint8 export_kind,
  1871. char *error_buf, uint32 error_buf_size)
  1872. {
  1873. WASMExport *export =
  1874. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  1875. export_kind, error_buf, error_buf_size);
  1876. return export;
  1877. }
  1878. #endif
  1879. #if WASM_ENABLE_MULTI_MODULE != 0
  1880. static WASMFunction *
  1881. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  1882. const WASMFuncType *expected_function_type,
  1883. char *error_buf, uint32 error_buf_size)
  1884. {
  1885. WASMModuleCommon *module_reg;
  1886. WASMFunction *function = NULL;
  1887. WASMExport *export = NULL;
  1888. WASMModule *module = NULL;
  1889. WASMFuncType *target_function_type = NULL;
  1890. module_reg = wasm_runtime_find_module_registered(module_name);
  1891. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1892. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  1893. function_name);
  1894. set_error_buf(error_buf, error_buf_size, "unknown import");
  1895. return NULL;
  1896. }
  1897. module = (WASMModule *)module_reg;
  1898. export =
  1899. wasm_loader_find_export(module, module_name, function_name,
  1900. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  1901. if (!export) {
  1902. return NULL;
  1903. }
  1904. /* resolve function type and function */
  1905. if (export->index < module->import_function_count) {
  1906. target_function_type =
  1907. module->import_functions[export->index].u.function.func_type;
  1908. function = module->import_functions[export->index]
  1909. .u.function.import_func_linked;
  1910. }
  1911. else {
  1912. target_function_type =
  1913. module->functions[export->index - module->import_function_count]
  1914. ->func_type;
  1915. function =
  1916. module->functions[export->index - module->import_function_count];
  1917. }
  1918. /* check function type */
  1919. if (!wasm_type_equal((WASMType *)expected_function_type,
  1920. (WASMType *)target_function_type, module->types,
  1921. module->type_count)) {
  1922. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  1923. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1924. return NULL;
  1925. }
  1926. return function;
  1927. }
  1928. static WASMTable *
  1929. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  1930. uint32 init_size, uint32 max_size, char *error_buf,
  1931. uint32 error_buf_size)
  1932. {
  1933. WASMModuleCommon *module_reg;
  1934. WASMTable *table = NULL;
  1935. WASMExport *export = NULL;
  1936. WASMModule *module = NULL;
  1937. module_reg = wasm_runtime_find_module_registered(module_name);
  1938. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1939. LOG_DEBUG("can not find a module named %s for table", module_name);
  1940. set_error_buf(error_buf, error_buf_size, "unknown import");
  1941. return NULL;
  1942. }
  1943. module = (WASMModule *)module_reg;
  1944. export =
  1945. wasm_loader_find_export(module, module_name, table_name,
  1946. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  1947. if (!export) {
  1948. return NULL;
  1949. }
  1950. /* resolve table and check the init/max size */
  1951. if (export->index < module->import_table_count) {
  1952. table =
  1953. module->import_tables[export->index].u.table.import_table_linked;
  1954. }
  1955. else {
  1956. table = &(module->tables[export->index - module->import_table_count]);
  1957. }
  1958. if (table->init_size < init_size || table->max_size > max_size) {
  1959. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  1960. module_name, table_name, table->init_size, table->max_size,
  1961. init_size, max_size);
  1962. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1963. return NULL;
  1964. }
  1965. return table;
  1966. }
  1967. static WASMMemory *
  1968. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  1969. uint32 init_page_count, uint32 max_page_count,
  1970. char *error_buf, uint32 error_buf_size)
  1971. {
  1972. WASMModuleCommon *module_reg;
  1973. WASMMemory *memory = NULL;
  1974. WASMExport *export = NULL;
  1975. WASMModule *module = NULL;
  1976. module_reg = wasm_runtime_find_module_registered(module_name);
  1977. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1978. LOG_DEBUG("can not find a module named %s for memory", module_name);
  1979. set_error_buf(error_buf, error_buf_size, "unknown import");
  1980. return NULL;
  1981. }
  1982. module = (WASMModule *)module_reg;
  1983. export =
  1984. wasm_loader_find_export(module, module_name, memory_name,
  1985. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  1986. if (!export) {
  1987. return NULL;
  1988. }
  1989. /* resolve memory and check the init/max page count */
  1990. if (export->index < module->import_memory_count) {
  1991. memory = module->import_memories[export->index]
  1992. .u.memory.import_memory_linked;
  1993. }
  1994. else {
  1995. memory =
  1996. &(module->memories[export->index - module->import_memory_count]);
  1997. }
  1998. if (memory->init_page_count < init_page_count
  1999. || memory->max_page_count > max_page_count) {
  2000. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2001. module_name, memory_name, memory->init_page_count,
  2002. memory->max_page_count, init_page_count, max_page_count);
  2003. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2004. return NULL;
  2005. }
  2006. return memory;
  2007. }
  2008. static WASMGlobal *
  2009. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2010. uint8 type, bool is_mutable, char *error_buf,
  2011. uint32 error_buf_size)
  2012. {
  2013. WASMModuleCommon *module_reg;
  2014. WASMGlobal *global = NULL;
  2015. WASMExport *export = NULL;
  2016. WASMModule *module = NULL;
  2017. module_reg = wasm_runtime_find_module_registered(module_name);
  2018. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2019. LOG_DEBUG("can not find a module named %s for global", module_name);
  2020. set_error_buf(error_buf, error_buf_size, "unknown import");
  2021. return NULL;
  2022. }
  2023. module = (WASMModule *)module_reg;
  2024. export =
  2025. wasm_loader_find_export(module, module_name, global_name,
  2026. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2027. if (!export) {
  2028. return NULL;
  2029. }
  2030. /* resolve and check the global */
  2031. if (export->index < module->import_global_count) {
  2032. global =
  2033. module->import_globals[export->index].u.global.import_global_linked;
  2034. }
  2035. else {
  2036. global =
  2037. &(module->globals[export->index - module->import_global_count]);
  2038. }
  2039. if (global->type != type || global->is_mutable != is_mutable) {
  2040. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2041. module_name, global_name, global->type, global->is_mutable,
  2042. type, is_mutable);
  2043. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2044. return NULL;
  2045. }
  2046. return global;
  2047. }
  2048. #if WASM_ENABLE_TAGS != 0
  2049. static WASMTag *
  2050. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2051. const WASMType *expected_tag_type,
  2052. uint32 *linked_tag_index, char *error_buf,
  2053. uint32 error_buf_size)
  2054. {
  2055. WASMModuleCommon *module_reg;
  2056. WASMTag *tag = NULL;
  2057. WASMExport *export = NULL;
  2058. WASMModule *module = NULL;
  2059. module_reg = wasm_runtime_find_module_registered(module_name);
  2060. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2061. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2062. tag_name);
  2063. set_error_buf(error_buf, error_buf_size, "unknown import");
  2064. return NULL;
  2065. }
  2066. module = (WASMModule *)module_reg;
  2067. export =
  2068. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2069. error_buf, error_buf_size);
  2070. if (!export) {
  2071. return NULL;
  2072. }
  2073. /* resolve tag type and tag */
  2074. if (export->index < module->import_tag_count) {
  2075. /* importing an imported tag from the submodule */
  2076. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2077. }
  2078. else {
  2079. /* importing an section tag from the submodule */
  2080. tag = module->tags[export->index - module->import_tag_count];
  2081. }
  2082. /* check function type */
  2083. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2084. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2085. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2086. return NULL;
  2087. }
  2088. if (linked_tag_index != NULL) {
  2089. *linked_tag_index = export->index;
  2090. }
  2091. return tag;
  2092. }
  2093. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2094. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2095. static bool
  2096. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2097. const WASMModule *parent_module,
  2098. const char *sub_module_name, const char *function_name,
  2099. WASMFunctionImport *function, char *error_buf,
  2100. uint32 error_buf_size)
  2101. {
  2102. const uint8 *p = *p_buf, *p_end = buf_end;
  2103. uint32 declare_type_index = 0;
  2104. WASMFuncType *declare_func_type = NULL;
  2105. WASMFunction *linked_func = NULL;
  2106. #if WASM_ENABLE_MULTI_MODULE != 0
  2107. WASMModule *sub_module = NULL;
  2108. #endif
  2109. const char *linked_signature = NULL;
  2110. void *linked_attachment = NULL;
  2111. bool linked_call_conv_raw = false;
  2112. bool is_native_symbol = false;
  2113. read_leb_uint32(p, p_end, declare_type_index);
  2114. *p_buf = p;
  2115. if (declare_type_index >= parent_module->type_count) {
  2116. set_error_buf(error_buf, error_buf_size, "unknown type");
  2117. return false;
  2118. }
  2119. #if WASM_ENABLE_GC != 0
  2120. function->type_idx = declare_type_index;
  2121. #endif
  2122. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2123. declare_type_index = wasm_get_smallest_type_idx(
  2124. parent_module->types, parent_module->type_count, declare_type_index);
  2125. #endif
  2126. declare_func_type =
  2127. (WASMFuncType *)parent_module->types[declare_type_index];
  2128. /* lookup registered native symbols first */
  2129. linked_func = wasm_native_resolve_symbol(
  2130. sub_module_name, function_name, declare_func_type, &linked_signature,
  2131. &linked_attachment, &linked_call_conv_raw);
  2132. if (linked_func) {
  2133. is_native_symbol = true;
  2134. }
  2135. #if WASM_ENABLE_MULTI_MODULE != 0
  2136. else {
  2137. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2138. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2139. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2140. error_buf_size);
  2141. if (!sub_module) {
  2142. return false;
  2143. }
  2144. }
  2145. linked_func = wasm_loader_resolve_function(
  2146. sub_module_name, function_name, declare_func_type, error_buf,
  2147. error_buf_size);
  2148. }
  2149. #endif
  2150. function->module_name = (char *)sub_module_name;
  2151. function->field_name = (char *)function_name;
  2152. function->func_type = declare_func_type;
  2153. /* func_ptr_linked is for native registered symbol */
  2154. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2155. function->signature = linked_signature;
  2156. function->attachment = linked_attachment;
  2157. function->call_conv_raw = linked_call_conv_raw;
  2158. #if WASM_ENABLE_MULTI_MODULE != 0
  2159. function->import_module = is_native_symbol ? NULL : sub_module;
  2160. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2161. #endif
  2162. return true;
  2163. fail:
  2164. return false;
  2165. }
  2166. static bool
  2167. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2168. uint32 error_buf_size)
  2169. {
  2170. if (max_size < init_size) {
  2171. set_error_buf(error_buf, error_buf_size,
  2172. "size minimum must not be greater than maximum");
  2173. return false;
  2174. }
  2175. return true;
  2176. }
  2177. static bool
  2178. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2179. WASMModule *parent_module, const char *sub_module_name,
  2180. const char *table_name, WASMTableImport *table,
  2181. char *error_buf, uint32 error_buf_size)
  2182. {
  2183. const uint8 *p = *p_buf, *p_end = buf_end;
  2184. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2185. declare_init_size = 0, declare_max_size = 0;
  2186. #if WASM_ENABLE_MULTI_MODULE != 0
  2187. WASMModule *sub_module = NULL;
  2188. WASMTable *linked_table = NULL;
  2189. #endif
  2190. #if WASM_ENABLE_GC != 0
  2191. WASMRefType ref_type;
  2192. bool need_ref_type_map;
  2193. #endif
  2194. #if WASM_ENABLE_GC == 0
  2195. CHECK_BUF(p, p_end, 1);
  2196. /* 0x70 or 0x6F */
  2197. declare_elem_type = read_uint8(p);
  2198. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2199. #if WASM_ENABLE_REF_TYPES != 0
  2200. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2201. #endif
  2202. ) {
  2203. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2204. return false;
  2205. }
  2206. #else /* else of WASM_ENABLE_GC == 0 */
  2207. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2208. &ref_type, false, error_buf, error_buf_size)) {
  2209. return false;
  2210. }
  2211. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2212. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2213. return false;
  2214. }
  2215. declare_elem_type = ref_type.ref_type;
  2216. if (need_ref_type_map) {
  2217. if (!(table->elem_ref_type =
  2218. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2219. error_buf, error_buf_size))) {
  2220. return false;
  2221. }
  2222. }
  2223. #if TRACE_WASM_LOADER != 0
  2224. os_printf("import table type: ");
  2225. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  2226. os_printf("\n");
  2227. #endif
  2228. #endif /* end of WASM_ENABLE_GC == 0 */
  2229. read_leb_uint32(p, p_end, declare_max_size_flag);
  2230. if (declare_max_size_flag > 1) {
  2231. set_error_buf(error_buf, error_buf_size, "integer too large");
  2232. return false;
  2233. }
  2234. read_leb_uint32(p, p_end, declare_init_size);
  2235. if (declare_max_size_flag) {
  2236. read_leb_uint32(p, p_end, declare_max_size);
  2237. if (!check_table_max_size(declare_init_size, declare_max_size,
  2238. error_buf, error_buf_size))
  2239. return false;
  2240. }
  2241. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2242. &declare_max_size);
  2243. *p_buf = p;
  2244. #if WASM_ENABLE_MULTI_MODULE != 0
  2245. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2246. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2247. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2248. error_buf_size);
  2249. if (!sub_module) {
  2250. return false;
  2251. }
  2252. linked_table = wasm_loader_resolve_table(
  2253. sub_module_name, table_name, declare_init_size, declare_max_size,
  2254. error_buf, error_buf_size);
  2255. if (!linked_table) {
  2256. return false;
  2257. }
  2258. /* reset with linked table limit */
  2259. declare_elem_type = linked_table->elem_type;
  2260. declare_init_size = linked_table->init_size;
  2261. declare_max_size = linked_table->max_size;
  2262. declare_max_size_flag = linked_table->flags;
  2263. table->import_table_linked = linked_table;
  2264. table->import_module = sub_module;
  2265. }
  2266. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2267. /* (table (export "table") 10 20 funcref) */
  2268. /* we need this section working in wamrc */
  2269. if (!strcmp("spectest", sub_module_name)) {
  2270. const uint32 spectest_table_init_size = 10;
  2271. const uint32 spectest_table_max_size = 20;
  2272. if (strcmp("table", table_name)) {
  2273. set_error_buf(error_buf, error_buf_size,
  2274. "incompatible import type or unknown import");
  2275. return false;
  2276. }
  2277. if (declare_init_size > spectest_table_init_size
  2278. || declare_max_size < spectest_table_max_size) {
  2279. set_error_buf(error_buf, error_buf_size,
  2280. "incompatible import type");
  2281. return false;
  2282. }
  2283. declare_init_size = spectest_table_init_size;
  2284. declare_max_size = spectest_table_max_size;
  2285. }
  2286. /* now we believe all declaration are ok */
  2287. table->elem_type = declare_elem_type;
  2288. table->init_size = declare_init_size;
  2289. table->flags = declare_max_size_flag;
  2290. table->max_size = declare_max_size;
  2291. #if WASM_ENABLE_WAMR_COMPILER != 0
  2292. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2293. parent_module->is_ref_types_used = true;
  2294. #endif
  2295. (void)parent_module;
  2296. return true;
  2297. fail:
  2298. return false;
  2299. }
  2300. static bool
  2301. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  2302. {
  2303. if (init_size > DEFAULT_MAX_PAGES) {
  2304. set_error_buf(error_buf, error_buf_size,
  2305. "memory size must be at most 65536 pages (4GiB)");
  2306. return false;
  2307. }
  2308. return true;
  2309. }
  2310. static bool
  2311. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2312. uint32 error_buf_size)
  2313. {
  2314. if (max_size < init_size) {
  2315. set_error_buf(error_buf, error_buf_size,
  2316. "size minimum must not be greater than maximum");
  2317. return false;
  2318. }
  2319. if (max_size > DEFAULT_MAX_PAGES) {
  2320. set_error_buf(error_buf, error_buf_size,
  2321. "memory size must be at most 65536 pages (4GiB)");
  2322. return false;
  2323. }
  2324. return true;
  2325. }
  2326. static bool
  2327. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2328. WASMModule *parent_module, const char *sub_module_name,
  2329. const char *memory_name, WASMMemoryImport *memory,
  2330. char *error_buf, uint32 error_buf_size)
  2331. {
  2332. const uint8 *p = *p_buf, *p_end = buf_end;
  2333. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2334. uint32 pool_size = wasm_runtime_memory_pool_size();
  2335. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2336. / DEFAULT_NUM_BYTES_PER_PAGE;
  2337. #else
  2338. uint32 max_page_count = DEFAULT_MAX_PAGES;
  2339. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2340. uint32 declare_max_page_count_flag = 0;
  2341. uint32 declare_init_page_count = 0;
  2342. uint32 declare_max_page_count = 0;
  2343. #if WASM_ENABLE_MULTI_MODULE != 0
  2344. WASMModule *sub_module = NULL;
  2345. WASMMemory *linked_memory = NULL;
  2346. #endif
  2347. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  2348. read_leb_uint32(p, p_end, declare_init_page_count);
  2349. if (!check_memory_init_size(declare_init_page_count, error_buf,
  2350. error_buf_size)) {
  2351. return false;
  2352. }
  2353. if (declare_max_page_count_flag & 1) {
  2354. read_leb_uint32(p, p_end, declare_max_page_count);
  2355. if (!check_memory_max_size(declare_init_page_count,
  2356. declare_max_page_count, error_buf,
  2357. error_buf_size)) {
  2358. return false;
  2359. }
  2360. if (declare_max_page_count > max_page_count) {
  2361. declare_max_page_count = max_page_count;
  2362. }
  2363. }
  2364. else {
  2365. /* Limit the maximum memory size to max_page_count */
  2366. declare_max_page_count = max_page_count;
  2367. }
  2368. #if WASM_ENABLE_MULTI_MODULE != 0
  2369. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2370. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2371. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2372. error_buf_size);
  2373. if (!sub_module) {
  2374. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  2375. /* Avoid memory import failure when wasi-threads is enabled
  2376. and the memory is shared */
  2377. if (!(declare_max_page_count_flag & 2))
  2378. return false;
  2379. #else
  2380. return false;
  2381. #endif /* WASM_ENABLE_LIB_WASI_THREADS */
  2382. }
  2383. else {
  2384. linked_memory = wasm_loader_resolve_memory(
  2385. sub_module_name, memory_name, declare_init_page_count,
  2386. declare_max_page_count, error_buf, error_buf_size);
  2387. if (!linked_memory) {
  2388. return false;
  2389. }
  2390. /**
  2391. * reset with linked memory limit
  2392. */
  2393. memory->import_module = sub_module;
  2394. memory->import_memory_linked = linked_memory;
  2395. declare_init_page_count = linked_memory->init_page_count;
  2396. declare_max_page_count = linked_memory->max_page_count;
  2397. }
  2398. }
  2399. #endif
  2400. /* (memory (export "memory") 1 2) */
  2401. if (!strcmp("spectest", sub_module_name)) {
  2402. uint32 spectest_memory_init_page = 1;
  2403. uint32 spectest_memory_max_page = 2;
  2404. if (strcmp("memory", memory_name)) {
  2405. set_error_buf(error_buf, error_buf_size,
  2406. "incompatible import type or unknown import");
  2407. return false;
  2408. }
  2409. if (declare_init_page_count > spectest_memory_init_page
  2410. || declare_max_page_count < spectest_memory_max_page) {
  2411. set_error_buf(error_buf, error_buf_size,
  2412. "incompatible import type");
  2413. return false;
  2414. }
  2415. declare_init_page_count = spectest_memory_init_page;
  2416. declare_max_page_count = spectest_memory_max_page;
  2417. }
  2418. /* now we believe all declaration are ok */
  2419. memory->flags = declare_max_page_count_flag;
  2420. memory->init_page_count = declare_init_page_count;
  2421. memory->max_page_count = declare_max_page_count;
  2422. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2423. *p_buf = p;
  2424. (void)parent_module;
  2425. return true;
  2426. fail:
  2427. return false;
  2428. }
  2429. #if WASM_ENABLE_TAGS != 0
  2430. static bool
  2431. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2432. const WASMModule *parent_module, /* this module ! */
  2433. const char *sub_module_name, const char *tag_name,
  2434. WASMTagImport *tag, /* structure to fill */
  2435. char *error_buf, uint32 error_buf_size)
  2436. {
  2437. /* attribute and type of the import statement */
  2438. uint8 declare_tag_attribute;
  2439. uint32 declare_type_index;
  2440. const uint8 *p = *p_buf, *p_end = buf_end;
  2441. #if WASM_ENABLE_MULTI_MODULE != 0
  2442. WASMModule *sub_module = NULL;
  2443. #endif
  2444. /* get the one byte attribute */
  2445. CHECK_BUF(p, p_end, 1);
  2446. declare_tag_attribute = read_uint8(p);
  2447. if (declare_tag_attribute != 0) {
  2448. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2449. goto fail;
  2450. }
  2451. /* get type */
  2452. read_leb_uint32(p, p_end, declare_type_index);
  2453. /* compare against module->types */
  2454. if (declare_type_index >= parent_module->type_count) {
  2455. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2456. goto fail;
  2457. }
  2458. WASMType *declare_tag_type = parent_module->types[declare_type_index];
  2459. /* check, that the type of the declared tag returns void */
  2460. if (declare_tag_type->result_count != 0) {
  2461. set_error_buf(error_buf, error_buf_size,
  2462. "tag type signature does not return void");
  2463. goto fail;
  2464. }
  2465. #if WASM_ENABLE_MULTI_MODULE != 0
  2466. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2467. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2468. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2469. error_buf_size);
  2470. if (!sub_module) {
  2471. return false;
  2472. }
  2473. /* wasm_loader_resolve_tag checks, that the imported tag
  2474. * and the declared tag have the same type
  2475. */
  2476. uint32 linked_tag_index = 0;
  2477. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2478. sub_module_name, tag_name, declare_tag_type,
  2479. &linked_tag_index /* out */, error_buf, error_buf_size);
  2480. if (linked_tag) {
  2481. tag->import_module = sub_module;
  2482. tag->import_tag_linked = linked_tag;
  2483. tag->import_tag_index_linked = linked_tag_index;
  2484. }
  2485. }
  2486. #endif
  2487. /* store to module tag declarations */
  2488. tag->attribute = declare_tag_attribute;
  2489. tag->type = declare_type_index;
  2490. tag->module_name = (char *)sub_module_name;
  2491. tag->field_name = (char *)tag_name;
  2492. tag->tag_type = declare_tag_type;
  2493. *p_buf = p;
  2494. (void)parent_module;
  2495. LOG_VERBOSE("Load tag import success\n");
  2496. return true;
  2497. fail:
  2498. return false;
  2499. }
  2500. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2501. static bool
  2502. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2503. WASMModule *parent_module, char *sub_module_name,
  2504. char *global_name, WASMGlobalImport *global, char *error_buf,
  2505. uint32 error_buf_size)
  2506. {
  2507. const uint8 *p = *p_buf, *p_end = buf_end;
  2508. uint8 declare_type = 0;
  2509. uint8 declare_mutable = 0;
  2510. #if WASM_ENABLE_MULTI_MODULE != 0
  2511. WASMModule *sub_module = NULL;
  2512. WASMGlobal *linked_global = NULL;
  2513. #endif
  2514. #if WASM_ENABLE_GC != 0
  2515. WASMRefType ref_type;
  2516. bool need_ref_type_map;
  2517. #endif
  2518. bool ret = false;
  2519. #if WASM_ENABLE_GC == 0
  2520. CHECK_BUF(p, p_end, 2);
  2521. declare_type = read_uint8(p);
  2522. declare_mutable = read_uint8(p);
  2523. #else
  2524. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2525. &ref_type, false, error_buf, error_buf_size)) {
  2526. return false;
  2527. }
  2528. declare_type = ref_type.ref_type;
  2529. if (need_ref_type_map) {
  2530. if (!(global->ref_type =
  2531. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2532. error_buf, error_buf_size))) {
  2533. return false;
  2534. }
  2535. }
  2536. #if TRACE_WASM_LOADER != 0
  2537. os_printf("import global type: ");
  2538. wasm_dump_value_type(declare_type, global->ref_type);
  2539. os_printf("\n");
  2540. #endif
  2541. CHECK_BUF(p, p_end, 1);
  2542. declare_mutable = read_uint8(p);
  2543. #endif /* end of WASM_ENABLE_GC == 0 */
  2544. *p_buf = p;
  2545. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2546. return false;
  2547. }
  2548. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2549. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2550. global);
  2551. if (ret) {
  2552. if (global->type != declare_type
  2553. || global->is_mutable != declare_mutable) {
  2554. set_error_buf(error_buf, error_buf_size,
  2555. "incompatible import type");
  2556. return false;
  2557. }
  2558. global->is_linked = true;
  2559. }
  2560. #endif
  2561. #if WASM_ENABLE_MULTI_MODULE != 0
  2562. if (!global->is_linked
  2563. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2564. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2565. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2566. error_buf_size);
  2567. if (!sub_module) {
  2568. return false;
  2569. }
  2570. /* check sub modules */
  2571. linked_global = wasm_loader_resolve_global(
  2572. sub_module_name, global_name, declare_type, declare_mutable,
  2573. error_buf, error_buf_size);
  2574. if (linked_global) {
  2575. global->import_module = sub_module;
  2576. global->import_global_linked = linked_global;
  2577. global->is_linked = true;
  2578. }
  2579. }
  2580. #endif
  2581. global->module_name = sub_module_name;
  2582. global->field_name = global_name;
  2583. global->type = declare_type;
  2584. global->is_mutable = (declare_mutable == 1);
  2585. #if WASM_ENABLE_WAMR_COMPILER != 0
  2586. if (global->type == VALUE_TYPE_V128)
  2587. parent_module->is_simd_used = true;
  2588. else if (global->type == VALUE_TYPE_EXTERNREF)
  2589. parent_module->is_ref_types_used = true;
  2590. #endif
  2591. (void)parent_module;
  2592. (void)ret;
  2593. return true;
  2594. fail:
  2595. return false;
  2596. }
  2597. static bool
  2598. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2599. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2600. {
  2601. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2602. #if WASM_ENABLE_GC != 0
  2603. WASMRefType ref_type;
  2604. bool need_ref_type_map;
  2605. #endif
  2606. #if WASM_ENABLE_GC == 0
  2607. CHECK_BUF(p, p_end, 1);
  2608. /* 0x70 or 0x6F */
  2609. table->elem_type = read_uint8(p);
  2610. if (VALUE_TYPE_FUNCREF != table->elem_type
  2611. #if WASM_ENABLE_REF_TYPES != 0
  2612. && VALUE_TYPE_EXTERNREF != table->elem_type
  2613. #endif
  2614. ) {
  2615. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2616. return false;
  2617. }
  2618. #else /* else of WASM_ENABLE_GC == 0 */
  2619. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  2620. false, error_buf, error_buf_size)) {
  2621. return false;
  2622. }
  2623. table->elem_type = ref_type.ref_type;
  2624. if (need_ref_type_map) {
  2625. if (!(table->elem_ref_type =
  2626. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2627. error_buf_size))) {
  2628. return false;
  2629. }
  2630. }
  2631. #if TRACE_WASM_LOADER != 0
  2632. os_printf("table type: ");
  2633. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2634. os_printf("\n");
  2635. #endif
  2636. #endif /* end of WASM_ENABLE_GC == 0 */
  2637. p_org = p;
  2638. read_leb_uint32(p, p_end, table->flags);
  2639. #if WASM_ENABLE_SHARED_MEMORY == 0
  2640. if (p - p_org > 1) {
  2641. set_error_buf(error_buf, error_buf_size,
  2642. "integer representation too long");
  2643. return false;
  2644. }
  2645. if (table->flags > 1) {
  2646. set_error_buf(error_buf, error_buf_size, "integer too large");
  2647. return false;
  2648. }
  2649. #else
  2650. if (p - p_org > 1) {
  2651. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2652. return false;
  2653. }
  2654. if (table->flags == 2) {
  2655. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2656. return false;
  2657. }
  2658. if (table->flags > 1) {
  2659. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2660. return false;
  2661. }
  2662. #endif
  2663. read_leb_uint32(p, p_end, table->init_size);
  2664. if (table->flags) {
  2665. read_leb_uint32(p, p_end, table->max_size);
  2666. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2667. error_buf_size))
  2668. return false;
  2669. }
  2670. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2671. #if WASM_ENABLE_WAMR_COMPILER != 0
  2672. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2673. module->is_ref_types_used = true;
  2674. #endif
  2675. *p_buf = p;
  2676. return true;
  2677. fail:
  2678. return false;
  2679. }
  2680. static bool
  2681. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2682. char *error_buf, uint32 error_buf_size)
  2683. {
  2684. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2685. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2686. uint32 pool_size = wasm_runtime_memory_pool_size();
  2687. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2688. / DEFAULT_NUM_BYTES_PER_PAGE;
  2689. #else
  2690. uint32 max_page_count = DEFAULT_MAX_PAGES;
  2691. #endif
  2692. p_org = p;
  2693. read_leb_uint32(p, p_end, memory->flags);
  2694. #if WASM_ENABLE_SHARED_MEMORY == 0
  2695. if (p - p_org > 1) {
  2696. set_error_buf(error_buf, error_buf_size,
  2697. "integer representation too long");
  2698. return false;
  2699. }
  2700. if (memory->flags > 1) {
  2701. if (memory->flags & 2) {
  2702. set_error_buf(error_buf, error_buf_size,
  2703. "shared memory flag was found, "
  2704. "please enable shared memory, lib-pthread "
  2705. "or lib-wasi-threads");
  2706. }
  2707. else {
  2708. set_error_buf(error_buf, error_buf_size, "invalid memory flags");
  2709. }
  2710. return false;
  2711. }
  2712. #else
  2713. if (p - p_org > 1) {
  2714. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2715. return false;
  2716. }
  2717. if (memory->flags > 3) {
  2718. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2719. return false;
  2720. }
  2721. else if (memory->flags == 2) {
  2722. set_error_buf(error_buf, error_buf_size,
  2723. "shared memory must have maximum");
  2724. return false;
  2725. }
  2726. #endif
  2727. read_leb_uint32(p, p_end, memory->init_page_count);
  2728. if (!check_memory_init_size(memory->init_page_count, error_buf,
  2729. error_buf_size))
  2730. return false;
  2731. if (memory->flags & 1) {
  2732. read_leb_uint32(p, p_end, memory->max_page_count);
  2733. if (!check_memory_max_size(memory->init_page_count,
  2734. memory->max_page_count, error_buf,
  2735. error_buf_size))
  2736. return false;
  2737. if (memory->max_page_count > max_page_count)
  2738. memory->max_page_count = max_page_count;
  2739. }
  2740. else {
  2741. /* Limit the maximum memory size to max_page_count */
  2742. memory->max_page_count = max_page_count;
  2743. }
  2744. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2745. *p_buf = p;
  2746. return true;
  2747. fail:
  2748. return false;
  2749. }
  2750. static bool
  2751. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2752. bool is_load_from_file_buf, char *error_buf,
  2753. uint32 error_buf_size)
  2754. {
  2755. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2756. uint32 import_count, name_len, type_index, i, u32, flags;
  2757. uint64 total_size;
  2758. WASMImport *import;
  2759. WASMImport *import_functions = NULL, *import_tables = NULL;
  2760. WASMImport *import_memories = NULL, *import_globals = NULL;
  2761. #if WASM_ENABLE_TAGS != 0
  2762. WASMImport *import_tags = NULL;
  2763. #endif
  2764. char *sub_module_name, *field_name;
  2765. uint8 u8, kind, global_type;
  2766. read_leb_uint32(p, p_end, import_count);
  2767. if (import_count) {
  2768. module->import_count = import_count;
  2769. total_size = sizeof(WASMImport) * (uint64)import_count;
  2770. if (!(module->imports =
  2771. loader_malloc(total_size, error_buf, error_buf_size))) {
  2772. return false;
  2773. }
  2774. p_old = p;
  2775. /* Scan firstly to get import count of each type */
  2776. for (i = 0; i < import_count; i++) {
  2777. /* module name */
  2778. read_leb_uint32(p, p_end, name_len);
  2779. CHECK_BUF(p, p_end, name_len);
  2780. p += name_len;
  2781. /* field name */
  2782. read_leb_uint32(p, p_end, name_len);
  2783. CHECK_BUF(p, p_end, name_len);
  2784. p += name_len;
  2785. CHECK_BUF(p, p_end, 1);
  2786. /* 0x00/0x01/0x02/0x03/0x04 */
  2787. kind = read_uint8(p);
  2788. switch (kind) {
  2789. case IMPORT_KIND_FUNC: /* import function */
  2790. read_leb_uint32(p, p_end, type_index);
  2791. module->import_function_count++;
  2792. break;
  2793. case IMPORT_KIND_TABLE: /* import table */
  2794. CHECK_BUF(p, p_end, 1);
  2795. /* 0x70 */
  2796. u8 = read_uint8(p);
  2797. read_leb_uint32(p, p_end, flags);
  2798. read_leb_uint32(p, p_end, u32);
  2799. if (flags & 1)
  2800. read_leb_uint32(p, p_end, u32);
  2801. module->import_table_count++;
  2802. if (module->import_table_count > 1) {
  2803. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2804. set_error_buf(error_buf, error_buf_size,
  2805. "multiple tables");
  2806. return false;
  2807. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2808. module->is_ref_types_used = true;
  2809. #endif
  2810. }
  2811. break;
  2812. case IMPORT_KIND_MEMORY: /* import memory */
  2813. read_leb_uint32(p, p_end, flags);
  2814. read_leb_uint32(p, p_end, u32);
  2815. if (flags & 1)
  2816. read_leb_uint32(p, p_end, u32);
  2817. module->import_memory_count++;
  2818. if (module->import_memory_count > 1) {
  2819. set_error_buf(error_buf, error_buf_size,
  2820. "multiple memories");
  2821. return false;
  2822. }
  2823. break;
  2824. #if WASM_ENABLE_TAGS != 0
  2825. case IMPORT_KIND_TAG: /* import tags */
  2826. /* it only counts the number of tags to import */
  2827. module->import_tag_count++;
  2828. CHECK_BUF(p, p_end, 1);
  2829. u8 = read_uint8(p);
  2830. read_leb_uint32(p, p_end, type_index);
  2831. break;
  2832. #endif
  2833. case IMPORT_KIND_GLOBAL: /* import global */
  2834. #if WASM_ENABLE_GC != 0
  2835. /* valtype */
  2836. CHECK_BUF(p, p_end, 1);
  2837. global_type = read_uint8(p);
  2838. if (wasm_is_type_multi_byte_type(global_type)) {
  2839. int32 heap_type;
  2840. read_leb_int32(p, p_end, heap_type);
  2841. (void)heap_type;
  2842. }
  2843. /* mutability */
  2844. CHECK_BUF(p, p_end, 1);
  2845. p += 1;
  2846. #else
  2847. CHECK_BUF(p, p_end, 2);
  2848. p += 2;
  2849. #endif
  2850. (void)global_type;
  2851. module->import_global_count++;
  2852. break;
  2853. default:
  2854. set_error_buf(error_buf, error_buf_size,
  2855. "invalid import kind");
  2856. return false;
  2857. }
  2858. }
  2859. if (module->import_function_count)
  2860. import_functions = module->import_functions = module->imports;
  2861. if (module->import_table_count)
  2862. import_tables = module->import_tables =
  2863. module->imports + module->import_function_count;
  2864. if (module->import_memory_count)
  2865. import_memories = module->import_memories =
  2866. module->imports + module->import_function_count
  2867. + module->import_table_count;
  2868. #if WASM_ENABLE_TAGS != 0
  2869. if (module->import_tag_count)
  2870. import_tags = module->import_tags =
  2871. module->imports + module->import_function_count
  2872. + module->import_table_count + module->import_memory_count;
  2873. if (module->import_global_count)
  2874. import_globals = module->import_globals =
  2875. module->imports + module->import_function_count
  2876. + module->import_table_count + module->import_memory_count
  2877. + module->import_tag_count;
  2878. #else
  2879. if (module->import_global_count)
  2880. import_globals = module->import_globals =
  2881. module->imports + module->import_function_count
  2882. + module->import_table_count + module->import_memory_count;
  2883. #endif
  2884. p = p_old;
  2885. /* Scan again to resolve the data */
  2886. for (i = 0; i < import_count; i++) {
  2887. /* load module name */
  2888. read_leb_uint32(p, p_end, name_len);
  2889. CHECK_BUF(p, p_end, name_len);
  2890. if (!(sub_module_name = wasm_const_str_list_insert(
  2891. p, name_len, module, is_load_from_file_buf, error_buf,
  2892. error_buf_size))) {
  2893. return false;
  2894. }
  2895. p += name_len;
  2896. /* load field name */
  2897. read_leb_uint32(p, p_end, name_len);
  2898. CHECK_BUF(p, p_end, name_len);
  2899. if (!(field_name = wasm_const_str_list_insert(
  2900. p, name_len, module, is_load_from_file_buf, error_buf,
  2901. error_buf_size))) {
  2902. return false;
  2903. }
  2904. p += name_len;
  2905. CHECK_BUF(p, p_end, 1);
  2906. /* 0x00/0x01/0x02/0x03/0x4 */
  2907. kind = read_uint8(p);
  2908. switch (kind) {
  2909. case IMPORT_KIND_FUNC: /* import function */
  2910. bh_assert(import_functions);
  2911. import = import_functions++;
  2912. if (!load_function_import(
  2913. &p, p_end, module, sub_module_name, field_name,
  2914. &import->u.function, error_buf, error_buf_size)) {
  2915. return false;
  2916. }
  2917. break;
  2918. case IMPORT_KIND_TABLE: /* import table */
  2919. bh_assert(import_tables);
  2920. import = import_tables++;
  2921. if (!load_table_import(&p, p_end, module, sub_module_name,
  2922. field_name, &import->u.table,
  2923. error_buf, error_buf_size)) {
  2924. LOG_DEBUG("can not import such a table (%s,%s)",
  2925. sub_module_name, field_name);
  2926. return false;
  2927. }
  2928. break;
  2929. case IMPORT_KIND_MEMORY: /* import memory */
  2930. bh_assert(import_memories);
  2931. import = import_memories++;
  2932. if (!load_memory_import(&p, p_end, module, sub_module_name,
  2933. field_name, &import->u.memory,
  2934. error_buf, error_buf_size)) {
  2935. return false;
  2936. }
  2937. break;
  2938. #if WASM_ENABLE_TAGS != 0
  2939. case IMPORT_KIND_TAG:
  2940. bh_assert(import_tags);
  2941. import = import_tags++;
  2942. if (!load_tag_import(&p, p_end, module, sub_module_name,
  2943. field_name, &import->u.tag, error_buf,
  2944. error_buf_size)) {
  2945. return false;
  2946. }
  2947. break;
  2948. #endif
  2949. case IMPORT_KIND_GLOBAL: /* import global */
  2950. bh_assert(import_globals);
  2951. import = import_globals++;
  2952. if (!load_global_import(&p, p_end, module, sub_module_name,
  2953. field_name, &import->u.global,
  2954. error_buf, error_buf_size)) {
  2955. return false;
  2956. }
  2957. break;
  2958. default:
  2959. set_error_buf(error_buf, error_buf_size,
  2960. "invalid import kind");
  2961. return false;
  2962. }
  2963. import->kind = kind;
  2964. import->u.names.module_name = sub_module_name;
  2965. import->u.names.field_name = field_name;
  2966. }
  2967. #if WASM_ENABLE_LIBC_WASI != 0
  2968. import = module->import_functions;
  2969. for (i = 0; i < module->import_function_count; i++, import++) {
  2970. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  2971. || !strcmp(import->u.names.module_name,
  2972. "wasi_snapshot_preview1")) {
  2973. module->import_wasi_api = true;
  2974. break;
  2975. }
  2976. }
  2977. #endif
  2978. }
  2979. if (p != p_end) {
  2980. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2981. return false;
  2982. }
  2983. LOG_VERBOSE("Load import section success.\n");
  2984. (void)u8;
  2985. (void)u32;
  2986. (void)type_index;
  2987. return true;
  2988. fail:
  2989. return false;
  2990. }
  2991. static bool
  2992. init_function_local_offsets(WASMFunction *func, char *error_buf,
  2993. uint32 error_buf_size)
  2994. {
  2995. WASMFuncType *param_type = func->func_type;
  2996. uint32 param_count = param_type->param_count;
  2997. uint8 *param_types = param_type->types;
  2998. uint32 local_count = func->local_count;
  2999. uint8 *local_types = func->local_types;
  3000. uint32 i, local_offset = 0;
  3001. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3002. /*
  3003. * Only allocate memory when total_size is not 0,
  3004. * or the return value of malloc(0) might be NULL on some platforms,
  3005. * which causes wasm loader return false.
  3006. */
  3007. if (total_size > 0
  3008. && !(func->local_offsets =
  3009. loader_malloc(total_size, error_buf, error_buf_size))) {
  3010. return false;
  3011. }
  3012. for (i = 0; i < param_count; i++) {
  3013. func->local_offsets[i] = (uint16)local_offset;
  3014. local_offset += wasm_value_type_cell_num(param_types[i]);
  3015. }
  3016. for (i = 0; i < local_count; i++) {
  3017. func->local_offsets[param_count + i] = (uint16)local_offset;
  3018. local_offset += wasm_value_type_cell_num(local_types[i]);
  3019. }
  3020. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3021. return true;
  3022. }
  3023. static bool
  3024. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3025. const uint8 *buf_code, const uint8 *buf_code_end,
  3026. WASMModule *module, char *error_buf,
  3027. uint32 error_buf_size)
  3028. {
  3029. const uint8 *p = buf, *p_end = buf_end;
  3030. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3031. uint32 func_count;
  3032. uint64 total_size;
  3033. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3034. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3035. uint8 type;
  3036. WASMFunction *func;
  3037. #if WASM_ENABLE_GC != 0
  3038. bool need_ref_type_map;
  3039. WASMRefType ref_type;
  3040. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3041. #endif
  3042. read_leb_uint32(p, p_end, func_count);
  3043. if (buf_code)
  3044. read_leb_uint32(p_code, buf_code_end, code_count);
  3045. if (func_count != code_count) {
  3046. set_error_buf(error_buf, error_buf_size,
  3047. "function and code section have inconsistent lengths or "
  3048. "unexpected end");
  3049. return false;
  3050. }
  3051. if (func_count) {
  3052. module->function_count = func_count;
  3053. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3054. if (!(module->functions =
  3055. loader_malloc(total_size, error_buf, error_buf_size))) {
  3056. return false;
  3057. }
  3058. for (i = 0; i < func_count; i++) {
  3059. /* Resolve function type */
  3060. read_leb_uint32(p, p_end, type_index);
  3061. if (type_index >= module->type_count) {
  3062. set_error_buf(error_buf, error_buf_size, "unknown type");
  3063. return false;
  3064. }
  3065. #if WASM_ENABLE_GC != 0
  3066. type_index_org = type_index;
  3067. #endif
  3068. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3069. type_index = wasm_get_smallest_type_idx(
  3070. module->types, module->type_count, type_index);
  3071. #endif
  3072. read_leb_uint32(p_code, buf_code_end, code_size);
  3073. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3074. set_error_buf(error_buf, error_buf_size,
  3075. "invalid function code size");
  3076. return false;
  3077. }
  3078. /* Resolve local set count */
  3079. p_code_end = p_code + code_size;
  3080. local_count = 0;
  3081. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3082. p_code_save = p_code;
  3083. #if WASM_ENABLE_GC != 0
  3084. ref_type_map_count = 0;
  3085. #endif
  3086. /* Calculate total local count */
  3087. for (j = 0; j < local_set_count; j++) {
  3088. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3089. if (sub_local_count > UINT32_MAX - local_count) {
  3090. set_error_buf(error_buf, error_buf_size, "too many locals");
  3091. return false;
  3092. }
  3093. #if WASM_ENABLE_GC == 0
  3094. CHECK_BUF(p_code, buf_code_end, 1);
  3095. /* 0x7F/0x7E/0x7D/0x7C */
  3096. type = read_uint8(p_code);
  3097. local_count += sub_local_count;
  3098. #if WASM_ENABLE_WAMR_COMPILER != 0
  3099. /* If any value's type is v128, mark the module as SIMD used */
  3100. if (type == VALUE_TYPE_V128)
  3101. module->is_simd_used = true;
  3102. #endif
  3103. #else
  3104. if (!resolve_value_type(&p_code, buf_code_end, module,
  3105. &need_ref_type_map, &ref_type, false,
  3106. error_buf, error_buf_size)) {
  3107. return false;
  3108. }
  3109. local_count += sub_local_count;
  3110. if (need_ref_type_map)
  3111. ref_type_map_count += sub_local_count;
  3112. #endif
  3113. }
  3114. /* Alloc memory, layout: function structure + local types */
  3115. code_size = (uint32)(p_code_end - p_code);
  3116. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3117. if (!(func = module->functions[i] =
  3118. loader_malloc(total_size, error_buf, error_buf_size))) {
  3119. return false;
  3120. }
  3121. #if WASM_ENABLE_GC != 0
  3122. if (ref_type_map_count > 0) {
  3123. total_size =
  3124. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3125. if (!(func->local_ref_type_maps = loader_malloc(
  3126. total_size, error_buf, error_buf_size))) {
  3127. return false;
  3128. }
  3129. func->local_ref_type_map_count = ref_type_map_count;
  3130. }
  3131. #endif
  3132. /* Set function type, local count, code size and code body */
  3133. func->func_type = (WASMFuncType *)module->types[type_index];
  3134. func->local_count = local_count;
  3135. if (local_count > 0)
  3136. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3137. func->code_size = code_size;
  3138. /*
  3139. * we shall make a copy of code body [p_code, p_code + code_size]
  3140. * when we are worrying about inappropriate releasing behaviour.
  3141. * all code bodies are actually in a buffer which user allocates in
  3142. * his embedding environment and we don't have power on them.
  3143. * it will be like:
  3144. * code_body_cp = malloc(code_size);
  3145. * memcpy(code_body_cp, p_code, code_size);
  3146. * func->code = code_body_cp;
  3147. */
  3148. func->code = (uint8 *)p_code;
  3149. #if WASM_ENABLE_GC != 0
  3150. func->type_idx = type_index_org;
  3151. #endif
  3152. #if WASM_ENABLE_GC != 0
  3153. t = 0;
  3154. #endif
  3155. /* Load each local type */
  3156. p_code = p_code_save;
  3157. local_type_index = 0;
  3158. for (j = 0; j < local_set_count; j++) {
  3159. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3160. /* Note: sub_local_count is allowed to be 0 */
  3161. if (local_type_index > UINT32_MAX - sub_local_count
  3162. || local_type_index + sub_local_count > local_count) {
  3163. set_error_buf(error_buf, error_buf_size,
  3164. "invalid local count");
  3165. return false;
  3166. }
  3167. #if WASM_ENABLE_GC == 0
  3168. CHECK_BUF(p_code, buf_code_end, 1);
  3169. /* 0x7F/0x7E/0x7D/0x7C */
  3170. type = read_uint8(p_code);
  3171. if (!is_value_type(type)) {
  3172. if (type == VALUE_TYPE_V128)
  3173. set_error_buf(error_buf, error_buf_size,
  3174. "v128 value type requires simd feature");
  3175. else if (type == VALUE_TYPE_FUNCREF
  3176. || type == VALUE_TYPE_EXTERNREF)
  3177. set_error_buf(error_buf, error_buf_size,
  3178. "ref value type requires "
  3179. "reference types feature");
  3180. else
  3181. set_error_buf_v(error_buf, error_buf_size,
  3182. "invalid local type 0x%02X", type);
  3183. return false;
  3184. }
  3185. #else
  3186. if (!resolve_value_type(&p_code, buf_code_end, module,
  3187. &need_ref_type_map, &ref_type, false,
  3188. error_buf, error_buf_size)) {
  3189. return false;
  3190. }
  3191. if (need_ref_type_map) {
  3192. WASMRefType *ref_type_tmp;
  3193. if (!(ref_type_tmp = reftype_set_insert(
  3194. module->ref_type_set, &ref_type, error_buf,
  3195. error_buf_size))) {
  3196. return false;
  3197. }
  3198. for (k = 0; k < sub_local_count; k++) {
  3199. func->local_ref_type_maps[t + k].ref_type =
  3200. ref_type_tmp;
  3201. func->local_ref_type_maps[t + k].index =
  3202. local_type_index + k;
  3203. }
  3204. t += sub_local_count;
  3205. }
  3206. type = ref_type.ref_type;
  3207. #endif
  3208. for (k = 0; k < sub_local_count; k++) {
  3209. func->local_types[local_type_index++] = type;
  3210. }
  3211. #if WASM_ENABLE_WAMR_COMPILER != 0
  3212. if (type == VALUE_TYPE_V128)
  3213. module->is_simd_used = true;
  3214. else if (type == VALUE_TYPE_FUNCREF
  3215. || type == VALUE_TYPE_EXTERNREF)
  3216. module->is_ref_types_used = true;
  3217. #endif
  3218. }
  3219. bh_assert(local_type_index == func->local_count);
  3220. #if WASM_ENABLE_GC != 0
  3221. bh_assert(t == func->local_ref_type_map_count);
  3222. #if TRACE_WASM_LOADER != 0
  3223. os_printf("func %u, local types: [", i);
  3224. k = 0;
  3225. for (j = 0; j < func->local_count; j++) {
  3226. WASMRefType *ref_type_tmp = NULL;
  3227. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3228. bh_assert(j == func->local_ref_type_maps[k].index);
  3229. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3230. }
  3231. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3232. if (j < func->local_count - 1)
  3233. os_printf(" ");
  3234. }
  3235. os_printf("]\n");
  3236. #endif
  3237. #endif
  3238. func->param_cell_num = func->func_type->param_cell_num;
  3239. func->ret_cell_num = func->func_type->ret_cell_num;
  3240. local_cell_num =
  3241. wasm_get_cell_num(func->local_types, func->local_count);
  3242. if (local_cell_num > UINT16_MAX) {
  3243. set_error_buf(error_buf, error_buf_size,
  3244. "local count too large");
  3245. return false;
  3246. }
  3247. func->local_cell_num = (uint16)local_cell_num;
  3248. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3249. return false;
  3250. p_code = p_code_end;
  3251. }
  3252. }
  3253. if (p != p_end) {
  3254. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3255. return false;
  3256. }
  3257. LOG_VERBOSE("Load function section success.\n");
  3258. return true;
  3259. fail:
  3260. return false;
  3261. }
  3262. static bool
  3263. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3264. char *error_buf, uint32 error_buf_size)
  3265. {
  3266. const uint8 *p = buf, *p_end = buf_end;
  3267. uint32 table_count, i;
  3268. uint64 total_size;
  3269. WASMTable *table;
  3270. read_leb_uint32(p, p_end, table_count);
  3271. if (module->import_table_count + table_count > 1) {
  3272. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3273. /* a total of one table is allowed */
  3274. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3275. return false;
  3276. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3277. module->is_ref_types_used = true;
  3278. #endif
  3279. }
  3280. if (table_count) {
  3281. module->table_count = table_count;
  3282. total_size = sizeof(WASMTable) * (uint64)table_count;
  3283. if (!(module->tables =
  3284. loader_malloc(total_size, error_buf, error_buf_size))) {
  3285. return false;
  3286. }
  3287. /* load each table */
  3288. table = module->tables;
  3289. for (i = 0; i < table_count; i++, table++) {
  3290. #if WASM_ENABLE_GC != 0
  3291. uint8 flag;
  3292. bool has_init = false;
  3293. CHECK_BUF(buf, buf_end, 1);
  3294. flag = read_uint8(p);
  3295. if (flag == TABLE_INIT_EXPR_FLAG) {
  3296. CHECK_BUF(buf, buf_end, 1);
  3297. flag = read_uint8(p);
  3298. if (flag != 0x00) {
  3299. set_error_buf(error_buf, error_buf_size,
  3300. "invalid leading bytes for table");
  3301. return false;
  3302. }
  3303. has_init = true;
  3304. }
  3305. else {
  3306. p--;
  3307. }
  3308. #endif /* end of WASM_ENABLE_GC != 0 */
  3309. if (!load_table(&p, p_end, module, table, error_buf,
  3310. error_buf_size))
  3311. return false;
  3312. #if WASM_ENABLE_GC != 0
  3313. if (has_init) {
  3314. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3315. table->elem_type, table->elem_ref_type,
  3316. error_buf, error_buf_size))
  3317. return false;
  3318. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3319. && table->init_expr.init_expr_type
  3320. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3321. set_error_buf(
  3322. error_buf, error_buf_size,
  3323. "unsupported initializer expression for table");
  3324. return false;
  3325. }
  3326. }
  3327. else {
  3328. if (wasm_is_reftype_htref_non_nullable(table->elem_type)) {
  3329. set_error_buf(
  3330. error_buf, error_buf_size,
  3331. "type mismatch: non-nullable table without init expr");
  3332. return false;
  3333. }
  3334. }
  3335. #endif /* end of WASM_ENABLE_GC != 0 */
  3336. #if WASM_ENABLE_WAMR_COMPILER != 0
  3337. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  3338. module->is_ref_types_used = true;
  3339. #endif
  3340. }
  3341. }
  3342. if (p != p_end) {
  3343. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3344. return false;
  3345. }
  3346. LOG_VERBOSE("Load table section success.\n");
  3347. return true;
  3348. fail:
  3349. return false;
  3350. }
  3351. static bool
  3352. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3353. char *error_buf, uint32 error_buf_size)
  3354. {
  3355. const uint8 *p = buf, *p_end = buf_end;
  3356. uint32 memory_count, i;
  3357. uint64 total_size;
  3358. WASMMemory *memory;
  3359. read_leb_uint32(p, p_end, memory_count);
  3360. /* a total of one memory is allowed */
  3361. if (module->import_memory_count + memory_count > 1) {
  3362. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3363. return false;
  3364. }
  3365. if (memory_count) {
  3366. module->memory_count = memory_count;
  3367. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3368. if (!(module->memories =
  3369. loader_malloc(total_size, error_buf, error_buf_size))) {
  3370. return false;
  3371. }
  3372. /* load each memory */
  3373. memory = module->memories;
  3374. for (i = 0; i < memory_count; i++, memory++)
  3375. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3376. return false;
  3377. }
  3378. if (p != p_end) {
  3379. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3380. return false;
  3381. }
  3382. LOG_VERBOSE("Load memory section success.\n");
  3383. return true;
  3384. fail:
  3385. return false;
  3386. }
  3387. static bool
  3388. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3389. char *error_buf, uint32 error_buf_size)
  3390. {
  3391. const uint8 *p = buf, *p_end = buf_end;
  3392. uint32 global_count, i;
  3393. uint64 total_size;
  3394. WASMGlobal *global;
  3395. uint8 mutable;
  3396. #if WASM_ENABLE_GC != 0
  3397. bool need_ref_type_map;
  3398. WASMRefType ref_type;
  3399. #endif
  3400. read_leb_uint32(p, p_end, global_count);
  3401. module->global_count = 0;
  3402. if (global_count) {
  3403. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3404. if (!(module->globals =
  3405. loader_malloc(total_size, error_buf, error_buf_size))) {
  3406. return false;
  3407. }
  3408. global = module->globals;
  3409. for (i = 0; i < global_count; i++, global++) {
  3410. #if WASM_ENABLE_GC == 0
  3411. CHECK_BUF(p, p_end, 2);
  3412. global->type = read_uint8(p);
  3413. mutable = read_uint8(p);
  3414. #else
  3415. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  3416. &ref_type, false, error_buf,
  3417. error_buf_size)) {
  3418. return false;
  3419. }
  3420. global->type = ref_type.ref_type;
  3421. CHECK_BUF(p, p_end, 1);
  3422. mutable = read_uint8(p);
  3423. #endif /* end of WASM_ENABLE_GC */
  3424. #if WASM_ENABLE_WAMR_COMPILER != 0
  3425. if (global->type == VALUE_TYPE_V128)
  3426. module->is_simd_used = true;
  3427. else if (global->type == VALUE_TYPE_FUNCREF
  3428. || global->type == VALUE_TYPE_EXTERNREF)
  3429. module->is_ref_types_used = true;
  3430. #endif
  3431. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3432. return false;
  3433. }
  3434. global->is_mutable = mutable ? true : false;
  3435. /* initialize expression */
  3436. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3437. global->type,
  3438. #if WASM_ENABLE_GC == 0
  3439. NULL,
  3440. #else
  3441. &ref_type,
  3442. #endif
  3443. error_buf, error_buf_size))
  3444. return false;
  3445. #if WASM_ENABLE_GC != 0
  3446. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3447. uint8 global_type;
  3448. WASMRefType *global_ref_type;
  3449. uint32 global_idx = global->init_expr.u.global_index;
  3450. if (global->init_expr.u.global_index
  3451. >= module->import_global_count + i) {
  3452. set_error_buf(error_buf, error_buf_size, "unknown global");
  3453. return false;
  3454. }
  3455. if (global_idx < module->import_global_count) {
  3456. global_type =
  3457. module->import_globals[global_idx].u.global.type;
  3458. global_ref_type =
  3459. module->import_globals[global_idx].u.global.ref_type;
  3460. }
  3461. else {
  3462. global_type =
  3463. module
  3464. ->globals[global_idx - module->import_global_count]
  3465. .type;
  3466. global_ref_type =
  3467. module
  3468. ->globals[global_idx - module->import_global_count]
  3469. .ref_type;
  3470. }
  3471. if (!wasm_reftype_is_subtype_of(
  3472. global_type, global_ref_type, global->type,
  3473. global->ref_type, module->types, module->type_count)) {
  3474. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3475. return false;
  3476. }
  3477. }
  3478. if (need_ref_type_map) {
  3479. if (!(global->ref_type =
  3480. reftype_set_insert(module->ref_type_set, &ref_type,
  3481. error_buf, error_buf_size))) {
  3482. return false;
  3483. }
  3484. }
  3485. #if TRACE_WASM_LOADER != 0
  3486. os_printf("global type: ");
  3487. wasm_dump_value_type(global->type, global->ref_type);
  3488. os_printf("\n");
  3489. #endif
  3490. #endif
  3491. module->global_count++;
  3492. }
  3493. bh_assert(module->global_count == global_count);
  3494. }
  3495. if (p != p_end) {
  3496. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3497. return false;
  3498. }
  3499. LOG_VERBOSE("Load global section success.\n");
  3500. return true;
  3501. fail:
  3502. return false;
  3503. }
  3504. static bool
  3505. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3506. bool is_load_from_file_buf, char *error_buf,
  3507. uint32 error_buf_size)
  3508. {
  3509. const uint8 *p = buf, *p_end = buf_end;
  3510. uint32 export_count, i, j, index;
  3511. uint64 total_size;
  3512. uint32 str_len;
  3513. WASMExport *export;
  3514. const char *name;
  3515. read_leb_uint32(p, p_end, export_count);
  3516. if (export_count) {
  3517. module->export_count = export_count;
  3518. total_size = sizeof(WASMExport) * (uint64)export_count;
  3519. if (!(module->exports =
  3520. loader_malloc(total_size, error_buf, error_buf_size))) {
  3521. return false;
  3522. }
  3523. export = module->exports;
  3524. for (i = 0; i < export_count; i++, export ++) {
  3525. #if WASM_ENABLE_THREAD_MGR == 0
  3526. if (p == p_end) {
  3527. /* export section with inconsistent count:
  3528. n export declared, but less than n given */
  3529. set_error_buf(error_buf, error_buf_size,
  3530. "length out of bounds");
  3531. return false;
  3532. }
  3533. #endif
  3534. read_leb_uint32(p, p_end, str_len);
  3535. CHECK_BUF(p, p_end, str_len);
  3536. for (j = 0; j < i; j++) {
  3537. name = module->exports[j].name;
  3538. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3539. set_error_buf(error_buf, error_buf_size,
  3540. "duplicate export name");
  3541. return false;
  3542. }
  3543. }
  3544. if (!(export->name = wasm_const_str_list_insert(
  3545. p, str_len, module, is_load_from_file_buf, error_buf,
  3546. error_buf_size))) {
  3547. return false;
  3548. }
  3549. p += str_len;
  3550. CHECK_BUF(p, p_end, 1);
  3551. export->kind = read_uint8(p);
  3552. read_leb_uint32(p, p_end, index);
  3553. export->index = index;
  3554. switch (export->kind) {
  3555. /* function index */
  3556. case EXPORT_KIND_FUNC:
  3557. if (index >= module->function_count
  3558. + module->import_function_count) {
  3559. set_error_buf(error_buf, error_buf_size,
  3560. "unknown function");
  3561. return false;
  3562. }
  3563. #if WASM_ENABLE_SIMD != 0
  3564. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3565. /* TODO: check func type, if it has v128 param or result,
  3566. report error */
  3567. #endif
  3568. #endif
  3569. break;
  3570. /* table index */
  3571. case EXPORT_KIND_TABLE:
  3572. if (index
  3573. >= module->table_count + module->import_table_count) {
  3574. set_error_buf(error_buf, error_buf_size,
  3575. "unknown table");
  3576. return false;
  3577. }
  3578. break;
  3579. /* memory index */
  3580. case EXPORT_KIND_MEMORY:
  3581. if (index
  3582. >= module->memory_count + module->import_memory_count) {
  3583. set_error_buf(error_buf, error_buf_size,
  3584. "unknown memory");
  3585. return false;
  3586. }
  3587. break;
  3588. #if WASM_ENABLE_TAGS != 0
  3589. /* export tag */
  3590. case EXPORT_KIND_TAG:
  3591. if (index >= module->tag_count + module->import_tag_count) {
  3592. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3593. return false;
  3594. }
  3595. break;
  3596. #endif
  3597. /* global index */
  3598. case EXPORT_KIND_GLOBAL:
  3599. if (index
  3600. >= module->global_count + module->import_global_count) {
  3601. set_error_buf(error_buf, error_buf_size,
  3602. "unknown global");
  3603. return false;
  3604. }
  3605. break;
  3606. default:
  3607. set_error_buf(error_buf, error_buf_size,
  3608. "invalid export kind");
  3609. return false;
  3610. }
  3611. }
  3612. }
  3613. if (p != p_end) {
  3614. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3615. return false;
  3616. }
  3617. LOG_VERBOSE("Load export section success.\n");
  3618. return true;
  3619. fail:
  3620. return false;
  3621. }
  3622. static bool
  3623. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3624. uint32 error_buf_size)
  3625. {
  3626. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3627. if (table_index != 0) {
  3628. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3629. return false;
  3630. }
  3631. #endif
  3632. if (table_index >= module->import_table_count + module->table_count) {
  3633. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3634. table_index);
  3635. return false;
  3636. }
  3637. return true;
  3638. }
  3639. static bool
  3640. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3641. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3642. {
  3643. const uint8 *p = *p_buf, *p_end = buf_end;
  3644. uint32 table_index;
  3645. read_leb_uint32(p, p_end, table_index);
  3646. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3647. return false;
  3648. }
  3649. *p_table_index = table_index;
  3650. *p_buf = p;
  3651. return true;
  3652. fail:
  3653. return false;
  3654. }
  3655. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3656. static bool
  3657. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3658. uint32 *p_elem_type,
  3659. #if WASM_ENABLE_GC != 0
  3660. WASMRefType **p_elem_ref_type,
  3661. #endif
  3662. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3663. {
  3664. const uint8 *p = *p_buf, *p_end = buf_end;
  3665. uint8 elem_type;
  3666. #if WASM_ENABLE_GC != 0
  3667. WASMRefType elem_ref_type;
  3668. bool need_ref_type_map;
  3669. #endif
  3670. CHECK_BUF(p, p_end, 1);
  3671. elem_type = read_uint8(p);
  3672. if (elemkind_zero) {
  3673. if (elem_type != 0) {
  3674. set_error_buf(error_buf, error_buf_size,
  3675. "invalid reference type or unknown type");
  3676. return false;
  3677. }
  3678. else {
  3679. *p_elem_type = VALUE_TYPE_FUNCREF;
  3680. *p_buf = p;
  3681. return true;
  3682. }
  3683. }
  3684. #if WASM_ENABLE_GC == 0
  3685. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3686. set_error_buf(error_buf, error_buf_size,
  3687. "invalid reference type or unknown type");
  3688. return false;
  3689. }
  3690. *p_elem_type = elem_type;
  3691. #else
  3692. p--;
  3693. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3694. &need_ref_type_map, &elem_ref_type, false,
  3695. error_buf, error_buf_size)) {
  3696. return false;
  3697. }
  3698. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3699. set_error_buf(error_buf, error_buf_size,
  3700. "invalid reference type or unknown type");
  3701. return false;
  3702. }
  3703. *p_elem_type = elem_ref_type.ref_type;
  3704. if (need_ref_type_map) {
  3705. if (!(*p_elem_ref_type =
  3706. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3707. error_buf, error_buf_size))) {
  3708. return false;
  3709. }
  3710. }
  3711. #endif
  3712. *p_buf = p;
  3713. return true;
  3714. fail:
  3715. return false;
  3716. }
  3717. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3718. static bool
  3719. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3720. WASMModule *module, WASMTableSeg *table_segment,
  3721. char *error_buf, uint32 error_buf_size)
  3722. {
  3723. const uint8 *p = *p_buf, *p_end = buf_end;
  3724. uint32 function_count, function_index = 0, i;
  3725. uint64 total_size;
  3726. read_leb_uint32(p, p_end, function_count);
  3727. table_segment->value_count = function_count;
  3728. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3729. if (total_size > 0
  3730. && !(table_segment->init_values =
  3731. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3732. error_buf_size))) {
  3733. return false;
  3734. }
  3735. for (i = 0; i < function_count; i++) {
  3736. InitializerExpression *init_expr = &table_segment->init_values[i];
  3737. read_leb_uint32(p, p_end, function_index);
  3738. if (!check_function_index(module, function_index, error_buf,
  3739. error_buf_size)) {
  3740. return false;
  3741. }
  3742. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3743. init_expr->u.ref_index = function_index;
  3744. }
  3745. *p_buf = p;
  3746. return true;
  3747. fail:
  3748. return false;
  3749. }
  3750. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3751. static bool
  3752. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3753. WASMModule *module, WASMTableSeg *table_segment,
  3754. char *error_buf, uint32 error_buf_size)
  3755. {
  3756. const uint8 *p = *p_buf, *p_end = buf_end;
  3757. uint32 ref_count, i;
  3758. uint64 total_size;
  3759. read_leb_uint32(p, p_end, ref_count);
  3760. table_segment->value_count = ref_count;
  3761. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3762. if (total_size > 0
  3763. && !(table_segment->init_values =
  3764. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3765. error_buf_size))) {
  3766. return false;
  3767. }
  3768. for (i = 0; i < ref_count; i++) {
  3769. InitializerExpression *init_expr = &table_segment->init_values[i];
  3770. if (!load_init_expr(module, &p, p_end, init_expr,
  3771. table_segment->elem_type,
  3772. #if WASM_ENABLE_GC == 0
  3773. NULL,
  3774. #else
  3775. table_segment->elem_ref_type,
  3776. #endif
  3777. error_buf, error_buf_size))
  3778. return false;
  3779. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3780. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3781. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3782. && init_expr->init_expr_type
  3783. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3784. }
  3785. *p_buf = p;
  3786. return true;
  3787. fail:
  3788. return false;
  3789. }
  3790. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3791. static bool
  3792. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3793. WASMModule *module, char *error_buf,
  3794. uint32 error_buf_size)
  3795. {
  3796. const uint8 *p = buf, *p_end = buf_end;
  3797. uint32 table_segment_count, i;
  3798. uint64 total_size;
  3799. WASMTableSeg *table_segment;
  3800. read_leb_uint32(p, p_end, table_segment_count);
  3801. if (table_segment_count) {
  3802. module->table_seg_count = table_segment_count;
  3803. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3804. if (!(module->table_segments =
  3805. loader_malloc(total_size, error_buf, error_buf_size))) {
  3806. return false;
  3807. }
  3808. table_segment = module->table_segments;
  3809. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3810. if (p >= p_end) {
  3811. set_error_buf(error_buf, error_buf_size,
  3812. "invalid value type or "
  3813. "invalid elements segment kind");
  3814. return false;
  3815. }
  3816. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3817. read_leb_uint32(p, p_end, table_segment->mode);
  3818. /* last three bits */
  3819. table_segment->mode = table_segment->mode & 0x07;
  3820. switch (table_segment->mode) {
  3821. /* elemkind/elemtype + active */
  3822. case 0:
  3823. case 4:
  3824. {
  3825. #if WASM_ENABLE_GC != 0
  3826. if (table_segment->mode == 0) {
  3827. /* vec(funcidx), set elem type to (ref func) */
  3828. WASMRefType elem_ref_type = { 0 };
  3829. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  3830. wasm_set_refheaptype_common(
  3831. &elem_ref_type.ref_ht_common, false,
  3832. HEAP_TYPE_FUNC);
  3833. if (!(table_segment->elem_ref_type = reftype_set_insert(
  3834. module->ref_type_set, &elem_ref_type,
  3835. error_buf, error_buf_size)))
  3836. return false;
  3837. }
  3838. else {
  3839. /* vec(expr), set elem type to funcref */
  3840. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3841. }
  3842. #else
  3843. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3844. #endif
  3845. table_segment->table_index = 0;
  3846. if (!check_table_index(module, table_segment->table_index,
  3847. error_buf, error_buf_size))
  3848. return false;
  3849. if (!load_init_expr(
  3850. module, &p, p_end, &table_segment->base_offset,
  3851. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3852. return false;
  3853. if (table_segment->mode == 0) {
  3854. /* vec(funcidx) */
  3855. if (!load_func_index_vec(&p, p_end, module,
  3856. table_segment, error_buf,
  3857. error_buf_size))
  3858. return false;
  3859. }
  3860. else {
  3861. /* vec(expr) */
  3862. if (!load_init_expr_vec(&p, p_end, module,
  3863. table_segment, error_buf,
  3864. error_buf_size))
  3865. return false;
  3866. }
  3867. break;
  3868. }
  3869. /* elemkind + passive/declarative */
  3870. case 1:
  3871. case 3:
  3872. if (!load_elem_type(module, &p, p_end,
  3873. &table_segment->elem_type,
  3874. #if WASM_ENABLE_GC != 0
  3875. &table_segment->elem_ref_type,
  3876. #endif
  3877. true, error_buf, error_buf_size))
  3878. return false;
  3879. /* vec(funcidx) */
  3880. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3881. error_buf, error_buf_size))
  3882. return false;
  3883. break;
  3884. /* elemkind/elemtype + table_idx + active */
  3885. case 2:
  3886. case 6:
  3887. if (!load_table_index(&p, p_end, module,
  3888. &table_segment->table_index,
  3889. error_buf, error_buf_size))
  3890. return false;
  3891. if (!load_init_expr(
  3892. module, &p, p_end, &table_segment->base_offset,
  3893. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3894. return false;
  3895. if (!load_elem_type(module, &p, p_end,
  3896. &table_segment->elem_type,
  3897. #if WASM_ENABLE_GC != 0
  3898. &table_segment->elem_ref_type,
  3899. #endif
  3900. table_segment->mode == 2 ? true : false,
  3901. error_buf, error_buf_size))
  3902. return false;
  3903. if (table_segment->mode == 2) {
  3904. /* vec(funcidx) */
  3905. if (!load_func_index_vec(&p, p_end, module,
  3906. table_segment, error_buf,
  3907. error_buf_size))
  3908. return false;
  3909. }
  3910. else {
  3911. /* vec(expr) */
  3912. if (!load_init_expr_vec(&p, p_end, module,
  3913. table_segment, error_buf,
  3914. error_buf_size))
  3915. return false;
  3916. }
  3917. break;
  3918. case 5:
  3919. case 7:
  3920. if (!load_elem_type(module, &p, p_end,
  3921. &table_segment->elem_type,
  3922. #if WASM_ENABLE_GC != 0
  3923. &table_segment->elem_ref_type,
  3924. #endif
  3925. false, error_buf, error_buf_size))
  3926. return false;
  3927. /* vec(expr) */
  3928. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  3929. error_buf, error_buf_size))
  3930. return false;
  3931. break;
  3932. default:
  3933. set_error_buf(error_buf, error_buf_size,
  3934. "unknown element segment kind");
  3935. return false;
  3936. }
  3937. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3938. /*
  3939. * like: 00 41 05 0b 04 00 01 00 01
  3940. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  3941. */
  3942. if (!load_table_index(&p, p_end, module,
  3943. &table_segment->table_index, error_buf,
  3944. error_buf_size))
  3945. return false;
  3946. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  3947. VALUE_TYPE_I32, NULL, error_buf,
  3948. error_buf_size))
  3949. return false;
  3950. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3951. error_buf, error_buf_size))
  3952. return false;
  3953. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3954. #if WASM_ENABLE_WAMR_COMPILER != 0
  3955. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  3956. module->is_ref_types_used = true;
  3957. #endif
  3958. }
  3959. }
  3960. if (p != p_end) {
  3961. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3962. return false;
  3963. }
  3964. LOG_VERBOSE("Load table segment section success.\n");
  3965. return true;
  3966. fail:
  3967. return false;
  3968. }
  3969. static bool
  3970. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  3971. WASMModule *module, char *error_buf,
  3972. uint32 error_buf_size)
  3973. {
  3974. const uint8 *p = buf, *p_end = buf_end;
  3975. uint32 data_seg_count, i, mem_index, data_seg_len;
  3976. uint64 total_size;
  3977. WASMDataSeg *dataseg;
  3978. InitializerExpression init_expr;
  3979. #if WASM_ENABLE_BULK_MEMORY != 0
  3980. bool is_passive = false;
  3981. uint32 mem_flag;
  3982. #endif
  3983. read_leb_uint32(p, p_end, data_seg_count);
  3984. #if WASM_ENABLE_BULK_MEMORY != 0
  3985. if ((module->data_seg_count1 != 0)
  3986. && (data_seg_count != module->data_seg_count1)) {
  3987. set_error_buf(error_buf, error_buf_size,
  3988. "data count and data section have inconsistent lengths");
  3989. return false;
  3990. }
  3991. #endif
  3992. if (data_seg_count) {
  3993. module->data_seg_count = data_seg_count;
  3994. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  3995. if (!(module->data_segments =
  3996. loader_malloc(total_size, error_buf, error_buf_size))) {
  3997. return false;
  3998. }
  3999. for (i = 0; i < data_seg_count; i++) {
  4000. read_leb_uint32(p, p_end, mem_index);
  4001. #if WASM_ENABLE_BULK_MEMORY != 0
  4002. is_passive = false;
  4003. mem_flag = mem_index & 0x03;
  4004. switch (mem_flag) {
  4005. case 0x01:
  4006. is_passive = true;
  4007. #if WASM_ENABLE_WAMR_COMPILER != 0
  4008. module->is_bulk_memory_used = true;
  4009. #endif
  4010. break;
  4011. case 0x00:
  4012. /* no memory index, treat index as 0 */
  4013. mem_index = 0;
  4014. goto check_mem_index;
  4015. case 0x02:
  4016. /* read following memory index */
  4017. read_leb_uint32(p, p_end, mem_index);
  4018. #if WASM_ENABLE_WAMR_COMPILER != 0
  4019. module->is_bulk_memory_used = true;
  4020. #endif
  4021. check_mem_index:
  4022. if (mem_index
  4023. >= module->import_memory_count + module->memory_count) {
  4024. set_error_buf_v(error_buf, error_buf_size,
  4025. "unknown memory %d", mem_index);
  4026. return false;
  4027. }
  4028. break;
  4029. case 0x03:
  4030. default:
  4031. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4032. return false;
  4033. break;
  4034. }
  4035. #else
  4036. if (mem_index
  4037. >= module->import_memory_count + module->memory_count) {
  4038. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4039. mem_index);
  4040. return false;
  4041. }
  4042. #endif /* WASM_ENABLE_BULK_MEMORY */
  4043. #if WASM_ENABLE_BULK_MEMORY != 0
  4044. if (!is_passive)
  4045. #endif
  4046. if (!load_init_expr(module, &p, p_end, &init_expr,
  4047. VALUE_TYPE_I32, NULL, error_buf,
  4048. error_buf_size))
  4049. return false;
  4050. read_leb_uint32(p, p_end, data_seg_len);
  4051. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4052. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4053. return false;
  4054. }
  4055. #if WASM_ENABLE_BULK_MEMORY != 0
  4056. dataseg->is_passive = is_passive;
  4057. if (!is_passive)
  4058. #endif
  4059. {
  4060. bh_memcpy_s(&dataseg->base_offset,
  4061. sizeof(InitializerExpression), &init_expr,
  4062. sizeof(InitializerExpression));
  4063. dataseg->memory_index = mem_index;
  4064. }
  4065. dataseg->data_length = data_seg_len;
  4066. CHECK_BUF(p, p_end, data_seg_len);
  4067. dataseg->data = (uint8 *)p;
  4068. p += data_seg_len;
  4069. }
  4070. }
  4071. if (p != p_end) {
  4072. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4073. return false;
  4074. }
  4075. LOG_VERBOSE("Load data segment section success.\n");
  4076. return true;
  4077. fail:
  4078. return false;
  4079. }
  4080. #if WASM_ENABLE_BULK_MEMORY != 0
  4081. static bool
  4082. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4083. WASMModule *module, char *error_buf,
  4084. uint32 error_buf_size)
  4085. {
  4086. const uint8 *p = buf, *p_end = buf_end;
  4087. uint32 data_seg_count1 = 0;
  4088. read_leb_uint32(p, p_end, data_seg_count1);
  4089. module->data_seg_count1 = data_seg_count1;
  4090. if (p != p_end) {
  4091. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4092. return false;
  4093. }
  4094. #if WASM_ENABLE_WAMR_COMPILER != 0
  4095. module->is_bulk_memory_used = true;
  4096. #endif
  4097. LOG_VERBOSE("Load datacount section success.\n");
  4098. return true;
  4099. fail:
  4100. return false;
  4101. }
  4102. #endif
  4103. #if WASM_ENABLE_TAGS != 0
  4104. static bool
  4105. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4106. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4107. uint32 error_buf_size)
  4108. {
  4109. (void)buf_code;
  4110. (void)buf_code_end;
  4111. const uint8 *p = buf, *p_end = buf_end;
  4112. size_t total_size = 0;
  4113. /* number of tags defined in the section */
  4114. uint32 section_tag_count = 0;
  4115. uint8 tag_attribute;
  4116. uint32 tag_type;
  4117. WASMTag *tag = NULL;
  4118. /* get tag count */
  4119. read_leb_uint32(p, p_end, section_tag_count);
  4120. module->tag_count = section_tag_count;
  4121. if (section_tag_count) {
  4122. total_size = sizeof(WASMTag *) * module->tag_count;
  4123. if (!(module->tags =
  4124. loader_malloc(total_size, error_buf, error_buf_size))) {
  4125. return false;
  4126. }
  4127. /* load each tag, imported tags precede the tags */
  4128. uint32 tag_index;
  4129. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4130. /* get the one byte attribute */
  4131. CHECK_BUF(p, p_end, 1);
  4132. tag_attribute = read_uint8(p);
  4133. /* get type */
  4134. read_leb_uint32(p, p_end, tag_type);
  4135. /* compare against module->types */
  4136. if (tag_type >= module->type_count) {
  4137. set_error_buf(error_buf, error_buf_size, "unknown type");
  4138. return false;
  4139. }
  4140. /* get return type (must be 0) */
  4141. /* check, that the type of the referred tag returns void */
  4142. WASMType *func_type = (WASMType *)module->types[tag_type];
  4143. if (func_type->result_count != 0) {
  4144. set_error_buf(error_buf, error_buf_size,
  4145. "non-empty tag result type");
  4146. goto fail;
  4147. }
  4148. if (!(tag = module->tags[tag_index] = loader_malloc(
  4149. sizeof(WASMTag), error_buf, error_buf_size))) {
  4150. return false;
  4151. }
  4152. /* store to module tag declarations */
  4153. tag->attribute = tag_attribute;
  4154. tag->type = tag_type;
  4155. tag->tag_type = func_type;
  4156. }
  4157. }
  4158. if (p != p_end) {
  4159. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4160. return false;
  4161. }
  4162. LOG_VERBOSE("Load tag section success.\n");
  4163. return true;
  4164. fail:
  4165. return false;
  4166. }
  4167. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4168. static bool
  4169. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4170. const uint8 *buf_func_end, WASMModule *module,
  4171. char *error_buf, uint32 error_buf_size)
  4172. {
  4173. const uint8 *p = buf, *p_end = buf_end;
  4174. const uint8 *p_func = buf_func;
  4175. uint32 func_count = 0, code_count;
  4176. /* code has been loaded in function section, so pass it here, just check
  4177. * whether function and code section have inconsistent lengths */
  4178. read_leb_uint32(p, p_end, code_count);
  4179. if (buf_func)
  4180. read_leb_uint32(p_func, buf_func_end, func_count);
  4181. if (func_count != code_count) {
  4182. set_error_buf(error_buf, error_buf_size,
  4183. "function and code section have inconsistent lengths");
  4184. return false;
  4185. }
  4186. LOG_VERBOSE("Load code segment section success.\n");
  4187. (void)module;
  4188. return true;
  4189. fail:
  4190. return false;
  4191. }
  4192. static bool
  4193. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4194. char *error_buf, uint32 error_buf_size)
  4195. {
  4196. const uint8 *p = buf, *p_end = buf_end;
  4197. WASMFuncType *type;
  4198. uint32 start_function;
  4199. read_leb_uint32(p, p_end, start_function);
  4200. if (start_function
  4201. >= module->function_count + module->import_function_count) {
  4202. set_error_buf(error_buf, error_buf_size, "unknown function");
  4203. return false;
  4204. }
  4205. if (start_function < module->import_function_count)
  4206. type = module->import_functions[start_function].u.function.func_type;
  4207. else
  4208. type = module->functions[start_function - module->import_function_count]
  4209. ->func_type;
  4210. if (type->param_count != 0 || type->result_count != 0) {
  4211. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4212. return false;
  4213. }
  4214. module->start_function = start_function;
  4215. if (p != p_end) {
  4216. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4217. return false;
  4218. }
  4219. LOG_VERBOSE("Load start section success.\n");
  4220. return true;
  4221. fail:
  4222. return false;
  4223. }
  4224. #if WASM_ENABLE_STRINGREF != 0
  4225. static bool
  4226. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4227. WASMModule *module, bool is_load_from_file_buf,
  4228. char *error_buf, uint32 error_buf_size)
  4229. {
  4230. const uint8 *p = buf, *p_end = buf_end;
  4231. int32 deferred_count, immediate_count, string_length, i;
  4232. uint64 total_size;
  4233. read_leb_uint32(p, p_end, deferred_count);
  4234. read_leb_uint32(p, p_end, immediate_count);
  4235. /* proposal set deferred_count for future extension */
  4236. if (deferred_count != 0) {
  4237. goto fail;
  4238. }
  4239. if (immediate_count > 0) {
  4240. total_size = sizeof(char *) * (uint64)immediate_count;
  4241. if (!(module->string_literal_ptrs =
  4242. loader_malloc(total_size, error_buf, error_buf_size))) {
  4243. goto fail;
  4244. }
  4245. module->string_literal_count = immediate_count;
  4246. total_size = sizeof(uint32) * (uint64)immediate_count;
  4247. if (!(module->string_literal_lengths =
  4248. loader_malloc(total_size, error_buf, error_buf_size))) {
  4249. goto fail;
  4250. }
  4251. for (i = 0; i < immediate_count; i++) {
  4252. read_leb_uint32(p, p_end, string_length);
  4253. CHECK_BUF(p, p_end, string_length);
  4254. module->string_literal_ptrs[i] = p;
  4255. module->string_literal_lengths[i] = string_length;
  4256. p += string_length;
  4257. }
  4258. }
  4259. if (p != p_end) {
  4260. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4261. goto fail;
  4262. }
  4263. LOG_VERBOSE("Load stringref section success.\n");
  4264. return true;
  4265. fail:
  4266. return false;
  4267. }
  4268. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4269. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4270. static bool
  4271. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4272. bool is_load_from_file_buf, char *error_buf,
  4273. uint32 error_buf_size)
  4274. {
  4275. const uint8 *p = buf, *p_end = buf_end;
  4276. uint32 name_type, subsection_size;
  4277. uint32 previous_name_type = 0;
  4278. uint32 num_func_name;
  4279. uint32 func_index;
  4280. uint32 previous_func_index = ~0U;
  4281. uint32 func_name_len;
  4282. uint32 name_index;
  4283. int i = 0;
  4284. if (p >= p_end) {
  4285. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4286. return false;
  4287. }
  4288. while (p < p_end) {
  4289. read_leb_uint32(p, p_end, name_type);
  4290. if (i != 0) {
  4291. if (name_type == previous_name_type) {
  4292. set_error_buf(error_buf, error_buf_size,
  4293. "duplicate sub-section");
  4294. return false;
  4295. }
  4296. if (name_type < previous_name_type) {
  4297. set_error_buf(error_buf, error_buf_size,
  4298. "out-of-order sub-section");
  4299. return false;
  4300. }
  4301. }
  4302. previous_name_type = name_type;
  4303. read_leb_uint32(p, p_end, subsection_size);
  4304. CHECK_BUF(p, p_end, subsection_size);
  4305. switch (name_type) {
  4306. case SUB_SECTION_TYPE_FUNC:
  4307. if (subsection_size) {
  4308. read_leb_uint32(p, p_end, num_func_name);
  4309. for (name_index = 0; name_index < num_func_name;
  4310. name_index++) {
  4311. read_leb_uint32(p, p_end, func_index);
  4312. if (func_index == previous_func_index) {
  4313. set_error_buf(error_buf, error_buf_size,
  4314. "duplicate function name");
  4315. return false;
  4316. }
  4317. if (func_index < previous_func_index
  4318. && previous_func_index != ~0U) {
  4319. set_error_buf(error_buf, error_buf_size,
  4320. "out-of-order function index ");
  4321. return false;
  4322. }
  4323. previous_func_index = func_index;
  4324. read_leb_uint32(p, p_end, func_name_len);
  4325. CHECK_BUF(p, p_end, func_name_len);
  4326. /* Skip the import functions */
  4327. if (func_index >= module->import_function_count) {
  4328. func_index -= module->import_function_count;
  4329. if (func_index >= module->function_count) {
  4330. set_error_buf(error_buf, error_buf_size,
  4331. "out-of-range function index");
  4332. return false;
  4333. }
  4334. if (!(module->functions[func_index]->field_name =
  4335. wasm_const_str_list_insert(
  4336. p, func_name_len, module,
  4337. #if WASM_ENABLE_WAMR_COMPILER != 0
  4338. false,
  4339. #else
  4340. is_load_from_file_buf,
  4341. #endif
  4342. error_buf, error_buf_size))) {
  4343. return false;
  4344. }
  4345. }
  4346. p += func_name_len;
  4347. }
  4348. }
  4349. break;
  4350. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4351. */
  4352. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4353. default:
  4354. p = p + subsection_size;
  4355. break;
  4356. }
  4357. i++;
  4358. }
  4359. return true;
  4360. fail:
  4361. return false;
  4362. }
  4363. #endif
  4364. static bool
  4365. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4366. bool is_load_from_file_buf, char *error_buf,
  4367. uint32 error_buf_size)
  4368. {
  4369. const uint8 *p = buf, *p_end = buf_end;
  4370. char section_name[32];
  4371. uint32 name_len, buffer_len;
  4372. if (p >= p_end) {
  4373. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4374. return false;
  4375. }
  4376. read_leb_uint32(p, p_end, name_len);
  4377. if (p + name_len > p_end) {
  4378. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4379. return false;
  4380. }
  4381. if (!wasm_check_utf8_str(p, name_len)) {
  4382. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4383. return false;
  4384. }
  4385. buffer_len = sizeof(section_name);
  4386. memset(section_name, 0, buffer_len);
  4387. if (name_len < buffer_len) {
  4388. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4389. }
  4390. else {
  4391. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4392. memset(section_name + buffer_len - 4, '.', 3);
  4393. }
  4394. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4395. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4396. module->name_section_buf = buf;
  4397. module->name_section_buf_end = buf_end;
  4398. p += name_len;
  4399. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  4400. error_buf_size);
  4401. LOG_VERBOSE("Load custom name section success.");
  4402. return true;
  4403. }
  4404. #endif
  4405. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4406. {
  4407. WASMCustomSection *section =
  4408. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4409. if (!section) {
  4410. return false;
  4411. }
  4412. section->name_addr = (char *)p;
  4413. section->name_len = name_len;
  4414. section->content_addr = (uint8 *)(p + name_len);
  4415. section->content_len = (uint32)(p_end - p - name_len);
  4416. section->next = module->custom_section_list;
  4417. module->custom_section_list = section;
  4418. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4419. return true;
  4420. }
  4421. #endif
  4422. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4423. (void)is_load_from_file_buf;
  4424. (void)module;
  4425. return true;
  4426. fail:
  4427. return false;
  4428. }
  4429. static void
  4430. calculate_global_data_offset(WASMModule *module)
  4431. {
  4432. uint32 i, data_offset;
  4433. data_offset = 0;
  4434. for (i = 0; i < module->import_global_count; i++) {
  4435. WASMGlobalImport *import_global =
  4436. &((module->import_globals + i)->u.global);
  4437. #if WASM_ENABLE_FAST_JIT != 0
  4438. import_global->data_offset = data_offset;
  4439. #endif
  4440. data_offset += wasm_value_type_size(import_global->type);
  4441. }
  4442. for (i = 0; i < module->global_count; i++) {
  4443. WASMGlobal *global = module->globals + i;
  4444. #if WASM_ENABLE_FAST_JIT != 0
  4445. global->data_offset = data_offset;
  4446. #endif
  4447. data_offset += wasm_value_type_size(global->type);
  4448. }
  4449. module->global_data_size = data_offset;
  4450. }
  4451. #if WASM_ENABLE_FAST_JIT != 0
  4452. static bool
  4453. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4454. uint32 error_buf_size)
  4455. {
  4456. #if WASM_ENABLE_LAZY_JIT != 0
  4457. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4458. #endif
  4459. uint32 i;
  4460. if (!module->function_count)
  4461. return true;
  4462. if (!(module->fast_jit_func_ptrs =
  4463. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4464. error_buf_size))) {
  4465. return false;
  4466. }
  4467. #if WASM_ENABLE_LAZY_JIT != 0
  4468. for (i = 0; i < module->function_count; i++) {
  4469. module->fast_jit_func_ptrs[i] =
  4470. jit_globals->compile_fast_jit_and_then_call;
  4471. }
  4472. #endif
  4473. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4474. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4475. set_error_buf(error_buf, error_buf_size,
  4476. "init fast jit thread lock failed");
  4477. return false;
  4478. }
  4479. module->fast_jit_thread_locks_inited[i] = true;
  4480. }
  4481. return true;
  4482. }
  4483. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4484. #if WASM_ENABLE_JIT != 0
  4485. static bool
  4486. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4487. uint32 error_buf_size)
  4488. {
  4489. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4490. AOTCompOption option = { 0 };
  4491. char *aot_last_error;
  4492. uint64 size;
  4493. #if WASM_ENABLE_GC != 0
  4494. bool gc_enabled = true;
  4495. #else
  4496. bool gc_enabled = false;
  4497. #endif
  4498. if (module->function_count == 0)
  4499. return true;
  4500. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4501. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4502. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4503. return false;
  4504. }
  4505. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4506. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4507. os_mutex_destroy(&module->tierup_wait_lock);
  4508. return false;
  4509. }
  4510. module->tierup_wait_lock_inited = true;
  4511. #endif
  4512. size = sizeof(void *) * (uint64)module->function_count
  4513. + sizeof(bool) * (uint64)module->function_count;
  4514. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4515. return false;
  4516. }
  4517. module->func_ptrs_compiled =
  4518. (bool *)((uint8 *)module->func_ptrs
  4519. + sizeof(void *) * module->function_count);
  4520. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4521. if (!module->comp_data) {
  4522. aot_last_error = aot_get_last_error();
  4523. bh_assert(aot_last_error != NULL);
  4524. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4525. return false;
  4526. }
  4527. option.is_jit_mode = true;
  4528. option.opt_level = llvm_jit_options->opt_level;
  4529. option.size_level = llvm_jit_options->size_level;
  4530. option.segue_flags = llvm_jit_options->segue_flags;
  4531. option.quick_invoke_c_api_import =
  4532. llvm_jit_options->quick_invoke_c_api_import;
  4533. #if WASM_ENABLE_BULK_MEMORY != 0
  4534. option.enable_bulk_memory = true;
  4535. #endif
  4536. #if WASM_ENABLE_THREAD_MGR != 0
  4537. option.enable_thread_mgr = true;
  4538. #endif
  4539. #if WASM_ENABLE_TAIL_CALL != 0
  4540. option.enable_tail_call = true;
  4541. #endif
  4542. #if WASM_ENABLE_SIMD != 0
  4543. option.enable_simd = true;
  4544. #endif
  4545. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4546. option.enable_ref_types = true;
  4547. #elif WASM_ENABLE_GC != 0
  4548. option.enable_gc = true;
  4549. #endif
  4550. option.enable_aux_stack_check = true;
  4551. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4552. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4553. option.enable_aux_stack_frame = true;
  4554. #endif
  4555. #if WASM_ENABLE_PERF_PROFILING != 0
  4556. option.enable_perf_profiling = true;
  4557. #endif
  4558. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4559. option.enable_memory_profiling = true;
  4560. option.enable_stack_estimation = true;
  4561. #endif
  4562. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4563. if (!module->comp_ctx) {
  4564. aot_last_error = aot_get_last_error();
  4565. bh_assert(aot_last_error != NULL);
  4566. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4567. return false;
  4568. }
  4569. return true;
  4570. }
  4571. static bool
  4572. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4573. uint32 error_buf_size)
  4574. {
  4575. char *aot_last_error;
  4576. uint32 i;
  4577. if (module->function_count == 0)
  4578. return true;
  4579. if (!aot_compile_wasm(module->comp_ctx)) {
  4580. aot_last_error = aot_get_last_error();
  4581. bh_assert(aot_last_error != NULL);
  4582. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4583. return false;
  4584. }
  4585. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4586. if (module->orcjit_stop_compiling)
  4587. return false;
  4588. #endif
  4589. bh_print_time("Begin to lookup llvm jit functions");
  4590. for (i = 0; i < module->function_count; i++) {
  4591. LLVMOrcJITTargetAddress func_addr = 0;
  4592. LLVMErrorRef error;
  4593. char func_name[48];
  4594. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4595. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4596. func_name);
  4597. if (error != LLVMErrorSuccess) {
  4598. char *err_msg = LLVMGetErrorMessage(error);
  4599. set_error_buf_v(error_buf, error_buf_size,
  4600. "failed to compile llvm jit function: %s", err_msg);
  4601. LLVMDisposeErrorMessage(err_msg);
  4602. return false;
  4603. }
  4604. /**
  4605. * No need to lock the func_ptr[func_idx] here as it is basic
  4606. * data type, the load/store for it can be finished by one cpu
  4607. * instruction, and there can be only one cpu instruction
  4608. * loading/storing at the same time.
  4609. */
  4610. module->func_ptrs[i] = (void *)func_addr;
  4611. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4612. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4613. if (module->orcjit_stop_compiling)
  4614. return false;
  4615. #endif
  4616. }
  4617. bh_print_time("End lookup llvm jit functions");
  4618. return true;
  4619. }
  4620. #endif /* end of WASM_ENABLE_JIT != 0 */
  4621. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4622. && WASM_ENABLE_LAZY_JIT != 0
  4623. static void *
  4624. init_llvm_jit_functions_stage2_callback(void *arg)
  4625. {
  4626. WASMModule *module = (WASMModule *)arg;
  4627. char error_buf[128];
  4628. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4629. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4630. module->orcjit_stop_compiling = true;
  4631. return NULL;
  4632. }
  4633. os_mutex_lock(&module->tierup_wait_lock);
  4634. module->llvm_jit_inited = true;
  4635. os_cond_broadcast(&module->tierup_wait_cond);
  4636. os_mutex_unlock(&module->tierup_wait_lock);
  4637. return NULL;
  4638. }
  4639. #endif
  4640. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4641. /* The callback function to compile jit functions */
  4642. static void *
  4643. orcjit_thread_callback(void *arg)
  4644. {
  4645. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4646. #if WASM_ENABLE_JIT != 0
  4647. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4648. #endif
  4649. WASMModule *module = thread_arg->module;
  4650. uint32 group_idx = thread_arg->group_idx;
  4651. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4652. uint32 func_count = module->function_count;
  4653. uint32 i;
  4654. #if WASM_ENABLE_FAST_JIT != 0
  4655. /* Compile fast jit funcitons of this group */
  4656. for (i = group_idx; i < func_count; i += group_stride) {
  4657. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4658. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4659. break;
  4660. }
  4661. if (module->orcjit_stop_compiling) {
  4662. return NULL;
  4663. }
  4664. }
  4665. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4666. os_mutex_lock(&module->tierup_wait_lock);
  4667. module->fast_jit_ready_groups++;
  4668. os_mutex_unlock(&module->tierup_wait_lock);
  4669. #endif
  4670. #endif
  4671. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4672. && WASM_ENABLE_LAZY_JIT != 0
  4673. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4674. for (i = group_idx; i < func_count;
  4675. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4676. uint32 j;
  4677. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4678. if (i + j * group_stride < func_count) {
  4679. if (!jit_compiler_set_call_to_fast_jit(
  4680. module,
  4681. i + j * group_stride + module->import_function_count)) {
  4682. LOG_ERROR(
  4683. "failed to compile call_to_fast_jit for func %u\n",
  4684. i + j * group_stride + module->import_function_count);
  4685. module->orcjit_stop_compiling = true;
  4686. return NULL;
  4687. }
  4688. }
  4689. if (module->orcjit_stop_compiling) {
  4690. return NULL;
  4691. }
  4692. }
  4693. }
  4694. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4695. fast jit functions are compiled */
  4696. os_mutex_lock(&module->tierup_wait_lock);
  4697. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4698. && module->fast_jit_ready_groups >= group_stride)) {
  4699. os_cond_reltimedwait(&module->tierup_wait_cond,
  4700. &module->tierup_wait_lock, 10000);
  4701. if (module->orcjit_stop_compiling) {
  4702. /* init_llvm_jit_functions_stage2 failed */
  4703. os_mutex_unlock(&module->tierup_wait_lock);
  4704. return NULL;
  4705. }
  4706. }
  4707. os_mutex_unlock(&module->tierup_wait_lock);
  4708. #endif
  4709. #if WASM_ENABLE_JIT != 0
  4710. /* Compile llvm jit functions of this group */
  4711. for (i = group_idx; i < func_count;
  4712. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4713. LLVMOrcJITTargetAddress func_addr = 0;
  4714. LLVMErrorRef error;
  4715. char func_name[48];
  4716. typedef void (*F)(void);
  4717. union {
  4718. F f;
  4719. void *v;
  4720. } u;
  4721. uint32 j;
  4722. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4723. "_wrapper");
  4724. LOG_DEBUG("compile llvm jit func %s", func_name);
  4725. error =
  4726. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4727. if (error != LLVMErrorSuccess) {
  4728. char *err_msg = LLVMGetErrorMessage(error);
  4729. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4730. LLVMDisposeErrorMessage(err_msg);
  4731. break;
  4732. }
  4733. /* Call the jit wrapper function to trigger its compilation, so as
  4734. to compile the actual jit functions, since we add the latter to
  4735. function list in the PartitionFunction callback */
  4736. u.v = (void *)func_addr;
  4737. u.f();
  4738. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4739. if (i + j * group_stride < func_count) {
  4740. module->func_ptrs_compiled[i + j * group_stride] = true;
  4741. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4742. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  4743. i + j * group_stride);
  4744. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  4745. func_name);
  4746. if (error != LLVMErrorSuccess) {
  4747. char *err_msg = LLVMGetErrorMessage(error);
  4748. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  4749. err_msg);
  4750. LLVMDisposeErrorMessage(err_msg);
  4751. /* Ignore current llvm jit func, as its func ptr is
  4752. previous set to call_to_fast_jit, which also works */
  4753. continue;
  4754. }
  4755. jit_compiler_set_llvm_jit_func_ptr(
  4756. module,
  4757. i + j * group_stride + module->import_function_count,
  4758. (void *)func_addr);
  4759. /* Try to switch to call this llvm jit funtion instead of
  4760. fast jit function from fast jit jitted code */
  4761. jit_compiler_set_call_to_llvm_jit(
  4762. module,
  4763. i + j * group_stride + module->import_function_count);
  4764. #endif
  4765. }
  4766. }
  4767. if (module->orcjit_stop_compiling) {
  4768. break;
  4769. }
  4770. }
  4771. #endif
  4772. return NULL;
  4773. }
  4774. static void
  4775. orcjit_stop_compile_threads(WASMModule *module)
  4776. {
  4777. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  4778. / sizeof(OrcJitThreadArg));
  4779. module->orcjit_stop_compiling = true;
  4780. for (i = 0; i < thread_num; i++) {
  4781. if (module->orcjit_threads[i])
  4782. os_thread_join(module->orcjit_threads[i], NULL);
  4783. }
  4784. }
  4785. static bool
  4786. compile_jit_functions(WASMModule *module, char *error_buf,
  4787. uint32 error_buf_size)
  4788. {
  4789. uint32 thread_num =
  4790. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  4791. uint32 i, j;
  4792. bh_print_time("Begin to compile jit functions");
  4793. /* Create threads to compile the jit functions */
  4794. for (i = 0; i < thread_num && i < module->function_count; i++) {
  4795. #if WASM_ENABLE_JIT != 0
  4796. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  4797. #endif
  4798. module->orcjit_thread_args[i].module = module;
  4799. module->orcjit_thread_args[i].group_idx = i;
  4800. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  4801. (void *)&module->orcjit_thread_args[i],
  4802. APP_THREAD_STACK_SIZE_DEFAULT)
  4803. != 0) {
  4804. set_error_buf(error_buf, error_buf_size,
  4805. "create orcjit compile thread failed");
  4806. /* Terminate the threads created */
  4807. module->orcjit_stop_compiling = true;
  4808. for (j = 0; j < i; j++) {
  4809. os_thread_join(module->orcjit_threads[j], NULL);
  4810. }
  4811. return false;
  4812. }
  4813. }
  4814. #if WASM_ENABLE_LAZY_JIT == 0
  4815. /* Wait until all jit functions are compiled for eager mode */
  4816. for (i = 0; i < thread_num; i++) {
  4817. if (module->orcjit_threads[i])
  4818. os_thread_join(module->orcjit_threads[i], NULL);
  4819. }
  4820. #if WASM_ENABLE_FAST_JIT != 0
  4821. /* Ensure all the fast-jit functions are compiled */
  4822. for (i = 0; i < module->function_count; i++) {
  4823. if (!jit_compiler_is_compiled(module,
  4824. i + module->import_function_count)) {
  4825. set_error_buf(error_buf, error_buf_size,
  4826. "failed to compile fast jit function");
  4827. return false;
  4828. }
  4829. }
  4830. #endif
  4831. #if WASM_ENABLE_JIT != 0
  4832. /* Ensure all the llvm-jit functions are compiled */
  4833. for (i = 0; i < module->function_count; i++) {
  4834. if (!module->func_ptrs_compiled[i]) {
  4835. set_error_buf(error_buf, error_buf_size,
  4836. "failed to compile llvm jit function");
  4837. return false;
  4838. }
  4839. }
  4840. #endif
  4841. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  4842. bh_print_time("End compile jit functions");
  4843. return true;
  4844. }
  4845. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  4846. static bool
  4847. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4848. uint32 cur_func_idx, char *error_buf,
  4849. uint32 error_buf_size);
  4850. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  4851. void **
  4852. wasm_interp_get_handle_table();
  4853. static void **handle_table;
  4854. #endif
  4855. static bool
  4856. load_from_sections(WASMModule *module, WASMSection *sections,
  4857. bool is_load_from_file_buf, char *error_buf,
  4858. uint32 error_buf_size)
  4859. {
  4860. WASMExport *export;
  4861. WASMSection *section = sections;
  4862. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  4863. *buf_func = NULL, *buf_func_end = NULL;
  4864. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  4865. WASMGlobal *aux_stack_top_global = NULL, *global;
  4866. uint64 aux_data_end = (uint64)-1, aux_heap_base = (uint64)-1,
  4867. aux_stack_top = (uint64)-1;
  4868. uint32 global_index, func_index, i;
  4869. uint32 aux_data_end_global_index = (uint32)-1;
  4870. uint32 aux_heap_base_global_index = (uint32)-1;
  4871. WASMFuncType *func_type;
  4872. /* Find code and function sections if have */
  4873. while (section) {
  4874. if (section->section_type == SECTION_TYPE_CODE) {
  4875. buf_code = section->section_body;
  4876. buf_code_end = buf_code + section->section_body_size;
  4877. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  4878. module->buf_code = (uint8 *)buf_code;
  4879. module->buf_code_size = section->section_body_size;
  4880. #endif
  4881. }
  4882. else if (section->section_type == SECTION_TYPE_FUNC) {
  4883. buf_func = section->section_body;
  4884. buf_func_end = buf_func + section->section_body_size;
  4885. }
  4886. section = section->next;
  4887. }
  4888. section = sections;
  4889. while (section) {
  4890. buf = section->section_body;
  4891. buf_end = buf + section->section_body_size;
  4892. switch (section->section_type) {
  4893. case SECTION_TYPE_USER:
  4894. /* unsupported user section, ignore it. */
  4895. if (!load_user_section(buf, buf_end, module,
  4896. is_load_from_file_buf, error_buf,
  4897. error_buf_size))
  4898. return false;
  4899. break;
  4900. case SECTION_TYPE_TYPE:
  4901. if (!load_type_section(buf, buf_end, module, error_buf,
  4902. error_buf_size))
  4903. return false;
  4904. break;
  4905. case SECTION_TYPE_IMPORT:
  4906. if (!load_import_section(buf, buf_end, module,
  4907. is_load_from_file_buf, error_buf,
  4908. error_buf_size))
  4909. return false;
  4910. break;
  4911. case SECTION_TYPE_FUNC:
  4912. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  4913. module, error_buf, error_buf_size))
  4914. return false;
  4915. break;
  4916. case SECTION_TYPE_TABLE:
  4917. if (!load_table_section(buf, buf_end, module, error_buf,
  4918. error_buf_size))
  4919. return false;
  4920. break;
  4921. case SECTION_TYPE_MEMORY:
  4922. if (!load_memory_section(buf, buf_end, module, error_buf,
  4923. error_buf_size))
  4924. return false;
  4925. break;
  4926. #if WASM_ENABLE_TAGS != 0
  4927. case SECTION_TYPE_TAG:
  4928. /* load tag declaration section */
  4929. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  4930. module, error_buf, error_buf_size))
  4931. return false;
  4932. break;
  4933. #endif
  4934. case SECTION_TYPE_GLOBAL:
  4935. if (!load_global_section(buf, buf_end, module, error_buf,
  4936. error_buf_size))
  4937. return false;
  4938. break;
  4939. case SECTION_TYPE_EXPORT:
  4940. if (!load_export_section(buf, buf_end, module,
  4941. is_load_from_file_buf, error_buf,
  4942. error_buf_size))
  4943. return false;
  4944. break;
  4945. case SECTION_TYPE_START:
  4946. if (!load_start_section(buf, buf_end, module, error_buf,
  4947. error_buf_size))
  4948. return false;
  4949. break;
  4950. case SECTION_TYPE_ELEM:
  4951. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  4952. error_buf_size))
  4953. return false;
  4954. break;
  4955. case SECTION_TYPE_CODE:
  4956. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  4957. module, error_buf, error_buf_size))
  4958. return false;
  4959. break;
  4960. case SECTION_TYPE_DATA:
  4961. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  4962. error_buf_size))
  4963. return false;
  4964. break;
  4965. #if WASM_ENABLE_BULK_MEMORY != 0
  4966. case SECTION_TYPE_DATACOUNT:
  4967. if (!load_datacount_section(buf, buf_end, module, error_buf,
  4968. error_buf_size))
  4969. return false;
  4970. break;
  4971. #endif
  4972. #if WASM_ENABLE_STRINGREF != 0
  4973. case SECTION_TYPE_STRINGREF:
  4974. if (!load_stringref_section(buf, buf_end, module,
  4975. is_load_from_file_buf, error_buf,
  4976. error_buf_size))
  4977. return false;
  4978. break;
  4979. #endif
  4980. default:
  4981. set_error_buf(error_buf, error_buf_size, "invalid section id");
  4982. return false;
  4983. }
  4984. section = section->next;
  4985. }
  4986. module->aux_data_end_global_index = (uint32)-1;
  4987. module->aux_heap_base_global_index = (uint32)-1;
  4988. module->aux_stack_top_global_index = (uint32)-1;
  4989. /* Resolve auxiliary data/stack/heap info and reset memory info */
  4990. export = module->exports;
  4991. for (i = 0; i < module->export_count; i++, export ++) {
  4992. if (export->kind == EXPORT_KIND_GLOBAL) {
  4993. if (!strcmp(export->name, "__heap_base")) {
  4994. global_index = export->index - module->import_global_count;
  4995. global = module->globals + global_index;
  4996. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  4997. && global->init_expr.init_expr_type
  4998. == INIT_EXPR_TYPE_I32_CONST) {
  4999. aux_heap_base_global = global;
  5000. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5001. aux_heap_base_global_index = export->index;
  5002. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  5003. aux_heap_base);
  5004. }
  5005. }
  5006. else if (!strcmp(export->name, "__data_end")) {
  5007. global_index = export->index - module->import_global_count;
  5008. global = module->globals + global_index;
  5009. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5010. && global->init_expr.init_expr_type
  5011. == INIT_EXPR_TYPE_I32_CONST) {
  5012. aux_data_end_global = global;
  5013. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5014. aux_data_end_global_index = export->index;
  5015. LOG_VERBOSE("Found aux __data_end global, value: %d",
  5016. aux_data_end);
  5017. aux_data_end = align_uint64(aux_data_end, 16);
  5018. }
  5019. }
  5020. /* For module compiled with -pthread option, the global is:
  5021. [0] stack_top <-- 0
  5022. [1] tls_pointer
  5023. [2] tls_size
  5024. [3] data_end <-- 3
  5025. [4] global_base
  5026. [5] heap_base <-- 5
  5027. [6] dso_handle
  5028. For module compiled without -pthread option:
  5029. [0] stack_top <-- 0
  5030. [1] data_end <-- 1
  5031. [2] global_base
  5032. [3] heap_base <-- 3
  5033. [4] dso_handle
  5034. */
  5035. if (aux_data_end_global && aux_heap_base_global
  5036. && aux_data_end <= aux_heap_base) {
  5037. module->aux_data_end_global_index = aux_data_end_global_index;
  5038. module->aux_data_end = aux_data_end;
  5039. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5040. module->aux_heap_base = aux_heap_base;
  5041. /* Resolve aux stack top global */
  5042. for (global_index = 0; global_index < module->global_count;
  5043. global_index++) {
  5044. global = module->globals + global_index;
  5045. if (global->is_mutable /* heap_base and data_end is
  5046. not mutable */
  5047. && global->type == VALUE_TYPE_I32
  5048. && global->init_expr.init_expr_type
  5049. == INIT_EXPR_TYPE_I32_CONST
  5050. && (uint64)(uint32)global->init_expr.u.i32
  5051. <= aux_heap_base) {
  5052. aux_stack_top_global = global;
  5053. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5054. module->aux_stack_top_global_index =
  5055. module->import_global_count + global_index;
  5056. module->aux_stack_bottom = aux_stack_top;
  5057. module->aux_stack_size =
  5058. aux_stack_top > aux_data_end
  5059. ? (uint32)(aux_stack_top - aux_data_end)
  5060. : (uint32)aux_stack_top;
  5061. LOG_VERBOSE("Found aux stack top global, value: %d, "
  5062. "global index: %d, stack size: %d",
  5063. aux_stack_top, global_index,
  5064. module->aux_stack_size);
  5065. break;
  5066. }
  5067. }
  5068. if (!aux_stack_top_global) {
  5069. /* Auxiliary stack global isn't found, it must be unused
  5070. in the wasm app, as if it is used, the global must be
  5071. defined. Here we set it to __heap_base global and set
  5072. its size to 0. */
  5073. aux_stack_top_global = aux_heap_base_global;
  5074. aux_stack_top = aux_heap_base;
  5075. module->aux_stack_top_global_index =
  5076. module->aux_heap_base_global_index;
  5077. module->aux_stack_bottom = aux_stack_top;
  5078. module->aux_stack_size = 0;
  5079. }
  5080. break;
  5081. }
  5082. }
  5083. }
  5084. module->malloc_function = (uint32)-1;
  5085. module->free_function = (uint32)-1;
  5086. module->retain_function = (uint32)-1;
  5087. /* Resolve malloc/free function exported by wasm module */
  5088. export = module->exports;
  5089. for (i = 0; i < module->export_count; i++, export ++) {
  5090. if (export->kind == EXPORT_KIND_FUNC) {
  5091. if (!strcmp(export->name, "malloc")
  5092. && export->index >= module->import_function_count) {
  5093. func_index = export->index - module->import_function_count;
  5094. func_type = module->functions[func_index]->func_type;
  5095. if (func_type->param_count == 1 && func_type->result_count == 1
  5096. && func_type->types[0] == VALUE_TYPE_I32
  5097. && func_type->types[1] == VALUE_TYPE_I32) {
  5098. bh_assert(module->malloc_function == (uint32)-1);
  5099. module->malloc_function = export->index;
  5100. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5101. export->name, export->index);
  5102. }
  5103. }
  5104. else if (!strcmp(export->name, "__new")
  5105. && export->index >= module->import_function_count) {
  5106. /* __new && __pin for AssemblyScript */
  5107. func_index = export->index - module->import_function_count;
  5108. func_type = module->functions[func_index]->func_type;
  5109. if (func_type->param_count == 2 && func_type->result_count == 1
  5110. && func_type->types[0] == VALUE_TYPE_I32
  5111. && func_type->types[1] == VALUE_TYPE_I32
  5112. && func_type->types[2] == VALUE_TYPE_I32) {
  5113. uint32 j;
  5114. WASMExport *export_tmp;
  5115. bh_assert(module->malloc_function == (uint32)-1);
  5116. module->malloc_function = export->index;
  5117. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5118. export->name, export->index);
  5119. /* resolve retain function.
  5120. If not found, reset malloc function index */
  5121. export_tmp = module->exports;
  5122. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5123. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5124. && (!strcmp(export_tmp->name, "__retain")
  5125. || (!strcmp(export_tmp->name, "__pin")))
  5126. && (export_tmp->index
  5127. >= module->import_function_count)) {
  5128. func_index = export_tmp->index
  5129. - module->import_function_count;
  5130. func_type =
  5131. module->functions[func_index]->func_type;
  5132. if (func_type->param_count == 1
  5133. && func_type->result_count == 1
  5134. && func_type->types[0] == VALUE_TYPE_I32
  5135. && func_type->types[1] == VALUE_TYPE_I32) {
  5136. bh_assert(module->retain_function
  5137. == (uint32)-1);
  5138. module->retain_function = export_tmp->index;
  5139. LOG_VERBOSE("Found retain function, name: %s, "
  5140. "index: %u",
  5141. export_tmp->name,
  5142. export_tmp->index);
  5143. break;
  5144. }
  5145. }
  5146. }
  5147. if (j == module->export_count) {
  5148. module->malloc_function = (uint32)-1;
  5149. LOG_VERBOSE("Can't find retain function,"
  5150. "reset malloc function index to -1");
  5151. }
  5152. }
  5153. }
  5154. else if (((!strcmp(export->name, "free"))
  5155. || (!strcmp(export->name, "__release"))
  5156. || (!strcmp(export->name, "__unpin")))
  5157. && export->index >= module->import_function_count) {
  5158. func_index = export->index - module->import_function_count;
  5159. func_type = module->functions[func_index]->func_type;
  5160. if (func_type->param_count == 1 && func_type->result_count == 0
  5161. && func_type->types[0] == VALUE_TYPE_I32) {
  5162. bh_assert(module->free_function == (uint32)-1);
  5163. module->free_function = export->index;
  5164. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5165. export->name, export->index);
  5166. }
  5167. }
  5168. }
  5169. }
  5170. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5171. handle_table = wasm_interp_get_handle_table();
  5172. #endif
  5173. for (i = 0; i < module->function_count; i++) {
  5174. WASMFunction *func = module->functions[i];
  5175. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5176. error_buf_size)) {
  5177. return false;
  5178. }
  5179. if (i == module->function_count - 1
  5180. && func->code + func->code_size != buf_code_end) {
  5181. set_error_buf(error_buf, error_buf_size,
  5182. "code section size mismatch");
  5183. return false;
  5184. }
  5185. }
  5186. if (!module->possible_memory_grow) {
  5187. WASMMemoryImport *memory_import;
  5188. WASMMemory *memory;
  5189. if (aux_data_end_global && aux_heap_base_global
  5190. && aux_stack_top_global) {
  5191. uint64 init_memory_size;
  5192. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5193. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5194. * valid range of uint32 */
  5195. if (shrunk_memory_size <= UINT32_MAX) {
  5196. if (module->import_memory_count) {
  5197. memory_import = &module->import_memories[0].u.memory;
  5198. init_memory_size = (uint64)memory_import->num_bytes_per_page
  5199. * memory_import->init_page_count;
  5200. if (shrunk_memory_size <= init_memory_size) {
  5201. /* Reset memory info to decrease memory usage */
  5202. memory_import->num_bytes_per_page = shrunk_memory_size;
  5203. memory_import->init_page_count = 1;
  5204. LOG_VERBOSE("Shrink import memory size to %d",
  5205. shrunk_memory_size);
  5206. }
  5207. }
  5208. if (module->memory_count) {
  5209. memory = &module->memories[0];
  5210. init_memory_size = (uint64)memory->num_bytes_per_page
  5211. * memory->init_page_count;
  5212. if (shrunk_memory_size <= init_memory_size) {
  5213. /* Reset memory info to decrease memory usage */
  5214. memory->num_bytes_per_page = shrunk_memory_size;
  5215. memory->init_page_count = 1;
  5216. LOG_VERBOSE("Shrink memory size to %d",
  5217. shrunk_memory_size);
  5218. }
  5219. }
  5220. }
  5221. }
  5222. #if WASM_ENABLE_MULTI_MODULE == 0
  5223. if (module->import_memory_count) {
  5224. memory_import = &module->import_memories[0].u.memory;
  5225. /* Only resize the memory to one big page if num_bytes_per_page is
  5226. * in valid range of uint32 */
  5227. if (memory_import->init_page_count < DEFAULT_MAX_PAGES) {
  5228. memory_import->num_bytes_per_page *=
  5229. memory_import->init_page_count;
  5230. if (memory_import->init_page_count > 0)
  5231. memory_import->init_page_count =
  5232. memory_import->max_page_count = 1;
  5233. else
  5234. memory_import->init_page_count =
  5235. memory_import->max_page_count = 0;
  5236. }
  5237. }
  5238. if (module->memory_count) {
  5239. memory = &module->memories[0];
  5240. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5241. * valid range of uint32 */
  5242. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5243. memory->num_bytes_per_page *= memory->init_page_count;
  5244. if (memory->init_page_count > 0)
  5245. memory->init_page_count = memory->max_page_count = 1;
  5246. else
  5247. memory->init_page_count = memory->max_page_count = 0;
  5248. }
  5249. }
  5250. #endif
  5251. }
  5252. calculate_global_data_offset(module);
  5253. #if WASM_ENABLE_FAST_JIT != 0
  5254. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5255. return false;
  5256. }
  5257. #endif
  5258. #if WASM_ENABLE_JIT != 0
  5259. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5260. return false;
  5261. }
  5262. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5263. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5264. return false;
  5265. }
  5266. #else
  5267. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5268. thread fast jit execution, since applying llvm optimizations in
  5269. aot_compile_wasm may cost a lot of time.
  5270. Create thread with enough native stack to apply llvm optimizations */
  5271. if (os_thread_create(&module->llvm_jit_init_thread,
  5272. init_llvm_jit_functions_stage2_callback,
  5273. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5274. != 0) {
  5275. set_error_buf(error_buf, error_buf_size,
  5276. "create orcjit compile thread failed");
  5277. return false;
  5278. }
  5279. #endif
  5280. #endif
  5281. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5282. /* Create threads to compile the jit functions */
  5283. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5284. return false;
  5285. }
  5286. #endif
  5287. #if WASM_ENABLE_MEMORY_TRACING != 0
  5288. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5289. #endif
  5290. return true;
  5291. }
  5292. static WASMModule *
  5293. create_module(char *error_buf, uint32 error_buf_size)
  5294. {
  5295. WASMModule *module =
  5296. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5297. bh_list_status ret;
  5298. if (!module) {
  5299. return NULL;
  5300. }
  5301. module->module_type = Wasm_Module_Bytecode;
  5302. /* Set start_function to -1, means no start function */
  5303. module->start_function = (uint32)-1;
  5304. module->name = "";
  5305. #if WASM_ENABLE_FAST_INTERP == 0
  5306. module->br_table_cache_list = &module->br_table_cache_list_head;
  5307. ret = bh_list_init(module->br_table_cache_list);
  5308. bh_assert(ret == BH_LIST_SUCCESS);
  5309. #endif
  5310. #if WASM_ENABLE_MULTI_MODULE != 0
  5311. module->import_module_list = &module->import_module_list_head;
  5312. ret = bh_list_init(module->import_module_list);
  5313. bh_assert(ret == BH_LIST_SUCCESS);
  5314. #endif
  5315. #if WASM_ENABLE_DEBUG_INTERP != 0
  5316. ret = bh_list_init(&module->fast_opcode_list);
  5317. bh_assert(ret == BH_LIST_SUCCESS);
  5318. #endif
  5319. #if WASM_ENABLE_GC != 0
  5320. if (!(module->ref_type_set =
  5321. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5322. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5323. goto fail1;
  5324. }
  5325. if (os_mutex_init(&module->rtt_type_lock)) {
  5326. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5327. goto fail2;
  5328. }
  5329. #endif
  5330. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5331. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5332. && WASM_ENABLE_LAZY_JIT != 0)
  5333. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5334. set_error_buf(error_buf, error_buf_size,
  5335. "init instance list lock failed");
  5336. goto fail3;
  5337. }
  5338. #endif
  5339. (void)ret;
  5340. return module;
  5341. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5342. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5343. && WASM_ENABLE_LAZY_JIT != 0)
  5344. fail3:
  5345. #endif
  5346. #if WASM_ENABLE_GC != 0
  5347. os_mutex_destroy(&module->rtt_type_lock);
  5348. fail2:
  5349. bh_hash_map_destroy(module->ref_type_set);
  5350. fail1:
  5351. #endif
  5352. wasm_runtime_free(module);
  5353. return NULL;
  5354. }
  5355. #if WASM_ENABLE_DEBUG_INTERP != 0
  5356. static bool
  5357. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5358. uint32 error_buf_size)
  5359. {
  5360. WASMFastOPCodeNode *fast_op =
  5361. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5362. if (fast_op) {
  5363. fast_op->offset = pos - module->load_addr;
  5364. fast_op->orig_op = orig_op;
  5365. bh_list_insert(&module->fast_opcode_list, fast_op);
  5366. }
  5367. return fast_op ? true : false;
  5368. }
  5369. #endif
  5370. WASMModule *
  5371. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5372. uint32 error_buf_size)
  5373. {
  5374. WASMModule *module = create_module(error_buf, error_buf_size);
  5375. if (!module)
  5376. return NULL;
  5377. if (!load_from_sections(module, section_list, false, error_buf,
  5378. error_buf_size)) {
  5379. wasm_loader_unload(module);
  5380. return NULL;
  5381. }
  5382. LOG_VERBOSE("Load module from sections success.\n");
  5383. return module;
  5384. }
  5385. static void
  5386. destroy_sections(WASMSection *section_list)
  5387. {
  5388. WASMSection *section = section_list, *next;
  5389. while (section) {
  5390. next = section->next;
  5391. wasm_runtime_free(section);
  5392. section = next;
  5393. }
  5394. }
  5395. /* clang-format off */
  5396. static uint8 section_ids[] = {
  5397. SECTION_TYPE_USER,
  5398. SECTION_TYPE_TYPE,
  5399. SECTION_TYPE_IMPORT,
  5400. SECTION_TYPE_FUNC,
  5401. SECTION_TYPE_TABLE,
  5402. SECTION_TYPE_MEMORY,
  5403. #if WASM_ENABLE_TAGS != 0
  5404. SECTION_TYPE_TAG,
  5405. #endif
  5406. #if WASM_ENABLE_STRINGREF != 0
  5407. /* must immediately precede the global section,
  5408. or where the global section would be */
  5409. SECTION_TYPE_STRINGREF,
  5410. #endif
  5411. SECTION_TYPE_GLOBAL,
  5412. SECTION_TYPE_EXPORT,
  5413. SECTION_TYPE_START,
  5414. SECTION_TYPE_ELEM,
  5415. #if WASM_ENABLE_BULK_MEMORY != 0
  5416. SECTION_TYPE_DATACOUNT,
  5417. #endif
  5418. SECTION_TYPE_CODE,
  5419. SECTION_TYPE_DATA
  5420. };
  5421. /* clang-format on */
  5422. static uint8
  5423. get_section_index(uint8 section_type)
  5424. {
  5425. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5426. for (uint8 i = 0; i < max_id; i++) {
  5427. if (section_type == section_ids[i])
  5428. return i;
  5429. }
  5430. return (uint8)-1;
  5431. }
  5432. static bool
  5433. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5434. char *error_buf, uint32 error_buf_size)
  5435. {
  5436. WASMSection *section_list_end = NULL, *section;
  5437. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  5438. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5439. uint32 section_size;
  5440. bh_assert(!*p_section_list);
  5441. p += 8;
  5442. while (p < p_end) {
  5443. CHECK_BUF(p, p_end, 1);
  5444. section_type = read_uint8(p);
  5445. section_index = get_section_index(section_type);
  5446. if (section_index != (uint8)-1) {
  5447. if (section_type != SECTION_TYPE_USER) {
  5448. /* Custom sections may be inserted at any place,
  5449. while other sections must occur at most once
  5450. and in prescribed order. */
  5451. if (last_section_index != (uint8)-1
  5452. && (section_index <= last_section_index)) {
  5453. set_error_buf(error_buf, error_buf_size,
  5454. "unexpected content after last section or "
  5455. "junk after last section");
  5456. return false;
  5457. }
  5458. last_section_index = section_index;
  5459. }
  5460. read_leb_uint32(p, p_end, section_size);
  5461. CHECK_BUF1(p, p_end, section_size);
  5462. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5463. error_buf_size))) {
  5464. return false;
  5465. }
  5466. section->section_type = section_type;
  5467. section->section_body = (uint8 *)p;
  5468. section->section_body_size = section_size;
  5469. if (!section_list_end)
  5470. *p_section_list = section_list_end = section;
  5471. else {
  5472. section_list_end->next = section;
  5473. section_list_end = section;
  5474. }
  5475. p += section_size;
  5476. }
  5477. else {
  5478. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5479. return false;
  5480. }
  5481. }
  5482. return true;
  5483. fail:
  5484. return false;
  5485. }
  5486. static void
  5487. exchange32(uint8 *p_data)
  5488. {
  5489. uint8 value = *p_data;
  5490. *p_data = *(p_data + 3);
  5491. *(p_data + 3) = value;
  5492. value = *(p_data + 1);
  5493. *(p_data + 1) = *(p_data + 2);
  5494. *(p_data + 2) = value;
  5495. }
  5496. static union {
  5497. int a;
  5498. char b;
  5499. } __ue = { .a = 1 };
  5500. #define is_little_endian() (__ue.b == 1)
  5501. static bool
  5502. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  5503. uint32 error_buf_size)
  5504. {
  5505. const uint8 *buf_end = buf + size;
  5506. const uint8 *p = buf, *p_end = buf_end;
  5507. uint32 magic_number, version;
  5508. WASMSection *section_list = NULL;
  5509. CHECK_BUF1(p, p_end, sizeof(uint32));
  5510. magic_number = read_uint32(p);
  5511. if (!is_little_endian())
  5512. exchange32((uint8 *)&magic_number);
  5513. if (magic_number != WASM_MAGIC_NUMBER) {
  5514. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5515. return false;
  5516. }
  5517. CHECK_BUF1(p, p_end, sizeof(uint32));
  5518. version = read_uint32(p);
  5519. if (!is_little_endian())
  5520. exchange32((uint8 *)&version);
  5521. if (version != WASM_CURRENT_VERSION) {
  5522. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5523. return false;
  5524. }
  5525. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5526. || !load_from_sections(module, section_list, true, error_buf,
  5527. error_buf_size)) {
  5528. destroy_sections(section_list);
  5529. return false;
  5530. }
  5531. destroy_sections(section_list);
  5532. return true;
  5533. fail:
  5534. return false;
  5535. }
  5536. #if WASM_ENABLE_LIBC_WASI != 0
  5537. /**
  5538. * refer to
  5539. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5540. */
  5541. static bool
  5542. check_wasi_abi_compatibility(const WASMModule *module,
  5543. #if WASM_ENABLE_MULTI_MODULE != 0
  5544. bool main_module,
  5545. #endif
  5546. char *error_buf, uint32 error_buf_size)
  5547. {
  5548. /**
  5549. * be careful with:
  5550. * wasi compatiable modules(command/reactor) which don't import any wasi
  5551. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5552. * reactor can depend on nothing. At the same time, each has its own entry
  5553. * point.
  5554. *
  5555. * observations:
  5556. * - clang always injects `_start` into a command
  5557. * - clang always injects `_initialize` into a reactor
  5558. * - `iwasm -f` allows to run a function in the reactor
  5559. *
  5560. * strong assumptions:
  5561. * - no one will define either `_start` or `_initialize` on purpose
  5562. * - `_start` should always be `void _start(void)`
  5563. * - `_initialize` should always be `void _initialize(void)`
  5564. *
  5565. */
  5566. /* clang-format off */
  5567. /**
  5568. *
  5569. * | | import_wasi_api True | | import_wasi_api False | |
  5570. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5571. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5572. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5573. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5574. */
  5575. /* clang-format on */
  5576. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5577. uint32 import_function_count = module->import_function_count;
  5578. WASMFuncType *func_type;
  5579. /* (func (export "_start") (...) */
  5580. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5581. error_buf, error_buf_size);
  5582. if (start) {
  5583. if (start->index < import_function_count) {
  5584. set_error_buf(
  5585. error_buf, error_buf_size,
  5586. "the builtin _start function can not be an import function");
  5587. return false;
  5588. }
  5589. func_type =
  5590. module->functions[start->index - import_function_count]->func_type;
  5591. if (func_type->param_count || func_type->result_count) {
  5592. set_error_buf(error_buf, error_buf_size,
  5593. "the signature of builtin _start function is wrong");
  5594. return false;
  5595. }
  5596. }
  5597. else {
  5598. /* (func (export "_initialize") (...) */
  5599. initialize =
  5600. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5601. error_buf, error_buf_size);
  5602. if (initialize) {
  5603. if (initialize->index < import_function_count) {
  5604. set_error_buf(error_buf, error_buf_size,
  5605. "the builtin _initialize function can not be an "
  5606. "import function");
  5607. return false;
  5608. }
  5609. func_type =
  5610. module->functions[initialize->index - import_function_count]
  5611. ->func_type;
  5612. if (func_type->param_count || func_type->result_count) {
  5613. set_error_buf(
  5614. error_buf, error_buf_size,
  5615. "the signature of builtin _initialize function is wrong");
  5616. return false;
  5617. }
  5618. }
  5619. }
  5620. /* filter out non-wasi compatiable modules */
  5621. if (!module->import_wasi_api && !start && !initialize) {
  5622. return true;
  5623. }
  5624. /* should have one at least */
  5625. if (module->import_wasi_api && !start && !initialize) {
  5626. LOG_WARNING("warning: a module with WASI apis should be either "
  5627. "a command or a reactor");
  5628. }
  5629. /*
  5630. * there is at least one of `_start` and `_initialize` in below cases.
  5631. * according to the assumption, they should be all wasi compatiable
  5632. */
  5633. #if WASM_ENABLE_MULTI_MODULE != 0
  5634. /* filter out commands (with `_start`) cases */
  5635. if (start && !main_module) {
  5636. set_error_buf(
  5637. error_buf, error_buf_size,
  5638. "a command (with _start function) can not be a sub-module");
  5639. return false;
  5640. }
  5641. #endif
  5642. /*
  5643. * it is ok a reactor acts as a main module,
  5644. * so skip the check about (with `_initialize`)
  5645. */
  5646. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5647. error_buf, error_buf_size);
  5648. if (!memory
  5649. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5650. /*
  5651. * with wasi-threads, it's still an open question if a memory
  5652. * should be exported.
  5653. *
  5654. * https://github.com/WebAssembly/wasi-threads/issues/22
  5655. * https://github.com/WebAssembly/WASI/issues/502
  5656. *
  5657. * Note: this code assumes the number of memories is at most 1.
  5658. */
  5659. && module->import_memory_count == 0
  5660. #endif
  5661. ) {
  5662. set_error_buf(error_buf, error_buf_size,
  5663. "a module with WASI apis must export memory by default");
  5664. return false;
  5665. }
  5666. return true;
  5667. }
  5668. #endif
  5669. WASMModule *
  5670. wasm_loader_load(uint8 *buf, uint32 size,
  5671. #if WASM_ENABLE_MULTI_MODULE != 0
  5672. bool main_module,
  5673. #endif
  5674. char *error_buf, uint32 error_buf_size)
  5675. {
  5676. WASMModule *module = create_module(error_buf, error_buf_size);
  5677. if (!module) {
  5678. return NULL;
  5679. }
  5680. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5681. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5682. module->load_addr = (uint8 *)buf;
  5683. module->load_size = size;
  5684. #endif
  5685. if (!load(buf, size, module, error_buf, error_buf_size)) {
  5686. goto fail;
  5687. }
  5688. #if WASM_ENABLE_LIBC_WASI != 0
  5689. /* Check the WASI application ABI */
  5690. if (!check_wasi_abi_compatibility(module,
  5691. #if WASM_ENABLE_MULTI_MODULE != 0
  5692. main_module,
  5693. #endif
  5694. error_buf, error_buf_size)) {
  5695. goto fail;
  5696. }
  5697. #endif
  5698. LOG_VERBOSE("Load module success.\n");
  5699. return module;
  5700. fail:
  5701. wasm_loader_unload(module);
  5702. return NULL;
  5703. }
  5704. void
  5705. wasm_loader_unload(WASMModule *module)
  5706. {
  5707. uint32 i;
  5708. if (!module)
  5709. return;
  5710. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5711. && WASM_ENABLE_LAZY_JIT != 0
  5712. module->orcjit_stop_compiling = true;
  5713. if (module->llvm_jit_init_thread)
  5714. os_thread_join(module->llvm_jit_init_thread, NULL);
  5715. #endif
  5716. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5717. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  5718. module internal data after they were freed */
  5719. orcjit_stop_compile_threads(module);
  5720. #endif
  5721. #if WASM_ENABLE_JIT != 0
  5722. if (module->func_ptrs)
  5723. wasm_runtime_free(module->func_ptrs);
  5724. if (module->comp_ctx)
  5725. aot_destroy_comp_context(module->comp_ctx);
  5726. if (module->comp_data)
  5727. aot_destroy_comp_data(module->comp_data);
  5728. #endif
  5729. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5730. && WASM_ENABLE_LAZY_JIT != 0
  5731. if (module->tierup_wait_lock_inited) {
  5732. os_mutex_destroy(&module->tierup_wait_lock);
  5733. os_cond_destroy(&module->tierup_wait_cond);
  5734. }
  5735. #endif
  5736. if (module->types) {
  5737. for (i = 0; i < module->type_count; i++) {
  5738. if (module->types[i])
  5739. destroy_wasm_type(module->types[i]);
  5740. }
  5741. wasm_runtime_free(module->types);
  5742. }
  5743. if (module->imports)
  5744. wasm_runtime_free(module->imports);
  5745. if (module->functions) {
  5746. for (i = 0; i < module->function_count; i++) {
  5747. if (module->functions[i]) {
  5748. if (module->functions[i]->local_offsets)
  5749. wasm_runtime_free(module->functions[i]->local_offsets);
  5750. #if WASM_ENABLE_FAST_INTERP != 0
  5751. if (module->functions[i]->code_compiled)
  5752. wasm_runtime_free(module->functions[i]->code_compiled);
  5753. if (module->functions[i]->consts)
  5754. wasm_runtime_free(module->functions[i]->consts);
  5755. #endif
  5756. #if WASM_ENABLE_FAST_JIT != 0
  5757. if (module->functions[i]->fast_jit_jitted_code) {
  5758. jit_code_cache_free(
  5759. module->functions[i]->fast_jit_jitted_code);
  5760. }
  5761. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5762. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  5763. jit_code_cache_free(
  5764. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  5765. }
  5766. #endif
  5767. #endif
  5768. #if WASM_ENABLE_GC != 0
  5769. if (module->functions[i]->local_ref_type_maps) {
  5770. wasm_runtime_free(
  5771. module->functions[i]->local_ref_type_maps);
  5772. }
  5773. #endif
  5774. wasm_runtime_free(module->functions[i]);
  5775. }
  5776. }
  5777. wasm_runtime_free(module->functions);
  5778. }
  5779. if (module->tables) {
  5780. #if WASM_ENABLE_GC != 0
  5781. for (i = 0; i < module->table_count; i++) {
  5782. destroy_init_expr(&module->tables[i].init_expr);
  5783. }
  5784. #endif
  5785. wasm_runtime_free(module->tables);
  5786. }
  5787. if (module->memories)
  5788. wasm_runtime_free(module->memories);
  5789. if (module->globals) {
  5790. #if WASM_ENABLE_GC != 0
  5791. for (i = 0; i < module->global_count; i++) {
  5792. destroy_init_expr(&module->globals[i].init_expr);
  5793. }
  5794. #endif
  5795. wasm_runtime_free(module->globals);
  5796. }
  5797. #if WASM_ENABLE_TAGS != 0
  5798. if (module->tags) {
  5799. for (i = 0; i < module->tag_count; i++) {
  5800. if (module->tags[i])
  5801. wasm_runtime_free(module->tags[i]);
  5802. }
  5803. wasm_runtime_free(module->tags);
  5804. }
  5805. #endif
  5806. if (module->exports)
  5807. wasm_runtime_free(module->exports);
  5808. if (module->table_segments) {
  5809. for (i = 0; i < module->table_seg_count; i++) {
  5810. if (module->table_segments[i].init_values) {
  5811. #if WASM_ENABLE_GC != 0
  5812. uint32 j;
  5813. for (j = 0; j < module->table_segments[i].value_count; j++) {
  5814. destroy_init_expr(
  5815. &module->table_segments[i].init_values[j]);
  5816. }
  5817. #endif
  5818. wasm_runtime_free(module->table_segments[i].init_values);
  5819. }
  5820. }
  5821. wasm_runtime_free(module->table_segments);
  5822. }
  5823. if (module->data_segments) {
  5824. for (i = 0; i < module->data_seg_count; i++) {
  5825. if (module->data_segments[i])
  5826. wasm_runtime_free(module->data_segments[i]);
  5827. }
  5828. wasm_runtime_free(module->data_segments);
  5829. }
  5830. if (module->const_str_list) {
  5831. StringNode *node = module->const_str_list, *node_next;
  5832. while (node) {
  5833. node_next = node->next;
  5834. wasm_runtime_free(node);
  5835. node = node_next;
  5836. }
  5837. }
  5838. #if WASM_ENABLE_STRINGREF != 0
  5839. if (module->string_literal_ptrs) {
  5840. wasm_runtime_free(module->string_literal_ptrs);
  5841. }
  5842. if (module->string_literal_lengths) {
  5843. wasm_runtime_free(module->string_literal_lengths);
  5844. }
  5845. #endif
  5846. #if WASM_ENABLE_FAST_INTERP == 0
  5847. if (module->br_table_cache_list) {
  5848. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  5849. BrTableCache *node_next;
  5850. while (node) {
  5851. node_next = bh_list_elem_next(node);
  5852. wasm_runtime_free(node);
  5853. node = node_next;
  5854. }
  5855. }
  5856. #endif
  5857. #if WASM_ENABLE_MULTI_MODULE != 0
  5858. /* just release the sub module list */
  5859. if (module->import_module_list) {
  5860. WASMRegisteredModule *node =
  5861. bh_list_first_elem(module->import_module_list);
  5862. while (node) {
  5863. WASMRegisteredModule *next = bh_list_elem_next(node);
  5864. bh_list_remove(module->import_module_list, node);
  5865. /*
  5866. * unload(sub_module) will be trigged during runtime_destroy().
  5867. * every module in the global module list will be unloaded one by
  5868. * one. so don't worry.
  5869. */
  5870. wasm_runtime_free(node);
  5871. /*
  5872. * the module file reading buffer will be released
  5873. * in runtime_destroy()
  5874. */
  5875. node = next;
  5876. }
  5877. }
  5878. #endif
  5879. #if WASM_ENABLE_DEBUG_INTERP != 0
  5880. WASMFastOPCodeNode *fast_opcode =
  5881. bh_list_first_elem(&module->fast_opcode_list);
  5882. while (fast_opcode) {
  5883. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  5884. wasm_runtime_free(fast_opcode);
  5885. fast_opcode = next;
  5886. }
  5887. #endif
  5888. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5889. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5890. && WASM_ENABLE_LAZY_JIT != 0)
  5891. os_mutex_destroy(&module->instance_list_lock);
  5892. #endif
  5893. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  5894. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  5895. #endif
  5896. #if WASM_ENABLE_FAST_JIT != 0
  5897. if (module->fast_jit_func_ptrs) {
  5898. wasm_runtime_free(module->fast_jit_func_ptrs);
  5899. }
  5900. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  5901. if (module->fast_jit_thread_locks_inited[i]) {
  5902. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  5903. }
  5904. }
  5905. #endif
  5906. #if WASM_ENABLE_GC != 0
  5907. os_mutex_destroy(&module->rtt_type_lock);
  5908. bh_hash_map_destroy(module->ref_type_set);
  5909. if (module->rtt_types) {
  5910. for (i = 0; i < module->type_count; i++) {
  5911. if (module->rtt_types[i])
  5912. wasm_runtime_free(module->rtt_types[i]);
  5913. }
  5914. wasm_runtime_free(module->rtt_types);
  5915. }
  5916. #if WASM_ENABLE_STRINGREF != 0
  5917. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  5918. if (module->stringref_rtts[i])
  5919. wasm_runtime_free(module->stringref_rtts[i]);
  5920. }
  5921. #endif
  5922. #endif
  5923. wasm_runtime_free(module);
  5924. }
  5925. bool
  5926. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  5927. const uint8 *start_addr, const uint8 *code_end_addr,
  5928. uint8 label_type, uint8 **p_else_addr,
  5929. uint8 **p_end_addr)
  5930. {
  5931. const uint8 *p = start_addr, *p_end = code_end_addr;
  5932. uint8 *else_addr = NULL;
  5933. char error_buf[128];
  5934. uint32 block_nested_depth = 1, count, i, j, t;
  5935. uint32 error_buf_size = sizeof(error_buf);
  5936. uint8 opcode, u8;
  5937. BlockAddr block_stack[16] = { { 0 } }, *block;
  5938. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  5939. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  5940. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  5941. if (block[j].start_addr == start_addr) {
  5942. /* Cache hit */
  5943. *p_else_addr = block[j].else_addr;
  5944. *p_end_addr = block[j].end_addr;
  5945. return true;
  5946. }
  5947. }
  5948. /* Cache unhit */
  5949. block_stack[0].start_addr = start_addr;
  5950. while (p < code_end_addr) {
  5951. opcode = *p++;
  5952. #if WASM_ENABLE_DEBUG_INTERP != 0
  5953. op_break_retry:
  5954. #endif
  5955. switch (opcode) {
  5956. case WASM_OP_UNREACHABLE:
  5957. case WASM_OP_NOP:
  5958. break;
  5959. #if WASM_ENABLE_EXCE_HANDLING != 0
  5960. case WASM_OP_TRY:
  5961. u8 = read_uint8(p);
  5962. if (block_nested_depth
  5963. < sizeof(block_stack) / sizeof(BlockAddr)) {
  5964. block_stack[block_nested_depth].start_addr = p;
  5965. block_stack[block_nested_depth].else_addr = NULL;
  5966. }
  5967. block_nested_depth++;
  5968. break;
  5969. case EXT_OP_TRY:
  5970. skip_leb_uint32(p, p_end);
  5971. if (block_nested_depth
  5972. < sizeof(block_stack) / sizeof(BlockAddr)) {
  5973. block_stack[block_nested_depth].start_addr = p;
  5974. block_stack[block_nested_depth].else_addr = NULL;
  5975. }
  5976. block_nested_depth++;
  5977. break;
  5978. case WASM_OP_CATCH:
  5979. if (block_nested_depth == 1) {
  5980. *p_end_addr = (uint8 *)(p - 1);
  5981. /* stop search and return the address of the catch block */
  5982. return true;
  5983. }
  5984. break;
  5985. case WASM_OP_CATCH_ALL:
  5986. if (block_nested_depth == 1) {
  5987. *p_end_addr = (uint8 *)(p - 1);
  5988. /* stop search and return the address of the catch_all block
  5989. */
  5990. return true;
  5991. }
  5992. break;
  5993. case WASM_OP_THROW:
  5994. /* skip tag_index */
  5995. skip_leb(p);
  5996. break;
  5997. case WASM_OP_RETHROW:
  5998. /* skip depth */
  5999. skip_leb(p);
  6000. break;
  6001. case WASM_OP_DELEGATE:
  6002. if (block_nested_depth == 1) {
  6003. *p_end_addr = (uint8 *)(p - 1);
  6004. return true;
  6005. }
  6006. else {
  6007. skip_leb(p);
  6008. /* the DELEGATE opcode ends the tryblock, */
  6009. block_nested_depth--;
  6010. if (block_nested_depth
  6011. < sizeof(block_stack) / sizeof(BlockAddr))
  6012. block_stack[block_nested_depth].end_addr =
  6013. (uint8 *)(p - 1);
  6014. }
  6015. break;
  6016. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6017. case WASM_OP_BLOCK:
  6018. case WASM_OP_LOOP:
  6019. case WASM_OP_IF:
  6020. {
  6021. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6022. u8 = read_uint8(p);
  6023. if (is_byte_a_type(u8)) {
  6024. #if WASM_ENABLE_GC != 0
  6025. if (wasm_is_type_multi_byte_type(u8)) {
  6026. /* the possible extra bytes of GC ref type have been
  6027. modified to OP_NOP, no need to resolve them again */
  6028. }
  6029. #endif
  6030. }
  6031. else {
  6032. p--;
  6033. skip_leb_uint32(p, p_end);
  6034. }
  6035. if (block_nested_depth
  6036. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6037. block_stack[block_nested_depth].start_addr = p;
  6038. block_stack[block_nested_depth].else_addr = NULL;
  6039. }
  6040. block_nested_depth++;
  6041. break;
  6042. }
  6043. case EXT_OP_BLOCK:
  6044. case EXT_OP_LOOP:
  6045. case EXT_OP_IF:
  6046. /* block type */
  6047. skip_leb_uint32(p, p_end);
  6048. if (block_nested_depth
  6049. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6050. block_stack[block_nested_depth].start_addr = p;
  6051. block_stack[block_nested_depth].else_addr = NULL;
  6052. }
  6053. block_nested_depth++;
  6054. break;
  6055. case WASM_OP_ELSE:
  6056. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6057. else_addr = (uint8 *)(p - 1);
  6058. if (block_nested_depth - 1
  6059. < sizeof(block_stack) / sizeof(BlockAddr))
  6060. block_stack[block_nested_depth - 1].else_addr =
  6061. (uint8 *)(p - 1);
  6062. break;
  6063. case WASM_OP_END:
  6064. if (block_nested_depth == 1) {
  6065. if (label_type == LABEL_TYPE_IF)
  6066. *p_else_addr = else_addr;
  6067. *p_end_addr = (uint8 *)(p - 1);
  6068. block_stack[0].end_addr = (uint8 *)(p - 1);
  6069. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6070. t++) {
  6071. start_addr = block_stack[t].start_addr;
  6072. if (start_addr) {
  6073. i = ((uintptr_t)start_addr)
  6074. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6075. block =
  6076. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6077. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6078. if (!block[j].start_addr)
  6079. break;
  6080. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6081. memmove(block + 1, block,
  6082. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6083. * sizeof(BlockAddr));
  6084. j = 0;
  6085. }
  6086. block[j].start_addr = block_stack[t].start_addr;
  6087. block[j].else_addr = block_stack[t].else_addr;
  6088. block[j].end_addr = block_stack[t].end_addr;
  6089. }
  6090. else
  6091. break;
  6092. }
  6093. return true;
  6094. }
  6095. else {
  6096. block_nested_depth--;
  6097. if (block_nested_depth
  6098. < sizeof(block_stack) / sizeof(BlockAddr))
  6099. block_stack[block_nested_depth].end_addr =
  6100. (uint8 *)(p - 1);
  6101. }
  6102. break;
  6103. case WASM_OP_BR:
  6104. case WASM_OP_BR_IF:
  6105. skip_leb_uint32(p, p_end); /* labelidx */
  6106. break;
  6107. case WASM_OP_BR_TABLE:
  6108. read_leb_uint32(p, p_end, count); /* lable num */
  6109. #if WASM_ENABLE_FAST_INTERP != 0
  6110. for (i = 0; i <= count; i++) /* lableidxs */
  6111. skip_leb_uint32(p, p_end);
  6112. #else
  6113. p += count + 1;
  6114. while (*p == WASM_OP_NOP)
  6115. p++;
  6116. #endif
  6117. break;
  6118. #if WASM_ENABLE_FAST_INTERP == 0
  6119. case EXT_OP_BR_TABLE_CACHE:
  6120. read_leb_uint32(p, p_end, count); /* lable num */
  6121. while (*p == WASM_OP_NOP)
  6122. p++;
  6123. break;
  6124. #endif
  6125. case WASM_OP_RETURN:
  6126. break;
  6127. case WASM_OP_CALL:
  6128. #if WASM_ENABLE_TAIL_CALL != 0
  6129. case WASM_OP_RETURN_CALL:
  6130. #endif
  6131. skip_leb_uint32(p, p_end); /* funcidx */
  6132. break;
  6133. case WASM_OP_CALL_INDIRECT:
  6134. #if WASM_ENABLE_TAIL_CALL != 0
  6135. case WASM_OP_RETURN_CALL_INDIRECT:
  6136. #endif
  6137. skip_leb_uint32(p, p_end); /* typeidx */
  6138. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6139. u8 = read_uint8(p); /* 0x00 */
  6140. #else
  6141. skip_leb_uint32(p, p_end); /* talbeidx */
  6142. #endif
  6143. break;
  6144. #if WASM_ENABLE_GC != 0
  6145. case WASM_OP_CALL_REF:
  6146. case WASM_OP_RETURN_CALL_REF:
  6147. skip_leb_uint32(p, p_end); /* typeidx */
  6148. break;
  6149. #endif
  6150. case WASM_OP_DROP:
  6151. case WASM_OP_SELECT:
  6152. case WASM_OP_DROP_64:
  6153. case WASM_OP_SELECT_64:
  6154. break;
  6155. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6156. case WASM_OP_SELECT_T:
  6157. {
  6158. skip_leb_uint32(p, p_end); /* vec length */
  6159. u8 = read_uint8(p); /* typeidx */
  6160. /* the possible extra bytes of GC ref type have been
  6161. modified to OP_NOP, no need to resolve them again */
  6162. break;
  6163. }
  6164. case WASM_OP_TABLE_GET:
  6165. case WASM_OP_TABLE_SET:
  6166. skip_leb_uint32(p, p_end); /* table index */
  6167. break;
  6168. case WASM_OP_REF_NULL:
  6169. {
  6170. u8 = read_uint8(p); /* type */
  6171. if (is_byte_a_type(u8)) {
  6172. #if WASM_ENABLE_GC != 0
  6173. if (wasm_is_type_multi_byte_type(u8)) {
  6174. /* the possible extra bytes of GC ref type have been
  6175. modified to OP_NOP, no need to resolve them again */
  6176. }
  6177. #endif
  6178. }
  6179. else {
  6180. p--;
  6181. skip_leb_uint32(p, p_end);
  6182. }
  6183. break;
  6184. }
  6185. case WASM_OP_REF_IS_NULL:
  6186. break;
  6187. case WASM_OP_REF_FUNC:
  6188. skip_leb_uint32(p, p_end); /* func index */
  6189. break;
  6190. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6191. #if WASM_ENABLE_GC != 0
  6192. case WASM_OP_REF_AS_NON_NULL:
  6193. case WASM_OP_REF_EQ:
  6194. break;
  6195. case WASM_OP_BR_ON_NULL:
  6196. case WASM_OP_BR_ON_NON_NULL:
  6197. skip_leb_uint32(p, p_end); /* label index */
  6198. break;
  6199. #endif /* end of WASM_ENABLE_GC != 0 */
  6200. case WASM_OP_GET_LOCAL:
  6201. case WASM_OP_SET_LOCAL:
  6202. case WASM_OP_TEE_LOCAL:
  6203. case WASM_OP_GET_GLOBAL:
  6204. case WASM_OP_SET_GLOBAL:
  6205. case WASM_OP_GET_GLOBAL_64:
  6206. case WASM_OP_SET_GLOBAL_64:
  6207. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6208. skip_leb_uint32(p, p_end); /* local index */
  6209. break;
  6210. case EXT_OP_GET_LOCAL_FAST:
  6211. case EXT_OP_SET_LOCAL_FAST:
  6212. case EXT_OP_TEE_LOCAL_FAST:
  6213. CHECK_BUF(p, p_end, 1);
  6214. p++;
  6215. break;
  6216. case WASM_OP_I32_LOAD:
  6217. case WASM_OP_I64_LOAD:
  6218. case WASM_OP_F32_LOAD:
  6219. case WASM_OP_F64_LOAD:
  6220. case WASM_OP_I32_LOAD8_S:
  6221. case WASM_OP_I32_LOAD8_U:
  6222. case WASM_OP_I32_LOAD16_S:
  6223. case WASM_OP_I32_LOAD16_U:
  6224. case WASM_OP_I64_LOAD8_S:
  6225. case WASM_OP_I64_LOAD8_U:
  6226. case WASM_OP_I64_LOAD16_S:
  6227. case WASM_OP_I64_LOAD16_U:
  6228. case WASM_OP_I64_LOAD32_S:
  6229. case WASM_OP_I64_LOAD32_U:
  6230. case WASM_OP_I32_STORE:
  6231. case WASM_OP_I64_STORE:
  6232. case WASM_OP_F32_STORE:
  6233. case WASM_OP_F64_STORE:
  6234. case WASM_OP_I32_STORE8:
  6235. case WASM_OP_I32_STORE16:
  6236. case WASM_OP_I64_STORE8:
  6237. case WASM_OP_I64_STORE16:
  6238. case WASM_OP_I64_STORE32:
  6239. skip_leb_uint32(p, p_end); /* align */
  6240. skip_leb_uint32(p, p_end); /* offset */
  6241. break;
  6242. case WASM_OP_MEMORY_SIZE:
  6243. case WASM_OP_MEMORY_GROW:
  6244. skip_leb_uint32(p, p_end); /* 0x00 */
  6245. break;
  6246. case WASM_OP_I32_CONST:
  6247. skip_leb_int32(p, p_end);
  6248. break;
  6249. case WASM_OP_I64_CONST:
  6250. skip_leb_int64(p, p_end);
  6251. break;
  6252. case WASM_OP_F32_CONST:
  6253. p += sizeof(float32);
  6254. break;
  6255. case WASM_OP_F64_CONST:
  6256. p += sizeof(float64);
  6257. break;
  6258. case WASM_OP_I32_EQZ:
  6259. case WASM_OP_I32_EQ:
  6260. case WASM_OP_I32_NE:
  6261. case WASM_OP_I32_LT_S:
  6262. case WASM_OP_I32_LT_U:
  6263. case WASM_OP_I32_GT_S:
  6264. case WASM_OP_I32_GT_U:
  6265. case WASM_OP_I32_LE_S:
  6266. case WASM_OP_I32_LE_U:
  6267. case WASM_OP_I32_GE_S:
  6268. case WASM_OP_I32_GE_U:
  6269. case WASM_OP_I64_EQZ:
  6270. case WASM_OP_I64_EQ:
  6271. case WASM_OP_I64_NE:
  6272. case WASM_OP_I64_LT_S:
  6273. case WASM_OP_I64_LT_U:
  6274. case WASM_OP_I64_GT_S:
  6275. case WASM_OP_I64_GT_U:
  6276. case WASM_OP_I64_LE_S:
  6277. case WASM_OP_I64_LE_U:
  6278. case WASM_OP_I64_GE_S:
  6279. case WASM_OP_I64_GE_U:
  6280. case WASM_OP_F32_EQ:
  6281. case WASM_OP_F32_NE:
  6282. case WASM_OP_F32_LT:
  6283. case WASM_OP_F32_GT:
  6284. case WASM_OP_F32_LE:
  6285. case WASM_OP_F32_GE:
  6286. case WASM_OP_F64_EQ:
  6287. case WASM_OP_F64_NE:
  6288. case WASM_OP_F64_LT:
  6289. case WASM_OP_F64_GT:
  6290. case WASM_OP_F64_LE:
  6291. case WASM_OP_F64_GE:
  6292. case WASM_OP_I32_CLZ:
  6293. case WASM_OP_I32_CTZ:
  6294. case WASM_OP_I32_POPCNT:
  6295. case WASM_OP_I32_ADD:
  6296. case WASM_OP_I32_SUB:
  6297. case WASM_OP_I32_MUL:
  6298. case WASM_OP_I32_DIV_S:
  6299. case WASM_OP_I32_DIV_U:
  6300. case WASM_OP_I32_REM_S:
  6301. case WASM_OP_I32_REM_U:
  6302. case WASM_OP_I32_AND:
  6303. case WASM_OP_I32_OR:
  6304. case WASM_OP_I32_XOR:
  6305. case WASM_OP_I32_SHL:
  6306. case WASM_OP_I32_SHR_S:
  6307. case WASM_OP_I32_SHR_U:
  6308. case WASM_OP_I32_ROTL:
  6309. case WASM_OP_I32_ROTR:
  6310. case WASM_OP_I64_CLZ:
  6311. case WASM_OP_I64_CTZ:
  6312. case WASM_OP_I64_POPCNT:
  6313. case WASM_OP_I64_ADD:
  6314. case WASM_OP_I64_SUB:
  6315. case WASM_OP_I64_MUL:
  6316. case WASM_OP_I64_DIV_S:
  6317. case WASM_OP_I64_DIV_U:
  6318. case WASM_OP_I64_REM_S:
  6319. case WASM_OP_I64_REM_U:
  6320. case WASM_OP_I64_AND:
  6321. case WASM_OP_I64_OR:
  6322. case WASM_OP_I64_XOR:
  6323. case WASM_OP_I64_SHL:
  6324. case WASM_OP_I64_SHR_S:
  6325. case WASM_OP_I64_SHR_U:
  6326. case WASM_OP_I64_ROTL:
  6327. case WASM_OP_I64_ROTR:
  6328. case WASM_OP_F32_ABS:
  6329. case WASM_OP_F32_NEG:
  6330. case WASM_OP_F32_CEIL:
  6331. case WASM_OP_F32_FLOOR:
  6332. case WASM_OP_F32_TRUNC:
  6333. case WASM_OP_F32_NEAREST:
  6334. case WASM_OP_F32_SQRT:
  6335. case WASM_OP_F32_ADD:
  6336. case WASM_OP_F32_SUB:
  6337. case WASM_OP_F32_MUL:
  6338. case WASM_OP_F32_DIV:
  6339. case WASM_OP_F32_MIN:
  6340. case WASM_OP_F32_MAX:
  6341. case WASM_OP_F32_COPYSIGN:
  6342. case WASM_OP_F64_ABS:
  6343. case WASM_OP_F64_NEG:
  6344. case WASM_OP_F64_CEIL:
  6345. case WASM_OP_F64_FLOOR:
  6346. case WASM_OP_F64_TRUNC:
  6347. case WASM_OP_F64_NEAREST:
  6348. case WASM_OP_F64_SQRT:
  6349. case WASM_OP_F64_ADD:
  6350. case WASM_OP_F64_SUB:
  6351. case WASM_OP_F64_MUL:
  6352. case WASM_OP_F64_DIV:
  6353. case WASM_OP_F64_MIN:
  6354. case WASM_OP_F64_MAX:
  6355. case WASM_OP_F64_COPYSIGN:
  6356. case WASM_OP_I32_WRAP_I64:
  6357. case WASM_OP_I32_TRUNC_S_F32:
  6358. case WASM_OP_I32_TRUNC_U_F32:
  6359. case WASM_OP_I32_TRUNC_S_F64:
  6360. case WASM_OP_I32_TRUNC_U_F64:
  6361. case WASM_OP_I64_EXTEND_S_I32:
  6362. case WASM_OP_I64_EXTEND_U_I32:
  6363. case WASM_OP_I64_TRUNC_S_F32:
  6364. case WASM_OP_I64_TRUNC_U_F32:
  6365. case WASM_OP_I64_TRUNC_S_F64:
  6366. case WASM_OP_I64_TRUNC_U_F64:
  6367. case WASM_OP_F32_CONVERT_S_I32:
  6368. case WASM_OP_F32_CONVERT_U_I32:
  6369. case WASM_OP_F32_CONVERT_S_I64:
  6370. case WASM_OP_F32_CONVERT_U_I64:
  6371. case WASM_OP_F32_DEMOTE_F64:
  6372. case WASM_OP_F64_CONVERT_S_I32:
  6373. case WASM_OP_F64_CONVERT_U_I32:
  6374. case WASM_OP_F64_CONVERT_S_I64:
  6375. case WASM_OP_F64_CONVERT_U_I64:
  6376. case WASM_OP_F64_PROMOTE_F32:
  6377. case WASM_OP_I32_REINTERPRET_F32:
  6378. case WASM_OP_I64_REINTERPRET_F64:
  6379. case WASM_OP_F32_REINTERPRET_I32:
  6380. case WASM_OP_F64_REINTERPRET_I64:
  6381. case WASM_OP_I32_EXTEND8_S:
  6382. case WASM_OP_I32_EXTEND16_S:
  6383. case WASM_OP_I64_EXTEND8_S:
  6384. case WASM_OP_I64_EXTEND16_S:
  6385. case WASM_OP_I64_EXTEND32_S:
  6386. break;
  6387. #if WASM_ENABLE_GC != 0
  6388. case WASM_OP_GC_PREFIX:
  6389. {
  6390. uint32 opcode1;
  6391. read_leb_uint32(p, p_end, opcode1);
  6392. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6393. is no larger than UINT8_MAX */
  6394. opcode = (uint8)opcode1;
  6395. switch (opcode) {
  6396. case WASM_OP_STRUCT_NEW:
  6397. case WASM_OP_STRUCT_NEW_DEFAULT:
  6398. skip_leb_uint32(p, p_end); /* typeidx */
  6399. break;
  6400. case WASM_OP_STRUCT_GET:
  6401. case WASM_OP_STRUCT_GET_S:
  6402. case WASM_OP_STRUCT_GET_U:
  6403. case WASM_OP_STRUCT_SET:
  6404. skip_leb_uint32(p, p_end); /* typeidx */
  6405. skip_leb_uint32(p, p_end); /* fieldidx */
  6406. break;
  6407. case WASM_OP_ARRAY_NEW:
  6408. case WASM_OP_ARRAY_NEW_DEFAULT:
  6409. case WASM_OP_ARRAY_GET:
  6410. case WASM_OP_ARRAY_GET_S:
  6411. case WASM_OP_ARRAY_GET_U:
  6412. case WASM_OP_ARRAY_SET:
  6413. case WASM_OP_ARRAY_FILL:
  6414. skip_leb_uint32(p, p_end); /* typeidx */
  6415. break;
  6416. case WASM_OP_ARRAY_COPY:
  6417. skip_leb_uint32(p, p_end); /* typeidx1 */
  6418. skip_leb_uint32(p, p_end); /* typeidx2 */
  6419. break;
  6420. case WASM_OP_ARRAY_LEN:
  6421. break;
  6422. case WASM_OP_ARRAY_NEW_FIXED:
  6423. case WASM_OP_ARRAY_NEW_DATA:
  6424. case WASM_OP_ARRAY_NEW_ELEM:
  6425. skip_leb_uint32(p, p_end); /* typeidx */
  6426. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6427. break;
  6428. case WASM_OP_REF_I31:
  6429. case WASM_OP_I31_GET_S:
  6430. case WASM_OP_I31_GET_U:
  6431. break;
  6432. case WASM_OP_REF_TEST:
  6433. case WASM_OP_REF_CAST:
  6434. case WASM_OP_REF_TEST_NULLABLE:
  6435. case WASM_OP_REF_CAST_NULLABLE:
  6436. skip_leb_int32(p, p_end); /* heaptype */
  6437. break;
  6438. case WASM_OP_BR_ON_CAST:
  6439. case WASM_OP_BR_ON_CAST_FAIL:
  6440. p += sizeof(uint8); /* castflag */
  6441. skip_leb_uint32(p, p_end); /* labelidx */
  6442. skip_leb_int32(p, p_end); /* heaptype */
  6443. skip_leb_int32(p, p_end); /* heaptype2 */
  6444. break;
  6445. case WASM_OP_ANY_CONVERT_EXTERN:
  6446. case WASM_OP_EXTERN_CONVERT_ANY:
  6447. break;
  6448. #if WASM_ENABLE_STRINGREF != 0
  6449. case WASM_OP_STRING_NEW_UTF8:
  6450. case WASM_OP_STRING_NEW_WTF16:
  6451. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6452. case WASM_OP_STRING_NEW_WTF8:
  6453. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6454. break;
  6455. case WASM_OP_STRING_CONST:
  6456. skip_leb_int32(p, p_end); /* contents */
  6457. break;
  6458. case WASM_OP_STRING_MEASURE_UTF8:
  6459. case WASM_OP_STRING_MEASURE_WTF8:
  6460. case WASM_OP_STRING_MEASURE_WTF16:
  6461. break;
  6462. case WASM_OP_STRING_ENCODE_UTF8:
  6463. case WASM_OP_STRING_ENCODE_WTF16:
  6464. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6465. case WASM_OP_STRING_ENCODE_WTF8:
  6466. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6467. break;
  6468. case WASM_OP_STRING_CONCAT:
  6469. case WASM_OP_STRING_EQ:
  6470. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6471. case WASM_OP_STRING_AS_WTF8:
  6472. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6473. break;
  6474. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6475. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6476. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6477. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6478. break;
  6479. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6480. case WASM_OP_STRING_AS_WTF16:
  6481. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6482. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6483. break;
  6484. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6485. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6486. break;
  6487. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6488. case WASM_OP_STRING_AS_ITER:
  6489. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6490. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6491. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6492. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6493. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6494. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6495. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6496. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6497. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6498. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6499. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6500. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6501. break;
  6502. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6503. default:
  6504. return false;
  6505. }
  6506. break;
  6507. }
  6508. #endif /* end of WASM_ENABLE_GC != 0 */
  6509. case WASM_OP_MISC_PREFIX:
  6510. {
  6511. uint32 opcode1;
  6512. read_leb_uint32(p, p_end, opcode1);
  6513. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6514. is no larger than UINT8_MAX */
  6515. opcode = (uint8)opcode1;
  6516. switch (opcode) {
  6517. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6518. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6519. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6520. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6521. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6522. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6523. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6524. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6525. break;
  6526. #if WASM_ENABLE_BULK_MEMORY != 0
  6527. case WASM_OP_MEMORY_INIT:
  6528. skip_leb_uint32(p, p_end);
  6529. /* skip memory idx */
  6530. p++;
  6531. break;
  6532. case WASM_OP_DATA_DROP:
  6533. skip_leb_uint32(p, p_end);
  6534. break;
  6535. case WASM_OP_MEMORY_COPY:
  6536. /* skip two memory idx */
  6537. p += 2;
  6538. break;
  6539. case WASM_OP_MEMORY_FILL:
  6540. /* skip memory idx */
  6541. p++;
  6542. break;
  6543. #endif /* WASM_ENABLE_BULK_MEMORY */
  6544. #if WASM_ENABLE_REF_TYPES != 0
  6545. case WASM_OP_TABLE_INIT:
  6546. case WASM_OP_TABLE_COPY:
  6547. /* tableidx */
  6548. skip_leb_uint32(p, p_end);
  6549. /* elemidx */
  6550. skip_leb_uint32(p, p_end);
  6551. break;
  6552. case WASM_OP_ELEM_DROP:
  6553. /* elemidx */
  6554. skip_leb_uint32(p, p_end);
  6555. break;
  6556. case WASM_OP_TABLE_SIZE:
  6557. case WASM_OP_TABLE_GROW:
  6558. case WASM_OP_TABLE_FILL:
  6559. skip_leb_uint32(p, p_end); /* table idx */
  6560. break;
  6561. #endif /* WASM_ENABLE_REF_TYPES */
  6562. default:
  6563. return false;
  6564. }
  6565. break;
  6566. }
  6567. #if WASM_ENABLE_SIMD != 0
  6568. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6569. case WASM_OP_SIMD_PREFIX:
  6570. {
  6571. uint32 opcode1;
  6572. read_leb_uint32(p, p_end, opcode1);
  6573. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6574. is no larger than UINT8_MAX */
  6575. opcode = (uint8)opcode1;
  6576. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6577. */
  6578. switch (opcode) {
  6579. case SIMD_v128_load:
  6580. case SIMD_v128_load8x8_s:
  6581. case SIMD_v128_load8x8_u:
  6582. case SIMD_v128_load16x4_s:
  6583. case SIMD_v128_load16x4_u:
  6584. case SIMD_v128_load32x2_s:
  6585. case SIMD_v128_load32x2_u:
  6586. case SIMD_v128_load8_splat:
  6587. case SIMD_v128_load16_splat:
  6588. case SIMD_v128_load32_splat:
  6589. case SIMD_v128_load64_splat:
  6590. case SIMD_v128_store:
  6591. /* memarg align */
  6592. skip_leb_uint32(p, p_end);
  6593. /* memarg offset*/
  6594. skip_leb_uint32(p, p_end);
  6595. break;
  6596. case SIMD_v128_const:
  6597. case SIMD_v8x16_shuffle:
  6598. /* immByte[16] immLaneId[16] */
  6599. CHECK_BUF1(p, p_end, 16);
  6600. p += 16;
  6601. break;
  6602. case SIMD_i8x16_extract_lane_s:
  6603. case SIMD_i8x16_extract_lane_u:
  6604. case SIMD_i8x16_replace_lane:
  6605. case SIMD_i16x8_extract_lane_s:
  6606. case SIMD_i16x8_extract_lane_u:
  6607. case SIMD_i16x8_replace_lane:
  6608. case SIMD_i32x4_extract_lane:
  6609. case SIMD_i32x4_replace_lane:
  6610. case SIMD_i64x2_extract_lane:
  6611. case SIMD_i64x2_replace_lane:
  6612. case SIMD_f32x4_extract_lane:
  6613. case SIMD_f32x4_replace_lane:
  6614. case SIMD_f64x2_extract_lane:
  6615. case SIMD_f64x2_replace_lane:
  6616. /* ImmLaneId */
  6617. CHECK_BUF(p, p_end, 1);
  6618. p++;
  6619. break;
  6620. case SIMD_v128_load8_lane:
  6621. case SIMD_v128_load16_lane:
  6622. case SIMD_v128_load32_lane:
  6623. case SIMD_v128_load64_lane:
  6624. case SIMD_v128_store8_lane:
  6625. case SIMD_v128_store16_lane:
  6626. case SIMD_v128_store32_lane:
  6627. case SIMD_v128_store64_lane:
  6628. /* memarg align */
  6629. skip_leb_uint32(p, p_end);
  6630. /* memarg offset*/
  6631. skip_leb_uint32(p, p_end);
  6632. /* ImmLaneId */
  6633. CHECK_BUF(p, p_end, 1);
  6634. p++;
  6635. break;
  6636. case SIMD_v128_load32_zero:
  6637. case SIMD_v128_load64_zero:
  6638. /* memarg align */
  6639. skip_leb_uint32(p, p_end);
  6640. /* memarg offset*/
  6641. skip_leb_uint32(p, p_end);
  6642. break;
  6643. default:
  6644. /*
  6645. * since latest SIMD specific used almost every value
  6646. * from 0x00 to 0xff, the default branch will present
  6647. * all opcodes without imm
  6648. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6649. */
  6650. break;
  6651. }
  6652. break;
  6653. }
  6654. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6655. #endif /* end of WASM_ENABLE_SIMD */
  6656. #if WASM_ENABLE_SHARED_MEMORY != 0
  6657. case WASM_OP_ATOMIC_PREFIX:
  6658. {
  6659. uint32 opcode1;
  6660. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6661. read_leb_uint32(p, p_end, opcode1);
  6662. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6663. is no larger than UINT8_MAX */
  6664. opcode = (uint8)opcode1;
  6665. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6666. skip_leb_uint32(p, p_end); /* align */
  6667. skip_leb_uint32(p, p_end); /* offset */
  6668. }
  6669. else {
  6670. /* atomic.fence doesn't have memarg */
  6671. p++;
  6672. }
  6673. break;
  6674. }
  6675. #endif
  6676. #if WASM_ENABLE_DEBUG_INTERP != 0
  6677. case DEBUG_OP_BREAK:
  6678. {
  6679. WASMDebugInstance *debug_instance =
  6680. wasm_exec_env_get_instance(exec_env);
  6681. char orignal_opcode[1];
  6682. uint64 size = 1;
  6683. WASMModuleInstance *module_inst =
  6684. (WASMModuleInstance *)exec_env->module_inst;
  6685. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6686. ? (p - 1) - module_inst->module->load_addr
  6687. : ~0;
  6688. if (debug_instance) {
  6689. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6690. orignal_opcode, &size)
  6691. && size == 1) {
  6692. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6693. "with %02x: ",
  6694. orignal_opcode[0]);
  6695. opcode = orignal_opcode[0];
  6696. goto op_break_retry;
  6697. }
  6698. }
  6699. break;
  6700. }
  6701. #endif
  6702. default:
  6703. return false;
  6704. }
  6705. }
  6706. (void)u8;
  6707. (void)exec_env;
  6708. return false;
  6709. fail:
  6710. return false;
  6711. }
  6712. #if WASM_ENABLE_FAST_INTERP != 0
  6713. #if WASM_DEBUG_PREPROCESSOR != 0
  6714. #define LOG_OP(...) os_printf(__VA_ARGS__)
  6715. #else
  6716. #define LOG_OP(...) (void)0
  6717. #endif
  6718. #define PATCH_ELSE 0
  6719. #define PATCH_END 1
  6720. typedef struct BranchBlockPatch {
  6721. struct BranchBlockPatch *next;
  6722. uint8 patch_type;
  6723. uint8 *code_compiled;
  6724. } BranchBlockPatch;
  6725. #endif
  6726. typedef struct BranchBlock {
  6727. uint8 label_type;
  6728. BlockType block_type;
  6729. uint8 *start_addr;
  6730. uint8 *else_addr;
  6731. uint8 *end_addr;
  6732. uint32 stack_cell_num;
  6733. #if WASM_ENABLE_GC != 0
  6734. uint32 reftype_map_num;
  6735. /* Indicate which local is used inside current block, used to validate
  6736. * local.get with non-nullable ref types */
  6737. uint8 *local_use_mask;
  6738. uint32 local_use_mask_size;
  6739. #endif
  6740. #if WASM_ENABLE_FAST_INTERP != 0
  6741. uint16 dynamic_offset;
  6742. uint8 *code_compiled;
  6743. BranchBlockPatch *patch_list;
  6744. /* This is used to save params frame_offset of of if block */
  6745. int16 *param_frame_offsets;
  6746. /* This is used to recover dynamic offset for else branch */
  6747. uint16 start_dynamic_offset;
  6748. #endif
  6749. /* Indicate the operand stack is in polymorphic state.
  6750. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  6751. * to polymorphic state until the block's 'end' opcode is processed.
  6752. * If stack is in polymorphic state and stack is empty, instruction can
  6753. * pop any type of value directly without decreasing stack top pointer
  6754. * and stack cell num. */
  6755. bool is_stack_polymorphic;
  6756. } BranchBlock;
  6757. typedef struct WASMLoaderContext {
  6758. /* frame ref stack */
  6759. uint8 *frame_ref;
  6760. uint8 *frame_ref_bottom;
  6761. uint8 *frame_ref_boundary;
  6762. uint32 frame_ref_size;
  6763. uint32 stack_cell_num;
  6764. uint32 max_stack_cell_num;
  6765. #if WASM_ENABLE_GC != 0
  6766. /* frame reftype map stack */
  6767. WASMRefTypeMap *frame_reftype_map;
  6768. WASMRefTypeMap *frame_reftype_map_bottom;
  6769. WASMRefTypeMap *frame_reftype_map_boundary;
  6770. uint32 frame_reftype_map_size;
  6771. uint32 reftype_map_num;
  6772. uint32 max_reftype_map_num;
  6773. /* Current module */
  6774. WASMModule *module;
  6775. /* Current module's ref_type_set */
  6776. HashMap *ref_type_set;
  6777. /* Always point to local variable ref_type of
  6778. wasm_loader_prepare_bytecode */
  6779. WASMRefType *ref_type_tmp;
  6780. #endif
  6781. /* frame csp stack */
  6782. BranchBlock *frame_csp;
  6783. BranchBlock *frame_csp_bottom;
  6784. BranchBlock *frame_csp_boundary;
  6785. uint32 frame_csp_size;
  6786. uint32 csp_num;
  6787. uint32 max_csp_num;
  6788. #if WASM_ENABLE_FAST_INTERP != 0
  6789. /* frame offset stack */
  6790. int16 *frame_offset;
  6791. int16 *frame_offset_bottom;
  6792. int16 *frame_offset_boundary;
  6793. uint32 frame_offset_size;
  6794. int16 dynamic_offset;
  6795. int16 start_dynamic_offset;
  6796. int16 max_dynamic_offset;
  6797. /* preserved local offset */
  6798. int16 preserved_local_offset;
  6799. /* const buffer */
  6800. uint8 *const_buf;
  6801. uint16 num_const;
  6802. uint16 const_cell_num;
  6803. uint32 const_buf_size;
  6804. /* processed code */
  6805. uint8 *p_code_compiled;
  6806. uint8 *p_code_compiled_end;
  6807. uint32 code_compiled_size;
  6808. /* If the last opcode will be dropped, the peak memory usage will be larger
  6809. * than the final code_compiled_size, we record the peak size to ensure
  6810. * there will not be invalid memory access during second traverse */
  6811. uint32 code_compiled_peak_size;
  6812. #endif
  6813. } WASMLoaderContext;
  6814. typedef struct Const {
  6815. WASMValue value;
  6816. uint16 slot_index;
  6817. uint8 value_type;
  6818. } Const;
  6819. #define CHECK_CSP_PUSH() \
  6820. do { \
  6821. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  6822. MEM_REALLOC( \
  6823. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  6824. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  6825. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  6826. ctx->frame_csp_boundary = \
  6827. ctx->frame_csp_bottom \
  6828. + ctx->frame_csp_size / sizeof(BranchBlock); \
  6829. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  6830. } \
  6831. } while (0)
  6832. #define CHECK_CSP_POP() \
  6833. do { \
  6834. if (ctx->csp_num < 1) { \
  6835. set_error_buf(error_buf, error_buf_size, \
  6836. "type mismatch: " \
  6837. "expect data but block stack was empty"); \
  6838. goto fail; \
  6839. } \
  6840. } while (0)
  6841. #if WASM_ENABLE_FAST_INTERP != 0
  6842. static bool
  6843. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  6844. uint32 error_buf_size)
  6845. {
  6846. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  6847. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  6848. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  6849. ctx->frame_offset_size + 16);
  6850. ctx->frame_offset_size += 16;
  6851. ctx->frame_offset_boundary =
  6852. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  6853. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  6854. }
  6855. return true;
  6856. fail:
  6857. return false;
  6858. }
  6859. static bool
  6860. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  6861. {
  6862. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  6863. return false;
  6864. return true;
  6865. }
  6866. static void
  6867. free_label_patch_list(BranchBlock *frame_csp)
  6868. {
  6869. BranchBlockPatch *label_patch = frame_csp->patch_list;
  6870. BranchBlockPatch *next;
  6871. while (label_patch != NULL) {
  6872. next = label_patch->next;
  6873. wasm_runtime_free(label_patch);
  6874. label_patch = next;
  6875. }
  6876. frame_csp->patch_list = NULL;
  6877. }
  6878. static void
  6879. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  6880. {
  6881. BranchBlock *tmp_csp = frame_csp;
  6882. for (uint32 i = 0; i < csp_num; i++) {
  6883. free_label_patch_list(tmp_csp);
  6884. tmp_csp++;
  6885. }
  6886. }
  6887. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6888. #if WASM_ENABLE_GC != 0
  6889. static bool
  6890. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  6891. char *error_buf, uint32 error_buf_size)
  6892. {
  6893. BranchBlock *current_csp = ctx->frame_csp - 1;
  6894. uint32 local_mask_size;
  6895. if (local_count == 0) {
  6896. current_csp->local_use_mask_size = 0;
  6897. return true;
  6898. }
  6899. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  6900. * else branch, we don't need to allocate memory again */
  6901. if (!current_csp->local_use_mask) {
  6902. local_mask_size = (local_count + 7) / sizeof(uint8);
  6903. if (!(current_csp->local_use_mask =
  6904. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  6905. return false;
  6906. }
  6907. current_csp->local_use_mask_size = local_mask_size;
  6908. }
  6909. else {
  6910. local_mask_size = current_csp->local_use_mask_size;
  6911. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  6912. }
  6913. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  6914. /* For non-function blocks, inherit the use status from parent block */
  6915. BranchBlock *parent_csp = current_csp - 1;
  6916. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  6917. bh_assert(parent_csp->local_use_mask);
  6918. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  6919. parent_csp->local_use_mask, local_mask_size);
  6920. }
  6921. return true;
  6922. }
  6923. static void
  6924. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  6925. {
  6926. BranchBlock *current_csp = ctx->frame_csp - 1;
  6927. bh_assert(current_csp->local_use_mask
  6928. || current_csp->local_use_mask_size == 0);
  6929. if (current_csp->local_use_mask) {
  6930. wasm_runtime_free(current_csp->local_use_mask);
  6931. }
  6932. current_csp->local_use_mask = NULL;
  6933. current_csp->local_use_mask_size = 0;
  6934. }
  6935. static void
  6936. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  6937. {
  6938. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  6939. uint32 i;
  6940. for (i = 0; i < ctx->csp_num; i++) {
  6941. if (tmp_csp->local_use_mask) {
  6942. wasm_runtime_free(tmp_csp->local_use_mask);
  6943. tmp_csp->local_use_mask = NULL;
  6944. tmp_csp->local_use_mask_size = 0;
  6945. }
  6946. tmp_csp++;
  6947. }
  6948. }
  6949. static void
  6950. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  6951. {
  6952. BranchBlock *current_csp = ctx->frame_csp - 1;
  6953. uint32 byte_offset = index / sizeof(uint8);
  6954. uint32 bit_offset = index % sizeof(uint8);
  6955. bh_assert(byte_offset < current_csp->local_use_mask_size);
  6956. bh_assert(current_csp->local_use_mask);
  6957. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  6958. }
  6959. static bool
  6960. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  6961. {
  6962. BranchBlock *current_csp = ctx->frame_csp - 1;
  6963. uint32 byte_offset = index / sizeof(uint8);
  6964. uint32 bit_offset = index % sizeof(uint8);
  6965. bh_assert(byte_offset < current_csp->local_use_mask_size);
  6966. bh_assert(current_csp->local_use_mask);
  6967. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  6968. ? true
  6969. : false;
  6970. }
  6971. #endif /* end of WASM_ENABLE_GC != 0 */
  6972. static void
  6973. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  6974. {
  6975. if (ctx) {
  6976. if (ctx->frame_ref_bottom)
  6977. wasm_runtime_free(ctx->frame_ref_bottom);
  6978. #if WASM_ENABLE_GC != 0
  6979. if (ctx->frame_reftype_map_bottom)
  6980. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  6981. #endif
  6982. if (ctx->frame_csp_bottom) {
  6983. #if WASM_ENABLE_FAST_INTERP != 0
  6984. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  6985. #endif
  6986. #if WASM_ENABLE_GC != 0
  6987. wasm_loader_clean_all_local_use_masks(ctx);
  6988. #endif
  6989. wasm_runtime_free(ctx->frame_csp_bottom);
  6990. }
  6991. #if WASM_ENABLE_FAST_INTERP != 0
  6992. if (ctx->frame_offset_bottom)
  6993. wasm_runtime_free(ctx->frame_offset_bottom);
  6994. if (ctx->const_buf)
  6995. wasm_runtime_free(ctx->const_buf);
  6996. #endif
  6997. wasm_runtime_free(ctx);
  6998. }
  6999. }
  7000. static WASMLoaderContext *
  7001. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7002. {
  7003. WASMLoaderContext *loader_ctx =
  7004. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7005. if (!loader_ctx)
  7006. return NULL;
  7007. loader_ctx->frame_ref_size = 32;
  7008. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7009. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7010. goto fail;
  7011. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7012. #if WASM_ENABLE_GC != 0
  7013. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7014. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7015. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7016. error_buf_size)))
  7017. goto fail;
  7018. loader_ctx->frame_reftype_map_boundary =
  7019. loader_ctx->frame_reftype_map_bottom + 16;
  7020. #endif
  7021. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7022. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7023. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7024. goto fail;
  7025. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7026. #if WASM_ENABLE_EXCE_HANDLING != 0
  7027. func->exception_handler_count = 0;
  7028. #endif
  7029. #if WASM_ENABLE_FAST_INTERP != 0
  7030. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7031. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7032. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7033. error_buf_size)))
  7034. goto fail;
  7035. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7036. loader_ctx->num_const = 0;
  7037. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7038. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7039. error_buf, error_buf_size)))
  7040. goto fail;
  7041. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7042. set_error_buf(error_buf, error_buf_size,
  7043. "fast interpreter offset overflow");
  7044. goto fail;
  7045. }
  7046. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7047. loader_ctx->max_dynamic_offset =
  7048. func->param_cell_num + func->local_cell_num;
  7049. #endif
  7050. return loader_ctx;
  7051. fail:
  7052. wasm_loader_ctx_destroy(loader_ctx);
  7053. return NULL;
  7054. }
  7055. static bool
  7056. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7057. uint32 error_buf_size)
  7058. {
  7059. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7060. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7061. /* Increase the frame ref stack */
  7062. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7063. ctx->frame_ref_size + 16);
  7064. ctx->frame_ref_size += 16;
  7065. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7066. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7067. }
  7068. #if WASM_ENABLE_GC != 0
  7069. if (wasm_is_type_multi_byte_type(type)
  7070. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7071. /* Increase the frame reftype map stack */
  7072. bh_assert(
  7073. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7074. * sizeof(WASMRefTypeMap))
  7075. == ctx->frame_reftype_map_size);
  7076. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7077. ctx->frame_reftype_map_size
  7078. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7079. ctx->frame_reftype_map =
  7080. ctx->frame_reftype_map_bottom
  7081. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7082. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7083. ctx->frame_reftype_map_boundary =
  7084. ctx->frame_reftype_map_bottom
  7085. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7086. }
  7087. #endif
  7088. return true;
  7089. fail:
  7090. return false;
  7091. }
  7092. static bool
  7093. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7094. uint32 error_buf_size)
  7095. {
  7096. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7097. uint32 i;
  7098. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7099. return false;
  7100. #if WASM_ENABLE_GC != 0
  7101. if (wasm_is_type_multi_byte_type(type)) {
  7102. WASMRefType *ref_type;
  7103. if (!(ref_type =
  7104. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7105. error_buf, error_buf_size))) {
  7106. return false;
  7107. }
  7108. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7109. /* Increase the frame reftype map stack */
  7110. bh_assert((uint32)((ctx->frame_reftype_map
  7111. - ctx->frame_reftype_map_bottom)
  7112. * sizeof(WASMRefTypeMap))
  7113. == ctx->frame_reftype_map_size);
  7114. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7115. ctx->frame_reftype_map_size,
  7116. ctx->frame_reftype_map_size
  7117. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7118. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7119. + ctx->frame_reftype_map_size
  7120. / ((uint32)sizeof(WASMRefTypeMap));
  7121. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7122. ctx->frame_reftype_map_boundary =
  7123. ctx->frame_reftype_map_bottom
  7124. + ctx->frame_reftype_map_size
  7125. / ((uint32)sizeof(WASMRefTypeMap));
  7126. }
  7127. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7128. ctx->frame_reftype_map->ref_type = ref_type;
  7129. ctx->frame_reftype_map++;
  7130. ctx->reftype_map_num++;
  7131. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7132. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7133. }
  7134. #endif
  7135. for (i = 0; i < type_cell_num; i++)
  7136. *ctx->frame_ref++ = type;
  7137. ctx->stack_cell_num += type_cell_num;
  7138. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7139. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7140. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7141. set_error_buf(error_buf, error_buf_size,
  7142. "operand stack depth limit exceeded");
  7143. return false;
  7144. }
  7145. }
  7146. return true;
  7147. #if WASM_ENABLE_GC != 0
  7148. fail:
  7149. return false;
  7150. #endif
  7151. }
  7152. static bool
  7153. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7154. int32 stack_cell_num,
  7155. #if WASM_ENABLE_GC != 0
  7156. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7157. #endif
  7158. uint8 type,
  7159. #if WASM_ENABLE_GC != 0
  7160. WASMRefType *ref_type,
  7161. #endif
  7162. char *error_buf, uint32 error_buf_size)
  7163. {
  7164. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7165. #if WASM_ENABLE_GC != 0
  7166. WASMRefType *frame_reftype = NULL;
  7167. #endif
  7168. if (stack_cell_num < type_cell_num) {
  7169. set_error_buf(error_buf, error_buf_size,
  7170. "type mismatch: expect data but stack was empty");
  7171. return false;
  7172. }
  7173. #if WASM_ENABLE_GC == 0
  7174. for (i = 0; i < type_cell_num; i++) {
  7175. if (*(frame_ref - 1 - i) != type) {
  7176. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7177. "type mismatch: expect ", type2str(type),
  7178. " but got other");
  7179. return false;
  7180. }
  7181. }
  7182. #else
  7183. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7184. bh_assert(reftype_map_num > 0);
  7185. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7186. }
  7187. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7188. ref_type, ctx->module->types,
  7189. ctx->module->type_count)) {
  7190. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7191. "type mismatch: expect ", type2str(type),
  7192. " but got other");
  7193. return false;
  7194. }
  7195. for (i = 0; i < type_cell_num - 1; i++) {
  7196. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7197. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7198. "type mismatch: expect ", type2str(type),
  7199. " but got other");
  7200. return false;
  7201. }
  7202. }
  7203. #endif
  7204. return true;
  7205. }
  7206. static bool
  7207. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7208. uint32 error_buf_size)
  7209. {
  7210. int32 block_stack_cell_num =
  7211. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7212. #if WASM_ENABLE_GC != 0
  7213. int32 reftype_map_num =
  7214. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7215. #endif
  7216. if (block_stack_cell_num > 0) {
  7217. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7218. /* the stack top is a value of any type, return success */
  7219. return true;
  7220. }
  7221. #if WASM_ENABLE_GC != 0
  7222. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7223. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7224. WASMRefType *stack_top_ref_type = NULL;
  7225. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7226. bh_assert(reftype_map_num > 0);
  7227. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7228. }
  7229. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7230. ctx->ref_type_tmp, ctx->module->types,
  7231. ctx->module->type_count)) {
  7232. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7233. uint32 ref_type_struct_size =
  7234. wasm_reftype_struct_size(stack_top_ref_type);
  7235. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7236. stack_top_ref_type, ref_type_struct_size);
  7237. }
  7238. return true;
  7239. }
  7240. }
  7241. #endif
  7242. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7243. #if WASM_ENABLE_GC != 0
  7244. ctx->frame_reftype_map, reftype_map_num,
  7245. #endif
  7246. type,
  7247. #if WASM_ENABLE_GC != 0
  7248. ctx->ref_type_tmp,
  7249. #endif
  7250. error_buf, error_buf_size)) {
  7251. return false;
  7252. }
  7253. return true;
  7254. }
  7255. static bool
  7256. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7257. uint32 error_buf_size)
  7258. {
  7259. BranchBlock *cur_block = ctx->frame_csp - 1;
  7260. int32 available_stack_cell =
  7261. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7262. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7263. /* Directly return success if current block is in stack
  7264. polymorphic state while stack is empty. */
  7265. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7266. return true;
  7267. if (type == VALUE_TYPE_VOID)
  7268. return true;
  7269. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7270. return false;
  7271. bh_assert(available_stack_cell > 0);
  7272. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7273. type = VALUE_TYPE_ANY;
  7274. cell_num_to_pop = 1;
  7275. }
  7276. ctx->frame_ref -= cell_num_to_pop;
  7277. ctx->stack_cell_num -= cell_num_to_pop;
  7278. #if WASM_ENABLE_GC != 0
  7279. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7280. ctx->frame_reftype_map--;
  7281. ctx->reftype_map_num--;
  7282. }
  7283. #endif
  7284. return true;
  7285. }
  7286. #if WASM_ENABLE_GC != 0
  7287. /* Get the stack top element of current block */
  7288. static bool
  7289. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7290. WASMRefType **p_ref_type, char *error_buf,
  7291. uint32 error_buf_size)
  7292. {
  7293. BranchBlock *cur_block = ctx->frame_csp - 1;
  7294. int32 available_stack_cell =
  7295. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7296. if (available_stack_cell <= 0) {
  7297. /* Directly return success if current block is in stack
  7298. polymorphic state while stack is empty. */
  7299. if (cur_block->is_stack_polymorphic) {
  7300. *p_type = VALUE_TYPE_ANY;
  7301. return true;
  7302. }
  7303. else {
  7304. set_error_buf(
  7305. error_buf, error_buf_size,
  7306. "type mismatch: expect data but block stack was empty");
  7307. return false;
  7308. }
  7309. }
  7310. *p_type = *(ctx->frame_ref - 1);
  7311. if (wasm_is_type_multi_byte_type(*p_type)) {
  7312. int32 available_reftype_map =
  7313. (int32)(ctx->reftype_map_num
  7314. - (ctx->frame_csp - 1)->reftype_map_num);
  7315. bh_assert(available_reftype_map > 0);
  7316. (void)available_reftype_map;
  7317. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7318. }
  7319. return true;
  7320. }
  7321. #if WASM_ENABLE_FAST_INTERP != 0
  7322. static bool
  7323. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7324. char *error_buf, uint32 error_buf_size);
  7325. #endif
  7326. /* Check whether the stack top elem is a heap object, and if yes,
  7327. pop and return it */
  7328. static bool
  7329. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7330. WASMRefType *ref_ht_ret, char *error_buf,
  7331. uint32 error_buf_size)
  7332. {
  7333. uint8 type = 0;
  7334. WASMRefType *ref_type = NULL;
  7335. /* Get stack top element */
  7336. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7337. error_buf_size)) {
  7338. return false;
  7339. }
  7340. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7341. /* stack top isn't a ref type */
  7342. && !wasm_is_type_reftype(type)) {
  7343. set_error_buf(error_buf, error_buf_size,
  7344. "type mismatch: expect heap object but got others");
  7345. return false;
  7346. }
  7347. /* POP stack top */
  7348. if (wasm_is_type_multi_byte_type(type)) {
  7349. bh_assert(ref_type);
  7350. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7351. wasm_reftype_struct_size(ref_type));
  7352. }
  7353. #if WASM_ENABLE_FAST_INTERP != 0
  7354. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7355. error_buf_size)) {
  7356. return false;
  7357. }
  7358. #else
  7359. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7360. return false;
  7361. }
  7362. #endif
  7363. if (p_type)
  7364. *p_type = type;
  7365. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7366. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7367. wasm_reftype_struct_size(ref_type));
  7368. }
  7369. return true;
  7370. }
  7371. /* Check whether the stack top elem is subtype of (ref null ht),
  7372. and if yes, pop it and return the converted (ref ht) */
  7373. static bool
  7374. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7375. WASMRefType *ref_ht_ret, char *error_buf,
  7376. uint32 error_buf_size)
  7377. {
  7378. uint8 type = 0;
  7379. WASMRefType ref_type = { 0 };
  7380. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7381. error_buf_size)) {
  7382. return false;
  7383. }
  7384. /* Convert to related (ref ht) and return */
  7385. if ((type >= REF_TYPE_EQREF && type <= REF_TYPE_FUNCREF)
  7386. || (type >= REF_TYPE_NULLREF && type <= REF_TYPE_I31REF)) {
  7387. /* Return (ref func/extern/any/eq/i31/nofunc/noextern/struct/array/none)
  7388. */
  7389. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7390. HEAP_TYPE_FUNC + (type - REF_TYPE_FUNCREF));
  7391. type = ref_ht_ret->ref_type;
  7392. }
  7393. else if (wasm_is_reftype_htref_nullable(type)
  7394. || wasm_is_reftype_htref_non_nullable(type)) {
  7395. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7396. wasm_reftype_struct_size(&ref_type));
  7397. /* Convert to (ref ht) */
  7398. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7399. ref_ht_ret->ref_ht_common.nullable = false;
  7400. type = ref_ht_ret->ref_type;
  7401. }
  7402. *p_type = type;
  7403. return true;
  7404. }
  7405. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7406. and if yes, pop it and return the type_idx */
  7407. static bool
  7408. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7409. uint32 *p_type_idx, char *error_buf,
  7410. uint32 error_buf_size)
  7411. {
  7412. uint8 type = 0;
  7413. int32 type_idx = -1;
  7414. WASMRefType *ref_type = NULL;
  7415. /* Get stack top element */
  7416. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7417. error_buf_size)) {
  7418. return false;
  7419. }
  7420. if (type != VALUE_TYPE_ANY) {
  7421. /* stack top isn't (ref null $t) */
  7422. if (!((wasm_is_reftype_htref_nullable(type)
  7423. || wasm_is_reftype_htref_non_nullable(type))
  7424. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7425. set_error_buf(error_buf, error_buf_size,
  7426. "type mismatch: expect (ref null $t) but got others");
  7427. return false;
  7428. }
  7429. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7430. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7431. wasm_reftype_struct_size(ref_type));
  7432. }
  7433. /* POP stack top */
  7434. #if WASM_ENABLE_FAST_INTERP != 0
  7435. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7436. error_buf_size)) {
  7437. return false;
  7438. }
  7439. #else
  7440. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7441. return false;
  7442. }
  7443. #endif
  7444. /* Convert to type_idx and return */
  7445. *p_type = type;
  7446. if (type != VALUE_TYPE_ANY)
  7447. *p_type_idx = (uint32)type_idx;
  7448. return true;
  7449. }
  7450. #endif /* WASM_ENABLE_GC != 0 */
  7451. #if WASM_ENABLE_FAST_INTERP == 0
  7452. static bool
  7453. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7454. uint8 type_push, uint8 type_pop, char *error_buf,
  7455. uint32 error_buf_size)
  7456. {
  7457. for (int i = 0; i < pop_cnt; i++) {
  7458. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7459. error_buf_size))
  7460. return false;
  7461. }
  7462. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7463. return false;
  7464. return true;
  7465. }
  7466. #endif
  7467. static bool
  7468. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7469. BlockType block_type, uint8 *start_addr,
  7470. char *error_buf, uint32 error_buf_size)
  7471. {
  7472. CHECK_CSP_PUSH();
  7473. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7474. ctx->frame_csp->label_type = label_type;
  7475. ctx->frame_csp->block_type = block_type;
  7476. ctx->frame_csp->start_addr = start_addr;
  7477. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7478. #if WASM_ENABLE_GC != 0
  7479. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7480. #endif
  7481. #if WASM_ENABLE_FAST_INTERP != 0
  7482. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7483. ctx->frame_csp->patch_list = NULL;
  7484. #endif
  7485. ctx->frame_csp++;
  7486. ctx->csp_num++;
  7487. if (ctx->csp_num > ctx->max_csp_num) {
  7488. ctx->max_csp_num = ctx->csp_num;
  7489. if (ctx->max_csp_num > UINT16_MAX) {
  7490. set_error_buf(error_buf, error_buf_size,
  7491. "label stack depth limit exceeded");
  7492. return false;
  7493. }
  7494. }
  7495. return true;
  7496. fail:
  7497. return false;
  7498. }
  7499. static bool
  7500. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7501. uint32 error_buf_size)
  7502. {
  7503. CHECK_CSP_POP();
  7504. #if WASM_ENABLE_FAST_INTERP != 0
  7505. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7506. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7507. #endif
  7508. ctx->frame_csp--;
  7509. ctx->csp_num--;
  7510. return true;
  7511. fail:
  7512. return false;
  7513. }
  7514. #if WASM_ENABLE_FAST_INTERP != 0
  7515. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7516. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7517. #define emit_label(opcode) \
  7518. do { \
  7519. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7520. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7521. } while (0)
  7522. #define skip_label() \
  7523. do { \
  7524. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7525. LOG_OP("\ndelete last op\n"); \
  7526. } while (0)
  7527. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7528. #if UINTPTR_MAX == UINT64_MAX
  7529. #define emit_label(opcode) \
  7530. do { \
  7531. int32 offset = \
  7532. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7533. /* emit int32 relative offset in 64-bit target */ \
  7534. wasm_loader_emit_uint32(loader_ctx, offset); \
  7535. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7536. } while (0)
  7537. #else
  7538. #define emit_label(opcode) \
  7539. do { \
  7540. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7541. /* emit uint32 label address in 32-bit target */ \
  7542. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7543. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7544. } while (0)
  7545. #endif
  7546. #define skip_label() \
  7547. do { \
  7548. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7549. LOG_OP("\ndelete last op\n"); \
  7550. } while (0)
  7551. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7552. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7553. #define emit_label(opcode) \
  7554. do { \
  7555. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7556. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7557. } while (0)
  7558. #define skip_label() \
  7559. do { \
  7560. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7561. LOG_OP("\ndelete last op\n"); \
  7562. } while (0)
  7563. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7564. #define emit_empty_label_addr_and_frame_ip(type) \
  7565. do { \
  7566. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7567. loader_ctx->p_code_compiled, error_buf, \
  7568. error_buf_size)) \
  7569. goto fail; \
  7570. /* label address, to be patched */ \
  7571. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7572. } while (0)
  7573. #define emit_br_info(frame_csp) \
  7574. do { \
  7575. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  7576. error_buf_size)) \
  7577. goto fail; \
  7578. } while (0)
  7579. #define LAST_OP_OUTPUT_I32() \
  7580. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7581. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7582. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7583. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7584. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7585. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7586. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7587. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7588. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7589. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7590. || (last_op == EXT_OP_COPY_STACK_TOP)
  7591. #define LAST_OP_OUTPUT_I64() \
  7592. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7593. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7594. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7595. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7596. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7597. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7598. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7599. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7600. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7601. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7602. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7603. #define GET_CONST_OFFSET(type, val) \
  7604. do { \
  7605. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7606. &operand_offset, error_buf, \
  7607. error_buf_size))) \
  7608. goto fail; \
  7609. } while (0)
  7610. #define GET_CONST_F32_OFFSET(type, fval) \
  7611. do { \
  7612. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7613. &operand_offset, error_buf, \
  7614. error_buf_size))) \
  7615. goto fail; \
  7616. } while (0)
  7617. #define GET_CONST_F64_OFFSET(type, fval) \
  7618. do { \
  7619. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7620. &operand_offset, error_buf, \
  7621. error_buf_size))) \
  7622. goto fail; \
  7623. } while (0)
  7624. #define emit_operand(ctx, offset) \
  7625. do { \
  7626. wasm_loader_emit_int16(ctx, offset); \
  7627. LOG_OP("%d\t", offset); \
  7628. } while (0)
  7629. #define emit_byte(ctx, byte) \
  7630. do { \
  7631. wasm_loader_emit_uint8(ctx, byte); \
  7632. LOG_OP("%d\t", byte); \
  7633. } while (0)
  7634. #define emit_uint32(ctx, value) \
  7635. do { \
  7636. wasm_loader_emit_uint32(ctx, value); \
  7637. LOG_OP("%d\t", value); \
  7638. } while (0)
  7639. #define emit_uint64(ctx, value) \
  7640. do { \
  7641. wasm_loader_emit_const(ctx, &value, false); \
  7642. LOG_OP("%lld\t", value); \
  7643. } while (0)
  7644. #define emit_float32(ctx, value) \
  7645. do { \
  7646. wasm_loader_emit_const(ctx, &value, true); \
  7647. LOG_OP("%f\t", value); \
  7648. } while (0)
  7649. #define emit_float64(ctx, value) \
  7650. do { \
  7651. wasm_loader_emit_const(ctx, &value, false); \
  7652. LOG_OP("%f\t", value); \
  7653. } while (0)
  7654. static bool
  7655. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7656. {
  7657. if (!(ctx->p_code_compiled =
  7658. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7659. return false;
  7660. ctx->p_code_compiled_end =
  7661. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7662. /* clean up frame ref */
  7663. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7664. ctx->frame_ref = ctx->frame_ref_bottom;
  7665. ctx->stack_cell_num = 0;
  7666. #if WASM_ENABLE_GC != 0
  7667. /* clean up reftype map */
  7668. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7669. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7670. ctx->reftype_map_num = 0;
  7671. #endif
  7672. /* clean up frame csp */
  7673. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7674. ctx->frame_csp = ctx->frame_csp_bottom;
  7675. ctx->csp_num = 0;
  7676. ctx->max_csp_num = 0;
  7677. /* clean up frame offset */
  7678. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7679. ctx->frame_offset = ctx->frame_offset_bottom;
  7680. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7681. /* init preserved local offsets */
  7682. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7683. /* const buf is reserved */
  7684. return true;
  7685. }
  7686. static void
  7687. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  7688. {
  7689. ctx->code_compiled_size += size;
  7690. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  7691. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  7692. }
  7693. }
  7694. static void
  7695. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  7696. {
  7697. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  7698. if (ctx->p_code_compiled) {
  7699. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7700. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7701. #endif
  7702. bh_memcpy_s(ctx->p_code_compiled,
  7703. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  7704. value, size);
  7705. ctx->p_code_compiled += size;
  7706. }
  7707. else {
  7708. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7709. bh_assert((ctx->code_compiled_size & 1) == 0);
  7710. #endif
  7711. increase_compiled_code_space(ctx, size);
  7712. }
  7713. }
  7714. static void
  7715. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  7716. {
  7717. if (ctx->p_code_compiled) {
  7718. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7719. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7720. #endif
  7721. STORE_U32(ctx->p_code_compiled, value);
  7722. ctx->p_code_compiled += sizeof(uint32);
  7723. }
  7724. else {
  7725. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7726. bh_assert((ctx->code_compiled_size & 1) == 0);
  7727. #endif
  7728. increase_compiled_code_space(ctx, sizeof(uint32));
  7729. }
  7730. }
  7731. static void
  7732. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  7733. {
  7734. if (ctx->p_code_compiled) {
  7735. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7736. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7737. #endif
  7738. STORE_U16(ctx->p_code_compiled, (uint16)value);
  7739. ctx->p_code_compiled += sizeof(int16);
  7740. }
  7741. else {
  7742. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7743. bh_assert((ctx->code_compiled_size & 1) == 0);
  7744. #endif
  7745. increase_compiled_code_space(ctx, sizeof(uint16));
  7746. }
  7747. }
  7748. static void
  7749. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  7750. {
  7751. if (ctx->p_code_compiled) {
  7752. *(ctx->p_code_compiled) = value;
  7753. ctx->p_code_compiled += sizeof(uint8);
  7754. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7755. ctx->p_code_compiled++;
  7756. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7757. #endif
  7758. }
  7759. else {
  7760. increase_compiled_code_space(ctx, sizeof(uint8));
  7761. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7762. increase_compiled_code_space(ctx, sizeof(uint8));
  7763. bh_assert((ctx->code_compiled_size & 1) == 0);
  7764. #endif
  7765. }
  7766. }
  7767. static void
  7768. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  7769. {
  7770. if (ctx->p_code_compiled) {
  7771. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7772. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7773. #endif
  7774. STORE_PTR(ctx->p_code_compiled, value);
  7775. ctx->p_code_compiled += sizeof(void *);
  7776. }
  7777. else {
  7778. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7779. bh_assert((ctx->code_compiled_size & 1) == 0);
  7780. #endif
  7781. increase_compiled_code_space(ctx, sizeof(void *));
  7782. }
  7783. }
  7784. static void
  7785. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  7786. {
  7787. if (ctx->p_code_compiled) {
  7788. ctx->p_code_compiled -= size;
  7789. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7790. if (size == sizeof(uint8)) {
  7791. ctx->p_code_compiled--;
  7792. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7793. }
  7794. #endif
  7795. }
  7796. else {
  7797. ctx->code_compiled_size -= size;
  7798. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7799. if (size == sizeof(uint8)) {
  7800. ctx->code_compiled_size--;
  7801. bh_assert((ctx->code_compiled_size & 1) == 0);
  7802. }
  7803. #endif
  7804. }
  7805. }
  7806. static bool
  7807. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  7808. uint32 local_index, uint32 local_type,
  7809. bool *preserved, char *error_buf,
  7810. uint32 error_buf_size)
  7811. {
  7812. uint32 i = 0;
  7813. int16 preserved_offset = (int16)local_index;
  7814. *preserved = false;
  7815. while (i < loader_ctx->stack_cell_num) {
  7816. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  7817. /* move previous local into dynamic space before a set/tee_local opcode
  7818. */
  7819. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  7820. if (!(*preserved)) {
  7821. *preserved = true;
  7822. skip_label();
  7823. preserved_offset = loader_ctx->preserved_local_offset;
  7824. if (loader_ctx->p_code_compiled) {
  7825. bh_assert(preserved_offset != (int16)local_index);
  7826. }
  7827. if (is_32bit_type(local_type)) {
  7828. /* Only increase preserve offset in the second traversal */
  7829. if (loader_ctx->p_code_compiled)
  7830. loader_ctx->preserved_local_offset++;
  7831. emit_label(EXT_OP_COPY_STACK_TOP);
  7832. }
  7833. else {
  7834. if (loader_ctx->p_code_compiled)
  7835. loader_ctx->preserved_local_offset += 2;
  7836. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  7837. }
  7838. emit_operand(loader_ctx, local_index);
  7839. emit_operand(loader_ctx, preserved_offset);
  7840. emit_label(opcode);
  7841. }
  7842. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  7843. }
  7844. if (is_32bit_type(cur_type))
  7845. i++;
  7846. else
  7847. i += 2;
  7848. }
  7849. (void)error_buf;
  7850. (void)error_buf_size;
  7851. return true;
  7852. }
  7853. static bool
  7854. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  7855. char *error_buf, uint32 error_buf_size)
  7856. {
  7857. uint32 i = 0;
  7858. bool preserve_local;
  7859. /* preserve locals before blocks to ensure that "tee/set_local" inside
  7860. blocks will not influence the value of these locals */
  7861. while (i < loader_ctx->stack_cell_num) {
  7862. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  7863. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  7864. if ((cur_offset < loader_ctx->start_dynamic_offset)
  7865. && (cur_offset >= 0)) {
  7866. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  7867. cur_type, &preserve_local,
  7868. error_buf, error_buf_size)))
  7869. return false;
  7870. }
  7871. if (is_32bit_type(cur_type)) {
  7872. i++;
  7873. }
  7874. else {
  7875. i += 2;
  7876. }
  7877. }
  7878. return true;
  7879. }
  7880. static bool
  7881. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  7882. uint8 *p_code_compiled, char *error_buf,
  7883. uint32 error_buf_size)
  7884. {
  7885. BranchBlockPatch *patch =
  7886. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  7887. if (!patch) {
  7888. return false;
  7889. }
  7890. patch->patch_type = patch_type;
  7891. patch->code_compiled = p_code_compiled;
  7892. if (!frame_csp->patch_list) {
  7893. frame_csp->patch_list = patch;
  7894. patch->next = NULL;
  7895. }
  7896. else {
  7897. patch->next = frame_csp->patch_list;
  7898. frame_csp->patch_list = patch;
  7899. }
  7900. return true;
  7901. }
  7902. static void
  7903. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  7904. {
  7905. BranchBlock *frame_csp = ctx->frame_csp - depth;
  7906. BranchBlockPatch *node = frame_csp->patch_list;
  7907. BranchBlockPatch *node_prev = NULL, *node_next;
  7908. if (!ctx->p_code_compiled)
  7909. return;
  7910. while (node) {
  7911. node_next = node->next;
  7912. if (node->patch_type == patch_type) {
  7913. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  7914. if (node_prev == NULL) {
  7915. frame_csp->patch_list = node_next;
  7916. }
  7917. else {
  7918. node_prev->next = node_next;
  7919. }
  7920. wasm_runtime_free(node);
  7921. }
  7922. else {
  7923. node_prev = node;
  7924. }
  7925. node = node_next;
  7926. }
  7927. }
  7928. static bool
  7929. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  7930. char *error_buf, uint32 error_buf_size)
  7931. {
  7932. /* br info layout:
  7933. * a) arity of target block
  7934. * b) total cell num of arity values
  7935. * c) each arity value's cell num
  7936. * d) each arity value's src frame offset
  7937. * e) each arity values's dst dynamic offset
  7938. * f) branch target address
  7939. *
  7940. * Note: b-e are omitted when arity is 0 so that
  7941. * interpreter can recover the br info quickly.
  7942. */
  7943. BlockType *block_type = &frame_csp->block_type;
  7944. uint8 *types = NULL, cell;
  7945. #if WASM_ENABLE_GC != 0
  7946. WASMRefTypeMap *reftype_maps;
  7947. uint32 reftype_map_count;
  7948. #endif
  7949. uint32 arity = 0;
  7950. int32 i;
  7951. int16 *frame_offset = ctx->frame_offset;
  7952. uint16 dynamic_offset;
  7953. /* Note: loop's arity is different from if and block. loop's arity is
  7954. * its parameter count while if and block arity is result count.
  7955. */
  7956. #if WASM_ENABLE_GC == 0
  7957. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  7958. arity = block_type_get_param_types(block_type, &types);
  7959. else
  7960. arity = block_type_get_result_types(block_type, &types);
  7961. #else
  7962. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  7963. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  7964. &reftype_map_count);
  7965. else
  7966. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  7967. &reftype_map_count);
  7968. #endif
  7969. /* Part a */
  7970. emit_uint32(ctx, arity);
  7971. if (arity) {
  7972. /* Part b */
  7973. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  7974. /* Part c */
  7975. for (i = (int32)arity - 1; i >= 0; i--) {
  7976. cell = (uint8)wasm_value_type_cell_num(types[i]);
  7977. emit_byte(ctx, cell);
  7978. }
  7979. /* Part d */
  7980. for (i = (int32)arity - 1; i >= 0; i--) {
  7981. cell = (uint8)wasm_value_type_cell_num(types[i]);
  7982. frame_offset -= cell;
  7983. emit_operand(ctx, *(int16 *)(frame_offset));
  7984. }
  7985. /* Part e */
  7986. dynamic_offset =
  7987. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  7988. for (i = (int32)arity - 1; i >= 0; i--) {
  7989. cell = (uint8)wasm_value_type_cell_num(types[i]);
  7990. dynamic_offset -= cell;
  7991. emit_operand(ctx, dynamic_offset);
  7992. }
  7993. }
  7994. /* Part f */
  7995. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  7996. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  7997. }
  7998. else {
  7999. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8000. error_buf, error_buf_size))
  8001. return false;
  8002. /* label address, to be patched */
  8003. wasm_loader_emit_ptr(ctx, NULL);
  8004. }
  8005. return true;
  8006. }
  8007. static bool
  8008. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8009. bool disable_emit, int16 operand_offset,
  8010. char *error_buf, uint32 error_buf_size)
  8011. {
  8012. if (type == VALUE_TYPE_VOID)
  8013. return true;
  8014. /* only check memory overflow in first traverse */
  8015. if (ctx->p_code_compiled == NULL) {
  8016. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8017. return false;
  8018. }
  8019. if (disable_emit)
  8020. *(ctx->frame_offset)++ = operand_offset;
  8021. else {
  8022. emit_operand(ctx, ctx->dynamic_offset);
  8023. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8024. ctx->dynamic_offset++;
  8025. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8026. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8027. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8028. goto fail;
  8029. }
  8030. }
  8031. }
  8032. if (is_32bit_type(type))
  8033. return true;
  8034. if (ctx->p_code_compiled == NULL) {
  8035. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8036. return false;
  8037. }
  8038. ctx->frame_offset++;
  8039. if (!disable_emit) {
  8040. ctx->dynamic_offset++;
  8041. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8042. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8043. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8044. goto fail;
  8045. }
  8046. }
  8047. }
  8048. return true;
  8049. fail:
  8050. set_error_buf(error_buf, error_buf_size,
  8051. "fast interpreter offset overflow");
  8052. return false;
  8053. }
  8054. /* This function should be in front of wasm_loader_pop_frame_ref
  8055. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8056. will be modified by wasm_loader_pop_frame_ref */
  8057. static bool
  8058. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8059. char *error_buf, uint32 error_buf_size)
  8060. {
  8061. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8062. then current block is the function block */
  8063. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8064. BranchBlock *cur_block = ctx->frame_csp - depth;
  8065. int32 available_stack_cell =
  8066. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8067. /* Directly return success if current block is in stack
  8068. * polymorphic state while stack is empty. */
  8069. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8070. return true;
  8071. if (type == VALUE_TYPE_VOID)
  8072. return true;
  8073. if (is_32bit_type(type)) {
  8074. /* Check the offset stack bottom to ensure the frame offset
  8075. stack will not go underflow. But we don't thrown error
  8076. and return true here, because the error msg should be
  8077. given in wasm_loader_pop_frame_ref */
  8078. if (!check_offset_pop(ctx, 1))
  8079. return true;
  8080. ctx->frame_offset -= 1;
  8081. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8082. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8083. ctx->dynamic_offset -= 1;
  8084. }
  8085. else {
  8086. if (!check_offset_pop(ctx, 2))
  8087. return true;
  8088. ctx->frame_offset -= 2;
  8089. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8090. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8091. ctx->dynamic_offset -= 2;
  8092. }
  8093. emit_operand(ctx, *(ctx->frame_offset));
  8094. (void)error_buf;
  8095. (void)error_buf_size;
  8096. return true;
  8097. }
  8098. static bool
  8099. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8100. bool disable_emit, int16 operand_offset,
  8101. char *error_buf, uint32 error_buf_size)
  8102. {
  8103. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8104. error_buf, error_buf_size)))
  8105. return false;
  8106. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8107. return false;
  8108. return true;
  8109. }
  8110. static bool
  8111. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8112. char *error_buf, uint32 error_buf_size)
  8113. {
  8114. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8115. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8116. return false;
  8117. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8118. return false;
  8119. return true;
  8120. }
  8121. static bool
  8122. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8123. uint8 type_push, uint8 type_pop,
  8124. bool disable_emit, int16 operand_offset,
  8125. char *error_buf, uint32 error_buf_size)
  8126. {
  8127. uint8 i;
  8128. for (i = 0; i < pop_cnt; i++) {
  8129. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8130. error_buf_size))
  8131. return false;
  8132. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8133. error_buf_size))
  8134. return false;
  8135. }
  8136. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8137. operand_offset, error_buf,
  8138. error_buf_size))
  8139. return false;
  8140. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8141. return false;
  8142. return true;
  8143. }
  8144. static bool
  8145. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8146. int16 *offset, char *error_buf,
  8147. uint32 error_buf_size)
  8148. {
  8149. int8 bytes_to_increase;
  8150. int16 operand_offset = 0;
  8151. Const *c;
  8152. /* Search existing constant */
  8153. for (c = (Const *)ctx->const_buf;
  8154. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8155. /* TODO: handle v128 type? */
  8156. if ((type == c->value_type)
  8157. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8158. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8159. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8160. || (type == VALUE_TYPE_FUNCREF
  8161. && *(int32 *)value == c->value.i32)
  8162. || (type == VALUE_TYPE_EXTERNREF
  8163. && *(int32 *)value == c->value.i32)
  8164. #endif
  8165. || (type == VALUE_TYPE_F64
  8166. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8167. || (type == VALUE_TYPE_F32
  8168. && (0
  8169. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8170. operand_offset = c->slot_index;
  8171. break;
  8172. }
  8173. if (is_32bit_type(c->value_type))
  8174. operand_offset += 1;
  8175. else
  8176. operand_offset += 2;
  8177. }
  8178. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8179. /* New constant, append to the const buffer */
  8180. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8181. bytes_to_increase = 2;
  8182. }
  8183. else {
  8184. bytes_to_increase = 1;
  8185. }
  8186. /* The max cell num of const buffer is 32768 since the valid index range
  8187. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8188. * full */
  8189. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8190. *offset = 0;
  8191. return true;
  8192. }
  8193. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8194. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8195. ctx->const_buf_size + 4 * sizeof(Const));
  8196. ctx->const_buf_size += 4 * sizeof(Const);
  8197. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8198. }
  8199. c->value_type = type;
  8200. switch (type) {
  8201. case VALUE_TYPE_F64:
  8202. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8203. sizeof(float64));
  8204. ctx->const_cell_num += 2;
  8205. /* The const buf will be reversed, we use the second cell */
  8206. /* of the i64/f64 const so the finnal offset is corrent */
  8207. operand_offset++;
  8208. break;
  8209. case VALUE_TYPE_I64:
  8210. c->value.i64 = *(int64 *)value;
  8211. ctx->const_cell_num += 2;
  8212. operand_offset++;
  8213. break;
  8214. case VALUE_TYPE_F32:
  8215. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8216. sizeof(float32));
  8217. ctx->const_cell_num++;
  8218. break;
  8219. case VALUE_TYPE_I32:
  8220. c->value.i32 = *(int32 *)value;
  8221. ctx->const_cell_num++;
  8222. break;
  8223. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8224. case VALUE_TYPE_EXTERNREF:
  8225. case VALUE_TYPE_FUNCREF:
  8226. c->value.i32 = *(int32 *)value;
  8227. ctx->const_cell_num++;
  8228. break;
  8229. #endif
  8230. default:
  8231. break;
  8232. }
  8233. c->slot_index = operand_offset;
  8234. ctx->num_const++;
  8235. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8236. (int64)c->value.i64);
  8237. }
  8238. /* use negetive index for const */
  8239. operand_offset = -(operand_offset + 1);
  8240. *offset = operand_offset;
  8241. return true;
  8242. fail:
  8243. return false;
  8244. }
  8245. /*
  8246. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8247. -- Mostly used for the binary / compare operation
  8248. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8249. -- Mostly used in block / control instructions
  8250. The POP will always emit the offset on the top of the frame_offset stack
  8251. PUSH can be used in two ways:
  8252. 1. directly PUSH:
  8253. PUSH_XXX();
  8254. will allocate a dynamic space and emit
  8255. 2. silent PUSH:
  8256. operand_offset = xxx; disable_emit = true;
  8257. PUSH_XXX();
  8258. only push the frame_offset stack, no emit
  8259. */
  8260. #define TEMPLATE_PUSH(Type) \
  8261. do { \
  8262. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8263. disable_emit, operand_offset, \
  8264. error_buf, error_buf_size)) \
  8265. goto fail; \
  8266. } while (0)
  8267. #define TEMPLATE_PUSH_REF(Type) \
  8268. do { \
  8269. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8270. operand_offset, error_buf, \
  8271. error_buf_size)) \
  8272. goto fail; \
  8273. } while (0)
  8274. #define TEMPLATE_POP(Type) \
  8275. do { \
  8276. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8277. error_buf, error_buf_size)) \
  8278. goto fail; \
  8279. } while (0)
  8280. #define TEMPLATE_POP_REF(Type) \
  8281. do { \
  8282. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8283. error_buf_size)) \
  8284. goto fail; \
  8285. } while (0)
  8286. #define PUSH_OFFSET_TYPE(type) \
  8287. do { \
  8288. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8289. operand_offset, error_buf, \
  8290. error_buf_size))) \
  8291. goto fail; \
  8292. } while (0)
  8293. #define POP_OFFSET_TYPE(type) \
  8294. do { \
  8295. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8296. error_buf_size))) \
  8297. goto fail; \
  8298. } while (0)
  8299. #define POP_AND_PUSH(type_pop, type_push) \
  8300. do { \
  8301. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8302. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8303. operand_offset, error_buf, error_buf_size))) \
  8304. goto fail; \
  8305. } while (0)
  8306. /* type of POPs should be the same */
  8307. #define POP2_AND_PUSH(type_pop, type_push) \
  8308. do { \
  8309. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8310. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8311. operand_offset, error_buf, error_buf_size))) \
  8312. goto fail; \
  8313. } while (0)
  8314. #else /* WASM_ENABLE_FAST_INTERP */
  8315. #define TEMPLATE_PUSH(Type) \
  8316. do { \
  8317. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8318. error_buf, error_buf_size))) \
  8319. goto fail; \
  8320. } while (0)
  8321. #define TEMPLATE_PUSH_REF(Type) \
  8322. do { \
  8323. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8324. error_buf_size))) \
  8325. goto fail; \
  8326. } while (0)
  8327. #define TEMPLATE_POP(Type) \
  8328. do { \
  8329. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8330. error_buf, error_buf_size))) \
  8331. goto fail; \
  8332. } while (0)
  8333. #define TEMPLATE_POP_REF(Type) \
  8334. do { \
  8335. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8336. error_buf_size))) \
  8337. goto fail; \
  8338. } while (0)
  8339. #define POP_AND_PUSH(type_pop, type_push) \
  8340. do { \
  8341. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8342. type_pop, error_buf, \
  8343. error_buf_size))) \
  8344. goto fail; \
  8345. } while (0)
  8346. /* type of POPs should be the same */
  8347. #define POP2_AND_PUSH(type_pop, type_push) \
  8348. do { \
  8349. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8350. type_pop, error_buf, \
  8351. error_buf_size))) \
  8352. goto fail; \
  8353. } while (0)
  8354. #endif /* WASM_ENABLE_FAST_INTERP */
  8355. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8356. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8357. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8358. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8359. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8360. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8361. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8362. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8363. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8364. #define POP_I32() TEMPLATE_POP(I32)
  8365. #define POP_F32() TEMPLATE_POP(F32)
  8366. #define POP_I64() TEMPLATE_POP(I64)
  8367. #define POP_F64() TEMPLATE_POP(F64)
  8368. #define POP_V128() TEMPLATE_POP(V128)
  8369. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8370. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8371. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8372. #if WASM_ENABLE_FAST_INTERP != 0
  8373. static bool
  8374. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8375. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8376. {
  8377. int16 operand_offset = 0;
  8378. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8379. : loader_ctx->frame_csp;
  8380. BlockType *block_type = &block->block_type;
  8381. uint8 *return_types = NULL;
  8382. #if WASM_ENABLE_GC != 0
  8383. WASMRefTypeMap *reftype_maps = NULL;
  8384. uint32 reftype_map_count;
  8385. #endif
  8386. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8387. int32 i = 0;
  8388. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8389. *frame_offset_org = NULL;
  8390. #if WASM_ENABLE_GC == 0
  8391. return_count = block_type_get_result_types(block_type, &return_types);
  8392. #else
  8393. return_count = block_type_get_result_types(
  8394. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8395. #endif
  8396. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8397. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8398. if (return_count == 1) {
  8399. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8400. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8401. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8402. /* insert op_copy before else opcode */
  8403. if (opcode == WASM_OP_ELSE)
  8404. skip_label();
  8405. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8406. : EXT_OP_COPY_STACK_TOP_I64);
  8407. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8408. emit_operand(loader_ctx, block->dynamic_offset);
  8409. if (opcode == WASM_OP_ELSE) {
  8410. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8411. }
  8412. else {
  8413. loader_ctx->frame_offset -= cell;
  8414. loader_ctx->dynamic_offset = block->dynamic_offset;
  8415. PUSH_OFFSET_TYPE(return_types[0]);
  8416. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8417. }
  8418. if (opcode == WASM_OP_ELSE)
  8419. emit_label(opcode);
  8420. }
  8421. return true;
  8422. }
  8423. /* Copy stack top values to block's results which are in dynamic space.
  8424. * The instruction format:
  8425. * Part a: values count
  8426. * Part b: all values total cell num
  8427. * Part c: each value's cell_num, src offset and dst offset
  8428. * Part d: each value's src offset and dst offset
  8429. * Part e: each value's dst offset
  8430. */
  8431. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8432. dynamic_offset = dynamic_offset_org =
  8433. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8434. /* First traversal to get the count of values needed to be copied. */
  8435. for (i = (int32)return_count - 1; i >= 0; i--) {
  8436. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8437. frame_offset -= cells;
  8438. dynamic_offset -= cells;
  8439. if (dynamic_offset != *frame_offset) {
  8440. value_count++;
  8441. total_cel_num += cells;
  8442. }
  8443. }
  8444. if (value_count) {
  8445. uint32 j = 0;
  8446. uint8 *emit_data = NULL, *cells = NULL;
  8447. int16 *src_offsets = NULL;
  8448. uint16 *dst_offsets = NULL;
  8449. uint64 size =
  8450. (uint64)value_count
  8451. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8452. /* Allocate memory for the emit data */
  8453. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8454. return false;
  8455. cells = emit_data;
  8456. src_offsets = (int16 *)(cells + value_count);
  8457. dst_offsets = (uint16 *)(src_offsets + value_count);
  8458. /* insert op_copy before else opcode */
  8459. if (opcode == WASM_OP_ELSE)
  8460. skip_label();
  8461. emit_label(EXT_OP_COPY_STACK_VALUES);
  8462. /* Part a) */
  8463. emit_uint32(loader_ctx, value_count);
  8464. /* Part b) */
  8465. emit_uint32(loader_ctx, total_cel_num);
  8466. /* Second traversal to get each value's cell num, src offset and dst
  8467. * offset. */
  8468. frame_offset = frame_offset_org;
  8469. dynamic_offset = dynamic_offset_org;
  8470. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8471. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8472. frame_offset -= cell;
  8473. dynamic_offset -= cell;
  8474. if (dynamic_offset != *frame_offset) {
  8475. /* cell num */
  8476. cells[j] = cell;
  8477. /* src offset */
  8478. src_offsets[j] = *frame_offset;
  8479. /* dst offset */
  8480. dst_offsets[j] = dynamic_offset;
  8481. j++;
  8482. }
  8483. if (opcode == WASM_OP_ELSE) {
  8484. *frame_offset = dynamic_offset;
  8485. }
  8486. else {
  8487. loader_ctx->frame_offset = frame_offset;
  8488. loader_ctx->dynamic_offset = dynamic_offset;
  8489. PUSH_OFFSET_TYPE(return_types[i]);
  8490. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8491. loader_ctx->frame_offset = frame_offset_org;
  8492. loader_ctx->dynamic_offset = dynamic_offset_org;
  8493. }
  8494. }
  8495. bh_assert(j == value_count);
  8496. /* Emit the cells, src_offsets and dst_offsets */
  8497. for (j = 0; j < value_count; j++)
  8498. emit_byte(loader_ctx, cells[j]);
  8499. for (j = 0; j < value_count; j++)
  8500. emit_operand(loader_ctx, src_offsets[j]);
  8501. for (j = 0; j < value_count; j++)
  8502. emit_operand(loader_ctx, dst_offsets[j]);
  8503. if (opcode == WASM_OP_ELSE)
  8504. emit_label(opcode);
  8505. wasm_runtime_free(emit_data);
  8506. }
  8507. return true;
  8508. fail:
  8509. return false;
  8510. }
  8511. #endif /* WASM_ENABLE_FAST_INTERP */
  8512. #define RESERVE_BLOCK_RET() \
  8513. do { \
  8514. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8515. error_buf_size)) \
  8516. goto fail; \
  8517. } while (0)
  8518. #define PUSH_TYPE(type) \
  8519. do { \
  8520. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8521. error_buf_size))) \
  8522. goto fail; \
  8523. } while (0)
  8524. #define POP_TYPE(type) \
  8525. do { \
  8526. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8527. error_buf_size))) \
  8528. goto fail; \
  8529. } while (0)
  8530. #if WASM_ENABLE_GC == 0
  8531. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8532. do { \
  8533. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8534. _start_addr, error_buf, \
  8535. error_buf_size)) \
  8536. goto fail; \
  8537. } while (0)
  8538. #define POP_CSP() \
  8539. do { \
  8540. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8541. goto fail; \
  8542. } while (0)
  8543. #else
  8544. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8545. do { \
  8546. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8547. _start_addr, error_buf, \
  8548. error_buf_size)) \
  8549. goto fail; \
  8550. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8551. error_buf, error_buf_size)) { \
  8552. goto fail; \
  8553. } \
  8554. } while (0)
  8555. #define POP_CSP() \
  8556. do { \
  8557. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8558. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8559. goto fail; \
  8560. } while (0)
  8561. #endif /* end of WASM_ENABLE_GC == 0 */
  8562. #if WASM_ENABLE_GC == 0
  8563. #define GET_LOCAL_REFTYPE() (void)0
  8564. #else
  8565. #define GET_LOCAL_REFTYPE() \
  8566. do { \
  8567. if (wasm_is_type_multi_byte_type(local_type)) { \
  8568. WASMRefType *_ref_type; \
  8569. if (local_idx < param_count) \
  8570. _ref_type = wasm_reftype_map_find( \
  8571. param_reftype_maps, param_reftype_map_count, local_idx); \
  8572. else \
  8573. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8574. local_reftype_map_count, \
  8575. local_idx - param_count); \
  8576. bh_assert(_ref_type); \
  8577. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8578. wasm_reftype_struct_size(_ref_type)); \
  8579. } \
  8580. } while (0)
  8581. #endif /* end of WASM_ENABLE_GC == 0 */
  8582. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8583. do { \
  8584. read_leb_uint32(p, p_end, local_idx); \
  8585. if (local_idx >= param_count + local_count) { \
  8586. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8587. goto fail; \
  8588. } \
  8589. local_type = local_idx < param_count \
  8590. ? param_types[local_idx] \
  8591. : local_types[local_idx - param_count]; \
  8592. local_offset = local_offsets[local_idx]; \
  8593. GET_LOCAL_REFTYPE(); \
  8594. } while (0)
  8595. static bool
  8596. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8597. {
  8598. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8599. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8600. return false;
  8601. }
  8602. return true;
  8603. }
  8604. #define CHECK_MEMORY() \
  8605. do { \
  8606. if (!check_memory(module, error_buf, error_buf_size)) \
  8607. goto fail; \
  8608. } while (0)
  8609. static bool
  8610. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8611. uint32 error_buf_size)
  8612. {
  8613. uint8 mem_access_aligns[] = {
  8614. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8615. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8616. };
  8617. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8618. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8619. set_error_buf(error_buf, error_buf_size,
  8620. "alignment must not be larger than natural");
  8621. return false;
  8622. }
  8623. return true;
  8624. }
  8625. #if WASM_ENABLE_SIMD != 0
  8626. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8627. static bool
  8628. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8629. uint32 error_buf_size)
  8630. {
  8631. uint8 mem_access_aligns[] = {
  8632. 4, /* load */
  8633. 3, 3, 3, 3, 3, 3, /* load and extend */
  8634. 0, 1, 2, 3, /* load and splat */
  8635. 4, /* store */
  8636. };
  8637. uint8 mem_access_aligns_load_lane[] = {
  8638. 0, 1, 2, 3, /* load lane */
  8639. 0, 1, 2, 3, /* store lane */
  8640. 2, 3 /* store zero */
  8641. };
  8642. if (!((opcode <= SIMD_v128_store)
  8643. || (SIMD_v128_load8_lane <= opcode
  8644. && opcode <= SIMD_v128_load64_zero))) {
  8645. set_error_buf(error_buf, error_buf_size,
  8646. "the opcode doesn't include memarg");
  8647. return false;
  8648. }
  8649. if ((opcode <= SIMD_v128_store
  8650. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8651. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8652. && align > mem_access_aligns_load_lane[opcode
  8653. - SIMD_v128_load8_lane])) {
  8654. set_error_buf(error_buf, error_buf_size,
  8655. "alignment must not be larger than natural");
  8656. return false;
  8657. }
  8658. return true;
  8659. }
  8660. static bool
  8661. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8662. uint32 error_buf_size)
  8663. {
  8664. switch (opcode) {
  8665. case SIMD_i8x16_extract_lane_s:
  8666. case SIMD_i8x16_extract_lane_u:
  8667. case SIMD_i8x16_replace_lane:
  8668. if (lane >= 16) {
  8669. goto fail;
  8670. }
  8671. break;
  8672. case SIMD_i16x8_extract_lane_s:
  8673. case SIMD_i16x8_extract_lane_u:
  8674. case SIMD_i16x8_replace_lane:
  8675. if (lane >= 8) {
  8676. goto fail;
  8677. }
  8678. break;
  8679. case SIMD_i32x4_extract_lane:
  8680. case SIMD_i32x4_replace_lane:
  8681. case SIMD_f32x4_extract_lane:
  8682. case SIMD_f32x4_replace_lane:
  8683. if (lane >= 4) {
  8684. goto fail;
  8685. }
  8686. break;
  8687. case SIMD_i64x2_extract_lane:
  8688. case SIMD_i64x2_replace_lane:
  8689. case SIMD_f64x2_extract_lane:
  8690. case SIMD_f64x2_replace_lane:
  8691. if (lane >= 2) {
  8692. goto fail;
  8693. }
  8694. break;
  8695. case SIMD_v128_load8_lane:
  8696. case SIMD_v128_load16_lane:
  8697. case SIMD_v128_load32_lane:
  8698. case SIMD_v128_load64_lane:
  8699. case SIMD_v128_store8_lane:
  8700. case SIMD_v128_store16_lane:
  8701. case SIMD_v128_store32_lane:
  8702. case SIMD_v128_store64_lane:
  8703. case SIMD_v128_load32_zero:
  8704. case SIMD_v128_load64_zero:
  8705. {
  8706. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  8707. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  8708. goto fail;
  8709. }
  8710. break;
  8711. }
  8712. default:
  8713. goto fail;
  8714. }
  8715. return true;
  8716. fail:
  8717. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8718. return false;
  8719. }
  8720. static bool
  8721. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  8722. {
  8723. uint8 i;
  8724. for (i = 0; i != 16; ++i) {
  8725. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  8726. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8727. return false;
  8728. }
  8729. }
  8730. return true;
  8731. }
  8732. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8733. #endif /* end of WASM_ENABLE_SIMD */
  8734. #if WASM_ENABLE_SHARED_MEMORY != 0
  8735. static bool
  8736. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  8737. uint32 error_buf_size)
  8738. {
  8739. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  8740. uint8 mem_access_aligns[] = {
  8741. 2, 3, 0, 1, 0, 1, 2,
  8742. };
  8743. uint8 expect;
  8744. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  8745. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  8746. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  8747. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  8748. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  8749. }
  8750. else {
  8751. /* 7 opcodes in every group */
  8752. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  8753. }
  8754. if (align != expect) {
  8755. set_error_buf(error_buf, error_buf_size,
  8756. "alignment isn't equal to natural");
  8757. return false;
  8758. }
  8759. return true;
  8760. }
  8761. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  8762. static bool
  8763. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  8764. bool is_br_table, char *error_buf, uint32 error_buf_size)
  8765. {
  8766. BranchBlock *target_block, *cur_block;
  8767. BlockType *target_block_type;
  8768. uint8 type, *types = NULL, *frame_ref;
  8769. uint32 arity = 0;
  8770. int32 i, available_stack_cell;
  8771. uint16 cell_num;
  8772. #if WASM_ENABLE_GC != 0
  8773. WASMRefTypeMap *frame_reftype_map;
  8774. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  8775. WASMRefType *ref_type;
  8776. uint32 reftype_map_count = 0;
  8777. int32 available_reftype_map;
  8778. bool is_type_multi_byte;
  8779. #endif
  8780. uint8 *frame_ref_old = loader_ctx->frame_ref;
  8781. uint8 *frame_ref_after_popped = NULL;
  8782. uint8 frame_ref_tmp[4] = { 0 };
  8783. uint8 *frame_ref_buf = frame_ref_tmp;
  8784. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  8785. #if WASM_ENABLE_GC != 0
  8786. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  8787. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  8788. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  8789. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  8790. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  8791. #endif
  8792. #if WASM_ENABLE_FAST_INTERP != 0
  8793. int16 *frame_offset_old = loader_ctx->frame_offset;
  8794. int16 *frame_offset_after_popped = NULL;
  8795. int16 frame_offset_tmp[4] = { 0 };
  8796. int16 *frame_offset_buf = frame_offset_tmp;
  8797. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  8798. #endif
  8799. bool ret = false;
  8800. bh_assert(loader_ctx->csp_num > 0);
  8801. if (loader_ctx->csp_num - 1 < depth) {
  8802. set_error_buf(error_buf, error_buf_size,
  8803. "unknown label, "
  8804. "unexpected end of section or function");
  8805. return false;
  8806. }
  8807. cur_block = loader_ctx->frame_csp - 1;
  8808. target_block = loader_ctx->frame_csp - (depth + 1);
  8809. target_block_type = &target_block->block_type;
  8810. frame_ref = loader_ctx->frame_ref;
  8811. #if WASM_ENABLE_GC != 0
  8812. frame_reftype_map = loader_ctx->frame_reftype_map;
  8813. #endif
  8814. /* Note: loop's arity is different from if and block. loop's arity is
  8815. * its parameter count while if and block arity is result count.
  8816. */
  8817. #if WASM_ENABLE_GC == 0
  8818. if (target_block->label_type == LABEL_TYPE_LOOP)
  8819. arity = block_type_get_param_types(target_block_type, &types);
  8820. else
  8821. arity = block_type_get_result_types(target_block_type, &types);
  8822. #else
  8823. if (target_block->label_type == LABEL_TYPE_LOOP)
  8824. arity = block_type_get_param_types(target_block_type, &types,
  8825. &reftype_maps, &reftype_map_count);
  8826. else
  8827. arity = block_type_get_result_types(target_block_type, &types,
  8828. &reftype_maps, &reftype_map_count);
  8829. #endif
  8830. /* If the stack is in polymorphic state, just clear the stack
  8831. * and then re-push the values to make the stack top values
  8832. * match block type. */
  8833. if (cur_block->is_stack_polymorphic) {
  8834. #if WASM_ENABLE_GC != 0
  8835. int32 j = reftype_map_count - 1;
  8836. #endif
  8837. for (i = (int32)arity - 1; i >= 0; i--) {
  8838. #if WASM_ENABLE_GC != 0
  8839. if (wasm_is_type_multi_byte_type(types[i])) {
  8840. bh_assert(reftype_maps[j].index == i);
  8841. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  8842. reftype_maps[j].ref_type,
  8843. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  8844. j--;
  8845. }
  8846. #endif
  8847. #if WASM_ENABLE_FAST_INTERP != 0
  8848. POP_OFFSET_TYPE(types[i]);
  8849. #endif
  8850. POP_TYPE(types[i]);
  8851. }
  8852. /* Backup stack data since it may be changed in the below
  8853. push operations, and the stack data may be used when
  8854. checking other target blocks of opcode br_table */
  8855. if (is_br_table) {
  8856. uint64 total_size;
  8857. frame_ref_after_popped = loader_ctx->frame_ref;
  8858. total_size = (uint64)sizeof(uint8)
  8859. * (frame_ref_old - frame_ref_after_popped);
  8860. if (total_size > sizeof(frame_ref_tmp)
  8861. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  8862. error_buf_size))) {
  8863. goto fail;
  8864. }
  8865. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  8866. frame_ref_after_popped, (uint32)total_size);
  8867. #if WASM_ENABLE_GC != 0
  8868. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  8869. total_size =
  8870. (uint64)sizeof(WASMRefTypeMap)
  8871. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  8872. if (total_size > sizeof(frame_reftype_map_tmp)
  8873. && !(frame_reftype_map_buf = loader_malloc(
  8874. total_size, error_buf, error_buf_size))) {
  8875. goto fail;
  8876. }
  8877. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  8878. frame_reftype_map_after_popped, (uint32)total_size);
  8879. #endif
  8880. #if WASM_ENABLE_FAST_INTERP != 0
  8881. frame_offset_after_popped = loader_ctx->frame_offset;
  8882. total_size = (uint64)sizeof(int16)
  8883. * (frame_offset_old - frame_offset_after_popped);
  8884. if (total_size > sizeof(frame_offset_tmp)
  8885. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  8886. error_buf_size))) {
  8887. goto fail;
  8888. }
  8889. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  8890. frame_offset_after_popped, (uint32)total_size);
  8891. #endif
  8892. }
  8893. #if WASM_ENABLE_GC != 0
  8894. j = 0;
  8895. #endif
  8896. for (i = 0; i < (int32)arity; i++) {
  8897. #if WASM_ENABLE_GC != 0
  8898. if (wasm_is_type_multi_byte_type(types[i])) {
  8899. bh_assert(reftype_maps[j].index == i);
  8900. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  8901. reftype_maps[j].ref_type,
  8902. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  8903. j++;
  8904. }
  8905. #endif
  8906. #if WASM_ENABLE_FAST_INTERP != 0
  8907. bool disable_emit = true;
  8908. int16 operand_offset = 0;
  8909. PUSH_OFFSET_TYPE(types[i]);
  8910. #endif
  8911. PUSH_TYPE(types[i]);
  8912. }
  8913. #if WASM_ENABLE_FAST_INTERP != 0
  8914. emit_br_info(target_block);
  8915. #endif
  8916. /* Restore the stack data, note that frame_ref_bottom,
  8917. frame_reftype_map_bottom, frame_offset_bottom may be
  8918. re-allocated in the above push operations */
  8919. if (is_br_table) {
  8920. uint32 total_size;
  8921. /* The stack operand num should not be smaller than before
  8922. after pop and push operations */
  8923. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  8924. loader_ctx->stack_cell_num = stack_cell_num_old;
  8925. loader_ctx->frame_ref =
  8926. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  8927. total_size = (uint32)sizeof(uint8)
  8928. * (frame_ref_old - frame_ref_after_popped);
  8929. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  8930. frame_ref_buf, total_size);
  8931. #if WASM_ENABLE_GC != 0
  8932. /* The stack operand num should not be smaller than before
  8933. after pop and push operations */
  8934. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  8935. loader_ctx->reftype_map_num = reftype_map_num_old;
  8936. loader_ctx->frame_reftype_map =
  8937. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  8938. total_size =
  8939. (uint32)sizeof(WASMRefTypeMap)
  8940. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  8941. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  8942. total_size, frame_reftype_map_buf, total_size);
  8943. #endif
  8944. #if WASM_ENABLE_FAST_INTERP != 0
  8945. loader_ctx->frame_offset =
  8946. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  8947. total_size = (uint32)sizeof(int16)
  8948. * (frame_offset_old - frame_offset_after_popped);
  8949. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  8950. total_size, frame_offset_buf, total_size);
  8951. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  8952. #endif
  8953. }
  8954. ret = true;
  8955. goto cleanup_and_return;
  8956. }
  8957. available_stack_cell =
  8958. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  8959. #if WASM_ENABLE_GC != 0
  8960. available_reftype_map =
  8961. (int32)(loader_ctx->reftype_map_num
  8962. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  8963. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  8964. #endif
  8965. /* Check stack top values match target block type */
  8966. for (i = (int32)arity - 1; i >= 0; i--) {
  8967. type = types[i];
  8968. #if WASM_ENABLE_GC != 0
  8969. ref_type = NULL;
  8970. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  8971. if (is_type_multi_byte) {
  8972. bh_assert(reftype_map);
  8973. ref_type = reftype_map->ref_type;
  8974. }
  8975. #endif
  8976. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8977. break;
  8978. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  8979. #if WASM_ENABLE_GC != 0
  8980. frame_reftype_map, available_reftype_map,
  8981. #endif
  8982. type,
  8983. #if WASM_ENABLE_GC != 0
  8984. ref_type,
  8985. #endif
  8986. error_buf, error_buf_size)) {
  8987. goto fail;
  8988. }
  8989. cell_num = wasm_value_type_cell_num(types[i]);
  8990. frame_ref -= cell_num;
  8991. available_stack_cell -= cell_num;
  8992. #if WASM_ENABLE_GC != 0
  8993. if (is_type_multi_byte) {
  8994. frame_reftype_map--;
  8995. available_reftype_map--;
  8996. reftype_map--;
  8997. }
  8998. #endif
  8999. }
  9000. #if WASM_ENABLE_FAST_INTERP != 0
  9001. emit_br_info(target_block);
  9002. #endif
  9003. ret = true;
  9004. cleanup_and_return:
  9005. fail:
  9006. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9007. wasm_runtime_free(frame_ref_buf);
  9008. #if WASM_ENABLE_GC != 0
  9009. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9010. wasm_runtime_free(frame_reftype_map_buf);
  9011. #endif
  9012. #if WASM_ENABLE_FAST_INTERP != 0
  9013. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9014. wasm_runtime_free(frame_offset_tmp);
  9015. #endif
  9016. return ret;
  9017. }
  9018. static BranchBlock *
  9019. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9020. bool is_br_table, char *error_buf, uint32 error_buf_size)
  9021. {
  9022. uint8 *p = *p_buf, *p_end = buf_end;
  9023. BranchBlock *frame_csp_tmp;
  9024. uint32 depth;
  9025. read_leb_uint32(p, p_end, depth);
  9026. if (!wasm_loader_check_br(loader_ctx, depth, is_br_table, error_buf,
  9027. error_buf_size)) {
  9028. goto fail;
  9029. }
  9030. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9031. *p_buf = p;
  9032. return frame_csp_tmp;
  9033. fail:
  9034. return NULL;
  9035. }
  9036. #if WASM_ENABLE_EXCE_HANDLING != 0
  9037. static BranchBlock *
  9038. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9039. uint8 *buf_end, char *error_buf,
  9040. uint32 error_buf_size)
  9041. {
  9042. uint8 *p = *p_buf, *p_end = buf_end;
  9043. BranchBlock *frame_csp_tmp;
  9044. uint32 depth;
  9045. read_leb_uint32(p, p_end, depth);
  9046. /*
  9047. * Note: "delegate 0" means the surrounding block, not the
  9048. * try-delegate block itself.
  9049. *
  9050. * Note: the caller hasn't popped the try-delegate frame yet.
  9051. */
  9052. bh_assert(loader_ctx->csp_num > 0);
  9053. if (loader_ctx->csp_num - 1 <= depth) {
  9054. #if WASM_ENABLE_SPEC_TEST == 0
  9055. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9056. #else
  9057. set_error_buf(error_buf, error_buf_size, "unknown label");
  9058. #endif
  9059. goto fail;
  9060. }
  9061. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9062. #if WASM_ENABLE_FAST_INTERP != 0
  9063. emit_br_info(frame_csp_tmp);
  9064. #endif
  9065. *p_buf = p;
  9066. return frame_csp_tmp;
  9067. fail:
  9068. return NULL;
  9069. }
  9070. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9071. static bool
  9072. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9073. char *error_buf, uint32 error_buf_size)
  9074. {
  9075. BlockType *block_type = &block->block_type;
  9076. uint8 *return_types = NULL;
  9077. uint32 return_count = 0;
  9078. int32 available_stack_cell, return_cell_num, i;
  9079. uint8 *frame_ref = NULL;
  9080. #if WASM_ENABLE_GC != 0
  9081. WASMRefTypeMap *frame_reftype_map;
  9082. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9083. WASMRefType *ref_type;
  9084. uint32 param_count, return_reftype_map_count = 0;
  9085. int32 available_reftype_map =
  9086. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9087. #endif
  9088. available_stack_cell =
  9089. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9090. #if WASM_ENABLE_GC == 0
  9091. return_count = block_type_get_result_types(block_type, &return_types);
  9092. #else
  9093. return_count = block_type_get_result_types(block_type, &return_types,
  9094. &return_reftype_maps,
  9095. &return_reftype_map_count);
  9096. param_count =
  9097. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9098. (void)param_count;
  9099. #endif
  9100. return_cell_num =
  9101. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9102. /* If the stack is in polymorphic state, just clear the stack
  9103. * and then re-push the values to make the stack top values
  9104. * match block type. */
  9105. if (block->is_stack_polymorphic) {
  9106. #if WASM_ENABLE_GC != 0
  9107. int32 j = return_reftype_map_count - 1;
  9108. #endif
  9109. for (i = (int32)return_count - 1; i >= 0; i--) {
  9110. #if WASM_ENABLE_GC != 0
  9111. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9112. bh_assert(return_reftype_maps[j].index == i + param_count);
  9113. bh_memcpy_s(
  9114. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9115. return_reftype_maps[j].ref_type,
  9116. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9117. j--;
  9118. }
  9119. #endif
  9120. #if WASM_ENABLE_FAST_INTERP != 0
  9121. POP_OFFSET_TYPE(return_types[i]);
  9122. #endif
  9123. POP_TYPE(return_types[i]);
  9124. }
  9125. /* Check stack is empty */
  9126. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9127. set_error_buf(
  9128. error_buf, error_buf_size,
  9129. "type mismatch: stack size does not match block type");
  9130. goto fail;
  9131. }
  9132. #if WASM_ENABLE_GC != 0
  9133. j = 0;
  9134. #endif
  9135. for (i = 0; i < (int32)return_count; i++) {
  9136. #if WASM_ENABLE_GC != 0
  9137. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9138. bh_assert(return_reftype_maps[j].index == i + param_count);
  9139. bh_memcpy_s(
  9140. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9141. return_reftype_maps[j].ref_type,
  9142. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9143. j++;
  9144. }
  9145. #endif
  9146. #if WASM_ENABLE_FAST_INTERP != 0
  9147. bool disable_emit = true;
  9148. int16 operand_offset = 0;
  9149. PUSH_OFFSET_TYPE(return_types[i]);
  9150. #endif
  9151. PUSH_TYPE(return_types[i]);
  9152. }
  9153. return true;
  9154. }
  9155. if (available_stack_cell != return_cell_num) {
  9156. #if WASM_ENABLE_EXCE_HANDLING != 0
  9157. /* testspec: this error message format is expected by try_catch.wast */
  9158. snprintf(
  9159. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9160. block->label_type == LABEL_TYPE_TRY
  9161. || (block->label_type == LABEL_TYPE_CATCH
  9162. && return_cell_num > 0)
  9163. ? "instruction"
  9164. : "block",
  9165. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9166. " but stack has ",
  9167. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9168. : "");
  9169. goto fail;
  9170. #else
  9171. set_error_buf(error_buf, error_buf_size,
  9172. "type mismatch: stack size does not match block type");
  9173. goto fail;
  9174. #endif
  9175. }
  9176. /* Check stack values match return types */
  9177. frame_ref = loader_ctx->frame_ref;
  9178. #if WASM_ENABLE_GC != 0
  9179. frame_reftype_map = loader_ctx->frame_reftype_map;
  9180. return_reftype_map =
  9181. return_reftype_map_count
  9182. ? return_reftype_maps + return_reftype_map_count - 1
  9183. : NULL;
  9184. #endif
  9185. for (i = (int32)return_count - 1; i >= 0; i--) {
  9186. uint8 type = return_types[i];
  9187. #if WASM_ENABLE_GC != 0
  9188. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9189. ref_type = NULL;
  9190. if (is_type_multi_byte) {
  9191. bh_assert(return_reftype_map);
  9192. ref_type = return_reftype_map->ref_type;
  9193. }
  9194. #endif
  9195. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9196. #if WASM_ENABLE_GC != 0
  9197. frame_reftype_map, available_reftype_map,
  9198. #endif
  9199. type,
  9200. #if WASM_ENABLE_GC != 0
  9201. ref_type,
  9202. #endif
  9203. error_buf, error_buf_size))
  9204. return false;
  9205. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9206. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9207. #if WASM_ENABLE_GC != 0
  9208. if (is_type_multi_byte) {
  9209. frame_reftype_map--;
  9210. available_reftype_map--;
  9211. return_reftype_map--;
  9212. }
  9213. #endif
  9214. }
  9215. return true;
  9216. fail:
  9217. return false;
  9218. }
  9219. #if WASM_ENABLE_FAST_INTERP != 0
  9220. /* Copy parameters to dynamic space.
  9221. * 1) POP original parameter out;
  9222. * 2) Push and copy original values to dynamic space.
  9223. * The copy instruction format:
  9224. * Part a: param count
  9225. * Part b: all param total cell num
  9226. * Part c: each param's cell_num, src offset and dst offset
  9227. * Part d: each param's src offset
  9228. * Part e: each param's dst offset
  9229. */
  9230. static bool
  9231. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  9232. char *error_buf, uint32 error_buf_size)
  9233. {
  9234. bool ret = false;
  9235. int16 *frame_offset = NULL;
  9236. uint8 *cells = NULL, cell;
  9237. int16 *src_offsets = NULL;
  9238. uint8 *emit_data = NULL;
  9239. uint32 i;
  9240. BranchBlock *block = loader_ctx->frame_csp - 1;
  9241. BlockType *block_type = &block->block_type;
  9242. WASMFuncType *wasm_type = block_type->u.type;
  9243. uint32 param_count = block_type->u.type->param_count;
  9244. int16 condition_offset = 0;
  9245. bool disable_emit = false;
  9246. int16 operand_offset = 0;
  9247. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9248. bh_assert(size > 0);
  9249. /* For if block, we also need copy the condition operand offset. */
  9250. if (is_if_block)
  9251. size += sizeof(*cells) + sizeof(*src_offsets);
  9252. /* Allocate memory for the emit data */
  9253. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9254. return false;
  9255. cells = emit_data;
  9256. src_offsets = (int16 *)(cells + param_count);
  9257. if (is_if_block)
  9258. condition_offset = *loader_ctx->frame_offset;
  9259. /* POP original parameter out */
  9260. for (i = 0; i < param_count; i++) {
  9261. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9262. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9263. }
  9264. frame_offset = loader_ctx->frame_offset;
  9265. /* Get each param's cell num and src offset */
  9266. for (i = 0; i < param_count; i++) {
  9267. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9268. cells[i] = cell;
  9269. src_offsets[i] = *frame_offset;
  9270. frame_offset += cell;
  9271. }
  9272. /* emit copy instruction */
  9273. emit_label(EXT_OP_COPY_STACK_VALUES);
  9274. /* Part a) */
  9275. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9276. /* Part b) */
  9277. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9278. : wasm_type->param_cell_num);
  9279. /* Part c) */
  9280. for (i = 0; i < param_count; i++)
  9281. emit_byte(loader_ctx, cells[i]);
  9282. if (is_if_block)
  9283. emit_byte(loader_ctx, 1);
  9284. /* Part d) */
  9285. for (i = 0; i < param_count; i++)
  9286. emit_operand(loader_ctx, src_offsets[i]);
  9287. if (is_if_block)
  9288. emit_operand(loader_ctx, condition_offset);
  9289. /* Part e) */
  9290. /* Push to dynamic space. The push will emit the dst offset. */
  9291. for (i = 0; i < param_count; i++)
  9292. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9293. if (is_if_block)
  9294. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9295. ret = true;
  9296. fail:
  9297. /* Free the emit data */
  9298. wasm_runtime_free(emit_data);
  9299. return ret;
  9300. }
  9301. #endif
  9302. #if WASM_ENABLE_GC == 0
  9303. #define RESET_REFTYPE_MAP_STACK() (void)0
  9304. #else
  9305. #define RESET_REFTYPE_MAP_STACK() \
  9306. do { \
  9307. loader_ctx->reftype_map_num = \
  9308. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9309. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9310. + loader_ctx->reftype_map_num; \
  9311. } while (0)
  9312. #endif
  9313. /* reset the stack to the state of before entering the last block */
  9314. #if WASM_ENABLE_FAST_INTERP != 0
  9315. #define RESET_STACK() \
  9316. do { \
  9317. loader_ctx->stack_cell_num = \
  9318. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9319. loader_ctx->frame_ref = \
  9320. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9321. loader_ctx->frame_offset = \
  9322. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9323. RESET_REFTYPE_MAP_STACK(); \
  9324. } while (0)
  9325. #else
  9326. #define RESET_STACK() \
  9327. do { \
  9328. loader_ctx->stack_cell_num = \
  9329. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9330. loader_ctx->frame_ref = \
  9331. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9332. RESET_REFTYPE_MAP_STACK(); \
  9333. } while (0)
  9334. #endif
  9335. /* set current block's stack polymorphic state */
  9336. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9337. do { \
  9338. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9339. _cur_block->is_stack_polymorphic = flag; \
  9340. } while (0)
  9341. #define BLOCK_HAS_PARAM(block_type) \
  9342. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9343. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9344. do { \
  9345. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9346. error_buf_size))) { \
  9347. goto fail; \
  9348. } \
  9349. } while (0)
  9350. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9351. static bool
  9352. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9353. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9354. uint32 error_buf_size)
  9355. {
  9356. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9357. return false;
  9358. }
  9359. if (table_idx < module->import_table_count) {
  9360. if (p_elem_type)
  9361. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  9362. #if WASM_ENABLE_GC != 0
  9363. if (p_ref_type)
  9364. *((WASMRefType **)p_ref_type) =
  9365. module->import_tables[table_idx].u.table.elem_ref_type;
  9366. #endif
  9367. }
  9368. else {
  9369. if (p_elem_type)
  9370. *p_elem_type =
  9371. module->tables[module->import_table_count + table_idx]
  9372. .elem_type;
  9373. #if WASM_ENABLE_GC != 0
  9374. if (p_ref_type)
  9375. *((WASMRefType **)p_ref_type) =
  9376. module->tables[module->import_table_count + table_idx]
  9377. .elem_ref_type;
  9378. #endif
  9379. }
  9380. return true;
  9381. }
  9382. static bool
  9383. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9384. uint8 *p_elem_type, void **p_elem_ref_type,
  9385. char *error_buf, uint32 error_buf_size)
  9386. {
  9387. if (table_seg_idx >= module->table_seg_count) {
  9388. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9389. table_seg_idx);
  9390. return false;
  9391. }
  9392. if (p_elem_type) {
  9393. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9394. }
  9395. #if WASM_ENABLE_GC != 0
  9396. if (p_elem_ref_type)
  9397. *((WASMRefType **)p_elem_ref_type) =
  9398. module->table_segments[table_seg_idx].elem_ref_type;
  9399. #endif
  9400. return true;
  9401. }
  9402. #endif
  9403. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9404. const uint8 *
  9405. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9406. uint32 *len)
  9407. {
  9408. WASMCustomSection *section = module->custom_section_list;
  9409. while (section) {
  9410. if ((section->name_len == strlen(name))
  9411. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9412. if (len) {
  9413. *len = section->content_len;
  9414. }
  9415. return section->content_addr;
  9416. }
  9417. section = section->next;
  9418. }
  9419. return NULL;
  9420. }
  9421. #endif
  9422. static bool
  9423. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9424. uint32 cur_func_idx, char *error_buf,
  9425. uint32 error_buf_size)
  9426. {
  9427. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9428. uint32 param_count, local_count, global_count;
  9429. uint8 *param_types, *local_types, local_type, global_type;
  9430. BlockType func_block_type;
  9431. uint16 *local_offsets, local_offset;
  9432. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9433. uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
  9434. int32 i32_const = 0;
  9435. int64 i64_const;
  9436. uint8 opcode;
  9437. bool return_value = false;
  9438. WASMLoaderContext *loader_ctx;
  9439. BranchBlock *frame_csp_tmp;
  9440. #if WASM_ENABLE_GC != 0
  9441. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9442. uint32 param_reftype_map_count, local_reftype_map_count;
  9443. int32 heap_type;
  9444. WASMRefType wasm_ref_type = { 0 };
  9445. bool need_ref_type_map;
  9446. #endif
  9447. #if WASM_ENABLE_FAST_INTERP != 0
  9448. uint8 *func_const_end, *func_const = NULL;
  9449. int16 operand_offset = 0;
  9450. uint8 last_op = 0;
  9451. bool disable_emit, preserve_local = false, if_condition_available = true;
  9452. float32 f32_const;
  9453. float64 f64_const;
  9454. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9455. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9456. #endif
  9457. global_count = module->import_global_count + module->global_count;
  9458. param_count = func->func_type->param_count;
  9459. param_types = func->func_type->types;
  9460. func_block_type.is_value_type = false;
  9461. func_block_type.u.type = func->func_type;
  9462. local_count = func->local_count;
  9463. local_types = func->local_types;
  9464. local_offsets = func->local_offsets;
  9465. #if WASM_ENABLE_GC != 0
  9466. param_reftype_maps = func->func_type->ref_type_maps;
  9467. param_reftype_map_count = func->func_type->ref_type_map_count;
  9468. local_reftype_maps = func->local_ref_type_maps;
  9469. local_reftype_map_count = func->local_ref_type_map_count;
  9470. #endif
  9471. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9472. goto fail;
  9473. }
  9474. #if WASM_ENABLE_GC != 0
  9475. loader_ctx->module = module;
  9476. loader_ctx->ref_type_set = module->ref_type_set;
  9477. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9478. #endif
  9479. #if WASM_ENABLE_FAST_INTERP != 0
  9480. /* For the first traverse, the initial value of preserved_local_offset has
  9481. * not been determined, we use the INT16_MAX to represent that a slot has
  9482. * been copied to preserve space. For second traverse, this field will be
  9483. * set to the appropriate value in wasm_loader_ctx_reinit.
  9484. * This is for Issue #1230,
  9485. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9486. * drop opcodes need to know which slots are preserved, so those slots will
  9487. * not be treated as dynamically allocated slots */
  9488. loader_ctx->preserved_local_offset = INT16_MAX;
  9489. re_scan:
  9490. if (loader_ctx->code_compiled_size > 0) {
  9491. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9492. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9493. goto fail;
  9494. }
  9495. p = func->code;
  9496. func->code_compiled = loader_ctx->p_code_compiled;
  9497. func->code_compiled_size = loader_ctx->code_compiled_size;
  9498. }
  9499. #endif
  9500. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9501. while (p < p_end) {
  9502. opcode = *p++;
  9503. #if WASM_ENABLE_FAST_INTERP != 0
  9504. p_org = p;
  9505. disable_emit = false;
  9506. emit_label(opcode);
  9507. #endif
  9508. switch (opcode) {
  9509. case WASM_OP_UNREACHABLE:
  9510. RESET_STACK();
  9511. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9512. break;
  9513. case WASM_OP_NOP:
  9514. #if WASM_ENABLE_FAST_INTERP != 0
  9515. skip_label();
  9516. #endif
  9517. break;
  9518. case WASM_OP_IF:
  9519. {
  9520. #if WASM_ENABLE_FAST_INTERP != 0
  9521. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9522. int32 available_stack_cell =
  9523. (int32)(loader_ctx->stack_cell_num
  9524. - parent_block->stack_cell_num);
  9525. if (available_stack_cell <= 0
  9526. && parent_block->is_stack_polymorphic)
  9527. if_condition_available = false;
  9528. else
  9529. if_condition_available = true;
  9530. PRESERVE_LOCAL_FOR_BLOCK();
  9531. #endif
  9532. #if WASM_ENABLE_GC == 0
  9533. POP_I32();
  9534. #endif
  9535. goto handle_op_block_and_loop;
  9536. }
  9537. case WASM_OP_BLOCK:
  9538. case WASM_OP_LOOP:
  9539. #if WASM_ENABLE_EXCE_HANDLING != 0
  9540. case WASM_OP_TRY:
  9541. if (opcode == WASM_OP_TRY) {
  9542. /*
  9543. * keep track of exception handlers to account for
  9544. * memory allocation
  9545. */
  9546. func->exception_handler_count++;
  9547. /*
  9548. * try is a block
  9549. * do nothing special, but execution continues to
  9550. * to handle_op_block_and_loop,
  9551. * and that be pushes the csp
  9552. */
  9553. }
  9554. #endif
  9555. #if WASM_ENABLE_FAST_INTERP != 0
  9556. PRESERVE_LOCAL_FOR_BLOCK();
  9557. #endif
  9558. handle_op_block_and_loop:
  9559. {
  9560. uint8 value_type;
  9561. BlockType block_type;
  9562. #if WASM_ENABLE_FAST_INTERP != 0
  9563. uint32 available_params = 0;
  9564. #endif
  9565. CHECK_BUF(p, p_end, 1);
  9566. value_type = read_uint8(p);
  9567. if (is_byte_a_type(value_type)) {
  9568. /* If the first byte is one of these special values:
  9569. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9570. * the single return value. */
  9571. block_type.is_value_type = true;
  9572. block_type.u.value_type.type = value_type;
  9573. #if WASM_ENABLE_WAMR_COMPILER != 0
  9574. if (value_type == VALUE_TYPE_V128)
  9575. module->is_simd_used = true;
  9576. else if (value_type == VALUE_TYPE_FUNCREF
  9577. || value_type == VALUE_TYPE_EXTERNREF)
  9578. module->is_ref_types_used = true;
  9579. #endif
  9580. #if WASM_ENABLE_GC != 0
  9581. if (value_type != VALUE_TYPE_VOID) {
  9582. p_org = p;
  9583. p--;
  9584. if (!resolve_value_type((const uint8 **)&p, p_end,
  9585. module, &need_ref_type_map,
  9586. &wasm_ref_type, false,
  9587. error_buf, error_buf_size)) {
  9588. goto fail;
  9589. }
  9590. if (need_ref_type_map) {
  9591. block_type.u.value_type.ref_type_map.index = 0;
  9592. if (!(block_type.u.value_type.ref_type_map
  9593. .ref_type = reftype_set_insert(
  9594. module->ref_type_set, &wasm_ref_type,
  9595. error_buf, error_buf_size))) {
  9596. goto fail;
  9597. }
  9598. }
  9599. /* Set again as the type might be changed, e.g.
  9600. (ref null any) to anyref */
  9601. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9602. #if WASM_ENABLE_FAST_INTERP == 0
  9603. while (p_org < p) {
  9604. #if WASM_ENABLE_DEBUG_INTERP != 0
  9605. if (!record_fast_op(module, p_org, *p_org,
  9606. error_buf, error_buf_size)) {
  9607. goto fail;
  9608. }
  9609. #endif
  9610. /* Ignore extra bytes for interpreter */
  9611. *p_org++ = WASM_OP_NOP;
  9612. }
  9613. #endif
  9614. }
  9615. #endif /* end of WASM_ENABLE_GC != 0 */
  9616. }
  9617. else {
  9618. uint32 type_index;
  9619. /* Resolve the leb128 encoded type index as block type */
  9620. p--;
  9621. p_org = p - 1;
  9622. read_leb_uint32(p, p_end, type_index);
  9623. if (type_index >= module->type_count) {
  9624. set_error_buf(error_buf, error_buf_size,
  9625. "unknown type");
  9626. goto fail;
  9627. }
  9628. block_type.is_value_type = false;
  9629. block_type.u.type =
  9630. (WASMFuncType *)module->types[type_index];
  9631. #if WASM_ENABLE_FAST_INTERP == 0
  9632. /* If block use type index as block type, change the opcode
  9633. * to new extended opcode so that interpreter can resolve
  9634. * the block quickly.
  9635. */
  9636. #if WASM_ENABLE_DEBUG_INTERP != 0
  9637. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9638. error_buf_size)) {
  9639. goto fail;
  9640. }
  9641. #endif
  9642. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9643. #endif
  9644. }
  9645. #if WASM_ENABLE_GC != 0
  9646. if (opcode == WASM_OP_IF) {
  9647. POP_I32();
  9648. }
  9649. #endif
  9650. /* Pop block parameters from stack */
  9651. if (BLOCK_HAS_PARAM(block_type)) {
  9652. WASMFuncType *wasm_type = block_type.u.type;
  9653. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9654. #if WASM_ENABLE_FAST_INTERP != 0
  9655. uint32 cell_num;
  9656. available_params = block_type.u.type->param_count;
  9657. #endif
  9658. for (i = 0; i < block_type.u.type->param_count; i++) {
  9659. int32 available_stack_cell =
  9660. (int32)(loader_ctx->stack_cell_num
  9661. - cur_block->stack_cell_num);
  9662. if (available_stack_cell <= 0
  9663. && cur_block->is_stack_polymorphic) {
  9664. #if WASM_ENABLE_FAST_INTERP != 0
  9665. available_params = i;
  9666. #endif
  9667. break;
  9668. }
  9669. POP_TYPE(
  9670. wasm_type->types[wasm_type->param_count - i - 1]);
  9671. #if WASM_ENABLE_FAST_INTERP != 0
  9672. /* decrease the frame_offset pointer accordingly to keep
  9673. * consistent with frame_ref stack */
  9674. cell_num = wasm_value_type_cell_num(
  9675. wasm_type->types[wasm_type->param_count - i - 1]);
  9676. loader_ctx->frame_offset -= cell_num;
  9677. #endif
  9678. }
  9679. }
  9680. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  9681. block_type, p);
  9682. /* Pass parameters to block */
  9683. if (BLOCK_HAS_PARAM(block_type)) {
  9684. for (i = 0; i < block_type.u.type->param_count; i++) {
  9685. #if WASM_ENABLE_FAST_INTERP != 0
  9686. uint32 cell_num = wasm_value_type_cell_num(
  9687. block_type.u.type->types[i]);
  9688. if (i >= available_params) {
  9689. /* If there isn't enough data on stack, push a dummy
  9690. * offset to keep the stack consistent with
  9691. * frame_ref.
  9692. * Since the stack is already in polymorphic state,
  9693. * the opcode will not be executed, so the dummy
  9694. * offset won't cause any error */
  9695. uint32 n;
  9696. for (n = 0; n < cell_num; n++) {
  9697. if (loader_ctx->p_code_compiled == NULL) {
  9698. if (!check_offset_push(loader_ctx,
  9699. error_buf,
  9700. error_buf_size))
  9701. goto fail;
  9702. }
  9703. *loader_ctx->frame_offset++ = 0;
  9704. }
  9705. }
  9706. else {
  9707. loader_ctx->frame_offset += cell_num;
  9708. }
  9709. #endif
  9710. PUSH_TYPE(block_type.u.type->types[i]);
  9711. }
  9712. }
  9713. #if WASM_ENABLE_FAST_INTERP != 0
  9714. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  9715. skip_label();
  9716. if (BLOCK_HAS_PARAM(block_type)) {
  9717. /* Make sure params are in dynamic space */
  9718. if (!copy_params_to_dynamic_space(
  9719. loader_ctx, false, error_buf, error_buf_size))
  9720. goto fail;
  9721. }
  9722. if (opcode == WASM_OP_LOOP) {
  9723. (loader_ctx->frame_csp - 1)->code_compiled =
  9724. loader_ctx->p_code_compiled;
  9725. }
  9726. }
  9727. #if WASM_ENABLE_EXCE_HANDLING != 0
  9728. else if (opcode == WASM_OP_TRY) {
  9729. skip_label();
  9730. }
  9731. #endif
  9732. else if (opcode == WASM_OP_IF) {
  9733. BranchBlock *block = loader_ctx->frame_csp - 1;
  9734. /* If block has parameters, we should make sure they are in
  9735. * dynamic space. Otherwise, when else branch is missing,
  9736. * the later opcode may consume incorrect operand offset.
  9737. * Spec case:
  9738. * (func (export "params-id") (param i32) (result i32)
  9739. * (i32.const 1)
  9740. * (i32.const 2)
  9741. * (if (param i32 i32) (result i32 i32) (local.get 0)
  9742. * (then)) (i32.add)
  9743. * )
  9744. *
  9745. * So we should emit a copy instruction before the if.
  9746. *
  9747. * And we also need to save the parameter offsets and
  9748. * recover them before entering else branch.
  9749. *
  9750. */
  9751. if (BLOCK_HAS_PARAM(block_type)) {
  9752. uint64 size;
  9753. /* In polymorphic state, there may be no if condition on
  9754. * the stack, so the offset may not emitted */
  9755. if (if_condition_available) {
  9756. /* skip the if condition operand offset */
  9757. wasm_loader_emit_backspace(loader_ctx,
  9758. sizeof(int16));
  9759. }
  9760. /* skip the if label */
  9761. skip_label();
  9762. /* Emit a copy instruction */
  9763. if (!copy_params_to_dynamic_space(
  9764. loader_ctx, true, error_buf, error_buf_size))
  9765. goto fail;
  9766. /* Emit the if instruction */
  9767. emit_label(opcode);
  9768. /* Emit the new condition operand offset */
  9769. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  9770. /* Save top param_count values of frame_offset stack, so
  9771. * that we can recover it before executing else branch
  9772. */
  9773. size = sizeof(int16)
  9774. * (uint64)block_type.u.type->param_cell_num;
  9775. if (!(block->param_frame_offsets = loader_malloc(
  9776. size, error_buf, error_buf_size)))
  9777. goto fail;
  9778. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  9779. loader_ctx->frame_offset
  9780. - size / sizeof(int16),
  9781. (uint32)size);
  9782. }
  9783. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9784. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  9785. emit_empty_label_addr_and_frame_ip(PATCH_END);
  9786. }
  9787. #endif
  9788. break;
  9789. }
  9790. #if WASM_ENABLE_EXCE_HANDLING != 0
  9791. case WASM_OP_THROW:
  9792. {
  9793. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9794. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9795. uint8 label_type = cur_block->label_type;
  9796. uint32 tag_index = 0;
  9797. read_leb_int32(p, p_end, tag_index);
  9798. /* check validity of tag_index against module->tag_count */
  9799. /* check tag index is within the tag index space */
  9800. if (tag_index >= module->import_tag_count + module->tag_count) {
  9801. snprintf(error_buf, error_buf_size, "unknown tag %d",
  9802. tag_index);
  9803. goto fail;
  9804. }
  9805. /* the tag_type is stored in either the WASMTag (section tags)
  9806. * or WASMTagImport (import tag) */
  9807. WASMType *tag_type = NULL;
  9808. if (tag_index < module->import_tag_count) {
  9809. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  9810. }
  9811. else {
  9812. tag_type =
  9813. module->tags[tag_index - module->import_tag_count]
  9814. ->tag_type;
  9815. }
  9816. if (tag_type->result_count != 0) {
  9817. set_error_buf(error_buf, error_buf_size,
  9818. "tag type signature does not return void");
  9819. goto fail;
  9820. }
  9821. int32 available_stack_cell =
  9822. (int32)(loader_ctx->stack_cell_num
  9823. - cur_block->stack_cell_num);
  9824. /* Check stack values match return types by comparing tag param
  9825. * types with stack cells */
  9826. uint8 *frame_ref = loader_ctx->frame_ref;
  9827. for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
  9828. tti--) {
  9829. if (!check_stack_top_values(
  9830. loader_ctx, frame_ref, available_stack_cell,
  9831. tag_type->types[tti], error_buf, error_buf_size)) {
  9832. snprintf(error_buf, error_buf_size,
  9833. "type mismatch: instruction requires [%s] but "
  9834. "stack has [%s]",
  9835. tag_type->param_count > 0
  9836. ? type2str(tag_type->types[tti])
  9837. : "",
  9838. available_stack_cell > 0
  9839. ? type2str(*(loader_ctx->frame_ref - 1))
  9840. : "");
  9841. goto fail;
  9842. }
  9843. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  9844. available_stack_cell -=
  9845. wasm_value_type_cell_num(tag_type->types[tti]);
  9846. }
  9847. /* throw is stack polymorphic */
  9848. (void)label_type;
  9849. RESET_STACK();
  9850. break;
  9851. }
  9852. case WASM_OP_RETHROW:
  9853. {
  9854. /* must be done before checking branch block */
  9855. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9856. /* check the target catching block: LABEL_TYPE_CATCH */
  9857. if (!(frame_csp_tmp =
  9858. check_branch_block(loader_ctx, &p, p_end, false,
  9859. error_buf, error_buf_size)))
  9860. goto fail;
  9861. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  9862. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  9863. /* trap according to spectest (rethrow.wast) */
  9864. set_error_buf(error_buf, error_buf_size,
  9865. "invalid rethrow label");
  9866. goto fail;
  9867. }
  9868. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9869. uint8 label_type = cur_block->label_type;
  9870. (void)label_type;
  9871. /* rethrow is stack polymorphic */
  9872. RESET_STACK();
  9873. break;
  9874. }
  9875. case WASM_OP_DELEGATE:
  9876. {
  9877. /* check target block is valid */
  9878. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  9879. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  9880. goto fail;
  9881. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9882. uint8 label_type = cur_block->label_type;
  9883. (void)label_type;
  9884. /* DELEGATE ends the block */
  9885. POP_CSP();
  9886. break;
  9887. }
  9888. case WASM_OP_CATCH:
  9889. {
  9890. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9891. uint8 label_type = cur_block->label_type;
  9892. uint32 tag_index = 0;
  9893. read_leb_int32(p, p_end, tag_index);
  9894. /* check validity of tag_index against module->tag_count */
  9895. /* check tag index is within the tag index space */
  9896. if (tag_index >= module->import_tag_count + module->tag_count) {
  9897. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  9898. __FUNCTION__);
  9899. set_error_buf(error_buf, error_buf_size, "unknown tag");
  9900. goto fail;
  9901. }
  9902. /* the tag_type is stored in either the WASMTag (section tags)
  9903. * or WASMTagImport (import tag) */
  9904. WASMType *func_type = NULL;
  9905. if (tag_index < module->import_tag_count) {
  9906. func_type = module->import_tags[tag_index].u.tag.tag_type;
  9907. }
  9908. else {
  9909. func_type =
  9910. module->tags[tag_index - module->import_tag_count]
  9911. ->tag_type;
  9912. }
  9913. if (func_type->result_count != 0) {
  9914. set_error_buf(error_buf, error_buf_size,
  9915. "tag type signature does not return void");
  9916. goto fail;
  9917. }
  9918. /* check validity of current label (expect LABEL_TYPE_TRY or
  9919. * LABEL_TYPE_CATCH) */
  9920. if ((LABEL_TYPE_CATCH != label_type)
  9921. && (LABEL_TYPE_TRY != label_type)) {
  9922. set_error_buf(error_buf, error_buf_size,
  9923. "Unexpected block sequence encountered.");
  9924. goto fail;
  9925. }
  9926. BlockType new_block_type;
  9927. new_block_type.is_value_type = false;
  9928. new_block_type.u.type = func_type;
  9929. /*
  9930. * replace frame_csp by LABEL_TYPE_CATCH
  9931. */
  9932. cur_block->label_type = LABEL_TYPE_CATCH;
  9933. /* RESET_STACK removes the values pushed in TRY or pervious
  9934. * CATCH Blocks */
  9935. RESET_STACK();
  9936. /* push types on the stack according to catched type */
  9937. if (BLOCK_HAS_PARAM(new_block_type)) {
  9938. for (i = 0; i < new_block_type.u.type->param_count; i++)
  9939. PUSH_TYPE(new_block_type.u.type->types[i]);
  9940. }
  9941. break;
  9942. }
  9943. case WASM_OP_CATCH_ALL:
  9944. {
  9945. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9946. /* expecting a TRY or CATCH, anything else will be considered an
  9947. * error */
  9948. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  9949. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  9950. set_error_buf(error_buf, error_buf_size,
  9951. "Unexpected block sequence encountered.");
  9952. goto fail;
  9953. }
  9954. /* no immediates */
  9955. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  9956. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  9957. /* RESET_STACK removes the values pushed in TRY or pervious
  9958. * CATCH Blocks */
  9959. RESET_STACK();
  9960. /* catch_all has no tagtype and therefore no parameters */
  9961. break;
  9962. }
  9963. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9964. case WASM_OP_ELSE:
  9965. {
  9966. BranchBlock *block = NULL;
  9967. BlockType block_type;
  9968. if (loader_ctx->csp_num < 2
  9969. || (loader_ctx->frame_csp - 1)->label_type
  9970. != LABEL_TYPE_IF) {
  9971. set_error_buf(
  9972. error_buf, error_buf_size,
  9973. "opcode else found without matched opcode if");
  9974. goto fail;
  9975. }
  9976. block = loader_ctx->frame_csp - 1;
  9977. /* check whether if branch's stack matches its result type */
  9978. if (!check_block_stack(loader_ctx, block, error_buf,
  9979. error_buf_size))
  9980. goto fail;
  9981. block->else_addr = p - 1;
  9982. block_type = block->block_type;
  9983. #if WASM_ENABLE_GC != 0
  9984. if (!wasm_loader_init_local_use_masks(
  9985. loader_ctx, local_count, error_buf, error_buf_size)) {
  9986. goto fail;
  9987. }
  9988. #endif
  9989. #if WASM_ENABLE_FAST_INTERP != 0
  9990. /* if the result of if branch is in local or const area, add a
  9991. * copy op */
  9992. RESERVE_BLOCK_RET();
  9993. emit_empty_label_addr_and_frame_ip(PATCH_END);
  9994. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  9995. #endif
  9996. RESET_STACK();
  9997. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  9998. /* Pass parameters to if-false branch */
  9999. if (BLOCK_HAS_PARAM(block_type)) {
  10000. for (i = 0; i < block_type.u.type->param_count; i++)
  10001. PUSH_TYPE(block_type.u.type->types[i]);
  10002. }
  10003. #if WASM_ENABLE_FAST_INTERP != 0
  10004. /* Recover top param_count values of frame_offset stack */
  10005. if (BLOCK_HAS_PARAM((block_type))) {
  10006. uint32 size;
  10007. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10008. bh_memcpy_s(loader_ctx->frame_offset, size,
  10009. block->param_frame_offsets, size);
  10010. loader_ctx->frame_offset += (size / sizeof(int16));
  10011. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10012. }
  10013. #endif
  10014. break;
  10015. }
  10016. case WASM_OP_END:
  10017. {
  10018. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10019. /* check whether block stack matches its result type */
  10020. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10021. error_buf_size))
  10022. goto fail;
  10023. /* if no else branch, and return types do not match param types,
  10024. report failure */
  10025. if (cur_block->label_type == LABEL_TYPE_IF
  10026. && !cur_block->else_addr) {
  10027. uint32 block_param_count = 0, block_ret_count = 0;
  10028. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  10029. BlockType *cur_block_type = &cur_block->block_type;
  10030. #if WASM_ENABLE_GC != 0
  10031. uint32 block_param_reftype_map_count;
  10032. uint32 block_ret_reftype_map_count;
  10033. WASMRefTypeMap *block_param_reftype_maps;
  10034. WASMRefTypeMap *block_ret_reftype_maps;
  10035. #endif
  10036. block_param_count = block_type_get_param_types(
  10037. cur_block_type, &block_param_types
  10038. #if WASM_ENABLE_GC != 0
  10039. ,
  10040. &block_param_reftype_maps,
  10041. &block_param_reftype_map_count
  10042. #endif
  10043. );
  10044. block_ret_count = block_type_get_result_types(
  10045. cur_block_type, &block_ret_types
  10046. #if WASM_ENABLE_GC != 0
  10047. ,
  10048. &block_ret_reftype_maps, &block_ret_reftype_map_count
  10049. #endif
  10050. );
  10051. if (block_param_count != block_ret_count
  10052. || (block_param_count
  10053. && memcmp(block_param_types, block_ret_types,
  10054. block_param_count))) {
  10055. set_error_buf(error_buf, error_buf_size,
  10056. "type mismatch: else branch missing");
  10057. goto fail;
  10058. }
  10059. #if WASM_ENABLE_GC != 0
  10060. if (block_param_reftype_map_count
  10061. != block_ret_reftype_map_count
  10062. || (block_param_reftype_map_count
  10063. && memcmp(block_param_reftype_maps,
  10064. block_ret_reftype_maps,
  10065. sizeof(WASMRefTypeMap)
  10066. * block_param_reftype_map_count))) {
  10067. set_error_buf(error_buf, error_buf_size,
  10068. "type mismatch: else branch missing");
  10069. goto fail;
  10070. }
  10071. #endif
  10072. }
  10073. POP_CSP();
  10074. #if WASM_ENABLE_FAST_INTERP != 0
  10075. skip_label();
  10076. /* copy the result to the block return address */
  10077. RESERVE_BLOCK_RET();
  10078. apply_label_patch(loader_ctx, 0, PATCH_END);
  10079. free_label_patch_list(loader_ctx->frame_csp);
  10080. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10081. int32 idx;
  10082. uint8 ret_type;
  10083. emit_label(WASM_OP_RETURN);
  10084. for (idx = (int32)func->func_type->result_count - 1;
  10085. idx >= 0; idx--) {
  10086. ret_type = *(func->func_type->types
  10087. + func->func_type->param_count + idx);
  10088. POP_OFFSET_TYPE(ret_type);
  10089. }
  10090. }
  10091. #endif
  10092. if (loader_ctx->csp_num > 0) {
  10093. loader_ctx->frame_csp->end_addr = p - 1;
  10094. }
  10095. else {
  10096. /* end of function block, function will return */
  10097. if (p < p_end) {
  10098. set_error_buf(error_buf, error_buf_size,
  10099. "section size mismatch");
  10100. goto fail;
  10101. }
  10102. }
  10103. break;
  10104. }
  10105. case WASM_OP_BR:
  10106. {
  10107. if (!(frame_csp_tmp =
  10108. check_branch_block(loader_ctx, &p, p_end, false,
  10109. error_buf, error_buf_size)))
  10110. goto fail;
  10111. RESET_STACK();
  10112. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10113. break;
  10114. }
  10115. case WASM_OP_BR_IF:
  10116. {
  10117. POP_I32();
  10118. if (!(frame_csp_tmp =
  10119. check_branch_block(loader_ctx, &p, p_end, false,
  10120. error_buf, error_buf_size)))
  10121. goto fail;
  10122. break;
  10123. }
  10124. case WASM_OP_BR_TABLE:
  10125. {
  10126. uint32 depth = 0, default_arity, arity = 0;
  10127. BranchBlock *target_block;
  10128. BlockType *target_block_type;
  10129. #if WASM_ENABLE_FAST_INTERP == 0
  10130. BrTableCache *br_table_cache = NULL;
  10131. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10132. uint32 j;
  10133. #endif
  10134. read_leb_uint32(p, p_end, count);
  10135. #if WASM_ENABLE_FAST_INTERP != 0
  10136. emit_uint32(loader_ctx, count);
  10137. #endif
  10138. POP_I32();
  10139. /* Get the default depth and check it */
  10140. p_org = p;
  10141. for (i = 0; i <= count; i++) {
  10142. read_leb_uint32(p, p_end, depth);
  10143. }
  10144. if (loader_ctx->csp_num < depth + 1) {
  10145. set_error_buf(error_buf, error_buf_size,
  10146. "unknown label, "
  10147. "unexpected end of section or function");
  10148. goto fail;
  10149. }
  10150. p = p_org;
  10151. /* Get the default block's arity */
  10152. target_block = loader_ctx->frame_csp - (depth + 1);
  10153. target_block_type = &target_block->block_type;
  10154. default_arity = block_type_get_arity(target_block_type,
  10155. target_block->label_type);
  10156. #if WASM_ENABLE_FAST_INTERP == 0
  10157. p_depth_begin = p_depth = p;
  10158. #endif
  10159. for (i = 0; i <= count; i++) {
  10160. p_org = p;
  10161. read_leb_uint32(p, p_end, depth);
  10162. if (loader_ctx->csp_num < depth + 1) {
  10163. set_error_buf(error_buf, error_buf_size,
  10164. "unknown label, "
  10165. "unexpected end of section or function");
  10166. goto fail;
  10167. }
  10168. p = p_org;
  10169. /* Get the target block's arity and check it */
  10170. target_block = loader_ctx->frame_csp - (depth + 1);
  10171. target_block_type = &target_block->block_type;
  10172. arity = block_type_get_arity(target_block_type,
  10173. target_block->label_type);
  10174. if (arity != default_arity) {
  10175. set_error_buf(error_buf, error_buf_size,
  10176. "type mismatch: br_table targets must "
  10177. "all use same result type");
  10178. goto fail;
  10179. }
  10180. if (!(frame_csp_tmp =
  10181. check_branch_block(loader_ctx, &p, p_end, true,
  10182. error_buf, error_buf_size))) {
  10183. goto fail;
  10184. }
  10185. #if WASM_ENABLE_FAST_INTERP == 0
  10186. if (br_table_cache) {
  10187. br_table_cache->br_depths[i] = depth;
  10188. }
  10189. else {
  10190. if (depth > 255) {
  10191. /* The depth cannot be stored in one byte,
  10192. create br_table cache to store each depth */
  10193. #if WASM_ENABLE_DEBUG_INTERP != 0
  10194. if (!record_fast_op(module, p_opcode, *p_opcode,
  10195. error_buf, error_buf_size)) {
  10196. goto fail;
  10197. }
  10198. #endif
  10199. if (!(br_table_cache = loader_malloc(
  10200. offsetof(BrTableCache, br_depths)
  10201. + sizeof(uint32)
  10202. * (uint64)(count + 1),
  10203. error_buf, error_buf_size))) {
  10204. goto fail;
  10205. }
  10206. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10207. br_table_cache->br_table_op_addr = p_opcode;
  10208. br_table_cache->br_count = count;
  10209. /* Copy previous depths which are one byte */
  10210. for (j = 0; j < i; j++) {
  10211. br_table_cache->br_depths[j] = p_depth_begin[j];
  10212. }
  10213. br_table_cache->br_depths[i] = depth;
  10214. bh_list_insert(module->br_table_cache_list,
  10215. br_table_cache);
  10216. }
  10217. else {
  10218. /* The depth can be stored in one byte, use the
  10219. byte of the leb to store it */
  10220. *p_depth++ = (uint8)depth;
  10221. }
  10222. }
  10223. #endif
  10224. }
  10225. #if WASM_ENABLE_FAST_INTERP == 0
  10226. /* Set the tailing bytes to nop */
  10227. if (br_table_cache)
  10228. p_depth = p_depth_begin;
  10229. while (p_depth < p)
  10230. *p_depth++ = WASM_OP_NOP;
  10231. #endif
  10232. RESET_STACK();
  10233. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10234. break;
  10235. }
  10236. case WASM_OP_RETURN:
  10237. {
  10238. int32 idx;
  10239. uint8 ret_type;
  10240. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  10241. idx--) {
  10242. ret_type = *(func->func_type->types
  10243. + func->func_type->param_count + idx);
  10244. #if WASM_ENABLE_FAST_INTERP != 0
  10245. /* emit the offset after return opcode */
  10246. POP_OFFSET_TYPE(ret_type);
  10247. #endif
  10248. POP_TYPE(ret_type);
  10249. }
  10250. RESET_STACK();
  10251. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10252. break;
  10253. }
  10254. case WASM_OP_CALL:
  10255. #if WASM_ENABLE_TAIL_CALL != 0
  10256. case WASM_OP_RETURN_CALL:
  10257. #endif
  10258. #if WASM_ENABLE_GC != 0
  10259. case WASM_OP_CALL_REF:
  10260. case WASM_OP_RETURN_CALL_REF:
  10261. #endif
  10262. {
  10263. WASMFuncType *func_type;
  10264. uint8 type;
  10265. int32 idx;
  10266. #if WASM_ENABLE_GC != 0
  10267. WASMRefType *ref_type;
  10268. uint32 type_idx1;
  10269. int32 j;
  10270. #endif
  10271. #if WASM_ENABLE_GC != 0
  10272. if (opcode == WASM_OP_CALL_REF
  10273. || opcode == WASM_OP_RETURN_CALL_REF) {
  10274. read_leb_uint32(p, p_end, type_idx1);
  10275. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10276. &type_idx, error_buf,
  10277. error_buf_size)) {
  10278. goto fail;
  10279. }
  10280. if (type == VALUE_TYPE_ANY) {
  10281. type_idx = type_idx1;
  10282. }
  10283. if (!check_type_index(module, type_idx, error_buf,
  10284. error_buf_size)) {
  10285. goto fail;
  10286. }
  10287. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10288. set_error_buf(error_buf, error_buf_size,
  10289. "unkown function type");
  10290. goto fail;
  10291. }
  10292. if (type_idx != type_idx1) {
  10293. set_error_buf(error_buf, error_buf_size,
  10294. "function type mismatch");
  10295. goto fail;
  10296. }
  10297. func_type = (WASMFuncType *)module->types[type_idx];
  10298. }
  10299. else
  10300. #endif
  10301. {
  10302. read_leb_uint32(p, p_end, func_idx);
  10303. #if WASM_ENABLE_FAST_INTERP != 0
  10304. /* we need to emit func_idx before arguments */
  10305. emit_uint32(loader_ctx, func_idx);
  10306. #endif
  10307. if (!check_function_index(module, func_idx, error_buf,
  10308. error_buf_size)) {
  10309. goto fail;
  10310. }
  10311. if (func_idx < module->import_function_count)
  10312. func_type = module->import_functions[func_idx]
  10313. .u.function.func_type;
  10314. else
  10315. func_type =
  10316. module
  10317. ->functions[func_idx
  10318. - module->import_function_count]
  10319. ->func_type;
  10320. }
  10321. if (func_type->param_count > 0) {
  10322. #if WASM_ENABLE_GC != 0
  10323. j = (int32)(func_type->result_ref_type_maps
  10324. - func_type->ref_type_maps - 1);
  10325. #endif
  10326. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10327. idx--) {
  10328. #if WASM_ENABLE_GC != 0
  10329. if (wasm_is_type_multi_byte_type(
  10330. func_type->types[idx])) {
  10331. ref_type = func_type->ref_type_maps[j].ref_type;
  10332. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10333. ref_type,
  10334. wasm_reftype_struct_size(ref_type));
  10335. j--;
  10336. }
  10337. #endif
  10338. #if WASM_ENABLE_FAST_INTERP != 0
  10339. POP_OFFSET_TYPE(func_type->types[idx]);
  10340. #endif
  10341. POP_TYPE(func_type->types[idx]);
  10342. }
  10343. }
  10344. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10345. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10346. #endif
  10347. #if WASM_ENABLE_GC != 0
  10348. j = (int32)(func_type->result_ref_type_maps
  10349. - func_type->ref_type_maps);
  10350. #endif
  10351. for (i = 0; i < func_type->result_count; i++) {
  10352. #if WASM_ENABLE_GC != 0
  10353. if (wasm_is_type_multi_byte_type(
  10354. func_type->types[func_type->param_count + i])) {
  10355. ref_type = func_type->ref_type_maps[j].ref_type;
  10356. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10357. ref_type,
  10358. wasm_reftype_struct_size(ref_type));
  10359. j++;
  10360. }
  10361. #endif
  10362. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10363. #if WASM_ENABLE_FAST_INTERP != 0
  10364. /* Here we emit each return value's dynamic_offset. But
  10365. * in fact these offsets are continuous, so interpreter
  10366. * only need to get the first return value's offset.
  10367. */
  10368. PUSH_OFFSET_TYPE(
  10369. func_type->types[func_type->param_count + i]);
  10370. #endif
  10371. }
  10372. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10373. }
  10374. else {
  10375. #if WASM_ENABLE_GC == 0
  10376. if (func_type->result_count
  10377. != func->func_type->result_count) {
  10378. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10379. "type mismatch: expect ",
  10380. func->func_type->result_count,
  10381. " return values but got other");
  10382. goto fail;
  10383. }
  10384. for (i = 0; i < func_type->result_count; i++) {
  10385. type = func->func_type
  10386. ->types[func->func_type->param_count + i];
  10387. if (func_type->types[func_type->param_count + i]
  10388. != type) {
  10389. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10390. "type mismatch: expect ",
  10391. type2str(type), " but got other");
  10392. goto fail;
  10393. }
  10394. }
  10395. #else
  10396. if (!wasm_func_type_result_is_subtype_of(
  10397. func_type, func->func_type, module->types,
  10398. module->type_count)) {
  10399. set_error_buf(
  10400. error_buf, error_buf_size,
  10401. "type mismatch: invalid func result types");
  10402. goto fail;
  10403. }
  10404. #endif
  10405. RESET_STACK();
  10406. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10407. }
  10408. #endif
  10409. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10410. || WASM_ENABLE_WAMR_COMPILER != 0
  10411. func->has_op_func_call = true;
  10412. #endif
  10413. (void)type;
  10414. break;
  10415. }
  10416. /*
  10417. * if disable reference type: call_indirect typeidx, 0x00
  10418. * if enable reference type: call_indirect typeidx, tableidx
  10419. */
  10420. case WASM_OP_CALL_INDIRECT:
  10421. #if WASM_ENABLE_TAIL_CALL != 0
  10422. case WASM_OP_RETURN_CALL_INDIRECT:
  10423. #endif
  10424. {
  10425. int32 idx;
  10426. WASMFuncType *func_type;
  10427. read_leb_uint32(p, p_end, type_idx);
  10428. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10429. read_leb_uint32(p, p_end, table_idx);
  10430. #else
  10431. CHECK_BUF(p, p_end, 1);
  10432. table_idx = read_uint8(p);
  10433. #endif
  10434. if (!check_table_index(module, table_idx, error_buf,
  10435. error_buf_size)) {
  10436. goto fail;
  10437. }
  10438. #if WASM_ENABLE_FAST_INTERP != 0
  10439. /* we need to emit before arguments */
  10440. #if WASM_ENABLE_TAIL_CALL != 0
  10441. emit_byte(loader_ctx, opcode);
  10442. #endif
  10443. emit_uint32(loader_ctx, type_idx);
  10444. emit_uint32(loader_ctx, table_idx);
  10445. #endif
  10446. /* skip elem idx */
  10447. POP_I32();
  10448. if (type_idx >= module->type_count) {
  10449. set_error_buf(error_buf, error_buf_size, "unknown type");
  10450. goto fail;
  10451. }
  10452. func_type = (WASMFuncType *)module->types[type_idx];
  10453. if (func_type->param_count > 0) {
  10454. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10455. idx--) {
  10456. #if WASM_ENABLE_FAST_INTERP != 0
  10457. POP_OFFSET_TYPE(func_type->types[idx]);
  10458. #endif
  10459. POP_TYPE(func_type->types[idx]);
  10460. }
  10461. }
  10462. #if WASM_ENABLE_TAIL_CALL != 0
  10463. if (opcode == WASM_OP_CALL_INDIRECT) {
  10464. #endif
  10465. for (i = 0; i < func_type->result_count; i++) {
  10466. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10467. #if WASM_ENABLE_FAST_INTERP != 0
  10468. PUSH_OFFSET_TYPE(
  10469. func_type->types[func_type->param_count + i]);
  10470. #endif
  10471. }
  10472. #if WASM_ENABLE_TAIL_CALL != 0
  10473. }
  10474. else {
  10475. uint8 type;
  10476. if (func_type->result_count
  10477. != func->func_type->result_count) {
  10478. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10479. "type mismatch: expect ",
  10480. func->func_type->result_count,
  10481. " return values but got other");
  10482. goto fail;
  10483. }
  10484. for (i = 0; i < func_type->result_count; i++) {
  10485. type = func->func_type
  10486. ->types[func->func_type->param_count + i];
  10487. if (func_type->types[func_type->param_count + i]
  10488. != type) {
  10489. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10490. "type mismatch: expect ",
  10491. type2str(type), " but got other");
  10492. goto fail;
  10493. }
  10494. }
  10495. RESET_STACK();
  10496. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10497. }
  10498. #endif
  10499. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10500. || WASM_ENABLE_WAMR_COMPILER != 0
  10501. func->has_op_func_call = true;
  10502. #endif
  10503. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10504. func->has_op_call_indirect = true;
  10505. #endif
  10506. break;
  10507. }
  10508. case WASM_OP_DROP:
  10509. {
  10510. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10511. int32 available_stack_cell =
  10512. (int32)(loader_ctx->stack_cell_num
  10513. - cur_block->stack_cell_num);
  10514. if (available_stack_cell <= 0
  10515. && !cur_block->is_stack_polymorphic) {
  10516. set_error_buf(error_buf, error_buf_size,
  10517. "type mismatch, opcode drop was found "
  10518. "but stack was empty");
  10519. goto fail;
  10520. }
  10521. if (available_stack_cell > 0) {
  10522. #if WASM_ENABLE_GC != 0
  10523. if (wasm_is_type_multi_byte_type(
  10524. *(loader_ctx->frame_ref - 1))) {
  10525. bh_assert((int32)(loader_ctx->reftype_map_num
  10526. - cur_block->reftype_map_num)
  10527. > 0);
  10528. loader_ctx->frame_reftype_map--;
  10529. loader_ctx->reftype_map_num--;
  10530. }
  10531. #endif
  10532. if (is_32bit_type(*(loader_ctx->frame_ref - 1))
  10533. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  10534. loader_ctx->frame_ref--;
  10535. loader_ctx->stack_cell_num--;
  10536. #if WASM_ENABLE_FAST_INTERP != 0
  10537. skip_label();
  10538. loader_ctx->frame_offset--;
  10539. if ((*(loader_ctx->frame_offset)
  10540. > loader_ctx->start_dynamic_offset)
  10541. && (*(loader_ctx->frame_offset)
  10542. < loader_ctx->max_dynamic_offset))
  10543. loader_ctx->dynamic_offset--;
  10544. #endif
  10545. }
  10546. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10547. loader_ctx->frame_ref -= 2;
  10548. loader_ctx->stack_cell_num -= 2;
  10549. #if WASM_ENABLE_FAST_INTERP == 0
  10550. *(p - 1) = WASM_OP_DROP_64;
  10551. #endif
  10552. #if WASM_ENABLE_FAST_INTERP != 0
  10553. skip_label();
  10554. loader_ctx->frame_offset -= 2;
  10555. if ((*(loader_ctx->frame_offset)
  10556. > loader_ctx->start_dynamic_offset)
  10557. && (*(loader_ctx->frame_offset)
  10558. < loader_ctx->max_dynamic_offset))
  10559. loader_ctx->dynamic_offset -= 2;
  10560. #endif
  10561. }
  10562. #if WASM_ENABLE_SIMD != 0
  10563. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10564. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10565. loader_ctx->frame_ref -= 4;
  10566. loader_ctx->stack_cell_num -= 4;
  10567. }
  10568. #endif
  10569. #endif
  10570. else {
  10571. set_error_buf(error_buf, error_buf_size,
  10572. "type mismatch");
  10573. goto fail;
  10574. }
  10575. }
  10576. else {
  10577. #if WASM_ENABLE_FAST_INTERP != 0
  10578. skip_label();
  10579. #endif
  10580. }
  10581. break;
  10582. }
  10583. case WASM_OP_SELECT:
  10584. {
  10585. uint8 ref_type;
  10586. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10587. int32 available_stack_cell;
  10588. #if WASM_ENABLE_FAST_INTERP != 0
  10589. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10590. #endif
  10591. POP_I32();
  10592. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10593. - cur_block->stack_cell_num);
  10594. if (available_stack_cell <= 0
  10595. && !cur_block->is_stack_polymorphic) {
  10596. set_error_buf(error_buf, error_buf_size,
  10597. "type mismatch or invalid result arity, "
  10598. "opcode select was found "
  10599. "but stack was empty");
  10600. goto fail;
  10601. }
  10602. if (available_stack_cell > 0) {
  10603. switch (*(loader_ctx->frame_ref - 1)) {
  10604. case VALUE_TYPE_I32:
  10605. case VALUE_TYPE_F32:
  10606. break;
  10607. case VALUE_TYPE_I64:
  10608. case VALUE_TYPE_F64:
  10609. #if WASM_ENABLE_FAST_INTERP == 0
  10610. *(p - 1) = WASM_OP_SELECT_64;
  10611. #endif
  10612. #if WASM_ENABLE_FAST_INTERP != 0
  10613. if (loader_ctx->p_code_compiled) {
  10614. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10615. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10616. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10617. *(void **)(p_code_compiled_tmp
  10618. - sizeof(void *)) =
  10619. handle_table[opcode_tmp];
  10620. #else
  10621. #if UINTPTR_MAX == UINT64_MAX
  10622. /* emit int32 relative offset in 64-bit target
  10623. */
  10624. int32 offset =
  10625. (int32)((uint8 *)handle_table[opcode_tmp]
  10626. - (uint8 *)handle_table[0]);
  10627. *(int32 *)(p_code_compiled_tmp
  10628. - sizeof(int32)) = offset;
  10629. #else
  10630. /* emit uint32 label address in 32-bit target */
  10631. *(uint32 *)(p_code_compiled_tmp
  10632. - sizeof(uint32)) =
  10633. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10634. #endif
  10635. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10636. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10637. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10638. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10639. #else
  10640. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10641. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10642. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10643. }
  10644. #endif /* end of WASM_ENABLE_FAST_INTERP */
  10645. break;
  10646. #if WASM_ENABLE_SIMD != 0
  10647. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10648. case VALUE_TYPE_V128:
  10649. break;
  10650. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  10651. #endif /* WASM_ENABLE_SIMD != 0 */
  10652. default:
  10653. {
  10654. set_error_buf(error_buf, error_buf_size,
  10655. "type mismatch");
  10656. goto fail;
  10657. }
  10658. }
  10659. ref_type = *(loader_ctx->frame_ref - 1);
  10660. #if WASM_ENABLE_FAST_INTERP != 0
  10661. POP_OFFSET_TYPE(ref_type);
  10662. POP_TYPE(ref_type);
  10663. POP_OFFSET_TYPE(ref_type);
  10664. POP_TYPE(ref_type);
  10665. PUSH_OFFSET_TYPE(ref_type);
  10666. PUSH_TYPE(ref_type);
  10667. #else
  10668. POP2_AND_PUSH(ref_type, ref_type);
  10669. #endif
  10670. }
  10671. else {
  10672. #if WASM_ENABLE_FAST_INTERP != 0
  10673. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  10674. #endif
  10675. PUSH_TYPE(VALUE_TYPE_ANY);
  10676. }
  10677. break;
  10678. }
  10679. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10680. case WASM_OP_SELECT_T:
  10681. {
  10682. uint8 vec_len, type;
  10683. #if WASM_ENABLE_GC != 0
  10684. WASMRefType *ref_type = NULL;
  10685. #endif
  10686. #if WASM_ENABLE_FAST_INTERP != 0
  10687. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10688. #endif
  10689. read_leb_uint32(p, p_end, vec_len);
  10690. if (vec_len != 1) {
  10691. /* typed select must have exactly one result */
  10692. set_error_buf(error_buf, error_buf_size,
  10693. "invalid result arity");
  10694. goto fail;
  10695. }
  10696. #if WASM_ENABLE_GC == 0
  10697. CHECK_BUF(p, p_end, 1);
  10698. type = read_uint8(p);
  10699. if (!is_value_type(type)) {
  10700. set_error_buf(error_buf, error_buf_size,
  10701. "unknown value type");
  10702. goto fail;
  10703. }
  10704. #else
  10705. p_org = p + 1;
  10706. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  10707. &need_ref_type_map, &wasm_ref_type,
  10708. false, error_buf, error_buf_size)) {
  10709. goto fail;
  10710. }
  10711. type = wasm_ref_type.ref_type;
  10712. if (need_ref_type_map) {
  10713. if (!(ref_type = reftype_set_insert(
  10714. module->ref_type_set, &wasm_ref_type, error_buf,
  10715. error_buf_size))) {
  10716. goto fail;
  10717. }
  10718. }
  10719. #if WASM_ENABLE_FAST_INTERP == 0
  10720. while (p_org < p) {
  10721. #if WASM_ENABLE_DEBUG_INTERP != 0
  10722. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10723. error_buf_size)) {
  10724. goto fail;
  10725. }
  10726. #endif
  10727. /* Ignore extra bytes for interpreter */
  10728. *p_org++ = WASM_OP_NOP;
  10729. }
  10730. #endif
  10731. #endif /* end of WASM_ENABLE_GC == 0 */
  10732. POP_I32();
  10733. #if WASM_ENABLE_FAST_INTERP != 0
  10734. if (loader_ctx->p_code_compiled) {
  10735. uint8 opcode_tmp = WASM_OP_SELECT;
  10736. if (type == VALUE_TYPE_V128) {
  10737. #if (WASM_ENABLE_SIMD == 0) \
  10738. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  10739. set_error_buf(error_buf, error_buf_size,
  10740. "SIMD v128 type isn't supported");
  10741. goto fail;
  10742. #endif
  10743. }
  10744. else {
  10745. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  10746. opcode_tmp = WASM_OP_SELECT_64;
  10747. #if WASM_ENABLE_GC != 0
  10748. if (wasm_is_type_reftype(type))
  10749. opcode_tmp = WASM_OP_SELECT_T;
  10750. #endif
  10751. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10752. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10753. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  10754. handle_table[opcode_tmp];
  10755. #else
  10756. #if UINTPTR_MAX == UINT64_MAX
  10757. /* emit int32 relative offset in 64-bit target */
  10758. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  10759. - (uint8 *)handle_table[0]);
  10760. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  10761. offset;
  10762. #else
  10763. /* emit uint32 label address in 32-bit target */
  10764. *(uint32 *)(p_code_compiled_tmp - 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. }
  10777. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  10778. POP_REF(type);
  10779. #if WASM_ENABLE_GC != 0
  10780. if (need_ref_type_map) {
  10781. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  10782. wasm_reftype_struct_size(ref_type));
  10783. }
  10784. #endif
  10785. POP_REF(type);
  10786. #if WASM_ENABLE_GC != 0
  10787. if (need_ref_type_map) {
  10788. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  10789. wasm_reftype_struct_size(ref_type));
  10790. }
  10791. #endif
  10792. PUSH_REF(type);
  10793. #if WASM_ENABLE_WAMR_COMPILER != 0
  10794. module->is_ref_types_used = true;
  10795. #endif
  10796. (void)vec_len;
  10797. break;
  10798. }
  10799. /* table.get x. tables[x]. [i32] -> [t] */
  10800. /* table.set x. tables[x]. [i32 t] -> [] */
  10801. case WASM_OP_TABLE_GET:
  10802. case WASM_OP_TABLE_SET:
  10803. {
  10804. uint8 decl_ref_type;
  10805. #if WASM_ENABLE_GC != 0
  10806. WASMRefType *ref_type;
  10807. #endif
  10808. read_leb_uint32(p, p_end, table_idx);
  10809. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  10810. #if WASM_ENABLE_GC != 0
  10811. (void **)&ref_type,
  10812. #else
  10813. NULL,
  10814. #endif
  10815. error_buf, error_buf_size))
  10816. goto fail;
  10817. #if WASM_ENABLE_GC != 0
  10818. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  10819. bh_assert(ref_type);
  10820. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  10821. ref_type, wasm_reftype_struct_size(ref_type));
  10822. }
  10823. #endif
  10824. #if WASM_ENABLE_FAST_INTERP != 0
  10825. emit_uint32(loader_ctx, table_idx);
  10826. #endif
  10827. if (opcode == WASM_OP_TABLE_GET) {
  10828. POP_I32();
  10829. #if WASM_ENABLE_FAST_INTERP != 0
  10830. PUSH_OFFSET_TYPE(decl_ref_type);
  10831. #endif
  10832. PUSH_TYPE(decl_ref_type);
  10833. }
  10834. else {
  10835. #if WASM_ENABLE_FAST_INTERP != 0
  10836. POP_OFFSET_TYPE(decl_ref_type);
  10837. #endif
  10838. POP_TYPE(decl_ref_type);
  10839. POP_I32();
  10840. }
  10841. #if WASM_ENABLE_WAMR_COMPILER != 0
  10842. module->is_ref_types_used = true;
  10843. #endif
  10844. break;
  10845. }
  10846. case WASM_OP_REF_NULL:
  10847. {
  10848. uint8 ref_type;
  10849. #if WASM_ENABLE_GC == 0
  10850. CHECK_BUF(p, p_end, 1);
  10851. ref_type = read_uint8(p);
  10852. if (ref_type != VALUE_TYPE_FUNCREF
  10853. && ref_type != VALUE_TYPE_EXTERNREF) {
  10854. set_error_buf(error_buf, error_buf_size, "type mismatch");
  10855. goto fail;
  10856. }
  10857. #else
  10858. read_leb_int32(p, p_end, heap_type);
  10859. if (heap_type >= 0) {
  10860. if (!check_type_index(module, heap_type, error_buf,
  10861. error_buf_size)) {
  10862. goto fail;
  10863. }
  10864. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  10865. true, heap_type);
  10866. ref_type = wasm_ref_type.ref_type;
  10867. }
  10868. else {
  10869. if (!wasm_is_valid_heap_type(heap_type)) {
  10870. set_error_buf(error_buf, error_buf_size,
  10871. "unknown type");
  10872. goto fail;
  10873. }
  10874. ref_type = (uint8)((int32)0x80 + heap_type);
  10875. }
  10876. #endif /* end of WASM_ENABLE_GC == 0 */
  10877. #if WASM_ENABLE_FAST_INTERP != 0
  10878. PUSH_OFFSET_TYPE(ref_type);
  10879. #endif
  10880. PUSH_TYPE(ref_type);
  10881. #if WASM_ENABLE_WAMR_COMPILER != 0
  10882. module->is_ref_types_used = true;
  10883. #endif
  10884. break;
  10885. }
  10886. case WASM_OP_REF_IS_NULL:
  10887. {
  10888. #if WASM_ENABLE_GC == 0
  10889. #if WASM_ENABLE_FAST_INTERP != 0
  10890. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10891. int32 block_stack_cell_num =
  10892. (int32)(loader_ctx->stack_cell_num
  10893. - cur_block->stack_cell_num);
  10894. if (block_stack_cell_num <= 0) {
  10895. if (!cur_block->is_stack_polymorphic) {
  10896. set_error_buf(
  10897. error_buf, error_buf_size,
  10898. "type mismatch: expect data but stack was empty");
  10899. goto fail;
  10900. }
  10901. }
  10902. else {
  10903. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  10904. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  10905. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  10906. if (!wasm_loader_pop_frame_ref_offset(
  10907. loader_ctx, *(loader_ctx->frame_ref - 1),
  10908. error_buf, error_buf_size)) {
  10909. goto fail;
  10910. }
  10911. }
  10912. else {
  10913. set_error_buf(error_buf, error_buf_size,
  10914. "type mismatch");
  10915. goto fail;
  10916. }
  10917. }
  10918. #else
  10919. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  10920. error_buf, error_buf_size)
  10921. && !wasm_loader_pop_frame_ref(loader_ctx,
  10922. VALUE_TYPE_EXTERNREF,
  10923. error_buf, error_buf_size)) {
  10924. goto fail;
  10925. }
  10926. #endif
  10927. #else /* else of WASM_ENABLE_GC == 0 */
  10928. uint8 type;
  10929. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  10930. error_buf, error_buf_size)) {
  10931. goto fail;
  10932. }
  10933. #endif
  10934. PUSH_I32();
  10935. #if WASM_ENABLE_WAMR_COMPILER != 0
  10936. module->is_ref_types_used = true;
  10937. #endif
  10938. break;
  10939. }
  10940. case WASM_OP_REF_FUNC:
  10941. {
  10942. read_leb_uint32(p, p_end, func_idx);
  10943. if (!check_function_index(module, func_idx, error_buf,
  10944. error_buf_size)) {
  10945. goto fail;
  10946. }
  10947. /* Refer to a forward-declared function */
  10948. if (func_idx >= cur_func_idx + module->import_function_count) {
  10949. WASMTableSeg *table_seg = module->table_segments;
  10950. bool func_declared = false;
  10951. uint32 j;
  10952. /* Check whether the function is declared in table segs,
  10953. note that it doesn't matter whether the table seg's mode
  10954. is passive, active or declarative. */
  10955. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  10956. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  10957. #if WASM_ENABLE_GC != 0
  10958. || (table_seg->elem_type == REF_TYPE_HT_NON_NULLABLE
  10959. && table_seg->elem_ref_type->ref_ht_common
  10960. .heap_type
  10961. == HEAP_TYPE_FUNC)
  10962. #endif
  10963. ) {
  10964. for (j = 0; j < table_seg->value_count; j++) {
  10965. if (table_seg->init_values[j].u.ref_index
  10966. == func_idx) {
  10967. func_declared = true;
  10968. break;
  10969. }
  10970. }
  10971. }
  10972. }
  10973. if (!func_declared) {
  10974. /* Check whether the function is exported */
  10975. for (i = 0; i < module->export_count; i++) {
  10976. if (module->exports[i].kind == EXPORT_KIND_FUNC
  10977. && module->exports[i].index == func_idx) {
  10978. func_declared = true;
  10979. break;
  10980. }
  10981. }
  10982. }
  10983. if (!func_declared) {
  10984. set_error_buf(error_buf, error_buf_size,
  10985. "undeclared function reference");
  10986. goto fail;
  10987. }
  10988. }
  10989. #if WASM_ENABLE_FAST_INTERP != 0
  10990. emit_uint32(loader_ctx, func_idx);
  10991. #endif
  10992. #if WASM_ENABLE_GC == 0
  10993. PUSH_FUNCREF();
  10994. #else
  10995. if (func_idx < module->import_function_count)
  10996. type_idx =
  10997. module->import_functions[func_idx].u.function.type_idx;
  10998. else
  10999. type_idx = module
  11000. ->functions[func_idx
  11001. - module->import_function_count]
  11002. ->type_idx;
  11003. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11004. false, type_idx);
  11005. PUSH_REF(wasm_ref_type.ref_type);
  11006. #endif
  11007. #if WASM_ENABLE_WAMR_COMPILER != 0
  11008. module->is_ref_types_used = true;
  11009. #endif
  11010. break;
  11011. }
  11012. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11013. #if WASM_ENABLE_GC != 0
  11014. case WASM_OP_REF_AS_NON_NULL:
  11015. case WASM_OP_BR_ON_NULL:
  11016. {
  11017. uint8 type;
  11018. WASMRefType ref_type;
  11019. /* POP (ref null ht) and get the converted (ref ht) */
  11020. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11021. error_buf, error_buf_size)) {
  11022. goto fail;
  11023. }
  11024. if (opcode == WASM_OP_BR_ON_NULL) {
  11025. if (!(frame_csp_tmp =
  11026. check_branch_block(loader_ctx, &p, p_end, false,
  11027. error_buf, error_buf_size))) {
  11028. goto fail;
  11029. }
  11030. #if WASM_ENABLE_FAST_INTERP != 0
  11031. disable_emit = true;
  11032. #endif
  11033. }
  11034. /* PUSH the converted (ref ht) */
  11035. if (type != VALUE_TYPE_ANY) {
  11036. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11037. sizeof(WASMRefType));
  11038. }
  11039. PUSH_REF(type);
  11040. break;
  11041. }
  11042. case WASM_OP_BR_ON_NON_NULL:
  11043. {
  11044. uint8 type;
  11045. WASMRefType ref_type;
  11046. uint32 available_stack_cell =
  11047. loader_ctx->stack_cell_num
  11048. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11049. /* POP (ref null ht) and get the converted (ref ht) */
  11050. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11051. error_buf, error_buf_size)) {
  11052. goto fail;
  11053. }
  11054. #if WASM_ENABLE_FAST_INTERP != 0
  11055. disable_emit = true;
  11056. #endif
  11057. /* Temporarily PUSH back (ref ht), check brach block and
  11058. then POP it */
  11059. if (available_stack_cell
  11060. > 0) { /* stack isn't in polymorphic state */
  11061. if (type != VALUE_TYPE_ANY) {
  11062. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11063. &ref_type, sizeof(WASMRefType));
  11064. }
  11065. PUSH_REF(type);
  11066. }
  11067. if (!(frame_csp_tmp =
  11068. check_branch_block(loader_ctx, &p, p_end, false,
  11069. error_buf, error_buf_size))) {
  11070. goto fail;
  11071. }
  11072. if (available_stack_cell
  11073. > 0) { /* stack isn't in polymorphic state */
  11074. POP_REF(type);
  11075. #if WASM_ENABLE_FAST_INTERP != 0
  11076. /* Erase the opnd offset emitted by POP_REF() */
  11077. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11078. #endif
  11079. }
  11080. break;
  11081. }
  11082. case WASM_OP_REF_EQ:
  11083. POP_REF(REF_TYPE_EQREF);
  11084. POP_REF(REF_TYPE_EQREF);
  11085. PUSH_I32();
  11086. break;
  11087. #endif /* end of WASM_ENABLE_GC != 0 */
  11088. case WASM_OP_GET_LOCAL:
  11089. {
  11090. p_org = p - 1;
  11091. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11092. PUSH_TYPE(local_type);
  11093. #if WASM_ENABLE_GC != 0
  11094. /* Cannot get a non-nullable and unset local */
  11095. if (local_idx >= param_count
  11096. && wasm_is_reftype_htref_non_nullable(local_type)
  11097. && !wasm_loader_get_local_status(loader_ctx,
  11098. local_idx - param_count)) {
  11099. set_error_buf(error_buf, error_buf_size,
  11100. "uninitialized local");
  11101. goto fail;
  11102. }
  11103. #endif
  11104. #if WASM_ENABLE_FAST_INTERP != 0
  11105. /* Get Local is optimized out */
  11106. skip_label();
  11107. disable_emit = true;
  11108. operand_offset = local_offset;
  11109. PUSH_OFFSET_TYPE(local_type);
  11110. #else
  11111. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11112. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11113. if (local_offset < 0x80
  11114. #if WASM_ENABLE_GC != 0
  11115. && !wasm_is_type_reftype(local_type)
  11116. #endif
  11117. ) {
  11118. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11119. if (is_32bit_type(local_type)) {
  11120. *p_org++ = (uint8)local_offset;
  11121. }
  11122. else {
  11123. *p_org++ = (uint8)(local_offset | 0x80);
  11124. }
  11125. while (p_org < p) {
  11126. *p_org++ = WASM_OP_NOP;
  11127. }
  11128. }
  11129. #endif
  11130. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11131. break;
  11132. }
  11133. case WASM_OP_SET_LOCAL:
  11134. {
  11135. p_org = p - 1;
  11136. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11137. #if WASM_ENABLE_FAST_INTERP != 0
  11138. if (!(preserve_referenced_local(
  11139. loader_ctx, opcode, local_offset, local_type,
  11140. &preserve_local, error_buf, error_buf_size)))
  11141. goto fail;
  11142. if (local_offset < 256
  11143. #if WASM_ENABLE_GC != 0
  11144. && !wasm_is_type_reftype(local_type)
  11145. #endif
  11146. ) {
  11147. skip_label();
  11148. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11149. if (loader_ctx->p_code_compiled)
  11150. STORE_U16(loader_ctx->p_code_compiled - 2,
  11151. local_offset);
  11152. loader_ctx->frame_offset--;
  11153. loader_ctx->dynamic_offset--;
  11154. }
  11155. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11156. if (loader_ctx->p_code_compiled)
  11157. STORE_U16(loader_ctx->p_code_compiled - 2,
  11158. local_offset);
  11159. loader_ctx->frame_offset -= 2;
  11160. loader_ctx->dynamic_offset -= 2;
  11161. }
  11162. else {
  11163. if (is_32bit_type(local_type)) {
  11164. emit_label(EXT_OP_SET_LOCAL_FAST);
  11165. emit_byte(loader_ctx, (uint8)local_offset);
  11166. }
  11167. else {
  11168. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11169. emit_byte(loader_ctx, (uint8)local_offset);
  11170. }
  11171. POP_OFFSET_TYPE(local_type);
  11172. }
  11173. }
  11174. else { /* local index larger than 255, reserve leb */
  11175. emit_uint32(loader_ctx, local_idx);
  11176. POP_OFFSET_TYPE(local_type);
  11177. }
  11178. #else
  11179. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11180. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11181. if (local_offset < 0x80
  11182. #if WASM_ENABLE_GC != 0
  11183. && !wasm_is_type_reftype(local_type)
  11184. #endif
  11185. ) {
  11186. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11187. if (is_32bit_type(local_type)) {
  11188. *p_org++ = (uint8)local_offset;
  11189. }
  11190. else {
  11191. *p_org++ = (uint8)(local_offset | 0x80);
  11192. }
  11193. while (p_org < p) {
  11194. *p_org++ = WASM_OP_NOP;
  11195. }
  11196. }
  11197. #endif
  11198. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11199. #if WASM_ENABLE_GC != 0
  11200. if (local_idx >= param_count) {
  11201. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11202. }
  11203. #endif
  11204. POP_TYPE(local_type);
  11205. break;
  11206. }
  11207. case WASM_OP_TEE_LOCAL:
  11208. {
  11209. p_org = p - 1;
  11210. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11211. #if WASM_ENABLE_FAST_INTERP != 0
  11212. /* If the stack is in polymorphic state, do fake pop and push on
  11213. offset stack to keep the depth of offset stack to be the
  11214. same with ref stack */
  11215. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11216. if (cur_block->is_stack_polymorphic) {
  11217. POP_OFFSET_TYPE(local_type);
  11218. PUSH_OFFSET_TYPE(local_type);
  11219. }
  11220. #endif
  11221. POP_TYPE(local_type);
  11222. PUSH_TYPE(local_type);
  11223. #if WASM_ENABLE_FAST_INTERP != 0
  11224. if (!(preserve_referenced_local(
  11225. loader_ctx, opcode, local_offset, local_type,
  11226. &preserve_local, error_buf, error_buf_size)))
  11227. goto fail;
  11228. if (local_offset < 256
  11229. #if WASM_ENABLE_GC != 0
  11230. && !wasm_is_type_reftype(local_type)
  11231. #endif
  11232. ) {
  11233. skip_label();
  11234. if (is_32bit_type(local_type)) {
  11235. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11236. emit_byte(loader_ctx, (uint8)local_offset);
  11237. }
  11238. else {
  11239. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11240. emit_byte(loader_ctx, (uint8)local_offset);
  11241. }
  11242. }
  11243. else { /* local index larger than 255, reserve leb */
  11244. emit_uint32(loader_ctx, local_idx);
  11245. }
  11246. emit_operand(loader_ctx,
  11247. *(loader_ctx->frame_offset
  11248. - wasm_value_type_cell_num(local_type)));
  11249. #else
  11250. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11251. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11252. if (local_offset < 0x80
  11253. #if WASM_ENABLE_GC != 0
  11254. && !wasm_is_type_reftype(local_type)
  11255. #endif
  11256. ) {
  11257. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11258. if (is_32bit_type(local_type)) {
  11259. *p_org++ = (uint8)local_offset;
  11260. }
  11261. else {
  11262. *p_org++ = (uint8)(local_offset | 0x80);
  11263. }
  11264. while (p_org < p) {
  11265. *p_org++ = WASM_OP_NOP;
  11266. }
  11267. }
  11268. #endif
  11269. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11270. #if WASM_ENABLE_GC != 0
  11271. if (local_idx >= param_count) {
  11272. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11273. }
  11274. #endif
  11275. break;
  11276. }
  11277. case WASM_OP_GET_GLOBAL:
  11278. {
  11279. #if WASM_ENABLE_GC != 0
  11280. WASMRefType *ref_type;
  11281. #endif
  11282. p_org = p - 1;
  11283. read_leb_uint32(p, p_end, global_idx);
  11284. if (global_idx >= global_count) {
  11285. set_error_buf(error_buf, error_buf_size, "unknown global");
  11286. goto fail;
  11287. }
  11288. global_type =
  11289. global_idx < module->import_global_count
  11290. ? module->import_globals[global_idx].u.global.type
  11291. : module
  11292. ->globals[global_idx
  11293. - module->import_global_count]
  11294. .type;
  11295. #if WASM_ENABLE_GC != 0
  11296. ref_type =
  11297. global_idx < module->import_global_count
  11298. ? module->import_globals[global_idx].u.global.ref_type
  11299. : module
  11300. ->globals[global_idx
  11301. - module->import_global_count]
  11302. .ref_type;
  11303. if (wasm_is_type_multi_byte_type(global_type)) {
  11304. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11305. wasm_reftype_struct_size(ref_type));
  11306. }
  11307. #endif
  11308. PUSH_TYPE(global_type);
  11309. #if WASM_ENABLE_FAST_INTERP == 0
  11310. if (global_type == VALUE_TYPE_I64
  11311. || global_type == VALUE_TYPE_F64) {
  11312. #if WASM_ENABLE_DEBUG_INTERP != 0
  11313. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11314. error_buf_size)) {
  11315. goto fail;
  11316. }
  11317. #endif
  11318. *p_org = WASM_OP_GET_GLOBAL_64;
  11319. }
  11320. #else /* else of WASM_ENABLE_FAST_INTERP */
  11321. if (global_type == VALUE_TYPE_I64
  11322. || global_type == VALUE_TYPE_F64) {
  11323. skip_label();
  11324. emit_label(WASM_OP_GET_GLOBAL_64);
  11325. }
  11326. emit_uint32(loader_ctx, global_idx);
  11327. PUSH_OFFSET_TYPE(global_type);
  11328. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11329. break;
  11330. }
  11331. case WASM_OP_SET_GLOBAL:
  11332. {
  11333. bool is_mutable = false;
  11334. #if WASM_ENABLE_GC != 0
  11335. WASMRefType *ref_type;
  11336. #endif
  11337. p_org = p - 1;
  11338. read_leb_uint32(p, p_end, global_idx);
  11339. if (global_idx >= global_count) {
  11340. set_error_buf(error_buf, error_buf_size, "unknown global");
  11341. goto fail;
  11342. }
  11343. is_mutable =
  11344. global_idx < module->import_global_count
  11345. ? module->import_globals[global_idx].u.global.is_mutable
  11346. : module
  11347. ->globals[global_idx
  11348. - module->import_global_count]
  11349. .is_mutable;
  11350. if (!is_mutable) {
  11351. #if WASM_ENABLE_GC == 0
  11352. set_error_buf(error_buf, error_buf_size,
  11353. "global is immutable");
  11354. #else
  11355. set_error_buf(error_buf, error_buf_size,
  11356. "immutable global");
  11357. #endif
  11358. goto fail;
  11359. }
  11360. global_type =
  11361. global_idx < module->import_global_count
  11362. ? module->import_globals[global_idx].u.global.type
  11363. : module
  11364. ->globals[global_idx
  11365. - module->import_global_count]
  11366. .type;
  11367. #if WASM_ENABLE_GC != 0
  11368. ref_type =
  11369. global_idx < module->import_global_count
  11370. ? module->import_globals[global_idx].u.global.ref_type
  11371. : module
  11372. ->globals[global_idx
  11373. - module->import_global_count]
  11374. .ref_type;
  11375. if (wasm_is_type_multi_byte_type(global_type)) {
  11376. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11377. wasm_reftype_struct_size(ref_type));
  11378. }
  11379. #endif
  11380. #if WASM_ENABLE_FAST_INTERP == 0
  11381. if (global_type == VALUE_TYPE_I64
  11382. || global_type == VALUE_TYPE_F64) {
  11383. #if WASM_ENABLE_DEBUG_INTERP != 0
  11384. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11385. error_buf_size)) {
  11386. goto fail;
  11387. }
  11388. #endif
  11389. *p_org = WASM_OP_SET_GLOBAL_64;
  11390. }
  11391. else if (module->aux_stack_size > 0
  11392. && global_idx == module->aux_stack_top_global_index) {
  11393. #if WASM_ENABLE_DEBUG_INTERP != 0
  11394. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11395. error_buf_size)) {
  11396. goto fail;
  11397. }
  11398. #endif
  11399. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11400. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11401. func->has_op_set_global_aux_stack = true;
  11402. #endif
  11403. }
  11404. #else /* else of WASM_ENABLE_FAST_INTERP */
  11405. if (global_type == VALUE_TYPE_I64
  11406. || global_type == VALUE_TYPE_F64) {
  11407. skip_label();
  11408. emit_label(WASM_OP_SET_GLOBAL_64);
  11409. }
  11410. else if (module->aux_stack_size > 0
  11411. && global_idx == module->aux_stack_top_global_index) {
  11412. skip_label();
  11413. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11414. }
  11415. emit_uint32(loader_ctx, global_idx);
  11416. POP_OFFSET_TYPE(global_type);
  11417. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11418. POP_TYPE(global_type);
  11419. break;
  11420. }
  11421. /* load */
  11422. case WASM_OP_I32_LOAD:
  11423. case WASM_OP_I32_LOAD8_S:
  11424. case WASM_OP_I32_LOAD8_U:
  11425. case WASM_OP_I32_LOAD16_S:
  11426. case WASM_OP_I32_LOAD16_U:
  11427. case WASM_OP_I64_LOAD:
  11428. case WASM_OP_I64_LOAD8_S:
  11429. case WASM_OP_I64_LOAD8_U:
  11430. case WASM_OP_I64_LOAD16_S:
  11431. case WASM_OP_I64_LOAD16_U:
  11432. case WASM_OP_I64_LOAD32_S:
  11433. case WASM_OP_I64_LOAD32_U:
  11434. case WASM_OP_F32_LOAD:
  11435. case WASM_OP_F64_LOAD:
  11436. /* store */
  11437. case WASM_OP_I32_STORE:
  11438. case WASM_OP_I32_STORE8:
  11439. case WASM_OP_I32_STORE16:
  11440. case WASM_OP_I64_STORE:
  11441. case WASM_OP_I64_STORE8:
  11442. case WASM_OP_I64_STORE16:
  11443. case WASM_OP_I64_STORE32:
  11444. case WASM_OP_F32_STORE:
  11445. case WASM_OP_F64_STORE:
  11446. {
  11447. #if WASM_ENABLE_FAST_INTERP != 0
  11448. /* change F32/F64 into I32/I64 */
  11449. if (opcode == WASM_OP_F32_LOAD) {
  11450. skip_label();
  11451. emit_label(WASM_OP_I32_LOAD);
  11452. }
  11453. else if (opcode == WASM_OP_F64_LOAD) {
  11454. skip_label();
  11455. emit_label(WASM_OP_I64_LOAD);
  11456. }
  11457. else if (opcode == WASM_OP_F32_STORE) {
  11458. skip_label();
  11459. emit_label(WASM_OP_I32_STORE);
  11460. }
  11461. else if (opcode == WASM_OP_F64_STORE) {
  11462. skip_label();
  11463. emit_label(WASM_OP_I64_STORE);
  11464. }
  11465. #endif
  11466. CHECK_MEMORY();
  11467. read_leb_uint32(p, p_end, align); /* align */
  11468. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11469. if (!check_memory_access_align(opcode, align, error_buf,
  11470. error_buf_size)) {
  11471. goto fail;
  11472. }
  11473. #if WASM_ENABLE_FAST_INTERP != 0
  11474. emit_uint32(loader_ctx, mem_offset);
  11475. #endif
  11476. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11477. func->has_memory_operations = true;
  11478. #endif
  11479. switch (opcode) {
  11480. /* load */
  11481. case WASM_OP_I32_LOAD:
  11482. case WASM_OP_I32_LOAD8_S:
  11483. case WASM_OP_I32_LOAD8_U:
  11484. case WASM_OP_I32_LOAD16_S:
  11485. case WASM_OP_I32_LOAD16_U:
  11486. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11487. break;
  11488. case WASM_OP_I64_LOAD:
  11489. case WASM_OP_I64_LOAD8_S:
  11490. case WASM_OP_I64_LOAD8_U:
  11491. case WASM_OP_I64_LOAD16_S:
  11492. case WASM_OP_I64_LOAD16_U:
  11493. case WASM_OP_I64_LOAD32_S:
  11494. case WASM_OP_I64_LOAD32_U:
  11495. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11496. break;
  11497. case WASM_OP_F32_LOAD:
  11498. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11499. break;
  11500. case WASM_OP_F64_LOAD:
  11501. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  11502. break;
  11503. /* store */
  11504. case WASM_OP_I32_STORE:
  11505. case WASM_OP_I32_STORE8:
  11506. case WASM_OP_I32_STORE16:
  11507. POP_I32();
  11508. POP_I32();
  11509. break;
  11510. case WASM_OP_I64_STORE:
  11511. case WASM_OP_I64_STORE8:
  11512. case WASM_OP_I64_STORE16:
  11513. case WASM_OP_I64_STORE32:
  11514. POP_I64();
  11515. POP_I32();
  11516. break;
  11517. case WASM_OP_F32_STORE:
  11518. POP_F32();
  11519. POP_I32();
  11520. break;
  11521. case WASM_OP_F64_STORE:
  11522. POP_F64();
  11523. POP_I32();
  11524. break;
  11525. default:
  11526. break;
  11527. }
  11528. break;
  11529. }
  11530. case WASM_OP_MEMORY_SIZE:
  11531. CHECK_MEMORY();
  11532. /* reserved byte 0x00 */
  11533. if (*p++ != 0x00) {
  11534. set_error_buf(error_buf, error_buf_size,
  11535. "zero byte expected");
  11536. goto fail;
  11537. }
  11538. PUSH_I32();
  11539. module->possible_memory_grow = true;
  11540. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11541. func->has_memory_operations = true;
  11542. #endif
  11543. break;
  11544. case WASM_OP_MEMORY_GROW:
  11545. CHECK_MEMORY();
  11546. /* reserved byte 0x00 */
  11547. if (*p++ != 0x00) {
  11548. set_error_buf(error_buf, error_buf_size,
  11549. "zero byte expected");
  11550. goto fail;
  11551. }
  11552. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11553. module->possible_memory_grow = true;
  11554. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11555. || WASM_ENABLE_WAMR_COMPILER != 0
  11556. func->has_op_memory_grow = true;
  11557. #endif
  11558. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11559. func->has_memory_operations = true;
  11560. #endif
  11561. break;
  11562. case WASM_OP_I32_CONST:
  11563. read_leb_int32(p, p_end, i32_const);
  11564. #if WASM_ENABLE_FAST_INTERP != 0
  11565. skip_label();
  11566. disable_emit = true;
  11567. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11568. if (operand_offset == 0) {
  11569. disable_emit = false;
  11570. emit_label(WASM_OP_I32_CONST);
  11571. emit_uint32(loader_ctx, i32_const);
  11572. }
  11573. #else
  11574. (void)i32_const;
  11575. #endif
  11576. PUSH_I32();
  11577. break;
  11578. case WASM_OP_I64_CONST:
  11579. read_leb_int64(p, p_end, i64_const);
  11580. #if WASM_ENABLE_FAST_INTERP != 0
  11581. skip_label();
  11582. disable_emit = true;
  11583. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11584. if (operand_offset == 0) {
  11585. disable_emit = false;
  11586. emit_label(WASM_OP_I64_CONST);
  11587. emit_uint64(loader_ctx, i64_const);
  11588. }
  11589. #endif
  11590. PUSH_I64();
  11591. break;
  11592. case WASM_OP_F32_CONST:
  11593. p += sizeof(float32);
  11594. #if WASM_ENABLE_FAST_INTERP != 0
  11595. skip_label();
  11596. disable_emit = true;
  11597. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11598. sizeof(float32));
  11599. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11600. if (operand_offset == 0) {
  11601. disable_emit = false;
  11602. emit_label(WASM_OP_F32_CONST);
  11603. emit_float32(loader_ctx, f32_const);
  11604. }
  11605. #endif
  11606. PUSH_F32();
  11607. break;
  11608. case WASM_OP_F64_CONST:
  11609. p += sizeof(float64);
  11610. #if WASM_ENABLE_FAST_INTERP != 0
  11611. skip_label();
  11612. disable_emit = true;
  11613. /* Some MCU may require 8-byte align */
  11614. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  11615. sizeof(float64));
  11616. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  11617. if (operand_offset == 0) {
  11618. disable_emit = false;
  11619. emit_label(WASM_OP_F64_CONST);
  11620. emit_float64(loader_ctx, f64_const);
  11621. }
  11622. #endif
  11623. PUSH_F64();
  11624. break;
  11625. case WASM_OP_I32_EQZ:
  11626. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11627. break;
  11628. case WASM_OP_I32_EQ:
  11629. case WASM_OP_I32_NE:
  11630. case WASM_OP_I32_LT_S:
  11631. case WASM_OP_I32_LT_U:
  11632. case WASM_OP_I32_GT_S:
  11633. case WASM_OP_I32_GT_U:
  11634. case WASM_OP_I32_LE_S:
  11635. case WASM_OP_I32_LE_U:
  11636. case WASM_OP_I32_GE_S:
  11637. case WASM_OP_I32_GE_U:
  11638. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11639. break;
  11640. case WASM_OP_I64_EQZ:
  11641. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11642. break;
  11643. case WASM_OP_I64_EQ:
  11644. case WASM_OP_I64_NE:
  11645. case WASM_OP_I64_LT_S:
  11646. case WASM_OP_I64_LT_U:
  11647. case WASM_OP_I64_GT_S:
  11648. case WASM_OP_I64_GT_U:
  11649. case WASM_OP_I64_LE_S:
  11650. case WASM_OP_I64_LE_U:
  11651. case WASM_OP_I64_GE_S:
  11652. case WASM_OP_I64_GE_U:
  11653. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11654. break;
  11655. case WASM_OP_F32_EQ:
  11656. case WASM_OP_F32_NE:
  11657. case WASM_OP_F32_LT:
  11658. case WASM_OP_F32_GT:
  11659. case WASM_OP_F32_LE:
  11660. case WASM_OP_F32_GE:
  11661. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11662. break;
  11663. case WASM_OP_F64_EQ:
  11664. case WASM_OP_F64_NE:
  11665. case WASM_OP_F64_LT:
  11666. case WASM_OP_F64_GT:
  11667. case WASM_OP_F64_LE:
  11668. case WASM_OP_F64_GE:
  11669. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11670. break;
  11671. case WASM_OP_I32_CLZ:
  11672. case WASM_OP_I32_CTZ:
  11673. case WASM_OP_I32_POPCNT:
  11674. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11675. break;
  11676. case WASM_OP_I32_ADD:
  11677. case WASM_OP_I32_SUB:
  11678. case WASM_OP_I32_MUL:
  11679. case WASM_OP_I32_DIV_S:
  11680. case WASM_OP_I32_DIV_U:
  11681. case WASM_OP_I32_REM_S:
  11682. case WASM_OP_I32_REM_U:
  11683. case WASM_OP_I32_AND:
  11684. case WASM_OP_I32_OR:
  11685. case WASM_OP_I32_XOR:
  11686. case WASM_OP_I32_SHL:
  11687. case WASM_OP_I32_SHR_S:
  11688. case WASM_OP_I32_SHR_U:
  11689. case WASM_OP_I32_ROTL:
  11690. case WASM_OP_I32_ROTR:
  11691. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11692. break;
  11693. case WASM_OP_I64_CLZ:
  11694. case WASM_OP_I64_CTZ:
  11695. case WASM_OP_I64_POPCNT:
  11696. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11697. break;
  11698. case WASM_OP_I64_ADD:
  11699. case WASM_OP_I64_SUB:
  11700. case WASM_OP_I64_MUL:
  11701. case WASM_OP_I64_DIV_S:
  11702. case WASM_OP_I64_DIV_U:
  11703. case WASM_OP_I64_REM_S:
  11704. case WASM_OP_I64_REM_U:
  11705. case WASM_OP_I64_AND:
  11706. case WASM_OP_I64_OR:
  11707. case WASM_OP_I64_XOR:
  11708. case WASM_OP_I64_SHL:
  11709. case WASM_OP_I64_SHR_S:
  11710. case WASM_OP_I64_SHR_U:
  11711. case WASM_OP_I64_ROTL:
  11712. case WASM_OP_I64_ROTR:
  11713. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11714. break;
  11715. case WASM_OP_F32_ABS:
  11716. case WASM_OP_F32_NEG:
  11717. case WASM_OP_F32_CEIL:
  11718. case WASM_OP_F32_FLOOR:
  11719. case WASM_OP_F32_TRUNC:
  11720. case WASM_OP_F32_NEAREST:
  11721. case WASM_OP_F32_SQRT:
  11722. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11723. break;
  11724. case WASM_OP_F32_ADD:
  11725. case WASM_OP_F32_SUB:
  11726. case WASM_OP_F32_MUL:
  11727. case WASM_OP_F32_DIV:
  11728. case WASM_OP_F32_MIN:
  11729. case WASM_OP_F32_MAX:
  11730. case WASM_OP_F32_COPYSIGN:
  11731. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11732. break;
  11733. case WASM_OP_F64_ABS:
  11734. case WASM_OP_F64_NEG:
  11735. case WASM_OP_F64_CEIL:
  11736. case WASM_OP_F64_FLOOR:
  11737. case WASM_OP_F64_TRUNC:
  11738. case WASM_OP_F64_NEAREST:
  11739. case WASM_OP_F64_SQRT:
  11740. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11741. break;
  11742. case WASM_OP_F64_ADD:
  11743. case WASM_OP_F64_SUB:
  11744. case WASM_OP_F64_MUL:
  11745. case WASM_OP_F64_DIV:
  11746. case WASM_OP_F64_MIN:
  11747. case WASM_OP_F64_MAX:
  11748. case WASM_OP_F64_COPYSIGN:
  11749. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11750. break;
  11751. case WASM_OP_I32_WRAP_I64:
  11752. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11753. break;
  11754. case WASM_OP_I32_TRUNC_S_F32:
  11755. case WASM_OP_I32_TRUNC_U_F32:
  11756. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11757. break;
  11758. case WASM_OP_I32_TRUNC_S_F64:
  11759. case WASM_OP_I32_TRUNC_U_F64:
  11760. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11761. break;
  11762. case WASM_OP_I64_EXTEND_S_I32:
  11763. case WASM_OP_I64_EXTEND_U_I32:
  11764. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11765. break;
  11766. case WASM_OP_I64_TRUNC_S_F32:
  11767. case WASM_OP_I64_TRUNC_U_F32:
  11768. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  11769. break;
  11770. case WASM_OP_I64_TRUNC_S_F64:
  11771. case WASM_OP_I64_TRUNC_U_F64:
  11772. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  11773. break;
  11774. case WASM_OP_F32_CONVERT_S_I32:
  11775. case WASM_OP_F32_CONVERT_U_I32:
  11776. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11777. break;
  11778. case WASM_OP_F32_CONVERT_S_I64:
  11779. case WASM_OP_F32_CONVERT_U_I64:
  11780. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  11781. break;
  11782. case WASM_OP_F32_DEMOTE_F64:
  11783. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  11784. break;
  11785. case WASM_OP_F64_CONVERT_S_I32:
  11786. case WASM_OP_F64_CONVERT_U_I32:
  11787. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  11788. break;
  11789. case WASM_OP_F64_CONVERT_S_I64:
  11790. case WASM_OP_F64_CONVERT_U_I64:
  11791. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  11792. break;
  11793. case WASM_OP_F64_PROMOTE_F32:
  11794. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  11795. break;
  11796. case WASM_OP_I32_REINTERPRET_F32:
  11797. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11798. break;
  11799. case WASM_OP_I64_REINTERPRET_F64:
  11800. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  11801. break;
  11802. case WASM_OP_F32_REINTERPRET_I32:
  11803. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11804. break;
  11805. case WASM_OP_F64_REINTERPRET_I64:
  11806. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  11807. break;
  11808. case WASM_OP_I32_EXTEND8_S:
  11809. case WASM_OP_I32_EXTEND16_S:
  11810. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11811. break;
  11812. case WASM_OP_I64_EXTEND8_S:
  11813. case WASM_OP_I64_EXTEND16_S:
  11814. case WASM_OP_I64_EXTEND32_S:
  11815. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11816. break;
  11817. #if WASM_ENABLE_GC != 0
  11818. case WASM_OP_GC_PREFIX:
  11819. {
  11820. uint32 opcode1;
  11821. read_leb_uint32(p, p_end, opcode1);
  11822. #if WASM_ENABLE_FAST_INTERP != 0
  11823. emit_byte(loader_ctx, ((uint8)opcode1));
  11824. #endif
  11825. switch (opcode1) {
  11826. case WASM_OP_STRUCT_NEW:
  11827. case WASM_OP_STRUCT_NEW_DEFAULT:
  11828. {
  11829. read_leb_uint32(p, p_end, type_idx);
  11830. #if WASM_ENABLE_FAST_INTERP != 0
  11831. emit_uint32(loader_ctx, type_idx);
  11832. #endif
  11833. if (!check_type_index(module, type_idx, error_buf,
  11834. error_buf_size)) {
  11835. goto fail;
  11836. }
  11837. if (module->types[type_idx]->type_flag
  11838. != WASM_TYPE_STRUCT) {
  11839. set_error_buf(error_buf, error_buf_size,
  11840. "unkown struct type");
  11841. goto fail;
  11842. }
  11843. if (opcode1 == WASM_OP_STRUCT_NEW) {
  11844. int32 j, k;
  11845. uint8 value_type;
  11846. uint32 ref_type_struct_size;
  11847. WASMStructType *struct_type =
  11848. (WASMStructType *)module->types[type_idx];
  11849. k = struct_type->ref_type_map_count - 1;
  11850. for (j = struct_type->field_count - 1; j >= 0;
  11851. j--) {
  11852. value_type = struct_type->fields[j].field_type;
  11853. if (wasm_is_type_reftype(value_type)) {
  11854. if (wasm_is_type_multi_byte_type(
  11855. value_type)) {
  11856. ref_type_struct_size =
  11857. wasm_reftype_struct_size(
  11858. struct_type->ref_type_maps[k]
  11859. .ref_type);
  11860. bh_memcpy_s(
  11861. &wasm_ref_type,
  11862. (uint32)sizeof(WASMRefType),
  11863. struct_type->ref_type_maps[k]
  11864. .ref_type,
  11865. ref_type_struct_size);
  11866. k--;
  11867. }
  11868. POP_REF(value_type);
  11869. }
  11870. else {
  11871. switch (value_type) {
  11872. case VALUE_TYPE_I32:
  11873. case PACKED_TYPE_I8:
  11874. case PACKED_TYPE_I16:
  11875. POP_I32();
  11876. break;
  11877. case VALUE_TYPE_I64:
  11878. POP_I64();
  11879. break;
  11880. case VALUE_TYPE_F32:
  11881. POP_F32();
  11882. break;
  11883. case VALUE_TYPE_F64:
  11884. POP_F64();
  11885. break;
  11886. default:
  11887. set_error_buf(error_buf,
  11888. error_buf_size,
  11889. "unknown type");
  11890. goto fail;
  11891. }
  11892. }
  11893. }
  11894. }
  11895. /* PUSH struct obj, (ref $t) */
  11896. wasm_set_refheaptype_typeidx(
  11897. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  11898. PUSH_REF(wasm_ref_type.ref_type);
  11899. break;
  11900. }
  11901. case WASM_OP_STRUCT_GET:
  11902. case WASM_OP_STRUCT_GET_S:
  11903. case WASM_OP_STRUCT_GET_U:
  11904. case WASM_OP_STRUCT_SET:
  11905. {
  11906. WASMStructType *struct_type;
  11907. WASMRefType *ref_type = NULL;
  11908. uint32 field_idx;
  11909. uint8 field_type;
  11910. read_leb_uint32(p, p_end, type_idx);
  11911. #if WASM_ENABLE_FAST_INTERP != 0
  11912. emit_uint32(loader_ctx, type_idx);
  11913. #endif
  11914. if (!check_type_index(module, type_idx, error_buf,
  11915. error_buf_size)) {
  11916. goto fail;
  11917. }
  11918. if (module->types[type_idx]->type_flag
  11919. != WASM_TYPE_STRUCT) {
  11920. set_error_buf(error_buf, error_buf_size,
  11921. "unknown struct type");
  11922. goto fail;
  11923. }
  11924. struct_type = (WASMStructType *)module->types[type_idx];
  11925. read_leb_uint32(p, p_end, field_idx);
  11926. #if WASM_ENABLE_FAST_INTERP != 0
  11927. emit_uint32(loader_ctx, field_idx);
  11928. #endif
  11929. if (field_idx >= struct_type->field_count) {
  11930. set_error_buf(error_buf, error_buf_size,
  11931. "unknown struct field");
  11932. goto fail;
  11933. }
  11934. if (opcode1 == WASM_OP_STRUCT_SET
  11935. && !(struct_type->fields[field_idx].field_flags
  11936. & 1)) {
  11937. set_error_buf(error_buf, error_buf_size,
  11938. "field is immutable");
  11939. goto fail;
  11940. }
  11941. field_type = struct_type->fields[field_idx].field_type;
  11942. if (is_packed_type(field_type)) {
  11943. if (opcode1 == WASM_OP_STRUCT_GET) {
  11944. set_error_buf(error_buf, error_buf_size,
  11945. "type mismatch");
  11946. goto fail;
  11947. }
  11948. else {
  11949. field_type = VALUE_TYPE_I32;
  11950. }
  11951. }
  11952. if (wasm_is_type_multi_byte_type(field_type)) {
  11953. ref_type = wasm_reftype_map_find(
  11954. struct_type->ref_type_maps,
  11955. struct_type->ref_type_map_count, field_idx);
  11956. bh_assert(ref_type);
  11957. }
  11958. if (opcode1 == WASM_OP_STRUCT_SET) {
  11959. /* POP field */
  11960. if (wasm_is_type_multi_byte_type(field_type)) {
  11961. bh_memcpy_s(&wasm_ref_type,
  11962. (uint32)sizeof(WASMRefType),
  11963. ref_type,
  11964. wasm_reftype_struct_size(ref_type));
  11965. }
  11966. POP_REF(field_type);
  11967. /* POP struct obj, (ref null $t) */
  11968. wasm_set_refheaptype_typeidx(
  11969. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  11970. POP_REF(wasm_ref_type.ref_type);
  11971. }
  11972. else {
  11973. /* POP struct obj, (ref null $t) */
  11974. wasm_set_refheaptype_typeidx(
  11975. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  11976. POP_REF(wasm_ref_type.ref_type);
  11977. /* PUSH field */
  11978. if (wasm_is_type_multi_byte_type(field_type)) {
  11979. bh_memcpy_s(&wasm_ref_type,
  11980. (uint32)sizeof(WASMRefType),
  11981. ref_type,
  11982. wasm_reftype_struct_size(ref_type));
  11983. }
  11984. PUSH_REF(field_type);
  11985. }
  11986. break;
  11987. }
  11988. case WASM_OP_ARRAY_NEW:
  11989. case WASM_OP_ARRAY_NEW_DEFAULT:
  11990. case WASM_OP_ARRAY_NEW_FIXED:
  11991. case WASM_OP_ARRAY_NEW_DATA:
  11992. case WASM_OP_ARRAY_NEW_ELEM:
  11993. {
  11994. WASMArrayType *array_type;
  11995. uint8 elem_type;
  11996. uint32 u32 = 0;
  11997. read_leb_uint32(p, p_end, type_idx);
  11998. #if WASM_ENABLE_FAST_INTERP != 0
  11999. emit_uint32(loader_ctx, type_idx);
  12000. #endif
  12001. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12002. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12003. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12004. read_leb_uint32(p, p_end, u32);
  12005. #if WASM_ENABLE_FAST_INTERP != 0
  12006. emit_uint32(loader_ctx, u32);
  12007. #endif
  12008. }
  12009. if (!check_array_type(module, type_idx, error_buf,
  12010. error_buf_size)) {
  12011. goto fail;
  12012. }
  12013. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12014. /* length */
  12015. POP_I32();
  12016. }
  12017. array_type = (WASMArrayType *)module->types[type_idx];
  12018. elem_type = array_type->elem_type;
  12019. if (opcode1 == WASM_OP_ARRAY_NEW
  12020. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12021. if (wasm_is_type_multi_byte_type(elem_type)) {
  12022. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12023. array_type->elem_ref_type,
  12024. wasm_reftype_struct_size(
  12025. array_type->elem_ref_type));
  12026. }
  12027. if (is_packed_type(elem_type)) {
  12028. elem_type = VALUE_TYPE_I32;
  12029. }
  12030. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12031. uint32 N = u32;
  12032. for (i = 0; i < N; i++) {
  12033. if (wasm_is_type_multi_byte_type(
  12034. elem_type)) {
  12035. bh_memcpy_s(
  12036. &wasm_ref_type, sizeof(WASMRefType),
  12037. array_type->elem_ref_type,
  12038. wasm_reftype_struct_size(
  12039. array_type->elem_ref_type));
  12040. }
  12041. POP_REF(elem_type);
  12042. }
  12043. }
  12044. else
  12045. POP_REF(elem_type);
  12046. }
  12047. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12048. /* offset of data segment */
  12049. POP_I32();
  12050. if (u32 >= module->data_seg_count) {
  12051. set_error_buf(error_buf, error_buf_size,
  12052. "unknown data segement");
  12053. goto fail;
  12054. }
  12055. if (wasm_is_type_reftype(elem_type)) {
  12056. set_error_buf(error_buf, error_buf_size,
  12057. "array elem type mismatch");
  12058. goto fail;
  12059. }
  12060. }
  12061. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12062. WASMTableSeg *table_seg =
  12063. module->table_segments + u32;
  12064. /* offset of element segment */
  12065. POP_I32();
  12066. if (u32 >= module->table_seg_count) {
  12067. set_error_buf(error_buf, error_buf_size,
  12068. "unknown element segement");
  12069. goto fail;
  12070. }
  12071. if (!wasm_reftype_is_subtype_of(
  12072. table_seg->elem_type,
  12073. table_seg->elem_ref_type, elem_type,
  12074. array_type->elem_ref_type, module->types,
  12075. module->type_count)) {
  12076. set_error_buf(error_buf, error_buf_size,
  12077. "array elem type mismatch");
  12078. goto fail;
  12079. }
  12080. }
  12081. /* PUSH array obj, (ref $t) */
  12082. wasm_set_refheaptype_typeidx(
  12083. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12084. PUSH_REF(wasm_ref_type.ref_type);
  12085. break;
  12086. }
  12087. case WASM_OP_ARRAY_GET:
  12088. case WASM_OP_ARRAY_GET_S:
  12089. case WASM_OP_ARRAY_GET_U:
  12090. case WASM_OP_ARRAY_SET:
  12091. {
  12092. uint8 elem_type;
  12093. WASMArrayType *array_type;
  12094. WASMRefType *ref_type = NULL;
  12095. read_leb_uint32(p, p_end, type_idx);
  12096. #if WASM_ENABLE_FAST_INTERP != 0
  12097. emit_uint32(loader_ctx, type_idx);
  12098. #endif
  12099. if (!check_array_type(module, type_idx, error_buf,
  12100. error_buf_size)) {
  12101. goto fail;
  12102. }
  12103. array_type = (WASMArrayType *)module->types[type_idx];
  12104. if (opcode1 == WASM_OP_ARRAY_SET
  12105. && !(array_type->elem_flags & 1)) {
  12106. set_error_buf(error_buf, error_buf_size,
  12107. "array is immutable");
  12108. goto fail;
  12109. }
  12110. elem_type = array_type->elem_type;
  12111. if (is_packed_type(elem_type)) {
  12112. if (opcode1 != WASM_OP_ARRAY_GET_S
  12113. && opcode1 != WASM_OP_ARRAY_GET_U
  12114. && opcode1 != WASM_OP_ARRAY_SET) {
  12115. set_error_buf(error_buf, error_buf_size,
  12116. "type mismatch");
  12117. goto fail;
  12118. }
  12119. else {
  12120. elem_type = VALUE_TYPE_I32;
  12121. }
  12122. }
  12123. ref_type = array_type->elem_ref_type;
  12124. if (opcode1 == WASM_OP_ARRAY_SET) {
  12125. /* POP elem to set */
  12126. if (wasm_is_type_multi_byte_type(elem_type)) {
  12127. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12128. ref_type,
  12129. wasm_reftype_struct_size(ref_type));
  12130. }
  12131. POP_REF(elem_type);
  12132. }
  12133. /* elem idx */
  12134. POP_I32();
  12135. /* POP array obj, (ref null $t) */
  12136. wasm_set_refheaptype_typeidx(
  12137. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12138. POP_REF(wasm_ref_type.ref_type);
  12139. if (opcode1 != WASM_OP_ARRAY_SET) {
  12140. /* PUSH elem */
  12141. if (wasm_is_type_multi_byte_type(elem_type)) {
  12142. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12143. ref_type,
  12144. wasm_reftype_struct_size(ref_type));
  12145. }
  12146. PUSH_REF(elem_type);
  12147. }
  12148. break;
  12149. }
  12150. case WASM_OP_ARRAY_LEN:
  12151. {
  12152. POP_REF(REF_TYPE_ARRAYREF);
  12153. /* length */
  12154. PUSH_I32();
  12155. break;
  12156. }
  12157. case WASM_OP_ARRAY_FILL:
  12158. {
  12159. WASMArrayType *array_type;
  12160. uint8 elem_type;
  12161. /* typeidx */
  12162. read_leb_uint32(p, p_end, type_idx);
  12163. #if WASM_ENABLE_FAST_INTERP != 0
  12164. emit_uint32(loader_ctx, type_idx);
  12165. #endif
  12166. if (!check_array_type(module, type_idx, error_buf,
  12167. error_buf_size)) {
  12168. goto fail;
  12169. }
  12170. array_type = (WASMArrayType *)module->types[type_idx];
  12171. if (!(array_type->elem_flags & 1)) {
  12172. set_error_buf(error_buf, error_buf_size,
  12173. "array is immutable");
  12174. goto fail;
  12175. }
  12176. elem_type = array_type->elem_type;
  12177. if (is_packed_type(elem_type)) {
  12178. elem_type = VALUE_TYPE_I32;
  12179. }
  12180. POP_I32(); /* length */
  12181. #if WASM_ENABLE_FAST_INTERP != 0
  12182. POP_OFFSET_TYPE(elem_type);
  12183. #endif
  12184. POP_TYPE(elem_type);
  12185. POP_I32(); /* start */
  12186. /* POP array obj, (ref null $t) */
  12187. wasm_set_refheaptype_typeidx(
  12188. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12189. POP_REF(wasm_ref_type.ref_type);
  12190. break;
  12191. }
  12192. case WASM_OP_ARRAY_COPY:
  12193. {
  12194. uint32 src_type_idx;
  12195. uint8 src_elem_type, dst_elem_type;
  12196. WASMRefType src_ref_type = { 0 },
  12197. *src_elem_ref_type = NULL;
  12198. WASMRefType dst_ref_type = { 0 },
  12199. *dst_elem_ref_type = NULL;
  12200. WASMArrayType *array_type;
  12201. /* typeidx1 */
  12202. read_leb_uint32(p, p_end, type_idx);
  12203. #if WASM_ENABLE_FAST_INTERP != 0
  12204. emit_uint32(loader_ctx, type_idx);
  12205. #endif
  12206. /* typeidx2 */
  12207. read_leb_uint32(p, p_end, src_type_idx);
  12208. #if WASM_ENABLE_FAST_INTERP != 0
  12209. emit_uint32(loader_ctx, src_type_idx);
  12210. #endif
  12211. if (!check_array_type(module, type_idx, error_buf,
  12212. error_buf_size)) {
  12213. goto fail;
  12214. }
  12215. if (!check_array_type(module, src_type_idx, error_buf,
  12216. error_buf_size)) {
  12217. goto fail;
  12218. }
  12219. POP_I32();
  12220. POP_I32();
  12221. /* POP array obj, (ref null $t) */
  12222. wasm_set_refheaptype_typeidx(
  12223. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12224. POP_REF(wasm_ref_type.ref_type);
  12225. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12226. &wasm_ref_type,
  12227. wasm_reftype_struct_size(&wasm_ref_type));
  12228. POP_I32();
  12229. /* POP array obj, (ref null $t) */
  12230. wasm_set_refheaptype_typeidx(
  12231. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12232. POP_REF(wasm_ref_type.ref_type);
  12233. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12234. &wasm_ref_type,
  12235. wasm_reftype_struct_size(&wasm_ref_type));
  12236. array_type = (WASMArrayType *)module->types[type_idx];
  12237. if (!(array_type->elem_flags & 1)) {
  12238. set_error_buf(error_buf, error_buf_size,
  12239. "destination array is immutable");
  12240. goto fail;
  12241. }
  12242. dst_elem_type = array_type->elem_type;
  12243. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12244. dst_elem_ref_type = array_type->elem_ref_type;
  12245. }
  12246. array_type =
  12247. (WASMArrayType *)module->types[src_type_idx];
  12248. src_elem_type = array_type->elem_type;
  12249. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12250. src_elem_ref_type = array_type->elem_ref_type;
  12251. }
  12252. if (!wasm_reftype_is_subtype_of(
  12253. src_elem_type, src_elem_ref_type, dst_elem_type,
  12254. dst_elem_ref_type, module->types,
  12255. module->type_count)) {
  12256. set_error_buf(error_buf, error_buf_size,
  12257. "array types do not match");
  12258. goto fail;
  12259. }
  12260. break;
  12261. }
  12262. case WASM_OP_REF_I31:
  12263. {
  12264. POP_I32();
  12265. wasm_set_refheaptype_common(
  12266. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12267. PUSH_REF(wasm_ref_type.ref_type);
  12268. break;
  12269. }
  12270. case WASM_OP_I31_GET_S:
  12271. case WASM_OP_I31_GET_U:
  12272. {
  12273. POP_REF(REF_TYPE_I31REF);
  12274. PUSH_I32();
  12275. break;
  12276. }
  12277. case WASM_OP_REF_TEST:
  12278. case WASM_OP_REF_CAST:
  12279. case WASM_OP_REF_TEST_NULLABLE:
  12280. case WASM_OP_REF_CAST_NULLABLE:
  12281. {
  12282. uint8 type;
  12283. read_leb_int32(p, p_end, heap_type);
  12284. #if WASM_ENABLE_FAST_INTERP != 0
  12285. emit_uint32(loader_ctx, (uint32)heap_type);
  12286. #endif
  12287. if (heap_type >= 0) {
  12288. if (!check_type_index(module, heap_type, error_buf,
  12289. error_buf_size)) {
  12290. goto fail;
  12291. }
  12292. }
  12293. else {
  12294. if (!wasm_is_valid_heap_type(heap_type)) {
  12295. set_error_buf(error_buf, error_buf_size,
  12296. "unknown type");
  12297. goto fail;
  12298. }
  12299. }
  12300. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12301. &wasm_ref_type, error_buf,
  12302. error_buf_size)) {
  12303. goto fail;
  12304. }
  12305. if (opcode1 == WASM_OP_REF_TEST
  12306. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12307. PUSH_I32();
  12308. else {
  12309. bool nullable =
  12310. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12311. : false;
  12312. if (heap_type >= 0 || !nullable) {
  12313. wasm_set_refheaptype_typeidx(
  12314. &wasm_ref_type.ref_ht_typeidx, nullable,
  12315. heap_type);
  12316. PUSH_REF(wasm_ref_type.ref_type);
  12317. }
  12318. else {
  12319. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12320. }
  12321. }
  12322. break;
  12323. }
  12324. case WASM_OP_BR_ON_CAST:
  12325. case WASM_OP_BR_ON_CAST_FAIL:
  12326. {
  12327. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12328. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12329. uint8 type_tmp, castflags;
  12330. uint32 depth;
  12331. int32 heap_type_dst;
  12332. bool src_nullable, dst_nullable;
  12333. CHECK_BUF(p, p_end, 1);
  12334. castflags = read_uint8(p);
  12335. #if WASM_ENABLE_FAST_INTERP != 0
  12336. /* Emit heap_type firstly */
  12337. emit_byte(loader_ctx, castflags);
  12338. #endif
  12339. p_org = p;
  12340. read_leb_uint32(p, p_end, depth);
  12341. read_leb_int32(p, p_end, heap_type);
  12342. #if WASM_ENABLE_FAST_INTERP != 0
  12343. /* Emit heap_type firstly */
  12344. emit_uint32(loader_ctx, (uint32)heap_type);
  12345. #endif
  12346. read_leb_int32(p, p_end, heap_type_dst);
  12347. #if WASM_ENABLE_FAST_INTERP != 0
  12348. /* Emit heap_type firstly */
  12349. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12350. #endif
  12351. (void)depth;
  12352. /*
  12353. * castflags should be 0~3:
  12354. * 0: (non-null, non-null)
  12355. * 1: (null, non-null)
  12356. * 2: (non-null, null)
  12357. * 3: (null, null)
  12358. */
  12359. if (castflags > 3) {
  12360. set_error_buf(error_buf, error_buf_size,
  12361. "invalid castflags");
  12362. break;
  12363. }
  12364. src_nullable =
  12365. (castflags == 1) || (castflags == 3) ? true : false;
  12366. dst_nullable =
  12367. (castflags == 2) || (castflags == 3) ? true : false;
  12368. /* Pop and backup the stack top's ref type */
  12369. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12370. &ref_type_tmp, error_buf,
  12371. error_buf_size)) {
  12372. goto fail;
  12373. }
  12374. /* The reference type rt1 must be valid */
  12375. if (!init_ref_type(module, &ref_type1, src_nullable,
  12376. heap_type, error_buf,
  12377. error_buf_size)) {
  12378. goto fail;
  12379. }
  12380. /* The reference type rt2 must be valid. */
  12381. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12382. heap_type_dst, error_buf,
  12383. error_buf_size)) {
  12384. goto fail;
  12385. }
  12386. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12387. &ref_type2);
  12388. /* The reference type rt2 must match rt1. */
  12389. if (!wasm_reftype_is_subtype_of(
  12390. ref_type2.ref_type, &ref_type2,
  12391. ref_type1.ref_type, &ref_type1, module->types,
  12392. module->type_count)) {
  12393. set_error_buf(error_buf, error_buf_size,
  12394. "type mismatch");
  12395. goto fail;
  12396. }
  12397. p = p_org;
  12398. /* Push ref type casted for branch block check */
  12399. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12400. /* The reference type rt2 must match rt′. */
  12401. type_tmp = ref_type2.ref_type;
  12402. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12403. bh_memcpy_s(
  12404. &wasm_ref_type,
  12405. wasm_reftype_struct_size(&ref_type2),
  12406. &ref_type2,
  12407. wasm_reftype_struct_size(&ref_type2));
  12408. }
  12409. }
  12410. else {
  12411. /* The reference type rt′1 must match rt′. */
  12412. type_tmp = ref_type_diff.ref_type;
  12413. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12414. bh_memcpy_s(
  12415. &wasm_ref_type,
  12416. wasm_reftype_struct_size(&ref_type_diff),
  12417. &ref_type_diff,
  12418. wasm_reftype_struct_size(&ref_type_diff));
  12419. }
  12420. }
  12421. PUSH_REF(type_tmp);
  12422. if (!(frame_csp_tmp = check_branch_block(
  12423. loader_ctx, &p, p_end, false, error_buf,
  12424. error_buf_size))) {
  12425. goto fail;
  12426. }
  12427. /* Ignore heap_types */
  12428. skip_leb_uint32(p, p_end);
  12429. skip_leb_uint32(p, p_end);
  12430. /* Restore the original stack top's ref type */
  12431. POP_REF(type_tmp);
  12432. #if WASM_ENABLE_FAST_INTERP != 0
  12433. /* Erase the opnd offset emitted by POP_REF() */
  12434. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12435. #endif
  12436. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12437. type_tmp = ref_type_diff.ref_type;
  12438. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12439. bh_memcpy_s(
  12440. &wasm_ref_type,
  12441. wasm_reftype_struct_size(&ref_type_diff),
  12442. &ref_type_diff,
  12443. wasm_reftype_struct_size(&ref_type_diff));
  12444. }
  12445. }
  12446. else {
  12447. type_tmp = ref_type2.ref_type;
  12448. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12449. bh_memcpy_s(
  12450. &wasm_ref_type,
  12451. wasm_reftype_struct_size(&ref_type2),
  12452. &ref_type2,
  12453. wasm_reftype_struct_size(&ref_type2));
  12454. }
  12455. }
  12456. PUSH_REF(type_tmp);
  12457. #if WASM_ENABLE_FAST_INTERP != 0
  12458. /* Erase the opnd offset emitted by PUSH_REF() */
  12459. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12460. #endif
  12461. break;
  12462. }
  12463. case WASM_OP_ANY_CONVERT_EXTERN:
  12464. {
  12465. uint8 type;
  12466. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12467. &wasm_ref_type, error_buf,
  12468. error_buf_size)) {
  12469. goto fail;
  12470. }
  12471. if (!(type == REF_TYPE_EXTERNREF
  12472. || (type == REF_TYPE_HT_NON_NULLABLE
  12473. && wasm_ref_type.ref_ht_common.heap_type
  12474. == HEAP_TYPE_EXTERN)
  12475. || type == VALUE_TYPE_ANY)) {
  12476. set_error_buf(error_buf, error_buf_size,
  12477. "type mismatch");
  12478. goto fail;
  12479. }
  12480. if (type == REF_TYPE_EXTERNREF)
  12481. type = REF_TYPE_ANYREF;
  12482. else {
  12483. wasm_ref_type.ref_ht_common.heap_type =
  12484. HEAP_TYPE_ANY;
  12485. }
  12486. PUSH_REF(type);
  12487. break;
  12488. }
  12489. case WASM_OP_EXTERN_CONVERT_ANY:
  12490. {
  12491. uint8 type;
  12492. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12493. &wasm_ref_type, error_buf,
  12494. error_buf_size)) {
  12495. goto fail;
  12496. }
  12497. if (type == REF_TYPE_EXTERNREF
  12498. || ((type == REF_TYPE_HT_NULLABLE
  12499. || type == REF_TYPE_HT_NON_NULLABLE)
  12500. && wasm_ref_type.ref_ht_common.heap_type
  12501. == HEAP_TYPE_EXTERN)) {
  12502. set_error_buf(error_buf, error_buf_size,
  12503. "type mismatch");
  12504. goto fail;
  12505. }
  12506. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12507. /* push (ref null extern) */
  12508. type = REF_TYPE_EXTERNREF;
  12509. }
  12510. else {
  12511. /* push (ref extern) */
  12512. type = REF_TYPE_HT_NON_NULLABLE;
  12513. wasm_set_refheaptype_common(
  12514. &wasm_ref_type.ref_ht_common, false,
  12515. HEAP_TYPE_EXTERN);
  12516. }
  12517. PUSH_REF(type);
  12518. break;
  12519. }
  12520. #if WASM_ENABLE_STRINGREF != 0
  12521. case WASM_OP_STRING_NEW_UTF8:
  12522. case WASM_OP_STRING_NEW_WTF16:
  12523. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12524. case WASM_OP_STRING_NEW_WTF8:
  12525. {
  12526. uint32 memidx;
  12527. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12528. func->has_memory_operations = true;
  12529. #endif
  12530. read_leb_uint32(p, p_end, memidx);
  12531. #if WASM_ENABLE_FAST_INTERP != 0
  12532. emit_uint32(loader_ctx, (uint32)memidx);
  12533. #endif
  12534. POP_I32();
  12535. POP_I32();
  12536. PUSH_REF(REF_TYPE_STRINGREF);
  12537. (void)memidx;
  12538. break;
  12539. }
  12540. case WASM_OP_STRING_CONST:
  12541. {
  12542. uint32 contents;
  12543. read_leb_uint32(p, p_end, contents);
  12544. #if WASM_ENABLE_FAST_INTERP != 0
  12545. emit_uint32(loader_ctx, (uint32)contents);
  12546. #endif
  12547. PUSH_REF(REF_TYPE_STRINGREF);
  12548. (void)contents;
  12549. break;
  12550. }
  12551. case WASM_OP_STRING_MEASURE_UTF8:
  12552. case WASM_OP_STRING_MEASURE_WTF8:
  12553. case WASM_OP_STRING_MEASURE_WTF16:
  12554. {
  12555. POP_STRINGREF();
  12556. PUSH_I32();
  12557. break;
  12558. }
  12559. case WASM_OP_STRING_ENCODE_UTF8:
  12560. case WASM_OP_STRING_ENCODE_WTF16:
  12561. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12562. case WASM_OP_STRING_ENCODE_WTF8:
  12563. {
  12564. uint32 memidx;
  12565. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12566. func->has_memory_operations = true;
  12567. #endif
  12568. read_leb_uint32(p, p_end, memidx);
  12569. #if WASM_ENABLE_FAST_INTERP != 0
  12570. emit_uint32(loader_ctx, (uint32)memidx);
  12571. #endif
  12572. POP_I32();
  12573. POP_STRINGREF();
  12574. PUSH_I32();
  12575. (void)memidx;
  12576. break;
  12577. }
  12578. case WASM_OP_STRING_CONCAT:
  12579. {
  12580. POP_STRINGREF();
  12581. POP_STRINGREF();
  12582. PUSH_REF(REF_TYPE_STRINGREF);
  12583. break;
  12584. }
  12585. case WASM_OP_STRING_EQ:
  12586. {
  12587. POP_STRINGREF();
  12588. POP_STRINGREF();
  12589. PUSH_I32();
  12590. break;
  12591. }
  12592. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12593. {
  12594. POP_STRINGREF();
  12595. PUSH_I32();
  12596. break;
  12597. }
  12598. case WASM_OP_STRING_AS_WTF8:
  12599. {
  12600. POP_STRINGREF();
  12601. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  12602. break;
  12603. }
  12604. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  12605. {
  12606. POP_I32();
  12607. POP_I32();
  12608. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12609. PUSH_I32();
  12610. break;
  12611. }
  12612. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  12613. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  12614. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  12615. {
  12616. uint32 memidx;
  12617. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12618. func->has_memory_operations = true;
  12619. #endif
  12620. read_leb_uint32(p, p_end, memidx);
  12621. #if WASM_ENABLE_FAST_INTERP != 0
  12622. emit_uint32(loader_ctx, (uint32)memidx);
  12623. #endif
  12624. POP_I32();
  12625. POP_I32();
  12626. POP_I32();
  12627. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12628. PUSH_I32();
  12629. PUSH_I32();
  12630. (void)memidx;
  12631. break;
  12632. }
  12633. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  12634. {
  12635. POP_I32();
  12636. POP_I32();
  12637. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12638. PUSH_REF(REF_TYPE_STRINGREF);
  12639. break;
  12640. }
  12641. case WASM_OP_STRING_AS_WTF16:
  12642. {
  12643. POP_STRINGREF();
  12644. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  12645. break;
  12646. }
  12647. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  12648. {
  12649. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12650. PUSH_I32();
  12651. break;
  12652. }
  12653. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  12654. {
  12655. POP_I32();
  12656. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12657. PUSH_I32();
  12658. break;
  12659. }
  12660. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  12661. {
  12662. uint32 memidx;
  12663. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12664. func->has_memory_operations = true;
  12665. #endif
  12666. read_leb_uint32(p, p_end, memidx);
  12667. #if WASM_ENABLE_FAST_INTERP != 0
  12668. emit_uint32(loader_ctx, (uint32)memidx);
  12669. #endif
  12670. POP_I32();
  12671. POP_I32();
  12672. POP_I32();
  12673. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12674. PUSH_I32();
  12675. (void)memidx;
  12676. break;
  12677. }
  12678. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  12679. {
  12680. POP_I32();
  12681. POP_I32();
  12682. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12683. PUSH_REF(REF_TYPE_STRINGREF);
  12684. break;
  12685. }
  12686. case WASM_OP_STRING_AS_ITER:
  12687. {
  12688. POP_STRINGREF();
  12689. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  12690. break;
  12691. }
  12692. case WASM_OP_STRINGVIEW_ITER_NEXT:
  12693. {
  12694. POP_REF(REF_TYPE_STRINGVIEWITER);
  12695. PUSH_I32();
  12696. break;
  12697. }
  12698. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  12699. case WASM_OP_STRINGVIEW_ITER_REWIND:
  12700. {
  12701. POP_I32();
  12702. POP_REF(REF_TYPE_STRINGVIEWITER);
  12703. PUSH_I32();
  12704. break;
  12705. }
  12706. case WASM_OP_STRINGVIEW_ITER_SLICE:
  12707. {
  12708. POP_I32();
  12709. POP_REF(REF_TYPE_STRINGVIEWITER);
  12710. PUSH_REF(REF_TYPE_STRINGREF);
  12711. break;
  12712. }
  12713. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  12714. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  12715. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  12716. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  12717. {
  12718. POP_I32();
  12719. POP_I32();
  12720. POP_REF(REF_TYPE_ARRAYREF);
  12721. PUSH_REF(REF_TYPE_STRINGREF);
  12722. break;
  12723. }
  12724. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  12725. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  12726. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  12727. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  12728. {
  12729. POP_I32();
  12730. POP_REF(REF_TYPE_ARRAYREF);
  12731. POP_STRINGREF();
  12732. PUSH_I32();
  12733. break;
  12734. }
  12735. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  12736. default:
  12737. set_error_buf_v(error_buf, error_buf_size,
  12738. "%s %02x %02x", "unsupported opcode",
  12739. 0xfb, opcode1);
  12740. goto fail;
  12741. }
  12742. break;
  12743. }
  12744. #endif /* end of WASM_ENABLE_GC != 0 */
  12745. case WASM_OP_MISC_PREFIX:
  12746. {
  12747. uint32 opcode1;
  12748. read_leb_uint32(p, p_end, opcode1);
  12749. #if WASM_ENABLE_FAST_INTERP != 0
  12750. emit_byte(loader_ctx, ((uint8)opcode1));
  12751. #endif
  12752. switch (opcode1) {
  12753. case WASM_OP_I32_TRUNC_SAT_S_F32:
  12754. case WASM_OP_I32_TRUNC_SAT_U_F32:
  12755. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12756. break;
  12757. case WASM_OP_I32_TRUNC_SAT_S_F64:
  12758. case WASM_OP_I32_TRUNC_SAT_U_F64:
  12759. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12760. break;
  12761. case WASM_OP_I64_TRUNC_SAT_S_F32:
  12762. case WASM_OP_I64_TRUNC_SAT_U_F32:
  12763. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12764. break;
  12765. case WASM_OP_I64_TRUNC_SAT_S_F64:
  12766. case WASM_OP_I64_TRUNC_SAT_U_F64:
  12767. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12768. break;
  12769. #if WASM_ENABLE_BULK_MEMORY != 0
  12770. case WASM_OP_MEMORY_INIT:
  12771. {
  12772. read_leb_uint32(p, p_end, data_seg_idx);
  12773. #if WASM_ENABLE_FAST_INTERP != 0
  12774. emit_uint32(loader_ctx, data_seg_idx);
  12775. #endif
  12776. if (module->import_memory_count == 0
  12777. && module->memory_count == 0)
  12778. goto fail_unknown_memory;
  12779. if (*p++ != 0x00)
  12780. goto fail_zero_byte_expected;
  12781. if (data_seg_idx >= module->data_seg_count) {
  12782. set_error_buf_v(error_buf, error_buf_size,
  12783. "unknown data segment %d",
  12784. data_seg_idx);
  12785. goto fail;
  12786. }
  12787. if (module->data_seg_count1 == 0)
  12788. goto fail_data_cnt_sec_require;
  12789. POP_I32();
  12790. POP_I32();
  12791. POP_I32();
  12792. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12793. func->has_memory_operations = true;
  12794. #endif
  12795. #if WASM_ENABLE_WAMR_COMPILER != 0
  12796. module->is_bulk_memory_used = true;
  12797. #endif
  12798. break;
  12799. }
  12800. case WASM_OP_DATA_DROP:
  12801. {
  12802. read_leb_uint32(p, p_end, data_seg_idx);
  12803. #if WASM_ENABLE_FAST_INTERP != 0
  12804. emit_uint32(loader_ctx, data_seg_idx);
  12805. #endif
  12806. if (data_seg_idx >= module->data_seg_count) {
  12807. set_error_buf(error_buf, error_buf_size,
  12808. "unknown data segment");
  12809. goto fail;
  12810. }
  12811. if (module->data_seg_count1 == 0)
  12812. goto fail_data_cnt_sec_require;
  12813. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12814. func->has_memory_operations = true;
  12815. #endif
  12816. #if WASM_ENABLE_WAMR_COMPILER != 0
  12817. module->is_bulk_memory_used = true;
  12818. #endif
  12819. break;
  12820. }
  12821. case WASM_OP_MEMORY_COPY:
  12822. {
  12823. /* both src and dst memory index should be 0 */
  12824. if (*(int16 *)p != 0x0000)
  12825. goto fail_zero_byte_expected;
  12826. p += 2;
  12827. if (module->import_memory_count == 0
  12828. && module->memory_count == 0)
  12829. goto fail_unknown_memory;
  12830. POP_I32();
  12831. POP_I32();
  12832. POP_I32();
  12833. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12834. func->has_memory_operations = true;
  12835. #endif
  12836. #if WASM_ENABLE_WAMR_COMPILER != 0
  12837. module->is_bulk_memory_used = true;
  12838. #endif
  12839. break;
  12840. }
  12841. case WASM_OP_MEMORY_FILL:
  12842. {
  12843. if (*p++ != 0x00) {
  12844. goto fail_zero_byte_expected;
  12845. }
  12846. if (module->import_memory_count == 0
  12847. && module->memory_count == 0) {
  12848. goto fail_unknown_memory;
  12849. }
  12850. POP_I32();
  12851. POP_I32();
  12852. POP_I32();
  12853. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12854. func->has_memory_operations = true;
  12855. #endif
  12856. #if WASM_ENABLE_WAMR_COMPILER != 0
  12857. module->is_bulk_memory_used = true;
  12858. #endif
  12859. break;
  12860. }
  12861. fail_zero_byte_expected:
  12862. set_error_buf(error_buf, error_buf_size,
  12863. "zero byte expected");
  12864. goto fail;
  12865. fail_unknown_memory:
  12866. set_error_buf(error_buf, error_buf_size,
  12867. "unknown memory 0");
  12868. goto fail;
  12869. fail_data_cnt_sec_require:
  12870. set_error_buf(error_buf, error_buf_size,
  12871. "data count section required");
  12872. goto fail;
  12873. #endif /* WASM_ENABLE_BULK_MEMORY */
  12874. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  12875. case WASM_OP_TABLE_INIT:
  12876. {
  12877. uint8 seg_type = 0, tbl_type = 0;
  12878. #if WASM_ENABLE_GC != 0
  12879. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  12880. #endif
  12881. read_leb_uint32(p, p_end, table_seg_idx);
  12882. read_leb_uint32(p, p_end, table_idx);
  12883. if (!get_table_elem_type(module, table_idx, &tbl_type,
  12884. #if WASM_ENABLE_GC != 0
  12885. (void **)&tbl_ref_type,
  12886. #else
  12887. NULL,
  12888. #endif
  12889. error_buf, error_buf_size))
  12890. goto fail;
  12891. if (!get_table_seg_elem_type(module, table_seg_idx,
  12892. &seg_type,
  12893. #if WASM_ENABLE_GC != 0
  12894. (void **)&seg_ref_type,
  12895. #else
  12896. NULL,
  12897. #endif
  12898. error_buf, error_buf_size))
  12899. goto fail;
  12900. #if WASM_ENABLE_GC == 0
  12901. if (seg_type != tbl_type) {
  12902. set_error_buf(error_buf, error_buf_size,
  12903. "type mismatch");
  12904. goto fail;
  12905. }
  12906. #else
  12907. if (!wasm_reftype_is_subtype_of(
  12908. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  12909. module->types, module->type_count)) {
  12910. set_error_buf(error_buf, error_buf_size,
  12911. "type mismatch");
  12912. goto fail;
  12913. }
  12914. #endif
  12915. #if WASM_ENABLE_FAST_INTERP != 0
  12916. emit_uint32(loader_ctx, table_seg_idx);
  12917. emit_uint32(loader_ctx, table_idx);
  12918. #endif
  12919. POP_I32();
  12920. POP_I32();
  12921. POP_I32();
  12922. #if WASM_ENABLE_WAMR_COMPILER != 0
  12923. module->is_ref_types_used = true;
  12924. #endif
  12925. break;
  12926. }
  12927. case WASM_OP_ELEM_DROP:
  12928. {
  12929. read_leb_uint32(p, p_end, table_seg_idx);
  12930. if (!get_table_seg_elem_type(module, table_seg_idx,
  12931. NULL, NULL, error_buf,
  12932. error_buf_size))
  12933. goto fail;
  12934. #if WASM_ENABLE_FAST_INTERP != 0
  12935. emit_uint32(loader_ctx, table_seg_idx);
  12936. #endif
  12937. #if WASM_ENABLE_WAMR_COMPILER != 0
  12938. module->is_ref_types_used = true;
  12939. #endif
  12940. break;
  12941. }
  12942. case WASM_OP_TABLE_COPY:
  12943. {
  12944. uint8 src_type, dst_type;
  12945. #if WASM_ENABLE_GC != 0
  12946. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  12947. #endif
  12948. uint32 src_tbl_idx, dst_tbl_idx;
  12949. read_leb_uint32(p, p_end, dst_tbl_idx);
  12950. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  12951. #if WASM_ENABLE_GC != 0
  12952. (void **)&dst_ref_type,
  12953. #else
  12954. NULL,
  12955. #endif
  12956. error_buf, error_buf_size))
  12957. goto fail;
  12958. read_leb_uint32(p, p_end, src_tbl_idx);
  12959. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  12960. #if WASM_ENABLE_GC != 0
  12961. (void **)&src_ref_type,
  12962. #else
  12963. NULL,
  12964. #endif
  12965. error_buf, error_buf_size))
  12966. goto fail;
  12967. #if WASM_ENABLE_GC == 0
  12968. if (src_type != dst_type) {
  12969. set_error_buf(error_buf, error_buf_size,
  12970. "type mismatch");
  12971. goto fail;
  12972. }
  12973. #else
  12974. if (!wasm_reftype_is_subtype_of(
  12975. src_type, src_ref_type, dst_type, dst_ref_type,
  12976. module->types, module->type_count)) {
  12977. set_error_buf(error_buf, error_buf_size,
  12978. "type mismatch");
  12979. goto fail;
  12980. }
  12981. #endif
  12982. #if WASM_ENABLE_FAST_INTERP != 0
  12983. emit_uint32(loader_ctx, dst_tbl_idx);
  12984. emit_uint32(loader_ctx, src_tbl_idx);
  12985. #endif
  12986. POP_I32();
  12987. POP_I32();
  12988. POP_I32();
  12989. #if WASM_ENABLE_WAMR_COMPILER != 0
  12990. module->is_ref_types_used = true;
  12991. #endif
  12992. break;
  12993. }
  12994. case WASM_OP_TABLE_SIZE:
  12995. {
  12996. read_leb_uint32(p, p_end, table_idx);
  12997. /* TODO: shall we create a new function to check
  12998. table idx instead of using below function? */
  12999. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13000. error_buf, error_buf_size))
  13001. goto fail;
  13002. #if WASM_ENABLE_FAST_INTERP != 0
  13003. emit_uint32(loader_ctx, table_idx);
  13004. #endif
  13005. PUSH_I32();
  13006. #if WASM_ENABLE_WAMR_COMPILER != 0
  13007. module->is_ref_types_used = true;
  13008. #endif
  13009. break;
  13010. }
  13011. case WASM_OP_TABLE_GROW:
  13012. case WASM_OP_TABLE_FILL:
  13013. {
  13014. uint8 decl_type;
  13015. #if WASM_ENABLE_GC != 0
  13016. WASMRefType *ref_type = NULL;
  13017. #endif
  13018. read_leb_uint32(p, p_end, table_idx);
  13019. if (!get_table_elem_type(module, table_idx, &decl_type,
  13020. #if WASM_ENABLE_GC != 0
  13021. (void **)&ref_type,
  13022. #else
  13023. NULL,
  13024. #endif
  13025. error_buf, error_buf_size))
  13026. goto fail;
  13027. #if WASM_ENABLE_GC != 0
  13028. if (wasm_is_type_multi_byte_type(decl_type)) {
  13029. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13030. ref_type,
  13031. wasm_reftype_struct_size(ref_type));
  13032. }
  13033. #endif
  13034. if (opcode1 == WASM_OP_TABLE_GROW) {
  13035. if (table_idx < module->import_table_count) {
  13036. module->import_tables[table_idx]
  13037. .u.table.possible_grow = true;
  13038. }
  13039. else {
  13040. module
  13041. ->tables[table_idx
  13042. - module->import_table_count]
  13043. .possible_grow = true;
  13044. }
  13045. }
  13046. #if WASM_ENABLE_FAST_INTERP != 0
  13047. emit_uint32(loader_ctx, table_idx);
  13048. #endif
  13049. POP_I32();
  13050. #if WASM_ENABLE_FAST_INTERP != 0
  13051. POP_OFFSET_TYPE(decl_type);
  13052. #endif
  13053. POP_TYPE(decl_type);
  13054. if (opcode1 == WASM_OP_TABLE_GROW)
  13055. PUSH_I32();
  13056. else
  13057. POP_I32();
  13058. #if WASM_ENABLE_WAMR_COMPILER != 0
  13059. module->is_ref_types_used = true;
  13060. #endif
  13061. break;
  13062. }
  13063. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13064. default:
  13065. set_error_buf_v(error_buf, error_buf_size,
  13066. "%s %02x %02x", "unsupported opcode",
  13067. 0xfc, opcode1);
  13068. goto fail;
  13069. }
  13070. break;
  13071. }
  13072. #if WASM_ENABLE_SIMD != 0
  13073. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13074. case WASM_OP_SIMD_PREFIX:
  13075. {
  13076. uint32 opcode1;
  13077. #if WASM_ENABLE_WAMR_COMPILER != 0
  13078. /* Mark the SIMD instruction is used in this module */
  13079. module->is_simd_used = true;
  13080. #endif
  13081. read_leb_uint32(p, p_end, opcode1);
  13082. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13083. */
  13084. switch (opcode1) {
  13085. /* memory instruction */
  13086. case SIMD_v128_load:
  13087. case SIMD_v128_load8x8_s:
  13088. case SIMD_v128_load8x8_u:
  13089. case SIMD_v128_load16x4_s:
  13090. case SIMD_v128_load16x4_u:
  13091. case SIMD_v128_load32x2_s:
  13092. case SIMD_v128_load32x2_u:
  13093. case SIMD_v128_load8_splat:
  13094. case SIMD_v128_load16_splat:
  13095. case SIMD_v128_load32_splat:
  13096. case SIMD_v128_load64_splat:
  13097. {
  13098. CHECK_MEMORY();
  13099. read_leb_uint32(p, p_end, align); /* align */
  13100. if (!check_simd_memory_access_align(
  13101. opcode1, align, error_buf, error_buf_size)) {
  13102. goto fail;
  13103. }
  13104. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13105. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13106. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13107. func->has_memory_operations = true;
  13108. #endif
  13109. break;
  13110. }
  13111. case SIMD_v128_store:
  13112. {
  13113. CHECK_MEMORY();
  13114. read_leb_uint32(p, p_end, align); /* align */
  13115. if (!check_simd_memory_access_align(
  13116. opcode1, align, error_buf, error_buf_size)) {
  13117. goto fail;
  13118. }
  13119. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13120. POP_V128();
  13121. POP_I32();
  13122. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13123. func->has_memory_operations = true;
  13124. #endif
  13125. break;
  13126. }
  13127. /* basic operation */
  13128. case SIMD_v128_const:
  13129. {
  13130. CHECK_BUF1(p, p_end, 16);
  13131. p += 16;
  13132. PUSH_V128();
  13133. break;
  13134. }
  13135. case SIMD_v8x16_shuffle:
  13136. {
  13137. V128 mask;
  13138. CHECK_BUF1(p, p_end, 16);
  13139. mask = read_i8x16(p, error_buf, error_buf_size);
  13140. p += 16;
  13141. if (!check_simd_shuffle_mask(mask, error_buf,
  13142. error_buf_size)) {
  13143. goto fail;
  13144. }
  13145. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13146. break;
  13147. }
  13148. case SIMD_v8x16_swizzle:
  13149. {
  13150. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13151. break;
  13152. }
  13153. /* splat operation */
  13154. case SIMD_i8x16_splat:
  13155. case SIMD_i16x8_splat:
  13156. case SIMD_i32x4_splat:
  13157. case SIMD_i64x2_splat:
  13158. case SIMD_f32x4_splat:
  13159. case SIMD_f64x2_splat:
  13160. {
  13161. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13162. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13163. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13164. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13165. VALUE_TYPE_V128);
  13166. break;
  13167. }
  13168. /* lane operation */
  13169. case SIMD_i8x16_extract_lane_s:
  13170. case SIMD_i8x16_extract_lane_u:
  13171. case SIMD_i8x16_replace_lane:
  13172. case SIMD_i16x8_extract_lane_s:
  13173. case SIMD_i16x8_extract_lane_u:
  13174. case SIMD_i16x8_replace_lane:
  13175. case SIMD_i32x4_extract_lane:
  13176. case SIMD_i32x4_replace_lane:
  13177. case SIMD_i64x2_extract_lane:
  13178. case SIMD_i64x2_replace_lane:
  13179. case SIMD_f32x4_extract_lane:
  13180. case SIMD_f32x4_replace_lane:
  13181. case SIMD_f64x2_extract_lane:
  13182. case SIMD_f64x2_replace_lane:
  13183. {
  13184. uint8 lane;
  13185. /* clang-format off */
  13186. uint8 replace[] = {
  13187. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13188. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13189. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13190. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13191. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13192. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13193. };
  13194. uint8 push_type[] = {
  13195. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13196. VALUE_TYPE_V128,
  13197. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13198. VALUE_TYPE_V128,
  13199. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13200. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13201. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13202. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13203. };
  13204. /* clang-format on */
  13205. CHECK_BUF(p, p_end, 1);
  13206. lane = read_uint8(p);
  13207. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13208. error_buf_size)) {
  13209. goto fail;
  13210. }
  13211. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13212. if (!(wasm_loader_pop_frame_ref(
  13213. loader_ctx,
  13214. replace[opcode1
  13215. - SIMD_i8x16_extract_lane_s],
  13216. error_buf, error_buf_size)))
  13217. goto fail;
  13218. }
  13219. POP_AND_PUSH(
  13220. VALUE_TYPE_V128,
  13221. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13222. break;
  13223. }
  13224. /* i8x16 compare operation */
  13225. case SIMD_i8x16_eq:
  13226. case SIMD_i8x16_ne:
  13227. case SIMD_i8x16_lt_s:
  13228. case SIMD_i8x16_lt_u:
  13229. case SIMD_i8x16_gt_s:
  13230. case SIMD_i8x16_gt_u:
  13231. case SIMD_i8x16_le_s:
  13232. case SIMD_i8x16_le_u:
  13233. case SIMD_i8x16_ge_s:
  13234. case SIMD_i8x16_ge_u:
  13235. /* i16x8 compare operation */
  13236. case SIMD_i16x8_eq:
  13237. case SIMD_i16x8_ne:
  13238. case SIMD_i16x8_lt_s:
  13239. case SIMD_i16x8_lt_u:
  13240. case SIMD_i16x8_gt_s:
  13241. case SIMD_i16x8_gt_u:
  13242. case SIMD_i16x8_le_s:
  13243. case SIMD_i16x8_le_u:
  13244. case SIMD_i16x8_ge_s:
  13245. case SIMD_i16x8_ge_u:
  13246. /* i32x4 compare operation */
  13247. case SIMD_i32x4_eq:
  13248. case SIMD_i32x4_ne:
  13249. case SIMD_i32x4_lt_s:
  13250. case SIMD_i32x4_lt_u:
  13251. case SIMD_i32x4_gt_s:
  13252. case SIMD_i32x4_gt_u:
  13253. case SIMD_i32x4_le_s:
  13254. case SIMD_i32x4_le_u:
  13255. case SIMD_i32x4_ge_s:
  13256. case SIMD_i32x4_ge_u:
  13257. /* f32x4 compare operation */
  13258. case SIMD_f32x4_eq:
  13259. case SIMD_f32x4_ne:
  13260. case SIMD_f32x4_lt:
  13261. case SIMD_f32x4_gt:
  13262. case SIMD_f32x4_le:
  13263. case SIMD_f32x4_ge:
  13264. /* f64x2 compare operation */
  13265. case SIMD_f64x2_eq:
  13266. case SIMD_f64x2_ne:
  13267. case SIMD_f64x2_lt:
  13268. case SIMD_f64x2_gt:
  13269. case SIMD_f64x2_le:
  13270. case SIMD_f64x2_ge:
  13271. {
  13272. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13273. break;
  13274. }
  13275. /* v128 operation */
  13276. case SIMD_v128_not:
  13277. {
  13278. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13279. break;
  13280. }
  13281. case SIMD_v128_and:
  13282. case SIMD_v128_andnot:
  13283. case SIMD_v128_or:
  13284. case SIMD_v128_xor:
  13285. {
  13286. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13287. break;
  13288. }
  13289. case SIMD_v128_bitselect:
  13290. {
  13291. POP_V128();
  13292. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13293. break;
  13294. }
  13295. case SIMD_v128_any_true:
  13296. {
  13297. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13298. break;
  13299. }
  13300. /* Load Lane Operation */
  13301. case SIMD_v128_load8_lane:
  13302. case SIMD_v128_load16_lane:
  13303. case SIMD_v128_load32_lane:
  13304. case SIMD_v128_load64_lane:
  13305. case SIMD_v128_store8_lane:
  13306. case SIMD_v128_store16_lane:
  13307. case SIMD_v128_store32_lane:
  13308. case SIMD_v128_store64_lane:
  13309. {
  13310. uint8 lane;
  13311. CHECK_MEMORY();
  13312. read_leb_uint32(p, p_end, align); /* align */
  13313. if (!check_simd_memory_access_align(
  13314. opcode1, align, error_buf, error_buf_size)) {
  13315. goto fail;
  13316. }
  13317. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13318. CHECK_BUF(p, p_end, 1);
  13319. lane = read_uint8(p);
  13320. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13321. error_buf_size)) {
  13322. goto fail;
  13323. }
  13324. POP_V128();
  13325. POP_I32();
  13326. if (opcode1 < SIMD_v128_store8_lane) {
  13327. PUSH_V128();
  13328. }
  13329. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13330. func->has_memory_operations = true;
  13331. #endif
  13332. break;
  13333. }
  13334. case SIMD_v128_load32_zero:
  13335. case SIMD_v128_load64_zero:
  13336. {
  13337. CHECK_MEMORY();
  13338. read_leb_uint32(p, p_end, align); /* align */
  13339. if (!check_simd_memory_access_align(
  13340. opcode1, align, error_buf, error_buf_size)) {
  13341. goto fail;
  13342. }
  13343. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13344. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13345. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13346. func->has_memory_operations = true;
  13347. #endif
  13348. break;
  13349. }
  13350. /* Float conversion */
  13351. case SIMD_f32x4_demote_f64x2_zero:
  13352. case SIMD_f64x2_promote_low_f32x4_zero:
  13353. {
  13354. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13355. break;
  13356. }
  13357. /* i8x16 Operation */
  13358. case SIMD_i8x16_abs:
  13359. case SIMD_i8x16_neg:
  13360. case SIMD_i8x16_popcnt:
  13361. {
  13362. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13363. break;
  13364. }
  13365. case SIMD_i8x16_all_true:
  13366. case SIMD_i8x16_bitmask:
  13367. {
  13368. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13369. break;
  13370. }
  13371. case SIMD_i8x16_narrow_i16x8_s:
  13372. case SIMD_i8x16_narrow_i16x8_u:
  13373. {
  13374. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13375. break;
  13376. }
  13377. case SIMD_f32x4_ceil:
  13378. case SIMD_f32x4_floor:
  13379. case SIMD_f32x4_trunc:
  13380. case SIMD_f32x4_nearest:
  13381. {
  13382. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13383. break;
  13384. }
  13385. case SIMD_i8x16_shl:
  13386. case SIMD_i8x16_shr_s:
  13387. case SIMD_i8x16_shr_u:
  13388. {
  13389. POP_I32();
  13390. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13391. break;
  13392. }
  13393. case SIMD_i8x16_add:
  13394. case SIMD_i8x16_add_sat_s:
  13395. case SIMD_i8x16_add_sat_u:
  13396. case SIMD_i8x16_sub:
  13397. case SIMD_i8x16_sub_sat_s:
  13398. case SIMD_i8x16_sub_sat_u:
  13399. {
  13400. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13401. break;
  13402. }
  13403. case SIMD_f64x2_ceil:
  13404. case SIMD_f64x2_floor:
  13405. {
  13406. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13407. break;
  13408. }
  13409. case SIMD_i8x16_min_s:
  13410. case SIMD_i8x16_min_u:
  13411. case SIMD_i8x16_max_s:
  13412. case SIMD_i8x16_max_u:
  13413. {
  13414. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13415. break;
  13416. }
  13417. case SIMD_f64x2_trunc:
  13418. {
  13419. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13420. break;
  13421. }
  13422. case SIMD_i8x16_avgr_u:
  13423. {
  13424. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13425. break;
  13426. }
  13427. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13428. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13429. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13430. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13431. /* i16x8 operation */
  13432. case SIMD_i16x8_abs:
  13433. case SIMD_i16x8_neg:
  13434. {
  13435. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13436. break;
  13437. }
  13438. case SIMD_i16x8_q15mulr_sat_s:
  13439. {
  13440. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13441. break;
  13442. }
  13443. case SIMD_i16x8_all_true:
  13444. case SIMD_i16x8_bitmask:
  13445. {
  13446. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13447. break;
  13448. }
  13449. case SIMD_i16x8_narrow_i32x4_s:
  13450. case SIMD_i16x8_narrow_i32x4_u:
  13451. {
  13452. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13453. break;
  13454. }
  13455. case SIMD_i16x8_extend_low_i8x16_s:
  13456. case SIMD_i16x8_extend_high_i8x16_s:
  13457. case SIMD_i16x8_extend_low_i8x16_u:
  13458. case SIMD_i16x8_extend_high_i8x16_u:
  13459. {
  13460. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13461. break;
  13462. }
  13463. case SIMD_i16x8_shl:
  13464. case SIMD_i16x8_shr_s:
  13465. case SIMD_i16x8_shr_u:
  13466. {
  13467. POP_I32();
  13468. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13469. break;
  13470. }
  13471. case SIMD_i16x8_add:
  13472. case SIMD_i16x8_add_sat_s:
  13473. case SIMD_i16x8_add_sat_u:
  13474. case SIMD_i16x8_sub:
  13475. case SIMD_i16x8_sub_sat_s:
  13476. case SIMD_i16x8_sub_sat_u:
  13477. {
  13478. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13479. break;
  13480. }
  13481. case SIMD_f64x2_nearest:
  13482. {
  13483. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13484. break;
  13485. }
  13486. case SIMD_i16x8_mul:
  13487. case SIMD_i16x8_min_s:
  13488. case SIMD_i16x8_min_u:
  13489. case SIMD_i16x8_max_s:
  13490. case SIMD_i16x8_max_u:
  13491. case SIMD_i16x8_avgr_u:
  13492. case SIMD_i16x8_extmul_low_i8x16_s:
  13493. case SIMD_i16x8_extmul_high_i8x16_s:
  13494. case SIMD_i16x8_extmul_low_i8x16_u:
  13495. case SIMD_i16x8_extmul_high_i8x16_u:
  13496. {
  13497. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13498. break;
  13499. }
  13500. /* i32x4 operation */
  13501. case SIMD_i32x4_abs:
  13502. case SIMD_i32x4_neg:
  13503. {
  13504. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13505. break;
  13506. }
  13507. case SIMD_i32x4_all_true:
  13508. case SIMD_i32x4_bitmask:
  13509. {
  13510. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13511. break;
  13512. }
  13513. case SIMD_i32x4_narrow_i64x2_s:
  13514. case SIMD_i32x4_narrow_i64x2_u:
  13515. {
  13516. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13517. break;
  13518. }
  13519. case SIMD_i32x4_extend_low_i16x8_s:
  13520. case SIMD_i32x4_extend_high_i16x8_s:
  13521. case SIMD_i32x4_extend_low_i16x8_u:
  13522. case SIMD_i32x4_extend_high_i16x8_u:
  13523. {
  13524. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13525. break;
  13526. }
  13527. case SIMD_i32x4_shl:
  13528. case SIMD_i32x4_shr_s:
  13529. case SIMD_i32x4_shr_u:
  13530. {
  13531. POP_I32();
  13532. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13533. break;
  13534. }
  13535. case SIMD_i32x4_add:
  13536. case SIMD_i32x4_sub:
  13537. case SIMD_i32x4_mul:
  13538. case SIMD_i32x4_min_s:
  13539. case SIMD_i32x4_min_u:
  13540. case SIMD_i32x4_max_s:
  13541. case SIMD_i32x4_max_u:
  13542. case SIMD_i32x4_dot_i16x8_s:
  13543. case SIMD_i32x4_avgr_u:
  13544. case SIMD_i32x4_extmul_low_i16x8_s:
  13545. case SIMD_i32x4_extmul_high_i16x8_s:
  13546. case SIMD_i32x4_extmul_low_i16x8_u:
  13547. case SIMD_i32x4_extmul_high_i16x8_u:
  13548. {
  13549. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13550. break;
  13551. }
  13552. /* i64x2 operation */
  13553. case SIMD_i64x2_abs:
  13554. case SIMD_i64x2_neg:
  13555. {
  13556. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13557. break;
  13558. }
  13559. case SIMD_i64x2_all_true:
  13560. case SIMD_i64x2_bitmask:
  13561. {
  13562. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13563. break;
  13564. }
  13565. case SIMD_i64x2_extend_low_i32x4_s:
  13566. case SIMD_i64x2_extend_high_i32x4_s:
  13567. case SIMD_i64x2_extend_low_i32x4_u:
  13568. case SIMD_i64x2_extend_high_i32x4_u:
  13569. {
  13570. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13571. break;
  13572. }
  13573. case SIMD_i64x2_shl:
  13574. case SIMD_i64x2_shr_s:
  13575. case SIMD_i64x2_shr_u:
  13576. {
  13577. POP_I32();
  13578. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13579. break;
  13580. }
  13581. case SIMD_i64x2_add:
  13582. case SIMD_i64x2_sub:
  13583. case SIMD_i64x2_mul:
  13584. case SIMD_i64x2_eq:
  13585. case SIMD_i64x2_ne:
  13586. case SIMD_i64x2_lt_s:
  13587. case SIMD_i64x2_gt_s:
  13588. case SIMD_i64x2_le_s:
  13589. case SIMD_i64x2_ge_s:
  13590. case SIMD_i64x2_extmul_low_i32x4_s:
  13591. case SIMD_i64x2_extmul_high_i32x4_s:
  13592. case SIMD_i64x2_extmul_low_i32x4_u:
  13593. case SIMD_i64x2_extmul_high_i32x4_u:
  13594. {
  13595. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13596. break;
  13597. }
  13598. /* f32x4 operation */
  13599. case SIMD_f32x4_abs:
  13600. case SIMD_f32x4_neg:
  13601. case SIMD_f32x4_round:
  13602. case SIMD_f32x4_sqrt:
  13603. {
  13604. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13605. break;
  13606. }
  13607. case SIMD_f32x4_add:
  13608. case SIMD_f32x4_sub:
  13609. case SIMD_f32x4_mul:
  13610. case SIMD_f32x4_div:
  13611. case SIMD_f32x4_min:
  13612. case SIMD_f32x4_max:
  13613. case SIMD_f32x4_pmin:
  13614. case SIMD_f32x4_pmax:
  13615. {
  13616. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13617. break;
  13618. }
  13619. /* f64x2 operation */
  13620. case SIMD_f64x2_abs:
  13621. case SIMD_f64x2_neg:
  13622. case SIMD_f64x2_round:
  13623. case SIMD_f64x2_sqrt:
  13624. {
  13625. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13626. break;
  13627. }
  13628. case SIMD_f64x2_add:
  13629. case SIMD_f64x2_sub:
  13630. case SIMD_f64x2_mul:
  13631. case SIMD_f64x2_div:
  13632. case SIMD_f64x2_min:
  13633. case SIMD_f64x2_max:
  13634. case SIMD_f64x2_pmin:
  13635. case SIMD_f64x2_pmax:
  13636. {
  13637. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13638. break;
  13639. }
  13640. case SIMD_i32x4_trunc_sat_f32x4_s:
  13641. case SIMD_i32x4_trunc_sat_f32x4_u:
  13642. case SIMD_f32x4_convert_i32x4_s:
  13643. case SIMD_f32x4_convert_i32x4_u:
  13644. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  13645. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  13646. case SIMD_f64x2_convert_low_i32x4_s:
  13647. case SIMD_f64x2_convert_low_i32x4_u:
  13648. {
  13649. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13650. break;
  13651. }
  13652. default:
  13653. {
  13654. if (error_buf != NULL) {
  13655. snprintf(error_buf, error_buf_size,
  13656. "WASM module load failed: "
  13657. "invalid opcode 0xfd %02x.",
  13658. opcode1);
  13659. }
  13660. goto fail;
  13661. }
  13662. }
  13663. break;
  13664. }
  13665. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  13666. #endif /* end of WASM_ENABLE_SIMD */
  13667. #if WASM_ENABLE_SHARED_MEMORY != 0
  13668. case WASM_OP_ATOMIC_PREFIX:
  13669. {
  13670. uint32 opcode1;
  13671. read_leb_uint32(p, p_end, opcode1);
  13672. #if WASM_ENABLE_FAST_INTERP != 0
  13673. emit_byte(loader_ctx, opcode1);
  13674. #endif
  13675. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  13676. CHECK_MEMORY();
  13677. read_leb_uint32(p, p_end, align); /* align */
  13678. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13679. if (!check_memory_align_equal(opcode1, align, error_buf,
  13680. error_buf_size)) {
  13681. goto fail;
  13682. }
  13683. #if WASM_ENABLE_FAST_INTERP != 0
  13684. emit_uint32(loader_ctx, mem_offset);
  13685. #endif
  13686. }
  13687. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13688. func->has_memory_operations = true;
  13689. #endif
  13690. switch (opcode1) {
  13691. case WASM_OP_ATOMIC_NOTIFY:
  13692. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13693. break;
  13694. case WASM_OP_ATOMIC_WAIT32:
  13695. POP_I64();
  13696. POP_I32();
  13697. POP_I32();
  13698. PUSH_I32();
  13699. break;
  13700. case WASM_OP_ATOMIC_WAIT64:
  13701. POP_I64();
  13702. POP_I64();
  13703. POP_I32();
  13704. PUSH_I32();
  13705. break;
  13706. case WASM_OP_ATOMIC_FENCE:
  13707. /* reserved byte 0x00 */
  13708. if (*p++ != 0x00) {
  13709. set_error_buf(error_buf, error_buf_size,
  13710. "zero byte expected");
  13711. goto fail;
  13712. }
  13713. break;
  13714. case WASM_OP_ATOMIC_I32_LOAD:
  13715. case WASM_OP_ATOMIC_I32_LOAD8_U:
  13716. case WASM_OP_ATOMIC_I32_LOAD16_U:
  13717. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13718. break;
  13719. case WASM_OP_ATOMIC_I32_STORE:
  13720. case WASM_OP_ATOMIC_I32_STORE8:
  13721. case WASM_OP_ATOMIC_I32_STORE16:
  13722. POP_I32();
  13723. POP_I32();
  13724. break;
  13725. case WASM_OP_ATOMIC_I64_LOAD:
  13726. case WASM_OP_ATOMIC_I64_LOAD8_U:
  13727. case WASM_OP_ATOMIC_I64_LOAD16_U:
  13728. case WASM_OP_ATOMIC_I64_LOAD32_U:
  13729. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  13730. break;
  13731. case WASM_OP_ATOMIC_I64_STORE:
  13732. case WASM_OP_ATOMIC_I64_STORE8:
  13733. case WASM_OP_ATOMIC_I64_STORE16:
  13734. case WASM_OP_ATOMIC_I64_STORE32:
  13735. POP_I64();
  13736. POP_I32();
  13737. break;
  13738. case WASM_OP_ATOMIC_RMW_I32_ADD:
  13739. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  13740. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  13741. case WASM_OP_ATOMIC_RMW_I32_SUB:
  13742. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  13743. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  13744. case WASM_OP_ATOMIC_RMW_I32_AND:
  13745. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  13746. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  13747. case WASM_OP_ATOMIC_RMW_I32_OR:
  13748. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  13749. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  13750. case WASM_OP_ATOMIC_RMW_I32_XOR:
  13751. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  13752. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  13753. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  13754. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  13755. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  13756. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13757. break;
  13758. case WASM_OP_ATOMIC_RMW_I64_ADD:
  13759. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  13760. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  13761. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  13762. case WASM_OP_ATOMIC_RMW_I64_SUB:
  13763. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  13764. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  13765. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  13766. case WASM_OP_ATOMIC_RMW_I64_AND:
  13767. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  13768. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  13769. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  13770. case WASM_OP_ATOMIC_RMW_I64_OR:
  13771. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  13772. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  13773. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  13774. case WASM_OP_ATOMIC_RMW_I64_XOR:
  13775. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  13776. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  13777. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  13778. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  13779. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  13780. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  13781. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  13782. POP_I64();
  13783. POP_I32();
  13784. PUSH_I64();
  13785. break;
  13786. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  13787. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  13788. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  13789. POP_I32();
  13790. POP_I32();
  13791. POP_I32();
  13792. PUSH_I32();
  13793. break;
  13794. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  13795. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  13796. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  13797. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  13798. POP_I64();
  13799. POP_I64();
  13800. POP_I32();
  13801. PUSH_I64();
  13802. break;
  13803. default:
  13804. set_error_buf_v(error_buf, error_buf_size,
  13805. "%s %02x %02x", "unsupported opcode",
  13806. 0xfe, opcode1);
  13807. goto fail;
  13808. }
  13809. break;
  13810. }
  13811. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  13812. default:
  13813. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  13814. "unsupported opcode", opcode);
  13815. goto fail;
  13816. }
  13817. #if WASM_ENABLE_FAST_INTERP != 0
  13818. last_op = opcode;
  13819. #endif
  13820. }
  13821. if (loader_ctx->csp_num > 0) {
  13822. if (cur_func_idx < module->function_count - 1)
  13823. /* Function with missing end marker (between two functions) */
  13824. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  13825. else
  13826. /* Function with missing end marker
  13827. (at EOF or end of code sections) */
  13828. set_error_buf(error_buf, error_buf_size,
  13829. "unexpected end of section or function, "
  13830. "or section size mismatch");
  13831. goto fail;
  13832. }
  13833. #if WASM_ENABLE_FAST_INTERP != 0
  13834. if (loader_ctx->p_code_compiled == NULL)
  13835. goto re_scan;
  13836. func->const_cell_num = loader_ctx->const_cell_num;
  13837. if (func->const_cell_num > 0) {
  13838. int32 j;
  13839. if (!(func->consts = func_const = loader_malloc(
  13840. func->const_cell_num * 4, error_buf, error_buf_size)))
  13841. goto fail;
  13842. func_const_end = func->consts + func->const_cell_num * 4;
  13843. /* reverse the const buf */
  13844. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  13845. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  13846. if (c->value_type == VALUE_TYPE_F64
  13847. || c->value_type == VALUE_TYPE_I64) {
  13848. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  13849. &(c->value.f64), (uint32)sizeof(int64));
  13850. func_const += sizeof(int64);
  13851. }
  13852. else {
  13853. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  13854. &(c->value.f32), (uint32)sizeof(int32));
  13855. func_const += sizeof(int32);
  13856. }
  13857. }
  13858. }
  13859. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  13860. - loader_ctx->start_dynamic_offset + 1;
  13861. #else
  13862. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  13863. #endif
  13864. func->max_block_num = loader_ctx->max_csp_num;
  13865. return_value = true;
  13866. fail:
  13867. wasm_loader_ctx_destroy(loader_ctx);
  13868. (void)table_idx;
  13869. (void)table_seg_idx;
  13870. (void)data_seg_idx;
  13871. (void)i64_const;
  13872. (void)local_offset;
  13873. (void)p_org;
  13874. (void)mem_offset;
  13875. (void)align;
  13876. return return_value;
  13877. }