wasm_loader.c 585 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874
  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 "wasm_loader_common.h"
  12. #include "../common/wasm_native.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_type.h"
  16. #include "../common/gc/gc_object.h"
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. #include "../libraries/debug-engine/debug_engine.h"
  20. #endif
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #include "../fast-jit/jit_codecache.h"
  24. #endif
  25. #if WASM_ENABLE_JIT != 0
  26. #include "../compilation/aot_llvm.h"
  27. #endif
  28. #ifndef TRACE_WASM_LOADER
  29. #define TRACE_WASM_LOADER 0
  30. #endif
  31. /* Read a value of given type from the address pointed to by the given
  32. pointer and increase the pointer to the position just after the
  33. value being read. */
  34. #define TEMPLATE_READ_VALUE(Type, p) \
  35. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  36. #if WASM_ENABLE_MEMORY64 != 0
  37. static bool
  38. has_module_memory64(WASMModule *module)
  39. {
  40. /* TODO: multi-memories for now assuming the memory idx type is consistent
  41. * across multi-memories */
  42. if (module->import_memory_count > 0)
  43. return !!(module->import_memories[0].u.memory.mem_type.flags
  44. & MEMORY64_FLAG);
  45. else if (module->memory_count > 0)
  46. return !!(module->memories[0].flags & MEMORY64_FLAG);
  47. return false;
  48. }
  49. #endif
  50. static void
  51. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  52. {
  53. if (error_buf != NULL) {
  54. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  55. string);
  56. }
  57. }
  58. #if WASM_ENABLE_MEMORY64 != 0
  59. static void
  60. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  61. {
  62. if (error_buf != NULL) {
  63. snprintf(error_buf, error_buf_size, "offset out of range");
  64. }
  65. }
  66. #endif
  67. static void
  68. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  69. {
  70. va_list args;
  71. char buf[128];
  72. if (error_buf != NULL) {
  73. va_start(args, format);
  74. vsnprintf(buf, sizeof(buf), format, args);
  75. va_end(args);
  76. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  77. }
  78. }
  79. static bool
  80. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  81. char *error_buf, uint32 error_buf_size)
  82. {
  83. if ((uintptr_t)buf + length < (uintptr_t)buf
  84. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  85. set_error_buf(error_buf, error_buf_size,
  86. "unexpected end of section or function");
  87. return false;
  88. }
  89. return true;
  90. }
  91. static bool
  92. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  93. char *error_buf, uint32 error_buf_size)
  94. {
  95. if ((uintptr_t)buf + length < (uintptr_t)buf
  96. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  97. set_error_buf(error_buf, error_buf_size, "unexpected end");
  98. return false;
  99. }
  100. return true;
  101. }
  102. #define CHECK_BUF(buf, buf_end, length) \
  103. do { \
  104. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  105. goto fail; \
  106. } \
  107. } while (0)
  108. #define CHECK_BUF1(buf, buf_end, length) \
  109. do { \
  110. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  111. goto fail; \
  112. } \
  113. } while (0)
  114. #define skip_leb(p) while (*p++ & 0x80)
  115. #define skip_leb_int64(p, p_end) skip_leb(p)
  116. #define skip_leb_uint32(p, p_end) skip_leb(p)
  117. #define skip_leb_int32(p, p_end) skip_leb(p)
  118. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  119. static bool
  120. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  121. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  122. {
  123. const uint8 *buf = *p_buf;
  124. uint64 result = 0;
  125. uint32 shift = 0;
  126. uint32 offset = 0, bcnt = 0;
  127. uint64 byte;
  128. while (true) {
  129. /* uN or SN must not exceed ceil(N/7) bytes */
  130. if (bcnt + 1 > (maxbits + 6) / 7) {
  131. set_error_buf(error_buf, error_buf_size,
  132. "integer representation too long");
  133. return false;
  134. }
  135. CHECK_BUF(buf, buf_end, offset + 1);
  136. byte = buf[offset];
  137. offset += 1;
  138. result |= ((byte & 0x7f) << shift);
  139. shift += 7;
  140. bcnt += 1;
  141. if ((byte & 0x80) == 0) {
  142. break;
  143. }
  144. }
  145. if (!sign && maxbits == 32 && shift >= maxbits) {
  146. /* The top bits set represent values > 32 bits */
  147. if (((uint8)byte) & 0xf0)
  148. goto fail_integer_too_large;
  149. }
  150. else if (sign && maxbits == 32) {
  151. if (shift < maxbits) {
  152. /* Sign extend, second-highest bit is the sign bit */
  153. if ((uint8)byte & 0x40)
  154. result |= (~((uint64)0)) << shift;
  155. }
  156. else {
  157. /* The top bits should be a sign-extension of the sign bit */
  158. bool sign_bit_set = ((uint8)byte) & 0x8;
  159. int top_bits = ((uint8)byte) & 0xf0;
  160. if ((sign_bit_set && top_bits != 0x70)
  161. || (!sign_bit_set && top_bits != 0))
  162. goto fail_integer_too_large;
  163. }
  164. }
  165. else if (sign && maxbits == 64) {
  166. if (shift < maxbits) {
  167. /* Sign extend, second-highest bit is the sign bit */
  168. if ((uint8)byte & 0x40)
  169. result |= (~((uint64)0)) << shift;
  170. }
  171. else {
  172. /* The top bits should be a sign-extension of the sign bit */
  173. bool sign_bit_set = ((uint8)byte) & 0x1;
  174. int top_bits = ((uint8)byte) & 0xfe;
  175. if ((sign_bit_set && top_bits != 0x7e)
  176. || (!sign_bit_set && top_bits != 0))
  177. goto fail_integer_too_large;
  178. }
  179. }
  180. *p_buf += offset;
  181. *p_result = result;
  182. return true;
  183. fail_integer_too_large:
  184. set_error_buf(error_buf, error_buf_size, "integer too large");
  185. fail:
  186. return false;
  187. }
  188. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  189. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  190. #define read_leb_int64(p, p_end, res) \
  191. do { \
  192. uint64 res64; \
  193. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  194. error_buf_size)) \
  195. goto fail; \
  196. res = (int64)res64; \
  197. } while (0)
  198. #if WASM_ENABLE_MEMORY64 != 0
  199. #define read_leb_mem_offset(p, p_end, res) \
  200. do { \
  201. uint64 res64; \
  202. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  203. &res64, error_buf, error_buf_size)) { \
  204. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  205. goto fail; \
  206. } \
  207. res = (mem_offset_t)res64; \
  208. } while (0)
  209. #else
  210. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  211. #endif
  212. #define read_leb_uint32(p, p_end, res) \
  213. do { \
  214. uint64 res64; \
  215. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  216. error_buf_size)) \
  217. goto fail; \
  218. res = (uint32)res64; \
  219. } while (0)
  220. #define read_leb_int32(p, p_end, res) \
  221. do { \
  222. uint64 res64; \
  223. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  224. error_buf_size)) \
  225. goto fail; \
  226. res = (int32)res64; \
  227. } while (0)
  228. static char *
  229. type2str(uint8 type)
  230. {
  231. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  232. #if WASM_ENABLE_GC != 0
  233. char *type_str_ref[] = { "stringview_iter",
  234. "stringview_wtf16",
  235. "(ref null ht)",
  236. "(ref ht)",
  237. "", /* reserved */
  238. "stringview_wtf8",
  239. "stringref",
  240. "", /* reserved */
  241. "", /* reserved */
  242. "arrayref",
  243. "structref",
  244. "i32ref",
  245. "eqref",
  246. "anyref",
  247. "externref",
  248. "funcref",
  249. "nullref",
  250. "nullexternref",
  251. "nullfuncref" };
  252. #endif
  253. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  254. return type_str[type - VALUE_TYPE_V128];
  255. #if WASM_ENABLE_GC != 0
  256. else if (wasm_is_type_reftype(type))
  257. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  258. #endif
  259. else if (type == VALUE_TYPE_FUNCREF)
  260. return "funcref";
  261. else if (type == VALUE_TYPE_EXTERNREF)
  262. return "externref";
  263. else
  264. return "unknown type";
  265. }
  266. static bool
  267. is_32bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I32
  270. || type == VALUE_TYPE_F32
  271. /* the operand stack is in polymorphic state */
  272. || type == VALUE_TYPE_ANY
  273. #if WASM_ENABLE_GC != 0
  274. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  275. #elif WASM_ENABLE_REF_TYPES != 0
  276. /* For reference types, we use uint32 index to represent
  277. the funcref and externref */
  278. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  279. #endif
  280. )
  281. return true;
  282. return false;
  283. }
  284. static bool
  285. is_64bit_type(uint8 type)
  286. {
  287. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  288. #if WASM_ENABLE_GC != 0
  289. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  290. #endif
  291. )
  292. return true;
  293. return false;
  294. }
  295. #if WASM_ENABLE_GC != 0
  296. static bool
  297. is_packed_type(uint8 type)
  298. {
  299. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  300. }
  301. #endif
  302. static bool
  303. is_byte_a_type(uint8 type)
  304. {
  305. return (is_valid_value_type_for_interpreter(type)
  306. || (type == VALUE_TYPE_VOID))
  307. ? true
  308. : false;
  309. }
  310. #if WASM_ENABLE_SIMD != 0
  311. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  312. static V128
  313. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  314. {
  315. V128 result;
  316. uint8 i;
  317. for (i = 0; i != 16; ++i) {
  318. result.i8x16[i] = read_uint8(p_buf);
  319. }
  320. return result;
  321. }
  322. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  323. #endif /* end of WASM_ENABLE_SIMD */
  324. static void *
  325. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  326. {
  327. void *mem;
  328. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  329. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  330. return NULL;
  331. }
  332. memset(mem, 0, (uint32)size);
  333. return mem;
  334. }
  335. static void *
  336. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  337. uint32 error_buf_size)
  338. {
  339. uint8 *mem_new;
  340. bh_assert(size_new > size_old);
  341. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  342. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  343. memset(mem_new + size_old, 0, size_new - size_old);
  344. wasm_runtime_free(mem_old);
  345. }
  346. return mem_new;
  347. }
  348. #define MEM_REALLOC(mem, size_old, size_new) \
  349. do { \
  350. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  351. error_buf_size); \
  352. if (!mem_new) \
  353. goto fail; \
  354. mem = mem_new; \
  355. } while (0)
  356. #if WASM_ENABLE_GC != 0
  357. static bool
  358. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  359. char *error_buf, uint32 error_buf_size)
  360. {
  361. if (type_index >= type_count) {
  362. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  363. type_index);
  364. return false;
  365. }
  366. return true;
  367. }
  368. static bool
  369. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  370. uint32 error_buf_size)
  371. {
  372. if (!check_type_index(module, module->type_count, type_index, error_buf,
  373. error_buf_size)) {
  374. return false;
  375. }
  376. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  377. set_error_buf(error_buf, error_buf_size, "unkown array type");
  378. return false;
  379. }
  380. return true;
  381. }
  382. #endif
  383. static bool
  384. check_function_index(const WASMModule *module, uint32 function_index,
  385. char *error_buf, uint32 error_buf_size)
  386. {
  387. if (function_index
  388. >= module->import_function_count + module->function_count) {
  389. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  390. function_index);
  391. return false;
  392. }
  393. return true;
  394. }
  395. typedef struct InitValue {
  396. uint8 type;
  397. uint8 flag;
  398. #if WASM_ENABLE_GC != 0
  399. uint8 gc_opcode;
  400. WASMRefType ref_type;
  401. #endif
  402. WASMValue value;
  403. } InitValue;
  404. typedef struct ConstExprContext {
  405. uint32 sp;
  406. uint32 size;
  407. WASMModule *module;
  408. InitValue *stack;
  409. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  410. } ConstExprContext;
  411. static void
  412. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  413. {
  414. ctx->sp = 0;
  415. ctx->module = module;
  416. ctx->stack = ctx->data;
  417. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  418. }
  419. static bool
  420. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  421. #if WASM_ENABLE_GC != 0
  422. WASMRefType *ref_type, uint8 gc_opcode,
  423. #endif
  424. WASMValue *value, char *error_buf, uint32 error_buf_size)
  425. {
  426. InitValue *cur_value;
  427. if (ctx->sp >= ctx->size) {
  428. if (ctx->stack != ctx->data) {
  429. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  430. (ctx->size + 4) * sizeof(InitValue));
  431. }
  432. else {
  433. if (!(ctx->stack =
  434. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  435. error_buf, error_buf_size))) {
  436. goto fail;
  437. }
  438. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  439. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  440. }
  441. ctx->size += 4;
  442. }
  443. cur_value = &ctx->stack[ctx->sp++];
  444. cur_value->type = type;
  445. cur_value->flag = flag;
  446. cur_value->value = *value;
  447. #if WASM_ENABLE_GC != 0
  448. cur_value->gc_opcode = gc_opcode;
  449. if (wasm_is_type_multi_byte_type(type)) {
  450. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  451. ref_type, wasm_reftype_struct_size(ref_type));
  452. }
  453. #endif
  454. return true;
  455. fail:
  456. return false;
  457. }
  458. #if WASM_ENABLE_GC != 0
  459. static void
  460. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  461. {
  462. WASMStructNewInitValues *struct_init_values =
  463. (WASMStructNewInitValues *)data;
  464. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  465. WASMType *wasm_type;
  466. uint32 i;
  467. if (!data)
  468. return;
  469. wasm_type = module->types[struct_init_values->type_idx];
  470. /* The data can only be type of `WASMStructNewInitValues *`
  471. or `WASMArrayNewInitValues *` */
  472. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  473. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  474. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  475. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  476. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  477. WASMRefType *ref_type;
  478. uint8 field_type;
  479. for (i = 0; i < struct_init_values->count; i++) {
  480. field_type = struct_type->fields[i].field_type;
  481. if (wasm_is_type_multi_byte_type(field_type))
  482. ref_type = ref_type_map->ref_type;
  483. else
  484. ref_type = NULL;
  485. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  486. REF_TYPE_STRUCTREF, NULL,
  487. module->types, module->type_count)
  488. || wasm_reftype_is_subtype_of(
  489. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  490. module->types, module->type_count)) {
  491. destroy_init_expr_data_recursive(
  492. module, struct_init_values->fields[i].data);
  493. }
  494. }
  495. }
  496. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  497. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  498. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  499. uint8 elem_type = array_type->elem_type;
  500. for (i = 0; i < array_init_values->length; i++) {
  501. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  502. REF_TYPE_STRUCTREF, NULL,
  503. module->types, module->type_count)
  504. || wasm_reftype_is_subtype_of(
  505. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  506. module->types, module->type_count)) {
  507. destroy_init_expr_data_recursive(
  508. module, array_init_values->elem_data[i].data);
  509. }
  510. }
  511. }
  512. wasm_runtime_free(data);
  513. }
  514. #endif
  515. static bool
  516. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  517. #if WASM_ENABLE_GC != 0
  518. WASMRefType *ref_type, uint8 *p_gc_opcode,
  519. #endif
  520. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  521. {
  522. InitValue *cur_value;
  523. if (ctx->sp == 0) {
  524. set_error_buf(error_buf, error_buf_size,
  525. "type mismatch: const expr stack underflow");
  526. return false;
  527. }
  528. cur_value = &ctx->stack[--ctx->sp];
  529. #if WASM_ENABLE_GC == 0
  530. if (cur_value->type != type) {
  531. set_error_buf(error_buf, error_buf_size, "type mismatch");
  532. return false;
  533. }
  534. #else
  535. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  536. ref_type, ctx->module->types,
  537. ctx->module->type_count)) {
  538. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  539. "type mismatch: expect ", type2str(type),
  540. " but got other");
  541. goto fail;
  542. }
  543. #endif
  544. if (p_flag)
  545. *p_flag = cur_value->flag;
  546. if (p_value)
  547. *p_value = cur_value->value;
  548. #if WASM_ENABLE_GC != 0
  549. if (p_gc_opcode)
  550. *p_gc_opcode = cur_value->gc_opcode;
  551. #endif
  552. return true;
  553. #if WASM_ENABLE_GC != 0
  554. fail:
  555. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  556. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  557. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  558. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  559. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  560. }
  561. return false;
  562. #endif
  563. }
  564. static void
  565. destroy_const_expr_stack(ConstExprContext *ctx)
  566. {
  567. #if WASM_ENABLE_GC != 0
  568. uint32 i;
  569. for (i = 0; i < ctx->sp; i++) {
  570. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  571. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  572. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  573. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  574. destroy_init_expr_data_recursive(ctx->module,
  575. ctx->stack[i].value.data);
  576. }
  577. }
  578. #endif
  579. if (ctx->stack != ctx->data) {
  580. wasm_runtime_free(ctx->stack);
  581. }
  582. }
  583. #if WASM_ENABLE_GC != 0
  584. static void
  585. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  586. {
  587. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  588. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  589. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  590. destroy_init_expr_data_recursive(module, expr->u.data);
  591. }
  592. }
  593. #endif /* end of WASM_ENABLE_GC != 0 */
  594. static bool
  595. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  596. InitializerExpression *init_expr, uint8 type, void *ref_type,
  597. char *error_buf, uint32 error_buf_size)
  598. {
  599. const uint8 *p = *p_buf, *p_end = buf_end;
  600. uint8 flag, *p_float;
  601. uint32 i;
  602. ConstExprContext const_expr_ctx = { 0 };
  603. WASMValue cur_value;
  604. #if WASM_ENABLE_GC != 0
  605. uint32 opcode1, type_idx;
  606. uint8 opcode;
  607. WASMRefType cur_ref_type = { 0 };
  608. #endif
  609. init_const_expr_stack(&const_expr_ctx, module);
  610. CHECK_BUF(p, p_end, 1);
  611. flag = read_uint8(p);
  612. while (flag != WASM_OP_END) {
  613. switch (flag) {
  614. /* i32.const */
  615. case INIT_EXPR_TYPE_I32_CONST:
  616. read_leb_int32(p, p_end, cur_value.i32);
  617. if (!push_const_expr_stack(
  618. &const_expr_ctx, flag, VALUE_TYPE_I32,
  619. #if WASM_ENABLE_GC != 0
  620. NULL, 0,
  621. #endif
  622. &cur_value, error_buf, error_buf_size))
  623. goto fail;
  624. break;
  625. /* i64.const */
  626. case INIT_EXPR_TYPE_I64_CONST:
  627. read_leb_int64(p, p_end, cur_value.i64);
  628. if (!push_const_expr_stack(
  629. &const_expr_ctx, flag, VALUE_TYPE_I64,
  630. #if WASM_ENABLE_GC != 0
  631. NULL, 0,
  632. #endif
  633. &cur_value, error_buf, error_buf_size))
  634. goto fail;
  635. break;
  636. /* f32.const */
  637. case INIT_EXPR_TYPE_F32_CONST:
  638. CHECK_BUF(p, p_end, 4);
  639. p_float = (uint8 *)&cur_value.f32;
  640. for (i = 0; i < sizeof(float32); i++)
  641. *p_float++ = *p++;
  642. if (!push_const_expr_stack(
  643. &const_expr_ctx, flag, VALUE_TYPE_F32,
  644. #if WASM_ENABLE_GC != 0
  645. NULL, 0,
  646. #endif
  647. &cur_value, error_buf, error_buf_size))
  648. goto fail;
  649. break;
  650. /* f64.const */
  651. case INIT_EXPR_TYPE_F64_CONST:
  652. CHECK_BUF(p, p_end, 8);
  653. p_float = (uint8 *)&cur_value.f64;
  654. for (i = 0; i < sizeof(float64); i++)
  655. *p_float++ = *p++;
  656. if (!push_const_expr_stack(
  657. &const_expr_ctx, flag, VALUE_TYPE_F64,
  658. #if WASM_ENABLE_GC != 0
  659. NULL, 0,
  660. #endif
  661. &cur_value, error_buf, error_buf_size))
  662. goto fail;
  663. break;
  664. #if WASM_ENABLE_SIMD != 0
  665. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  666. /* v128.const */
  667. case INIT_EXPR_TYPE_V128_CONST:
  668. {
  669. uint64 high, low;
  670. CHECK_BUF(p, p_end, 1);
  671. (void)read_uint8(p);
  672. CHECK_BUF(p, p_end, 16);
  673. wasm_runtime_read_v128(p, &high, &low);
  674. p += 16;
  675. cur_value.v128.i64x2[0] = high;
  676. cur_value.v128.i64x2[1] = low;
  677. if (!push_const_expr_stack(
  678. &const_expr_ctx, flag, VALUE_TYPE_V128,
  679. #if WASM_ENABLE_GC != 0
  680. NULL, 0,
  681. #endif
  682. &cur_value, error_buf, error_buf_size))
  683. goto fail;
  684. #if WASM_ENABLE_WAMR_COMPILER != 0
  685. /* If any init_expr is v128.const, mark SIMD used */
  686. module->is_simd_used = true;
  687. #endif
  688. break;
  689. }
  690. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  691. #endif /* end of WASM_ENABLE_SIMD */
  692. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  693. /* ref.func */
  694. case INIT_EXPR_TYPE_FUNCREF_CONST:
  695. {
  696. uint32 func_idx;
  697. read_leb_uint32(p, p_end, func_idx);
  698. cur_value.ref_index = func_idx;
  699. if (!check_function_index(module, func_idx, error_buf,
  700. error_buf_size)) {
  701. goto fail;
  702. }
  703. #if WASM_ENABLE_GC == 0
  704. if (!push_const_expr_stack(&const_expr_ctx, flag,
  705. VALUE_TYPE_FUNCREF, &cur_value,
  706. error_buf, error_buf_size))
  707. goto fail;
  708. #else
  709. if (func_idx < module->import_function_count) {
  710. type_idx =
  711. module->import_functions[func_idx].u.function.type_idx;
  712. }
  713. else {
  714. type_idx = module
  715. ->functions[func_idx
  716. - module->import_function_count]
  717. ->type_idx;
  718. }
  719. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  720. false, type_idx);
  721. if (!push_const_expr_stack(&const_expr_ctx, flag,
  722. cur_ref_type.ref_type, &cur_ref_type,
  723. 0, &cur_value, error_buf,
  724. error_buf_size))
  725. goto fail;
  726. #endif
  727. #if WASM_ENABLE_WAMR_COMPILER != 0
  728. module->is_ref_types_used = true;
  729. #endif
  730. break;
  731. }
  732. /* ref.null */
  733. case INIT_EXPR_TYPE_REFNULL_CONST:
  734. {
  735. uint8 type1;
  736. CHECK_BUF(p, p_end, 1);
  737. type1 = read_uint8(p);
  738. #if WASM_ENABLE_GC == 0
  739. cur_value.ref_index = NULL_REF;
  740. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  741. &cur_value, error_buf,
  742. error_buf_size))
  743. goto fail;
  744. #else
  745. cur_value.gc_obj = NULL_REF;
  746. if (!is_byte_a_type(type1)) {
  747. p--;
  748. read_leb_uint32(p, p_end, type_idx);
  749. if (!check_type_index(module, module->type_count, type_idx,
  750. error_buf, error_buf_size))
  751. goto fail;
  752. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  753. true, type_idx);
  754. if (!push_const_expr_stack(&const_expr_ctx, flag,
  755. cur_ref_type.ref_type,
  756. &cur_ref_type, 0, &cur_value,
  757. error_buf, error_buf_size))
  758. goto fail;
  759. }
  760. else {
  761. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  762. NULL, 0, &cur_value, error_buf,
  763. error_buf_size))
  764. goto fail;
  765. }
  766. #endif
  767. #if WASM_ENABLE_WAMR_COMPILER != 0
  768. module->is_ref_types_used = true;
  769. #endif
  770. break;
  771. }
  772. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  773. /* get_global */
  774. case INIT_EXPR_TYPE_GET_GLOBAL:
  775. {
  776. uint32 global_idx;
  777. uint8 global_type;
  778. read_leb_uint32(p, p_end, cur_value.global_index);
  779. global_idx = cur_value.global_index;
  780. /*
  781. * Currently, constant expressions occurring as initializers
  782. * of globals are further constrained in that contained
  783. * global.get instructions are
  784. * only allowed to refer to imported globals.
  785. *
  786. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  787. */
  788. if (global_idx >= module->import_global_count
  789. /* make spec test happy */
  790. #if WASM_ENABLE_GC != 0
  791. + module->global_count
  792. #endif
  793. ) {
  794. set_error_buf_v(error_buf, error_buf_size,
  795. "unknown global %u", global_idx);
  796. goto fail;
  797. }
  798. if (
  799. /* make spec test happy */
  800. #if WASM_ENABLE_GC != 0
  801. global_idx < module->import_global_count &&
  802. #endif
  803. module->import_globals[global_idx]
  804. .u.global.type.is_mutable) {
  805. set_error_buf_v(error_buf, error_buf_size,
  806. "constant expression required");
  807. goto fail;
  808. }
  809. if (global_idx < module->import_global_count) {
  810. global_type = module->import_globals[global_idx]
  811. .u.global.type.val_type;
  812. #if WASM_ENABLE_GC != 0
  813. if (wasm_is_type_multi_byte_type(global_type)) {
  814. WASMRefType *global_ref_type =
  815. module->import_globals[global_idx]
  816. .u.global.ref_type;
  817. bh_memcpy_s(&cur_ref_type,
  818. wasm_reftype_struct_size(global_ref_type),
  819. global_ref_type,
  820. wasm_reftype_struct_size(global_ref_type));
  821. }
  822. #endif
  823. }
  824. else {
  825. global_type =
  826. module
  827. ->globals[global_idx - module->import_global_count]
  828. .type.val_type;
  829. #if WASM_ENABLE_GC != 0
  830. if (wasm_is_type_multi_byte_type(global_type)) {
  831. WASMRefType *global_ref_type =
  832. module
  833. ->globals[global_idx
  834. - module->import_global_count]
  835. .ref_type;
  836. bh_memcpy_s(&cur_ref_type,
  837. wasm_reftype_struct_size(global_ref_type),
  838. global_ref_type,
  839. wasm_reftype_struct_size(global_ref_type));
  840. }
  841. #endif
  842. }
  843. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  844. #if WASM_ENABLE_GC != 0
  845. &cur_ref_type, 0,
  846. #endif
  847. &cur_value, error_buf,
  848. error_buf_size))
  849. goto fail;
  850. break;
  851. }
  852. #if WASM_ENABLE_GC != 0
  853. /* struct.new and array.new */
  854. case WASM_OP_GC_PREFIX:
  855. {
  856. read_leb_uint32(p, p_end, opcode1);
  857. switch (opcode1) {
  858. case WASM_OP_STRUCT_NEW:
  859. {
  860. WASMStructType *struct_type;
  861. WASMStructNewInitValues *struct_init_values = NULL;
  862. uint32 field_count;
  863. read_leb_uint32(p, p_end, type_idx);
  864. if (!check_type_index(module, module->type_count,
  865. type_idx, error_buf,
  866. error_buf_size)) {
  867. goto fail;
  868. }
  869. struct_type = (WASMStructType *)module->types[type_idx];
  870. if (struct_type->base_type.type_flag
  871. != WASM_TYPE_STRUCT) {
  872. set_error_buf(error_buf, error_buf_size,
  873. "unkown struct type");
  874. goto fail;
  875. }
  876. field_count = struct_type->field_count;
  877. if (!(struct_init_values = loader_malloc(
  878. offsetof(WASMStructNewInitValues, fields)
  879. + (uint64)field_count * sizeof(WASMValue),
  880. error_buf, error_buf_size))) {
  881. goto fail;
  882. }
  883. struct_init_values->type_idx = type_idx;
  884. struct_init_values->count = field_count;
  885. for (i = field_count; i > 0; i--) {
  886. WASMRefType *field_ref_type = NULL;
  887. uint32 field_idx = i - 1;
  888. uint8 field_type =
  889. struct_type->fields[field_idx].field_type;
  890. if (wasm_is_type_multi_byte_type(field_type)) {
  891. field_ref_type = wasm_reftype_map_find(
  892. struct_type->ref_type_maps,
  893. struct_type->ref_type_map_count, field_idx);
  894. }
  895. if (is_packed_type(field_type)) {
  896. field_type = VALUE_TYPE_I32;
  897. }
  898. if (!pop_const_expr_stack(
  899. &const_expr_ctx, NULL, field_type,
  900. field_ref_type, NULL,
  901. &struct_init_values->fields[field_idx],
  902. error_buf, error_buf_size)) {
  903. destroy_init_expr_data_recursive(
  904. module, struct_init_values);
  905. goto fail;
  906. }
  907. }
  908. cur_value.data = struct_init_values;
  909. wasm_set_refheaptype_typeidx(
  910. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  911. if (!push_const_expr_stack(
  912. &const_expr_ctx, flag, cur_ref_type.ref_type,
  913. &cur_ref_type, (uint8)opcode1, &cur_value,
  914. error_buf, error_buf_size)) {
  915. destroy_init_expr_data_recursive(
  916. module, struct_init_values);
  917. goto fail;
  918. }
  919. break;
  920. }
  921. case WASM_OP_STRUCT_NEW_DEFAULT:
  922. {
  923. read_leb_uint32(p, p_end, cur_value.type_index);
  924. type_idx = cur_value.type_index;
  925. if (!check_type_index(module, module->type_count,
  926. type_idx, error_buf,
  927. error_buf_size)) {
  928. goto fail;
  929. }
  930. if (module->types[type_idx]->type_flag
  931. != WASM_TYPE_STRUCT) {
  932. set_error_buf(error_buf, error_buf_size,
  933. "unkown struct type");
  934. goto fail;
  935. }
  936. cur_value.type_index = type_idx;
  937. cur_value.data = NULL;
  938. wasm_set_refheaptype_typeidx(
  939. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  940. if (!push_const_expr_stack(
  941. &const_expr_ctx, flag, cur_ref_type.ref_type,
  942. &cur_ref_type, (uint8)opcode1, &cur_value,
  943. error_buf, error_buf_size)) {
  944. goto fail;
  945. }
  946. break;
  947. }
  948. case WASM_OP_ARRAY_NEW:
  949. case WASM_OP_ARRAY_NEW_DEFAULT:
  950. case WASM_OP_ARRAY_NEW_FIXED:
  951. {
  952. WASMArrayNewInitValues *array_init_values = NULL;
  953. WASMArrayType *array_type = NULL;
  954. WASMRefType *elem_ref_type = NULL;
  955. uint64 total_size;
  956. uint8 elem_type;
  957. read_leb_uint32(p, p_end, cur_value.type_index);
  958. type_idx = cur_value.type_index;
  959. if (!check_type_index(module, module->type_count,
  960. type_idx, error_buf,
  961. error_buf_size)) {
  962. goto fail;
  963. }
  964. array_type = (WASMArrayType *)module->types[type_idx];
  965. if (array_type->base_type.type_flag
  966. != WASM_TYPE_ARRAY) {
  967. set_error_buf(error_buf, error_buf_size,
  968. "unkown array type");
  969. goto fail;
  970. }
  971. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  972. elem_type = array_type->elem_type;
  973. if (wasm_is_type_multi_byte_type(elem_type)) {
  974. elem_ref_type = array_type->elem_ref_type;
  975. }
  976. if (is_packed_type(elem_type)) {
  977. elem_type = VALUE_TYPE_I32;
  978. }
  979. if (opcode1 == WASM_OP_ARRAY_NEW) {
  980. WASMValue len_val;
  981. if (!(array_init_values = loader_malloc(
  982. sizeof(WASMArrayNewInitValues),
  983. error_buf, error_buf_size))) {
  984. goto fail;
  985. }
  986. array_init_values->type_idx = type_idx;
  987. if (!pop_const_expr_stack(
  988. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  989. NULL, NULL, &len_val, error_buf,
  990. error_buf_size)) {
  991. destroy_init_expr_data_recursive(
  992. module, array_init_values);
  993. goto fail;
  994. }
  995. array_init_values->length = len_val.i32;
  996. if (!pop_const_expr_stack(
  997. &const_expr_ctx, NULL, elem_type,
  998. elem_ref_type, NULL,
  999. &array_init_values->elem_data[0],
  1000. error_buf, error_buf_size)) {
  1001. destroy_init_expr_data_recursive(
  1002. module, array_init_values);
  1003. goto fail;
  1004. }
  1005. cur_value.data = array_init_values;
  1006. }
  1007. else {
  1008. /* WASM_OP_ARRAY_NEW_FIXED */
  1009. uint32 len;
  1010. read_leb_uint32(p, p_end, len);
  1011. total_size =
  1012. (uint64)offsetof(WASMArrayNewInitValues,
  1013. elem_data)
  1014. + (uint64)sizeof(WASMValue) * len;
  1015. if (!(array_init_values =
  1016. loader_malloc(total_size, error_buf,
  1017. error_buf_size))) {
  1018. goto fail;
  1019. }
  1020. array_init_values->type_idx = type_idx;
  1021. array_init_values->length = len;
  1022. for (i = len; i > 0; i--) {
  1023. if (!pop_const_expr_stack(
  1024. &const_expr_ctx, NULL, elem_type,
  1025. elem_ref_type, NULL,
  1026. &array_init_values
  1027. ->elem_data[i - 1],
  1028. error_buf, error_buf_size)) {
  1029. destroy_init_expr_data_recursive(
  1030. module, array_init_values);
  1031. goto fail;
  1032. }
  1033. }
  1034. cur_value.data = array_init_values;
  1035. }
  1036. }
  1037. else {
  1038. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1039. WASMValue len_val;
  1040. uint32 len;
  1041. /* POP(i32) */
  1042. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1043. VALUE_TYPE_I32, NULL,
  1044. NULL, &len_val, error_buf,
  1045. error_buf_size)) {
  1046. goto fail;
  1047. }
  1048. len = len_val.i32;
  1049. cur_value.array_new_default.type_index = type_idx;
  1050. cur_value.array_new_default.length = len;
  1051. }
  1052. wasm_set_refheaptype_typeidx(
  1053. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1054. if (!push_const_expr_stack(
  1055. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1056. &cur_ref_type, (uint8)opcode1, &cur_value,
  1057. error_buf, error_buf_size)) {
  1058. if (array_init_values) {
  1059. destroy_init_expr_data_recursive(
  1060. module, array_init_values);
  1061. }
  1062. goto fail;
  1063. }
  1064. break;
  1065. }
  1066. case WASM_OP_ANY_CONVERT_EXTERN:
  1067. {
  1068. set_error_buf(error_buf, error_buf_size,
  1069. "unsupported constant expression of "
  1070. "extern.internalize");
  1071. goto fail;
  1072. }
  1073. case WASM_OP_EXTERN_CONVERT_ANY:
  1074. {
  1075. set_error_buf(error_buf, error_buf_size,
  1076. "unsupported constant expression of "
  1077. "extern.externalize");
  1078. goto fail;
  1079. }
  1080. case WASM_OP_REF_I31:
  1081. {
  1082. /* POP(i32) */
  1083. if (!pop_const_expr_stack(
  1084. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1085. NULL, &cur_value, error_buf, error_buf_size)) {
  1086. goto fail;
  1087. }
  1088. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1089. false, HEAP_TYPE_I31);
  1090. if (!push_const_expr_stack(
  1091. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1092. &cur_ref_type, (uint8)opcode1, &cur_value,
  1093. error_buf, error_buf_size)) {
  1094. goto fail;
  1095. }
  1096. break;
  1097. }
  1098. default:
  1099. set_error_buf(
  1100. error_buf, error_buf_size,
  1101. "type mismatch or constant expression required");
  1102. goto fail;
  1103. }
  1104. break;
  1105. }
  1106. #endif /* end of WASM_ENABLE_GC != 0 */
  1107. default:
  1108. {
  1109. set_error_buf(error_buf, error_buf_size,
  1110. "illegal opcode "
  1111. "or constant expression required "
  1112. "or type mismatch");
  1113. goto fail;
  1114. }
  1115. }
  1116. CHECK_BUF(p, p_end, 1);
  1117. flag = read_uint8(p);
  1118. }
  1119. /* There should be only one value left on the init value stack */
  1120. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1121. #if WASM_ENABLE_GC != 0
  1122. ref_type, &opcode,
  1123. #endif
  1124. &cur_value, error_buf, error_buf_size)) {
  1125. goto fail;
  1126. }
  1127. if (const_expr_ctx.sp != 0) {
  1128. set_error_buf(error_buf, error_buf_size,
  1129. "type mismatch: illegal constant opcode sequence");
  1130. goto fail;
  1131. }
  1132. init_expr->init_expr_type = flag;
  1133. init_expr->u = cur_value;
  1134. #if WASM_ENABLE_GC != 0
  1135. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1136. switch (opcode) {
  1137. case WASM_OP_STRUCT_NEW:
  1138. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1139. break;
  1140. case WASM_OP_STRUCT_NEW_DEFAULT:
  1141. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1142. break;
  1143. case WASM_OP_ARRAY_NEW:
  1144. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1145. break;
  1146. case WASM_OP_ARRAY_NEW_DEFAULT:
  1147. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1148. break;
  1149. case WASM_OP_ARRAY_NEW_FIXED:
  1150. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1151. break;
  1152. case WASM_OP_REF_I31:
  1153. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1154. break;
  1155. default:
  1156. bh_assert(0);
  1157. break;
  1158. }
  1159. }
  1160. #endif /* end of WASM_ENABLE_GC != 0 */
  1161. *p_buf = p;
  1162. destroy_const_expr_stack(&const_expr_ctx);
  1163. return true;
  1164. fail:
  1165. destroy_const_expr_stack(&const_expr_ctx);
  1166. return false;
  1167. }
  1168. static bool
  1169. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1170. {
  1171. if (mutable >= 2) {
  1172. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1173. return false;
  1174. }
  1175. return true;
  1176. }
  1177. #if WASM_ENABLE_GC != 0
  1178. static void
  1179. destroy_func_type(WASMFuncType *type)
  1180. {
  1181. /* Destroy the reference type hash set */
  1182. if (type->ref_type_maps)
  1183. wasm_runtime_free(type->ref_type_maps);
  1184. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1185. && WASM_ENABLE_LAZY_JIT != 0
  1186. if (type->call_to_llvm_jit_from_fast_jit)
  1187. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1188. #endif
  1189. /* Free the type */
  1190. wasm_runtime_free(type);
  1191. }
  1192. static void
  1193. destroy_struct_type(WASMStructType *type)
  1194. {
  1195. if (type->ref_type_maps)
  1196. wasm_runtime_free(type->ref_type_maps);
  1197. wasm_runtime_free(type);
  1198. }
  1199. static void
  1200. destroy_array_type(WASMArrayType *type)
  1201. {
  1202. wasm_runtime_free(type);
  1203. }
  1204. static void
  1205. destroy_wasm_type(WASMType *type)
  1206. {
  1207. if (type->ref_count > 1) {
  1208. /* The type is referenced by other types
  1209. of current wasm module */
  1210. type->ref_count--;
  1211. return;
  1212. }
  1213. if (type->type_flag == WASM_TYPE_FUNC)
  1214. destroy_func_type((WASMFuncType *)type);
  1215. else if (type->type_flag == WASM_TYPE_STRUCT)
  1216. destroy_struct_type((WASMStructType *)type);
  1217. else if (type->type_flag == WASM_TYPE_ARRAY)
  1218. destroy_array_type((WASMArrayType *)type);
  1219. else {
  1220. bh_assert(0);
  1221. }
  1222. }
  1223. /* Resolve (ref null ht) or (ref ht) */
  1224. static bool
  1225. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1226. WASMModule *module, uint32 type_count, bool nullable,
  1227. WASMRefType *ref_type, char *error_buf,
  1228. uint32 error_buf_size)
  1229. {
  1230. const uint8 *p = *p_buf, *p_end = buf_end;
  1231. ref_type->ref_type =
  1232. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1233. ref_type->ref_ht_common.nullable = nullable;
  1234. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1235. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1236. /* heap type is (type i), i : typeidx, >= 0 */
  1237. if (!check_type_index(module, type_count,
  1238. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1239. error_buf_size)) {
  1240. return false;
  1241. }
  1242. }
  1243. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1244. /* heap type is func, extern, any, eq, i31 or data */
  1245. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1246. return false;
  1247. }
  1248. *p_buf = p;
  1249. return true;
  1250. fail:
  1251. return false;
  1252. }
  1253. static bool
  1254. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1255. WASMModule *module, uint32 type_count,
  1256. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1257. bool allow_packed_type, char *error_buf,
  1258. uint32 error_buf_size)
  1259. {
  1260. const uint8 *p = *p_buf, *p_end = buf_end;
  1261. uint8 type;
  1262. memset(ref_type, 0, sizeof(WASMRefType));
  1263. CHECK_BUF(p, p_end, 1);
  1264. type = read_uint8(p);
  1265. if (wasm_is_reftype_htref_nullable(type)) {
  1266. /* (ref null ht) */
  1267. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1268. ref_type, error_buf, error_buf_size))
  1269. return false;
  1270. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1271. *p_need_ref_type_map = true;
  1272. else {
  1273. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1274. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1275. multi-byte type to one-byte type to reduce the footprint and
  1276. the complexity of type equal/subtype checking */
  1277. ref_type->ref_type =
  1278. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1279. *p_need_ref_type_map = false;
  1280. }
  1281. }
  1282. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1283. /* (ref ht) */
  1284. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1285. ref_type, error_buf, error_buf_size))
  1286. return false;
  1287. *p_need_ref_type_map = true;
  1288. #if WASM_ENABLE_STRINGREF != 0
  1289. /* covert (ref string) to stringref */
  1290. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1291. ref_type->ref_type =
  1292. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1293. *p_need_ref_type_map = false;
  1294. }
  1295. #endif
  1296. }
  1297. else {
  1298. /* type which can be represented by one byte */
  1299. if (!is_valid_value_type_for_interpreter(type)
  1300. && !(allow_packed_type && is_packed_type(type))) {
  1301. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1302. return false;
  1303. }
  1304. ref_type->ref_type = type;
  1305. *p_need_ref_type_map = false;
  1306. #if WASM_ENABLE_WAMR_COMPILER != 0
  1307. /* If any value's type is v128, mark the module as SIMD used */
  1308. if (type == VALUE_TYPE_V128)
  1309. module->is_simd_used = true;
  1310. #endif
  1311. }
  1312. *p_buf = p;
  1313. return true;
  1314. fail:
  1315. return false;
  1316. }
  1317. static WASMRefType *
  1318. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1319. char *error_buf, uint32 error_buf_size)
  1320. {
  1321. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1322. if (!ret) {
  1323. set_error_buf(error_buf, error_buf_size,
  1324. "insert ref type to hash set failed");
  1325. }
  1326. return ret;
  1327. }
  1328. static bool
  1329. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1330. uint32 type_count, uint32 type_idx, char *error_buf,
  1331. uint32 error_buf_size)
  1332. {
  1333. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1334. uint32 param_count, result_count, i, j = 0;
  1335. uint32 param_cell_num, ret_cell_num;
  1336. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1337. uint64 total_size;
  1338. bool need_ref_type_map;
  1339. WASMRefType ref_type;
  1340. WASMFuncType *type = NULL;
  1341. /* Parse first time to resolve param count, result count and
  1342. ref type map count */
  1343. read_leb_uint32(p, p_end, param_count);
  1344. p_org = p;
  1345. for (i = 0; i < param_count; i++) {
  1346. if (!resolve_value_type(&p, p_end, module, type_count,
  1347. &need_ref_type_map, &ref_type, false, error_buf,
  1348. error_buf_size)) {
  1349. return false;
  1350. }
  1351. if (need_ref_type_map)
  1352. ref_type_map_count++;
  1353. }
  1354. read_leb_uint32(p, p_end, result_count);
  1355. for (i = 0; i < result_count; i++) {
  1356. if (!resolve_value_type(&p, p_end, module, type_count,
  1357. &need_ref_type_map, &ref_type, false, error_buf,
  1358. error_buf_size)) {
  1359. return false;
  1360. }
  1361. if (need_ref_type_map) {
  1362. ref_type_map_count++;
  1363. result_ref_type_map_count++;
  1364. }
  1365. }
  1366. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1367. "ref type map count: %d",
  1368. type_idx, param_count, result_count, ref_type_map_count);
  1369. /* Parse second time to resolve param types, result types and
  1370. ref type map info */
  1371. p = p_org;
  1372. total_size = offsetof(WASMFuncType, types)
  1373. + sizeof(uint8) * (uint64)(param_count + result_count);
  1374. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1375. return false;
  1376. }
  1377. if (ref_type_map_count > 0) {
  1378. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1379. if (!(type->ref_type_maps =
  1380. loader_malloc(total_size, error_buf, error_buf_size))) {
  1381. goto fail;
  1382. }
  1383. }
  1384. type->base_type.type_flag = WASM_TYPE_FUNC;
  1385. type->param_count = param_count;
  1386. type->result_count = result_count;
  1387. type->ref_type_map_count = ref_type_map_count;
  1388. if (ref_type_map_count > 0) {
  1389. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1390. - result_ref_type_map_count;
  1391. }
  1392. for (i = 0; i < param_count; i++) {
  1393. if (!resolve_value_type(&p, p_end, module, type_count,
  1394. &need_ref_type_map, &ref_type, false, error_buf,
  1395. error_buf_size)) {
  1396. goto fail;
  1397. }
  1398. type->types[i] = ref_type.ref_type;
  1399. if (need_ref_type_map) {
  1400. type->ref_type_maps[j].index = i;
  1401. if (!(type->ref_type_maps[j++].ref_type =
  1402. reftype_set_insert(module->ref_type_set, &ref_type,
  1403. error_buf, error_buf_size))) {
  1404. goto fail;
  1405. }
  1406. }
  1407. }
  1408. read_leb_uint32(p, p_end, result_count);
  1409. for (i = 0; i < result_count; i++) {
  1410. if (!resolve_value_type(&p, p_end, module, type_count,
  1411. &need_ref_type_map, &ref_type, false, error_buf,
  1412. error_buf_size)) {
  1413. goto fail;
  1414. }
  1415. type->types[param_count + i] = ref_type.ref_type;
  1416. if (need_ref_type_map) {
  1417. type->ref_type_maps[j].index = param_count + i;
  1418. if (!(type->ref_type_maps[j++].ref_type =
  1419. reftype_set_insert(module->ref_type_set, &ref_type,
  1420. error_buf, error_buf_size))) {
  1421. goto fail;
  1422. }
  1423. }
  1424. }
  1425. bh_assert(j == type->ref_type_map_count);
  1426. #if TRACE_WASM_LOADER != 0
  1427. os_printf("type %d = ", type_idx);
  1428. wasm_dump_func_type(type);
  1429. #endif
  1430. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1431. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1432. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1433. set_error_buf(error_buf, error_buf_size,
  1434. "param count or result count too large");
  1435. goto fail;
  1436. }
  1437. type->param_cell_num = (uint16)param_cell_num;
  1438. type->ret_cell_num = (uint16)ret_cell_num;
  1439. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1440. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1441. #endif
  1442. #if WASM_ENABLE_WAMR_COMPILER != 0
  1443. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1444. if (type->types[i] == VALUE_TYPE_V128)
  1445. module->is_simd_used = true;
  1446. }
  1447. #endif
  1448. *p_buf = p;
  1449. module->types[type_idx] = (WASMType *)type;
  1450. return true;
  1451. fail:
  1452. if (type)
  1453. destroy_func_type(type);
  1454. return false;
  1455. }
  1456. static bool
  1457. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1458. WASMModule *module, uint32 type_count, uint32 type_idx,
  1459. char *error_buf, uint32 error_buf_size)
  1460. {
  1461. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1462. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1463. uint32 i, j = 0, offset;
  1464. uint16 *reference_table;
  1465. uint64 total_size;
  1466. uint8 mutable;
  1467. bool need_ref_type_map;
  1468. WASMRefType ref_type;
  1469. WASMStructType *type = NULL;
  1470. /* Parse first time to resolve field count and ref type map count */
  1471. read_leb_uint32(p, p_end, field_count);
  1472. p_org = p;
  1473. for (i = 0; i < field_count; i++) {
  1474. if (!resolve_value_type(&p, p_end, module, type_count,
  1475. &need_ref_type_map, &ref_type, true, error_buf,
  1476. error_buf_size)) {
  1477. return false;
  1478. }
  1479. if (need_ref_type_map)
  1480. ref_type_map_count++;
  1481. if (wasm_is_type_reftype(ref_type.ref_type))
  1482. ref_field_count++;
  1483. CHECK_BUF(p, p_end, 1);
  1484. mutable = read_uint8(p);
  1485. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1486. return false;
  1487. }
  1488. }
  1489. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1490. type_idx, field_count, ref_type_map_count);
  1491. /* Parse second time to resolve field types and ref type map info */
  1492. p = p_org;
  1493. total_size = offsetof(WASMStructType, fields)
  1494. + sizeof(WASMStructFieldType) * (uint64)field_count
  1495. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1496. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1497. return false;
  1498. }
  1499. if (ref_type_map_count > 0) {
  1500. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1501. if (!(type->ref_type_maps =
  1502. loader_malloc(total_size, error_buf, error_buf_size))) {
  1503. goto fail;
  1504. }
  1505. }
  1506. type->reference_table = reference_table =
  1507. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1508. + sizeof(WASMStructFieldType) * field_count);
  1509. *reference_table++ = ref_field_count;
  1510. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1511. type->field_count = field_count;
  1512. type->ref_type_map_count = ref_type_map_count;
  1513. offset = (uint32)offsetof(WASMStructObject, field_data);
  1514. for (i = 0; i < field_count; i++) {
  1515. if (!resolve_value_type(&p, p_end, module, type_count,
  1516. &need_ref_type_map, &ref_type, true, error_buf,
  1517. error_buf_size)) {
  1518. goto fail;
  1519. }
  1520. type->fields[i].field_type = ref_type.ref_type;
  1521. if (need_ref_type_map) {
  1522. type->ref_type_maps[j].index = i;
  1523. if (!(type->ref_type_maps[j++].ref_type =
  1524. reftype_set_insert(module->ref_type_set, &ref_type,
  1525. error_buf, error_buf_size))) {
  1526. goto fail;
  1527. }
  1528. }
  1529. CHECK_BUF(p, p_end, 1);
  1530. type->fields[i].field_flags = read_uint8(p);
  1531. type->fields[i].field_size =
  1532. (uint8)wasm_reftype_size(ref_type.ref_type);
  1533. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1534. || defined(BUILD_TARGET_X86_32))
  1535. if (type->fields[i].field_size == 2)
  1536. offset = align_uint(offset, 2);
  1537. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1538. offset = align_uint(offset, 4);
  1539. #endif
  1540. type->fields[i].field_offset = offset;
  1541. if (wasm_is_type_reftype(ref_type.ref_type))
  1542. *reference_table++ = offset;
  1543. offset += type->fields[i].field_size;
  1544. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1545. type->fields[i].field_flags, type->fields[i].field_type);
  1546. }
  1547. type->total_size = offset;
  1548. bh_assert(j == type->ref_type_map_count);
  1549. #if TRACE_WASM_LOADER != 0
  1550. os_printf("type %d = ", type_idx);
  1551. wasm_dump_struct_type(type);
  1552. #endif
  1553. *p_buf = p;
  1554. module->types[type_idx] = (WASMType *)type;
  1555. return true;
  1556. fail:
  1557. if (type)
  1558. destroy_struct_type(type);
  1559. return false;
  1560. }
  1561. static bool
  1562. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1563. WASMModule *module, uint32 type_count, uint32 type_idx,
  1564. char *error_buf, uint32 error_buf_size)
  1565. {
  1566. const uint8 *p = *p_buf, *p_end = buf_end;
  1567. uint8 mutable;
  1568. bool need_ref_type_map;
  1569. WASMRefType ref_type;
  1570. WASMArrayType *type = NULL;
  1571. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1572. &ref_type, true, error_buf, error_buf_size)) {
  1573. return false;
  1574. }
  1575. CHECK_BUF(p, p_end, 1);
  1576. mutable = read_uint8(p);
  1577. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1578. return false;
  1579. }
  1580. LOG_VERBOSE("type %u: array", type_idx);
  1581. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1582. error_buf_size))) {
  1583. return false;
  1584. }
  1585. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1586. type->elem_flags = mutable;
  1587. type->elem_type = ref_type.ref_type;
  1588. if (need_ref_type_map) {
  1589. if (!(type->elem_ref_type =
  1590. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1591. error_buf_size))) {
  1592. goto fail;
  1593. }
  1594. }
  1595. #if TRACE_WASM_LOADER != 0
  1596. os_printf("type %d = ", type_idx);
  1597. wasm_dump_array_type(type);
  1598. #endif
  1599. *p_buf = p;
  1600. module->types[type_idx] = (WASMType *)type;
  1601. return true;
  1602. fail:
  1603. if (type)
  1604. destroy_array_type(type);
  1605. return false;
  1606. }
  1607. static bool
  1608. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1609. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1610. {
  1611. if (heap_type >= 0) {
  1612. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1613. error_buf_size)) {
  1614. return false;
  1615. }
  1616. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1617. heap_type);
  1618. }
  1619. else {
  1620. if (!wasm_is_valid_heap_type(heap_type)) {
  1621. set_error_buf(error_buf, error_buf_size, "unknown type");
  1622. return false;
  1623. }
  1624. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1625. heap_type);
  1626. if (nullable) {
  1627. /* For (ref null func/extern/any/eq/i31/data),
  1628. they are same as
  1629. funcref/externref/anyref/eqref/i31ref/dataref,
  1630. we convert the multi-byte type to one-byte
  1631. type to reduce the footprint and the
  1632. complexity of type equal/subtype checking */
  1633. ref_type->ref_type =
  1634. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1635. }
  1636. }
  1637. return true;
  1638. }
  1639. static void
  1640. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1641. WASMRefType *ref_type2)
  1642. {
  1643. /**
  1644. * The difference rt1 ∖ rt2 between two reference types is defined as
  1645. * follows:
  1646. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1647. * (ref ht2) = (ref null?1 ht1)
  1648. */
  1649. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1650. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1651. ref_type1, wasm_reftype_struct_size(ref_type1));
  1652. }
  1653. else {
  1654. ref_type_diff->ref_type = ref_type1->ref_type;
  1655. }
  1656. if (ref_type2->ref_ht_common.nullable) {
  1657. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1658. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1659. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1660. (int32)ref_type_diff->ref_type - 0x80);
  1661. }
  1662. else {
  1663. ref_type_diff->ref_ht_typeidx.nullable = false;
  1664. }
  1665. }
  1666. }
  1667. #else /* else of WASM_ENABLE_GC != 0 */
  1668. static void
  1669. destroy_wasm_type(WASMType *type)
  1670. {
  1671. if (type->ref_count > 1) {
  1672. /* The type is referenced by other types
  1673. of current wasm module */
  1674. type->ref_count--;
  1675. return;
  1676. }
  1677. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1678. && WASM_ENABLE_LAZY_JIT != 0
  1679. if (type->call_to_llvm_jit_from_fast_jit)
  1680. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1681. #endif
  1682. wasm_runtime_free(type);
  1683. }
  1684. #endif /* end of WASM_ENABLE_GC != 0 */
  1685. static bool
  1686. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1687. char *error_buf, uint32 error_buf_size)
  1688. {
  1689. const uint8 *p = buf, *p_end = buf_end;
  1690. uint32 type_count, i;
  1691. uint64 total_size;
  1692. uint8 flag;
  1693. #if WASM_ENABLE_GC != 0
  1694. uint32 processed_type_count = 0;
  1695. #endif
  1696. read_leb_uint32(p, p_end, type_count);
  1697. if (type_count) {
  1698. module->type_count = type_count;
  1699. total_size = sizeof(WASMType *) * (uint64)type_count;
  1700. if (!(module->types =
  1701. loader_malloc(total_size, error_buf, error_buf_size))) {
  1702. return false;
  1703. }
  1704. #if WASM_ENABLE_GC == 0
  1705. for (i = 0; i < type_count; i++) {
  1706. WASMFuncType *type;
  1707. const uint8 *p_org;
  1708. uint32 param_count, result_count, j;
  1709. uint32 param_cell_num, ret_cell_num;
  1710. CHECK_BUF(p, p_end, 1);
  1711. flag = read_uint8(p);
  1712. if (flag != 0x60) {
  1713. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1714. return false;
  1715. }
  1716. read_leb_uint32(p, p_end, param_count);
  1717. /* Resolve param count and result count firstly */
  1718. p_org = p;
  1719. CHECK_BUF(p, p_end, param_count);
  1720. p += param_count;
  1721. read_leb_uint32(p, p_end, result_count);
  1722. CHECK_BUF(p, p_end, result_count);
  1723. p = p_org;
  1724. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1725. set_error_buf(error_buf, error_buf_size,
  1726. "param count or result count too large");
  1727. return false;
  1728. }
  1729. total_size = offsetof(WASMFuncType, types)
  1730. + sizeof(uint8) * (uint64)(param_count + result_count);
  1731. if (!(type = module->types[i] =
  1732. loader_malloc(total_size, error_buf, error_buf_size))) {
  1733. return false;
  1734. }
  1735. /* Resolve param types and result types */
  1736. type->ref_count = 1;
  1737. type->param_count = (uint16)param_count;
  1738. type->result_count = (uint16)result_count;
  1739. for (j = 0; j < param_count; j++) {
  1740. CHECK_BUF(p, p_end, 1);
  1741. type->types[j] = read_uint8(p);
  1742. }
  1743. read_leb_uint32(p, p_end, result_count);
  1744. for (j = 0; j < result_count; j++) {
  1745. CHECK_BUF(p, p_end, 1);
  1746. type->types[param_count + j] = read_uint8(p);
  1747. }
  1748. for (j = 0; j < param_count + result_count; j++) {
  1749. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1750. set_error_buf(error_buf, error_buf_size,
  1751. "unknown value type");
  1752. return false;
  1753. }
  1754. }
  1755. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1756. ret_cell_num =
  1757. wasm_get_cell_num(type->types + param_count, result_count);
  1758. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1759. set_error_buf(error_buf, error_buf_size,
  1760. "param count or result count too large");
  1761. return false;
  1762. }
  1763. type->param_cell_num = (uint16)param_cell_num;
  1764. type->ret_cell_num = (uint16)ret_cell_num;
  1765. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1766. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1767. #endif
  1768. #if WASM_ENABLE_WAMR_COMPILER != 0
  1769. for (j = 0; j < type->param_count + type->result_count; j++) {
  1770. if (type->types[j] == VALUE_TYPE_V128)
  1771. module->is_simd_used = true;
  1772. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1773. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1774. module->is_ref_types_used = true;
  1775. }
  1776. #endif
  1777. /* If there is already a same type created, use it instead */
  1778. for (j = 0; j < i; j++) {
  1779. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1780. if (module->types[j]->ref_count == UINT16_MAX) {
  1781. set_error_buf(error_buf, error_buf_size,
  1782. "wasm type's ref count too large");
  1783. return false;
  1784. }
  1785. destroy_wasm_type(type);
  1786. module->types[i] = module->types[j];
  1787. module->types[j]->ref_count++;
  1788. break;
  1789. }
  1790. }
  1791. }
  1792. #else /* else of WASM_ENABLE_GC == 0 */
  1793. for (i = 0; i < type_count; i++) {
  1794. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1795. uint32 rec_count = 1, j;
  1796. bool is_sub_final = true;
  1797. CHECK_BUF(p, p_end, 1);
  1798. flag = read_uint8(p);
  1799. if (flag == DEFINED_TYPE_REC) {
  1800. read_leb_uint32(p, p_end, rec_count);
  1801. if (rec_count > 1) {
  1802. uint64 new_total_size;
  1803. /* integer overflow */
  1804. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1805. set_error_buf(error_buf, error_buf_size,
  1806. "recursive type count too large");
  1807. return false;
  1808. }
  1809. module->type_count += rec_count - 1;
  1810. new_total_size =
  1811. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1812. if (new_total_size > UINT32_MAX) {
  1813. set_error_buf(error_buf, error_buf_size,
  1814. "allocate memory failed");
  1815. return false;
  1816. }
  1817. MEM_REALLOC(module->types, (uint32)total_size,
  1818. (uint32)new_total_size);
  1819. total_size = new_total_size;
  1820. }
  1821. LOG_VERBOSE("Processing rec group [%d-%d]",
  1822. processed_type_count,
  1823. processed_type_count + rec_count - 1);
  1824. }
  1825. else {
  1826. p--;
  1827. }
  1828. for (j = 0; j < rec_count; j++) {
  1829. WASMType *cur_type = NULL;
  1830. CHECK_BUF(p, p_end, 1);
  1831. flag = read_uint8(p);
  1832. parent_type_idx = -1;
  1833. if (flag == DEFINED_TYPE_SUB
  1834. || flag == DEFINED_TYPE_SUB_FINAL) {
  1835. read_leb_uint32(p, p_end, super_type_count);
  1836. if (super_type_count > 1) {
  1837. set_error_buf(error_buf, error_buf_size,
  1838. "super type count too large");
  1839. return false;
  1840. }
  1841. if (super_type_count > 0) {
  1842. read_leb_uint32(p, p_end, parent_type_idx);
  1843. if (parent_type_idx >= processed_type_count + j) {
  1844. set_error_buf_v(error_buf, error_buf_size,
  1845. "unknown type %d", parent_type_idx);
  1846. return false;
  1847. }
  1848. if (module->types[parent_type_idx]->is_sub_final) {
  1849. set_error_buf(error_buf, error_buf_size,
  1850. "sub type can not inherit from "
  1851. "a final super type");
  1852. return false;
  1853. }
  1854. }
  1855. if (flag == DEFINED_TYPE_SUB)
  1856. is_sub_final = false;
  1857. CHECK_BUF(p, p_end, 1);
  1858. flag = read_uint8(p);
  1859. }
  1860. if (flag == DEFINED_TYPE_FUNC) {
  1861. if (!resolve_func_type(&p, buf_end, module,
  1862. processed_type_count + rec_count,
  1863. processed_type_count + j, error_buf,
  1864. error_buf_size)) {
  1865. return false;
  1866. }
  1867. }
  1868. else if (flag == DEFINED_TYPE_STRUCT) {
  1869. if (!resolve_struct_type(&p, buf_end, module,
  1870. processed_type_count + rec_count,
  1871. processed_type_count + j,
  1872. error_buf, error_buf_size)) {
  1873. return false;
  1874. }
  1875. }
  1876. else if (flag == DEFINED_TYPE_ARRAY) {
  1877. if (!resolve_array_type(&p, buf_end, module,
  1878. processed_type_count + rec_count,
  1879. processed_type_count + j, error_buf,
  1880. error_buf_size)) {
  1881. return false;
  1882. }
  1883. }
  1884. else {
  1885. set_error_buf(error_buf, error_buf_size,
  1886. "invalid type flag");
  1887. return false;
  1888. }
  1889. cur_type = module->types[processed_type_count + j];
  1890. cur_type->ref_count = 1;
  1891. cur_type->parent_type_idx = parent_type_idx;
  1892. cur_type->is_sub_final = is_sub_final;
  1893. cur_type->rec_count = rec_count;
  1894. cur_type->rec_idx = j;
  1895. cur_type->rec_begin_type_idx = processed_type_count;
  1896. }
  1897. /* resolve subtyping relationship in current rec group */
  1898. for (j = 0; j < rec_count; j++) {
  1899. WASMType *cur_type = module->types[processed_type_count + j];
  1900. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1901. WASMType *parent_type =
  1902. module->types[cur_type->parent_type_idx];
  1903. cur_type->parent_type = parent_type;
  1904. cur_type->root_type = parent_type->root_type;
  1905. if (parent_type->inherit_depth == UINT16_MAX) {
  1906. set_error_buf(error_buf, error_buf_size,
  1907. "parent type's inherit depth too large");
  1908. return false;
  1909. }
  1910. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1911. }
  1912. else {
  1913. cur_type->parent_type = NULL;
  1914. cur_type->root_type = cur_type;
  1915. cur_type->inherit_depth = 0;
  1916. }
  1917. }
  1918. for (j = 0; j < rec_count; j++) {
  1919. WASMType *cur_type = module->types[processed_type_count + j];
  1920. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1921. WASMType *parent_type =
  1922. module->types[cur_type->parent_type_idx];
  1923. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1924. module->types,
  1925. module->type_count)) {
  1926. set_error_buf(error_buf, error_buf_size,
  1927. "sub type does not match super type");
  1928. return false;
  1929. }
  1930. }
  1931. }
  1932. /* If there is already an equivalence type or a group of equivalence
  1933. recursive types created, use it or them instead */
  1934. for (j = 0; j < processed_type_count;) {
  1935. WASMType *src_type = module->types[j];
  1936. WASMType *cur_type = module->types[processed_type_count];
  1937. uint32 k, src_rec_count;
  1938. src_rec_count = src_type->rec_count;
  1939. if (src_rec_count != rec_count) {
  1940. /* no type equivalence */
  1941. j += src_rec_count;
  1942. continue;
  1943. }
  1944. for (k = 0; k < rec_count; k++) {
  1945. src_type = module->types[j + k];
  1946. cur_type = module->types[processed_type_count + k];
  1947. if (!wasm_type_equal(src_type, cur_type, module->types,
  1948. module->type_count)) {
  1949. break;
  1950. }
  1951. }
  1952. if (k < rec_count) {
  1953. /* no type equivalence */
  1954. j += src_rec_count;
  1955. continue;
  1956. }
  1957. /* type equivalence */
  1958. for (k = 0; k < rec_count; k++) {
  1959. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1960. set_error_buf(error_buf, error_buf_size,
  1961. "wasm type's ref count too large");
  1962. return false;
  1963. }
  1964. destroy_wasm_type(module->types[processed_type_count + k]);
  1965. module->types[processed_type_count + k] =
  1966. module->types[j + k];
  1967. module->types[j + k]->ref_count++;
  1968. }
  1969. break;
  1970. }
  1971. if (rec_count > 1) {
  1972. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1973. processed_type_count,
  1974. processed_type_count + rec_count - 1);
  1975. }
  1976. processed_type_count += rec_count;
  1977. }
  1978. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1979. * module->type_count,
  1980. error_buf, error_buf_size))) {
  1981. return false;
  1982. }
  1983. #endif /* end of WASM_ENABLE_GC == 0 */
  1984. }
  1985. if (p != p_end) {
  1986. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1987. return false;
  1988. }
  1989. LOG_VERBOSE("Load type section success.\n");
  1990. return true;
  1991. fail:
  1992. return false;
  1993. }
  1994. static void
  1995. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1996. {
  1997. uint32 default_max_size;
  1998. if (UINT32_MAX / 2 > init_size)
  1999. default_max_size = init_size * 2;
  2000. else
  2001. default_max_size = UINT32_MAX;
  2002. if (default_max_size < WASM_TABLE_MAX_SIZE)
  2003. default_max_size = WASM_TABLE_MAX_SIZE;
  2004. if (max_size_flag) {
  2005. /* module defines the table limitation */
  2006. bh_assert(init_size <= *max_size);
  2007. if (init_size < *max_size) {
  2008. *max_size =
  2009. *max_size < default_max_size ? *max_size : default_max_size;
  2010. }
  2011. }
  2012. else {
  2013. /* partial defined table limitation, gives a default value */
  2014. *max_size = default_max_size;
  2015. }
  2016. }
  2017. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2018. /**
  2019. * Find export item of a module with export info:
  2020. * module name, field name and export kind
  2021. */
  2022. static WASMExport *
  2023. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2024. const char *field_name, uint8 export_kind,
  2025. char *error_buf, uint32 error_buf_size)
  2026. {
  2027. WASMExport *export =
  2028. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2029. export_kind, error_buf, error_buf_size);
  2030. return export;
  2031. }
  2032. #endif
  2033. #if WASM_ENABLE_MULTI_MODULE != 0
  2034. static WASMFunction *
  2035. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  2036. const WASMFuncType *expected_function_type,
  2037. char *error_buf, uint32 error_buf_size)
  2038. {
  2039. WASMModuleCommon *module_reg;
  2040. WASMFunction *function = NULL;
  2041. WASMExport *export = NULL;
  2042. WASMModule *module = NULL;
  2043. WASMFuncType *target_function_type = NULL;
  2044. module_reg = wasm_runtime_find_module_registered(module_name);
  2045. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2046. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2047. function_name);
  2048. set_error_buf(error_buf, error_buf_size, "unknown import");
  2049. return NULL;
  2050. }
  2051. module = (WASMModule *)module_reg;
  2052. export =
  2053. wasm_loader_find_export(module, module_name, function_name,
  2054. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2055. if (!export) {
  2056. return NULL;
  2057. }
  2058. /* resolve function type and function */
  2059. if (export->index < module->import_function_count) {
  2060. target_function_type =
  2061. module->import_functions[export->index].u.function.func_type;
  2062. function = module->import_functions[export->index]
  2063. .u.function.import_func_linked;
  2064. }
  2065. else {
  2066. target_function_type =
  2067. module->functions[export->index - module->import_function_count]
  2068. ->func_type;
  2069. function =
  2070. module->functions[export->index - module->import_function_count];
  2071. }
  2072. /* check function type */
  2073. if (!wasm_type_equal((WASMType *)expected_function_type,
  2074. (WASMType *)target_function_type, module->types,
  2075. module->type_count)) {
  2076. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2077. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2078. return NULL;
  2079. }
  2080. return function;
  2081. }
  2082. static WASMTable *
  2083. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2084. uint32 init_size, uint32 max_size, char *error_buf,
  2085. uint32 error_buf_size)
  2086. {
  2087. WASMModuleCommon *module_reg;
  2088. WASMTable *table = NULL;
  2089. WASMExport *export = NULL;
  2090. WASMModule *module = NULL;
  2091. module_reg = wasm_runtime_find_module_registered(module_name);
  2092. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2093. LOG_DEBUG("can not find a module named %s for table", module_name);
  2094. set_error_buf(error_buf, error_buf_size, "unknown import");
  2095. return NULL;
  2096. }
  2097. module = (WASMModule *)module_reg;
  2098. export =
  2099. wasm_loader_find_export(module, module_name, table_name,
  2100. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2101. if (!export) {
  2102. return NULL;
  2103. }
  2104. /* resolve table and check the init/max size */
  2105. if (export->index < module->import_table_count) {
  2106. table =
  2107. module->import_tables[export->index].u.table.import_table_linked;
  2108. }
  2109. else {
  2110. table = &(module->tables[export->index - module->import_table_count]);
  2111. }
  2112. if (table->table_type.init_size < init_size
  2113. || table->table_type.max_size > max_size) {
  2114. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2115. module_name, table_name, table->table_type.init_size,
  2116. table->table_type.max_size, init_size, max_size);
  2117. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2118. return NULL;
  2119. }
  2120. return table;
  2121. }
  2122. static WASMMemory *
  2123. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2124. uint32 init_page_count, uint32 max_page_count,
  2125. char *error_buf, uint32 error_buf_size)
  2126. {
  2127. WASMModuleCommon *module_reg;
  2128. WASMMemory *memory = NULL;
  2129. WASMExport *export = NULL;
  2130. WASMModule *module = NULL;
  2131. module_reg = wasm_runtime_find_module_registered(module_name);
  2132. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2133. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2134. set_error_buf(error_buf, error_buf_size, "unknown import");
  2135. return NULL;
  2136. }
  2137. module = (WASMModule *)module_reg;
  2138. export =
  2139. wasm_loader_find_export(module, module_name, memory_name,
  2140. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2141. if (!export) {
  2142. return NULL;
  2143. }
  2144. /* resolve memory and check the init/max page count */
  2145. if (export->index < module->import_memory_count) {
  2146. memory = module->import_memories[export->index]
  2147. .u.memory.import_memory_linked;
  2148. }
  2149. else {
  2150. memory =
  2151. &(module->memories[export->index - module->import_memory_count]);
  2152. }
  2153. if (memory->init_page_count < init_page_count
  2154. || memory->max_page_count > max_page_count) {
  2155. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2156. module_name, memory_name, memory->init_page_count,
  2157. memory->max_page_count, init_page_count, max_page_count);
  2158. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2159. return NULL;
  2160. }
  2161. return memory;
  2162. }
  2163. static WASMGlobal *
  2164. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2165. uint8 type, bool is_mutable, char *error_buf,
  2166. uint32 error_buf_size)
  2167. {
  2168. WASMModuleCommon *module_reg;
  2169. WASMGlobal *global = NULL;
  2170. WASMExport *export = NULL;
  2171. WASMModule *module = NULL;
  2172. module_reg = wasm_runtime_find_module_registered(module_name);
  2173. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2174. LOG_DEBUG("can not find a module named %s for global", module_name);
  2175. set_error_buf(error_buf, error_buf_size, "unknown import");
  2176. return NULL;
  2177. }
  2178. module = (WASMModule *)module_reg;
  2179. export =
  2180. wasm_loader_find_export(module, module_name, global_name,
  2181. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2182. if (!export) {
  2183. return NULL;
  2184. }
  2185. /* resolve and check the global */
  2186. if (export->index < module->import_global_count) {
  2187. global =
  2188. module->import_globals[export->index].u.global.import_global_linked;
  2189. }
  2190. else {
  2191. global =
  2192. &(module->globals[export->index - module->import_global_count]);
  2193. }
  2194. if (global->type.val_type != type
  2195. || global->type.is_mutable != is_mutable) {
  2196. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2197. module_name, global_name, global->type.val_type,
  2198. global->type.is_mutable, type, is_mutable);
  2199. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2200. return NULL;
  2201. }
  2202. return global;
  2203. }
  2204. #if WASM_ENABLE_TAGS != 0
  2205. static WASMTag *
  2206. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2207. const WASMType *expected_tag_type,
  2208. uint32 *linked_tag_index, char *error_buf,
  2209. uint32 error_buf_size)
  2210. {
  2211. WASMModuleCommon *module_reg;
  2212. WASMTag *tag = NULL;
  2213. WASMExport *export = NULL;
  2214. WASMModule *module = NULL;
  2215. module_reg = wasm_runtime_find_module_registered(module_name);
  2216. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2217. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2218. tag_name);
  2219. set_error_buf(error_buf, error_buf_size, "unknown import");
  2220. return NULL;
  2221. }
  2222. module = (WASMModule *)module_reg;
  2223. export =
  2224. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2225. error_buf, error_buf_size);
  2226. if (!export) {
  2227. return NULL;
  2228. }
  2229. /* resolve tag type and tag */
  2230. if (export->index < module->import_tag_count) {
  2231. /* importing an imported tag from the submodule */
  2232. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2233. }
  2234. else {
  2235. /* importing an section tag from the submodule */
  2236. tag = module->tags[export->index - module->import_tag_count];
  2237. }
  2238. /* check function type */
  2239. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2240. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2241. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2242. return NULL;
  2243. }
  2244. if (linked_tag_index != NULL) {
  2245. *linked_tag_index = export->index;
  2246. }
  2247. return tag;
  2248. }
  2249. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2250. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2251. static bool
  2252. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2253. const WASMModule *parent_module,
  2254. const char *sub_module_name, const char *function_name,
  2255. WASMFunctionImport *function, char *error_buf,
  2256. uint32 error_buf_size)
  2257. {
  2258. const uint8 *p = *p_buf, *p_end = buf_end;
  2259. uint32 declare_type_index = 0;
  2260. WASMFuncType *declare_func_type = NULL;
  2261. WASMFunction *linked_func = NULL;
  2262. #if WASM_ENABLE_MULTI_MODULE != 0
  2263. WASMModule *sub_module = NULL;
  2264. bool is_built_in_module = false;
  2265. #endif
  2266. const char *linked_signature = NULL;
  2267. void *linked_attachment = NULL;
  2268. bool linked_call_conv_raw = false;
  2269. bool is_native_symbol = false;
  2270. read_leb_uint32(p, p_end, declare_type_index);
  2271. *p_buf = p;
  2272. if (declare_type_index >= parent_module->type_count) {
  2273. set_error_buf(error_buf, error_buf_size, "unknown type");
  2274. return false;
  2275. }
  2276. #if WASM_ENABLE_GC != 0
  2277. function->type_idx = declare_type_index;
  2278. #endif
  2279. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2280. declare_type_index = wasm_get_smallest_type_idx(
  2281. parent_module->types, parent_module->type_count, declare_type_index);
  2282. #endif
  2283. declare_func_type =
  2284. (WASMFuncType *)parent_module->types[declare_type_index];
  2285. /* lookup registered native symbols first */
  2286. linked_func = wasm_native_resolve_symbol(
  2287. sub_module_name, function_name, declare_func_type, &linked_signature,
  2288. &linked_attachment, &linked_call_conv_raw);
  2289. if (linked_func) {
  2290. is_native_symbol = true;
  2291. }
  2292. #if WASM_ENABLE_MULTI_MODULE != 0
  2293. else {
  2294. if (!(is_built_in_module =
  2295. wasm_runtime_is_built_in_module(sub_module_name))) {
  2296. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2297. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2298. error_buf_size);
  2299. }
  2300. if (is_built_in_module || sub_module)
  2301. linked_func = wasm_loader_resolve_function(
  2302. sub_module_name, function_name, declare_func_type, error_buf,
  2303. error_buf_size);
  2304. }
  2305. #endif
  2306. function->module_name = (char *)sub_module_name;
  2307. function->field_name = (char *)function_name;
  2308. function->func_type = declare_func_type;
  2309. /* func_ptr_linked is for native registered symbol */
  2310. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2311. function->signature = linked_signature;
  2312. function->attachment = linked_attachment;
  2313. function->call_conv_raw = linked_call_conv_raw;
  2314. #if WASM_ENABLE_MULTI_MODULE != 0
  2315. function->import_module = is_native_symbol ? NULL : sub_module;
  2316. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2317. #endif
  2318. return true;
  2319. fail:
  2320. return false;
  2321. }
  2322. static bool
  2323. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2324. uint32 error_buf_size)
  2325. {
  2326. if (max_size < init_size) {
  2327. set_error_buf(error_buf, error_buf_size,
  2328. "size minimum must not be greater than maximum");
  2329. return false;
  2330. }
  2331. return true;
  2332. }
  2333. static bool
  2334. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2335. WASMModule *parent_module, const char *sub_module_name,
  2336. const char *table_name, WASMTableImport *table,
  2337. char *error_buf, uint32 error_buf_size)
  2338. {
  2339. const uint8 *p = *p_buf, *p_end = buf_end;
  2340. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2341. declare_init_size = 0, declare_max_size = 0;
  2342. #if WASM_ENABLE_MULTI_MODULE != 0
  2343. WASMModule *sub_module = NULL;
  2344. WASMTable *linked_table = NULL;
  2345. #endif
  2346. #if WASM_ENABLE_GC != 0
  2347. WASMRefType ref_type;
  2348. bool need_ref_type_map;
  2349. #endif
  2350. #if WASM_ENABLE_GC == 0
  2351. CHECK_BUF(p, p_end, 1);
  2352. /* 0x70 or 0x6F */
  2353. declare_elem_type = read_uint8(p);
  2354. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2355. #if WASM_ENABLE_REF_TYPES != 0
  2356. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2357. #endif
  2358. ) {
  2359. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2360. return false;
  2361. }
  2362. #else /* else of WASM_ENABLE_GC == 0 */
  2363. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2364. &need_ref_type_map, &ref_type, false, error_buf,
  2365. error_buf_size)) {
  2366. return false;
  2367. }
  2368. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2369. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2370. return false;
  2371. }
  2372. declare_elem_type = ref_type.ref_type;
  2373. if (need_ref_type_map) {
  2374. if (!(table->table_type.elem_ref_type =
  2375. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2376. error_buf, error_buf_size))) {
  2377. return false;
  2378. }
  2379. }
  2380. #if TRACE_WASM_LOADER != 0
  2381. os_printf("import table type: ");
  2382. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2383. os_printf("\n");
  2384. #endif
  2385. #endif /* end of WASM_ENABLE_GC == 0 */
  2386. read_leb_uint32(p, p_end, declare_max_size_flag);
  2387. if (declare_max_size_flag > 1) {
  2388. set_error_buf(error_buf, error_buf_size, "integer too large");
  2389. return false;
  2390. }
  2391. read_leb_uint32(p, p_end, declare_init_size);
  2392. if (declare_max_size_flag) {
  2393. read_leb_uint32(p, p_end, declare_max_size);
  2394. if (!check_table_max_size(declare_init_size, declare_max_size,
  2395. error_buf, error_buf_size))
  2396. return false;
  2397. }
  2398. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2399. &declare_max_size);
  2400. *p_buf = p;
  2401. #if WASM_ENABLE_MULTI_MODULE != 0
  2402. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2403. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2404. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2405. error_buf_size);
  2406. if (sub_module) {
  2407. linked_table = wasm_loader_resolve_table(
  2408. sub_module_name, table_name, declare_init_size,
  2409. declare_max_size, error_buf, error_buf_size);
  2410. if (linked_table) {
  2411. /* reset with linked table limit */
  2412. declare_elem_type = linked_table->table_type.elem_type;
  2413. declare_init_size = linked_table->table_type.init_size;
  2414. declare_max_size = linked_table->table_type.max_size;
  2415. declare_max_size_flag = linked_table->table_type.flags;
  2416. table->import_table_linked = linked_table;
  2417. table->import_module = sub_module;
  2418. }
  2419. }
  2420. }
  2421. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2422. /* (table (export "table") 10 20 funcref) */
  2423. /* we need this section working in wamrc */
  2424. if (!strcmp("spectest", sub_module_name)) {
  2425. const uint32 spectest_table_init_size = 10;
  2426. const uint32 spectest_table_max_size = 20;
  2427. if (strcmp("table", table_name)) {
  2428. set_error_buf(error_buf, error_buf_size,
  2429. "incompatible import type or unknown import");
  2430. return false;
  2431. }
  2432. if (declare_init_size > spectest_table_init_size
  2433. || declare_max_size < spectest_table_max_size) {
  2434. set_error_buf(error_buf, error_buf_size,
  2435. "incompatible import type");
  2436. return false;
  2437. }
  2438. declare_init_size = spectest_table_init_size;
  2439. declare_max_size = spectest_table_max_size;
  2440. }
  2441. /* now we believe all declaration are ok */
  2442. table->table_type.elem_type = declare_elem_type;
  2443. table->table_type.init_size = declare_init_size;
  2444. table->table_type.flags = declare_max_size_flag;
  2445. table->table_type.max_size = declare_max_size;
  2446. #if WASM_ENABLE_WAMR_COMPILER != 0
  2447. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2448. parent_module->is_ref_types_used = true;
  2449. #endif
  2450. (void)parent_module;
  2451. return true;
  2452. fail:
  2453. return false;
  2454. }
  2455. static bool
  2456. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2457. uint32 error_buf_size)
  2458. {
  2459. uint32 default_max_size =
  2460. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2461. if (!is_memory64 && init_size > default_max_size) {
  2462. set_error_buf(error_buf, error_buf_size,
  2463. "memory size must be at most 65536 pages (4GiB)");
  2464. return false;
  2465. }
  2466. #if WASM_ENABLE_MEMORY64 != 0
  2467. else if (is_memory64 && init_size > default_max_size) {
  2468. set_error_buf(
  2469. error_buf, error_buf_size,
  2470. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2471. return false;
  2472. }
  2473. #endif
  2474. return true;
  2475. }
  2476. static bool
  2477. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2478. char *error_buf, uint32 error_buf_size)
  2479. {
  2480. uint32 default_max_size =
  2481. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2482. if (max_size < init_size) {
  2483. set_error_buf(error_buf, error_buf_size,
  2484. "size minimum must not be greater than maximum");
  2485. return false;
  2486. }
  2487. if (!is_memory64 && max_size > default_max_size) {
  2488. set_error_buf(error_buf, error_buf_size,
  2489. "memory size must be at most 65536 pages (4GiB)");
  2490. return false;
  2491. }
  2492. #if WASM_ENABLE_MEMORY64 != 0
  2493. else if (is_memory64 && max_size > default_max_size) {
  2494. set_error_buf(
  2495. error_buf, error_buf_size,
  2496. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2497. return false;
  2498. }
  2499. #endif
  2500. return true;
  2501. }
  2502. static bool
  2503. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2504. WASMModule *parent_module, const char *sub_module_name,
  2505. const char *memory_name, WASMMemoryImport *memory,
  2506. char *error_buf, uint32 error_buf_size)
  2507. {
  2508. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2509. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2510. uint32 pool_size = wasm_runtime_memory_pool_size();
  2511. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2512. / DEFAULT_NUM_BYTES_PER_PAGE;
  2513. #else
  2514. uint32 max_page_count;
  2515. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2516. uint32 mem_flag = 0;
  2517. bool is_memory64 = false;
  2518. uint32 declare_init_page_count = 0;
  2519. uint32 declare_max_page_count = 0;
  2520. #if WASM_ENABLE_MULTI_MODULE != 0
  2521. WASMModule *sub_module = NULL;
  2522. WASMMemory *linked_memory = NULL;
  2523. #endif
  2524. p_org = p;
  2525. read_leb_uint32(p, p_end, mem_flag);
  2526. is_memory64 = mem_flag & MEMORY64_FLAG;
  2527. if (p - p_org > 1) {
  2528. LOG_VERBOSE("integer representation too long");
  2529. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2530. return false;
  2531. }
  2532. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2533. return false;
  2534. }
  2535. read_leb_uint32(p, p_end, declare_init_page_count);
  2536. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2537. error_buf_size)) {
  2538. return false;
  2539. }
  2540. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2541. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2542. #endif
  2543. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2544. read_leb_uint32(p, p_end, declare_max_page_count);
  2545. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2546. declare_max_page_count, error_buf,
  2547. error_buf_size)) {
  2548. return false;
  2549. }
  2550. if (declare_max_page_count > max_page_count) {
  2551. declare_max_page_count = max_page_count;
  2552. }
  2553. }
  2554. else {
  2555. /* Limit the maximum memory size to max_page_count */
  2556. declare_max_page_count = max_page_count;
  2557. }
  2558. #if WASM_ENABLE_MULTI_MODULE != 0
  2559. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2560. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2561. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2562. error_buf_size);
  2563. if (sub_module) {
  2564. linked_memory = wasm_loader_resolve_memory(
  2565. sub_module_name, memory_name, declare_init_page_count,
  2566. declare_max_page_count, error_buf, error_buf_size);
  2567. if (linked_memory) {
  2568. /**
  2569. * reset with linked memory limit
  2570. */
  2571. memory->import_module = sub_module;
  2572. memory->import_memory_linked = linked_memory;
  2573. declare_init_page_count = linked_memory->init_page_count;
  2574. declare_max_page_count = linked_memory->max_page_count;
  2575. }
  2576. }
  2577. }
  2578. #endif
  2579. /* (memory (export "memory") 1 2) */
  2580. if (!strcmp("spectest", sub_module_name)) {
  2581. uint32 spectest_memory_init_page = 1;
  2582. uint32 spectest_memory_max_page = 2;
  2583. if (strcmp("memory", memory_name)) {
  2584. set_error_buf(error_buf, error_buf_size,
  2585. "incompatible import type or unknown import");
  2586. return false;
  2587. }
  2588. if (declare_init_page_count > spectest_memory_init_page
  2589. || declare_max_page_count < spectest_memory_max_page) {
  2590. set_error_buf(error_buf, error_buf_size,
  2591. "incompatible import type");
  2592. return false;
  2593. }
  2594. declare_init_page_count = spectest_memory_init_page;
  2595. declare_max_page_count = spectest_memory_max_page;
  2596. }
  2597. #if WASM_ENABLE_WASI_TEST != 0
  2598. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2599. else if (!strcmp("foo", sub_module_name)) {
  2600. uint32 spectest_memory_init_page = 1;
  2601. uint32 spectest_memory_max_page = 1;
  2602. if (strcmp("bar", memory_name)) {
  2603. set_error_buf(error_buf, error_buf_size,
  2604. "incompatible import type or unknown import");
  2605. return false;
  2606. }
  2607. if (declare_init_page_count > spectest_memory_init_page
  2608. || declare_max_page_count < spectest_memory_max_page) {
  2609. set_error_buf(error_buf, error_buf_size,
  2610. "incompatible import type");
  2611. return false;
  2612. }
  2613. declare_init_page_count = spectest_memory_init_page;
  2614. declare_max_page_count = spectest_memory_max_page;
  2615. }
  2616. #endif
  2617. /* now we believe all declaration are ok */
  2618. memory->mem_type.flags = mem_flag;
  2619. memory->mem_type.init_page_count = declare_init_page_count;
  2620. memory->mem_type.max_page_count = declare_max_page_count;
  2621. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2622. *p_buf = p;
  2623. (void)parent_module;
  2624. return true;
  2625. fail:
  2626. return false;
  2627. }
  2628. #if WASM_ENABLE_TAGS != 0
  2629. static bool
  2630. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2631. const WASMModule *parent_module, /* this module ! */
  2632. const char *sub_module_name, const char *tag_name,
  2633. WASMTagImport *tag, /* structure to fill */
  2634. char *error_buf, uint32 error_buf_size)
  2635. {
  2636. /* attribute and type of the import statement */
  2637. uint8 declare_tag_attribute;
  2638. uint32 declare_type_index;
  2639. const uint8 *p = *p_buf, *p_end = buf_end;
  2640. #if WASM_ENABLE_MULTI_MODULE != 0
  2641. WASMModule *sub_module = NULL;
  2642. #endif
  2643. /* get the one byte attribute */
  2644. CHECK_BUF(p, p_end, 1);
  2645. declare_tag_attribute = read_uint8(p);
  2646. if (declare_tag_attribute != 0) {
  2647. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2648. goto fail;
  2649. }
  2650. /* get type */
  2651. read_leb_uint32(p, p_end, declare_type_index);
  2652. /* compare against module->types */
  2653. if (declare_type_index >= parent_module->type_count) {
  2654. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2655. goto fail;
  2656. }
  2657. WASMFuncType *declare_tag_type =
  2658. (WASMFuncType *)parent_module->types[declare_type_index];
  2659. /* check, that the type of the declared tag returns void */
  2660. if (declare_tag_type->result_count != 0) {
  2661. set_error_buf(error_buf, error_buf_size,
  2662. "tag type signature does not return void");
  2663. goto fail;
  2664. }
  2665. #if WASM_ENABLE_MULTI_MODULE != 0
  2666. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2667. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2668. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2669. error_buf_size);
  2670. if (sub_module) {
  2671. /* wasm_loader_resolve_tag checks, that the imported tag
  2672. * and the declared tag have the same type
  2673. */
  2674. uint32 linked_tag_index = 0;
  2675. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2676. sub_module_name, tag_name, declare_tag_type,
  2677. &linked_tag_index /* out */, error_buf, error_buf_size);
  2678. if (linked_tag) {
  2679. tag->import_module = sub_module;
  2680. tag->import_tag_linked = linked_tag;
  2681. tag->import_tag_index_linked = linked_tag_index;
  2682. }
  2683. }
  2684. }
  2685. #endif
  2686. /* store to module tag declarations */
  2687. tag->attribute = declare_tag_attribute;
  2688. tag->type = declare_type_index;
  2689. tag->module_name = (char *)sub_module_name;
  2690. tag->field_name = (char *)tag_name;
  2691. tag->tag_type = declare_tag_type;
  2692. *p_buf = p;
  2693. (void)parent_module;
  2694. LOG_VERBOSE("Load tag import success\n");
  2695. return true;
  2696. fail:
  2697. return false;
  2698. }
  2699. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2700. static bool
  2701. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2702. WASMModule *parent_module, char *sub_module_name,
  2703. char *global_name, WASMGlobalImport *global, char *error_buf,
  2704. uint32 error_buf_size)
  2705. {
  2706. const uint8 *p = *p_buf, *p_end = buf_end;
  2707. uint8 declare_type = 0;
  2708. uint8 declare_mutable = 0;
  2709. #if WASM_ENABLE_MULTI_MODULE != 0
  2710. WASMModule *sub_module = NULL;
  2711. WASMGlobal *linked_global = NULL;
  2712. #endif
  2713. #if WASM_ENABLE_GC != 0
  2714. WASMRefType ref_type;
  2715. bool need_ref_type_map;
  2716. #endif
  2717. bool ret = false;
  2718. #if WASM_ENABLE_GC == 0
  2719. CHECK_BUF(p, p_end, 2);
  2720. /* global type */
  2721. declare_type = read_uint8(p);
  2722. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2723. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2724. return false;
  2725. }
  2726. declare_mutable = read_uint8(p);
  2727. #else
  2728. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2729. &need_ref_type_map, &ref_type, false, error_buf,
  2730. error_buf_size)) {
  2731. return false;
  2732. }
  2733. declare_type = ref_type.ref_type;
  2734. if (need_ref_type_map) {
  2735. if (!(global->ref_type =
  2736. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2737. error_buf, error_buf_size))) {
  2738. return false;
  2739. }
  2740. }
  2741. #if TRACE_WASM_LOADER != 0
  2742. os_printf("import global type: ");
  2743. wasm_dump_value_type(declare_type, global->ref_type);
  2744. os_printf("\n");
  2745. #endif
  2746. CHECK_BUF(p, p_end, 1);
  2747. declare_mutable = read_uint8(p);
  2748. #endif /* end of WASM_ENABLE_GC == 0 */
  2749. *p_buf = p;
  2750. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2751. return false;
  2752. }
  2753. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2754. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2755. global);
  2756. if (ret) {
  2757. if (global->type.val_type != declare_type
  2758. || global->type.is_mutable != declare_mutable) {
  2759. set_error_buf(error_buf, error_buf_size,
  2760. "incompatible import type");
  2761. return false;
  2762. }
  2763. global->is_linked = true;
  2764. }
  2765. #endif
  2766. #if WASM_ENABLE_MULTI_MODULE != 0
  2767. if (!global->is_linked
  2768. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2769. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2770. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2771. error_buf_size);
  2772. if (sub_module) {
  2773. /* check sub modules */
  2774. linked_global = wasm_loader_resolve_global(
  2775. sub_module_name, global_name, declare_type, declare_mutable,
  2776. error_buf, error_buf_size);
  2777. if (linked_global) {
  2778. global->import_module = sub_module;
  2779. global->import_global_linked = linked_global;
  2780. global->is_linked = true;
  2781. }
  2782. }
  2783. }
  2784. #endif
  2785. global->module_name = sub_module_name;
  2786. global->field_name = global_name;
  2787. global->type.val_type = declare_type;
  2788. global->type.is_mutable = (declare_mutable == 1);
  2789. #if WASM_ENABLE_WAMR_COMPILER != 0
  2790. if (global->type.val_type == VALUE_TYPE_V128)
  2791. parent_module->is_simd_used = true;
  2792. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2793. parent_module->is_ref_types_used = true;
  2794. #endif
  2795. (void)parent_module;
  2796. (void)ret;
  2797. return true;
  2798. fail:
  2799. return false;
  2800. }
  2801. static bool
  2802. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2803. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2804. {
  2805. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2806. #if WASM_ENABLE_GC != 0
  2807. WASMRefType ref_type;
  2808. bool need_ref_type_map;
  2809. #endif
  2810. #if WASM_ENABLE_GC == 0
  2811. CHECK_BUF(p, p_end, 1);
  2812. /* 0x70 or 0x6F */
  2813. table->table_type.elem_type = read_uint8(p);
  2814. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2815. #if WASM_ENABLE_REF_TYPES != 0
  2816. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2817. #endif
  2818. ) {
  2819. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2820. return false;
  2821. }
  2822. #else /* else of WASM_ENABLE_GC == 0 */
  2823. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2824. &need_ref_type_map, &ref_type, false, error_buf,
  2825. error_buf_size)) {
  2826. return false;
  2827. }
  2828. table->table_type.elem_type = ref_type.ref_type;
  2829. if (need_ref_type_map) {
  2830. if (!(table->table_type.elem_ref_type =
  2831. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2832. error_buf_size))) {
  2833. return false;
  2834. }
  2835. }
  2836. #if TRACE_WASM_LOADER != 0
  2837. os_printf("table type: ");
  2838. wasm_dump_value_type(table->table_type.elem_type,
  2839. table->table_type.elem_ref_type);
  2840. os_printf("\n");
  2841. #endif
  2842. #endif /* end of WASM_ENABLE_GC == 0 */
  2843. p_org = p;
  2844. read_leb_uint32(p, p_end, table->table_type.flags);
  2845. #if WASM_ENABLE_SHARED_MEMORY == 0
  2846. if (p - p_org > 1) {
  2847. set_error_buf(error_buf, error_buf_size,
  2848. "integer representation too long");
  2849. return false;
  2850. }
  2851. if (table->table_type.flags > 1) {
  2852. set_error_buf(error_buf, error_buf_size, "integer too large");
  2853. return false;
  2854. }
  2855. #else
  2856. if (p - p_org > 1) {
  2857. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2858. return false;
  2859. }
  2860. if (table->table_type.flags == 2) {
  2861. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2862. return false;
  2863. }
  2864. if (table->table_type.flags > 1) {
  2865. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2866. return false;
  2867. }
  2868. #endif
  2869. read_leb_uint32(p, p_end, table->table_type.init_size);
  2870. if (table->table_type.flags) {
  2871. read_leb_uint32(p, p_end, table->table_type.max_size);
  2872. if (!check_table_max_size(table->table_type.init_size,
  2873. table->table_type.max_size, error_buf,
  2874. error_buf_size))
  2875. return false;
  2876. }
  2877. adjust_table_max_size(table->table_type.init_size, table->table_type.flags,
  2878. &table->table_type.max_size);
  2879. #if WASM_ENABLE_WAMR_COMPILER != 0
  2880. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2881. module->is_ref_types_used = true;
  2882. #endif
  2883. *p_buf = p;
  2884. return true;
  2885. fail:
  2886. return false;
  2887. }
  2888. static bool
  2889. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2890. char *error_buf, uint32 error_buf_size)
  2891. {
  2892. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2893. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2894. uint32 pool_size = wasm_runtime_memory_pool_size();
  2895. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2896. / DEFAULT_NUM_BYTES_PER_PAGE;
  2897. #else
  2898. uint32 max_page_count;
  2899. #endif
  2900. bool is_memory64 = false;
  2901. p_org = p;
  2902. read_leb_uint32(p, p_end, memory->flags);
  2903. is_memory64 = memory->flags & MEMORY64_FLAG;
  2904. if (p - p_org > 1) {
  2905. LOG_VERBOSE("integer representation too long");
  2906. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2907. return false;
  2908. }
  2909. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2910. false)) {
  2911. return false;
  2912. }
  2913. read_leb_uint32(p, p_end, memory->init_page_count);
  2914. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2915. error_buf_size))
  2916. return false;
  2917. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2918. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2919. #endif
  2920. if (memory->flags & 1) {
  2921. read_leb_uint32(p, p_end, memory->max_page_count);
  2922. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2923. memory->max_page_count, error_buf,
  2924. error_buf_size))
  2925. return false;
  2926. if (memory->max_page_count > max_page_count)
  2927. memory->max_page_count = max_page_count;
  2928. }
  2929. else {
  2930. /* Limit the maximum memory size to max_page_count */
  2931. memory->max_page_count = max_page_count;
  2932. }
  2933. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2934. *p_buf = p;
  2935. return true;
  2936. fail:
  2937. return false;
  2938. }
  2939. static bool
  2940. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2941. bool is_load_from_file_buf, char *error_buf,
  2942. uint32 error_buf_size)
  2943. {
  2944. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2945. uint32 import_count, name_len, type_index, i, u32, flags;
  2946. uint64 total_size;
  2947. WASMImport *import;
  2948. WASMImport *import_functions = NULL, *import_tables = NULL;
  2949. WASMImport *import_memories = NULL, *import_globals = NULL;
  2950. #if WASM_ENABLE_TAGS != 0
  2951. WASMImport *import_tags = NULL;
  2952. #endif
  2953. char *sub_module_name, *field_name;
  2954. uint8 u8, kind, global_type;
  2955. read_leb_uint32(p, p_end, import_count);
  2956. if (import_count) {
  2957. module->import_count = import_count;
  2958. total_size = sizeof(WASMImport) * (uint64)import_count;
  2959. if (!(module->imports =
  2960. loader_malloc(total_size, error_buf, error_buf_size))) {
  2961. return false;
  2962. }
  2963. p_old = p;
  2964. /* Scan firstly to get import count of each type */
  2965. for (i = 0; i < import_count; i++) {
  2966. /* module name */
  2967. read_leb_uint32(p, p_end, name_len);
  2968. CHECK_BUF(p, p_end, name_len);
  2969. p += name_len;
  2970. /* field name */
  2971. read_leb_uint32(p, p_end, name_len);
  2972. CHECK_BUF(p, p_end, name_len);
  2973. p += name_len;
  2974. CHECK_BUF(p, p_end, 1);
  2975. /* 0x00/0x01/0x02/0x03/0x04 */
  2976. kind = read_uint8(p);
  2977. switch (kind) {
  2978. case IMPORT_KIND_FUNC: /* import function */
  2979. read_leb_uint32(p, p_end, type_index);
  2980. module->import_function_count++;
  2981. break;
  2982. case IMPORT_KIND_TABLE: /* import table */
  2983. CHECK_BUF(p, p_end, 1);
  2984. /* 0x70 */
  2985. u8 = read_uint8(p);
  2986. read_leb_uint32(p, p_end, flags);
  2987. read_leb_uint32(p, p_end, u32);
  2988. if (flags & 1)
  2989. read_leb_uint32(p, p_end, u32);
  2990. module->import_table_count++;
  2991. if (module->import_table_count > 1) {
  2992. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2993. set_error_buf(error_buf, error_buf_size,
  2994. "multiple tables");
  2995. return false;
  2996. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2997. module->is_ref_types_used = true;
  2998. #endif
  2999. }
  3000. break;
  3001. case IMPORT_KIND_MEMORY: /* import memory */
  3002. read_leb_uint32(p, p_end, flags);
  3003. read_leb_uint32(p, p_end, u32);
  3004. if (flags & 1)
  3005. read_leb_uint32(p, p_end, u32);
  3006. module->import_memory_count++;
  3007. if (module->import_memory_count > 1) {
  3008. set_error_buf(error_buf, error_buf_size,
  3009. "multiple memories");
  3010. return false;
  3011. }
  3012. break;
  3013. #if WASM_ENABLE_TAGS != 0
  3014. case IMPORT_KIND_TAG: /* import tags */
  3015. /* it only counts the number of tags to import */
  3016. module->import_tag_count++;
  3017. CHECK_BUF(p, p_end, 1);
  3018. u8 = read_uint8(p);
  3019. read_leb_uint32(p, p_end, type_index);
  3020. break;
  3021. #endif
  3022. case IMPORT_KIND_GLOBAL: /* import global */
  3023. #if WASM_ENABLE_GC != 0
  3024. /* valtype */
  3025. CHECK_BUF(p, p_end, 1);
  3026. global_type = read_uint8(p);
  3027. if (wasm_is_type_multi_byte_type(global_type)) {
  3028. int32 heap_type;
  3029. read_leb_int32(p, p_end, heap_type);
  3030. (void)heap_type;
  3031. }
  3032. /* mutability */
  3033. CHECK_BUF(p, p_end, 1);
  3034. p += 1;
  3035. #else
  3036. CHECK_BUF(p, p_end, 2);
  3037. p += 2;
  3038. #endif
  3039. (void)global_type;
  3040. module->import_global_count++;
  3041. break;
  3042. default:
  3043. set_error_buf(error_buf, error_buf_size,
  3044. "invalid import kind");
  3045. return false;
  3046. }
  3047. }
  3048. if (module->import_function_count)
  3049. import_functions = module->import_functions = module->imports;
  3050. if (module->import_table_count)
  3051. import_tables = module->import_tables =
  3052. module->imports + module->import_function_count;
  3053. if (module->import_memory_count)
  3054. import_memories = module->import_memories =
  3055. module->imports + module->import_function_count
  3056. + module->import_table_count;
  3057. #if WASM_ENABLE_TAGS != 0
  3058. if (module->import_tag_count)
  3059. import_tags = module->import_tags =
  3060. module->imports + module->import_function_count
  3061. + module->import_table_count + module->import_memory_count;
  3062. if (module->import_global_count)
  3063. import_globals = module->import_globals =
  3064. module->imports + module->import_function_count
  3065. + module->import_table_count + module->import_memory_count
  3066. + module->import_tag_count;
  3067. #else
  3068. if (module->import_global_count)
  3069. import_globals = module->import_globals =
  3070. module->imports + module->import_function_count
  3071. + module->import_table_count + module->import_memory_count;
  3072. #endif
  3073. p = p_old;
  3074. /* Scan again to resolve the data */
  3075. for (i = 0; i < import_count; i++) {
  3076. /* load module name */
  3077. read_leb_uint32(p, p_end, name_len);
  3078. CHECK_BUF(p, p_end, name_len);
  3079. if (!(sub_module_name = wasm_const_str_list_insert(
  3080. p, name_len, module, is_load_from_file_buf, error_buf,
  3081. error_buf_size))) {
  3082. return false;
  3083. }
  3084. p += name_len;
  3085. /* load field name */
  3086. read_leb_uint32(p, p_end, name_len);
  3087. CHECK_BUF(p, p_end, name_len);
  3088. if (!(field_name = wasm_const_str_list_insert(
  3089. p, name_len, module, is_load_from_file_buf, error_buf,
  3090. error_buf_size))) {
  3091. return false;
  3092. }
  3093. p += name_len;
  3094. CHECK_BUF(p, p_end, 1);
  3095. /* 0x00/0x01/0x02/0x03/0x4 */
  3096. kind = read_uint8(p);
  3097. switch (kind) {
  3098. case IMPORT_KIND_FUNC: /* import function */
  3099. bh_assert(import_functions);
  3100. import = import_functions++;
  3101. if (!load_function_import(
  3102. &p, p_end, module, sub_module_name, field_name,
  3103. &import->u.function, error_buf, error_buf_size)) {
  3104. return false;
  3105. }
  3106. break;
  3107. case IMPORT_KIND_TABLE: /* import table */
  3108. bh_assert(import_tables);
  3109. import = import_tables++;
  3110. if (!load_table_import(&p, p_end, module, sub_module_name,
  3111. field_name, &import->u.table,
  3112. error_buf, error_buf_size)) {
  3113. LOG_DEBUG("can not import such a table (%s,%s)",
  3114. sub_module_name, field_name);
  3115. return false;
  3116. }
  3117. break;
  3118. case IMPORT_KIND_MEMORY: /* import memory */
  3119. bh_assert(import_memories);
  3120. import = import_memories++;
  3121. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3122. field_name, &import->u.memory,
  3123. error_buf, error_buf_size)) {
  3124. return false;
  3125. }
  3126. break;
  3127. #if WASM_ENABLE_TAGS != 0
  3128. case IMPORT_KIND_TAG:
  3129. bh_assert(import_tags);
  3130. import = import_tags++;
  3131. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3132. field_name, &import->u.tag, error_buf,
  3133. error_buf_size)) {
  3134. return false;
  3135. }
  3136. break;
  3137. #endif
  3138. case IMPORT_KIND_GLOBAL: /* import global */
  3139. bh_assert(import_globals);
  3140. import = import_globals++;
  3141. if (!load_global_import(&p, p_end, module, sub_module_name,
  3142. field_name, &import->u.global,
  3143. error_buf, error_buf_size)) {
  3144. return false;
  3145. }
  3146. break;
  3147. default:
  3148. set_error_buf(error_buf, error_buf_size,
  3149. "invalid import kind");
  3150. return false;
  3151. }
  3152. import->kind = kind;
  3153. import->u.names.module_name = sub_module_name;
  3154. import->u.names.field_name = field_name;
  3155. }
  3156. #if WASM_ENABLE_LIBC_WASI != 0
  3157. import = module->import_functions;
  3158. for (i = 0; i < module->import_function_count; i++, import++) {
  3159. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3160. || !strcmp(import->u.names.module_name,
  3161. "wasi_snapshot_preview1")) {
  3162. module->import_wasi_api = true;
  3163. break;
  3164. }
  3165. }
  3166. #endif
  3167. }
  3168. if (p != p_end) {
  3169. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3170. return false;
  3171. }
  3172. LOG_VERBOSE("Load import section success.\n");
  3173. (void)u8;
  3174. (void)u32;
  3175. (void)type_index;
  3176. return true;
  3177. fail:
  3178. return false;
  3179. }
  3180. static bool
  3181. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3182. uint32 error_buf_size)
  3183. {
  3184. WASMFuncType *param_type = func->func_type;
  3185. uint32 param_count = param_type->param_count;
  3186. uint8 *param_types = param_type->types;
  3187. uint32 local_count = func->local_count;
  3188. uint8 *local_types = func->local_types;
  3189. uint32 i, local_offset = 0;
  3190. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3191. /*
  3192. * Only allocate memory when total_size is not 0,
  3193. * or the return value of malloc(0) might be NULL on some platforms,
  3194. * which causes wasm loader return false.
  3195. */
  3196. if (total_size > 0
  3197. && !(func->local_offsets =
  3198. loader_malloc(total_size, error_buf, error_buf_size))) {
  3199. return false;
  3200. }
  3201. for (i = 0; i < param_count; i++) {
  3202. func->local_offsets[i] = (uint16)local_offset;
  3203. local_offset += wasm_value_type_cell_num(param_types[i]);
  3204. }
  3205. for (i = 0; i < local_count; i++) {
  3206. func->local_offsets[param_count + i] = (uint16)local_offset;
  3207. local_offset += wasm_value_type_cell_num(local_types[i]);
  3208. }
  3209. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3210. return true;
  3211. }
  3212. static bool
  3213. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3214. const uint8 *buf_code, const uint8 *buf_code_end,
  3215. WASMModule *module, char *error_buf,
  3216. uint32 error_buf_size)
  3217. {
  3218. const uint8 *p = buf, *p_end = buf_end;
  3219. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3220. uint32 func_count;
  3221. uint64 total_size;
  3222. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3223. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3224. uint8 type;
  3225. WASMFunction *func;
  3226. #if WASM_ENABLE_GC != 0
  3227. bool need_ref_type_map;
  3228. WASMRefType ref_type;
  3229. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3230. #endif
  3231. read_leb_uint32(p, p_end, func_count);
  3232. if (buf_code)
  3233. read_leb_uint32(p_code, buf_code_end, code_count);
  3234. if (func_count != code_count) {
  3235. set_error_buf(error_buf, error_buf_size,
  3236. "function and code section have inconsistent lengths or "
  3237. "unexpected end");
  3238. return false;
  3239. }
  3240. if (is_indices_overflow(module->import_function_count, func_count,
  3241. error_buf, error_buf_size))
  3242. return false;
  3243. if (func_count) {
  3244. module->function_count = func_count;
  3245. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3246. if (!(module->functions =
  3247. loader_malloc(total_size, error_buf, error_buf_size))) {
  3248. return false;
  3249. }
  3250. for (i = 0; i < func_count; i++) {
  3251. /* Resolve function type */
  3252. read_leb_uint32(p, p_end, type_index);
  3253. if (type_index >= module->type_count) {
  3254. set_error_buf(error_buf, error_buf_size, "unknown type");
  3255. return false;
  3256. }
  3257. #if WASM_ENABLE_GC != 0
  3258. type_index_org = type_index;
  3259. #endif
  3260. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3261. && WASM_ENABLE_GC == 0
  3262. type_index = wasm_get_smallest_type_idx(
  3263. module->types, module->type_count, type_index);
  3264. #endif
  3265. read_leb_uint32(p_code, buf_code_end, code_size);
  3266. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3267. set_error_buf(error_buf, error_buf_size,
  3268. "invalid function code size");
  3269. return false;
  3270. }
  3271. /* Resolve local set count */
  3272. p_code_end = p_code + code_size;
  3273. local_count = 0;
  3274. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3275. p_code_save = p_code;
  3276. #if WASM_ENABLE_GC != 0
  3277. ref_type_map_count = 0;
  3278. #endif
  3279. /* Calculate total local count */
  3280. for (j = 0; j < local_set_count; j++) {
  3281. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3282. if (sub_local_count > UINT32_MAX - local_count) {
  3283. set_error_buf(error_buf, error_buf_size, "too many locals");
  3284. return false;
  3285. }
  3286. #if WASM_ENABLE_GC == 0
  3287. CHECK_BUF(p_code, buf_code_end, 1);
  3288. /* 0x7F/0x7E/0x7D/0x7C */
  3289. type = read_uint8(p_code);
  3290. local_count += sub_local_count;
  3291. #if WASM_ENABLE_WAMR_COMPILER != 0
  3292. /* If any value's type is v128, mark the module as SIMD used */
  3293. if (type == VALUE_TYPE_V128)
  3294. module->is_simd_used = true;
  3295. #endif
  3296. #else
  3297. if (!resolve_value_type(&p_code, buf_code_end, module,
  3298. module->type_count, &need_ref_type_map,
  3299. &ref_type, false, error_buf,
  3300. error_buf_size)) {
  3301. return false;
  3302. }
  3303. local_count += sub_local_count;
  3304. if (need_ref_type_map)
  3305. ref_type_map_count += sub_local_count;
  3306. #endif
  3307. }
  3308. /* Alloc memory, layout: function structure + local types */
  3309. code_size = (uint32)(p_code_end - p_code);
  3310. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3311. if (!(func = module->functions[i] =
  3312. loader_malloc(total_size, error_buf, error_buf_size))) {
  3313. return false;
  3314. }
  3315. #if WASM_ENABLE_GC != 0
  3316. if (ref_type_map_count > 0) {
  3317. total_size =
  3318. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3319. if (!(func->local_ref_type_maps = loader_malloc(
  3320. total_size, error_buf, error_buf_size))) {
  3321. return false;
  3322. }
  3323. func->local_ref_type_map_count = ref_type_map_count;
  3324. }
  3325. #endif
  3326. /* Set function type, local count, code size and code body */
  3327. func->func_type = (WASMFuncType *)module->types[type_index];
  3328. func->local_count = local_count;
  3329. if (local_count > 0)
  3330. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3331. func->code_size = code_size;
  3332. /*
  3333. * we shall make a copy of code body [p_code, p_code + code_size]
  3334. * when we are worrying about inappropriate releasing behaviour.
  3335. * all code bodies are actually in a buffer which user allocates in
  3336. * his embedding environment and we don't have power on them.
  3337. * it will be like:
  3338. * code_body_cp = malloc(code_size);
  3339. * memcpy(code_body_cp, p_code, code_size);
  3340. * func->code = code_body_cp;
  3341. */
  3342. func->code = (uint8 *)p_code;
  3343. #if WASM_ENABLE_GC != 0
  3344. func->type_idx = type_index_org;
  3345. #endif
  3346. #if WASM_ENABLE_GC != 0
  3347. t = 0;
  3348. #endif
  3349. /* Load each local type */
  3350. p_code = p_code_save;
  3351. local_type_index = 0;
  3352. for (j = 0; j < local_set_count; j++) {
  3353. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3354. /* Note: sub_local_count is allowed to be 0 */
  3355. if (local_type_index > UINT32_MAX - sub_local_count
  3356. || local_type_index + sub_local_count > local_count) {
  3357. set_error_buf(error_buf, error_buf_size,
  3358. "invalid local count");
  3359. return false;
  3360. }
  3361. #if WASM_ENABLE_GC == 0
  3362. CHECK_BUF(p_code, buf_code_end, 1);
  3363. /* 0x7F/0x7E/0x7D/0x7C */
  3364. type = read_uint8(p_code);
  3365. if (!is_valid_value_type_for_interpreter(type)) {
  3366. if (type == VALUE_TYPE_V128)
  3367. set_error_buf(error_buf, error_buf_size,
  3368. "v128 value type requires simd feature");
  3369. else if (type == VALUE_TYPE_FUNCREF
  3370. || type == VALUE_TYPE_EXTERNREF)
  3371. set_error_buf(error_buf, error_buf_size,
  3372. "ref value type requires "
  3373. "reference types feature");
  3374. else
  3375. set_error_buf_v(error_buf, error_buf_size,
  3376. "invalid local type 0x%02X", type);
  3377. return false;
  3378. }
  3379. #else
  3380. if (!resolve_value_type(&p_code, buf_code_end, module,
  3381. module->type_count, &need_ref_type_map,
  3382. &ref_type, false, error_buf,
  3383. error_buf_size)) {
  3384. return false;
  3385. }
  3386. if (need_ref_type_map) {
  3387. WASMRefType *ref_type_tmp;
  3388. if (!(ref_type_tmp = reftype_set_insert(
  3389. module->ref_type_set, &ref_type, error_buf,
  3390. error_buf_size))) {
  3391. return false;
  3392. }
  3393. for (k = 0; k < sub_local_count; k++) {
  3394. func->local_ref_type_maps[t + k].ref_type =
  3395. ref_type_tmp;
  3396. func->local_ref_type_maps[t + k].index =
  3397. local_type_index + k;
  3398. }
  3399. t += sub_local_count;
  3400. }
  3401. type = ref_type.ref_type;
  3402. #endif
  3403. for (k = 0; k < sub_local_count; k++) {
  3404. func->local_types[local_type_index++] = type;
  3405. }
  3406. #if WASM_ENABLE_WAMR_COMPILER != 0
  3407. if (type == VALUE_TYPE_V128)
  3408. module->is_simd_used = true;
  3409. else if (type == VALUE_TYPE_FUNCREF
  3410. || type == VALUE_TYPE_EXTERNREF)
  3411. module->is_ref_types_used = true;
  3412. #endif
  3413. }
  3414. bh_assert(local_type_index == func->local_count);
  3415. #if WASM_ENABLE_GC != 0
  3416. bh_assert(t == func->local_ref_type_map_count);
  3417. #if TRACE_WASM_LOADER != 0
  3418. os_printf("func %u, local types: [", i);
  3419. k = 0;
  3420. for (j = 0; j < func->local_count; j++) {
  3421. WASMRefType *ref_type_tmp = NULL;
  3422. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3423. bh_assert(j == func->local_ref_type_maps[k].index);
  3424. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3425. }
  3426. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3427. if (j < func->local_count - 1)
  3428. os_printf(" ");
  3429. }
  3430. os_printf("]\n");
  3431. #endif
  3432. #endif
  3433. func->param_cell_num = func->func_type->param_cell_num;
  3434. func->ret_cell_num = func->func_type->ret_cell_num;
  3435. local_cell_num =
  3436. wasm_get_cell_num(func->local_types, func->local_count);
  3437. if (local_cell_num > UINT16_MAX) {
  3438. set_error_buf(error_buf, error_buf_size,
  3439. "local count too large");
  3440. return false;
  3441. }
  3442. func->local_cell_num = (uint16)local_cell_num;
  3443. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3444. return false;
  3445. p_code = p_code_end;
  3446. }
  3447. }
  3448. if (p != p_end) {
  3449. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3450. return false;
  3451. }
  3452. LOG_VERBOSE("Load function section success.\n");
  3453. return true;
  3454. fail:
  3455. return false;
  3456. }
  3457. static bool
  3458. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3459. char *error_buf, uint32 error_buf_size)
  3460. {
  3461. const uint8 *p = buf, *p_end = buf_end;
  3462. uint32 table_count, i;
  3463. uint64 total_size;
  3464. WASMTable *table;
  3465. read_leb_uint32(p, p_end, table_count);
  3466. if (module->import_table_count + table_count > 1) {
  3467. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3468. /* a total of one table is allowed */
  3469. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3470. return false;
  3471. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3472. module->is_ref_types_used = true;
  3473. #endif
  3474. }
  3475. if (table_count) {
  3476. module->table_count = table_count;
  3477. total_size = sizeof(WASMTable) * (uint64)table_count;
  3478. if (!(module->tables =
  3479. loader_malloc(total_size, error_buf, error_buf_size))) {
  3480. return false;
  3481. }
  3482. /* load each table */
  3483. table = module->tables;
  3484. for (i = 0; i < table_count; i++, table++) {
  3485. #if WASM_ENABLE_GC != 0
  3486. uint8 flag;
  3487. bool has_init = false;
  3488. CHECK_BUF(buf, buf_end, 1);
  3489. flag = read_uint8(p);
  3490. if (flag == TABLE_INIT_EXPR_FLAG) {
  3491. CHECK_BUF(buf, buf_end, 1);
  3492. flag = read_uint8(p);
  3493. if (flag != 0x00) {
  3494. set_error_buf(error_buf, error_buf_size,
  3495. "invalid leading bytes for table");
  3496. return false;
  3497. }
  3498. has_init = true;
  3499. }
  3500. else {
  3501. p--;
  3502. }
  3503. #endif /* end of WASM_ENABLE_GC != 0 */
  3504. if (!load_table(&p, p_end, module, table, error_buf,
  3505. error_buf_size))
  3506. return false;
  3507. #if WASM_ENABLE_GC != 0
  3508. if (has_init) {
  3509. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3510. table->table_type.elem_type,
  3511. table->table_type.elem_ref_type, error_buf,
  3512. error_buf_size))
  3513. return false;
  3514. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3515. && table->init_expr.init_expr_type
  3516. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3517. set_error_buf(
  3518. error_buf, error_buf_size,
  3519. "unsupported initializer expression for table");
  3520. return false;
  3521. }
  3522. }
  3523. else {
  3524. if (wasm_is_reftype_htref_non_nullable(
  3525. table->table_type.elem_type)) {
  3526. set_error_buf(
  3527. error_buf, error_buf_size,
  3528. "type mismatch: non-nullable table without init expr");
  3529. return false;
  3530. }
  3531. }
  3532. #endif /* end of WASM_ENABLE_GC != 0 */
  3533. #if WASM_ENABLE_WAMR_COMPILER != 0
  3534. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3535. module->is_ref_types_used = true;
  3536. #endif
  3537. }
  3538. }
  3539. if (p != p_end) {
  3540. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3541. return false;
  3542. }
  3543. LOG_VERBOSE("Load table section success.\n");
  3544. return true;
  3545. fail:
  3546. return false;
  3547. }
  3548. static bool
  3549. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3550. char *error_buf, uint32 error_buf_size)
  3551. {
  3552. const uint8 *p = buf, *p_end = buf_end;
  3553. uint32 memory_count, i;
  3554. uint64 total_size;
  3555. WASMMemory *memory;
  3556. read_leb_uint32(p, p_end, memory_count);
  3557. /* a total of one memory is allowed */
  3558. if (module->import_memory_count + memory_count > 1) {
  3559. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3560. return false;
  3561. }
  3562. if (memory_count) {
  3563. module->memory_count = memory_count;
  3564. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3565. if (!(module->memories =
  3566. loader_malloc(total_size, error_buf, error_buf_size))) {
  3567. return false;
  3568. }
  3569. /* load each memory */
  3570. memory = module->memories;
  3571. for (i = 0; i < memory_count; i++, memory++)
  3572. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3573. return false;
  3574. }
  3575. if (p != p_end) {
  3576. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3577. return false;
  3578. }
  3579. LOG_VERBOSE("Load memory section success.\n");
  3580. return true;
  3581. fail:
  3582. return false;
  3583. }
  3584. static bool
  3585. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3586. char *error_buf, uint32 error_buf_size)
  3587. {
  3588. const uint8 *p = buf, *p_end = buf_end;
  3589. uint32 global_count, i;
  3590. uint64 total_size;
  3591. WASMGlobal *global;
  3592. uint8 mutable;
  3593. #if WASM_ENABLE_GC != 0
  3594. bool need_ref_type_map;
  3595. WASMRefType ref_type;
  3596. #endif
  3597. read_leb_uint32(p, p_end, global_count);
  3598. if (is_indices_overflow(module->import_global_count, global_count,
  3599. error_buf, error_buf_size))
  3600. return false;
  3601. module->global_count = 0;
  3602. if (global_count) {
  3603. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3604. if (!(module->globals =
  3605. loader_malloc(total_size, error_buf, error_buf_size))) {
  3606. return false;
  3607. }
  3608. global = module->globals;
  3609. for (i = 0; i < global_count; i++, global++) {
  3610. #if WASM_ENABLE_GC == 0
  3611. CHECK_BUF(p, p_end, 2);
  3612. /* global type */
  3613. global->type.val_type = read_uint8(p);
  3614. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3615. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3616. return false;
  3617. }
  3618. mutable = read_uint8(p);
  3619. #else
  3620. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3621. &need_ref_type_map, &ref_type, false,
  3622. error_buf, error_buf_size)) {
  3623. return false;
  3624. }
  3625. global->type.val_type = ref_type.ref_type;
  3626. CHECK_BUF(p, p_end, 1);
  3627. mutable = read_uint8(p);
  3628. #endif /* end of WASM_ENABLE_GC */
  3629. #if WASM_ENABLE_WAMR_COMPILER != 0
  3630. if (global->type.val_type == VALUE_TYPE_V128)
  3631. module->is_simd_used = true;
  3632. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3633. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3634. module->is_ref_types_used = true;
  3635. #endif
  3636. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3637. return false;
  3638. }
  3639. global->type.is_mutable = mutable ? true : false;
  3640. /* initialize expression */
  3641. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3642. global->type.val_type,
  3643. #if WASM_ENABLE_GC == 0
  3644. NULL,
  3645. #else
  3646. &ref_type,
  3647. #endif
  3648. error_buf, error_buf_size))
  3649. return false;
  3650. #if WASM_ENABLE_GC != 0
  3651. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3652. uint8 global_type;
  3653. WASMRefType *global_ref_type;
  3654. uint32 global_idx = global->init_expr.u.global_index;
  3655. if (global->init_expr.u.global_index
  3656. >= module->import_global_count + i) {
  3657. set_error_buf(error_buf, error_buf_size, "unknown global");
  3658. return false;
  3659. }
  3660. if (global_idx < module->import_global_count) {
  3661. global_type = module->import_globals[global_idx]
  3662. .u.global.type.val_type;
  3663. global_ref_type =
  3664. module->import_globals[global_idx].u.global.ref_type;
  3665. }
  3666. else {
  3667. global_type =
  3668. module
  3669. ->globals[global_idx - module->import_global_count]
  3670. .type.val_type;
  3671. global_ref_type =
  3672. module
  3673. ->globals[global_idx - module->import_global_count]
  3674. .ref_type;
  3675. }
  3676. if (!wasm_reftype_is_subtype_of(
  3677. global_type, global_ref_type, global->type.val_type,
  3678. global->ref_type, module->types, module->type_count)) {
  3679. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3680. return false;
  3681. }
  3682. }
  3683. if (need_ref_type_map) {
  3684. if (!(global->ref_type =
  3685. reftype_set_insert(module->ref_type_set, &ref_type,
  3686. error_buf, error_buf_size))) {
  3687. return false;
  3688. }
  3689. }
  3690. #if TRACE_WASM_LOADER != 0
  3691. os_printf("global type: ");
  3692. wasm_dump_value_type(global->type, global->ref_type);
  3693. os_printf("\n");
  3694. #endif
  3695. #endif
  3696. module->global_count++;
  3697. }
  3698. bh_assert(module->global_count == global_count);
  3699. }
  3700. if (p != p_end) {
  3701. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3702. return false;
  3703. }
  3704. LOG_VERBOSE("Load global section success.\n");
  3705. return true;
  3706. fail:
  3707. return false;
  3708. }
  3709. static bool
  3710. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3711. bool is_load_from_file_buf, char *error_buf,
  3712. uint32 error_buf_size)
  3713. {
  3714. const uint8 *p = buf, *p_end = buf_end;
  3715. uint32 export_count, i, j, index;
  3716. uint64 total_size;
  3717. uint32 str_len;
  3718. WASMExport *export;
  3719. const char *name;
  3720. read_leb_uint32(p, p_end, export_count);
  3721. if (export_count) {
  3722. module->export_count = export_count;
  3723. total_size = sizeof(WASMExport) * (uint64)export_count;
  3724. if (!(module->exports =
  3725. loader_malloc(total_size, error_buf, error_buf_size))) {
  3726. return false;
  3727. }
  3728. export = module->exports;
  3729. for (i = 0; i < export_count; i++, export ++) {
  3730. #if WASM_ENABLE_THREAD_MGR == 0
  3731. if (p == p_end) {
  3732. /* export section with inconsistent count:
  3733. n export declared, but less than n given */
  3734. set_error_buf(error_buf, error_buf_size,
  3735. "length out of bounds");
  3736. return false;
  3737. }
  3738. #endif
  3739. read_leb_uint32(p, p_end, str_len);
  3740. CHECK_BUF(p, p_end, str_len);
  3741. for (j = 0; j < i; j++) {
  3742. name = module->exports[j].name;
  3743. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3744. set_error_buf(error_buf, error_buf_size,
  3745. "duplicate export name");
  3746. return false;
  3747. }
  3748. }
  3749. if (!(export->name = wasm_const_str_list_insert(
  3750. p, str_len, module, is_load_from_file_buf, error_buf,
  3751. error_buf_size))) {
  3752. return false;
  3753. }
  3754. p += str_len;
  3755. CHECK_BUF(p, p_end, 1);
  3756. export->kind = read_uint8(p);
  3757. read_leb_uint32(p, p_end, index);
  3758. export->index = index;
  3759. switch (export->kind) {
  3760. /* function index */
  3761. case EXPORT_KIND_FUNC:
  3762. if (index >= module->function_count
  3763. + module->import_function_count) {
  3764. set_error_buf(error_buf, error_buf_size,
  3765. "unknown function");
  3766. return false;
  3767. }
  3768. #if WASM_ENABLE_SIMD != 0
  3769. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3770. /* TODO: check func type, if it has v128 param or result,
  3771. report error */
  3772. #endif
  3773. #endif
  3774. break;
  3775. /* table index */
  3776. case EXPORT_KIND_TABLE:
  3777. if (index
  3778. >= module->table_count + module->import_table_count) {
  3779. set_error_buf(error_buf, error_buf_size,
  3780. "unknown table");
  3781. return false;
  3782. }
  3783. break;
  3784. /* memory index */
  3785. case EXPORT_KIND_MEMORY:
  3786. if (index
  3787. >= module->memory_count + module->import_memory_count) {
  3788. set_error_buf(error_buf, error_buf_size,
  3789. "unknown memory");
  3790. return false;
  3791. }
  3792. break;
  3793. #if WASM_ENABLE_TAGS != 0
  3794. /* export tag */
  3795. case EXPORT_KIND_TAG:
  3796. if (index >= module->tag_count + module->import_tag_count) {
  3797. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3798. return false;
  3799. }
  3800. break;
  3801. #endif
  3802. /* global index */
  3803. case EXPORT_KIND_GLOBAL:
  3804. if (index
  3805. >= module->global_count + module->import_global_count) {
  3806. set_error_buf(error_buf, error_buf_size,
  3807. "unknown global");
  3808. return false;
  3809. }
  3810. break;
  3811. default:
  3812. set_error_buf(error_buf, error_buf_size,
  3813. "invalid export kind");
  3814. return false;
  3815. }
  3816. }
  3817. }
  3818. if (p != p_end) {
  3819. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3820. return false;
  3821. }
  3822. LOG_VERBOSE("Load export section success.\n");
  3823. return true;
  3824. fail:
  3825. return false;
  3826. }
  3827. static bool
  3828. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3829. uint32 error_buf_size)
  3830. {
  3831. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3832. if (table_index != 0) {
  3833. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3834. return false;
  3835. }
  3836. #endif
  3837. if (table_index >= module->import_table_count + module->table_count) {
  3838. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3839. table_index);
  3840. return false;
  3841. }
  3842. return true;
  3843. }
  3844. static bool
  3845. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3846. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3847. {
  3848. const uint8 *p = *p_buf, *p_end = buf_end;
  3849. uint32 table_index;
  3850. read_leb_uint32(p, p_end, table_index);
  3851. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3852. return false;
  3853. }
  3854. *p_table_index = table_index;
  3855. *p_buf = p;
  3856. return true;
  3857. fail:
  3858. return false;
  3859. }
  3860. /* Element segments must match element type of table */
  3861. static bool
  3862. check_table_elem_type(WASMModule *module, uint32 table_index,
  3863. uint32 type_from_elem_seg, char *error_buf,
  3864. uint32 error_buf_size)
  3865. {
  3866. uint32 table_declared_elem_type;
  3867. if (table_index < module->import_table_count)
  3868. table_declared_elem_type =
  3869. module->import_tables[table_index].u.table.table_type.elem_type;
  3870. else
  3871. table_declared_elem_type =
  3872. (module->tables + table_index)->table_type.elem_type;
  3873. if (table_declared_elem_type == type_from_elem_seg)
  3874. return true;
  3875. #if WASM_ENABLE_GC != 0
  3876. /*
  3877. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3878. */
  3879. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3880. return true;
  3881. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3882. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3883. return true;
  3884. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3885. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3886. return true;
  3887. #endif
  3888. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3889. return false;
  3890. }
  3891. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3892. static bool
  3893. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3894. uint32 *p_elem_type,
  3895. #if WASM_ENABLE_GC != 0
  3896. WASMRefType **p_elem_ref_type,
  3897. #endif
  3898. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3899. {
  3900. const uint8 *p = *p_buf, *p_end = buf_end;
  3901. uint8 elem_type;
  3902. #if WASM_ENABLE_GC != 0
  3903. WASMRefType elem_ref_type;
  3904. bool need_ref_type_map;
  3905. #endif
  3906. CHECK_BUF(p, p_end, 1);
  3907. elem_type = read_uint8(p);
  3908. if (elemkind_zero) {
  3909. if (elem_type != 0) {
  3910. set_error_buf(error_buf, error_buf_size,
  3911. "invalid reference type or unknown type");
  3912. return false;
  3913. }
  3914. else {
  3915. *p_elem_type = VALUE_TYPE_FUNCREF;
  3916. *p_buf = p;
  3917. return true;
  3918. }
  3919. }
  3920. #if WASM_ENABLE_GC == 0
  3921. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3922. set_error_buf(error_buf, error_buf_size,
  3923. "invalid reference type or unknown type");
  3924. return false;
  3925. }
  3926. *p_elem_type = elem_type;
  3927. #else
  3928. p--;
  3929. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3930. module->type_count, &need_ref_type_map,
  3931. &elem_ref_type, false, error_buf, error_buf_size)) {
  3932. return false;
  3933. }
  3934. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3935. set_error_buf(error_buf, error_buf_size,
  3936. "invalid reference type or unknown type");
  3937. return false;
  3938. }
  3939. *p_elem_type = elem_ref_type.ref_type;
  3940. if (need_ref_type_map) {
  3941. if (!(*p_elem_ref_type =
  3942. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3943. error_buf, error_buf_size))) {
  3944. return false;
  3945. }
  3946. }
  3947. #endif
  3948. *p_buf = p;
  3949. return true;
  3950. fail:
  3951. return false;
  3952. }
  3953. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3954. static bool
  3955. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3956. WASMModule *module, WASMTableSeg *table_segment,
  3957. char *error_buf, uint32 error_buf_size)
  3958. {
  3959. const uint8 *p = *p_buf, *p_end = buf_end;
  3960. uint32 function_count, function_index = 0, i;
  3961. uint64 total_size;
  3962. read_leb_uint32(p, p_end, function_count);
  3963. table_segment->value_count = function_count;
  3964. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3965. if (total_size > 0
  3966. && !(table_segment->init_values =
  3967. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3968. error_buf_size))) {
  3969. return false;
  3970. }
  3971. for (i = 0; i < function_count; i++) {
  3972. InitializerExpression *init_expr = &table_segment->init_values[i];
  3973. read_leb_uint32(p, p_end, function_index);
  3974. if (!check_function_index(module, function_index, error_buf,
  3975. error_buf_size)) {
  3976. return false;
  3977. }
  3978. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3979. init_expr->u.ref_index = function_index;
  3980. }
  3981. *p_buf = p;
  3982. return true;
  3983. fail:
  3984. return false;
  3985. }
  3986. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3987. static bool
  3988. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3989. WASMModule *module, WASMTableSeg *table_segment,
  3990. char *error_buf, uint32 error_buf_size)
  3991. {
  3992. const uint8 *p = *p_buf, *p_end = buf_end;
  3993. uint32 ref_count, i;
  3994. uint64 total_size;
  3995. read_leb_uint32(p, p_end, ref_count);
  3996. table_segment->value_count = ref_count;
  3997. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3998. if (total_size > 0
  3999. && !(table_segment->init_values =
  4000. (InitializerExpression *)loader_malloc(total_size, error_buf,
  4001. error_buf_size))) {
  4002. return false;
  4003. }
  4004. for (i = 0; i < ref_count; i++) {
  4005. InitializerExpression *init_expr = &table_segment->init_values[i];
  4006. if (!load_init_expr(module, &p, p_end, init_expr,
  4007. table_segment->elem_type,
  4008. #if WASM_ENABLE_GC == 0
  4009. NULL,
  4010. #else
  4011. table_segment->elem_ref_type,
  4012. #endif
  4013. error_buf, error_buf_size))
  4014. return false;
  4015. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  4016. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  4017. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  4018. && init_expr->init_expr_type
  4019. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  4020. }
  4021. *p_buf = p;
  4022. return true;
  4023. fail:
  4024. return false;
  4025. }
  4026. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4027. static bool
  4028. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4029. WASMModule *module, char *error_buf,
  4030. uint32 error_buf_size)
  4031. {
  4032. const uint8 *p = buf, *p_end = buf_end;
  4033. uint32 table_segment_count, i;
  4034. uint64 total_size;
  4035. WASMTableSeg *table_segment;
  4036. read_leb_uint32(p, p_end, table_segment_count);
  4037. if (table_segment_count) {
  4038. module->table_seg_count = table_segment_count;
  4039. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4040. if (!(module->table_segments =
  4041. loader_malloc(total_size, error_buf, error_buf_size))) {
  4042. return false;
  4043. }
  4044. table_segment = module->table_segments;
  4045. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4046. if (p >= p_end) {
  4047. set_error_buf(error_buf, error_buf_size,
  4048. "invalid value type or "
  4049. "invalid elements segment kind");
  4050. return false;
  4051. }
  4052. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4053. read_leb_uint32(p, p_end, table_segment->mode);
  4054. /* last three bits */
  4055. table_segment->mode = table_segment->mode & 0x07;
  4056. switch (table_segment->mode) {
  4057. /* elemkind/elemtype + active */
  4058. case 0:
  4059. case 4:
  4060. {
  4061. #if WASM_ENABLE_GC != 0
  4062. if (table_segment->mode == 0) {
  4063. /* vec(funcidx), set elem type to (ref func) */
  4064. WASMRefType elem_ref_type = { 0 };
  4065. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4066. wasm_set_refheaptype_common(
  4067. &elem_ref_type.ref_ht_common, false,
  4068. HEAP_TYPE_FUNC);
  4069. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4070. module->ref_type_set, &elem_ref_type,
  4071. error_buf, error_buf_size)))
  4072. return false;
  4073. }
  4074. else {
  4075. /* vec(expr), set elem type to funcref */
  4076. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4077. }
  4078. #else
  4079. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4080. #endif
  4081. table_segment->table_index = 0;
  4082. if (!check_table_index(module, table_segment->table_index,
  4083. error_buf, error_buf_size))
  4084. return false;
  4085. if (!load_init_expr(
  4086. module, &p, p_end, &table_segment->base_offset,
  4087. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4088. return false;
  4089. if (table_segment->mode == 0) {
  4090. /* vec(funcidx) */
  4091. if (!load_func_index_vec(&p, p_end, module,
  4092. table_segment, error_buf,
  4093. error_buf_size))
  4094. return false;
  4095. }
  4096. else {
  4097. /* vec(expr) */
  4098. if (!load_init_expr_vec(&p, p_end, module,
  4099. table_segment, error_buf,
  4100. error_buf_size))
  4101. return false;
  4102. }
  4103. if (!check_table_elem_type(module,
  4104. table_segment->table_index,
  4105. table_segment->elem_type,
  4106. error_buf, error_buf_size))
  4107. return false;
  4108. break;
  4109. }
  4110. /* elemkind + passive/declarative */
  4111. case 1:
  4112. case 3:
  4113. if (!load_elem_type(module, &p, p_end,
  4114. &table_segment->elem_type,
  4115. #if WASM_ENABLE_GC != 0
  4116. &table_segment->elem_ref_type,
  4117. #endif
  4118. true, error_buf, error_buf_size))
  4119. return false;
  4120. /* vec(funcidx) */
  4121. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4122. error_buf, error_buf_size))
  4123. return false;
  4124. break;
  4125. /* elemkind/elemtype + table_idx + active */
  4126. case 2:
  4127. case 6:
  4128. if (!load_table_index(&p, p_end, module,
  4129. &table_segment->table_index,
  4130. error_buf, error_buf_size))
  4131. return false;
  4132. if (!load_init_expr(
  4133. module, &p, p_end, &table_segment->base_offset,
  4134. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4135. return false;
  4136. if (!load_elem_type(module, &p, p_end,
  4137. &table_segment->elem_type,
  4138. #if WASM_ENABLE_GC != 0
  4139. &table_segment->elem_ref_type,
  4140. #endif
  4141. table_segment->mode == 2 ? true : false,
  4142. error_buf, error_buf_size))
  4143. return false;
  4144. if (table_segment->mode == 2) {
  4145. /* vec(funcidx) */
  4146. if (!load_func_index_vec(&p, p_end, module,
  4147. table_segment, error_buf,
  4148. error_buf_size))
  4149. return false;
  4150. }
  4151. else {
  4152. /* vec(expr) */
  4153. if (!load_init_expr_vec(&p, p_end, module,
  4154. table_segment, error_buf,
  4155. error_buf_size))
  4156. return false;
  4157. }
  4158. if (!check_table_elem_type(module,
  4159. table_segment->table_index,
  4160. table_segment->elem_type,
  4161. error_buf, error_buf_size))
  4162. return false;
  4163. break;
  4164. case 5:
  4165. case 7:
  4166. if (!load_elem_type(module, &p, p_end,
  4167. &table_segment->elem_type,
  4168. #if WASM_ENABLE_GC != 0
  4169. &table_segment->elem_ref_type,
  4170. #endif
  4171. false, error_buf, error_buf_size))
  4172. return false;
  4173. /* vec(expr) */
  4174. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4175. error_buf, error_buf_size))
  4176. return false;
  4177. break;
  4178. default:
  4179. set_error_buf(error_buf, error_buf_size,
  4180. "unknown element segment kind");
  4181. return false;
  4182. }
  4183. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4184. /*
  4185. * like: 00 41 05 0b 04 00 01 00 01
  4186. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4187. */
  4188. if (!load_table_index(&p, p_end, module,
  4189. &table_segment->table_index, error_buf,
  4190. error_buf_size))
  4191. return false;
  4192. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4193. VALUE_TYPE_I32, NULL, error_buf,
  4194. error_buf_size))
  4195. return false;
  4196. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4197. error_buf, error_buf_size))
  4198. return false;
  4199. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4200. if (!check_table_elem_type(module, table_segment->table_index,
  4201. table_segment->elem_type, error_buf,
  4202. error_buf_size))
  4203. return false;
  4204. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4205. #if WASM_ENABLE_WAMR_COMPILER != 0
  4206. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4207. module->is_ref_types_used = true;
  4208. #endif
  4209. }
  4210. }
  4211. if (p != p_end) {
  4212. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4213. return false;
  4214. }
  4215. LOG_VERBOSE("Load table segment section success.\n");
  4216. return true;
  4217. fail:
  4218. return false;
  4219. }
  4220. static bool
  4221. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4222. WASMModule *module,
  4223. #if WASM_ENABLE_BULK_MEMORY != 0
  4224. bool has_datacount_section,
  4225. #endif
  4226. bool clone_data_seg, char *error_buf,
  4227. uint32 error_buf_size)
  4228. {
  4229. const uint8 *p = buf, *p_end = buf_end;
  4230. uint32 data_seg_count, i, mem_index, data_seg_len;
  4231. uint64 total_size;
  4232. WASMDataSeg *dataseg;
  4233. InitializerExpression init_expr;
  4234. #if WASM_ENABLE_BULK_MEMORY != 0
  4235. bool is_passive = false;
  4236. uint32 mem_flag;
  4237. #endif
  4238. uint8 mem_offset_type = VALUE_TYPE_I32;
  4239. read_leb_uint32(p, p_end, data_seg_count);
  4240. #if WASM_ENABLE_BULK_MEMORY != 0
  4241. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4242. set_error_buf(error_buf, error_buf_size,
  4243. "data count and data section have inconsistent lengths");
  4244. return false;
  4245. }
  4246. #endif
  4247. if (data_seg_count) {
  4248. module->data_seg_count = data_seg_count;
  4249. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4250. if (!(module->data_segments =
  4251. loader_malloc(total_size, error_buf, error_buf_size))) {
  4252. return false;
  4253. }
  4254. for (i = 0; i < data_seg_count; i++) {
  4255. read_leb_uint32(p, p_end, mem_index);
  4256. #if WASM_ENABLE_BULK_MEMORY != 0
  4257. is_passive = false;
  4258. mem_flag = mem_index & 0x03;
  4259. switch (mem_flag) {
  4260. case 0x01:
  4261. is_passive = true;
  4262. #if WASM_ENABLE_WAMR_COMPILER != 0
  4263. module->is_bulk_memory_used = true;
  4264. #endif
  4265. break;
  4266. case 0x00:
  4267. /* no memory index, treat index as 0 */
  4268. mem_index = 0;
  4269. goto check_mem_index;
  4270. case 0x02:
  4271. /* read following memory index */
  4272. read_leb_uint32(p, p_end, mem_index);
  4273. #if WASM_ENABLE_WAMR_COMPILER != 0
  4274. module->is_bulk_memory_used = true;
  4275. #endif
  4276. check_mem_index:
  4277. if (mem_index
  4278. >= module->import_memory_count + module->memory_count) {
  4279. set_error_buf_v(error_buf, error_buf_size,
  4280. "unknown memory %d", mem_index);
  4281. return false;
  4282. }
  4283. break;
  4284. case 0x03:
  4285. default:
  4286. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4287. return false;
  4288. break;
  4289. }
  4290. #else
  4291. if (mem_index
  4292. >= module->import_memory_count + module->memory_count) {
  4293. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4294. mem_index);
  4295. return false;
  4296. }
  4297. #endif /* WASM_ENABLE_BULK_MEMORY */
  4298. #if WASM_ENABLE_BULK_MEMORY != 0
  4299. if (!is_passive)
  4300. #endif
  4301. {
  4302. #if WASM_ENABLE_MEMORY64 != 0
  4303. /* This memory_flag is from memory instead of data segment */
  4304. uint8 memory_flag;
  4305. if (module->import_memory_count > 0) {
  4306. memory_flag = module->import_memories[mem_index]
  4307. .u.memory.mem_type.flags;
  4308. }
  4309. else {
  4310. memory_flag =
  4311. module
  4312. ->memories[mem_index - module->import_memory_count]
  4313. .flags;
  4314. }
  4315. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4316. : VALUE_TYPE_I32;
  4317. #else
  4318. mem_offset_type = VALUE_TYPE_I32;
  4319. #endif
  4320. }
  4321. #if WASM_ENABLE_BULK_MEMORY != 0
  4322. if (!is_passive)
  4323. #endif
  4324. if (!load_init_expr(module, &p, p_end, &init_expr,
  4325. mem_offset_type, NULL, error_buf,
  4326. error_buf_size))
  4327. return false;
  4328. read_leb_uint32(p, p_end, data_seg_len);
  4329. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4330. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4331. return false;
  4332. }
  4333. #if WASM_ENABLE_BULK_MEMORY != 0
  4334. dataseg->is_passive = is_passive;
  4335. if (!is_passive)
  4336. #endif
  4337. {
  4338. bh_memcpy_s(&dataseg->base_offset,
  4339. sizeof(InitializerExpression), &init_expr,
  4340. sizeof(InitializerExpression));
  4341. dataseg->memory_index = mem_index;
  4342. }
  4343. dataseg->data_length = data_seg_len;
  4344. CHECK_BUF(p, p_end, data_seg_len);
  4345. if (clone_data_seg) {
  4346. if (!(dataseg->data = loader_malloc(
  4347. dataseg->data_length, error_buf, error_buf_size))) {
  4348. return false;
  4349. }
  4350. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4351. data_seg_len);
  4352. }
  4353. else {
  4354. dataseg->data = (uint8 *)p;
  4355. }
  4356. dataseg->is_data_cloned = clone_data_seg;
  4357. p += data_seg_len;
  4358. }
  4359. }
  4360. if (p != p_end) {
  4361. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4362. return false;
  4363. }
  4364. LOG_VERBOSE("Load data segment section success.\n");
  4365. return true;
  4366. fail:
  4367. return false;
  4368. }
  4369. #if WASM_ENABLE_BULK_MEMORY != 0
  4370. static bool
  4371. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4372. WASMModule *module, char *error_buf,
  4373. uint32 error_buf_size)
  4374. {
  4375. const uint8 *p = buf, *p_end = buf_end;
  4376. uint32 data_seg_count1 = 0;
  4377. read_leb_uint32(p, p_end, data_seg_count1);
  4378. module->data_seg_count1 = data_seg_count1;
  4379. if (p != p_end) {
  4380. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4381. return false;
  4382. }
  4383. #if WASM_ENABLE_WAMR_COMPILER != 0
  4384. module->is_bulk_memory_used = true;
  4385. #endif
  4386. LOG_VERBOSE("Load datacount section success.\n");
  4387. return true;
  4388. fail:
  4389. return false;
  4390. }
  4391. #endif
  4392. #if WASM_ENABLE_TAGS != 0
  4393. static bool
  4394. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4395. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4396. uint32 error_buf_size)
  4397. {
  4398. (void)buf_code;
  4399. (void)buf_code_end;
  4400. const uint8 *p = buf, *p_end = buf_end;
  4401. size_t total_size = 0;
  4402. /* number of tags defined in the section */
  4403. uint32 section_tag_count = 0;
  4404. uint8 tag_attribute;
  4405. uint32 tag_type;
  4406. WASMTag *tag = NULL;
  4407. /* get tag count */
  4408. read_leb_uint32(p, p_end, section_tag_count);
  4409. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4410. error_buf, error_buf_size))
  4411. return false;
  4412. module->tag_count = section_tag_count;
  4413. if (section_tag_count) {
  4414. total_size = sizeof(WASMTag *) * module->tag_count;
  4415. if (!(module->tags =
  4416. loader_malloc(total_size, error_buf, error_buf_size))) {
  4417. return false;
  4418. }
  4419. /* load each tag, imported tags precede the tags */
  4420. uint32 tag_index;
  4421. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4422. /* get the one byte attribute */
  4423. CHECK_BUF(p, p_end, 1);
  4424. tag_attribute = read_uint8(p);
  4425. /* get type */
  4426. read_leb_uint32(p, p_end, tag_type);
  4427. /* compare against module->types */
  4428. if (tag_type >= module->type_count) {
  4429. set_error_buf(error_buf, error_buf_size, "unknown type");
  4430. return false;
  4431. }
  4432. /* get return type (must be 0) */
  4433. /* check, that the type of the referred tag returns void */
  4434. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4435. if (func_type->result_count != 0) {
  4436. set_error_buf(error_buf, error_buf_size,
  4437. "non-empty tag result type");
  4438. goto fail;
  4439. }
  4440. if (!(tag = module->tags[tag_index] = loader_malloc(
  4441. sizeof(WASMTag), error_buf, error_buf_size))) {
  4442. return false;
  4443. }
  4444. /* store to module tag declarations */
  4445. tag->attribute = tag_attribute;
  4446. tag->type = tag_type;
  4447. tag->tag_type = func_type;
  4448. }
  4449. }
  4450. if (p != p_end) {
  4451. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4452. return false;
  4453. }
  4454. LOG_VERBOSE("Load tag section success.\n");
  4455. return true;
  4456. fail:
  4457. return false;
  4458. }
  4459. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4460. static bool
  4461. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4462. const uint8 *buf_func_end, WASMModule *module,
  4463. char *error_buf, uint32 error_buf_size)
  4464. {
  4465. const uint8 *p = buf, *p_end = buf_end;
  4466. const uint8 *p_func = buf_func;
  4467. uint32 func_count = 0, code_count;
  4468. /* code has been loaded in function section, so pass it here, just check
  4469. * whether function and code section have inconsistent lengths */
  4470. read_leb_uint32(p, p_end, code_count);
  4471. if (buf_func)
  4472. read_leb_uint32(p_func, buf_func_end, func_count);
  4473. if (func_count != code_count) {
  4474. set_error_buf(error_buf, error_buf_size,
  4475. "function and code section have inconsistent lengths");
  4476. return false;
  4477. }
  4478. LOG_VERBOSE("Load code segment section success.\n");
  4479. (void)module;
  4480. return true;
  4481. fail:
  4482. return false;
  4483. }
  4484. static bool
  4485. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4486. char *error_buf, uint32 error_buf_size)
  4487. {
  4488. const uint8 *p = buf, *p_end = buf_end;
  4489. WASMFuncType *type;
  4490. uint32 start_function;
  4491. read_leb_uint32(p, p_end, start_function);
  4492. if (start_function
  4493. >= module->function_count + module->import_function_count) {
  4494. set_error_buf(error_buf, error_buf_size, "unknown function");
  4495. return false;
  4496. }
  4497. if (start_function < module->import_function_count)
  4498. type = module->import_functions[start_function].u.function.func_type;
  4499. else
  4500. type = module->functions[start_function - module->import_function_count]
  4501. ->func_type;
  4502. if (type->param_count != 0 || type->result_count != 0) {
  4503. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4504. return false;
  4505. }
  4506. module->start_function = start_function;
  4507. if (p != p_end) {
  4508. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4509. return false;
  4510. }
  4511. LOG_VERBOSE("Load start section success.\n");
  4512. return true;
  4513. fail:
  4514. return false;
  4515. }
  4516. #if WASM_ENABLE_STRINGREF != 0
  4517. static bool
  4518. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4519. WASMModule *module, bool is_load_from_file_buf,
  4520. char *error_buf, uint32 error_buf_size)
  4521. {
  4522. const uint8 *p = buf, *p_end = buf_end;
  4523. int32 deferred_count, immediate_count, string_length, i;
  4524. uint64 total_size;
  4525. read_leb_uint32(p, p_end, deferred_count);
  4526. read_leb_uint32(p, p_end, immediate_count);
  4527. /* proposal set deferred_count for future extension */
  4528. if (deferred_count != 0) {
  4529. goto fail;
  4530. }
  4531. if (immediate_count > 0) {
  4532. total_size = sizeof(char *) * (uint64)immediate_count;
  4533. if (!(module->string_literal_ptrs =
  4534. loader_malloc(total_size, error_buf, error_buf_size))) {
  4535. goto fail;
  4536. }
  4537. module->string_literal_count = immediate_count;
  4538. total_size = sizeof(uint32) * (uint64)immediate_count;
  4539. if (!(module->string_literal_lengths =
  4540. loader_malloc(total_size, error_buf, error_buf_size))) {
  4541. goto fail;
  4542. }
  4543. for (i = 0; i < immediate_count; i++) {
  4544. read_leb_uint32(p, p_end, string_length);
  4545. CHECK_BUF(p, p_end, string_length);
  4546. module->string_literal_ptrs[i] = p;
  4547. module->string_literal_lengths[i] = string_length;
  4548. p += string_length;
  4549. }
  4550. }
  4551. if (p != p_end) {
  4552. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4553. goto fail;
  4554. }
  4555. LOG_VERBOSE("Load stringref section success.\n");
  4556. return true;
  4557. fail:
  4558. return false;
  4559. }
  4560. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4561. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4562. static bool
  4563. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4564. bool is_load_from_file_buf, char *error_buf,
  4565. uint32 error_buf_size)
  4566. {
  4567. const uint8 *p = buf, *p_end = buf_end;
  4568. uint32 name_type, subsection_size;
  4569. uint32 previous_name_type = 0;
  4570. uint32 num_func_name;
  4571. uint32 func_index;
  4572. uint32 previous_func_index = ~0U;
  4573. uint32 func_name_len;
  4574. uint32 name_index;
  4575. int i = 0;
  4576. if (p >= p_end) {
  4577. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4578. return false;
  4579. }
  4580. while (p < p_end) {
  4581. read_leb_uint32(p, p_end, name_type);
  4582. if (i != 0) {
  4583. if (name_type == previous_name_type) {
  4584. set_error_buf(error_buf, error_buf_size,
  4585. "duplicate sub-section");
  4586. return false;
  4587. }
  4588. if (name_type < previous_name_type) {
  4589. set_error_buf(error_buf, error_buf_size,
  4590. "out-of-order sub-section");
  4591. return false;
  4592. }
  4593. }
  4594. previous_name_type = name_type;
  4595. read_leb_uint32(p, p_end, subsection_size);
  4596. CHECK_BUF(p, p_end, subsection_size);
  4597. switch (name_type) {
  4598. case SUB_SECTION_TYPE_FUNC:
  4599. if (subsection_size) {
  4600. read_leb_uint32(p, p_end, num_func_name);
  4601. for (name_index = 0; name_index < num_func_name;
  4602. name_index++) {
  4603. read_leb_uint32(p, p_end, func_index);
  4604. if (func_index == previous_func_index) {
  4605. set_error_buf(error_buf, error_buf_size,
  4606. "duplicate function name");
  4607. return false;
  4608. }
  4609. if (func_index < previous_func_index
  4610. && previous_func_index != ~0U) {
  4611. set_error_buf(error_buf, error_buf_size,
  4612. "out-of-order function index ");
  4613. return false;
  4614. }
  4615. previous_func_index = func_index;
  4616. read_leb_uint32(p, p_end, func_name_len);
  4617. CHECK_BUF(p, p_end, func_name_len);
  4618. /* Skip the import functions */
  4619. if (func_index >= module->import_function_count) {
  4620. func_index -= module->import_function_count;
  4621. if (func_index >= module->function_count) {
  4622. set_error_buf(error_buf, error_buf_size,
  4623. "out-of-range function index");
  4624. return false;
  4625. }
  4626. if (!(module->functions[func_index]->field_name =
  4627. wasm_const_str_list_insert(
  4628. p, func_name_len, module,
  4629. #if WASM_ENABLE_WAMR_COMPILER != 0
  4630. false,
  4631. #else
  4632. is_load_from_file_buf,
  4633. #endif
  4634. error_buf, error_buf_size))) {
  4635. return false;
  4636. }
  4637. }
  4638. p += func_name_len;
  4639. }
  4640. }
  4641. break;
  4642. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4643. */
  4644. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4645. default:
  4646. p = p + subsection_size;
  4647. break;
  4648. }
  4649. i++;
  4650. }
  4651. return true;
  4652. fail:
  4653. return false;
  4654. }
  4655. #endif
  4656. static bool
  4657. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4658. bool is_load_from_file_buf, char *error_buf,
  4659. uint32 error_buf_size)
  4660. {
  4661. const uint8 *p = buf, *p_end = buf_end;
  4662. char section_name[32];
  4663. uint32 name_len, buffer_len;
  4664. if (p >= p_end) {
  4665. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4666. return false;
  4667. }
  4668. read_leb_uint32(p, p_end, name_len);
  4669. if (p + name_len > p_end) {
  4670. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4671. return false;
  4672. }
  4673. if (!wasm_check_utf8_str(p, name_len)) {
  4674. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4675. return false;
  4676. }
  4677. buffer_len = sizeof(section_name);
  4678. memset(section_name, 0, buffer_len);
  4679. if (name_len < buffer_len) {
  4680. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4681. }
  4682. else {
  4683. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4684. memset(section_name + buffer_len - 4, '.', 3);
  4685. }
  4686. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4687. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4688. module->name_section_buf = buf;
  4689. module->name_section_buf_end = buf_end;
  4690. p += name_len;
  4691. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4692. error_buf, error_buf_size)) {
  4693. return false;
  4694. }
  4695. LOG_VERBOSE("Load custom name section success.");
  4696. }
  4697. #endif
  4698. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4699. {
  4700. WASMCustomSection *section =
  4701. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4702. if (!section) {
  4703. return false;
  4704. }
  4705. section->name_addr = (char *)p;
  4706. section->name_len = name_len;
  4707. section->content_addr = (uint8 *)(p + name_len);
  4708. section->content_len = (uint32)(p_end - p - name_len);
  4709. section->next = module->custom_section_list;
  4710. module->custom_section_list = section;
  4711. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4712. return true;
  4713. }
  4714. #endif
  4715. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4716. (void)is_load_from_file_buf;
  4717. (void)module;
  4718. return true;
  4719. fail:
  4720. return false;
  4721. }
  4722. static void
  4723. calculate_global_data_offset(WASMModule *module)
  4724. {
  4725. uint32 i, data_offset;
  4726. data_offset = 0;
  4727. for (i = 0; i < module->import_global_count; i++) {
  4728. WASMGlobalImport *import_global =
  4729. &((module->import_globals + i)->u.global);
  4730. #if WASM_ENABLE_FAST_JIT != 0
  4731. import_global->data_offset = data_offset;
  4732. #endif
  4733. data_offset += wasm_value_type_size(import_global->type.val_type);
  4734. }
  4735. for (i = 0; i < module->global_count; i++) {
  4736. WASMGlobal *global = module->globals + i;
  4737. #if WASM_ENABLE_FAST_JIT != 0
  4738. global->data_offset = data_offset;
  4739. #endif
  4740. data_offset += wasm_value_type_size(global->type.val_type);
  4741. }
  4742. module->global_data_size = data_offset;
  4743. }
  4744. #if WASM_ENABLE_FAST_JIT != 0
  4745. static bool
  4746. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4747. uint32 error_buf_size)
  4748. {
  4749. #if WASM_ENABLE_LAZY_JIT != 0
  4750. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4751. #endif
  4752. uint32 i;
  4753. if (!module->function_count)
  4754. return true;
  4755. if (!(module->fast_jit_func_ptrs =
  4756. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4757. error_buf_size))) {
  4758. return false;
  4759. }
  4760. #if WASM_ENABLE_LAZY_JIT != 0
  4761. for (i = 0; i < module->function_count; i++) {
  4762. module->fast_jit_func_ptrs[i] =
  4763. jit_globals->compile_fast_jit_and_then_call;
  4764. }
  4765. #endif
  4766. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4767. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4768. set_error_buf(error_buf, error_buf_size,
  4769. "init fast jit thread lock failed");
  4770. return false;
  4771. }
  4772. module->fast_jit_thread_locks_inited[i] = true;
  4773. }
  4774. return true;
  4775. }
  4776. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4777. #if WASM_ENABLE_JIT != 0
  4778. static bool
  4779. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4780. uint32 error_buf_size)
  4781. {
  4782. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4783. AOTCompOption option = { 0 };
  4784. char *aot_last_error;
  4785. uint64 size;
  4786. #if WASM_ENABLE_GC != 0
  4787. bool gc_enabled = true;
  4788. #else
  4789. bool gc_enabled = false;
  4790. #endif
  4791. if (module->function_count == 0)
  4792. return true;
  4793. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4794. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4795. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4796. return false;
  4797. }
  4798. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4799. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4800. os_mutex_destroy(&module->tierup_wait_lock);
  4801. return false;
  4802. }
  4803. module->tierup_wait_lock_inited = true;
  4804. #endif
  4805. size = sizeof(void *) * (uint64)module->function_count
  4806. + sizeof(bool) * (uint64)module->function_count;
  4807. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4808. return false;
  4809. }
  4810. module->func_ptrs_compiled =
  4811. (bool *)((uint8 *)module->func_ptrs
  4812. + sizeof(void *) * module->function_count);
  4813. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4814. if (!module->comp_data) {
  4815. aot_last_error = aot_get_last_error();
  4816. bh_assert(aot_last_error != NULL);
  4817. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4818. return false;
  4819. }
  4820. option.is_jit_mode = true;
  4821. option.opt_level = llvm_jit_options->opt_level;
  4822. option.size_level = llvm_jit_options->size_level;
  4823. option.segue_flags = llvm_jit_options->segue_flags;
  4824. option.quick_invoke_c_api_import =
  4825. llvm_jit_options->quick_invoke_c_api_import;
  4826. #if WASM_ENABLE_BULK_MEMORY != 0
  4827. option.enable_bulk_memory = true;
  4828. #endif
  4829. #if WASM_ENABLE_THREAD_MGR != 0
  4830. option.enable_thread_mgr = true;
  4831. #endif
  4832. #if WASM_ENABLE_TAIL_CALL != 0
  4833. option.enable_tail_call = true;
  4834. #endif
  4835. #if WASM_ENABLE_SIMD != 0
  4836. option.enable_simd = true;
  4837. #endif
  4838. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4839. option.enable_ref_types = true;
  4840. #elif WASM_ENABLE_GC != 0
  4841. option.enable_gc = true;
  4842. #endif
  4843. option.enable_aux_stack_check = true;
  4844. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4845. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4846. option.enable_aux_stack_frame = true;
  4847. #endif
  4848. #if WASM_ENABLE_PERF_PROFILING != 0
  4849. option.enable_perf_profiling = true;
  4850. #endif
  4851. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4852. option.enable_memory_profiling = true;
  4853. option.enable_stack_estimation = true;
  4854. #endif
  4855. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4856. if (!module->comp_ctx) {
  4857. aot_last_error = aot_get_last_error();
  4858. bh_assert(aot_last_error != NULL);
  4859. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4860. return false;
  4861. }
  4862. return true;
  4863. }
  4864. static bool
  4865. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4866. uint32 error_buf_size)
  4867. {
  4868. char *aot_last_error;
  4869. uint32 i;
  4870. if (module->function_count == 0)
  4871. return true;
  4872. if (!aot_compile_wasm(module->comp_ctx)) {
  4873. aot_last_error = aot_get_last_error();
  4874. bh_assert(aot_last_error != NULL);
  4875. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4876. return false;
  4877. }
  4878. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4879. if (module->orcjit_stop_compiling)
  4880. return false;
  4881. #endif
  4882. bh_print_time("Begin to lookup llvm jit functions");
  4883. for (i = 0; i < module->function_count; i++) {
  4884. LLVMOrcJITTargetAddress func_addr = 0;
  4885. LLVMErrorRef error;
  4886. char func_name[48];
  4887. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4888. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4889. func_name);
  4890. if (error != LLVMErrorSuccess) {
  4891. char *err_msg = LLVMGetErrorMessage(error);
  4892. set_error_buf_v(error_buf, error_buf_size,
  4893. "failed to compile llvm jit function: %s", err_msg);
  4894. LLVMDisposeErrorMessage(err_msg);
  4895. return false;
  4896. }
  4897. /**
  4898. * No need to lock the func_ptr[func_idx] here as it is basic
  4899. * data type, the load/store for it can be finished by one cpu
  4900. * instruction, and there can be only one cpu instruction
  4901. * loading/storing at the same time.
  4902. */
  4903. module->func_ptrs[i] = (void *)func_addr;
  4904. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4905. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4906. if (module->orcjit_stop_compiling)
  4907. return false;
  4908. #endif
  4909. }
  4910. bh_print_time("End lookup llvm jit functions");
  4911. return true;
  4912. }
  4913. #endif /* end of WASM_ENABLE_JIT != 0 */
  4914. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4915. && WASM_ENABLE_LAZY_JIT != 0
  4916. static void *
  4917. init_llvm_jit_functions_stage2_callback(void *arg)
  4918. {
  4919. WASMModule *module = (WASMModule *)arg;
  4920. char error_buf[128];
  4921. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4922. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4923. module->orcjit_stop_compiling = true;
  4924. return NULL;
  4925. }
  4926. os_mutex_lock(&module->tierup_wait_lock);
  4927. module->llvm_jit_inited = true;
  4928. os_cond_broadcast(&module->tierup_wait_cond);
  4929. os_mutex_unlock(&module->tierup_wait_lock);
  4930. return NULL;
  4931. }
  4932. #endif
  4933. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4934. /* The callback function to compile jit functions */
  4935. static void *
  4936. orcjit_thread_callback(void *arg)
  4937. {
  4938. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4939. #if WASM_ENABLE_JIT != 0
  4940. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4941. #endif
  4942. WASMModule *module = thread_arg->module;
  4943. uint32 group_idx = thread_arg->group_idx;
  4944. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4945. uint32 func_count = module->function_count;
  4946. uint32 i;
  4947. #if WASM_ENABLE_FAST_JIT != 0
  4948. /* Compile fast jit functions of this group */
  4949. for (i = group_idx; i < func_count; i += group_stride) {
  4950. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4951. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4952. break;
  4953. }
  4954. if (module->orcjit_stop_compiling) {
  4955. return NULL;
  4956. }
  4957. }
  4958. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4959. os_mutex_lock(&module->tierup_wait_lock);
  4960. module->fast_jit_ready_groups++;
  4961. os_mutex_unlock(&module->tierup_wait_lock);
  4962. #endif
  4963. #endif
  4964. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4965. && WASM_ENABLE_LAZY_JIT != 0
  4966. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4967. for (i = group_idx; i < func_count;
  4968. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4969. uint32 j;
  4970. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4971. if (i + j * group_stride < func_count) {
  4972. if (!jit_compiler_set_call_to_fast_jit(
  4973. module,
  4974. i + j * group_stride + module->import_function_count)) {
  4975. LOG_ERROR(
  4976. "failed to compile call_to_fast_jit for func %u\n",
  4977. i + j * group_stride + module->import_function_count);
  4978. module->orcjit_stop_compiling = true;
  4979. return NULL;
  4980. }
  4981. }
  4982. if (module->orcjit_stop_compiling) {
  4983. return NULL;
  4984. }
  4985. }
  4986. }
  4987. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4988. fast jit functions are compiled */
  4989. os_mutex_lock(&module->tierup_wait_lock);
  4990. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4991. && module->fast_jit_ready_groups >= group_stride)) {
  4992. os_cond_reltimedwait(&module->tierup_wait_cond,
  4993. &module->tierup_wait_lock, 10000);
  4994. if (module->orcjit_stop_compiling) {
  4995. /* init_llvm_jit_functions_stage2 failed */
  4996. os_mutex_unlock(&module->tierup_wait_lock);
  4997. return NULL;
  4998. }
  4999. }
  5000. os_mutex_unlock(&module->tierup_wait_lock);
  5001. #endif
  5002. #if WASM_ENABLE_JIT != 0
  5003. /* Compile llvm jit functions of this group */
  5004. for (i = group_idx; i < func_count;
  5005. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5006. LLVMOrcJITTargetAddress func_addr = 0;
  5007. LLVMErrorRef error;
  5008. char func_name[48];
  5009. typedef void (*F)(void);
  5010. union {
  5011. F f;
  5012. void *v;
  5013. } u;
  5014. uint32 j;
  5015. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5016. "_wrapper");
  5017. LOG_DEBUG("compile llvm jit func %s", func_name);
  5018. error =
  5019. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5020. if (error != LLVMErrorSuccess) {
  5021. char *err_msg = LLVMGetErrorMessage(error);
  5022. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5023. LLVMDisposeErrorMessage(err_msg);
  5024. break;
  5025. }
  5026. /* Call the jit wrapper function to trigger its compilation, so as
  5027. to compile the actual jit functions, since we add the latter to
  5028. function list in the PartitionFunction callback */
  5029. u.v = (void *)func_addr;
  5030. u.f();
  5031. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5032. if (i + j * group_stride < func_count) {
  5033. module->func_ptrs_compiled[i + j * group_stride] = true;
  5034. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5035. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5036. i + j * group_stride);
  5037. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5038. func_name);
  5039. if (error != LLVMErrorSuccess) {
  5040. char *err_msg = LLVMGetErrorMessage(error);
  5041. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5042. err_msg);
  5043. LLVMDisposeErrorMessage(err_msg);
  5044. /* Ignore current llvm jit func, as its func ptr is
  5045. previous set to call_to_fast_jit, which also works */
  5046. continue;
  5047. }
  5048. jit_compiler_set_llvm_jit_func_ptr(
  5049. module,
  5050. i + j * group_stride + module->import_function_count,
  5051. (void *)func_addr);
  5052. /* Try to switch to call this llvm jit function instead of
  5053. fast jit function from fast jit jitted code */
  5054. jit_compiler_set_call_to_llvm_jit(
  5055. module,
  5056. i + j * group_stride + module->import_function_count);
  5057. #endif
  5058. }
  5059. }
  5060. if (module->orcjit_stop_compiling) {
  5061. break;
  5062. }
  5063. }
  5064. #endif
  5065. return NULL;
  5066. }
  5067. static void
  5068. orcjit_stop_compile_threads(WASMModule *module)
  5069. {
  5070. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5071. / sizeof(OrcJitThreadArg));
  5072. module->orcjit_stop_compiling = true;
  5073. for (i = 0; i < thread_num; i++) {
  5074. if (module->orcjit_threads[i])
  5075. os_thread_join(module->orcjit_threads[i], NULL);
  5076. }
  5077. }
  5078. static bool
  5079. compile_jit_functions(WASMModule *module, char *error_buf,
  5080. uint32 error_buf_size)
  5081. {
  5082. uint32 thread_num =
  5083. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5084. uint32 i, j;
  5085. bh_print_time("Begin to compile jit functions");
  5086. /* Create threads to compile the jit functions */
  5087. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5088. #if WASM_ENABLE_JIT != 0
  5089. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5090. #endif
  5091. module->orcjit_thread_args[i].module = module;
  5092. module->orcjit_thread_args[i].group_idx = i;
  5093. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5094. (void *)&module->orcjit_thread_args[i],
  5095. APP_THREAD_STACK_SIZE_DEFAULT)
  5096. != 0) {
  5097. set_error_buf(error_buf, error_buf_size,
  5098. "create orcjit compile thread failed");
  5099. /* Terminate the threads created */
  5100. module->orcjit_stop_compiling = true;
  5101. for (j = 0; j < i; j++) {
  5102. os_thread_join(module->orcjit_threads[j], NULL);
  5103. }
  5104. return false;
  5105. }
  5106. }
  5107. #if WASM_ENABLE_LAZY_JIT == 0
  5108. /* Wait until all jit functions are compiled for eager mode */
  5109. for (i = 0; i < thread_num; i++) {
  5110. if (module->orcjit_threads[i])
  5111. os_thread_join(module->orcjit_threads[i], NULL);
  5112. }
  5113. #if WASM_ENABLE_FAST_JIT != 0
  5114. /* Ensure all the fast-jit functions are compiled */
  5115. for (i = 0; i < module->function_count; i++) {
  5116. if (!jit_compiler_is_compiled(module,
  5117. i + module->import_function_count)) {
  5118. set_error_buf(error_buf, error_buf_size,
  5119. "failed to compile fast jit function");
  5120. return false;
  5121. }
  5122. }
  5123. #endif
  5124. #if WASM_ENABLE_JIT != 0
  5125. /* Ensure all the llvm-jit functions are compiled */
  5126. for (i = 0; i < module->function_count; i++) {
  5127. if (!module->func_ptrs_compiled[i]) {
  5128. set_error_buf(error_buf, error_buf_size,
  5129. "failed to compile llvm jit function");
  5130. return false;
  5131. }
  5132. }
  5133. #endif
  5134. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5135. bh_print_time("End compile jit functions");
  5136. return true;
  5137. }
  5138. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5139. static bool
  5140. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5141. uint32 cur_func_idx, char *error_buf,
  5142. uint32 error_buf_size);
  5143. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5144. void **
  5145. wasm_interp_get_handle_table();
  5146. static void **handle_table;
  5147. #endif
  5148. static bool
  5149. load_from_sections(WASMModule *module, WASMSection *sections,
  5150. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5151. char *error_buf, uint32 error_buf_size)
  5152. {
  5153. WASMExport *export;
  5154. WASMSection *section = sections;
  5155. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5156. *buf_func = NULL, *buf_func_end = NULL;
  5157. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5158. WASMGlobal *aux_stack_top_global = NULL, *global;
  5159. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5160. aux_stack_top = (uint64)-1LL;
  5161. uint32 global_index, func_index, i;
  5162. uint32 aux_data_end_global_index = (uint32)-1;
  5163. uint32 aux_heap_base_global_index = (uint32)-1;
  5164. WASMFuncType *func_type;
  5165. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5166. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5167. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5168. #if WASM_ENABLE_BULK_MEMORY != 0
  5169. bool has_datacount_section = false;
  5170. #endif
  5171. /* Find code and function sections if have */
  5172. while (section) {
  5173. if (section->section_type == SECTION_TYPE_CODE) {
  5174. buf_code = section->section_body;
  5175. buf_code_end = buf_code + section->section_body_size;
  5176. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5177. module->buf_code = (uint8 *)buf_code;
  5178. module->buf_code_size = section->section_body_size;
  5179. #endif
  5180. }
  5181. else if (section->section_type == SECTION_TYPE_FUNC) {
  5182. buf_func = section->section_body;
  5183. buf_func_end = buf_func + section->section_body_size;
  5184. }
  5185. section = section->next;
  5186. }
  5187. section = sections;
  5188. while (section) {
  5189. buf = section->section_body;
  5190. buf_end = buf + section->section_body_size;
  5191. switch (section->section_type) {
  5192. case SECTION_TYPE_USER:
  5193. /* unsupported user section, ignore it. */
  5194. if (!load_user_section(buf, buf_end, module,
  5195. reuse_const_strings, error_buf,
  5196. error_buf_size))
  5197. return false;
  5198. break;
  5199. case SECTION_TYPE_TYPE:
  5200. if (!load_type_section(buf, buf_end, module, error_buf,
  5201. error_buf_size))
  5202. return false;
  5203. break;
  5204. case SECTION_TYPE_IMPORT:
  5205. if (!load_import_section(buf, buf_end, module,
  5206. reuse_const_strings, error_buf,
  5207. error_buf_size))
  5208. return false;
  5209. break;
  5210. case SECTION_TYPE_FUNC:
  5211. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5212. module, error_buf, error_buf_size))
  5213. return false;
  5214. break;
  5215. case SECTION_TYPE_TABLE:
  5216. if (!load_table_section(buf, buf_end, module, error_buf,
  5217. error_buf_size))
  5218. return false;
  5219. break;
  5220. case SECTION_TYPE_MEMORY:
  5221. if (!load_memory_section(buf, buf_end, module, error_buf,
  5222. error_buf_size))
  5223. return false;
  5224. break;
  5225. #if WASM_ENABLE_TAGS != 0
  5226. case SECTION_TYPE_TAG:
  5227. /* load tag declaration section */
  5228. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5229. module, error_buf, error_buf_size))
  5230. return false;
  5231. break;
  5232. #endif
  5233. case SECTION_TYPE_GLOBAL:
  5234. if (!load_global_section(buf, buf_end, module, error_buf,
  5235. error_buf_size))
  5236. return false;
  5237. break;
  5238. case SECTION_TYPE_EXPORT:
  5239. if (!load_export_section(buf, buf_end, module,
  5240. reuse_const_strings, error_buf,
  5241. error_buf_size))
  5242. return false;
  5243. break;
  5244. case SECTION_TYPE_START:
  5245. if (!load_start_section(buf, buf_end, module, error_buf,
  5246. error_buf_size))
  5247. return false;
  5248. break;
  5249. case SECTION_TYPE_ELEM:
  5250. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5251. error_buf_size))
  5252. return false;
  5253. break;
  5254. case SECTION_TYPE_CODE:
  5255. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5256. module, error_buf, error_buf_size))
  5257. return false;
  5258. break;
  5259. case SECTION_TYPE_DATA:
  5260. if (!load_data_segment_section(buf, buf_end, module,
  5261. #if WASM_ENABLE_BULK_MEMORY != 0
  5262. has_datacount_section,
  5263. #endif
  5264. clone_data_seg, error_buf,
  5265. error_buf_size))
  5266. return false;
  5267. break;
  5268. #if WASM_ENABLE_BULK_MEMORY != 0
  5269. case SECTION_TYPE_DATACOUNT:
  5270. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5271. error_buf_size))
  5272. return false;
  5273. has_datacount_section = true;
  5274. break;
  5275. #endif
  5276. #if WASM_ENABLE_STRINGREF != 0
  5277. case SECTION_TYPE_STRINGREF:
  5278. if (!load_stringref_section(buf, buf_end, module,
  5279. reuse_const_strings, error_buf,
  5280. error_buf_size))
  5281. return false;
  5282. break;
  5283. #endif
  5284. default:
  5285. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5286. return false;
  5287. }
  5288. section = section->next;
  5289. }
  5290. module->aux_data_end_global_index = (uint32)-1;
  5291. module->aux_heap_base_global_index = (uint32)-1;
  5292. module->aux_stack_top_global_index = (uint32)-1;
  5293. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5294. export = module->exports;
  5295. for (i = 0; i < module->export_count; i++, export ++) {
  5296. if (export->kind == EXPORT_KIND_GLOBAL) {
  5297. if (!strcmp(export->name, "__heap_base")) {
  5298. global_index = export->index - module->import_global_count;
  5299. global = module->globals + global_index;
  5300. if (global->type.val_type == VALUE_TYPE_I32
  5301. && !global->type.is_mutable
  5302. && global->init_expr.init_expr_type
  5303. == INIT_EXPR_TYPE_I32_CONST) {
  5304. aux_heap_base_global = global;
  5305. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5306. aux_heap_base_global_index = export->index;
  5307. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5308. aux_heap_base);
  5309. }
  5310. }
  5311. else if (!strcmp(export->name, "__data_end")) {
  5312. global_index = export->index - module->import_global_count;
  5313. global = module->globals + global_index;
  5314. if (global->type.val_type == VALUE_TYPE_I32
  5315. && !global->type.is_mutable
  5316. && global->init_expr.init_expr_type
  5317. == INIT_EXPR_TYPE_I32_CONST) {
  5318. aux_data_end_global = global;
  5319. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5320. aux_data_end_global_index = export->index;
  5321. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5322. aux_data_end);
  5323. aux_data_end = align_uint64(aux_data_end, 16);
  5324. }
  5325. }
  5326. /* For module compiled with -pthread option, the global is:
  5327. [0] stack_top <-- 0
  5328. [1] tls_pointer
  5329. [2] tls_size
  5330. [3] data_end <-- 3
  5331. [4] global_base
  5332. [5] heap_base <-- 5
  5333. [6] dso_handle
  5334. For module compiled without -pthread option:
  5335. [0] stack_top <-- 0
  5336. [1] data_end <-- 1
  5337. [2] global_base
  5338. [3] heap_base <-- 3
  5339. [4] dso_handle
  5340. */
  5341. if (aux_data_end_global && aux_heap_base_global
  5342. && aux_data_end <= aux_heap_base) {
  5343. module->aux_data_end_global_index = aux_data_end_global_index;
  5344. module->aux_data_end = aux_data_end;
  5345. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5346. module->aux_heap_base = aux_heap_base;
  5347. /* Resolve aux stack top global */
  5348. for (global_index = 0; global_index < module->global_count;
  5349. global_index++) {
  5350. global = module->globals + global_index;
  5351. if (global->type.is_mutable /* heap_base and data_end is
  5352. not mutable */
  5353. && global->type.val_type == VALUE_TYPE_I32
  5354. && global->init_expr.init_expr_type
  5355. == INIT_EXPR_TYPE_I32_CONST
  5356. && (uint64)(uint32)global->init_expr.u.i32
  5357. <= aux_heap_base) {
  5358. aux_stack_top_global = global;
  5359. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5360. module->aux_stack_top_global_index =
  5361. module->import_global_count + global_index;
  5362. module->aux_stack_bottom = aux_stack_top;
  5363. module->aux_stack_size =
  5364. aux_stack_top > aux_data_end
  5365. ? (uint32)(aux_stack_top - aux_data_end)
  5366. : (uint32)aux_stack_top;
  5367. LOG_VERBOSE(
  5368. "Found aux stack top global, value: %" PRIu64 ", "
  5369. "global index: %d, stack size: %d",
  5370. aux_stack_top, global_index,
  5371. module->aux_stack_size);
  5372. break;
  5373. }
  5374. }
  5375. if (!aux_stack_top_global) {
  5376. /* Auxiliary stack global isn't found, it must be unused
  5377. in the wasm app, as if it is used, the global must be
  5378. defined. Here we set it to __heap_base global and set
  5379. its size to 0. */
  5380. aux_stack_top_global = aux_heap_base_global;
  5381. aux_stack_top = aux_heap_base;
  5382. module->aux_stack_top_global_index =
  5383. module->aux_heap_base_global_index;
  5384. module->aux_stack_bottom = aux_stack_top;
  5385. module->aux_stack_size = 0;
  5386. }
  5387. break;
  5388. }
  5389. }
  5390. }
  5391. module->malloc_function = (uint32)-1;
  5392. module->free_function = (uint32)-1;
  5393. module->retain_function = (uint32)-1;
  5394. /* Resolve malloc/free function exported by wasm module */
  5395. #if WASM_ENABLE_MEMORY64 != 0
  5396. if (has_module_memory64(module))
  5397. malloc_free_io_type = VALUE_TYPE_I64;
  5398. #endif
  5399. export = module->exports;
  5400. for (i = 0; i < module->export_count; i++, export ++) {
  5401. if (export->kind == EXPORT_KIND_FUNC) {
  5402. if (!strcmp(export->name, "malloc")
  5403. && export->index >= module->import_function_count) {
  5404. func_index = export->index - module->import_function_count;
  5405. func_type = module->functions[func_index]->func_type;
  5406. if (func_type->param_count == 1 && func_type->result_count == 1
  5407. && func_type->types[0] == malloc_free_io_type
  5408. && func_type->types[1] == malloc_free_io_type) {
  5409. bh_assert(module->malloc_function == (uint32)-1);
  5410. module->malloc_function = export->index;
  5411. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5412. export->name, export->index);
  5413. }
  5414. }
  5415. else if (!strcmp(export->name, "__new")
  5416. && export->index >= module->import_function_count) {
  5417. /* __new && __pin for AssemblyScript */
  5418. func_index = export->index - module->import_function_count;
  5419. func_type = module->functions[func_index]->func_type;
  5420. if (func_type->param_count == 2 && func_type->result_count == 1
  5421. && func_type->types[0] == malloc_free_io_type
  5422. && func_type->types[1] == VALUE_TYPE_I32
  5423. && func_type->types[2] == malloc_free_io_type) {
  5424. uint32 j;
  5425. WASMExport *export_tmp;
  5426. bh_assert(module->malloc_function == (uint32)-1);
  5427. module->malloc_function = export->index;
  5428. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5429. export->name, export->index);
  5430. /* resolve retain function.
  5431. If not found, reset malloc function index */
  5432. export_tmp = module->exports;
  5433. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5434. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5435. && (!strcmp(export_tmp->name, "__retain")
  5436. || (!strcmp(export_tmp->name, "__pin")))
  5437. && (export_tmp->index
  5438. >= module->import_function_count)) {
  5439. func_index = export_tmp->index
  5440. - module->import_function_count;
  5441. func_type =
  5442. module->functions[func_index]->func_type;
  5443. if (func_type->param_count == 1
  5444. && func_type->result_count == 1
  5445. && func_type->types[0] == malloc_free_io_type
  5446. && func_type->types[1] == malloc_free_io_type) {
  5447. bh_assert(module->retain_function
  5448. == (uint32)-1);
  5449. module->retain_function = export_tmp->index;
  5450. LOG_VERBOSE("Found retain function, name: %s, "
  5451. "index: %u",
  5452. export_tmp->name,
  5453. export_tmp->index);
  5454. break;
  5455. }
  5456. }
  5457. }
  5458. if (j == module->export_count) {
  5459. module->malloc_function = (uint32)-1;
  5460. LOG_VERBOSE("Can't find retain function,"
  5461. "reset malloc function index to -1");
  5462. }
  5463. }
  5464. }
  5465. else if (((!strcmp(export->name, "free"))
  5466. || (!strcmp(export->name, "__release"))
  5467. || (!strcmp(export->name, "__unpin")))
  5468. && export->index >= module->import_function_count) {
  5469. func_index = export->index - module->import_function_count;
  5470. func_type = module->functions[func_index]->func_type;
  5471. if (func_type->param_count == 1 && func_type->result_count == 0
  5472. && func_type->types[0] == malloc_free_io_type) {
  5473. bh_assert(module->free_function == (uint32)-1);
  5474. module->free_function = export->index;
  5475. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5476. export->name, export->index);
  5477. }
  5478. }
  5479. }
  5480. }
  5481. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5482. handle_table = wasm_interp_get_handle_table();
  5483. #endif
  5484. for (i = 0; i < module->function_count; i++) {
  5485. WASMFunction *func = module->functions[i];
  5486. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5487. error_buf_size)) {
  5488. return false;
  5489. }
  5490. if (i == module->function_count - 1
  5491. && func->code + func->code_size != buf_code_end) {
  5492. set_error_buf(error_buf, error_buf_size,
  5493. "code section size mismatch");
  5494. return false;
  5495. }
  5496. }
  5497. if (!module->possible_memory_grow) {
  5498. WASMMemoryImport *memory_import;
  5499. WASMMemory *memory;
  5500. if (aux_data_end_global && aux_heap_base_global
  5501. && aux_stack_top_global) {
  5502. uint64 init_memory_size;
  5503. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5504. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5505. * valid range of uint32 */
  5506. if (shrunk_memory_size <= UINT32_MAX) {
  5507. if (module->import_memory_count) {
  5508. memory_import = &module->import_memories[0].u.memory;
  5509. init_memory_size =
  5510. (uint64)memory_import->mem_type.num_bytes_per_page
  5511. * memory_import->mem_type.init_page_count;
  5512. if (shrunk_memory_size <= init_memory_size) {
  5513. /* Reset memory info to decrease memory usage */
  5514. memory_import->mem_type.num_bytes_per_page =
  5515. (uint32)shrunk_memory_size;
  5516. memory_import->mem_type.init_page_count = 1;
  5517. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5518. shrunk_memory_size);
  5519. }
  5520. }
  5521. if (module->memory_count) {
  5522. memory = &module->memories[0];
  5523. init_memory_size = (uint64)memory->num_bytes_per_page
  5524. * memory->init_page_count;
  5525. if (shrunk_memory_size <= init_memory_size) {
  5526. /* Reset memory info to decrease memory usage */
  5527. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5528. memory->init_page_count = 1;
  5529. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5530. shrunk_memory_size);
  5531. }
  5532. }
  5533. }
  5534. }
  5535. #if WASM_ENABLE_MULTI_MODULE == 0
  5536. if (module->import_memory_count) {
  5537. memory_import = &module->import_memories[0].u.memory;
  5538. /* Only resize the memory to one big page if num_bytes_per_page is
  5539. * in valid range of uint32 */
  5540. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5541. memory_import->mem_type.num_bytes_per_page *=
  5542. memory_import->mem_type.init_page_count;
  5543. if (memory_import->mem_type.init_page_count > 0)
  5544. memory_import->mem_type.init_page_count =
  5545. memory_import->mem_type.max_page_count = 1;
  5546. else
  5547. memory_import->mem_type.init_page_count =
  5548. memory_import->mem_type.max_page_count = 0;
  5549. }
  5550. }
  5551. if (module->memory_count) {
  5552. memory = &module->memories[0];
  5553. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5554. * valid range of uint32 */
  5555. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5556. memory->num_bytes_per_page *= memory->init_page_count;
  5557. if (memory->init_page_count > 0)
  5558. memory->init_page_count = memory->max_page_count = 1;
  5559. else
  5560. memory->init_page_count = memory->max_page_count = 0;
  5561. }
  5562. }
  5563. #endif
  5564. }
  5565. calculate_global_data_offset(module);
  5566. #if WASM_ENABLE_FAST_JIT != 0
  5567. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5568. return false;
  5569. }
  5570. #endif
  5571. #if WASM_ENABLE_JIT != 0
  5572. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5573. return false;
  5574. }
  5575. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5576. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5577. return false;
  5578. }
  5579. #else
  5580. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5581. thread fast jit execution, since applying llvm optimizations in
  5582. aot_compile_wasm may cost a lot of time.
  5583. Create thread with enough native stack to apply llvm optimizations */
  5584. if (os_thread_create(&module->llvm_jit_init_thread,
  5585. init_llvm_jit_functions_stage2_callback,
  5586. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5587. != 0) {
  5588. set_error_buf(error_buf, error_buf_size,
  5589. "create orcjit compile thread failed");
  5590. return false;
  5591. }
  5592. #endif
  5593. #endif
  5594. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5595. /* Create threads to compile the jit functions */
  5596. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5597. return false;
  5598. }
  5599. #endif
  5600. #if WASM_ENABLE_MEMORY_TRACING != 0
  5601. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5602. #endif
  5603. return true;
  5604. }
  5605. static WASMModule *
  5606. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5607. {
  5608. WASMModule *module =
  5609. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5610. bh_list_status ret;
  5611. if (!module) {
  5612. return NULL;
  5613. }
  5614. module->module_type = Wasm_Module_Bytecode;
  5615. /* Set start_function to -1, means no start function */
  5616. module->start_function = (uint32)-1;
  5617. module->name = name;
  5618. module->is_binary_freeable = false;
  5619. #if WASM_ENABLE_FAST_INTERP == 0
  5620. module->br_table_cache_list = &module->br_table_cache_list_head;
  5621. ret = bh_list_init(module->br_table_cache_list);
  5622. bh_assert(ret == BH_LIST_SUCCESS);
  5623. #endif
  5624. #if WASM_ENABLE_MULTI_MODULE != 0
  5625. module->import_module_list = &module->import_module_list_head;
  5626. ret = bh_list_init(module->import_module_list);
  5627. bh_assert(ret == BH_LIST_SUCCESS);
  5628. #endif
  5629. #if WASM_ENABLE_DEBUG_INTERP != 0
  5630. ret = bh_list_init(&module->fast_opcode_list);
  5631. bh_assert(ret == BH_LIST_SUCCESS);
  5632. #endif
  5633. #if WASM_ENABLE_GC != 0
  5634. if (!(module->ref_type_set =
  5635. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5636. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5637. goto fail1;
  5638. }
  5639. if (os_mutex_init(&module->rtt_type_lock)) {
  5640. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5641. goto fail2;
  5642. }
  5643. #endif
  5644. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5645. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5646. && WASM_ENABLE_LAZY_JIT != 0)
  5647. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5648. set_error_buf(error_buf, error_buf_size,
  5649. "init instance list lock failed");
  5650. goto fail3;
  5651. }
  5652. #endif
  5653. (void)ret;
  5654. return module;
  5655. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5656. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5657. && WASM_ENABLE_LAZY_JIT != 0)
  5658. fail3:
  5659. #endif
  5660. #if WASM_ENABLE_GC != 0
  5661. os_mutex_destroy(&module->rtt_type_lock);
  5662. fail2:
  5663. bh_hash_map_destroy(module->ref_type_set);
  5664. fail1:
  5665. #endif
  5666. wasm_runtime_free(module);
  5667. return NULL;
  5668. }
  5669. #if WASM_ENABLE_DEBUG_INTERP != 0
  5670. static bool
  5671. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5672. uint32 error_buf_size)
  5673. {
  5674. WASMFastOPCodeNode *fast_op =
  5675. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5676. if (fast_op) {
  5677. fast_op->offset = pos - module->load_addr;
  5678. fast_op->orig_op = orig_op;
  5679. bh_list_insert(&module->fast_opcode_list, fast_op);
  5680. }
  5681. return fast_op ? true : false;
  5682. }
  5683. #endif
  5684. WASMModule *
  5685. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5686. uint32 error_buf_size)
  5687. {
  5688. WASMModule *module = create_module("", error_buf, error_buf_size);
  5689. if (!module)
  5690. return NULL;
  5691. if (!load_from_sections(module, section_list, false, true, error_buf,
  5692. error_buf_size)) {
  5693. wasm_loader_unload(module);
  5694. return NULL;
  5695. }
  5696. LOG_VERBOSE("Load module from sections success.\n");
  5697. return module;
  5698. }
  5699. static void
  5700. destroy_sections(WASMSection *section_list)
  5701. {
  5702. WASMSection *section = section_list, *next;
  5703. while (section) {
  5704. next = section->next;
  5705. wasm_runtime_free(section);
  5706. section = next;
  5707. }
  5708. }
  5709. /* clang-format off */
  5710. static uint8 section_ids[] = {
  5711. SECTION_TYPE_USER,
  5712. SECTION_TYPE_TYPE,
  5713. SECTION_TYPE_IMPORT,
  5714. SECTION_TYPE_FUNC,
  5715. SECTION_TYPE_TABLE,
  5716. SECTION_TYPE_MEMORY,
  5717. #if WASM_ENABLE_TAGS != 0
  5718. SECTION_TYPE_TAG,
  5719. #endif
  5720. #if WASM_ENABLE_STRINGREF != 0
  5721. /* must immediately precede the global section,
  5722. or where the global section would be */
  5723. SECTION_TYPE_STRINGREF,
  5724. #endif
  5725. SECTION_TYPE_GLOBAL,
  5726. SECTION_TYPE_EXPORT,
  5727. SECTION_TYPE_START,
  5728. SECTION_TYPE_ELEM,
  5729. #if WASM_ENABLE_BULK_MEMORY != 0
  5730. SECTION_TYPE_DATACOUNT,
  5731. #endif
  5732. SECTION_TYPE_CODE,
  5733. SECTION_TYPE_DATA
  5734. };
  5735. /* clang-format on */
  5736. static uint8
  5737. get_section_index(uint8 section_type)
  5738. {
  5739. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5740. for (uint8 i = 0; i < max_id; i++) {
  5741. if (section_type == section_ids[i])
  5742. return i;
  5743. }
  5744. return (uint8)-1;
  5745. }
  5746. static bool
  5747. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5748. char *error_buf, uint32 error_buf_size)
  5749. {
  5750. WASMSection *section_list_end = NULL, *section;
  5751. const uint8 *p = buf, *p_end = buf + size;
  5752. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5753. uint32 section_size;
  5754. bh_assert(!*p_section_list);
  5755. p += 8;
  5756. while (p < p_end) {
  5757. CHECK_BUF(p, p_end, 1);
  5758. section_type = read_uint8(p);
  5759. section_index = get_section_index(section_type);
  5760. if (section_index != (uint8)-1) {
  5761. if (section_type != SECTION_TYPE_USER) {
  5762. /* Custom sections may be inserted at any place,
  5763. while other sections must occur at most once
  5764. and in prescribed order. */
  5765. if (last_section_index != (uint8)-1
  5766. && (section_index <= last_section_index)) {
  5767. set_error_buf(error_buf, error_buf_size,
  5768. "unexpected content after last section or "
  5769. "junk after last section");
  5770. return false;
  5771. }
  5772. last_section_index = section_index;
  5773. }
  5774. read_leb_uint32(p, p_end, section_size);
  5775. CHECK_BUF1(p, p_end, section_size);
  5776. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5777. error_buf_size))) {
  5778. return false;
  5779. }
  5780. section->section_type = section_type;
  5781. section->section_body = (uint8 *)p;
  5782. section->section_body_size = section_size;
  5783. if (!section_list_end)
  5784. *p_section_list = section_list_end = section;
  5785. else {
  5786. section_list_end->next = section;
  5787. section_list_end = section;
  5788. }
  5789. p += section_size;
  5790. }
  5791. else {
  5792. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5793. return false;
  5794. }
  5795. }
  5796. return true;
  5797. fail:
  5798. return false;
  5799. }
  5800. static void
  5801. exchange32(uint8 *p_data)
  5802. {
  5803. uint8 value = *p_data;
  5804. *p_data = *(p_data + 3);
  5805. *(p_data + 3) = value;
  5806. value = *(p_data + 1);
  5807. *(p_data + 1) = *(p_data + 2);
  5808. *(p_data + 2) = value;
  5809. }
  5810. static union {
  5811. int a;
  5812. char b;
  5813. } __ue = { .a = 1 };
  5814. #define is_little_endian() (__ue.b == 1)
  5815. static bool
  5816. load(const uint8 *buf, uint32 size, WASMModule *module,
  5817. bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
  5818. {
  5819. const uint8 *buf_end = buf + size;
  5820. const uint8 *p = buf, *p_end = buf_end;
  5821. uint32 magic_number, version;
  5822. WASMSection *section_list = NULL;
  5823. CHECK_BUF1(p, p_end, sizeof(uint32));
  5824. magic_number = read_uint32(p);
  5825. if (!is_little_endian())
  5826. exchange32((uint8 *)&magic_number);
  5827. if (magic_number != WASM_MAGIC_NUMBER) {
  5828. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5829. return false;
  5830. }
  5831. CHECK_BUF1(p, p_end, sizeof(uint32));
  5832. version = read_uint32(p);
  5833. if (!is_little_endian())
  5834. exchange32((uint8 *)&version);
  5835. if (version != WASM_CURRENT_VERSION) {
  5836. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5837. return false;
  5838. }
  5839. module->package_version = version;
  5840. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5841. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5842. error_buf, error_buf_size)) {
  5843. destroy_sections(section_list);
  5844. return false;
  5845. }
  5846. destroy_sections(section_list);
  5847. return true;
  5848. fail:
  5849. return false;
  5850. }
  5851. #if WASM_ENABLE_LIBC_WASI != 0
  5852. /**
  5853. * refer to
  5854. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5855. */
  5856. static bool
  5857. check_wasi_abi_compatibility(const WASMModule *module,
  5858. #if WASM_ENABLE_MULTI_MODULE != 0
  5859. bool main_module,
  5860. #endif
  5861. char *error_buf, uint32 error_buf_size)
  5862. {
  5863. /**
  5864. * be careful with:
  5865. * wasi compatible modules(command/reactor) which don't import any wasi
  5866. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5867. * reactor can depend on nothing. At the same time, each has its own entry
  5868. * point.
  5869. *
  5870. * observations:
  5871. * - clang always injects `_start` into a command
  5872. * - clang always injects `_initialize` into a reactor
  5873. * - `iwasm -f` allows to run a function in the reactor
  5874. *
  5875. * strong assumptions:
  5876. * - no one will define either `_start` or `_initialize` on purpose
  5877. * - `_start` should always be `void _start(void)`
  5878. * - `_initialize` should always be `void _initialize(void)`
  5879. *
  5880. */
  5881. /* clang-format off */
  5882. /**
  5883. *
  5884. * | | import_wasi_api True | | import_wasi_api False | |
  5885. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5886. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5887. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5888. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5889. */
  5890. /* clang-format on */
  5891. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5892. uint32 import_function_count = module->import_function_count;
  5893. WASMFuncType *func_type;
  5894. /* (func (export "_start") (...) */
  5895. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5896. error_buf, error_buf_size);
  5897. if (start) {
  5898. if (start->index < import_function_count) {
  5899. set_error_buf(
  5900. error_buf, error_buf_size,
  5901. "the builtin _start function can not be an import function");
  5902. return false;
  5903. }
  5904. func_type =
  5905. module->functions[start->index - import_function_count]->func_type;
  5906. if (func_type->param_count || func_type->result_count) {
  5907. set_error_buf(error_buf, error_buf_size,
  5908. "the signature of builtin _start function is wrong");
  5909. return false;
  5910. }
  5911. }
  5912. else {
  5913. /* (func (export "_initialize") (...) */
  5914. initialize =
  5915. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5916. error_buf, error_buf_size);
  5917. if (initialize) {
  5918. if (initialize->index < import_function_count) {
  5919. set_error_buf(error_buf, error_buf_size,
  5920. "the builtin _initialize function can not be an "
  5921. "import function");
  5922. return false;
  5923. }
  5924. func_type =
  5925. module->functions[initialize->index - import_function_count]
  5926. ->func_type;
  5927. if (func_type->param_count || func_type->result_count) {
  5928. set_error_buf(
  5929. error_buf, error_buf_size,
  5930. "the signature of builtin _initialize function is wrong");
  5931. return false;
  5932. }
  5933. }
  5934. }
  5935. /* filter out non-wasi compatible modules */
  5936. if (!module->import_wasi_api && !start && !initialize) {
  5937. return true;
  5938. }
  5939. /* should have one at least */
  5940. if (module->import_wasi_api && !start && !initialize) {
  5941. LOG_WARNING("warning: a module with WASI apis should be either "
  5942. "a command or a reactor");
  5943. }
  5944. /*
  5945. * there is at least one of `_start` and `_initialize` in below cases.
  5946. * according to the assumption, they should be all wasi compatible
  5947. */
  5948. #if WASM_ENABLE_MULTI_MODULE != 0
  5949. /* filter out commands (with `_start`) cases */
  5950. if (start && !main_module) {
  5951. set_error_buf(
  5952. error_buf, error_buf_size,
  5953. "a command (with _start function) can not be a sub-module");
  5954. return false;
  5955. }
  5956. #endif
  5957. /*
  5958. * it is ok a reactor acts as a main module,
  5959. * so skip the check about (with `_initialize`)
  5960. */
  5961. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5962. error_buf, error_buf_size);
  5963. if (!memory
  5964. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5965. /*
  5966. * with wasi-threads, it's still an open question if a memory
  5967. * should be exported.
  5968. *
  5969. * https://github.com/WebAssembly/wasi-threads/issues/22
  5970. * https://github.com/WebAssembly/WASI/issues/502
  5971. *
  5972. * Note: this code assumes the number of memories is at most 1.
  5973. */
  5974. && module->import_memory_count == 0
  5975. #endif
  5976. ) {
  5977. set_error_buf(error_buf, error_buf_size,
  5978. "a module with WASI apis must export memory by default");
  5979. return false;
  5980. }
  5981. return true;
  5982. }
  5983. #endif
  5984. WASMModule *
  5985. wasm_loader_load(uint8 *buf, uint32 size,
  5986. #if WASM_ENABLE_MULTI_MODULE != 0
  5987. bool main_module,
  5988. #endif
  5989. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5990. {
  5991. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5992. if (!module) {
  5993. return NULL;
  5994. }
  5995. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5996. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5997. module->load_addr = (uint8 *)buf;
  5998. module->load_size = size;
  5999. #endif
  6000. if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
  6001. error_buf_size)) {
  6002. goto fail;
  6003. }
  6004. #if WASM_ENABLE_LIBC_WASI != 0
  6005. /* Check the WASI application ABI */
  6006. if (!check_wasi_abi_compatibility(module,
  6007. #if WASM_ENABLE_MULTI_MODULE != 0
  6008. main_module,
  6009. #endif
  6010. error_buf, error_buf_size)) {
  6011. goto fail;
  6012. }
  6013. #endif
  6014. LOG_VERBOSE("Load module success.\n");
  6015. return module;
  6016. fail:
  6017. wasm_loader_unload(module);
  6018. return NULL;
  6019. }
  6020. void
  6021. wasm_loader_unload(WASMModule *module)
  6022. {
  6023. uint32 i;
  6024. if (!module)
  6025. return;
  6026. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6027. && WASM_ENABLE_LAZY_JIT != 0
  6028. module->orcjit_stop_compiling = true;
  6029. if (module->llvm_jit_init_thread)
  6030. os_thread_join(module->llvm_jit_init_thread, NULL);
  6031. #endif
  6032. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6033. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6034. module internal data after they were freed */
  6035. orcjit_stop_compile_threads(module);
  6036. #endif
  6037. #if WASM_ENABLE_JIT != 0
  6038. if (module->func_ptrs)
  6039. wasm_runtime_free(module->func_ptrs);
  6040. if (module->comp_ctx)
  6041. aot_destroy_comp_context(module->comp_ctx);
  6042. if (module->comp_data)
  6043. aot_destroy_comp_data(module->comp_data);
  6044. #endif
  6045. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6046. && WASM_ENABLE_LAZY_JIT != 0
  6047. if (module->tierup_wait_lock_inited) {
  6048. os_mutex_destroy(&module->tierup_wait_lock);
  6049. os_cond_destroy(&module->tierup_wait_cond);
  6050. }
  6051. #endif
  6052. if (module->imports)
  6053. wasm_runtime_free(module->imports);
  6054. if (module->functions) {
  6055. for (i = 0; i < module->function_count; i++) {
  6056. if (module->functions[i]) {
  6057. if (module->functions[i]->local_offsets)
  6058. wasm_runtime_free(module->functions[i]->local_offsets);
  6059. #if WASM_ENABLE_FAST_INTERP != 0
  6060. if (module->functions[i]->code_compiled)
  6061. wasm_runtime_free(module->functions[i]->code_compiled);
  6062. if (module->functions[i]->consts)
  6063. wasm_runtime_free(module->functions[i]->consts);
  6064. #endif
  6065. #if WASM_ENABLE_FAST_JIT != 0
  6066. if (module->functions[i]->fast_jit_jitted_code) {
  6067. jit_code_cache_free(
  6068. module->functions[i]->fast_jit_jitted_code);
  6069. }
  6070. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6071. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6072. jit_code_cache_free(
  6073. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6074. }
  6075. #endif
  6076. #endif
  6077. #if WASM_ENABLE_GC != 0
  6078. if (module->functions[i]->local_ref_type_maps) {
  6079. wasm_runtime_free(
  6080. module->functions[i]->local_ref_type_maps);
  6081. }
  6082. #endif
  6083. wasm_runtime_free(module->functions[i]);
  6084. }
  6085. }
  6086. wasm_runtime_free(module->functions);
  6087. }
  6088. if (module->tables) {
  6089. #if WASM_ENABLE_GC != 0
  6090. for (i = 0; i < module->table_count; i++) {
  6091. destroy_init_expr(module, &module->tables[i].init_expr);
  6092. }
  6093. #endif
  6094. wasm_runtime_free(module->tables);
  6095. }
  6096. if (module->memories)
  6097. wasm_runtime_free(module->memories);
  6098. if (module->globals) {
  6099. #if WASM_ENABLE_GC != 0
  6100. for (i = 0; i < module->global_count; i++) {
  6101. destroy_init_expr(module, &module->globals[i].init_expr);
  6102. }
  6103. #endif
  6104. wasm_runtime_free(module->globals);
  6105. }
  6106. #if WASM_ENABLE_TAGS != 0
  6107. if (module->tags) {
  6108. for (i = 0; i < module->tag_count; i++) {
  6109. if (module->tags[i])
  6110. wasm_runtime_free(module->tags[i]);
  6111. }
  6112. wasm_runtime_free(module->tags);
  6113. }
  6114. #endif
  6115. if (module->exports)
  6116. wasm_runtime_free(module->exports);
  6117. if (module->table_segments) {
  6118. for (i = 0; i < module->table_seg_count; i++) {
  6119. if (module->table_segments[i].init_values) {
  6120. #if WASM_ENABLE_GC != 0
  6121. uint32 j;
  6122. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6123. destroy_init_expr(
  6124. module, &module->table_segments[i].init_values[j]);
  6125. }
  6126. #endif
  6127. wasm_runtime_free(module->table_segments[i].init_values);
  6128. }
  6129. }
  6130. wasm_runtime_free(module->table_segments);
  6131. }
  6132. if (module->data_segments) {
  6133. for (i = 0; i < module->data_seg_count; i++) {
  6134. if (module->data_segments[i]) {
  6135. if (module->data_segments[i]->is_data_cloned)
  6136. wasm_runtime_free(module->data_segments[i]->data);
  6137. wasm_runtime_free(module->data_segments[i]);
  6138. }
  6139. }
  6140. wasm_runtime_free(module->data_segments);
  6141. }
  6142. if (module->types) {
  6143. for (i = 0; i < module->type_count; i++) {
  6144. if (module->types[i])
  6145. destroy_wasm_type(module->types[i]);
  6146. }
  6147. wasm_runtime_free(module->types);
  6148. }
  6149. if (module->const_str_list) {
  6150. StringNode *node = module->const_str_list, *node_next;
  6151. while (node) {
  6152. node_next = node->next;
  6153. wasm_runtime_free(node);
  6154. node = node_next;
  6155. }
  6156. }
  6157. #if WASM_ENABLE_STRINGREF != 0
  6158. if (module->string_literal_ptrs) {
  6159. wasm_runtime_free((void *)module->string_literal_ptrs);
  6160. }
  6161. if (module->string_literal_lengths) {
  6162. wasm_runtime_free(module->string_literal_lengths);
  6163. }
  6164. #endif
  6165. #if WASM_ENABLE_FAST_INTERP == 0
  6166. if (module->br_table_cache_list) {
  6167. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6168. BrTableCache *node_next;
  6169. while (node) {
  6170. node_next = bh_list_elem_next(node);
  6171. wasm_runtime_free(node);
  6172. node = node_next;
  6173. }
  6174. }
  6175. #endif
  6176. #if WASM_ENABLE_MULTI_MODULE != 0
  6177. /* just release the sub module list */
  6178. if (module->import_module_list) {
  6179. WASMRegisteredModule *node =
  6180. bh_list_first_elem(module->import_module_list);
  6181. while (node) {
  6182. WASMRegisteredModule *next = bh_list_elem_next(node);
  6183. bh_list_remove(module->import_module_list, node);
  6184. /*
  6185. * unload(sub_module) will be triggered during runtime_destroy().
  6186. * every module in the global module list will be unloaded one by
  6187. * one. so don't worry.
  6188. */
  6189. wasm_runtime_free(node);
  6190. /*
  6191. * the module file reading buffer will be released
  6192. * in runtime_destroy()
  6193. */
  6194. node = next;
  6195. }
  6196. }
  6197. #endif
  6198. #if WASM_ENABLE_DEBUG_INTERP != 0
  6199. WASMFastOPCodeNode *fast_opcode =
  6200. bh_list_first_elem(&module->fast_opcode_list);
  6201. while (fast_opcode) {
  6202. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6203. wasm_runtime_free(fast_opcode);
  6204. fast_opcode = next;
  6205. }
  6206. #endif
  6207. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6208. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6209. && WASM_ENABLE_LAZY_JIT != 0)
  6210. os_mutex_destroy(&module->instance_list_lock);
  6211. #endif
  6212. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6213. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6214. #endif
  6215. #if WASM_ENABLE_FAST_JIT != 0
  6216. if (module->fast_jit_func_ptrs) {
  6217. wasm_runtime_free(module->fast_jit_func_ptrs);
  6218. }
  6219. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6220. if (module->fast_jit_thread_locks_inited[i]) {
  6221. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6222. }
  6223. }
  6224. #endif
  6225. #if WASM_ENABLE_GC != 0
  6226. os_mutex_destroy(&module->rtt_type_lock);
  6227. bh_hash_map_destroy(module->ref_type_set);
  6228. if (module->rtt_types) {
  6229. for (i = 0; i < module->type_count; i++) {
  6230. if (module->rtt_types[i])
  6231. wasm_runtime_free(module->rtt_types[i]);
  6232. }
  6233. wasm_runtime_free(module->rtt_types);
  6234. }
  6235. #if WASM_ENABLE_STRINGREF != 0
  6236. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6237. if (module->stringref_rtts[i])
  6238. wasm_runtime_free(module->stringref_rtts[i]);
  6239. }
  6240. #endif
  6241. #endif
  6242. wasm_runtime_free(module);
  6243. }
  6244. bool
  6245. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6246. const uint8 *start_addr, const uint8 *code_end_addr,
  6247. uint8 label_type, uint8 **p_else_addr,
  6248. uint8 **p_end_addr)
  6249. {
  6250. const uint8 *p = start_addr, *p_end = code_end_addr;
  6251. uint8 *else_addr = NULL;
  6252. char error_buf[128];
  6253. uint32 block_nested_depth = 1, count, i, j, t;
  6254. uint32 error_buf_size = sizeof(error_buf);
  6255. uint8 opcode, u8;
  6256. BlockAddr block_stack[16] = { { 0 } }, *block;
  6257. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6258. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6259. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6260. if (block[j].start_addr == start_addr) {
  6261. /* Cache hit */
  6262. *p_else_addr = block[j].else_addr;
  6263. *p_end_addr = block[j].end_addr;
  6264. return true;
  6265. }
  6266. }
  6267. /* Cache unhit */
  6268. block_stack[0].start_addr = start_addr;
  6269. while (p < code_end_addr) {
  6270. opcode = *p++;
  6271. #if WASM_ENABLE_DEBUG_INTERP != 0
  6272. op_break_retry:
  6273. #endif
  6274. switch (opcode) {
  6275. case WASM_OP_UNREACHABLE:
  6276. case WASM_OP_NOP:
  6277. break;
  6278. #if WASM_ENABLE_EXCE_HANDLING != 0
  6279. case WASM_OP_TRY:
  6280. u8 = read_uint8(p);
  6281. if (block_nested_depth
  6282. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6283. block_stack[block_nested_depth].start_addr = p;
  6284. block_stack[block_nested_depth].else_addr = NULL;
  6285. }
  6286. block_nested_depth++;
  6287. break;
  6288. case EXT_OP_TRY:
  6289. skip_leb_uint32(p, p_end);
  6290. if (block_nested_depth
  6291. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6292. block_stack[block_nested_depth].start_addr = p;
  6293. block_stack[block_nested_depth].else_addr = NULL;
  6294. }
  6295. block_nested_depth++;
  6296. break;
  6297. case WASM_OP_CATCH:
  6298. if (block_nested_depth == 1) {
  6299. *p_end_addr = (uint8 *)(p - 1);
  6300. /* stop search and return the address of the catch block */
  6301. return true;
  6302. }
  6303. break;
  6304. case WASM_OP_CATCH_ALL:
  6305. if (block_nested_depth == 1) {
  6306. *p_end_addr = (uint8 *)(p - 1);
  6307. /* stop search and return the address of the catch_all block
  6308. */
  6309. return true;
  6310. }
  6311. break;
  6312. case WASM_OP_THROW:
  6313. /* skip tag_index */
  6314. skip_leb(p);
  6315. break;
  6316. case WASM_OP_RETHROW:
  6317. /* skip depth */
  6318. skip_leb(p);
  6319. break;
  6320. case WASM_OP_DELEGATE:
  6321. if (block_nested_depth == 1) {
  6322. *p_end_addr = (uint8 *)(p - 1);
  6323. return true;
  6324. }
  6325. else {
  6326. skip_leb(p);
  6327. /* the DELEGATE opcode ends the tryblock, */
  6328. block_nested_depth--;
  6329. if (block_nested_depth
  6330. < sizeof(block_stack) / sizeof(BlockAddr))
  6331. block_stack[block_nested_depth].end_addr =
  6332. (uint8 *)(p - 1);
  6333. }
  6334. break;
  6335. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6336. case WASM_OP_BLOCK:
  6337. case WASM_OP_LOOP:
  6338. case WASM_OP_IF:
  6339. {
  6340. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6341. u8 = read_uint8(p);
  6342. if (is_byte_a_type(u8)) {
  6343. #if WASM_ENABLE_GC != 0
  6344. if (wasm_is_type_multi_byte_type(u8)) {
  6345. /* the possible extra bytes of GC ref type have been
  6346. modified to OP_NOP, no need to resolve them again */
  6347. }
  6348. #endif
  6349. }
  6350. else {
  6351. p--;
  6352. /* block type */
  6353. skip_leb_int32(p, p_end);
  6354. }
  6355. if (block_nested_depth
  6356. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6357. block_stack[block_nested_depth].start_addr = p;
  6358. block_stack[block_nested_depth].else_addr = NULL;
  6359. }
  6360. block_nested_depth++;
  6361. break;
  6362. }
  6363. case EXT_OP_BLOCK:
  6364. case EXT_OP_LOOP:
  6365. case EXT_OP_IF:
  6366. /* block type */
  6367. skip_leb_int32(p, p_end);
  6368. if (block_nested_depth
  6369. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6370. block_stack[block_nested_depth].start_addr = p;
  6371. block_stack[block_nested_depth].else_addr = NULL;
  6372. }
  6373. block_nested_depth++;
  6374. break;
  6375. case WASM_OP_ELSE:
  6376. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6377. else_addr = (uint8 *)(p - 1);
  6378. if (block_nested_depth - 1
  6379. < sizeof(block_stack) / sizeof(BlockAddr))
  6380. block_stack[block_nested_depth - 1].else_addr =
  6381. (uint8 *)(p - 1);
  6382. break;
  6383. case WASM_OP_END:
  6384. if (block_nested_depth == 1) {
  6385. if (label_type == LABEL_TYPE_IF)
  6386. *p_else_addr = else_addr;
  6387. *p_end_addr = (uint8 *)(p - 1);
  6388. block_stack[0].end_addr = (uint8 *)(p - 1);
  6389. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6390. t++) {
  6391. start_addr = block_stack[t].start_addr;
  6392. if (start_addr) {
  6393. i = ((uintptr_t)start_addr)
  6394. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6395. block =
  6396. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6397. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6398. if (!block[j].start_addr)
  6399. break;
  6400. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6401. memmove(block + 1, block,
  6402. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6403. * sizeof(BlockAddr));
  6404. j = 0;
  6405. }
  6406. block[j].start_addr = block_stack[t].start_addr;
  6407. block[j].else_addr = block_stack[t].else_addr;
  6408. block[j].end_addr = block_stack[t].end_addr;
  6409. }
  6410. else
  6411. break;
  6412. }
  6413. return true;
  6414. }
  6415. else {
  6416. block_nested_depth--;
  6417. if (block_nested_depth
  6418. < sizeof(block_stack) / sizeof(BlockAddr))
  6419. block_stack[block_nested_depth].end_addr =
  6420. (uint8 *)(p - 1);
  6421. }
  6422. break;
  6423. case WASM_OP_BR:
  6424. case WASM_OP_BR_IF:
  6425. skip_leb_uint32(p, p_end); /* labelidx */
  6426. break;
  6427. case WASM_OP_BR_TABLE:
  6428. read_leb_uint32(p, p_end, count); /* label num */
  6429. #if WASM_ENABLE_FAST_INTERP != 0
  6430. for (i = 0; i <= count; i++) /* labelidxs */
  6431. skip_leb_uint32(p, p_end);
  6432. #else
  6433. p += count + 1;
  6434. while (*p == WASM_OP_NOP)
  6435. p++;
  6436. #endif
  6437. break;
  6438. #if WASM_ENABLE_FAST_INTERP == 0
  6439. case EXT_OP_BR_TABLE_CACHE:
  6440. read_leb_uint32(p, p_end, count); /* label num */
  6441. while (*p == WASM_OP_NOP)
  6442. p++;
  6443. break;
  6444. #endif
  6445. case WASM_OP_RETURN:
  6446. break;
  6447. case WASM_OP_CALL:
  6448. #if WASM_ENABLE_TAIL_CALL != 0
  6449. case WASM_OP_RETURN_CALL:
  6450. #endif
  6451. skip_leb_uint32(p, p_end); /* funcidx */
  6452. break;
  6453. case WASM_OP_CALL_INDIRECT:
  6454. #if WASM_ENABLE_TAIL_CALL != 0
  6455. case WASM_OP_RETURN_CALL_INDIRECT:
  6456. #endif
  6457. skip_leb_uint32(p, p_end); /* typeidx */
  6458. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6459. u8 = read_uint8(p); /* 0x00 */
  6460. #else
  6461. skip_leb_uint32(p, p_end); /* tableidx */
  6462. #endif
  6463. break;
  6464. #if WASM_ENABLE_GC != 0
  6465. case WASM_OP_CALL_REF:
  6466. case WASM_OP_RETURN_CALL_REF:
  6467. skip_leb_uint32(p, p_end); /* typeidx */
  6468. break;
  6469. #endif
  6470. case WASM_OP_DROP:
  6471. case WASM_OP_SELECT:
  6472. case WASM_OP_DROP_64:
  6473. case WASM_OP_SELECT_64:
  6474. break;
  6475. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6476. case WASM_OP_SELECT_T:
  6477. {
  6478. skip_leb_uint32(p, p_end); /* vec length */
  6479. u8 = read_uint8(p); /* typeidx */
  6480. /* the possible extra bytes of GC ref type have been
  6481. modified to OP_NOP, no need to resolve them again */
  6482. break;
  6483. }
  6484. case WASM_OP_TABLE_GET:
  6485. case WASM_OP_TABLE_SET:
  6486. skip_leb_uint32(p, p_end); /* table index */
  6487. break;
  6488. case WASM_OP_REF_NULL:
  6489. {
  6490. u8 = read_uint8(p); /* type */
  6491. if (is_byte_a_type(u8)) {
  6492. #if WASM_ENABLE_GC != 0
  6493. if (wasm_is_type_multi_byte_type(u8)) {
  6494. /* the possible extra bytes of GC ref type have been
  6495. modified to OP_NOP, no need to resolve them again */
  6496. }
  6497. #endif
  6498. }
  6499. else {
  6500. p--;
  6501. skip_leb_uint32(p, p_end);
  6502. }
  6503. break;
  6504. }
  6505. case WASM_OP_REF_IS_NULL:
  6506. break;
  6507. case WASM_OP_REF_FUNC:
  6508. skip_leb_uint32(p, p_end); /* func index */
  6509. break;
  6510. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6511. #if WASM_ENABLE_GC != 0
  6512. case WASM_OP_REF_AS_NON_NULL:
  6513. case WASM_OP_REF_EQ:
  6514. break;
  6515. case WASM_OP_BR_ON_NULL:
  6516. case WASM_OP_BR_ON_NON_NULL:
  6517. skip_leb_uint32(p, p_end); /* label index */
  6518. break;
  6519. #endif /* end of WASM_ENABLE_GC != 0 */
  6520. case WASM_OP_GET_LOCAL:
  6521. case WASM_OP_SET_LOCAL:
  6522. case WASM_OP_TEE_LOCAL:
  6523. case WASM_OP_GET_GLOBAL:
  6524. case WASM_OP_SET_GLOBAL:
  6525. case WASM_OP_GET_GLOBAL_64:
  6526. case WASM_OP_SET_GLOBAL_64:
  6527. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6528. skip_leb_uint32(p, p_end); /* local index */
  6529. break;
  6530. case EXT_OP_GET_LOCAL_FAST:
  6531. case EXT_OP_SET_LOCAL_FAST:
  6532. case EXT_OP_TEE_LOCAL_FAST:
  6533. CHECK_BUF(p, p_end, 1);
  6534. p++;
  6535. break;
  6536. case WASM_OP_I32_LOAD:
  6537. case WASM_OP_I64_LOAD:
  6538. case WASM_OP_F32_LOAD:
  6539. case WASM_OP_F64_LOAD:
  6540. case WASM_OP_I32_LOAD8_S:
  6541. case WASM_OP_I32_LOAD8_U:
  6542. case WASM_OP_I32_LOAD16_S:
  6543. case WASM_OP_I32_LOAD16_U:
  6544. case WASM_OP_I64_LOAD8_S:
  6545. case WASM_OP_I64_LOAD8_U:
  6546. case WASM_OP_I64_LOAD16_S:
  6547. case WASM_OP_I64_LOAD16_U:
  6548. case WASM_OP_I64_LOAD32_S:
  6549. case WASM_OP_I64_LOAD32_U:
  6550. case WASM_OP_I32_STORE:
  6551. case WASM_OP_I64_STORE:
  6552. case WASM_OP_F32_STORE:
  6553. case WASM_OP_F64_STORE:
  6554. case WASM_OP_I32_STORE8:
  6555. case WASM_OP_I32_STORE16:
  6556. case WASM_OP_I64_STORE8:
  6557. case WASM_OP_I64_STORE16:
  6558. case WASM_OP_I64_STORE32:
  6559. skip_leb_uint32(p, p_end); /* align */
  6560. skip_leb_mem_offset(p, p_end); /* offset */
  6561. break;
  6562. case WASM_OP_MEMORY_SIZE:
  6563. case WASM_OP_MEMORY_GROW:
  6564. skip_leb_uint32(p, p_end); /* 0x00 */
  6565. break;
  6566. case WASM_OP_I32_CONST:
  6567. skip_leb_int32(p, p_end);
  6568. break;
  6569. case WASM_OP_I64_CONST:
  6570. skip_leb_int64(p, p_end);
  6571. break;
  6572. case WASM_OP_F32_CONST:
  6573. p += sizeof(float32);
  6574. break;
  6575. case WASM_OP_F64_CONST:
  6576. p += sizeof(float64);
  6577. break;
  6578. case WASM_OP_I32_EQZ:
  6579. case WASM_OP_I32_EQ:
  6580. case WASM_OP_I32_NE:
  6581. case WASM_OP_I32_LT_S:
  6582. case WASM_OP_I32_LT_U:
  6583. case WASM_OP_I32_GT_S:
  6584. case WASM_OP_I32_GT_U:
  6585. case WASM_OP_I32_LE_S:
  6586. case WASM_OP_I32_LE_U:
  6587. case WASM_OP_I32_GE_S:
  6588. case WASM_OP_I32_GE_U:
  6589. case WASM_OP_I64_EQZ:
  6590. case WASM_OP_I64_EQ:
  6591. case WASM_OP_I64_NE:
  6592. case WASM_OP_I64_LT_S:
  6593. case WASM_OP_I64_LT_U:
  6594. case WASM_OP_I64_GT_S:
  6595. case WASM_OP_I64_GT_U:
  6596. case WASM_OP_I64_LE_S:
  6597. case WASM_OP_I64_LE_U:
  6598. case WASM_OP_I64_GE_S:
  6599. case WASM_OP_I64_GE_U:
  6600. case WASM_OP_F32_EQ:
  6601. case WASM_OP_F32_NE:
  6602. case WASM_OP_F32_LT:
  6603. case WASM_OP_F32_GT:
  6604. case WASM_OP_F32_LE:
  6605. case WASM_OP_F32_GE:
  6606. case WASM_OP_F64_EQ:
  6607. case WASM_OP_F64_NE:
  6608. case WASM_OP_F64_LT:
  6609. case WASM_OP_F64_GT:
  6610. case WASM_OP_F64_LE:
  6611. case WASM_OP_F64_GE:
  6612. case WASM_OP_I32_CLZ:
  6613. case WASM_OP_I32_CTZ:
  6614. case WASM_OP_I32_POPCNT:
  6615. case WASM_OP_I32_ADD:
  6616. case WASM_OP_I32_SUB:
  6617. case WASM_OP_I32_MUL:
  6618. case WASM_OP_I32_DIV_S:
  6619. case WASM_OP_I32_DIV_U:
  6620. case WASM_OP_I32_REM_S:
  6621. case WASM_OP_I32_REM_U:
  6622. case WASM_OP_I32_AND:
  6623. case WASM_OP_I32_OR:
  6624. case WASM_OP_I32_XOR:
  6625. case WASM_OP_I32_SHL:
  6626. case WASM_OP_I32_SHR_S:
  6627. case WASM_OP_I32_SHR_U:
  6628. case WASM_OP_I32_ROTL:
  6629. case WASM_OP_I32_ROTR:
  6630. case WASM_OP_I64_CLZ:
  6631. case WASM_OP_I64_CTZ:
  6632. case WASM_OP_I64_POPCNT:
  6633. case WASM_OP_I64_ADD:
  6634. case WASM_OP_I64_SUB:
  6635. case WASM_OP_I64_MUL:
  6636. case WASM_OP_I64_DIV_S:
  6637. case WASM_OP_I64_DIV_U:
  6638. case WASM_OP_I64_REM_S:
  6639. case WASM_OP_I64_REM_U:
  6640. case WASM_OP_I64_AND:
  6641. case WASM_OP_I64_OR:
  6642. case WASM_OP_I64_XOR:
  6643. case WASM_OP_I64_SHL:
  6644. case WASM_OP_I64_SHR_S:
  6645. case WASM_OP_I64_SHR_U:
  6646. case WASM_OP_I64_ROTL:
  6647. case WASM_OP_I64_ROTR:
  6648. case WASM_OP_F32_ABS:
  6649. case WASM_OP_F32_NEG:
  6650. case WASM_OP_F32_CEIL:
  6651. case WASM_OP_F32_FLOOR:
  6652. case WASM_OP_F32_TRUNC:
  6653. case WASM_OP_F32_NEAREST:
  6654. case WASM_OP_F32_SQRT:
  6655. case WASM_OP_F32_ADD:
  6656. case WASM_OP_F32_SUB:
  6657. case WASM_OP_F32_MUL:
  6658. case WASM_OP_F32_DIV:
  6659. case WASM_OP_F32_MIN:
  6660. case WASM_OP_F32_MAX:
  6661. case WASM_OP_F32_COPYSIGN:
  6662. case WASM_OP_F64_ABS:
  6663. case WASM_OP_F64_NEG:
  6664. case WASM_OP_F64_CEIL:
  6665. case WASM_OP_F64_FLOOR:
  6666. case WASM_OP_F64_TRUNC:
  6667. case WASM_OP_F64_NEAREST:
  6668. case WASM_OP_F64_SQRT:
  6669. case WASM_OP_F64_ADD:
  6670. case WASM_OP_F64_SUB:
  6671. case WASM_OP_F64_MUL:
  6672. case WASM_OP_F64_DIV:
  6673. case WASM_OP_F64_MIN:
  6674. case WASM_OP_F64_MAX:
  6675. case WASM_OP_F64_COPYSIGN:
  6676. case WASM_OP_I32_WRAP_I64:
  6677. case WASM_OP_I32_TRUNC_S_F32:
  6678. case WASM_OP_I32_TRUNC_U_F32:
  6679. case WASM_OP_I32_TRUNC_S_F64:
  6680. case WASM_OP_I32_TRUNC_U_F64:
  6681. case WASM_OP_I64_EXTEND_S_I32:
  6682. case WASM_OP_I64_EXTEND_U_I32:
  6683. case WASM_OP_I64_TRUNC_S_F32:
  6684. case WASM_OP_I64_TRUNC_U_F32:
  6685. case WASM_OP_I64_TRUNC_S_F64:
  6686. case WASM_OP_I64_TRUNC_U_F64:
  6687. case WASM_OP_F32_CONVERT_S_I32:
  6688. case WASM_OP_F32_CONVERT_U_I32:
  6689. case WASM_OP_F32_CONVERT_S_I64:
  6690. case WASM_OP_F32_CONVERT_U_I64:
  6691. case WASM_OP_F32_DEMOTE_F64:
  6692. case WASM_OP_F64_CONVERT_S_I32:
  6693. case WASM_OP_F64_CONVERT_U_I32:
  6694. case WASM_OP_F64_CONVERT_S_I64:
  6695. case WASM_OP_F64_CONVERT_U_I64:
  6696. case WASM_OP_F64_PROMOTE_F32:
  6697. case WASM_OP_I32_REINTERPRET_F32:
  6698. case WASM_OP_I64_REINTERPRET_F64:
  6699. case WASM_OP_F32_REINTERPRET_I32:
  6700. case WASM_OP_F64_REINTERPRET_I64:
  6701. case WASM_OP_I32_EXTEND8_S:
  6702. case WASM_OP_I32_EXTEND16_S:
  6703. case WASM_OP_I64_EXTEND8_S:
  6704. case WASM_OP_I64_EXTEND16_S:
  6705. case WASM_OP_I64_EXTEND32_S:
  6706. break;
  6707. #if WASM_ENABLE_GC != 0
  6708. case WASM_OP_GC_PREFIX:
  6709. {
  6710. uint32 opcode1;
  6711. read_leb_uint32(p, p_end, opcode1);
  6712. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6713. is no larger than UINT8_MAX */
  6714. opcode = (uint8)opcode1;
  6715. switch (opcode) {
  6716. case WASM_OP_STRUCT_NEW:
  6717. case WASM_OP_STRUCT_NEW_DEFAULT:
  6718. skip_leb_uint32(p, p_end); /* typeidx */
  6719. break;
  6720. case WASM_OP_STRUCT_GET:
  6721. case WASM_OP_STRUCT_GET_S:
  6722. case WASM_OP_STRUCT_GET_U:
  6723. case WASM_OP_STRUCT_SET:
  6724. skip_leb_uint32(p, p_end); /* typeidx */
  6725. skip_leb_uint32(p, p_end); /* fieldidx */
  6726. break;
  6727. case WASM_OP_ARRAY_NEW:
  6728. case WASM_OP_ARRAY_NEW_DEFAULT:
  6729. case WASM_OP_ARRAY_GET:
  6730. case WASM_OP_ARRAY_GET_S:
  6731. case WASM_OP_ARRAY_GET_U:
  6732. case WASM_OP_ARRAY_SET:
  6733. case WASM_OP_ARRAY_FILL:
  6734. skip_leb_uint32(p, p_end); /* typeidx */
  6735. break;
  6736. case WASM_OP_ARRAY_COPY:
  6737. skip_leb_uint32(p, p_end); /* typeidx1 */
  6738. skip_leb_uint32(p, p_end); /* typeidx2 */
  6739. break;
  6740. case WASM_OP_ARRAY_LEN:
  6741. break;
  6742. case WASM_OP_ARRAY_NEW_FIXED:
  6743. case WASM_OP_ARRAY_NEW_DATA:
  6744. case WASM_OP_ARRAY_NEW_ELEM:
  6745. skip_leb_uint32(p, p_end); /* typeidx */
  6746. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6747. break;
  6748. case WASM_OP_REF_I31:
  6749. case WASM_OP_I31_GET_S:
  6750. case WASM_OP_I31_GET_U:
  6751. break;
  6752. case WASM_OP_REF_TEST:
  6753. case WASM_OP_REF_CAST:
  6754. case WASM_OP_REF_TEST_NULLABLE:
  6755. case WASM_OP_REF_CAST_NULLABLE:
  6756. skip_leb_int32(p, p_end); /* heaptype */
  6757. break;
  6758. case WASM_OP_BR_ON_CAST:
  6759. case WASM_OP_BR_ON_CAST_FAIL:
  6760. p += sizeof(uint8); /* castflag */
  6761. skip_leb_uint32(p, p_end); /* labelidx */
  6762. skip_leb_int32(p, p_end); /* heaptype */
  6763. skip_leb_int32(p, p_end); /* heaptype2 */
  6764. break;
  6765. case WASM_OP_ANY_CONVERT_EXTERN:
  6766. case WASM_OP_EXTERN_CONVERT_ANY:
  6767. break;
  6768. #if WASM_ENABLE_STRINGREF != 0
  6769. case WASM_OP_STRING_NEW_UTF8:
  6770. case WASM_OP_STRING_NEW_WTF16:
  6771. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6772. case WASM_OP_STRING_NEW_WTF8:
  6773. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6774. break;
  6775. case WASM_OP_STRING_CONST:
  6776. skip_leb_int32(p, p_end); /* contents */
  6777. break;
  6778. case WASM_OP_STRING_MEASURE_UTF8:
  6779. case WASM_OP_STRING_MEASURE_WTF8:
  6780. case WASM_OP_STRING_MEASURE_WTF16:
  6781. break;
  6782. case WASM_OP_STRING_ENCODE_UTF8:
  6783. case WASM_OP_STRING_ENCODE_WTF16:
  6784. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6785. case WASM_OP_STRING_ENCODE_WTF8:
  6786. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6787. break;
  6788. case WASM_OP_STRING_CONCAT:
  6789. case WASM_OP_STRING_EQ:
  6790. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6791. case WASM_OP_STRING_AS_WTF8:
  6792. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6793. break;
  6794. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6795. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6796. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6797. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6798. break;
  6799. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6800. case WASM_OP_STRING_AS_WTF16:
  6801. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6802. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6803. break;
  6804. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6805. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6806. break;
  6807. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6808. case WASM_OP_STRING_AS_ITER:
  6809. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6810. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6811. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6812. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6813. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6814. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6815. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6816. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6817. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6818. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6819. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6820. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6821. break;
  6822. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6823. default:
  6824. return false;
  6825. }
  6826. break;
  6827. }
  6828. #endif /* end of WASM_ENABLE_GC != 0 */
  6829. case WASM_OP_MISC_PREFIX:
  6830. {
  6831. uint32 opcode1;
  6832. read_leb_uint32(p, p_end, opcode1);
  6833. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6834. is no larger than UINT8_MAX */
  6835. opcode = (uint8)opcode1;
  6836. switch (opcode) {
  6837. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6838. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6839. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6840. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6841. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6842. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6843. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6844. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6845. break;
  6846. #if WASM_ENABLE_BULK_MEMORY != 0
  6847. case WASM_OP_MEMORY_INIT:
  6848. skip_leb_uint32(p, p_end);
  6849. /* skip memory idx */
  6850. p++;
  6851. break;
  6852. case WASM_OP_DATA_DROP:
  6853. skip_leb_uint32(p, p_end);
  6854. break;
  6855. case WASM_OP_MEMORY_COPY:
  6856. /* skip two memory idx */
  6857. p += 2;
  6858. break;
  6859. case WASM_OP_MEMORY_FILL:
  6860. /* skip memory idx */
  6861. p++;
  6862. break;
  6863. #endif /* WASM_ENABLE_BULK_MEMORY */
  6864. #if WASM_ENABLE_REF_TYPES != 0
  6865. case WASM_OP_TABLE_INIT:
  6866. case WASM_OP_TABLE_COPY:
  6867. /* tableidx */
  6868. skip_leb_uint32(p, p_end);
  6869. /* elemidx */
  6870. skip_leb_uint32(p, p_end);
  6871. break;
  6872. case WASM_OP_ELEM_DROP:
  6873. /* elemidx */
  6874. skip_leb_uint32(p, p_end);
  6875. break;
  6876. case WASM_OP_TABLE_SIZE:
  6877. case WASM_OP_TABLE_GROW:
  6878. case WASM_OP_TABLE_FILL:
  6879. skip_leb_uint32(p, p_end); /* table idx */
  6880. break;
  6881. #endif /* WASM_ENABLE_REF_TYPES */
  6882. default:
  6883. return false;
  6884. }
  6885. break;
  6886. }
  6887. #if WASM_ENABLE_SIMD != 0
  6888. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6889. case WASM_OP_SIMD_PREFIX:
  6890. {
  6891. uint32 opcode1;
  6892. read_leb_uint32(p, p_end, opcode1);
  6893. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6894. is no larger than UINT8_MAX */
  6895. opcode = (uint8)opcode1;
  6896. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6897. */
  6898. switch (opcode) {
  6899. case SIMD_v128_load:
  6900. case SIMD_v128_load8x8_s:
  6901. case SIMD_v128_load8x8_u:
  6902. case SIMD_v128_load16x4_s:
  6903. case SIMD_v128_load16x4_u:
  6904. case SIMD_v128_load32x2_s:
  6905. case SIMD_v128_load32x2_u:
  6906. case SIMD_v128_load8_splat:
  6907. case SIMD_v128_load16_splat:
  6908. case SIMD_v128_load32_splat:
  6909. case SIMD_v128_load64_splat:
  6910. case SIMD_v128_store:
  6911. /* memarg align */
  6912. skip_leb_uint32(p, p_end);
  6913. /* memarg offset */
  6914. skip_leb_mem_offset(p, p_end);
  6915. break;
  6916. case SIMD_v128_const:
  6917. case SIMD_v8x16_shuffle:
  6918. /* immByte[16] immLaneId[16] */
  6919. CHECK_BUF1(p, p_end, 16);
  6920. p += 16;
  6921. break;
  6922. case SIMD_i8x16_extract_lane_s:
  6923. case SIMD_i8x16_extract_lane_u:
  6924. case SIMD_i8x16_replace_lane:
  6925. case SIMD_i16x8_extract_lane_s:
  6926. case SIMD_i16x8_extract_lane_u:
  6927. case SIMD_i16x8_replace_lane:
  6928. case SIMD_i32x4_extract_lane:
  6929. case SIMD_i32x4_replace_lane:
  6930. case SIMD_i64x2_extract_lane:
  6931. case SIMD_i64x2_replace_lane:
  6932. case SIMD_f32x4_extract_lane:
  6933. case SIMD_f32x4_replace_lane:
  6934. case SIMD_f64x2_extract_lane:
  6935. case SIMD_f64x2_replace_lane:
  6936. /* ImmLaneId */
  6937. CHECK_BUF(p, p_end, 1);
  6938. p++;
  6939. break;
  6940. case SIMD_v128_load8_lane:
  6941. case SIMD_v128_load16_lane:
  6942. case SIMD_v128_load32_lane:
  6943. case SIMD_v128_load64_lane:
  6944. case SIMD_v128_store8_lane:
  6945. case SIMD_v128_store16_lane:
  6946. case SIMD_v128_store32_lane:
  6947. case SIMD_v128_store64_lane:
  6948. /* memarg align */
  6949. skip_leb_uint32(p, p_end);
  6950. /* memarg offset */
  6951. skip_leb_mem_offset(p, p_end);
  6952. /* ImmLaneId */
  6953. CHECK_BUF(p, p_end, 1);
  6954. p++;
  6955. break;
  6956. case SIMD_v128_load32_zero:
  6957. case SIMD_v128_load64_zero:
  6958. /* memarg align */
  6959. skip_leb_uint32(p, p_end);
  6960. /* memarg offset */
  6961. skip_leb_mem_offset(p, p_end);
  6962. break;
  6963. default:
  6964. /*
  6965. * since latest SIMD specific used almost every value
  6966. * from 0x00 to 0xff, the default branch will present
  6967. * all opcodes without imm
  6968. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6969. */
  6970. break;
  6971. }
  6972. break;
  6973. }
  6974. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6975. #endif /* end of WASM_ENABLE_SIMD */
  6976. #if WASM_ENABLE_SHARED_MEMORY != 0
  6977. case WASM_OP_ATOMIC_PREFIX:
  6978. {
  6979. /* TODO: memory64 offset type changes */
  6980. uint32 opcode1;
  6981. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6982. read_leb_uint32(p, p_end, opcode1);
  6983. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6984. is no larger than UINT8_MAX */
  6985. opcode = (uint8)opcode1;
  6986. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6987. skip_leb_uint32(p, p_end); /* align */
  6988. skip_leb_mem_offset(p, p_end); /* offset */
  6989. }
  6990. else {
  6991. /* atomic.fence doesn't have memarg */
  6992. p++;
  6993. }
  6994. break;
  6995. }
  6996. #endif
  6997. #if WASM_ENABLE_DEBUG_INTERP != 0
  6998. case DEBUG_OP_BREAK:
  6999. {
  7000. WASMDebugInstance *debug_instance =
  7001. wasm_exec_env_get_instance(exec_env);
  7002. char original_opcode[1];
  7003. uint64 size = 1;
  7004. WASMModuleInstance *module_inst =
  7005. (WASMModuleInstance *)exec_env->module_inst;
  7006. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7007. ? (p - 1) - module_inst->module->load_addr
  7008. : ~0;
  7009. if (debug_instance) {
  7010. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7011. original_opcode, &size)
  7012. && size == 1) {
  7013. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7014. "with %02x: ",
  7015. original_opcode[0]);
  7016. opcode = original_opcode[0];
  7017. goto op_break_retry;
  7018. }
  7019. }
  7020. break;
  7021. }
  7022. #endif
  7023. default:
  7024. return false;
  7025. }
  7026. }
  7027. (void)u8;
  7028. (void)exec_env;
  7029. return false;
  7030. fail:
  7031. return false;
  7032. }
  7033. #if WASM_ENABLE_FAST_INTERP != 0
  7034. #if WASM_DEBUG_PREPROCESSOR != 0
  7035. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7036. #else
  7037. #define LOG_OP(...) (void)0
  7038. #endif
  7039. #define PATCH_ELSE 0
  7040. #define PATCH_END 1
  7041. typedef struct BranchBlockPatch {
  7042. struct BranchBlockPatch *next;
  7043. uint8 patch_type;
  7044. uint8 *code_compiled;
  7045. } BranchBlockPatch;
  7046. #endif
  7047. typedef struct BranchBlock {
  7048. uint8 label_type;
  7049. BlockType block_type;
  7050. uint8 *start_addr;
  7051. uint8 *else_addr;
  7052. uint8 *end_addr;
  7053. uint32 stack_cell_num;
  7054. #if WASM_ENABLE_GC != 0
  7055. uint32 reftype_map_num;
  7056. /* Indicate which local is used inside current block, used to validate
  7057. * local.get with non-nullable ref types */
  7058. uint8 *local_use_mask;
  7059. uint32 local_use_mask_size;
  7060. #endif
  7061. #if WASM_ENABLE_FAST_INTERP != 0
  7062. uint16 dynamic_offset;
  7063. uint8 *code_compiled;
  7064. BranchBlockPatch *patch_list;
  7065. /* This is used to save params frame_offset of of if block */
  7066. int16 *param_frame_offsets;
  7067. /* This is used to recover the dynamic offset for else branch,
  7068. * and also to remember the start offset of dynamic space which
  7069. * stores the block arguments for loop block, so we can use it
  7070. * to copy the stack operands to the loop block's arguments in
  7071. * wasm_loader_emit_br_info for opcode br. */
  7072. uint16 start_dynamic_offset;
  7073. #endif
  7074. /* Indicate the operand stack is in polymorphic state.
  7075. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7076. * to polymorphic state until the block's 'end' opcode is processed.
  7077. * If stack is in polymorphic state and stack is empty, instruction can
  7078. * pop any type of value directly without decreasing stack top pointer
  7079. * and stack cell num. */
  7080. bool is_stack_polymorphic;
  7081. } BranchBlock;
  7082. typedef struct WASMLoaderContext {
  7083. /* frame ref stack */
  7084. uint8 *frame_ref;
  7085. uint8 *frame_ref_bottom;
  7086. uint8 *frame_ref_boundary;
  7087. uint32 frame_ref_size;
  7088. uint32 stack_cell_num;
  7089. uint32 max_stack_cell_num;
  7090. #if WASM_ENABLE_GC != 0
  7091. /* frame reftype map stack */
  7092. WASMRefTypeMap *frame_reftype_map;
  7093. WASMRefTypeMap *frame_reftype_map_bottom;
  7094. WASMRefTypeMap *frame_reftype_map_boundary;
  7095. uint32 frame_reftype_map_size;
  7096. uint32 reftype_map_num;
  7097. uint32 max_reftype_map_num;
  7098. /* Current module */
  7099. WASMModule *module;
  7100. /* Current module's ref_type_set */
  7101. HashMap *ref_type_set;
  7102. /* Always point to local variable ref_type of
  7103. wasm_loader_prepare_bytecode */
  7104. WASMRefType *ref_type_tmp;
  7105. #endif
  7106. /* frame csp stack */
  7107. BranchBlock *frame_csp;
  7108. BranchBlock *frame_csp_bottom;
  7109. BranchBlock *frame_csp_boundary;
  7110. uint32 frame_csp_size;
  7111. uint32 csp_num;
  7112. uint32 max_csp_num;
  7113. #if WASM_ENABLE_FAST_INTERP != 0
  7114. /* frame offset stack */
  7115. int16 *frame_offset;
  7116. int16 *frame_offset_bottom;
  7117. int16 *frame_offset_boundary;
  7118. uint32 frame_offset_size;
  7119. int16 dynamic_offset;
  7120. int16 start_dynamic_offset;
  7121. int16 max_dynamic_offset;
  7122. /* preserved local offset */
  7123. int16 preserved_local_offset;
  7124. /* const buffer */
  7125. uint8 *const_buf;
  7126. uint16 num_const;
  7127. uint16 const_cell_num;
  7128. uint32 const_buf_size;
  7129. /* processed code */
  7130. uint8 *p_code_compiled;
  7131. uint8 *p_code_compiled_end;
  7132. uint32 code_compiled_size;
  7133. /* If the last opcode will be dropped, the peak memory usage will be larger
  7134. * than the final code_compiled_size, we record the peak size to ensure
  7135. * there will not be invalid memory access during second traverse */
  7136. uint32 code_compiled_peak_size;
  7137. #endif
  7138. } WASMLoaderContext;
  7139. typedef struct Const {
  7140. WASMValue value;
  7141. uint16 slot_index;
  7142. uint8 value_type;
  7143. } Const;
  7144. #define CHECK_CSP_PUSH() \
  7145. do { \
  7146. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7147. MEM_REALLOC( \
  7148. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7149. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7150. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7151. ctx->frame_csp_boundary = \
  7152. ctx->frame_csp_bottom \
  7153. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7154. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7155. } \
  7156. } while (0)
  7157. #define CHECK_CSP_POP() \
  7158. do { \
  7159. if (ctx->csp_num < 1) { \
  7160. set_error_buf(error_buf, error_buf_size, \
  7161. "type mismatch: " \
  7162. "expect data but block stack was empty"); \
  7163. goto fail; \
  7164. } \
  7165. } while (0)
  7166. #if WASM_ENABLE_FAST_INTERP != 0
  7167. static bool
  7168. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7169. uint32 error_buf_size)
  7170. {
  7171. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7172. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7173. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7174. ctx->frame_offset_size + 16);
  7175. ctx->frame_offset_size += 16;
  7176. ctx->frame_offset_boundary =
  7177. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7178. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7179. }
  7180. return true;
  7181. fail:
  7182. return false;
  7183. }
  7184. static bool
  7185. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7186. {
  7187. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7188. return false;
  7189. return true;
  7190. }
  7191. static void
  7192. free_label_patch_list(BranchBlock *frame_csp)
  7193. {
  7194. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7195. BranchBlockPatch *next;
  7196. while (label_patch != NULL) {
  7197. next = label_patch->next;
  7198. wasm_runtime_free(label_patch);
  7199. label_patch = next;
  7200. }
  7201. frame_csp->patch_list = NULL;
  7202. }
  7203. static void
  7204. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7205. {
  7206. BranchBlock *tmp_csp = frame_csp;
  7207. uint32 i;
  7208. for (i = 0; i < csp_num; i++) {
  7209. free_label_patch_list(tmp_csp);
  7210. tmp_csp++;
  7211. }
  7212. }
  7213. static void
  7214. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7215. {
  7216. BranchBlock *tmp_csp = frame_csp;
  7217. uint32 i;
  7218. for (i = 0; i < csp_num; i++) {
  7219. if (tmp_csp->param_frame_offsets)
  7220. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7221. tmp_csp++;
  7222. }
  7223. }
  7224. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7225. #if WASM_ENABLE_GC != 0
  7226. static bool
  7227. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7228. char *error_buf, uint32 error_buf_size)
  7229. {
  7230. BranchBlock *current_csp = ctx->frame_csp - 1;
  7231. uint32 local_mask_size;
  7232. if (local_count == 0) {
  7233. current_csp->local_use_mask_size = 0;
  7234. return true;
  7235. }
  7236. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7237. * else branch, we don't need to allocate memory again */
  7238. if (!current_csp->local_use_mask) {
  7239. local_mask_size = (local_count + 7) / sizeof(uint8);
  7240. if (!(current_csp->local_use_mask =
  7241. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7242. return false;
  7243. }
  7244. current_csp->local_use_mask_size = local_mask_size;
  7245. }
  7246. else {
  7247. local_mask_size = current_csp->local_use_mask_size;
  7248. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7249. }
  7250. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7251. /* For non-function blocks, inherit the use status from parent block */
  7252. BranchBlock *parent_csp = current_csp - 1;
  7253. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7254. bh_assert(parent_csp->local_use_mask);
  7255. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7256. parent_csp->local_use_mask, local_mask_size);
  7257. }
  7258. return true;
  7259. }
  7260. static void
  7261. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7262. {
  7263. BranchBlock *current_csp = ctx->frame_csp - 1;
  7264. bh_assert(current_csp->local_use_mask
  7265. || current_csp->local_use_mask_size == 0);
  7266. if (current_csp->local_use_mask) {
  7267. wasm_runtime_free(current_csp->local_use_mask);
  7268. }
  7269. current_csp->local_use_mask = NULL;
  7270. current_csp->local_use_mask_size = 0;
  7271. }
  7272. static void
  7273. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7274. {
  7275. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7276. uint32 i;
  7277. for (i = 0; i < ctx->csp_num; i++) {
  7278. if (tmp_csp->local_use_mask) {
  7279. wasm_runtime_free(tmp_csp->local_use_mask);
  7280. tmp_csp->local_use_mask = NULL;
  7281. tmp_csp->local_use_mask_size = 0;
  7282. }
  7283. tmp_csp++;
  7284. }
  7285. }
  7286. static void
  7287. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7288. {
  7289. BranchBlock *current_csp = ctx->frame_csp - 1;
  7290. uint32 byte_offset = index / sizeof(uint8);
  7291. uint32 bit_offset = index % sizeof(uint8);
  7292. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7293. bh_assert(current_csp->local_use_mask);
  7294. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7295. }
  7296. static bool
  7297. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7298. {
  7299. BranchBlock *current_csp = ctx->frame_csp - 1;
  7300. uint32 byte_offset = index / sizeof(uint8);
  7301. uint32 bit_offset = index % sizeof(uint8);
  7302. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7303. bh_assert(current_csp->local_use_mask);
  7304. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7305. ? true
  7306. : false;
  7307. }
  7308. #endif /* end of WASM_ENABLE_GC != 0 */
  7309. static void
  7310. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7311. {
  7312. if (ctx) {
  7313. if (ctx->frame_ref_bottom)
  7314. wasm_runtime_free(ctx->frame_ref_bottom);
  7315. #if WASM_ENABLE_GC != 0
  7316. if (ctx->frame_reftype_map_bottom)
  7317. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7318. #endif
  7319. if (ctx->frame_csp_bottom) {
  7320. #if WASM_ENABLE_FAST_INTERP != 0
  7321. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7322. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7323. ctx->csp_num);
  7324. #endif
  7325. #if WASM_ENABLE_GC != 0
  7326. wasm_loader_clean_all_local_use_masks(ctx);
  7327. #endif
  7328. wasm_runtime_free(ctx->frame_csp_bottom);
  7329. }
  7330. #if WASM_ENABLE_FAST_INTERP != 0
  7331. if (ctx->frame_offset_bottom)
  7332. wasm_runtime_free(ctx->frame_offset_bottom);
  7333. if (ctx->const_buf)
  7334. wasm_runtime_free(ctx->const_buf);
  7335. #endif
  7336. wasm_runtime_free(ctx);
  7337. }
  7338. }
  7339. static WASMLoaderContext *
  7340. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7341. {
  7342. WASMLoaderContext *loader_ctx =
  7343. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7344. if (!loader_ctx)
  7345. return NULL;
  7346. loader_ctx->frame_ref_size = 32;
  7347. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7348. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7349. goto fail;
  7350. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7351. #if WASM_ENABLE_GC != 0
  7352. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7353. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7354. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7355. error_buf_size)))
  7356. goto fail;
  7357. loader_ctx->frame_reftype_map_boundary =
  7358. loader_ctx->frame_reftype_map_bottom + 16;
  7359. #endif
  7360. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7361. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7362. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7363. goto fail;
  7364. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7365. #if WASM_ENABLE_EXCE_HANDLING != 0
  7366. func->exception_handler_count = 0;
  7367. #endif
  7368. #if WASM_ENABLE_FAST_INTERP != 0
  7369. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7370. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7371. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7372. error_buf_size)))
  7373. goto fail;
  7374. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7375. loader_ctx->num_const = 0;
  7376. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7377. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7378. error_buf, error_buf_size)))
  7379. goto fail;
  7380. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7381. set_error_buf(error_buf, error_buf_size,
  7382. "fast interpreter offset overflow");
  7383. goto fail;
  7384. }
  7385. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7386. loader_ctx->max_dynamic_offset =
  7387. func->param_cell_num + func->local_cell_num;
  7388. #endif
  7389. return loader_ctx;
  7390. fail:
  7391. wasm_loader_ctx_destroy(loader_ctx);
  7392. return NULL;
  7393. }
  7394. static bool
  7395. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7396. uint32 error_buf_size)
  7397. {
  7398. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7399. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7400. /* Increase the frame ref stack */
  7401. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7402. ctx->frame_ref_size + 16);
  7403. ctx->frame_ref_size += 16;
  7404. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7405. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7406. }
  7407. #if WASM_ENABLE_GC != 0
  7408. if (wasm_is_type_multi_byte_type(type)
  7409. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7410. /* Increase the frame reftype map stack */
  7411. bh_assert(
  7412. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7413. * sizeof(WASMRefTypeMap))
  7414. == ctx->frame_reftype_map_size);
  7415. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7416. ctx->frame_reftype_map_size
  7417. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7418. ctx->frame_reftype_map =
  7419. ctx->frame_reftype_map_bottom
  7420. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7421. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7422. ctx->frame_reftype_map_boundary =
  7423. ctx->frame_reftype_map_bottom
  7424. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7425. }
  7426. #endif
  7427. return true;
  7428. fail:
  7429. return false;
  7430. }
  7431. static bool
  7432. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7433. uint32 error_buf_size)
  7434. {
  7435. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7436. uint32 i;
  7437. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7438. return false;
  7439. #if WASM_ENABLE_GC != 0
  7440. if (wasm_is_type_multi_byte_type(type)) {
  7441. WASMRefType *ref_type;
  7442. if (!(ref_type =
  7443. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7444. error_buf, error_buf_size))) {
  7445. return false;
  7446. }
  7447. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7448. /* Increase the frame reftype map stack */
  7449. bh_assert((uint32)((ctx->frame_reftype_map
  7450. - ctx->frame_reftype_map_bottom)
  7451. * sizeof(WASMRefTypeMap))
  7452. == ctx->frame_reftype_map_size);
  7453. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7454. ctx->frame_reftype_map_size,
  7455. ctx->frame_reftype_map_size
  7456. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7457. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7458. + ctx->frame_reftype_map_size
  7459. / ((uint32)sizeof(WASMRefTypeMap));
  7460. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7461. ctx->frame_reftype_map_boundary =
  7462. ctx->frame_reftype_map_bottom
  7463. + ctx->frame_reftype_map_size
  7464. / ((uint32)sizeof(WASMRefTypeMap));
  7465. }
  7466. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7467. ctx->frame_reftype_map->ref_type = ref_type;
  7468. ctx->frame_reftype_map++;
  7469. ctx->reftype_map_num++;
  7470. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7471. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7472. }
  7473. #endif
  7474. for (i = 0; i < type_cell_num; i++)
  7475. *ctx->frame_ref++ = type;
  7476. ctx->stack_cell_num += type_cell_num;
  7477. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7478. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7479. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7480. set_error_buf(error_buf, error_buf_size,
  7481. "operand stack depth limit exceeded");
  7482. return false;
  7483. }
  7484. }
  7485. return true;
  7486. #if WASM_ENABLE_GC != 0
  7487. fail:
  7488. return false;
  7489. #endif
  7490. }
  7491. static bool
  7492. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7493. int32 stack_cell_num,
  7494. #if WASM_ENABLE_GC != 0
  7495. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7496. #endif
  7497. uint8 type,
  7498. #if WASM_ENABLE_GC != 0
  7499. WASMRefType *ref_type,
  7500. #endif
  7501. char *error_buf, uint32 error_buf_size)
  7502. {
  7503. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7504. #if WASM_ENABLE_GC != 0
  7505. WASMRefType *frame_reftype = NULL;
  7506. #endif
  7507. if (stack_cell_num < type_cell_num) {
  7508. set_error_buf(error_buf, error_buf_size,
  7509. "type mismatch: expect data but stack was empty");
  7510. return false;
  7511. }
  7512. #if WASM_ENABLE_GC == 0
  7513. for (i = 0; i < type_cell_num; i++) {
  7514. if (*(frame_ref - 1 - i) != type) {
  7515. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7516. "type mismatch: expect ", type2str(type),
  7517. " but got other");
  7518. return false;
  7519. }
  7520. }
  7521. #else
  7522. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7523. bh_assert(reftype_map_num > 0);
  7524. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7525. }
  7526. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7527. ref_type, ctx->module->types,
  7528. ctx->module->type_count)) {
  7529. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7530. "type mismatch: expect ", type2str(type),
  7531. " but got other");
  7532. return false;
  7533. }
  7534. for (i = 0; i < type_cell_num - 1; i++) {
  7535. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7536. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7537. "type mismatch: expect ", type2str(type),
  7538. " but got other");
  7539. return false;
  7540. }
  7541. }
  7542. #endif
  7543. return true;
  7544. }
  7545. static bool
  7546. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7547. uint32 error_buf_size)
  7548. {
  7549. int32 block_stack_cell_num =
  7550. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7551. #if WASM_ENABLE_GC != 0
  7552. int32 reftype_map_num =
  7553. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7554. #endif
  7555. if (block_stack_cell_num > 0) {
  7556. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7557. /* the stack top is a value of any type, return success */
  7558. return true;
  7559. }
  7560. #if WASM_ENABLE_GC != 0
  7561. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7562. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7563. WASMRefType *stack_top_ref_type = NULL;
  7564. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7565. bh_assert(reftype_map_num > 0);
  7566. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7567. }
  7568. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7569. ctx->ref_type_tmp, ctx->module->types,
  7570. ctx->module->type_count)) {
  7571. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7572. uint32 ref_type_struct_size =
  7573. wasm_reftype_struct_size(stack_top_ref_type);
  7574. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7575. stack_top_ref_type, ref_type_struct_size);
  7576. }
  7577. return true;
  7578. }
  7579. }
  7580. #endif
  7581. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7582. #if WASM_ENABLE_GC != 0
  7583. ctx->frame_reftype_map, reftype_map_num,
  7584. #endif
  7585. type,
  7586. #if WASM_ENABLE_GC != 0
  7587. ctx->ref_type_tmp,
  7588. #endif
  7589. error_buf, error_buf_size)) {
  7590. return false;
  7591. }
  7592. return true;
  7593. }
  7594. static bool
  7595. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7596. uint32 error_buf_size)
  7597. {
  7598. BranchBlock *cur_block = ctx->frame_csp - 1;
  7599. int32 available_stack_cell =
  7600. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7601. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7602. /* Directly return success if current block is in stack
  7603. polymorphic state while stack is empty. */
  7604. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7605. return true;
  7606. if (type == VALUE_TYPE_VOID)
  7607. return true;
  7608. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7609. return false;
  7610. bh_assert(available_stack_cell > 0);
  7611. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7612. type = VALUE_TYPE_ANY;
  7613. cell_num_to_pop = 1;
  7614. }
  7615. ctx->frame_ref -= cell_num_to_pop;
  7616. ctx->stack_cell_num -= cell_num_to_pop;
  7617. #if WASM_ENABLE_GC != 0
  7618. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7619. ctx->frame_reftype_map--;
  7620. ctx->reftype_map_num--;
  7621. }
  7622. #endif
  7623. return true;
  7624. }
  7625. #if WASM_ENABLE_GC != 0
  7626. /* Get the stack top element of current block */
  7627. static bool
  7628. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7629. WASMRefType **p_ref_type, char *error_buf,
  7630. uint32 error_buf_size)
  7631. {
  7632. BranchBlock *cur_block = ctx->frame_csp - 1;
  7633. int32 available_stack_cell =
  7634. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7635. if (available_stack_cell <= 0) {
  7636. /* Directly return success if current block is in stack
  7637. polymorphic state while stack is empty. */
  7638. if (cur_block->is_stack_polymorphic) {
  7639. *p_type = VALUE_TYPE_ANY;
  7640. return true;
  7641. }
  7642. else {
  7643. set_error_buf(
  7644. error_buf, error_buf_size,
  7645. "type mismatch: expect data but block stack was empty");
  7646. return false;
  7647. }
  7648. }
  7649. *p_type = *(ctx->frame_ref - 1);
  7650. if (wasm_is_type_multi_byte_type(*p_type)) {
  7651. int32 available_reftype_map =
  7652. (int32)(ctx->reftype_map_num
  7653. - (ctx->frame_csp - 1)->reftype_map_num);
  7654. bh_assert(available_reftype_map > 0);
  7655. (void)available_reftype_map;
  7656. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7657. }
  7658. return true;
  7659. }
  7660. #if WASM_ENABLE_FAST_INTERP != 0
  7661. static bool
  7662. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7663. char *error_buf, uint32 error_buf_size);
  7664. #endif
  7665. /* Check whether the stack top elem is a heap object, and if yes,
  7666. pop and return it */
  7667. static bool
  7668. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7669. WASMRefType *ref_ht_ret, char *error_buf,
  7670. uint32 error_buf_size)
  7671. {
  7672. uint8 type = 0;
  7673. WASMRefType *ref_type = NULL;
  7674. /* Get stack top element */
  7675. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7676. error_buf_size)) {
  7677. return false;
  7678. }
  7679. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7680. /* stack top isn't a ref type */
  7681. && !wasm_is_type_reftype(type)) {
  7682. set_error_buf(error_buf, error_buf_size,
  7683. "type mismatch: expect heap object but got others");
  7684. return false;
  7685. }
  7686. /* POP stack top */
  7687. if (wasm_is_type_multi_byte_type(type)) {
  7688. bh_assert(ref_type);
  7689. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7690. wasm_reftype_struct_size(ref_type));
  7691. }
  7692. #if WASM_ENABLE_FAST_INTERP != 0
  7693. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7694. error_buf_size)) {
  7695. return false;
  7696. }
  7697. #else
  7698. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7699. return false;
  7700. }
  7701. #endif
  7702. if (p_type)
  7703. *p_type = type;
  7704. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7705. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7706. wasm_reftype_struct_size(ref_type));
  7707. }
  7708. return true;
  7709. }
  7710. /* Check whether the stack top elem is subtype of (ref null ht),
  7711. and if yes, pop it and return the converted (ref ht) */
  7712. static bool
  7713. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7714. WASMRefType *ref_ht_ret, char *error_buf,
  7715. uint32 error_buf_size)
  7716. {
  7717. uint8 type = 0;
  7718. WASMRefType ref_type = { 0 };
  7719. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7720. error_buf_size)) {
  7721. return false;
  7722. }
  7723. /* Convert to related (ref ht) and return */
  7724. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7725. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7726. */
  7727. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7728. HEAP_TYPE_ARRAY
  7729. + (type - REF_TYPE_ARRAYREF));
  7730. type = ref_ht_ret->ref_type;
  7731. }
  7732. else if (wasm_is_reftype_htref_nullable(type)
  7733. || wasm_is_reftype_htref_non_nullable(type)) {
  7734. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7735. wasm_reftype_struct_size(&ref_type));
  7736. /* Convert to (ref ht) */
  7737. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7738. ref_ht_ret->ref_ht_common.nullable = false;
  7739. type = ref_ht_ret->ref_type;
  7740. }
  7741. *p_type = type;
  7742. return true;
  7743. }
  7744. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7745. and if yes, pop it and return the type_idx */
  7746. static bool
  7747. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7748. uint32 *p_type_idx, char *error_buf,
  7749. uint32 error_buf_size)
  7750. {
  7751. uint8 type = 0;
  7752. int32 type_idx = -1;
  7753. WASMRefType *ref_type = NULL;
  7754. /* Get stack top element */
  7755. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7756. error_buf_size)) {
  7757. return false;
  7758. }
  7759. if (type != VALUE_TYPE_ANY) {
  7760. /* stack top isn't (ref null $t) */
  7761. if (!((wasm_is_reftype_htref_nullable(type)
  7762. || wasm_is_reftype_htref_non_nullable(type))
  7763. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7764. set_error_buf(error_buf, error_buf_size,
  7765. "type mismatch: expect (ref null $t) but got others");
  7766. return false;
  7767. }
  7768. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7769. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7770. wasm_reftype_struct_size(ref_type));
  7771. }
  7772. /* POP stack top */
  7773. #if WASM_ENABLE_FAST_INTERP != 0
  7774. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7775. error_buf_size)) {
  7776. return false;
  7777. }
  7778. #else
  7779. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7780. return false;
  7781. }
  7782. #endif
  7783. /* Convert to type_idx and return */
  7784. *p_type = type;
  7785. if (type != VALUE_TYPE_ANY)
  7786. *p_type_idx = (uint32)type_idx;
  7787. return true;
  7788. }
  7789. #endif /* WASM_ENABLE_GC != 0 */
  7790. #if WASM_ENABLE_FAST_INTERP == 0
  7791. static bool
  7792. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7793. uint8 type_push, uint8 type_pop, char *error_buf,
  7794. uint32 error_buf_size)
  7795. {
  7796. for (int i = 0; i < pop_cnt; i++) {
  7797. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7798. error_buf_size))
  7799. return false;
  7800. }
  7801. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7802. return false;
  7803. return true;
  7804. }
  7805. #endif
  7806. static bool
  7807. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7808. BlockType block_type, uint8 *start_addr,
  7809. char *error_buf, uint32 error_buf_size)
  7810. {
  7811. CHECK_CSP_PUSH();
  7812. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7813. ctx->frame_csp->label_type = label_type;
  7814. ctx->frame_csp->block_type = block_type;
  7815. ctx->frame_csp->start_addr = start_addr;
  7816. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7817. #if WASM_ENABLE_GC != 0
  7818. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7819. #endif
  7820. #if WASM_ENABLE_FAST_INTERP != 0
  7821. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7822. ctx->frame_csp->patch_list = NULL;
  7823. #endif
  7824. ctx->frame_csp++;
  7825. ctx->csp_num++;
  7826. if (ctx->csp_num > ctx->max_csp_num) {
  7827. ctx->max_csp_num = ctx->csp_num;
  7828. if (ctx->max_csp_num > UINT16_MAX) {
  7829. set_error_buf(error_buf, error_buf_size,
  7830. "label stack depth limit exceeded");
  7831. return false;
  7832. }
  7833. }
  7834. return true;
  7835. fail:
  7836. return false;
  7837. }
  7838. static bool
  7839. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7840. uint32 error_buf_size)
  7841. {
  7842. CHECK_CSP_POP();
  7843. #if WASM_ENABLE_FAST_INTERP != 0
  7844. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7845. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7846. #endif
  7847. ctx->frame_csp--;
  7848. ctx->csp_num--;
  7849. return true;
  7850. fail:
  7851. return false;
  7852. }
  7853. #if WASM_ENABLE_FAST_INTERP != 0
  7854. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7855. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7856. #define emit_label(opcode) \
  7857. do { \
  7858. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7859. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7860. } while (0)
  7861. #define skip_label() \
  7862. do { \
  7863. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7864. LOG_OP("\ndelete last op\n"); \
  7865. } while (0)
  7866. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7867. #if UINTPTR_MAX == UINT64_MAX
  7868. #define emit_label(opcode) \
  7869. do { \
  7870. int32 offset = \
  7871. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7872. /* emit int32 relative offset in 64-bit target */ \
  7873. wasm_loader_emit_uint32(loader_ctx, offset); \
  7874. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7875. } while (0)
  7876. #else
  7877. #define emit_label(opcode) \
  7878. do { \
  7879. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7880. /* emit uint32 label address in 32-bit target */ \
  7881. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7882. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7883. } while (0)
  7884. #endif
  7885. #define skip_label() \
  7886. do { \
  7887. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7888. LOG_OP("\ndelete last op\n"); \
  7889. } while (0)
  7890. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7891. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7892. #define emit_label(opcode) \
  7893. do { \
  7894. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7895. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7896. } while (0)
  7897. #define skip_label() \
  7898. do { \
  7899. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7900. LOG_OP("\ndelete last op\n"); \
  7901. } while (0)
  7902. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7903. #define emit_empty_label_addr_and_frame_ip(type) \
  7904. do { \
  7905. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7906. loader_ctx->p_code_compiled, error_buf, \
  7907. error_buf_size)) \
  7908. goto fail; \
  7909. /* label address, to be patched */ \
  7910. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7911. } while (0)
  7912. #define emit_br_info(frame_csp, is_br) \
  7913. do { \
  7914. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7915. error_buf_size)) \
  7916. goto fail; \
  7917. } while (0)
  7918. #define LAST_OP_OUTPUT_I32() \
  7919. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7920. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7921. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7922. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7923. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7924. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7925. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7926. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7927. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7928. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7929. || (last_op == EXT_OP_COPY_STACK_TOP)
  7930. #define LAST_OP_OUTPUT_I64() \
  7931. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7932. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7933. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7934. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7935. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7936. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7937. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7938. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7939. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7940. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7941. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7942. #define GET_CONST_OFFSET(type, val) \
  7943. do { \
  7944. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7945. &operand_offset, error_buf, \
  7946. error_buf_size))) \
  7947. goto fail; \
  7948. } while (0)
  7949. #define GET_CONST_F32_OFFSET(type, fval) \
  7950. do { \
  7951. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7952. &operand_offset, error_buf, \
  7953. error_buf_size))) \
  7954. goto fail; \
  7955. } while (0)
  7956. #define GET_CONST_F64_OFFSET(type, fval) \
  7957. do { \
  7958. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7959. &operand_offset, error_buf, \
  7960. error_buf_size))) \
  7961. goto fail; \
  7962. } while (0)
  7963. #define emit_operand(ctx, offset) \
  7964. do { \
  7965. wasm_loader_emit_int16(ctx, offset); \
  7966. LOG_OP("%d\t", offset); \
  7967. } while (0)
  7968. #define emit_byte(ctx, byte) \
  7969. do { \
  7970. wasm_loader_emit_uint8(ctx, byte); \
  7971. LOG_OP("%d\t", byte); \
  7972. } while (0)
  7973. #define emit_uint32(ctx, value) \
  7974. do { \
  7975. wasm_loader_emit_uint32(ctx, value); \
  7976. LOG_OP("%d\t", value); \
  7977. } while (0)
  7978. #define emit_uint64(ctx, value) \
  7979. do { \
  7980. wasm_loader_emit_const(ctx, &value, false); \
  7981. LOG_OP("%lld\t", value); \
  7982. } while (0)
  7983. #define emit_float32(ctx, value) \
  7984. do { \
  7985. wasm_loader_emit_const(ctx, &value, true); \
  7986. LOG_OP("%f\t", value); \
  7987. } while (0)
  7988. #define emit_float64(ctx, value) \
  7989. do { \
  7990. wasm_loader_emit_const(ctx, &value, false); \
  7991. LOG_OP("%f\t", value); \
  7992. } while (0)
  7993. static bool
  7994. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7995. {
  7996. if (!(ctx->p_code_compiled =
  7997. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7998. return false;
  7999. ctx->p_code_compiled_end =
  8000. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8001. /* clean up frame ref */
  8002. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8003. ctx->frame_ref = ctx->frame_ref_bottom;
  8004. ctx->stack_cell_num = 0;
  8005. #if WASM_ENABLE_GC != 0
  8006. /* clean up reftype map */
  8007. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8008. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8009. ctx->reftype_map_num = 0;
  8010. #endif
  8011. /* clean up frame csp */
  8012. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8013. ctx->frame_csp = ctx->frame_csp_bottom;
  8014. ctx->csp_num = 0;
  8015. ctx->max_csp_num = 0;
  8016. /* clean up frame offset */
  8017. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8018. ctx->frame_offset = ctx->frame_offset_bottom;
  8019. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8020. /* init preserved local offsets */
  8021. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8022. /* const buf is reserved */
  8023. return true;
  8024. }
  8025. static void
  8026. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8027. {
  8028. ctx->code_compiled_size += size;
  8029. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8030. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8031. }
  8032. }
  8033. static void
  8034. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8035. {
  8036. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8037. if (ctx->p_code_compiled) {
  8038. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8039. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8040. #endif
  8041. bh_memcpy_s(ctx->p_code_compiled,
  8042. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8043. value, size);
  8044. ctx->p_code_compiled += size;
  8045. }
  8046. else {
  8047. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8048. bh_assert((ctx->code_compiled_size & 1) == 0);
  8049. #endif
  8050. increase_compiled_code_space(ctx, size);
  8051. }
  8052. }
  8053. static void
  8054. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8055. {
  8056. if (ctx->p_code_compiled) {
  8057. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8058. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8059. #endif
  8060. STORE_U32(ctx->p_code_compiled, value);
  8061. ctx->p_code_compiled += sizeof(uint32);
  8062. }
  8063. else {
  8064. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8065. bh_assert((ctx->code_compiled_size & 1) == 0);
  8066. #endif
  8067. increase_compiled_code_space(ctx, sizeof(uint32));
  8068. }
  8069. }
  8070. static void
  8071. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8072. {
  8073. if (ctx->p_code_compiled) {
  8074. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8075. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8076. #endif
  8077. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8078. ctx->p_code_compiled += sizeof(int16);
  8079. }
  8080. else {
  8081. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8082. bh_assert((ctx->code_compiled_size & 1) == 0);
  8083. #endif
  8084. increase_compiled_code_space(ctx, sizeof(uint16));
  8085. }
  8086. }
  8087. static void
  8088. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8089. {
  8090. if (ctx->p_code_compiled) {
  8091. *(ctx->p_code_compiled) = value;
  8092. ctx->p_code_compiled += sizeof(uint8);
  8093. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8094. ctx->p_code_compiled++;
  8095. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8096. #endif
  8097. }
  8098. else {
  8099. increase_compiled_code_space(ctx, sizeof(uint8));
  8100. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8101. increase_compiled_code_space(ctx, sizeof(uint8));
  8102. bh_assert((ctx->code_compiled_size & 1) == 0);
  8103. #endif
  8104. }
  8105. }
  8106. static void
  8107. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8108. {
  8109. if (ctx->p_code_compiled) {
  8110. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8111. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8112. #endif
  8113. STORE_PTR(ctx->p_code_compiled, value);
  8114. ctx->p_code_compiled += sizeof(void *);
  8115. }
  8116. else {
  8117. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8118. bh_assert((ctx->code_compiled_size & 1) == 0);
  8119. #endif
  8120. increase_compiled_code_space(ctx, sizeof(void *));
  8121. }
  8122. }
  8123. static void
  8124. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8125. {
  8126. if (ctx->p_code_compiled) {
  8127. ctx->p_code_compiled -= size;
  8128. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8129. if (size == sizeof(uint8)) {
  8130. ctx->p_code_compiled--;
  8131. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8132. }
  8133. #endif
  8134. }
  8135. else {
  8136. ctx->code_compiled_size -= size;
  8137. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8138. if (size == sizeof(uint8)) {
  8139. ctx->code_compiled_size--;
  8140. bh_assert((ctx->code_compiled_size & 1) == 0);
  8141. }
  8142. #endif
  8143. }
  8144. }
  8145. static bool
  8146. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8147. uint32 local_index, uint32 local_type,
  8148. bool *preserved, char *error_buf,
  8149. uint32 error_buf_size)
  8150. {
  8151. uint32 i = 0;
  8152. int16 preserved_offset = (int16)local_index;
  8153. *preserved = false;
  8154. while (i < loader_ctx->stack_cell_num) {
  8155. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8156. /* move previous local into dynamic space before a set/tee_local opcode
  8157. */
  8158. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8159. if (!(*preserved)) {
  8160. *preserved = true;
  8161. skip_label();
  8162. preserved_offset = loader_ctx->preserved_local_offset;
  8163. if (loader_ctx->p_code_compiled) {
  8164. bh_assert(preserved_offset != (int16)local_index);
  8165. }
  8166. if (is_32bit_type(local_type)) {
  8167. /* Only increase preserve offset in the second traversal */
  8168. if (loader_ctx->p_code_compiled)
  8169. loader_ctx->preserved_local_offset++;
  8170. emit_label(EXT_OP_COPY_STACK_TOP);
  8171. }
  8172. else {
  8173. if (loader_ctx->p_code_compiled)
  8174. loader_ctx->preserved_local_offset += 2;
  8175. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8176. }
  8177. emit_operand(loader_ctx, local_index);
  8178. emit_operand(loader_ctx, preserved_offset);
  8179. emit_label(opcode);
  8180. }
  8181. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8182. }
  8183. if (is_32bit_type(cur_type))
  8184. i++;
  8185. else
  8186. i += 2;
  8187. }
  8188. (void)error_buf;
  8189. (void)error_buf_size;
  8190. return true;
  8191. }
  8192. static bool
  8193. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8194. char *error_buf, uint32 error_buf_size)
  8195. {
  8196. uint32 i = 0;
  8197. bool preserve_local;
  8198. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8199. blocks will not influence the value of these locals */
  8200. while (i < loader_ctx->stack_cell_num) {
  8201. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8202. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8203. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8204. && (cur_offset >= 0)) {
  8205. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8206. cur_type, &preserve_local,
  8207. error_buf, error_buf_size)))
  8208. return false;
  8209. }
  8210. if (is_32bit_type(cur_type)) {
  8211. i++;
  8212. }
  8213. else {
  8214. i += 2;
  8215. }
  8216. }
  8217. return true;
  8218. }
  8219. static bool
  8220. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8221. uint8 *p_code_compiled, char *error_buf,
  8222. uint32 error_buf_size)
  8223. {
  8224. BranchBlockPatch *patch =
  8225. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8226. if (!patch) {
  8227. return false;
  8228. }
  8229. patch->patch_type = patch_type;
  8230. patch->code_compiled = p_code_compiled;
  8231. if (!frame_csp->patch_list) {
  8232. frame_csp->patch_list = patch;
  8233. patch->next = NULL;
  8234. }
  8235. else {
  8236. patch->next = frame_csp->patch_list;
  8237. frame_csp->patch_list = patch;
  8238. }
  8239. return true;
  8240. }
  8241. static void
  8242. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8243. {
  8244. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8245. BranchBlockPatch *node = frame_csp->patch_list;
  8246. BranchBlockPatch *node_prev = NULL, *node_next;
  8247. if (!ctx->p_code_compiled)
  8248. return;
  8249. while (node) {
  8250. node_next = node->next;
  8251. if (node->patch_type == patch_type) {
  8252. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8253. if (node_prev == NULL) {
  8254. frame_csp->patch_list = node_next;
  8255. }
  8256. else {
  8257. node_prev->next = node_next;
  8258. }
  8259. wasm_runtime_free(node);
  8260. }
  8261. else {
  8262. node_prev = node;
  8263. }
  8264. node = node_next;
  8265. }
  8266. }
  8267. static bool
  8268. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8269. bool is_br, char *error_buf, uint32 error_buf_size)
  8270. {
  8271. /* br info layout:
  8272. * a) arity of target block
  8273. * b) total cell num of arity values
  8274. * c) each arity value's cell num
  8275. * d) each arity value's src frame offset
  8276. * e) each arity values's dst dynamic offset
  8277. * f) branch target address
  8278. *
  8279. * Note: b-e are omitted when arity is 0 so that
  8280. * interpreter can recover the br info quickly.
  8281. */
  8282. BlockType *block_type = &frame_csp->block_type;
  8283. uint8 *types = NULL, cell;
  8284. #if WASM_ENABLE_GC != 0
  8285. WASMRefTypeMap *reftype_maps;
  8286. uint32 reftype_map_count;
  8287. #endif
  8288. uint32 arity = 0;
  8289. int32 i;
  8290. int16 *frame_offset = ctx->frame_offset;
  8291. uint16 dynamic_offset;
  8292. /* Note: loop's arity is different from if and block. loop's arity is
  8293. * its parameter count while if and block arity is result count.
  8294. */
  8295. #if WASM_ENABLE_GC == 0
  8296. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8297. arity = block_type_get_param_types(block_type, &types);
  8298. else
  8299. arity = block_type_get_result_types(block_type, &types);
  8300. #else
  8301. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8302. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8303. &reftype_map_count);
  8304. else
  8305. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8306. &reftype_map_count);
  8307. #endif
  8308. /* Part a */
  8309. emit_uint32(ctx, arity);
  8310. if (arity) {
  8311. /* Part b */
  8312. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8313. /* Part c */
  8314. for (i = (int32)arity - 1; i >= 0; i--) {
  8315. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8316. emit_byte(ctx, cell);
  8317. }
  8318. /* Part d */
  8319. for (i = (int32)arity - 1; i >= 0; i--) {
  8320. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8321. frame_offset -= cell;
  8322. emit_operand(ctx, *(int16 *)(frame_offset));
  8323. }
  8324. /* Part e */
  8325. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8326. /* Use start_dynamic_offset which was set in
  8327. copy_params_to_dynamic_space */
  8328. dynamic_offset = frame_csp->start_dynamic_offset
  8329. + wasm_get_cell_num(types, arity);
  8330. else
  8331. dynamic_offset =
  8332. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8333. if (is_br)
  8334. ctx->dynamic_offset = dynamic_offset;
  8335. for (i = (int32)arity - 1; i >= 0; i--) {
  8336. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8337. dynamic_offset -= cell;
  8338. emit_operand(ctx, dynamic_offset);
  8339. }
  8340. }
  8341. /* Part f */
  8342. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8343. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8344. }
  8345. else {
  8346. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8347. error_buf, error_buf_size))
  8348. return false;
  8349. /* label address, to be patched */
  8350. wasm_loader_emit_ptr(ctx, NULL);
  8351. }
  8352. return true;
  8353. }
  8354. static bool
  8355. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8356. bool disable_emit, int16 operand_offset,
  8357. char *error_buf, uint32 error_buf_size)
  8358. {
  8359. uint32 cell_num_to_push, i;
  8360. if (type == VALUE_TYPE_VOID)
  8361. return true;
  8362. /* only check memory overflow in first traverse */
  8363. if (ctx->p_code_compiled == NULL) {
  8364. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8365. return false;
  8366. }
  8367. if (disable_emit)
  8368. *(ctx->frame_offset)++ = operand_offset;
  8369. else {
  8370. emit_operand(ctx, ctx->dynamic_offset);
  8371. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8372. ctx->dynamic_offset++;
  8373. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8374. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8375. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8376. goto fail;
  8377. }
  8378. }
  8379. }
  8380. if (is_32bit_type(type))
  8381. return true;
  8382. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8383. for (i = 0; i < cell_num_to_push; i++) {
  8384. if (ctx->p_code_compiled == NULL) {
  8385. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8386. return false;
  8387. }
  8388. ctx->frame_offset++;
  8389. if (!disable_emit) {
  8390. ctx->dynamic_offset++;
  8391. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8392. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8393. if (ctx->max_dynamic_offset >= INT16_MAX)
  8394. goto fail;
  8395. }
  8396. }
  8397. }
  8398. return true;
  8399. fail:
  8400. set_error_buf(error_buf, error_buf_size,
  8401. "fast interpreter offset overflow");
  8402. return false;
  8403. }
  8404. /* This function should be in front of wasm_loader_pop_frame_ref
  8405. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8406. will be modified by wasm_loader_pop_frame_ref */
  8407. static bool
  8408. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8409. char *error_buf, uint32 error_buf_size)
  8410. {
  8411. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8412. then current block is the function block */
  8413. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8414. BranchBlock *cur_block = ctx->frame_csp - depth;
  8415. int32 available_stack_cell =
  8416. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8417. uint32 cell_num_to_pop;
  8418. /* Directly return success if current block is in stack
  8419. polymorphic state while stack is empty. */
  8420. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8421. return true;
  8422. if (type == VALUE_TYPE_VOID)
  8423. return true;
  8424. /* Change type to ANY when the stack top is ANY, so as to avoid
  8425. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8426. two offsets */
  8427. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8428. type = VALUE_TYPE_ANY;
  8429. cell_num_to_pop = wasm_value_type_cell_num(type);
  8430. /* Check the offset stack bottom to ensure the frame offset
  8431. stack will not go underflow. But we don't thrown error
  8432. and return true here, because the error msg should be
  8433. given in wasm_loader_pop_frame_ref */
  8434. if (!check_offset_pop(ctx, cell_num_to_pop))
  8435. return true;
  8436. ctx->frame_offset -= cell_num_to_pop;
  8437. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8438. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8439. ctx->dynamic_offset -= cell_num_to_pop;
  8440. emit_operand(ctx, *(ctx->frame_offset));
  8441. (void)error_buf;
  8442. (void)error_buf_size;
  8443. return true;
  8444. }
  8445. static bool
  8446. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8447. bool disable_emit, int16 operand_offset,
  8448. char *error_buf, uint32 error_buf_size)
  8449. {
  8450. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8451. error_buf, error_buf_size)))
  8452. return false;
  8453. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8454. return false;
  8455. return true;
  8456. }
  8457. static bool
  8458. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8459. char *error_buf, uint32 error_buf_size)
  8460. {
  8461. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8462. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8463. return false;
  8464. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8465. return false;
  8466. return true;
  8467. }
  8468. static bool
  8469. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8470. uint8 type_push, uint8 type_pop,
  8471. bool disable_emit, int16 operand_offset,
  8472. char *error_buf, uint32 error_buf_size)
  8473. {
  8474. uint8 i;
  8475. for (i = 0; i < pop_cnt; i++) {
  8476. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8477. error_buf_size))
  8478. return false;
  8479. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8480. error_buf_size))
  8481. return false;
  8482. }
  8483. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8484. operand_offset, error_buf,
  8485. error_buf_size))
  8486. return false;
  8487. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8488. return false;
  8489. return true;
  8490. }
  8491. static bool
  8492. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8493. int16 *offset, char *error_buf,
  8494. uint32 error_buf_size)
  8495. {
  8496. int8 bytes_to_increase;
  8497. int16 operand_offset = 0;
  8498. Const *c;
  8499. /* Search existing constant */
  8500. for (c = (Const *)ctx->const_buf;
  8501. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8502. /* TODO: handle v128 type? */
  8503. if ((type == c->value_type)
  8504. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8505. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8506. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8507. || (type == VALUE_TYPE_FUNCREF
  8508. && *(int32 *)value == c->value.i32)
  8509. || (type == VALUE_TYPE_EXTERNREF
  8510. && *(int32 *)value == c->value.i32)
  8511. #endif
  8512. || (type == VALUE_TYPE_F64
  8513. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8514. || (type == VALUE_TYPE_F32
  8515. && (0
  8516. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8517. operand_offset = c->slot_index;
  8518. break;
  8519. }
  8520. if (is_32bit_type(c->value_type))
  8521. operand_offset += 1;
  8522. else
  8523. operand_offset += 2;
  8524. }
  8525. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8526. /* New constant, append to the const buffer */
  8527. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8528. bytes_to_increase = 2;
  8529. }
  8530. else {
  8531. bytes_to_increase = 1;
  8532. }
  8533. /* The max cell num of const buffer is 32768 since the valid index range
  8534. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8535. * full */
  8536. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8537. *offset = 0;
  8538. return true;
  8539. }
  8540. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8541. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8542. ctx->const_buf_size + 4 * sizeof(Const));
  8543. ctx->const_buf_size += 4 * sizeof(Const);
  8544. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8545. }
  8546. c->value_type = type;
  8547. switch (type) {
  8548. case VALUE_TYPE_F64:
  8549. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8550. sizeof(float64));
  8551. ctx->const_cell_num += 2;
  8552. /* The const buf will be reversed, we use the second cell */
  8553. /* of the i64/f64 const so the final offset is correct */
  8554. operand_offset++;
  8555. break;
  8556. case VALUE_TYPE_I64:
  8557. c->value.i64 = *(int64 *)value;
  8558. ctx->const_cell_num += 2;
  8559. operand_offset++;
  8560. break;
  8561. case VALUE_TYPE_F32:
  8562. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8563. sizeof(float32));
  8564. ctx->const_cell_num++;
  8565. break;
  8566. case VALUE_TYPE_I32:
  8567. c->value.i32 = *(int32 *)value;
  8568. ctx->const_cell_num++;
  8569. break;
  8570. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8571. case VALUE_TYPE_EXTERNREF:
  8572. case VALUE_TYPE_FUNCREF:
  8573. c->value.i32 = *(int32 *)value;
  8574. ctx->const_cell_num++;
  8575. break;
  8576. #endif
  8577. default:
  8578. break;
  8579. }
  8580. c->slot_index = operand_offset;
  8581. ctx->num_const++;
  8582. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8583. (int64)c->value.i64);
  8584. }
  8585. /* use negative index for const */
  8586. operand_offset = -(operand_offset + 1);
  8587. *offset = operand_offset;
  8588. return true;
  8589. fail:
  8590. return false;
  8591. }
  8592. /*
  8593. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8594. -- Mostly used for the binary / compare operation
  8595. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8596. -- Mostly used in block / control instructions
  8597. The POP will always emit the offset on the top of the frame_offset stack
  8598. PUSH can be used in two ways:
  8599. 1. directly PUSH:
  8600. PUSH_XXX();
  8601. will allocate a dynamic space and emit
  8602. 2. silent PUSH:
  8603. operand_offset = xxx; disable_emit = true;
  8604. PUSH_XXX();
  8605. only push the frame_offset stack, no emit
  8606. */
  8607. #define TEMPLATE_PUSH(Type) \
  8608. do { \
  8609. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8610. disable_emit, operand_offset, \
  8611. error_buf, error_buf_size)) \
  8612. goto fail; \
  8613. } while (0)
  8614. #define TEMPLATE_PUSH_REF(Type) \
  8615. do { \
  8616. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8617. operand_offset, error_buf, \
  8618. error_buf_size)) \
  8619. goto fail; \
  8620. } while (0)
  8621. #define TEMPLATE_POP(Type) \
  8622. do { \
  8623. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8624. error_buf, error_buf_size)) \
  8625. goto fail; \
  8626. } while (0)
  8627. #define TEMPLATE_POP_REF(Type) \
  8628. do { \
  8629. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8630. error_buf_size)) \
  8631. goto fail; \
  8632. } while (0)
  8633. #define PUSH_OFFSET_TYPE(type) \
  8634. do { \
  8635. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8636. operand_offset, error_buf, \
  8637. error_buf_size))) \
  8638. goto fail; \
  8639. } while (0)
  8640. #define POP_OFFSET_TYPE(type) \
  8641. do { \
  8642. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8643. error_buf_size))) \
  8644. goto fail; \
  8645. } while (0)
  8646. #define POP_AND_PUSH(type_pop, type_push) \
  8647. do { \
  8648. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8649. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8650. operand_offset, error_buf, error_buf_size))) \
  8651. goto fail; \
  8652. } while (0)
  8653. /* type of POPs should be the same */
  8654. #define POP2_AND_PUSH(type_pop, type_push) \
  8655. do { \
  8656. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8657. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8658. operand_offset, error_buf, error_buf_size))) \
  8659. goto fail; \
  8660. } while (0)
  8661. #else /* WASM_ENABLE_FAST_INTERP */
  8662. #define TEMPLATE_PUSH(Type) \
  8663. do { \
  8664. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8665. error_buf, error_buf_size))) \
  8666. goto fail; \
  8667. } while (0)
  8668. #define TEMPLATE_PUSH_REF(Type) \
  8669. do { \
  8670. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8671. error_buf_size))) \
  8672. goto fail; \
  8673. } while (0)
  8674. #define TEMPLATE_POP(Type) \
  8675. do { \
  8676. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8677. error_buf, error_buf_size))) \
  8678. goto fail; \
  8679. } while (0)
  8680. #define TEMPLATE_POP_REF(Type) \
  8681. do { \
  8682. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8683. error_buf_size))) \
  8684. goto fail; \
  8685. } while (0)
  8686. #define POP_AND_PUSH(type_pop, type_push) \
  8687. do { \
  8688. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8689. type_pop, error_buf, \
  8690. error_buf_size))) \
  8691. goto fail; \
  8692. } while (0)
  8693. /* type of POPs should be the same */
  8694. #define POP2_AND_PUSH(type_pop, type_push) \
  8695. do { \
  8696. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8697. type_pop, error_buf, \
  8698. error_buf_size))) \
  8699. goto fail; \
  8700. } while (0)
  8701. #endif /* WASM_ENABLE_FAST_INTERP */
  8702. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8703. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8704. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8705. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8706. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8707. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8708. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8709. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8710. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8711. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8712. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8713. #define POP_I32() TEMPLATE_POP(I32)
  8714. #define POP_F32() TEMPLATE_POP(F32)
  8715. #define POP_I64() TEMPLATE_POP(I64)
  8716. #define POP_F64() TEMPLATE_POP(F64)
  8717. #define POP_V128() TEMPLATE_POP(V128)
  8718. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8719. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8720. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8721. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8722. #if WASM_ENABLE_FAST_INTERP != 0
  8723. static bool
  8724. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8725. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8726. {
  8727. int16 operand_offset = 0;
  8728. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8729. : loader_ctx->frame_csp;
  8730. BlockType *block_type = &block->block_type;
  8731. uint8 *return_types = NULL;
  8732. #if WASM_ENABLE_GC != 0
  8733. WASMRefTypeMap *reftype_maps = NULL;
  8734. uint32 reftype_map_count;
  8735. #endif
  8736. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8737. int32 i = 0;
  8738. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8739. *frame_offset_org = NULL;
  8740. #if WASM_ENABLE_GC == 0
  8741. return_count = block_type_get_result_types(block_type, &return_types);
  8742. #else
  8743. return_count = block_type_get_result_types(
  8744. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8745. #endif
  8746. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8747. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8748. if (return_count == 1) {
  8749. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8750. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8751. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8752. /* insert op_copy before else opcode */
  8753. if (opcode == WASM_OP_ELSE)
  8754. skip_label();
  8755. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8756. : EXT_OP_COPY_STACK_TOP_I64);
  8757. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8758. emit_operand(loader_ctx, block->dynamic_offset);
  8759. if (opcode == WASM_OP_ELSE) {
  8760. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8761. }
  8762. else {
  8763. loader_ctx->frame_offset -= cell;
  8764. loader_ctx->dynamic_offset = block->dynamic_offset;
  8765. PUSH_OFFSET_TYPE(return_types[0]);
  8766. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8767. }
  8768. if (opcode == WASM_OP_ELSE)
  8769. emit_label(opcode);
  8770. }
  8771. return true;
  8772. }
  8773. /* Copy stack top values to block's results which are in dynamic space.
  8774. * The instruction format:
  8775. * Part a: values count
  8776. * Part b: all values total cell num
  8777. * Part c: each value's cell_num, src offset and dst offset
  8778. * Part d: each value's src offset and dst offset
  8779. * Part e: each value's dst offset
  8780. */
  8781. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8782. dynamic_offset = dynamic_offset_org =
  8783. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8784. /* First traversal to get the count of values needed to be copied. */
  8785. for (i = (int32)return_count - 1; i >= 0; i--) {
  8786. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8787. frame_offset -= cells;
  8788. dynamic_offset -= cells;
  8789. if (dynamic_offset != *frame_offset) {
  8790. value_count++;
  8791. total_cel_num += cells;
  8792. }
  8793. }
  8794. if (value_count) {
  8795. uint32 j = 0;
  8796. uint8 *emit_data = NULL, *cells = NULL;
  8797. int16 *src_offsets = NULL;
  8798. uint16 *dst_offsets = NULL;
  8799. uint64 size =
  8800. (uint64)value_count
  8801. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8802. /* Allocate memory for the emit data */
  8803. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8804. return false;
  8805. cells = emit_data;
  8806. src_offsets = (int16 *)(cells + value_count);
  8807. dst_offsets = (uint16 *)(src_offsets + value_count);
  8808. /* insert op_copy before else opcode */
  8809. if (opcode == WASM_OP_ELSE)
  8810. skip_label();
  8811. emit_label(EXT_OP_COPY_STACK_VALUES);
  8812. /* Part a) */
  8813. emit_uint32(loader_ctx, value_count);
  8814. /* Part b) */
  8815. emit_uint32(loader_ctx, total_cel_num);
  8816. /* Second traversal to get each value's cell num, src offset and dst
  8817. * offset. */
  8818. frame_offset = frame_offset_org;
  8819. dynamic_offset = dynamic_offset_org;
  8820. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8821. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8822. frame_offset -= cell;
  8823. dynamic_offset -= cell;
  8824. if (dynamic_offset != *frame_offset) {
  8825. /* cell num */
  8826. cells[j] = cell;
  8827. /* src offset */
  8828. src_offsets[j] = *frame_offset;
  8829. /* dst offset */
  8830. dst_offsets[j] = dynamic_offset;
  8831. j++;
  8832. }
  8833. if (opcode == WASM_OP_ELSE) {
  8834. *frame_offset = dynamic_offset;
  8835. }
  8836. else {
  8837. loader_ctx->frame_offset = frame_offset;
  8838. loader_ctx->dynamic_offset = dynamic_offset;
  8839. PUSH_OFFSET_TYPE(return_types[i]);
  8840. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8841. loader_ctx->frame_offset = frame_offset_org;
  8842. loader_ctx->dynamic_offset = dynamic_offset_org;
  8843. }
  8844. }
  8845. bh_assert(j == value_count);
  8846. /* Emit the cells, src_offsets and dst_offsets */
  8847. for (j = 0; j < value_count; j++)
  8848. emit_byte(loader_ctx, cells[j]);
  8849. for (j = 0; j < value_count; j++)
  8850. emit_operand(loader_ctx, src_offsets[j]);
  8851. for (j = 0; j < value_count; j++)
  8852. emit_operand(loader_ctx, dst_offsets[j]);
  8853. if (opcode == WASM_OP_ELSE)
  8854. emit_label(opcode);
  8855. wasm_runtime_free(emit_data);
  8856. }
  8857. return true;
  8858. fail:
  8859. return false;
  8860. }
  8861. #endif /* WASM_ENABLE_FAST_INTERP */
  8862. #define RESERVE_BLOCK_RET() \
  8863. do { \
  8864. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8865. error_buf_size)) \
  8866. goto fail; \
  8867. } while (0)
  8868. #define PUSH_TYPE(type) \
  8869. do { \
  8870. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8871. error_buf_size))) \
  8872. goto fail; \
  8873. } while (0)
  8874. #define POP_TYPE(type) \
  8875. do { \
  8876. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8877. error_buf_size))) \
  8878. goto fail; \
  8879. } while (0)
  8880. #if WASM_ENABLE_GC == 0
  8881. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8882. do { \
  8883. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8884. _start_addr, error_buf, \
  8885. error_buf_size)) \
  8886. goto fail; \
  8887. } while (0)
  8888. #define POP_CSP() \
  8889. do { \
  8890. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8891. goto fail; \
  8892. } while (0)
  8893. #else
  8894. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8895. do { \
  8896. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8897. _start_addr, error_buf, \
  8898. error_buf_size)) \
  8899. goto fail; \
  8900. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8901. error_buf, error_buf_size)) { \
  8902. goto fail; \
  8903. } \
  8904. } while (0)
  8905. #define POP_CSP() \
  8906. do { \
  8907. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8908. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8909. goto fail; \
  8910. } while (0)
  8911. #endif /* end of WASM_ENABLE_GC == 0 */
  8912. #if WASM_ENABLE_GC == 0
  8913. #define GET_LOCAL_REFTYPE() (void)0
  8914. #else
  8915. #define GET_LOCAL_REFTYPE() \
  8916. do { \
  8917. if (wasm_is_type_multi_byte_type(local_type)) { \
  8918. WASMRefType *_ref_type; \
  8919. if (local_idx < param_count) \
  8920. _ref_type = wasm_reftype_map_find( \
  8921. param_reftype_maps, param_reftype_map_count, local_idx); \
  8922. else \
  8923. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8924. local_reftype_map_count, \
  8925. local_idx - param_count); \
  8926. bh_assert(_ref_type); \
  8927. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8928. wasm_reftype_struct_size(_ref_type)); \
  8929. } \
  8930. } while (0)
  8931. #endif /* end of WASM_ENABLE_GC == 0 */
  8932. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8933. do { \
  8934. read_leb_uint32(p, p_end, local_idx); \
  8935. if (local_idx >= param_count + local_count) { \
  8936. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8937. goto fail; \
  8938. } \
  8939. local_type = local_idx < param_count \
  8940. ? param_types[local_idx] \
  8941. : local_types[local_idx - param_count]; \
  8942. local_offset = local_offsets[local_idx]; \
  8943. GET_LOCAL_REFTYPE(); \
  8944. } while (0)
  8945. static bool
  8946. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8947. {
  8948. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8949. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8950. return false;
  8951. }
  8952. return true;
  8953. }
  8954. #define CHECK_MEMORY() \
  8955. do { \
  8956. if (!check_memory(module, error_buf, error_buf_size)) \
  8957. goto fail; \
  8958. } while (0)
  8959. static bool
  8960. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8961. uint32 error_buf_size)
  8962. {
  8963. uint8 mem_access_aligns[] = {
  8964. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8965. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8966. };
  8967. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8968. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8969. set_error_buf(error_buf, error_buf_size,
  8970. "alignment must not be larger than natural");
  8971. return false;
  8972. }
  8973. return true;
  8974. }
  8975. #if WASM_ENABLE_SIMD != 0
  8976. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8977. static bool
  8978. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8979. uint32 error_buf_size)
  8980. {
  8981. uint8 mem_access_aligns[] = {
  8982. 4, /* load */
  8983. 3, 3, 3, 3, 3, 3, /* load and extend */
  8984. 0, 1, 2, 3, /* load and splat */
  8985. 4, /* store */
  8986. };
  8987. uint8 mem_access_aligns_load_lane[] = {
  8988. 0, 1, 2, 3, /* load lane */
  8989. 0, 1, 2, 3, /* store lane */
  8990. 2, 3 /* store zero */
  8991. };
  8992. if (!((opcode <= SIMD_v128_store)
  8993. || (SIMD_v128_load8_lane <= opcode
  8994. && opcode <= SIMD_v128_load64_zero))) {
  8995. set_error_buf(error_buf, error_buf_size,
  8996. "the opcode doesn't include memarg");
  8997. return false;
  8998. }
  8999. if ((opcode <= SIMD_v128_store
  9000. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9001. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9002. && align > mem_access_aligns_load_lane[opcode
  9003. - SIMD_v128_load8_lane])) {
  9004. set_error_buf(error_buf, error_buf_size,
  9005. "alignment must not be larger than natural");
  9006. return false;
  9007. }
  9008. return true;
  9009. }
  9010. static bool
  9011. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9012. uint32 error_buf_size)
  9013. {
  9014. switch (opcode) {
  9015. case SIMD_i8x16_extract_lane_s:
  9016. case SIMD_i8x16_extract_lane_u:
  9017. case SIMD_i8x16_replace_lane:
  9018. if (lane >= 16) {
  9019. goto fail;
  9020. }
  9021. break;
  9022. case SIMD_i16x8_extract_lane_s:
  9023. case SIMD_i16x8_extract_lane_u:
  9024. case SIMD_i16x8_replace_lane:
  9025. if (lane >= 8) {
  9026. goto fail;
  9027. }
  9028. break;
  9029. case SIMD_i32x4_extract_lane:
  9030. case SIMD_i32x4_replace_lane:
  9031. case SIMD_f32x4_extract_lane:
  9032. case SIMD_f32x4_replace_lane:
  9033. if (lane >= 4) {
  9034. goto fail;
  9035. }
  9036. break;
  9037. case SIMD_i64x2_extract_lane:
  9038. case SIMD_i64x2_replace_lane:
  9039. case SIMD_f64x2_extract_lane:
  9040. case SIMD_f64x2_replace_lane:
  9041. if (lane >= 2) {
  9042. goto fail;
  9043. }
  9044. break;
  9045. case SIMD_v128_load8_lane:
  9046. case SIMD_v128_load16_lane:
  9047. case SIMD_v128_load32_lane:
  9048. case SIMD_v128_load64_lane:
  9049. case SIMD_v128_store8_lane:
  9050. case SIMD_v128_store16_lane:
  9051. case SIMD_v128_store32_lane:
  9052. case SIMD_v128_store64_lane:
  9053. case SIMD_v128_load32_zero:
  9054. case SIMD_v128_load64_zero:
  9055. {
  9056. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9057. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9058. goto fail;
  9059. }
  9060. break;
  9061. }
  9062. default:
  9063. goto fail;
  9064. }
  9065. return true;
  9066. fail:
  9067. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9068. return false;
  9069. }
  9070. static bool
  9071. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9072. {
  9073. uint8 i;
  9074. for (i = 0; i != 16; ++i) {
  9075. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9076. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9077. return false;
  9078. }
  9079. }
  9080. return true;
  9081. }
  9082. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9083. #endif /* end of WASM_ENABLE_SIMD */
  9084. #if WASM_ENABLE_SHARED_MEMORY != 0
  9085. static bool
  9086. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9087. uint32 error_buf_size)
  9088. {
  9089. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9090. uint8 mem_access_aligns[] = {
  9091. 2, 3, 0, 1, 0, 1, 2,
  9092. };
  9093. uint8 expect;
  9094. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9095. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9096. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9097. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9098. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9099. }
  9100. else {
  9101. /* 7 opcodes in every group */
  9102. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9103. }
  9104. if (align != expect) {
  9105. set_error_buf(error_buf, error_buf_size,
  9106. "alignment isn't equal to natural");
  9107. return false;
  9108. }
  9109. return true;
  9110. }
  9111. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9112. static bool
  9113. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9114. char *error_buf, uint32 error_buf_size)
  9115. {
  9116. BranchBlock *target_block, *cur_block;
  9117. BlockType *target_block_type;
  9118. uint8 type, *types = NULL, *frame_ref;
  9119. uint32 arity = 0;
  9120. int32 i, available_stack_cell;
  9121. uint16 cell_num;
  9122. #if WASM_ENABLE_GC != 0
  9123. WASMRefTypeMap *frame_reftype_map;
  9124. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9125. WASMRefType *ref_type;
  9126. uint32 reftype_map_count = 0;
  9127. int32 available_reftype_map;
  9128. bool is_type_multi_byte;
  9129. #endif
  9130. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9131. uint8 *frame_ref_after_popped = NULL;
  9132. uint8 frame_ref_tmp[4] = { 0 };
  9133. uint8 *frame_ref_buf = frame_ref_tmp;
  9134. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9135. #if WASM_ENABLE_GC != 0
  9136. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9137. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9138. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9139. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9140. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9141. #endif
  9142. #if WASM_ENABLE_FAST_INTERP != 0
  9143. int16 *frame_offset_old = loader_ctx->frame_offset;
  9144. int16 *frame_offset_after_popped = NULL;
  9145. int16 frame_offset_tmp[4] = { 0 };
  9146. int16 *frame_offset_buf = frame_offset_tmp;
  9147. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9148. #endif
  9149. bool ret = false;
  9150. bh_assert(loader_ctx->csp_num > 0);
  9151. if (loader_ctx->csp_num - 1 < depth) {
  9152. set_error_buf(error_buf, error_buf_size,
  9153. "unknown label, "
  9154. "unexpected end of section or function");
  9155. return false;
  9156. }
  9157. cur_block = loader_ctx->frame_csp - 1;
  9158. target_block = loader_ctx->frame_csp - (depth + 1);
  9159. target_block_type = &target_block->block_type;
  9160. frame_ref = loader_ctx->frame_ref;
  9161. #if WASM_ENABLE_GC != 0
  9162. frame_reftype_map = loader_ctx->frame_reftype_map;
  9163. #endif
  9164. /* Note: loop's arity is different from if and block. loop's arity is
  9165. * its parameter count while if and block arity is result count.
  9166. */
  9167. #if WASM_ENABLE_GC == 0
  9168. if (target_block->label_type == LABEL_TYPE_LOOP)
  9169. arity = block_type_get_param_types(target_block_type, &types);
  9170. else
  9171. arity = block_type_get_result_types(target_block_type, &types);
  9172. #else
  9173. if (target_block->label_type == LABEL_TYPE_LOOP)
  9174. arity = block_type_get_param_types(target_block_type, &types,
  9175. &reftype_maps, &reftype_map_count);
  9176. else
  9177. arity = block_type_get_result_types(target_block_type, &types,
  9178. &reftype_maps, &reftype_map_count);
  9179. #endif
  9180. /* If the stack is in polymorphic state, just clear the stack
  9181. * and then re-push the values to make the stack top values
  9182. * match block type. */
  9183. if (cur_block->is_stack_polymorphic) {
  9184. #if WASM_ENABLE_GC != 0
  9185. int32 j = reftype_map_count - 1;
  9186. #endif
  9187. for (i = (int32)arity - 1; i >= 0; i--) {
  9188. #if WASM_ENABLE_GC != 0
  9189. if (wasm_is_type_multi_byte_type(types[i])) {
  9190. bh_assert(reftype_maps[j].index == i);
  9191. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9192. reftype_maps[j].ref_type,
  9193. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9194. j--;
  9195. }
  9196. #endif
  9197. #if WASM_ENABLE_FAST_INTERP != 0
  9198. POP_OFFSET_TYPE(types[i]);
  9199. #endif
  9200. POP_TYPE(types[i]);
  9201. }
  9202. /* Backup stack data since it may be changed in the below
  9203. push operations, and the stack data may be used when
  9204. checking other target blocks of opcode br_table */
  9205. if (opcode == WASM_OP_BR_TABLE) {
  9206. uint64 total_size;
  9207. frame_ref_after_popped = loader_ctx->frame_ref;
  9208. total_size = (uint64)sizeof(uint8)
  9209. * (frame_ref_old - frame_ref_after_popped);
  9210. if (total_size > sizeof(frame_ref_tmp)
  9211. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9212. error_buf_size))) {
  9213. goto fail;
  9214. }
  9215. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9216. frame_ref_after_popped, (uint32)total_size);
  9217. #if WASM_ENABLE_GC != 0
  9218. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9219. total_size =
  9220. (uint64)sizeof(WASMRefTypeMap)
  9221. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9222. if (total_size > sizeof(frame_reftype_map_tmp)
  9223. && !(frame_reftype_map_buf = loader_malloc(
  9224. total_size, error_buf, error_buf_size))) {
  9225. goto fail;
  9226. }
  9227. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9228. frame_reftype_map_after_popped, (uint32)total_size);
  9229. #endif
  9230. #if WASM_ENABLE_FAST_INTERP != 0
  9231. frame_offset_after_popped = loader_ctx->frame_offset;
  9232. total_size = (uint64)sizeof(int16)
  9233. * (frame_offset_old - frame_offset_after_popped);
  9234. if (total_size > sizeof(frame_offset_tmp)
  9235. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9236. error_buf_size))) {
  9237. goto fail;
  9238. }
  9239. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9240. frame_offset_after_popped, (uint32)total_size);
  9241. #endif
  9242. }
  9243. #if WASM_ENABLE_GC != 0
  9244. j = 0;
  9245. #endif
  9246. for (i = 0; i < (int32)arity; i++) {
  9247. #if WASM_ENABLE_GC != 0
  9248. if (wasm_is_type_multi_byte_type(types[i])) {
  9249. bh_assert(reftype_maps[j].index == i);
  9250. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9251. reftype_maps[j].ref_type,
  9252. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9253. j++;
  9254. }
  9255. #endif
  9256. #if WASM_ENABLE_FAST_INTERP != 0
  9257. bool disable_emit = true;
  9258. int16 operand_offset = 0;
  9259. PUSH_OFFSET_TYPE(types[i]);
  9260. #endif
  9261. PUSH_TYPE(types[i]);
  9262. }
  9263. #if WASM_ENABLE_FAST_INTERP != 0
  9264. emit_br_info(target_block, opcode == WASM_OP_BR);
  9265. #endif
  9266. /* Restore the stack data, note that frame_ref_bottom,
  9267. frame_reftype_map_bottom, frame_offset_bottom may be
  9268. re-allocated in the above push operations */
  9269. if (opcode == WASM_OP_BR_TABLE) {
  9270. uint32 total_size;
  9271. /* The stack operand num should not be smaller than before
  9272. after pop and push operations */
  9273. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9274. loader_ctx->stack_cell_num = stack_cell_num_old;
  9275. loader_ctx->frame_ref =
  9276. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9277. total_size = (uint32)(sizeof(uint8)
  9278. * (frame_ref_old - frame_ref_after_popped));
  9279. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9280. frame_ref_buf, total_size);
  9281. #if WASM_ENABLE_GC != 0
  9282. /* The stack operand num should not be smaller than before
  9283. after pop and push operations */
  9284. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9285. loader_ctx->reftype_map_num = reftype_map_num_old;
  9286. loader_ctx->frame_reftype_map =
  9287. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9288. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9289. * (frame_reftype_map_old
  9290. - frame_reftype_map_after_popped));
  9291. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9292. total_size, frame_reftype_map_buf, total_size);
  9293. #endif
  9294. #if WASM_ENABLE_FAST_INTERP != 0
  9295. loader_ctx->frame_offset =
  9296. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9297. total_size =
  9298. (uint32)(sizeof(int16)
  9299. * (frame_offset_old - frame_offset_after_popped));
  9300. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9301. total_size, frame_offset_buf, total_size);
  9302. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9303. #endif
  9304. }
  9305. ret = true;
  9306. goto cleanup_and_return;
  9307. }
  9308. available_stack_cell =
  9309. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9310. #if WASM_ENABLE_GC != 0
  9311. available_reftype_map =
  9312. (int32)(loader_ctx->reftype_map_num
  9313. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9314. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9315. #endif
  9316. /* Check stack top values match target block type */
  9317. for (i = (int32)arity - 1; i >= 0; i--) {
  9318. type = types[i];
  9319. #if WASM_ENABLE_GC != 0
  9320. ref_type = NULL;
  9321. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9322. if (is_type_multi_byte) {
  9323. bh_assert(reftype_map);
  9324. ref_type = reftype_map->ref_type;
  9325. }
  9326. #endif
  9327. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9328. break;
  9329. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9330. #if WASM_ENABLE_GC != 0
  9331. frame_reftype_map, available_reftype_map,
  9332. #endif
  9333. type,
  9334. #if WASM_ENABLE_GC != 0
  9335. ref_type,
  9336. #endif
  9337. error_buf, error_buf_size)) {
  9338. goto fail;
  9339. }
  9340. cell_num = wasm_value_type_cell_num(types[i]);
  9341. frame_ref -= cell_num;
  9342. available_stack_cell -= cell_num;
  9343. #if WASM_ENABLE_GC != 0
  9344. if (is_type_multi_byte) {
  9345. frame_reftype_map--;
  9346. available_reftype_map--;
  9347. reftype_map--;
  9348. }
  9349. #endif
  9350. }
  9351. #if WASM_ENABLE_FAST_INTERP != 0
  9352. emit_br_info(target_block, opcode == WASM_OP_BR);
  9353. #endif
  9354. ret = true;
  9355. cleanup_and_return:
  9356. fail:
  9357. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9358. wasm_runtime_free(frame_ref_buf);
  9359. #if WASM_ENABLE_GC != 0
  9360. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9361. wasm_runtime_free(frame_reftype_map_buf);
  9362. #endif
  9363. #if WASM_ENABLE_FAST_INTERP != 0
  9364. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9365. wasm_runtime_free(frame_offset_buf);
  9366. #endif
  9367. return ret;
  9368. }
  9369. static BranchBlock *
  9370. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9371. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9372. {
  9373. uint8 *p = *p_buf, *p_end = buf_end;
  9374. BranchBlock *frame_csp_tmp;
  9375. uint32 depth;
  9376. read_leb_uint32(p, p_end, depth);
  9377. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9378. error_buf_size)) {
  9379. goto fail;
  9380. }
  9381. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9382. *p_buf = p;
  9383. return frame_csp_tmp;
  9384. fail:
  9385. return NULL;
  9386. }
  9387. #if WASM_ENABLE_EXCE_HANDLING != 0
  9388. static BranchBlock *
  9389. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9390. uint8 *buf_end, char *error_buf,
  9391. uint32 error_buf_size)
  9392. {
  9393. uint8 *p = *p_buf, *p_end = buf_end;
  9394. BranchBlock *frame_csp_tmp;
  9395. uint32 depth;
  9396. read_leb_uint32(p, p_end, depth);
  9397. /*
  9398. * Note: "delegate 0" means the surrounding block, not the
  9399. * try-delegate block itself.
  9400. *
  9401. * Note: the caller hasn't popped the try-delegate frame yet.
  9402. */
  9403. bh_assert(loader_ctx->csp_num > 0);
  9404. if (loader_ctx->csp_num - 1 <= depth) {
  9405. #if WASM_ENABLE_SPEC_TEST == 0
  9406. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9407. #else
  9408. set_error_buf(error_buf, error_buf_size, "unknown label");
  9409. #endif
  9410. goto fail;
  9411. }
  9412. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9413. #if WASM_ENABLE_FAST_INTERP != 0
  9414. emit_br_info(frame_csp_tmp, false);
  9415. #endif
  9416. *p_buf = p;
  9417. return frame_csp_tmp;
  9418. fail:
  9419. return NULL;
  9420. }
  9421. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9422. static bool
  9423. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9424. char *error_buf, uint32 error_buf_size)
  9425. {
  9426. BlockType *block_type = &block->block_type;
  9427. uint8 *return_types = NULL;
  9428. uint32 return_count = 0;
  9429. int32 available_stack_cell, return_cell_num, i;
  9430. uint8 *frame_ref = NULL;
  9431. #if WASM_ENABLE_GC != 0
  9432. WASMRefTypeMap *frame_reftype_map;
  9433. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9434. WASMRefType *ref_type;
  9435. uint32 param_count, return_reftype_map_count = 0;
  9436. int32 available_reftype_map =
  9437. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9438. #endif
  9439. available_stack_cell =
  9440. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9441. #if WASM_ENABLE_GC == 0
  9442. return_count = block_type_get_result_types(block_type, &return_types);
  9443. #else
  9444. return_count = block_type_get_result_types(block_type, &return_types,
  9445. &return_reftype_maps,
  9446. &return_reftype_map_count);
  9447. param_count =
  9448. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9449. (void)param_count;
  9450. #endif
  9451. return_cell_num =
  9452. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9453. /* If the stack is in polymorphic state, just clear the stack
  9454. * and then re-push the values to make the stack top values
  9455. * match block type. */
  9456. if (block->is_stack_polymorphic) {
  9457. #if WASM_ENABLE_GC != 0
  9458. int32 j = return_reftype_map_count - 1;
  9459. #endif
  9460. for (i = (int32)return_count - 1; i >= 0; i--) {
  9461. #if WASM_ENABLE_GC != 0
  9462. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9463. bh_assert(return_reftype_maps[j].index == i + param_count);
  9464. bh_memcpy_s(
  9465. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9466. return_reftype_maps[j].ref_type,
  9467. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9468. j--;
  9469. }
  9470. #endif
  9471. #if WASM_ENABLE_FAST_INTERP != 0
  9472. POP_OFFSET_TYPE(return_types[i]);
  9473. #endif
  9474. POP_TYPE(return_types[i]);
  9475. }
  9476. /* Check stack is empty */
  9477. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9478. set_error_buf(
  9479. error_buf, error_buf_size,
  9480. "type mismatch: stack size does not match block type");
  9481. goto fail;
  9482. }
  9483. #if WASM_ENABLE_GC != 0
  9484. j = 0;
  9485. #endif
  9486. for (i = 0; i < (int32)return_count; i++) {
  9487. #if WASM_ENABLE_GC != 0
  9488. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9489. bh_assert(return_reftype_maps[j].index == i + param_count);
  9490. bh_memcpy_s(
  9491. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9492. return_reftype_maps[j].ref_type,
  9493. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9494. j++;
  9495. }
  9496. #endif
  9497. #if WASM_ENABLE_FAST_INTERP != 0
  9498. bool disable_emit = true;
  9499. int16 operand_offset = 0;
  9500. PUSH_OFFSET_TYPE(return_types[i]);
  9501. #endif
  9502. PUSH_TYPE(return_types[i]);
  9503. }
  9504. return true;
  9505. }
  9506. if (available_stack_cell != return_cell_num) {
  9507. #if WASM_ENABLE_EXCE_HANDLING != 0
  9508. /* testspec: this error message format is expected by try_catch.wast */
  9509. snprintf(
  9510. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9511. block->label_type == LABEL_TYPE_TRY
  9512. || (block->label_type == LABEL_TYPE_CATCH
  9513. && return_cell_num > 0)
  9514. ? "instruction"
  9515. : "block",
  9516. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9517. " but stack has ",
  9518. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9519. : "");
  9520. goto fail;
  9521. #else
  9522. set_error_buf(error_buf, error_buf_size,
  9523. "type mismatch: stack size does not match block type");
  9524. goto fail;
  9525. #endif
  9526. }
  9527. /* Check stack values match return types */
  9528. frame_ref = loader_ctx->frame_ref;
  9529. #if WASM_ENABLE_GC != 0
  9530. frame_reftype_map = loader_ctx->frame_reftype_map;
  9531. return_reftype_map =
  9532. return_reftype_map_count
  9533. ? return_reftype_maps + return_reftype_map_count - 1
  9534. : NULL;
  9535. #endif
  9536. for (i = (int32)return_count - 1; i >= 0; i--) {
  9537. uint8 type = return_types[i];
  9538. #if WASM_ENABLE_GC != 0
  9539. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9540. ref_type = NULL;
  9541. if (is_type_multi_byte) {
  9542. bh_assert(return_reftype_map);
  9543. ref_type = return_reftype_map->ref_type;
  9544. }
  9545. #endif
  9546. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9547. #if WASM_ENABLE_GC != 0
  9548. frame_reftype_map, available_reftype_map,
  9549. #endif
  9550. type,
  9551. #if WASM_ENABLE_GC != 0
  9552. ref_type,
  9553. #endif
  9554. error_buf, error_buf_size))
  9555. return false;
  9556. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9557. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9558. #if WASM_ENABLE_GC != 0
  9559. if (is_type_multi_byte) {
  9560. frame_reftype_map--;
  9561. available_reftype_map--;
  9562. return_reftype_map--;
  9563. }
  9564. #endif
  9565. }
  9566. return true;
  9567. fail:
  9568. return false;
  9569. }
  9570. #if WASM_ENABLE_FAST_INTERP != 0
  9571. /* Copy parameters to dynamic space.
  9572. * 1) POP original parameter out;
  9573. * 2) Push and copy original values to dynamic space.
  9574. * The copy instruction format:
  9575. * Part a: param count
  9576. * Part b: all param total cell num
  9577. * Part c: each param's cell_num, src offset and dst offset
  9578. * Part d: each param's src offset
  9579. * Part e: each param's dst offset
  9580. */
  9581. static bool
  9582. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9583. uint32 error_buf_size)
  9584. {
  9585. bool ret = false;
  9586. int16 *frame_offset = NULL;
  9587. uint8 *cells = NULL, cell;
  9588. int16 *src_offsets = NULL;
  9589. uint8 *emit_data = NULL;
  9590. uint32 i;
  9591. BranchBlock *block = loader_ctx->frame_csp - 1;
  9592. BlockType *block_type = &block->block_type;
  9593. WASMFuncType *wasm_type = block_type->u.type;
  9594. uint32 param_count = block_type->u.type->param_count;
  9595. int16 condition_offset = 0;
  9596. bool disable_emit = false;
  9597. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9598. int16 operand_offset = 0;
  9599. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9600. bh_assert(size > 0);
  9601. /* For if block, we also need copy the condition operand offset. */
  9602. if (is_if_block)
  9603. size += sizeof(*cells) + sizeof(*src_offsets);
  9604. /* Allocate memory for the emit data */
  9605. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9606. return false;
  9607. cells = emit_data;
  9608. src_offsets = (int16 *)(cells + param_count);
  9609. if (is_if_block)
  9610. condition_offset = *loader_ctx->frame_offset;
  9611. /* POP original parameter out */
  9612. for (i = 0; i < param_count; i++) {
  9613. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9614. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9615. }
  9616. frame_offset = loader_ctx->frame_offset;
  9617. /* Get each param's cell num and src offset */
  9618. for (i = 0; i < param_count; i++) {
  9619. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9620. cells[i] = cell;
  9621. src_offsets[i] = *frame_offset;
  9622. frame_offset += cell;
  9623. }
  9624. /* emit copy instruction */
  9625. emit_label(EXT_OP_COPY_STACK_VALUES);
  9626. /* Part a) */
  9627. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9628. /* Part b) */
  9629. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9630. : wasm_type->param_cell_num);
  9631. /* Part c) */
  9632. for (i = 0; i < param_count; i++)
  9633. emit_byte(loader_ctx, cells[i]);
  9634. if (is_if_block)
  9635. emit_byte(loader_ctx, 1);
  9636. /* Part d) */
  9637. for (i = 0; i < param_count; i++)
  9638. emit_operand(loader_ctx, src_offsets[i]);
  9639. if (is_if_block)
  9640. emit_operand(loader_ctx, condition_offset);
  9641. /* Since the start offset to save the block's params and
  9642. * the start offset to save the block's results may be
  9643. * different, we remember the dynamic offset for loop block
  9644. * so that we can use it to copy the stack operands to the
  9645. * loop block's params in wasm_loader_emit_br_info. */
  9646. if (block->label_type == LABEL_TYPE_LOOP)
  9647. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9648. /* Part e) */
  9649. /* Push to dynamic space. The push will emit the dst offset. */
  9650. for (i = 0; i < param_count; i++)
  9651. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9652. if (is_if_block)
  9653. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9654. ret = true;
  9655. fail:
  9656. /* Free the emit data */
  9657. wasm_runtime_free(emit_data);
  9658. return ret;
  9659. }
  9660. #endif
  9661. #if WASM_ENABLE_GC == 0
  9662. #define RESET_REFTYPE_MAP_STACK() (void)0
  9663. #else
  9664. #define RESET_REFTYPE_MAP_STACK() \
  9665. do { \
  9666. loader_ctx->reftype_map_num = \
  9667. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9668. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9669. + loader_ctx->reftype_map_num; \
  9670. } while (0)
  9671. #endif
  9672. /* reset the stack to the state of before entering the last block */
  9673. #if WASM_ENABLE_FAST_INTERP != 0
  9674. #define RESET_STACK() \
  9675. do { \
  9676. loader_ctx->stack_cell_num = \
  9677. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9678. loader_ctx->frame_ref = \
  9679. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9680. loader_ctx->frame_offset = \
  9681. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9682. RESET_REFTYPE_MAP_STACK(); \
  9683. } while (0)
  9684. #else
  9685. #define RESET_STACK() \
  9686. do { \
  9687. loader_ctx->stack_cell_num = \
  9688. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9689. loader_ctx->frame_ref = \
  9690. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9691. RESET_REFTYPE_MAP_STACK(); \
  9692. } while (0)
  9693. #endif
  9694. /* set current block's stack polymorphic state */
  9695. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9696. do { \
  9697. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9698. _cur_block->is_stack_polymorphic = flag; \
  9699. } while (0)
  9700. #define BLOCK_HAS_PARAM(block_type) \
  9701. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9702. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9703. do { \
  9704. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9705. error_buf_size))) { \
  9706. goto fail; \
  9707. } \
  9708. } while (0)
  9709. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9710. static bool
  9711. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9712. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9713. uint32 error_buf_size)
  9714. {
  9715. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9716. return false;
  9717. }
  9718. if (table_idx < module->import_table_count) {
  9719. if (p_elem_type)
  9720. *p_elem_type =
  9721. module->import_tables[table_idx].u.table.table_type.elem_type;
  9722. #if WASM_ENABLE_GC != 0
  9723. if (p_ref_type)
  9724. *((WASMRefType **)p_ref_type) =
  9725. module->import_tables[table_idx]
  9726. .u.table.table_type.elem_ref_type;
  9727. #endif
  9728. }
  9729. else {
  9730. if (p_elem_type)
  9731. *p_elem_type =
  9732. module->tables[module->import_table_count + table_idx]
  9733. .table_type.elem_type;
  9734. #if WASM_ENABLE_GC != 0
  9735. if (p_ref_type)
  9736. *((WASMRefType **)p_ref_type) =
  9737. module->tables[module->import_table_count + table_idx]
  9738. .table_type.elem_ref_type;
  9739. #endif
  9740. }
  9741. return true;
  9742. }
  9743. static bool
  9744. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9745. uint8 *p_elem_type, void **p_elem_ref_type,
  9746. char *error_buf, uint32 error_buf_size)
  9747. {
  9748. if (table_seg_idx >= module->table_seg_count) {
  9749. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9750. table_seg_idx);
  9751. return false;
  9752. }
  9753. if (p_elem_type) {
  9754. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9755. }
  9756. #if WASM_ENABLE_GC != 0
  9757. if (p_elem_ref_type)
  9758. *((WASMRefType **)p_elem_ref_type) =
  9759. module->table_segments[table_seg_idx].elem_ref_type;
  9760. #endif
  9761. return true;
  9762. }
  9763. #endif
  9764. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9765. const uint8 *
  9766. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9767. uint32 *len)
  9768. {
  9769. WASMCustomSection *section = module->custom_section_list;
  9770. while (section) {
  9771. if ((section->name_len == strlen(name))
  9772. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9773. if (len) {
  9774. *len = section->content_len;
  9775. }
  9776. return section->content_addr;
  9777. }
  9778. section = section->next;
  9779. }
  9780. return NULL;
  9781. }
  9782. #endif
  9783. #if 0
  9784. #define HANDLE_OPCODE(opcode) #opcode
  9785. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9786. #undef HANDLE_OPCODE
  9787. #endif
  9788. static bool
  9789. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9790. uint32 cur_func_idx, char *error_buf,
  9791. uint32 error_buf_size)
  9792. {
  9793. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9794. uint32 param_count, local_count, global_count;
  9795. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9796. BlockType func_block_type;
  9797. uint16 *local_offsets, local_offset;
  9798. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9799. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9800. mem_offset_t mem_offset;
  9801. int32 i32_const = 0;
  9802. int64 i64_const;
  9803. uint8 opcode;
  9804. bool return_value = false;
  9805. WASMLoaderContext *loader_ctx;
  9806. BranchBlock *frame_csp_tmp;
  9807. #if WASM_ENABLE_GC != 0
  9808. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9809. uint32 param_reftype_map_count, local_reftype_map_count;
  9810. int32 heap_type;
  9811. WASMRefType wasm_ref_type = { 0 };
  9812. bool need_ref_type_map;
  9813. #endif
  9814. #if WASM_ENABLE_FAST_INTERP != 0
  9815. uint8 *func_const_end, *func_const = NULL;
  9816. int16 operand_offset = 0;
  9817. uint8 last_op = 0;
  9818. bool disable_emit, preserve_local = false, if_condition_available = true;
  9819. float32 f32_const;
  9820. float64 f64_const;
  9821. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9822. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9823. #endif
  9824. #if WASM_ENABLE_MEMORY64 != 0
  9825. bool is_memory64 = has_module_memory64(module);
  9826. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9827. #else
  9828. mem_offset_type = VALUE_TYPE_I32;
  9829. #endif
  9830. global_count = module->import_global_count + module->global_count;
  9831. param_count = func->func_type->param_count;
  9832. param_types = func->func_type->types;
  9833. func_block_type.is_value_type = false;
  9834. func_block_type.u.type = func->func_type;
  9835. local_count = func->local_count;
  9836. local_types = func->local_types;
  9837. local_offsets = func->local_offsets;
  9838. #if WASM_ENABLE_GC != 0
  9839. param_reftype_maps = func->func_type->ref_type_maps;
  9840. param_reftype_map_count = func->func_type->ref_type_map_count;
  9841. local_reftype_maps = func->local_ref_type_maps;
  9842. local_reftype_map_count = func->local_ref_type_map_count;
  9843. #endif
  9844. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9845. goto fail;
  9846. }
  9847. #if WASM_ENABLE_GC != 0
  9848. loader_ctx->module = module;
  9849. loader_ctx->ref_type_set = module->ref_type_set;
  9850. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9851. #endif
  9852. #if WASM_ENABLE_FAST_INTERP != 0
  9853. /* For the first traverse, the initial value of preserved_local_offset has
  9854. * not been determined, we use the INT16_MAX to represent that a slot has
  9855. * been copied to preserve space. For second traverse, this field will be
  9856. * set to the appropriate value in wasm_loader_ctx_reinit.
  9857. * This is for Issue #1230,
  9858. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9859. * drop opcodes need to know which slots are preserved, so those slots will
  9860. * not be treated as dynamically allocated slots */
  9861. loader_ctx->preserved_local_offset = INT16_MAX;
  9862. re_scan:
  9863. if (loader_ctx->code_compiled_size > 0) {
  9864. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9865. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9866. goto fail;
  9867. }
  9868. p = func->code;
  9869. func->code_compiled = loader_ctx->p_code_compiled;
  9870. func->code_compiled_size = loader_ctx->code_compiled_size;
  9871. }
  9872. #endif
  9873. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9874. while (p < p_end) {
  9875. opcode = *p++;
  9876. #if WASM_ENABLE_FAST_INTERP != 0
  9877. p_org = p;
  9878. disable_emit = false;
  9879. emit_label(opcode);
  9880. #endif
  9881. switch (opcode) {
  9882. case WASM_OP_UNREACHABLE:
  9883. RESET_STACK();
  9884. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9885. break;
  9886. case WASM_OP_NOP:
  9887. #if WASM_ENABLE_FAST_INTERP != 0
  9888. skip_label();
  9889. #endif
  9890. break;
  9891. case WASM_OP_IF:
  9892. {
  9893. #if WASM_ENABLE_FAST_INTERP != 0
  9894. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9895. int32 available_stack_cell =
  9896. (int32)(loader_ctx->stack_cell_num
  9897. - parent_block->stack_cell_num);
  9898. if (available_stack_cell <= 0
  9899. && parent_block->is_stack_polymorphic)
  9900. if_condition_available = false;
  9901. else
  9902. if_condition_available = true;
  9903. PRESERVE_LOCAL_FOR_BLOCK();
  9904. #endif
  9905. #if WASM_ENABLE_GC == 0
  9906. POP_I32();
  9907. #endif
  9908. goto handle_op_block_and_loop;
  9909. }
  9910. case WASM_OP_BLOCK:
  9911. case WASM_OP_LOOP:
  9912. #if WASM_ENABLE_EXCE_HANDLING != 0
  9913. case WASM_OP_TRY:
  9914. if (opcode == WASM_OP_TRY) {
  9915. /*
  9916. * keep track of exception handlers to account for
  9917. * memory allocation
  9918. */
  9919. func->exception_handler_count++;
  9920. /*
  9921. * try is a block
  9922. * do nothing special, but execution continues to
  9923. * to handle_op_block_and_loop,
  9924. * and that be pushes the csp
  9925. */
  9926. }
  9927. #endif
  9928. #if WASM_ENABLE_FAST_INTERP != 0
  9929. PRESERVE_LOCAL_FOR_BLOCK();
  9930. #endif
  9931. handle_op_block_and_loop:
  9932. {
  9933. uint8 value_type;
  9934. BlockType block_type;
  9935. #if WASM_ENABLE_FAST_INTERP != 0
  9936. uint32 available_params = 0;
  9937. #endif
  9938. CHECK_BUF(p, p_end, 1);
  9939. value_type = read_uint8(p);
  9940. if (is_byte_a_type(value_type)) {
  9941. /* If the first byte is one of these special values:
  9942. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9943. * the single return value. */
  9944. block_type.is_value_type = true;
  9945. block_type.u.value_type.type = value_type;
  9946. #if WASM_ENABLE_WAMR_COMPILER != 0
  9947. if (value_type == VALUE_TYPE_V128)
  9948. module->is_simd_used = true;
  9949. else if (value_type == VALUE_TYPE_FUNCREF
  9950. || value_type == VALUE_TYPE_EXTERNREF)
  9951. module->is_ref_types_used = true;
  9952. #endif
  9953. #if WASM_ENABLE_GC != 0
  9954. if (value_type != VALUE_TYPE_VOID) {
  9955. p_org = p;
  9956. p--;
  9957. if (!resolve_value_type((const uint8 **)&p, p_end,
  9958. module, module->type_count,
  9959. &need_ref_type_map,
  9960. &wasm_ref_type, false,
  9961. error_buf, error_buf_size)) {
  9962. goto fail;
  9963. }
  9964. if (need_ref_type_map) {
  9965. block_type.u.value_type.ref_type_map.index = 0;
  9966. if (!(block_type.u.value_type.ref_type_map
  9967. .ref_type = reftype_set_insert(
  9968. module->ref_type_set, &wasm_ref_type,
  9969. error_buf, error_buf_size))) {
  9970. goto fail;
  9971. }
  9972. }
  9973. /* Set again as the type might be changed, e.g.
  9974. (ref null any) to anyref */
  9975. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9976. #if WASM_ENABLE_FAST_INTERP == 0
  9977. while (p_org < p) {
  9978. #if WASM_ENABLE_DEBUG_INTERP != 0
  9979. if (!record_fast_op(module, p_org, *p_org,
  9980. error_buf, error_buf_size)) {
  9981. goto fail;
  9982. }
  9983. #endif
  9984. /* Ignore extra bytes for interpreter */
  9985. *p_org++ = WASM_OP_NOP;
  9986. }
  9987. #endif
  9988. }
  9989. #endif /* end of WASM_ENABLE_GC != 0 */
  9990. }
  9991. else {
  9992. int32 type_index;
  9993. /* Resolve the leb128 encoded type index as block type */
  9994. p--;
  9995. p_org = p - 1;
  9996. read_leb_int32(p, p_end, type_index);
  9997. if ((uint32)type_index >= module->type_count) {
  9998. set_error_buf(error_buf, error_buf_size,
  9999. "unknown type");
  10000. goto fail;
  10001. }
  10002. block_type.is_value_type = false;
  10003. block_type.u.type =
  10004. (WASMFuncType *)module->types[type_index];
  10005. #if WASM_ENABLE_FAST_INTERP == 0
  10006. /* If block use type index as block type, change the opcode
  10007. * to new extended opcode so that interpreter can resolve
  10008. * the block quickly.
  10009. */
  10010. #if WASM_ENABLE_DEBUG_INTERP != 0
  10011. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10012. error_buf_size)) {
  10013. goto fail;
  10014. }
  10015. #endif
  10016. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10017. #endif
  10018. }
  10019. #if WASM_ENABLE_GC != 0
  10020. if (opcode == WASM_OP_IF) {
  10021. POP_I32();
  10022. }
  10023. #endif
  10024. /* Pop block parameters from stack */
  10025. if (BLOCK_HAS_PARAM(block_type)) {
  10026. WASMFuncType *wasm_type = block_type.u.type;
  10027. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10028. #if WASM_ENABLE_FAST_INTERP != 0
  10029. uint32 cell_num;
  10030. available_params = block_type.u.type->param_count;
  10031. #endif
  10032. for (i = 0; i < block_type.u.type->param_count; i++) {
  10033. int32 available_stack_cell =
  10034. (int32)(loader_ctx->stack_cell_num
  10035. - cur_block->stack_cell_num);
  10036. if (available_stack_cell <= 0
  10037. && cur_block->is_stack_polymorphic) {
  10038. #if WASM_ENABLE_FAST_INTERP != 0
  10039. available_params = i;
  10040. #endif
  10041. break;
  10042. }
  10043. POP_TYPE(
  10044. wasm_type->types[wasm_type->param_count - i - 1]);
  10045. #if WASM_ENABLE_FAST_INTERP != 0
  10046. /* decrease the frame_offset pointer accordingly to keep
  10047. * consistent with frame_ref stack */
  10048. cell_num = wasm_value_type_cell_num(
  10049. wasm_type->types[wasm_type->param_count - i - 1]);
  10050. loader_ctx->frame_offset -= cell_num;
  10051. #endif
  10052. }
  10053. }
  10054. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10055. block_type, p);
  10056. /* Pass parameters to block */
  10057. if (BLOCK_HAS_PARAM(block_type)) {
  10058. WASMFuncType *func_type = block_type.u.type;
  10059. #if WASM_ENABLE_GC != 0
  10060. WASMRefType *ref_type;
  10061. uint32 j = 0;
  10062. #endif
  10063. for (i = 0; i < func_type->param_count; i++) {
  10064. #if WASM_ENABLE_FAST_INTERP != 0
  10065. uint32 cell_num =
  10066. wasm_value_type_cell_num(func_type->types[i]);
  10067. if (i >= available_params) {
  10068. /* If there isn't enough data on stack, push a dummy
  10069. * offset to keep the stack consistent with
  10070. * frame_ref.
  10071. * Since the stack is already in polymorphic state,
  10072. * the opcode will not be executed, so the dummy
  10073. * offset won't cause any error */
  10074. uint32 n;
  10075. for (n = 0; n < cell_num; n++) {
  10076. if (loader_ctx->p_code_compiled == NULL) {
  10077. if (!check_offset_push(loader_ctx,
  10078. error_buf,
  10079. error_buf_size))
  10080. goto fail;
  10081. }
  10082. *loader_ctx->frame_offset++ = 0;
  10083. }
  10084. }
  10085. else {
  10086. loader_ctx->frame_offset += cell_num;
  10087. }
  10088. #endif
  10089. #if WASM_ENABLE_GC != 0
  10090. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10091. bh_assert(func_type->ref_type_maps[j].index == i);
  10092. ref_type = func_type->ref_type_maps[j].ref_type;
  10093. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10094. ref_type,
  10095. wasm_reftype_struct_size(ref_type));
  10096. j++;
  10097. }
  10098. #endif
  10099. PUSH_TYPE(func_type->types[i]);
  10100. }
  10101. }
  10102. #if WASM_ENABLE_FAST_INTERP != 0
  10103. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10104. skip_label();
  10105. if (BLOCK_HAS_PARAM(block_type)) {
  10106. /* Make sure params are in dynamic space */
  10107. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10108. error_buf_size))
  10109. goto fail;
  10110. }
  10111. if (opcode == WASM_OP_LOOP) {
  10112. (loader_ctx->frame_csp - 1)->code_compiled =
  10113. loader_ctx->p_code_compiled;
  10114. }
  10115. }
  10116. #if WASM_ENABLE_EXCE_HANDLING != 0
  10117. else if (opcode == WASM_OP_TRY) {
  10118. skip_label();
  10119. }
  10120. #endif
  10121. else if (opcode == WASM_OP_IF) {
  10122. BranchBlock *block = loader_ctx->frame_csp - 1;
  10123. /* If block has parameters, we should make sure they are in
  10124. * dynamic space. Otherwise, when else branch is missing,
  10125. * the later opcode may consume incorrect operand offset.
  10126. * Spec case:
  10127. * (func (export "params-id") (param i32) (result i32)
  10128. * (i32.const 1)
  10129. * (i32.const 2)
  10130. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10131. * (then)) (i32.add)
  10132. * )
  10133. *
  10134. * So we should emit a copy instruction before the if.
  10135. *
  10136. * And we also need to save the parameter offsets and
  10137. * recover them before entering else branch.
  10138. *
  10139. */
  10140. if (BLOCK_HAS_PARAM(block_type)) {
  10141. uint64 size;
  10142. /* In polymorphic state, there may be no if condition on
  10143. * the stack, so the offset may not emitted */
  10144. if (if_condition_available) {
  10145. /* skip the if condition operand offset */
  10146. wasm_loader_emit_backspace(loader_ctx,
  10147. sizeof(int16));
  10148. }
  10149. /* skip the if label */
  10150. skip_label();
  10151. /* Emit a copy instruction */
  10152. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10153. error_buf_size))
  10154. goto fail;
  10155. /* Emit the if instruction */
  10156. emit_label(opcode);
  10157. /* Emit the new condition operand offset */
  10158. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10159. /* Save top param_count values of frame_offset stack, so
  10160. * that we can recover it before executing else branch
  10161. */
  10162. size = sizeof(int16)
  10163. * (uint64)block_type.u.type->param_cell_num;
  10164. if (!(block->param_frame_offsets = loader_malloc(
  10165. size, error_buf, error_buf_size)))
  10166. goto fail;
  10167. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10168. loader_ctx->frame_offset
  10169. - size / sizeof(int16),
  10170. (uint32)size);
  10171. }
  10172. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10173. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10174. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10175. }
  10176. #endif
  10177. break;
  10178. }
  10179. #if WASM_ENABLE_EXCE_HANDLING != 0
  10180. case WASM_OP_THROW:
  10181. {
  10182. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10183. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10184. uint8 label_type = cur_block->label_type;
  10185. uint32 tag_index = 0;
  10186. read_leb_int32(p, p_end, tag_index);
  10187. /* check validity of tag_index against module->tag_count */
  10188. /* check tag index is within the tag index space */
  10189. if (tag_index >= module->import_tag_count + module->tag_count) {
  10190. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10191. tag_index);
  10192. goto fail;
  10193. }
  10194. /* the tag_type is stored in either the WASMTag (section tags)
  10195. * or WASMTagImport (import tag) */
  10196. WASMFuncType *tag_type = NULL;
  10197. if (tag_index < module->import_tag_count) {
  10198. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10199. }
  10200. else {
  10201. tag_type =
  10202. module->tags[tag_index - module->import_tag_count]
  10203. ->tag_type;
  10204. }
  10205. if (tag_type->result_count != 0) {
  10206. set_error_buf(error_buf, error_buf_size,
  10207. "tag type signature does not return void");
  10208. goto fail;
  10209. }
  10210. int32 available_stack_cell =
  10211. (int32)(loader_ctx->stack_cell_num
  10212. - cur_block->stack_cell_num);
  10213. int32 tti;
  10214. /* Check stack values match return types by comparing tag param
  10215. * types with stack cells */
  10216. uint8 *frame_ref = loader_ctx->frame_ref;
  10217. #if WASM_ENABLE_GC != 0
  10218. WASMRefTypeMap *frame_reftype_map =
  10219. loader_ctx->frame_reftype_map;
  10220. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10221. /* Temporarily set these values since they may be used in
  10222. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10223. param_reftype_maps = tag_type->ref_type_maps;
  10224. /* For tag_type function, it shouldn't have result_count = 0 */
  10225. param_reftype_map_count = tag_type->ref_type_map_count;
  10226. param_count = tag_type->param_count;
  10227. #endif
  10228. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10229. #if WASM_ENABLE_GC != 0
  10230. local_type = tag_type->types[tti];
  10231. local_idx = tti;
  10232. /* Get the wasm_ref_type if the local_type is multibyte
  10233. type */
  10234. GET_LOCAL_REFTYPE();
  10235. #endif
  10236. if (!check_stack_top_values(
  10237. loader_ctx, frame_ref, available_stack_cell,
  10238. #if WASM_ENABLE_GC != 0
  10239. frame_reftype_map, frame_reftype_map_num,
  10240. #endif
  10241. tag_type->types[tti],
  10242. #if WASM_ENABLE_GC != 0
  10243. &wasm_ref_type,
  10244. #endif
  10245. error_buf, error_buf_size)) {
  10246. snprintf(error_buf, error_buf_size,
  10247. "type mismatch: instruction requires [%s] but "
  10248. "stack has [%s]",
  10249. tag_type->param_count > 0
  10250. ? type2str(tag_type->types[tti])
  10251. : "",
  10252. available_stack_cell > 0
  10253. ? type2str(*(loader_ctx->frame_ref - 1))
  10254. : "");
  10255. goto fail;
  10256. }
  10257. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10258. available_stack_cell -=
  10259. wasm_value_type_cell_num(tag_type->types[tti]);
  10260. }
  10261. #if WASM_ENABLE_GC != 0
  10262. /* Restore the values */
  10263. param_reftype_maps = func->func_type->ref_type_maps;
  10264. param_reftype_map_count = func->func_type->ref_type_map_count;
  10265. param_count = func->func_type->param_count;
  10266. #endif
  10267. /* throw is stack polymorphic */
  10268. (void)label_type;
  10269. RESET_STACK();
  10270. break;
  10271. }
  10272. case WASM_OP_RETHROW:
  10273. {
  10274. /* must be done before checking branch block */
  10275. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10276. /* check the target catching block: LABEL_TYPE_CATCH */
  10277. if (!(frame_csp_tmp =
  10278. check_branch_block(loader_ctx, &p, p_end, opcode,
  10279. error_buf, error_buf_size)))
  10280. goto fail;
  10281. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10282. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10283. /* trap according to spectest (rethrow.wast) */
  10284. set_error_buf(error_buf, error_buf_size,
  10285. "invalid rethrow label");
  10286. goto fail;
  10287. }
  10288. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10289. uint8 label_type = cur_block->label_type;
  10290. (void)label_type;
  10291. /* rethrow is stack polymorphic */
  10292. RESET_STACK();
  10293. break;
  10294. }
  10295. case WASM_OP_DELEGATE:
  10296. {
  10297. /* check target block is valid */
  10298. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10299. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10300. goto fail;
  10301. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10302. uint8 label_type = cur_block->label_type;
  10303. (void)label_type;
  10304. /* DELEGATE ends the block */
  10305. POP_CSP();
  10306. break;
  10307. }
  10308. case WASM_OP_CATCH:
  10309. {
  10310. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10311. uint8 label_type = cur_block->label_type;
  10312. uint32 tag_index = 0;
  10313. read_leb_int32(p, p_end, tag_index);
  10314. /* check validity of tag_index against module->tag_count */
  10315. /* check tag index is within the tag index space */
  10316. if (tag_index >= module->import_tag_count + module->tag_count) {
  10317. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10318. __FUNCTION__);
  10319. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10320. goto fail;
  10321. }
  10322. /* the tag_type is stored in either the WASMTag (section tags)
  10323. * or WASMTagImport (import tag) */
  10324. WASMFuncType *func_type = NULL;
  10325. if (tag_index < module->import_tag_count) {
  10326. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10327. }
  10328. else {
  10329. func_type =
  10330. module->tags[tag_index - module->import_tag_count]
  10331. ->tag_type;
  10332. }
  10333. if (func_type->result_count != 0) {
  10334. set_error_buf(error_buf, error_buf_size,
  10335. "tag type signature does not return void");
  10336. goto fail;
  10337. }
  10338. /* check validity of current label (expect LABEL_TYPE_TRY or
  10339. * LABEL_TYPE_CATCH) */
  10340. if ((LABEL_TYPE_CATCH != label_type)
  10341. && (LABEL_TYPE_TRY != label_type)) {
  10342. set_error_buf(error_buf, error_buf_size,
  10343. "Unexpected block sequence encountered.");
  10344. goto fail;
  10345. }
  10346. /*
  10347. * replace frame_csp by LABEL_TYPE_CATCH
  10348. */
  10349. cur_block->label_type = LABEL_TYPE_CATCH;
  10350. /* RESET_STACK removes the values pushed in TRY or pervious
  10351. * CATCH Blocks */
  10352. RESET_STACK();
  10353. #if WASM_ENABLE_GC != 0
  10354. WASMRefType *ref_type;
  10355. uint32 j = 0;
  10356. #endif
  10357. /* push types on the stack according to caught type */
  10358. for (i = 0; i < func_type->param_count; i++) {
  10359. #if WASM_ENABLE_GC != 0
  10360. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10361. bh_assert(func_type->ref_type_maps[j].index == i);
  10362. ref_type = func_type->ref_type_maps[j].ref_type;
  10363. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10364. ref_type,
  10365. wasm_reftype_struct_size(ref_type));
  10366. j++;
  10367. }
  10368. #endif
  10369. PUSH_TYPE(func_type->types[i]);
  10370. }
  10371. break;
  10372. }
  10373. case WASM_OP_CATCH_ALL:
  10374. {
  10375. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10376. /* expecting a TRY or CATCH, anything else will be considered an
  10377. * error */
  10378. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10379. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10380. set_error_buf(error_buf, error_buf_size,
  10381. "Unexpected block sequence encountered.");
  10382. goto fail;
  10383. }
  10384. /* no immediates */
  10385. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10386. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10387. /* RESET_STACK removes the values pushed in TRY or pervious
  10388. * CATCH Blocks */
  10389. RESET_STACK();
  10390. /* catch_all has no tagtype and therefore no parameters */
  10391. break;
  10392. }
  10393. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10394. case WASM_OP_ELSE:
  10395. handle_op_else:
  10396. {
  10397. BranchBlock *block = NULL;
  10398. BlockType block_type;
  10399. if (loader_ctx->csp_num < 2
  10400. /* the matched if isn't found */
  10401. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10402. /* duplicated else is found */
  10403. || (loader_ctx->frame_csp - 1)->else_addr) {
  10404. set_error_buf(
  10405. error_buf, error_buf_size,
  10406. "opcode else found without matched opcode if");
  10407. goto fail;
  10408. }
  10409. block = loader_ctx->frame_csp - 1;
  10410. /* check whether if branch's stack matches its result type */
  10411. if (!check_block_stack(loader_ctx, block, error_buf,
  10412. error_buf_size))
  10413. goto fail;
  10414. block->else_addr = p - 1;
  10415. block_type = block->block_type;
  10416. #if WASM_ENABLE_GC != 0
  10417. if (!wasm_loader_init_local_use_masks(
  10418. loader_ctx, local_count, error_buf, error_buf_size)) {
  10419. goto fail;
  10420. }
  10421. #endif
  10422. #if WASM_ENABLE_FAST_INTERP != 0
  10423. /* if the result of if branch is in local or const area, add a
  10424. * copy op */
  10425. RESERVE_BLOCK_RET();
  10426. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10427. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10428. #endif
  10429. RESET_STACK();
  10430. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10431. /* Pass parameters to if-false branch */
  10432. if (BLOCK_HAS_PARAM(block_type)) {
  10433. for (i = 0; i < block_type.u.type->param_count; i++)
  10434. PUSH_TYPE(block_type.u.type->types[i]);
  10435. }
  10436. #if WASM_ENABLE_FAST_INTERP != 0
  10437. /* Recover top param_count values of frame_offset stack */
  10438. if (BLOCK_HAS_PARAM((block_type))) {
  10439. uint32 size;
  10440. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10441. bh_memcpy_s(loader_ctx->frame_offset, size,
  10442. block->param_frame_offsets, size);
  10443. loader_ctx->frame_offset += (size / sizeof(int16));
  10444. }
  10445. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10446. #endif
  10447. break;
  10448. }
  10449. case WASM_OP_END:
  10450. {
  10451. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10452. /* check whether block stack matches its result type */
  10453. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10454. error_buf_size))
  10455. goto fail;
  10456. /* if there is no else branch, make a virtual else opcode for
  10457. easier integrity check and to copy the correct results to
  10458. the block return address for fast-interp mode:
  10459. change if block from `if ... end` to `if ... else end` */
  10460. if (cur_block->label_type == LABEL_TYPE_IF
  10461. && !cur_block->else_addr) {
  10462. opcode = WASM_OP_ELSE;
  10463. p--;
  10464. #if WASM_ENABLE_FAST_INTERP != 0
  10465. p_org = p;
  10466. skip_label();
  10467. disable_emit = false;
  10468. emit_label(opcode);
  10469. #endif
  10470. goto handle_op_else;
  10471. }
  10472. POP_CSP();
  10473. #if WASM_ENABLE_FAST_INTERP != 0
  10474. skip_label();
  10475. /* copy the result to the block return address */
  10476. RESERVE_BLOCK_RET();
  10477. apply_label_patch(loader_ctx, 0, PATCH_END);
  10478. free_label_patch_list(loader_ctx->frame_csp);
  10479. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10480. int32 idx;
  10481. uint8 ret_type;
  10482. emit_label(WASM_OP_RETURN);
  10483. for (idx = (int32)func->func_type->result_count - 1;
  10484. idx >= 0; idx--) {
  10485. ret_type = *(func->func_type->types
  10486. + func->func_type->param_count + idx);
  10487. POP_OFFSET_TYPE(ret_type);
  10488. }
  10489. }
  10490. #endif
  10491. if (loader_ctx->csp_num > 0) {
  10492. loader_ctx->frame_csp->end_addr = p - 1;
  10493. }
  10494. else {
  10495. /* end of function block, function will return */
  10496. if (p < p_end) {
  10497. set_error_buf(error_buf, error_buf_size,
  10498. "section size mismatch");
  10499. goto fail;
  10500. }
  10501. }
  10502. break;
  10503. }
  10504. case WASM_OP_BR:
  10505. {
  10506. if (!(frame_csp_tmp =
  10507. check_branch_block(loader_ctx, &p, p_end, opcode,
  10508. error_buf, error_buf_size)))
  10509. goto fail;
  10510. RESET_STACK();
  10511. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10512. break;
  10513. }
  10514. case WASM_OP_BR_IF:
  10515. {
  10516. POP_I32();
  10517. if (!(frame_csp_tmp =
  10518. check_branch_block(loader_ctx, &p, p_end, opcode,
  10519. error_buf, error_buf_size)))
  10520. goto fail;
  10521. break;
  10522. }
  10523. case WASM_OP_BR_TABLE:
  10524. {
  10525. uint32 depth = 0, default_arity, arity = 0;
  10526. BranchBlock *target_block;
  10527. BlockType *target_block_type;
  10528. #if WASM_ENABLE_FAST_INTERP == 0
  10529. BrTableCache *br_table_cache = NULL;
  10530. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10531. uint32 j;
  10532. #endif
  10533. read_leb_uint32(p, p_end, count);
  10534. #if WASM_ENABLE_FAST_INTERP != 0
  10535. emit_uint32(loader_ctx, count);
  10536. #endif
  10537. POP_I32();
  10538. /* Get each depth and check it */
  10539. p_org = p;
  10540. for (i = 0; i <= count; i++) {
  10541. read_leb_uint32(p, p_end, depth);
  10542. bh_assert(loader_ctx->csp_num > 0);
  10543. if (loader_ctx->csp_num - 1 < depth) {
  10544. set_error_buf(error_buf, error_buf_size,
  10545. "unknown label, "
  10546. "unexpected end of section or function");
  10547. goto fail;
  10548. }
  10549. }
  10550. p = p_org;
  10551. /* Get the default block's arity */
  10552. target_block = loader_ctx->frame_csp - (depth + 1);
  10553. target_block_type = &target_block->block_type;
  10554. default_arity = block_type_get_arity(target_block_type,
  10555. target_block->label_type);
  10556. #if WASM_ENABLE_FAST_INTERP == 0
  10557. p_depth_begin = p_depth = p;
  10558. #endif
  10559. for (i = 0; i <= count; i++) {
  10560. p_org = p;
  10561. read_leb_uint32(p, p_end, depth);
  10562. p = p_org;
  10563. /* Get the target block's arity and check it */
  10564. target_block = loader_ctx->frame_csp - (depth + 1);
  10565. target_block_type = &target_block->block_type;
  10566. arity = block_type_get_arity(target_block_type,
  10567. target_block->label_type);
  10568. if (arity != default_arity) {
  10569. set_error_buf(error_buf, error_buf_size,
  10570. "type mismatch: br_table targets must "
  10571. "all use same result type");
  10572. goto fail;
  10573. }
  10574. if (!(frame_csp_tmp =
  10575. check_branch_block(loader_ctx, &p, p_end, opcode,
  10576. error_buf, error_buf_size))) {
  10577. goto fail;
  10578. }
  10579. #if WASM_ENABLE_FAST_INTERP == 0
  10580. if (br_table_cache) {
  10581. br_table_cache->br_depths[i] = depth;
  10582. }
  10583. else {
  10584. if (depth > 255) {
  10585. /* The depth cannot be stored in one byte,
  10586. create br_table cache to store each depth */
  10587. #if WASM_ENABLE_DEBUG_INTERP != 0
  10588. if (!record_fast_op(module, p_opcode, *p_opcode,
  10589. error_buf, error_buf_size)) {
  10590. goto fail;
  10591. }
  10592. #endif
  10593. if (!(br_table_cache = loader_malloc(
  10594. offsetof(BrTableCache, br_depths)
  10595. + sizeof(uint32)
  10596. * (uint64)(count + 1),
  10597. error_buf, error_buf_size))) {
  10598. goto fail;
  10599. }
  10600. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10601. br_table_cache->br_table_op_addr = p_opcode;
  10602. br_table_cache->br_count = count;
  10603. /* Copy previous depths which are one byte */
  10604. for (j = 0; j < i; j++) {
  10605. br_table_cache->br_depths[j] = p_depth_begin[j];
  10606. }
  10607. br_table_cache->br_depths[i] = depth;
  10608. bh_list_insert(module->br_table_cache_list,
  10609. br_table_cache);
  10610. }
  10611. else {
  10612. /* The depth can be stored in one byte, use the
  10613. byte of the leb to store it */
  10614. *p_depth++ = (uint8)depth;
  10615. }
  10616. }
  10617. #endif
  10618. }
  10619. #if WASM_ENABLE_FAST_INTERP == 0
  10620. /* Set the tailing bytes to nop */
  10621. if (br_table_cache)
  10622. p_depth = p_depth_begin;
  10623. while (p_depth < p)
  10624. *p_depth++ = WASM_OP_NOP;
  10625. #endif
  10626. RESET_STACK();
  10627. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10628. break;
  10629. }
  10630. case WASM_OP_RETURN:
  10631. {
  10632. WASMFuncType *func_type = func->func_type;
  10633. int32 idx;
  10634. uint8 ret_type;
  10635. #if WASM_ENABLE_GC != 0
  10636. uint32 j = func_type->ref_type_map_count - 1;
  10637. #endif
  10638. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10639. idx--) {
  10640. ret_type =
  10641. *(func_type->types + func_type->param_count + idx);
  10642. #if WASM_ENABLE_GC != 0
  10643. if (wasm_is_type_multi_byte_type(ret_type)) {
  10644. WASMRefType *ref_type =
  10645. func_type->ref_type_maps[j].ref_type;
  10646. bh_assert(func_type->ref_type_maps[j].index
  10647. == func_type->param_count + idx);
  10648. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10649. ref_type,
  10650. wasm_reftype_struct_size(ref_type));
  10651. j--;
  10652. }
  10653. #endif
  10654. #if WASM_ENABLE_FAST_INTERP != 0
  10655. /* emit the offset after return opcode */
  10656. POP_OFFSET_TYPE(ret_type);
  10657. #endif
  10658. POP_TYPE(ret_type);
  10659. }
  10660. RESET_STACK();
  10661. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10662. break;
  10663. }
  10664. case WASM_OP_CALL:
  10665. #if WASM_ENABLE_TAIL_CALL != 0
  10666. case WASM_OP_RETURN_CALL:
  10667. #endif
  10668. #if WASM_ENABLE_GC != 0
  10669. case WASM_OP_CALL_REF:
  10670. case WASM_OP_RETURN_CALL_REF:
  10671. #endif
  10672. {
  10673. WASMFuncType *func_type;
  10674. uint8 type;
  10675. int32 idx;
  10676. #if WASM_ENABLE_GC != 0
  10677. WASMRefType *ref_type;
  10678. uint32 type_idx1;
  10679. int32 j;
  10680. #endif
  10681. #if WASM_ENABLE_GC != 0
  10682. if (opcode == WASM_OP_CALL_REF
  10683. || opcode == WASM_OP_RETURN_CALL_REF) {
  10684. read_leb_uint32(p, p_end, type_idx1);
  10685. if (!check_type_index(module, module->type_count, type_idx1,
  10686. error_buf, error_buf_size)) {
  10687. goto fail;
  10688. }
  10689. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10690. set_error_buf(error_buf, error_buf_size,
  10691. "unkown function type");
  10692. goto fail;
  10693. }
  10694. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10695. &type_idx, error_buf,
  10696. error_buf_size)) {
  10697. goto fail;
  10698. }
  10699. if (type == VALUE_TYPE_ANY) {
  10700. type_idx = type_idx1;
  10701. }
  10702. if (!check_type_index(module, module->type_count, type_idx,
  10703. error_buf, error_buf_size)) {
  10704. goto fail;
  10705. }
  10706. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10707. set_error_buf(error_buf, error_buf_size,
  10708. "unkown function type");
  10709. goto fail;
  10710. }
  10711. if (!wasm_func_type_is_super_of(
  10712. (WASMFuncType *)module->types[type_idx1],
  10713. (WASMFuncType *)module->types[type_idx])) {
  10714. set_error_buf(error_buf, error_buf_size,
  10715. "function type mismatch");
  10716. goto fail;
  10717. }
  10718. func_type = (WASMFuncType *)module->types[type_idx];
  10719. }
  10720. else
  10721. #endif
  10722. {
  10723. read_leb_uint32(p, p_end, func_idx);
  10724. #if WASM_ENABLE_FAST_INTERP != 0
  10725. /* we need to emit func_idx before arguments */
  10726. emit_uint32(loader_ctx, func_idx);
  10727. #endif
  10728. if (!check_function_index(module, func_idx, error_buf,
  10729. error_buf_size)) {
  10730. goto fail;
  10731. }
  10732. if (func_idx < module->import_function_count)
  10733. func_type = module->import_functions[func_idx]
  10734. .u.function.func_type;
  10735. else
  10736. func_type =
  10737. module
  10738. ->functions[func_idx
  10739. - module->import_function_count]
  10740. ->func_type;
  10741. }
  10742. if (func_type->param_count > 0) {
  10743. #if WASM_ENABLE_GC != 0
  10744. j = (int32)(func_type->result_ref_type_maps
  10745. - func_type->ref_type_maps - 1);
  10746. #endif
  10747. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10748. idx--) {
  10749. #if WASM_ENABLE_GC != 0
  10750. if (wasm_is_type_multi_byte_type(
  10751. func_type->types[idx])) {
  10752. ref_type = func_type->ref_type_maps[j].ref_type;
  10753. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10754. ref_type,
  10755. wasm_reftype_struct_size(ref_type));
  10756. j--;
  10757. }
  10758. #endif
  10759. #if WASM_ENABLE_FAST_INTERP != 0
  10760. POP_OFFSET_TYPE(func_type->types[idx]);
  10761. #endif
  10762. POP_TYPE(func_type->types[idx]);
  10763. }
  10764. }
  10765. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10766. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10767. #endif
  10768. #if WASM_ENABLE_GC != 0
  10769. j = (int32)(func_type->result_ref_type_maps
  10770. - func_type->ref_type_maps);
  10771. #endif
  10772. for (i = 0; i < func_type->result_count; i++) {
  10773. #if WASM_ENABLE_GC != 0
  10774. if (wasm_is_type_multi_byte_type(
  10775. func_type->types[func_type->param_count + i])) {
  10776. ref_type = func_type->ref_type_maps[j].ref_type;
  10777. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10778. ref_type,
  10779. wasm_reftype_struct_size(ref_type));
  10780. j++;
  10781. }
  10782. #endif
  10783. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10784. #if WASM_ENABLE_FAST_INTERP != 0
  10785. /* Here we emit each return value's dynamic_offset. But
  10786. * in fact these offsets are continuous, so interpreter
  10787. * only need to get the first return value's offset.
  10788. */
  10789. PUSH_OFFSET_TYPE(
  10790. func_type->types[func_type->param_count + i]);
  10791. #endif
  10792. }
  10793. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10794. }
  10795. else {
  10796. #if WASM_ENABLE_GC == 0
  10797. if (func_type->result_count
  10798. != func->func_type->result_count) {
  10799. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10800. "type mismatch: expect ",
  10801. func->func_type->result_count,
  10802. " return values but got other");
  10803. goto fail;
  10804. }
  10805. for (i = 0; i < func_type->result_count; i++) {
  10806. type = func->func_type
  10807. ->types[func->func_type->param_count + i];
  10808. if (func_type->types[func_type->param_count + i]
  10809. != type) {
  10810. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10811. "type mismatch: expect ",
  10812. type2str(type), " but got other");
  10813. goto fail;
  10814. }
  10815. }
  10816. #else
  10817. if (!wasm_func_type_result_is_subtype_of(
  10818. func_type, func->func_type, module->types,
  10819. module->type_count)) {
  10820. set_error_buf(
  10821. error_buf, error_buf_size,
  10822. "type mismatch: invalid func result types");
  10823. goto fail;
  10824. }
  10825. #endif
  10826. RESET_STACK();
  10827. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10828. }
  10829. #endif
  10830. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10831. || WASM_ENABLE_WAMR_COMPILER != 0
  10832. func->has_op_func_call = true;
  10833. #endif
  10834. (void)type;
  10835. break;
  10836. }
  10837. /*
  10838. * if disable reference type: call_indirect typeidx, 0x00
  10839. * if enable reference type: call_indirect typeidx, tableidx
  10840. */
  10841. case WASM_OP_CALL_INDIRECT:
  10842. #if WASM_ENABLE_TAIL_CALL != 0
  10843. case WASM_OP_RETURN_CALL_INDIRECT:
  10844. #endif
  10845. {
  10846. int32 idx;
  10847. WASMFuncType *func_type;
  10848. read_leb_uint32(p, p_end, type_idx);
  10849. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10850. read_leb_uint32(p, p_end, table_idx);
  10851. #else
  10852. CHECK_BUF(p, p_end, 1);
  10853. table_idx = read_uint8(p);
  10854. #endif
  10855. if (!check_table_index(module, table_idx, error_buf,
  10856. error_buf_size)) {
  10857. goto fail;
  10858. }
  10859. #if WASM_ENABLE_FAST_INTERP != 0
  10860. /* we need to emit before arguments */
  10861. #if WASM_ENABLE_TAIL_CALL != 0
  10862. emit_byte(loader_ctx, opcode);
  10863. #endif
  10864. emit_uint32(loader_ctx, type_idx);
  10865. emit_uint32(loader_ctx, table_idx);
  10866. #endif
  10867. /* skip elem idx */
  10868. POP_I32();
  10869. if (type_idx >= module->type_count) {
  10870. set_error_buf(error_buf, error_buf_size, "unknown type");
  10871. goto fail;
  10872. }
  10873. func_type = (WASMFuncType *)module->types[type_idx];
  10874. if (func_type->param_count > 0) {
  10875. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10876. idx--) {
  10877. #if WASM_ENABLE_FAST_INTERP != 0
  10878. POP_OFFSET_TYPE(func_type->types[idx]);
  10879. #endif
  10880. POP_TYPE(func_type->types[idx]);
  10881. }
  10882. }
  10883. #if WASM_ENABLE_TAIL_CALL != 0
  10884. if (opcode == WASM_OP_CALL_INDIRECT) {
  10885. #endif
  10886. for (i = 0; i < func_type->result_count; i++) {
  10887. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10888. #if WASM_ENABLE_FAST_INTERP != 0
  10889. PUSH_OFFSET_TYPE(
  10890. func_type->types[func_type->param_count + i]);
  10891. #endif
  10892. }
  10893. #if WASM_ENABLE_TAIL_CALL != 0
  10894. }
  10895. else {
  10896. uint8 type;
  10897. if (func_type->result_count
  10898. != func->func_type->result_count) {
  10899. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10900. "type mismatch: expect ",
  10901. func->func_type->result_count,
  10902. " return values but got other");
  10903. goto fail;
  10904. }
  10905. for (i = 0; i < func_type->result_count; i++) {
  10906. type = func->func_type
  10907. ->types[func->func_type->param_count + i];
  10908. if (func_type->types[func_type->param_count + i]
  10909. != type) {
  10910. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10911. "type mismatch: expect ",
  10912. type2str(type), " but got other");
  10913. goto fail;
  10914. }
  10915. }
  10916. RESET_STACK();
  10917. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10918. }
  10919. #endif
  10920. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10921. || WASM_ENABLE_WAMR_COMPILER != 0
  10922. func->has_op_func_call = true;
  10923. #endif
  10924. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10925. func->has_op_call_indirect = true;
  10926. #endif
  10927. break;
  10928. }
  10929. case WASM_OP_DROP:
  10930. {
  10931. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10932. int32 available_stack_cell =
  10933. (int32)(loader_ctx->stack_cell_num
  10934. - cur_block->stack_cell_num);
  10935. if (available_stack_cell <= 0
  10936. && !cur_block->is_stack_polymorphic) {
  10937. set_error_buf(error_buf, error_buf_size,
  10938. "type mismatch, opcode drop was found "
  10939. "but stack was empty");
  10940. goto fail;
  10941. }
  10942. if (available_stack_cell > 0) {
  10943. #if WASM_ENABLE_GC != 0
  10944. if (wasm_is_type_multi_byte_type(
  10945. *(loader_ctx->frame_ref - 1))) {
  10946. bh_assert((int32)(loader_ctx->reftype_map_num
  10947. - cur_block->reftype_map_num)
  10948. > 0);
  10949. loader_ctx->frame_reftype_map--;
  10950. loader_ctx->reftype_map_num--;
  10951. }
  10952. #endif
  10953. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10954. loader_ctx->frame_ref--;
  10955. loader_ctx->stack_cell_num--;
  10956. #if WASM_ENABLE_FAST_INTERP != 0
  10957. skip_label();
  10958. loader_ctx->frame_offset--;
  10959. if ((*(loader_ctx->frame_offset)
  10960. > loader_ctx->start_dynamic_offset)
  10961. && (*(loader_ctx->frame_offset)
  10962. < loader_ctx->max_dynamic_offset))
  10963. loader_ctx->dynamic_offset--;
  10964. #endif
  10965. }
  10966. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10967. loader_ctx->frame_ref -= 2;
  10968. loader_ctx->stack_cell_num -= 2;
  10969. #if WASM_ENABLE_FAST_INTERP == 0
  10970. *(p - 1) = WASM_OP_DROP_64;
  10971. #endif
  10972. #if WASM_ENABLE_FAST_INTERP != 0
  10973. skip_label();
  10974. loader_ctx->frame_offset -= 2;
  10975. if ((*(loader_ctx->frame_offset)
  10976. > loader_ctx->start_dynamic_offset)
  10977. && (*(loader_ctx->frame_offset)
  10978. < loader_ctx->max_dynamic_offset))
  10979. loader_ctx->dynamic_offset -= 2;
  10980. #endif
  10981. }
  10982. #if WASM_ENABLE_SIMD != 0
  10983. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10984. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10985. loader_ctx->frame_ref -= 4;
  10986. loader_ctx->stack_cell_num -= 4;
  10987. }
  10988. #endif
  10989. #endif
  10990. else {
  10991. set_error_buf(error_buf, error_buf_size,
  10992. "type mismatch");
  10993. goto fail;
  10994. }
  10995. }
  10996. else {
  10997. #if WASM_ENABLE_FAST_INTERP != 0
  10998. skip_label();
  10999. #endif
  11000. }
  11001. break;
  11002. }
  11003. case WASM_OP_SELECT:
  11004. {
  11005. uint8 ref_type;
  11006. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11007. int32 available_stack_cell;
  11008. #if WASM_ENABLE_FAST_INTERP != 0
  11009. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11010. #endif
  11011. POP_I32();
  11012. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11013. - cur_block->stack_cell_num);
  11014. if (available_stack_cell <= 0
  11015. && !cur_block->is_stack_polymorphic) {
  11016. set_error_buf(error_buf, error_buf_size,
  11017. "type mismatch or invalid result arity, "
  11018. "opcode select was found "
  11019. "but stack was empty");
  11020. goto fail;
  11021. }
  11022. if (available_stack_cell > 0) {
  11023. switch (*(loader_ctx->frame_ref - 1)) {
  11024. case VALUE_TYPE_I32:
  11025. case VALUE_TYPE_F32:
  11026. case VALUE_TYPE_ANY:
  11027. break;
  11028. case VALUE_TYPE_I64:
  11029. case VALUE_TYPE_F64:
  11030. #if WASM_ENABLE_FAST_INTERP == 0
  11031. *(p - 1) = WASM_OP_SELECT_64;
  11032. #endif
  11033. #if WASM_ENABLE_FAST_INTERP != 0
  11034. if (loader_ctx->p_code_compiled) {
  11035. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11036. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11037. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11038. *(void **)(p_code_compiled_tmp
  11039. - sizeof(void *)) =
  11040. handle_table[opcode_tmp];
  11041. #else
  11042. #if UINTPTR_MAX == UINT64_MAX
  11043. /* emit int32 relative offset in 64-bit target
  11044. */
  11045. int32 offset =
  11046. (int32)((uint8 *)handle_table[opcode_tmp]
  11047. - (uint8 *)handle_table[0]);
  11048. *(int32 *)(p_code_compiled_tmp
  11049. - sizeof(int32)) = offset;
  11050. #else
  11051. /* emit uint32 label address in 32-bit target */
  11052. *(uint32 *)(p_code_compiled_tmp
  11053. - sizeof(uint32)) =
  11054. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11055. #endif
  11056. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11057. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11058. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11059. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11060. #else
  11061. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11062. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11063. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11064. }
  11065. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11066. break;
  11067. #if WASM_ENABLE_SIMD != 0
  11068. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11069. case VALUE_TYPE_V128:
  11070. break;
  11071. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11072. #endif /* WASM_ENABLE_SIMD != 0 */
  11073. default:
  11074. {
  11075. set_error_buf(error_buf, error_buf_size,
  11076. "type mismatch");
  11077. goto fail;
  11078. }
  11079. }
  11080. ref_type = *(loader_ctx->frame_ref - 1);
  11081. #if WASM_ENABLE_FAST_INTERP != 0
  11082. POP_OFFSET_TYPE(ref_type);
  11083. POP_TYPE(ref_type);
  11084. POP_OFFSET_TYPE(ref_type);
  11085. POP_TYPE(ref_type);
  11086. PUSH_OFFSET_TYPE(ref_type);
  11087. PUSH_TYPE(ref_type);
  11088. #else
  11089. POP2_AND_PUSH(ref_type, ref_type);
  11090. #endif
  11091. }
  11092. else {
  11093. #if WASM_ENABLE_FAST_INTERP != 0
  11094. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11095. #endif
  11096. PUSH_TYPE(VALUE_TYPE_ANY);
  11097. }
  11098. break;
  11099. }
  11100. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11101. case WASM_OP_SELECT_T:
  11102. {
  11103. uint8 vec_len, type;
  11104. #if WASM_ENABLE_GC != 0
  11105. WASMRefType *ref_type = NULL;
  11106. #endif
  11107. #if WASM_ENABLE_FAST_INTERP != 0
  11108. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11109. #endif
  11110. read_leb_uint32(p, p_end, vec_len);
  11111. if (vec_len != 1) {
  11112. /* typed select must have exactly one result */
  11113. set_error_buf(error_buf, error_buf_size,
  11114. "invalid result arity");
  11115. goto fail;
  11116. }
  11117. #if WASM_ENABLE_GC == 0
  11118. CHECK_BUF(p, p_end, 1);
  11119. type = read_uint8(p);
  11120. if (!is_valid_value_type_for_interpreter(type)) {
  11121. set_error_buf(error_buf, error_buf_size,
  11122. "unknown value type");
  11123. goto fail;
  11124. }
  11125. #else
  11126. p_org = p + 1;
  11127. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11128. module->type_count, &need_ref_type_map,
  11129. &wasm_ref_type, false, error_buf,
  11130. error_buf_size)) {
  11131. goto fail;
  11132. }
  11133. type = wasm_ref_type.ref_type;
  11134. if (need_ref_type_map) {
  11135. if (!(ref_type = reftype_set_insert(
  11136. module->ref_type_set, &wasm_ref_type, error_buf,
  11137. error_buf_size))) {
  11138. goto fail;
  11139. }
  11140. }
  11141. #if WASM_ENABLE_FAST_INTERP == 0
  11142. while (p_org < p) {
  11143. #if WASM_ENABLE_DEBUG_INTERP != 0
  11144. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11145. error_buf_size)) {
  11146. goto fail;
  11147. }
  11148. #endif
  11149. /* Ignore extra bytes for interpreter */
  11150. *p_org++ = WASM_OP_NOP;
  11151. }
  11152. #endif
  11153. #endif /* end of WASM_ENABLE_GC == 0 */
  11154. POP_I32();
  11155. #if WASM_ENABLE_FAST_INTERP != 0
  11156. if (loader_ctx->p_code_compiled) {
  11157. uint8 opcode_tmp = WASM_OP_SELECT;
  11158. if (type == VALUE_TYPE_V128) {
  11159. #if (WASM_ENABLE_SIMD == 0) \
  11160. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11161. set_error_buf(error_buf, error_buf_size,
  11162. "SIMD v128 type isn't supported");
  11163. goto fail;
  11164. #endif
  11165. }
  11166. else {
  11167. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11168. opcode_tmp = WASM_OP_SELECT_64;
  11169. #if WASM_ENABLE_GC != 0
  11170. if (wasm_is_type_reftype(type))
  11171. opcode_tmp = WASM_OP_SELECT_T;
  11172. #endif
  11173. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11174. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11175. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11176. handle_table[opcode_tmp];
  11177. #else
  11178. #if UINTPTR_MAX == UINT64_MAX
  11179. /* emit int32 relative offset in 64-bit target */
  11180. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11181. - (uint8 *)handle_table[0]);
  11182. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11183. offset;
  11184. #else
  11185. /* emit uint32 label address in 32-bit target */
  11186. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11187. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11188. #endif
  11189. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11190. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11191. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11192. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11193. #else
  11194. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11195. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11196. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11197. }
  11198. }
  11199. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11200. POP_REF(type);
  11201. #if WASM_ENABLE_GC != 0
  11202. if (need_ref_type_map) {
  11203. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11204. wasm_reftype_struct_size(ref_type));
  11205. }
  11206. #endif
  11207. POP_REF(type);
  11208. #if WASM_ENABLE_GC != 0
  11209. if (need_ref_type_map) {
  11210. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11211. wasm_reftype_struct_size(ref_type));
  11212. }
  11213. #endif
  11214. PUSH_REF(type);
  11215. #if WASM_ENABLE_WAMR_COMPILER != 0
  11216. module->is_ref_types_used = true;
  11217. #endif
  11218. (void)vec_len;
  11219. break;
  11220. }
  11221. /* table.get x. tables[x]. [i32] -> [t] */
  11222. /* table.set x. tables[x]. [i32 t] -> [] */
  11223. case WASM_OP_TABLE_GET:
  11224. case WASM_OP_TABLE_SET:
  11225. {
  11226. uint8 decl_ref_type;
  11227. #if WASM_ENABLE_GC != 0
  11228. WASMRefType *ref_type;
  11229. #endif
  11230. read_leb_uint32(p, p_end, table_idx);
  11231. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11232. #if WASM_ENABLE_GC != 0
  11233. (void **)&ref_type,
  11234. #else
  11235. NULL,
  11236. #endif
  11237. error_buf, error_buf_size))
  11238. goto fail;
  11239. #if WASM_ENABLE_GC != 0
  11240. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11241. bh_assert(ref_type);
  11242. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11243. ref_type, wasm_reftype_struct_size(ref_type));
  11244. }
  11245. #endif
  11246. #if WASM_ENABLE_FAST_INTERP != 0
  11247. emit_uint32(loader_ctx, table_idx);
  11248. #endif
  11249. if (opcode == WASM_OP_TABLE_GET) {
  11250. POP_I32();
  11251. #if WASM_ENABLE_FAST_INTERP != 0
  11252. PUSH_OFFSET_TYPE(decl_ref_type);
  11253. #endif
  11254. PUSH_TYPE(decl_ref_type);
  11255. }
  11256. else {
  11257. #if WASM_ENABLE_FAST_INTERP != 0
  11258. POP_OFFSET_TYPE(decl_ref_type);
  11259. #endif
  11260. POP_TYPE(decl_ref_type);
  11261. POP_I32();
  11262. }
  11263. #if WASM_ENABLE_WAMR_COMPILER != 0
  11264. module->is_ref_types_used = true;
  11265. #endif
  11266. break;
  11267. }
  11268. case WASM_OP_REF_NULL:
  11269. {
  11270. uint8 ref_type;
  11271. #if WASM_ENABLE_GC == 0
  11272. CHECK_BUF(p, p_end, 1);
  11273. ref_type = read_uint8(p);
  11274. if (ref_type != VALUE_TYPE_FUNCREF
  11275. && ref_type != VALUE_TYPE_EXTERNREF) {
  11276. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11277. goto fail;
  11278. }
  11279. #else
  11280. read_leb_int32(p, p_end, heap_type);
  11281. if (heap_type >= 0) {
  11282. if (!check_type_index(module, module->type_count, heap_type,
  11283. error_buf, error_buf_size)) {
  11284. goto fail;
  11285. }
  11286. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11287. true, heap_type);
  11288. ref_type = wasm_ref_type.ref_type;
  11289. }
  11290. else {
  11291. if (!wasm_is_valid_heap_type(heap_type)) {
  11292. set_error_buf(error_buf, error_buf_size,
  11293. "unknown type");
  11294. goto fail;
  11295. }
  11296. ref_type = (uint8)((int32)0x80 + heap_type);
  11297. }
  11298. #endif /* end of WASM_ENABLE_GC == 0 */
  11299. #if WASM_ENABLE_FAST_INTERP != 0
  11300. PUSH_OFFSET_TYPE(ref_type);
  11301. #endif
  11302. PUSH_TYPE(ref_type);
  11303. #if WASM_ENABLE_WAMR_COMPILER != 0
  11304. module->is_ref_types_used = true;
  11305. #endif
  11306. break;
  11307. }
  11308. case WASM_OP_REF_IS_NULL:
  11309. {
  11310. #if WASM_ENABLE_GC == 0
  11311. #if WASM_ENABLE_FAST_INTERP != 0
  11312. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11313. int32 block_stack_cell_num =
  11314. (int32)(loader_ctx->stack_cell_num
  11315. - cur_block->stack_cell_num);
  11316. if (block_stack_cell_num <= 0) {
  11317. if (!cur_block->is_stack_polymorphic) {
  11318. set_error_buf(
  11319. error_buf, error_buf_size,
  11320. "type mismatch: expect data but stack was empty");
  11321. goto fail;
  11322. }
  11323. }
  11324. else {
  11325. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11326. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11327. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11328. if (!wasm_loader_pop_frame_ref_offset(
  11329. loader_ctx, *(loader_ctx->frame_ref - 1),
  11330. error_buf, error_buf_size)) {
  11331. goto fail;
  11332. }
  11333. }
  11334. else {
  11335. set_error_buf(error_buf, error_buf_size,
  11336. "type mismatch");
  11337. goto fail;
  11338. }
  11339. }
  11340. #else
  11341. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11342. error_buf, error_buf_size)
  11343. && !wasm_loader_pop_frame_ref(loader_ctx,
  11344. VALUE_TYPE_EXTERNREF,
  11345. error_buf, error_buf_size)) {
  11346. goto fail;
  11347. }
  11348. #endif
  11349. #else /* else of WASM_ENABLE_GC == 0 */
  11350. uint8 type;
  11351. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11352. error_buf, error_buf_size)) {
  11353. goto fail;
  11354. }
  11355. #endif
  11356. PUSH_I32();
  11357. #if WASM_ENABLE_WAMR_COMPILER != 0
  11358. module->is_ref_types_used = true;
  11359. #endif
  11360. break;
  11361. }
  11362. case WASM_OP_REF_FUNC:
  11363. {
  11364. read_leb_uint32(p, p_end, func_idx);
  11365. if (!check_function_index(module, func_idx, error_buf,
  11366. error_buf_size)) {
  11367. goto fail;
  11368. }
  11369. /* Refer to a forward-declared function:
  11370. the function must be an import, exported, or present in
  11371. a table elem segment or global initializer to be used as
  11372. the operand to ref.func */
  11373. if (func_idx >= module->import_function_count) {
  11374. WASMTableSeg *table_seg = module->table_segments;
  11375. bool func_declared = false;
  11376. uint32 j;
  11377. for (i = 0; i < module->global_count; i++) {
  11378. if (module->globals[i].type.val_type
  11379. == VALUE_TYPE_FUNCREF
  11380. && module->globals[i].init_expr.init_expr_type
  11381. == INIT_EXPR_TYPE_FUNCREF_CONST
  11382. && module->globals[i].init_expr.u.u32 == func_idx) {
  11383. func_declared = true;
  11384. break;
  11385. }
  11386. }
  11387. if (!func_declared) {
  11388. /* Check whether the function is declared in table segs,
  11389. note that it doesn't matter whether the table seg's
  11390. mode is passive, active or declarative. */
  11391. for (i = 0; i < module->table_seg_count;
  11392. i++, table_seg++) {
  11393. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11394. #if WASM_ENABLE_GC != 0
  11395. /* elem type is (ref null? func) or
  11396. (ref null? $t) */
  11397. || ((table_seg->elem_type
  11398. == REF_TYPE_HT_NON_NULLABLE
  11399. || table_seg->elem_type
  11400. == REF_TYPE_HT_NULLABLE)
  11401. && (table_seg->elem_ref_type->ref_ht_common
  11402. .heap_type
  11403. == HEAP_TYPE_FUNC
  11404. || table_seg->elem_ref_type
  11405. ->ref_ht_common.heap_type
  11406. > 0))
  11407. #endif
  11408. ) {
  11409. for (j = 0; j < table_seg->value_count; j++) {
  11410. if (table_seg->init_values[j].u.ref_index
  11411. == func_idx) {
  11412. func_declared = true;
  11413. break;
  11414. }
  11415. }
  11416. }
  11417. }
  11418. }
  11419. if (!func_declared) {
  11420. /* Check whether the function is exported */
  11421. for (i = 0; i < module->export_count; i++) {
  11422. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11423. && module->exports[i].index == func_idx) {
  11424. func_declared = true;
  11425. break;
  11426. }
  11427. }
  11428. }
  11429. if (!func_declared) {
  11430. set_error_buf(error_buf, error_buf_size,
  11431. "undeclared function reference");
  11432. goto fail;
  11433. }
  11434. }
  11435. #if WASM_ENABLE_FAST_INTERP != 0
  11436. emit_uint32(loader_ctx, func_idx);
  11437. #endif
  11438. #if WASM_ENABLE_GC == 0
  11439. PUSH_FUNCREF();
  11440. #else
  11441. if (func_idx < module->import_function_count)
  11442. type_idx =
  11443. module->import_functions[func_idx].u.function.type_idx;
  11444. else
  11445. type_idx = module
  11446. ->functions[func_idx
  11447. - module->import_function_count]
  11448. ->type_idx;
  11449. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11450. false, type_idx);
  11451. PUSH_REF(wasm_ref_type.ref_type);
  11452. #endif
  11453. #if WASM_ENABLE_WAMR_COMPILER != 0
  11454. module->is_ref_types_used = true;
  11455. #endif
  11456. break;
  11457. }
  11458. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11459. #if WASM_ENABLE_GC != 0
  11460. case WASM_OP_REF_AS_NON_NULL:
  11461. case WASM_OP_BR_ON_NULL:
  11462. {
  11463. uint8 type;
  11464. WASMRefType ref_type;
  11465. /* POP (ref null ht) and get the converted (ref ht) */
  11466. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11467. error_buf, error_buf_size)) {
  11468. goto fail;
  11469. }
  11470. if (opcode == WASM_OP_BR_ON_NULL) {
  11471. if (!(frame_csp_tmp =
  11472. check_branch_block(loader_ctx, &p, p_end, opcode,
  11473. error_buf, error_buf_size))) {
  11474. goto fail;
  11475. }
  11476. #if WASM_ENABLE_FAST_INTERP != 0
  11477. disable_emit = true;
  11478. #endif
  11479. }
  11480. /* PUSH the converted (ref ht) */
  11481. if (type != VALUE_TYPE_ANY) {
  11482. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11483. sizeof(WASMRefType));
  11484. }
  11485. PUSH_REF(type);
  11486. break;
  11487. }
  11488. case WASM_OP_BR_ON_NON_NULL:
  11489. {
  11490. uint8 type;
  11491. WASMRefType ref_type;
  11492. uint32 available_stack_cell =
  11493. loader_ctx->stack_cell_num
  11494. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11495. /* POP (ref null ht) and get the converted (ref ht) */
  11496. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11497. error_buf, error_buf_size)) {
  11498. goto fail;
  11499. }
  11500. #if WASM_ENABLE_FAST_INTERP != 0
  11501. disable_emit = true;
  11502. #endif
  11503. /* Temporarily PUSH back (ref ht), check brach block and
  11504. then POP it */
  11505. if (available_stack_cell
  11506. > 0) { /* stack isn't in polymorphic state */
  11507. if (type != VALUE_TYPE_ANY) {
  11508. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11509. &ref_type, sizeof(WASMRefType));
  11510. }
  11511. PUSH_REF(type);
  11512. }
  11513. if (!(frame_csp_tmp =
  11514. check_branch_block(loader_ctx, &p, p_end, opcode,
  11515. error_buf, error_buf_size))) {
  11516. goto fail;
  11517. }
  11518. if (available_stack_cell
  11519. > 0) { /* stack isn't in polymorphic state */
  11520. POP_REF(type);
  11521. #if WASM_ENABLE_FAST_INTERP != 0
  11522. /* Erase the opnd offset emitted by POP_REF() */
  11523. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11524. #endif
  11525. }
  11526. break;
  11527. }
  11528. case WASM_OP_REF_EQ:
  11529. POP_REF(REF_TYPE_EQREF);
  11530. POP_REF(REF_TYPE_EQREF);
  11531. PUSH_I32();
  11532. break;
  11533. #endif /* end of WASM_ENABLE_GC != 0 */
  11534. case WASM_OP_GET_LOCAL:
  11535. {
  11536. p_org = p - 1;
  11537. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11538. PUSH_TYPE(local_type);
  11539. #if WASM_ENABLE_GC != 0
  11540. /* Cannot get a non-nullable and unset local */
  11541. if (local_idx >= param_count
  11542. && wasm_is_reftype_htref_non_nullable(local_type)
  11543. && !wasm_loader_get_local_status(loader_ctx,
  11544. local_idx - param_count)) {
  11545. set_error_buf(error_buf, error_buf_size,
  11546. "uninitialized local");
  11547. goto fail;
  11548. }
  11549. #endif
  11550. #if WASM_ENABLE_FAST_INTERP != 0
  11551. /* Get Local is optimized out */
  11552. skip_label();
  11553. disable_emit = true;
  11554. operand_offset = local_offset;
  11555. PUSH_OFFSET_TYPE(local_type);
  11556. #else
  11557. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11558. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11559. if (local_offset < 0x80
  11560. #if WASM_ENABLE_GC != 0
  11561. && !wasm_is_type_reftype(local_type)
  11562. #endif
  11563. ) {
  11564. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11565. if (is_32bit_type(local_type)) {
  11566. *p_org++ = (uint8)local_offset;
  11567. }
  11568. else {
  11569. *p_org++ = (uint8)(local_offset | 0x80);
  11570. }
  11571. while (p_org < p) {
  11572. *p_org++ = WASM_OP_NOP;
  11573. }
  11574. }
  11575. #endif
  11576. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11577. break;
  11578. }
  11579. case WASM_OP_SET_LOCAL:
  11580. {
  11581. p_org = p - 1;
  11582. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11583. #if WASM_ENABLE_FAST_INTERP != 0
  11584. if (!(preserve_referenced_local(
  11585. loader_ctx, opcode, local_offset, local_type,
  11586. &preserve_local, error_buf, error_buf_size)))
  11587. goto fail;
  11588. if (local_offset < 256
  11589. #if WASM_ENABLE_GC != 0
  11590. && !wasm_is_type_reftype(local_type)
  11591. #endif
  11592. ) {
  11593. skip_label();
  11594. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11595. if (loader_ctx->p_code_compiled)
  11596. STORE_U16(loader_ctx->p_code_compiled - 2,
  11597. local_offset);
  11598. loader_ctx->frame_offset--;
  11599. loader_ctx->dynamic_offset--;
  11600. }
  11601. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11602. if (loader_ctx->p_code_compiled)
  11603. STORE_U16(loader_ctx->p_code_compiled - 2,
  11604. local_offset);
  11605. loader_ctx->frame_offset -= 2;
  11606. loader_ctx->dynamic_offset -= 2;
  11607. }
  11608. else {
  11609. if (is_32bit_type(local_type)) {
  11610. emit_label(EXT_OP_SET_LOCAL_FAST);
  11611. emit_byte(loader_ctx, (uint8)local_offset);
  11612. }
  11613. else {
  11614. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11615. emit_byte(loader_ctx, (uint8)local_offset);
  11616. }
  11617. POP_OFFSET_TYPE(local_type);
  11618. }
  11619. }
  11620. else { /* local index larger than 255, reserve leb */
  11621. emit_uint32(loader_ctx, local_idx);
  11622. POP_OFFSET_TYPE(local_type);
  11623. }
  11624. #else
  11625. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11626. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11627. if (local_offset < 0x80
  11628. #if WASM_ENABLE_GC != 0
  11629. && !wasm_is_type_reftype(local_type)
  11630. #endif
  11631. ) {
  11632. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11633. if (is_32bit_type(local_type)) {
  11634. *p_org++ = (uint8)local_offset;
  11635. }
  11636. else {
  11637. *p_org++ = (uint8)(local_offset | 0x80);
  11638. }
  11639. while (p_org < p) {
  11640. *p_org++ = WASM_OP_NOP;
  11641. }
  11642. }
  11643. #endif
  11644. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11645. #if WASM_ENABLE_GC != 0
  11646. if (local_idx >= param_count) {
  11647. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11648. }
  11649. #endif
  11650. POP_TYPE(local_type);
  11651. break;
  11652. }
  11653. case WASM_OP_TEE_LOCAL:
  11654. {
  11655. p_org = p - 1;
  11656. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11657. #if WASM_ENABLE_FAST_INTERP != 0
  11658. /* If the stack is in polymorphic state, do fake pop and push on
  11659. offset stack to keep the depth of offset stack to be the
  11660. same with ref stack */
  11661. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11662. if (cur_block->is_stack_polymorphic) {
  11663. POP_OFFSET_TYPE(local_type);
  11664. PUSH_OFFSET_TYPE(local_type);
  11665. }
  11666. #endif
  11667. POP_TYPE(local_type);
  11668. PUSH_TYPE(local_type);
  11669. #if WASM_ENABLE_FAST_INTERP != 0
  11670. if (!(preserve_referenced_local(
  11671. loader_ctx, opcode, local_offset, local_type,
  11672. &preserve_local, error_buf, error_buf_size)))
  11673. goto fail;
  11674. if (local_offset < 256
  11675. #if WASM_ENABLE_GC != 0
  11676. && !wasm_is_type_reftype(local_type)
  11677. #endif
  11678. ) {
  11679. skip_label();
  11680. if (is_32bit_type(local_type)) {
  11681. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11682. emit_byte(loader_ctx, (uint8)local_offset);
  11683. }
  11684. else {
  11685. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11686. emit_byte(loader_ctx, (uint8)local_offset);
  11687. }
  11688. }
  11689. else { /* local index larger than 255, reserve leb */
  11690. emit_uint32(loader_ctx, local_idx);
  11691. }
  11692. emit_operand(loader_ctx,
  11693. *(loader_ctx->frame_offset
  11694. - wasm_value_type_cell_num(local_type)));
  11695. #else
  11696. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11697. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11698. if (local_offset < 0x80
  11699. #if WASM_ENABLE_GC != 0
  11700. && !wasm_is_type_reftype(local_type)
  11701. #endif
  11702. ) {
  11703. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11704. if (is_32bit_type(local_type)) {
  11705. *p_org++ = (uint8)local_offset;
  11706. }
  11707. else {
  11708. *p_org++ = (uint8)(local_offset | 0x80);
  11709. }
  11710. while (p_org < p) {
  11711. *p_org++ = WASM_OP_NOP;
  11712. }
  11713. }
  11714. #endif
  11715. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11716. #if WASM_ENABLE_GC != 0
  11717. if (local_idx >= param_count) {
  11718. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11719. }
  11720. #endif
  11721. break;
  11722. }
  11723. case WASM_OP_GET_GLOBAL:
  11724. {
  11725. #if WASM_ENABLE_GC != 0
  11726. WASMRefType *ref_type;
  11727. #endif
  11728. p_org = p - 1;
  11729. read_leb_uint32(p, p_end, global_idx);
  11730. if (global_idx >= global_count) {
  11731. set_error_buf(error_buf, error_buf_size, "unknown global");
  11732. goto fail;
  11733. }
  11734. global_type = global_idx < module->import_global_count
  11735. ? module->import_globals[global_idx]
  11736. .u.global.type.val_type
  11737. : module
  11738. ->globals[global_idx
  11739. - module->import_global_count]
  11740. .type.val_type;
  11741. #if WASM_ENABLE_GC != 0
  11742. ref_type =
  11743. global_idx < module->import_global_count
  11744. ? module->import_globals[global_idx].u.global.ref_type
  11745. : module
  11746. ->globals[global_idx
  11747. - module->import_global_count]
  11748. .ref_type;
  11749. if (wasm_is_type_multi_byte_type(global_type)) {
  11750. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11751. wasm_reftype_struct_size(ref_type));
  11752. }
  11753. #endif
  11754. PUSH_TYPE(global_type);
  11755. #if WASM_ENABLE_FAST_INTERP == 0
  11756. if (global_type == VALUE_TYPE_I64
  11757. || global_type == VALUE_TYPE_F64) {
  11758. #if WASM_ENABLE_DEBUG_INTERP != 0
  11759. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11760. error_buf_size)) {
  11761. goto fail;
  11762. }
  11763. #endif
  11764. *p_org = WASM_OP_GET_GLOBAL_64;
  11765. }
  11766. #else /* else of WASM_ENABLE_FAST_INTERP */
  11767. if (global_type == VALUE_TYPE_I64
  11768. || global_type == VALUE_TYPE_F64) {
  11769. skip_label();
  11770. emit_label(WASM_OP_GET_GLOBAL_64);
  11771. }
  11772. emit_uint32(loader_ctx, global_idx);
  11773. PUSH_OFFSET_TYPE(global_type);
  11774. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11775. break;
  11776. }
  11777. case WASM_OP_SET_GLOBAL:
  11778. {
  11779. bool is_mutable = false;
  11780. #if WASM_ENABLE_GC != 0
  11781. WASMRefType *ref_type;
  11782. #endif
  11783. p_org = p - 1;
  11784. read_leb_uint32(p, p_end, global_idx);
  11785. if (global_idx >= global_count) {
  11786. set_error_buf(error_buf, error_buf_size, "unknown global");
  11787. goto fail;
  11788. }
  11789. is_mutable = global_idx < module->import_global_count
  11790. ? module->import_globals[global_idx]
  11791. .u.global.type.is_mutable
  11792. : module
  11793. ->globals[global_idx
  11794. - module->import_global_count]
  11795. .type.is_mutable;
  11796. if (!is_mutable) {
  11797. #if WASM_ENABLE_GC == 0
  11798. set_error_buf(error_buf, error_buf_size,
  11799. "global is immutable");
  11800. #else
  11801. set_error_buf(error_buf, error_buf_size,
  11802. "immutable global");
  11803. #endif
  11804. goto fail;
  11805. }
  11806. global_type = global_idx < module->import_global_count
  11807. ? module->import_globals[global_idx]
  11808. .u.global.type.val_type
  11809. : module
  11810. ->globals[global_idx
  11811. - module->import_global_count]
  11812. .type.val_type;
  11813. #if WASM_ENABLE_GC != 0
  11814. ref_type =
  11815. global_idx < module->import_global_count
  11816. ? module->import_globals[global_idx].u.global.ref_type
  11817. : module
  11818. ->globals[global_idx
  11819. - module->import_global_count]
  11820. .ref_type;
  11821. if (wasm_is_type_multi_byte_type(global_type)) {
  11822. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11823. wasm_reftype_struct_size(ref_type));
  11824. }
  11825. #endif
  11826. #if WASM_ENABLE_FAST_INTERP == 0
  11827. if (global_type == VALUE_TYPE_I64
  11828. || global_type == VALUE_TYPE_F64) {
  11829. #if WASM_ENABLE_DEBUG_INTERP != 0
  11830. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11831. error_buf_size)) {
  11832. goto fail;
  11833. }
  11834. #endif
  11835. *p_org = WASM_OP_SET_GLOBAL_64;
  11836. }
  11837. else if (module->aux_stack_size > 0
  11838. && global_idx == module->aux_stack_top_global_index) {
  11839. #if WASM_ENABLE_DEBUG_INTERP != 0
  11840. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11841. error_buf_size)) {
  11842. goto fail;
  11843. }
  11844. #endif
  11845. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11846. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11847. func->has_op_set_global_aux_stack = true;
  11848. #endif
  11849. }
  11850. #else /* else of WASM_ENABLE_FAST_INTERP */
  11851. if (global_type == VALUE_TYPE_I64
  11852. || global_type == VALUE_TYPE_F64) {
  11853. skip_label();
  11854. emit_label(WASM_OP_SET_GLOBAL_64);
  11855. }
  11856. else if (module->aux_stack_size > 0
  11857. && global_idx == module->aux_stack_top_global_index) {
  11858. skip_label();
  11859. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11860. }
  11861. emit_uint32(loader_ctx, global_idx);
  11862. POP_OFFSET_TYPE(global_type);
  11863. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11864. POP_TYPE(global_type);
  11865. break;
  11866. }
  11867. /* load */
  11868. case WASM_OP_I32_LOAD:
  11869. case WASM_OP_I32_LOAD8_S:
  11870. case WASM_OP_I32_LOAD8_U:
  11871. case WASM_OP_I32_LOAD16_S:
  11872. case WASM_OP_I32_LOAD16_U:
  11873. case WASM_OP_I64_LOAD:
  11874. case WASM_OP_I64_LOAD8_S:
  11875. case WASM_OP_I64_LOAD8_U:
  11876. case WASM_OP_I64_LOAD16_S:
  11877. case WASM_OP_I64_LOAD16_U:
  11878. case WASM_OP_I64_LOAD32_S:
  11879. case WASM_OP_I64_LOAD32_U:
  11880. case WASM_OP_F32_LOAD:
  11881. case WASM_OP_F64_LOAD:
  11882. /* store */
  11883. case WASM_OP_I32_STORE:
  11884. case WASM_OP_I32_STORE8:
  11885. case WASM_OP_I32_STORE16:
  11886. case WASM_OP_I64_STORE:
  11887. case WASM_OP_I64_STORE8:
  11888. case WASM_OP_I64_STORE16:
  11889. case WASM_OP_I64_STORE32:
  11890. case WASM_OP_F32_STORE:
  11891. case WASM_OP_F64_STORE:
  11892. {
  11893. #if WASM_ENABLE_FAST_INTERP != 0
  11894. /* change F32/F64 into I32/I64 */
  11895. if (opcode == WASM_OP_F32_LOAD) {
  11896. skip_label();
  11897. emit_label(WASM_OP_I32_LOAD);
  11898. }
  11899. else if (opcode == WASM_OP_F64_LOAD) {
  11900. skip_label();
  11901. emit_label(WASM_OP_I64_LOAD);
  11902. }
  11903. else if (opcode == WASM_OP_F32_STORE) {
  11904. skip_label();
  11905. emit_label(WASM_OP_I32_STORE);
  11906. }
  11907. else if (opcode == WASM_OP_F64_STORE) {
  11908. skip_label();
  11909. emit_label(WASM_OP_I64_STORE);
  11910. }
  11911. #endif
  11912. CHECK_MEMORY();
  11913. read_leb_uint32(p, p_end, align); /* align */
  11914. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11915. if (!check_memory_access_align(opcode, align, error_buf,
  11916. error_buf_size)) {
  11917. goto fail;
  11918. }
  11919. #if WASM_ENABLE_FAST_INTERP != 0
  11920. emit_uint32(loader_ctx, mem_offset);
  11921. #endif
  11922. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11923. func->has_memory_operations = true;
  11924. #endif
  11925. switch (opcode) {
  11926. /* load */
  11927. case WASM_OP_I32_LOAD:
  11928. case WASM_OP_I32_LOAD8_S:
  11929. case WASM_OP_I32_LOAD8_U:
  11930. case WASM_OP_I32_LOAD16_S:
  11931. case WASM_OP_I32_LOAD16_U:
  11932. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11933. break;
  11934. case WASM_OP_I64_LOAD:
  11935. case WASM_OP_I64_LOAD8_S:
  11936. case WASM_OP_I64_LOAD8_U:
  11937. case WASM_OP_I64_LOAD16_S:
  11938. case WASM_OP_I64_LOAD16_U:
  11939. case WASM_OP_I64_LOAD32_S:
  11940. case WASM_OP_I64_LOAD32_U:
  11941. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11942. break;
  11943. case WASM_OP_F32_LOAD:
  11944. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11945. break;
  11946. case WASM_OP_F64_LOAD:
  11947. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11948. break;
  11949. /* store */
  11950. case WASM_OP_I32_STORE:
  11951. case WASM_OP_I32_STORE8:
  11952. case WASM_OP_I32_STORE16:
  11953. POP_I32();
  11954. POP_MEM_OFFSET();
  11955. break;
  11956. case WASM_OP_I64_STORE:
  11957. case WASM_OP_I64_STORE8:
  11958. case WASM_OP_I64_STORE16:
  11959. case WASM_OP_I64_STORE32:
  11960. POP_I64();
  11961. POP_MEM_OFFSET();
  11962. break;
  11963. case WASM_OP_F32_STORE:
  11964. POP_F32();
  11965. POP_MEM_OFFSET();
  11966. break;
  11967. case WASM_OP_F64_STORE:
  11968. POP_F64();
  11969. POP_MEM_OFFSET();
  11970. break;
  11971. default:
  11972. break;
  11973. }
  11974. break;
  11975. }
  11976. case WASM_OP_MEMORY_SIZE:
  11977. CHECK_MEMORY();
  11978. /* reserved byte 0x00 */
  11979. if (*p++ != 0x00) {
  11980. set_error_buf(error_buf, error_buf_size,
  11981. "zero byte expected");
  11982. goto fail;
  11983. }
  11984. PUSH_PAGE_COUNT();
  11985. module->possible_memory_grow = true;
  11986. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11987. func->has_memory_operations = true;
  11988. #endif
  11989. break;
  11990. case WASM_OP_MEMORY_GROW:
  11991. CHECK_MEMORY();
  11992. /* reserved byte 0x00 */
  11993. if (*p++ != 0x00) {
  11994. set_error_buf(error_buf, error_buf_size,
  11995. "zero byte expected");
  11996. goto fail;
  11997. }
  11998. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11999. module->possible_memory_grow = true;
  12000. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12001. || WASM_ENABLE_WAMR_COMPILER != 0
  12002. func->has_op_memory_grow = true;
  12003. #endif
  12004. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12005. func->has_memory_operations = true;
  12006. #endif
  12007. break;
  12008. case WASM_OP_I32_CONST:
  12009. read_leb_int32(p, p_end, i32_const);
  12010. #if WASM_ENABLE_FAST_INTERP != 0
  12011. skip_label();
  12012. disable_emit = true;
  12013. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12014. if (operand_offset == 0) {
  12015. disable_emit = false;
  12016. emit_label(WASM_OP_I32_CONST);
  12017. emit_uint32(loader_ctx, i32_const);
  12018. }
  12019. #else
  12020. (void)i32_const;
  12021. #endif
  12022. PUSH_I32();
  12023. break;
  12024. case WASM_OP_I64_CONST:
  12025. read_leb_int64(p, p_end, i64_const);
  12026. #if WASM_ENABLE_FAST_INTERP != 0
  12027. skip_label();
  12028. disable_emit = true;
  12029. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12030. if (operand_offset == 0) {
  12031. disable_emit = false;
  12032. emit_label(WASM_OP_I64_CONST);
  12033. emit_uint64(loader_ctx, i64_const);
  12034. }
  12035. #endif
  12036. PUSH_I64();
  12037. break;
  12038. case WASM_OP_F32_CONST:
  12039. CHECK_BUF(p, p_end, sizeof(float32));
  12040. p += sizeof(float32);
  12041. #if WASM_ENABLE_FAST_INTERP != 0
  12042. skip_label();
  12043. disable_emit = true;
  12044. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12045. sizeof(float32));
  12046. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12047. if (operand_offset == 0) {
  12048. disable_emit = false;
  12049. emit_label(WASM_OP_F32_CONST);
  12050. emit_float32(loader_ctx, f32_const);
  12051. }
  12052. #endif
  12053. PUSH_F32();
  12054. break;
  12055. case WASM_OP_F64_CONST:
  12056. CHECK_BUF(p, p_end, sizeof(float64));
  12057. p += sizeof(float64);
  12058. #if WASM_ENABLE_FAST_INTERP != 0
  12059. skip_label();
  12060. disable_emit = true;
  12061. /* Some MCU may require 8-byte align */
  12062. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12063. sizeof(float64));
  12064. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12065. if (operand_offset == 0) {
  12066. disable_emit = false;
  12067. emit_label(WASM_OP_F64_CONST);
  12068. emit_float64(loader_ctx, f64_const);
  12069. }
  12070. #endif
  12071. PUSH_F64();
  12072. break;
  12073. case WASM_OP_I32_EQZ:
  12074. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12075. break;
  12076. case WASM_OP_I32_EQ:
  12077. case WASM_OP_I32_NE:
  12078. case WASM_OP_I32_LT_S:
  12079. case WASM_OP_I32_LT_U:
  12080. case WASM_OP_I32_GT_S:
  12081. case WASM_OP_I32_GT_U:
  12082. case WASM_OP_I32_LE_S:
  12083. case WASM_OP_I32_LE_U:
  12084. case WASM_OP_I32_GE_S:
  12085. case WASM_OP_I32_GE_U:
  12086. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12087. break;
  12088. case WASM_OP_I64_EQZ:
  12089. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12090. break;
  12091. case WASM_OP_I64_EQ:
  12092. case WASM_OP_I64_NE:
  12093. case WASM_OP_I64_LT_S:
  12094. case WASM_OP_I64_LT_U:
  12095. case WASM_OP_I64_GT_S:
  12096. case WASM_OP_I64_GT_U:
  12097. case WASM_OP_I64_LE_S:
  12098. case WASM_OP_I64_LE_U:
  12099. case WASM_OP_I64_GE_S:
  12100. case WASM_OP_I64_GE_U:
  12101. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12102. break;
  12103. case WASM_OP_F32_EQ:
  12104. case WASM_OP_F32_NE:
  12105. case WASM_OP_F32_LT:
  12106. case WASM_OP_F32_GT:
  12107. case WASM_OP_F32_LE:
  12108. case WASM_OP_F32_GE:
  12109. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12110. break;
  12111. case WASM_OP_F64_EQ:
  12112. case WASM_OP_F64_NE:
  12113. case WASM_OP_F64_LT:
  12114. case WASM_OP_F64_GT:
  12115. case WASM_OP_F64_LE:
  12116. case WASM_OP_F64_GE:
  12117. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12118. break;
  12119. case WASM_OP_I32_CLZ:
  12120. case WASM_OP_I32_CTZ:
  12121. case WASM_OP_I32_POPCNT:
  12122. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12123. break;
  12124. case WASM_OP_I32_ADD:
  12125. case WASM_OP_I32_SUB:
  12126. case WASM_OP_I32_MUL:
  12127. case WASM_OP_I32_DIV_S:
  12128. case WASM_OP_I32_DIV_U:
  12129. case WASM_OP_I32_REM_S:
  12130. case WASM_OP_I32_REM_U:
  12131. case WASM_OP_I32_AND:
  12132. case WASM_OP_I32_OR:
  12133. case WASM_OP_I32_XOR:
  12134. case WASM_OP_I32_SHL:
  12135. case WASM_OP_I32_SHR_S:
  12136. case WASM_OP_I32_SHR_U:
  12137. case WASM_OP_I32_ROTL:
  12138. case WASM_OP_I32_ROTR:
  12139. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12140. break;
  12141. case WASM_OP_I64_CLZ:
  12142. case WASM_OP_I64_CTZ:
  12143. case WASM_OP_I64_POPCNT:
  12144. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12145. break;
  12146. case WASM_OP_I64_ADD:
  12147. case WASM_OP_I64_SUB:
  12148. case WASM_OP_I64_MUL:
  12149. case WASM_OP_I64_DIV_S:
  12150. case WASM_OP_I64_DIV_U:
  12151. case WASM_OP_I64_REM_S:
  12152. case WASM_OP_I64_REM_U:
  12153. case WASM_OP_I64_AND:
  12154. case WASM_OP_I64_OR:
  12155. case WASM_OP_I64_XOR:
  12156. case WASM_OP_I64_SHL:
  12157. case WASM_OP_I64_SHR_S:
  12158. case WASM_OP_I64_SHR_U:
  12159. case WASM_OP_I64_ROTL:
  12160. case WASM_OP_I64_ROTR:
  12161. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12162. break;
  12163. case WASM_OP_F32_ABS:
  12164. case WASM_OP_F32_NEG:
  12165. case WASM_OP_F32_CEIL:
  12166. case WASM_OP_F32_FLOOR:
  12167. case WASM_OP_F32_TRUNC:
  12168. case WASM_OP_F32_NEAREST:
  12169. case WASM_OP_F32_SQRT:
  12170. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12171. break;
  12172. case WASM_OP_F32_ADD:
  12173. case WASM_OP_F32_SUB:
  12174. case WASM_OP_F32_MUL:
  12175. case WASM_OP_F32_DIV:
  12176. case WASM_OP_F32_MIN:
  12177. case WASM_OP_F32_MAX:
  12178. case WASM_OP_F32_COPYSIGN:
  12179. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12180. break;
  12181. case WASM_OP_F64_ABS:
  12182. case WASM_OP_F64_NEG:
  12183. case WASM_OP_F64_CEIL:
  12184. case WASM_OP_F64_FLOOR:
  12185. case WASM_OP_F64_TRUNC:
  12186. case WASM_OP_F64_NEAREST:
  12187. case WASM_OP_F64_SQRT:
  12188. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12189. break;
  12190. case WASM_OP_F64_ADD:
  12191. case WASM_OP_F64_SUB:
  12192. case WASM_OP_F64_MUL:
  12193. case WASM_OP_F64_DIV:
  12194. case WASM_OP_F64_MIN:
  12195. case WASM_OP_F64_MAX:
  12196. case WASM_OP_F64_COPYSIGN:
  12197. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12198. break;
  12199. case WASM_OP_I32_WRAP_I64:
  12200. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12201. break;
  12202. case WASM_OP_I32_TRUNC_S_F32:
  12203. case WASM_OP_I32_TRUNC_U_F32:
  12204. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12205. break;
  12206. case WASM_OP_I32_TRUNC_S_F64:
  12207. case WASM_OP_I32_TRUNC_U_F64:
  12208. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12209. break;
  12210. case WASM_OP_I64_EXTEND_S_I32:
  12211. case WASM_OP_I64_EXTEND_U_I32:
  12212. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12213. break;
  12214. case WASM_OP_I64_TRUNC_S_F32:
  12215. case WASM_OP_I64_TRUNC_U_F32:
  12216. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12217. break;
  12218. case WASM_OP_I64_TRUNC_S_F64:
  12219. case WASM_OP_I64_TRUNC_U_F64:
  12220. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12221. break;
  12222. case WASM_OP_F32_CONVERT_S_I32:
  12223. case WASM_OP_F32_CONVERT_U_I32:
  12224. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12225. break;
  12226. case WASM_OP_F32_CONVERT_S_I64:
  12227. case WASM_OP_F32_CONVERT_U_I64:
  12228. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12229. break;
  12230. case WASM_OP_F32_DEMOTE_F64:
  12231. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12232. break;
  12233. case WASM_OP_F64_CONVERT_S_I32:
  12234. case WASM_OP_F64_CONVERT_U_I32:
  12235. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12236. break;
  12237. case WASM_OP_F64_CONVERT_S_I64:
  12238. case WASM_OP_F64_CONVERT_U_I64:
  12239. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12240. break;
  12241. case WASM_OP_F64_PROMOTE_F32:
  12242. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12243. break;
  12244. case WASM_OP_I32_REINTERPRET_F32:
  12245. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12246. break;
  12247. case WASM_OP_I64_REINTERPRET_F64:
  12248. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12249. break;
  12250. case WASM_OP_F32_REINTERPRET_I32:
  12251. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12252. break;
  12253. case WASM_OP_F64_REINTERPRET_I64:
  12254. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12255. break;
  12256. case WASM_OP_I32_EXTEND8_S:
  12257. case WASM_OP_I32_EXTEND16_S:
  12258. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12259. break;
  12260. case WASM_OP_I64_EXTEND8_S:
  12261. case WASM_OP_I64_EXTEND16_S:
  12262. case WASM_OP_I64_EXTEND32_S:
  12263. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12264. break;
  12265. #if WASM_ENABLE_GC != 0
  12266. case WASM_OP_GC_PREFIX:
  12267. {
  12268. uint32 opcode1;
  12269. read_leb_uint32(p, p_end, opcode1);
  12270. #if WASM_ENABLE_FAST_INTERP != 0
  12271. emit_byte(loader_ctx, ((uint8)opcode1));
  12272. #endif
  12273. switch (opcode1) {
  12274. case WASM_OP_STRUCT_NEW:
  12275. case WASM_OP_STRUCT_NEW_DEFAULT:
  12276. {
  12277. read_leb_uint32(p, p_end, type_idx);
  12278. #if WASM_ENABLE_FAST_INTERP != 0
  12279. emit_uint32(loader_ctx, type_idx);
  12280. #endif
  12281. if (!check_type_index(module, module->type_count,
  12282. type_idx, error_buf,
  12283. error_buf_size)) {
  12284. goto fail;
  12285. }
  12286. if (module->types[type_idx]->type_flag
  12287. != WASM_TYPE_STRUCT) {
  12288. set_error_buf(error_buf, error_buf_size,
  12289. "unkown struct type");
  12290. goto fail;
  12291. }
  12292. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12293. int32 j, k;
  12294. uint8 value_type;
  12295. uint32 ref_type_struct_size;
  12296. WASMStructType *struct_type =
  12297. (WASMStructType *)module->types[type_idx];
  12298. k = struct_type->ref_type_map_count - 1;
  12299. for (j = struct_type->field_count - 1; j >= 0;
  12300. j--) {
  12301. value_type = struct_type->fields[j].field_type;
  12302. if (wasm_is_type_reftype(value_type)) {
  12303. if (wasm_is_type_multi_byte_type(
  12304. value_type)) {
  12305. ref_type_struct_size =
  12306. wasm_reftype_struct_size(
  12307. struct_type->ref_type_maps[k]
  12308. .ref_type);
  12309. bh_memcpy_s(
  12310. &wasm_ref_type,
  12311. (uint32)sizeof(WASMRefType),
  12312. struct_type->ref_type_maps[k]
  12313. .ref_type,
  12314. ref_type_struct_size);
  12315. k--;
  12316. }
  12317. POP_REF(value_type);
  12318. }
  12319. else {
  12320. switch (value_type) {
  12321. case VALUE_TYPE_I32:
  12322. case PACKED_TYPE_I8:
  12323. case PACKED_TYPE_I16:
  12324. POP_I32();
  12325. break;
  12326. case VALUE_TYPE_I64:
  12327. POP_I64();
  12328. break;
  12329. case VALUE_TYPE_F32:
  12330. POP_F32();
  12331. break;
  12332. case VALUE_TYPE_F64:
  12333. POP_F64();
  12334. break;
  12335. default:
  12336. set_error_buf(error_buf,
  12337. error_buf_size,
  12338. "unknown type");
  12339. goto fail;
  12340. }
  12341. }
  12342. }
  12343. }
  12344. /* PUSH struct obj, (ref $t) */
  12345. wasm_set_refheaptype_typeidx(
  12346. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12347. PUSH_REF(wasm_ref_type.ref_type);
  12348. break;
  12349. }
  12350. case WASM_OP_STRUCT_GET:
  12351. case WASM_OP_STRUCT_GET_S:
  12352. case WASM_OP_STRUCT_GET_U:
  12353. case WASM_OP_STRUCT_SET:
  12354. {
  12355. WASMStructType *struct_type;
  12356. WASMRefType *ref_type = NULL;
  12357. uint32 field_idx;
  12358. uint8 field_type;
  12359. read_leb_uint32(p, p_end, type_idx);
  12360. #if WASM_ENABLE_FAST_INTERP != 0
  12361. emit_uint32(loader_ctx, type_idx);
  12362. #endif
  12363. if (!check_type_index(module, module->type_count,
  12364. type_idx, error_buf,
  12365. error_buf_size)) {
  12366. goto fail;
  12367. }
  12368. if (module->types[type_idx]->type_flag
  12369. != WASM_TYPE_STRUCT) {
  12370. set_error_buf(error_buf, error_buf_size,
  12371. "unknown struct type");
  12372. goto fail;
  12373. }
  12374. struct_type = (WASMStructType *)module->types[type_idx];
  12375. read_leb_uint32(p, p_end, field_idx);
  12376. #if WASM_ENABLE_FAST_INTERP != 0
  12377. emit_uint32(loader_ctx, field_idx);
  12378. #endif
  12379. if (field_idx >= struct_type->field_count) {
  12380. set_error_buf(error_buf, error_buf_size,
  12381. "unknown struct field");
  12382. goto fail;
  12383. }
  12384. if (opcode1 == WASM_OP_STRUCT_SET
  12385. && !(struct_type->fields[field_idx].field_flags
  12386. & 1)) {
  12387. set_error_buf(error_buf, error_buf_size,
  12388. "field is immutable");
  12389. goto fail;
  12390. }
  12391. field_type = struct_type->fields[field_idx].field_type;
  12392. if (is_packed_type(field_type)) {
  12393. if (opcode1 == WASM_OP_STRUCT_GET) {
  12394. set_error_buf(error_buf, error_buf_size,
  12395. "type mismatch");
  12396. goto fail;
  12397. }
  12398. else {
  12399. field_type = VALUE_TYPE_I32;
  12400. }
  12401. }
  12402. if (wasm_is_type_multi_byte_type(field_type)) {
  12403. ref_type = wasm_reftype_map_find(
  12404. struct_type->ref_type_maps,
  12405. struct_type->ref_type_map_count, field_idx);
  12406. bh_assert(ref_type);
  12407. }
  12408. if (opcode1 == WASM_OP_STRUCT_SET) {
  12409. /* POP field */
  12410. if (wasm_is_type_multi_byte_type(field_type)) {
  12411. bh_memcpy_s(&wasm_ref_type,
  12412. (uint32)sizeof(WASMRefType),
  12413. ref_type,
  12414. wasm_reftype_struct_size(ref_type));
  12415. }
  12416. POP_REF(field_type);
  12417. /* POP struct obj, (ref null $t) */
  12418. wasm_set_refheaptype_typeidx(
  12419. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12420. POP_REF(wasm_ref_type.ref_type);
  12421. }
  12422. else {
  12423. /* POP struct obj, (ref null $t) */
  12424. wasm_set_refheaptype_typeidx(
  12425. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12426. POP_REF(wasm_ref_type.ref_type);
  12427. /* PUSH field */
  12428. if (wasm_is_type_multi_byte_type(field_type)) {
  12429. bh_memcpy_s(&wasm_ref_type,
  12430. (uint32)sizeof(WASMRefType),
  12431. ref_type,
  12432. wasm_reftype_struct_size(ref_type));
  12433. }
  12434. PUSH_REF(field_type);
  12435. }
  12436. break;
  12437. }
  12438. case WASM_OP_ARRAY_NEW:
  12439. case WASM_OP_ARRAY_NEW_DEFAULT:
  12440. case WASM_OP_ARRAY_NEW_FIXED:
  12441. case WASM_OP_ARRAY_NEW_DATA:
  12442. case WASM_OP_ARRAY_NEW_ELEM:
  12443. {
  12444. WASMArrayType *array_type;
  12445. uint8 elem_type;
  12446. uint32 u32 = 0;
  12447. read_leb_uint32(p, p_end, type_idx);
  12448. #if WASM_ENABLE_FAST_INTERP != 0
  12449. emit_uint32(loader_ctx, type_idx);
  12450. #endif
  12451. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12452. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12453. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12454. read_leb_uint32(p, p_end, u32);
  12455. #if WASM_ENABLE_FAST_INTERP != 0
  12456. emit_uint32(loader_ctx, u32);
  12457. #endif
  12458. }
  12459. if (!check_array_type(module, type_idx, error_buf,
  12460. error_buf_size)) {
  12461. goto fail;
  12462. }
  12463. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12464. /* length */
  12465. POP_I32();
  12466. }
  12467. array_type = (WASMArrayType *)module->types[type_idx];
  12468. elem_type = array_type->elem_type;
  12469. if (opcode1 == WASM_OP_ARRAY_NEW
  12470. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12471. if (wasm_is_type_multi_byte_type(elem_type)) {
  12472. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12473. array_type->elem_ref_type,
  12474. wasm_reftype_struct_size(
  12475. array_type->elem_ref_type));
  12476. }
  12477. if (is_packed_type(elem_type)) {
  12478. elem_type = VALUE_TYPE_I32;
  12479. }
  12480. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12481. uint32 N = u32;
  12482. for (i = 0; i < N; i++) {
  12483. if (wasm_is_type_multi_byte_type(
  12484. elem_type)) {
  12485. bh_memcpy_s(
  12486. &wasm_ref_type, sizeof(WASMRefType),
  12487. array_type->elem_ref_type,
  12488. wasm_reftype_struct_size(
  12489. array_type->elem_ref_type));
  12490. }
  12491. POP_REF(elem_type);
  12492. }
  12493. }
  12494. else
  12495. POP_REF(elem_type);
  12496. }
  12497. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12498. /* offset of data segment */
  12499. POP_I32();
  12500. if (u32 >= module->data_seg_count) {
  12501. set_error_buf(error_buf, error_buf_size,
  12502. "unknown data segment");
  12503. goto fail;
  12504. }
  12505. if (wasm_is_type_reftype(elem_type)) {
  12506. set_error_buf(error_buf, error_buf_size,
  12507. "array elem type mismatch");
  12508. goto fail;
  12509. }
  12510. }
  12511. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12512. WASMTableSeg *table_seg =
  12513. module->table_segments + u32;
  12514. /* offset of element segment */
  12515. POP_I32();
  12516. if (u32 >= module->table_seg_count) {
  12517. set_error_buf(error_buf, error_buf_size,
  12518. "unknown element segment");
  12519. goto fail;
  12520. }
  12521. if (!wasm_reftype_is_subtype_of(
  12522. table_seg->elem_type,
  12523. table_seg->elem_ref_type, elem_type,
  12524. array_type->elem_ref_type, module->types,
  12525. module->type_count)) {
  12526. set_error_buf(error_buf, error_buf_size,
  12527. "array elem type mismatch");
  12528. goto fail;
  12529. }
  12530. }
  12531. /* PUSH array obj, (ref $t) */
  12532. wasm_set_refheaptype_typeidx(
  12533. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12534. PUSH_REF(wasm_ref_type.ref_type);
  12535. break;
  12536. }
  12537. case WASM_OP_ARRAY_GET:
  12538. case WASM_OP_ARRAY_GET_S:
  12539. case WASM_OP_ARRAY_GET_U:
  12540. case WASM_OP_ARRAY_SET:
  12541. {
  12542. uint8 elem_type;
  12543. WASMArrayType *array_type;
  12544. WASMRefType *ref_type = NULL;
  12545. read_leb_uint32(p, p_end, type_idx);
  12546. #if WASM_ENABLE_FAST_INTERP != 0
  12547. emit_uint32(loader_ctx, type_idx);
  12548. #endif
  12549. if (!check_array_type(module, type_idx, error_buf,
  12550. error_buf_size)) {
  12551. goto fail;
  12552. }
  12553. array_type = (WASMArrayType *)module->types[type_idx];
  12554. if (opcode1 == WASM_OP_ARRAY_SET
  12555. && !(array_type->elem_flags & 1)) {
  12556. set_error_buf(error_buf, error_buf_size,
  12557. "array is immutable");
  12558. goto fail;
  12559. }
  12560. elem_type = array_type->elem_type;
  12561. if (is_packed_type(elem_type)) {
  12562. if (opcode1 != WASM_OP_ARRAY_GET_S
  12563. && opcode1 != WASM_OP_ARRAY_GET_U
  12564. && opcode1 != WASM_OP_ARRAY_SET) {
  12565. set_error_buf(error_buf, error_buf_size,
  12566. "type mismatch");
  12567. goto fail;
  12568. }
  12569. else {
  12570. elem_type = VALUE_TYPE_I32;
  12571. }
  12572. }
  12573. ref_type = array_type->elem_ref_type;
  12574. if (opcode1 == WASM_OP_ARRAY_SET) {
  12575. /* POP elem to set */
  12576. if (wasm_is_type_multi_byte_type(elem_type)) {
  12577. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12578. ref_type,
  12579. wasm_reftype_struct_size(ref_type));
  12580. }
  12581. POP_REF(elem_type);
  12582. }
  12583. /* elem idx */
  12584. POP_I32();
  12585. /* POP array obj, (ref null $t) */
  12586. wasm_set_refheaptype_typeidx(
  12587. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12588. POP_REF(wasm_ref_type.ref_type);
  12589. if (opcode1 != WASM_OP_ARRAY_SET) {
  12590. /* PUSH elem */
  12591. if (wasm_is_type_multi_byte_type(elem_type)) {
  12592. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12593. ref_type,
  12594. wasm_reftype_struct_size(ref_type));
  12595. }
  12596. PUSH_REF(elem_type);
  12597. }
  12598. break;
  12599. }
  12600. case WASM_OP_ARRAY_LEN:
  12601. {
  12602. POP_REF(REF_TYPE_ARRAYREF);
  12603. /* length */
  12604. PUSH_I32();
  12605. break;
  12606. }
  12607. case WASM_OP_ARRAY_FILL:
  12608. {
  12609. WASMArrayType *array_type;
  12610. uint8 elem_type;
  12611. /* typeidx */
  12612. read_leb_uint32(p, p_end, type_idx);
  12613. #if WASM_ENABLE_FAST_INTERP != 0
  12614. emit_uint32(loader_ctx, type_idx);
  12615. #endif
  12616. if (!check_array_type(module, type_idx, error_buf,
  12617. error_buf_size)) {
  12618. goto fail;
  12619. }
  12620. array_type = (WASMArrayType *)module->types[type_idx];
  12621. if (!(array_type->elem_flags & 1)) {
  12622. set_error_buf(error_buf, error_buf_size,
  12623. "array is immutable");
  12624. goto fail;
  12625. }
  12626. elem_type = array_type->elem_type;
  12627. if (is_packed_type(elem_type)) {
  12628. elem_type = VALUE_TYPE_I32;
  12629. }
  12630. POP_I32(); /* length */
  12631. #if WASM_ENABLE_FAST_INTERP != 0
  12632. POP_OFFSET_TYPE(elem_type);
  12633. #endif
  12634. POP_TYPE(elem_type);
  12635. POP_I32(); /* start */
  12636. /* POP array obj, (ref null $t) */
  12637. wasm_set_refheaptype_typeidx(
  12638. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12639. POP_REF(wasm_ref_type.ref_type);
  12640. break;
  12641. }
  12642. case WASM_OP_ARRAY_COPY:
  12643. {
  12644. uint32 src_type_idx;
  12645. uint8 src_elem_type, dst_elem_type;
  12646. WASMRefType src_ref_type = { 0 },
  12647. *src_elem_ref_type = NULL;
  12648. WASMRefType dst_ref_type = { 0 },
  12649. *dst_elem_ref_type = NULL;
  12650. WASMArrayType *array_type;
  12651. /* typeidx1 */
  12652. read_leb_uint32(p, p_end, type_idx);
  12653. #if WASM_ENABLE_FAST_INTERP != 0
  12654. emit_uint32(loader_ctx, type_idx);
  12655. #endif
  12656. /* typeidx2 */
  12657. read_leb_uint32(p, p_end, src_type_idx);
  12658. #if WASM_ENABLE_FAST_INTERP != 0
  12659. emit_uint32(loader_ctx, src_type_idx);
  12660. #endif
  12661. if (!check_array_type(module, type_idx, error_buf,
  12662. error_buf_size)) {
  12663. goto fail;
  12664. }
  12665. if (!check_array_type(module, src_type_idx, error_buf,
  12666. error_buf_size)) {
  12667. goto fail;
  12668. }
  12669. POP_I32();
  12670. POP_I32();
  12671. /* POP array obj, (ref null $t) */
  12672. wasm_set_refheaptype_typeidx(
  12673. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12674. POP_REF(wasm_ref_type.ref_type);
  12675. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12676. &wasm_ref_type,
  12677. wasm_reftype_struct_size(&wasm_ref_type));
  12678. POP_I32();
  12679. /* POP array obj, (ref null $t) */
  12680. wasm_set_refheaptype_typeidx(
  12681. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12682. POP_REF(wasm_ref_type.ref_type);
  12683. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12684. &wasm_ref_type,
  12685. wasm_reftype_struct_size(&wasm_ref_type));
  12686. array_type = (WASMArrayType *)module->types[type_idx];
  12687. if (!(array_type->elem_flags & 1)) {
  12688. set_error_buf(error_buf, error_buf_size,
  12689. "destination array is immutable");
  12690. goto fail;
  12691. }
  12692. dst_elem_type = array_type->elem_type;
  12693. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12694. dst_elem_ref_type = array_type->elem_ref_type;
  12695. }
  12696. array_type =
  12697. (WASMArrayType *)module->types[src_type_idx];
  12698. src_elem_type = array_type->elem_type;
  12699. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12700. src_elem_ref_type = array_type->elem_ref_type;
  12701. }
  12702. if (!wasm_reftype_is_subtype_of(
  12703. src_elem_type, src_elem_ref_type, dst_elem_type,
  12704. dst_elem_ref_type, module->types,
  12705. module->type_count)) {
  12706. set_error_buf(error_buf, error_buf_size,
  12707. "array types do not match");
  12708. goto fail;
  12709. }
  12710. break;
  12711. }
  12712. case WASM_OP_REF_I31:
  12713. {
  12714. POP_I32();
  12715. wasm_set_refheaptype_common(
  12716. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12717. PUSH_REF(wasm_ref_type.ref_type);
  12718. break;
  12719. }
  12720. case WASM_OP_I31_GET_S:
  12721. case WASM_OP_I31_GET_U:
  12722. {
  12723. POP_REF(REF_TYPE_I31REF);
  12724. PUSH_I32();
  12725. break;
  12726. }
  12727. case WASM_OP_REF_TEST:
  12728. case WASM_OP_REF_CAST:
  12729. case WASM_OP_REF_TEST_NULLABLE:
  12730. case WASM_OP_REF_CAST_NULLABLE:
  12731. {
  12732. uint8 type;
  12733. read_leb_int32(p, p_end, heap_type);
  12734. #if WASM_ENABLE_FAST_INTERP != 0
  12735. emit_uint32(loader_ctx, (uint32)heap_type);
  12736. #endif
  12737. if (heap_type >= 0) {
  12738. if (!check_type_index(module, module->type_count,
  12739. heap_type, error_buf,
  12740. error_buf_size)) {
  12741. goto fail;
  12742. }
  12743. }
  12744. else {
  12745. if (!wasm_is_valid_heap_type(heap_type)) {
  12746. set_error_buf(error_buf, error_buf_size,
  12747. "unknown type");
  12748. goto fail;
  12749. }
  12750. }
  12751. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12752. &wasm_ref_type, error_buf,
  12753. error_buf_size)) {
  12754. goto fail;
  12755. }
  12756. if (opcode1 == WASM_OP_REF_TEST
  12757. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12758. PUSH_I32();
  12759. else {
  12760. bool nullable =
  12761. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12762. : false;
  12763. if (heap_type >= 0 || !nullable) {
  12764. wasm_set_refheaptype_typeidx(
  12765. &wasm_ref_type.ref_ht_typeidx, nullable,
  12766. heap_type);
  12767. PUSH_REF(wasm_ref_type.ref_type);
  12768. }
  12769. else {
  12770. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12771. }
  12772. }
  12773. break;
  12774. }
  12775. case WASM_OP_BR_ON_CAST:
  12776. case WASM_OP_BR_ON_CAST_FAIL:
  12777. {
  12778. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12779. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12780. uint8 type_tmp, castflags;
  12781. uint32 depth;
  12782. int32 heap_type_dst;
  12783. bool src_nullable, dst_nullable;
  12784. CHECK_BUF(p, p_end, 1);
  12785. castflags = read_uint8(p);
  12786. #if WASM_ENABLE_FAST_INTERP != 0
  12787. /* Emit heap_type firstly */
  12788. emit_byte(loader_ctx, castflags);
  12789. #endif
  12790. p_org = p;
  12791. read_leb_uint32(p, p_end, depth);
  12792. read_leb_int32(p, p_end, heap_type);
  12793. #if WASM_ENABLE_FAST_INTERP != 0
  12794. /* Emit heap_type firstly */
  12795. emit_uint32(loader_ctx, (uint32)heap_type);
  12796. #endif
  12797. read_leb_int32(p, p_end, heap_type_dst);
  12798. #if WASM_ENABLE_FAST_INTERP != 0
  12799. /* Emit heap_type firstly */
  12800. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12801. #endif
  12802. (void)depth;
  12803. /*
  12804. * castflags should be 0~3:
  12805. * 0: (non-null, non-null)
  12806. * 1: (null, non-null)
  12807. * 2: (non-null, null)
  12808. * 3: (null, null)
  12809. */
  12810. if (castflags > 3) {
  12811. set_error_buf(error_buf, error_buf_size,
  12812. "invalid castflags");
  12813. break;
  12814. }
  12815. src_nullable =
  12816. (castflags == 1) || (castflags == 3) ? true : false;
  12817. dst_nullable =
  12818. (castflags == 2) || (castflags == 3) ? true : false;
  12819. /* Pop and backup the stack top's ref type */
  12820. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12821. &ref_type_tmp, error_buf,
  12822. error_buf_size)) {
  12823. goto fail;
  12824. }
  12825. /* The reference type rt1 must be valid */
  12826. if (!init_ref_type(module, &ref_type1, src_nullable,
  12827. heap_type, error_buf,
  12828. error_buf_size)) {
  12829. goto fail;
  12830. }
  12831. /* The reference type rt2 must be valid. */
  12832. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12833. heap_type_dst, error_buf,
  12834. error_buf_size)) {
  12835. goto fail;
  12836. }
  12837. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12838. &ref_type2);
  12839. /* The reference type rt2 must match rt1. */
  12840. if (!wasm_reftype_is_subtype_of(
  12841. ref_type2.ref_type, &ref_type2,
  12842. ref_type1.ref_type, &ref_type1, module->types,
  12843. module->type_count)) {
  12844. set_error_buf(error_buf, error_buf_size,
  12845. "type mismatch");
  12846. goto fail;
  12847. }
  12848. p = p_org;
  12849. /* Push ref type casted for branch block check */
  12850. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12851. /* The reference type rt2 must match rt′. */
  12852. type_tmp = ref_type2.ref_type;
  12853. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12854. bh_memcpy_s(
  12855. &wasm_ref_type,
  12856. wasm_reftype_struct_size(&ref_type2),
  12857. &ref_type2,
  12858. wasm_reftype_struct_size(&ref_type2));
  12859. }
  12860. }
  12861. else {
  12862. /* The reference type rt′1 must match rt′. */
  12863. type_tmp = ref_type_diff.ref_type;
  12864. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12865. bh_memcpy_s(
  12866. &wasm_ref_type,
  12867. wasm_reftype_struct_size(&ref_type_diff),
  12868. &ref_type_diff,
  12869. wasm_reftype_struct_size(&ref_type_diff));
  12870. }
  12871. }
  12872. PUSH_REF(type_tmp);
  12873. if (!(frame_csp_tmp = check_branch_block(
  12874. loader_ctx, &p, p_end, opcode, error_buf,
  12875. error_buf_size))) {
  12876. goto fail;
  12877. }
  12878. /* Ignore heap_types */
  12879. skip_leb_uint32(p, p_end);
  12880. skip_leb_uint32(p, p_end);
  12881. /* Restore the original stack top's ref type */
  12882. POP_REF(type_tmp);
  12883. #if WASM_ENABLE_FAST_INTERP != 0
  12884. /* Erase the opnd offset emitted by POP_REF() */
  12885. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12886. #endif
  12887. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12888. type_tmp = ref_type_diff.ref_type;
  12889. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12890. bh_memcpy_s(
  12891. &wasm_ref_type,
  12892. wasm_reftype_struct_size(&ref_type_diff),
  12893. &ref_type_diff,
  12894. wasm_reftype_struct_size(&ref_type_diff));
  12895. }
  12896. }
  12897. else {
  12898. type_tmp = ref_type2.ref_type;
  12899. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12900. bh_memcpy_s(
  12901. &wasm_ref_type,
  12902. wasm_reftype_struct_size(&ref_type2),
  12903. &ref_type2,
  12904. wasm_reftype_struct_size(&ref_type2));
  12905. }
  12906. }
  12907. PUSH_REF(type_tmp);
  12908. #if WASM_ENABLE_FAST_INTERP != 0
  12909. /* Erase the opnd offset emitted by PUSH_REF() */
  12910. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12911. #endif
  12912. break;
  12913. }
  12914. case WASM_OP_ANY_CONVERT_EXTERN:
  12915. {
  12916. uint8 type;
  12917. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12918. &wasm_ref_type, error_buf,
  12919. error_buf_size)) {
  12920. goto fail;
  12921. }
  12922. if (!(type == REF_TYPE_EXTERNREF
  12923. || (type == REF_TYPE_HT_NON_NULLABLE
  12924. && wasm_ref_type.ref_ht_common.heap_type
  12925. == HEAP_TYPE_EXTERN)
  12926. || type == VALUE_TYPE_ANY)) {
  12927. set_error_buf(error_buf, error_buf_size,
  12928. "type mismatch");
  12929. goto fail;
  12930. }
  12931. if (type == REF_TYPE_EXTERNREF)
  12932. type = REF_TYPE_ANYREF;
  12933. else {
  12934. wasm_ref_type.ref_ht_common.heap_type =
  12935. HEAP_TYPE_ANY;
  12936. }
  12937. PUSH_REF(type);
  12938. break;
  12939. }
  12940. case WASM_OP_EXTERN_CONVERT_ANY:
  12941. {
  12942. uint8 type;
  12943. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12944. &wasm_ref_type, error_buf,
  12945. error_buf_size)) {
  12946. goto fail;
  12947. }
  12948. if (type == REF_TYPE_EXTERNREF
  12949. || ((type == REF_TYPE_HT_NULLABLE
  12950. || type == REF_TYPE_HT_NON_NULLABLE)
  12951. && wasm_ref_type.ref_ht_common.heap_type
  12952. == HEAP_TYPE_EXTERN)) {
  12953. set_error_buf(error_buf, error_buf_size,
  12954. "type mismatch");
  12955. goto fail;
  12956. }
  12957. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12958. /* push (ref null extern) */
  12959. type = REF_TYPE_EXTERNREF;
  12960. }
  12961. else {
  12962. /* push (ref extern) */
  12963. type = REF_TYPE_HT_NON_NULLABLE;
  12964. wasm_set_refheaptype_common(
  12965. &wasm_ref_type.ref_ht_common, false,
  12966. HEAP_TYPE_EXTERN);
  12967. }
  12968. PUSH_REF(type);
  12969. break;
  12970. }
  12971. #if WASM_ENABLE_STRINGREF != 0
  12972. case WASM_OP_STRING_NEW_UTF8:
  12973. case WASM_OP_STRING_NEW_WTF16:
  12974. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12975. case WASM_OP_STRING_NEW_WTF8:
  12976. {
  12977. uint32 memidx;
  12978. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12979. func->has_memory_operations = true;
  12980. #endif
  12981. read_leb_uint32(p, p_end, memidx);
  12982. #if WASM_ENABLE_FAST_INTERP != 0
  12983. emit_uint32(loader_ctx, (uint32)memidx);
  12984. #endif
  12985. POP_I32();
  12986. POP_I32();
  12987. PUSH_REF(REF_TYPE_STRINGREF);
  12988. (void)memidx;
  12989. break;
  12990. }
  12991. case WASM_OP_STRING_CONST:
  12992. {
  12993. uint32 contents;
  12994. read_leb_uint32(p, p_end, contents);
  12995. #if WASM_ENABLE_FAST_INTERP != 0
  12996. emit_uint32(loader_ctx, (uint32)contents);
  12997. #endif
  12998. PUSH_REF(REF_TYPE_STRINGREF);
  12999. (void)contents;
  13000. break;
  13001. }
  13002. case WASM_OP_STRING_MEASURE_UTF8:
  13003. case WASM_OP_STRING_MEASURE_WTF8:
  13004. case WASM_OP_STRING_MEASURE_WTF16:
  13005. {
  13006. POP_STRINGREF();
  13007. PUSH_I32();
  13008. break;
  13009. }
  13010. case WASM_OP_STRING_ENCODE_UTF8:
  13011. case WASM_OP_STRING_ENCODE_WTF16:
  13012. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13013. case WASM_OP_STRING_ENCODE_WTF8:
  13014. {
  13015. uint32 memidx;
  13016. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13017. func->has_memory_operations = true;
  13018. #endif
  13019. read_leb_uint32(p, p_end, memidx);
  13020. #if WASM_ENABLE_FAST_INTERP != 0
  13021. emit_uint32(loader_ctx, (uint32)memidx);
  13022. #endif
  13023. POP_I32();
  13024. POP_STRINGREF();
  13025. PUSH_I32();
  13026. (void)memidx;
  13027. break;
  13028. }
  13029. case WASM_OP_STRING_CONCAT:
  13030. {
  13031. POP_STRINGREF();
  13032. POP_STRINGREF();
  13033. PUSH_REF(REF_TYPE_STRINGREF);
  13034. break;
  13035. }
  13036. case WASM_OP_STRING_EQ:
  13037. {
  13038. POP_STRINGREF();
  13039. POP_STRINGREF();
  13040. PUSH_I32();
  13041. break;
  13042. }
  13043. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13044. {
  13045. POP_STRINGREF();
  13046. PUSH_I32();
  13047. break;
  13048. }
  13049. case WASM_OP_STRING_AS_WTF8:
  13050. {
  13051. POP_STRINGREF();
  13052. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13053. break;
  13054. }
  13055. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13056. {
  13057. POP_I32();
  13058. POP_I32();
  13059. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13060. PUSH_I32();
  13061. break;
  13062. }
  13063. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13064. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13065. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13066. {
  13067. uint32 memidx;
  13068. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13069. func->has_memory_operations = true;
  13070. #endif
  13071. read_leb_uint32(p, p_end, memidx);
  13072. #if WASM_ENABLE_FAST_INTERP != 0
  13073. emit_uint32(loader_ctx, (uint32)memidx);
  13074. #endif
  13075. POP_I32();
  13076. POP_I32();
  13077. POP_I32();
  13078. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13079. PUSH_I32();
  13080. PUSH_I32();
  13081. (void)memidx;
  13082. break;
  13083. }
  13084. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13085. {
  13086. POP_I32();
  13087. POP_I32();
  13088. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13089. PUSH_REF(REF_TYPE_STRINGREF);
  13090. break;
  13091. }
  13092. case WASM_OP_STRING_AS_WTF16:
  13093. {
  13094. POP_STRINGREF();
  13095. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13096. break;
  13097. }
  13098. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13099. {
  13100. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13101. PUSH_I32();
  13102. break;
  13103. }
  13104. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13105. {
  13106. POP_I32();
  13107. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13108. PUSH_I32();
  13109. break;
  13110. }
  13111. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13112. {
  13113. uint32 memidx;
  13114. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13115. func->has_memory_operations = true;
  13116. #endif
  13117. read_leb_uint32(p, p_end, memidx);
  13118. #if WASM_ENABLE_FAST_INTERP != 0
  13119. emit_uint32(loader_ctx, (uint32)memidx);
  13120. #endif
  13121. POP_I32();
  13122. POP_I32();
  13123. POP_I32();
  13124. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13125. PUSH_I32();
  13126. (void)memidx;
  13127. break;
  13128. }
  13129. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13130. {
  13131. POP_I32();
  13132. POP_I32();
  13133. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13134. PUSH_REF(REF_TYPE_STRINGREF);
  13135. break;
  13136. }
  13137. case WASM_OP_STRING_AS_ITER:
  13138. {
  13139. POP_STRINGREF();
  13140. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13141. break;
  13142. }
  13143. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13144. {
  13145. POP_REF(REF_TYPE_STRINGVIEWITER);
  13146. PUSH_I32();
  13147. break;
  13148. }
  13149. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13150. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13151. {
  13152. POP_I32();
  13153. POP_REF(REF_TYPE_STRINGVIEWITER);
  13154. PUSH_I32();
  13155. break;
  13156. }
  13157. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13158. {
  13159. POP_I32();
  13160. POP_REF(REF_TYPE_STRINGVIEWITER);
  13161. PUSH_REF(REF_TYPE_STRINGREF);
  13162. break;
  13163. }
  13164. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13165. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13166. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13167. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13168. {
  13169. POP_I32();
  13170. POP_I32();
  13171. POP_REF(REF_TYPE_ARRAYREF);
  13172. PUSH_REF(REF_TYPE_STRINGREF);
  13173. break;
  13174. }
  13175. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13176. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13177. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13178. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13179. {
  13180. POP_I32();
  13181. POP_REF(REF_TYPE_ARRAYREF);
  13182. POP_STRINGREF();
  13183. PUSH_I32();
  13184. break;
  13185. }
  13186. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13187. default:
  13188. set_error_buf_v(error_buf, error_buf_size,
  13189. "%s %02x %02x", "unsupported opcode",
  13190. 0xfb, opcode1);
  13191. goto fail;
  13192. }
  13193. break;
  13194. }
  13195. #endif /* end of WASM_ENABLE_GC != 0 */
  13196. case WASM_OP_MISC_PREFIX:
  13197. {
  13198. uint32 opcode1;
  13199. read_leb_uint32(p, p_end, opcode1);
  13200. #if WASM_ENABLE_FAST_INTERP != 0
  13201. emit_byte(loader_ctx, ((uint8)opcode1));
  13202. #endif
  13203. switch (opcode1) {
  13204. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13205. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13206. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13207. break;
  13208. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13209. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13210. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13211. break;
  13212. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13213. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13214. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13215. break;
  13216. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13217. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13218. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13219. break;
  13220. #if WASM_ENABLE_BULK_MEMORY != 0
  13221. case WASM_OP_MEMORY_INIT:
  13222. {
  13223. read_leb_uint32(p, p_end, data_seg_idx);
  13224. #if WASM_ENABLE_FAST_INTERP != 0
  13225. emit_uint32(loader_ctx, data_seg_idx);
  13226. #endif
  13227. if (module->import_memory_count == 0
  13228. && module->memory_count == 0)
  13229. goto fail_unknown_memory;
  13230. if (*p++ != 0x00)
  13231. goto fail_zero_byte_expected;
  13232. if (data_seg_idx >= module->data_seg_count) {
  13233. set_error_buf_v(error_buf, error_buf_size,
  13234. "unknown data segment %d",
  13235. data_seg_idx);
  13236. goto fail;
  13237. }
  13238. if (module->data_seg_count1 == 0)
  13239. goto fail_data_cnt_sec_require;
  13240. POP_I32();
  13241. POP_I32();
  13242. POP_MEM_OFFSET();
  13243. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13244. func->has_memory_operations = true;
  13245. #endif
  13246. #if WASM_ENABLE_WAMR_COMPILER != 0
  13247. module->is_bulk_memory_used = true;
  13248. #endif
  13249. break;
  13250. }
  13251. case WASM_OP_DATA_DROP:
  13252. {
  13253. read_leb_uint32(p, p_end, data_seg_idx);
  13254. #if WASM_ENABLE_FAST_INTERP != 0
  13255. emit_uint32(loader_ctx, data_seg_idx);
  13256. #endif
  13257. if (data_seg_idx >= module->data_seg_count) {
  13258. set_error_buf(error_buf, error_buf_size,
  13259. "unknown data segment");
  13260. goto fail;
  13261. }
  13262. if (module->data_seg_count1 == 0)
  13263. goto fail_data_cnt_sec_require;
  13264. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13265. func->has_memory_operations = true;
  13266. #endif
  13267. #if WASM_ENABLE_WAMR_COMPILER != 0
  13268. module->is_bulk_memory_used = true;
  13269. #endif
  13270. break;
  13271. }
  13272. case WASM_OP_MEMORY_COPY:
  13273. {
  13274. CHECK_BUF(p, p_end, sizeof(int16));
  13275. /* both src and dst memory index should be 0 */
  13276. if (*(int16 *)p != 0x0000)
  13277. goto fail_zero_byte_expected;
  13278. p += 2;
  13279. if (module->import_memory_count == 0
  13280. && module->memory_count == 0)
  13281. goto fail_unknown_memory;
  13282. POP_MEM_OFFSET();
  13283. POP_MEM_OFFSET();
  13284. POP_MEM_OFFSET();
  13285. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13286. func->has_memory_operations = true;
  13287. #endif
  13288. #if WASM_ENABLE_WAMR_COMPILER != 0
  13289. module->is_bulk_memory_used = true;
  13290. #endif
  13291. break;
  13292. }
  13293. case WASM_OP_MEMORY_FILL:
  13294. {
  13295. if (*p++ != 0x00) {
  13296. goto fail_zero_byte_expected;
  13297. }
  13298. if (module->import_memory_count == 0
  13299. && module->memory_count == 0) {
  13300. goto fail_unknown_memory;
  13301. }
  13302. POP_MEM_OFFSET();
  13303. POP_I32();
  13304. POP_MEM_OFFSET();
  13305. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13306. func->has_memory_operations = true;
  13307. #endif
  13308. #if WASM_ENABLE_WAMR_COMPILER != 0
  13309. module->is_bulk_memory_used = true;
  13310. #endif
  13311. break;
  13312. }
  13313. fail_zero_byte_expected:
  13314. set_error_buf(error_buf, error_buf_size,
  13315. "zero byte expected");
  13316. goto fail;
  13317. fail_unknown_memory:
  13318. set_error_buf(error_buf, error_buf_size,
  13319. "unknown memory 0");
  13320. goto fail;
  13321. fail_data_cnt_sec_require:
  13322. set_error_buf(error_buf, error_buf_size,
  13323. "data count section required");
  13324. goto fail;
  13325. #endif /* WASM_ENABLE_BULK_MEMORY */
  13326. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13327. case WASM_OP_TABLE_INIT:
  13328. {
  13329. uint8 seg_type = 0, tbl_type = 0;
  13330. #if WASM_ENABLE_GC != 0
  13331. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13332. #endif
  13333. read_leb_uint32(p, p_end, table_seg_idx);
  13334. read_leb_uint32(p, p_end, table_idx);
  13335. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13336. #if WASM_ENABLE_GC != 0
  13337. (void **)&tbl_ref_type,
  13338. #else
  13339. NULL,
  13340. #endif
  13341. error_buf, error_buf_size))
  13342. goto fail;
  13343. if (!get_table_seg_elem_type(module, table_seg_idx,
  13344. &seg_type,
  13345. #if WASM_ENABLE_GC != 0
  13346. (void **)&seg_ref_type,
  13347. #else
  13348. NULL,
  13349. #endif
  13350. error_buf, error_buf_size))
  13351. goto fail;
  13352. #if WASM_ENABLE_GC == 0
  13353. if (seg_type != tbl_type) {
  13354. set_error_buf(error_buf, error_buf_size,
  13355. "type mismatch");
  13356. goto fail;
  13357. }
  13358. #else
  13359. if (!wasm_reftype_is_subtype_of(
  13360. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13361. module->types, module->type_count)) {
  13362. set_error_buf(error_buf, error_buf_size,
  13363. "type mismatch");
  13364. goto fail;
  13365. }
  13366. #endif
  13367. #if WASM_ENABLE_FAST_INTERP != 0
  13368. emit_uint32(loader_ctx, table_seg_idx);
  13369. emit_uint32(loader_ctx, table_idx);
  13370. #endif
  13371. POP_I32();
  13372. POP_I32();
  13373. POP_I32();
  13374. #if WASM_ENABLE_WAMR_COMPILER != 0
  13375. module->is_ref_types_used = true;
  13376. #endif
  13377. break;
  13378. }
  13379. case WASM_OP_ELEM_DROP:
  13380. {
  13381. read_leb_uint32(p, p_end, table_seg_idx);
  13382. if (!get_table_seg_elem_type(module, table_seg_idx,
  13383. NULL, NULL, error_buf,
  13384. error_buf_size))
  13385. goto fail;
  13386. #if WASM_ENABLE_FAST_INTERP != 0
  13387. emit_uint32(loader_ctx, table_seg_idx);
  13388. #endif
  13389. #if WASM_ENABLE_WAMR_COMPILER != 0
  13390. module->is_ref_types_used = true;
  13391. #endif
  13392. break;
  13393. }
  13394. case WASM_OP_TABLE_COPY:
  13395. {
  13396. uint8 src_type, dst_type;
  13397. #if WASM_ENABLE_GC != 0
  13398. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13399. #endif
  13400. uint32 src_tbl_idx, dst_tbl_idx;
  13401. read_leb_uint32(p, p_end, dst_tbl_idx);
  13402. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13403. #if WASM_ENABLE_GC != 0
  13404. (void **)&dst_ref_type,
  13405. #else
  13406. NULL,
  13407. #endif
  13408. error_buf, error_buf_size))
  13409. goto fail;
  13410. read_leb_uint32(p, p_end, src_tbl_idx);
  13411. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13412. #if WASM_ENABLE_GC != 0
  13413. (void **)&src_ref_type,
  13414. #else
  13415. NULL,
  13416. #endif
  13417. error_buf, error_buf_size))
  13418. goto fail;
  13419. #if WASM_ENABLE_GC == 0
  13420. if (src_type != dst_type) {
  13421. set_error_buf(error_buf, error_buf_size,
  13422. "type mismatch");
  13423. goto fail;
  13424. }
  13425. #else
  13426. if (!wasm_reftype_is_subtype_of(
  13427. src_type, src_ref_type, dst_type, dst_ref_type,
  13428. module->types, module->type_count)) {
  13429. set_error_buf(error_buf, error_buf_size,
  13430. "type mismatch");
  13431. goto fail;
  13432. }
  13433. #endif
  13434. #if WASM_ENABLE_FAST_INTERP != 0
  13435. emit_uint32(loader_ctx, dst_tbl_idx);
  13436. emit_uint32(loader_ctx, src_tbl_idx);
  13437. #endif
  13438. POP_I32();
  13439. POP_I32();
  13440. POP_I32();
  13441. #if WASM_ENABLE_WAMR_COMPILER != 0
  13442. module->is_ref_types_used = true;
  13443. #endif
  13444. break;
  13445. }
  13446. case WASM_OP_TABLE_SIZE:
  13447. {
  13448. read_leb_uint32(p, p_end, table_idx);
  13449. /* TODO: shall we create a new function to check
  13450. table idx instead of using below function? */
  13451. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13452. error_buf, error_buf_size))
  13453. goto fail;
  13454. #if WASM_ENABLE_FAST_INTERP != 0
  13455. emit_uint32(loader_ctx, table_idx);
  13456. #endif
  13457. PUSH_I32();
  13458. #if WASM_ENABLE_WAMR_COMPILER != 0
  13459. module->is_ref_types_used = true;
  13460. #endif
  13461. break;
  13462. }
  13463. case WASM_OP_TABLE_GROW:
  13464. case WASM_OP_TABLE_FILL:
  13465. {
  13466. uint8 decl_type;
  13467. #if WASM_ENABLE_GC != 0
  13468. WASMRefType *ref_type = NULL;
  13469. #endif
  13470. read_leb_uint32(p, p_end, table_idx);
  13471. if (!get_table_elem_type(module, table_idx, &decl_type,
  13472. #if WASM_ENABLE_GC != 0
  13473. (void **)&ref_type,
  13474. #else
  13475. NULL,
  13476. #endif
  13477. error_buf, error_buf_size))
  13478. goto fail;
  13479. #if WASM_ENABLE_GC != 0
  13480. if (wasm_is_type_multi_byte_type(decl_type)) {
  13481. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13482. ref_type,
  13483. wasm_reftype_struct_size(ref_type));
  13484. }
  13485. #endif
  13486. if (opcode1 == WASM_OP_TABLE_GROW) {
  13487. if (table_idx < module->import_table_count) {
  13488. module->import_tables[table_idx]
  13489. .u.table.table_type.possible_grow = true;
  13490. }
  13491. else {
  13492. module
  13493. ->tables[table_idx
  13494. - module->import_table_count]
  13495. .table_type.possible_grow = true;
  13496. }
  13497. }
  13498. #if WASM_ENABLE_FAST_INTERP != 0
  13499. emit_uint32(loader_ctx, table_idx);
  13500. #endif
  13501. POP_I32();
  13502. #if WASM_ENABLE_FAST_INTERP != 0
  13503. POP_OFFSET_TYPE(decl_type);
  13504. #endif
  13505. POP_TYPE(decl_type);
  13506. if (opcode1 == WASM_OP_TABLE_GROW)
  13507. PUSH_I32();
  13508. else
  13509. POP_I32();
  13510. #if WASM_ENABLE_WAMR_COMPILER != 0
  13511. module->is_ref_types_used = true;
  13512. #endif
  13513. break;
  13514. }
  13515. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13516. default:
  13517. set_error_buf_v(error_buf, error_buf_size,
  13518. "%s %02x %02x", "unsupported opcode",
  13519. 0xfc, opcode1);
  13520. goto fail;
  13521. }
  13522. break;
  13523. }
  13524. #if WASM_ENABLE_SIMD != 0
  13525. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13526. case WASM_OP_SIMD_PREFIX:
  13527. {
  13528. /* TODO: memory64 offset type changes */
  13529. uint32 opcode1;
  13530. #if WASM_ENABLE_WAMR_COMPILER != 0
  13531. /* Mark the SIMD instruction is used in this module */
  13532. module->is_simd_used = true;
  13533. #endif
  13534. read_leb_uint32(p, p_end, opcode1);
  13535. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13536. */
  13537. switch (opcode1) {
  13538. /* memory instruction */
  13539. case SIMD_v128_load:
  13540. case SIMD_v128_load8x8_s:
  13541. case SIMD_v128_load8x8_u:
  13542. case SIMD_v128_load16x4_s:
  13543. case SIMD_v128_load16x4_u:
  13544. case SIMD_v128_load32x2_s:
  13545. case SIMD_v128_load32x2_u:
  13546. case SIMD_v128_load8_splat:
  13547. case SIMD_v128_load16_splat:
  13548. case SIMD_v128_load32_splat:
  13549. case SIMD_v128_load64_splat:
  13550. {
  13551. CHECK_MEMORY();
  13552. read_leb_uint32(p, p_end, align); /* align */
  13553. if (!check_simd_memory_access_align(
  13554. opcode1, align, error_buf, error_buf_size)) {
  13555. goto fail;
  13556. }
  13557. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13558. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13559. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13560. func->has_memory_operations = true;
  13561. #endif
  13562. break;
  13563. }
  13564. case SIMD_v128_store:
  13565. {
  13566. CHECK_MEMORY();
  13567. read_leb_uint32(p, p_end, align); /* align */
  13568. if (!check_simd_memory_access_align(
  13569. opcode1, align, error_buf, error_buf_size)) {
  13570. goto fail;
  13571. }
  13572. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13573. POP_V128();
  13574. POP_MEM_OFFSET();
  13575. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13576. func->has_memory_operations = true;
  13577. #endif
  13578. break;
  13579. }
  13580. /* basic operation */
  13581. case SIMD_v128_const:
  13582. {
  13583. CHECK_BUF1(p, p_end, 16);
  13584. p += 16;
  13585. PUSH_V128();
  13586. break;
  13587. }
  13588. case SIMD_v8x16_shuffle:
  13589. {
  13590. V128 mask;
  13591. CHECK_BUF1(p, p_end, 16);
  13592. mask = read_i8x16(p, error_buf, error_buf_size);
  13593. p += 16;
  13594. if (!check_simd_shuffle_mask(mask, error_buf,
  13595. error_buf_size)) {
  13596. goto fail;
  13597. }
  13598. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13599. break;
  13600. }
  13601. case SIMD_v8x16_swizzle:
  13602. {
  13603. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13604. break;
  13605. }
  13606. /* splat operation */
  13607. case SIMD_i8x16_splat:
  13608. case SIMD_i16x8_splat:
  13609. case SIMD_i32x4_splat:
  13610. case SIMD_i64x2_splat:
  13611. case SIMD_f32x4_splat:
  13612. case SIMD_f64x2_splat:
  13613. {
  13614. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13615. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13616. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13617. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13618. VALUE_TYPE_V128);
  13619. break;
  13620. }
  13621. /* lane operation */
  13622. case SIMD_i8x16_extract_lane_s:
  13623. case SIMD_i8x16_extract_lane_u:
  13624. case SIMD_i8x16_replace_lane:
  13625. case SIMD_i16x8_extract_lane_s:
  13626. case SIMD_i16x8_extract_lane_u:
  13627. case SIMD_i16x8_replace_lane:
  13628. case SIMD_i32x4_extract_lane:
  13629. case SIMD_i32x4_replace_lane:
  13630. case SIMD_i64x2_extract_lane:
  13631. case SIMD_i64x2_replace_lane:
  13632. case SIMD_f32x4_extract_lane:
  13633. case SIMD_f32x4_replace_lane:
  13634. case SIMD_f64x2_extract_lane:
  13635. case SIMD_f64x2_replace_lane:
  13636. {
  13637. uint8 lane;
  13638. /* clang-format off */
  13639. uint8 replace[] = {
  13640. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13641. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13642. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13643. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13644. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13645. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13646. };
  13647. uint8 push_type[] = {
  13648. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13649. VALUE_TYPE_V128,
  13650. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13651. VALUE_TYPE_V128,
  13652. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13653. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13654. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13655. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13656. };
  13657. /* clang-format on */
  13658. CHECK_BUF(p, p_end, 1);
  13659. lane = read_uint8(p);
  13660. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13661. error_buf_size)) {
  13662. goto fail;
  13663. }
  13664. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13665. if (!(wasm_loader_pop_frame_ref(
  13666. loader_ctx,
  13667. replace[opcode1
  13668. - SIMD_i8x16_extract_lane_s],
  13669. error_buf, error_buf_size)))
  13670. goto fail;
  13671. }
  13672. POP_AND_PUSH(
  13673. VALUE_TYPE_V128,
  13674. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13675. break;
  13676. }
  13677. /* i8x16 compare operation */
  13678. case SIMD_i8x16_eq:
  13679. case SIMD_i8x16_ne:
  13680. case SIMD_i8x16_lt_s:
  13681. case SIMD_i8x16_lt_u:
  13682. case SIMD_i8x16_gt_s:
  13683. case SIMD_i8x16_gt_u:
  13684. case SIMD_i8x16_le_s:
  13685. case SIMD_i8x16_le_u:
  13686. case SIMD_i8x16_ge_s:
  13687. case SIMD_i8x16_ge_u:
  13688. /* i16x8 compare operation */
  13689. case SIMD_i16x8_eq:
  13690. case SIMD_i16x8_ne:
  13691. case SIMD_i16x8_lt_s:
  13692. case SIMD_i16x8_lt_u:
  13693. case SIMD_i16x8_gt_s:
  13694. case SIMD_i16x8_gt_u:
  13695. case SIMD_i16x8_le_s:
  13696. case SIMD_i16x8_le_u:
  13697. case SIMD_i16x8_ge_s:
  13698. case SIMD_i16x8_ge_u:
  13699. /* i32x4 compare operation */
  13700. case SIMD_i32x4_eq:
  13701. case SIMD_i32x4_ne:
  13702. case SIMD_i32x4_lt_s:
  13703. case SIMD_i32x4_lt_u:
  13704. case SIMD_i32x4_gt_s:
  13705. case SIMD_i32x4_gt_u:
  13706. case SIMD_i32x4_le_s:
  13707. case SIMD_i32x4_le_u:
  13708. case SIMD_i32x4_ge_s:
  13709. case SIMD_i32x4_ge_u:
  13710. /* f32x4 compare operation */
  13711. case SIMD_f32x4_eq:
  13712. case SIMD_f32x4_ne:
  13713. case SIMD_f32x4_lt:
  13714. case SIMD_f32x4_gt:
  13715. case SIMD_f32x4_le:
  13716. case SIMD_f32x4_ge:
  13717. /* f64x2 compare operation */
  13718. case SIMD_f64x2_eq:
  13719. case SIMD_f64x2_ne:
  13720. case SIMD_f64x2_lt:
  13721. case SIMD_f64x2_gt:
  13722. case SIMD_f64x2_le:
  13723. case SIMD_f64x2_ge:
  13724. {
  13725. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13726. break;
  13727. }
  13728. /* v128 operation */
  13729. case SIMD_v128_not:
  13730. {
  13731. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13732. break;
  13733. }
  13734. case SIMD_v128_and:
  13735. case SIMD_v128_andnot:
  13736. case SIMD_v128_or:
  13737. case SIMD_v128_xor:
  13738. {
  13739. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13740. break;
  13741. }
  13742. case SIMD_v128_bitselect:
  13743. {
  13744. POP_V128();
  13745. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13746. break;
  13747. }
  13748. case SIMD_v128_any_true:
  13749. {
  13750. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13751. break;
  13752. }
  13753. /* Load Lane Operation */
  13754. case SIMD_v128_load8_lane:
  13755. case SIMD_v128_load16_lane:
  13756. case SIMD_v128_load32_lane:
  13757. case SIMD_v128_load64_lane:
  13758. case SIMD_v128_store8_lane:
  13759. case SIMD_v128_store16_lane:
  13760. case SIMD_v128_store32_lane:
  13761. case SIMD_v128_store64_lane:
  13762. {
  13763. uint8 lane;
  13764. CHECK_MEMORY();
  13765. read_leb_uint32(p, p_end, align); /* align */
  13766. if (!check_simd_memory_access_align(
  13767. opcode1, align, error_buf, error_buf_size)) {
  13768. goto fail;
  13769. }
  13770. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13771. CHECK_BUF(p, p_end, 1);
  13772. lane = read_uint8(p);
  13773. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13774. error_buf_size)) {
  13775. goto fail;
  13776. }
  13777. POP_V128();
  13778. POP_MEM_OFFSET();
  13779. if (opcode1 < SIMD_v128_store8_lane) {
  13780. PUSH_V128();
  13781. }
  13782. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13783. func->has_memory_operations = true;
  13784. #endif
  13785. break;
  13786. }
  13787. case SIMD_v128_load32_zero:
  13788. case SIMD_v128_load64_zero:
  13789. {
  13790. CHECK_MEMORY();
  13791. read_leb_uint32(p, p_end, align); /* align */
  13792. if (!check_simd_memory_access_align(
  13793. opcode1, align, error_buf, error_buf_size)) {
  13794. goto fail;
  13795. }
  13796. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13797. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13798. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13799. func->has_memory_operations = true;
  13800. #endif
  13801. break;
  13802. }
  13803. /* Float conversion */
  13804. case SIMD_f32x4_demote_f64x2_zero:
  13805. case SIMD_f64x2_promote_low_f32x4_zero:
  13806. {
  13807. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13808. break;
  13809. }
  13810. /* i8x16 Operation */
  13811. case SIMD_i8x16_abs:
  13812. case SIMD_i8x16_neg:
  13813. case SIMD_i8x16_popcnt:
  13814. {
  13815. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13816. break;
  13817. }
  13818. case SIMD_i8x16_all_true:
  13819. case SIMD_i8x16_bitmask:
  13820. {
  13821. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13822. break;
  13823. }
  13824. case SIMD_i8x16_narrow_i16x8_s:
  13825. case SIMD_i8x16_narrow_i16x8_u:
  13826. {
  13827. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13828. break;
  13829. }
  13830. case SIMD_f32x4_ceil:
  13831. case SIMD_f32x4_floor:
  13832. case SIMD_f32x4_trunc:
  13833. case SIMD_f32x4_nearest:
  13834. {
  13835. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13836. break;
  13837. }
  13838. case SIMD_i8x16_shl:
  13839. case SIMD_i8x16_shr_s:
  13840. case SIMD_i8x16_shr_u:
  13841. {
  13842. POP_I32();
  13843. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13844. break;
  13845. }
  13846. case SIMD_i8x16_add:
  13847. case SIMD_i8x16_add_sat_s:
  13848. case SIMD_i8x16_add_sat_u:
  13849. case SIMD_i8x16_sub:
  13850. case SIMD_i8x16_sub_sat_s:
  13851. case SIMD_i8x16_sub_sat_u:
  13852. {
  13853. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13854. break;
  13855. }
  13856. case SIMD_f64x2_ceil:
  13857. case SIMD_f64x2_floor:
  13858. {
  13859. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13860. break;
  13861. }
  13862. case SIMD_i8x16_min_s:
  13863. case SIMD_i8x16_min_u:
  13864. case SIMD_i8x16_max_s:
  13865. case SIMD_i8x16_max_u:
  13866. {
  13867. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13868. break;
  13869. }
  13870. case SIMD_f64x2_trunc:
  13871. {
  13872. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13873. break;
  13874. }
  13875. case SIMD_i8x16_avgr_u:
  13876. {
  13877. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13878. break;
  13879. }
  13880. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13881. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13882. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13883. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13884. /* i16x8 operation */
  13885. case SIMD_i16x8_abs:
  13886. case SIMD_i16x8_neg:
  13887. {
  13888. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13889. break;
  13890. }
  13891. case SIMD_i16x8_q15mulr_sat_s:
  13892. {
  13893. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13894. break;
  13895. }
  13896. case SIMD_i16x8_all_true:
  13897. case SIMD_i16x8_bitmask:
  13898. {
  13899. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13900. break;
  13901. }
  13902. case SIMD_i16x8_narrow_i32x4_s:
  13903. case SIMD_i16x8_narrow_i32x4_u:
  13904. {
  13905. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13906. break;
  13907. }
  13908. case SIMD_i16x8_extend_low_i8x16_s:
  13909. case SIMD_i16x8_extend_high_i8x16_s:
  13910. case SIMD_i16x8_extend_low_i8x16_u:
  13911. case SIMD_i16x8_extend_high_i8x16_u:
  13912. {
  13913. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13914. break;
  13915. }
  13916. case SIMD_i16x8_shl:
  13917. case SIMD_i16x8_shr_s:
  13918. case SIMD_i16x8_shr_u:
  13919. {
  13920. POP_I32();
  13921. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13922. break;
  13923. }
  13924. case SIMD_i16x8_add:
  13925. case SIMD_i16x8_add_sat_s:
  13926. case SIMD_i16x8_add_sat_u:
  13927. case SIMD_i16x8_sub:
  13928. case SIMD_i16x8_sub_sat_s:
  13929. case SIMD_i16x8_sub_sat_u:
  13930. {
  13931. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13932. break;
  13933. }
  13934. case SIMD_f64x2_nearest:
  13935. {
  13936. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13937. break;
  13938. }
  13939. case SIMD_i16x8_mul:
  13940. case SIMD_i16x8_min_s:
  13941. case SIMD_i16x8_min_u:
  13942. case SIMD_i16x8_max_s:
  13943. case SIMD_i16x8_max_u:
  13944. case SIMD_i16x8_avgr_u:
  13945. case SIMD_i16x8_extmul_low_i8x16_s:
  13946. case SIMD_i16x8_extmul_high_i8x16_s:
  13947. case SIMD_i16x8_extmul_low_i8x16_u:
  13948. case SIMD_i16x8_extmul_high_i8x16_u:
  13949. {
  13950. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13951. break;
  13952. }
  13953. /* i32x4 operation */
  13954. case SIMD_i32x4_abs:
  13955. case SIMD_i32x4_neg:
  13956. {
  13957. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13958. break;
  13959. }
  13960. case SIMD_i32x4_all_true:
  13961. case SIMD_i32x4_bitmask:
  13962. {
  13963. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13964. break;
  13965. }
  13966. case SIMD_i32x4_extend_low_i16x8_s:
  13967. case SIMD_i32x4_extend_high_i16x8_s:
  13968. case SIMD_i32x4_extend_low_i16x8_u:
  13969. case SIMD_i32x4_extend_high_i16x8_u:
  13970. {
  13971. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13972. break;
  13973. }
  13974. case SIMD_i32x4_shl:
  13975. case SIMD_i32x4_shr_s:
  13976. case SIMD_i32x4_shr_u:
  13977. {
  13978. POP_I32();
  13979. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13980. break;
  13981. }
  13982. case SIMD_i32x4_add:
  13983. case SIMD_i32x4_sub:
  13984. case SIMD_i32x4_mul:
  13985. case SIMD_i32x4_min_s:
  13986. case SIMD_i32x4_min_u:
  13987. case SIMD_i32x4_max_s:
  13988. case SIMD_i32x4_max_u:
  13989. case SIMD_i32x4_dot_i16x8_s:
  13990. case SIMD_i32x4_extmul_low_i16x8_s:
  13991. case SIMD_i32x4_extmul_high_i16x8_s:
  13992. case SIMD_i32x4_extmul_low_i16x8_u:
  13993. case SIMD_i32x4_extmul_high_i16x8_u:
  13994. {
  13995. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13996. break;
  13997. }
  13998. /* i64x2 operation */
  13999. case SIMD_i64x2_abs:
  14000. case SIMD_i64x2_neg:
  14001. {
  14002. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14003. break;
  14004. }
  14005. case SIMD_i64x2_all_true:
  14006. case SIMD_i64x2_bitmask:
  14007. {
  14008. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14009. break;
  14010. }
  14011. case SIMD_i64x2_extend_low_i32x4_s:
  14012. case SIMD_i64x2_extend_high_i32x4_s:
  14013. case SIMD_i64x2_extend_low_i32x4_u:
  14014. case SIMD_i64x2_extend_high_i32x4_u:
  14015. {
  14016. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14017. break;
  14018. }
  14019. case SIMD_i64x2_shl:
  14020. case SIMD_i64x2_shr_s:
  14021. case SIMD_i64x2_shr_u:
  14022. {
  14023. POP_I32();
  14024. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14025. break;
  14026. }
  14027. case SIMD_i64x2_add:
  14028. case SIMD_i64x2_sub:
  14029. case SIMD_i64x2_mul:
  14030. case SIMD_i64x2_eq:
  14031. case SIMD_i64x2_ne:
  14032. case SIMD_i64x2_lt_s:
  14033. case SIMD_i64x2_gt_s:
  14034. case SIMD_i64x2_le_s:
  14035. case SIMD_i64x2_ge_s:
  14036. case SIMD_i64x2_extmul_low_i32x4_s:
  14037. case SIMD_i64x2_extmul_high_i32x4_s:
  14038. case SIMD_i64x2_extmul_low_i32x4_u:
  14039. case SIMD_i64x2_extmul_high_i32x4_u:
  14040. {
  14041. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14042. break;
  14043. }
  14044. /* f32x4 operation */
  14045. case SIMD_f32x4_abs:
  14046. case SIMD_f32x4_neg:
  14047. case SIMD_f32x4_sqrt:
  14048. {
  14049. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14050. break;
  14051. }
  14052. case SIMD_f32x4_add:
  14053. case SIMD_f32x4_sub:
  14054. case SIMD_f32x4_mul:
  14055. case SIMD_f32x4_div:
  14056. case SIMD_f32x4_min:
  14057. case SIMD_f32x4_max:
  14058. case SIMD_f32x4_pmin:
  14059. case SIMD_f32x4_pmax:
  14060. {
  14061. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14062. break;
  14063. }
  14064. /* f64x2 operation */
  14065. case SIMD_f64x2_abs:
  14066. case SIMD_f64x2_neg:
  14067. case SIMD_f64x2_sqrt:
  14068. {
  14069. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14070. break;
  14071. }
  14072. case SIMD_f64x2_add:
  14073. case SIMD_f64x2_sub:
  14074. case SIMD_f64x2_mul:
  14075. case SIMD_f64x2_div:
  14076. case SIMD_f64x2_min:
  14077. case SIMD_f64x2_max:
  14078. case SIMD_f64x2_pmin:
  14079. case SIMD_f64x2_pmax:
  14080. {
  14081. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14082. break;
  14083. }
  14084. case SIMD_i32x4_trunc_sat_f32x4_s:
  14085. case SIMD_i32x4_trunc_sat_f32x4_u:
  14086. case SIMD_f32x4_convert_i32x4_s:
  14087. case SIMD_f32x4_convert_i32x4_u:
  14088. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14089. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14090. case SIMD_f64x2_convert_low_i32x4_s:
  14091. case SIMD_f64x2_convert_low_i32x4_u:
  14092. {
  14093. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14094. break;
  14095. }
  14096. default:
  14097. {
  14098. if (error_buf != NULL) {
  14099. snprintf(error_buf, error_buf_size,
  14100. "WASM module load failed: "
  14101. "invalid opcode 0xfd %02x.",
  14102. opcode1);
  14103. }
  14104. goto fail;
  14105. }
  14106. }
  14107. break;
  14108. }
  14109. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14110. #endif /* end of WASM_ENABLE_SIMD */
  14111. #if WASM_ENABLE_SHARED_MEMORY != 0
  14112. case WASM_OP_ATOMIC_PREFIX:
  14113. {
  14114. uint32 opcode1;
  14115. read_leb_uint32(p, p_end, opcode1);
  14116. #if WASM_ENABLE_FAST_INTERP != 0
  14117. emit_byte(loader_ctx, opcode1);
  14118. #endif
  14119. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14120. CHECK_MEMORY();
  14121. read_leb_uint32(p, p_end, align); /* align */
  14122. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14123. if (!check_memory_align_equal(opcode1, align, error_buf,
  14124. error_buf_size)) {
  14125. goto fail;
  14126. }
  14127. #if WASM_ENABLE_FAST_INTERP != 0
  14128. emit_uint32(loader_ctx, mem_offset);
  14129. #endif
  14130. }
  14131. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14132. func->has_memory_operations = true;
  14133. #endif
  14134. switch (opcode1) {
  14135. case WASM_OP_ATOMIC_NOTIFY:
  14136. POP_I32();
  14137. POP_MEM_OFFSET();
  14138. PUSH_I32();
  14139. break;
  14140. case WASM_OP_ATOMIC_WAIT32:
  14141. POP_I64();
  14142. POP_I32();
  14143. POP_MEM_OFFSET();
  14144. PUSH_I32();
  14145. break;
  14146. case WASM_OP_ATOMIC_WAIT64:
  14147. POP_I64();
  14148. POP_I64();
  14149. POP_MEM_OFFSET();
  14150. PUSH_I32();
  14151. break;
  14152. case WASM_OP_ATOMIC_FENCE:
  14153. /* reserved byte 0x00 */
  14154. if (*p++ != 0x00) {
  14155. set_error_buf(error_buf, error_buf_size,
  14156. "zero byte expected");
  14157. goto fail;
  14158. }
  14159. break;
  14160. case WASM_OP_ATOMIC_I32_LOAD:
  14161. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14162. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14163. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14164. break;
  14165. case WASM_OP_ATOMIC_I32_STORE:
  14166. case WASM_OP_ATOMIC_I32_STORE8:
  14167. case WASM_OP_ATOMIC_I32_STORE16:
  14168. POP_I32();
  14169. POP_MEM_OFFSET();
  14170. break;
  14171. case WASM_OP_ATOMIC_I64_LOAD:
  14172. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14173. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14174. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14175. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14176. break;
  14177. case WASM_OP_ATOMIC_I64_STORE:
  14178. case WASM_OP_ATOMIC_I64_STORE8:
  14179. case WASM_OP_ATOMIC_I64_STORE16:
  14180. case WASM_OP_ATOMIC_I64_STORE32:
  14181. POP_I64();
  14182. POP_MEM_OFFSET();
  14183. break;
  14184. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14185. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14186. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14187. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14188. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14189. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14190. case WASM_OP_ATOMIC_RMW_I32_AND:
  14191. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14192. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14193. case WASM_OP_ATOMIC_RMW_I32_OR:
  14194. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14195. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14196. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14197. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14198. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14199. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14200. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14201. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14202. POP_I32();
  14203. POP_MEM_OFFSET();
  14204. PUSH_I32();
  14205. break;
  14206. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14207. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14208. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14209. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14210. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14211. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14212. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14213. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14214. case WASM_OP_ATOMIC_RMW_I64_AND:
  14215. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14216. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14217. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14218. case WASM_OP_ATOMIC_RMW_I64_OR:
  14219. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14220. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14221. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14222. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14223. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14224. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14225. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14226. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14227. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14228. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14229. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14230. POP_I64();
  14231. POP_MEM_OFFSET();
  14232. PUSH_I64();
  14233. break;
  14234. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14235. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14236. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14237. POP_I32();
  14238. POP_I32();
  14239. POP_MEM_OFFSET();
  14240. PUSH_I32();
  14241. break;
  14242. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14243. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14244. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14245. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14246. POP_I64();
  14247. POP_I64();
  14248. POP_MEM_OFFSET();
  14249. PUSH_I64();
  14250. break;
  14251. default:
  14252. set_error_buf_v(error_buf, error_buf_size,
  14253. "%s %02x %02x", "unsupported opcode",
  14254. 0xfe, opcode1);
  14255. goto fail;
  14256. }
  14257. break;
  14258. }
  14259. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14260. default:
  14261. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14262. "unsupported opcode", opcode);
  14263. goto fail;
  14264. }
  14265. #if WASM_ENABLE_FAST_INTERP != 0
  14266. last_op = opcode;
  14267. #endif
  14268. }
  14269. if (loader_ctx->csp_num > 0) {
  14270. if (cur_func_idx < module->function_count - 1)
  14271. /* Function with missing end marker (between two functions) */
  14272. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14273. else
  14274. /* Function with missing end marker
  14275. (at EOF or end of code sections) */
  14276. set_error_buf(error_buf, error_buf_size,
  14277. "unexpected end of section or function, "
  14278. "or section size mismatch");
  14279. goto fail;
  14280. }
  14281. #if WASM_ENABLE_FAST_INTERP != 0
  14282. if (loader_ctx->p_code_compiled == NULL)
  14283. goto re_scan;
  14284. func->const_cell_num = loader_ctx->const_cell_num;
  14285. if (func->const_cell_num > 0) {
  14286. int32 j;
  14287. if (!(func->consts = func_const = loader_malloc(
  14288. func->const_cell_num * 4, error_buf, error_buf_size)))
  14289. goto fail;
  14290. func_const_end = func->consts + func->const_cell_num * 4;
  14291. /* reverse the const buf */
  14292. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14293. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14294. if (c->value_type == VALUE_TYPE_F64
  14295. || c->value_type == VALUE_TYPE_I64) {
  14296. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14297. &(c->value.f64), (uint32)sizeof(int64));
  14298. func_const += sizeof(int64);
  14299. }
  14300. else {
  14301. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14302. &(c->value.f32), (uint32)sizeof(int32));
  14303. func_const += sizeof(int32);
  14304. }
  14305. }
  14306. }
  14307. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14308. - loader_ctx->start_dynamic_offset + 1;
  14309. #else
  14310. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14311. #endif
  14312. func->max_block_num = loader_ctx->max_csp_num;
  14313. return_value = true;
  14314. fail:
  14315. wasm_loader_ctx_destroy(loader_ctx);
  14316. (void)table_idx;
  14317. (void)table_seg_idx;
  14318. (void)data_seg_idx;
  14319. (void)i64_const;
  14320. (void)local_offset;
  14321. (void)p_org;
  14322. (void)mem_offset;
  14323. (void)align;
  14324. return return_value;
  14325. }