wasm_loader.c 559 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. static void
  36. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  37. {
  38. if (error_buf != NULL) {
  39. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  40. string);
  41. }
  42. }
  43. static void
  44. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  45. {
  46. va_list args;
  47. char buf[128];
  48. if (error_buf != NULL) {
  49. va_start(args, format);
  50. vsnprintf(buf, sizeof(buf), format, args);
  51. va_end(args);
  52. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  53. }
  54. }
  55. static bool
  56. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  57. char *error_buf, uint32 error_buf_size)
  58. {
  59. if ((uintptr_t)buf + length < (uintptr_t)buf
  60. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  61. set_error_buf(error_buf, error_buf_size,
  62. "unexpected end of section or function");
  63. return false;
  64. }
  65. return true;
  66. }
  67. static bool
  68. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  69. char *error_buf, uint32 error_buf_size)
  70. {
  71. if ((uintptr_t)buf + length < (uintptr_t)buf
  72. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  73. set_error_buf(error_buf, error_buf_size, "unexpected end");
  74. return false;
  75. }
  76. return true;
  77. }
  78. #define CHECK_BUF(buf, buf_end, length) \
  79. do { \
  80. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  81. goto fail; \
  82. } \
  83. } while (0)
  84. #define CHECK_BUF1(buf, buf_end, length) \
  85. do { \
  86. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  87. goto fail; \
  88. } \
  89. } while (0)
  90. #define skip_leb(p) while (*p++ & 0x80)
  91. #define skip_leb_int64(p, p_end) skip_leb(p)
  92. #define skip_leb_uint32(p, p_end) skip_leb(p)
  93. #define skip_leb_int32(p, p_end) skip_leb(p)
  94. static bool
  95. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  96. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  97. {
  98. const uint8 *buf = *p_buf;
  99. uint64 result = 0;
  100. uint32 shift = 0;
  101. uint32 offset = 0, bcnt = 0;
  102. uint64 byte;
  103. while (true) {
  104. /* uN or SN must not exceed ceil(N/7) bytes */
  105. if (bcnt + 1 > (maxbits + 6) / 7) {
  106. set_error_buf(error_buf, error_buf_size,
  107. "integer representation too long");
  108. return false;
  109. }
  110. CHECK_BUF(buf, buf_end, offset + 1);
  111. byte = buf[offset];
  112. offset += 1;
  113. result |= ((byte & 0x7f) << shift);
  114. shift += 7;
  115. bcnt += 1;
  116. if ((byte & 0x80) == 0) {
  117. break;
  118. }
  119. }
  120. if (!sign && maxbits == 32 && shift >= maxbits) {
  121. /* The top bits set represent values > 32 bits */
  122. if (((uint8)byte) & 0xf0)
  123. goto fail_integer_too_large;
  124. }
  125. else if (sign && maxbits == 32) {
  126. if (shift < maxbits) {
  127. /* Sign extend, second highest bit is the sign bit */
  128. if ((uint8)byte & 0x40)
  129. result |= (~((uint64)0)) << shift;
  130. }
  131. else {
  132. /* The top bits should be a sign-extension of the sign bit */
  133. bool sign_bit_set = ((uint8)byte) & 0x8;
  134. int top_bits = ((uint8)byte) & 0xf0;
  135. if ((sign_bit_set && top_bits != 0x70)
  136. || (!sign_bit_set && top_bits != 0))
  137. goto fail_integer_too_large;
  138. }
  139. }
  140. else if (sign && maxbits == 64) {
  141. if (shift < maxbits) {
  142. /* Sign extend, second highest bit is the sign bit */
  143. if ((uint8)byte & 0x40)
  144. result |= (~((uint64)0)) << shift;
  145. }
  146. else {
  147. /* The top bits should be a sign-extension of the sign bit */
  148. bool sign_bit_set = ((uint8)byte) & 0x1;
  149. int top_bits = ((uint8)byte) & 0xfe;
  150. if ((sign_bit_set && top_bits != 0x7e)
  151. || (!sign_bit_set && top_bits != 0))
  152. goto fail_integer_too_large;
  153. }
  154. }
  155. *p_buf += offset;
  156. *p_result = result;
  157. return true;
  158. fail_integer_too_large:
  159. set_error_buf(error_buf, error_buf_size, "integer too large");
  160. fail:
  161. return false;
  162. }
  163. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  164. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  165. #define read_leb_int64(p, p_end, res) \
  166. do { \
  167. uint64 res64; \
  168. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  169. error_buf_size)) \
  170. goto fail; \
  171. res = (int64)res64; \
  172. } while (0)
  173. #define read_leb_uint32(p, p_end, res) \
  174. do { \
  175. uint64 res64; \
  176. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  177. error_buf_size)) \
  178. goto fail; \
  179. res = (uint32)res64; \
  180. } while (0)
  181. #define read_leb_int32(p, p_end, res) \
  182. do { \
  183. uint64 res64; \
  184. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  185. error_buf_size)) \
  186. goto fail; \
  187. res = (int32)res64; \
  188. } while (0)
  189. static char *
  190. type2str(uint8 type)
  191. {
  192. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  193. #if WASM_ENABLE_GC != 0
  194. char *type_str_ref[] = { "stringview_iter",
  195. "stringview_wtf16",
  196. "(ref null ht)",
  197. "(ref ht)",
  198. "", /* reserved */
  199. "stringview_wtf8",
  200. "stringref",
  201. "", /* reserved */
  202. "", /* reserved */
  203. "arrayref",
  204. "structref",
  205. "i32ref",
  206. "eqref",
  207. "anyref",
  208. "externref",
  209. "funcref",
  210. "nullref",
  211. "nullexternref",
  212. "nullfuncref" };
  213. #endif
  214. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  215. return type_str[type - VALUE_TYPE_V128];
  216. #if WASM_ENABLE_GC != 0
  217. else if (wasm_is_type_reftype(type))
  218. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  219. #endif
  220. else if (type == VALUE_TYPE_FUNCREF)
  221. return "funcref";
  222. else if (type == VALUE_TYPE_EXTERNREF)
  223. return "externref";
  224. else
  225. return "unknown type";
  226. }
  227. static bool
  228. is_32bit_type(uint8 type)
  229. {
  230. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  231. #if WASM_ENABLE_GC != 0
  232. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  233. #elif WASM_ENABLE_REF_TYPES != 0
  234. /* For reference types, we use uint32 index to represent
  235. the funcref and externref */
  236. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  237. #endif
  238. )
  239. return true;
  240. return false;
  241. }
  242. static bool
  243. is_64bit_type(uint8 type)
  244. {
  245. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  246. #if WASM_ENABLE_GC != 0
  247. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  248. #endif
  249. )
  250. return true;
  251. return false;
  252. }
  253. static bool
  254. is_value_type(uint8 type)
  255. {
  256. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  257. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  258. #if WASM_ENABLE_GC != 0
  259. /* reference types, 0x65 to 0x70 */
  260. || wasm_is_type_reftype(type)
  261. #elif WASM_ENABLE_REF_TYPES != 0
  262. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  263. #endif
  264. #if WASM_ENABLE_SIMD != 0
  265. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  266. || type == VALUE_TYPE_V128 /* 0x7B */
  267. #endif
  268. #endif
  269. )
  270. return true;
  271. return false;
  272. }
  273. #if WASM_ENABLE_GC != 0
  274. static bool
  275. is_packed_type(uint8 type)
  276. {
  277. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  278. }
  279. #endif
  280. static bool
  281. is_byte_a_type(uint8 type)
  282. {
  283. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  284. }
  285. #if WASM_ENABLE_SIMD != 0
  286. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  287. static V128
  288. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  289. {
  290. V128 result;
  291. uint8 i;
  292. for (i = 0; i != 16; ++i) {
  293. result.i8x16[i] = read_uint8(p_buf);
  294. }
  295. return result;
  296. }
  297. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  298. #endif /* end of WASM_ENABLE_SIMD */
  299. static void *
  300. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  301. {
  302. void *mem;
  303. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  304. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  305. return NULL;
  306. }
  307. memset(mem, 0, (uint32)size);
  308. return mem;
  309. }
  310. static void *
  311. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  312. uint32 error_buf_size)
  313. {
  314. uint8 *mem_new;
  315. bh_assert(size_new > size_old);
  316. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  317. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  318. memset(mem_new + size_old, 0, size_new - size_old);
  319. wasm_runtime_free(mem_old);
  320. }
  321. return mem_new;
  322. }
  323. #define MEM_REALLOC(mem, size_old, size_new) \
  324. do { \
  325. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  326. error_buf_size); \
  327. if (!mem_new) \
  328. goto fail; \
  329. mem = mem_new; \
  330. } while (0)
  331. static bool
  332. check_utf8_str(const uint8 *str, uint32 len)
  333. {
  334. /* The valid ranges are taken from page 125, below link
  335. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  336. const uint8 *p = str, *p_end = str + len;
  337. uint8 chr;
  338. while (p < p_end) {
  339. chr = *p;
  340. if (chr == 0) {
  341. LOG_WARNING(
  342. "LIMITATION: a string which contains '\\00' is unsupported");
  343. return false;
  344. }
  345. else if (chr < 0x80) {
  346. p++;
  347. }
  348. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  349. if (p[1] < 0x80 || p[1] > 0xBF) {
  350. return false;
  351. }
  352. p += 2;
  353. }
  354. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  355. if (chr == 0xE0) {
  356. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  357. return false;
  358. }
  359. }
  360. else if (chr == 0xED) {
  361. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  362. return false;
  363. }
  364. }
  365. else { /* chr >= 0xE1 && chr <= 0xEF */
  366. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  367. return false;
  368. }
  369. }
  370. p += 3;
  371. }
  372. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  373. if (chr == 0xF0) {
  374. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  375. || p[3] < 0x80 || p[3] > 0xBF) {
  376. return false;
  377. }
  378. }
  379. else if (chr <= 0xF3) { /* and also chr >= 0xF1 */
  380. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  381. || p[3] < 0x80 || p[3] > 0xBF) {
  382. return false;
  383. }
  384. }
  385. else { /* chr == 0xF4 */
  386. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  387. || p[3] < 0x80 || p[3] > 0xBF) {
  388. return false;
  389. }
  390. }
  391. p += 4;
  392. }
  393. else {
  394. return false;
  395. }
  396. }
  397. return (p == p_end);
  398. }
  399. static char *
  400. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  401. bool is_load_from_file_buf, char *error_buf,
  402. uint32 error_buf_size)
  403. {
  404. StringNode *node, *node_next;
  405. if (!check_utf8_str(str, len)) {
  406. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  407. return NULL;
  408. }
  409. if (len == 0) {
  410. return "";
  411. }
  412. else if (is_load_from_file_buf) {
  413. /* As the file buffer can be referred to after loading, we use
  414. the previous byte of leb encoded size to adjust the string:
  415. move string 1 byte backward and then append '\0' */
  416. char *c_str = (char *)str - 1;
  417. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  418. c_str[len] = '\0';
  419. return c_str;
  420. }
  421. /* Search const str list */
  422. node = module->const_str_list;
  423. while (node) {
  424. node_next = node->next;
  425. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  426. break;
  427. node = node_next;
  428. }
  429. if (node) {
  430. return node->str;
  431. }
  432. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  433. error_buf_size))) {
  434. return NULL;
  435. }
  436. node->str = ((char *)node) + sizeof(StringNode);
  437. bh_memcpy_s(node->str, len + 1, str, len);
  438. node->str[len] = '\0';
  439. if (!module->const_str_list) {
  440. /* set as head */
  441. module->const_str_list = node;
  442. node->next = NULL;
  443. }
  444. else {
  445. /* insert it */
  446. node->next = module->const_str_list;
  447. module->const_str_list = node;
  448. }
  449. return node->str;
  450. }
  451. #if WASM_ENABLE_GC != 0
  452. static bool
  453. check_type_index(const WASMModule *module, uint32 type_index, char *error_buf,
  454. uint32 error_buf_size)
  455. {
  456. if (type_index >= module->type_count) {
  457. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  458. type_index);
  459. return false;
  460. }
  461. return true;
  462. }
  463. static bool
  464. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  465. uint32 error_buf_size)
  466. {
  467. if (!check_type_index(module, type_index, error_buf, error_buf_size)) {
  468. return false;
  469. }
  470. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  471. set_error_buf(error_buf, error_buf_size, "unkown array type");
  472. return false;
  473. }
  474. return true;
  475. }
  476. #endif
  477. static bool
  478. check_function_index(const WASMModule *module, uint32 function_index,
  479. char *error_buf, uint32 error_buf_size)
  480. {
  481. if (function_index
  482. >= module->import_function_count + module->function_count) {
  483. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  484. function_index);
  485. return false;
  486. }
  487. return true;
  488. }
  489. typedef struct InitValue {
  490. uint8 type;
  491. uint8 flag;
  492. #if WASM_ENABLE_GC != 0
  493. uint8 gc_opcode;
  494. WASMRefType ref_type;
  495. #endif
  496. WASMValue value;
  497. } InitValue;
  498. typedef struct ConstExprContext {
  499. uint32 sp;
  500. uint32 size;
  501. WASMModule *module;
  502. InitValue *stack;
  503. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  504. } ConstExprContext;
  505. static void
  506. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  507. {
  508. ctx->sp = 0;
  509. ctx->module = module;
  510. ctx->stack = ctx->data;
  511. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  512. }
  513. static bool
  514. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  515. #if WASM_ENABLE_GC != 0
  516. WASMRefType *ref_type, uint8 gc_opcode,
  517. #endif
  518. WASMValue *value, char *error_buf, uint32 error_buf_size)
  519. {
  520. InitValue *cur_value;
  521. if (ctx->sp >= ctx->size) {
  522. if (ctx->stack != ctx->data) {
  523. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  524. (ctx->size + 4) * sizeof(InitValue));
  525. }
  526. else {
  527. if (!(ctx->stack =
  528. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  529. error_buf, error_buf_size))) {
  530. goto fail;
  531. }
  532. }
  533. ctx->size += 4;
  534. }
  535. cur_value = &ctx->stack[ctx->sp++];
  536. cur_value->type = type;
  537. cur_value->flag = flag;
  538. cur_value->value = *value;
  539. #if WASM_ENABLE_GC != 0
  540. cur_value->gc_opcode = gc_opcode;
  541. if (wasm_is_type_multi_byte_type(type)) {
  542. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  543. ref_type, wasm_reftype_struct_size(ref_type));
  544. }
  545. #endif
  546. return true;
  547. fail:
  548. return false;
  549. }
  550. static bool
  551. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  552. #if WASM_ENABLE_GC != 0
  553. WASMRefType *ref_type, uint8 *p_gc_opcode,
  554. #endif
  555. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  556. {
  557. InitValue *cur_value;
  558. if (ctx->sp == 0) {
  559. set_error_buf(error_buf, error_buf_size,
  560. "type mismatch: const expr stack underflow");
  561. return false;
  562. }
  563. cur_value = &ctx->stack[--ctx->sp];
  564. #if WASM_ENABLE_GC == 0
  565. if (cur_value->type != type) {
  566. set_error_buf(error_buf, error_buf_size, "type mismatch");
  567. return false;
  568. }
  569. #else
  570. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  571. ref_type, ctx->module->types,
  572. ctx->module->type_count)) {
  573. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  574. "type mismatch: expect ", type2str(type),
  575. " but got other");
  576. goto fail;
  577. }
  578. if ((ctx->sp != 0) && (cur_value->flag == WASM_OP_GC_PREFIX)
  579. && (cur_value->gc_opcode != WASM_OP_REF_I31)) {
  580. /* To reduce complexity, we don't allow initialize struct fields/array
  581. * element with references, so struct/array must be at the bottom of the
  582. * init value stack */
  583. set_error_buf(
  584. error_buf, error_buf_size,
  585. "struct or array as field is not supported in constant expr");
  586. goto fail;
  587. }
  588. #endif
  589. if (p_flag)
  590. *p_flag = cur_value->flag;
  591. if (p_value)
  592. *p_value = cur_value->value;
  593. #if WASM_ENABLE_GC != 0
  594. if (p_gc_opcode)
  595. *p_gc_opcode = cur_value->gc_opcode;
  596. #endif
  597. return true;
  598. #if WASM_ENABLE_GC != 0
  599. fail:
  600. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  601. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  602. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  603. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  604. wasm_runtime_free(cur_value->value.data);
  605. }
  606. return false;
  607. #endif
  608. }
  609. static void
  610. destroy_const_expr_stack(ConstExprContext *ctx)
  611. {
  612. #if WASM_ENABLE_GC != 0
  613. uint32 i;
  614. for (i = 0; i < ctx->sp; i++) {
  615. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  616. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  617. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  618. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  619. wasm_runtime_free(ctx->stack[i].value.data);
  620. }
  621. }
  622. #endif
  623. if (ctx->stack != ctx->data) {
  624. wasm_runtime_free(ctx->stack);
  625. }
  626. }
  627. #if WASM_ENABLE_GC != 0
  628. static void
  629. destroy_init_expr(InitializerExpression *expr)
  630. {
  631. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  632. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  633. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  634. wasm_runtime_free(expr->u.data);
  635. }
  636. }
  637. #endif /* end of WASM_ENABLE_GC != 0 */
  638. static bool
  639. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  640. InitializerExpression *init_expr, uint8 type, void *ref_type,
  641. char *error_buf, uint32 error_buf_size)
  642. {
  643. const uint8 *p = *p_buf, *p_end = buf_end;
  644. uint8 flag, *p_float;
  645. uint32 i;
  646. ConstExprContext const_expr_ctx = { 0 };
  647. WASMValue cur_value;
  648. #if WASM_ENABLE_GC != 0
  649. uint8 opcode1;
  650. uint32 type_idx;
  651. WASMRefType cur_ref_type = { 0 };
  652. #endif
  653. init_const_expr_stack(&const_expr_ctx, module);
  654. CHECK_BUF(p, p_end, 1);
  655. flag = read_uint8(p);
  656. while (flag != WASM_OP_END) {
  657. switch (flag) {
  658. /* i32.const */
  659. case INIT_EXPR_TYPE_I32_CONST:
  660. read_leb_int32(p, p_end, cur_value.i32);
  661. if (!push_const_expr_stack(
  662. &const_expr_ctx, flag, VALUE_TYPE_I32,
  663. #if WASM_ENABLE_GC != 0
  664. NULL, 0,
  665. #endif
  666. &cur_value, error_buf, error_buf_size))
  667. goto fail;
  668. break;
  669. /* i64.const */
  670. case INIT_EXPR_TYPE_I64_CONST:
  671. read_leb_int64(p, p_end, cur_value.i64);
  672. if (!push_const_expr_stack(
  673. &const_expr_ctx, flag, VALUE_TYPE_I64,
  674. #if WASM_ENABLE_GC != 0
  675. NULL, 0,
  676. #endif
  677. &cur_value, error_buf, error_buf_size))
  678. goto fail;
  679. break;
  680. /* f32.const */
  681. case INIT_EXPR_TYPE_F32_CONST:
  682. CHECK_BUF(p, p_end, 4);
  683. p_float = (uint8 *)&cur_value.f32;
  684. for (i = 0; i < sizeof(float32); i++)
  685. *p_float++ = *p++;
  686. if (!push_const_expr_stack(
  687. &const_expr_ctx, flag, VALUE_TYPE_F32,
  688. #if WASM_ENABLE_GC != 0
  689. NULL, 0,
  690. #endif
  691. &cur_value, error_buf, error_buf_size))
  692. goto fail;
  693. break;
  694. /* f64.const */
  695. case INIT_EXPR_TYPE_F64_CONST:
  696. CHECK_BUF(p, p_end, 8);
  697. p_float = (uint8 *)&cur_value.f64;
  698. for (i = 0; i < sizeof(float64); i++)
  699. *p_float++ = *p++;
  700. if (!push_const_expr_stack(
  701. &const_expr_ctx, flag, VALUE_TYPE_F64,
  702. #if WASM_ENABLE_GC != 0
  703. NULL, 0,
  704. #endif
  705. &cur_value, error_buf, error_buf_size))
  706. goto fail;
  707. break;
  708. #if WASM_ENABLE_SIMD != 0
  709. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  710. /* v128.const */
  711. case INIT_EXPR_TYPE_V128_CONST:
  712. {
  713. uint64 high, low;
  714. CHECK_BUF(p, p_end, 1);
  715. (void)read_uint8(p);
  716. CHECK_BUF(p, p_end, 16);
  717. wasm_runtime_read_v128(p, &high, &low);
  718. p += 16;
  719. cur_value.v128.i64x2[0] = high;
  720. cur_value.v128.i64x2[1] = low;
  721. if (!push_const_expr_stack(
  722. &const_expr_ctx, flag, VALUE_TYPE_V128,
  723. #if WASM_ENABLE_GC != 0
  724. NULL, 0,
  725. #endif
  726. &cur_value, error_buf, error_buf_size))
  727. goto fail;
  728. #if WASM_ENABLE_WAMR_COMPILER != 0
  729. /* If any init_expr is v128.const, mark SIMD used */
  730. module->is_simd_used = true;
  731. #endif
  732. break;
  733. }
  734. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  735. #endif /* end of WASM_ENABLE_SIMD */
  736. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  737. /* ref.func */
  738. case INIT_EXPR_TYPE_FUNCREF_CONST:
  739. {
  740. uint32 func_idx;
  741. read_leb_uint32(p, p_end, func_idx);
  742. cur_value.ref_index = func_idx;
  743. if (!check_function_index(module, func_idx, error_buf,
  744. error_buf_size)) {
  745. goto fail;
  746. }
  747. #if WASM_ENABLE_GC == 0
  748. if (!push_const_expr_stack(&const_expr_ctx, flag,
  749. VALUE_TYPE_FUNCREF, &cur_value,
  750. error_buf, error_buf_size))
  751. goto fail;
  752. #else
  753. if (func_idx < module->import_function_count) {
  754. type_idx =
  755. module->import_functions[func_idx].u.function.type_idx;
  756. }
  757. else {
  758. type_idx = module
  759. ->functions[func_idx
  760. - module->import_function_count]
  761. ->type_idx;
  762. }
  763. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  764. false, type_idx);
  765. if (!push_const_expr_stack(&const_expr_ctx, flag,
  766. cur_ref_type.ref_type, &cur_ref_type,
  767. 0, &cur_value, error_buf,
  768. error_buf_size))
  769. goto fail;
  770. #endif
  771. #if WASM_ENABLE_WAMR_COMPILER != 0
  772. module->is_ref_types_used = true;
  773. #endif
  774. break;
  775. }
  776. /* ref.null */
  777. case INIT_EXPR_TYPE_REFNULL_CONST:
  778. {
  779. uint8 type1;
  780. CHECK_BUF(p, p_end, 1);
  781. type1 = read_uint8(p);
  782. #if WASM_ENABLE_GC == 0
  783. cur_value.ref_index = NULL_REF;
  784. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  785. &cur_value, error_buf,
  786. error_buf_size))
  787. goto fail;
  788. #else
  789. cur_value.gc_obj = NULL_REF;
  790. if (!is_byte_a_type(type1)) {
  791. p--;
  792. read_leb_uint32(p, p_end, type_idx);
  793. if (!check_type_index(module, type_idx, error_buf,
  794. error_buf_size))
  795. goto fail;
  796. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  797. true, type_idx);
  798. if (!push_const_expr_stack(&const_expr_ctx, flag,
  799. cur_ref_type.ref_type,
  800. &cur_ref_type, 0, &cur_value,
  801. error_buf, error_buf_size))
  802. goto fail;
  803. }
  804. else {
  805. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  806. NULL, 0, &cur_value, error_buf,
  807. error_buf_size))
  808. goto fail;
  809. }
  810. #endif
  811. #if WASM_ENABLE_WAMR_COMPILER != 0
  812. module->is_ref_types_used = true;
  813. #endif
  814. break;
  815. }
  816. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  817. /* get_global */
  818. case INIT_EXPR_TYPE_GET_GLOBAL:
  819. {
  820. uint32 global_idx;
  821. uint8 global_type;
  822. read_leb_uint32(p, p_end, cur_value.global_index);
  823. global_idx = cur_value.global_index;
  824. #if WASM_ENABLE_GC == 0
  825. if (global_idx >= module->import_global_count) {
  826. /**
  827. * Currently, constant expressions occurring as initializers
  828. * of globals are further constrained in that contained
  829. * global.get instructions are
  830. * only allowed to refer to imported globals.
  831. */
  832. set_error_buf_v(error_buf, error_buf_size,
  833. "unknown global %u", global_idx);
  834. goto fail;
  835. }
  836. if (module->import_globals[global_idx].u.global.is_mutable) {
  837. set_error_buf_v(error_buf, error_buf_size,
  838. "constant expression required");
  839. goto fail;
  840. }
  841. #else
  842. if (global_idx
  843. >= module->import_global_count + module->global_count) {
  844. set_error_buf_v(error_buf, error_buf_size,
  845. "unknown global %u", global_idx);
  846. goto fail;
  847. }
  848. if (global_idx < module->import_global_count
  849. && module->import_globals[global_idx].u.global.is_mutable) {
  850. set_error_buf_v(error_buf, error_buf_size,
  851. "constant expression required");
  852. goto fail;
  853. }
  854. #endif
  855. if (global_idx < module->import_global_count) {
  856. global_type =
  857. module->import_globals[global_idx].u.global.type;
  858. #if WASM_ENABLE_GC != 0
  859. if (wasm_is_type_multi_byte_type(global_type)) {
  860. WASMRefType *global_ref_type =
  861. module->import_globals[global_idx]
  862. .u.global.ref_type;
  863. bh_memcpy_s(&cur_ref_type,
  864. wasm_reftype_struct_size(global_ref_type),
  865. global_ref_type,
  866. wasm_reftype_struct_size(global_ref_type));
  867. }
  868. #endif
  869. }
  870. else {
  871. global_type =
  872. module
  873. ->globals[global_idx - module->import_global_count]
  874. .type;
  875. #if WASM_ENABLE_GC != 0
  876. if (wasm_is_type_multi_byte_type(global_type)) {
  877. WASMRefType *global_ref_type =
  878. module
  879. ->globals[global_idx
  880. - module->import_global_count]
  881. .ref_type;
  882. bh_memcpy_s(&cur_ref_type,
  883. wasm_reftype_struct_size(global_ref_type),
  884. global_ref_type,
  885. wasm_reftype_struct_size(global_ref_type));
  886. }
  887. #endif
  888. }
  889. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  890. #if WASM_ENABLE_GC != 0
  891. &cur_ref_type, 0,
  892. #endif
  893. &cur_value, error_buf,
  894. error_buf_size))
  895. goto fail;
  896. break;
  897. }
  898. #if WASM_ENABLE_GC != 0
  899. /* struct.new and array.new */
  900. case WASM_OP_GC_PREFIX:
  901. {
  902. CHECK_BUF(p, p_end, 1);
  903. opcode1 = read_uint8(p);
  904. switch (opcode1) {
  905. case WASM_OP_STRUCT_NEW:
  906. {
  907. WASMStructType *struct_type;
  908. WASMStructNewInitValues *struct_init_values = NULL;
  909. uint32 field_count;
  910. read_leb_uint32(p, p_end, type_idx);
  911. if (!check_type_index(module, type_idx, error_buf,
  912. error_buf_size)) {
  913. goto fail;
  914. }
  915. struct_type = (WASMStructType *)module->types[type_idx];
  916. if (struct_type->base_type.type_flag
  917. != WASM_TYPE_STRUCT) {
  918. set_error_buf(error_buf, error_buf_size,
  919. "unkown struct type");
  920. goto fail;
  921. }
  922. field_count = struct_type->field_count;
  923. if (!(struct_init_values = loader_malloc(
  924. offsetof(WASMStructNewInitValues, fields)
  925. + (uint64)field_count * sizeof(WASMValue),
  926. error_buf, error_buf_size))) {
  927. goto fail;
  928. }
  929. struct_init_values->count = field_count;
  930. for (i = field_count; i > 0; i--) {
  931. WASMRefType *field_ref_type = NULL;
  932. uint32 field_idx = i - 1;
  933. uint8 field_type =
  934. struct_type->fields[field_idx].field_type;
  935. if (wasm_is_type_multi_byte_type(field_type)) {
  936. field_ref_type = wasm_reftype_map_find(
  937. struct_type->ref_type_maps,
  938. struct_type->ref_type_map_count, field_idx);
  939. }
  940. if (is_packed_type(field_type)) {
  941. field_type = VALUE_TYPE_I32;
  942. }
  943. if (!pop_const_expr_stack(
  944. &const_expr_ctx, NULL, field_type,
  945. field_ref_type, NULL,
  946. &struct_init_values->fields[field_idx],
  947. error_buf, error_buf_size)) {
  948. wasm_runtime_free(struct_init_values);
  949. goto fail;
  950. }
  951. }
  952. cur_value.data = struct_init_values;
  953. wasm_set_refheaptype_typeidx(
  954. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  955. if (!push_const_expr_stack(
  956. &const_expr_ctx, flag, cur_ref_type.ref_type,
  957. &cur_ref_type, opcode1, &cur_value, error_buf,
  958. error_buf_size)) {
  959. wasm_runtime_free(struct_init_values);
  960. goto fail;
  961. }
  962. break;
  963. }
  964. case WASM_OP_STRUCT_NEW_DEFAULT:
  965. {
  966. read_leb_uint32(p, p_end, cur_value.type_index);
  967. type_idx = cur_value.type_index;
  968. if (!check_type_index(module, type_idx, error_buf,
  969. error_buf_size)) {
  970. goto fail;
  971. }
  972. if (module->types[type_idx]->type_flag
  973. != WASM_TYPE_STRUCT) {
  974. set_error_buf(error_buf, error_buf_size,
  975. "unkown struct type");
  976. goto fail;
  977. }
  978. cur_value.type_index = type_idx;
  979. wasm_set_refheaptype_typeidx(
  980. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  981. if (!push_const_expr_stack(
  982. &const_expr_ctx, flag, cur_ref_type.ref_type,
  983. &cur_ref_type, opcode1, &cur_value, error_buf,
  984. error_buf_size)) {
  985. goto fail;
  986. }
  987. break;
  988. }
  989. case WASM_OP_ARRAY_NEW:
  990. case WASM_OP_ARRAY_NEW_DEFAULT:
  991. case WASM_OP_ARRAY_NEW_FIXED:
  992. {
  993. WASMArrayNewInitValues *array_init_values = NULL;
  994. WASMArrayType *array_type = NULL;
  995. WASMRefType *elem_ref_type = NULL;
  996. uint64 total_size;
  997. uint8 elem_type;
  998. read_leb_uint32(p, p_end, cur_value.type_index);
  999. type_idx = cur_value.type_index;
  1000. if (!check_type_index(module, type_idx, error_buf,
  1001. error_buf_size)) {
  1002. goto fail;
  1003. }
  1004. array_type = (WASMArrayType *)module->types[type_idx];
  1005. if (array_type->base_type.type_flag
  1006. != WASM_TYPE_ARRAY) {
  1007. set_error_buf(error_buf, error_buf_size,
  1008. "unkown array type");
  1009. goto fail;
  1010. }
  1011. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  1012. elem_type = array_type->elem_type;
  1013. if (wasm_is_type_multi_byte_type(elem_type)) {
  1014. elem_ref_type = array_type->elem_ref_type;
  1015. }
  1016. if (is_packed_type(elem_type)) {
  1017. elem_type = VALUE_TYPE_I32;
  1018. }
  1019. if (opcode1 == WASM_OP_ARRAY_NEW) {
  1020. WASMValue len_val;
  1021. if (!(array_init_values = loader_malloc(
  1022. sizeof(WASMArrayNewInitValues),
  1023. error_buf, error_buf_size))) {
  1024. goto fail;
  1025. }
  1026. array_init_values->type_idx = type_idx;
  1027. if (!pop_const_expr_stack(
  1028. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  1029. NULL, NULL, &len_val, error_buf,
  1030. error_buf_size)) {
  1031. wasm_runtime_free(array_init_values);
  1032. goto fail;
  1033. }
  1034. array_init_values->length = len_val.i32;
  1035. if (!pop_const_expr_stack(
  1036. &const_expr_ctx, NULL, elem_type,
  1037. elem_ref_type, NULL,
  1038. &array_init_values->elem_data[0],
  1039. error_buf, error_buf_size)) {
  1040. wasm_runtime_free(array_init_values);
  1041. goto fail;
  1042. }
  1043. cur_value.data = array_init_values;
  1044. }
  1045. else {
  1046. /* WASM_OP_ARRAY_NEW_FIXED */
  1047. uint32 len;
  1048. read_leb_uint32(p, p_end, len);
  1049. total_size =
  1050. (uint64)offsetof(WASMArrayNewInitValues,
  1051. elem_data)
  1052. + (uint64)sizeof(WASMValue) * len;
  1053. if (!(array_init_values =
  1054. loader_malloc(total_size, error_buf,
  1055. error_buf_size))) {
  1056. goto fail;
  1057. }
  1058. array_init_values->type_idx = type_idx;
  1059. array_init_values->length = len;
  1060. for (i = len; i > 0; i--) {
  1061. if (!pop_const_expr_stack(
  1062. &const_expr_ctx, NULL, elem_type,
  1063. elem_ref_type, NULL,
  1064. &array_init_values
  1065. ->elem_data[i - 1],
  1066. error_buf, error_buf_size)) {
  1067. wasm_runtime_free(array_init_values);
  1068. goto fail;
  1069. }
  1070. }
  1071. cur_value.data = array_init_values;
  1072. }
  1073. }
  1074. else {
  1075. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1076. WASMValue len_val;
  1077. uint32 len;
  1078. /* POP(i32) */
  1079. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1080. VALUE_TYPE_I32, NULL,
  1081. NULL, &len_val, error_buf,
  1082. error_buf_size)) {
  1083. goto fail;
  1084. }
  1085. len = len_val.i32;
  1086. cur_value.array_new_default.type_index = type_idx;
  1087. cur_value.array_new_default.length = len;
  1088. }
  1089. wasm_set_refheaptype_typeidx(
  1090. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1091. if (!push_const_expr_stack(
  1092. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1093. &cur_ref_type, opcode1, &cur_value, error_buf,
  1094. error_buf_size)) {
  1095. if (array_init_values) {
  1096. wasm_runtime_free(array_init_values);
  1097. }
  1098. goto fail;
  1099. }
  1100. break;
  1101. }
  1102. case WASM_OP_ANY_CONVERT_EXTERN:
  1103. {
  1104. set_error_buf(error_buf, error_buf_size,
  1105. "unsuppoted constant expression of "
  1106. "extern.internalize");
  1107. goto fail;
  1108. }
  1109. case WASM_OP_EXTERN_CONVERT_ANY:
  1110. {
  1111. set_error_buf(error_buf, error_buf_size,
  1112. "unsuppoted constant expression of "
  1113. "extern.externalize");
  1114. goto fail;
  1115. }
  1116. case WASM_OP_REF_I31:
  1117. {
  1118. /* POP(i32) */
  1119. if (!pop_const_expr_stack(
  1120. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1121. NULL, &cur_value, error_buf, error_buf_size)) {
  1122. goto fail;
  1123. }
  1124. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1125. false, HEAP_TYPE_I31);
  1126. if (!push_const_expr_stack(
  1127. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1128. &cur_ref_type, opcode1, &cur_value, error_buf,
  1129. error_buf_size)) {
  1130. goto fail;
  1131. }
  1132. break;
  1133. }
  1134. default:
  1135. set_error_buf(
  1136. error_buf, error_buf_size,
  1137. "type mismatch or constant expression required");
  1138. goto fail;
  1139. }
  1140. break;
  1141. }
  1142. #endif /* end of WASM_ENABLE_GC != 0 */
  1143. default:
  1144. {
  1145. set_error_buf(error_buf, error_buf_size,
  1146. "illegal opcode "
  1147. "or constant expression required "
  1148. "or type mismatch");
  1149. goto fail;
  1150. }
  1151. }
  1152. CHECK_BUF(p, p_end, 1);
  1153. flag = read_uint8(p);
  1154. }
  1155. /* There should be only one value left on the init value stack */
  1156. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1157. #if WASM_ENABLE_GC != 0
  1158. ref_type, &opcode1,
  1159. #endif
  1160. &cur_value, error_buf, error_buf_size)) {
  1161. goto fail;
  1162. }
  1163. if (const_expr_ctx.sp != 0) {
  1164. set_error_buf(error_buf, error_buf_size,
  1165. "type mismatch: illegal constant opcode sequence");
  1166. goto fail;
  1167. }
  1168. init_expr->init_expr_type = flag;
  1169. init_expr->u = cur_value;
  1170. #if WASM_ENABLE_GC != 0
  1171. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1172. switch (opcode1) {
  1173. case WASM_OP_STRUCT_NEW:
  1174. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1175. break;
  1176. case WASM_OP_STRUCT_NEW_DEFAULT:
  1177. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1178. break;
  1179. case WASM_OP_ARRAY_NEW:
  1180. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1181. break;
  1182. case WASM_OP_ARRAY_NEW_DEFAULT:
  1183. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1184. break;
  1185. case WASM_OP_ARRAY_NEW_FIXED:
  1186. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1187. break;
  1188. case WASM_OP_REF_I31:
  1189. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1190. break;
  1191. default:
  1192. bh_assert(0);
  1193. break;
  1194. }
  1195. }
  1196. #endif /* end of WASM_ENABLE_GC != 0 */
  1197. *p_buf = p;
  1198. destroy_const_expr_stack(&const_expr_ctx);
  1199. return true;
  1200. fail:
  1201. destroy_const_expr_stack(&const_expr_ctx);
  1202. return false;
  1203. }
  1204. static bool
  1205. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1206. {
  1207. if (mutable >= 2) {
  1208. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1209. return false;
  1210. }
  1211. return true;
  1212. }
  1213. #if WASM_ENABLE_GC != 0
  1214. static void
  1215. destroy_func_type(WASMFuncType *type)
  1216. {
  1217. /* Destroy the reference type hash set */
  1218. if (type->ref_type_maps)
  1219. wasm_runtime_free(type->ref_type_maps);
  1220. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1221. && WASM_ENABLE_LAZY_JIT != 0
  1222. if (type->call_to_llvm_jit_from_fast_jit)
  1223. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1224. #endif
  1225. /* Free the type */
  1226. wasm_runtime_free(type);
  1227. }
  1228. static void
  1229. destroy_struct_type(WASMStructType *type)
  1230. {
  1231. if (type->ref_type_maps)
  1232. wasm_runtime_free(type->ref_type_maps);
  1233. wasm_runtime_free(type);
  1234. }
  1235. static void
  1236. destroy_array_type(WASMArrayType *type)
  1237. {
  1238. wasm_runtime_free(type);
  1239. }
  1240. static void
  1241. destroy_wasm_type(WASMType *type)
  1242. {
  1243. if (type->type_flag == WASM_TYPE_FUNC)
  1244. destroy_func_type((WASMFuncType *)type);
  1245. else if (type->type_flag == WASM_TYPE_STRUCT)
  1246. destroy_struct_type((WASMStructType *)type);
  1247. else if (type->type_flag == WASM_TYPE_ARRAY)
  1248. destroy_array_type((WASMArrayType *)type);
  1249. else {
  1250. bh_assert(0);
  1251. }
  1252. }
  1253. /* Resolve (ref null ht) or (ref ht) */
  1254. static bool
  1255. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1256. WASMModule *module, bool nullable, WASMRefType *ref_type,
  1257. char *error_buf, uint32 error_buf_size)
  1258. {
  1259. const uint8 *p = *p_buf, *p_end = buf_end;
  1260. ref_type->ref_type =
  1261. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1262. ref_type->ref_ht_common.nullable = nullable;
  1263. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1264. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1265. /* heap type is (type i), i : typeidx, >= 0 */
  1266. if (!check_type_index(module, ref_type->ref_ht_typeidx.type_idx,
  1267. error_buf, error_buf_size)) {
  1268. return false;
  1269. }
  1270. }
  1271. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1272. /* heap type is func, extern, any, eq, i31 or data */
  1273. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1274. return false;
  1275. }
  1276. *p_buf = p;
  1277. return true;
  1278. fail:
  1279. return false;
  1280. }
  1281. static bool
  1282. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1283. WASMModule *module, bool *p_need_ref_type_map,
  1284. WASMRefType *ref_type, bool allow_packed_type,
  1285. char *error_buf, uint32 error_buf_size)
  1286. {
  1287. const uint8 *p = *p_buf, *p_end = buf_end;
  1288. uint8 type;
  1289. memset(ref_type, 0, sizeof(WASMRefType));
  1290. CHECK_BUF(p, p_end, 1);
  1291. type = read_uint8(p);
  1292. if (wasm_is_reftype_htref_nullable(type)) {
  1293. /* (ref null ht) */
  1294. if (!resolve_reftype_htref(&p, p_end, module, true, ref_type, error_buf,
  1295. error_buf_size))
  1296. return false;
  1297. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1298. *p_need_ref_type_map = true;
  1299. else {
  1300. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1301. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1302. multi-byte type to one-byte type to reduce the footprint and
  1303. the complexity of type equal/subtype checking */
  1304. ref_type->ref_type =
  1305. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1306. *p_need_ref_type_map = false;
  1307. }
  1308. }
  1309. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1310. /* (ref ht) */
  1311. if (!resolve_reftype_htref(&p, p_end, module, false, ref_type,
  1312. error_buf, error_buf_size))
  1313. return false;
  1314. *p_need_ref_type_map = true;
  1315. #if WASM_ENABLE_STRINGREF != 0
  1316. /* covert (ref string) to stringref */
  1317. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1318. ref_type->ref_type =
  1319. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1320. *p_need_ref_type_map = false;
  1321. }
  1322. #endif
  1323. }
  1324. else {
  1325. /* type which can be represented by one byte */
  1326. if (!is_value_type(type)
  1327. && !(allow_packed_type && is_packed_type(type))) {
  1328. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1329. return false;
  1330. }
  1331. ref_type->ref_type = type;
  1332. *p_need_ref_type_map = false;
  1333. #if WASM_ENABLE_WAMR_COMPILER != 0
  1334. /* If any value's type is v128, mark the module as SIMD used */
  1335. if (type == VALUE_TYPE_V128)
  1336. module->is_simd_used = true;
  1337. #endif
  1338. }
  1339. *p_buf = p;
  1340. return true;
  1341. fail:
  1342. return false;
  1343. }
  1344. static WASMRefType *
  1345. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1346. char *error_buf, uint32 error_buf_size)
  1347. {
  1348. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1349. if (!ret) {
  1350. set_error_buf(error_buf, error_buf_size,
  1351. "insert ref type to hash set failed");
  1352. }
  1353. return ret;
  1354. }
  1355. static bool
  1356. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1357. uint32 type_idx, char *error_buf, uint32 error_buf_size)
  1358. {
  1359. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1360. uint32 param_count, result_count, i, j = 0;
  1361. uint32 param_cell_num, ret_cell_num;
  1362. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1363. uint64 total_size;
  1364. bool need_ref_type_map;
  1365. WASMRefType ref_type;
  1366. WASMFuncType *type = NULL;
  1367. /* Parse first time to resolve param count, result count and
  1368. ref type map count */
  1369. read_leb_uint32(p, p_end, param_count);
  1370. p_org = p;
  1371. for (i = 0; i < param_count; i++) {
  1372. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1373. &ref_type, false, error_buf, error_buf_size)) {
  1374. return false;
  1375. }
  1376. if (need_ref_type_map)
  1377. ref_type_map_count++;
  1378. }
  1379. read_leb_uint32(p, p_end, result_count);
  1380. for (i = 0; i < result_count; i++) {
  1381. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1382. &ref_type, false, error_buf, error_buf_size)) {
  1383. return false;
  1384. }
  1385. if (need_ref_type_map) {
  1386. ref_type_map_count++;
  1387. result_ref_type_map_count++;
  1388. }
  1389. }
  1390. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1391. "ref type map count: %d",
  1392. type_idx, param_count, result_count, ref_type_map_count);
  1393. /* Parse second time to resolve param types, result types and
  1394. ref type map info */
  1395. p = p_org;
  1396. total_size = offsetof(WASMFuncType, types)
  1397. + sizeof(uint8) * (uint64)(param_count + result_count);
  1398. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1399. return false;
  1400. }
  1401. if (ref_type_map_count > 0) {
  1402. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1403. if (!(type->ref_type_maps =
  1404. loader_malloc(total_size, error_buf, error_buf_size))) {
  1405. goto fail;
  1406. }
  1407. }
  1408. type->base_type.type_flag = WASM_TYPE_FUNC;
  1409. type->param_count = param_count;
  1410. type->result_count = result_count;
  1411. type->ref_type_map_count = ref_type_map_count;
  1412. type->result_ref_type_maps =
  1413. type->ref_type_maps + ref_type_map_count - result_ref_type_map_count;
  1414. for (i = 0; i < param_count; i++) {
  1415. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1416. &ref_type, false, error_buf, error_buf_size)) {
  1417. goto fail;
  1418. }
  1419. type->types[i] = ref_type.ref_type;
  1420. if (need_ref_type_map) {
  1421. type->ref_type_maps[j].index = i;
  1422. if (!(type->ref_type_maps[j++].ref_type =
  1423. reftype_set_insert(module->ref_type_set, &ref_type,
  1424. error_buf, error_buf_size))) {
  1425. goto fail;
  1426. }
  1427. }
  1428. }
  1429. read_leb_uint32(p, p_end, result_count);
  1430. for (i = 0; i < result_count; i++) {
  1431. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1432. &ref_type, false, error_buf, error_buf_size)) {
  1433. goto fail;
  1434. }
  1435. type->types[param_count + i] = ref_type.ref_type;
  1436. if (need_ref_type_map) {
  1437. type->ref_type_maps[j].index = param_count + i;
  1438. if (!(type->ref_type_maps[j++].ref_type =
  1439. reftype_set_insert(module->ref_type_set, &ref_type,
  1440. error_buf, error_buf_size))) {
  1441. goto fail;
  1442. }
  1443. }
  1444. }
  1445. bh_assert(j == type->ref_type_map_count);
  1446. #if TRACE_WASM_LOADER != 0
  1447. os_printf("type %d = ", type_idx);
  1448. wasm_dump_func_type(type);
  1449. #endif
  1450. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1451. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1452. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1453. set_error_buf(error_buf, error_buf_size,
  1454. "param count or result count too large");
  1455. goto fail;
  1456. }
  1457. type->param_cell_num = (uint16)param_cell_num;
  1458. type->ret_cell_num = (uint16)ret_cell_num;
  1459. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1460. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1461. #endif
  1462. #if WASM_ENABLE_WAMR_COMPILER != 0
  1463. for (i = 0; i < type->param_count + type->result_count; i++) {
  1464. if (type->types[i] == VALUE_TYPE_V128)
  1465. module->is_simd_used = true;
  1466. }
  1467. #endif
  1468. /* Calculate the minimal type index of the type equal to this type */
  1469. type->min_type_idx_normalized = type_idx;
  1470. for (i = 0; i < type_idx; i++) {
  1471. WASMFuncType *func_type = (WASMFuncType *)module->types[i];
  1472. if (func_type->base_type.type_flag == WASM_TYPE_FUNC
  1473. && wasm_func_type_equal(type, func_type, module->types,
  1474. type_idx + 1)) {
  1475. type->min_type_idx_normalized = i;
  1476. break;
  1477. }
  1478. }
  1479. *p_buf = p;
  1480. module->types[type_idx] = (WASMType *)type;
  1481. return true;
  1482. fail:
  1483. if (type)
  1484. destroy_func_type(type);
  1485. return false;
  1486. }
  1487. static bool
  1488. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1489. WASMModule *module, uint32 type_idx, char *error_buf,
  1490. uint32 error_buf_size)
  1491. {
  1492. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1493. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1494. uint32 i, j = 0, offset;
  1495. uint16 *reference_table;
  1496. uint64 total_size;
  1497. uint8 mutable;
  1498. bool need_ref_type_map;
  1499. WASMRefType ref_type;
  1500. WASMStructType *type = NULL;
  1501. /* Parse first time to resolve field count and ref type map count */
  1502. read_leb_uint32(p, p_end, field_count);
  1503. p_org = p;
  1504. for (i = 0; i < field_count; i++) {
  1505. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1506. &ref_type, true, error_buf, error_buf_size)) {
  1507. return false;
  1508. }
  1509. if (need_ref_type_map)
  1510. ref_type_map_count++;
  1511. if (wasm_is_type_reftype(ref_type.ref_type))
  1512. ref_field_count++;
  1513. CHECK_BUF(p, p_end, 1);
  1514. mutable = read_uint8(p);
  1515. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1516. return false;
  1517. }
  1518. }
  1519. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1520. type_idx, field_count, ref_type_map_count);
  1521. /* Parse second time to resolve field types and ref type map info */
  1522. p = p_org;
  1523. total_size = offsetof(WASMStructType, fields)
  1524. + sizeof(WASMStructFieldType) * (uint64)field_count
  1525. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1526. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1527. return false;
  1528. }
  1529. if (ref_type_map_count > 0) {
  1530. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1531. if (!(type->ref_type_maps =
  1532. loader_malloc(total_size, error_buf, error_buf_size))) {
  1533. goto fail;
  1534. }
  1535. }
  1536. type->reference_table = reference_table =
  1537. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1538. + sizeof(WASMStructFieldType) * field_count);
  1539. *reference_table++ = ref_field_count;
  1540. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1541. type->field_count = field_count;
  1542. type->ref_type_map_count = ref_type_map_count;
  1543. offset = (uint32)offsetof(WASMStructObject, field_data);
  1544. for (i = 0; i < field_count; i++) {
  1545. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1546. &ref_type, true, error_buf, error_buf_size)) {
  1547. goto fail;
  1548. }
  1549. type->fields[i].field_type = ref_type.ref_type;
  1550. if (need_ref_type_map) {
  1551. type->ref_type_maps[j].index = i;
  1552. if (!(type->ref_type_maps[j++].ref_type =
  1553. reftype_set_insert(module->ref_type_set, &ref_type,
  1554. error_buf, error_buf_size))) {
  1555. goto fail;
  1556. }
  1557. }
  1558. CHECK_BUF(p, p_end, 1);
  1559. type->fields[i].field_flags = read_uint8(p);
  1560. type->fields[i].field_size =
  1561. (uint8)wasm_reftype_size(ref_type.ref_type);
  1562. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1563. || defined(BUILD_TARGET_X86_32))
  1564. if (type->fields[i].field_size == 2)
  1565. offset = align_uint(offset, 2);
  1566. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1567. offset = align_uint(offset, 4);
  1568. #endif
  1569. type->fields[i].field_offset = offset;
  1570. if (wasm_is_type_reftype(ref_type.ref_type))
  1571. *reference_table++ = offset;
  1572. offset += type->fields[i].field_size;
  1573. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1574. type->fields[i].field_flags, type->fields[i].field_type);
  1575. }
  1576. type->total_size = offset;
  1577. bh_assert(j == type->ref_type_map_count);
  1578. #if TRACE_WASM_LOADER != 0
  1579. os_printf("type %d = ", type_idx);
  1580. wasm_dump_struct_type(type);
  1581. #endif
  1582. *p_buf = p;
  1583. module->types[type_idx] = (WASMType *)type;
  1584. return true;
  1585. fail:
  1586. if (type)
  1587. destroy_struct_type(type);
  1588. return false;
  1589. }
  1590. static bool
  1591. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1592. WASMModule *module, uint32 type_idx, char *error_buf,
  1593. uint32 error_buf_size)
  1594. {
  1595. const uint8 *p = *p_buf, *p_end = buf_end;
  1596. uint8 mutable;
  1597. bool need_ref_type_map;
  1598. WASMRefType ref_type;
  1599. WASMArrayType *type = NULL;
  1600. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  1601. true, error_buf, error_buf_size)) {
  1602. return false;
  1603. }
  1604. CHECK_BUF(p, p_end, 1);
  1605. mutable = read_uint8(p);
  1606. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1607. return false;
  1608. }
  1609. LOG_VERBOSE("type %u: array", type_idx);
  1610. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1611. error_buf_size))) {
  1612. return false;
  1613. }
  1614. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1615. type->elem_flags = mutable;
  1616. type->elem_type = ref_type.ref_type;
  1617. if (need_ref_type_map) {
  1618. if (!(type->elem_ref_type =
  1619. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1620. error_buf_size))) {
  1621. goto fail;
  1622. }
  1623. }
  1624. #if TRACE_WASM_LOADER != 0
  1625. os_printf("type %d = ", type_idx);
  1626. wasm_dump_array_type(type);
  1627. #endif
  1628. *p_buf = p;
  1629. module->types[type_idx] = (WASMType *)type;
  1630. return true;
  1631. fail:
  1632. if (type)
  1633. destroy_array_type(type);
  1634. return false;
  1635. }
  1636. static bool
  1637. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1638. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1639. {
  1640. if (heap_type >= 0) {
  1641. if (!check_type_index(module, heap_type, error_buf, error_buf_size)) {
  1642. return false;
  1643. }
  1644. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1645. heap_type);
  1646. }
  1647. else {
  1648. if (!wasm_is_valid_heap_type(heap_type)) {
  1649. set_error_buf(error_buf, error_buf_size, "unknown type");
  1650. return false;
  1651. }
  1652. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1653. heap_type);
  1654. if (nullable) {
  1655. /* For (ref null func/extern/any/eq/i31/data),
  1656. they are same as
  1657. funcref/externref/anyref/eqref/i31ref/dataref,
  1658. we convert the multi-byte type to one-byte
  1659. type to reduce the footprint and the
  1660. complexity of type equal/subtype checking */
  1661. ref_type->ref_type =
  1662. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1663. }
  1664. }
  1665. return true;
  1666. }
  1667. static void
  1668. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1669. WASMRefType *ref_type2)
  1670. {
  1671. /**
  1672. * The difference rt1 ∖ rt2 between two reference types is defined as
  1673. * follows:
  1674. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1675. * (ref ht2) = (ref null?1 ht1)
  1676. */
  1677. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1678. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1679. ref_type1, wasm_reftype_struct_size(ref_type1));
  1680. }
  1681. else {
  1682. ref_type_diff->ref_type = ref_type1->ref_type;
  1683. }
  1684. if (ref_type2->ref_ht_common.nullable) {
  1685. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1686. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1687. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1688. (int32)ref_type_diff->ref_type - 0x80);
  1689. }
  1690. else {
  1691. ref_type_diff->ref_ht_typeidx.nullable = false;
  1692. }
  1693. }
  1694. }
  1695. #else /* else of WASM_ENABLE_GC != 0 */
  1696. static void
  1697. destroy_wasm_type(WASMType *type)
  1698. {
  1699. if (type->ref_count > 1) {
  1700. /* The type is referenced by other types
  1701. of current wasm module */
  1702. type->ref_count--;
  1703. return;
  1704. }
  1705. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1706. && WASM_ENABLE_LAZY_JIT != 0
  1707. if (type->call_to_llvm_jit_from_fast_jit)
  1708. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1709. #endif
  1710. wasm_runtime_free(type);
  1711. }
  1712. #endif /* end of WASM_ENABLE_GC != 0 */
  1713. static bool
  1714. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1715. char *error_buf, uint32 error_buf_size)
  1716. {
  1717. const uint8 *p = buf, *p_end = buf_end;
  1718. uint32 type_count, i;
  1719. uint64 total_size;
  1720. uint8 flag;
  1721. #if WASM_ENABLE_GC != 0
  1722. uint32 processed_type_count = 0;
  1723. #endif
  1724. read_leb_uint32(p, p_end, type_count);
  1725. if (type_count) {
  1726. module->type_count = type_count;
  1727. total_size = sizeof(WASMType *) * (uint64)type_count;
  1728. if (!(module->types =
  1729. loader_malloc(total_size, error_buf, error_buf_size))) {
  1730. return false;
  1731. }
  1732. #if WASM_ENABLE_GC == 0
  1733. for (i = 0; i < type_count; i++) {
  1734. WASMFuncType *type;
  1735. const uint8 *p_org;
  1736. uint32 param_count, result_count, j;
  1737. uint32 param_cell_num, ret_cell_num;
  1738. CHECK_BUF(p, p_end, 1);
  1739. flag = read_uint8(p);
  1740. if (flag != 0x60) {
  1741. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1742. return false;
  1743. }
  1744. read_leb_uint32(p, p_end, param_count);
  1745. /* Resolve param count and result count firstly */
  1746. p_org = p;
  1747. CHECK_BUF(p, p_end, param_count);
  1748. p += param_count;
  1749. read_leb_uint32(p, p_end, result_count);
  1750. CHECK_BUF(p, p_end, result_count);
  1751. p = p_org;
  1752. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1753. set_error_buf(error_buf, error_buf_size,
  1754. "param count or result count too large");
  1755. return false;
  1756. }
  1757. total_size = offsetof(WASMFuncType, types)
  1758. + sizeof(uint8) * (uint64)(param_count + result_count);
  1759. if (!(type = module->types[i] =
  1760. loader_malloc(total_size, error_buf, error_buf_size))) {
  1761. return false;
  1762. }
  1763. /* Resolve param types and result types */
  1764. type->ref_count = 1;
  1765. type->param_count = (uint16)param_count;
  1766. type->result_count = (uint16)result_count;
  1767. for (j = 0; j < param_count; j++) {
  1768. CHECK_BUF(p, p_end, 1);
  1769. type->types[j] = read_uint8(p);
  1770. }
  1771. read_leb_uint32(p, p_end, result_count);
  1772. for (j = 0; j < result_count; j++) {
  1773. CHECK_BUF(p, p_end, 1);
  1774. type->types[param_count + j] = read_uint8(p);
  1775. }
  1776. for (j = 0; j < param_count + result_count; j++) {
  1777. if (!is_value_type(type->types[j])) {
  1778. set_error_buf(error_buf, error_buf_size,
  1779. "unknown value type");
  1780. return false;
  1781. }
  1782. }
  1783. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1784. ret_cell_num =
  1785. wasm_get_cell_num(type->types + param_count, result_count);
  1786. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1787. set_error_buf(error_buf, error_buf_size,
  1788. "param count or result count too large");
  1789. return false;
  1790. }
  1791. type->param_cell_num = (uint16)param_cell_num;
  1792. type->ret_cell_num = (uint16)ret_cell_num;
  1793. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1794. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1795. #endif
  1796. #if WASM_ENABLE_WAMR_COMPILER != 0
  1797. for (j = 0; j < type->param_count + type->result_count; j++) {
  1798. if (type->types[j] == VALUE_TYPE_V128)
  1799. module->is_simd_used = true;
  1800. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1801. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1802. module->is_ref_types_used = true;
  1803. }
  1804. #endif
  1805. /* If there is already a same type created, use it instead */
  1806. for (j = 0; j < i; j++) {
  1807. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1808. if (module->types[j]->ref_count == UINT16_MAX) {
  1809. set_error_buf(error_buf, error_buf_size,
  1810. "wasm type's ref count too large");
  1811. return false;
  1812. }
  1813. destroy_wasm_type(type);
  1814. module->types[i] = module->types[j];
  1815. module->types[j]->ref_count++;
  1816. break;
  1817. }
  1818. }
  1819. }
  1820. #else /* else of WASM_ENABLE_GC == 0 */
  1821. for (i = 0; i < type_count; i++) {
  1822. uint32 super_type_count = 0, parent_type_idx = (uint32)-1,
  1823. rec_count = 1, j;
  1824. bool is_sub_final = true;
  1825. CHECK_BUF(p, p_end, 1);
  1826. flag = read_uint8(p);
  1827. if (flag == DEFINED_TYPE_REC) {
  1828. read_leb_uint32(p, p_end, rec_count);
  1829. if (rec_count > 1) {
  1830. uint64 new_total_size;
  1831. module->type_count += rec_count - 1;
  1832. new_total_size =
  1833. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1834. MEM_REALLOC(module->types, total_size, new_total_size);
  1835. total_size = new_total_size;
  1836. }
  1837. LOG_VERBOSE("Processing rec group [%d-%d]",
  1838. processed_type_count,
  1839. processed_type_count + rec_count - 1);
  1840. }
  1841. else {
  1842. p--;
  1843. }
  1844. for (j = 0; j < rec_count; j++) {
  1845. WASMType *cur_type = NULL;
  1846. CHECK_BUF(p, p_end, 1);
  1847. flag = read_uint8(p);
  1848. parent_type_idx = -1;
  1849. if (flag == DEFINED_TYPE_SUB
  1850. || flag == DEFINED_TYPE_SUB_FINAL) {
  1851. read_leb_uint32(p, p_end, super_type_count);
  1852. if (super_type_count > 1) {
  1853. set_error_buf(error_buf, error_buf_size,
  1854. "super type count too large");
  1855. return false;
  1856. }
  1857. if (super_type_count > 0) {
  1858. read_leb_uint32(p, p_end, parent_type_idx);
  1859. if (parent_type_idx >= processed_type_count + j) {
  1860. set_error_buf_v(error_buf, error_buf_size,
  1861. "unknown type %d", parent_type_idx);
  1862. return false;
  1863. }
  1864. if (module->types[parent_type_idx]->is_sub_final) {
  1865. set_error_buf(error_buf, error_buf_size,
  1866. "sub type can not inherit from "
  1867. "a final super type");
  1868. return false;
  1869. }
  1870. }
  1871. if (flag == DEFINED_TYPE_SUB)
  1872. is_sub_final = false;
  1873. CHECK_BUF(p, p_end, 1);
  1874. flag = read_uint8(p);
  1875. }
  1876. if (flag == DEFINED_TYPE_FUNC) {
  1877. if (!resolve_func_type(&p, buf_end, module,
  1878. processed_type_count + j, error_buf,
  1879. error_buf_size)) {
  1880. return false;
  1881. }
  1882. }
  1883. else if (flag == DEFINED_TYPE_STRUCT) {
  1884. if (!resolve_struct_type(&p, buf_end, module,
  1885. processed_type_count + j,
  1886. error_buf, error_buf_size)) {
  1887. return false;
  1888. }
  1889. }
  1890. else if (flag == DEFINED_TYPE_ARRAY) {
  1891. if (!resolve_array_type(&p, buf_end, module,
  1892. processed_type_count + j, error_buf,
  1893. error_buf_size)) {
  1894. return false;
  1895. }
  1896. }
  1897. else {
  1898. set_error_buf(error_buf, error_buf_size,
  1899. "invalid type flag");
  1900. return false;
  1901. }
  1902. cur_type = module->types[processed_type_count + j];
  1903. cur_type->parent_type_idx = parent_type_idx;
  1904. cur_type->is_sub_final = is_sub_final;
  1905. if (rec_count > 1) {
  1906. cur_type->rec_count = rec_count;
  1907. cur_type->rec_idx = j;
  1908. }
  1909. }
  1910. /* resolve subtyping relationship in current rec group */
  1911. for (j = 0; j < rec_count; j++) {
  1912. WASMType *cur_type = module->types[processed_type_count + j];
  1913. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1914. WASMType *parent_type =
  1915. module->types[cur_type->parent_type_idx];
  1916. cur_type->parent_type = parent_type;
  1917. cur_type->root_type = parent_type->root_type;
  1918. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1919. }
  1920. else {
  1921. cur_type->parent_type = NULL;
  1922. cur_type->root_type = cur_type;
  1923. cur_type->inherit_depth = 0;
  1924. }
  1925. }
  1926. for (j = 0; j < rec_count; j++) {
  1927. WASMType *cur_type = module->types[processed_type_count + j];
  1928. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1929. WASMType *parent_type =
  1930. module->types[cur_type->parent_type_idx];
  1931. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1932. module->types,
  1933. module->type_count)) {
  1934. set_error_buf(error_buf, error_buf_size,
  1935. "sub type does not match super type");
  1936. return false;
  1937. }
  1938. }
  1939. }
  1940. if (rec_count > 1) {
  1941. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1942. processed_type_count,
  1943. processed_type_count + rec_count - 1);
  1944. }
  1945. processed_type_count += rec_count;
  1946. }
  1947. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1948. * module->type_count,
  1949. error_buf, error_buf_size))) {
  1950. return false;
  1951. }
  1952. #endif /* end of WASM_ENABLE_GC == 0 */
  1953. }
  1954. if (p != p_end) {
  1955. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1956. return false;
  1957. }
  1958. LOG_VERBOSE("Load type section success.\n");
  1959. return true;
  1960. fail:
  1961. return false;
  1962. }
  1963. static void
  1964. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1965. {
  1966. uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
  1967. ? init_size * 2
  1968. : WASM_TABLE_MAX_SIZE;
  1969. if (max_size_flag) {
  1970. /* module defines the table limitation */
  1971. bh_assert(init_size <= *max_size);
  1972. if (init_size < *max_size) {
  1973. *max_size =
  1974. *max_size < default_max_size ? *max_size : default_max_size;
  1975. }
  1976. }
  1977. else {
  1978. /* partial defined table limitation, gives a default value */
  1979. *max_size = default_max_size;
  1980. }
  1981. }
  1982. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1983. /**
  1984. * Find export item of a module with export info:
  1985. * module name, field name and export kind
  1986. */
  1987. static WASMExport *
  1988. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1989. const char *field_name, uint8 export_kind,
  1990. char *error_buf, uint32 error_buf_size)
  1991. {
  1992. WASMExport *export =
  1993. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  1994. export_kind, error_buf, error_buf_size);
  1995. return export;
  1996. }
  1997. #endif
  1998. #if WASM_ENABLE_MULTI_MODULE != 0
  1999. static WASMFunction *
  2000. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  2001. const WASMFuncType *expected_function_type,
  2002. char *error_buf, uint32 error_buf_size)
  2003. {
  2004. WASMModuleCommon *module_reg;
  2005. WASMFunction *function = NULL;
  2006. WASMExport *export = NULL;
  2007. WASMModule *module = NULL;
  2008. WASMFuncType *target_function_type = NULL;
  2009. module_reg = wasm_runtime_find_module_registered(module_name);
  2010. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2011. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2012. function_name);
  2013. set_error_buf(error_buf, error_buf_size, "unknown import");
  2014. return NULL;
  2015. }
  2016. module = (WASMModule *)module_reg;
  2017. export =
  2018. wasm_loader_find_export(module, module_name, function_name,
  2019. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2020. if (!export) {
  2021. return NULL;
  2022. }
  2023. /* resolve function type and function */
  2024. if (export->index < module->import_function_count) {
  2025. target_function_type =
  2026. module->import_functions[export->index].u.function.func_type;
  2027. function = module->import_functions[export->index]
  2028. .u.function.import_func_linked;
  2029. }
  2030. else {
  2031. target_function_type =
  2032. module->functions[export->index - module->import_function_count]
  2033. ->func_type;
  2034. function =
  2035. module->functions[export->index - module->import_function_count];
  2036. }
  2037. /* check function type */
  2038. if (!wasm_type_equal((WASMType *)expected_function_type,
  2039. (WASMType *)target_function_type, module->types,
  2040. module->type_count)) {
  2041. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2042. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2043. return NULL;
  2044. }
  2045. return function;
  2046. }
  2047. static WASMTable *
  2048. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2049. uint32 init_size, uint32 max_size, char *error_buf,
  2050. uint32 error_buf_size)
  2051. {
  2052. WASMModuleCommon *module_reg;
  2053. WASMTable *table = NULL;
  2054. WASMExport *export = NULL;
  2055. WASMModule *module = NULL;
  2056. module_reg = wasm_runtime_find_module_registered(module_name);
  2057. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2058. LOG_DEBUG("can not find a module named %s for table", module_name);
  2059. set_error_buf(error_buf, error_buf_size, "unknown import");
  2060. return NULL;
  2061. }
  2062. module = (WASMModule *)module_reg;
  2063. export =
  2064. wasm_loader_find_export(module, module_name, table_name,
  2065. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2066. if (!export) {
  2067. return NULL;
  2068. }
  2069. /* resolve table and check the init/max size */
  2070. if (export->index < module->import_table_count) {
  2071. table =
  2072. module->import_tables[export->index].u.table.import_table_linked;
  2073. }
  2074. else {
  2075. table = &(module->tables[export->index - module->import_table_count]);
  2076. }
  2077. if (table->init_size < init_size || table->max_size > max_size) {
  2078. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2079. module_name, table_name, table->init_size, table->max_size,
  2080. init_size, max_size);
  2081. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2082. return NULL;
  2083. }
  2084. return table;
  2085. }
  2086. static WASMMemory *
  2087. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2088. uint32 init_page_count, uint32 max_page_count,
  2089. char *error_buf, uint32 error_buf_size)
  2090. {
  2091. WASMModuleCommon *module_reg;
  2092. WASMMemory *memory = NULL;
  2093. WASMExport *export = NULL;
  2094. WASMModule *module = NULL;
  2095. module_reg = wasm_runtime_find_module_registered(module_name);
  2096. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2097. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2098. set_error_buf(error_buf, error_buf_size, "unknown import");
  2099. return NULL;
  2100. }
  2101. module = (WASMModule *)module_reg;
  2102. export =
  2103. wasm_loader_find_export(module, module_name, memory_name,
  2104. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2105. if (!export) {
  2106. return NULL;
  2107. }
  2108. /* resolve memory and check the init/max page count */
  2109. if (export->index < module->import_memory_count) {
  2110. memory = module->import_memories[export->index]
  2111. .u.memory.import_memory_linked;
  2112. }
  2113. else {
  2114. memory =
  2115. &(module->memories[export->index - module->import_memory_count]);
  2116. }
  2117. if (memory->init_page_count < init_page_count
  2118. || memory->max_page_count > max_page_count) {
  2119. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2120. module_name, memory_name, memory->init_page_count,
  2121. memory->max_page_count, init_page_count, max_page_count);
  2122. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2123. return NULL;
  2124. }
  2125. return memory;
  2126. }
  2127. static WASMGlobal *
  2128. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2129. uint8 type, bool is_mutable, char *error_buf,
  2130. uint32 error_buf_size)
  2131. {
  2132. WASMModuleCommon *module_reg;
  2133. WASMGlobal *global = NULL;
  2134. WASMExport *export = NULL;
  2135. WASMModule *module = NULL;
  2136. module_reg = wasm_runtime_find_module_registered(module_name);
  2137. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2138. LOG_DEBUG("can not find a module named %s for global", module_name);
  2139. set_error_buf(error_buf, error_buf_size, "unknown import");
  2140. return NULL;
  2141. }
  2142. module = (WASMModule *)module_reg;
  2143. export =
  2144. wasm_loader_find_export(module, module_name, global_name,
  2145. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2146. if (!export) {
  2147. return NULL;
  2148. }
  2149. /* resolve and check the global */
  2150. if (export->index < module->import_global_count) {
  2151. global =
  2152. module->import_globals[export->index].u.global.import_global_linked;
  2153. }
  2154. else {
  2155. global =
  2156. &(module->globals[export->index - module->import_global_count]);
  2157. }
  2158. if (global->type != type || global->is_mutable != is_mutable) {
  2159. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2160. module_name, global_name, global->type, global->is_mutable,
  2161. type, is_mutable);
  2162. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2163. return NULL;
  2164. }
  2165. return global;
  2166. }
  2167. #if WASM_ENABLE_TAGS != 0
  2168. static WASMTag *
  2169. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2170. const WASMType *expected_tag_type,
  2171. uint32 *linked_tag_index, char *error_buf,
  2172. uint32 error_buf_size)
  2173. {
  2174. WASMModuleCommon *module_reg;
  2175. WASMTag *tag = NULL;
  2176. WASMExport *export = NULL;
  2177. WASMModule *module = NULL;
  2178. module_reg = wasm_runtime_find_module_registered(module_name);
  2179. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2180. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2181. tag_name);
  2182. set_error_buf(error_buf, error_buf_size, "unknown import");
  2183. return NULL;
  2184. }
  2185. module = (WASMModule *)module_reg;
  2186. export =
  2187. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2188. error_buf, error_buf_size);
  2189. if (!export) {
  2190. return NULL;
  2191. }
  2192. /* resolve tag type and tag */
  2193. if (export->index < module->import_tag_count) {
  2194. /* importing an imported tag from the submodule */
  2195. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2196. }
  2197. else {
  2198. /* importing an section tag from the submodule */
  2199. tag = module->tags[export->index - module->import_tag_count];
  2200. }
  2201. /* check function type */
  2202. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2203. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2204. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2205. return NULL;
  2206. }
  2207. if (linked_tag_index != NULL) {
  2208. *linked_tag_index = export->index;
  2209. }
  2210. return tag;
  2211. }
  2212. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2213. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2214. static bool
  2215. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2216. const WASMModule *parent_module,
  2217. const char *sub_module_name, const char *function_name,
  2218. WASMFunctionImport *function, char *error_buf,
  2219. uint32 error_buf_size)
  2220. {
  2221. const uint8 *p = *p_buf, *p_end = buf_end;
  2222. uint32 declare_type_index = 0;
  2223. WASMFuncType *declare_func_type = NULL;
  2224. WASMFunction *linked_func = NULL;
  2225. #if WASM_ENABLE_MULTI_MODULE != 0
  2226. WASMModule *sub_module = NULL;
  2227. #endif
  2228. const char *linked_signature = NULL;
  2229. void *linked_attachment = NULL;
  2230. bool linked_call_conv_raw = false;
  2231. bool is_native_symbol = false;
  2232. read_leb_uint32(p, p_end, declare_type_index);
  2233. *p_buf = p;
  2234. if (declare_type_index >= parent_module->type_count) {
  2235. set_error_buf(error_buf, error_buf_size, "unknown type");
  2236. return false;
  2237. }
  2238. #if WASM_ENABLE_GC != 0
  2239. function->type_idx = declare_type_index;
  2240. #endif
  2241. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2242. declare_type_index = wasm_get_smallest_type_idx(
  2243. parent_module->types, parent_module->type_count, declare_type_index);
  2244. #endif
  2245. declare_func_type =
  2246. (WASMFuncType *)parent_module->types[declare_type_index];
  2247. /* lookup registered native symbols first */
  2248. linked_func = wasm_native_resolve_symbol(
  2249. sub_module_name, function_name, declare_func_type, &linked_signature,
  2250. &linked_attachment, &linked_call_conv_raw);
  2251. if (linked_func) {
  2252. is_native_symbol = true;
  2253. }
  2254. #if WASM_ENABLE_MULTI_MODULE != 0
  2255. else {
  2256. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2257. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2258. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2259. error_buf_size);
  2260. if (!sub_module) {
  2261. return false;
  2262. }
  2263. }
  2264. linked_func = wasm_loader_resolve_function(
  2265. sub_module_name, function_name, declare_func_type, error_buf,
  2266. error_buf_size);
  2267. }
  2268. #endif
  2269. function->module_name = (char *)sub_module_name;
  2270. function->field_name = (char *)function_name;
  2271. function->func_type = declare_func_type;
  2272. /* func_ptr_linked is for native registered symbol */
  2273. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2274. function->signature = linked_signature;
  2275. function->attachment = linked_attachment;
  2276. function->call_conv_raw = linked_call_conv_raw;
  2277. #if WASM_ENABLE_MULTI_MODULE != 0
  2278. function->import_module = is_native_symbol ? NULL : sub_module;
  2279. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2280. #endif
  2281. return true;
  2282. fail:
  2283. return false;
  2284. }
  2285. static bool
  2286. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2287. uint32 error_buf_size)
  2288. {
  2289. if (max_size < init_size) {
  2290. set_error_buf(error_buf, error_buf_size,
  2291. "size minimum must not be greater than maximum");
  2292. return false;
  2293. }
  2294. return true;
  2295. }
  2296. static bool
  2297. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2298. WASMModule *parent_module, const char *sub_module_name,
  2299. const char *table_name, WASMTableImport *table,
  2300. char *error_buf, uint32 error_buf_size)
  2301. {
  2302. const uint8 *p = *p_buf, *p_end = buf_end;
  2303. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2304. declare_init_size = 0, declare_max_size = 0;
  2305. #if WASM_ENABLE_MULTI_MODULE != 0
  2306. WASMModule *sub_module = NULL;
  2307. WASMTable *linked_table = NULL;
  2308. #endif
  2309. #if WASM_ENABLE_GC != 0
  2310. WASMRefType ref_type;
  2311. bool need_ref_type_map;
  2312. #endif
  2313. #if WASM_ENABLE_GC == 0
  2314. CHECK_BUF(p, p_end, 1);
  2315. /* 0x70 or 0x6F */
  2316. declare_elem_type = read_uint8(p);
  2317. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2318. #if WASM_ENABLE_REF_TYPES != 0
  2319. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2320. #endif
  2321. ) {
  2322. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2323. return false;
  2324. }
  2325. #else /* else of WASM_ENABLE_GC == 0 */
  2326. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2327. &ref_type, false, error_buf, error_buf_size)) {
  2328. return false;
  2329. }
  2330. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2331. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2332. return false;
  2333. }
  2334. declare_elem_type = ref_type.ref_type;
  2335. if (need_ref_type_map) {
  2336. if (!(table->elem_ref_type =
  2337. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2338. error_buf, error_buf_size))) {
  2339. return false;
  2340. }
  2341. }
  2342. #if TRACE_WASM_LOADER != 0
  2343. os_printf("import table type: ");
  2344. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  2345. os_printf("\n");
  2346. #endif
  2347. #endif /* end of WASM_ENABLE_GC == 0 */
  2348. read_leb_uint32(p, p_end, declare_max_size_flag);
  2349. if (declare_max_size_flag > 1) {
  2350. set_error_buf(error_buf, error_buf_size, "integer too large");
  2351. return false;
  2352. }
  2353. read_leb_uint32(p, p_end, declare_init_size);
  2354. if (declare_max_size_flag) {
  2355. read_leb_uint32(p, p_end, declare_max_size);
  2356. if (!check_table_max_size(declare_init_size, declare_max_size,
  2357. error_buf, error_buf_size))
  2358. return false;
  2359. }
  2360. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2361. &declare_max_size);
  2362. *p_buf = p;
  2363. #if WASM_ENABLE_MULTI_MODULE != 0
  2364. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2365. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2366. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2367. error_buf_size);
  2368. if (!sub_module) {
  2369. return false;
  2370. }
  2371. linked_table = wasm_loader_resolve_table(
  2372. sub_module_name, table_name, declare_init_size, declare_max_size,
  2373. error_buf, error_buf_size);
  2374. if (!linked_table) {
  2375. return false;
  2376. }
  2377. /* reset with linked table limit */
  2378. declare_elem_type = linked_table->elem_type;
  2379. declare_init_size = linked_table->init_size;
  2380. declare_max_size = linked_table->max_size;
  2381. declare_max_size_flag = linked_table->flags;
  2382. table->import_table_linked = linked_table;
  2383. table->import_module = sub_module;
  2384. }
  2385. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2386. /* (table (export "table") 10 20 funcref) */
  2387. /* we need this section working in wamrc */
  2388. if (!strcmp("spectest", sub_module_name)) {
  2389. const uint32 spectest_table_init_size = 10;
  2390. const uint32 spectest_table_max_size = 20;
  2391. if (strcmp("table", table_name)) {
  2392. set_error_buf(error_buf, error_buf_size,
  2393. "incompatible import type or unknown import");
  2394. return false;
  2395. }
  2396. if (declare_init_size > spectest_table_init_size
  2397. || declare_max_size < spectest_table_max_size) {
  2398. set_error_buf(error_buf, error_buf_size,
  2399. "incompatible import type");
  2400. return false;
  2401. }
  2402. declare_init_size = spectest_table_init_size;
  2403. declare_max_size = spectest_table_max_size;
  2404. }
  2405. /* now we believe all declaration are ok */
  2406. table->elem_type = declare_elem_type;
  2407. table->init_size = declare_init_size;
  2408. table->flags = declare_max_size_flag;
  2409. table->max_size = declare_max_size;
  2410. #if WASM_ENABLE_WAMR_COMPILER != 0
  2411. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2412. parent_module->is_ref_types_used = true;
  2413. #endif
  2414. (void)parent_module;
  2415. return true;
  2416. fail:
  2417. return false;
  2418. }
  2419. static bool
  2420. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  2421. {
  2422. if (init_size > DEFAULT_MAX_PAGES) {
  2423. set_error_buf(error_buf, error_buf_size,
  2424. "memory size must be at most 65536 pages (4GiB)");
  2425. return false;
  2426. }
  2427. return true;
  2428. }
  2429. static bool
  2430. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2431. uint32 error_buf_size)
  2432. {
  2433. if (max_size < init_size) {
  2434. set_error_buf(error_buf, error_buf_size,
  2435. "size minimum must not be greater than maximum");
  2436. return false;
  2437. }
  2438. if (max_size > DEFAULT_MAX_PAGES) {
  2439. set_error_buf(error_buf, error_buf_size,
  2440. "memory size must be at most 65536 pages (4GiB)");
  2441. return false;
  2442. }
  2443. return true;
  2444. }
  2445. static bool
  2446. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2447. WASMModule *parent_module, const char *sub_module_name,
  2448. const char *memory_name, WASMMemoryImport *memory,
  2449. char *error_buf, uint32 error_buf_size)
  2450. {
  2451. const uint8 *p = *p_buf, *p_end = buf_end;
  2452. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2453. uint32 pool_size = wasm_runtime_memory_pool_size();
  2454. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2455. / DEFAULT_NUM_BYTES_PER_PAGE;
  2456. #else
  2457. uint32 max_page_count = DEFAULT_MAX_PAGES;
  2458. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2459. uint32 declare_max_page_count_flag = 0;
  2460. uint32 declare_init_page_count = 0;
  2461. uint32 declare_max_page_count = 0;
  2462. #if WASM_ENABLE_MULTI_MODULE != 0
  2463. WASMModule *sub_module = NULL;
  2464. WASMMemory *linked_memory = NULL;
  2465. #endif
  2466. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  2467. read_leb_uint32(p, p_end, declare_init_page_count);
  2468. if (!check_memory_init_size(declare_init_page_count, error_buf,
  2469. error_buf_size)) {
  2470. return false;
  2471. }
  2472. if (declare_max_page_count_flag & 1) {
  2473. read_leb_uint32(p, p_end, declare_max_page_count);
  2474. if (!check_memory_max_size(declare_init_page_count,
  2475. declare_max_page_count, error_buf,
  2476. error_buf_size)) {
  2477. return false;
  2478. }
  2479. if (declare_max_page_count > max_page_count) {
  2480. declare_max_page_count = max_page_count;
  2481. }
  2482. }
  2483. else {
  2484. /* Limit the maximum memory size to max_page_count */
  2485. declare_max_page_count = max_page_count;
  2486. }
  2487. #if WASM_ENABLE_MULTI_MODULE != 0
  2488. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2489. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2490. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2491. error_buf_size);
  2492. if (!sub_module) {
  2493. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  2494. /* Avoid memory import failure when wasi-threads is enabled
  2495. and the memory is shared */
  2496. if (!(declare_max_page_count_flag & 2))
  2497. return false;
  2498. #else
  2499. return false;
  2500. #endif /* WASM_ENABLE_LIB_WASI_THREADS */
  2501. }
  2502. else {
  2503. linked_memory = wasm_loader_resolve_memory(
  2504. sub_module_name, memory_name, declare_init_page_count,
  2505. declare_max_page_count, error_buf, error_buf_size);
  2506. if (!linked_memory) {
  2507. return false;
  2508. }
  2509. /**
  2510. * reset with linked memory limit
  2511. */
  2512. memory->import_module = sub_module;
  2513. memory->import_memory_linked = linked_memory;
  2514. declare_init_page_count = linked_memory->init_page_count;
  2515. declare_max_page_count = linked_memory->max_page_count;
  2516. }
  2517. }
  2518. #endif
  2519. /* (memory (export "memory") 1 2) */
  2520. if (!strcmp("spectest", sub_module_name)) {
  2521. uint32 spectest_memory_init_page = 1;
  2522. uint32 spectest_memory_max_page = 2;
  2523. if (strcmp("memory", memory_name)) {
  2524. set_error_buf(error_buf, error_buf_size,
  2525. "incompatible import type or unknown import");
  2526. return false;
  2527. }
  2528. if (declare_init_page_count > spectest_memory_init_page
  2529. || declare_max_page_count < spectest_memory_max_page) {
  2530. set_error_buf(error_buf, error_buf_size,
  2531. "incompatible import type");
  2532. return false;
  2533. }
  2534. declare_init_page_count = spectest_memory_init_page;
  2535. declare_max_page_count = spectest_memory_max_page;
  2536. }
  2537. /* now we believe all declaration are ok */
  2538. memory->flags = declare_max_page_count_flag;
  2539. memory->init_page_count = declare_init_page_count;
  2540. memory->max_page_count = declare_max_page_count;
  2541. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2542. *p_buf = p;
  2543. (void)parent_module;
  2544. return true;
  2545. fail:
  2546. return false;
  2547. }
  2548. #if WASM_ENABLE_TAGS != 0
  2549. static bool
  2550. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2551. const WASMModule *parent_module, /* this module ! */
  2552. const char *sub_module_name, const char *tag_name,
  2553. WASMTagImport *tag, /* structure to fill */
  2554. char *error_buf, uint32 error_buf_size)
  2555. {
  2556. /* attribute and type of the import statement */
  2557. uint8 declare_tag_attribute;
  2558. uint32 declare_type_index;
  2559. const uint8 *p = *p_buf, *p_end = buf_end;
  2560. #if WASM_ENABLE_MULTI_MODULE != 0
  2561. WASMModule *sub_module = NULL;
  2562. #endif
  2563. /* get the one byte attribute */
  2564. CHECK_BUF(p, p_end, 1);
  2565. declare_tag_attribute = read_uint8(p);
  2566. if (declare_tag_attribute != 0) {
  2567. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2568. goto fail;
  2569. }
  2570. /* get type */
  2571. read_leb_uint32(p, p_end, declare_type_index);
  2572. /* compare against module->types */
  2573. if (declare_type_index >= parent_module->type_count) {
  2574. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2575. goto fail;
  2576. }
  2577. WASMType *declare_tag_type = parent_module->types[declare_type_index];
  2578. /* check, that the type of the declared tag returns void */
  2579. if (declare_tag_type->result_count != 0) {
  2580. set_error_buf(error_buf, error_buf_size,
  2581. "tag type signature does not return void");
  2582. goto fail;
  2583. }
  2584. #if WASM_ENABLE_MULTI_MODULE != 0
  2585. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2586. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2587. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2588. error_buf_size);
  2589. if (!sub_module) {
  2590. return false;
  2591. }
  2592. /* wasm_loader_resolve_tag checks, that the imported tag
  2593. * and the declared tag have the same type
  2594. */
  2595. uint32 linked_tag_index = 0;
  2596. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2597. sub_module_name, tag_name, declare_tag_type,
  2598. &linked_tag_index /* out */, error_buf, error_buf_size);
  2599. if (linked_tag) {
  2600. tag->import_module = sub_module;
  2601. tag->import_tag_linked = linked_tag;
  2602. tag->import_tag_index_linked = linked_tag_index;
  2603. }
  2604. }
  2605. #endif
  2606. /* store to module tag declarations */
  2607. tag->attribute = declare_tag_attribute;
  2608. tag->type = declare_type_index;
  2609. tag->module_name = (char *)sub_module_name;
  2610. tag->field_name = (char *)tag_name;
  2611. tag->tag_type = declare_tag_type;
  2612. *p_buf = p;
  2613. (void)parent_module;
  2614. LOG_VERBOSE("Load tag import success\n");
  2615. return true;
  2616. fail:
  2617. return false;
  2618. }
  2619. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2620. static bool
  2621. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2622. WASMModule *parent_module, char *sub_module_name,
  2623. char *global_name, WASMGlobalImport *global, char *error_buf,
  2624. uint32 error_buf_size)
  2625. {
  2626. const uint8 *p = *p_buf, *p_end = buf_end;
  2627. uint8 declare_type = 0;
  2628. uint8 declare_mutable = 0;
  2629. #if WASM_ENABLE_MULTI_MODULE != 0
  2630. WASMModule *sub_module = NULL;
  2631. WASMGlobal *linked_global = NULL;
  2632. #endif
  2633. #if WASM_ENABLE_GC != 0
  2634. WASMRefType ref_type;
  2635. bool need_ref_type_map;
  2636. #endif
  2637. bool ret = false;
  2638. #if WASM_ENABLE_GC == 0
  2639. CHECK_BUF(p, p_end, 2);
  2640. declare_type = read_uint8(p);
  2641. declare_mutable = read_uint8(p);
  2642. #else
  2643. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2644. &ref_type, false, error_buf, error_buf_size)) {
  2645. return false;
  2646. }
  2647. declare_type = ref_type.ref_type;
  2648. if (need_ref_type_map) {
  2649. if (!(global->ref_type =
  2650. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2651. error_buf, error_buf_size))) {
  2652. return false;
  2653. }
  2654. }
  2655. #if TRACE_WASM_LOADER != 0
  2656. os_printf("import global type: ");
  2657. wasm_dump_value_type(declare_type, global->ref_type);
  2658. os_printf("\n");
  2659. #endif
  2660. CHECK_BUF(p, p_end, 1);
  2661. declare_mutable = read_uint8(p);
  2662. #endif /* end of WASM_ENABLE_GC == 0 */
  2663. *p_buf = p;
  2664. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2665. return false;
  2666. }
  2667. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2668. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2669. global);
  2670. if (ret) {
  2671. if (global->type != declare_type
  2672. || global->is_mutable != declare_mutable) {
  2673. set_error_buf(error_buf, error_buf_size,
  2674. "incompatible import type");
  2675. return false;
  2676. }
  2677. global->is_linked = true;
  2678. }
  2679. #endif
  2680. #if WASM_ENABLE_MULTI_MODULE != 0
  2681. if (!global->is_linked
  2682. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2683. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2684. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2685. error_buf_size);
  2686. if (!sub_module) {
  2687. return false;
  2688. }
  2689. /* check sub modules */
  2690. linked_global = wasm_loader_resolve_global(
  2691. sub_module_name, global_name, declare_type, declare_mutable,
  2692. error_buf, error_buf_size);
  2693. if (linked_global) {
  2694. global->import_module = sub_module;
  2695. global->import_global_linked = linked_global;
  2696. global->is_linked = true;
  2697. }
  2698. }
  2699. #endif
  2700. global->module_name = sub_module_name;
  2701. global->field_name = global_name;
  2702. global->type = declare_type;
  2703. global->is_mutable = (declare_mutable == 1);
  2704. #if WASM_ENABLE_WAMR_COMPILER != 0
  2705. if (global->type == VALUE_TYPE_V128)
  2706. parent_module->is_simd_used = true;
  2707. else if (global->type == VALUE_TYPE_EXTERNREF)
  2708. parent_module->is_ref_types_used = true;
  2709. #endif
  2710. (void)parent_module;
  2711. (void)ret;
  2712. return true;
  2713. fail:
  2714. return false;
  2715. }
  2716. static bool
  2717. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2718. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2719. {
  2720. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2721. #if WASM_ENABLE_GC != 0
  2722. WASMRefType ref_type;
  2723. bool need_ref_type_map;
  2724. #endif
  2725. #if WASM_ENABLE_GC == 0
  2726. CHECK_BUF(p, p_end, 1);
  2727. /* 0x70 or 0x6F */
  2728. table->elem_type = read_uint8(p);
  2729. if (VALUE_TYPE_FUNCREF != table->elem_type
  2730. #if WASM_ENABLE_REF_TYPES != 0
  2731. && VALUE_TYPE_EXTERNREF != table->elem_type
  2732. #endif
  2733. ) {
  2734. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2735. return false;
  2736. }
  2737. #else /* else of WASM_ENABLE_GC == 0 */
  2738. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  2739. false, error_buf, error_buf_size)) {
  2740. return false;
  2741. }
  2742. table->elem_type = ref_type.ref_type;
  2743. if (need_ref_type_map) {
  2744. if (!(table->elem_ref_type =
  2745. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2746. error_buf_size))) {
  2747. return false;
  2748. }
  2749. }
  2750. #if TRACE_WASM_LOADER != 0
  2751. os_printf("table type: ");
  2752. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2753. os_printf("\n");
  2754. #endif
  2755. #endif /* end of WASM_ENABLE_GC == 0 */
  2756. p_org = p;
  2757. read_leb_uint32(p, p_end, table->flags);
  2758. #if WASM_ENABLE_SHARED_MEMORY == 0
  2759. if (p - p_org > 1) {
  2760. set_error_buf(error_buf, error_buf_size,
  2761. "integer representation too long");
  2762. return false;
  2763. }
  2764. if (table->flags > 1) {
  2765. set_error_buf(error_buf, error_buf_size, "integer too large");
  2766. return false;
  2767. }
  2768. #else
  2769. if (p - p_org > 1) {
  2770. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2771. return false;
  2772. }
  2773. if (table->flags == 2) {
  2774. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2775. return false;
  2776. }
  2777. if (table->flags > 1) {
  2778. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2779. return false;
  2780. }
  2781. #endif
  2782. read_leb_uint32(p, p_end, table->init_size);
  2783. if (table->flags) {
  2784. read_leb_uint32(p, p_end, table->max_size);
  2785. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2786. error_buf_size))
  2787. return false;
  2788. }
  2789. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2790. #if WASM_ENABLE_WAMR_COMPILER != 0
  2791. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2792. module->is_ref_types_used = true;
  2793. #endif
  2794. *p_buf = p;
  2795. return true;
  2796. fail:
  2797. return false;
  2798. }
  2799. static bool
  2800. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2801. char *error_buf, uint32 error_buf_size)
  2802. {
  2803. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2804. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2805. uint32 pool_size = wasm_runtime_memory_pool_size();
  2806. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2807. / DEFAULT_NUM_BYTES_PER_PAGE;
  2808. #else
  2809. uint32 max_page_count = DEFAULT_MAX_PAGES;
  2810. #endif
  2811. p_org = p;
  2812. read_leb_uint32(p, p_end, memory->flags);
  2813. #if WASM_ENABLE_SHARED_MEMORY == 0
  2814. if (p - p_org > 1) {
  2815. set_error_buf(error_buf, error_buf_size,
  2816. "integer representation too long");
  2817. return false;
  2818. }
  2819. if (memory->flags > 1) {
  2820. if (memory->flags & 2) {
  2821. set_error_buf(error_buf, error_buf_size,
  2822. "shared memory flag was found, "
  2823. "please enable shared memory, lib-pthread "
  2824. "or lib-wasi-threads");
  2825. }
  2826. else {
  2827. set_error_buf(error_buf, error_buf_size, "invalid memory flags");
  2828. }
  2829. return false;
  2830. }
  2831. #else
  2832. if (p - p_org > 1) {
  2833. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2834. return false;
  2835. }
  2836. if (memory->flags > 3) {
  2837. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2838. return false;
  2839. }
  2840. else if (memory->flags == 2) {
  2841. set_error_buf(error_buf, error_buf_size,
  2842. "shared memory must have maximum");
  2843. return false;
  2844. }
  2845. #endif
  2846. read_leb_uint32(p, p_end, memory->init_page_count);
  2847. if (!check_memory_init_size(memory->init_page_count, error_buf,
  2848. error_buf_size))
  2849. return false;
  2850. if (memory->flags & 1) {
  2851. read_leb_uint32(p, p_end, memory->max_page_count);
  2852. if (!check_memory_max_size(memory->init_page_count,
  2853. memory->max_page_count, error_buf,
  2854. error_buf_size))
  2855. return false;
  2856. if (memory->max_page_count > max_page_count)
  2857. memory->max_page_count = max_page_count;
  2858. }
  2859. else {
  2860. /* Limit the maximum memory size to max_page_count */
  2861. memory->max_page_count = max_page_count;
  2862. }
  2863. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2864. *p_buf = p;
  2865. return true;
  2866. fail:
  2867. return false;
  2868. }
  2869. static bool
  2870. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2871. bool is_load_from_file_buf, char *error_buf,
  2872. uint32 error_buf_size)
  2873. {
  2874. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2875. uint32 import_count, name_len, type_index, i, u32, flags;
  2876. uint64 total_size;
  2877. WASMImport *import;
  2878. WASMImport *import_functions = NULL, *import_tables = NULL;
  2879. WASMImport *import_memories = NULL, *import_globals = NULL;
  2880. #if WASM_ENABLE_TAGS != 0
  2881. WASMImport *import_tags = NULL;
  2882. #endif
  2883. char *sub_module_name, *field_name;
  2884. uint8 u8, kind, global_type;
  2885. read_leb_uint32(p, p_end, import_count);
  2886. if (import_count) {
  2887. module->import_count = import_count;
  2888. total_size = sizeof(WASMImport) * (uint64)import_count;
  2889. if (!(module->imports =
  2890. loader_malloc(total_size, error_buf, error_buf_size))) {
  2891. return false;
  2892. }
  2893. p_old = p;
  2894. /* Scan firstly to get import count of each type */
  2895. for (i = 0; i < import_count; i++) {
  2896. /* module name */
  2897. read_leb_uint32(p, p_end, name_len);
  2898. CHECK_BUF(p, p_end, name_len);
  2899. p += name_len;
  2900. /* field name */
  2901. read_leb_uint32(p, p_end, name_len);
  2902. CHECK_BUF(p, p_end, name_len);
  2903. p += name_len;
  2904. CHECK_BUF(p, p_end, 1);
  2905. /* 0x00/0x01/0x02/0x03/0x04 */
  2906. kind = read_uint8(p);
  2907. switch (kind) {
  2908. case IMPORT_KIND_FUNC: /* import function */
  2909. read_leb_uint32(p, p_end, type_index);
  2910. module->import_function_count++;
  2911. break;
  2912. case IMPORT_KIND_TABLE: /* import table */
  2913. CHECK_BUF(p, p_end, 1);
  2914. /* 0x70 */
  2915. u8 = read_uint8(p);
  2916. read_leb_uint32(p, p_end, flags);
  2917. read_leb_uint32(p, p_end, u32);
  2918. if (flags & 1)
  2919. read_leb_uint32(p, p_end, u32);
  2920. module->import_table_count++;
  2921. if (module->import_table_count > 1) {
  2922. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2923. set_error_buf(error_buf, error_buf_size,
  2924. "multiple tables");
  2925. return false;
  2926. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2927. module->is_ref_types_used = true;
  2928. #endif
  2929. }
  2930. break;
  2931. case IMPORT_KIND_MEMORY: /* import memory */
  2932. read_leb_uint32(p, p_end, flags);
  2933. read_leb_uint32(p, p_end, u32);
  2934. if (flags & 1)
  2935. read_leb_uint32(p, p_end, u32);
  2936. module->import_memory_count++;
  2937. if (module->import_memory_count > 1) {
  2938. set_error_buf(error_buf, error_buf_size,
  2939. "multiple memories");
  2940. return false;
  2941. }
  2942. break;
  2943. #if WASM_ENABLE_TAGS != 0
  2944. case IMPORT_KIND_TAG: /* import tags */
  2945. /* it only counts the number of tags to import */
  2946. module->import_tag_count++;
  2947. CHECK_BUF(p, p_end, 1);
  2948. u8 = read_uint8(p);
  2949. read_leb_uint32(p, p_end, type_index);
  2950. break;
  2951. #endif
  2952. case IMPORT_KIND_GLOBAL: /* import global */
  2953. #if WASM_ENABLE_GC != 0
  2954. /* valtype */
  2955. CHECK_BUF(p, p_end, 1);
  2956. global_type = read_uint8(p);
  2957. if (wasm_is_type_multi_byte_type(global_type)) {
  2958. int32 heap_type;
  2959. read_leb_int32(p, p_end, heap_type);
  2960. (void)heap_type;
  2961. }
  2962. /* mutability */
  2963. CHECK_BUF(p, p_end, 1);
  2964. p += 1;
  2965. #else
  2966. CHECK_BUF(p, p_end, 2);
  2967. p += 2;
  2968. #endif
  2969. (void)global_type;
  2970. module->import_global_count++;
  2971. break;
  2972. default:
  2973. set_error_buf(error_buf, error_buf_size,
  2974. "invalid import kind");
  2975. return false;
  2976. }
  2977. }
  2978. if (module->import_function_count)
  2979. import_functions = module->import_functions = module->imports;
  2980. if (module->import_table_count)
  2981. import_tables = module->import_tables =
  2982. module->imports + module->import_function_count;
  2983. if (module->import_memory_count)
  2984. import_memories = module->import_memories =
  2985. module->imports + module->import_function_count
  2986. + module->import_table_count;
  2987. #if WASM_ENABLE_TAGS != 0
  2988. if (module->import_tag_count)
  2989. import_tags = module->import_tags =
  2990. module->imports + module->import_function_count
  2991. + module->import_table_count + module->import_memory_count;
  2992. if (module->import_global_count)
  2993. import_globals = module->import_globals =
  2994. module->imports + module->import_function_count
  2995. + module->import_table_count + module->import_memory_count
  2996. + module->import_tag_count;
  2997. #else
  2998. if (module->import_global_count)
  2999. import_globals = module->import_globals =
  3000. module->imports + module->import_function_count
  3001. + module->import_table_count + module->import_memory_count;
  3002. #endif
  3003. p = p_old;
  3004. /* Scan again to resolve the data */
  3005. for (i = 0; i < import_count; i++) {
  3006. /* load module name */
  3007. read_leb_uint32(p, p_end, name_len);
  3008. CHECK_BUF(p, p_end, name_len);
  3009. if (!(sub_module_name = const_str_list_insert(
  3010. p, name_len, module, is_load_from_file_buf, error_buf,
  3011. error_buf_size))) {
  3012. return false;
  3013. }
  3014. p += name_len;
  3015. /* load field name */
  3016. read_leb_uint32(p, p_end, name_len);
  3017. CHECK_BUF(p, p_end, name_len);
  3018. if (!(field_name = const_str_list_insert(
  3019. p, name_len, module, is_load_from_file_buf, error_buf,
  3020. error_buf_size))) {
  3021. return false;
  3022. }
  3023. p += name_len;
  3024. CHECK_BUF(p, p_end, 1);
  3025. /* 0x00/0x01/0x02/0x03/0x4 */
  3026. kind = read_uint8(p);
  3027. switch (kind) {
  3028. case IMPORT_KIND_FUNC: /* import function */
  3029. bh_assert(import_functions);
  3030. import = import_functions++;
  3031. if (!load_function_import(
  3032. &p, p_end, module, sub_module_name, field_name,
  3033. &import->u.function, error_buf, error_buf_size)) {
  3034. return false;
  3035. }
  3036. break;
  3037. case IMPORT_KIND_TABLE: /* import table */
  3038. bh_assert(import_tables);
  3039. import = import_tables++;
  3040. if (!load_table_import(&p, p_end, module, sub_module_name,
  3041. field_name, &import->u.table,
  3042. error_buf, error_buf_size)) {
  3043. LOG_DEBUG("can not import such a table (%s,%s)",
  3044. sub_module_name, field_name);
  3045. return false;
  3046. }
  3047. break;
  3048. case IMPORT_KIND_MEMORY: /* import memory */
  3049. bh_assert(import_memories);
  3050. import = import_memories++;
  3051. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3052. field_name, &import->u.memory,
  3053. error_buf, error_buf_size)) {
  3054. return false;
  3055. }
  3056. break;
  3057. #if WASM_ENABLE_TAGS != 0
  3058. case IMPORT_KIND_TAG:
  3059. bh_assert(import_tags);
  3060. import = import_tags++;
  3061. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3062. field_name, &import->u.tag, error_buf,
  3063. error_buf_size)) {
  3064. return false;
  3065. }
  3066. break;
  3067. #endif
  3068. case IMPORT_KIND_GLOBAL: /* import global */
  3069. bh_assert(import_globals);
  3070. import = import_globals++;
  3071. if (!load_global_import(&p, p_end, module, sub_module_name,
  3072. field_name, &import->u.global,
  3073. error_buf, error_buf_size)) {
  3074. return false;
  3075. }
  3076. break;
  3077. default:
  3078. set_error_buf(error_buf, error_buf_size,
  3079. "invalid import kind");
  3080. return false;
  3081. }
  3082. import->kind = kind;
  3083. import->u.names.module_name = sub_module_name;
  3084. import->u.names.field_name = field_name;
  3085. }
  3086. #if WASM_ENABLE_LIBC_WASI != 0
  3087. import = module->import_functions;
  3088. for (i = 0; i < module->import_function_count; i++, import++) {
  3089. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3090. || !strcmp(import->u.names.module_name,
  3091. "wasi_snapshot_preview1")) {
  3092. module->import_wasi_api = true;
  3093. break;
  3094. }
  3095. }
  3096. #endif
  3097. }
  3098. if (p != p_end) {
  3099. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3100. return false;
  3101. }
  3102. LOG_VERBOSE("Load import section success.\n");
  3103. (void)u8;
  3104. (void)u32;
  3105. (void)type_index;
  3106. return true;
  3107. fail:
  3108. return false;
  3109. }
  3110. static bool
  3111. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3112. uint32 error_buf_size)
  3113. {
  3114. WASMFuncType *param_type = func->func_type;
  3115. uint32 param_count = param_type->param_count;
  3116. uint8 *param_types = param_type->types;
  3117. uint32 local_count = func->local_count;
  3118. uint8 *local_types = func->local_types;
  3119. uint32 i, local_offset = 0;
  3120. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3121. /*
  3122. * Only allocate memory when total_size is not 0,
  3123. * or the return value of malloc(0) might be NULL on some platforms,
  3124. * which causes wasm loader return false.
  3125. */
  3126. if (total_size > 0
  3127. && !(func->local_offsets =
  3128. loader_malloc(total_size, error_buf, error_buf_size))) {
  3129. return false;
  3130. }
  3131. for (i = 0; i < param_count; i++) {
  3132. func->local_offsets[i] = (uint16)local_offset;
  3133. local_offset += wasm_value_type_cell_num(param_types[i]);
  3134. }
  3135. for (i = 0; i < local_count; i++) {
  3136. func->local_offsets[param_count + i] = (uint16)local_offset;
  3137. local_offset += wasm_value_type_cell_num(local_types[i]);
  3138. }
  3139. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3140. return true;
  3141. }
  3142. static bool
  3143. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3144. const uint8 *buf_code, const uint8 *buf_code_end,
  3145. WASMModule *module, char *error_buf,
  3146. uint32 error_buf_size)
  3147. {
  3148. const uint8 *p = buf, *p_end = buf_end;
  3149. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3150. uint32 func_count;
  3151. uint64 total_size;
  3152. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3153. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3154. uint8 type;
  3155. WASMFunction *func;
  3156. #if WASM_ENABLE_GC != 0
  3157. bool need_ref_type_map;
  3158. WASMRefType ref_type;
  3159. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3160. #endif
  3161. read_leb_uint32(p, p_end, func_count);
  3162. if (buf_code)
  3163. read_leb_uint32(p_code, buf_code_end, code_count);
  3164. if (func_count != code_count) {
  3165. set_error_buf(error_buf, error_buf_size,
  3166. "function and code section have inconsistent lengths or "
  3167. "unexpected end");
  3168. return false;
  3169. }
  3170. if (func_count) {
  3171. module->function_count = func_count;
  3172. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3173. if (!(module->functions =
  3174. loader_malloc(total_size, error_buf, error_buf_size))) {
  3175. return false;
  3176. }
  3177. for (i = 0; i < func_count; i++) {
  3178. /* Resolve function type */
  3179. read_leb_uint32(p, p_end, type_index);
  3180. if (type_index >= module->type_count) {
  3181. set_error_buf(error_buf, error_buf_size, "unknown type");
  3182. return false;
  3183. }
  3184. #if WASM_ENABLE_GC != 0
  3185. type_index_org = type_index;
  3186. #endif
  3187. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3188. type_index = wasm_get_smallest_type_idx(
  3189. module->types, module->type_count, type_index);
  3190. #endif
  3191. read_leb_uint32(p_code, buf_code_end, code_size);
  3192. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3193. set_error_buf(error_buf, error_buf_size,
  3194. "invalid function code size");
  3195. return false;
  3196. }
  3197. /* Resolve local set count */
  3198. p_code_end = p_code + code_size;
  3199. local_count = 0;
  3200. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3201. p_code_save = p_code;
  3202. #if WASM_ENABLE_GC != 0
  3203. ref_type_map_count = 0;
  3204. #endif
  3205. /* Calculate total local count */
  3206. for (j = 0; j < local_set_count; j++) {
  3207. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3208. if (sub_local_count > UINT32_MAX - local_count) {
  3209. set_error_buf(error_buf, error_buf_size, "too many locals");
  3210. return false;
  3211. }
  3212. #if WASM_ENABLE_GC == 0
  3213. CHECK_BUF(p_code, buf_code_end, 1);
  3214. /* 0x7F/0x7E/0x7D/0x7C */
  3215. type = read_uint8(p_code);
  3216. local_count += sub_local_count;
  3217. #if WASM_ENABLE_WAMR_COMPILER != 0
  3218. /* If any value's type is v128, mark the module as SIMD used */
  3219. if (type == VALUE_TYPE_V128)
  3220. module->is_simd_used = true;
  3221. #endif
  3222. #else
  3223. if (!resolve_value_type(&p_code, buf_code_end, module,
  3224. &need_ref_type_map, &ref_type, false,
  3225. error_buf, error_buf_size)) {
  3226. return false;
  3227. }
  3228. local_count += sub_local_count;
  3229. if (need_ref_type_map)
  3230. ref_type_map_count += sub_local_count;
  3231. #endif
  3232. }
  3233. /* Alloc memory, layout: function structure + local types */
  3234. code_size = (uint32)(p_code_end - p_code);
  3235. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3236. if (!(func = module->functions[i] =
  3237. loader_malloc(total_size, error_buf, error_buf_size))) {
  3238. return false;
  3239. }
  3240. #if WASM_ENABLE_GC != 0
  3241. if (ref_type_map_count > 0) {
  3242. total_size =
  3243. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3244. if (!(func->local_ref_type_maps = loader_malloc(
  3245. total_size, error_buf, error_buf_size))) {
  3246. return false;
  3247. }
  3248. func->local_ref_type_map_count = ref_type_map_count;
  3249. }
  3250. #endif
  3251. /* Set function type, local count, code size and code body */
  3252. func->func_type = (WASMFuncType *)module->types[type_index];
  3253. func->local_count = local_count;
  3254. if (local_count > 0)
  3255. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3256. func->code_size = code_size;
  3257. /*
  3258. * we shall make a copy of code body [p_code, p_code + code_size]
  3259. * when we are worrying about inappropriate releasing behaviour.
  3260. * all code bodies are actually in a buffer which user allocates in
  3261. * his embedding environment and we don't have power on them.
  3262. * it will be like:
  3263. * code_body_cp = malloc(code_size);
  3264. * memcpy(code_body_cp, p_code, code_size);
  3265. * func->code = code_body_cp;
  3266. */
  3267. func->code = (uint8 *)p_code;
  3268. #if WASM_ENABLE_GC != 0
  3269. func->type_idx = type_index_org;
  3270. #endif
  3271. #if WASM_ENABLE_GC != 0
  3272. t = 0;
  3273. #endif
  3274. /* Load each local type */
  3275. p_code = p_code_save;
  3276. local_type_index = 0;
  3277. for (j = 0; j < local_set_count; j++) {
  3278. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3279. /* Note: sub_local_count is allowed to be 0 */
  3280. if (local_type_index > UINT32_MAX - sub_local_count
  3281. || local_type_index + sub_local_count > local_count) {
  3282. set_error_buf(error_buf, error_buf_size,
  3283. "invalid local count");
  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. if (!is_value_type(type)) {
  3291. if (type == VALUE_TYPE_V128)
  3292. set_error_buf(error_buf, error_buf_size,
  3293. "v128 value type requires simd feature");
  3294. else if (type == VALUE_TYPE_FUNCREF
  3295. || type == VALUE_TYPE_EXTERNREF)
  3296. set_error_buf(error_buf, error_buf_size,
  3297. "ref value type requires "
  3298. "reference types feature");
  3299. else
  3300. set_error_buf_v(error_buf, error_buf_size,
  3301. "invalid local type 0x%02X", type);
  3302. return false;
  3303. }
  3304. #else
  3305. if (!resolve_value_type(&p_code, buf_code_end, module,
  3306. &need_ref_type_map, &ref_type, false,
  3307. error_buf, error_buf_size)) {
  3308. return false;
  3309. }
  3310. if (need_ref_type_map) {
  3311. WASMRefType *ref_type_tmp;
  3312. if (!(ref_type_tmp = reftype_set_insert(
  3313. module->ref_type_set, &ref_type, error_buf,
  3314. error_buf_size))) {
  3315. return false;
  3316. }
  3317. for (k = 0; k < sub_local_count; k++) {
  3318. func->local_ref_type_maps[t + k].ref_type =
  3319. ref_type_tmp;
  3320. func->local_ref_type_maps[t + k].index =
  3321. local_type_index + k;
  3322. }
  3323. t += sub_local_count;
  3324. }
  3325. type = ref_type.ref_type;
  3326. #endif
  3327. for (k = 0; k < sub_local_count; k++) {
  3328. func->local_types[local_type_index++] = type;
  3329. }
  3330. #if WASM_ENABLE_WAMR_COMPILER != 0
  3331. if (type == VALUE_TYPE_V128)
  3332. module->is_simd_used = true;
  3333. else if (type == VALUE_TYPE_FUNCREF
  3334. || type == VALUE_TYPE_EXTERNREF)
  3335. module->is_ref_types_used = true;
  3336. #endif
  3337. }
  3338. bh_assert(local_type_index == func->local_count);
  3339. #if WASM_ENABLE_GC != 0
  3340. bh_assert(t == func->local_ref_type_map_count);
  3341. #if TRACE_WASM_LOADER != 0
  3342. os_printf("func %u, local types: [", i);
  3343. k = 0;
  3344. for (j = 0; j < func->local_count; j++) {
  3345. WASMRefType *ref_type_tmp = NULL;
  3346. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3347. bh_assert(j == func->local_ref_type_maps[k].index);
  3348. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3349. }
  3350. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3351. if (j < func->local_count - 1)
  3352. os_printf(" ");
  3353. }
  3354. os_printf("]\n");
  3355. #endif
  3356. #endif
  3357. func->param_cell_num = func->func_type->param_cell_num;
  3358. func->ret_cell_num = func->func_type->ret_cell_num;
  3359. local_cell_num =
  3360. wasm_get_cell_num(func->local_types, func->local_count);
  3361. if (local_cell_num > UINT16_MAX) {
  3362. set_error_buf(error_buf, error_buf_size,
  3363. "local count too large");
  3364. return false;
  3365. }
  3366. func->local_cell_num = (uint16)local_cell_num;
  3367. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3368. return false;
  3369. p_code = p_code_end;
  3370. }
  3371. }
  3372. if (p != p_end) {
  3373. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3374. return false;
  3375. }
  3376. LOG_VERBOSE("Load function section success.\n");
  3377. return true;
  3378. fail:
  3379. return false;
  3380. }
  3381. static bool
  3382. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3383. char *error_buf, uint32 error_buf_size)
  3384. {
  3385. const uint8 *p = buf, *p_end = buf_end;
  3386. uint32 table_count, i;
  3387. uint64 total_size;
  3388. WASMTable *table;
  3389. read_leb_uint32(p, p_end, table_count);
  3390. if (module->import_table_count + table_count > 1) {
  3391. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3392. /* a total of one table is allowed */
  3393. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3394. return false;
  3395. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3396. module->is_ref_types_used = true;
  3397. #endif
  3398. }
  3399. if (table_count) {
  3400. module->table_count = table_count;
  3401. total_size = sizeof(WASMTable) * (uint64)table_count;
  3402. if (!(module->tables =
  3403. loader_malloc(total_size, error_buf, error_buf_size))) {
  3404. return false;
  3405. }
  3406. /* load each table */
  3407. table = module->tables;
  3408. for (i = 0; i < table_count; i++, table++) {
  3409. #if WASM_ENABLE_GC != 0
  3410. uint8 flag;
  3411. bool has_init = false;
  3412. CHECK_BUF(buf, buf_end, 1);
  3413. flag = read_uint8(p);
  3414. if (flag == TABLE_INIT_EXPR_FLAG) {
  3415. CHECK_BUF(buf, buf_end, 1);
  3416. flag = read_uint8(p);
  3417. if (flag != 0x00) {
  3418. set_error_buf(error_buf, error_buf_size,
  3419. "invalid leading bytes for table");
  3420. return false;
  3421. }
  3422. has_init = true;
  3423. }
  3424. else {
  3425. p--;
  3426. }
  3427. #endif /* end of WASM_ENABLE_GC != 0 */
  3428. if (!load_table(&p, p_end, module, table, error_buf,
  3429. error_buf_size))
  3430. return false;
  3431. #if WASM_ENABLE_GC != 0
  3432. if (has_init) {
  3433. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3434. table->elem_type, table->elem_ref_type,
  3435. error_buf, error_buf_size))
  3436. return false;
  3437. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3438. && table->init_expr.init_expr_type
  3439. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3440. set_error_buf(
  3441. error_buf, error_buf_size,
  3442. "unsupported initializer expression for table");
  3443. return false;
  3444. }
  3445. }
  3446. else {
  3447. if (wasm_is_reftype_htref_non_nullable(table->elem_type)) {
  3448. set_error_buf(
  3449. error_buf, error_buf_size,
  3450. "type mismatch: non-nullable table without init expr");
  3451. return false;
  3452. }
  3453. }
  3454. #endif /* end of WASM_ENABLE_GC != 0 */
  3455. #if WASM_ENABLE_WAMR_COMPILER != 0
  3456. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  3457. module->is_ref_types_used = true;
  3458. #endif
  3459. }
  3460. }
  3461. if (p != p_end) {
  3462. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3463. return false;
  3464. }
  3465. LOG_VERBOSE("Load table section success.\n");
  3466. return true;
  3467. fail:
  3468. return false;
  3469. }
  3470. static bool
  3471. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3472. char *error_buf, uint32 error_buf_size)
  3473. {
  3474. const uint8 *p = buf, *p_end = buf_end;
  3475. uint32 memory_count, i;
  3476. uint64 total_size;
  3477. WASMMemory *memory;
  3478. read_leb_uint32(p, p_end, memory_count);
  3479. /* a total of one memory is allowed */
  3480. if (module->import_memory_count + memory_count > 1) {
  3481. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3482. return false;
  3483. }
  3484. if (memory_count) {
  3485. module->memory_count = memory_count;
  3486. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3487. if (!(module->memories =
  3488. loader_malloc(total_size, error_buf, error_buf_size))) {
  3489. return false;
  3490. }
  3491. /* load each memory */
  3492. memory = module->memories;
  3493. for (i = 0; i < memory_count; i++, memory++)
  3494. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3495. return false;
  3496. }
  3497. if (p != p_end) {
  3498. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3499. return false;
  3500. }
  3501. LOG_VERBOSE("Load memory section success.\n");
  3502. return true;
  3503. fail:
  3504. return false;
  3505. }
  3506. static bool
  3507. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3508. char *error_buf, uint32 error_buf_size)
  3509. {
  3510. const uint8 *p = buf, *p_end = buf_end;
  3511. uint32 global_count, i;
  3512. uint64 total_size;
  3513. WASMGlobal *global;
  3514. uint8 mutable;
  3515. #if WASM_ENABLE_GC != 0
  3516. bool need_ref_type_map;
  3517. WASMRefType ref_type;
  3518. #endif
  3519. read_leb_uint32(p, p_end, global_count);
  3520. module->global_count = 0;
  3521. if (global_count) {
  3522. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3523. if (!(module->globals =
  3524. loader_malloc(total_size, error_buf, error_buf_size))) {
  3525. return false;
  3526. }
  3527. global = module->globals;
  3528. for (i = 0; i < global_count; i++, global++) {
  3529. #if WASM_ENABLE_GC == 0
  3530. CHECK_BUF(p, p_end, 2);
  3531. global->type = read_uint8(p);
  3532. mutable = read_uint8(p);
  3533. #else
  3534. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  3535. &ref_type, false, error_buf,
  3536. error_buf_size)) {
  3537. return false;
  3538. }
  3539. global->type = ref_type.ref_type;
  3540. CHECK_BUF(p, p_end, 1);
  3541. mutable = read_uint8(p);
  3542. #endif /* end of WASM_ENABLE_GC */
  3543. #if WASM_ENABLE_WAMR_COMPILER != 0
  3544. if (global->type == VALUE_TYPE_V128)
  3545. module->is_simd_used = true;
  3546. else if (global->type == VALUE_TYPE_FUNCREF
  3547. || global->type == VALUE_TYPE_EXTERNREF)
  3548. module->is_ref_types_used = true;
  3549. #endif
  3550. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3551. return false;
  3552. }
  3553. global->is_mutable = mutable ? true : false;
  3554. /* initialize expression */
  3555. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3556. global->type,
  3557. #if WASM_ENABLE_GC == 0
  3558. NULL,
  3559. #else
  3560. &ref_type,
  3561. #endif
  3562. error_buf, error_buf_size))
  3563. return false;
  3564. #if WASM_ENABLE_GC != 0
  3565. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3566. uint8 global_type;
  3567. WASMRefType *global_ref_type;
  3568. uint32 global_idx = global->init_expr.u.global_index;
  3569. if (global->init_expr.u.global_index
  3570. >= module->import_global_count + i) {
  3571. set_error_buf(error_buf, error_buf_size, "unknown global");
  3572. return false;
  3573. }
  3574. if (global_idx < module->import_global_count) {
  3575. global_type =
  3576. module->import_globals[global_idx].u.global.type;
  3577. global_ref_type =
  3578. module->import_globals[global_idx].u.global.ref_type;
  3579. }
  3580. else {
  3581. global_type =
  3582. module
  3583. ->globals[global_idx - module->import_global_count]
  3584. .type;
  3585. global_ref_type =
  3586. module
  3587. ->globals[global_idx - module->import_global_count]
  3588. .ref_type;
  3589. }
  3590. if (!wasm_reftype_is_subtype_of(
  3591. global_type, global_ref_type, global->type,
  3592. global->ref_type, module->types, module->type_count)) {
  3593. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3594. return false;
  3595. }
  3596. }
  3597. if (need_ref_type_map) {
  3598. if (!(global->ref_type =
  3599. reftype_set_insert(module->ref_type_set, &ref_type,
  3600. error_buf, error_buf_size))) {
  3601. return false;
  3602. }
  3603. }
  3604. #if TRACE_WASM_LOADER != 0
  3605. os_printf("global type: ");
  3606. wasm_dump_value_type(global->type, global->ref_type);
  3607. os_printf("\n");
  3608. #endif
  3609. #endif
  3610. module->global_count++;
  3611. }
  3612. bh_assert(module->global_count == global_count);
  3613. }
  3614. if (p != p_end) {
  3615. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3616. return false;
  3617. }
  3618. LOG_VERBOSE("Load global section success.\n");
  3619. return true;
  3620. fail:
  3621. return false;
  3622. }
  3623. static bool
  3624. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3625. bool is_load_from_file_buf, char *error_buf,
  3626. uint32 error_buf_size)
  3627. {
  3628. const uint8 *p = buf, *p_end = buf_end;
  3629. uint32 export_count, i, j, index;
  3630. uint64 total_size;
  3631. uint32 str_len;
  3632. WASMExport *export;
  3633. const char *name;
  3634. read_leb_uint32(p, p_end, export_count);
  3635. if (export_count) {
  3636. module->export_count = export_count;
  3637. total_size = sizeof(WASMExport) * (uint64)export_count;
  3638. if (!(module->exports =
  3639. loader_malloc(total_size, error_buf, error_buf_size))) {
  3640. return false;
  3641. }
  3642. export = module->exports;
  3643. for (i = 0; i < export_count; i++, export ++) {
  3644. #if WASM_ENABLE_THREAD_MGR == 0
  3645. if (p == p_end) {
  3646. /* export section with inconsistent count:
  3647. n export declared, but less than n given */
  3648. set_error_buf(error_buf, error_buf_size,
  3649. "length out of bounds");
  3650. return false;
  3651. }
  3652. #endif
  3653. read_leb_uint32(p, p_end, str_len);
  3654. CHECK_BUF(p, p_end, str_len);
  3655. for (j = 0; j < i; j++) {
  3656. name = module->exports[j].name;
  3657. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3658. set_error_buf(error_buf, error_buf_size,
  3659. "duplicate export name");
  3660. return false;
  3661. }
  3662. }
  3663. if (!(export->name = const_str_list_insert(
  3664. p, str_len, module, is_load_from_file_buf, error_buf,
  3665. error_buf_size))) {
  3666. return false;
  3667. }
  3668. p += str_len;
  3669. CHECK_BUF(p, p_end, 1);
  3670. export->kind = read_uint8(p);
  3671. read_leb_uint32(p, p_end, index);
  3672. export->index = index;
  3673. switch (export->kind) {
  3674. /* function index */
  3675. case EXPORT_KIND_FUNC:
  3676. if (index >= module->function_count
  3677. + module->import_function_count) {
  3678. set_error_buf(error_buf, error_buf_size,
  3679. "unknown function");
  3680. return false;
  3681. }
  3682. #if WASM_ENABLE_SIMD != 0
  3683. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3684. /* TODO: check func type, if it has v128 param or result,
  3685. report error */
  3686. #endif
  3687. #endif
  3688. break;
  3689. /* table index */
  3690. case EXPORT_KIND_TABLE:
  3691. if (index
  3692. >= module->table_count + module->import_table_count) {
  3693. set_error_buf(error_buf, error_buf_size,
  3694. "unknown table");
  3695. return false;
  3696. }
  3697. break;
  3698. /* memory index */
  3699. case EXPORT_KIND_MEMORY:
  3700. if (index
  3701. >= module->memory_count + module->import_memory_count) {
  3702. set_error_buf(error_buf, error_buf_size,
  3703. "unknown memory");
  3704. return false;
  3705. }
  3706. break;
  3707. #if WASM_ENABLE_TAGS != 0
  3708. /* export tag */
  3709. case EXPORT_KIND_TAG:
  3710. if (index >= module->tag_count + module->import_tag_count) {
  3711. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3712. return false;
  3713. }
  3714. break;
  3715. #endif
  3716. /* global index */
  3717. case EXPORT_KIND_GLOBAL:
  3718. if (index
  3719. >= module->global_count + module->import_global_count) {
  3720. set_error_buf(error_buf, error_buf_size,
  3721. "unknown global");
  3722. return false;
  3723. }
  3724. break;
  3725. default:
  3726. set_error_buf(error_buf, error_buf_size,
  3727. "invalid export kind");
  3728. return false;
  3729. }
  3730. }
  3731. }
  3732. if (p != p_end) {
  3733. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3734. return false;
  3735. }
  3736. LOG_VERBOSE("Load export section success.\n");
  3737. return true;
  3738. fail:
  3739. return false;
  3740. }
  3741. static bool
  3742. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3743. uint32 error_buf_size)
  3744. {
  3745. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3746. if (table_index != 0) {
  3747. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3748. return false;
  3749. }
  3750. #endif
  3751. if (table_index >= module->import_table_count + module->table_count) {
  3752. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3753. table_index);
  3754. return false;
  3755. }
  3756. return true;
  3757. }
  3758. static bool
  3759. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3760. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3761. {
  3762. const uint8 *p = *p_buf, *p_end = buf_end;
  3763. uint32 table_index;
  3764. read_leb_uint32(p, p_end, table_index);
  3765. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3766. return false;
  3767. }
  3768. *p_table_index = table_index;
  3769. *p_buf = p;
  3770. return true;
  3771. fail:
  3772. return false;
  3773. }
  3774. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3775. static bool
  3776. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3777. uint32 *p_elem_type,
  3778. #if WASM_ENABLE_GC != 0
  3779. WASMRefType **p_elem_ref_type,
  3780. #endif
  3781. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3782. {
  3783. const uint8 *p = *p_buf, *p_end = buf_end;
  3784. uint8 elem_type;
  3785. #if WASM_ENABLE_GC != 0
  3786. WASMRefType elem_ref_type;
  3787. bool need_ref_type_map;
  3788. #endif
  3789. CHECK_BUF(p, p_end, 1);
  3790. elem_type = read_uint8(p);
  3791. if (elemkind_zero) {
  3792. if (elem_type != 0) {
  3793. set_error_buf(error_buf, error_buf_size,
  3794. "invalid reference type or unknown type");
  3795. return false;
  3796. }
  3797. else {
  3798. *p_elem_type = VALUE_TYPE_FUNCREF;
  3799. *p_buf = p;
  3800. return true;
  3801. }
  3802. }
  3803. #if WASM_ENABLE_GC == 0
  3804. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3805. set_error_buf(error_buf, error_buf_size,
  3806. "invalid reference type or unknown type");
  3807. return false;
  3808. }
  3809. *p_elem_type = elem_type;
  3810. #else
  3811. p--;
  3812. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3813. &need_ref_type_map, &elem_ref_type, false,
  3814. error_buf, error_buf_size)) {
  3815. return false;
  3816. }
  3817. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3818. set_error_buf(error_buf, error_buf_size,
  3819. "invalid reference type or unknown type");
  3820. return false;
  3821. }
  3822. *p_elem_type = elem_ref_type.ref_type;
  3823. if (need_ref_type_map) {
  3824. if (!(*p_elem_ref_type =
  3825. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3826. error_buf, error_buf_size))) {
  3827. return false;
  3828. }
  3829. }
  3830. #endif
  3831. *p_buf = p;
  3832. return true;
  3833. fail:
  3834. return false;
  3835. }
  3836. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3837. static bool
  3838. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3839. WASMModule *module, WASMTableSeg *table_segment,
  3840. char *error_buf, uint32 error_buf_size)
  3841. {
  3842. const uint8 *p = *p_buf, *p_end = buf_end;
  3843. uint32 function_count, function_index = 0, i;
  3844. uint64 total_size;
  3845. read_leb_uint32(p, p_end, function_count);
  3846. table_segment->value_count = function_count;
  3847. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3848. if (total_size > 0
  3849. && !(table_segment->init_values =
  3850. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3851. error_buf_size))) {
  3852. return false;
  3853. }
  3854. for (i = 0; i < function_count; i++) {
  3855. InitializerExpression *init_expr = &table_segment->init_values[i];
  3856. read_leb_uint32(p, p_end, function_index);
  3857. if (!check_function_index(module, function_index, error_buf,
  3858. error_buf_size)) {
  3859. return false;
  3860. }
  3861. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3862. init_expr->u.ref_index = function_index;
  3863. }
  3864. *p_buf = p;
  3865. return true;
  3866. fail:
  3867. return false;
  3868. }
  3869. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3870. static bool
  3871. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3872. WASMModule *module, WASMTableSeg *table_segment,
  3873. char *error_buf, uint32 error_buf_size)
  3874. {
  3875. const uint8 *p = *p_buf, *p_end = buf_end;
  3876. uint32 ref_count, i;
  3877. uint64 total_size;
  3878. read_leb_uint32(p, p_end, ref_count);
  3879. table_segment->value_count = ref_count;
  3880. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3881. if (total_size > 0
  3882. && !(table_segment->init_values =
  3883. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3884. error_buf_size))) {
  3885. return false;
  3886. }
  3887. for (i = 0; i < ref_count; i++) {
  3888. InitializerExpression *init_expr = &table_segment->init_values[i];
  3889. if (!load_init_expr(module, &p, p_end, init_expr,
  3890. table_segment->elem_type,
  3891. #if WASM_ENABLE_GC == 0
  3892. NULL,
  3893. #else
  3894. table_segment->elem_ref_type,
  3895. #endif
  3896. error_buf, error_buf_size))
  3897. return false;
  3898. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3899. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3900. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3901. && init_expr->init_expr_type
  3902. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3903. }
  3904. *p_buf = p;
  3905. return true;
  3906. fail:
  3907. return false;
  3908. }
  3909. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3910. static bool
  3911. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3912. WASMModule *module, char *error_buf,
  3913. uint32 error_buf_size)
  3914. {
  3915. const uint8 *p = buf, *p_end = buf_end;
  3916. uint32 table_segment_count, i;
  3917. uint64 total_size;
  3918. WASMTableSeg *table_segment;
  3919. read_leb_uint32(p, p_end, table_segment_count);
  3920. if (table_segment_count) {
  3921. module->table_seg_count = table_segment_count;
  3922. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3923. if (!(module->table_segments =
  3924. loader_malloc(total_size, error_buf, error_buf_size))) {
  3925. return false;
  3926. }
  3927. table_segment = module->table_segments;
  3928. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3929. if (p >= p_end) {
  3930. set_error_buf(error_buf, error_buf_size,
  3931. "invalid value type or "
  3932. "invalid elements segment kind");
  3933. return false;
  3934. }
  3935. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3936. read_leb_uint32(p, p_end, table_segment->mode);
  3937. /* last three bits */
  3938. table_segment->mode = table_segment->mode & 0x07;
  3939. switch (table_segment->mode) {
  3940. /* elemkind/elemtype + active */
  3941. case 0:
  3942. case 4:
  3943. {
  3944. #if WASM_ENABLE_GC != 0
  3945. if (table_segment->mode == 0) {
  3946. /* vec(funcidx), set elem type to (ref func) */
  3947. WASMRefType elem_ref_type = { 0 };
  3948. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  3949. wasm_set_refheaptype_common(
  3950. &elem_ref_type.ref_ht_common, false,
  3951. HEAP_TYPE_FUNC);
  3952. if (!(table_segment->elem_ref_type = reftype_set_insert(
  3953. module->ref_type_set, &elem_ref_type,
  3954. error_buf, error_buf_size)))
  3955. return false;
  3956. }
  3957. else {
  3958. /* vec(expr), set elem type to funcref */
  3959. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3960. }
  3961. #else
  3962. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3963. #endif
  3964. table_segment->table_index = 0;
  3965. if (!check_table_index(module, table_segment->table_index,
  3966. error_buf, error_buf_size))
  3967. return false;
  3968. if (!load_init_expr(
  3969. module, &p, p_end, &table_segment->base_offset,
  3970. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3971. return false;
  3972. if (table_segment->mode == 0) {
  3973. /* vec(funcidx) */
  3974. if (!load_func_index_vec(&p, p_end, module,
  3975. table_segment, error_buf,
  3976. error_buf_size))
  3977. return false;
  3978. }
  3979. else {
  3980. /* vec(expr) */
  3981. if (!load_init_expr_vec(&p, p_end, module,
  3982. table_segment, error_buf,
  3983. error_buf_size))
  3984. return false;
  3985. }
  3986. break;
  3987. }
  3988. /* elemkind + passive/declarative */
  3989. case 1:
  3990. case 3:
  3991. if (!load_elem_type(module, &p, p_end,
  3992. &table_segment->elem_type,
  3993. #if WASM_ENABLE_GC != 0
  3994. &table_segment->elem_ref_type,
  3995. #endif
  3996. true, error_buf, error_buf_size))
  3997. return false;
  3998. /* vec(funcidx) */
  3999. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4000. error_buf, error_buf_size))
  4001. return false;
  4002. break;
  4003. /* elemkind/elemtype + table_idx + active */
  4004. case 2:
  4005. case 6:
  4006. if (!load_table_index(&p, p_end, module,
  4007. &table_segment->table_index,
  4008. error_buf, error_buf_size))
  4009. return false;
  4010. if (!load_init_expr(
  4011. module, &p, p_end, &table_segment->base_offset,
  4012. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4013. return false;
  4014. if (!load_elem_type(module, &p, p_end,
  4015. &table_segment->elem_type,
  4016. #if WASM_ENABLE_GC != 0
  4017. &table_segment->elem_ref_type,
  4018. #endif
  4019. table_segment->mode == 2 ? true : false,
  4020. error_buf, error_buf_size))
  4021. return false;
  4022. if (table_segment->mode == 2) {
  4023. /* vec(funcidx) */
  4024. if (!load_func_index_vec(&p, p_end, module,
  4025. table_segment, error_buf,
  4026. error_buf_size))
  4027. return false;
  4028. }
  4029. else {
  4030. /* vec(expr) */
  4031. if (!load_init_expr_vec(&p, p_end, module,
  4032. table_segment, error_buf,
  4033. error_buf_size))
  4034. return false;
  4035. }
  4036. break;
  4037. case 5:
  4038. case 7:
  4039. if (!load_elem_type(module, &p, p_end,
  4040. &table_segment->elem_type,
  4041. #if WASM_ENABLE_GC != 0
  4042. &table_segment->elem_ref_type,
  4043. #endif
  4044. false, error_buf, error_buf_size))
  4045. return false;
  4046. /* vec(expr) */
  4047. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4048. error_buf, error_buf_size))
  4049. return false;
  4050. break;
  4051. default:
  4052. set_error_buf(error_buf, error_buf_size,
  4053. "unknown element segment kind");
  4054. return false;
  4055. }
  4056. #else
  4057. /*
  4058. * like: 00 41 05 0b 04 00 01 00 01
  4059. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4060. */
  4061. if (!load_table_index(&p, p_end, module,
  4062. &table_segment->table_index, error_buf,
  4063. error_buf_size))
  4064. return false;
  4065. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4066. VALUE_TYPE_I32, NULL, error_buf,
  4067. error_buf_size))
  4068. return false;
  4069. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4070. error_buf, error_buf_size))
  4071. return false;
  4072. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  4073. #if WASM_ENABLE_WAMR_COMPILER != 0
  4074. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4075. module->is_ref_types_used = true;
  4076. #endif
  4077. }
  4078. }
  4079. if (p != p_end) {
  4080. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4081. return false;
  4082. }
  4083. LOG_VERBOSE("Load table segment section success.\n");
  4084. return true;
  4085. fail:
  4086. return false;
  4087. }
  4088. static bool
  4089. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4090. WASMModule *module, char *error_buf,
  4091. uint32 error_buf_size)
  4092. {
  4093. const uint8 *p = buf, *p_end = buf_end;
  4094. uint32 data_seg_count, i, mem_index, data_seg_len;
  4095. uint64 total_size;
  4096. WASMDataSeg *dataseg;
  4097. InitializerExpression init_expr;
  4098. #if WASM_ENABLE_BULK_MEMORY != 0
  4099. bool is_passive = false;
  4100. uint32 mem_flag;
  4101. #endif
  4102. read_leb_uint32(p, p_end, data_seg_count);
  4103. #if WASM_ENABLE_BULK_MEMORY != 0
  4104. if ((module->data_seg_count1 != 0)
  4105. && (data_seg_count != module->data_seg_count1)) {
  4106. set_error_buf(error_buf, error_buf_size,
  4107. "data count and data section have inconsistent lengths");
  4108. return false;
  4109. }
  4110. #endif
  4111. if (data_seg_count) {
  4112. module->data_seg_count = data_seg_count;
  4113. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4114. if (!(module->data_segments =
  4115. loader_malloc(total_size, error_buf, error_buf_size))) {
  4116. return false;
  4117. }
  4118. for (i = 0; i < data_seg_count; i++) {
  4119. read_leb_uint32(p, p_end, mem_index);
  4120. #if WASM_ENABLE_BULK_MEMORY != 0
  4121. is_passive = false;
  4122. mem_flag = mem_index & 0x03;
  4123. switch (mem_flag) {
  4124. case 0x01:
  4125. is_passive = true;
  4126. #if WASM_ENABLE_WAMR_COMPILER != 0
  4127. module->is_bulk_memory_used = true;
  4128. #endif
  4129. break;
  4130. case 0x00:
  4131. /* no memory index, treat index as 0 */
  4132. mem_index = 0;
  4133. goto check_mem_index;
  4134. case 0x02:
  4135. /* read following memory index */
  4136. read_leb_uint32(p, p_end, mem_index);
  4137. #if WASM_ENABLE_WAMR_COMPILER != 0
  4138. module->is_bulk_memory_used = true;
  4139. #endif
  4140. check_mem_index:
  4141. if (mem_index
  4142. >= module->import_memory_count + module->memory_count) {
  4143. set_error_buf_v(error_buf, error_buf_size,
  4144. "unknown memory %d", mem_index);
  4145. return false;
  4146. }
  4147. break;
  4148. case 0x03:
  4149. default:
  4150. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4151. return false;
  4152. break;
  4153. }
  4154. #else
  4155. if (mem_index
  4156. >= module->import_memory_count + module->memory_count) {
  4157. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4158. mem_index);
  4159. return false;
  4160. }
  4161. #endif /* WASM_ENABLE_BULK_MEMORY */
  4162. #if WASM_ENABLE_BULK_MEMORY != 0
  4163. if (!is_passive)
  4164. #endif
  4165. if (!load_init_expr(module, &p, p_end, &init_expr,
  4166. VALUE_TYPE_I32, NULL, error_buf,
  4167. error_buf_size))
  4168. return false;
  4169. read_leb_uint32(p, p_end, data_seg_len);
  4170. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4171. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4172. return false;
  4173. }
  4174. #if WASM_ENABLE_BULK_MEMORY != 0
  4175. dataseg->is_passive = is_passive;
  4176. if (!is_passive)
  4177. #endif
  4178. {
  4179. bh_memcpy_s(&dataseg->base_offset,
  4180. sizeof(InitializerExpression), &init_expr,
  4181. sizeof(InitializerExpression));
  4182. dataseg->memory_index = mem_index;
  4183. }
  4184. dataseg->data_length = data_seg_len;
  4185. CHECK_BUF(p, p_end, data_seg_len);
  4186. dataseg->data = (uint8 *)p;
  4187. p += data_seg_len;
  4188. }
  4189. }
  4190. if (p != p_end) {
  4191. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4192. return false;
  4193. }
  4194. LOG_VERBOSE("Load data segment section success.\n");
  4195. return true;
  4196. fail:
  4197. return false;
  4198. }
  4199. #if WASM_ENABLE_BULK_MEMORY != 0
  4200. static bool
  4201. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4202. WASMModule *module, char *error_buf,
  4203. uint32 error_buf_size)
  4204. {
  4205. const uint8 *p = buf, *p_end = buf_end;
  4206. uint32 data_seg_count1 = 0;
  4207. read_leb_uint32(p, p_end, data_seg_count1);
  4208. module->data_seg_count1 = data_seg_count1;
  4209. if (p != p_end) {
  4210. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4211. return false;
  4212. }
  4213. #if WASM_ENABLE_WAMR_COMPILER != 0
  4214. module->is_bulk_memory_used = true;
  4215. #endif
  4216. LOG_VERBOSE("Load datacount section success.\n");
  4217. return true;
  4218. fail:
  4219. return false;
  4220. }
  4221. #endif
  4222. #if WASM_ENABLE_TAGS != 0
  4223. static bool
  4224. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4225. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4226. uint32 error_buf_size)
  4227. {
  4228. (void)buf_code;
  4229. (void)buf_code_end;
  4230. const uint8 *p = buf, *p_end = buf_end;
  4231. size_t total_size = 0;
  4232. /* number of tags defined in the section */
  4233. uint32 section_tag_count = 0;
  4234. uint8 tag_attribute;
  4235. uint32 tag_type;
  4236. WASMTag *tag = NULL;
  4237. /* get tag count */
  4238. read_leb_uint32(p, p_end, section_tag_count);
  4239. module->tag_count = section_tag_count;
  4240. if (section_tag_count) {
  4241. total_size = sizeof(WASMTag *) * module->tag_count;
  4242. if (!(module->tags =
  4243. loader_malloc(total_size, error_buf, error_buf_size))) {
  4244. return false;
  4245. }
  4246. /* load each tag, imported tags precede the tags */
  4247. uint32 tag_index;
  4248. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4249. /* get the one byte attribute */
  4250. CHECK_BUF(p, p_end, 1);
  4251. tag_attribute = read_uint8(p);
  4252. /* get type */
  4253. read_leb_uint32(p, p_end, tag_type);
  4254. /* compare against module->types */
  4255. if (tag_type >= module->type_count) {
  4256. set_error_buf(error_buf, error_buf_size, "unknown type");
  4257. return false;
  4258. }
  4259. /* get return type (must be 0) */
  4260. /* check, that the type of the referred tag returns void */
  4261. WASMType *func_type = (WASMType *)module->types[tag_type];
  4262. if (func_type->result_count != 0) {
  4263. set_error_buf(error_buf, error_buf_size,
  4264. "non-empty tag result type");
  4265. goto fail;
  4266. }
  4267. if (!(tag = module->tags[tag_index] = loader_malloc(
  4268. sizeof(WASMTag), error_buf, error_buf_size))) {
  4269. return false;
  4270. }
  4271. /* store to module tag declarations */
  4272. tag->attribute = tag_attribute;
  4273. tag->type = tag_type;
  4274. tag->tag_type = func_type;
  4275. }
  4276. }
  4277. if (p != p_end) {
  4278. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4279. return false;
  4280. }
  4281. LOG_VERBOSE("Load tag section success.\n");
  4282. return true;
  4283. fail:
  4284. return false;
  4285. }
  4286. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4287. static bool
  4288. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4289. const uint8 *buf_func_end, WASMModule *module,
  4290. char *error_buf, uint32 error_buf_size)
  4291. {
  4292. const uint8 *p = buf, *p_end = buf_end;
  4293. const uint8 *p_func = buf_func;
  4294. uint32 func_count = 0, code_count;
  4295. /* code has been loaded in function section, so pass it here, just check
  4296. * whether function and code section have inconsistent lengths */
  4297. read_leb_uint32(p, p_end, code_count);
  4298. if (buf_func)
  4299. read_leb_uint32(p_func, buf_func_end, func_count);
  4300. if (func_count != code_count) {
  4301. set_error_buf(error_buf, error_buf_size,
  4302. "function and code section have inconsistent lengths");
  4303. return false;
  4304. }
  4305. LOG_VERBOSE("Load code segment section success.\n");
  4306. (void)module;
  4307. return true;
  4308. fail:
  4309. return false;
  4310. }
  4311. static bool
  4312. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4313. char *error_buf, uint32 error_buf_size)
  4314. {
  4315. const uint8 *p = buf, *p_end = buf_end;
  4316. WASMFuncType *type;
  4317. uint32 start_function;
  4318. read_leb_uint32(p, p_end, start_function);
  4319. if (start_function
  4320. >= module->function_count + module->import_function_count) {
  4321. set_error_buf(error_buf, error_buf_size, "unknown function");
  4322. return false;
  4323. }
  4324. if (start_function < module->import_function_count)
  4325. type = module->import_functions[start_function].u.function.func_type;
  4326. else
  4327. type = module->functions[start_function - module->import_function_count]
  4328. ->func_type;
  4329. if (type->param_count != 0 || type->result_count != 0) {
  4330. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4331. return false;
  4332. }
  4333. module->start_function = start_function;
  4334. if (p != p_end) {
  4335. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4336. return false;
  4337. }
  4338. LOG_VERBOSE("Load start section success.\n");
  4339. return true;
  4340. fail:
  4341. return false;
  4342. }
  4343. #if WASM_ENABLE_STRINGREF != 0
  4344. static bool
  4345. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4346. WASMModule *module, bool is_load_from_file_buf,
  4347. char *error_buf, uint32 error_buf_size)
  4348. {
  4349. const uint8 *p = buf, *p_end = buf_end;
  4350. int32 deferred_count, immediate_count, string_length, i;
  4351. uint64 total_size;
  4352. read_leb_uint32(p, p_end, deferred_count);
  4353. read_leb_uint32(p, p_end, immediate_count);
  4354. /* proposal set deferred_count for future extension */
  4355. if (deferred_count != 0) {
  4356. goto fail;
  4357. }
  4358. if (immediate_count > 0) {
  4359. total_size = sizeof(char *) * (uint64)immediate_count;
  4360. if (!(module->string_literal_ptrs =
  4361. loader_malloc(total_size, error_buf, error_buf_size))) {
  4362. goto fail;
  4363. }
  4364. module->string_literal_count = immediate_count;
  4365. total_size = sizeof(uint32) * (uint64)immediate_count;
  4366. if (!(module->string_literal_lengths =
  4367. loader_malloc(total_size, error_buf, error_buf_size))) {
  4368. goto fail;
  4369. }
  4370. for (i = 0; i < immediate_count; i++) {
  4371. read_leb_uint32(p, p_end, string_length);
  4372. CHECK_BUF(p, p_end, string_length);
  4373. module->string_literal_ptrs[i] = p;
  4374. module->string_literal_lengths[i] = string_length;
  4375. p += string_length;
  4376. }
  4377. }
  4378. if (p != p_end) {
  4379. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4380. goto fail;
  4381. }
  4382. LOG_VERBOSE("Load stringref section success.\n");
  4383. return true;
  4384. fail:
  4385. return false;
  4386. }
  4387. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4388. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4389. static bool
  4390. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4391. bool is_load_from_file_buf, char *error_buf,
  4392. uint32 error_buf_size)
  4393. {
  4394. const uint8 *p = buf, *p_end = buf_end;
  4395. uint32 name_type, subsection_size;
  4396. uint32 previous_name_type = 0;
  4397. uint32 num_func_name;
  4398. uint32 func_index;
  4399. uint32 previous_func_index = ~0U;
  4400. uint32 func_name_len;
  4401. uint32 name_index;
  4402. int i = 0;
  4403. if (p >= p_end) {
  4404. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4405. return false;
  4406. }
  4407. while (p < p_end) {
  4408. read_leb_uint32(p, p_end, name_type);
  4409. if (i != 0) {
  4410. if (name_type == previous_name_type) {
  4411. set_error_buf(error_buf, error_buf_size,
  4412. "duplicate sub-section");
  4413. return false;
  4414. }
  4415. if (name_type < previous_name_type) {
  4416. set_error_buf(error_buf, error_buf_size,
  4417. "out-of-order sub-section");
  4418. return false;
  4419. }
  4420. }
  4421. previous_name_type = name_type;
  4422. read_leb_uint32(p, p_end, subsection_size);
  4423. CHECK_BUF(p, p_end, subsection_size);
  4424. switch (name_type) {
  4425. case SUB_SECTION_TYPE_FUNC:
  4426. if (subsection_size) {
  4427. read_leb_uint32(p, p_end, num_func_name);
  4428. for (name_index = 0; name_index < num_func_name;
  4429. name_index++) {
  4430. read_leb_uint32(p, p_end, func_index);
  4431. if (func_index == previous_func_index) {
  4432. set_error_buf(error_buf, error_buf_size,
  4433. "duplicate function name");
  4434. return false;
  4435. }
  4436. if (func_index < previous_func_index
  4437. && previous_func_index != ~0U) {
  4438. set_error_buf(error_buf, error_buf_size,
  4439. "out-of-order function index ");
  4440. return false;
  4441. }
  4442. previous_func_index = func_index;
  4443. read_leb_uint32(p, p_end, func_name_len);
  4444. CHECK_BUF(p, p_end, func_name_len);
  4445. /* Skip the import functions */
  4446. if (func_index >= module->import_function_count) {
  4447. func_index -= module->import_function_count;
  4448. if (func_index >= module->function_count) {
  4449. set_error_buf(error_buf, error_buf_size,
  4450. "out-of-range function index");
  4451. return false;
  4452. }
  4453. if (!(module->functions[func_index]->field_name =
  4454. const_str_list_insert(
  4455. p, func_name_len, module,
  4456. #if WASM_ENABLE_WAMR_COMPILER != 0
  4457. false,
  4458. #else
  4459. is_load_from_file_buf,
  4460. #endif
  4461. error_buf, error_buf_size))) {
  4462. return false;
  4463. }
  4464. }
  4465. p += func_name_len;
  4466. }
  4467. }
  4468. break;
  4469. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4470. */
  4471. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4472. default:
  4473. p = p + subsection_size;
  4474. break;
  4475. }
  4476. i++;
  4477. }
  4478. return true;
  4479. fail:
  4480. return false;
  4481. }
  4482. #endif
  4483. static bool
  4484. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4485. bool is_load_from_file_buf, char *error_buf,
  4486. uint32 error_buf_size)
  4487. {
  4488. const uint8 *p = buf, *p_end = buf_end;
  4489. char section_name[32];
  4490. uint32 name_len, buffer_len;
  4491. if (p >= p_end) {
  4492. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4493. return false;
  4494. }
  4495. read_leb_uint32(p, p_end, name_len);
  4496. if (p + name_len > p_end) {
  4497. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4498. return false;
  4499. }
  4500. if (!check_utf8_str(p, name_len)) {
  4501. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4502. return false;
  4503. }
  4504. buffer_len = sizeof(section_name);
  4505. memset(section_name, 0, buffer_len);
  4506. if (name_len < buffer_len) {
  4507. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4508. }
  4509. else {
  4510. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4511. memset(section_name + buffer_len - 4, '.', 3);
  4512. }
  4513. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4514. if (memcmp(p, "name", 4) == 0) {
  4515. module->name_section_buf = buf;
  4516. module->name_section_buf_end = buf_end;
  4517. p += name_len;
  4518. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  4519. error_buf_size);
  4520. LOG_VERBOSE("Load custom name section success.");
  4521. return true;
  4522. }
  4523. #endif
  4524. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4525. {
  4526. WASMCustomSection *section =
  4527. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4528. if (!section) {
  4529. return false;
  4530. }
  4531. section->name_addr = (char *)p;
  4532. section->name_len = name_len;
  4533. section->content_addr = (uint8 *)(p + name_len);
  4534. section->content_len = (uint32)(p_end - p - name_len);
  4535. section->next = module->custom_section_list;
  4536. module->custom_section_list = section;
  4537. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4538. return true;
  4539. }
  4540. #endif
  4541. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4542. (void)is_load_from_file_buf;
  4543. (void)module;
  4544. return true;
  4545. fail:
  4546. return false;
  4547. }
  4548. static void
  4549. calculate_global_data_offset(WASMModule *module)
  4550. {
  4551. uint32 i, data_offset;
  4552. data_offset = 0;
  4553. for (i = 0; i < module->import_global_count; i++) {
  4554. WASMGlobalImport *import_global =
  4555. &((module->import_globals + i)->u.global);
  4556. #if WASM_ENABLE_FAST_JIT != 0
  4557. import_global->data_offset = data_offset;
  4558. #endif
  4559. data_offset += wasm_value_type_size(import_global->type);
  4560. }
  4561. for (i = 0; i < module->global_count; i++) {
  4562. WASMGlobal *global = module->globals + i;
  4563. #if WASM_ENABLE_FAST_JIT != 0
  4564. global->data_offset = data_offset;
  4565. #endif
  4566. data_offset += wasm_value_type_size(global->type);
  4567. }
  4568. module->global_data_size = data_offset;
  4569. }
  4570. #if WASM_ENABLE_FAST_JIT != 0
  4571. static bool
  4572. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4573. uint32 error_buf_size)
  4574. {
  4575. #if WASM_ENABLE_LAZY_JIT != 0
  4576. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4577. #endif
  4578. uint32 i;
  4579. if (!module->function_count)
  4580. return true;
  4581. if (!(module->fast_jit_func_ptrs =
  4582. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4583. error_buf_size))) {
  4584. return false;
  4585. }
  4586. #if WASM_ENABLE_LAZY_JIT != 0
  4587. for (i = 0; i < module->function_count; i++) {
  4588. module->fast_jit_func_ptrs[i] =
  4589. jit_globals->compile_fast_jit_and_then_call;
  4590. }
  4591. #endif
  4592. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4593. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4594. set_error_buf(error_buf, error_buf_size,
  4595. "init fast jit thread lock failed");
  4596. return false;
  4597. }
  4598. module->fast_jit_thread_locks_inited[i] = true;
  4599. }
  4600. return true;
  4601. }
  4602. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4603. #if WASM_ENABLE_JIT != 0
  4604. static bool
  4605. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4606. uint32 error_buf_size)
  4607. {
  4608. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4609. AOTCompOption option = { 0 };
  4610. char *aot_last_error;
  4611. uint64 size;
  4612. #if WASM_ENABLE_GC != 0
  4613. bool gc_enabled = true;
  4614. #else
  4615. bool gc_enabled = false;
  4616. #endif
  4617. if (module->function_count == 0)
  4618. return true;
  4619. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4620. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4621. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4622. return false;
  4623. }
  4624. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4625. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4626. os_mutex_destroy(&module->tierup_wait_lock);
  4627. return false;
  4628. }
  4629. module->tierup_wait_lock_inited = true;
  4630. #endif
  4631. size = sizeof(void *) * (uint64)module->function_count
  4632. + sizeof(bool) * (uint64)module->function_count;
  4633. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4634. return false;
  4635. }
  4636. module->func_ptrs_compiled =
  4637. (bool *)((uint8 *)module->func_ptrs
  4638. + sizeof(void *) * module->function_count);
  4639. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4640. if (!module->comp_data) {
  4641. aot_last_error = aot_get_last_error();
  4642. bh_assert(aot_last_error != NULL);
  4643. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4644. return false;
  4645. }
  4646. option.is_jit_mode = true;
  4647. option.opt_level = llvm_jit_options->opt_level;
  4648. option.size_level = llvm_jit_options->size_level;
  4649. option.segue_flags = llvm_jit_options->segue_flags;
  4650. option.quick_invoke_c_api_import =
  4651. llvm_jit_options->quick_invoke_c_api_import;
  4652. #if WASM_ENABLE_BULK_MEMORY != 0
  4653. option.enable_bulk_memory = true;
  4654. #endif
  4655. #if WASM_ENABLE_THREAD_MGR != 0
  4656. option.enable_thread_mgr = true;
  4657. #endif
  4658. #if WASM_ENABLE_TAIL_CALL != 0
  4659. option.enable_tail_call = true;
  4660. #endif
  4661. #if WASM_ENABLE_SIMD != 0
  4662. option.enable_simd = true;
  4663. #endif
  4664. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4665. option.enable_ref_types = true;
  4666. #elif WASM_ENABLE_GC != 0
  4667. option.enable_gc = true;
  4668. #endif
  4669. option.enable_aux_stack_check = true;
  4670. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4671. || WASM_ENABLE_JIT_STACK_FRAME != 0
  4672. option.enable_aux_stack_frame = true;
  4673. #endif
  4674. #if WASM_ENABLE_PERF_PROFILING != 0
  4675. option.enable_perf_profiling = true;
  4676. #endif
  4677. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4678. option.enable_memory_profiling = true;
  4679. option.enable_stack_estimation = true;
  4680. #endif
  4681. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4682. if (!module->comp_ctx) {
  4683. aot_last_error = aot_get_last_error();
  4684. bh_assert(aot_last_error != NULL);
  4685. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4686. return false;
  4687. }
  4688. return true;
  4689. }
  4690. static bool
  4691. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4692. uint32 error_buf_size)
  4693. {
  4694. char *aot_last_error;
  4695. uint32 i;
  4696. if (module->function_count == 0)
  4697. return true;
  4698. if (!aot_compile_wasm(module->comp_ctx)) {
  4699. aot_last_error = aot_get_last_error();
  4700. bh_assert(aot_last_error != NULL);
  4701. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4702. return false;
  4703. }
  4704. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4705. if (module->orcjit_stop_compiling)
  4706. return false;
  4707. #endif
  4708. bh_print_time("Begin to lookup llvm jit functions");
  4709. for (i = 0; i < module->function_count; i++) {
  4710. LLVMOrcJITTargetAddress func_addr = 0;
  4711. LLVMErrorRef error;
  4712. char func_name[48];
  4713. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4714. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4715. func_name);
  4716. if (error != LLVMErrorSuccess) {
  4717. char *err_msg = LLVMGetErrorMessage(error);
  4718. set_error_buf_v(error_buf, error_buf_size,
  4719. "failed to compile llvm jit function: %s", err_msg);
  4720. LLVMDisposeErrorMessage(err_msg);
  4721. return false;
  4722. }
  4723. /**
  4724. * No need to lock the func_ptr[func_idx] here as it is basic
  4725. * data type, the load/store for it can be finished by one cpu
  4726. * instruction, and there can be only one cpu instruction
  4727. * loading/storing at the same time.
  4728. */
  4729. module->func_ptrs[i] = (void *)func_addr;
  4730. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4731. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4732. if (module->orcjit_stop_compiling)
  4733. return false;
  4734. #endif
  4735. }
  4736. bh_print_time("End lookup llvm jit functions");
  4737. return true;
  4738. }
  4739. #endif /* end of WASM_ENABLE_JIT != 0 */
  4740. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4741. && WASM_ENABLE_LAZY_JIT != 0
  4742. static void *
  4743. init_llvm_jit_functions_stage2_callback(void *arg)
  4744. {
  4745. WASMModule *module = (WASMModule *)arg;
  4746. char error_buf[128];
  4747. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4748. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4749. module->orcjit_stop_compiling = true;
  4750. return NULL;
  4751. }
  4752. os_mutex_lock(&module->tierup_wait_lock);
  4753. module->llvm_jit_inited = true;
  4754. os_cond_broadcast(&module->tierup_wait_cond);
  4755. os_mutex_unlock(&module->tierup_wait_lock);
  4756. return NULL;
  4757. }
  4758. #endif
  4759. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4760. /* The callback function to compile jit functions */
  4761. static void *
  4762. orcjit_thread_callback(void *arg)
  4763. {
  4764. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4765. #if WASM_ENABLE_JIT != 0
  4766. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4767. #endif
  4768. WASMModule *module = thread_arg->module;
  4769. uint32 group_idx = thread_arg->group_idx;
  4770. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4771. uint32 func_count = module->function_count;
  4772. uint32 i;
  4773. #if WASM_ENABLE_FAST_JIT != 0
  4774. /* Compile fast jit funcitons of this group */
  4775. for (i = group_idx; i < func_count; i += group_stride) {
  4776. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4777. os_printf("failed to compile fast jit function %u\n", i);
  4778. break;
  4779. }
  4780. if (module->orcjit_stop_compiling) {
  4781. return NULL;
  4782. }
  4783. }
  4784. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4785. os_mutex_lock(&module->tierup_wait_lock);
  4786. module->fast_jit_ready_groups++;
  4787. os_mutex_unlock(&module->tierup_wait_lock);
  4788. #endif
  4789. #endif
  4790. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4791. && WASM_ENABLE_LAZY_JIT != 0
  4792. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4793. for (i = group_idx; i < func_count;
  4794. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4795. uint32 j;
  4796. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4797. if (i + j * group_stride < func_count) {
  4798. if (!jit_compiler_set_call_to_fast_jit(
  4799. module,
  4800. i + j * group_stride + module->import_function_count)) {
  4801. os_printf(
  4802. "failed to compile call_to_fast_jit for func %u\n",
  4803. i + j * group_stride + module->import_function_count);
  4804. module->orcjit_stop_compiling = true;
  4805. return NULL;
  4806. }
  4807. }
  4808. if (module->orcjit_stop_compiling) {
  4809. return NULL;
  4810. }
  4811. }
  4812. }
  4813. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4814. fast jit functions are compiled */
  4815. os_mutex_lock(&module->tierup_wait_lock);
  4816. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4817. && module->fast_jit_ready_groups >= group_stride)) {
  4818. os_cond_reltimedwait(&module->tierup_wait_cond,
  4819. &module->tierup_wait_lock, 10000);
  4820. if (module->orcjit_stop_compiling) {
  4821. /* init_llvm_jit_functions_stage2 failed */
  4822. os_mutex_unlock(&module->tierup_wait_lock);
  4823. return NULL;
  4824. }
  4825. }
  4826. os_mutex_unlock(&module->tierup_wait_lock);
  4827. #endif
  4828. #if WASM_ENABLE_JIT != 0
  4829. /* Compile llvm jit functions of this group */
  4830. for (i = group_idx; i < func_count;
  4831. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4832. LLVMOrcJITTargetAddress func_addr = 0;
  4833. LLVMErrorRef error;
  4834. char func_name[48];
  4835. typedef void (*F)(void);
  4836. union {
  4837. F f;
  4838. void *v;
  4839. } u;
  4840. uint32 j;
  4841. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4842. "_wrapper");
  4843. LOG_DEBUG("compile llvm jit func %s", func_name);
  4844. error =
  4845. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4846. if (error != LLVMErrorSuccess) {
  4847. char *err_msg = LLVMGetErrorMessage(error);
  4848. os_printf("failed to compile llvm jit function %u: %s", i, err_msg);
  4849. LLVMDisposeErrorMessage(err_msg);
  4850. break;
  4851. }
  4852. /* Call the jit wrapper function to trigger its compilation, so as
  4853. to compile the actual jit functions, since we add the latter to
  4854. function list in the PartitionFunction callback */
  4855. u.v = (void *)func_addr;
  4856. u.f();
  4857. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4858. if (i + j * group_stride < func_count) {
  4859. module->func_ptrs_compiled[i + j * group_stride] = true;
  4860. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4861. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  4862. i + j * group_stride);
  4863. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  4864. func_name);
  4865. if (error != LLVMErrorSuccess) {
  4866. char *err_msg = LLVMGetErrorMessage(error);
  4867. os_printf("failed to compile llvm jit function %u: %s", i,
  4868. err_msg);
  4869. LLVMDisposeErrorMessage(err_msg);
  4870. /* Ignore current llvm jit func, as its func ptr is
  4871. previous set to call_to_fast_jit, which also works */
  4872. continue;
  4873. }
  4874. jit_compiler_set_llvm_jit_func_ptr(
  4875. module,
  4876. i + j * group_stride + module->import_function_count,
  4877. (void *)func_addr);
  4878. /* Try to switch to call this llvm jit funtion instead of
  4879. fast jit function from fast jit jitted code */
  4880. jit_compiler_set_call_to_llvm_jit(
  4881. module,
  4882. i + j * group_stride + module->import_function_count);
  4883. #endif
  4884. }
  4885. }
  4886. if (module->orcjit_stop_compiling) {
  4887. break;
  4888. }
  4889. }
  4890. #endif
  4891. return NULL;
  4892. }
  4893. static void
  4894. orcjit_stop_compile_threads(WASMModule *module)
  4895. {
  4896. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  4897. / sizeof(OrcJitThreadArg));
  4898. module->orcjit_stop_compiling = true;
  4899. for (i = 0; i < thread_num; i++) {
  4900. if (module->orcjit_threads[i])
  4901. os_thread_join(module->orcjit_threads[i], NULL);
  4902. }
  4903. }
  4904. static bool
  4905. compile_jit_functions(WASMModule *module, char *error_buf,
  4906. uint32 error_buf_size)
  4907. {
  4908. uint32 thread_num =
  4909. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  4910. uint32 i, j;
  4911. bh_print_time("Begin to compile jit functions");
  4912. /* Create threads to compile the jit functions */
  4913. for (i = 0; i < thread_num && i < module->function_count; i++) {
  4914. #if WASM_ENABLE_JIT != 0
  4915. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  4916. #endif
  4917. module->orcjit_thread_args[i].module = module;
  4918. module->orcjit_thread_args[i].group_idx = i;
  4919. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  4920. (void *)&module->orcjit_thread_args[i],
  4921. APP_THREAD_STACK_SIZE_DEFAULT)
  4922. != 0) {
  4923. set_error_buf(error_buf, error_buf_size,
  4924. "create orcjit compile thread failed");
  4925. /* Terminate the threads created */
  4926. module->orcjit_stop_compiling = true;
  4927. for (j = 0; j < i; j++) {
  4928. os_thread_join(module->orcjit_threads[j], NULL);
  4929. }
  4930. return false;
  4931. }
  4932. }
  4933. #if WASM_ENABLE_LAZY_JIT == 0
  4934. /* Wait until all jit functions are compiled for eager mode */
  4935. for (i = 0; i < thread_num; i++) {
  4936. if (module->orcjit_threads[i])
  4937. os_thread_join(module->orcjit_threads[i], NULL);
  4938. }
  4939. #if WASM_ENABLE_FAST_JIT != 0
  4940. /* Ensure all the fast-jit functions are compiled */
  4941. for (i = 0; i < module->function_count; i++) {
  4942. if (!jit_compiler_is_compiled(module,
  4943. i + module->import_function_count)) {
  4944. set_error_buf(error_buf, error_buf_size,
  4945. "failed to compile fast jit function");
  4946. return false;
  4947. }
  4948. }
  4949. #endif
  4950. #if WASM_ENABLE_JIT != 0
  4951. /* Ensure all the llvm-jit functions are compiled */
  4952. for (i = 0; i < module->function_count; i++) {
  4953. if (!module->func_ptrs_compiled[i]) {
  4954. set_error_buf(error_buf, error_buf_size,
  4955. "failed to compile llvm jit function");
  4956. return false;
  4957. }
  4958. }
  4959. #endif
  4960. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  4961. bh_print_time("End compile jit functions");
  4962. return true;
  4963. }
  4964. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  4965. static bool
  4966. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4967. uint32 cur_func_idx, char *error_buf,
  4968. uint32 error_buf_size);
  4969. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  4970. void **
  4971. wasm_interp_get_handle_table();
  4972. static void **handle_table;
  4973. #endif
  4974. static bool
  4975. load_from_sections(WASMModule *module, WASMSection *sections,
  4976. bool is_load_from_file_buf, char *error_buf,
  4977. uint32 error_buf_size)
  4978. {
  4979. WASMExport *export;
  4980. WASMSection *section = sections;
  4981. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  4982. *buf_func = NULL, *buf_func_end = NULL;
  4983. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  4984. WASMGlobal *aux_stack_top_global = NULL, *global;
  4985. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  4986. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  4987. uint32 aux_data_end_global_index = (uint32)-1;
  4988. uint32 aux_heap_base_global_index = (uint32)-1;
  4989. WASMFuncType *func_type;
  4990. /* Find code and function sections if have */
  4991. while (section) {
  4992. if (section->section_type == SECTION_TYPE_CODE) {
  4993. buf_code = section->section_body;
  4994. buf_code_end = buf_code + section->section_body_size;
  4995. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  4996. module->buf_code = (uint8 *)buf_code;
  4997. module->buf_code_size = section->section_body_size;
  4998. #endif
  4999. }
  5000. else if (section->section_type == SECTION_TYPE_FUNC) {
  5001. buf_func = section->section_body;
  5002. buf_func_end = buf_func + section->section_body_size;
  5003. }
  5004. section = section->next;
  5005. }
  5006. section = sections;
  5007. while (section) {
  5008. buf = section->section_body;
  5009. buf_end = buf + section->section_body_size;
  5010. switch (section->section_type) {
  5011. case SECTION_TYPE_USER:
  5012. /* unsupported user section, ignore it. */
  5013. if (!load_user_section(buf, buf_end, module,
  5014. is_load_from_file_buf, error_buf,
  5015. error_buf_size))
  5016. return false;
  5017. break;
  5018. case SECTION_TYPE_TYPE:
  5019. if (!load_type_section(buf, buf_end, module, error_buf,
  5020. error_buf_size))
  5021. return false;
  5022. break;
  5023. case SECTION_TYPE_IMPORT:
  5024. if (!load_import_section(buf, buf_end, module,
  5025. is_load_from_file_buf, error_buf,
  5026. error_buf_size))
  5027. return false;
  5028. break;
  5029. case SECTION_TYPE_FUNC:
  5030. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5031. module, error_buf, error_buf_size))
  5032. return false;
  5033. break;
  5034. case SECTION_TYPE_TABLE:
  5035. if (!load_table_section(buf, buf_end, module, error_buf,
  5036. error_buf_size))
  5037. return false;
  5038. break;
  5039. case SECTION_TYPE_MEMORY:
  5040. if (!load_memory_section(buf, buf_end, module, error_buf,
  5041. error_buf_size))
  5042. return false;
  5043. break;
  5044. #if WASM_ENABLE_TAGS != 0
  5045. case SECTION_TYPE_TAG:
  5046. /* load tag declaration section */
  5047. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5048. module, error_buf, error_buf_size))
  5049. return false;
  5050. break;
  5051. #endif
  5052. case SECTION_TYPE_GLOBAL:
  5053. if (!load_global_section(buf, buf_end, module, error_buf,
  5054. error_buf_size))
  5055. return false;
  5056. break;
  5057. case SECTION_TYPE_EXPORT:
  5058. if (!load_export_section(buf, buf_end, module,
  5059. is_load_from_file_buf, error_buf,
  5060. error_buf_size))
  5061. return false;
  5062. break;
  5063. case SECTION_TYPE_START:
  5064. if (!load_start_section(buf, buf_end, module, error_buf,
  5065. error_buf_size))
  5066. return false;
  5067. break;
  5068. case SECTION_TYPE_ELEM:
  5069. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5070. error_buf_size))
  5071. return false;
  5072. break;
  5073. case SECTION_TYPE_CODE:
  5074. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5075. module, error_buf, error_buf_size))
  5076. return false;
  5077. break;
  5078. case SECTION_TYPE_DATA:
  5079. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  5080. error_buf_size))
  5081. return false;
  5082. break;
  5083. #if WASM_ENABLE_BULK_MEMORY != 0
  5084. case SECTION_TYPE_DATACOUNT:
  5085. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5086. error_buf_size))
  5087. return false;
  5088. break;
  5089. #endif
  5090. #if WASM_ENABLE_STRINGREF != 0
  5091. case SECTION_TYPE_STRINGREF:
  5092. if (!load_stringref_section(buf, buf_end, module,
  5093. is_load_from_file_buf, error_buf,
  5094. error_buf_size))
  5095. return false;
  5096. break;
  5097. #endif
  5098. default:
  5099. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5100. return false;
  5101. }
  5102. section = section->next;
  5103. }
  5104. module->aux_data_end_global_index = (uint32)-1;
  5105. module->aux_heap_base_global_index = (uint32)-1;
  5106. module->aux_stack_top_global_index = (uint32)-1;
  5107. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5108. export = module->exports;
  5109. for (i = 0; i < module->export_count; i++, export ++) {
  5110. if (export->kind == EXPORT_KIND_GLOBAL) {
  5111. if (!strcmp(export->name, "__heap_base")) {
  5112. global_index = export->index - module->import_global_count;
  5113. global = module->globals + global_index;
  5114. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5115. && global->init_expr.init_expr_type
  5116. == INIT_EXPR_TYPE_I32_CONST) {
  5117. aux_heap_base_global = global;
  5118. aux_heap_base = global->init_expr.u.i32;
  5119. aux_heap_base_global_index = export->index;
  5120. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  5121. aux_heap_base);
  5122. }
  5123. }
  5124. else if (!strcmp(export->name, "__data_end")) {
  5125. global_index = export->index - module->import_global_count;
  5126. global = module->globals + global_index;
  5127. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5128. && global->init_expr.init_expr_type
  5129. == INIT_EXPR_TYPE_I32_CONST) {
  5130. aux_data_end_global = global;
  5131. aux_data_end = global->init_expr.u.i32;
  5132. aux_data_end_global_index = export->index;
  5133. LOG_VERBOSE("Found aux __data_end global, value: %d",
  5134. aux_data_end);
  5135. aux_data_end = align_uint(aux_data_end, 16);
  5136. }
  5137. }
  5138. /* For module compiled with -pthread option, the global is:
  5139. [0] stack_top <-- 0
  5140. [1] tls_pointer
  5141. [2] tls_size
  5142. [3] data_end <-- 3
  5143. [4] global_base
  5144. [5] heap_base <-- 5
  5145. [6] dso_handle
  5146. For module compiled without -pthread option:
  5147. [0] stack_top <-- 0
  5148. [1] data_end <-- 1
  5149. [2] global_base
  5150. [3] heap_base <-- 3
  5151. [4] dso_handle
  5152. */
  5153. if (aux_data_end_global && aux_heap_base_global
  5154. && aux_data_end <= aux_heap_base) {
  5155. module->aux_data_end_global_index = aux_data_end_global_index;
  5156. module->aux_data_end = aux_data_end;
  5157. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5158. module->aux_heap_base = aux_heap_base;
  5159. /* Resolve aux stack top global */
  5160. for (global_index = 0; global_index < module->global_count;
  5161. global_index++) {
  5162. global = module->globals + global_index;
  5163. if (global->is_mutable /* heap_base and data_end is
  5164. not mutable */
  5165. && global->type == VALUE_TYPE_I32
  5166. && global->init_expr.init_expr_type
  5167. == INIT_EXPR_TYPE_I32_CONST
  5168. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  5169. aux_stack_top_global = global;
  5170. aux_stack_top = (uint32)global->init_expr.u.i32;
  5171. module->aux_stack_top_global_index =
  5172. module->import_global_count + global_index;
  5173. module->aux_stack_bottom = aux_stack_top;
  5174. module->aux_stack_size =
  5175. aux_stack_top > aux_data_end
  5176. ? aux_stack_top - aux_data_end
  5177. : aux_stack_top;
  5178. LOG_VERBOSE("Found aux stack top global, value: %d, "
  5179. "global index: %d, stack size: %d",
  5180. aux_stack_top, global_index,
  5181. module->aux_stack_size);
  5182. break;
  5183. }
  5184. }
  5185. if (!aux_stack_top_global) {
  5186. /* Auxiliary stack global isn't found, it must be unused
  5187. in the wasm app, as if it is used, the global must be
  5188. defined. Here we set it to __heap_base global and set
  5189. its size to 0. */
  5190. aux_stack_top_global = aux_heap_base_global;
  5191. aux_stack_top = aux_heap_base;
  5192. module->aux_stack_top_global_index =
  5193. module->aux_heap_base_global_index;
  5194. module->aux_stack_bottom = aux_stack_top;
  5195. module->aux_stack_size = 0;
  5196. }
  5197. break;
  5198. }
  5199. }
  5200. }
  5201. module->malloc_function = (uint32)-1;
  5202. module->free_function = (uint32)-1;
  5203. module->retain_function = (uint32)-1;
  5204. /* Resolve malloc/free function exported by wasm module */
  5205. export = module->exports;
  5206. for (i = 0; i < module->export_count; i++, export ++) {
  5207. if (export->kind == EXPORT_KIND_FUNC) {
  5208. if (!strcmp(export->name, "malloc")
  5209. && export->index >= module->import_function_count) {
  5210. func_index = export->index - module->import_function_count;
  5211. func_type = module->functions[func_index]->func_type;
  5212. if (func_type->param_count == 1 && func_type->result_count == 1
  5213. && func_type->types[0] == VALUE_TYPE_I32
  5214. && func_type->types[1] == VALUE_TYPE_I32) {
  5215. bh_assert(module->malloc_function == (uint32)-1);
  5216. module->malloc_function = export->index;
  5217. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5218. export->name, export->index);
  5219. }
  5220. }
  5221. else if (!strcmp(export->name, "__new")
  5222. && export->index >= module->import_function_count) {
  5223. /* __new && __pin for AssemblyScript */
  5224. func_index = export->index - module->import_function_count;
  5225. func_type = module->functions[func_index]->func_type;
  5226. if (func_type->param_count == 2 && func_type->result_count == 1
  5227. && func_type->types[0] == VALUE_TYPE_I32
  5228. && func_type->types[1] == VALUE_TYPE_I32
  5229. && func_type->types[2] == VALUE_TYPE_I32) {
  5230. uint32 j;
  5231. WASMExport *export_tmp;
  5232. bh_assert(module->malloc_function == (uint32)-1);
  5233. module->malloc_function = export->index;
  5234. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5235. export->name, export->index);
  5236. /* resolve retain function.
  5237. If not found, reset malloc function index */
  5238. export_tmp = module->exports;
  5239. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5240. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5241. && (!strcmp(export_tmp->name, "__retain")
  5242. || (!strcmp(export_tmp->name, "__pin")))
  5243. && (export_tmp->index
  5244. >= module->import_function_count)) {
  5245. func_index = export_tmp->index
  5246. - module->import_function_count;
  5247. func_type =
  5248. module->functions[func_index]->func_type;
  5249. if (func_type->param_count == 1
  5250. && func_type->result_count == 1
  5251. && func_type->types[0] == VALUE_TYPE_I32
  5252. && func_type->types[1] == VALUE_TYPE_I32) {
  5253. bh_assert(module->retain_function
  5254. == (uint32)-1);
  5255. module->retain_function = export_tmp->index;
  5256. LOG_VERBOSE("Found retain function, name: %s, "
  5257. "index: %u",
  5258. export_tmp->name,
  5259. export_tmp->index);
  5260. break;
  5261. }
  5262. }
  5263. }
  5264. if (j == module->export_count) {
  5265. module->malloc_function = (uint32)-1;
  5266. LOG_VERBOSE("Can't find retain function,"
  5267. "reset malloc function index to -1");
  5268. }
  5269. }
  5270. }
  5271. else if (((!strcmp(export->name, "free"))
  5272. || (!strcmp(export->name, "__release"))
  5273. || (!strcmp(export->name, "__unpin")))
  5274. && export->index >= module->import_function_count) {
  5275. func_index = export->index - module->import_function_count;
  5276. func_type = module->functions[func_index]->func_type;
  5277. if (func_type->param_count == 1 && func_type->result_count == 0
  5278. && func_type->types[0] == VALUE_TYPE_I32) {
  5279. bh_assert(module->free_function == (uint32)-1);
  5280. module->free_function = export->index;
  5281. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5282. export->name, export->index);
  5283. }
  5284. }
  5285. }
  5286. }
  5287. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5288. handle_table = wasm_interp_get_handle_table();
  5289. #endif
  5290. for (i = 0; i < module->function_count; i++) {
  5291. WASMFunction *func = module->functions[i];
  5292. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5293. error_buf_size)) {
  5294. return false;
  5295. }
  5296. if (i == module->function_count - 1
  5297. && func->code + func->code_size != buf_code_end) {
  5298. set_error_buf(error_buf, error_buf_size,
  5299. "code section size mismatch");
  5300. return false;
  5301. }
  5302. }
  5303. if (!module->possible_memory_grow) {
  5304. WASMMemoryImport *memory_import;
  5305. WASMMemory *memory;
  5306. if (aux_data_end_global && aux_heap_base_global
  5307. && aux_stack_top_global) {
  5308. uint64 init_memory_size;
  5309. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  5310. if (module->import_memory_count) {
  5311. memory_import = &module->import_memories[0].u.memory;
  5312. init_memory_size = (uint64)memory_import->num_bytes_per_page
  5313. * memory_import->init_page_count;
  5314. if (shrunk_memory_size <= init_memory_size) {
  5315. /* Reset memory info to decrease memory usage */
  5316. memory_import->num_bytes_per_page = shrunk_memory_size;
  5317. memory_import->init_page_count = 1;
  5318. LOG_VERBOSE("Shrink import memory size to %d",
  5319. shrunk_memory_size);
  5320. }
  5321. }
  5322. if (module->memory_count) {
  5323. memory = &module->memories[0];
  5324. init_memory_size = (uint64)memory->num_bytes_per_page
  5325. * memory->init_page_count;
  5326. if (shrunk_memory_size <= init_memory_size) {
  5327. /* Reset memory info to decrease memory usage */
  5328. memory->num_bytes_per_page = shrunk_memory_size;
  5329. memory->init_page_count = 1;
  5330. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  5331. }
  5332. }
  5333. }
  5334. #if WASM_ENABLE_MULTI_MODULE == 0
  5335. if (module->import_memory_count) {
  5336. memory_import = &module->import_memories[0].u.memory;
  5337. if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
  5338. memory_import->num_bytes_per_page *=
  5339. memory_import->init_page_count;
  5340. else
  5341. memory_import->num_bytes_per_page = UINT32_MAX;
  5342. if (memory_import->init_page_count > 0)
  5343. memory_import->init_page_count = memory_import->max_page_count =
  5344. 1;
  5345. else
  5346. memory_import->init_page_count = memory_import->max_page_count =
  5347. 0;
  5348. }
  5349. if (module->memory_count) {
  5350. memory = &module->memories[0];
  5351. if (memory->init_page_count < DEFAULT_MAX_PAGES)
  5352. memory->num_bytes_per_page *= memory->init_page_count;
  5353. else
  5354. memory->num_bytes_per_page = UINT32_MAX;
  5355. if (memory->init_page_count > 0)
  5356. memory->init_page_count = memory->max_page_count = 1;
  5357. else
  5358. memory->init_page_count = memory->max_page_count = 0;
  5359. }
  5360. #endif
  5361. }
  5362. calculate_global_data_offset(module);
  5363. #if WASM_ENABLE_FAST_JIT != 0
  5364. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5365. return false;
  5366. }
  5367. #endif
  5368. #if WASM_ENABLE_JIT != 0
  5369. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5370. return false;
  5371. }
  5372. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5373. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5374. return false;
  5375. }
  5376. #else
  5377. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5378. thread fast jit execution, since applying llvm optimizations in
  5379. aot_compile_wasm may cost a lot of time.
  5380. Create thread with enough native stack to apply llvm optimizations */
  5381. if (os_thread_create(&module->llvm_jit_init_thread,
  5382. init_llvm_jit_functions_stage2_callback,
  5383. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5384. != 0) {
  5385. set_error_buf(error_buf, error_buf_size,
  5386. "create orcjit compile thread failed");
  5387. return false;
  5388. }
  5389. #endif
  5390. #endif
  5391. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5392. /* Create threads to compile the jit functions */
  5393. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5394. return false;
  5395. }
  5396. #endif
  5397. #if WASM_ENABLE_MEMORY_TRACING != 0
  5398. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5399. #endif
  5400. return true;
  5401. }
  5402. static WASMModule *
  5403. create_module(char *error_buf, uint32 error_buf_size)
  5404. {
  5405. WASMModule *module =
  5406. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5407. bh_list_status ret;
  5408. if (!module) {
  5409. return NULL;
  5410. }
  5411. module->module_type = Wasm_Module_Bytecode;
  5412. /* Set start_function to -1, means no start function */
  5413. module->start_function = (uint32)-1;
  5414. #if WASM_ENABLE_FAST_INTERP == 0
  5415. module->br_table_cache_list = &module->br_table_cache_list_head;
  5416. ret = bh_list_init(module->br_table_cache_list);
  5417. bh_assert(ret == BH_LIST_SUCCESS);
  5418. #endif
  5419. #if WASM_ENABLE_MULTI_MODULE != 0
  5420. module->import_module_list = &module->import_module_list_head;
  5421. ret = bh_list_init(module->import_module_list);
  5422. bh_assert(ret == BH_LIST_SUCCESS);
  5423. #endif
  5424. #if WASM_ENABLE_DEBUG_INTERP != 0
  5425. ret = bh_list_init(&module->fast_opcode_list);
  5426. bh_assert(ret == BH_LIST_SUCCESS);
  5427. #endif
  5428. #if WASM_ENABLE_GC != 0
  5429. if (!(module->ref_type_set =
  5430. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5431. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5432. goto fail1;
  5433. }
  5434. if (os_mutex_init(&module->rtt_type_lock)) {
  5435. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5436. goto fail2;
  5437. }
  5438. #endif
  5439. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5440. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5441. && WASM_ENABLE_LAZY_JIT != 0)
  5442. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5443. set_error_buf(error_buf, error_buf_size,
  5444. "init instance list lock failed");
  5445. goto fail3;
  5446. }
  5447. #endif
  5448. (void)ret;
  5449. return module;
  5450. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5451. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5452. && WASM_ENABLE_LAZY_JIT != 0)
  5453. fail3:
  5454. #endif
  5455. #if WASM_ENABLE_GC != 0
  5456. os_mutex_destroy(&module->rtt_type_lock);
  5457. fail2:
  5458. bh_hash_map_destroy(module->ref_type_set);
  5459. fail1:
  5460. #endif
  5461. wasm_runtime_free(module);
  5462. return NULL;
  5463. }
  5464. #if WASM_ENABLE_DEBUG_INTERP != 0
  5465. static bool
  5466. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5467. uint32 error_buf_size)
  5468. {
  5469. WASMFastOPCodeNode *fast_op =
  5470. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5471. if (fast_op) {
  5472. fast_op->offset = pos - module->load_addr;
  5473. fast_op->orig_op = orig_op;
  5474. bh_list_insert(&module->fast_opcode_list, fast_op);
  5475. }
  5476. return fast_op ? true : false;
  5477. }
  5478. #endif
  5479. WASMModule *
  5480. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5481. uint32 error_buf_size)
  5482. {
  5483. WASMModule *module = create_module(error_buf, error_buf_size);
  5484. if (!module)
  5485. return NULL;
  5486. if (!load_from_sections(module, section_list, false, error_buf,
  5487. error_buf_size)) {
  5488. wasm_loader_unload(module);
  5489. return NULL;
  5490. }
  5491. LOG_VERBOSE("Load module from sections success.\n");
  5492. return module;
  5493. }
  5494. static void
  5495. destroy_sections(WASMSection *section_list)
  5496. {
  5497. WASMSection *section = section_list, *next;
  5498. while (section) {
  5499. next = section->next;
  5500. wasm_runtime_free(section);
  5501. section = next;
  5502. }
  5503. }
  5504. /* clang-format off */
  5505. static uint8 section_ids[] = {
  5506. SECTION_TYPE_USER,
  5507. SECTION_TYPE_TYPE,
  5508. SECTION_TYPE_IMPORT,
  5509. SECTION_TYPE_FUNC,
  5510. SECTION_TYPE_TABLE,
  5511. SECTION_TYPE_MEMORY,
  5512. #if WASM_ENABLE_TAGS != 0
  5513. SECTION_TYPE_TAG,
  5514. #endif
  5515. #if WASM_ENABLE_STRINGREF != 0
  5516. /* must immediately precede the global section,
  5517. or where the global section would be */
  5518. SECTION_TYPE_STRINGREF,
  5519. #endif
  5520. SECTION_TYPE_GLOBAL,
  5521. SECTION_TYPE_EXPORT,
  5522. SECTION_TYPE_START,
  5523. SECTION_TYPE_ELEM,
  5524. #if WASM_ENABLE_BULK_MEMORY != 0
  5525. SECTION_TYPE_DATACOUNT,
  5526. #endif
  5527. SECTION_TYPE_CODE,
  5528. SECTION_TYPE_DATA
  5529. };
  5530. /* clang-format on */
  5531. static uint8
  5532. get_section_index(uint8 section_type)
  5533. {
  5534. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5535. for (uint8 i = 0; i < max_id; i++) {
  5536. if (section_type == section_ids[i])
  5537. return i;
  5538. }
  5539. return (uint8)-1;
  5540. }
  5541. static bool
  5542. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5543. char *error_buf, uint32 error_buf_size)
  5544. {
  5545. WASMSection *section_list_end = NULL, *section;
  5546. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  5547. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5548. uint32 section_size;
  5549. bh_assert(!*p_section_list);
  5550. p += 8;
  5551. while (p < p_end) {
  5552. CHECK_BUF(p, p_end, 1);
  5553. section_type = read_uint8(p);
  5554. section_index = get_section_index(section_type);
  5555. if (section_index != (uint8)-1) {
  5556. if (section_type != SECTION_TYPE_USER) {
  5557. /* Custom sections may be inserted at any place,
  5558. while other sections must occur at most once
  5559. and in prescribed order. */
  5560. if (last_section_index != (uint8)-1
  5561. && (section_index <= last_section_index)) {
  5562. set_error_buf(error_buf, error_buf_size,
  5563. "unexpected content after last section or "
  5564. "junk after last section");
  5565. return false;
  5566. }
  5567. last_section_index = section_index;
  5568. }
  5569. read_leb_uint32(p, p_end, section_size);
  5570. CHECK_BUF1(p, p_end, section_size);
  5571. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5572. error_buf_size))) {
  5573. return false;
  5574. }
  5575. section->section_type = section_type;
  5576. section->section_body = (uint8 *)p;
  5577. section->section_body_size = section_size;
  5578. if (!section_list_end)
  5579. *p_section_list = section_list_end = section;
  5580. else {
  5581. section_list_end->next = section;
  5582. section_list_end = section;
  5583. }
  5584. p += section_size;
  5585. }
  5586. else {
  5587. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5588. return false;
  5589. }
  5590. }
  5591. return true;
  5592. fail:
  5593. return false;
  5594. }
  5595. static void
  5596. exchange32(uint8 *p_data)
  5597. {
  5598. uint8 value = *p_data;
  5599. *p_data = *(p_data + 3);
  5600. *(p_data + 3) = value;
  5601. value = *(p_data + 1);
  5602. *(p_data + 1) = *(p_data + 2);
  5603. *(p_data + 2) = value;
  5604. }
  5605. static union {
  5606. int a;
  5607. char b;
  5608. } __ue = { .a = 1 };
  5609. #define is_little_endian() (__ue.b == 1)
  5610. static bool
  5611. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  5612. uint32 error_buf_size)
  5613. {
  5614. const uint8 *buf_end = buf + size;
  5615. const uint8 *p = buf, *p_end = buf_end;
  5616. uint32 magic_number, version;
  5617. WASMSection *section_list = NULL;
  5618. CHECK_BUF1(p, p_end, sizeof(uint32));
  5619. magic_number = read_uint32(p);
  5620. if (!is_little_endian())
  5621. exchange32((uint8 *)&magic_number);
  5622. if (magic_number != WASM_MAGIC_NUMBER) {
  5623. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5624. return false;
  5625. }
  5626. CHECK_BUF1(p, p_end, sizeof(uint32));
  5627. version = read_uint32(p);
  5628. if (!is_little_endian())
  5629. exchange32((uint8 *)&version);
  5630. if (version != WASM_CURRENT_VERSION) {
  5631. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5632. return false;
  5633. }
  5634. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5635. || !load_from_sections(module, section_list, true, error_buf,
  5636. error_buf_size)) {
  5637. destroy_sections(section_list);
  5638. return false;
  5639. }
  5640. destroy_sections(section_list);
  5641. return true;
  5642. fail:
  5643. return false;
  5644. }
  5645. #if WASM_ENABLE_LIBC_WASI != 0
  5646. /**
  5647. * refer to
  5648. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5649. */
  5650. static bool
  5651. check_wasi_abi_compatibility(const WASMModule *module,
  5652. #if WASM_ENABLE_MULTI_MODULE != 0
  5653. bool main_module,
  5654. #endif
  5655. char *error_buf, uint32 error_buf_size)
  5656. {
  5657. /**
  5658. * be careful with:
  5659. * wasi compatiable modules(command/reactor) which don't import any wasi
  5660. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5661. * reactor can depend on nothing. At the same time, each has its own entry
  5662. * point.
  5663. *
  5664. * observations:
  5665. * - clang always injects `_start` into a command
  5666. * - clang always injects `_initialize` into a reactor
  5667. * - `iwasm -f` allows to run a function in the reactor
  5668. *
  5669. * strong assumptions:
  5670. * - no one will define either `_start` or `_initialize` on purpose
  5671. * - `_start` should always be `void _start(void)`
  5672. * - `_initialize` should always be `void _initialize(void)`
  5673. *
  5674. */
  5675. /* clang-format off */
  5676. /**
  5677. *
  5678. * | | import_wasi_api True | | import_wasi_api False | |
  5679. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5680. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5681. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5682. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5683. */
  5684. /* clang-format on */
  5685. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5686. /* (func (export "_start") (...) */
  5687. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5688. error_buf, error_buf_size);
  5689. if (start) {
  5690. WASMFuncType *func_type =
  5691. module->functions[start->index - module->import_function_count]
  5692. ->func_type;
  5693. if (func_type->param_count || func_type->result_count) {
  5694. set_error_buf(error_buf, error_buf_size,
  5695. "the signature of builtin _start function is wrong");
  5696. return false;
  5697. }
  5698. }
  5699. else {
  5700. /* (func (export "_initialize") (...) */
  5701. initialize =
  5702. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5703. error_buf, error_buf_size);
  5704. if (initialize) {
  5705. WASMFuncType *func_type =
  5706. module
  5707. ->functions[initialize->index
  5708. - module->import_function_count]
  5709. ->func_type;
  5710. if (func_type->param_count || func_type->result_count) {
  5711. set_error_buf(
  5712. error_buf, error_buf_size,
  5713. "the signature of builtin _initialize function is wrong");
  5714. return false;
  5715. }
  5716. }
  5717. }
  5718. /* filter out non-wasi compatiable modules */
  5719. if (!module->import_wasi_api && !start && !initialize) {
  5720. return true;
  5721. }
  5722. /* should have one at least */
  5723. if (module->import_wasi_api && !start && !initialize) {
  5724. LOG_WARNING("warning: a module with WASI apis should be either "
  5725. "a command or a reactor");
  5726. }
  5727. /*
  5728. * there is at least one of `_start` and `_initialize` in below cases.
  5729. * according to the assumption, they should be all wasi compatiable
  5730. */
  5731. #if WASM_ENABLE_MULTI_MODULE != 0
  5732. /* filter out commands (with `_start`) cases */
  5733. if (start && !main_module) {
  5734. set_error_buf(
  5735. error_buf, error_buf_size,
  5736. "a command (with _start function) can not be a sub-module");
  5737. return false;
  5738. }
  5739. #endif
  5740. /*
  5741. * it is ok a reactor acts as a main module,
  5742. * so skip the check about (with `_initialize`)
  5743. */
  5744. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5745. error_buf, error_buf_size);
  5746. if (!memory
  5747. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5748. /*
  5749. * with wasi-threads, it's still an open question if a memory
  5750. * should be exported.
  5751. *
  5752. * https://github.com/WebAssembly/wasi-threads/issues/22
  5753. * https://github.com/WebAssembly/WASI/issues/502
  5754. *
  5755. * Note: this code assumes the number of memories is at most 1.
  5756. */
  5757. && module->import_memory_count == 0
  5758. #endif
  5759. ) {
  5760. set_error_buf(error_buf, error_buf_size,
  5761. "a module with WASI apis must export memory by default");
  5762. return false;
  5763. }
  5764. return true;
  5765. }
  5766. #endif
  5767. WASMModule *
  5768. wasm_loader_load(uint8 *buf, uint32 size,
  5769. #if WASM_ENABLE_MULTI_MODULE != 0
  5770. bool main_module,
  5771. #endif
  5772. char *error_buf, uint32 error_buf_size)
  5773. {
  5774. WASMModule *module = create_module(error_buf, error_buf_size);
  5775. if (!module) {
  5776. return NULL;
  5777. }
  5778. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5779. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5780. module->load_addr = (uint8 *)buf;
  5781. module->load_size = size;
  5782. #endif
  5783. if (!load(buf, size, module, error_buf, error_buf_size)) {
  5784. goto fail;
  5785. }
  5786. #if WASM_ENABLE_LIBC_WASI != 0
  5787. /* Check the WASI application ABI */
  5788. if (!check_wasi_abi_compatibility(module,
  5789. #if WASM_ENABLE_MULTI_MODULE != 0
  5790. main_module,
  5791. #endif
  5792. error_buf, error_buf_size)) {
  5793. goto fail;
  5794. }
  5795. #endif
  5796. LOG_VERBOSE("Load module success.\n");
  5797. return module;
  5798. fail:
  5799. wasm_loader_unload(module);
  5800. return NULL;
  5801. }
  5802. void
  5803. wasm_loader_unload(WASMModule *module)
  5804. {
  5805. uint32 i;
  5806. if (!module)
  5807. return;
  5808. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5809. && WASM_ENABLE_LAZY_JIT != 0
  5810. module->orcjit_stop_compiling = true;
  5811. if (module->llvm_jit_init_thread)
  5812. os_thread_join(module->llvm_jit_init_thread, NULL);
  5813. #endif
  5814. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5815. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  5816. module internal data after they were freed */
  5817. orcjit_stop_compile_threads(module);
  5818. #endif
  5819. #if WASM_ENABLE_JIT != 0
  5820. if (module->func_ptrs)
  5821. wasm_runtime_free(module->func_ptrs);
  5822. if (module->comp_ctx)
  5823. aot_destroy_comp_context(module->comp_ctx);
  5824. if (module->comp_data)
  5825. aot_destroy_comp_data(module->comp_data);
  5826. #endif
  5827. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5828. && WASM_ENABLE_LAZY_JIT != 0
  5829. if (module->tierup_wait_lock_inited) {
  5830. os_mutex_destroy(&module->tierup_wait_lock);
  5831. os_cond_destroy(&module->tierup_wait_cond);
  5832. }
  5833. #endif
  5834. if (module->types) {
  5835. for (i = 0; i < module->type_count; i++) {
  5836. if (module->types[i])
  5837. destroy_wasm_type(module->types[i]);
  5838. }
  5839. wasm_runtime_free(module->types);
  5840. }
  5841. if (module->imports)
  5842. wasm_runtime_free(module->imports);
  5843. if (module->functions) {
  5844. for (i = 0; i < module->function_count; i++) {
  5845. if (module->functions[i]) {
  5846. if (module->functions[i]->local_offsets)
  5847. wasm_runtime_free(module->functions[i]->local_offsets);
  5848. #if WASM_ENABLE_FAST_INTERP != 0
  5849. if (module->functions[i]->code_compiled)
  5850. wasm_runtime_free(module->functions[i]->code_compiled);
  5851. if (module->functions[i]->consts)
  5852. wasm_runtime_free(module->functions[i]->consts);
  5853. #endif
  5854. #if WASM_ENABLE_FAST_JIT != 0
  5855. if (module->functions[i]->fast_jit_jitted_code) {
  5856. jit_code_cache_free(
  5857. module->functions[i]->fast_jit_jitted_code);
  5858. }
  5859. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5860. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  5861. jit_code_cache_free(
  5862. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  5863. }
  5864. #endif
  5865. #endif
  5866. #if WASM_ENABLE_GC != 0
  5867. if (module->functions[i]->local_ref_type_maps) {
  5868. wasm_runtime_free(
  5869. module->functions[i]->local_ref_type_maps);
  5870. }
  5871. #endif
  5872. wasm_runtime_free(module->functions[i]);
  5873. }
  5874. }
  5875. wasm_runtime_free(module->functions);
  5876. }
  5877. if (module->tables) {
  5878. #if WASM_ENABLE_GC != 0
  5879. for (i = 0; i < module->table_count; i++) {
  5880. destroy_init_expr(&module->tables[i].init_expr);
  5881. }
  5882. #endif
  5883. wasm_runtime_free(module->tables);
  5884. }
  5885. if (module->memories)
  5886. wasm_runtime_free(module->memories);
  5887. if (module->globals) {
  5888. #if WASM_ENABLE_GC != 0
  5889. for (i = 0; i < module->global_count; i++) {
  5890. destroy_init_expr(&module->globals[i].init_expr);
  5891. }
  5892. #endif
  5893. wasm_runtime_free(module->globals);
  5894. }
  5895. #if WASM_ENABLE_TAGS != 0
  5896. if (module->tags) {
  5897. for (i = 0; i < module->tag_count; i++) {
  5898. if (module->tags[i])
  5899. wasm_runtime_free(module->tags[i]);
  5900. }
  5901. wasm_runtime_free(module->tags);
  5902. }
  5903. #endif
  5904. if (module->exports)
  5905. wasm_runtime_free(module->exports);
  5906. if (module->table_segments) {
  5907. for (i = 0; i < module->table_seg_count; i++) {
  5908. if (module->table_segments[i].init_values) {
  5909. #if WASM_ENABLE_GC != 0
  5910. uint32 j;
  5911. for (j = 0; j < module->table_segments[i].value_count; j++) {
  5912. destroy_init_expr(
  5913. &module->table_segments[i].init_values[j]);
  5914. }
  5915. #endif
  5916. wasm_runtime_free(module->table_segments[i].init_values);
  5917. }
  5918. }
  5919. wasm_runtime_free(module->table_segments);
  5920. }
  5921. if (module->data_segments) {
  5922. for (i = 0; i < module->data_seg_count; i++) {
  5923. if (module->data_segments[i])
  5924. wasm_runtime_free(module->data_segments[i]);
  5925. }
  5926. wasm_runtime_free(module->data_segments);
  5927. }
  5928. if (module->const_str_list) {
  5929. StringNode *node = module->const_str_list, *node_next;
  5930. while (node) {
  5931. node_next = node->next;
  5932. wasm_runtime_free(node);
  5933. node = node_next;
  5934. }
  5935. }
  5936. #if WASM_ENABLE_STRINGREF != 0
  5937. if (module->string_literal_ptrs) {
  5938. wasm_runtime_free(module->string_literal_ptrs);
  5939. }
  5940. if (module->string_literal_lengths) {
  5941. wasm_runtime_free(module->string_literal_lengths);
  5942. }
  5943. #endif
  5944. #if WASM_ENABLE_FAST_INTERP == 0
  5945. if (module->br_table_cache_list) {
  5946. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  5947. BrTableCache *node_next;
  5948. while (node) {
  5949. node_next = bh_list_elem_next(node);
  5950. wasm_runtime_free(node);
  5951. node = node_next;
  5952. }
  5953. }
  5954. #endif
  5955. #if WASM_ENABLE_MULTI_MODULE != 0
  5956. /* just release the sub module list */
  5957. if (module->import_module_list) {
  5958. WASMRegisteredModule *node =
  5959. bh_list_first_elem(module->import_module_list);
  5960. while (node) {
  5961. WASMRegisteredModule *next = bh_list_elem_next(node);
  5962. bh_list_remove(module->import_module_list, node);
  5963. /*
  5964. * unload(sub_module) will be trigged during runtime_destroy().
  5965. * every module in the global module list will be unloaded one by
  5966. * one. so don't worry.
  5967. */
  5968. wasm_runtime_free(node);
  5969. /*
  5970. * the module file reading buffer will be released
  5971. * in runtime_destroy()
  5972. */
  5973. node = next;
  5974. }
  5975. }
  5976. #endif
  5977. #if WASM_ENABLE_DEBUG_INTERP != 0
  5978. WASMFastOPCodeNode *fast_opcode =
  5979. bh_list_first_elem(&module->fast_opcode_list);
  5980. while (fast_opcode) {
  5981. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  5982. wasm_runtime_free(fast_opcode);
  5983. fast_opcode = next;
  5984. }
  5985. #endif
  5986. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5987. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5988. && WASM_ENABLE_LAZY_JIT != 0)
  5989. os_mutex_destroy(&module->instance_list_lock);
  5990. #endif
  5991. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  5992. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  5993. #endif
  5994. #if WASM_ENABLE_FAST_JIT != 0
  5995. if (module->fast_jit_func_ptrs) {
  5996. wasm_runtime_free(module->fast_jit_func_ptrs);
  5997. }
  5998. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  5999. if (module->fast_jit_thread_locks_inited[i]) {
  6000. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6001. }
  6002. }
  6003. #endif
  6004. #if WASM_ENABLE_GC != 0
  6005. os_mutex_destroy(&module->rtt_type_lock);
  6006. bh_hash_map_destroy(module->ref_type_set);
  6007. if (module->rtt_types) {
  6008. for (i = 0; i < module->type_count; i++) {
  6009. if (module->rtt_types[i])
  6010. wasm_runtime_free(module->rtt_types[i]);
  6011. }
  6012. wasm_runtime_free(module->rtt_types);
  6013. }
  6014. #if WASM_ENABLE_STRINGREF != 0
  6015. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6016. if (module->stringref_rtts[i])
  6017. wasm_runtime_free(module->stringref_rtts[i]);
  6018. }
  6019. #endif
  6020. #endif
  6021. wasm_runtime_free(module);
  6022. }
  6023. bool
  6024. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6025. const uint8 *start_addr, const uint8 *code_end_addr,
  6026. uint8 label_type, uint8 **p_else_addr,
  6027. uint8 **p_end_addr)
  6028. {
  6029. const uint8 *p = start_addr, *p_end = code_end_addr;
  6030. uint8 *else_addr = NULL;
  6031. char error_buf[128];
  6032. uint32 block_nested_depth = 1, count, i, j, t;
  6033. uint32 error_buf_size = sizeof(error_buf);
  6034. uint8 opcode, u8;
  6035. BlockAddr block_stack[16] = { { 0 } }, *block;
  6036. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6037. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6038. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6039. if (block[j].start_addr == start_addr) {
  6040. /* Cache hit */
  6041. *p_else_addr = block[j].else_addr;
  6042. *p_end_addr = block[j].end_addr;
  6043. return true;
  6044. }
  6045. }
  6046. /* Cache unhit */
  6047. block_stack[0].start_addr = start_addr;
  6048. while (p < code_end_addr) {
  6049. opcode = *p++;
  6050. #if WASM_ENABLE_DEBUG_INTERP != 0
  6051. op_break_retry:
  6052. #endif
  6053. switch (opcode) {
  6054. case WASM_OP_UNREACHABLE:
  6055. case WASM_OP_NOP:
  6056. break;
  6057. #if WASM_ENABLE_EXCE_HANDLING != 0
  6058. case WASM_OP_TRY:
  6059. u8 = read_uint8(p);
  6060. if (block_nested_depth
  6061. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6062. block_stack[block_nested_depth].start_addr = p;
  6063. block_stack[block_nested_depth].else_addr = NULL;
  6064. }
  6065. block_nested_depth++;
  6066. break;
  6067. case EXT_OP_TRY:
  6068. skip_leb_uint32(p, p_end);
  6069. if (block_nested_depth
  6070. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6071. block_stack[block_nested_depth].start_addr = p;
  6072. block_stack[block_nested_depth].else_addr = NULL;
  6073. }
  6074. block_nested_depth++;
  6075. break;
  6076. case WASM_OP_CATCH:
  6077. if (block_nested_depth == 1) {
  6078. *p_end_addr = (uint8 *)(p - 1);
  6079. /* stop search and return the address of the catch block */
  6080. return true;
  6081. }
  6082. break;
  6083. case WASM_OP_CATCH_ALL:
  6084. if (block_nested_depth == 1) {
  6085. *p_end_addr = (uint8 *)(p - 1);
  6086. /* stop search and return the address of the catch_all block
  6087. */
  6088. return true;
  6089. }
  6090. break;
  6091. case WASM_OP_THROW:
  6092. /* skip tag_index */
  6093. skip_leb(p);
  6094. break;
  6095. case WASM_OP_RETHROW:
  6096. /* skip depth */
  6097. skip_leb(p);
  6098. break;
  6099. case WASM_OP_DELEGATE:
  6100. if (block_nested_depth == 1) {
  6101. *p_end_addr = (uint8 *)(p - 1);
  6102. return true;
  6103. }
  6104. else {
  6105. skip_leb(p);
  6106. /* the DELEGATE opcode ends the tryblock, */
  6107. block_nested_depth--;
  6108. if (block_nested_depth
  6109. < sizeof(block_stack) / sizeof(BlockAddr))
  6110. block_stack[block_nested_depth].end_addr =
  6111. (uint8 *)(p - 1);
  6112. }
  6113. break;
  6114. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6115. case WASM_OP_BLOCK:
  6116. case WASM_OP_LOOP:
  6117. case WASM_OP_IF:
  6118. {
  6119. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6120. u8 = read_uint8(p);
  6121. if (is_byte_a_type(u8)) {
  6122. #if WASM_ENABLE_GC != 0
  6123. if (wasm_is_type_multi_byte_type(u8)) {
  6124. /* the possible extra bytes of GC ref type have been
  6125. modified to OP_NOP, no need to resolve them again */
  6126. }
  6127. #endif
  6128. }
  6129. else {
  6130. p--;
  6131. skip_leb_uint32(p, p_end);
  6132. }
  6133. if (block_nested_depth
  6134. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6135. block_stack[block_nested_depth].start_addr = p;
  6136. block_stack[block_nested_depth].else_addr = NULL;
  6137. }
  6138. block_nested_depth++;
  6139. break;
  6140. }
  6141. case EXT_OP_BLOCK:
  6142. case EXT_OP_LOOP:
  6143. case EXT_OP_IF:
  6144. /* block type */
  6145. skip_leb_uint32(p, p_end);
  6146. if (block_nested_depth
  6147. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6148. block_stack[block_nested_depth].start_addr = p;
  6149. block_stack[block_nested_depth].else_addr = NULL;
  6150. }
  6151. block_nested_depth++;
  6152. break;
  6153. case WASM_OP_ELSE:
  6154. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6155. else_addr = (uint8 *)(p - 1);
  6156. if (block_nested_depth - 1
  6157. < sizeof(block_stack) / sizeof(BlockAddr))
  6158. block_stack[block_nested_depth - 1].else_addr =
  6159. (uint8 *)(p - 1);
  6160. break;
  6161. case WASM_OP_END:
  6162. if (block_nested_depth == 1) {
  6163. if (label_type == LABEL_TYPE_IF)
  6164. *p_else_addr = else_addr;
  6165. *p_end_addr = (uint8 *)(p - 1);
  6166. block_stack[0].end_addr = (uint8 *)(p - 1);
  6167. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6168. t++) {
  6169. start_addr = block_stack[t].start_addr;
  6170. if (start_addr) {
  6171. i = ((uintptr_t)start_addr)
  6172. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6173. block =
  6174. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6175. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6176. if (!block[j].start_addr)
  6177. break;
  6178. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6179. memmove(block + 1, block,
  6180. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6181. * sizeof(BlockAddr));
  6182. j = 0;
  6183. }
  6184. block[j].start_addr = block_stack[t].start_addr;
  6185. block[j].else_addr = block_stack[t].else_addr;
  6186. block[j].end_addr = block_stack[t].end_addr;
  6187. }
  6188. else
  6189. break;
  6190. }
  6191. return true;
  6192. }
  6193. else {
  6194. block_nested_depth--;
  6195. if (block_nested_depth
  6196. < sizeof(block_stack) / sizeof(BlockAddr))
  6197. block_stack[block_nested_depth].end_addr =
  6198. (uint8 *)(p - 1);
  6199. }
  6200. break;
  6201. case WASM_OP_BR:
  6202. case WASM_OP_BR_IF:
  6203. skip_leb_uint32(p, p_end); /* labelidx */
  6204. break;
  6205. case WASM_OP_BR_TABLE:
  6206. read_leb_uint32(p, p_end, count); /* lable num */
  6207. #if WASM_ENABLE_FAST_INTERP != 0
  6208. for (i = 0; i <= count; i++) /* lableidxs */
  6209. skip_leb_uint32(p, p_end);
  6210. #else
  6211. p += count + 1;
  6212. while (*p == WASM_OP_NOP)
  6213. p++;
  6214. #endif
  6215. break;
  6216. #if WASM_ENABLE_FAST_INTERP == 0
  6217. case EXT_OP_BR_TABLE_CACHE:
  6218. read_leb_uint32(p, p_end, count); /* lable num */
  6219. while (*p == WASM_OP_NOP)
  6220. p++;
  6221. break;
  6222. #endif
  6223. case WASM_OP_RETURN:
  6224. break;
  6225. case WASM_OP_CALL:
  6226. #if WASM_ENABLE_TAIL_CALL != 0
  6227. case WASM_OP_RETURN_CALL:
  6228. #endif
  6229. skip_leb_uint32(p, p_end); /* funcidx */
  6230. break;
  6231. case WASM_OP_CALL_INDIRECT:
  6232. #if WASM_ENABLE_TAIL_CALL != 0
  6233. case WASM_OP_RETURN_CALL_INDIRECT:
  6234. #endif
  6235. skip_leb_uint32(p, p_end); /* typeidx */
  6236. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6237. u8 = read_uint8(p); /* 0x00 */
  6238. #else
  6239. skip_leb_uint32(p, p_end); /* talbeidx */
  6240. #endif
  6241. break;
  6242. #if WASM_ENABLE_GC != 0
  6243. case WASM_OP_CALL_REF:
  6244. case WASM_OP_RETURN_CALL_REF:
  6245. skip_leb_uint32(p, p_end); /* typeidx */
  6246. break;
  6247. #endif
  6248. case WASM_OP_DROP:
  6249. case WASM_OP_SELECT:
  6250. case WASM_OP_DROP_64:
  6251. case WASM_OP_SELECT_64:
  6252. break;
  6253. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6254. case WASM_OP_SELECT_T:
  6255. {
  6256. skip_leb_uint32(p, p_end); /* vec length */
  6257. u8 = read_uint8(p); /* typeidx */
  6258. /* the possible extra bytes of GC ref type have been
  6259. modified to OP_NOP, no need to resolve them again */
  6260. break;
  6261. }
  6262. case WASM_OP_TABLE_GET:
  6263. case WASM_OP_TABLE_SET:
  6264. skip_leb_uint32(p, p_end); /* table index */
  6265. break;
  6266. case WASM_OP_REF_NULL:
  6267. {
  6268. u8 = read_uint8(p); /* type */
  6269. if (is_byte_a_type(u8)) {
  6270. #if WASM_ENABLE_GC != 0
  6271. if (wasm_is_type_multi_byte_type(u8)) {
  6272. /* the possible extra bytes of GC ref type have been
  6273. modified to OP_NOP, no need to resolve them again */
  6274. }
  6275. #endif
  6276. }
  6277. else {
  6278. p--;
  6279. skip_leb_uint32(p, p_end);
  6280. }
  6281. break;
  6282. }
  6283. case WASM_OP_REF_IS_NULL:
  6284. break;
  6285. case WASM_OP_REF_FUNC:
  6286. skip_leb_uint32(p, p_end); /* func index */
  6287. break;
  6288. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6289. #if WASM_ENABLE_GC != 0
  6290. case WASM_OP_REF_AS_NON_NULL:
  6291. case WASM_OP_REF_EQ:
  6292. break;
  6293. case WASM_OP_BR_ON_NULL:
  6294. case WASM_OP_BR_ON_NON_NULL:
  6295. skip_leb_uint32(p, p_end); /* label index */
  6296. break;
  6297. #endif /* end of WASM_ENABLE_GC != 0 */
  6298. case WASM_OP_GET_LOCAL:
  6299. case WASM_OP_SET_LOCAL:
  6300. case WASM_OP_TEE_LOCAL:
  6301. case WASM_OP_GET_GLOBAL:
  6302. case WASM_OP_SET_GLOBAL:
  6303. case WASM_OP_GET_GLOBAL_64:
  6304. case WASM_OP_SET_GLOBAL_64:
  6305. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6306. skip_leb_uint32(p, p_end); /* local index */
  6307. break;
  6308. case EXT_OP_GET_LOCAL_FAST:
  6309. case EXT_OP_SET_LOCAL_FAST:
  6310. case EXT_OP_TEE_LOCAL_FAST:
  6311. CHECK_BUF(p, p_end, 1);
  6312. p++;
  6313. break;
  6314. case WASM_OP_I32_LOAD:
  6315. case WASM_OP_I64_LOAD:
  6316. case WASM_OP_F32_LOAD:
  6317. case WASM_OP_F64_LOAD:
  6318. case WASM_OP_I32_LOAD8_S:
  6319. case WASM_OP_I32_LOAD8_U:
  6320. case WASM_OP_I32_LOAD16_S:
  6321. case WASM_OP_I32_LOAD16_U:
  6322. case WASM_OP_I64_LOAD8_S:
  6323. case WASM_OP_I64_LOAD8_U:
  6324. case WASM_OP_I64_LOAD16_S:
  6325. case WASM_OP_I64_LOAD16_U:
  6326. case WASM_OP_I64_LOAD32_S:
  6327. case WASM_OP_I64_LOAD32_U:
  6328. case WASM_OP_I32_STORE:
  6329. case WASM_OP_I64_STORE:
  6330. case WASM_OP_F32_STORE:
  6331. case WASM_OP_F64_STORE:
  6332. case WASM_OP_I32_STORE8:
  6333. case WASM_OP_I32_STORE16:
  6334. case WASM_OP_I64_STORE8:
  6335. case WASM_OP_I64_STORE16:
  6336. case WASM_OP_I64_STORE32:
  6337. skip_leb_uint32(p, p_end); /* align */
  6338. skip_leb_uint32(p, p_end); /* offset */
  6339. break;
  6340. case WASM_OP_MEMORY_SIZE:
  6341. case WASM_OP_MEMORY_GROW:
  6342. skip_leb_uint32(p, p_end); /* 0x00 */
  6343. break;
  6344. case WASM_OP_I32_CONST:
  6345. skip_leb_int32(p, p_end);
  6346. break;
  6347. case WASM_OP_I64_CONST:
  6348. skip_leb_int64(p, p_end);
  6349. break;
  6350. case WASM_OP_F32_CONST:
  6351. p += sizeof(float32);
  6352. break;
  6353. case WASM_OP_F64_CONST:
  6354. p += sizeof(float64);
  6355. break;
  6356. case WASM_OP_I32_EQZ:
  6357. case WASM_OP_I32_EQ:
  6358. case WASM_OP_I32_NE:
  6359. case WASM_OP_I32_LT_S:
  6360. case WASM_OP_I32_LT_U:
  6361. case WASM_OP_I32_GT_S:
  6362. case WASM_OP_I32_GT_U:
  6363. case WASM_OP_I32_LE_S:
  6364. case WASM_OP_I32_LE_U:
  6365. case WASM_OP_I32_GE_S:
  6366. case WASM_OP_I32_GE_U:
  6367. case WASM_OP_I64_EQZ:
  6368. case WASM_OP_I64_EQ:
  6369. case WASM_OP_I64_NE:
  6370. case WASM_OP_I64_LT_S:
  6371. case WASM_OP_I64_LT_U:
  6372. case WASM_OP_I64_GT_S:
  6373. case WASM_OP_I64_GT_U:
  6374. case WASM_OP_I64_LE_S:
  6375. case WASM_OP_I64_LE_U:
  6376. case WASM_OP_I64_GE_S:
  6377. case WASM_OP_I64_GE_U:
  6378. case WASM_OP_F32_EQ:
  6379. case WASM_OP_F32_NE:
  6380. case WASM_OP_F32_LT:
  6381. case WASM_OP_F32_GT:
  6382. case WASM_OP_F32_LE:
  6383. case WASM_OP_F32_GE:
  6384. case WASM_OP_F64_EQ:
  6385. case WASM_OP_F64_NE:
  6386. case WASM_OP_F64_LT:
  6387. case WASM_OP_F64_GT:
  6388. case WASM_OP_F64_LE:
  6389. case WASM_OP_F64_GE:
  6390. case WASM_OP_I32_CLZ:
  6391. case WASM_OP_I32_CTZ:
  6392. case WASM_OP_I32_POPCNT:
  6393. case WASM_OP_I32_ADD:
  6394. case WASM_OP_I32_SUB:
  6395. case WASM_OP_I32_MUL:
  6396. case WASM_OP_I32_DIV_S:
  6397. case WASM_OP_I32_DIV_U:
  6398. case WASM_OP_I32_REM_S:
  6399. case WASM_OP_I32_REM_U:
  6400. case WASM_OP_I32_AND:
  6401. case WASM_OP_I32_OR:
  6402. case WASM_OP_I32_XOR:
  6403. case WASM_OP_I32_SHL:
  6404. case WASM_OP_I32_SHR_S:
  6405. case WASM_OP_I32_SHR_U:
  6406. case WASM_OP_I32_ROTL:
  6407. case WASM_OP_I32_ROTR:
  6408. case WASM_OP_I64_CLZ:
  6409. case WASM_OP_I64_CTZ:
  6410. case WASM_OP_I64_POPCNT:
  6411. case WASM_OP_I64_ADD:
  6412. case WASM_OP_I64_SUB:
  6413. case WASM_OP_I64_MUL:
  6414. case WASM_OP_I64_DIV_S:
  6415. case WASM_OP_I64_DIV_U:
  6416. case WASM_OP_I64_REM_S:
  6417. case WASM_OP_I64_REM_U:
  6418. case WASM_OP_I64_AND:
  6419. case WASM_OP_I64_OR:
  6420. case WASM_OP_I64_XOR:
  6421. case WASM_OP_I64_SHL:
  6422. case WASM_OP_I64_SHR_S:
  6423. case WASM_OP_I64_SHR_U:
  6424. case WASM_OP_I64_ROTL:
  6425. case WASM_OP_I64_ROTR:
  6426. case WASM_OP_F32_ABS:
  6427. case WASM_OP_F32_NEG:
  6428. case WASM_OP_F32_CEIL:
  6429. case WASM_OP_F32_FLOOR:
  6430. case WASM_OP_F32_TRUNC:
  6431. case WASM_OP_F32_NEAREST:
  6432. case WASM_OP_F32_SQRT:
  6433. case WASM_OP_F32_ADD:
  6434. case WASM_OP_F32_SUB:
  6435. case WASM_OP_F32_MUL:
  6436. case WASM_OP_F32_DIV:
  6437. case WASM_OP_F32_MIN:
  6438. case WASM_OP_F32_MAX:
  6439. case WASM_OP_F32_COPYSIGN:
  6440. case WASM_OP_F64_ABS:
  6441. case WASM_OP_F64_NEG:
  6442. case WASM_OP_F64_CEIL:
  6443. case WASM_OP_F64_FLOOR:
  6444. case WASM_OP_F64_TRUNC:
  6445. case WASM_OP_F64_NEAREST:
  6446. case WASM_OP_F64_SQRT:
  6447. case WASM_OP_F64_ADD:
  6448. case WASM_OP_F64_SUB:
  6449. case WASM_OP_F64_MUL:
  6450. case WASM_OP_F64_DIV:
  6451. case WASM_OP_F64_MIN:
  6452. case WASM_OP_F64_MAX:
  6453. case WASM_OP_F64_COPYSIGN:
  6454. case WASM_OP_I32_WRAP_I64:
  6455. case WASM_OP_I32_TRUNC_S_F32:
  6456. case WASM_OP_I32_TRUNC_U_F32:
  6457. case WASM_OP_I32_TRUNC_S_F64:
  6458. case WASM_OP_I32_TRUNC_U_F64:
  6459. case WASM_OP_I64_EXTEND_S_I32:
  6460. case WASM_OP_I64_EXTEND_U_I32:
  6461. case WASM_OP_I64_TRUNC_S_F32:
  6462. case WASM_OP_I64_TRUNC_U_F32:
  6463. case WASM_OP_I64_TRUNC_S_F64:
  6464. case WASM_OP_I64_TRUNC_U_F64:
  6465. case WASM_OP_F32_CONVERT_S_I32:
  6466. case WASM_OP_F32_CONVERT_U_I32:
  6467. case WASM_OP_F32_CONVERT_S_I64:
  6468. case WASM_OP_F32_CONVERT_U_I64:
  6469. case WASM_OP_F32_DEMOTE_F64:
  6470. case WASM_OP_F64_CONVERT_S_I32:
  6471. case WASM_OP_F64_CONVERT_U_I32:
  6472. case WASM_OP_F64_CONVERT_S_I64:
  6473. case WASM_OP_F64_CONVERT_U_I64:
  6474. case WASM_OP_F64_PROMOTE_F32:
  6475. case WASM_OP_I32_REINTERPRET_F32:
  6476. case WASM_OP_I64_REINTERPRET_F64:
  6477. case WASM_OP_F32_REINTERPRET_I32:
  6478. case WASM_OP_F64_REINTERPRET_I64:
  6479. case WASM_OP_I32_EXTEND8_S:
  6480. case WASM_OP_I32_EXTEND16_S:
  6481. case WASM_OP_I64_EXTEND8_S:
  6482. case WASM_OP_I64_EXTEND16_S:
  6483. case WASM_OP_I64_EXTEND32_S:
  6484. break;
  6485. #if WASM_ENABLE_GC != 0
  6486. case WASM_OP_GC_PREFIX:
  6487. {
  6488. uint32 opcode1;
  6489. read_leb_uint32(p, p_end, opcode1);
  6490. switch (opcode1) {
  6491. case WASM_OP_STRUCT_NEW:
  6492. case WASM_OP_STRUCT_NEW_DEFAULT:
  6493. skip_leb_uint32(p, p_end); /* typeidx */
  6494. break;
  6495. case WASM_OP_STRUCT_GET:
  6496. case WASM_OP_STRUCT_GET_S:
  6497. case WASM_OP_STRUCT_GET_U:
  6498. case WASM_OP_STRUCT_SET:
  6499. skip_leb_uint32(p, p_end); /* typeidx */
  6500. skip_leb_uint32(p, p_end); /* fieldidx */
  6501. break;
  6502. case WASM_OP_ARRAY_NEW:
  6503. case WASM_OP_ARRAY_NEW_DEFAULT:
  6504. case WASM_OP_ARRAY_GET:
  6505. case WASM_OP_ARRAY_GET_S:
  6506. case WASM_OP_ARRAY_GET_U:
  6507. case WASM_OP_ARRAY_SET:
  6508. case WASM_OP_ARRAY_FILL:
  6509. skip_leb_uint32(p, p_end); /* typeidx */
  6510. break;
  6511. case WASM_OP_ARRAY_COPY:
  6512. skip_leb_uint32(p, p_end); /* typeidx1 */
  6513. skip_leb_uint32(p, p_end); /* typeidx2 */
  6514. break;
  6515. case WASM_OP_ARRAY_LEN:
  6516. break;
  6517. case WASM_OP_ARRAY_NEW_FIXED:
  6518. case WASM_OP_ARRAY_NEW_DATA:
  6519. case WASM_OP_ARRAY_NEW_ELEM:
  6520. skip_leb_uint32(p, p_end); /* typeidx */
  6521. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6522. break;
  6523. case WASM_OP_REF_I31:
  6524. case WASM_OP_I31_GET_S:
  6525. case WASM_OP_I31_GET_U:
  6526. break;
  6527. case WASM_OP_REF_TEST:
  6528. case WASM_OP_REF_CAST:
  6529. case WASM_OP_REF_TEST_NULLABLE:
  6530. case WASM_OP_REF_CAST_NULLABLE:
  6531. skip_leb_int32(p, p_end); /* heaptype */
  6532. break;
  6533. case WASM_OP_BR_ON_CAST:
  6534. case WASM_OP_BR_ON_CAST_FAIL:
  6535. p += sizeof(uint8); /* castflag */
  6536. skip_leb_uint32(p, p_end); /* labelidx */
  6537. skip_leb_int32(p, p_end); /* heaptype */
  6538. skip_leb_int32(p, p_end); /* heaptype2 */
  6539. break;
  6540. case WASM_OP_ANY_CONVERT_EXTERN:
  6541. case WASM_OP_EXTERN_CONVERT_ANY:
  6542. break;
  6543. #if WASM_ENABLE_STRINGREF != 0
  6544. case WASM_OP_STRING_NEW_UTF8:
  6545. case WASM_OP_STRING_NEW_WTF16:
  6546. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6547. case WASM_OP_STRING_NEW_WTF8:
  6548. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6549. break;
  6550. case WASM_OP_STRING_CONST:
  6551. skip_leb_int32(p, p_end); /* contents */
  6552. break;
  6553. case WASM_OP_STRING_MEASURE_UTF8:
  6554. case WASM_OP_STRING_MEASURE_WTF8:
  6555. case WASM_OP_STRING_MEASURE_WTF16:
  6556. break;
  6557. case WASM_OP_STRING_ENCODE_UTF8:
  6558. case WASM_OP_STRING_ENCODE_WTF16:
  6559. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6560. case WASM_OP_STRING_ENCODE_WTF8:
  6561. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6562. break;
  6563. case WASM_OP_STRING_CONCAT:
  6564. case WASM_OP_STRING_EQ:
  6565. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6566. case WASM_OP_STRING_AS_WTF8:
  6567. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6568. break;
  6569. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6570. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6571. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6572. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6573. break;
  6574. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6575. case WASM_OP_STRING_AS_WTF16:
  6576. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6577. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6578. break;
  6579. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6580. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6581. break;
  6582. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6583. case WASM_OP_STRING_AS_ITER:
  6584. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6585. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6586. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6587. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6588. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6589. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6590. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6591. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6592. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6593. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6594. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6595. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6596. break;
  6597. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6598. default:
  6599. return false;
  6600. }
  6601. break;
  6602. }
  6603. #endif /* end of WASM_ENABLE_GC != 0 */
  6604. case WASM_OP_MISC_PREFIX:
  6605. {
  6606. uint32 opcode1;
  6607. read_leb_uint32(p, p_end, opcode1);
  6608. switch (opcode1) {
  6609. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6610. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6611. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6612. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6613. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6614. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6615. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6616. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6617. break;
  6618. #if WASM_ENABLE_BULK_MEMORY != 0
  6619. case WASM_OP_MEMORY_INIT:
  6620. skip_leb_uint32(p, p_end);
  6621. /* skip memory idx */
  6622. p++;
  6623. break;
  6624. case WASM_OP_DATA_DROP:
  6625. skip_leb_uint32(p, p_end);
  6626. break;
  6627. case WASM_OP_MEMORY_COPY:
  6628. /* skip two memory idx */
  6629. p += 2;
  6630. break;
  6631. case WASM_OP_MEMORY_FILL:
  6632. /* skip memory idx */
  6633. p++;
  6634. break;
  6635. #endif /* WASM_ENABLE_BULK_MEMORY */
  6636. #if WASM_ENABLE_REF_TYPES != 0
  6637. case WASM_OP_TABLE_INIT:
  6638. case WASM_OP_TABLE_COPY:
  6639. /* tableidx */
  6640. skip_leb_uint32(p, p_end);
  6641. /* elemidx */
  6642. skip_leb_uint32(p, p_end);
  6643. break;
  6644. case WASM_OP_ELEM_DROP:
  6645. /* elemidx */
  6646. skip_leb_uint32(p, p_end);
  6647. break;
  6648. case WASM_OP_TABLE_SIZE:
  6649. case WASM_OP_TABLE_GROW:
  6650. case WASM_OP_TABLE_FILL:
  6651. skip_leb_uint32(p, p_end); /* table idx */
  6652. break;
  6653. #endif /* WASM_ENABLE_REF_TYPES */
  6654. default:
  6655. return false;
  6656. }
  6657. break;
  6658. }
  6659. #if WASM_ENABLE_SIMD != 0
  6660. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6661. case WASM_OP_SIMD_PREFIX:
  6662. {
  6663. uint32 opcode1;
  6664. read_leb_uint32(p, p_end, opcode1);
  6665. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6666. is no larger than UINT8_MAX */
  6667. opcode = (uint8)opcode1;
  6668. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6669. */
  6670. switch (opcode) {
  6671. case SIMD_v128_load:
  6672. case SIMD_v128_load8x8_s:
  6673. case SIMD_v128_load8x8_u:
  6674. case SIMD_v128_load16x4_s:
  6675. case SIMD_v128_load16x4_u:
  6676. case SIMD_v128_load32x2_s:
  6677. case SIMD_v128_load32x2_u:
  6678. case SIMD_v128_load8_splat:
  6679. case SIMD_v128_load16_splat:
  6680. case SIMD_v128_load32_splat:
  6681. case SIMD_v128_load64_splat:
  6682. case SIMD_v128_store:
  6683. /* memarg align */
  6684. skip_leb_uint32(p, p_end);
  6685. /* memarg offset*/
  6686. skip_leb_uint32(p, p_end);
  6687. break;
  6688. case SIMD_v128_const:
  6689. case SIMD_v8x16_shuffle:
  6690. /* immByte[16] immLaneId[16] */
  6691. CHECK_BUF1(p, p_end, 16);
  6692. p += 16;
  6693. break;
  6694. case SIMD_i8x16_extract_lane_s:
  6695. case SIMD_i8x16_extract_lane_u:
  6696. case SIMD_i8x16_replace_lane:
  6697. case SIMD_i16x8_extract_lane_s:
  6698. case SIMD_i16x8_extract_lane_u:
  6699. case SIMD_i16x8_replace_lane:
  6700. case SIMD_i32x4_extract_lane:
  6701. case SIMD_i32x4_replace_lane:
  6702. case SIMD_i64x2_extract_lane:
  6703. case SIMD_i64x2_replace_lane:
  6704. case SIMD_f32x4_extract_lane:
  6705. case SIMD_f32x4_replace_lane:
  6706. case SIMD_f64x2_extract_lane:
  6707. case SIMD_f64x2_replace_lane:
  6708. /* ImmLaneId */
  6709. CHECK_BUF(p, p_end, 1);
  6710. p++;
  6711. break;
  6712. case SIMD_v128_load8_lane:
  6713. case SIMD_v128_load16_lane:
  6714. case SIMD_v128_load32_lane:
  6715. case SIMD_v128_load64_lane:
  6716. case SIMD_v128_store8_lane:
  6717. case SIMD_v128_store16_lane:
  6718. case SIMD_v128_store32_lane:
  6719. case SIMD_v128_store64_lane:
  6720. /* memarg align */
  6721. skip_leb_uint32(p, p_end);
  6722. /* memarg offset*/
  6723. skip_leb_uint32(p, p_end);
  6724. /* ImmLaneId */
  6725. CHECK_BUF(p, p_end, 1);
  6726. p++;
  6727. break;
  6728. case SIMD_v128_load32_zero:
  6729. case SIMD_v128_load64_zero:
  6730. /* memarg align */
  6731. skip_leb_uint32(p, p_end);
  6732. /* memarg offset*/
  6733. skip_leb_uint32(p, p_end);
  6734. break;
  6735. default:
  6736. /*
  6737. * since latest SIMD specific used almost every value
  6738. * from 0x00 to 0xff, the default branch will present
  6739. * all opcodes without imm
  6740. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6741. */
  6742. break;
  6743. }
  6744. break;
  6745. }
  6746. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6747. #endif /* end of WASM_ENABLE_SIMD */
  6748. #if WASM_ENABLE_SHARED_MEMORY != 0
  6749. case WASM_OP_ATOMIC_PREFIX:
  6750. {
  6751. uint32 opcode1;
  6752. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6753. read_leb_uint32(p, p_end, opcode1);
  6754. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6755. is no larger than UINT8_MAX */
  6756. opcode = (uint8)opcode1;
  6757. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6758. skip_leb_uint32(p, p_end); /* align */
  6759. skip_leb_uint32(p, p_end); /* offset */
  6760. }
  6761. else {
  6762. /* atomic.fence doesn't have memarg */
  6763. p++;
  6764. }
  6765. break;
  6766. }
  6767. #endif
  6768. #if WASM_ENABLE_DEBUG_INTERP != 0
  6769. case DEBUG_OP_BREAK:
  6770. {
  6771. WASMDebugInstance *debug_instance =
  6772. wasm_exec_env_get_instance(exec_env);
  6773. char orignal_opcode[1];
  6774. uint64 size = 1;
  6775. WASMModuleInstance *module_inst =
  6776. (WASMModuleInstance *)exec_env->module_inst;
  6777. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6778. ? (p - 1) - module_inst->module->load_addr
  6779. : ~0;
  6780. if (debug_instance) {
  6781. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6782. orignal_opcode, &size)
  6783. && size == 1) {
  6784. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6785. "with %02x: ",
  6786. orignal_opcode[0]);
  6787. opcode = orignal_opcode[0];
  6788. goto op_break_retry;
  6789. }
  6790. }
  6791. break;
  6792. }
  6793. #endif
  6794. default:
  6795. return false;
  6796. }
  6797. }
  6798. (void)u8;
  6799. (void)exec_env;
  6800. return false;
  6801. fail:
  6802. return false;
  6803. }
  6804. #if WASM_ENABLE_FAST_INTERP != 0
  6805. #if WASM_DEBUG_PREPROCESSOR != 0
  6806. #define LOG_OP(...) os_printf(__VA_ARGS__)
  6807. #else
  6808. #define LOG_OP(...) (void)0
  6809. #endif
  6810. #define PATCH_ELSE 0
  6811. #define PATCH_END 1
  6812. typedef struct BranchBlockPatch {
  6813. struct BranchBlockPatch *next;
  6814. uint8 patch_type;
  6815. uint8 *code_compiled;
  6816. } BranchBlockPatch;
  6817. #endif
  6818. typedef struct BranchBlock {
  6819. uint8 label_type;
  6820. BlockType block_type;
  6821. uint8 *start_addr;
  6822. uint8 *else_addr;
  6823. uint8 *end_addr;
  6824. uint32 stack_cell_num;
  6825. #if WASM_ENABLE_GC != 0
  6826. uint32 reftype_map_num;
  6827. /* Indicate which local is used inside current block, used to validate
  6828. * local.get with non-nullable ref types */
  6829. uint8 *local_use_mask;
  6830. uint32 local_use_mask_size;
  6831. #endif
  6832. #if WASM_ENABLE_FAST_INTERP != 0
  6833. uint16 dynamic_offset;
  6834. uint8 *code_compiled;
  6835. BranchBlockPatch *patch_list;
  6836. /* This is used to save params frame_offset of of if block */
  6837. int16 *param_frame_offsets;
  6838. /* This is used to recover dynamic offset for else branch */
  6839. uint16 start_dynamic_offset;
  6840. #endif
  6841. /* Indicate the operand stack is in polymorphic state.
  6842. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  6843. * to polymorphic state until the block's 'end' opcode is processed.
  6844. * If stack is in polymorphic state and stack is empty, instruction can
  6845. * pop any type of value directly without decreasing stack top pointer
  6846. * and stack cell num. */
  6847. bool is_stack_polymorphic;
  6848. } BranchBlock;
  6849. typedef struct WASMLoaderContext {
  6850. /* frame ref stack */
  6851. uint8 *frame_ref;
  6852. uint8 *frame_ref_bottom;
  6853. uint8 *frame_ref_boundary;
  6854. uint32 frame_ref_size;
  6855. uint32 stack_cell_num;
  6856. uint32 max_stack_cell_num;
  6857. #if WASM_ENABLE_GC != 0
  6858. /* frame reftype map stack */
  6859. WASMRefTypeMap *frame_reftype_map;
  6860. WASMRefTypeMap *frame_reftype_map_bottom;
  6861. WASMRefTypeMap *frame_reftype_map_boundary;
  6862. uint32 frame_reftype_map_size;
  6863. uint32 reftype_map_num;
  6864. uint32 max_reftype_map_num;
  6865. /* Current module */
  6866. WASMModule *module;
  6867. /* Current module's ref_type_set */
  6868. HashMap *ref_type_set;
  6869. /* Always point to local variable ref_type of
  6870. wasm_loader_prepare_bytecode */
  6871. WASMRefType *ref_type_tmp;
  6872. #endif
  6873. /* frame csp stack */
  6874. BranchBlock *frame_csp;
  6875. BranchBlock *frame_csp_bottom;
  6876. BranchBlock *frame_csp_boundary;
  6877. uint32 frame_csp_size;
  6878. uint32 csp_num;
  6879. uint32 max_csp_num;
  6880. #if WASM_ENABLE_FAST_INTERP != 0
  6881. /* frame offset stack */
  6882. int16 *frame_offset;
  6883. int16 *frame_offset_bottom;
  6884. int16 *frame_offset_boundary;
  6885. uint32 frame_offset_size;
  6886. int16 dynamic_offset;
  6887. int16 start_dynamic_offset;
  6888. int16 max_dynamic_offset;
  6889. /* preserved local offset */
  6890. int16 preserved_local_offset;
  6891. /* const buffer */
  6892. uint8 *const_buf;
  6893. uint16 num_const;
  6894. uint16 const_cell_num;
  6895. uint32 const_buf_size;
  6896. /* processed code */
  6897. uint8 *p_code_compiled;
  6898. uint8 *p_code_compiled_end;
  6899. uint32 code_compiled_size;
  6900. /* If the last opcode will be dropped, the peak memory usage will be larger
  6901. * than the final code_compiled_size, we record the peak size to ensure
  6902. * there will not be invalid memory access during second traverse */
  6903. uint32 code_compiled_peak_size;
  6904. #endif
  6905. } WASMLoaderContext;
  6906. typedef struct Const {
  6907. WASMValue value;
  6908. uint16 slot_index;
  6909. uint8 value_type;
  6910. } Const;
  6911. #define CHECK_CSP_PUSH() \
  6912. do { \
  6913. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  6914. MEM_REALLOC( \
  6915. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  6916. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  6917. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  6918. ctx->frame_csp_boundary = \
  6919. ctx->frame_csp_bottom \
  6920. + ctx->frame_csp_size / sizeof(BranchBlock); \
  6921. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  6922. } \
  6923. } while (0)
  6924. #define CHECK_CSP_POP() \
  6925. do { \
  6926. if (ctx->csp_num < 1) { \
  6927. set_error_buf(error_buf, error_buf_size, \
  6928. "type mismatch: " \
  6929. "expect data but block stack was empty"); \
  6930. goto fail; \
  6931. } \
  6932. } while (0)
  6933. #if WASM_ENABLE_FAST_INTERP != 0
  6934. static bool
  6935. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  6936. uint32 error_buf_size)
  6937. {
  6938. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  6939. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  6940. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  6941. ctx->frame_offset_size + 16);
  6942. ctx->frame_offset_size += 16;
  6943. ctx->frame_offset_boundary =
  6944. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  6945. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  6946. }
  6947. return true;
  6948. fail:
  6949. return false;
  6950. }
  6951. static bool
  6952. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  6953. {
  6954. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  6955. return false;
  6956. return true;
  6957. }
  6958. static void
  6959. free_label_patch_list(BranchBlock *frame_csp)
  6960. {
  6961. BranchBlockPatch *label_patch = frame_csp->patch_list;
  6962. BranchBlockPatch *next;
  6963. while (label_patch != NULL) {
  6964. next = label_patch->next;
  6965. wasm_runtime_free(label_patch);
  6966. label_patch = next;
  6967. }
  6968. frame_csp->patch_list = NULL;
  6969. }
  6970. static void
  6971. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  6972. {
  6973. BranchBlock *tmp_csp = frame_csp;
  6974. for (uint32 i = 0; i < csp_num; i++) {
  6975. free_label_patch_list(tmp_csp);
  6976. tmp_csp++;
  6977. }
  6978. }
  6979. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6980. #if WASM_ENABLE_GC != 0
  6981. static bool
  6982. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  6983. char *error_buf, uint32 error_buf_size)
  6984. {
  6985. BranchBlock *current_csp = ctx->frame_csp - 1;
  6986. uint32 local_mask_size;
  6987. if (local_count == 0) {
  6988. current_csp->local_use_mask_size = 0;
  6989. return true;
  6990. }
  6991. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  6992. * else branch, we don't need to allocate memory again */
  6993. if (!current_csp->local_use_mask) {
  6994. local_mask_size = (local_count + 7) / sizeof(uint8);
  6995. if (!(current_csp->local_use_mask =
  6996. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  6997. return false;
  6998. }
  6999. current_csp->local_use_mask_size = local_mask_size;
  7000. }
  7001. else {
  7002. local_mask_size = current_csp->local_use_mask_size;
  7003. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7004. }
  7005. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7006. /* For non-function blocks, inherit the use status from parent block */
  7007. BranchBlock *parent_csp = current_csp - 1;
  7008. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7009. bh_assert(parent_csp->local_use_mask);
  7010. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7011. parent_csp->local_use_mask, local_mask_size);
  7012. }
  7013. return true;
  7014. }
  7015. static void
  7016. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7017. {
  7018. BranchBlock *current_csp = ctx->frame_csp - 1;
  7019. bh_assert(current_csp->local_use_mask
  7020. || current_csp->local_use_mask_size == 0);
  7021. if (current_csp->local_use_mask) {
  7022. wasm_runtime_free(current_csp->local_use_mask);
  7023. }
  7024. current_csp->local_use_mask = NULL;
  7025. current_csp->local_use_mask_size = 0;
  7026. }
  7027. static void
  7028. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7029. {
  7030. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7031. uint32 i;
  7032. for (i = 0; i < ctx->csp_num; i++) {
  7033. if (tmp_csp->local_use_mask) {
  7034. wasm_runtime_free(tmp_csp->local_use_mask);
  7035. tmp_csp->local_use_mask = NULL;
  7036. tmp_csp->local_use_mask_size = 0;
  7037. }
  7038. tmp_csp++;
  7039. }
  7040. }
  7041. static void
  7042. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7043. {
  7044. BranchBlock *current_csp = ctx->frame_csp - 1;
  7045. uint32 byte_offset = index / sizeof(uint8);
  7046. uint32 bit_offset = index % sizeof(uint8);
  7047. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7048. bh_assert(current_csp->local_use_mask);
  7049. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7050. }
  7051. static bool
  7052. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7053. {
  7054. BranchBlock *current_csp = ctx->frame_csp - 1;
  7055. uint32 byte_offset = index / sizeof(uint8);
  7056. uint32 bit_offset = index % sizeof(uint8);
  7057. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7058. bh_assert(current_csp->local_use_mask);
  7059. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7060. ? true
  7061. : false;
  7062. }
  7063. #endif /* end of WASM_ENABLE_GC != 0 */
  7064. static void
  7065. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7066. {
  7067. if (ctx) {
  7068. if (ctx->frame_ref_bottom)
  7069. wasm_runtime_free(ctx->frame_ref_bottom);
  7070. #if WASM_ENABLE_GC != 0
  7071. if (ctx->frame_reftype_map_bottom)
  7072. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7073. #endif
  7074. if (ctx->frame_csp_bottom) {
  7075. #if WASM_ENABLE_FAST_INTERP != 0
  7076. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7077. #endif
  7078. #if WASM_ENABLE_GC != 0
  7079. wasm_loader_clean_all_local_use_masks(ctx);
  7080. #endif
  7081. wasm_runtime_free(ctx->frame_csp_bottom);
  7082. }
  7083. #if WASM_ENABLE_FAST_INTERP != 0
  7084. if (ctx->frame_offset_bottom)
  7085. wasm_runtime_free(ctx->frame_offset_bottom);
  7086. if (ctx->const_buf)
  7087. wasm_runtime_free(ctx->const_buf);
  7088. #endif
  7089. wasm_runtime_free(ctx);
  7090. }
  7091. }
  7092. static WASMLoaderContext *
  7093. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7094. {
  7095. WASMLoaderContext *loader_ctx =
  7096. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7097. if (!loader_ctx)
  7098. return NULL;
  7099. loader_ctx->frame_ref_size = 32;
  7100. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7101. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7102. goto fail;
  7103. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7104. #if WASM_ENABLE_GC != 0
  7105. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7106. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7107. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7108. error_buf_size)))
  7109. goto fail;
  7110. loader_ctx->frame_reftype_map_boundary =
  7111. loader_ctx->frame_reftype_map_bottom + 16;
  7112. #endif
  7113. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7114. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7115. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7116. goto fail;
  7117. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7118. #if WASM_ENABLE_EXCE_HANDLING != 0
  7119. func->exception_handler_count = 0;
  7120. #endif
  7121. #if WASM_ENABLE_FAST_INTERP != 0
  7122. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7123. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7124. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7125. error_buf_size)))
  7126. goto fail;
  7127. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7128. loader_ctx->num_const = 0;
  7129. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7130. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7131. error_buf, error_buf_size)))
  7132. goto fail;
  7133. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7134. set_error_buf(error_buf, error_buf_size,
  7135. "fast interpreter offset overflow");
  7136. goto fail;
  7137. }
  7138. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7139. loader_ctx->max_dynamic_offset =
  7140. func->param_cell_num + func->local_cell_num;
  7141. #endif
  7142. return loader_ctx;
  7143. fail:
  7144. wasm_loader_ctx_destroy(loader_ctx);
  7145. return NULL;
  7146. }
  7147. static bool
  7148. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7149. uint32 error_buf_size)
  7150. {
  7151. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7152. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7153. /* Increase the frame ref stack */
  7154. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7155. ctx->frame_ref_size + 16);
  7156. ctx->frame_ref_size += 16;
  7157. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7158. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7159. }
  7160. #if WASM_ENABLE_GC != 0
  7161. if (wasm_is_type_multi_byte_type(type)
  7162. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7163. /* Increase the frame reftype map stack */
  7164. bh_assert(
  7165. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7166. * sizeof(WASMRefTypeMap))
  7167. == ctx->frame_reftype_map_size);
  7168. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7169. ctx->frame_reftype_map_size
  7170. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7171. ctx->frame_reftype_map =
  7172. ctx->frame_reftype_map_bottom
  7173. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7174. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7175. ctx->frame_reftype_map_boundary =
  7176. ctx->frame_reftype_map_bottom
  7177. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7178. }
  7179. #endif
  7180. return true;
  7181. fail:
  7182. return false;
  7183. }
  7184. static bool
  7185. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7186. uint32 error_buf_size)
  7187. {
  7188. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7189. uint32 i;
  7190. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7191. return false;
  7192. #if WASM_ENABLE_GC != 0
  7193. if (wasm_is_type_multi_byte_type(type)) {
  7194. WASMRefType *ref_type;
  7195. if (!(ref_type =
  7196. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7197. error_buf, error_buf_size))) {
  7198. return false;
  7199. }
  7200. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7201. /* Increase the frame reftype map stack */
  7202. bh_assert((uint32)((ctx->frame_reftype_map
  7203. - ctx->frame_reftype_map_bottom)
  7204. * sizeof(WASMRefTypeMap))
  7205. == ctx->frame_reftype_map_size);
  7206. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7207. ctx->frame_reftype_map_size,
  7208. ctx->frame_reftype_map_size
  7209. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7210. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7211. + ctx->frame_reftype_map_size
  7212. / ((uint32)sizeof(WASMRefTypeMap));
  7213. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7214. ctx->frame_reftype_map_boundary =
  7215. ctx->frame_reftype_map_bottom
  7216. + ctx->frame_reftype_map_size
  7217. / ((uint32)sizeof(WASMRefTypeMap));
  7218. }
  7219. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7220. ctx->frame_reftype_map->ref_type = ref_type;
  7221. ctx->frame_reftype_map++;
  7222. ctx->reftype_map_num++;
  7223. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7224. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7225. }
  7226. #endif
  7227. for (i = 0; i < type_cell_num; i++)
  7228. *ctx->frame_ref++ = type;
  7229. ctx->stack_cell_num += type_cell_num;
  7230. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7231. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7232. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7233. set_error_buf(error_buf, error_buf_size,
  7234. "operand stack depth limit exceeded");
  7235. return false;
  7236. }
  7237. }
  7238. return true;
  7239. #if WASM_ENABLE_GC != 0
  7240. fail:
  7241. return false;
  7242. #endif
  7243. }
  7244. static bool
  7245. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7246. int32 stack_cell_num,
  7247. #if WASM_ENABLE_GC != 0
  7248. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7249. #endif
  7250. uint8 type,
  7251. #if WASM_ENABLE_GC != 0
  7252. WASMRefType *ref_type,
  7253. #endif
  7254. char *error_buf, uint32 error_buf_size)
  7255. {
  7256. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7257. #if WASM_ENABLE_GC != 0
  7258. WASMRefType *frame_reftype = NULL;
  7259. #endif
  7260. if (stack_cell_num < type_cell_num) {
  7261. set_error_buf(error_buf, error_buf_size,
  7262. "type mismatch: expect data but stack was empty");
  7263. return false;
  7264. }
  7265. #if WASM_ENABLE_GC == 0
  7266. for (i = 0; i < type_cell_num; i++) {
  7267. if (*(frame_ref - 1 - i) != type) {
  7268. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7269. "type mismatch: expect ", type2str(type),
  7270. " but got other");
  7271. return false;
  7272. }
  7273. }
  7274. #else
  7275. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7276. bh_assert(reftype_map_num > 0);
  7277. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7278. }
  7279. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7280. ref_type, ctx->module->types,
  7281. ctx->module->type_count)) {
  7282. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7283. "type mismatch: expect ", type2str(type),
  7284. " but got other");
  7285. return false;
  7286. }
  7287. for (i = 0; i < type_cell_num - 1; i++) {
  7288. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7289. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7290. "type mismatch: expect ", type2str(type),
  7291. " but got other");
  7292. return false;
  7293. }
  7294. }
  7295. #endif
  7296. return true;
  7297. }
  7298. static bool
  7299. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7300. uint32 error_buf_size)
  7301. {
  7302. int32 block_stack_cell_num =
  7303. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7304. #if WASM_ENABLE_GC != 0
  7305. int32 reftype_map_num =
  7306. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7307. #endif
  7308. if (block_stack_cell_num > 0) {
  7309. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7310. /* the stack top is a value of any type, return success */
  7311. return true;
  7312. }
  7313. #if WASM_ENABLE_GC != 0
  7314. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7315. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7316. WASMRefType *stack_top_ref_type = NULL;
  7317. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7318. bh_assert(reftype_map_num > 0);
  7319. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7320. }
  7321. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7322. ctx->ref_type_tmp, ctx->module->types,
  7323. ctx->module->type_count)) {
  7324. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7325. uint32 ref_type_struct_size =
  7326. wasm_reftype_struct_size(stack_top_ref_type);
  7327. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7328. stack_top_ref_type, ref_type_struct_size);
  7329. }
  7330. return true;
  7331. }
  7332. }
  7333. #endif
  7334. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7335. #if WASM_ENABLE_GC != 0
  7336. ctx->frame_reftype_map, reftype_map_num,
  7337. #endif
  7338. type,
  7339. #if WASM_ENABLE_GC != 0
  7340. ctx->ref_type_tmp,
  7341. #endif
  7342. error_buf, error_buf_size)) {
  7343. return false;
  7344. }
  7345. return true;
  7346. }
  7347. static bool
  7348. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7349. uint32 error_buf_size)
  7350. {
  7351. BranchBlock *cur_block = ctx->frame_csp - 1;
  7352. int32 available_stack_cell =
  7353. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7354. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7355. /* Directly return success if current block is in stack
  7356. polymorphic state while stack is empty. */
  7357. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7358. return true;
  7359. if (type == VALUE_TYPE_VOID)
  7360. return true;
  7361. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7362. return false;
  7363. bh_assert(available_stack_cell > 0);
  7364. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7365. type = VALUE_TYPE_ANY;
  7366. cell_num_to_pop = 1;
  7367. }
  7368. ctx->frame_ref -= cell_num_to_pop;
  7369. ctx->stack_cell_num -= cell_num_to_pop;
  7370. #if WASM_ENABLE_GC != 0
  7371. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7372. ctx->frame_reftype_map--;
  7373. ctx->reftype_map_num--;
  7374. }
  7375. #endif
  7376. return true;
  7377. }
  7378. #if WASM_ENABLE_GC != 0
  7379. /* Get the stack top element of current block */
  7380. static bool
  7381. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7382. WASMRefType **p_ref_type, char *error_buf,
  7383. uint32 error_buf_size)
  7384. {
  7385. BranchBlock *cur_block = ctx->frame_csp - 1;
  7386. int32 available_stack_cell =
  7387. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7388. if (available_stack_cell <= 0) {
  7389. /* Directly return success if current block is in stack
  7390. polymorphic state while stack is empty. */
  7391. if (cur_block->is_stack_polymorphic) {
  7392. *p_type = VALUE_TYPE_ANY;
  7393. return true;
  7394. }
  7395. else {
  7396. set_error_buf(
  7397. error_buf, error_buf_size,
  7398. "type mismatch: expect data but block stack was empty");
  7399. return false;
  7400. }
  7401. }
  7402. *p_type = *(ctx->frame_ref - 1);
  7403. if (wasm_is_type_multi_byte_type(*p_type)) {
  7404. int32 available_reftype_map =
  7405. (int32)(ctx->reftype_map_num
  7406. - (ctx->frame_csp - 1)->reftype_map_num);
  7407. bh_assert(available_reftype_map > 0);
  7408. (void)available_reftype_map;
  7409. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7410. }
  7411. return true;
  7412. }
  7413. #if WASM_ENABLE_FAST_INTERP != 0
  7414. static bool
  7415. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7416. char *error_buf, uint32 error_buf_size);
  7417. #endif
  7418. /* Check whether the stack top elem is a heap object, and if yes,
  7419. pop and return it */
  7420. static bool
  7421. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7422. WASMRefType *ref_ht_ret, char *error_buf,
  7423. uint32 error_buf_size)
  7424. {
  7425. uint8 type = 0;
  7426. WASMRefType *ref_type = NULL;
  7427. /* Get stack top element */
  7428. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7429. error_buf_size)) {
  7430. return false;
  7431. }
  7432. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7433. /* stack top isn't a ref type */
  7434. && !wasm_is_type_reftype(type)) {
  7435. set_error_buf(error_buf, error_buf_size,
  7436. "type mismatch: expect heap object but got others");
  7437. return false;
  7438. }
  7439. /* POP stack top */
  7440. if (wasm_is_type_multi_byte_type(type)) {
  7441. bh_assert(ref_type);
  7442. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7443. wasm_reftype_struct_size(ref_type));
  7444. }
  7445. #if WASM_ENABLE_FAST_INTERP != 0
  7446. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7447. error_buf_size)) {
  7448. return false;
  7449. }
  7450. #else
  7451. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7452. return false;
  7453. }
  7454. #endif
  7455. if (p_type)
  7456. *p_type = type;
  7457. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7458. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7459. wasm_reftype_struct_size(ref_type));
  7460. }
  7461. return true;
  7462. }
  7463. /* Check whether the stack top elem is subtype of (ref null ht),
  7464. and if yes, pop it and return the converted (ref ht) */
  7465. static bool
  7466. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7467. WASMRefType *ref_ht_ret, char *error_buf,
  7468. uint32 error_buf_size)
  7469. {
  7470. uint8 type = 0;
  7471. WASMRefType ref_type = { 0 };
  7472. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7473. error_buf_size)) {
  7474. return false;
  7475. }
  7476. /* Convert to related (ref ht) and return */
  7477. if ((type >= REF_TYPE_EQREF && type <= REF_TYPE_FUNCREF)
  7478. || (type >= REF_TYPE_NULLREF && type <= REF_TYPE_I31REF)) {
  7479. /* Return (ref func/extern/any/eq/i31/nofunc/noextern/struct/array/none)
  7480. */
  7481. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7482. HEAP_TYPE_FUNC + (type - REF_TYPE_FUNCREF));
  7483. type = ref_ht_ret->ref_type;
  7484. }
  7485. else if (wasm_is_reftype_htref_nullable(type)
  7486. || wasm_is_reftype_htref_non_nullable(type)) {
  7487. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7488. wasm_reftype_struct_size(&ref_type));
  7489. /* Convert to (ref ht) */
  7490. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7491. ref_ht_ret->ref_ht_common.nullable = false;
  7492. type = ref_ht_ret->ref_type;
  7493. }
  7494. *p_type = type;
  7495. return true;
  7496. }
  7497. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7498. and if yes, pop it and return the type_idx */
  7499. static bool
  7500. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7501. uint32 *p_type_idx, char *error_buf,
  7502. uint32 error_buf_size)
  7503. {
  7504. uint8 type = 0;
  7505. int32 type_idx = -1;
  7506. WASMRefType *ref_type = NULL;
  7507. /* Get stack top element */
  7508. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7509. error_buf_size)) {
  7510. return false;
  7511. }
  7512. if (type != VALUE_TYPE_ANY) {
  7513. /* stack top isn't (ref null $t) */
  7514. if (!((wasm_is_reftype_htref_nullable(type)
  7515. || wasm_is_reftype_htref_non_nullable(type))
  7516. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7517. set_error_buf(error_buf, error_buf_size,
  7518. "type mismatch: expect (ref null $t) but got others");
  7519. return false;
  7520. }
  7521. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7522. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7523. wasm_reftype_struct_size(ref_type));
  7524. }
  7525. /* POP stack top */
  7526. #if WASM_ENABLE_FAST_INTERP != 0
  7527. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7528. error_buf_size)) {
  7529. return false;
  7530. }
  7531. #else
  7532. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7533. return false;
  7534. }
  7535. #endif
  7536. /* Convert to type_idx and return */
  7537. *p_type = type;
  7538. if (type != VALUE_TYPE_ANY)
  7539. *p_type_idx = (uint32)type_idx;
  7540. return true;
  7541. }
  7542. #endif /* WASM_ENABLE_GC != 0 */
  7543. #if WASM_ENABLE_FAST_INTERP == 0
  7544. static bool
  7545. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7546. uint8 type_push, uint8 type_pop, char *error_buf,
  7547. uint32 error_buf_size)
  7548. {
  7549. for (int i = 0; i < pop_cnt; i++) {
  7550. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7551. error_buf_size))
  7552. return false;
  7553. }
  7554. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7555. return false;
  7556. return true;
  7557. }
  7558. #endif
  7559. static bool
  7560. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7561. BlockType block_type, uint8 *start_addr,
  7562. char *error_buf, uint32 error_buf_size)
  7563. {
  7564. CHECK_CSP_PUSH();
  7565. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7566. ctx->frame_csp->label_type = label_type;
  7567. ctx->frame_csp->block_type = block_type;
  7568. ctx->frame_csp->start_addr = start_addr;
  7569. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7570. #if WASM_ENABLE_GC != 0
  7571. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7572. #endif
  7573. #if WASM_ENABLE_FAST_INTERP != 0
  7574. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7575. ctx->frame_csp->patch_list = NULL;
  7576. #endif
  7577. ctx->frame_csp++;
  7578. ctx->csp_num++;
  7579. if (ctx->csp_num > ctx->max_csp_num) {
  7580. ctx->max_csp_num = ctx->csp_num;
  7581. if (ctx->max_csp_num > UINT16_MAX) {
  7582. set_error_buf(error_buf, error_buf_size,
  7583. "label stack depth limit exceeded");
  7584. return false;
  7585. }
  7586. }
  7587. return true;
  7588. fail:
  7589. return false;
  7590. }
  7591. static bool
  7592. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7593. uint32 error_buf_size)
  7594. {
  7595. CHECK_CSP_POP();
  7596. #if WASM_ENABLE_FAST_INTERP != 0
  7597. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7598. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7599. #endif
  7600. ctx->frame_csp--;
  7601. ctx->csp_num--;
  7602. return true;
  7603. fail:
  7604. return false;
  7605. }
  7606. #if WASM_ENABLE_FAST_INTERP != 0
  7607. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7608. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7609. #define emit_label(opcode) \
  7610. do { \
  7611. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7612. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7613. } while (0)
  7614. #define skip_label() \
  7615. do { \
  7616. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7617. LOG_OP("\ndelete last op\n"); \
  7618. } while (0)
  7619. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7620. #if UINTPTR_MAX == UINT64_MAX
  7621. #define emit_label(opcode) \
  7622. do { \
  7623. int32 offset = \
  7624. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7625. /* emit int32 relative offset in 64-bit target */ \
  7626. wasm_loader_emit_uint32(loader_ctx, offset); \
  7627. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7628. } while (0)
  7629. #else
  7630. #define emit_label(opcode) \
  7631. do { \
  7632. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7633. /* emit uint32 label address in 32-bit target */ \
  7634. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7635. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7636. } while (0)
  7637. #endif
  7638. #define skip_label() \
  7639. do { \
  7640. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7641. LOG_OP("\ndelete last op\n"); \
  7642. } while (0)
  7643. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7644. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7645. #define emit_label(opcode) \
  7646. do { \
  7647. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7648. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7649. } while (0)
  7650. #define skip_label() \
  7651. do { \
  7652. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7653. LOG_OP("\ndelete last op\n"); \
  7654. } while (0)
  7655. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7656. #define emit_empty_label_addr_and_frame_ip(type) \
  7657. do { \
  7658. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7659. loader_ctx->p_code_compiled, error_buf, \
  7660. error_buf_size)) \
  7661. goto fail; \
  7662. /* label address, to be patched */ \
  7663. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7664. } while (0)
  7665. #define emit_br_info(frame_csp) \
  7666. do { \
  7667. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  7668. error_buf_size)) \
  7669. goto fail; \
  7670. } while (0)
  7671. #define LAST_OP_OUTPUT_I32() \
  7672. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7673. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7674. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7675. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7676. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7677. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7678. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7679. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7680. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7681. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7682. || (last_op == EXT_OP_COPY_STACK_TOP)
  7683. #define LAST_OP_OUTPUT_I64() \
  7684. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7685. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7686. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7687. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7688. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7689. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7690. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7691. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7692. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7693. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7694. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7695. #define GET_CONST_OFFSET(type, val) \
  7696. do { \
  7697. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7698. &operand_offset, error_buf, \
  7699. error_buf_size))) \
  7700. goto fail; \
  7701. } while (0)
  7702. #define GET_CONST_F32_OFFSET(type, fval) \
  7703. do { \
  7704. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7705. &operand_offset, error_buf, \
  7706. error_buf_size))) \
  7707. goto fail; \
  7708. } while (0)
  7709. #define GET_CONST_F64_OFFSET(type, fval) \
  7710. do { \
  7711. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7712. &operand_offset, error_buf, \
  7713. error_buf_size))) \
  7714. goto fail; \
  7715. } while (0)
  7716. #define emit_operand(ctx, offset) \
  7717. do { \
  7718. wasm_loader_emit_int16(ctx, offset); \
  7719. LOG_OP("%d\t", offset); \
  7720. } while (0)
  7721. #define emit_byte(ctx, byte) \
  7722. do { \
  7723. wasm_loader_emit_uint8(ctx, byte); \
  7724. LOG_OP("%d\t", byte); \
  7725. } while (0)
  7726. #define emit_uint32(ctx, value) \
  7727. do { \
  7728. wasm_loader_emit_uint32(ctx, value); \
  7729. LOG_OP("%d\t", value); \
  7730. } while (0)
  7731. #define emit_uint64(ctx, value) \
  7732. do { \
  7733. wasm_loader_emit_const(ctx, &value, false); \
  7734. LOG_OP("%lld\t", value); \
  7735. } while (0)
  7736. #define emit_float32(ctx, value) \
  7737. do { \
  7738. wasm_loader_emit_const(ctx, &value, true); \
  7739. LOG_OP("%f\t", value); \
  7740. } while (0)
  7741. #define emit_float64(ctx, value) \
  7742. do { \
  7743. wasm_loader_emit_const(ctx, &value, false); \
  7744. LOG_OP("%f\t", value); \
  7745. } while (0)
  7746. static bool
  7747. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7748. {
  7749. if (!(ctx->p_code_compiled =
  7750. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7751. return false;
  7752. ctx->p_code_compiled_end =
  7753. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7754. /* clean up frame ref */
  7755. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7756. ctx->frame_ref = ctx->frame_ref_bottom;
  7757. ctx->stack_cell_num = 0;
  7758. #if WASM_ENABLE_GC != 0
  7759. /* clean up reftype map */
  7760. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7761. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7762. ctx->reftype_map_num = 0;
  7763. #endif
  7764. /* clean up frame csp */
  7765. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7766. ctx->frame_csp = ctx->frame_csp_bottom;
  7767. ctx->csp_num = 0;
  7768. ctx->max_csp_num = 0;
  7769. /* clean up frame offset */
  7770. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7771. ctx->frame_offset = ctx->frame_offset_bottom;
  7772. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7773. /* init preserved local offsets */
  7774. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7775. /* const buf is reserved */
  7776. return true;
  7777. }
  7778. static void
  7779. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  7780. {
  7781. ctx->code_compiled_size += size;
  7782. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  7783. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  7784. }
  7785. }
  7786. static void
  7787. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  7788. {
  7789. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  7790. if (ctx->p_code_compiled) {
  7791. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7792. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7793. #endif
  7794. bh_memcpy_s(ctx->p_code_compiled,
  7795. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  7796. value, size);
  7797. ctx->p_code_compiled += size;
  7798. }
  7799. else {
  7800. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7801. bh_assert((ctx->code_compiled_size & 1) == 0);
  7802. #endif
  7803. increase_compiled_code_space(ctx, size);
  7804. }
  7805. }
  7806. static void
  7807. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  7808. {
  7809. if (ctx->p_code_compiled) {
  7810. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7811. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7812. #endif
  7813. STORE_U32(ctx->p_code_compiled, value);
  7814. ctx->p_code_compiled += sizeof(uint32);
  7815. }
  7816. else {
  7817. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7818. bh_assert((ctx->code_compiled_size & 1) == 0);
  7819. #endif
  7820. increase_compiled_code_space(ctx, sizeof(uint32));
  7821. }
  7822. }
  7823. static void
  7824. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  7825. {
  7826. if (ctx->p_code_compiled) {
  7827. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7828. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7829. #endif
  7830. STORE_U16(ctx->p_code_compiled, (uint16)value);
  7831. ctx->p_code_compiled += sizeof(int16);
  7832. }
  7833. else {
  7834. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7835. bh_assert((ctx->code_compiled_size & 1) == 0);
  7836. #endif
  7837. increase_compiled_code_space(ctx, sizeof(uint16));
  7838. }
  7839. }
  7840. static void
  7841. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  7842. {
  7843. if (ctx->p_code_compiled) {
  7844. *(ctx->p_code_compiled) = value;
  7845. ctx->p_code_compiled += sizeof(uint8);
  7846. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7847. ctx->p_code_compiled++;
  7848. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7849. #endif
  7850. }
  7851. else {
  7852. increase_compiled_code_space(ctx, sizeof(uint8));
  7853. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7854. increase_compiled_code_space(ctx, sizeof(uint8));
  7855. bh_assert((ctx->code_compiled_size & 1) == 0);
  7856. #endif
  7857. }
  7858. }
  7859. static void
  7860. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  7861. {
  7862. if (ctx->p_code_compiled) {
  7863. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7864. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7865. #endif
  7866. STORE_PTR(ctx->p_code_compiled, value);
  7867. ctx->p_code_compiled += sizeof(void *);
  7868. }
  7869. else {
  7870. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7871. bh_assert((ctx->code_compiled_size & 1) == 0);
  7872. #endif
  7873. increase_compiled_code_space(ctx, sizeof(void *));
  7874. }
  7875. }
  7876. static void
  7877. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  7878. {
  7879. if (ctx->p_code_compiled) {
  7880. ctx->p_code_compiled -= size;
  7881. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7882. if (size == sizeof(uint8)) {
  7883. ctx->p_code_compiled--;
  7884. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7885. }
  7886. #endif
  7887. }
  7888. else {
  7889. ctx->code_compiled_size -= size;
  7890. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7891. if (size == sizeof(uint8)) {
  7892. ctx->code_compiled_size--;
  7893. bh_assert((ctx->code_compiled_size & 1) == 0);
  7894. }
  7895. #endif
  7896. }
  7897. }
  7898. static bool
  7899. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  7900. uint32 local_index, uint32 local_type,
  7901. bool *preserved, char *error_buf,
  7902. uint32 error_buf_size)
  7903. {
  7904. uint32 i = 0;
  7905. int16 preserved_offset = (int16)local_index;
  7906. *preserved = false;
  7907. while (i < loader_ctx->stack_cell_num) {
  7908. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  7909. /* move previous local into dynamic space before a set/tee_local opcode
  7910. */
  7911. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  7912. if (!(*preserved)) {
  7913. *preserved = true;
  7914. skip_label();
  7915. preserved_offset = loader_ctx->preserved_local_offset;
  7916. if (loader_ctx->p_code_compiled) {
  7917. bh_assert(preserved_offset != (int16)local_index);
  7918. }
  7919. if (is_32bit_type(local_type)) {
  7920. /* Only increase preserve offset in the second traversal */
  7921. if (loader_ctx->p_code_compiled)
  7922. loader_ctx->preserved_local_offset++;
  7923. emit_label(EXT_OP_COPY_STACK_TOP);
  7924. }
  7925. else {
  7926. if (loader_ctx->p_code_compiled)
  7927. loader_ctx->preserved_local_offset += 2;
  7928. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  7929. }
  7930. emit_operand(loader_ctx, local_index);
  7931. emit_operand(loader_ctx, preserved_offset);
  7932. emit_label(opcode);
  7933. }
  7934. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  7935. }
  7936. if (is_32bit_type(cur_type))
  7937. i++;
  7938. else
  7939. i += 2;
  7940. }
  7941. (void)error_buf;
  7942. (void)error_buf_size;
  7943. return true;
  7944. }
  7945. static bool
  7946. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  7947. char *error_buf, uint32 error_buf_size)
  7948. {
  7949. uint32 i = 0;
  7950. bool preserve_local;
  7951. /* preserve locals before blocks to ensure that "tee/set_local" inside
  7952. blocks will not influence the value of these locals */
  7953. while (i < loader_ctx->stack_cell_num) {
  7954. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  7955. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  7956. if ((cur_offset < loader_ctx->start_dynamic_offset)
  7957. && (cur_offset >= 0)) {
  7958. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  7959. cur_type, &preserve_local,
  7960. error_buf, error_buf_size)))
  7961. return false;
  7962. }
  7963. if (is_32bit_type(cur_type)) {
  7964. i++;
  7965. }
  7966. else {
  7967. i += 2;
  7968. }
  7969. }
  7970. return true;
  7971. }
  7972. static bool
  7973. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  7974. uint8 *p_code_compiled, char *error_buf,
  7975. uint32 error_buf_size)
  7976. {
  7977. BranchBlockPatch *patch =
  7978. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  7979. if (!patch) {
  7980. return false;
  7981. }
  7982. patch->patch_type = patch_type;
  7983. patch->code_compiled = p_code_compiled;
  7984. if (!frame_csp->patch_list) {
  7985. frame_csp->patch_list = patch;
  7986. patch->next = NULL;
  7987. }
  7988. else {
  7989. patch->next = frame_csp->patch_list;
  7990. frame_csp->patch_list = patch;
  7991. }
  7992. return true;
  7993. }
  7994. static void
  7995. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  7996. {
  7997. BranchBlock *frame_csp = ctx->frame_csp - depth;
  7998. BranchBlockPatch *node = frame_csp->patch_list;
  7999. BranchBlockPatch *node_prev = NULL, *node_next;
  8000. if (!ctx->p_code_compiled)
  8001. return;
  8002. while (node) {
  8003. node_next = node->next;
  8004. if (node->patch_type == patch_type) {
  8005. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8006. if (node_prev == NULL) {
  8007. frame_csp->patch_list = node_next;
  8008. }
  8009. else {
  8010. node_prev->next = node_next;
  8011. }
  8012. wasm_runtime_free(node);
  8013. }
  8014. else {
  8015. node_prev = node;
  8016. }
  8017. node = node_next;
  8018. }
  8019. }
  8020. static bool
  8021. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8022. char *error_buf, uint32 error_buf_size)
  8023. {
  8024. /* br info layout:
  8025. * a) arity of target block
  8026. * b) total cell num of arity values
  8027. * c) each arity value's cell num
  8028. * d) each arity value's src frame offset
  8029. * e) each arity values's dst dynamic offset
  8030. * f) branch target address
  8031. *
  8032. * Note: b-e are omitted when arity is 0 so that
  8033. * interpreter can recover the br info quickly.
  8034. */
  8035. BlockType *block_type = &frame_csp->block_type;
  8036. uint8 *types = NULL, cell;
  8037. #if WASM_ENABLE_GC != 0
  8038. WASMRefTypeMap *reftype_maps;
  8039. uint32 reftype_map_count;
  8040. #endif
  8041. uint32 arity = 0;
  8042. int32 i;
  8043. int16 *frame_offset = ctx->frame_offset;
  8044. uint16 dynamic_offset;
  8045. /* Note: loop's arity is different from if and block. loop's arity is
  8046. * its parameter count while if and block arity is result count.
  8047. */
  8048. #if WASM_ENABLE_GC == 0
  8049. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8050. arity = block_type_get_param_types(block_type, &types);
  8051. else
  8052. arity = block_type_get_result_types(block_type, &types);
  8053. #else
  8054. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8055. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8056. &reftype_map_count);
  8057. else
  8058. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8059. &reftype_map_count);
  8060. #endif
  8061. /* Part a */
  8062. emit_uint32(ctx, arity);
  8063. if (arity) {
  8064. /* Part b */
  8065. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8066. /* Part c */
  8067. for (i = (int32)arity - 1; i >= 0; i--) {
  8068. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8069. emit_byte(ctx, cell);
  8070. }
  8071. /* Part d */
  8072. for (i = (int32)arity - 1; i >= 0; i--) {
  8073. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8074. frame_offset -= cell;
  8075. emit_operand(ctx, *(int16 *)(frame_offset));
  8076. }
  8077. /* Part e */
  8078. dynamic_offset =
  8079. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8080. for (i = (int32)arity - 1; i >= 0; i--) {
  8081. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8082. dynamic_offset -= cell;
  8083. emit_operand(ctx, dynamic_offset);
  8084. }
  8085. }
  8086. /* Part f */
  8087. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8088. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8089. }
  8090. else {
  8091. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8092. error_buf, error_buf_size))
  8093. return false;
  8094. /* label address, to be patched */
  8095. wasm_loader_emit_ptr(ctx, NULL);
  8096. }
  8097. return true;
  8098. }
  8099. static bool
  8100. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8101. bool disable_emit, int16 operand_offset,
  8102. char *error_buf, uint32 error_buf_size)
  8103. {
  8104. if (type == VALUE_TYPE_VOID)
  8105. return true;
  8106. /* only check memory overflow in first traverse */
  8107. if (ctx->p_code_compiled == NULL) {
  8108. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8109. return false;
  8110. }
  8111. if (disable_emit)
  8112. *(ctx->frame_offset)++ = operand_offset;
  8113. else {
  8114. emit_operand(ctx, ctx->dynamic_offset);
  8115. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8116. ctx->dynamic_offset++;
  8117. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8118. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8119. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8120. goto fail;
  8121. }
  8122. }
  8123. }
  8124. if (is_32bit_type(type))
  8125. return true;
  8126. if (ctx->p_code_compiled == NULL) {
  8127. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8128. return false;
  8129. }
  8130. ctx->frame_offset++;
  8131. if (!disable_emit) {
  8132. ctx->dynamic_offset++;
  8133. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8134. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8135. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8136. goto fail;
  8137. }
  8138. }
  8139. }
  8140. return true;
  8141. fail:
  8142. set_error_buf(error_buf, error_buf_size,
  8143. "fast interpreter offset overflow");
  8144. return false;
  8145. }
  8146. /* This function should be in front of wasm_loader_pop_frame_ref
  8147. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8148. will be modified by wasm_loader_pop_frame_ref */
  8149. static bool
  8150. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8151. char *error_buf, uint32 error_buf_size)
  8152. {
  8153. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8154. then current block is the function block */
  8155. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8156. BranchBlock *cur_block = ctx->frame_csp - depth;
  8157. int32 available_stack_cell =
  8158. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8159. /* Directly return success if current block is in stack
  8160. * polymorphic state while stack is empty. */
  8161. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8162. return true;
  8163. if (type == VALUE_TYPE_VOID)
  8164. return true;
  8165. if (is_32bit_type(type)) {
  8166. /* Check the offset stack bottom to ensure the frame offset
  8167. stack will not go underflow. But we don't thrown error
  8168. and return true here, because the error msg should be
  8169. given in wasm_loader_pop_frame_ref */
  8170. if (!check_offset_pop(ctx, 1))
  8171. return true;
  8172. ctx->frame_offset -= 1;
  8173. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8174. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8175. ctx->dynamic_offset -= 1;
  8176. }
  8177. else {
  8178. if (!check_offset_pop(ctx, 2))
  8179. return true;
  8180. ctx->frame_offset -= 2;
  8181. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8182. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8183. ctx->dynamic_offset -= 2;
  8184. }
  8185. emit_operand(ctx, *(ctx->frame_offset));
  8186. (void)error_buf;
  8187. (void)error_buf_size;
  8188. return true;
  8189. }
  8190. static bool
  8191. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8192. bool disable_emit, int16 operand_offset,
  8193. char *error_buf, uint32 error_buf_size)
  8194. {
  8195. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8196. error_buf, error_buf_size)))
  8197. return false;
  8198. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8199. return false;
  8200. return true;
  8201. }
  8202. static bool
  8203. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8204. char *error_buf, uint32 error_buf_size)
  8205. {
  8206. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8207. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8208. return false;
  8209. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8210. return false;
  8211. return true;
  8212. }
  8213. static bool
  8214. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8215. uint8 type_push, uint8 type_pop,
  8216. bool disable_emit, int16 operand_offset,
  8217. char *error_buf, uint32 error_buf_size)
  8218. {
  8219. uint8 i;
  8220. for (i = 0; i < pop_cnt; i++) {
  8221. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8222. error_buf_size))
  8223. return false;
  8224. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8225. error_buf_size))
  8226. return false;
  8227. }
  8228. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8229. operand_offset, error_buf,
  8230. error_buf_size))
  8231. return false;
  8232. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8233. return false;
  8234. return true;
  8235. }
  8236. static bool
  8237. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8238. int16 *offset, char *error_buf,
  8239. uint32 error_buf_size)
  8240. {
  8241. int8 bytes_to_increase;
  8242. int16 operand_offset = 0;
  8243. Const *c;
  8244. /* Search existing constant */
  8245. for (c = (Const *)ctx->const_buf;
  8246. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8247. /* TODO: handle v128 type? */
  8248. if ((type == c->value_type)
  8249. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8250. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8251. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8252. || (type == VALUE_TYPE_FUNCREF
  8253. && *(int32 *)value == c->value.i32)
  8254. || (type == VALUE_TYPE_EXTERNREF
  8255. && *(int32 *)value == c->value.i32)
  8256. #endif
  8257. || (type == VALUE_TYPE_F64
  8258. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8259. || (type == VALUE_TYPE_F32
  8260. && (0
  8261. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8262. operand_offset = c->slot_index;
  8263. break;
  8264. }
  8265. if (is_32bit_type(c->value_type))
  8266. operand_offset += 1;
  8267. else
  8268. operand_offset += 2;
  8269. }
  8270. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8271. /* New constant, append to the const buffer */
  8272. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8273. bytes_to_increase = 2;
  8274. }
  8275. else {
  8276. bytes_to_increase = 1;
  8277. }
  8278. /* The max cell num of const buffer is 32768 since the valid index range
  8279. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8280. * full */
  8281. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8282. *offset = 0;
  8283. return true;
  8284. }
  8285. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8286. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8287. ctx->const_buf_size + 4 * sizeof(Const));
  8288. ctx->const_buf_size += 4 * sizeof(Const);
  8289. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8290. }
  8291. c->value_type = type;
  8292. switch (type) {
  8293. case VALUE_TYPE_F64:
  8294. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8295. sizeof(float64));
  8296. ctx->const_cell_num += 2;
  8297. /* The const buf will be reversed, we use the second cell */
  8298. /* of the i64/f64 const so the finnal offset is corrent */
  8299. operand_offset++;
  8300. break;
  8301. case VALUE_TYPE_I64:
  8302. c->value.i64 = *(int64 *)value;
  8303. ctx->const_cell_num += 2;
  8304. operand_offset++;
  8305. break;
  8306. case VALUE_TYPE_F32:
  8307. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8308. sizeof(float32));
  8309. ctx->const_cell_num++;
  8310. break;
  8311. case VALUE_TYPE_I32:
  8312. c->value.i32 = *(int32 *)value;
  8313. ctx->const_cell_num++;
  8314. break;
  8315. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8316. case VALUE_TYPE_EXTERNREF:
  8317. case VALUE_TYPE_FUNCREF:
  8318. c->value.i32 = *(int32 *)value;
  8319. ctx->const_cell_num++;
  8320. break;
  8321. #endif
  8322. default:
  8323. break;
  8324. }
  8325. c->slot_index = operand_offset;
  8326. ctx->num_const++;
  8327. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8328. (int64)c->value.i64);
  8329. }
  8330. /* use negetive index for const */
  8331. operand_offset = -(operand_offset + 1);
  8332. *offset = operand_offset;
  8333. return true;
  8334. fail:
  8335. return false;
  8336. }
  8337. /*
  8338. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8339. -- Mostly used for the binary / compare operation
  8340. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8341. -- Mostly used in block / control instructions
  8342. The POP will always emit the offset on the top of the frame_offset stack
  8343. PUSH can be used in two ways:
  8344. 1. directly PUSH:
  8345. PUSH_XXX();
  8346. will allocate a dynamic space and emit
  8347. 2. silent PUSH:
  8348. operand_offset = xxx; disable_emit = true;
  8349. PUSH_XXX();
  8350. only push the frame_offset stack, no emit
  8351. */
  8352. #define TEMPLATE_PUSH(Type) \
  8353. do { \
  8354. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8355. disable_emit, operand_offset, \
  8356. error_buf, error_buf_size)) \
  8357. goto fail; \
  8358. } while (0)
  8359. #define TEMPLATE_PUSH_REF(Type) \
  8360. do { \
  8361. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8362. operand_offset, error_buf, \
  8363. error_buf_size)) \
  8364. goto fail; \
  8365. } while (0)
  8366. #define TEMPLATE_POP(Type) \
  8367. do { \
  8368. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8369. error_buf, error_buf_size)) \
  8370. goto fail; \
  8371. } while (0)
  8372. #define TEMPLATE_POP_REF(Type) \
  8373. do { \
  8374. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8375. error_buf_size)) \
  8376. goto fail; \
  8377. } while (0)
  8378. #define PUSH_OFFSET_TYPE(type) \
  8379. do { \
  8380. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8381. operand_offset, error_buf, \
  8382. error_buf_size))) \
  8383. goto fail; \
  8384. } while (0)
  8385. #define POP_OFFSET_TYPE(type) \
  8386. do { \
  8387. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8388. error_buf_size))) \
  8389. goto fail; \
  8390. } while (0)
  8391. #define POP_AND_PUSH(type_pop, type_push) \
  8392. do { \
  8393. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8394. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8395. operand_offset, error_buf, error_buf_size))) \
  8396. goto fail; \
  8397. } while (0)
  8398. /* type of POPs should be the same */
  8399. #define POP2_AND_PUSH(type_pop, type_push) \
  8400. do { \
  8401. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8402. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8403. operand_offset, error_buf, error_buf_size))) \
  8404. goto fail; \
  8405. } while (0)
  8406. #else /* WASM_ENABLE_FAST_INTERP */
  8407. #define TEMPLATE_PUSH(Type) \
  8408. do { \
  8409. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8410. error_buf, error_buf_size))) \
  8411. goto fail; \
  8412. } while (0)
  8413. #define TEMPLATE_PUSH_REF(Type) \
  8414. do { \
  8415. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8416. error_buf_size))) \
  8417. goto fail; \
  8418. } while (0)
  8419. #define TEMPLATE_POP(Type) \
  8420. do { \
  8421. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8422. error_buf, error_buf_size))) \
  8423. goto fail; \
  8424. } while (0)
  8425. #define TEMPLATE_POP_REF(Type) \
  8426. do { \
  8427. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8428. error_buf_size))) \
  8429. goto fail; \
  8430. } while (0)
  8431. #define POP_AND_PUSH(type_pop, type_push) \
  8432. do { \
  8433. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8434. type_pop, error_buf, \
  8435. error_buf_size))) \
  8436. goto fail; \
  8437. } while (0)
  8438. /* type of POPs should be the same */
  8439. #define POP2_AND_PUSH(type_pop, type_push) \
  8440. do { \
  8441. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8442. type_pop, error_buf, \
  8443. error_buf_size))) \
  8444. goto fail; \
  8445. } while (0)
  8446. #endif /* WASM_ENABLE_FAST_INTERP */
  8447. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8448. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8449. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8450. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8451. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8452. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8453. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8454. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8455. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8456. #define POP_I32() TEMPLATE_POP(I32)
  8457. #define POP_F32() TEMPLATE_POP(F32)
  8458. #define POP_I64() TEMPLATE_POP(I64)
  8459. #define POP_F64() TEMPLATE_POP(F64)
  8460. #define POP_V128() TEMPLATE_POP(V128)
  8461. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8462. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8463. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8464. #if WASM_ENABLE_FAST_INTERP != 0
  8465. static bool
  8466. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8467. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8468. {
  8469. int16 operand_offset = 0;
  8470. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8471. : loader_ctx->frame_csp;
  8472. BlockType *block_type = &block->block_type;
  8473. uint8 *return_types = NULL;
  8474. #if WASM_ENABLE_GC != 0
  8475. WASMRefTypeMap *reftype_maps = NULL;
  8476. uint32 reftype_map_count;
  8477. #endif
  8478. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8479. int32 i = 0;
  8480. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8481. *frame_offset_org = NULL;
  8482. #if WASM_ENABLE_GC == 0
  8483. return_count = block_type_get_result_types(block_type, &return_types);
  8484. #else
  8485. return_count = block_type_get_result_types(
  8486. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8487. #endif
  8488. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8489. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8490. if (return_count == 1) {
  8491. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8492. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8493. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8494. /* insert op_copy before else opcode */
  8495. if (opcode == WASM_OP_ELSE)
  8496. skip_label();
  8497. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8498. : EXT_OP_COPY_STACK_TOP_I64);
  8499. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8500. emit_operand(loader_ctx, block->dynamic_offset);
  8501. if (opcode == WASM_OP_ELSE) {
  8502. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8503. }
  8504. else {
  8505. loader_ctx->frame_offset -= cell;
  8506. loader_ctx->dynamic_offset = block->dynamic_offset;
  8507. PUSH_OFFSET_TYPE(return_types[0]);
  8508. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8509. }
  8510. if (opcode == WASM_OP_ELSE)
  8511. emit_label(opcode);
  8512. }
  8513. return true;
  8514. }
  8515. /* Copy stack top values to block's results which are in dynamic space.
  8516. * The instruction format:
  8517. * Part a: values count
  8518. * Part b: all values total cell num
  8519. * Part c: each value's cell_num, src offset and dst offset
  8520. * Part d: each value's src offset and dst offset
  8521. * Part e: each value's dst offset
  8522. */
  8523. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8524. dynamic_offset = dynamic_offset_org =
  8525. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8526. /* First traversal to get the count of values needed to be copied. */
  8527. for (i = (int32)return_count - 1; i >= 0; i--) {
  8528. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8529. frame_offset -= cells;
  8530. dynamic_offset -= cells;
  8531. if (dynamic_offset != *frame_offset) {
  8532. value_count++;
  8533. total_cel_num += cells;
  8534. }
  8535. }
  8536. if (value_count) {
  8537. uint32 j = 0;
  8538. uint8 *emit_data = NULL, *cells = NULL;
  8539. int16 *src_offsets = NULL;
  8540. uint16 *dst_offsets = NULL;
  8541. uint64 size =
  8542. (uint64)value_count
  8543. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8544. /* Allocate memory for the emit data */
  8545. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8546. return false;
  8547. cells = emit_data;
  8548. src_offsets = (int16 *)(cells + value_count);
  8549. dst_offsets = (uint16 *)(src_offsets + value_count);
  8550. /* insert op_copy before else opcode */
  8551. if (opcode == WASM_OP_ELSE)
  8552. skip_label();
  8553. emit_label(EXT_OP_COPY_STACK_VALUES);
  8554. /* Part a) */
  8555. emit_uint32(loader_ctx, value_count);
  8556. /* Part b) */
  8557. emit_uint32(loader_ctx, total_cel_num);
  8558. /* Second traversal to get each value's cell num, src offset and dst
  8559. * offset. */
  8560. frame_offset = frame_offset_org;
  8561. dynamic_offset = dynamic_offset_org;
  8562. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8563. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8564. frame_offset -= cell;
  8565. dynamic_offset -= cell;
  8566. if (dynamic_offset != *frame_offset) {
  8567. /* cell num */
  8568. cells[j] = cell;
  8569. /* src offset */
  8570. src_offsets[j] = *frame_offset;
  8571. /* dst offset */
  8572. dst_offsets[j] = dynamic_offset;
  8573. j++;
  8574. }
  8575. if (opcode == WASM_OP_ELSE) {
  8576. *frame_offset = dynamic_offset;
  8577. }
  8578. else {
  8579. loader_ctx->frame_offset = frame_offset;
  8580. loader_ctx->dynamic_offset = dynamic_offset;
  8581. PUSH_OFFSET_TYPE(return_types[i]);
  8582. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8583. loader_ctx->frame_offset = frame_offset_org;
  8584. loader_ctx->dynamic_offset = dynamic_offset_org;
  8585. }
  8586. }
  8587. bh_assert(j == value_count);
  8588. /* Emit the cells, src_offsets and dst_offsets */
  8589. for (j = 0; j < value_count; j++)
  8590. emit_byte(loader_ctx, cells[j]);
  8591. for (j = 0; j < value_count; j++)
  8592. emit_operand(loader_ctx, src_offsets[j]);
  8593. for (j = 0; j < value_count; j++)
  8594. emit_operand(loader_ctx, dst_offsets[j]);
  8595. if (opcode == WASM_OP_ELSE)
  8596. emit_label(opcode);
  8597. wasm_runtime_free(emit_data);
  8598. }
  8599. return true;
  8600. fail:
  8601. return false;
  8602. }
  8603. #endif /* WASM_ENABLE_FAST_INTERP */
  8604. #define RESERVE_BLOCK_RET() \
  8605. do { \
  8606. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8607. error_buf_size)) \
  8608. goto fail; \
  8609. } while (0)
  8610. #define PUSH_TYPE(type) \
  8611. do { \
  8612. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8613. error_buf_size))) \
  8614. goto fail; \
  8615. } while (0)
  8616. #define POP_TYPE(type) \
  8617. do { \
  8618. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8619. error_buf_size))) \
  8620. goto fail; \
  8621. } while (0)
  8622. #if WASM_ENABLE_GC == 0
  8623. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8624. do { \
  8625. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8626. _start_addr, error_buf, \
  8627. error_buf_size)) \
  8628. goto fail; \
  8629. } while (0)
  8630. #define POP_CSP() \
  8631. do { \
  8632. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8633. goto fail; \
  8634. } while (0)
  8635. #else
  8636. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8637. do { \
  8638. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8639. _start_addr, error_buf, \
  8640. error_buf_size)) \
  8641. goto fail; \
  8642. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8643. error_buf, error_buf_size)) { \
  8644. goto fail; \
  8645. } \
  8646. } while (0)
  8647. #define POP_CSP() \
  8648. do { \
  8649. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8650. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8651. goto fail; \
  8652. } while (0)
  8653. #endif /* end of WASM_ENABLE_GC == 0 */
  8654. #if WASM_ENABLE_GC == 0
  8655. #define GET_LOCAL_REFTYPE() (void)0
  8656. #else
  8657. #define GET_LOCAL_REFTYPE() \
  8658. do { \
  8659. if (wasm_is_type_multi_byte_type(local_type)) { \
  8660. WASMRefType *_ref_type; \
  8661. if (local_idx < param_count) \
  8662. _ref_type = wasm_reftype_map_find( \
  8663. param_reftype_maps, param_reftype_map_count, local_idx); \
  8664. else \
  8665. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8666. local_reftype_map_count, \
  8667. local_idx - param_count); \
  8668. bh_assert(_ref_type); \
  8669. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8670. wasm_reftype_struct_size(_ref_type)); \
  8671. } \
  8672. } while (0)
  8673. #endif /* end of WASM_ENABLE_GC == 0 */
  8674. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8675. do { \
  8676. read_leb_uint32(p, p_end, local_idx); \
  8677. if (local_idx >= param_count + local_count) { \
  8678. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8679. goto fail; \
  8680. } \
  8681. local_type = local_idx < param_count \
  8682. ? param_types[local_idx] \
  8683. : local_types[local_idx - param_count]; \
  8684. local_offset = local_offsets[local_idx]; \
  8685. GET_LOCAL_REFTYPE(); \
  8686. } while (0)
  8687. #define CHECK_BR(depth) \
  8688. do { \
  8689. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  8690. error_buf_size)) \
  8691. goto fail; \
  8692. } while (0)
  8693. static bool
  8694. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8695. {
  8696. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8697. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8698. return false;
  8699. }
  8700. return true;
  8701. }
  8702. #define CHECK_MEMORY() \
  8703. do { \
  8704. if (!check_memory(module, error_buf, error_buf_size)) \
  8705. goto fail; \
  8706. } while (0)
  8707. static bool
  8708. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8709. uint32 error_buf_size)
  8710. {
  8711. uint8 mem_access_aligns[] = {
  8712. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8713. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8714. };
  8715. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8716. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8717. set_error_buf(error_buf, error_buf_size,
  8718. "alignment must not be larger than natural");
  8719. return false;
  8720. }
  8721. return true;
  8722. }
  8723. #if WASM_ENABLE_SIMD != 0
  8724. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8725. static bool
  8726. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8727. uint32 error_buf_size)
  8728. {
  8729. uint8 mem_access_aligns[] = {
  8730. 4, /* load */
  8731. 3, 3, 3, 3, 3, 3, /* load and extend */
  8732. 0, 1, 2, 3, /* load and splat */
  8733. 4, /* store */
  8734. };
  8735. uint8 mem_access_aligns_load_lane[] = {
  8736. 0, 1, 2, 3, /* load lane */
  8737. 0, 1, 2, 3, /* store lane */
  8738. 2, 3 /* store zero */
  8739. };
  8740. if (!((opcode <= SIMD_v128_store)
  8741. || (SIMD_v128_load8_lane <= opcode
  8742. && opcode <= SIMD_v128_load64_zero))) {
  8743. set_error_buf(error_buf, error_buf_size,
  8744. "the opcode doesn't include memarg");
  8745. return false;
  8746. }
  8747. if ((opcode <= SIMD_v128_store
  8748. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8749. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8750. && align > mem_access_aligns_load_lane[opcode
  8751. - SIMD_v128_load8_lane])) {
  8752. set_error_buf(error_buf, error_buf_size,
  8753. "alignment must not be larger than natural");
  8754. return false;
  8755. }
  8756. return true;
  8757. }
  8758. static bool
  8759. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8760. uint32 error_buf_size)
  8761. {
  8762. switch (opcode) {
  8763. case SIMD_i8x16_extract_lane_s:
  8764. case SIMD_i8x16_extract_lane_u:
  8765. case SIMD_i8x16_replace_lane:
  8766. if (lane >= 16) {
  8767. goto fail;
  8768. }
  8769. break;
  8770. case SIMD_i16x8_extract_lane_s:
  8771. case SIMD_i16x8_extract_lane_u:
  8772. case SIMD_i16x8_replace_lane:
  8773. if (lane >= 8) {
  8774. goto fail;
  8775. }
  8776. break;
  8777. case SIMD_i32x4_extract_lane:
  8778. case SIMD_i32x4_replace_lane:
  8779. case SIMD_f32x4_extract_lane:
  8780. case SIMD_f32x4_replace_lane:
  8781. if (lane >= 4) {
  8782. goto fail;
  8783. }
  8784. break;
  8785. case SIMD_i64x2_extract_lane:
  8786. case SIMD_i64x2_replace_lane:
  8787. case SIMD_f64x2_extract_lane:
  8788. case SIMD_f64x2_replace_lane:
  8789. if (lane >= 2) {
  8790. goto fail;
  8791. }
  8792. break;
  8793. case SIMD_v128_load8_lane:
  8794. case SIMD_v128_load16_lane:
  8795. case SIMD_v128_load32_lane:
  8796. case SIMD_v128_load64_lane:
  8797. case SIMD_v128_store8_lane:
  8798. case SIMD_v128_store16_lane:
  8799. case SIMD_v128_store32_lane:
  8800. case SIMD_v128_store64_lane:
  8801. case SIMD_v128_load32_zero:
  8802. case SIMD_v128_load64_zero:
  8803. {
  8804. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  8805. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  8806. goto fail;
  8807. }
  8808. break;
  8809. }
  8810. default:
  8811. goto fail;
  8812. }
  8813. return true;
  8814. fail:
  8815. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8816. return false;
  8817. }
  8818. static bool
  8819. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  8820. {
  8821. uint8 i;
  8822. for (i = 0; i != 16; ++i) {
  8823. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  8824. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8825. return false;
  8826. }
  8827. }
  8828. return true;
  8829. }
  8830. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8831. #endif /* end of WASM_ENABLE_SIMD */
  8832. #if WASM_ENABLE_SHARED_MEMORY != 0
  8833. static bool
  8834. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  8835. uint32 error_buf_size)
  8836. {
  8837. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  8838. uint8 mem_access_aligns[] = {
  8839. 2, 3, 0, 1, 0, 1, 2,
  8840. };
  8841. uint8 expect;
  8842. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  8843. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  8844. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  8845. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  8846. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  8847. }
  8848. else {
  8849. /* 7 opcodes in every group */
  8850. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  8851. }
  8852. if (align != expect) {
  8853. set_error_buf(error_buf, error_buf_size,
  8854. "alignment isn't equal to natural");
  8855. return false;
  8856. }
  8857. return true;
  8858. }
  8859. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  8860. static bool
  8861. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  8862. bool is_br_table, char *error_buf, uint32 error_buf_size)
  8863. {
  8864. BranchBlock *target_block, *cur_block;
  8865. BlockType *target_block_type;
  8866. uint8 type, *types = NULL, *frame_ref;
  8867. uint32 arity = 0;
  8868. int32 i, available_stack_cell;
  8869. uint16 cell_num;
  8870. #if WASM_ENABLE_GC != 0
  8871. WASMRefTypeMap *frame_reftype_map;
  8872. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  8873. WASMRefType *ref_type;
  8874. uint32 reftype_map_count = 0;
  8875. int32 available_reftype_map;
  8876. bool is_type_multi_byte;
  8877. #endif
  8878. if (loader_ctx->csp_num < depth + 1) {
  8879. set_error_buf(error_buf, error_buf_size,
  8880. "unknown label, "
  8881. "unexpected end of section or function");
  8882. return false;
  8883. }
  8884. cur_block = loader_ctx->frame_csp - 1;
  8885. target_block = loader_ctx->frame_csp - (depth + 1);
  8886. target_block_type = &target_block->block_type;
  8887. frame_ref = loader_ctx->frame_ref;
  8888. #if WASM_ENABLE_GC != 0
  8889. frame_reftype_map = loader_ctx->frame_reftype_map;
  8890. #endif
  8891. /* Note: loop's arity is different from if and block. loop's arity is
  8892. * its parameter count while if and block arity is result count.
  8893. */
  8894. #if WASM_ENABLE_GC == 0
  8895. if (target_block->label_type == LABEL_TYPE_LOOP)
  8896. arity = block_type_get_param_types(target_block_type, &types);
  8897. else
  8898. arity = block_type_get_result_types(target_block_type, &types);
  8899. #else
  8900. if (target_block->label_type == LABEL_TYPE_LOOP)
  8901. arity = block_type_get_param_types(target_block_type, &types,
  8902. &reftype_maps, &reftype_map_count);
  8903. else
  8904. arity = block_type_get_result_types(target_block_type, &types,
  8905. &reftype_maps, &reftype_map_count);
  8906. #endif
  8907. /* If the stack is in polymorphic state, just clear the stack
  8908. * and then re-push the values to make the stack top values
  8909. * match block type. */
  8910. if (cur_block->is_stack_polymorphic && !is_br_table) {
  8911. #if WASM_ENABLE_GC != 0
  8912. int32 j = reftype_map_count - 1;
  8913. #endif
  8914. for (i = (int32)arity - 1; i >= 0; i--) {
  8915. #if WASM_ENABLE_GC != 0
  8916. if (wasm_is_type_multi_byte_type(types[i])) {
  8917. bh_assert(reftype_maps[j].index == i);
  8918. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  8919. reftype_maps[j].ref_type,
  8920. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  8921. j--;
  8922. }
  8923. #endif
  8924. #if WASM_ENABLE_FAST_INTERP != 0
  8925. POP_OFFSET_TYPE(types[i]);
  8926. #endif
  8927. POP_TYPE(types[i]);
  8928. }
  8929. #if WASM_ENABLE_GC != 0
  8930. j = 0;
  8931. #endif
  8932. for (i = 0; i < (int32)arity; i++) {
  8933. #if WASM_ENABLE_GC != 0
  8934. if (wasm_is_type_multi_byte_type(types[i])) {
  8935. bh_assert(reftype_maps[j].index == i);
  8936. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  8937. reftype_maps[j].ref_type,
  8938. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  8939. j++;
  8940. }
  8941. #endif
  8942. #if WASM_ENABLE_FAST_INTERP != 0
  8943. bool disable_emit = true;
  8944. int16 operand_offset = 0;
  8945. PUSH_OFFSET_TYPE(types[i]);
  8946. #endif
  8947. PUSH_TYPE(types[i]);
  8948. }
  8949. return true;
  8950. }
  8951. available_stack_cell =
  8952. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  8953. #if WASM_ENABLE_GC != 0
  8954. available_reftype_map =
  8955. (int32)(loader_ctx->reftype_map_num
  8956. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  8957. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  8958. #endif
  8959. /* Check stack top values match target block type */
  8960. for (i = (int32)arity - 1; i >= 0; i--) {
  8961. type = types[i];
  8962. #if WASM_ENABLE_GC != 0
  8963. ref_type = NULL;
  8964. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  8965. if (is_type_multi_byte) {
  8966. bh_assert(reftype_map);
  8967. ref_type = reftype_map->ref_type;
  8968. }
  8969. #endif
  8970. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8971. break;
  8972. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  8973. #if WASM_ENABLE_GC != 0
  8974. frame_reftype_map, available_reftype_map,
  8975. #endif
  8976. type,
  8977. #if WASM_ENABLE_GC != 0
  8978. ref_type,
  8979. #endif
  8980. error_buf, error_buf_size)) {
  8981. return false;
  8982. }
  8983. cell_num = wasm_value_type_cell_num(types[i]);
  8984. frame_ref -= cell_num;
  8985. available_stack_cell -= cell_num;
  8986. #if WASM_ENABLE_GC != 0
  8987. if (is_type_multi_byte) {
  8988. frame_reftype_map--;
  8989. available_reftype_map--;
  8990. reftype_map--;
  8991. }
  8992. #endif
  8993. }
  8994. return true;
  8995. fail:
  8996. return false;
  8997. }
  8998. static BranchBlock *
  8999. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9000. bool is_br_table, char *error_buf, uint32 error_buf_size)
  9001. {
  9002. uint8 *p = *p_buf, *p_end = buf_end;
  9003. BranchBlock *frame_csp_tmp;
  9004. uint32 depth;
  9005. read_leb_uint32(p, p_end, depth);
  9006. if (!wasm_loader_check_br(loader_ctx, depth, is_br_table, error_buf,
  9007. error_buf_size)) {
  9008. goto fail;
  9009. }
  9010. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9011. #if WASM_ENABLE_FAST_INTERP != 0
  9012. emit_br_info(frame_csp_tmp);
  9013. #endif
  9014. *p_buf = p;
  9015. return frame_csp_tmp;
  9016. fail:
  9017. return NULL;
  9018. }
  9019. #if WASM_ENABLE_EXCE_HANDLING != 0
  9020. static BranchBlock *
  9021. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9022. uint8 *buf_end, char *error_buf,
  9023. uint32 error_buf_size)
  9024. {
  9025. uint8 *p = *p_buf, *p_end = buf_end;
  9026. BranchBlock *frame_csp_tmp;
  9027. uint32 depth;
  9028. read_leb_uint32(p, p_end, depth);
  9029. /*
  9030. * Note: "delegate 0" means the surrounding block, not the
  9031. * try-delegate block itself.
  9032. *
  9033. * Note: the caller hasn't popped the try-delegate frame yet.
  9034. */
  9035. bh_assert(loader_ctx->csp_num > 0);
  9036. if (loader_ctx->csp_num - 1 <= depth) {
  9037. #if WASM_ENABLE_SPEC_TEST == 0
  9038. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9039. #else
  9040. set_error_buf(error_buf, error_buf_size, "unknown label");
  9041. #endif
  9042. goto fail;
  9043. }
  9044. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9045. #if WASM_ENABLE_FAST_INTERP != 0
  9046. emit_br_info(frame_csp_tmp);
  9047. #endif
  9048. *p_buf = p;
  9049. return frame_csp_tmp;
  9050. fail:
  9051. return NULL;
  9052. }
  9053. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9054. static bool
  9055. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9056. char *error_buf, uint32 error_buf_size)
  9057. {
  9058. BlockType *block_type = &block->block_type;
  9059. uint8 *return_types = NULL;
  9060. uint32 return_count = 0;
  9061. int32 available_stack_cell, return_cell_num, i;
  9062. uint8 *frame_ref = NULL;
  9063. #if WASM_ENABLE_GC != 0
  9064. WASMRefTypeMap *frame_reftype_map;
  9065. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9066. WASMRefType *ref_type;
  9067. uint32 param_count, return_reftype_map_count = 0;
  9068. int32 available_reftype_map =
  9069. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9070. #endif
  9071. available_stack_cell =
  9072. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9073. #if WASM_ENABLE_GC == 0
  9074. return_count = block_type_get_result_types(block_type, &return_types);
  9075. #else
  9076. return_count = block_type_get_result_types(block_type, &return_types,
  9077. &return_reftype_maps,
  9078. &return_reftype_map_count);
  9079. param_count =
  9080. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9081. (void)param_count;
  9082. #endif
  9083. return_cell_num =
  9084. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9085. /* If the stack is in polymorphic state, just clear the stack
  9086. * and then re-push the values to make the stack top values
  9087. * match block type. */
  9088. if (block->is_stack_polymorphic) {
  9089. #if WASM_ENABLE_GC != 0
  9090. int32 j = return_reftype_map_count - 1;
  9091. #endif
  9092. for (i = (int32)return_count - 1; i >= 0; i--) {
  9093. #if WASM_ENABLE_GC != 0
  9094. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9095. bh_assert(return_reftype_maps[j].index == i + param_count);
  9096. bh_memcpy_s(
  9097. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9098. return_reftype_maps[j].ref_type,
  9099. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9100. j--;
  9101. }
  9102. #endif
  9103. #if WASM_ENABLE_FAST_INTERP != 0
  9104. POP_OFFSET_TYPE(return_types[i]);
  9105. #endif
  9106. POP_TYPE(return_types[i]);
  9107. }
  9108. /* Check stack is empty */
  9109. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9110. set_error_buf(
  9111. error_buf, error_buf_size,
  9112. "type mismatch: stack size does not match block type");
  9113. goto fail;
  9114. }
  9115. #if WASM_ENABLE_GC != 0
  9116. j = 0;
  9117. #endif
  9118. for (i = 0; i < (int32)return_count; i++) {
  9119. #if WASM_ENABLE_GC != 0
  9120. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9121. bh_assert(return_reftype_maps[j].index == i + param_count);
  9122. bh_memcpy_s(
  9123. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9124. return_reftype_maps[j].ref_type,
  9125. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9126. j++;
  9127. }
  9128. #endif
  9129. #if WASM_ENABLE_FAST_INTERP != 0
  9130. bool disable_emit = true;
  9131. int16 operand_offset = 0;
  9132. PUSH_OFFSET_TYPE(return_types[i]);
  9133. #endif
  9134. PUSH_TYPE(return_types[i]);
  9135. }
  9136. return true;
  9137. }
  9138. if (available_stack_cell != return_cell_num) {
  9139. #if WASM_ENABLE_EXCE_HANDLING != 0
  9140. /* testspec: this error message format is expected by try_catch.wast */
  9141. snprintf(
  9142. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9143. block->label_type == LABEL_TYPE_TRY
  9144. || (block->label_type == LABEL_TYPE_CATCH
  9145. && return_cell_num > 0)
  9146. ? "instruction"
  9147. : "block",
  9148. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9149. " but stack has ",
  9150. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9151. : "");
  9152. goto fail;
  9153. #else
  9154. set_error_buf(error_buf, error_buf_size,
  9155. "type mismatch: stack size does not match block type");
  9156. goto fail;
  9157. #endif
  9158. }
  9159. /* Check stack values match return types */
  9160. frame_ref = loader_ctx->frame_ref;
  9161. #if WASM_ENABLE_GC != 0
  9162. frame_reftype_map = loader_ctx->frame_reftype_map;
  9163. return_reftype_map =
  9164. return_reftype_map_count
  9165. ? return_reftype_maps + return_reftype_map_count - 1
  9166. : NULL;
  9167. #endif
  9168. for (i = (int32)return_count - 1; i >= 0; i--) {
  9169. uint8 type = return_types[i];
  9170. #if WASM_ENABLE_GC != 0
  9171. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9172. ref_type = NULL;
  9173. if (is_type_multi_byte) {
  9174. bh_assert(return_reftype_map);
  9175. ref_type = return_reftype_map->ref_type;
  9176. }
  9177. #endif
  9178. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9179. #if WASM_ENABLE_GC != 0
  9180. frame_reftype_map, available_reftype_map,
  9181. #endif
  9182. type,
  9183. #if WASM_ENABLE_GC != 0
  9184. ref_type,
  9185. #endif
  9186. error_buf, error_buf_size))
  9187. return false;
  9188. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9189. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9190. #if WASM_ENABLE_GC != 0
  9191. if (is_type_multi_byte) {
  9192. frame_reftype_map--;
  9193. available_reftype_map--;
  9194. return_reftype_map--;
  9195. }
  9196. #endif
  9197. }
  9198. return true;
  9199. fail:
  9200. return false;
  9201. }
  9202. #if WASM_ENABLE_FAST_INTERP != 0
  9203. /* Copy parameters to dynamic space.
  9204. * 1) POP original parameter out;
  9205. * 2) Push and copy original values to dynamic space.
  9206. * The copy instruction format:
  9207. * Part a: param count
  9208. * Part b: all param total cell num
  9209. * Part c: each param's cell_num, src offset and dst offset
  9210. * Part d: each param's src offset
  9211. * Part e: each param's dst offset
  9212. */
  9213. static bool
  9214. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  9215. char *error_buf, uint32 error_buf_size)
  9216. {
  9217. bool ret = false;
  9218. int16 *frame_offset = NULL;
  9219. uint8 *cells = NULL, cell;
  9220. int16 *src_offsets = NULL;
  9221. uint8 *emit_data = NULL;
  9222. uint32 i;
  9223. BranchBlock *block = loader_ctx->frame_csp - 1;
  9224. BlockType *block_type = &block->block_type;
  9225. WASMFuncType *wasm_type = block_type->u.type;
  9226. uint32 param_count = block_type->u.type->param_count;
  9227. int16 condition_offset = 0;
  9228. bool disable_emit = false;
  9229. int16 operand_offset = 0;
  9230. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9231. bh_assert(size > 0);
  9232. /* For if block, we also need copy the condition operand offset. */
  9233. if (is_if_block)
  9234. size += sizeof(*cells) + sizeof(*src_offsets);
  9235. /* Allocate memory for the emit data */
  9236. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9237. return false;
  9238. cells = emit_data;
  9239. src_offsets = (int16 *)(cells + param_count);
  9240. if (is_if_block)
  9241. condition_offset = *loader_ctx->frame_offset;
  9242. /* POP original parameter out */
  9243. for (i = 0; i < param_count; i++) {
  9244. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9245. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9246. }
  9247. frame_offset = loader_ctx->frame_offset;
  9248. /* Get each param's cell num and src offset */
  9249. for (i = 0; i < param_count; i++) {
  9250. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9251. cells[i] = cell;
  9252. src_offsets[i] = *frame_offset;
  9253. frame_offset += cell;
  9254. }
  9255. /* emit copy instruction */
  9256. emit_label(EXT_OP_COPY_STACK_VALUES);
  9257. /* Part a) */
  9258. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9259. /* Part b) */
  9260. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9261. : wasm_type->param_cell_num);
  9262. /* Part c) */
  9263. for (i = 0; i < param_count; i++)
  9264. emit_byte(loader_ctx, cells[i]);
  9265. if (is_if_block)
  9266. emit_byte(loader_ctx, 1);
  9267. /* Part d) */
  9268. for (i = 0; i < param_count; i++)
  9269. emit_operand(loader_ctx, src_offsets[i]);
  9270. if (is_if_block)
  9271. emit_operand(loader_ctx, condition_offset);
  9272. /* Part e) */
  9273. /* Push to dynamic space. The push will emit the dst offset. */
  9274. for (i = 0; i < param_count; i++)
  9275. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9276. if (is_if_block)
  9277. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9278. ret = true;
  9279. fail:
  9280. /* Free the emit data */
  9281. wasm_runtime_free(emit_data);
  9282. return ret;
  9283. }
  9284. #endif
  9285. #if WASM_ENABLE_GC == 0
  9286. #define RESET_REFTYPE_MAP_STACK() (void)0
  9287. #else
  9288. #define RESET_REFTYPE_MAP_STACK() \
  9289. do { \
  9290. loader_ctx->reftype_map_num = \
  9291. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9292. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9293. + loader_ctx->reftype_map_num; \
  9294. } while (0)
  9295. #endif
  9296. /* reset the stack to the state of before entering the last block */
  9297. #if WASM_ENABLE_FAST_INTERP != 0
  9298. #define RESET_STACK() \
  9299. do { \
  9300. loader_ctx->stack_cell_num = \
  9301. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9302. loader_ctx->frame_ref = \
  9303. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9304. loader_ctx->frame_offset = \
  9305. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9306. RESET_REFTYPE_MAP_STACK(); \
  9307. } while (0)
  9308. #else
  9309. #define RESET_STACK() \
  9310. do { \
  9311. loader_ctx->stack_cell_num = \
  9312. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9313. loader_ctx->frame_ref = \
  9314. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9315. RESET_REFTYPE_MAP_STACK(); \
  9316. } while (0)
  9317. #endif
  9318. /* set current block's stack polymorphic state */
  9319. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9320. do { \
  9321. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9322. _cur_block->is_stack_polymorphic = flag; \
  9323. } while (0)
  9324. #define BLOCK_HAS_PARAM(block_type) \
  9325. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9326. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9327. do { \
  9328. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9329. error_buf_size))) { \
  9330. goto fail; \
  9331. } \
  9332. } while (0)
  9333. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9334. static bool
  9335. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9336. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9337. uint32 error_buf_size)
  9338. {
  9339. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9340. return false;
  9341. }
  9342. if (table_idx < module->import_table_count) {
  9343. if (p_elem_type)
  9344. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  9345. #if WASM_ENABLE_GC != 0
  9346. if (p_ref_type)
  9347. *((WASMRefType **)p_ref_type) =
  9348. module->import_tables[table_idx].u.table.elem_ref_type;
  9349. #endif
  9350. }
  9351. else {
  9352. if (p_elem_type)
  9353. *p_elem_type =
  9354. module->tables[module->import_table_count + table_idx]
  9355. .elem_type;
  9356. #if WASM_ENABLE_GC != 0
  9357. if (p_ref_type)
  9358. *((WASMRefType **)p_ref_type) =
  9359. module->tables[module->import_table_count + table_idx]
  9360. .elem_ref_type;
  9361. #endif
  9362. }
  9363. return true;
  9364. }
  9365. static bool
  9366. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9367. uint8 *p_elem_type, void **p_elem_ref_type,
  9368. char *error_buf, uint32 error_buf_size)
  9369. {
  9370. if (table_seg_idx >= module->table_seg_count) {
  9371. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9372. table_seg_idx);
  9373. return false;
  9374. }
  9375. if (p_elem_type) {
  9376. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9377. }
  9378. #if WASM_ENABLE_GC != 0
  9379. if (p_elem_ref_type)
  9380. *((WASMRefType **)p_elem_ref_type) =
  9381. module->table_segments[table_seg_idx].elem_ref_type;
  9382. #endif
  9383. return true;
  9384. }
  9385. #endif
  9386. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9387. const uint8 *
  9388. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9389. uint32 *len)
  9390. {
  9391. WASMCustomSection *section = module->custom_section_list;
  9392. while (section) {
  9393. if ((section->name_len == strlen(name))
  9394. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9395. if (len) {
  9396. *len = section->content_len;
  9397. }
  9398. return section->content_addr;
  9399. }
  9400. section = section->next;
  9401. }
  9402. return NULL;
  9403. }
  9404. #endif
  9405. static bool
  9406. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9407. uint32 cur_func_idx, char *error_buf,
  9408. uint32 error_buf_size)
  9409. {
  9410. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9411. uint32 param_count, local_count, global_count;
  9412. uint8 *param_types, *local_types, local_type, global_type;
  9413. BlockType func_block_type;
  9414. uint16 *local_offsets, local_offset;
  9415. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9416. uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
  9417. int32 i32_const = 0;
  9418. int64 i64_const;
  9419. uint8 opcode;
  9420. bool return_value = false;
  9421. WASMLoaderContext *loader_ctx;
  9422. BranchBlock *frame_csp_tmp;
  9423. #if WASM_ENABLE_GC != 0
  9424. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9425. uint32 param_reftype_map_count, local_reftype_map_count;
  9426. int32 heap_type;
  9427. WASMRefType wasm_ref_type = { 0 };
  9428. bool need_ref_type_map;
  9429. #endif
  9430. #if WASM_ENABLE_FAST_INTERP != 0
  9431. uint8 *func_const_end, *func_const = NULL;
  9432. int16 operand_offset = 0;
  9433. uint8 last_op = 0;
  9434. bool disable_emit, preserve_local = false, if_condition_available = true;
  9435. float32 f32_const;
  9436. float64 f64_const;
  9437. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9438. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9439. #endif
  9440. global_count = module->import_global_count + module->global_count;
  9441. param_count = func->func_type->param_count;
  9442. param_types = func->func_type->types;
  9443. func_block_type.is_value_type = false;
  9444. func_block_type.u.type = func->func_type;
  9445. local_count = func->local_count;
  9446. local_types = func->local_types;
  9447. local_offsets = func->local_offsets;
  9448. #if WASM_ENABLE_GC != 0
  9449. param_reftype_maps = func->func_type->ref_type_maps;
  9450. param_reftype_map_count = func->func_type->ref_type_map_count;
  9451. local_reftype_maps = func->local_ref_type_maps;
  9452. local_reftype_map_count = func->local_ref_type_map_count;
  9453. #endif
  9454. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9455. goto fail;
  9456. }
  9457. #if WASM_ENABLE_GC != 0
  9458. loader_ctx->module = module;
  9459. loader_ctx->ref_type_set = module->ref_type_set;
  9460. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9461. #endif
  9462. #if WASM_ENABLE_FAST_INTERP != 0
  9463. /* For the first traverse, the initial value of preserved_local_offset has
  9464. * not been determined, we use the INT16_MAX to represent that a slot has
  9465. * been copied to preserve space. For second traverse, this field will be
  9466. * set to the appropriate value in wasm_loader_ctx_reinit.
  9467. * This is for Issue #1230,
  9468. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9469. * drop opcodes need to know which slots are preserved, so those slots will
  9470. * not be treated as dynamically allocated slots */
  9471. loader_ctx->preserved_local_offset = INT16_MAX;
  9472. re_scan:
  9473. if (loader_ctx->code_compiled_size > 0) {
  9474. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9475. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9476. goto fail;
  9477. }
  9478. p = func->code;
  9479. func->code_compiled = loader_ctx->p_code_compiled;
  9480. func->code_compiled_size = loader_ctx->code_compiled_size;
  9481. }
  9482. #endif
  9483. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9484. while (p < p_end) {
  9485. opcode = *p++;
  9486. #if WASM_ENABLE_FAST_INTERP != 0
  9487. p_org = p;
  9488. disable_emit = false;
  9489. emit_label(opcode);
  9490. #endif
  9491. switch (opcode) {
  9492. case WASM_OP_UNREACHABLE:
  9493. RESET_STACK();
  9494. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9495. break;
  9496. case WASM_OP_NOP:
  9497. #if WASM_ENABLE_FAST_INTERP != 0
  9498. skip_label();
  9499. #endif
  9500. break;
  9501. case WASM_OP_IF:
  9502. {
  9503. #if WASM_ENABLE_FAST_INTERP != 0
  9504. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9505. int32 available_stack_cell =
  9506. (int32)(loader_ctx->stack_cell_num
  9507. - parent_block->stack_cell_num);
  9508. if (available_stack_cell <= 0
  9509. && parent_block->is_stack_polymorphic)
  9510. if_condition_available = false;
  9511. else
  9512. if_condition_available = true;
  9513. PRESERVE_LOCAL_FOR_BLOCK();
  9514. #endif
  9515. #if WASM_ENABLE_GC == 0
  9516. POP_I32();
  9517. #endif
  9518. goto handle_op_block_and_loop;
  9519. }
  9520. case WASM_OP_BLOCK:
  9521. case WASM_OP_LOOP:
  9522. #if WASM_ENABLE_EXCE_HANDLING != 0
  9523. case WASM_OP_TRY:
  9524. if (opcode == WASM_OP_TRY) {
  9525. /*
  9526. * keep track of exception handlers to account for
  9527. * memory allocation
  9528. */
  9529. func->exception_handler_count++;
  9530. /*
  9531. * try is a block
  9532. * do nothing special, but execution continues to
  9533. * to handle_op_block_and_loop,
  9534. * and that be pushes the csp
  9535. */
  9536. }
  9537. #endif
  9538. #if WASM_ENABLE_FAST_INTERP != 0
  9539. PRESERVE_LOCAL_FOR_BLOCK();
  9540. #endif
  9541. handle_op_block_and_loop:
  9542. {
  9543. uint8 value_type;
  9544. BlockType block_type;
  9545. #if WASM_ENABLE_FAST_INTERP != 0
  9546. uint32 available_params = 0;
  9547. #endif
  9548. CHECK_BUF(p, p_end, 1);
  9549. value_type = read_uint8(p);
  9550. if (is_byte_a_type(value_type)) {
  9551. /* If the first byte is one of these special values:
  9552. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9553. * the single return value. */
  9554. block_type.is_value_type = true;
  9555. block_type.u.value_type.type = value_type;
  9556. #if WASM_ENABLE_WAMR_COMPILER != 0
  9557. if (value_type == VALUE_TYPE_V128)
  9558. module->is_simd_used = true;
  9559. else if (value_type == VALUE_TYPE_FUNCREF
  9560. || value_type == VALUE_TYPE_EXTERNREF)
  9561. module->is_ref_types_used = true;
  9562. #endif
  9563. #if WASM_ENABLE_GC != 0
  9564. if (value_type != VALUE_TYPE_VOID) {
  9565. p_org = p;
  9566. p--;
  9567. if (!resolve_value_type((const uint8 **)&p, p_end,
  9568. module, &need_ref_type_map,
  9569. &wasm_ref_type, false,
  9570. error_buf, error_buf_size)) {
  9571. goto fail;
  9572. }
  9573. if (need_ref_type_map) {
  9574. block_type.u.value_type.ref_type_map.index = 0;
  9575. if (!(block_type.u.value_type.ref_type_map
  9576. .ref_type = reftype_set_insert(
  9577. module->ref_type_set, &wasm_ref_type,
  9578. error_buf, error_buf_size))) {
  9579. goto fail;
  9580. }
  9581. }
  9582. /* Set again as the type might be changed, e.g.
  9583. (ref null any) to anyref */
  9584. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9585. #if WASM_ENABLE_FAST_INTERP == 0
  9586. while (p_org < p) {
  9587. #if WASM_ENABLE_DEBUG_INTERP != 0
  9588. if (!record_fast_op(module, p_org, *p_org,
  9589. error_buf, error_buf_size)) {
  9590. goto fail;
  9591. }
  9592. #endif
  9593. /* Ignore extra bytes for interpreter */
  9594. *p_org++ = WASM_OP_NOP;
  9595. }
  9596. #endif
  9597. }
  9598. #endif /* end of WASM_ENABLE_GC != 0 */
  9599. }
  9600. else {
  9601. uint32 type_index;
  9602. /* Resolve the leb128 encoded type index as block type */
  9603. p--;
  9604. p_org = p - 1;
  9605. read_leb_uint32(p, p_end, type_index);
  9606. if (type_index >= module->type_count) {
  9607. set_error_buf(error_buf, error_buf_size,
  9608. "unknown type");
  9609. goto fail;
  9610. }
  9611. block_type.is_value_type = false;
  9612. block_type.u.type =
  9613. (WASMFuncType *)module->types[type_index];
  9614. #if WASM_ENABLE_FAST_INTERP == 0
  9615. /* If block use type index as block type, change the opcode
  9616. * to new extended opcode so that interpreter can resolve
  9617. * the block quickly.
  9618. */
  9619. #if WASM_ENABLE_DEBUG_INTERP != 0
  9620. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9621. error_buf_size)) {
  9622. goto fail;
  9623. }
  9624. #endif
  9625. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9626. #endif
  9627. }
  9628. #if WASM_ENABLE_GC != 0
  9629. if (opcode == WASM_OP_IF) {
  9630. POP_I32();
  9631. }
  9632. #endif
  9633. /* Pop block parameters from stack */
  9634. if (BLOCK_HAS_PARAM(block_type)) {
  9635. WASMFuncType *wasm_type = block_type.u.type;
  9636. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9637. #if WASM_ENABLE_FAST_INTERP != 0
  9638. uint32 cell_num;
  9639. available_params = block_type.u.type->param_count;
  9640. #endif
  9641. for (i = 0; i < block_type.u.type->param_count; i++) {
  9642. int32 available_stack_cell =
  9643. (int32)(loader_ctx->stack_cell_num
  9644. - cur_block->stack_cell_num);
  9645. if (available_stack_cell <= 0
  9646. && cur_block->is_stack_polymorphic) {
  9647. #if WASM_ENABLE_FAST_INTERP != 0
  9648. available_params = i;
  9649. #endif
  9650. break;
  9651. }
  9652. POP_TYPE(
  9653. wasm_type->types[wasm_type->param_count - i - 1]);
  9654. #if WASM_ENABLE_FAST_INTERP != 0
  9655. /* decrease the frame_offset pointer accordingly to keep
  9656. * consistent with frame_ref stack */
  9657. cell_num = wasm_value_type_cell_num(
  9658. wasm_type->types[wasm_type->param_count - i - 1]);
  9659. loader_ctx->frame_offset -= cell_num;
  9660. #endif
  9661. }
  9662. }
  9663. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  9664. block_type, p);
  9665. /* Pass parameters to block */
  9666. if (BLOCK_HAS_PARAM(block_type)) {
  9667. for (i = 0; i < block_type.u.type->param_count; i++) {
  9668. #if WASM_ENABLE_FAST_INTERP != 0
  9669. uint32 cell_num = wasm_value_type_cell_num(
  9670. block_type.u.type->types[i]);
  9671. if (i >= available_params) {
  9672. /* If there isn't enough data on stack, push a dummy
  9673. * offset to keep the stack consistent with
  9674. * frame_ref.
  9675. * Since the stack is already in polymorphic state,
  9676. * the opcode will not be executed, so the dummy
  9677. * offset won't cause any error */
  9678. *loader_ctx->frame_offset++ = 0;
  9679. if (cell_num > 1) {
  9680. *loader_ctx->frame_offset++ = 0;
  9681. }
  9682. }
  9683. else {
  9684. loader_ctx->frame_offset += cell_num;
  9685. }
  9686. #endif
  9687. PUSH_TYPE(block_type.u.type->types[i]);
  9688. }
  9689. }
  9690. #if WASM_ENABLE_FAST_INTERP != 0
  9691. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  9692. skip_label();
  9693. if (BLOCK_HAS_PARAM(block_type)) {
  9694. /* Make sure params are in dynamic space */
  9695. if (!copy_params_to_dynamic_space(
  9696. loader_ctx, false, error_buf, error_buf_size))
  9697. goto fail;
  9698. }
  9699. if (opcode == WASM_OP_LOOP) {
  9700. (loader_ctx->frame_csp - 1)->code_compiled =
  9701. loader_ctx->p_code_compiled;
  9702. }
  9703. }
  9704. #if WASM_ENABLE_EXCE_HANDLING != 0
  9705. else if (opcode == WASM_OP_TRY) {
  9706. skip_label();
  9707. }
  9708. #endif
  9709. else if (opcode == WASM_OP_IF) {
  9710. BranchBlock *block = loader_ctx->frame_csp - 1;
  9711. /* If block has parameters, we should make sure they are in
  9712. * dynamic space. Otherwise, when else branch is missing,
  9713. * the later opcode may consume incorrect operand offset.
  9714. * Spec case:
  9715. * (func (export "params-id") (param i32) (result i32)
  9716. * (i32.const 1)
  9717. * (i32.const 2)
  9718. * (if (param i32 i32) (result i32 i32) (local.get 0)
  9719. * (then)) (i32.add)
  9720. * )
  9721. *
  9722. * So we should emit a copy instruction before the if.
  9723. *
  9724. * And we also need to save the parameter offsets and
  9725. * recover them before entering else branch.
  9726. *
  9727. */
  9728. if (BLOCK_HAS_PARAM(block_type)) {
  9729. uint64 size;
  9730. /* In polymorphic state, there may be no if condition on
  9731. * the stack, so the offset may not emitted */
  9732. if (if_condition_available) {
  9733. /* skip the if condition operand offset */
  9734. wasm_loader_emit_backspace(loader_ctx,
  9735. sizeof(int16));
  9736. }
  9737. /* skip the if label */
  9738. skip_label();
  9739. /* Emit a copy instruction */
  9740. if (!copy_params_to_dynamic_space(
  9741. loader_ctx, true, error_buf, error_buf_size))
  9742. goto fail;
  9743. /* Emit the if instruction */
  9744. emit_label(opcode);
  9745. /* Emit the new condition operand offset */
  9746. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  9747. /* Save top param_count values of frame_offset stack, so
  9748. * that we can recover it before executing else branch
  9749. */
  9750. size = sizeof(int16)
  9751. * (uint64)block_type.u.type->param_cell_num;
  9752. if (!(block->param_frame_offsets = loader_malloc(
  9753. size, error_buf, error_buf_size)))
  9754. goto fail;
  9755. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  9756. loader_ctx->frame_offset
  9757. - size / sizeof(int16),
  9758. (uint32)size);
  9759. }
  9760. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9761. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  9762. emit_empty_label_addr_and_frame_ip(PATCH_END);
  9763. }
  9764. #endif
  9765. break;
  9766. }
  9767. #if WASM_ENABLE_EXCE_HANDLING != 0
  9768. case WASM_OP_THROW:
  9769. {
  9770. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9771. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9772. uint8 label_type = cur_block->label_type;
  9773. uint32 tag_index = 0;
  9774. read_leb_int32(p, p_end, tag_index);
  9775. /* check validity of tag_index against module->tag_count */
  9776. /* check tag index is within the tag index space */
  9777. if (tag_index >= module->import_tag_count + module->tag_count) {
  9778. snprintf(error_buf, error_buf_size, "unknown tag %d",
  9779. tag_index);
  9780. goto fail;
  9781. }
  9782. /* the tag_type is stored in either the WASMTag (section tags)
  9783. * or WASMTagImport (import tag) */
  9784. WASMType *tag_type = NULL;
  9785. if (tag_index < module->import_tag_count) {
  9786. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  9787. }
  9788. else {
  9789. tag_type =
  9790. module->tags[tag_index - module->import_tag_count]
  9791. ->tag_type;
  9792. }
  9793. if (tag_type->result_count != 0) {
  9794. set_error_buf(error_buf, error_buf_size,
  9795. "tag type signature does not return void");
  9796. goto fail;
  9797. }
  9798. int32 available_stack_cell =
  9799. (int32)(loader_ctx->stack_cell_num
  9800. - cur_block->stack_cell_num);
  9801. /* Check stack values match return types by comparing tag param
  9802. * types with stack cells */
  9803. uint8 *frame_ref = loader_ctx->frame_ref;
  9804. for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
  9805. tti--) {
  9806. if (!check_stack_top_values(
  9807. loader_ctx, frame_ref, available_stack_cell,
  9808. tag_type->types[tti], error_buf, error_buf_size)) {
  9809. snprintf(error_buf, error_buf_size,
  9810. "type mismatch: instruction requires [%s] but "
  9811. "stack has [%s]",
  9812. tag_type->param_count > 0
  9813. ? type2str(tag_type->types[tti])
  9814. : "",
  9815. available_stack_cell > 0
  9816. ? type2str(*(loader_ctx->frame_ref - 1))
  9817. : "");
  9818. goto fail;
  9819. }
  9820. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  9821. available_stack_cell -=
  9822. wasm_value_type_cell_num(tag_type->types[tti]);
  9823. }
  9824. /* throw is stack polymorphic */
  9825. (void)label_type;
  9826. RESET_STACK();
  9827. break;
  9828. }
  9829. case WASM_OP_RETHROW:
  9830. {
  9831. /* must be done before checking branch block */
  9832. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9833. /* check the target catching block: LABEL_TYPE_CATCH */
  9834. if (!(frame_csp_tmp =
  9835. check_branch_block(loader_ctx, &p, p_end, false,
  9836. error_buf, error_buf_size)))
  9837. goto fail;
  9838. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  9839. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  9840. /* trap according to spectest (rethrow.wast) */
  9841. set_error_buf(error_buf, error_buf_size,
  9842. "invalid rethrow label");
  9843. goto fail;
  9844. }
  9845. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9846. uint8 label_type = cur_block->label_type;
  9847. (void)label_type;
  9848. /* rethrow is stack polymorphic */
  9849. RESET_STACK();
  9850. break;
  9851. }
  9852. case WASM_OP_DELEGATE:
  9853. {
  9854. /* check target block is valid */
  9855. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  9856. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  9857. goto fail;
  9858. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9859. uint8 label_type = cur_block->label_type;
  9860. (void)label_type;
  9861. /* DELEGATE ends the block */
  9862. POP_CSP();
  9863. break;
  9864. }
  9865. case WASM_OP_CATCH:
  9866. {
  9867. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9868. uint8 label_type = cur_block->label_type;
  9869. uint32 tag_index = 0;
  9870. read_leb_int32(p, p_end, tag_index);
  9871. /* check validity of tag_index against module->tag_count */
  9872. /* check tag index is within the tag index space */
  9873. if (tag_index >= module->import_tag_count + module->tag_count) {
  9874. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  9875. __FUNCTION__);
  9876. set_error_buf(error_buf, error_buf_size, "unknown tag");
  9877. goto fail;
  9878. }
  9879. /* the tag_type is stored in either the WASMTag (section tags)
  9880. * or WASMTagImport (import tag) */
  9881. WASMType *func_type = NULL;
  9882. if (tag_index < module->import_tag_count) {
  9883. func_type = module->import_tags[tag_index].u.tag.tag_type;
  9884. }
  9885. else {
  9886. func_type =
  9887. module->tags[tag_index - module->import_tag_count]
  9888. ->tag_type;
  9889. }
  9890. if (func_type->result_count != 0) {
  9891. set_error_buf(error_buf, error_buf_size,
  9892. "tag type signature does not return void");
  9893. goto fail;
  9894. }
  9895. /* check validity of current label (expect LABEL_TYPE_TRY or
  9896. * LABEL_TYPE_CATCH) */
  9897. if ((LABEL_TYPE_CATCH != label_type)
  9898. && (LABEL_TYPE_TRY != label_type)) {
  9899. set_error_buf(error_buf, error_buf_size,
  9900. "Unexpected block sequence encountered.");
  9901. goto fail;
  9902. }
  9903. BlockType new_block_type;
  9904. new_block_type.is_value_type = false;
  9905. new_block_type.u.type = func_type;
  9906. /*
  9907. * replace frame_csp by LABEL_TYPE_CATCH
  9908. */
  9909. cur_block->label_type = LABEL_TYPE_CATCH;
  9910. /* RESET_STACK removes the values pushed in TRY or pervious
  9911. * CATCH Blocks */
  9912. RESET_STACK();
  9913. /* push types on the stack according to catched type */
  9914. if (BLOCK_HAS_PARAM(new_block_type)) {
  9915. for (i = 0; i < new_block_type.u.type->param_count; i++)
  9916. PUSH_TYPE(new_block_type.u.type->types[i]);
  9917. }
  9918. break;
  9919. }
  9920. case WASM_OP_CATCH_ALL:
  9921. {
  9922. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9923. /* expecting a TRY or CATCH, anything else will be considered an
  9924. * error */
  9925. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  9926. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  9927. set_error_buf(error_buf, error_buf_size,
  9928. "Unexpected block sequence encountered.");
  9929. goto fail;
  9930. }
  9931. /* no immediates */
  9932. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  9933. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  9934. /* RESET_STACK removes the values pushed in TRY or pervious
  9935. * CATCH Blocks */
  9936. RESET_STACK();
  9937. /* catch_all has no tagtype and therefore no parameters */
  9938. break;
  9939. }
  9940. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9941. case WASM_OP_ELSE:
  9942. {
  9943. BranchBlock *block = NULL;
  9944. BlockType block_type;
  9945. if (loader_ctx->csp_num < 2
  9946. || (loader_ctx->frame_csp - 1)->label_type
  9947. != LABEL_TYPE_IF) {
  9948. set_error_buf(
  9949. error_buf, error_buf_size,
  9950. "opcode else found without matched opcode if");
  9951. goto fail;
  9952. }
  9953. block = loader_ctx->frame_csp - 1;
  9954. /* check whether if branch's stack matches its result type */
  9955. if (!check_block_stack(loader_ctx, block, error_buf,
  9956. error_buf_size))
  9957. goto fail;
  9958. block->else_addr = p - 1;
  9959. block_type = block->block_type;
  9960. #if WASM_ENABLE_GC != 0
  9961. if (!wasm_loader_init_local_use_masks(
  9962. loader_ctx, local_count, error_buf, error_buf_size)) {
  9963. goto fail;
  9964. }
  9965. #endif
  9966. #if WASM_ENABLE_FAST_INTERP != 0
  9967. /* if the result of if branch is in local or const area, add a
  9968. * copy op */
  9969. RESERVE_BLOCK_RET();
  9970. emit_empty_label_addr_and_frame_ip(PATCH_END);
  9971. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  9972. #endif
  9973. RESET_STACK();
  9974. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  9975. /* Pass parameters to if-false branch */
  9976. if (BLOCK_HAS_PARAM(block_type)) {
  9977. for (i = 0; i < block_type.u.type->param_count; i++)
  9978. PUSH_TYPE(block_type.u.type->types[i]);
  9979. }
  9980. #if WASM_ENABLE_FAST_INTERP != 0
  9981. /* Recover top param_count values of frame_offset stack */
  9982. if (BLOCK_HAS_PARAM((block_type))) {
  9983. uint32 size;
  9984. size = sizeof(int16) * block_type.u.type->param_cell_num;
  9985. bh_memcpy_s(loader_ctx->frame_offset, size,
  9986. block->param_frame_offsets, size);
  9987. loader_ctx->frame_offset += (size / sizeof(int16));
  9988. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  9989. }
  9990. #endif
  9991. break;
  9992. }
  9993. case WASM_OP_END:
  9994. {
  9995. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9996. /* check whether block stack matches its result type */
  9997. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  9998. error_buf_size))
  9999. goto fail;
  10000. /* if no else branch, and return types do not match param types,
  10001. report failure */
  10002. if (cur_block->label_type == LABEL_TYPE_IF
  10003. && !cur_block->else_addr) {
  10004. uint32 block_param_count = 0, block_ret_count = 0;
  10005. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  10006. BlockType *cur_block_type = &cur_block->block_type;
  10007. #if WASM_ENABLE_GC != 0
  10008. uint32 block_param_reftype_map_count;
  10009. uint32 block_ret_reftype_map_count;
  10010. WASMRefTypeMap *block_param_reftype_maps;
  10011. WASMRefTypeMap *block_ret_reftype_maps;
  10012. #endif
  10013. block_param_count = block_type_get_param_types(
  10014. cur_block_type, &block_param_types
  10015. #if WASM_ENABLE_GC != 0
  10016. ,
  10017. &block_param_reftype_maps,
  10018. &block_param_reftype_map_count
  10019. #endif
  10020. );
  10021. block_ret_count = block_type_get_result_types(
  10022. cur_block_type, &block_ret_types
  10023. #if WASM_ENABLE_GC != 0
  10024. ,
  10025. &block_ret_reftype_maps, &block_ret_reftype_map_count
  10026. #endif
  10027. );
  10028. if (block_param_count != block_ret_count
  10029. || (block_param_count
  10030. && memcmp(block_param_types, block_ret_types,
  10031. block_param_count))) {
  10032. set_error_buf(error_buf, error_buf_size,
  10033. "type mismatch: else branch missing");
  10034. goto fail;
  10035. }
  10036. #if WASM_ENABLE_GC != 0
  10037. if (block_param_reftype_map_count
  10038. != block_ret_reftype_map_count
  10039. || (block_param_reftype_map_count
  10040. && memcmp(block_param_reftype_maps,
  10041. block_ret_reftype_maps,
  10042. sizeof(WASMRefTypeMap)
  10043. * block_param_reftype_map_count))) {
  10044. set_error_buf(error_buf, error_buf_size,
  10045. "type mismatch: else branch missing");
  10046. goto fail;
  10047. }
  10048. #endif
  10049. }
  10050. POP_CSP();
  10051. #if WASM_ENABLE_FAST_INTERP != 0
  10052. skip_label();
  10053. /* copy the result to the block return address */
  10054. RESERVE_BLOCK_RET();
  10055. apply_label_patch(loader_ctx, 0, PATCH_END);
  10056. free_label_patch_list(loader_ctx->frame_csp);
  10057. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10058. int32 idx;
  10059. uint8 ret_type;
  10060. emit_label(WASM_OP_RETURN);
  10061. for (idx = (int32)func->func_type->result_count - 1;
  10062. idx >= 0; idx--) {
  10063. ret_type = *(func->func_type->types
  10064. + func->func_type->param_count + idx);
  10065. POP_OFFSET_TYPE(ret_type);
  10066. }
  10067. }
  10068. #endif
  10069. if (loader_ctx->csp_num > 0) {
  10070. loader_ctx->frame_csp->end_addr = p - 1;
  10071. }
  10072. else {
  10073. /* end of function block, function will return */
  10074. if (p < p_end) {
  10075. set_error_buf(error_buf, error_buf_size,
  10076. "section size mismatch");
  10077. goto fail;
  10078. }
  10079. }
  10080. break;
  10081. }
  10082. case WASM_OP_BR:
  10083. {
  10084. if (!(frame_csp_tmp =
  10085. check_branch_block(loader_ctx, &p, p_end, false,
  10086. error_buf, error_buf_size)))
  10087. goto fail;
  10088. RESET_STACK();
  10089. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10090. break;
  10091. }
  10092. case WASM_OP_BR_IF:
  10093. {
  10094. POP_I32();
  10095. if (!(frame_csp_tmp =
  10096. check_branch_block(loader_ctx, &p, p_end, false,
  10097. error_buf, error_buf_size)))
  10098. goto fail;
  10099. break;
  10100. }
  10101. case WASM_OP_BR_TABLE:
  10102. {
  10103. uint32 depth, default_arity, arity = 0;
  10104. BranchBlock *target_block;
  10105. BlockType *target_block_type;
  10106. #if WASM_ENABLE_FAST_INTERP == 0
  10107. BrTableCache *br_table_cache = NULL;
  10108. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10109. uint32 j;
  10110. #endif
  10111. read_leb_uint32(p, p_end, count);
  10112. #if WASM_ENABLE_FAST_INTERP != 0
  10113. emit_uint32(loader_ctx, count);
  10114. #endif
  10115. POP_I32();
  10116. /* Get the default depth and check it */
  10117. p_org = p;
  10118. for (i = 0; i <= count; i++) {
  10119. read_leb_uint32(p, p_end, depth);
  10120. }
  10121. if (loader_ctx->csp_num < depth + 1) {
  10122. set_error_buf(error_buf, error_buf_size,
  10123. "unknown label, "
  10124. "unexpected end of section or function");
  10125. goto fail;
  10126. }
  10127. p = p_org;
  10128. /* Get the default block's arity */
  10129. target_block = loader_ctx->frame_csp - (depth + 1);
  10130. target_block_type = &target_block->block_type;
  10131. default_arity = block_type_get_arity(target_block_type,
  10132. target_block->label_type);
  10133. #if WASM_ENABLE_FAST_INTERP == 0
  10134. p_depth_begin = p_depth = p;
  10135. #endif
  10136. for (i = 0; i <= count; i++) {
  10137. p_org = p;
  10138. read_leb_uint32(p, p_end, depth);
  10139. if (loader_ctx->csp_num < depth + 1) {
  10140. set_error_buf(error_buf, error_buf_size,
  10141. "unknown label, "
  10142. "unexpected end of section or function");
  10143. goto fail;
  10144. }
  10145. p = p_org;
  10146. /* Get the target block's arity and check it */
  10147. target_block = loader_ctx->frame_csp - (depth + 1);
  10148. target_block_type = &target_block->block_type;
  10149. arity = block_type_get_arity(target_block_type,
  10150. target_block->label_type);
  10151. if (arity != default_arity) {
  10152. set_error_buf(error_buf, error_buf_size,
  10153. "type mismatch: br_table targets must "
  10154. "all use same result type");
  10155. goto fail;
  10156. }
  10157. if (!(frame_csp_tmp =
  10158. check_branch_block(loader_ctx, &p, p_end, true,
  10159. error_buf, error_buf_size))) {
  10160. goto fail;
  10161. }
  10162. #if WASM_ENABLE_FAST_INTERP == 0
  10163. if (br_table_cache) {
  10164. br_table_cache->br_depths[i] = depth;
  10165. }
  10166. else {
  10167. if (depth > 255) {
  10168. /* The depth cannot be stored in one byte,
  10169. create br_table cache to store each depth */
  10170. #if WASM_ENABLE_DEBUG_INTERP != 0
  10171. if (!record_fast_op(module, p_opcode, *p_opcode,
  10172. error_buf, error_buf_size)) {
  10173. goto fail;
  10174. }
  10175. #endif
  10176. if (!(br_table_cache = loader_malloc(
  10177. offsetof(BrTableCache, br_depths)
  10178. + sizeof(uint32)
  10179. * (uint64)(count + 1),
  10180. error_buf, error_buf_size))) {
  10181. goto fail;
  10182. }
  10183. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10184. br_table_cache->br_table_op_addr = p_opcode;
  10185. br_table_cache->br_count = count;
  10186. /* Copy previous depths which are one byte */
  10187. for (j = 0; j < i; j++) {
  10188. br_table_cache->br_depths[j] = p_depth_begin[j];
  10189. }
  10190. br_table_cache->br_depths[i] = depth;
  10191. bh_list_insert(module->br_table_cache_list,
  10192. br_table_cache);
  10193. }
  10194. else {
  10195. /* The depth can be stored in one byte, use the
  10196. byte of the leb to store it */
  10197. *p_depth++ = (uint8)depth;
  10198. }
  10199. }
  10200. #endif
  10201. }
  10202. #if WASM_ENABLE_FAST_INTERP == 0
  10203. /* Set the tailing bytes to nop */
  10204. if (br_table_cache)
  10205. p_depth = p_depth_begin;
  10206. while (p_depth < p)
  10207. *p_depth++ = WASM_OP_NOP;
  10208. #endif
  10209. RESET_STACK();
  10210. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10211. break;
  10212. }
  10213. case WASM_OP_RETURN:
  10214. {
  10215. int32 idx;
  10216. uint8 ret_type;
  10217. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  10218. idx--) {
  10219. ret_type = *(func->func_type->types
  10220. + func->func_type->param_count + idx);
  10221. #if WASM_ENABLE_FAST_INTERP != 0
  10222. /* emit the offset after return opcode */
  10223. POP_OFFSET_TYPE(ret_type);
  10224. #endif
  10225. POP_TYPE(ret_type);
  10226. }
  10227. RESET_STACK();
  10228. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10229. break;
  10230. }
  10231. case WASM_OP_CALL:
  10232. #if WASM_ENABLE_TAIL_CALL != 0
  10233. case WASM_OP_RETURN_CALL:
  10234. #endif
  10235. #if WASM_ENABLE_GC != 0
  10236. case WASM_OP_CALL_REF:
  10237. case WASM_OP_RETURN_CALL_REF:
  10238. #endif
  10239. {
  10240. WASMFuncType *func_type;
  10241. uint8 type;
  10242. int32 idx;
  10243. #if WASM_ENABLE_GC != 0
  10244. WASMRefType *ref_type;
  10245. uint32 type_idx1;
  10246. int32 j;
  10247. #endif
  10248. #if WASM_ENABLE_GC != 0
  10249. if (opcode == WASM_OP_CALL_REF
  10250. || opcode == WASM_OP_RETURN_CALL_REF) {
  10251. read_leb_uint32(p, p_end, type_idx1);
  10252. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10253. &type_idx, error_buf,
  10254. error_buf_size)) {
  10255. goto fail;
  10256. }
  10257. if (type == VALUE_TYPE_ANY) {
  10258. type_idx = type_idx1;
  10259. }
  10260. if (!check_type_index(module, type_idx, error_buf,
  10261. error_buf_size)) {
  10262. goto fail;
  10263. }
  10264. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10265. set_error_buf(error_buf, error_buf_size,
  10266. "unkown function type");
  10267. goto fail;
  10268. }
  10269. if (type_idx != type_idx1) {
  10270. set_error_buf(error_buf, error_buf_size,
  10271. "function type mismatch");
  10272. goto fail;
  10273. }
  10274. func_type = (WASMFuncType *)module->types[type_idx];
  10275. }
  10276. else
  10277. #endif
  10278. {
  10279. read_leb_uint32(p, p_end, func_idx);
  10280. #if WASM_ENABLE_FAST_INTERP != 0
  10281. /* we need to emit func_idx before arguments */
  10282. emit_uint32(loader_ctx, func_idx);
  10283. #endif
  10284. if (!check_function_index(module, func_idx, error_buf,
  10285. error_buf_size)) {
  10286. goto fail;
  10287. }
  10288. if (func_idx < module->import_function_count)
  10289. func_type = module->import_functions[func_idx]
  10290. .u.function.func_type;
  10291. else
  10292. func_type =
  10293. module
  10294. ->functions[func_idx
  10295. - module->import_function_count]
  10296. ->func_type;
  10297. }
  10298. if (func_type->param_count > 0) {
  10299. #if WASM_ENABLE_GC != 0
  10300. j = (int32)(func_type->result_ref_type_maps
  10301. - func_type->ref_type_maps - 1);
  10302. #endif
  10303. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10304. idx--) {
  10305. #if WASM_ENABLE_GC != 0
  10306. if (wasm_is_type_multi_byte_type(
  10307. func_type->types[idx])) {
  10308. ref_type = func_type->ref_type_maps[j].ref_type;
  10309. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10310. ref_type,
  10311. wasm_reftype_struct_size(ref_type));
  10312. j--;
  10313. }
  10314. #endif
  10315. #if WASM_ENABLE_FAST_INTERP != 0
  10316. POP_OFFSET_TYPE(func_type->types[idx]);
  10317. #endif
  10318. POP_TYPE(func_type->types[idx]);
  10319. }
  10320. }
  10321. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10322. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10323. #endif
  10324. #if WASM_ENABLE_GC != 0
  10325. j = (int32)(func_type->result_ref_type_maps
  10326. - func_type->ref_type_maps);
  10327. #endif
  10328. for (i = 0; i < func_type->result_count; i++) {
  10329. #if WASM_ENABLE_GC != 0
  10330. if (wasm_is_type_multi_byte_type(
  10331. func_type->types[func_type->param_count + i])) {
  10332. ref_type = func_type->ref_type_maps[j].ref_type;
  10333. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10334. ref_type,
  10335. wasm_reftype_struct_size(ref_type));
  10336. j++;
  10337. }
  10338. #endif
  10339. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10340. #if WASM_ENABLE_FAST_INTERP != 0
  10341. /* Here we emit each return value's dynamic_offset. But
  10342. * in fact these offsets are continuous, so interpreter
  10343. * only need to get the first return value's offset.
  10344. */
  10345. PUSH_OFFSET_TYPE(
  10346. func_type->types[func_type->param_count + i]);
  10347. #endif
  10348. }
  10349. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10350. }
  10351. else {
  10352. #if WASM_ENABLE_GC == 0
  10353. if (func_type->result_count
  10354. != func->func_type->result_count) {
  10355. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10356. "type mismatch: expect ",
  10357. func->func_type->result_count,
  10358. " return values but got other");
  10359. goto fail;
  10360. }
  10361. for (i = 0; i < func_type->result_count; i++) {
  10362. type = func->func_type
  10363. ->types[func->func_type->param_count + i];
  10364. if (func_type->types[func_type->param_count + i]
  10365. != type) {
  10366. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10367. "type mismatch: expect ",
  10368. type2str(type), " but got other");
  10369. goto fail;
  10370. }
  10371. }
  10372. #else
  10373. if (!wasm_func_type_result_is_subtype_of(
  10374. func_type, func->func_type, module->types,
  10375. module->type_count)) {
  10376. set_error_buf(
  10377. error_buf, error_buf_size,
  10378. "type mismatch: invalid func result types");
  10379. goto fail;
  10380. }
  10381. #endif
  10382. RESET_STACK();
  10383. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10384. }
  10385. #endif
  10386. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10387. || WASM_ENABLE_WAMR_COMPILER != 0
  10388. func->has_op_func_call = true;
  10389. #endif
  10390. (void)type;
  10391. break;
  10392. }
  10393. /*
  10394. * if disable reference type: call_indirect typeidx, 0x00
  10395. * if enable reference type: call_indirect typeidx, tableidx
  10396. */
  10397. case WASM_OP_CALL_INDIRECT:
  10398. #if WASM_ENABLE_TAIL_CALL != 0
  10399. case WASM_OP_RETURN_CALL_INDIRECT:
  10400. #endif
  10401. {
  10402. int32 idx;
  10403. WASMFuncType *func_type;
  10404. read_leb_uint32(p, p_end, type_idx);
  10405. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10406. read_leb_uint32(p, p_end, table_idx);
  10407. #else
  10408. CHECK_BUF(p, p_end, 1);
  10409. table_idx = read_uint8(p);
  10410. #endif
  10411. if (!check_table_index(module, table_idx, error_buf,
  10412. error_buf_size)) {
  10413. goto fail;
  10414. }
  10415. #if WASM_ENABLE_FAST_INTERP != 0
  10416. /* we need to emit before arguments */
  10417. #if WASM_ENABLE_TAIL_CALL != 0
  10418. emit_byte(loader_ctx, opcode);
  10419. #endif
  10420. emit_uint32(loader_ctx, type_idx);
  10421. emit_uint32(loader_ctx, table_idx);
  10422. #endif
  10423. /* skip elem idx */
  10424. POP_I32();
  10425. if (type_idx >= module->type_count) {
  10426. set_error_buf(error_buf, error_buf_size, "unknown type");
  10427. goto fail;
  10428. }
  10429. func_type = (WASMFuncType *)module->types[type_idx];
  10430. if (func_type->param_count > 0) {
  10431. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10432. idx--) {
  10433. #if WASM_ENABLE_FAST_INTERP != 0
  10434. POP_OFFSET_TYPE(func_type->types[idx]);
  10435. #endif
  10436. POP_TYPE(func_type->types[idx]);
  10437. }
  10438. }
  10439. #if WASM_ENABLE_TAIL_CALL != 0
  10440. if (opcode == WASM_OP_CALL_INDIRECT) {
  10441. #endif
  10442. for (i = 0; i < func_type->result_count; i++) {
  10443. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10444. #if WASM_ENABLE_FAST_INTERP != 0
  10445. PUSH_OFFSET_TYPE(
  10446. func_type->types[func_type->param_count + i]);
  10447. #endif
  10448. }
  10449. #if WASM_ENABLE_TAIL_CALL != 0
  10450. }
  10451. else {
  10452. uint8 type;
  10453. if (func_type->result_count
  10454. != func->func_type->result_count) {
  10455. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10456. "type mismatch: expect ",
  10457. func->func_type->result_count,
  10458. " return values but got other");
  10459. goto fail;
  10460. }
  10461. for (i = 0; i < func_type->result_count; i++) {
  10462. type = func->func_type
  10463. ->types[func->func_type->param_count + i];
  10464. if (func_type->types[func_type->param_count + i]
  10465. != type) {
  10466. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10467. "type mismatch: expect ",
  10468. type2str(type), " but got other");
  10469. goto fail;
  10470. }
  10471. }
  10472. RESET_STACK();
  10473. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10474. }
  10475. #endif
  10476. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10477. || WASM_ENABLE_WAMR_COMPILER != 0
  10478. func->has_op_func_call = true;
  10479. #endif
  10480. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10481. func->has_op_call_indirect = true;
  10482. #endif
  10483. break;
  10484. }
  10485. case WASM_OP_DROP:
  10486. {
  10487. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10488. int32 available_stack_cell =
  10489. (int32)(loader_ctx->stack_cell_num
  10490. - cur_block->stack_cell_num);
  10491. if (available_stack_cell <= 0
  10492. && !cur_block->is_stack_polymorphic) {
  10493. set_error_buf(error_buf, error_buf_size,
  10494. "type mismatch, opcode drop was found "
  10495. "but stack was empty");
  10496. goto fail;
  10497. }
  10498. if (available_stack_cell > 0) {
  10499. #if WASM_ENABLE_GC != 0
  10500. if (wasm_is_type_multi_byte_type(
  10501. *(loader_ctx->frame_ref - 1))) {
  10502. bh_assert((int32)(loader_ctx->reftype_map_num
  10503. - cur_block->reftype_map_num)
  10504. > 0);
  10505. loader_ctx->frame_reftype_map--;
  10506. loader_ctx->reftype_map_num--;
  10507. }
  10508. #endif
  10509. if (is_32bit_type(*(loader_ctx->frame_ref - 1))
  10510. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  10511. loader_ctx->frame_ref--;
  10512. loader_ctx->stack_cell_num--;
  10513. #if WASM_ENABLE_FAST_INTERP != 0
  10514. skip_label();
  10515. loader_ctx->frame_offset--;
  10516. if ((*(loader_ctx->frame_offset)
  10517. > loader_ctx->start_dynamic_offset)
  10518. && (*(loader_ctx->frame_offset)
  10519. < loader_ctx->max_dynamic_offset))
  10520. loader_ctx->dynamic_offset--;
  10521. #endif
  10522. }
  10523. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10524. loader_ctx->frame_ref -= 2;
  10525. loader_ctx->stack_cell_num -= 2;
  10526. #if WASM_ENABLE_FAST_INTERP == 0
  10527. *(p - 1) = WASM_OP_DROP_64;
  10528. #endif
  10529. #if WASM_ENABLE_FAST_INTERP != 0
  10530. skip_label();
  10531. loader_ctx->frame_offset -= 2;
  10532. if ((*(loader_ctx->frame_offset)
  10533. > loader_ctx->start_dynamic_offset)
  10534. && (*(loader_ctx->frame_offset)
  10535. < loader_ctx->max_dynamic_offset))
  10536. loader_ctx->dynamic_offset -= 2;
  10537. #endif
  10538. }
  10539. #if WASM_ENABLE_SIMD != 0
  10540. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10541. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10542. loader_ctx->frame_ref -= 4;
  10543. loader_ctx->stack_cell_num -= 4;
  10544. }
  10545. #endif
  10546. #endif
  10547. else {
  10548. set_error_buf(error_buf, error_buf_size,
  10549. "type mismatch");
  10550. goto fail;
  10551. }
  10552. }
  10553. else {
  10554. #if WASM_ENABLE_FAST_INTERP != 0
  10555. skip_label();
  10556. #endif
  10557. }
  10558. break;
  10559. }
  10560. case WASM_OP_SELECT:
  10561. {
  10562. uint8 ref_type;
  10563. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10564. int32 available_stack_cell;
  10565. #if WASM_ENABLE_FAST_INTERP != 0
  10566. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10567. #endif
  10568. POP_I32();
  10569. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10570. - cur_block->stack_cell_num);
  10571. if (available_stack_cell <= 0
  10572. && !cur_block->is_stack_polymorphic) {
  10573. set_error_buf(error_buf, error_buf_size,
  10574. "type mismatch or invalid result arity, "
  10575. "opcode select was found "
  10576. "but stack was empty");
  10577. goto fail;
  10578. }
  10579. if (available_stack_cell > 0) {
  10580. switch (*(loader_ctx->frame_ref - 1)) {
  10581. case VALUE_TYPE_I32:
  10582. case VALUE_TYPE_F32:
  10583. break;
  10584. case VALUE_TYPE_I64:
  10585. case VALUE_TYPE_F64:
  10586. #if WASM_ENABLE_FAST_INTERP == 0
  10587. *(p - 1) = WASM_OP_SELECT_64;
  10588. #endif
  10589. #if WASM_ENABLE_FAST_INTERP != 0
  10590. if (loader_ctx->p_code_compiled) {
  10591. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10592. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10593. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10594. *(void **)(p_code_compiled_tmp
  10595. - sizeof(void *)) =
  10596. handle_table[opcode_tmp];
  10597. #else
  10598. #if UINTPTR_MAX == UINT64_MAX
  10599. /* emit int32 relative offset in 64-bit target
  10600. */
  10601. int32 offset =
  10602. (int32)((uint8 *)handle_table[opcode_tmp]
  10603. - (uint8 *)handle_table[0]);
  10604. *(int32 *)(p_code_compiled_tmp
  10605. - sizeof(int32)) = offset;
  10606. #else
  10607. /* emit uint32 label address in 32-bit target */
  10608. *(uint32 *)(p_code_compiled_tmp
  10609. - sizeof(uint32)) =
  10610. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10611. #endif
  10612. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10613. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10614. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10615. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10616. #else
  10617. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10618. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10619. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10620. }
  10621. #endif /* end of WASM_ENABLE_FAST_INTERP */
  10622. break;
  10623. #if WASM_ENABLE_SIMD != 0
  10624. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10625. case VALUE_TYPE_V128:
  10626. break;
  10627. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  10628. #endif /* WASM_ENABLE_SIMD != 0 */
  10629. default:
  10630. {
  10631. set_error_buf(error_buf, error_buf_size,
  10632. "type mismatch");
  10633. goto fail;
  10634. }
  10635. }
  10636. ref_type = *(loader_ctx->frame_ref - 1);
  10637. #if WASM_ENABLE_FAST_INTERP != 0
  10638. POP_OFFSET_TYPE(ref_type);
  10639. POP_TYPE(ref_type);
  10640. POP_OFFSET_TYPE(ref_type);
  10641. POP_TYPE(ref_type);
  10642. PUSH_OFFSET_TYPE(ref_type);
  10643. PUSH_TYPE(ref_type);
  10644. #else
  10645. POP2_AND_PUSH(ref_type, ref_type);
  10646. #endif
  10647. }
  10648. else {
  10649. #if WASM_ENABLE_FAST_INTERP != 0
  10650. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  10651. #endif
  10652. PUSH_TYPE(VALUE_TYPE_ANY);
  10653. }
  10654. break;
  10655. }
  10656. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10657. case WASM_OP_SELECT_T:
  10658. {
  10659. uint8 vec_len, type;
  10660. #if WASM_ENABLE_GC != 0
  10661. WASMRefType *ref_type = NULL;
  10662. #endif
  10663. #if WASM_ENABLE_FAST_INTERP != 0
  10664. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10665. #endif
  10666. read_leb_uint32(p, p_end, vec_len);
  10667. if (vec_len != 1) {
  10668. /* typed select must have exactly one result */
  10669. set_error_buf(error_buf, error_buf_size,
  10670. "invalid result arity");
  10671. goto fail;
  10672. }
  10673. #if WASM_ENABLE_GC == 0
  10674. CHECK_BUF(p, p_end, 1);
  10675. type = read_uint8(p);
  10676. if (!is_value_type(type)) {
  10677. set_error_buf(error_buf, error_buf_size,
  10678. "unknown value type");
  10679. goto fail;
  10680. }
  10681. #else
  10682. p_org = p + 1;
  10683. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  10684. &need_ref_type_map, &wasm_ref_type,
  10685. false, error_buf, error_buf_size)) {
  10686. goto fail;
  10687. }
  10688. type = wasm_ref_type.ref_type;
  10689. if (need_ref_type_map) {
  10690. if (!(ref_type = reftype_set_insert(
  10691. module->ref_type_set, &wasm_ref_type, error_buf,
  10692. error_buf_size))) {
  10693. goto fail;
  10694. }
  10695. }
  10696. #if WASM_ENABLE_FAST_INTERP == 0
  10697. while (p_org < p) {
  10698. #if WASM_ENABLE_DEBUG_INTERP != 0
  10699. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10700. error_buf_size)) {
  10701. goto fail;
  10702. }
  10703. #endif
  10704. /* Ignore extra bytes for interpreter */
  10705. *p_org++ = WASM_OP_NOP;
  10706. }
  10707. #endif
  10708. #endif /* end of WASM_ENABLE_GC == 0 */
  10709. POP_I32();
  10710. #if WASM_ENABLE_FAST_INTERP != 0
  10711. if (loader_ctx->p_code_compiled) {
  10712. uint8 opcode_tmp = WASM_OP_SELECT;
  10713. if (type == VALUE_TYPE_V128) {
  10714. #if (WASM_ENABLE_SIMD == 0) \
  10715. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  10716. set_error_buf(error_buf, error_buf_size,
  10717. "SIMD v128 type isn't supported");
  10718. goto fail;
  10719. #endif
  10720. }
  10721. else {
  10722. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  10723. opcode_tmp = WASM_OP_SELECT_64;
  10724. #if WASM_ENABLE_GC != 0
  10725. if (wasm_is_type_reftype(type))
  10726. opcode_tmp = WASM_OP_SELECT_T;
  10727. #endif
  10728. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10729. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10730. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  10731. handle_table[opcode_tmp];
  10732. #else
  10733. #if UINTPTR_MAX == UINT64_MAX
  10734. /* emit int32 relative offset in 64-bit target */
  10735. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  10736. - (uint8 *)handle_table[0]);
  10737. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  10738. offset;
  10739. #else
  10740. /* emit uint32 label address in 32-bit target */
  10741. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  10742. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10743. #endif
  10744. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10745. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10746. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10747. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10748. #else
  10749. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10750. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10751. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10752. }
  10753. }
  10754. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  10755. POP_REF(type);
  10756. #if WASM_ENABLE_GC != 0
  10757. if (need_ref_type_map) {
  10758. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  10759. wasm_reftype_struct_size(ref_type));
  10760. }
  10761. #endif
  10762. POP_REF(type);
  10763. #if WASM_ENABLE_GC != 0
  10764. if (need_ref_type_map) {
  10765. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  10766. wasm_reftype_struct_size(ref_type));
  10767. }
  10768. #endif
  10769. PUSH_REF(type);
  10770. #if WASM_ENABLE_WAMR_COMPILER != 0
  10771. module->is_ref_types_used = true;
  10772. #endif
  10773. (void)vec_len;
  10774. break;
  10775. }
  10776. /* table.get x. tables[x]. [i32] -> [t] */
  10777. /* table.set x. tables[x]. [i32 t] -> [] */
  10778. case WASM_OP_TABLE_GET:
  10779. case WASM_OP_TABLE_SET:
  10780. {
  10781. uint8 decl_ref_type;
  10782. #if WASM_ENABLE_GC != 0
  10783. WASMRefType *ref_type;
  10784. #endif
  10785. read_leb_uint32(p, p_end, table_idx);
  10786. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  10787. #if WASM_ENABLE_GC != 0
  10788. (void **)&ref_type,
  10789. #else
  10790. NULL,
  10791. #endif
  10792. error_buf, error_buf_size))
  10793. goto fail;
  10794. #if WASM_ENABLE_GC != 0
  10795. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  10796. bh_assert(ref_type);
  10797. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  10798. ref_type, wasm_reftype_struct_size(ref_type));
  10799. }
  10800. #endif
  10801. #if WASM_ENABLE_FAST_INTERP != 0
  10802. emit_uint32(loader_ctx, table_idx);
  10803. #endif
  10804. if (opcode == WASM_OP_TABLE_GET) {
  10805. POP_I32();
  10806. #if WASM_ENABLE_FAST_INTERP != 0
  10807. PUSH_OFFSET_TYPE(decl_ref_type);
  10808. #endif
  10809. PUSH_TYPE(decl_ref_type);
  10810. }
  10811. else {
  10812. #if WASM_ENABLE_FAST_INTERP != 0
  10813. POP_OFFSET_TYPE(decl_ref_type);
  10814. #endif
  10815. POP_TYPE(decl_ref_type);
  10816. POP_I32();
  10817. }
  10818. #if WASM_ENABLE_WAMR_COMPILER != 0
  10819. module->is_ref_types_used = true;
  10820. #endif
  10821. break;
  10822. }
  10823. case WASM_OP_REF_NULL:
  10824. {
  10825. uint8 ref_type;
  10826. #if WASM_ENABLE_GC == 0
  10827. CHECK_BUF(p, p_end, 1);
  10828. ref_type = read_uint8(p);
  10829. if (ref_type != VALUE_TYPE_FUNCREF
  10830. && ref_type != VALUE_TYPE_EXTERNREF) {
  10831. set_error_buf(error_buf, error_buf_size, "type mismatch");
  10832. goto fail;
  10833. }
  10834. #else
  10835. read_leb_int32(p, p_end, heap_type);
  10836. if (heap_type >= 0) {
  10837. if (!check_type_index(module, heap_type, error_buf,
  10838. error_buf_size)) {
  10839. goto fail;
  10840. }
  10841. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  10842. true, heap_type);
  10843. ref_type = wasm_ref_type.ref_type;
  10844. }
  10845. else {
  10846. if (!wasm_is_valid_heap_type(heap_type)) {
  10847. set_error_buf(error_buf, error_buf_size,
  10848. "unknown type");
  10849. goto fail;
  10850. }
  10851. ref_type = (uint8)((int32)0x80 + heap_type);
  10852. }
  10853. #endif /* end of WASM_ENABLE_GC == 0 */
  10854. #if WASM_ENABLE_FAST_INTERP != 0
  10855. PUSH_OFFSET_TYPE(ref_type);
  10856. #endif
  10857. PUSH_TYPE(ref_type);
  10858. #if WASM_ENABLE_WAMR_COMPILER != 0
  10859. module->is_ref_types_used = true;
  10860. #endif
  10861. break;
  10862. }
  10863. case WASM_OP_REF_IS_NULL:
  10864. {
  10865. #if WASM_ENABLE_GC == 0
  10866. #if WASM_ENABLE_FAST_INTERP != 0
  10867. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10868. int32 block_stack_cell_num =
  10869. (int32)(loader_ctx->stack_cell_num
  10870. - cur_block->stack_cell_num);
  10871. if (block_stack_cell_num <= 0) {
  10872. if (!cur_block->is_stack_polymorphic) {
  10873. set_error_buf(
  10874. error_buf, error_buf_size,
  10875. "type mismatch: expect data but stack was empty");
  10876. goto fail;
  10877. }
  10878. }
  10879. else {
  10880. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  10881. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  10882. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  10883. if (!wasm_loader_pop_frame_ref_offset(
  10884. loader_ctx, *(loader_ctx->frame_ref - 1),
  10885. error_buf, error_buf_size)) {
  10886. goto fail;
  10887. }
  10888. }
  10889. else {
  10890. set_error_buf(error_buf, error_buf_size,
  10891. "type mismatch");
  10892. goto fail;
  10893. }
  10894. }
  10895. #else
  10896. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  10897. error_buf, error_buf_size)
  10898. && !wasm_loader_pop_frame_ref(loader_ctx,
  10899. VALUE_TYPE_EXTERNREF,
  10900. error_buf, error_buf_size)) {
  10901. goto fail;
  10902. }
  10903. #endif
  10904. #else /* else of WASM_ENABLE_GC == 0 */
  10905. uint8 type;
  10906. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  10907. error_buf, error_buf_size)) {
  10908. goto fail;
  10909. }
  10910. #endif
  10911. PUSH_I32();
  10912. #if WASM_ENABLE_WAMR_COMPILER != 0
  10913. module->is_ref_types_used = true;
  10914. #endif
  10915. break;
  10916. }
  10917. case WASM_OP_REF_FUNC:
  10918. {
  10919. read_leb_uint32(p, p_end, func_idx);
  10920. if (!check_function_index(module, func_idx, error_buf,
  10921. error_buf_size)) {
  10922. goto fail;
  10923. }
  10924. /* Refer to a forward-declared function */
  10925. if (func_idx >= cur_func_idx + module->import_function_count) {
  10926. WASMTableSeg *table_seg = module->table_segments;
  10927. bool func_declared = false;
  10928. uint32 j;
  10929. /* Check whether the function is declared in table segs,
  10930. note that it doesn't matter whether the table seg's mode
  10931. is passive, active or declarative. */
  10932. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  10933. if (table_seg->elem_type == VALUE_TYPE_FUNCREF) {
  10934. for (j = 0; j < table_seg->value_count; j++) {
  10935. if (table_seg->init_values[j].u.ref_index
  10936. == func_idx) {
  10937. func_declared = true;
  10938. break;
  10939. }
  10940. }
  10941. }
  10942. }
  10943. if (!func_declared) {
  10944. /* Check whether the function is exported */
  10945. for (i = 0; i < module->export_count; i++) {
  10946. if (module->exports[i].kind == EXPORT_KIND_FUNC
  10947. && module->exports[i].index == func_idx) {
  10948. func_declared = true;
  10949. break;
  10950. }
  10951. }
  10952. }
  10953. if (!func_declared) {
  10954. set_error_buf(error_buf, error_buf_size,
  10955. "undeclared function reference");
  10956. goto fail;
  10957. }
  10958. }
  10959. #if WASM_ENABLE_FAST_INTERP != 0
  10960. emit_uint32(loader_ctx, func_idx);
  10961. #endif
  10962. #if WASM_ENABLE_GC == 0
  10963. PUSH_FUNCREF();
  10964. #else
  10965. if (func_idx < module->import_function_count)
  10966. type_idx =
  10967. module->import_functions[func_idx].u.function.type_idx;
  10968. else
  10969. type_idx = module
  10970. ->functions[func_idx
  10971. - module->import_function_count]
  10972. ->type_idx;
  10973. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  10974. false, type_idx);
  10975. PUSH_REF(wasm_ref_type.ref_type);
  10976. #endif
  10977. #if WASM_ENABLE_WAMR_COMPILER != 0
  10978. module->is_ref_types_used = true;
  10979. #endif
  10980. break;
  10981. }
  10982. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  10983. #if WASM_ENABLE_GC != 0
  10984. case WASM_OP_REF_AS_NON_NULL:
  10985. case WASM_OP_BR_ON_NULL:
  10986. {
  10987. uint8 type;
  10988. WASMRefType ref_type;
  10989. /* POP (ref null ht) and get the converted (ref ht) */
  10990. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  10991. error_buf, error_buf_size)) {
  10992. goto fail;
  10993. }
  10994. if (opcode == WASM_OP_BR_ON_NULL) {
  10995. if (!(frame_csp_tmp =
  10996. check_branch_block(loader_ctx, &p, p_end, false,
  10997. error_buf, error_buf_size))) {
  10998. goto fail;
  10999. }
  11000. }
  11001. #if WASM_ENABLE_FAST_INTERP != 0
  11002. disable_emit = true;
  11003. #endif
  11004. /* PUSH the converted (ref ht) */
  11005. if (type != VALUE_TYPE_ANY) {
  11006. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11007. sizeof(WASMRefType));
  11008. }
  11009. PUSH_REF(type);
  11010. break;
  11011. }
  11012. case WASM_OP_BR_ON_NON_NULL:
  11013. {
  11014. uint8 type;
  11015. WASMRefType ref_type;
  11016. uint32 available_stack_cell =
  11017. loader_ctx->stack_cell_num
  11018. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11019. /* POP (ref null ht) and get the converted (ref ht) */
  11020. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11021. error_buf, error_buf_size)) {
  11022. goto fail;
  11023. }
  11024. #if WASM_ENABLE_FAST_INTERP != 0
  11025. disable_emit = true;
  11026. #endif
  11027. /* Temporarily PUSH back (ref ht), check brach block and
  11028. then POP it */
  11029. if (available_stack_cell
  11030. > 0) { /* stack isn't in polymorphic state */
  11031. if (type != VALUE_TYPE_ANY) {
  11032. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11033. &ref_type, sizeof(WASMRefType));
  11034. }
  11035. PUSH_REF(type);
  11036. }
  11037. if (!(frame_csp_tmp =
  11038. check_branch_block(loader_ctx, &p, p_end, false,
  11039. error_buf, error_buf_size))) {
  11040. goto fail;
  11041. }
  11042. if (available_stack_cell
  11043. > 0) { /* stack isn't in polymorphic state */
  11044. POP_REF(type);
  11045. #if WASM_ENABLE_FAST_INTERP != 0
  11046. /* Erase the opnd offset emitted by POP_REF() */
  11047. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11048. #endif
  11049. }
  11050. break;
  11051. }
  11052. case WASM_OP_REF_EQ:
  11053. POP_REF(REF_TYPE_EQREF);
  11054. POP_REF(REF_TYPE_EQREF);
  11055. PUSH_I32();
  11056. break;
  11057. #endif /* end of WASM_ENABLE_GC != 0 */
  11058. case WASM_OP_GET_LOCAL:
  11059. {
  11060. p_org = p - 1;
  11061. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11062. PUSH_TYPE(local_type);
  11063. #if WASM_ENABLE_GC != 0
  11064. /* Cannot get a non-nullable and unset local */
  11065. if (local_idx >= param_count
  11066. && wasm_is_reftype_htref_non_nullable(local_type)
  11067. && !wasm_loader_get_local_status(loader_ctx,
  11068. local_idx - param_count)) {
  11069. set_error_buf(error_buf, error_buf_size,
  11070. "uninitialized local");
  11071. goto fail;
  11072. }
  11073. #endif
  11074. #if WASM_ENABLE_FAST_INTERP != 0
  11075. /* Get Local is optimized out */
  11076. skip_label();
  11077. disable_emit = true;
  11078. operand_offset = local_offset;
  11079. PUSH_OFFSET_TYPE(local_type);
  11080. #else
  11081. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11082. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11083. if (local_offset < 0x80
  11084. #if WASM_ENABLE_GC != 0
  11085. && !wasm_is_type_reftype(local_type)
  11086. #endif
  11087. ) {
  11088. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11089. if (is_32bit_type(local_type)) {
  11090. *p_org++ = (uint8)local_offset;
  11091. }
  11092. else {
  11093. *p_org++ = (uint8)(local_offset | 0x80);
  11094. }
  11095. while (p_org < p) {
  11096. *p_org++ = WASM_OP_NOP;
  11097. }
  11098. }
  11099. #endif
  11100. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11101. break;
  11102. }
  11103. case WASM_OP_SET_LOCAL:
  11104. {
  11105. p_org = p - 1;
  11106. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11107. POP_TYPE(local_type);
  11108. #if WASM_ENABLE_FAST_INTERP != 0
  11109. if (!(preserve_referenced_local(
  11110. loader_ctx, opcode, local_offset, local_type,
  11111. &preserve_local, error_buf, error_buf_size)))
  11112. goto fail;
  11113. if (local_offset < 256
  11114. #if WASM_ENABLE_GC != 0
  11115. && !wasm_is_type_reftype(local_type)
  11116. #endif
  11117. ) {
  11118. skip_label();
  11119. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11120. if (loader_ctx->p_code_compiled)
  11121. STORE_U16(loader_ctx->p_code_compiled - 2,
  11122. local_offset);
  11123. loader_ctx->frame_offset--;
  11124. loader_ctx->dynamic_offset--;
  11125. }
  11126. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11127. if (loader_ctx->p_code_compiled)
  11128. STORE_U16(loader_ctx->p_code_compiled - 2,
  11129. local_offset);
  11130. loader_ctx->frame_offset -= 2;
  11131. loader_ctx->dynamic_offset -= 2;
  11132. }
  11133. else {
  11134. if (is_32bit_type(local_type)) {
  11135. emit_label(EXT_OP_SET_LOCAL_FAST);
  11136. emit_byte(loader_ctx, (uint8)local_offset);
  11137. }
  11138. else {
  11139. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11140. emit_byte(loader_ctx, (uint8)local_offset);
  11141. }
  11142. POP_OFFSET_TYPE(local_type);
  11143. }
  11144. }
  11145. else { /* local index larger than 255, reserve leb */
  11146. emit_uint32(loader_ctx, local_idx);
  11147. POP_OFFSET_TYPE(local_type);
  11148. }
  11149. #else
  11150. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11151. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11152. if (local_offset < 0x80
  11153. #if WASM_ENABLE_GC != 0
  11154. && !wasm_is_type_reftype(local_type)
  11155. #endif
  11156. ) {
  11157. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11158. if (is_32bit_type(local_type)) {
  11159. *p_org++ = (uint8)local_offset;
  11160. }
  11161. else {
  11162. *p_org++ = (uint8)(local_offset | 0x80);
  11163. }
  11164. while (p_org < p) {
  11165. *p_org++ = WASM_OP_NOP;
  11166. }
  11167. }
  11168. #endif
  11169. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11170. #if WASM_ENABLE_GC != 0
  11171. if (local_idx >= param_count) {
  11172. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11173. }
  11174. #endif
  11175. break;
  11176. }
  11177. case WASM_OP_TEE_LOCAL:
  11178. {
  11179. p_org = p - 1;
  11180. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11181. #if WASM_ENABLE_FAST_INTERP != 0
  11182. /* If the stack is in polymorphic state, do fake pop and push on
  11183. offset stack to keep the depth of offset stack to be the
  11184. same with ref stack */
  11185. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11186. if (cur_block->is_stack_polymorphic) {
  11187. POP_OFFSET_TYPE(local_type);
  11188. PUSH_OFFSET_TYPE(local_type);
  11189. }
  11190. #endif
  11191. POP_TYPE(local_type);
  11192. PUSH_TYPE(local_type);
  11193. #if WASM_ENABLE_FAST_INTERP != 0
  11194. if (!(preserve_referenced_local(
  11195. loader_ctx, opcode, local_offset, local_type,
  11196. &preserve_local, error_buf, error_buf_size)))
  11197. goto fail;
  11198. if (local_offset < 256
  11199. #if WASM_ENABLE_GC != 0
  11200. && !wasm_is_type_reftype(local_type)
  11201. #endif
  11202. ) {
  11203. skip_label();
  11204. if (is_32bit_type(local_type)) {
  11205. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11206. emit_byte(loader_ctx, (uint8)local_offset);
  11207. }
  11208. else {
  11209. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11210. emit_byte(loader_ctx, (uint8)local_offset);
  11211. }
  11212. }
  11213. else { /* local index larger than 255, reserve leb */
  11214. emit_uint32(loader_ctx, local_idx);
  11215. }
  11216. emit_operand(loader_ctx,
  11217. *(loader_ctx->frame_offset
  11218. - wasm_value_type_cell_num(local_type)));
  11219. #else
  11220. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11221. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11222. if (local_offset < 0x80
  11223. #if WASM_ENABLE_GC != 0
  11224. && !wasm_is_type_reftype(local_type)
  11225. #endif
  11226. ) {
  11227. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11228. if (is_32bit_type(local_type)) {
  11229. *p_org++ = (uint8)local_offset;
  11230. }
  11231. else {
  11232. *p_org++ = (uint8)(local_offset | 0x80);
  11233. }
  11234. while (p_org < p) {
  11235. *p_org++ = WASM_OP_NOP;
  11236. }
  11237. }
  11238. #endif
  11239. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11240. #if WASM_ENABLE_GC != 0
  11241. if (local_idx >= param_count) {
  11242. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11243. }
  11244. #endif
  11245. break;
  11246. }
  11247. case WASM_OP_GET_GLOBAL:
  11248. {
  11249. #if WASM_ENABLE_GC != 0
  11250. WASMRefType *ref_type;
  11251. #endif
  11252. p_org = p - 1;
  11253. read_leb_uint32(p, p_end, global_idx);
  11254. if (global_idx >= global_count) {
  11255. set_error_buf(error_buf, error_buf_size, "unknown global");
  11256. goto fail;
  11257. }
  11258. global_type =
  11259. global_idx < module->import_global_count
  11260. ? module->import_globals[global_idx].u.global.type
  11261. : module
  11262. ->globals[global_idx
  11263. - module->import_global_count]
  11264. .type;
  11265. #if WASM_ENABLE_GC != 0
  11266. ref_type =
  11267. global_idx < module->import_global_count
  11268. ? module->import_globals[global_idx].u.global.ref_type
  11269. : module
  11270. ->globals[global_idx
  11271. - module->import_global_count]
  11272. .ref_type;
  11273. if (wasm_is_type_multi_byte_type(global_type)) {
  11274. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11275. wasm_reftype_struct_size(ref_type));
  11276. }
  11277. #endif
  11278. PUSH_TYPE(global_type);
  11279. #if WASM_ENABLE_FAST_INTERP == 0
  11280. if (global_type == VALUE_TYPE_I64
  11281. || global_type == VALUE_TYPE_F64) {
  11282. #if WASM_ENABLE_DEBUG_INTERP != 0
  11283. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11284. error_buf_size)) {
  11285. goto fail;
  11286. }
  11287. #endif
  11288. *p_org = WASM_OP_GET_GLOBAL_64;
  11289. }
  11290. #else /* else of WASM_ENABLE_FAST_INTERP */
  11291. if (global_type == VALUE_TYPE_I64
  11292. || global_type == VALUE_TYPE_F64) {
  11293. skip_label();
  11294. emit_label(WASM_OP_GET_GLOBAL_64);
  11295. }
  11296. emit_uint32(loader_ctx, global_idx);
  11297. PUSH_OFFSET_TYPE(global_type);
  11298. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11299. break;
  11300. }
  11301. case WASM_OP_SET_GLOBAL:
  11302. {
  11303. bool is_mutable = false;
  11304. #if WASM_ENABLE_GC != 0
  11305. WASMRefType *ref_type;
  11306. #endif
  11307. p_org = p - 1;
  11308. read_leb_uint32(p, p_end, global_idx);
  11309. if (global_idx >= global_count) {
  11310. set_error_buf(error_buf, error_buf_size, "unknown global");
  11311. goto fail;
  11312. }
  11313. is_mutable =
  11314. global_idx < module->import_global_count
  11315. ? module->import_globals[global_idx].u.global.is_mutable
  11316. : module
  11317. ->globals[global_idx
  11318. - module->import_global_count]
  11319. .is_mutable;
  11320. if (!is_mutable) {
  11321. #if WASM_ENABLE_GC == 0
  11322. set_error_buf(error_buf, error_buf_size,
  11323. "global is immutable");
  11324. #else
  11325. set_error_buf(error_buf, error_buf_size,
  11326. "immutable global");
  11327. #endif
  11328. goto fail;
  11329. }
  11330. global_type =
  11331. global_idx < module->import_global_count
  11332. ? module->import_globals[global_idx].u.global.type
  11333. : module
  11334. ->globals[global_idx
  11335. - module->import_global_count]
  11336. .type;
  11337. #if WASM_ENABLE_GC != 0
  11338. ref_type =
  11339. global_idx < module->import_global_count
  11340. ? module->import_globals[global_idx].u.global.ref_type
  11341. : module
  11342. ->globals[global_idx
  11343. - module->import_global_count]
  11344. .ref_type;
  11345. if (wasm_is_type_multi_byte_type(global_type)) {
  11346. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11347. wasm_reftype_struct_size(ref_type));
  11348. }
  11349. #endif
  11350. #if WASM_ENABLE_FAST_INTERP == 0
  11351. if (global_type == VALUE_TYPE_I64
  11352. || global_type == VALUE_TYPE_F64) {
  11353. #if WASM_ENABLE_DEBUG_INTERP != 0
  11354. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11355. error_buf_size)) {
  11356. goto fail;
  11357. }
  11358. #endif
  11359. *p_org = WASM_OP_SET_GLOBAL_64;
  11360. }
  11361. else if (module->aux_stack_size > 0
  11362. && global_idx == module->aux_stack_top_global_index) {
  11363. #if WASM_ENABLE_DEBUG_INTERP != 0
  11364. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11365. error_buf_size)) {
  11366. goto fail;
  11367. }
  11368. #endif
  11369. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11370. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11371. func->has_op_set_global_aux_stack = true;
  11372. #endif
  11373. }
  11374. #else /* else of WASM_ENABLE_FAST_INTERP */
  11375. if (global_type == VALUE_TYPE_I64
  11376. || global_type == VALUE_TYPE_F64) {
  11377. skip_label();
  11378. emit_label(WASM_OP_SET_GLOBAL_64);
  11379. }
  11380. else if (module->aux_stack_size > 0
  11381. && global_idx == module->aux_stack_top_global_index) {
  11382. skip_label();
  11383. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11384. }
  11385. emit_uint32(loader_ctx, global_idx);
  11386. POP_OFFSET_TYPE(global_type);
  11387. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11388. POP_TYPE(global_type);
  11389. break;
  11390. }
  11391. /* load */
  11392. case WASM_OP_I32_LOAD:
  11393. case WASM_OP_I32_LOAD8_S:
  11394. case WASM_OP_I32_LOAD8_U:
  11395. case WASM_OP_I32_LOAD16_S:
  11396. case WASM_OP_I32_LOAD16_U:
  11397. case WASM_OP_I64_LOAD:
  11398. case WASM_OP_I64_LOAD8_S:
  11399. case WASM_OP_I64_LOAD8_U:
  11400. case WASM_OP_I64_LOAD16_S:
  11401. case WASM_OP_I64_LOAD16_U:
  11402. case WASM_OP_I64_LOAD32_S:
  11403. case WASM_OP_I64_LOAD32_U:
  11404. case WASM_OP_F32_LOAD:
  11405. case WASM_OP_F64_LOAD:
  11406. /* store */
  11407. case WASM_OP_I32_STORE:
  11408. case WASM_OP_I32_STORE8:
  11409. case WASM_OP_I32_STORE16:
  11410. case WASM_OP_I64_STORE:
  11411. case WASM_OP_I64_STORE8:
  11412. case WASM_OP_I64_STORE16:
  11413. case WASM_OP_I64_STORE32:
  11414. case WASM_OP_F32_STORE:
  11415. case WASM_OP_F64_STORE:
  11416. {
  11417. #if WASM_ENABLE_FAST_INTERP != 0
  11418. /* change F32/F64 into I32/I64 */
  11419. if (opcode == WASM_OP_F32_LOAD) {
  11420. skip_label();
  11421. emit_label(WASM_OP_I32_LOAD);
  11422. }
  11423. else if (opcode == WASM_OP_F64_LOAD) {
  11424. skip_label();
  11425. emit_label(WASM_OP_I64_LOAD);
  11426. }
  11427. else if (opcode == WASM_OP_F32_STORE) {
  11428. skip_label();
  11429. emit_label(WASM_OP_I32_STORE);
  11430. }
  11431. else if (opcode == WASM_OP_F64_STORE) {
  11432. skip_label();
  11433. emit_label(WASM_OP_I64_STORE);
  11434. }
  11435. #endif
  11436. CHECK_MEMORY();
  11437. read_leb_uint32(p, p_end, align); /* align */
  11438. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11439. if (!check_memory_access_align(opcode, align, error_buf,
  11440. error_buf_size)) {
  11441. goto fail;
  11442. }
  11443. #if WASM_ENABLE_FAST_INTERP != 0
  11444. emit_uint32(loader_ctx, mem_offset);
  11445. #endif
  11446. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11447. func->has_memory_operations = true;
  11448. #endif
  11449. switch (opcode) {
  11450. /* load */
  11451. case WASM_OP_I32_LOAD:
  11452. case WASM_OP_I32_LOAD8_S:
  11453. case WASM_OP_I32_LOAD8_U:
  11454. case WASM_OP_I32_LOAD16_S:
  11455. case WASM_OP_I32_LOAD16_U:
  11456. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11457. break;
  11458. case WASM_OP_I64_LOAD:
  11459. case WASM_OP_I64_LOAD8_S:
  11460. case WASM_OP_I64_LOAD8_U:
  11461. case WASM_OP_I64_LOAD16_S:
  11462. case WASM_OP_I64_LOAD16_U:
  11463. case WASM_OP_I64_LOAD32_S:
  11464. case WASM_OP_I64_LOAD32_U:
  11465. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11466. break;
  11467. case WASM_OP_F32_LOAD:
  11468. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11469. break;
  11470. case WASM_OP_F64_LOAD:
  11471. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  11472. break;
  11473. /* store */
  11474. case WASM_OP_I32_STORE:
  11475. case WASM_OP_I32_STORE8:
  11476. case WASM_OP_I32_STORE16:
  11477. POP_I32();
  11478. POP_I32();
  11479. break;
  11480. case WASM_OP_I64_STORE:
  11481. case WASM_OP_I64_STORE8:
  11482. case WASM_OP_I64_STORE16:
  11483. case WASM_OP_I64_STORE32:
  11484. POP_I64();
  11485. POP_I32();
  11486. break;
  11487. case WASM_OP_F32_STORE:
  11488. POP_F32();
  11489. POP_I32();
  11490. break;
  11491. case WASM_OP_F64_STORE:
  11492. POP_F64();
  11493. POP_I32();
  11494. break;
  11495. default:
  11496. break;
  11497. }
  11498. break;
  11499. }
  11500. case WASM_OP_MEMORY_SIZE:
  11501. CHECK_MEMORY();
  11502. /* reserved byte 0x00 */
  11503. if (*p++ != 0x00) {
  11504. set_error_buf(error_buf, error_buf_size,
  11505. "zero byte expected");
  11506. goto fail;
  11507. }
  11508. PUSH_I32();
  11509. module->possible_memory_grow = true;
  11510. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11511. func->has_memory_operations = true;
  11512. #endif
  11513. break;
  11514. case WASM_OP_MEMORY_GROW:
  11515. CHECK_MEMORY();
  11516. /* reserved byte 0x00 */
  11517. if (*p++ != 0x00) {
  11518. set_error_buf(error_buf, error_buf_size,
  11519. "zero byte expected");
  11520. goto fail;
  11521. }
  11522. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11523. module->possible_memory_grow = true;
  11524. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11525. || WASM_ENABLE_WAMR_COMPILER != 0
  11526. func->has_op_memory_grow = true;
  11527. #endif
  11528. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11529. func->has_memory_operations = true;
  11530. #endif
  11531. break;
  11532. case WASM_OP_I32_CONST:
  11533. read_leb_int32(p, p_end, i32_const);
  11534. #if WASM_ENABLE_FAST_INTERP != 0
  11535. skip_label();
  11536. disable_emit = true;
  11537. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11538. if (operand_offset == 0) {
  11539. disable_emit = false;
  11540. emit_label(WASM_OP_I32_CONST);
  11541. emit_uint32(loader_ctx, i32_const);
  11542. }
  11543. #else
  11544. (void)i32_const;
  11545. #endif
  11546. PUSH_I32();
  11547. break;
  11548. case WASM_OP_I64_CONST:
  11549. read_leb_int64(p, p_end, i64_const);
  11550. #if WASM_ENABLE_FAST_INTERP != 0
  11551. skip_label();
  11552. disable_emit = true;
  11553. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11554. if (operand_offset == 0) {
  11555. disable_emit = false;
  11556. emit_label(WASM_OP_I64_CONST);
  11557. emit_uint64(loader_ctx, i64_const);
  11558. }
  11559. #endif
  11560. PUSH_I64();
  11561. break;
  11562. case WASM_OP_F32_CONST:
  11563. p += sizeof(float32);
  11564. #if WASM_ENABLE_FAST_INTERP != 0
  11565. skip_label();
  11566. disable_emit = true;
  11567. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11568. sizeof(float32));
  11569. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11570. if (operand_offset == 0) {
  11571. disable_emit = false;
  11572. emit_label(WASM_OP_F32_CONST);
  11573. emit_float32(loader_ctx, f32_const);
  11574. }
  11575. #endif
  11576. PUSH_F32();
  11577. break;
  11578. case WASM_OP_F64_CONST:
  11579. p += sizeof(float64);
  11580. #if WASM_ENABLE_FAST_INTERP != 0
  11581. skip_label();
  11582. disable_emit = true;
  11583. /* Some MCU may require 8-byte align */
  11584. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  11585. sizeof(float64));
  11586. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  11587. if (operand_offset == 0) {
  11588. disable_emit = false;
  11589. emit_label(WASM_OP_F64_CONST);
  11590. emit_float64(loader_ctx, f64_const);
  11591. }
  11592. #endif
  11593. PUSH_F64();
  11594. break;
  11595. case WASM_OP_I32_EQZ:
  11596. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11597. break;
  11598. case WASM_OP_I32_EQ:
  11599. case WASM_OP_I32_NE:
  11600. case WASM_OP_I32_LT_S:
  11601. case WASM_OP_I32_LT_U:
  11602. case WASM_OP_I32_GT_S:
  11603. case WASM_OP_I32_GT_U:
  11604. case WASM_OP_I32_LE_S:
  11605. case WASM_OP_I32_LE_U:
  11606. case WASM_OP_I32_GE_S:
  11607. case WASM_OP_I32_GE_U:
  11608. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11609. break;
  11610. case WASM_OP_I64_EQZ:
  11611. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11612. break;
  11613. case WASM_OP_I64_EQ:
  11614. case WASM_OP_I64_NE:
  11615. case WASM_OP_I64_LT_S:
  11616. case WASM_OP_I64_LT_U:
  11617. case WASM_OP_I64_GT_S:
  11618. case WASM_OP_I64_GT_U:
  11619. case WASM_OP_I64_LE_S:
  11620. case WASM_OP_I64_LE_U:
  11621. case WASM_OP_I64_GE_S:
  11622. case WASM_OP_I64_GE_U:
  11623. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11624. break;
  11625. case WASM_OP_F32_EQ:
  11626. case WASM_OP_F32_NE:
  11627. case WASM_OP_F32_LT:
  11628. case WASM_OP_F32_GT:
  11629. case WASM_OP_F32_LE:
  11630. case WASM_OP_F32_GE:
  11631. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11632. break;
  11633. case WASM_OP_F64_EQ:
  11634. case WASM_OP_F64_NE:
  11635. case WASM_OP_F64_LT:
  11636. case WASM_OP_F64_GT:
  11637. case WASM_OP_F64_LE:
  11638. case WASM_OP_F64_GE:
  11639. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11640. break;
  11641. case WASM_OP_I32_CLZ:
  11642. case WASM_OP_I32_CTZ:
  11643. case WASM_OP_I32_POPCNT:
  11644. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11645. break;
  11646. case WASM_OP_I32_ADD:
  11647. case WASM_OP_I32_SUB:
  11648. case WASM_OP_I32_MUL:
  11649. case WASM_OP_I32_DIV_S:
  11650. case WASM_OP_I32_DIV_U:
  11651. case WASM_OP_I32_REM_S:
  11652. case WASM_OP_I32_REM_U:
  11653. case WASM_OP_I32_AND:
  11654. case WASM_OP_I32_OR:
  11655. case WASM_OP_I32_XOR:
  11656. case WASM_OP_I32_SHL:
  11657. case WASM_OP_I32_SHR_S:
  11658. case WASM_OP_I32_SHR_U:
  11659. case WASM_OP_I32_ROTL:
  11660. case WASM_OP_I32_ROTR:
  11661. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11662. break;
  11663. case WASM_OP_I64_CLZ:
  11664. case WASM_OP_I64_CTZ:
  11665. case WASM_OP_I64_POPCNT:
  11666. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11667. break;
  11668. case WASM_OP_I64_ADD:
  11669. case WASM_OP_I64_SUB:
  11670. case WASM_OP_I64_MUL:
  11671. case WASM_OP_I64_DIV_S:
  11672. case WASM_OP_I64_DIV_U:
  11673. case WASM_OP_I64_REM_S:
  11674. case WASM_OP_I64_REM_U:
  11675. case WASM_OP_I64_AND:
  11676. case WASM_OP_I64_OR:
  11677. case WASM_OP_I64_XOR:
  11678. case WASM_OP_I64_SHL:
  11679. case WASM_OP_I64_SHR_S:
  11680. case WASM_OP_I64_SHR_U:
  11681. case WASM_OP_I64_ROTL:
  11682. case WASM_OP_I64_ROTR:
  11683. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11684. break;
  11685. case WASM_OP_F32_ABS:
  11686. case WASM_OP_F32_NEG:
  11687. case WASM_OP_F32_CEIL:
  11688. case WASM_OP_F32_FLOOR:
  11689. case WASM_OP_F32_TRUNC:
  11690. case WASM_OP_F32_NEAREST:
  11691. case WASM_OP_F32_SQRT:
  11692. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11693. break;
  11694. case WASM_OP_F32_ADD:
  11695. case WASM_OP_F32_SUB:
  11696. case WASM_OP_F32_MUL:
  11697. case WASM_OP_F32_DIV:
  11698. case WASM_OP_F32_MIN:
  11699. case WASM_OP_F32_MAX:
  11700. case WASM_OP_F32_COPYSIGN:
  11701. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11702. break;
  11703. case WASM_OP_F64_ABS:
  11704. case WASM_OP_F64_NEG:
  11705. case WASM_OP_F64_CEIL:
  11706. case WASM_OP_F64_FLOOR:
  11707. case WASM_OP_F64_TRUNC:
  11708. case WASM_OP_F64_NEAREST:
  11709. case WASM_OP_F64_SQRT:
  11710. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11711. break;
  11712. case WASM_OP_F64_ADD:
  11713. case WASM_OP_F64_SUB:
  11714. case WASM_OP_F64_MUL:
  11715. case WASM_OP_F64_DIV:
  11716. case WASM_OP_F64_MIN:
  11717. case WASM_OP_F64_MAX:
  11718. case WASM_OP_F64_COPYSIGN:
  11719. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11720. break;
  11721. case WASM_OP_I32_WRAP_I64:
  11722. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11723. break;
  11724. case WASM_OP_I32_TRUNC_S_F32:
  11725. case WASM_OP_I32_TRUNC_U_F32:
  11726. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11727. break;
  11728. case WASM_OP_I32_TRUNC_S_F64:
  11729. case WASM_OP_I32_TRUNC_U_F64:
  11730. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11731. break;
  11732. case WASM_OP_I64_EXTEND_S_I32:
  11733. case WASM_OP_I64_EXTEND_U_I32:
  11734. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11735. break;
  11736. case WASM_OP_I64_TRUNC_S_F32:
  11737. case WASM_OP_I64_TRUNC_U_F32:
  11738. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  11739. break;
  11740. case WASM_OP_I64_TRUNC_S_F64:
  11741. case WASM_OP_I64_TRUNC_U_F64:
  11742. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  11743. break;
  11744. case WASM_OP_F32_CONVERT_S_I32:
  11745. case WASM_OP_F32_CONVERT_U_I32:
  11746. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11747. break;
  11748. case WASM_OP_F32_CONVERT_S_I64:
  11749. case WASM_OP_F32_CONVERT_U_I64:
  11750. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  11751. break;
  11752. case WASM_OP_F32_DEMOTE_F64:
  11753. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  11754. break;
  11755. case WASM_OP_F64_CONVERT_S_I32:
  11756. case WASM_OP_F64_CONVERT_U_I32:
  11757. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  11758. break;
  11759. case WASM_OP_F64_CONVERT_S_I64:
  11760. case WASM_OP_F64_CONVERT_U_I64:
  11761. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  11762. break;
  11763. case WASM_OP_F64_PROMOTE_F32:
  11764. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  11765. break;
  11766. case WASM_OP_I32_REINTERPRET_F32:
  11767. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11768. break;
  11769. case WASM_OP_I64_REINTERPRET_F64:
  11770. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  11771. break;
  11772. case WASM_OP_F32_REINTERPRET_I32:
  11773. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  11774. break;
  11775. case WASM_OP_F64_REINTERPRET_I64:
  11776. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  11777. break;
  11778. case WASM_OP_I32_EXTEND8_S:
  11779. case WASM_OP_I32_EXTEND16_S:
  11780. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11781. break;
  11782. case WASM_OP_I64_EXTEND8_S:
  11783. case WASM_OP_I64_EXTEND16_S:
  11784. case WASM_OP_I64_EXTEND32_S:
  11785. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11786. break;
  11787. #if WASM_ENABLE_GC != 0
  11788. case WASM_OP_GC_PREFIX:
  11789. {
  11790. uint32 opcode1;
  11791. read_leb_uint32(p, p_end, opcode1);
  11792. #if WASM_ENABLE_FAST_INTERP != 0
  11793. emit_byte(loader_ctx, ((uint8)opcode1));
  11794. #endif
  11795. switch (opcode1) {
  11796. case WASM_OP_STRUCT_NEW:
  11797. case WASM_OP_STRUCT_NEW_DEFAULT:
  11798. {
  11799. read_leb_uint32(p, p_end, type_idx);
  11800. #if WASM_ENABLE_FAST_INTERP != 0
  11801. emit_uint32(loader_ctx, type_idx);
  11802. #endif
  11803. if (!check_type_index(module, type_idx, error_buf,
  11804. error_buf_size)) {
  11805. goto fail;
  11806. }
  11807. if (module->types[type_idx]->type_flag
  11808. != WASM_TYPE_STRUCT) {
  11809. set_error_buf(error_buf, error_buf_size,
  11810. "unkown struct type");
  11811. goto fail;
  11812. }
  11813. if (opcode1 == WASM_OP_STRUCT_NEW) {
  11814. int32 j, k;
  11815. uint8 value_type;
  11816. uint32 ref_type_struct_size;
  11817. WASMStructType *struct_type =
  11818. (WASMStructType *)module->types[type_idx];
  11819. k = struct_type->ref_type_map_count - 1;
  11820. for (j = struct_type->field_count - 1; j >= 0;
  11821. j--) {
  11822. value_type = struct_type->fields[j].field_type;
  11823. if (wasm_is_type_reftype(value_type)) {
  11824. if (wasm_is_type_multi_byte_type(
  11825. value_type)) {
  11826. ref_type_struct_size =
  11827. wasm_reftype_struct_size(
  11828. struct_type->ref_type_maps[k]
  11829. .ref_type);
  11830. bh_memcpy_s(
  11831. &wasm_ref_type,
  11832. (uint32)sizeof(WASMRefType),
  11833. struct_type->ref_type_maps[k]
  11834. .ref_type,
  11835. ref_type_struct_size);
  11836. k--;
  11837. }
  11838. POP_REF(value_type);
  11839. }
  11840. else {
  11841. switch (value_type) {
  11842. case VALUE_TYPE_I32:
  11843. case PACKED_TYPE_I8:
  11844. case PACKED_TYPE_I16:
  11845. POP_I32();
  11846. break;
  11847. case VALUE_TYPE_I64:
  11848. POP_I64();
  11849. break;
  11850. case VALUE_TYPE_F32:
  11851. POP_F32();
  11852. break;
  11853. case VALUE_TYPE_F64:
  11854. POP_F64();
  11855. break;
  11856. default:
  11857. set_error_buf(error_buf,
  11858. error_buf_size,
  11859. "unknown type");
  11860. goto fail;
  11861. }
  11862. }
  11863. }
  11864. }
  11865. /* PUSH struct obj, (ref $t) */
  11866. wasm_set_refheaptype_typeidx(
  11867. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  11868. PUSH_REF(wasm_ref_type.ref_type);
  11869. break;
  11870. }
  11871. case WASM_OP_STRUCT_GET:
  11872. case WASM_OP_STRUCT_GET_S:
  11873. case WASM_OP_STRUCT_GET_U:
  11874. case WASM_OP_STRUCT_SET:
  11875. {
  11876. WASMStructType *struct_type;
  11877. WASMRefType *ref_type = NULL;
  11878. uint32 field_idx;
  11879. uint8 field_type;
  11880. read_leb_uint32(p, p_end, type_idx);
  11881. #if WASM_ENABLE_FAST_INTERP != 0
  11882. emit_uint32(loader_ctx, type_idx);
  11883. #endif
  11884. if (!check_type_index(module, type_idx, error_buf,
  11885. error_buf_size)) {
  11886. goto fail;
  11887. }
  11888. if (module->types[type_idx]->type_flag
  11889. != WASM_TYPE_STRUCT) {
  11890. set_error_buf(error_buf, error_buf_size,
  11891. "unknown struct type");
  11892. goto fail;
  11893. }
  11894. struct_type = (WASMStructType *)module->types[type_idx];
  11895. read_leb_uint32(p, p_end, field_idx);
  11896. #if WASM_ENABLE_FAST_INTERP != 0
  11897. emit_uint32(loader_ctx, field_idx);
  11898. #endif
  11899. if (field_idx >= struct_type->field_count) {
  11900. set_error_buf(error_buf, error_buf_size,
  11901. "unknown struct field");
  11902. goto fail;
  11903. }
  11904. if (opcode1 == WASM_OP_STRUCT_SET
  11905. && !(struct_type->fields[field_idx].field_flags
  11906. & 1)) {
  11907. set_error_buf(error_buf, error_buf_size,
  11908. "field is immutable");
  11909. goto fail;
  11910. }
  11911. field_type = struct_type->fields[field_idx].field_type;
  11912. if (is_packed_type(field_type)) {
  11913. if (opcode1 == WASM_OP_STRUCT_GET) {
  11914. set_error_buf(error_buf, error_buf_size,
  11915. "type mismatch");
  11916. goto fail;
  11917. }
  11918. else {
  11919. field_type = VALUE_TYPE_I32;
  11920. }
  11921. }
  11922. if (wasm_is_type_multi_byte_type(field_type)) {
  11923. ref_type = wasm_reftype_map_find(
  11924. struct_type->ref_type_maps,
  11925. struct_type->ref_type_map_count, field_idx);
  11926. bh_assert(ref_type);
  11927. }
  11928. if (opcode1 == WASM_OP_STRUCT_SET) {
  11929. /* POP field */
  11930. if (wasm_is_type_multi_byte_type(field_type)) {
  11931. bh_memcpy_s(&wasm_ref_type,
  11932. (uint32)sizeof(WASMRefType),
  11933. ref_type,
  11934. wasm_reftype_struct_size(ref_type));
  11935. }
  11936. POP_REF(field_type);
  11937. /* POP struct obj, (ref null $t) */
  11938. wasm_set_refheaptype_typeidx(
  11939. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  11940. POP_REF(wasm_ref_type.ref_type);
  11941. }
  11942. else {
  11943. /* POP struct obj, (ref null $t) */
  11944. wasm_set_refheaptype_typeidx(
  11945. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  11946. POP_REF(wasm_ref_type.ref_type);
  11947. /* PUSH field */
  11948. if (wasm_is_type_multi_byte_type(field_type)) {
  11949. bh_memcpy_s(&wasm_ref_type,
  11950. (uint32)sizeof(WASMRefType),
  11951. ref_type,
  11952. wasm_reftype_struct_size(ref_type));
  11953. }
  11954. PUSH_REF(field_type);
  11955. }
  11956. break;
  11957. }
  11958. case WASM_OP_ARRAY_NEW:
  11959. case WASM_OP_ARRAY_NEW_DEFAULT:
  11960. case WASM_OP_ARRAY_NEW_FIXED:
  11961. case WASM_OP_ARRAY_NEW_DATA:
  11962. case WASM_OP_ARRAY_NEW_ELEM:
  11963. {
  11964. WASMArrayType *array_type;
  11965. uint8 elem_type;
  11966. uint32 u32 = 0;
  11967. read_leb_uint32(p, p_end, type_idx);
  11968. #if WASM_ENABLE_FAST_INTERP != 0
  11969. emit_uint32(loader_ctx, type_idx);
  11970. #endif
  11971. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  11972. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  11973. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  11974. read_leb_uint32(p, p_end, u32);
  11975. #if WASM_ENABLE_FAST_INTERP != 0
  11976. emit_uint32(loader_ctx, u32);
  11977. #endif
  11978. }
  11979. if (!check_array_type(module, type_idx, error_buf,
  11980. error_buf_size)) {
  11981. goto fail;
  11982. }
  11983. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  11984. /* length */
  11985. POP_I32();
  11986. }
  11987. array_type = (WASMArrayType *)module->types[type_idx];
  11988. elem_type = array_type->elem_type;
  11989. if (opcode1 == WASM_OP_ARRAY_NEW
  11990. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  11991. if (wasm_is_type_multi_byte_type(elem_type)) {
  11992. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11993. array_type->elem_ref_type,
  11994. wasm_reftype_struct_size(
  11995. array_type->elem_ref_type));
  11996. }
  11997. if (is_packed_type(elem_type)) {
  11998. elem_type = VALUE_TYPE_I32;
  11999. }
  12000. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12001. uint32 N = u32;
  12002. for (i = 0; i < N; i++) {
  12003. if (wasm_is_type_multi_byte_type(
  12004. elem_type)) {
  12005. bh_memcpy_s(
  12006. &wasm_ref_type, sizeof(WASMRefType),
  12007. array_type->elem_ref_type,
  12008. wasm_reftype_struct_size(
  12009. array_type->elem_ref_type));
  12010. }
  12011. POP_REF(elem_type);
  12012. }
  12013. }
  12014. else
  12015. POP_REF(elem_type);
  12016. }
  12017. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12018. /* offset of data segment */
  12019. POP_I32();
  12020. if (u32 >= module->data_seg_count) {
  12021. set_error_buf(error_buf, error_buf_size,
  12022. "unknown data segement");
  12023. goto fail;
  12024. }
  12025. if (wasm_is_type_reftype(elem_type)) {
  12026. set_error_buf(error_buf, error_buf_size,
  12027. "array elem type mismatch");
  12028. goto fail;
  12029. }
  12030. }
  12031. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12032. WASMTableSeg *table_seg =
  12033. module->table_segments + u32;
  12034. /* offset of element segment */
  12035. POP_I32();
  12036. if (u32 >= module->table_seg_count) {
  12037. set_error_buf(error_buf, error_buf_size,
  12038. "unknown element segement");
  12039. goto fail;
  12040. }
  12041. if (!wasm_reftype_is_subtype_of(
  12042. table_seg->elem_type,
  12043. table_seg->elem_ref_type, elem_type,
  12044. array_type->elem_ref_type, module->types,
  12045. module->type_count)) {
  12046. set_error_buf(error_buf, error_buf_size,
  12047. "array elem type mismatch");
  12048. goto fail;
  12049. }
  12050. }
  12051. /* PUSH array obj, (ref $t) */
  12052. wasm_set_refheaptype_typeidx(
  12053. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12054. PUSH_REF(wasm_ref_type.ref_type);
  12055. break;
  12056. }
  12057. case WASM_OP_ARRAY_GET:
  12058. case WASM_OP_ARRAY_GET_S:
  12059. case WASM_OP_ARRAY_GET_U:
  12060. case WASM_OP_ARRAY_SET:
  12061. {
  12062. uint8 elem_type;
  12063. WASMArrayType *array_type;
  12064. WASMRefType *ref_type = NULL;
  12065. read_leb_uint32(p, p_end, type_idx);
  12066. #if WASM_ENABLE_FAST_INTERP != 0
  12067. emit_uint32(loader_ctx, type_idx);
  12068. #endif
  12069. if (!check_array_type(module, type_idx, error_buf,
  12070. error_buf_size)) {
  12071. goto fail;
  12072. }
  12073. array_type = (WASMArrayType *)module->types[type_idx];
  12074. if (opcode1 == WASM_OP_ARRAY_SET
  12075. && !(array_type->elem_flags & 1)) {
  12076. set_error_buf(error_buf, error_buf_size,
  12077. "array is immutable");
  12078. goto fail;
  12079. }
  12080. elem_type = array_type->elem_type;
  12081. if (is_packed_type(elem_type)) {
  12082. if (opcode1 != WASM_OP_ARRAY_GET_S
  12083. && opcode1 != WASM_OP_ARRAY_GET_U
  12084. && opcode1 != WASM_OP_ARRAY_SET) {
  12085. set_error_buf(error_buf, error_buf_size,
  12086. "type mismatch");
  12087. goto fail;
  12088. }
  12089. else {
  12090. elem_type = VALUE_TYPE_I32;
  12091. }
  12092. }
  12093. ref_type = array_type->elem_ref_type;
  12094. if (opcode1 == WASM_OP_ARRAY_SET) {
  12095. /* POP elem to set */
  12096. if (wasm_is_type_multi_byte_type(elem_type)) {
  12097. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12098. ref_type,
  12099. wasm_reftype_struct_size(ref_type));
  12100. }
  12101. POP_REF(elem_type);
  12102. }
  12103. /* elem idx */
  12104. POP_I32();
  12105. /* POP array obj, (ref null $t) */
  12106. wasm_set_refheaptype_typeidx(
  12107. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12108. POP_REF(wasm_ref_type.ref_type);
  12109. if (opcode1 != WASM_OP_ARRAY_SET) {
  12110. /* PUSH elem */
  12111. if (wasm_is_type_multi_byte_type(elem_type)) {
  12112. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12113. ref_type,
  12114. wasm_reftype_struct_size(ref_type));
  12115. }
  12116. PUSH_REF(elem_type);
  12117. }
  12118. break;
  12119. }
  12120. case WASM_OP_ARRAY_LEN:
  12121. {
  12122. POP_REF(REF_TYPE_ARRAYREF);
  12123. /* length */
  12124. PUSH_I32();
  12125. break;
  12126. }
  12127. case WASM_OP_ARRAY_FILL:
  12128. {
  12129. WASMArrayType *array_type;
  12130. uint8 elem_type;
  12131. /* typeidx */
  12132. read_leb_uint32(p, p_end, type_idx);
  12133. #if WASM_ENABLE_FAST_INTERP != 0
  12134. emit_uint32(loader_ctx, type_idx);
  12135. #endif
  12136. if (!check_array_type(module, type_idx, error_buf,
  12137. error_buf_size)) {
  12138. goto fail;
  12139. }
  12140. array_type = (WASMArrayType *)module->types[type_idx];
  12141. if (!(array_type->elem_flags & 1)) {
  12142. set_error_buf(error_buf, error_buf_size,
  12143. "array is immutable");
  12144. goto fail;
  12145. }
  12146. elem_type = array_type->elem_type;
  12147. if (is_packed_type(elem_type)) {
  12148. elem_type = VALUE_TYPE_I32;
  12149. }
  12150. POP_I32(); /* length */
  12151. #if WASM_ENABLE_FAST_INTERP != 0
  12152. POP_OFFSET_TYPE(elem_type);
  12153. #endif
  12154. POP_TYPE(elem_type);
  12155. POP_I32(); /* start */
  12156. /* POP array obj, (ref null $t) */
  12157. wasm_set_refheaptype_typeidx(
  12158. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12159. POP_REF(wasm_ref_type.ref_type);
  12160. break;
  12161. }
  12162. case WASM_OP_ARRAY_COPY:
  12163. {
  12164. uint32 src_type_idx;
  12165. uint8 src_elem_type, dst_elem_type;
  12166. WASMRefType src_ref_type = { 0 },
  12167. *src_elem_ref_type = NULL;
  12168. WASMRefType dst_ref_type = { 0 },
  12169. *dst_elem_ref_type = NULL;
  12170. WASMArrayType *array_type;
  12171. /* typeidx1 */
  12172. read_leb_uint32(p, p_end, type_idx);
  12173. #if WASM_ENABLE_FAST_INTERP != 0
  12174. emit_uint32(loader_ctx, type_idx);
  12175. #endif
  12176. /* typeidx2 */
  12177. read_leb_uint32(p, p_end, src_type_idx);
  12178. #if WASM_ENABLE_FAST_INTERP != 0
  12179. emit_uint32(loader_ctx, src_type_idx);
  12180. #endif
  12181. if (!check_array_type(module, type_idx, error_buf,
  12182. error_buf_size)) {
  12183. goto fail;
  12184. }
  12185. if (!check_array_type(module, src_type_idx, error_buf,
  12186. error_buf_size)) {
  12187. goto fail;
  12188. }
  12189. POP_I32();
  12190. POP_I32();
  12191. /* POP array obj, (ref null $t) */
  12192. wasm_set_refheaptype_typeidx(
  12193. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12194. POP_REF(wasm_ref_type.ref_type);
  12195. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12196. &wasm_ref_type,
  12197. wasm_reftype_struct_size(&wasm_ref_type));
  12198. POP_I32();
  12199. /* POP array obj, (ref null $t) */
  12200. wasm_set_refheaptype_typeidx(
  12201. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12202. POP_REF(wasm_ref_type.ref_type);
  12203. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12204. &wasm_ref_type,
  12205. wasm_reftype_struct_size(&wasm_ref_type));
  12206. array_type = (WASMArrayType *)module->types[type_idx];
  12207. if (!(array_type->elem_flags & 1)) {
  12208. set_error_buf(error_buf, error_buf_size,
  12209. "destination array is immutable");
  12210. goto fail;
  12211. }
  12212. dst_elem_type = array_type->elem_type;
  12213. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12214. dst_elem_ref_type = array_type->elem_ref_type;
  12215. }
  12216. array_type =
  12217. (WASMArrayType *)module->types[src_type_idx];
  12218. src_elem_type = array_type->elem_type;
  12219. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12220. src_elem_ref_type = array_type->elem_ref_type;
  12221. }
  12222. if (!wasm_reftype_is_subtype_of(
  12223. src_elem_type, src_elem_ref_type, dst_elem_type,
  12224. dst_elem_ref_type, module->types,
  12225. module->type_count)) {
  12226. set_error_buf(error_buf, error_buf_size,
  12227. "array types do not match");
  12228. goto fail;
  12229. }
  12230. break;
  12231. }
  12232. case WASM_OP_REF_I31:
  12233. {
  12234. POP_I32();
  12235. wasm_set_refheaptype_common(
  12236. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12237. PUSH_REF(wasm_ref_type.ref_type);
  12238. break;
  12239. }
  12240. case WASM_OP_I31_GET_S:
  12241. case WASM_OP_I31_GET_U:
  12242. {
  12243. POP_REF(REF_TYPE_I31REF);
  12244. PUSH_I32();
  12245. break;
  12246. }
  12247. case WASM_OP_REF_TEST:
  12248. case WASM_OP_REF_CAST:
  12249. case WASM_OP_REF_TEST_NULLABLE:
  12250. case WASM_OP_REF_CAST_NULLABLE:
  12251. {
  12252. uint8 type;
  12253. read_leb_int32(p, p_end, heap_type);
  12254. #if WASM_ENABLE_FAST_INTERP != 0
  12255. emit_uint32(loader_ctx, (uint32)heap_type);
  12256. #endif
  12257. if (heap_type >= 0) {
  12258. if (!check_type_index(module, heap_type, error_buf,
  12259. error_buf_size)) {
  12260. goto fail;
  12261. }
  12262. }
  12263. else {
  12264. if (!wasm_is_valid_heap_type(heap_type)) {
  12265. set_error_buf(error_buf, error_buf_size,
  12266. "unknown type");
  12267. goto fail;
  12268. }
  12269. }
  12270. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12271. &wasm_ref_type, error_buf,
  12272. error_buf_size)) {
  12273. goto fail;
  12274. }
  12275. if (opcode1 == WASM_OP_REF_TEST
  12276. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12277. PUSH_I32();
  12278. else {
  12279. bool nullable =
  12280. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12281. : false;
  12282. if (heap_type >= 0 || !nullable) {
  12283. wasm_set_refheaptype_typeidx(
  12284. &wasm_ref_type.ref_ht_typeidx, nullable,
  12285. heap_type);
  12286. PUSH_REF(wasm_ref_type.ref_type);
  12287. }
  12288. else {
  12289. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12290. }
  12291. }
  12292. break;
  12293. }
  12294. case WASM_OP_BR_ON_CAST:
  12295. case WASM_OP_BR_ON_CAST_FAIL:
  12296. {
  12297. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12298. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12299. uint8 type_tmp, castflags;
  12300. uint32 depth;
  12301. int32 heap_type_dst;
  12302. bool src_nullable, dst_nullable;
  12303. CHECK_BUF(p, p_end, 1);
  12304. castflags = read_uint8(p);
  12305. #if WASM_ENABLE_FAST_INTERP != 0
  12306. /* Emit heap_type firstly */
  12307. emit_byte(loader_ctx, castflags);
  12308. #endif
  12309. p_org = p;
  12310. read_leb_uint32(p, p_end, depth);
  12311. read_leb_int32(p, p_end, heap_type);
  12312. #if WASM_ENABLE_FAST_INTERP != 0
  12313. /* Emit heap_type firstly */
  12314. emit_uint32(loader_ctx, (uint32)heap_type);
  12315. #endif
  12316. read_leb_int32(p, p_end, heap_type_dst);
  12317. #if WASM_ENABLE_FAST_INTERP != 0
  12318. /* Emit heap_type firstly */
  12319. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12320. #endif
  12321. (void)depth;
  12322. /*
  12323. * castflags should be 0~3:
  12324. * 0: (non-null, non-null)
  12325. * 1: (null, non-null)
  12326. * 2: (non-null, null)
  12327. * 3: (null, null)
  12328. */
  12329. if (castflags > 3) {
  12330. set_error_buf(error_buf, error_buf_size,
  12331. "invalid castflags");
  12332. break;
  12333. }
  12334. src_nullable =
  12335. (castflags == 1) || (castflags == 3) ? true : false;
  12336. dst_nullable =
  12337. (castflags == 2) || (castflags == 3) ? true : false;
  12338. /* Pop and backup the stack top's ref type */
  12339. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12340. &ref_type_tmp, error_buf,
  12341. error_buf_size)) {
  12342. goto fail;
  12343. }
  12344. /* The reference type rt1 must be valid */
  12345. if (!init_ref_type(module, &ref_type1, src_nullable,
  12346. heap_type, error_buf,
  12347. error_buf_size)) {
  12348. goto fail;
  12349. }
  12350. /* The reference type rt2 must be valid. */
  12351. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12352. heap_type_dst, error_buf,
  12353. error_buf_size)) {
  12354. goto fail;
  12355. }
  12356. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12357. &ref_type2);
  12358. /* The reference type rt2 must match rt1. */
  12359. if (!wasm_reftype_is_subtype_of(
  12360. ref_type2.ref_type, &ref_type2,
  12361. ref_type1.ref_type, &ref_type1, module->types,
  12362. module->type_count)) {
  12363. set_error_buf(error_buf, error_buf_size,
  12364. "type mismatch");
  12365. goto fail;
  12366. }
  12367. p = p_org;
  12368. /* Push ref type casted for branch block check */
  12369. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12370. /* The reference type rt2 must match rt′. */
  12371. type_tmp = ref_type2.ref_type;
  12372. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12373. bh_memcpy_s(
  12374. &wasm_ref_type,
  12375. wasm_reftype_struct_size(&ref_type2),
  12376. &ref_type2,
  12377. wasm_reftype_struct_size(&ref_type2));
  12378. }
  12379. }
  12380. else {
  12381. /* The reference type rt′1 must match rt′. */
  12382. type_tmp = ref_type_diff.ref_type;
  12383. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12384. bh_memcpy_s(
  12385. &wasm_ref_type,
  12386. wasm_reftype_struct_size(&ref_type_diff),
  12387. &ref_type_diff,
  12388. wasm_reftype_struct_size(&ref_type_diff));
  12389. }
  12390. }
  12391. PUSH_REF(type_tmp);
  12392. if (!(frame_csp_tmp = check_branch_block(
  12393. loader_ctx, &p, p_end, false, error_buf,
  12394. error_buf_size))) {
  12395. goto fail;
  12396. }
  12397. /* Ignore heap_types */
  12398. skip_leb_uint32(p, p_end);
  12399. skip_leb_uint32(p, p_end);
  12400. /* Restore the original stack top's ref type */
  12401. POP_REF(type_tmp);
  12402. #if WASM_ENABLE_FAST_INTERP != 0
  12403. /* Erase the opnd offset emitted by POP_REF() */
  12404. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12405. #endif
  12406. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12407. type_tmp = ref_type_diff.ref_type;
  12408. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12409. bh_memcpy_s(
  12410. &wasm_ref_type,
  12411. wasm_reftype_struct_size(&ref_type_diff),
  12412. &ref_type_diff,
  12413. wasm_reftype_struct_size(&ref_type_diff));
  12414. }
  12415. }
  12416. else {
  12417. type_tmp = ref_type2.ref_type;
  12418. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12419. bh_memcpy_s(
  12420. &wasm_ref_type,
  12421. wasm_reftype_struct_size(&ref_type2),
  12422. &ref_type2,
  12423. wasm_reftype_struct_size(&ref_type2));
  12424. }
  12425. }
  12426. PUSH_REF(type_tmp);
  12427. #if WASM_ENABLE_FAST_INTERP != 0
  12428. /* Erase the opnd offset emitted by PUSH_REF() */
  12429. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12430. #endif
  12431. break;
  12432. }
  12433. case WASM_OP_ANY_CONVERT_EXTERN:
  12434. {
  12435. uint8 type;
  12436. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12437. &wasm_ref_type, error_buf,
  12438. error_buf_size)) {
  12439. goto fail;
  12440. }
  12441. if (!(type == REF_TYPE_EXTERNREF
  12442. || (type == REF_TYPE_HT_NON_NULLABLE
  12443. && wasm_ref_type.ref_ht_common.heap_type
  12444. == HEAP_TYPE_EXTERN)
  12445. || type == VALUE_TYPE_ANY)) {
  12446. set_error_buf(error_buf, error_buf_size,
  12447. "type mismatch");
  12448. goto fail;
  12449. }
  12450. if (type == REF_TYPE_EXTERNREF)
  12451. type = REF_TYPE_ANYREF;
  12452. else {
  12453. wasm_ref_type.ref_ht_common.heap_type =
  12454. HEAP_TYPE_ANY;
  12455. }
  12456. PUSH_REF(type);
  12457. break;
  12458. }
  12459. case WASM_OP_EXTERN_CONVERT_ANY:
  12460. {
  12461. uint8 type;
  12462. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12463. &wasm_ref_type, error_buf,
  12464. error_buf_size)) {
  12465. goto fail;
  12466. }
  12467. if (type == REF_TYPE_EXTERNREF
  12468. || ((type == REF_TYPE_HT_NULLABLE
  12469. || type == REF_TYPE_HT_NON_NULLABLE)
  12470. && wasm_ref_type.ref_ht_common.heap_type
  12471. == HEAP_TYPE_EXTERN)) {
  12472. set_error_buf(error_buf, error_buf_size,
  12473. "type mismatch");
  12474. goto fail;
  12475. }
  12476. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12477. /* push (ref null extern) */
  12478. type = REF_TYPE_EXTERNREF;
  12479. }
  12480. else {
  12481. /* push (ref extern) */
  12482. type = REF_TYPE_HT_NON_NULLABLE;
  12483. wasm_set_refheaptype_common(
  12484. &wasm_ref_type.ref_ht_common, false,
  12485. HEAP_TYPE_EXTERN);
  12486. }
  12487. PUSH_REF(type);
  12488. break;
  12489. }
  12490. #if WASM_ENABLE_STRINGREF != 0
  12491. case WASM_OP_STRING_NEW_UTF8:
  12492. case WASM_OP_STRING_NEW_WTF16:
  12493. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12494. case WASM_OP_STRING_NEW_WTF8:
  12495. {
  12496. uint32 memidx;
  12497. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12498. func->has_memory_operations = true;
  12499. #endif
  12500. read_leb_uint32(p, p_end, memidx);
  12501. #if WASM_ENABLE_FAST_INTERP != 0
  12502. emit_uint32(loader_ctx, (uint32)memidx);
  12503. #endif
  12504. POP_I32();
  12505. POP_I32();
  12506. PUSH_REF(REF_TYPE_STRINGREF);
  12507. (void)memidx;
  12508. break;
  12509. }
  12510. case WASM_OP_STRING_CONST:
  12511. {
  12512. uint32 contents;
  12513. read_leb_uint32(p, p_end, contents);
  12514. #if WASM_ENABLE_FAST_INTERP != 0
  12515. emit_uint32(loader_ctx, (uint32)contents);
  12516. #endif
  12517. PUSH_REF(REF_TYPE_STRINGREF);
  12518. (void)contents;
  12519. break;
  12520. }
  12521. case WASM_OP_STRING_MEASURE_UTF8:
  12522. case WASM_OP_STRING_MEASURE_WTF8:
  12523. case WASM_OP_STRING_MEASURE_WTF16:
  12524. {
  12525. POP_STRINGREF();
  12526. PUSH_I32();
  12527. break;
  12528. }
  12529. case WASM_OP_STRING_ENCODE_UTF8:
  12530. case WASM_OP_STRING_ENCODE_WTF16:
  12531. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12532. case WASM_OP_STRING_ENCODE_WTF8:
  12533. {
  12534. uint32 memidx;
  12535. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12536. func->has_memory_operations = true;
  12537. #endif
  12538. read_leb_uint32(p, p_end, memidx);
  12539. #if WASM_ENABLE_FAST_INTERP != 0
  12540. emit_uint32(loader_ctx, (uint32)memidx);
  12541. #endif
  12542. POP_I32();
  12543. POP_STRINGREF();
  12544. PUSH_I32();
  12545. (void)memidx;
  12546. break;
  12547. }
  12548. case WASM_OP_STRING_CONCAT:
  12549. {
  12550. POP_STRINGREF();
  12551. POP_STRINGREF();
  12552. PUSH_REF(REF_TYPE_STRINGREF);
  12553. break;
  12554. }
  12555. case WASM_OP_STRING_EQ:
  12556. {
  12557. POP_STRINGREF();
  12558. POP_STRINGREF();
  12559. PUSH_I32();
  12560. break;
  12561. }
  12562. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12563. {
  12564. POP_STRINGREF();
  12565. PUSH_I32();
  12566. break;
  12567. }
  12568. case WASM_OP_STRING_AS_WTF8:
  12569. {
  12570. POP_STRINGREF();
  12571. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  12572. break;
  12573. }
  12574. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  12575. {
  12576. POP_I32();
  12577. POP_I32();
  12578. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12579. PUSH_I32();
  12580. break;
  12581. }
  12582. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  12583. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  12584. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  12585. {
  12586. uint32 memidx;
  12587. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12588. func->has_memory_operations = true;
  12589. #endif
  12590. read_leb_uint32(p, p_end, memidx);
  12591. #if WASM_ENABLE_FAST_INTERP != 0
  12592. emit_uint32(loader_ctx, (uint32)memidx);
  12593. #endif
  12594. POP_I32();
  12595. POP_I32();
  12596. POP_I32();
  12597. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12598. PUSH_I32();
  12599. PUSH_I32();
  12600. (void)memidx;
  12601. break;
  12602. }
  12603. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  12604. {
  12605. POP_I32();
  12606. POP_I32();
  12607. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12608. PUSH_REF(REF_TYPE_STRINGREF);
  12609. break;
  12610. }
  12611. case WASM_OP_STRING_AS_WTF16:
  12612. {
  12613. POP_STRINGREF();
  12614. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  12615. break;
  12616. }
  12617. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  12618. {
  12619. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12620. PUSH_I32();
  12621. break;
  12622. }
  12623. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  12624. {
  12625. POP_I32();
  12626. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12627. PUSH_I32();
  12628. break;
  12629. }
  12630. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  12631. {
  12632. uint32 memidx;
  12633. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12634. func->has_memory_operations = true;
  12635. #endif
  12636. read_leb_uint32(p, p_end, memidx);
  12637. #if WASM_ENABLE_FAST_INTERP != 0
  12638. emit_uint32(loader_ctx, (uint32)memidx);
  12639. #endif
  12640. POP_I32();
  12641. POP_I32();
  12642. POP_I32();
  12643. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12644. PUSH_I32();
  12645. (void)memidx;
  12646. break;
  12647. }
  12648. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  12649. {
  12650. POP_I32();
  12651. POP_I32();
  12652. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12653. PUSH_REF(REF_TYPE_STRINGREF);
  12654. break;
  12655. }
  12656. case WASM_OP_STRING_AS_ITER:
  12657. {
  12658. POP_STRINGREF();
  12659. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  12660. break;
  12661. }
  12662. case WASM_OP_STRINGVIEW_ITER_NEXT:
  12663. {
  12664. POP_REF(REF_TYPE_STRINGVIEWITER);
  12665. PUSH_I32();
  12666. break;
  12667. }
  12668. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  12669. case WASM_OP_STRINGVIEW_ITER_REWIND:
  12670. {
  12671. POP_I32();
  12672. POP_REF(REF_TYPE_STRINGVIEWITER);
  12673. PUSH_I32();
  12674. break;
  12675. }
  12676. case WASM_OP_STRINGVIEW_ITER_SLICE:
  12677. {
  12678. POP_I32();
  12679. POP_REF(REF_TYPE_STRINGVIEWITER);
  12680. PUSH_REF(REF_TYPE_STRINGREF);
  12681. break;
  12682. }
  12683. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  12684. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  12685. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  12686. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  12687. {
  12688. POP_I32();
  12689. POP_I32();
  12690. POP_REF(REF_TYPE_ARRAYREF);
  12691. PUSH_REF(REF_TYPE_STRINGREF);
  12692. break;
  12693. }
  12694. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  12695. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  12696. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  12697. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  12698. {
  12699. POP_I32();
  12700. POP_REF(REF_TYPE_ARRAYREF);
  12701. POP_STRINGREF();
  12702. PUSH_I32();
  12703. break;
  12704. }
  12705. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  12706. default:
  12707. set_error_buf_v(error_buf, error_buf_size,
  12708. "%s %02x %02x", "unsupported opcode",
  12709. 0xfb, opcode1);
  12710. goto fail;
  12711. }
  12712. break;
  12713. }
  12714. #endif /* end of WASM_ENABLE_GC != 0 */
  12715. case WASM_OP_MISC_PREFIX:
  12716. {
  12717. uint32 opcode1;
  12718. read_leb_uint32(p, p_end, opcode1);
  12719. #if WASM_ENABLE_FAST_INTERP != 0
  12720. emit_byte(loader_ctx, ((uint8)opcode1));
  12721. #endif
  12722. switch (opcode1) {
  12723. case WASM_OP_I32_TRUNC_SAT_S_F32:
  12724. case WASM_OP_I32_TRUNC_SAT_U_F32:
  12725. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12726. break;
  12727. case WASM_OP_I32_TRUNC_SAT_S_F64:
  12728. case WASM_OP_I32_TRUNC_SAT_U_F64:
  12729. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12730. break;
  12731. case WASM_OP_I64_TRUNC_SAT_S_F32:
  12732. case WASM_OP_I64_TRUNC_SAT_U_F32:
  12733. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12734. break;
  12735. case WASM_OP_I64_TRUNC_SAT_S_F64:
  12736. case WASM_OP_I64_TRUNC_SAT_U_F64:
  12737. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12738. break;
  12739. #if WASM_ENABLE_BULK_MEMORY != 0
  12740. case WASM_OP_MEMORY_INIT:
  12741. {
  12742. read_leb_uint32(p, p_end, data_seg_idx);
  12743. #if WASM_ENABLE_FAST_INTERP != 0
  12744. emit_uint32(loader_ctx, data_seg_idx);
  12745. #endif
  12746. if (module->import_memory_count == 0
  12747. && module->memory_count == 0)
  12748. goto fail_unknown_memory;
  12749. if (*p++ != 0x00)
  12750. goto fail_zero_byte_expected;
  12751. if (data_seg_idx >= module->data_seg_count) {
  12752. set_error_buf_v(error_buf, error_buf_size,
  12753. "unknown data segment %d",
  12754. data_seg_idx);
  12755. goto fail;
  12756. }
  12757. if (module->data_seg_count1 == 0)
  12758. goto fail_data_cnt_sec_require;
  12759. POP_I32();
  12760. POP_I32();
  12761. POP_I32();
  12762. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12763. func->has_memory_operations = true;
  12764. #endif
  12765. #if WASM_ENABLE_WAMR_COMPILER != 0
  12766. module->is_bulk_memory_used = true;
  12767. #endif
  12768. break;
  12769. }
  12770. case WASM_OP_DATA_DROP:
  12771. {
  12772. read_leb_uint32(p, p_end, data_seg_idx);
  12773. #if WASM_ENABLE_FAST_INTERP != 0
  12774. emit_uint32(loader_ctx, data_seg_idx);
  12775. #endif
  12776. if (data_seg_idx >= module->data_seg_count) {
  12777. set_error_buf(error_buf, error_buf_size,
  12778. "unknown data segment");
  12779. goto fail;
  12780. }
  12781. if (module->data_seg_count1 == 0)
  12782. goto fail_data_cnt_sec_require;
  12783. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12784. func->has_memory_operations = true;
  12785. #endif
  12786. #if WASM_ENABLE_WAMR_COMPILER != 0
  12787. module->is_bulk_memory_used = true;
  12788. #endif
  12789. break;
  12790. }
  12791. case WASM_OP_MEMORY_COPY:
  12792. {
  12793. /* both src and dst memory index should be 0 */
  12794. if (*(int16 *)p != 0x0000)
  12795. goto fail_zero_byte_expected;
  12796. p += 2;
  12797. if (module->import_memory_count == 0
  12798. && module->memory_count == 0)
  12799. goto fail_unknown_memory;
  12800. POP_I32();
  12801. POP_I32();
  12802. POP_I32();
  12803. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12804. func->has_memory_operations = true;
  12805. #endif
  12806. #if WASM_ENABLE_WAMR_COMPILER != 0
  12807. module->is_bulk_memory_used = true;
  12808. #endif
  12809. break;
  12810. }
  12811. case WASM_OP_MEMORY_FILL:
  12812. {
  12813. if (*p++ != 0x00) {
  12814. goto fail_zero_byte_expected;
  12815. }
  12816. if (module->import_memory_count == 0
  12817. && module->memory_count == 0) {
  12818. goto fail_unknown_memory;
  12819. }
  12820. POP_I32();
  12821. POP_I32();
  12822. POP_I32();
  12823. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12824. func->has_memory_operations = true;
  12825. #endif
  12826. #if WASM_ENABLE_WAMR_COMPILER != 0
  12827. module->is_bulk_memory_used = true;
  12828. #endif
  12829. break;
  12830. }
  12831. fail_zero_byte_expected:
  12832. set_error_buf(error_buf, error_buf_size,
  12833. "zero byte expected");
  12834. goto fail;
  12835. fail_unknown_memory:
  12836. set_error_buf(error_buf, error_buf_size,
  12837. "unknown memory 0");
  12838. goto fail;
  12839. fail_data_cnt_sec_require:
  12840. set_error_buf(error_buf, error_buf_size,
  12841. "data count section required");
  12842. goto fail;
  12843. #endif /* WASM_ENABLE_BULK_MEMORY */
  12844. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  12845. case WASM_OP_TABLE_INIT:
  12846. {
  12847. uint8 seg_type = 0, tbl_type = 0;
  12848. #if WASM_ENABLE_GC != 0
  12849. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  12850. #endif
  12851. read_leb_uint32(p, p_end, table_seg_idx);
  12852. read_leb_uint32(p, p_end, table_idx);
  12853. if (!get_table_elem_type(module, table_idx, &tbl_type,
  12854. #if WASM_ENABLE_GC != 0
  12855. (void **)&tbl_ref_type,
  12856. #else
  12857. NULL,
  12858. #endif
  12859. error_buf, error_buf_size))
  12860. goto fail;
  12861. if (!get_table_seg_elem_type(module, table_seg_idx,
  12862. &seg_type,
  12863. #if WASM_ENABLE_GC != 0
  12864. (void **)&seg_ref_type,
  12865. #else
  12866. NULL,
  12867. #endif
  12868. error_buf, error_buf_size))
  12869. goto fail;
  12870. #if WASM_ENABLE_GC == 0
  12871. if (seg_type != tbl_type) {
  12872. set_error_buf(error_buf, error_buf_size,
  12873. "type mismatch");
  12874. goto fail;
  12875. }
  12876. #else
  12877. if (!wasm_reftype_is_subtype_of(
  12878. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  12879. module->types, module->type_count)) {
  12880. set_error_buf(error_buf, error_buf_size,
  12881. "type mismatch");
  12882. goto fail;
  12883. }
  12884. #endif
  12885. #if WASM_ENABLE_FAST_INTERP != 0
  12886. emit_uint32(loader_ctx, table_seg_idx);
  12887. emit_uint32(loader_ctx, table_idx);
  12888. #endif
  12889. POP_I32();
  12890. POP_I32();
  12891. POP_I32();
  12892. #if WASM_ENABLE_WAMR_COMPILER != 0
  12893. module->is_ref_types_used = true;
  12894. #endif
  12895. break;
  12896. }
  12897. case WASM_OP_ELEM_DROP:
  12898. {
  12899. read_leb_uint32(p, p_end, table_seg_idx);
  12900. if (!get_table_seg_elem_type(module, table_seg_idx,
  12901. NULL, NULL, error_buf,
  12902. error_buf_size))
  12903. goto fail;
  12904. #if WASM_ENABLE_FAST_INTERP != 0
  12905. emit_uint32(loader_ctx, table_seg_idx);
  12906. #endif
  12907. #if WASM_ENABLE_WAMR_COMPILER != 0
  12908. module->is_ref_types_used = true;
  12909. #endif
  12910. break;
  12911. }
  12912. case WASM_OP_TABLE_COPY:
  12913. {
  12914. uint8 src_type, dst_type;
  12915. #if WASM_ENABLE_GC != 0
  12916. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  12917. #endif
  12918. uint32 src_tbl_idx, dst_tbl_idx;
  12919. read_leb_uint32(p, p_end, dst_tbl_idx);
  12920. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  12921. #if WASM_ENABLE_GC != 0
  12922. (void **)&dst_ref_type,
  12923. #else
  12924. NULL,
  12925. #endif
  12926. error_buf, error_buf_size))
  12927. goto fail;
  12928. read_leb_uint32(p, p_end, src_tbl_idx);
  12929. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  12930. #if WASM_ENABLE_GC != 0
  12931. (void **)&src_ref_type,
  12932. #else
  12933. NULL,
  12934. #endif
  12935. error_buf, error_buf_size))
  12936. goto fail;
  12937. #if WASM_ENABLE_GC == 0
  12938. if (src_type != dst_type) {
  12939. set_error_buf(error_buf, error_buf_size,
  12940. "type mismatch");
  12941. goto fail;
  12942. }
  12943. #else
  12944. if (!wasm_reftype_is_subtype_of(
  12945. src_type, src_ref_type, dst_type, dst_ref_type,
  12946. module->types, module->type_count)) {
  12947. set_error_buf(error_buf, error_buf_size,
  12948. "type mismatch");
  12949. goto fail;
  12950. }
  12951. #endif
  12952. #if WASM_ENABLE_FAST_INTERP != 0
  12953. emit_uint32(loader_ctx, dst_tbl_idx);
  12954. emit_uint32(loader_ctx, src_tbl_idx);
  12955. #endif
  12956. POP_I32();
  12957. POP_I32();
  12958. POP_I32();
  12959. #if WASM_ENABLE_WAMR_COMPILER != 0
  12960. module->is_ref_types_used = true;
  12961. #endif
  12962. break;
  12963. }
  12964. case WASM_OP_TABLE_SIZE:
  12965. {
  12966. read_leb_uint32(p, p_end, table_idx);
  12967. /* TODO: shall we create a new function to check
  12968. table idx instead of using below function? */
  12969. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  12970. error_buf, error_buf_size))
  12971. goto fail;
  12972. #if WASM_ENABLE_FAST_INTERP != 0
  12973. emit_uint32(loader_ctx, table_idx);
  12974. #endif
  12975. PUSH_I32();
  12976. #if WASM_ENABLE_WAMR_COMPILER != 0
  12977. module->is_ref_types_used = true;
  12978. #endif
  12979. break;
  12980. }
  12981. case WASM_OP_TABLE_GROW:
  12982. case WASM_OP_TABLE_FILL:
  12983. {
  12984. uint8 decl_type;
  12985. #if WASM_ENABLE_GC != 0
  12986. WASMRefType *ref_type = NULL;
  12987. #endif
  12988. read_leb_uint32(p, p_end, table_idx);
  12989. if (!get_table_elem_type(module, table_idx, &decl_type,
  12990. #if WASM_ENABLE_GC != 0
  12991. (void **)&ref_type,
  12992. #else
  12993. NULL,
  12994. #endif
  12995. error_buf, error_buf_size))
  12996. goto fail;
  12997. #if WASM_ENABLE_GC != 0
  12998. if (wasm_is_type_multi_byte_type(decl_type)) {
  12999. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13000. ref_type,
  13001. wasm_reftype_struct_size(ref_type));
  13002. }
  13003. #endif
  13004. if (opcode1 == WASM_OP_TABLE_GROW) {
  13005. if (table_idx < module->import_table_count) {
  13006. module->import_tables[table_idx]
  13007. .u.table.possible_grow = true;
  13008. }
  13009. else {
  13010. module
  13011. ->tables[table_idx
  13012. - module->import_table_count]
  13013. .possible_grow = true;
  13014. }
  13015. }
  13016. #if WASM_ENABLE_FAST_INTERP != 0
  13017. emit_uint32(loader_ctx, table_idx);
  13018. #endif
  13019. POP_I32();
  13020. #if WASM_ENABLE_FAST_INTERP != 0
  13021. POP_OFFSET_TYPE(decl_type);
  13022. #endif
  13023. POP_TYPE(decl_type);
  13024. if (opcode1 == WASM_OP_TABLE_GROW)
  13025. PUSH_I32();
  13026. else
  13027. POP_I32();
  13028. #if WASM_ENABLE_WAMR_COMPILER != 0
  13029. module->is_ref_types_used = true;
  13030. #endif
  13031. break;
  13032. }
  13033. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13034. default:
  13035. set_error_buf_v(error_buf, error_buf_size,
  13036. "%s %02x %02x", "unsupported opcode",
  13037. 0xfc, opcode1);
  13038. goto fail;
  13039. }
  13040. break;
  13041. }
  13042. #if WASM_ENABLE_SIMD != 0
  13043. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13044. case WASM_OP_SIMD_PREFIX:
  13045. {
  13046. uint32 opcode1;
  13047. #if WASM_ENABLE_WAMR_COMPILER != 0
  13048. /* Mark the SIMD instruction is used in this module */
  13049. module->is_simd_used = true;
  13050. #endif
  13051. read_leb_uint32(p, p_end, opcode1);
  13052. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13053. */
  13054. switch (opcode1) {
  13055. /* memory instruction */
  13056. case SIMD_v128_load:
  13057. case SIMD_v128_load8x8_s:
  13058. case SIMD_v128_load8x8_u:
  13059. case SIMD_v128_load16x4_s:
  13060. case SIMD_v128_load16x4_u:
  13061. case SIMD_v128_load32x2_s:
  13062. case SIMD_v128_load32x2_u:
  13063. case SIMD_v128_load8_splat:
  13064. case SIMD_v128_load16_splat:
  13065. case SIMD_v128_load32_splat:
  13066. case SIMD_v128_load64_splat:
  13067. {
  13068. CHECK_MEMORY();
  13069. read_leb_uint32(p, p_end, align); /* align */
  13070. if (!check_simd_memory_access_align(
  13071. opcode1, align, error_buf, error_buf_size)) {
  13072. goto fail;
  13073. }
  13074. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13075. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13076. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13077. func->has_memory_operations = true;
  13078. #endif
  13079. break;
  13080. }
  13081. case SIMD_v128_store:
  13082. {
  13083. CHECK_MEMORY();
  13084. read_leb_uint32(p, p_end, align); /* align */
  13085. if (!check_simd_memory_access_align(
  13086. opcode1, align, error_buf, error_buf_size)) {
  13087. goto fail;
  13088. }
  13089. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13090. POP_V128();
  13091. POP_I32();
  13092. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13093. func->has_memory_operations = true;
  13094. #endif
  13095. break;
  13096. }
  13097. /* basic operation */
  13098. case SIMD_v128_const:
  13099. {
  13100. CHECK_BUF1(p, p_end, 16);
  13101. p += 16;
  13102. PUSH_V128();
  13103. break;
  13104. }
  13105. case SIMD_v8x16_shuffle:
  13106. {
  13107. V128 mask;
  13108. CHECK_BUF1(p, p_end, 16);
  13109. mask = read_i8x16(p, error_buf, error_buf_size);
  13110. p += 16;
  13111. if (!check_simd_shuffle_mask(mask, error_buf,
  13112. error_buf_size)) {
  13113. goto fail;
  13114. }
  13115. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13116. break;
  13117. }
  13118. case SIMD_v8x16_swizzle:
  13119. {
  13120. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13121. break;
  13122. }
  13123. /* splat operation */
  13124. case SIMD_i8x16_splat:
  13125. case SIMD_i16x8_splat:
  13126. case SIMD_i32x4_splat:
  13127. case SIMD_i64x2_splat:
  13128. case SIMD_f32x4_splat:
  13129. case SIMD_f64x2_splat:
  13130. {
  13131. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13132. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13133. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13134. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13135. VALUE_TYPE_V128);
  13136. break;
  13137. }
  13138. /* lane operation */
  13139. case SIMD_i8x16_extract_lane_s:
  13140. case SIMD_i8x16_extract_lane_u:
  13141. case SIMD_i8x16_replace_lane:
  13142. case SIMD_i16x8_extract_lane_s:
  13143. case SIMD_i16x8_extract_lane_u:
  13144. case SIMD_i16x8_replace_lane:
  13145. case SIMD_i32x4_extract_lane:
  13146. case SIMD_i32x4_replace_lane:
  13147. case SIMD_i64x2_extract_lane:
  13148. case SIMD_i64x2_replace_lane:
  13149. case SIMD_f32x4_extract_lane:
  13150. case SIMD_f32x4_replace_lane:
  13151. case SIMD_f64x2_extract_lane:
  13152. case SIMD_f64x2_replace_lane:
  13153. {
  13154. uint8 lane;
  13155. /* clang-format off */
  13156. uint8 replace[] = {
  13157. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13158. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13159. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13160. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13161. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13162. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13163. };
  13164. uint8 push_type[] = {
  13165. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13166. VALUE_TYPE_V128,
  13167. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13168. VALUE_TYPE_V128,
  13169. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13170. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13171. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13172. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13173. };
  13174. /* clang-format on */
  13175. CHECK_BUF(p, p_end, 1);
  13176. lane = read_uint8(p);
  13177. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13178. error_buf_size)) {
  13179. goto fail;
  13180. }
  13181. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13182. if (!(wasm_loader_pop_frame_ref(
  13183. loader_ctx,
  13184. replace[opcode1
  13185. - SIMD_i8x16_extract_lane_s],
  13186. error_buf, error_buf_size)))
  13187. goto fail;
  13188. }
  13189. POP_AND_PUSH(
  13190. VALUE_TYPE_V128,
  13191. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13192. break;
  13193. }
  13194. /* i8x16 compare operation */
  13195. case SIMD_i8x16_eq:
  13196. case SIMD_i8x16_ne:
  13197. case SIMD_i8x16_lt_s:
  13198. case SIMD_i8x16_lt_u:
  13199. case SIMD_i8x16_gt_s:
  13200. case SIMD_i8x16_gt_u:
  13201. case SIMD_i8x16_le_s:
  13202. case SIMD_i8x16_le_u:
  13203. case SIMD_i8x16_ge_s:
  13204. case SIMD_i8x16_ge_u:
  13205. /* i16x8 compare operation */
  13206. case SIMD_i16x8_eq:
  13207. case SIMD_i16x8_ne:
  13208. case SIMD_i16x8_lt_s:
  13209. case SIMD_i16x8_lt_u:
  13210. case SIMD_i16x8_gt_s:
  13211. case SIMD_i16x8_gt_u:
  13212. case SIMD_i16x8_le_s:
  13213. case SIMD_i16x8_le_u:
  13214. case SIMD_i16x8_ge_s:
  13215. case SIMD_i16x8_ge_u:
  13216. /* i32x4 compare operation */
  13217. case SIMD_i32x4_eq:
  13218. case SIMD_i32x4_ne:
  13219. case SIMD_i32x4_lt_s:
  13220. case SIMD_i32x4_lt_u:
  13221. case SIMD_i32x4_gt_s:
  13222. case SIMD_i32x4_gt_u:
  13223. case SIMD_i32x4_le_s:
  13224. case SIMD_i32x4_le_u:
  13225. case SIMD_i32x4_ge_s:
  13226. case SIMD_i32x4_ge_u:
  13227. /* f32x4 compare operation */
  13228. case SIMD_f32x4_eq:
  13229. case SIMD_f32x4_ne:
  13230. case SIMD_f32x4_lt:
  13231. case SIMD_f32x4_gt:
  13232. case SIMD_f32x4_le:
  13233. case SIMD_f32x4_ge:
  13234. /* f64x2 compare operation */
  13235. case SIMD_f64x2_eq:
  13236. case SIMD_f64x2_ne:
  13237. case SIMD_f64x2_lt:
  13238. case SIMD_f64x2_gt:
  13239. case SIMD_f64x2_le:
  13240. case SIMD_f64x2_ge:
  13241. {
  13242. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13243. break;
  13244. }
  13245. /* v128 operation */
  13246. case SIMD_v128_not:
  13247. {
  13248. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13249. break;
  13250. }
  13251. case SIMD_v128_and:
  13252. case SIMD_v128_andnot:
  13253. case SIMD_v128_or:
  13254. case SIMD_v128_xor:
  13255. {
  13256. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13257. break;
  13258. }
  13259. case SIMD_v128_bitselect:
  13260. {
  13261. POP_V128();
  13262. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13263. break;
  13264. }
  13265. case SIMD_v128_any_true:
  13266. {
  13267. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13268. break;
  13269. }
  13270. /* Load Lane Operation */
  13271. case SIMD_v128_load8_lane:
  13272. case SIMD_v128_load16_lane:
  13273. case SIMD_v128_load32_lane:
  13274. case SIMD_v128_load64_lane:
  13275. case SIMD_v128_store8_lane:
  13276. case SIMD_v128_store16_lane:
  13277. case SIMD_v128_store32_lane:
  13278. case SIMD_v128_store64_lane:
  13279. {
  13280. uint8 lane;
  13281. CHECK_MEMORY();
  13282. read_leb_uint32(p, p_end, align); /* align */
  13283. if (!check_simd_memory_access_align(
  13284. opcode1, align, error_buf, error_buf_size)) {
  13285. goto fail;
  13286. }
  13287. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13288. CHECK_BUF(p, p_end, 1);
  13289. lane = read_uint8(p);
  13290. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13291. error_buf_size)) {
  13292. goto fail;
  13293. }
  13294. POP_V128();
  13295. POP_I32();
  13296. if (opcode1 < SIMD_v128_store8_lane) {
  13297. PUSH_V128();
  13298. }
  13299. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13300. func->has_memory_operations = true;
  13301. #endif
  13302. break;
  13303. }
  13304. case SIMD_v128_load32_zero:
  13305. case SIMD_v128_load64_zero:
  13306. {
  13307. CHECK_MEMORY();
  13308. read_leb_uint32(p, p_end, align); /* align */
  13309. if (!check_simd_memory_access_align(
  13310. opcode1, align, error_buf, error_buf_size)) {
  13311. goto fail;
  13312. }
  13313. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13314. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13315. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13316. func->has_memory_operations = true;
  13317. #endif
  13318. break;
  13319. }
  13320. /* Float conversion */
  13321. case SIMD_f32x4_demote_f64x2_zero:
  13322. case SIMD_f64x2_promote_low_f32x4_zero:
  13323. {
  13324. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13325. break;
  13326. }
  13327. /* i8x16 Operation */
  13328. case SIMD_i8x16_abs:
  13329. case SIMD_i8x16_neg:
  13330. case SIMD_i8x16_popcnt:
  13331. {
  13332. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13333. break;
  13334. }
  13335. case SIMD_i8x16_all_true:
  13336. case SIMD_i8x16_bitmask:
  13337. {
  13338. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13339. break;
  13340. }
  13341. case SIMD_i8x16_narrow_i16x8_s:
  13342. case SIMD_i8x16_narrow_i16x8_u:
  13343. {
  13344. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13345. break;
  13346. }
  13347. case SIMD_f32x4_ceil:
  13348. case SIMD_f32x4_floor:
  13349. case SIMD_f32x4_trunc:
  13350. case SIMD_f32x4_nearest:
  13351. {
  13352. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13353. break;
  13354. }
  13355. case SIMD_i8x16_shl:
  13356. case SIMD_i8x16_shr_s:
  13357. case SIMD_i8x16_shr_u:
  13358. {
  13359. POP_I32();
  13360. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13361. break;
  13362. }
  13363. case SIMD_i8x16_add:
  13364. case SIMD_i8x16_add_sat_s:
  13365. case SIMD_i8x16_add_sat_u:
  13366. case SIMD_i8x16_sub:
  13367. case SIMD_i8x16_sub_sat_s:
  13368. case SIMD_i8x16_sub_sat_u:
  13369. {
  13370. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13371. break;
  13372. }
  13373. case SIMD_f64x2_ceil:
  13374. case SIMD_f64x2_floor:
  13375. {
  13376. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13377. break;
  13378. }
  13379. case SIMD_i8x16_min_s:
  13380. case SIMD_i8x16_min_u:
  13381. case SIMD_i8x16_max_s:
  13382. case SIMD_i8x16_max_u:
  13383. {
  13384. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13385. break;
  13386. }
  13387. case SIMD_f64x2_trunc:
  13388. {
  13389. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13390. break;
  13391. }
  13392. case SIMD_i8x16_avgr_u:
  13393. {
  13394. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13395. break;
  13396. }
  13397. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13398. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13399. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13400. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13401. /* i16x8 operation */
  13402. case SIMD_i16x8_abs:
  13403. case SIMD_i16x8_neg:
  13404. {
  13405. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13406. break;
  13407. }
  13408. case SIMD_i16x8_q15mulr_sat_s:
  13409. {
  13410. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13411. break;
  13412. }
  13413. case SIMD_i16x8_all_true:
  13414. case SIMD_i16x8_bitmask:
  13415. {
  13416. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13417. break;
  13418. }
  13419. case SIMD_i16x8_narrow_i32x4_s:
  13420. case SIMD_i16x8_narrow_i32x4_u:
  13421. {
  13422. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13423. break;
  13424. }
  13425. case SIMD_i16x8_extend_low_i8x16_s:
  13426. case SIMD_i16x8_extend_high_i8x16_s:
  13427. case SIMD_i16x8_extend_low_i8x16_u:
  13428. case SIMD_i16x8_extend_high_i8x16_u:
  13429. {
  13430. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13431. break;
  13432. }
  13433. case SIMD_i16x8_shl:
  13434. case SIMD_i16x8_shr_s:
  13435. case SIMD_i16x8_shr_u:
  13436. {
  13437. POP_I32();
  13438. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13439. break;
  13440. }
  13441. case SIMD_i16x8_add:
  13442. case SIMD_i16x8_add_sat_s:
  13443. case SIMD_i16x8_add_sat_u:
  13444. case SIMD_i16x8_sub:
  13445. case SIMD_i16x8_sub_sat_s:
  13446. case SIMD_i16x8_sub_sat_u:
  13447. {
  13448. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13449. break;
  13450. }
  13451. case SIMD_f64x2_nearest:
  13452. {
  13453. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13454. break;
  13455. }
  13456. case SIMD_i16x8_mul:
  13457. case SIMD_i16x8_min_s:
  13458. case SIMD_i16x8_min_u:
  13459. case SIMD_i16x8_max_s:
  13460. case SIMD_i16x8_max_u:
  13461. case SIMD_i16x8_avgr_u:
  13462. case SIMD_i16x8_extmul_low_i8x16_s:
  13463. case SIMD_i16x8_extmul_high_i8x16_s:
  13464. case SIMD_i16x8_extmul_low_i8x16_u:
  13465. case SIMD_i16x8_extmul_high_i8x16_u:
  13466. {
  13467. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13468. break;
  13469. }
  13470. /* i32x4 operation */
  13471. case SIMD_i32x4_abs:
  13472. case SIMD_i32x4_neg:
  13473. {
  13474. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13475. break;
  13476. }
  13477. case SIMD_i32x4_all_true:
  13478. case SIMD_i32x4_bitmask:
  13479. {
  13480. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13481. break;
  13482. }
  13483. case SIMD_i32x4_narrow_i64x2_s:
  13484. case SIMD_i32x4_narrow_i64x2_u:
  13485. {
  13486. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13487. break;
  13488. }
  13489. case SIMD_i32x4_extend_low_i16x8_s:
  13490. case SIMD_i32x4_extend_high_i16x8_s:
  13491. case SIMD_i32x4_extend_low_i16x8_u:
  13492. case SIMD_i32x4_extend_high_i16x8_u:
  13493. {
  13494. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13495. break;
  13496. }
  13497. case SIMD_i32x4_shl:
  13498. case SIMD_i32x4_shr_s:
  13499. case SIMD_i32x4_shr_u:
  13500. {
  13501. POP_I32();
  13502. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13503. break;
  13504. }
  13505. case SIMD_i32x4_add:
  13506. case SIMD_i32x4_sub:
  13507. case SIMD_i32x4_mul:
  13508. case SIMD_i32x4_min_s:
  13509. case SIMD_i32x4_min_u:
  13510. case SIMD_i32x4_max_s:
  13511. case SIMD_i32x4_max_u:
  13512. case SIMD_i32x4_dot_i16x8_s:
  13513. case SIMD_i32x4_avgr_u:
  13514. case SIMD_i32x4_extmul_low_i16x8_s:
  13515. case SIMD_i32x4_extmul_high_i16x8_s:
  13516. case SIMD_i32x4_extmul_low_i16x8_u:
  13517. case SIMD_i32x4_extmul_high_i16x8_u:
  13518. {
  13519. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13520. break;
  13521. }
  13522. /* i64x2 operation */
  13523. case SIMD_i64x2_abs:
  13524. case SIMD_i64x2_neg:
  13525. {
  13526. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13527. break;
  13528. }
  13529. case SIMD_i64x2_all_true:
  13530. case SIMD_i64x2_bitmask:
  13531. {
  13532. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13533. break;
  13534. }
  13535. case SIMD_i64x2_extend_low_i32x4_s:
  13536. case SIMD_i64x2_extend_high_i32x4_s:
  13537. case SIMD_i64x2_extend_low_i32x4_u:
  13538. case SIMD_i64x2_extend_high_i32x4_u:
  13539. {
  13540. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13541. break;
  13542. }
  13543. case SIMD_i64x2_shl:
  13544. case SIMD_i64x2_shr_s:
  13545. case SIMD_i64x2_shr_u:
  13546. {
  13547. POP_I32();
  13548. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13549. break;
  13550. }
  13551. case SIMD_i64x2_add:
  13552. case SIMD_i64x2_sub:
  13553. case SIMD_i64x2_mul:
  13554. case SIMD_i64x2_eq:
  13555. case SIMD_i64x2_ne:
  13556. case SIMD_i64x2_lt_s:
  13557. case SIMD_i64x2_gt_s:
  13558. case SIMD_i64x2_le_s:
  13559. case SIMD_i64x2_ge_s:
  13560. case SIMD_i64x2_extmul_low_i32x4_s:
  13561. case SIMD_i64x2_extmul_high_i32x4_s:
  13562. case SIMD_i64x2_extmul_low_i32x4_u:
  13563. case SIMD_i64x2_extmul_high_i32x4_u:
  13564. {
  13565. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13566. break;
  13567. }
  13568. /* f32x4 operation */
  13569. case SIMD_f32x4_abs:
  13570. case SIMD_f32x4_neg:
  13571. case SIMD_f32x4_round:
  13572. case SIMD_f32x4_sqrt:
  13573. {
  13574. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13575. break;
  13576. }
  13577. case SIMD_f32x4_add:
  13578. case SIMD_f32x4_sub:
  13579. case SIMD_f32x4_mul:
  13580. case SIMD_f32x4_div:
  13581. case SIMD_f32x4_min:
  13582. case SIMD_f32x4_max:
  13583. case SIMD_f32x4_pmin:
  13584. case SIMD_f32x4_pmax:
  13585. {
  13586. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13587. break;
  13588. }
  13589. /* f64x2 operation */
  13590. case SIMD_f64x2_abs:
  13591. case SIMD_f64x2_neg:
  13592. case SIMD_f64x2_round:
  13593. case SIMD_f64x2_sqrt:
  13594. {
  13595. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13596. break;
  13597. }
  13598. case SIMD_f64x2_add:
  13599. case SIMD_f64x2_sub:
  13600. case SIMD_f64x2_mul:
  13601. case SIMD_f64x2_div:
  13602. case SIMD_f64x2_min:
  13603. case SIMD_f64x2_max:
  13604. case SIMD_f64x2_pmin:
  13605. case SIMD_f64x2_pmax:
  13606. {
  13607. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13608. break;
  13609. }
  13610. case SIMD_i32x4_trunc_sat_f32x4_s:
  13611. case SIMD_i32x4_trunc_sat_f32x4_u:
  13612. case SIMD_f32x4_convert_i32x4_s:
  13613. case SIMD_f32x4_convert_i32x4_u:
  13614. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  13615. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  13616. case SIMD_f64x2_convert_low_i32x4_s:
  13617. case SIMD_f64x2_convert_low_i32x4_u:
  13618. {
  13619. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13620. break;
  13621. }
  13622. default:
  13623. {
  13624. if (error_buf != NULL) {
  13625. snprintf(error_buf, error_buf_size,
  13626. "WASM module load failed: "
  13627. "invalid opcode 0xfd %02x.",
  13628. opcode1);
  13629. }
  13630. goto fail;
  13631. }
  13632. }
  13633. break;
  13634. }
  13635. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  13636. #endif /* end of WASM_ENABLE_SIMD */
  13637. #if WASM_ENABLE_SHARED_MEMORY != 0
  13638. case WASM_OP_ATOMIC_PREFIX:
  13639. {
  13640. uint32 opcode1;
  13641. read_leb_uint32(p, p_end, opcode1);
  13642. #if WASM_ENABLE_FAST_INTERP != 0
  13643. emit_byte(loader_ctx, opcode1);
  13644. #endif
  13645. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  13646. CHECK_MEMORY();
  13647. read_leb_uint32(p, p_end, align); /* align */
  13648. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13649. if (!check_memory_align_equal(opcode1, align, error_buf,
  13650. error_buf_size)) {
  13651. goto fail;
  13652. }
  13653. #if WASM_ENABLE_FAST_INTERP != 0
  13654. emit_uint32(loader_ctx, mem_offset);
  13655. #endif
  13656. }
  13657. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13658. func->has_memory_operations = true;
  13659. #endif
  13660. switch (opcode1) {
  13661. case WASM_OP_ATOMIC_NOTIFY:
  13662. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13663. break;
  13664. case WASM_OP_ATOMIC_WAIT32:
  13665. POP_I64();
  13666. POP_I32();
  13667. POP_I32();
  13668. PUSH_I32();
  13669. break;
  13670. case WASM_OP_ATOMIC_WAIT64:
  13671. POP_I64();
  13672. POP_I64();
  13673. POP_I32();
  13674. PUSH_I32();
  13675. break;
  13676. case WASM_OP_ATOMIC_FENCE:
  13677. /* reserved byte 0x00 */
  13678. if (*p++ != 0x00) {
  13679. set_error_buf(error_buf, error_buf_size,
  13680. "zero byte expected");
  13681. goto fail;
  13682. }
  13683. break;
  13684. case WASM_OP_ATOMIC_I32_LOAD:
  13685. case WASM_OP_ATOMIC_I32_LOAD8_U:
  13686. case WASM_OP_ATOMIC_I32_LOAD16_U:
  13687. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13688. break;
  13689. case WASM_OP_ATOMIC_I32_STORE:
  13690. case WASM_OP_ATOMIC_I32_STORE8:
  13691. case WASM_OP_ATOMIC_I32_STORE16:
  13692. POP_I32();
  13693. POP_I32();
  13694. break;
  13695. case WASM_OP_ATOMIC_I64_LOAD:
  13696. case WASM_OP_ATOMIC_I64_LOAD8_U:
  13697. case WASM_OP_ATOMIC_I64_LOAD16_U:
  13698. case WASM_OP_ATOMIC_I64_LOAD32_U:
  13699. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  13700. break;
  13701. case WASM_OP_ATOMIC_I64_STORE:
  13702. case WASM_OP_ATOMIC_I64_STORE8:
  13703. case WASM_OP_ATOMIC_I64_STORE16:
  13704. case WASM_OP_ATOMIC_I64_STORE32:
  13705. POP_I64();
  13706. POP_I32();
  13707. break;
  13708. case WASM_OP_ATOMIC_RMW_I32_ADD:
  13709. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  13710. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  13711. case WASM_OP_ATOMIC_RMW_I32_SUB:
  13712. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  13713. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  13714. case WASM_OP_ATOMIC_RMW_I32_AND:
  13715. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  13716. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  13717. case WASM_OP_ATOMIC_RMW_I32_OR:
  13718. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  13719. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  13720. case WASM_OP_ATOMIC_RMW_I32_XOR:
  13721. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  13722. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  13723. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  13724. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  13725. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  13726. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13727. break;
  13728. case WASM_OP_ATOMIC_RMW_I64_ADD:
  13729. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  13730. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  13731. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  13732. case WASM_OP_ATOMIC_RMW_I64_SUB:
  13733. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  13734. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  13735. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  13736. case WASM_OP_ATOMIC_RMW_I64_AND:
  13737. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  13738. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  13739. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  13740. case WASM_OP_ATOMIC_RMW_I64_OR:
  13741. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  13742. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  13743. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  13744. case WASM_OP_ATOMIC_RMW_I64_XOR:
  13745. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  13746. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  13747. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  13748. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  13749. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  13750. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  13751. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  13752. POP_I64();
  13753. POP_I32();
  13754. PUSH_I64();
  13755. break;
  13756. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  13757. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  13758. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  13759. POP_I32();
  13760. POP_I32();
  13761. POP_I32();
  13762. PUSH_I32();
  13763. break;
  13764. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  13765. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  13766. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  13767. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  13768. POP_I64();
  13769. POP_I64();
  13770. POP_I32();
  13771. PUSH_I64();
  13772. break;
  13773. default:
  13774. set_error_buf_v(error_buf, error_buf_size,
  13775. "%s %02x %02x", "unsupported opcode",
  13776. 0xfe, opcode1);
  13777. goto fail;
  13778. }
  13779. break;
  13780. }
  13781. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  13782. default:
  13783. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  13784. "unsupported opcode", opcode);
  13785. goto fail;
  13786. }
  13787. #if WASM_ENABLE_FAST_INTERP != 0
  13788. last_op = opcode;
  13789. #endif
  13790. }
  13791. if (loader_ctx->csp_num > 0) {
  13792. if (cur_func_idx < module->function_count - 1)
  13793. /* Function with missing end marker (between two functions) */
  13794. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  13795. else
  13796. /* Function with missing end marker
  13797. (at EOF or end of code sections) */
  13798. set_error_buf(error_buf, error_buf_size,
  13799. "unexpected end of section or function, "
  13800. "or section size mismatch");
  13801. goto fail;
  13802. }
  13803. #if WASM_ENABLE_FAST_INTERP != 0
  13804. if (loader_ctx->p_code_compiled == NULL)
  13805. goto re_scan;
  13806. func->const_cell_num = loader_ctx->const_cell_num;
  13807. if (func->const_cell_num > 0) {
  13808. int32 j;
  13809. if (!(func->consts = func_const = loader_malloc(
  13810. func->const_cell_num * 4, error_buf, error_buf_size)))
  13811. goto fail;
  13812. func_const_end = func->consts + func->const_cell_num * 4;
  13813. /* reverse the const buf */
  13814. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  13815. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  13816. if (c->value_type == VALUE_TYPE_F64
  13817. || c->value_type == VALUE_TYPE_I64) {
  13818. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  13819. &(c->value.f64), (uint32)sizeof(int64));
  13820. func_const += sizeof(int64);
  13821. }
  13822. else {
  13823. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  13824. &(c->value.f32), (uint32)sizeof(int32));
  13825. func_const += sizeof(int32);
  13826. }
  13827. }
  13828. }
  13829. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  13830. - loader_ctx->start_dynamic_offset + 1;
  13831. #else
  13832. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  13833. #endif
  13834. func->max_block_num = loader_ctx->max_csp_num;
  13835. return_value = true;
  13836. fail:
  13837. wasm_loader_ctx_destroy(loader_ctx);
  13838. (void)table_idx;
  13839. (void)table_seg_idx;
  13840. (void)data_seg_idx;
  13841. (void)i64_const;
  13842. (void)local_offset;
  13843. (void)p_org;
  13844. (void)mem_offset;
  13845. (void)align;
  13846. return return_value;
  13847. }