wasm_loader.c 478 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215
  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_bool(p) TEMPLATE_READ_VALUE(bool, p)
  166. #define read_leb_int64(p, p_end, res) \
  167. do { \
  168. uint64 res64; \
  169. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  170. error_buf_size)) \
  171. goto fail; \
  172. res = (int64)res64; \
  173. } while (0)
  174. #define read_leb_uint32(p, p_end, res) \
  175. do { \
  176. uint64 res64; \
  177. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  178. error_buf_size)) \
  179. goto fail; \
  180. res = (uint32)res64; \
  181. } while (0)
  182. #define read_leb_int32(p, p_end, res) \
  183. do { \
  184. uint64 res64; \
  185. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  186. error_buf_size)) \
  187. goto fail; \
  188. res = (int32)res64; \
  189. } while (0)
  190. static char *
  191. type2str(uint8 type)
  192. {
  193. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  194. #if WASM_ENABLE_GC != 0
  195. char *type_str_ref[] = { "nullref", "arrayref", "structref",
  196. "nullexternref", "nullfuncref", "i31ref",
  197. "(ref ht)", "(ref null ht)", "eqref",
  198. "anyref", "externref", "funcref" };
  199. #endif
  200. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  201. return type_str[type - VALUE_TYPE_V128];
  202. #if WASM_ENABLE_GC != 0
  203. else if (wasm_is_type_reftype(type))
  204. return type_str_ref[type - REF_TYPE_NULLREF];
  205. #endif
  206. else if (type == VALUE_TYPE_FUNCREF)
  207. return "funcref";
  208. else if (type == VALUE_TYPE_EXTERNREF)
  209. return "externref";
  210. else
  211. return "unknown type";
  212. }
  213. static bool
  214. is_32bit_type(uint8 type)
  215. {
  216. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  217. #if WASM_ENABLE_GC != 0
  218. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  219. #elif WASM_ENABLE_REF_TYPES != 0
  220. /* For reference types, we use uint32 index to represent
  221. the funcref and externref */
  222. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  223. #endif
  224. )
  225. return true;
  226. return false;
  227. }
  228. static bool
  229. is_64bit_type(uint8 type)
  230. {
  231. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  232. #if WASM_ENABLE_GC != 0
  233. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  234. #endif
  235. )
  236. return true;
  237. return false;
  238. }
  239. static bool
  240. is_value_type(uint8 type)
  241. {
  242. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  243. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  244. #if WASM_ENABLE_GC != 0
  245. /* reference types, 0x65 to 0x70 */
  246. || wasm_is_type_reftype(type)
  247. #elif WASM_ENABLE_REF_TYPES != 0
  248. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  249. #endif
  250. #if WASM_ENABLE_SIMD != 0
  251. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  252. || type == VALUE_TYPE_V128 /* 0x7B */
  253. #endif
  254. #endif
  255. )
  256. return true;
  257. return false;
  258. }
  259. #if WASM_ENABLE_GC != 0
  260. static bool
  261. is_packed_type(uint8 type)
  262. {
  263. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  264. }
  265. #endif
  266. static bool
  267. is_byte_a_type(uint8 type)
  268. {
  269. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  270. }
  271. #if WASM_ENABLE_SIMD != 0
  272. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  273. static V128
  274. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  275. {
  276. V128 result;
  277. uint8 i;
  278. for (i = 0; i != 16; ++i) {
  279. result.i8x16[i] = read_uint8(p_buf);
  280. }
  281. return result;
  282. }
  283. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  284. #endif /* end of WASM_ENABLE_SIMD */
  285. static void *
  286. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  287. {
  288. void *mem;
  289. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  290. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  291. return NULL;
  292. }
  293. memset(mem, 0, (uint32)size);
  294. return mem;
  295. }
  296. static bool
  297. check_utf8_str(const uint8 *str, uint32 len)
  298. {
  299. /* The valid ranges are taken from page 125, below link
  300. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  301. const uint8 *p = str, *p_end = str + len;
  302. uint8 chr;
  303. while (p < p_end) {
  304. chr = *p;
  305. if (chr < 0x80) {
  306. p++;
  307. }
  308. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  309. if (p[1] < 0x80 || p[1] > 0xBF) {
  310. return false;
  311. }
  312. p += 2;
  313. }
  314. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  315. if (chr == 0xE0) {
  316. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  317. return false;
  318. }
  319. }
  320. else if (chr == 0xED) {
  321. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  322. return false;
  323. }
  324. }
  325. else if (chr >= 0xE1 && chr <= 0xEF) {
  326. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  327. return false;
  328. }
  329. }
  330. p += 3;
  331. }
  332. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  333. if (chr == 0xF0) {
  334. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  335. || p[3] < 0x80 || p[3] > 0xBF) {
  336. return false;
  337. }
  338. }
  339. else if (chr >= 0xF1 && chr <= 0xF3) {
  340. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  341. || p[3] < 0x80 || p[3] > 0xBF) {
  342. return false;
  343. }
  344. }
  345. else if (chr == 0xF4) {
  346. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  347. || p[3] < 0x80 || p[3] > 0xBF) {
  348. return false;
  349. }
  350. }
  351. p += 4;
  352. }
  353. else {
  354. return false;
  355. }
  356. }
  357. return (p == p_end);
  358. }
  359. static char *
  360. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  361. bool is_load_from_file_buf, char *error_buf,
  362. uint32 error_buf_size)
  363. {
  364. StringNode *node, *node_next;
  365. if (!check_utf8_str(str, len)) {
  366. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  367. return NULL;
  368. }
  369. if (len == 0) {
  370. return "";
  371. }
  372. else if (is_load_from_file_buf) {
  373. /* As the file buffer can be referred to after loading, we use
  374. the previous byte of leb encoded size to adjust the string:
  375. move string 1 byte backward and then append '\0' */
  376. char *c_str = (char *)str - 1;
  377. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  378. c_str[len] = '\0';
  379. return c_str;
  380. }
  381. /* Search const str list */
  382. node = module->const_str_list;
  383. while (node) {
  384. node_next = node->next;
  385. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  386. break;
  387. node = node_next;
  388. }
  389. if (node) {
  390. return node->str;
  391. }
  392. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  393. error_buf_size))) {
  394. return NULL;
  395. }
  396. node->str = ((char *)node) + sizeof(StringNode);
  397. bh_memcpy_s(node->str, len + 1, str, len);
  398. node->str[len] = '\0';
  399. if (!module->const_str_list) {
  400. /* set as head */
  401. module->const_str_list = node;
  402. node->next = NULL;
  403. }
  404. else {
  405. /* insert it */
  406. node->next = module->const_str_list;
  407. module->const_str_list = node;
  408. }
  409. return node->str;
  410. }
  411. #if WASM_ENABLE_GC != 0
  412. static bool
  413. check_type_index(const WASMModule *module, uint32 type_index, char *error_buf,
  414. uint32 error_buf_size)
  415. {
  416. if (type_index >= module->type_count) {
  417. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  418. type_index);
  419. return false;
  420. }
  421. return true;
  422. }
  423. static bool
  424. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  425. uint32 error_buf_size)
  426. {
  427. if (!check_type_index(module, type_index, error_buf, error_buf_size)) {
  428. return false;
  429. }
  430. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  431. set_error_buf(error_buf, error_buf_size, "unkown array type");
  432. return false;
  433. }
  434. return true;
  435. }
  436. #endif
  437. static bool
  438. check_function_index(const WASMModule *module, uint32 function_index,
  439. char *error_buf, uint32 error_buf_size)
  440. {
  441. if (function_index
  442. >= module->import_function_count + module->function_count) {
  443. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  444. function_index);
  445. return false;
  446. }
  447. return true;
  448. }
  449. static bool
  450. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  451. InitializerExpression *init_expr, uint8 type, void *ref_type,
  452. char *error_buf, uint32 error_buf_size)
  453. {
  454. const uint8 *p = *p_buf, *p_end = buf_end;
  455. uint8 flag, end_byte, *p_float;
  456. uint32 i;
  457. #if WASM_ENABLE_GC != 0
  458. uint32 type_idx;
  459. #endif
  460. CHECK_BUF(p, p_end, 1);
  461. init_expr->init_expr_type = read_uint8(p);
  462. flag = init_expr->init_expr_type;
  463. switch (flag) {
  464. /* i32.const */
  465. case INIT_EXPR_TYPE_I32_CONST:
  466. read_leb_int32(p, p_end, init_expr->u.i32);
  467. #if WASM_ENABLE_GC == 0
  468. if (type != VALUE_TYPE_I32)
  469. goto fail_type_mismatch;
  470. #else
  471. if (type != VALUE_TYPE_I32) {
  472. if (!wasm_reftype_is_subtype_of(
  473. type, (WASMRefType *)ref_type, REF_TYPE_I31REF, NULL,
  474. module->types, module->type_count)) {
  475. goto fail_type_mismatch;
  476. }
  477. else {
  478. CHECK_BUF(p, p_end, 2);
  479. if (*p == WASM_OP_GC_PREFIX
  480. && *(p + 1) == WASM_OP_I31_NEW) {
  481. p += 2;
  482. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  483. }
  484. else
  485. goto fail_type_mismatch;
  486. }
  487. }
  488. #endif
  489. break;
  490. /* i64.const */
  491. case INIT_EXPR_TYPE_I64_CONST:
  492. if (type != VALUE_TYPE_I64)
  493. goto fail_type_mismatch;
  494. read_leb_int64(p, p_end, init_expr->u.i64);
  495. break;
  496. /* f32.const */
  497. case INIT_EXPR_TYPE_F32_CONST:
  498. if (type != VALUE_TYPE_F32)
  499. goto fail_type_mismatch;
  500. CHECK_BUF(p, p_end, 4);
  501. p_float = (uint8 *)&init_expr->u.f32;
  502. for (i = 0; i < sizeof(float32); i++)
  503. *p_float++ = *p++;
  504. break;
  505. /* f64.const */
  506. case INIT_EXPR_TYPE_F64_CONST:
  507. if (type != VALUE_TYPE_F64)
  508. goto fail_type_mismatch;
  509. CHECK_BUF(p, p_end, 8);
  510. p_float = (uint8 *)&init_expr->u.f64;
  511. for (i = 0; i < sizeof(float64); i++)
  512. *p_float++ = *p++;
  513. break;
  514. #if WASM_ENABLE_SIMD != 0
  515. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  516. /* v128.const */
  517. case INIT_EXPR_TYPE_V128_CONST:
  518. {
  519. uint64 high, low;
  520. if (type != VALUE_TYPE_V128)
  521. goto fail_type_mismatch;
  522. flag = read_uint8(p);
  523. (void)flag;
  524. CHECK_BUF(p, p_end, 16);
  525. wasm_runtime_read_v128(p, &high, &low);
  526. p += 16;
  527. init_expr->u.v128.i64x2[0] = high;
  528. init_expr->u.v128.i64x2[1] = low;
  529. break;
  530. }
  531. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  532. #endif /* end of WASM_ENABLE_SIMD */
  533. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  534. /* ref.func */
  535. case INIT_EXPR_TYPE_FUNCREF_CONST:
  536. {
  537. #if WASM_ENABLE_GC == 0
  538. if (type != VALUE_TYPE_FUNCREF)
  539. goto fail_type_mismatch;
  540. #else
  541. if (!wasm_reftype_is_subtype_of(type, (WASMRefType *)ref_type,
  542. VALUE_TYPE_FUNCREF, NULL,
  543. module->types, module->type_count))
  544. goto fail_type_mismatch;
  545. #endif
  546. read_leb_uint32(p, p_end, init_expr->u.ref_index);
  547. if (!check_function_index(module, init_expr->u.ref_index, error_buf,
  548. error_buf_size)) {
  549. return false;
  550. }
  551. break;
  552. }
  553. /* ref.null */
  554. case INIT_EXPR_TYPE_REFNULL_CONST:
  555. {
  556. uint8 type1;
  557. #if WASM_ENABLE_GC == 0
  558. CHECK_BUF(p, p_end, 1);
  559. type1 = read_uint8(p);
  560. if (type1 != type)
  561. goto fail_type_mismatch;
  562. init_expr->u.ref_index = NULL_REF;
  563. #else
  564. WASMRefType ref_type1;
  565. type1 = read_uint8(p);
  566. if (!is_byte_a_type(type1)) {
  567. p--;
  568. read_leb_uint32(p, p_end, type_idx);
  569. if (!check_type_index(module, type_idx, error_buf,
  570. error_buf_size))
  571. return false;
  572. wasm_set_refheaptype_typeidx(&ref_type1.ref_ht_typeidx, false,
  573. type_idx);
  574. type1 = ref_type1.ref_type;
  575. }
  576. if (!wasm_reftype_is_subtype_of(type1, &ref_type1, type, ref_type,
  577. module->types,
  578. module->type_count)) {
  579. goto fail_type_mismatch;
  580. }
  581. /* Use UINT32_MAX to indicate that it is an null reference */
  582. init_expr->u.ref_index = UINT32_MAX;
  583. #endif
  584. break;
  585. }
  586. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  587. /* get_global */
  588. case INIT_EXPR_TYPE_GET_GLOBAL:
  589. {
  590. uint32 global_idx;
  591. read_leb_uint32(p, p_end, init_expr->u.global_index);
  592. global_idx = init_expr->u.global_index;
  593. #if WASM_ENABLE_GC == 0
  594. if (global_idx >= module->import_global_count) {
  595. /**
  596. * Currently, constant expressions occurring as initializers
  597. * of globals are further constrained in that contained
  598. * global.get instructions are
  599. * only allowed to refer to imported globals.
  600. */
  601. set_error_buf_v(error_buf, error_buf_size, "unknown global %u",
  602. global_idx);
  603. return false;
  604. }
  605. if (module->import_globals[global_idx].u.global.is_mutable) {
  606. set_error_buf_v(error_buf, error_buf_size,
  607. "constant expression required");
  608. return false;
  609. }
  610. #else
  611. if (global_idx
  612. >= module->import_global_count + module->global_count) {
  613. set_error_buf_v(error_buf, error_buf_size, "unknown global %u",
  614. global_idx);
  615. return false;
  616. }
  617. if (global_idx < module->import_global_count
  618. && module->import_globals[global_idx].u.global.is_mutable) {
  619. set_error_buf_v(error_buf, error_buf_size,
  620. "constant expression required");
  621. return false;
  622. }
  623. #endif
  624. break;
  625. }
  626. #if WASM_ENABLE_GC != 0
  627. /* struct.new and array.new */
  628. case WASM_OP_GC_PREFIX:
  629. {
  630. uint8 opcode1;
  631. CHECK_BUF(p, p_end, 1);
  632. opcode1 = read_uint8(p);
  633. switch (opcode1) {
  634. case WASM_OP_STRUCT_NEW_CANON:
  635. {
  636. set_error_buf(
  637. error_buf, error_buf_size,
  638. "unsuppoted constant expression of struct.new_canon");
  639. return false;
  640. }
  641. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  642. {
  643. init_expr->init_expr_type =
  644. INIT_EXPR_TYPE_STRUCT_NEW_CANON_DEFAULT;
  645. read_leb_uint32(p, p_end, init_expr->u.type_index);
  646. type_idx = init_expr->u.type_index;
  647. if (!check_type_index(module, type_idx, error_buf,
  648. error_buf_size)) {
  649. return false;
  650. }
  651. if (module->types[type_idx]->type_flag
  652. != WASM_TYPE_STRUCT) {
  653. set_error_buf(error_buf, error_buf_size,
  654. "unkown struct type");
  655. return false;
  656. }
  657. break;
  658. }
  659. case WASM_OP_ARRAY_NEW_CANON:
  660. {
  661. set_error_buf(
  662. error_buf, error_buf_size,
  663. "unsuppoted constant expression of array.new_canon");
  664. return false;
  665. }
  666. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  667. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  668. {
  669. init_expr->init_expr_type =
  670. (opcode1 == WASM_OP_ARRAY_NEW_CANON_DEFAULT)
  671. ? INIT_EXPR_TYPE_ARRAY_NEW_CANON_DEFAULT
  672. : INIT_EXPR_TYPE_ARRAY_NEW_CANON_FIXED;
  673. read_leb_uint32(p, p_end, init_expr->u.type_index);
  674. type_idx = init_expr->u.type_index;
  675. if (!check_array_type(module, type_idx, error_buf,
  676. error_buf_size)) {
  677. return false;
  678. }
  679. if (opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  680. read_leb_uint32(p, p_end,
  681. init_expr->u.array_new_canon_fixed.N);
  682. }
  683. break;
  684. }
  685. case WASM_OP_EXTERN_INTERNALIZE:
  686. {
  687. set_error_buf(
  688. error_buf, error_buf_size,
  689. "unsuppoted constant expression of extern.internalize");
  690. return false;
  691. }
  692. case WASM_OP_EXTERN_EXTERNALIZE:
  693. {
  694. set_error_buf(
  695. error_buf, error_buf_size,
  696. "unsuppoted constant expression of extern.externalize");
  697. return false;
  698. }
  699. default:
  700. goto fail_type_mismatch;
  701. }
  702. break;
  703. }
  704. #endif /* end of WASM_ENABLE_GC != 0 */
  705. default:
  706. {
  707. set_error_buf(error_buf, error_buf_size,
  708. "illegal opcode "
  709. "or constant expression required "
  710. "or type mismatch");
  711. goto fail;
  712. }
  713. }
  714. CHECK_BUF(p, p_end, 1);
  715. end_byte = read_uint8(p);
  716. if (end_byte != 0x0b)
  717. goto fail_type_mismatch;
  718. *p_buf = p;
  719. return true;
  720. fail_type_mismatch:
  721. set_error_buf(error_buf, error_buf_size,
  722. "type mismatch or constant expression required");
  723. fail:
  724. return false;
  725. }
  726. static bool
  727. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  728. {
  729. if (mutable >= 2) {
  730. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  731. return false;
  732. }
  733. return true;
  734. }
  735. #if WASM_ENABLE_GC != 0
  736. static void
  737. destroy_func_type(WASMFuncType *type)
  738. {
  739. /* Destroy the reference type hash set */
  740. if (type->ref_type_maps)
  741. wasm_runtime_free(type->ref_type_maps);
  742. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  743. && WASM_ENABLE_LAZY_JIT != 0
  744. if (type->call_to_llvm_jit_from_fast_jit)
  745. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  746. #endif
  747. /* Free the type */
  748. wasm_runtime_free(type);
  749. }
  750. static void
  751. destroy_struct_type(WASMStructType *type)
  752. {
  753. if (type->ref_type_maps)
  754. wasm_runtime_free(type->ref_type_maps);
  755. wasm_runtime_free(type);
  756. }
  757. static void
  758. destroy_array_type(WASMArrayType *type)
  759. {
  760. wasm_runtime_free(type);
  761. }
  762. static void
  763. destroy_wasm_type(WASMType *type)
  764. {
  765. if (type->type_flag == WASM_TYPE_FUNC)
  766. destroy_func_type((WASMFuncType *)type);
  767. else if (type->type_flag == WASM_TYPE_STRUCT)
  768. destroy_struct_type((WASMStructType *)type);
  769. else if (type->type_flag == WASM_TYPE_ARRAY)
  770. destroy_array_type((WASMArrayType *)type);
  771. else {
  772. bh_assert(0);
  773. }
  774. }
  775. /* Resolve (ref null ht) or (ref ht) */
  776. static bool
  777. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  778. WASMModule *module, bool nullable, WASMRefType *ref_type,
  779. char *error_buf, uint32 error_buf_size)
  780. {
  781. const uint8 *p = *p_buf, *p_end = buf_end;
  782. ref_type->ref_type =
  783. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  784. ref_type->ref_ht_common.nullable = nullable;
  785. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  786. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  787. /* heap type is (type i), i : typeidx, >= 0 */
  788. if (!check_type_index(module, ref_type->ref_ht_typeidx.type_idx,
  789. error_buf, error_buf_size)) {
  790. return false;
  791. }
  792. }
  793. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  794. /* heap type is func, extern, any, eq, i31 or data */
  795. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  796. return false;
  797. }
  798. *p_buf = p;
  799. return true;
  800. fail:
  801. return false;
  802. }
  803. static bool
  804. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  805. WASMModule *module, bool *p_need_ref_type_map,
  806. WASMRefType *ref_type, bool allow_packed_type,
  807. char *error_buf, uint32 error_buf_size)
  808. {
  809. const uint8 *p = *p_buf, *p_end = buf_end;
  810. uint8 type;
  811. memset(ref_type, 0, sizeof(WASMRefType));
  812. CHECK_BUF(p, p_end, 1);
  813. type = read_uint8(p);
  814. if (wasm_is_reftype_htref_nullable(type)) {
  815. /* (ref null ht) */
  816. if (!resolve_reftype_htref(&p, p_end, module, true, ref_type, error_buf,
  817. error_buf_size))
  818. return false;
  819. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  820. *p_need_ref_type_map = true;
  821. else {
  822. /* For (ref null func/extern/any/eq/i31/data), they are same as
  823. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  824. multi-byte type to one-byte type to reduce the footprint and
  825. the complexity of type equal/subtype checking */
  826. ref_type->ref_type =
  827. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  828. *p_need_ref_type_map = false;
  829. }
  830. }
  831. else if (wasm_is_reftype_htref_non_nullable(type)) {
  832. /* (ref ht) */
  833. if (!resolve_reftype_htref(&p, p_end, module, false, ref_type,
  834. error_buf, error_buf_size))
  835. return false;
  836. *p_need_ref_type_map = true;
  837. }
  838. else {
  839. /* type which can be represented by one byte */
  840. if (!is_value_type(type)
  841. && !(allow_packed_type && is_packed_type(type))) {
  842. set_error_buf(error_buf, error_buf_size, "type mismatch");
  843. return false;
  844. }
  845. ref_type->ref_type = type;
  846. *p_need_ref_type_map = false;
  847. }
  848. *p_buf = p;
  849. return true;
  850. fail:
  851. return false;
  852. }
  853. static WASMRefType *
  854. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  855. char *error_buf, uint32 error_buf_size)
  856. {
  857. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  858. if (!ret) {
  859. set_error_buf(error_buf, error_buf_size,
  860. "insert ref type to hash set failed");
  861. }
  862. return ret;
  863. }
  864. static bool
  865. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  866. uint32 type_idx, char *error_buf, uint32 error_buf_size)
  867. {
  868. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  869. uint32 param_count, result_count, i, j = 0;
  870. uint32 param_cell_num, ret_cell_num;
  871. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  872. uint64 total_size;
  873. bool need_ref_type_map;
  874. WASMRefType ref_type;
  875. WASMFuncType *type = NULL;
  876. /* Parse first time to resolve param count, result count and
  877. ref type map count */
  878. read_leb_uint32(p, p_end, param_count);
  879. p_org = p;
  880. for (i = 0; i < param_count; i++) {
  881. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  882. &ref_type, false, error_buf, error_buf_size)) {
  883. return false;
  884. }
  885. if (need_ref_type_map)
  886. ref_type_map_count++;
  887. }
  888. read_leb_uint32(p, p_end, result_count);
  889. for (i = 0; i < result_count; i++) {
  890. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  891. &ref_type, false, error_buf, error_buf_size)) {
  892. return false;
  893. }
  894. if (need_ref_type_map) {
  895. ref_type_map_count++;
  896. result_ref_type_map_count++;
  897. }
  898. }
  899. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  900. "ref type map count: %d\n",
  901. type_idx, param_count, result_count, ref_type_map_count);
  902. /* Parse second time to resolve param types, result types and
  903. ref type map info */
  904. p = p_org;
  905. total_size = offsetof(WASMFuncType, types)
  906. + sizeof(uint8) * (uint64)(param_count + result_count);
  907. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  908. return false;
  909. }
  910. if (ref_type_map_count > 0) {
  911. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  912. if (!(type->ref_type_maps =
  913. loader_malloc(total_size, error_buf, error_buf_size))) {
  914. goto fail;
  915. }
  916. }
  917. type->type_flag = WASM_TYPE_FUNC;
  918. type->param_count = param_count;
  919. type->result_count = result_count;
  920. type->ref_type_map_count = ref_type_map_count;
  921. type->result_ref_type_maps =
  922. type->ref_type_maps + ref_type_map_count - result_ref_type_map_count;
  923. for (i = 0; i < param_count; i++) {
  924. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  925. &ref_type, false, error_buf, error_buf_size)) {
  926. goto fail;
  927. }
  928. type->types[i] = ref_type.ref_type;
  929. if (need_ref_type_map) {
  930. type->ref_type_maps[j].index = i;
  931. if (!(type->ref_type_maps[j++].ref_type =
  932. reftype_set_insert(module->ref_type_set, &ref_type,
  933. error_buf, error_buf_size))) {
  934. goto fail;
  935. }
  936. }
  937. }
  938. read_leb_uint32(p, p_end, result_count);
  939. for (i = 0; i < result_count; i++) {
  940. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  941. &ref_type, false, error_buf, error_buf_size)) {
  942. goto fail;
  943. }
  944. type->types[param_count + i] = ref_type.ref_type;
  945. if (need_ref_type_map) {
  946. type->ref_type_maps[j].index = param_count + i;
  947. if (!(type->ref_type_maps[j++].ref_type =
  948. reftype_set_insert(module->ref_type_set, &ref_type,
  949. error_buf, error_buf_size))) {
  950. goto fail;
  951. }
  952. }
  953. }
  954. bh_assert(j == type->ref_type_map_count);
  955. #if TRACE_WASM_LOADER != 0
  956. os_printf("type %d = ", type_idx);
  957. wasm_dump_func_type(type);
  958. #endif
  959. param_cell_num = wasm_get_cell_num(type->types, param_count);
  960. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  961. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  962. set_error_buf(error_buf, error_buf_size,
  963. "param count or result count too large");
  964. goto fail;
  965. }
  966. type->param_cell_num = (uint16)param_cell_num;
  967. type->ret_cell_num = (uint16)ret_cell_num;
  968. /* Calculate the minimal type index of the type equal to this type */
  969. type->min_type_idx_normalized = type_idx;
  970. for (i = 0; i < type_idx; i++) {
  971. WASMFuncType *func_type = (WASMFuncType *)module->types[i];
  972. if (func_type->type_flag == WASM_TYPE_FUNC
  973. && wasm_func_type_equal(type, func_type, module->types,
  974. type_idx + 1)) {
  975. type->min_type_idx_normalized = i;
  976. break;
  977. }
  978. }
  979. *p_buf = p;
  980. module->types[type_idx] = (WASMType *)type;
  981. return true;
  982. fail:
  983. if (type)
  984. destroy_func_type(type);
  985. return false;
  986. }
  987. static bool
  988. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  989. WASMModule *module, uint32 type_idx, char *error_buf,
  990. uint32 error_buf_size)
  991. {
  992. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  993. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  994. uint32 i, j = 0, offset;
  995. uint16 *reference_table;
  996. uint64 total_size;
  997. uint8 mutable;
  998. bool need_ref_type_map;
  999. WASMRefType ref_type;
  1000. WASMStructType *type = NULL;
  1001. /* Parse first time to resolve field count and ref type map count */
  1002. read_leb_uint32(p, p_end, field_count);
  1003. p_org = p;
  1004. for (i = 0; i < field_count; i++) {
  1005. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1006. &ref_type, true, error_buf, error_buf_size)) {
  1007. return false;
  1008. }
  1009. if (need_ref_type_map)
  1010. ref_type_map_count++;
  1011. if (wasm_is_type_reftype(ref_type.ref_type))
  1012. ref_field_count++;
  1013. CHECK_BUF(p, p_end, 1);
  1014. mutable = read_uint8(p);
  1015. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1016. return false;
  1017. }
  1018. }
  1019. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d\n",
  1020. type_idx, field_count, ref_type_map_count);
  1021. /* Parse second time to resolve field types and ref type map info */
  1022. p = p_org;
  1023. total_size = offsetof(WASMStructType, fields)
  1024. + sizeof(WASMStructFieldType) * (uint64)field_count
  1025. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1026. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1027. return false;
  1028. }
  1029. if (ref_type_map_count > 0) {
  1030. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1031. if (!(type->ref_type_maps =
  1032. loader_malloc(total_size, error_buf, error_buf_size))) {
  1033. goto fail;
  1034. }
  1035. }
  1036. type->reference_table = reference_table =
  1037. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1038. + sizeof(WASMStructFieldType) * field_count);
  1039. *reference_table++ = ref_field_count;
  1040. type->type_flag = WASM_TYPE_STRUCT;
  1041. type->field_count = field_count;
  1042. type->ref_type_map_count = ref_type_map_count;
  1043. offset = (uint32)sizeof(WASMStructObject);
  1044. for (i = 0; i < field_count; i++) {
  1045. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  1046. &ref_type, true, error_buf, error_buf_size)) {
  1047. goto fail;
  1048. }
  1049. type->fields[i].field_type = ref_type.ref_type;
  1050. if (need_ref_type_map) {
  1051. type->ref_type_maps[j].index = i;
  1052. if (!(type->ref_type_maps[j++].ref_type =
  1053. reftype_set_insert(module->ref_type_set, &ref_type,
  1054. error_buf, error_buf_size))) {
  1055. goto fail;
  1056. }
  1057. }
  1058. type->fields[i].field_flags = read_uint8(p);
  1059. type->fields[i].field_size =
  1060. (uint8)wasm_reftype_size(ref_type.ref_type);
  1061. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1062. || defined(BUILD_TARGET_X86_32))
  1063. if (type->fields[i].field_size == 2)
  1064. offset = align_uint(offset, 2);
  1065. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1066. offset = align_uint(offset, 4);
  1067. #endif
  1068. type->fields[i].field_offset = offset;
  1069. if (wasm_is_type_reftype(ref_type.ref_type))
  1070. *reference_table++ = offset;
  1071. offset += type->fields[i].field_size;
  1072. }
  1073. type->total_size = offset;
  1074. bh_assert(j == type->ref_type_map_count);
  1075. #if TRACE_WASM_LOADER != 0
  1076. os_printf("type %d = ", type_idx);
  1077. wasm_dump_struct_type(type);
  1078. #endif
  1079. *p_buf = p;
  1080. module->types[type_idx] = (WASMType *)type;
  1081. return true;
  1082. fail:
  1083. if (type)
  1084. destroy_struct_type(type);
  1085. return false;
  1086. }
  1087. static bool
  1088. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1089. WASMModule *module, uint32 type_idx, char *error_buf,
  1090. uint32 error_buf_size)
  1091. {
  1092. const uint8 *p = *p_buf, *p_end = buf_end;
  1093. uint8 mutable;
  1094. bool need_ref_type_map;
  1095. WASMRefType ref_type;
  1096. WASMArrayType *type = NULL;
  1097. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  1098. true, error_buf, error_buf_size)) {
  1099. return false;
  1100. }
  1101. CHECK_BUF(p, p_end, 1);
  1102. mutable = read_uint8(p);
  1103. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1104. return false;
  1105. }
  1106. LOG_VERBOSE("type %u: array\n", type_idx);
  1107. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1108. error_buf_size))) {
  1109. return false;
  1110. }
  1111. type->type_flag = WASM_TYPE_ARRAY;
  1112. type->elem_flags = mutable;
  1113. type->elem_type = ref_type.ref_type;
  1114. if (need_ref_type_map) {
  1115. if (!(type->elem_ref_type =
  1116. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1117. error_buf_size))) {
  1118. goto fail;
  1119. }
  1120. }
  1121. #if TRACE_WASM_LOADER != 0
  1122. os_printf("type %d = ", type_idx);
  1123. wasm_dump_array_type(type);
  1124. #endif
  1125. *p_buf = p;
  1126. module->types[type_idx] = (WASMType *)type;
  1127. return true;
  1128. fail:
  1129. if (type)
  1130. destroy_array_type(type);
  1131. return false;
  1132. }
  1133. #else /* else of WASM_ENABLE_GC != 0 */
  1134. static void
  1135. destroy_wasm_type(WASMType *type)
  1136. {
  1137. if (type->ref_count > 1) {
  1138. /* The type is referenced by other types
  1139. of current wasm module */
  1140. type->ref_count--;
  1141. return;
  1142. }
  1143. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1144. && WASM_ENABLE_LAZY_JIT != 0
  1145. if (type->call_to_llvm_jit_from_fast_jit)
  1146. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1147. #endif
  1148. wasm_runtime_free(type);
  1149. }
  1150. #endif /* end of WASM_ENABLE_GC != 0 */
  1151. static bool
  1152. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1153. char *error_buf, uint32 error_buf_size)
  1154. {
  1155. const uint8 *p = buf, *p_end = buf_end;
  1156. uint32 type_count, i;
  1157. uint64 total_size;
  1158. uint8 flag;
  1159. read_leb_uint32(p, p_end, type_count);
  1160. if (type_count) {
  1161. module->type_count = type_count;
  1162. total_size = sizeof(WASMFuncType *) * (uint64)type_count;
  1163. if (!(module->types =
  1164. loader_malloc(total_size, error_buf, error_buf_size))) {
  1165. return false;
  1166. }
  1167. #if WASM_ENABLE_GC == 0
  1168. for (i = 0; i < type_count; i++) {
  1169. WASMFuncType *type;
  1170. const uint8 *p_org;
  1171. uint32 param_count, result_count, j;
  1172. uint32 param_cell_num, ret_cell_num;
  1173. CHECK_BUF(p, p_end, 1);
  1174. flag = read_uint8(p);
  1175. if (flag != 0x60) {
  1176. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1177. return false;
  1178. }
  1179. read_leb_uint32(p, p_end, param_count);
  1180. /* Resolve param count and result count firstly */
  1181. p_org = p;
  1182. CHECK_BUF(p, p_end, param_count);
  1183. p += param_count;
  1184. read_leb_uint32(p, p_end, result_count);
  1185. CHECK_BUF(p, p_end, result_count);
  1186. p = p_org;
  1187. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1188. set_error_buf(error_buf, error_buf_size,
  1189. "param count or result count too large");
  1190. return false;
  1191. }
  1192. total_size = offsetof(WASMFuncType, types)
  1193. + sizeof(uint8) * (uint64)(param_count + result_count);
  1194. if (!(type = module->types[i] =
  1195. loader_malloc(total_size, error_buf, error_buf_size))) {
  1196. return false;
  1197. }
  1198. /* Resolve param types and result types */
  1199. type->ref_count = 1;
  1200. type->param_count = (uint16)param_count;
  1201. type->result_count = (uint16)result_count;
  1202. for (j = 0; j < param_count; j++) {
  1203. CHECK_BUF(p, p_end, 1);
  1204. type->types[j] = read_uint8(p);
  1205. }
  1206. read_leb_uint32(p, p_end, result_count);
  1207. for (j = 0; j < result_count; j++) {
  1208. CHECK_BUF(p, p_end, 1);
  1209. type->types[param_count + j] = read_uint8(p);
  1210. }
  1211. for (j = 0; j < param_count + result_count; j++) {
  1212. if (!is_value_type(type->types[j])) {
  1213. set_error_buf(error_buf, error_buf_size,
  1214. "unknown value type");
  1215. return false;
  1216. }
  1217. }
  1218. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1219. ret_cell_num =
  1220. wasm_get_cell_num(type->types + param_count, result_count);
  1221. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1222. set_error_buf(error_buf, error_buf_size,
  1223. "param count or result count too large");
  1224. return false;
  1225. }
  1226. type->param_cell_num = (uint16)param_cell_num;
  1227. type->ret_cell_num = (uint16)ret_cell_num;
  1228. /* If there is already a same type created, use it instead */
  1229. for (j = 0; j < i; j++) {
  1230. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1231. if (module->types[j]->ref_count == UINT16_MAX) {
  1232. set_error_buf(error_buf, error_buf_size,
  1233. "wasm type's ref count too large");
  1234. return false;
  1235. }
  1236. destroy_wasm_type(type);
  1237. module->types[i] = module->types[j];
  1238. module->types[j]->ref_count++;
  1239. break;
  1240. }
  1241. }
  1242. }
  1243. #else /* else of WASM_ENABLE_GC == 0 */
  1244. for (i = 0; i < type_count; i++) {
  1245. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1246. bool is_sub_final = true;
  1247. CHECK_BUF(p, p_end, 1);
  1248. flag = read_uint8(p);
  1249. if (flag == DEFINED_TYPE_SUB || flag == DEFINED_TYPE_SUB_FINAL) {
  1250. read_leb_uint32(p, p_end, super_type_count);
  1251. if (super_type_count > 1) {
  1252. set_error_buf(error_buf, error_buf_size,
  1253. "super type count too large");
  1254. return false;
  1255. }
  1256. if (super_type_count > 0) {
  1257. read_leb_uint32(p, p_end, parent_type_idx);
  1258. if (parent_type_idx >= i) {
  1259. set_error_buf_v(error_buf, error_buf_size,
  1260. "unknown type %d", parent_type_idx);
  1261. return false;
  1262. }
  1263. if (module->types[parent_type_idx]->is_sub_final) {
  1264. set_error_buf(error_buf, error_buf_size,
  1265. "sub type can not inherit from "
  1266. "a final super type");
  1267. return false;
  1268. }
  1269. }
  1270. if (flag == DEFINED_TYPE_SUB)
  1271. is_sub_final = false;
  1272. CHECK_BUF(p, p_end, 1);
  1273. flag = read_uint8(p);
  1274. }
  1275. #if WASM_ENABLE_GC_BINARYEN != 0
  1276. else {
  1277. is_sub_final = false;
  1278. }
  1279. #endif
  1280. if (flag == DEFINED_TYPE_FUNC) {
  1281. if (!resolve_func_type(&p, buf_end, module, i, error_buf,
  1282. error_buf_size)) {
  1283. return false;
  1284. }
  1285. }
  1286. else if (flag == DEFINED_TYPE_STRUCT) {
  1287. if (!resolve_struct_type(&p, buf_end, module, i, error_buf,
  1288. error_buf_size)) {
  1289. return false;
  1290. }
  1291. }
  1292. else if (flag == DEFINED_TYPE_ARRAY) {
  1293. if (!resolve_array_type(&p, buf_end, module, i, error_buf,
  1294. error_buf_size)) {
  1295. return false;
  1296. }
  1297. }
  1298. else {
  1299. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1300. return false;
  1301. }
  1302. if (parent_type_idx != (uint32)-1) { /* has parent */
  1303. WASMType *parent_type = module->types[parent_type_idx];
  1304. if (!wasm_type_is_subtype_of(module->types[i], parent_type,
  1305. module->types, i)) {
  1306. set_error_buf(error_buf, error_buf_size,
  1307. "sub type does not match super type");
  1308. return false;
  1309. }
  1310. module->types[i]->parent_type = parent_type;
  1311. module->types[i]->root_type = parent_type->root_type;
  1312. module->types[i]->inherit_depth =
  1313. parent_type->inherit_depth + 1;
  1314. }
  1315. else {
  1316. module->types[i]->parent_type = NULL;
  1317. module->types[i]->root_type = module->types[i];
  1318. module->types[i]->inherit_depth = 0;
  1319. }
  1320. module->types[i]->parent_type_idx = parent_type_idx;
  1321. module->types[i]->is_sub_final = is_sub_final;
  1322. }
  1323. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1324. * module->type_count,
  1325. error_buf, error_buf_size))) {
  1326. return false;
  1327. }
  1328. #endif /* end of WASM_ENABLE_GC == 0 */
  1329. }
  1330. if (p != p_end) {
  1331. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1332. return false;
  1333. }
  1334. LOG_VERBOSE("Load type section success.\n");
  1335. return true;
  1336. fail:
  1337. return false;
  1338. }
  1339. static void
  1340. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1341. {
  1342. uint32 default_max_size =
  1343. init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
  1344. if (max_size_flag) {
  1345. /* module defines the table limitation */
  1346. bh_assert(init_size <= *max_size);
  1347. if (init_size < *max_size) {
  1348. *max_size =
  1349. *max_size < default_max_size ? *max_size : default_max_size;
  1350. }
  1351. }
  1352. else {
  1353. /* partial defined table limitation, gives a default value */
  1354. *max_size = default_max_size;
  1355. }
  1356. }
  1357. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1358. /**
  1359. * Find export item of a module with export info:
  1360. * module name, field name and export kind
  1361. */
  1362. static WASMExport *
  1363. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1364. const char *field_name, uint8 export_kind,
  1365. char *error_buf, uint32 error_buf_size)
  1366. {
  1367. WASMExport *export;
  1368. uint32 i;
  1369. for (i = 0, export = module->exports; i < module->export_count;
  1370. ++i, ++export) {
  1371. /**
  1372. * need to consider a scenario that different kinds of exports
  1373. * may have the same name, like
  1374. * (table (export "m1" "exported") 10 funcref)
  1375. * (memory (export "m1" "exported") 10)
  1376. **/
  1377. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  1378. break;
  1379. }
  1380. }
  1381. if (i == module->export_count) {
  1382. LOG_DEBUG("can not find an export %d named %s in the module %s",
  1383. export_kind, field_name, module_name);
  1384. set_error_buf(error_buf, error_buf_size,
  1385. "unknown import or incompatible import type");
  1386. return NULL;
  1387. }
  1388. (void)module_name;
  1389. /* since there is a validation in load_export_section(), it is for sure
  1390. * export->index is valid*/
  1391. return export;
  1392. }
  1393. #endif
  1394. #if WASM_ENABLE_MULTI_MODULE != 0
  1395. static WASMFunction *
  1396. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  1397. const WASMFuncType *expected_function_type,
  1398. char *error_buf, uint32 error_buf_size)
  1399. {
  1400. WASMModuleCommon *module_reg;
  1401. WASMFunction *function = NULL;
  1402. WASMExport *export = NULL;
  1403. WASMModule *module = NULL;
  1404. WASMFuncType *target_function_type = NULL;
  1405. module_reg = wasm_runtime_find_module_registered(module_name);
  1406. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1407. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  1408. function_name);
  1409. set_error_buf(error_buf, error_buf_size, "unknown import");
  1410. return NULL;
  1411. }
  1412. module = (WASMModule *)module_reg;
  1413. export =
  1414. wasm_loader_find_export(module, module_name, function_name,
  1415. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  1416. if (!export) {
  1417. return NULL;
  1418. }
  1419. /* resolve function type and function */
  1420. if (export->index < module->import_function_count) {
  1421. target_function_type =
  1422. module->import_functions[export->index].u.function.func_type;
  1423. function = module->import_functions[export->index]
  1424. .u.function.import_func_linked;
  1425. }
  1426. else {
  1427. target_function_type =
  1428. module->functions[export->index - module->import_function_count]
  1429. ->func_type;
  1430. function =
  1431. module->functions[export->index - module->import_function_count];
  1432. }
  1433. /* check function type */
  1434. if (!wasm_type_equal((WASMType *)expected_function_type,
  1435. (WASMType *)target_function_type, module->types,
  1436. module->type_count)) {
  1437. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  1438. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1439. return NULL;
  1440. }
  1441. return function;
  1442. }
  1443. static WASMTable *
  1444. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  1445. uint32 init_size, uint32 max_size, char *error_buf,
  1446. uint32 error_buf_size)
  1447. {
  1448. WASMModuleCommon *module_reg;
  1449. WASMTable *table = NULL;
  1450. WASMExport *export = NULL;
  1451. WASMModule *module = NULL;
  1452. module_reg = wasm_runtime_find_module_registered(module_name);
  1453. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1454. LOG_DEBUG("can not find a module named %s for table", module_name);
  1455. set_error_buf(error_buf, error_buf_size, "unknown import");
  1456. return NULL;
  1457. }
  1458. module = (WASMModule *)module_reg;
  1459. export =
  1460. wasm_loader_find_export(module, module_name, table_name,
  1461. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  1462. if (!export) {
  1463. return NULL;
  1464. }
  1465. /* resolve table and check the init/max size */
  1466. if (export->index < module->import_table_count) {
  1467. table =
  1468. module->import_tables[export->index].u.table.import_table_linked;
  1469. }
  1470. else {
  1471. table = &(module->tables[export->index - module->import_table_count]);
  1472. }
  1473. if (table->init_size < init_size || table->max_size > max_size) {
  1474. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  1475. module_name, table_name, table->init_size, table->max_size,
  1476. init_size, max_size);
  1477. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1478. return NULL;
  1479. }
  1480. return table;
  1481. }
  1482. static WASMMemory *
  1483. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  1484. uint32 init_page_count, uint32 max_page_count,
  1485. char *error_buf, uint32 error_buf_size)
  1486. {
  1487. WASMModuleCommon *module_reg;
  1488. WASMMemory *memory = NULL;
  1489. WASMExport *export = NULL;
  1490. WASMModule *module = NULL;
  1491. module_reg = wasm_runtime_find_module_registered(module_name);
  1492. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1493. LOG_DEBUG("can not find a module named %s for memory", module_name);
  1494. set_error_buf(error_buf, error_buf_size, "unknown import");
  1495. return NULL;
  1496. }
  1497. module = (WASMModule *)module_reg;
  1498. export =
  1499. wasm_loader_find_export(module, module_name, memory_name,
  1500. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  1501. if (!export) {
  1502. return NULL;
  1503. }
  1504. /* resolve memory and check the init/max page count */
  1505. if (export->index < module->import_memory_count) {
  1506. memory = module->import_memories[export->index]
  1507. .u.memory.import_memory_linked;
  1508. }
  1509. else {
  1510. memory =
  1511. &(module->memories[export->index - module->import_memory_count]);
  1512. }
  1513. if (memory->init_page_count < init_page_count
  1514. || memory->max_page_count > max_page_count) {
  1515. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  1516. module_name, memory_name, memory->init_page_count,
  1517. memory->max_page_count, init_page_count, max_page_count);
  1518. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1519. return NULL;
  1520. }
  1521. return memory;
  1522. }
  1523. static WASMGlobal *
  1524. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  1525. uint8 type, bool is_mutable, char *error_buf,
  1526. uint32 error_buf_size)
  1527. {
  1528. WASMModuleCommon *module_reg;
  1529. WASMGlobal *global = NULL;
  1530. WASMExport *export = NULL;
  1531. WASMModule *module = NULL;
  1532. module_reg = wasm_runtime_find_module_registered(module_name);
  1533. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1534. LOG_DEBUG("can not find a module named %s for global", module_name);
  1535. set_error_buf(error_buf, error_buf_size, "unknown import");
  1536. return NULL;
  1537. }
  1538. module = (WASMModule *)module_reg;
  1539. export =
  1540. wasm_loader_find_export(module, module_name, global_name,
  1541. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  1542. if (!export) {
  1543. return NULL;
  1544. }
  1545. /* resolve and check the global */
  1546. if (export->index < module->import_global_count) {
  1547. global =
  1548. module->import_globals[export->index].u.global.import_global_linked;
  1549. }
  1550. else {
  1551. global =
  1552. &(module->globals[export->index - module->import_global_count]);
  1553. }
  1554. if (global->type != type || global->is_mutable != is_mutable) {
  1555. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  1556. module_name, global_name, global->type, global->is_mutable,
  1557. type, is_mutable);
  1558. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1559. return NULL;
  1560. }
  1561. return global;
  1562. }
  1563. static WASMModule *
  1564. search_sub_module(const WASMModule *parent_module, const char *sub_module_name)
  1565. {
  1566. WASMRegisteredModule *node =
  1567. bh_list_first_elem(parent_module->import_module_list);
  1568. while (node && strcmp(sub_module_name, node->module_name)) {
  1569. node = bh_list_elem_next(node);
  1570. }
  1571. return node ? (WASMModule *)node->module : NULL;
  1572. }
  1573. static bool
  1574. register_sub_module(const WASMModule *parent_module,
  1575. const char *sub_module_name, WASMModule *sub_module)
  1576. {
  1577. /* register sub_module into its parent sub module list */
  1578. WASMRegisteredModule *node = NULL;
  1579. bh_list_status ret;
  1580. if (search_sub_module(parent_module, sub_module_name)) {
  1581. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  1582. return true;
  1583. }
  1584. node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
  1585. if (!node) {
  1586. return false;
  1587. }
  1588. node->module_name = sub_module_name;
  1589. node->module = (WASMModuleCommon *)sub_module;
  1590. ret = bh_list_insert(parent_module->import_module_list, node);
  1591. bh_assert(BH_LIST_SUCCESS == ret);
  1592. (void)ret;
  1593. return true;
  1594. }
  1595. static WASMModule *
  1596. load_depended_module(const WASMModule *parent_module,
  1597. const char *sub_module_name, char *error_buf,
  1598. uint32 error_buf_size)
  1599. {
  1600. WASMModule *sub_module = NULL;
  1601. bool ret = false;
  1602. uint8 *buffer = NULL;
  1603. uint32 buffer_size = 0;
  1604. const module_reader reader = wasm_runtime_get_module_reader();
  1605. const module_destroyer destroyer = wasm_runtime_get_module_destroyer();
  1606. /* check the registered module list of the parent */
  1607. sub_module = search_sub_module(parent_module, sub_module_name);
  1608. if (sub_module) {
  1609. LOG_DEBUG("%s has been loaded before", sub_module_name);
  1610. return sub_module;
  1611. }
  1612. /* check the global registered module list */
  1613. sub_module =
  1614. (WASMModule *)wasm_runtime_find_module_registered(sub_module_name);
  1615. if (sub_module) {
  1616. LOG_DEBUG("%s has been loaded", sub_module_name);
  1617. goto register_sub_module;
  1618. }
  1619. LOG_VERBOSE("loading %s", sub_module_name);
  1620. if (!reader) {
  1621. set_error_buf_v(error_buf, error_buf_size,
  1622. "no sub module reader to load %s", sub_module_name);
  1623. return NULL;
  1624. }
  1625. /* start to maintain a loading module list */
  1626. ret = wasm_runtime_is_loading_module(sub_module_name);
  1627. if (ret) {
  1628. set_error_buf_v(error_buf, error_buf_size,
  1629. "found circular dependency on %s", sub_module_name);
  1630. return NULL;
  1631. }
  1632. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  1633. error_buf_size);
  1634. if (!ret) {
  1635. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  1636. return NULL;
  1637. }
  1638. ret = reader(sub_module_name, &buffer, &buffer_size);
  1639. if (!ret) {
  1640. LOG_DEBUG("read the file of %s failed", sub_module_name);
  1641. set_error_buf_v(error_buf, error_buf_size, "unknown import",
  1642. sub_module_name);
  1643. goto delete_loading_module;
  1644. }
  1645. sub_module =
  1646. wasm_loader_load(buffer, buffer_size, false, error_buf, error_buf_size);
  1647. if (!sub_module) {
  1648. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  1649. /* others will be destroyed in runtime_destroy() */
  1650. goto destroy_file_buffer;
  1651. }
  1652. wasm_runtime_delete_loading_module(sub_module_name);
  1653. /* register on a global list */
  1654. ret = wasm_runtime_register_module_internal(
  1655. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  1656. error_buf, error_buf_size);
  1657. if (!ret) {
  1658. LOG_DEBUG("error: can not register module %s globally\n",
  1659. sub_module_name);
  1660. /* others will be unloaded in runtime_destroy() */
  1661. goto unload_module;
  1662. }
  1663. /* register into its parent list */
  1664. register_sub_module:
  1665. ret = register_sub_module(parent_module, sub_module_name, sub_module);
  1666. if (!ret) {
  1667. set_error_buf_v(error_buf, error_buf_size,
  1668. "failed to register sub module %s", sub_module_name);
  1669. /* since it is in the global module list, no need to
  1670. * unload the module. the runtime_destroy() will do it
  1671. */
  1672. return NULL;
  1673. }
  1674. return sub_module;
  1675. unload_module:
  1676. wasm_loader_unload(sub_module);
  1677. destroy_file_buffer:
  1678. if (destroyer) {
  1679. destroyer(buffer, buffer_size);
  1680. }
  1681. else {
  1682. LOG_WARNING("need to release the reading buffer of %s manually",
  1683. sub_module_name);
  1684. }
  1685. delete_loading_module:
  1686. wasm_runtime_delete_loading_module(sub_module_name);
  1687. return NULL;
  1688. }
  1689. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  1690. static bool
  1691. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  1692. const WASMModule *parent_module,
  1693. const char *sub_module_name, const char *function_name,
  1694. WASMFunctionImport *function, char *error_buf,
  1695. uint32 error_buf_size)
  1696. {
  1697. const uint8 *p = *p_buf, *p_end = buf_end;
  1698. uint32 declare_type_index = 0;
  1699. WASMFuncType *declare_func_type = NULL;
  1700. WASMFunction *linked_func = NULL;
  1701. #if WASM_ENABLE_MULTI_MODULE != 0
  1702. WASMModule *sub_module = NULL;
  1703. #endif
  1704. const char *linked_signature = NULL;
  1705. void *linked_attachment = NULL;
  1706. bool linked_call_conv_raw = false;
  1707. bool is_native_symbol = false;
  1708. read_leb_uint32(p, p_end, declare_type_index);
  1709. *p_buf = p;
  1710. if (declare_type_index >= parent_module->type_count) {
  1711. set_error_buf(error_buf, error_buf_size, "unknown type");
  1712. return false;
  1713. }
  1714. #if WASM_ENABLE_GC != 0
  1715. function->type_idx = declare_type_index;
  1716. #endif
  1717. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1718. declare_type_index = wasm_get_smallest_type_idx(
  1719. parent_module->types, parent_module->type_count, declare_type_index);
  1720. #endif
  1721. declare_func_type =
  1722. (WASMFuncType *)parent_module->types[declare_type_index];
  1723. /* lookup registered native symbols first */
  1724. linked_func = wasm_native_resolve_symbol(
  1725. sub_module_name, function_name, declare_func_type, &linked_signature,
  1726. &linked_attachment, &linked_call_conv_raw);
  1727. if (linked_func) {
  1728. is_native_symbol = true;
  1729. }
  1730. #if WASM_ENABLE_MULTI_MODULE != 0
  1731. else {
  1732. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1733. sub_module = load_depended_module(parent_module, sub_module_name,
  1734. error_buf, error_buf_size);
  1735. if (!sub_module) {
  1736. return false;
  1737. }
  1738. }
  1739. linked_func = wasm_loader_resolve_function(
  1740. sub_module_name, function_name, declare_func_type, error_buf,
  1741. error_buf_size);
  1742. }
  1743. #endif
  1744. function->module_name = (char *)sub_module_name;
  1745. function->field_name = (char *)function_name;
  1746. function->func_type = declare_func_type;
  1747. /* func_ptr_linked is for native registered symbol */
  1748. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  1749. function->signature = linked_signature;
  1750. function->attachment = linked_attachment;
  1751. function->call_conv_raw = linked_call_conv_raw;
  1752. #if WASM_ENABLE_MULTI_MODULE != 0
  1753. function->import_module = is_native_symbol ? NULL : sub_module;
  1754. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  1755. #endif
  1756. return true;
  1757. fail:
  1758. return false;
  1759. }
  1760. static bool
  1761. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1762. uint32 error_buf_size)
  1763. {
  1764. if (max_size < init_size) {
  1765. set_error_buf(error_buf, error_buf_size,
  1766. "size minimum must not be greater than maximum");
  1767. return false;
  1768. }
  1769. return true;
  1770. }
  1771. static bool
  1772. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  1773. WASMModule *parent_module, const char *sub_module_name,
  1774. const char *table_name, WASMTableImport *table,
  1775. char *error_buf, uint32 error_buf_size)
  1776. {
  1777. const uint8 *p = *p_buf, *p_end = buf_end;
  1778. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  1779. declare_init_size = 0, declare_max_size = 0;
  1780. #if WASM_ENABLE_MULTI_MODULE != 0
  1781. WASMModule *sub_module = NULL;
  1782. WASMTable *linked_table = NULL;
  1783. #endif
  1784. #if WASM_ENABLE_GC != 0
  1785. WASMRefType ref_type;
  1786. bool need_ref_type_map;
  1787. #endif
  1788. #if WASM_ENABLE_GC == 0
  1789. CHECK_BUF(p, p_end, 1);
  1790. /* 0x70 or 0x6F */
  1791. declare_elem_type = read_uint8(p);
  1792. if (VALUE_TYPE_FUNCREF != declare_elem_type
  1793. #if WASM_ENABLE_REF_TYPES != 0
  1794. && VALUE_TYPE_EXTERNREF != declare_elem_type
  1795. #endif
  1796. ) {
  1797. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1798. return false;
  1799. }
  1800. #else /* else of WASM_ENABLE_GC == 0 */
  1801. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  1802. &ref_type, false, error_buf, error_buf_size)) {
  1803. return false;
  1804. }
  1805. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  1806. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1807. return false;
  1808. }
  1809. declare_elem_type = ref_type.ref_type;
  1810. if (need_ref_type_map) {
  1811. if (!(table->elem_ref_type =
  1812. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  1813. error_buf, error_buf_size))) {
  1814. return false;
  1815. }
  1816. }
  1817. #if TRACE_WASM_LOADER != 0
  1818. os_printf("import table type: ");
  1819. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  1820. os_printf("\n");
  1821. #endif
  1822. #endif /* end of WASM_ENABLE_GC == 0 */
  1823. read_leb_uint32(p, p_end, declare_max_size_flag);
  1824. if (declare_max_size_flag > 1) {
  1825. set_error_buf(error_buf, error_buf_size, "integer too large");
  1826. return false;
  1827. }
  1828. read_leb_uint32(p, p_end, declare_init_size);
  1829. if (declare_max_size_flag) {
  1830. read_leb_uint32(p, p_end, declare_max_size);
  1831. if (!check_table_max_size(declare_init_size, declare_max_size,
  1832. error_buf, error_buf_size))
  1833. return false;
  1834. }
  1835. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  1836. &declare_max_size);
  1837. *p_buf = p;
  1838. #if WASM_ENABLE_MULTI_MODULE != 0
  1839. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1840. sub_module = load_depended_module(parent_module, sub_module_name,
  1841. error_buf, error_buf_size);
  1842. if (!sub_module) {
  1843. return false;
  1844. }
  1845. linked_table = wasm_loader_resolve_table(
  1846. sub_module_name, table_name, declare_init_size, declare_max_size,
  1847. error_buf, error_buf_size);
  1848. if (!linked_table) {
  1849. return false;
  1850. }
  1851. /* reset with linked table limit */
  1852. declare_elem_type = linked_table->elem_type;
  1853. declare_init_size = linked_table->init_size;
  1854. declare_max_size = linked_table->max_size;
  1855. declare_max_size_flag = linked_table->flags;
  1856. table->import_table_linked = linked_table;
  1857. table->import_module = sub_module;
  1858. }
  1859. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  1860. /* (table (export "table") 10 20 funcref) */
  1861. /* we need this section working in wamrc */
  1862. if (!strcmp("spectest", sub_module_name)) {
  1863. const uint32 spectest_table_init_size = 10;
  1864. const uint32 spectest_table_max_size = 20;
  1865. if (strcmp("table", table_name)) {
  1866. set_error_buf(error_buf, error_buf_size,
  1867. "incompatible import type or unknown import");
  1868. return false;
  1869. }
  1870. if (declare_init_size > spectest_table_init_size
  1871. || declare_max_size < spectest_table_max_size) {
  1872. set_error_buf(error_buf, error_buf_size,
  1873. "incompatible import type");
  1874. return false;
  1875. }
  1876. declare_init_size = spectest_table_init_size;
  1877. declare_max_size = spectest_table_max_size;
  1878. }
  1879. /* now we believe all declaration are ok */
  1880. table->elem_type = declare_elem_type;
  1881. table->init_size = declare_init_size;
  1882. table->flags = declare_max_size_flag;
  1883. table->max_size = declare_max_size;
  1884. (void)parent_module;
  1885. return true;
  1886. fail:
  1887. return false;
  1888. }
  1889. static bool
  1890. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  1891. {
  1892. if (init_size > DEFAULT_MAX_PAGES) {
  1893. set_error_buf(error_buf, error_buf_size,
  1894. "memory size must be at most 65536 pages (4GiB)");
  1895. return false;
  1896. }
  1897. return true;
  1898. }
  1899. static bool
  1900. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1901. uint32 error_buf_size)
  1902. {
  1903. if (max_size < init_size) {
  1904. set_error_buf(error_buf, error_buf_size,
  1905. "size minimum must not be greater than maximum");
  1906. return false;
  1907. }
  1908. if (max_size > DEFAULT_MAX_PAGES) {
  1909. set_error_buf(error_buf, error_buf_size,
  1910. "memory size must be at most 65536 pages (4GiB)");
  1911. return false;
  1912. }
  1913. return true;
  1914. }
  1915. static bool
  1916. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  1917. WASMModule *parent_module, const char *sub_module_name,
  1918. const char *memory_name, WASMMemoryImport *memory,
  1919. char *error_buf, uint32 error_buf_size)
  1920. {
  1921. const uint8 *p = *p_buf, *p_end = buf_end;
  1922. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1923. uint32 pool_size = wasm_runtime_memory_pool_size();
  1924. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1925. / DEFAULT_NUM_BYTES_PER_PAGE;
  1926. #else
  1927. uint32 max_page_count = DEFAULT_MAX_PAGES;
  1928. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  1929. uint32 declare_max_page_count_flag = 0;
  1930. uint32 declare_init_page_count = 0;
  1931. uint32 declare_max_page_count = 0;
  1932. #if WASM_ENABLE_MULTI_MODULE != 0
  1933. WASMModule *sub_module = NULL;
  1934. WASMMemory *linked_memory = NULL;
  1935. #endif
  1936. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  1937. read_leb_uint32(p, p_end, declare_init_page_count);
  1938. if (!check_memory_init_size(declare_init_page_count, error_buf,
  1939. error_buf_size)) {
  1940. return false;
  1941. }
  1942. if (declare_max_page_count_flag & 1) {
  1943. read_leb_uint32(p, p_end, declare_max_page_count);
  1944. if (!check_memory_max_size(declare_init_page_count,
  1945. declare_max_page_count, error_buf,
  1946. error_buf_size)) {
  1947. return false;
  1948. }
  1949. if (declare_max_page_count > max_page_count) {
  1950. declare_max_page_count = max_page_count;
  1951. }
  1952. }
  1953. else {
  1954. /* Limit the maximum memory size to max_page_count */
  1955. declare_max_page_count = max_page_count;
  1956. }
  1957. #if WASM_ENABLE_MULTI_MODULE != 0
  1958. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1959. sub_module = load_depended_module(parent_module, sub_module_name,
  1960. error_buf, error_buf_size);
  1961. if (!sub_module) {
  1962. return false;
  1963. }
  1964. linked_memory = wasm_loader_resolve_memory(
  1965. sub_module_name, memory_name, declare_init_page_count,
  1966. declare_max_page_count, error_buf, error_buf_size);
  1967. if (!linked_memory) {
  1968. return false;
  1969. }
  1970. /**
  1971. * reset with linked memory limit
  1972. */
  1973. memory->import_module = sub_module;
  1974. memory->import_memory_linked = linked_memory;
  1975. declare_init_page_count = linked_memory->init_page_count;
  1976. declare_max_page_count = linked_memory->max_page_count;
  1977. }
  1978. #endif
  1979. /* (memory (export "memory") 1 2) */
  1980. if (!strcmp("spectest", sub_module_name)) {
  1981. uint32 spectest_memory_init_page = 1;
  1982. uint32 spectest_memory_max_page = 2;
  1983. if (strcmp("memory", memory_name)) {
  1984. set_error_buf(error_buf, error_buf_size,
  1985. "incompatible import type or unknown import");
  1986. return false;
  1987. }
  1988. if (declare_init_page_count > spectest_memory_init_page
  1989. || declare_max_page_count < spectest_memory_max_page) {
  1990. set_error_buf(error_buf, error_buf_size,
  1991. "incompatible import type");
  1992. return false;
  1993. }
  1994. declare_init_page_count = spectest_memory_init_page;
  1995. declare_max_page_count = spectest_memory_max_page;
  1996. }
  1997. /* now we believe all declaration are ok */
  1998. memory->flags = declare_max_page_count_flag;
  1999. memory->init_page_count = declare_init_page_count;
  2000. memory->max_page_count = declare_max_page_count;
  2001. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2002. *p_buf = p;
  2003. (void)parent_module;
  2004. return true;
  2005. fail:
  2006. return false;
  2007. }
  2008. static bool
  2009. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2010. WASMModule *parent_module, char *sub_module_name,
  2011. char *global_name, WASMGlobalImport *global, char *error_buf,
  2012. uint32 error_buf_size)
  2013. {
  2014. const uint8 *p = *p_buf, *p_end = buf_end;
  2015. uint8 declare_type = 0;
  2016. uint8 declare_mutable = 0;
  2017. #if WASM_ENABLE_MULTI_MODULE != 0
  2018. WASMModule *sub_module = NULL;
  2019. WASMGlobal *linked_global = NULL;
  2020. #endif
  2021. #if WASM_ENABLE_GC != 0
  2022. WASMRefType ref_type;
  2023. bool need_ref_type_map;
  2024. #endif
  2025. bool ret = false;
  2026. #if WASM_ENABLE_GC == 0
  2027. CHECK_BUF(p, p_end, 2);
  2028. declare_type = read_uint8(p);
  2029. declare_mutable = read_uint8(p);
  2030. #else
  2031. if (!resolve_value_type(&p, p_end, parent_module, &need_ref_type_map,
  2032. &ref_type, false, error_buf, error_buf_size)) {
  2033. return false;
  2034. }
  2035. declare_type = ref_type.ref_type;
  2036. if (need_ref_type_map) {
  2037. if (!(global->ref_type =
  2038. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2039. error_buf, error_buf_size))) {
  2040. return false;
  2041. }
  2042. }
  2043. #if TRACE_WASM_LOADER != 0
  2044. os_printf("import global type: ");
  2045. wasm_dump_value_type(declare_type, global->ref_type);
  2046. os_printf("\n");
  2047. #endif
  2048. CHECK_BUF(p, p_end, 1);
  2049. declare_mutable = read_uint8(p);
  2050. #endif /* end of WASM_ENABLE_GC == 0 */
  2051. *p_buf = p;
  2052. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2053. return false;
  2054. }
  2055. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2056. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2057. global);
  2058. if (ret) {
  2059. if (global->type != declare_type
  2060. || global->is_mutable != declare_mutable) {
  2061. set_error_buf(error_buf, error_buf_size,
  2062. "incompatible import type");
  2063. return false;
  2064. }
  2065. global->is_linked = true;
  2066. }
  2067. #endif
  2068. #if WASM_ENABLE_MULTI_MODULE != 0
  2069. if (!global->is_linked
  2070. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2071. sub_module = load_depended_module(parent_module, sub_module_name,
  2072. error_buf, error_buf_size);
  2073. if (!sub_module) {
  2074. return false;
  2075. }
  2076. /* check sub modules */
  2077. linked_global = wasm_loader_resolve_global(
  2078. sub_module_name, global_name, declare_type, declare_mutable,
  2079. error_buf, error_buf_size);
  2080. if (linked_global) {
  2081. global->import_module = sub_module;
  2082. global->import_global_linked = linked_global;
  2083. global->is_linked = true;
  2084. }
  2085. }
  2086. #endif
  2087. global->module_name = sub_module_name;
  2088. global->field_name = global_name;
  2089. global->type = declare_type;
  2090. global->is_mutable = (declare_mutable == 1);
  2091. (void)parent_module;
  2092. (void)ret;
  2093. return true;
  2094. fail:
  2095. return false;
  2096. }
  2097. static bool
  2098. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2099. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2100. {
  2101. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2102. #if WASM_ENABLE_GC != 0
  2103. WASMRefType ref_type;
  2104. bool need_ref_type_map;
  2105. #endif
  2106. #if WASM_ENABLE_GC == 0
  2107. CHECK_BUF(p, p_end, 1);
  2108. /* 0x70 or 0x6F */
  2109. table->elem_type = read_uint8(p);
  2110. if (VALUE_TYPE_FUNCREF != table->elem_type
  2111. #if WASM_ENABLE_REF_TYPES != 0
  2112. && VALUE_TYPE_EXTERNREF != table->elem_type
  2113. #endif
  2114. ) {
  2115. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2116. return false;
  2117. }
  2118. #else /* else of WASM_ENABLE_GC == 0 */
  2119. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map, &ref_type,
  2120. false, error_buf, error_buf_size)) {
  2121. return false;
  2122. }
  2123. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2124. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2125. return false;
  2126. }
  2127. table->elem_type = ref_type.ref_type;
  2128. if (need_ref_type_map) {
  2129. if (!(table->elem_ref_type =
  2130. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2131. error_buf_size))) {
  2132. return false;
  2133. }
  2134. }
  2135. #if TRACE_WASM_LOADER != 0
  2136. os_printf("table type: ");
  2137. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2138. os_printf("\n");
  2139. #endif
  2140. #endif /* end of WASM_ENABLE_GC == 0 */
  2141. p_org = p;
  2142. read_leb_uint32(p, p_end, table->flags);
  2143. #if WASM_ENABLE_SHARED_MEMORY == 0
  2144. if (p - p_org > 1) {
  2145. set_error_buf(error_buf, error_buf_size,
  2146. "integer representation too long");
  2147. return false;
  2148. }
  2149. if (table->flags > 1) {
  2150. set_error_buf(error_buf, error_buf_size, "integer too large");
  2151. return false;
  2152. }
  2153. #else
  2154. if (p - p_org > 1) {
  2155. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2156. return false;
  2157. }
  2158. if (table->flags == 2) {
  2159. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2160. return false;
  2161. }
  2162. if (table->flags > 1) {
  2163. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2164. return false;
  2165. }
  2166. #endif
  2167. read_leb_uint32(p, p_end, table->init_size);
  2168. if (table->flags) {
  2169. read_leb_uint32(p, p_end, table->max_size);
  2170. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2171. error_buf_size))
  2172. return false;
  2173. }
  2174. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2175. *p_buf = p;
  2176. return true;
  2177. fail:
  2178. return false;
  2179. }
  2180. static bool
  2181. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2182. char *error_buf, uint32 error_buf_size)
  2183. {
  2184. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2185. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2186. uint32 pool_size = wasm_runtime_memory_pool_size();
  2187. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2188. / DEFAULT_NUM_BYTES_PER_PAGE;
  2189. #else
  2190. uint32 max_page_count = DEFAULT_MAX_PAGES;
  2191. #endif
  2192. p_org = p;
  2193. read_leb_uint32(p, p_end, memory->flags);
  2194. #if WASM_ENABLE_SHARED_MEMORY == 0
  2195. if (p - p_org > 1) {
  2196. set_error_buf(error_buf, error_buf_size,
  2197. "integer representation too long");
  2198. return false;
  2199. }
  2200. if (memory->flags > 1) {
  2201. set_error_buf(error_buf, error_buf_size, "integer too large");
  2202. return false;
  2203. }
  2204. #else
  2205. if (p - p_org > 1) {
  2206. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2207. return false;
  2208. }
  2209. if (memory->flags > 3) {
  2210. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2211. return false;
  2212. }
  2213. else if (memory->flags == 2) {
  2214. set_error_buf(error_buf, error_buf_size,
  2215. "shared memory must have maximum");
  2216. return false;
  2217. }
  2218. #endif
  2219. read_leb_uint32(p, p_end, memory->init_page_count);
  2220. if (!check_memory_init_size(memory->init_page_count, error_buf,
  2221. error_buf_size))
  2222. return false;
  2223. if (memory->flags & 1) {
  2224. read_leb_uint32(p, p_end, memory->max_page_count);
  2225. if (!check_memory_max_size(memory->init_page_count,
  2226. memory->max_page_count, error_buf,
  2227. error_buf_size))
  2228. return false;
  2229. if (memory->max_page_count > max_page_count)
  2230. memory->max_page_count = max_page_count;
  2231. }
  2232. else {
  2233. /* Limit the maximum memory size to max_page_count */
  2234. memory->max_page_count = max_page_count;
  2235. }
  2236. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2237. *p_buf = p;
  2238. return true;
  2239. fail:
  2240. return false;
  2241. }
  2242. static bool
  2243. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2244. bool is_load_from_file_buf, char *error_buf,
  2245. uint32 error_buf_size)
  2246. {
  2247. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2248. uint32 import_count, name_len, type_index, i, u32, flags;
  2249. uint64 total_size;
  2250. WASMImport *import;
  2251. WASMImport *import_functions = NULL, *import_tables = NULL;
  2252. WASMImport *import_memories = NULL, *import_globals = NULL;
  2253. char *sub_module_name, *field_name;
  2254. uint8 u8, kind, global_type;
  2255. read_leb_uint32(p, p_end, import_count);
  2256. if (import_count) {
  2257. module->import_count = import_count;
  2258. total_size = sizeof(WASMImport) * (uint64)import_count;
  2259. if (!(module->imports =
  2260. loader_malloc(total_size, error_buf, error_buf_size))) {
  2261. return false;
  2262. }
  2263. p_old = p;
  2264. /* Scan firstly to get import count of each type */
  2265. for (i = 0; i < import_count; i++) {
  2266. /* module name */
  2267. read_leb_uint32(p, p_end, name_len);
  2268. CHECK_BUF(p, p_end, name_len);
  2269. p += name_len;
  2270. /* field name */
  2271. read_leb_uint32(p, p_end, name_len);
  2272. CHECK_BUF(p, p_end, name_len);
  2273. p += name_len;
  2274. CHECK_BUF(p, p_end, 1);
  2275. /* 0x00/0x01/0x02/0x03 */
  2276. kind = read_uint8(p);
  2277. switch (kind) {
  2278. case IMPORT_KIND_FUNC: /* import function */
  2279. read_leb_uint32(p, p_end, type_index);
  2280. module->import_function_count++;
  2281. break;
  2282. case IMPORT_KIND_TABLE: /* import table */
  2283. CHECK_BUF(p, p_end, 1);
  2284. /* 0x70 */
  2285. u8 = read_uint8(p);
  2286. read_leb_uint32(p, p_end, flags);
  2287. read_leb_uint32(p, p_end, u32);
  2288. if (flags & 1)
  2289. read_leb_uint32(p, p_end, u32);
  2290. module->import_table_count++;
  2291. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2292. if (module->import_table_count > 1) {
  2293. set_error_buf(error_buf, error_buf_size,
  2294. "multiple tables");
  2295. return false;
  2296. }
  2297. #endif
  2298. break;
  2299. case IMPORT_KIND_MEMORY: /* import memory */
  2300. read_leb_uint32(p, p_end, flags);
  2301. read_leb_uint32(p, p_end, u32);
  2302. if (flags & 1)
  2303. read_leb_uint32(p, p_end, u32);
  2304. module->import_memory_count++;
  2305. if (module->import_memory_count > 1) {
  2306. set_error_buf(error_buf, error_buf_size,
  2307. "multiple memories");
  2308. return false;
  2309. }
  2310. break;
  2311. case IMPORT_KIND_GLOBAL: /* import global */
  2312. #if WASM_ENABLE_GC != 0
  2313. /* valtype */
  2314. CHECK_BUF(p, p_end, 1);
  2315. global_type = read_uint8(p);
  2316. if (wasm_is_type_multi_byte_type(global_type)) {
  2317. int32 heap_type;
  2318. read_leb_int32(p, p_end, heap_type);
  2319. (void)heap_type;
  2320. }
  2321. /* mutability */
  2322. CHECK_BUF(p, p_end, 1);
  2323. p += 1;
  2324. #else
  2325. CHECK_BUF(p, p_end, 2);
  2326. p += 2;
  2327. #endif
  2328. (void)global_type;
  2329. module->import_global_count++;
  2330. break;
  2331. default:
  2332. set_error_buf(error_buf, error_buf_size,
  2333. "invalid import kind");
  2334. return false;
  2335. }
  2336. }
  2337. if (module->import_function_count)
  2338. import_functions = module->import_functions = module->imports;
  2339. if (module->import_table_count)
  2340. import_tables = module->import_tables =
  2341. module->imports + module->import_function_count;
  2342. if (module->import_memory_count)
  2343. import_memories = module->import_memories =
  2344. module->imports + module->import_function_count
  2345. + module->import_table_count;
  2346. if (module->import_global_count)
  2347. import_globals = module->import_globals =
  2348. module->imports + module->import_function_count
  2349. + module->import_table_count + module->import_memory_count;
  2350. p = p_old;
  2351. /* Scan again to resolve the data */
  2352. for (i = 0; i < import_count; i++) {
  2353. /* load module name */
  2354. read_leb_uint32(p, p_end, name_len);
  2355. CHECK_BUF(p, p_end, name_len);
  2356. if (!(sub_module_name = const_str_list_insert(
  2357. p, name_len, module, is_load_from_file_buf, error_buf,
  2358. error_buf_size))) {
  2359. return false;
  2360. }
  2361. p += name_len;
  2362. /* load field name */
  2363. read_leb_uint32(p, p_end, name_len);
  2364. CHECK_BUF(p, p_end, name_len);
  2365. if (!(field_name = const_str_list_insert(
  2366. p, name_len, module, is_load_from_file_buf, error_buf,
  2367. error_buf_size))) {
  2368. return false;
  2369. }
  2370. p += name_len;
  2371. CHECK_BUF(p, p_end, 1);
  2372. /* 0x00/0x01/0x02/0x03 */
  2373. kind = read_uint8(p);
  2374. switch (kind) {
  2375. case IMPORT_KIND_FUNC: /* import function */
  2376. bh_assert(import_functions);
  2377. import = import_functions++;
  2378. if (!load_function_import(
  2379. &p, p_end, module, sub_module_name, field_name,
  2380. &import->u.function, error_buf, error_buf_size)) {
  2381. return false;
  2382. }
  2383. break;
  2384. case IMPORT_KIND_TABLE: /* import table */
  2385. bh_assert(import_tables);
  2386. import = import_tables++;
  2387. if (!load_table_import(&p, p_end, module, sub_module_name,
  2388. field_name, &import->u.table,
  2389. error_buf, error_buf_size)) {
  2390. LOG_DEBUG("can not import such a table (%s,%s)",
  2391. sub_module_name, field_name);
  2392. return false;
  2393. }
  2394. break;
  2395. case IMPORT_KIND_MEMORY: /* import memory */
  2396. bh_assert(import_memories);
  2397. import = import_memories++;
  2398. if (!load_memory_import(&p, p_end, module, sub_module_name,
  2399. field_name, &import->u.memory,
  2400. error_buf, error_buf_size)) {
  2401. return false;
  2402. }
  2403. break;
  2404. case IMPORT_KIND_GLOBAL: /* import global */
  2405. bh_assert(import_globals);
  2406. import = import_globals++;
  2407. if (!load_global_import(&p, p_end, module, sub_module_name,
  2408. field_name, &import->u.global,
  2409. error_buf, error_buf_size)) {
  2410. return false;
  2411. }
  2412. break;
  2413. default:
  2414. set_error_buf(error_buf, error_buf_size,
  2415. "invalid import kind");
  2416. return false;
  2417. }
  2418. import->kind = kind;
  2419. import->u.names.module_name = sub_module_name;
  2420. import->u.names.field_name = field_name;
  2421. }
  2422. #if WASM_ENABLE_LIBC_WASI != 0
  2423. import = module->import_functions;
  2424. for (i = 0; i < module->import_function_count; i++, import++) {
  2425. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  2426. || !strcmp(import->u.names.module_name,
  2427. "wasi_snapshot_preview1")) {
  2428. module->import_wasi_api = true;
  2429. break;
  2430. }
  2431. }
  2432. #endif
  2433. }
  2434. if (p != p_end) {
  2435. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2436. return false;
  2437. }
  2438. LOG_VERBOSE("Load import section success.\n");
  2439. (void)u8;
  2440. (void)u32;
  2441. (void)type_index;
  2442. return true;
  2443. fail:
  2444. return false;
  2445. }
  2446. static bool
  2447. init_function_local_offsets(WASMFunction *func, char *error_buf,
  2448. uint32 error_buf_size)
  2449. {
  2450. WASMFuncType *param_type = func->func_type;
  2451. uint32 param_count = param_type->param_count;
  2452. uint8 *param_types = param_type->types;
  2453. uint32 local_count = func->local_count;
  2454. uint8 *local_types = func->local_types;
  2455. uint32 i, local_offset = 0;
  2456. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  2457. /*
  2458. * Only allocate memory when total_size is not 0,
  2459. * or the return value of malloc(0) might be NULL on some platforms,
  2460. * which causes wasm loader return false.
  2461. */
  2462. if (total_size > 0
  2463. && !(func->local_offsets =
  2464. loader_malloc(total_size, error_buf, error_buf_size))) {
  2465. return false;
  2466. }
  2467. for (i = 0; i < param_count; i++) {
  2468. func->local_offsets[i] = (uint16)local_offset;
  2469. local_offset += wasm_value_type_cell_num(param_types[i]);
  2470. }
  2471. for (i = 0; i < local_count; i++) {
  2472. func->local_offsets[param_count + i] = (uint16)local_offset;
  2473. local_offset += wasm_value_type_cell_num(local_types[i]);
  2474. }
  2475. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  2476. return true;
  2477. }
  2478. static bool
  2479. load_function_section(const uint8 *buf, const uint8 *buf_end,
  2480. const uint8 *buf_code, const uint8 *buf_code_end,
  2481. WASMModule *module, char *error_buf,
  2482. uint32 error_buf_size)
  2483. {
  2484. const uint8 *p = buf, *p_end = buf_end;
  2485. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  2486. uint32 func_count;
  2487. uint64 total_size;
  2488. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  2489. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  2490. uint8 type;
  2491. WASMFunction *func;
  2492. #if WASM_ENABLE_GC != 0
  2493. bool need_ref_type_map;
  2494. WASMRefType ref_type;
  2495. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  2496. #endif
  2497. read_leb_uint32(p, p_end, func_count);
  2498. if (buf_code)
  2499. read_leb_uint32(p_code, buf_code_end, code_count);
  2500. if (func_count != code_count) {
  2501. set_error_buf(error_buf, error_buf_size,
  2502. "function and code section have inconsistent lengths or "
  2503. "unexpected end");
  2504. return false;
  2505. }
  2506. if (func_count) {
  2507. module->function_count = func_count;
  2508. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  2509. if (!(module->functions =
  2510. loader_malloc(total_size, error_buf, error_buf_size))) {
  2511. return false;
  2512. }
  2513. for (i = 0; i < func_count; i++) {
  2514. /* Resolve function type */
  2515. read_leb_uint32(p, p_end, type_index);
  2516. if (type_index >= module->type_count) {
  2517. set_error_buf(error_buf, error_buf_size, "unknown type");
  2518. return false;
  2519. }
  2520. #if WASM_ENABLE_GC != 0
  2521. type_index_org = type_index;
  2522. #endif
  2523. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2524. type_index = wasm_get_smallest_type_idx(
  2525. module->types, module->type_count, type_index);
  2526. #endif
  2527. read_leb_uint32(p_code, buf_code_end, code_size);
  2528. if (code_size == 0 || p_code + code_size > buf_code_end) {
  2529. set_error_buf(error_buf, error_buf_size,
  2530. "invalid function code size");
  2531. return false;
  2532. }
  2533. /* Resolve local set count */
  2534. p_code_end = p_code + code_size;
  2535. local_count = 0;
  2536. read_leb_uint32(p_code, buf_code_end, local_set_count);
  2537. p_code_save = p_code;
  2538. #if WASM_ENABLE_GC != 0
  2539. ref_type_map_count = 0;
  2540. #endif
  2541. /* Calculate total local count */
  2542. for (j = 0; j < local_set_count; j++) {
  2543. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  2544. if (sub_local_count > UINT32_MAX - local_count) {
  2545. set_error_buf(error_buf, error_buf_size, "too many locals");
  2546. return false;
  2547. }
  2548. #if WASM_ENABLE_GC == 0
  2549. CHECK_BUF(p_code, buf_code_end, 1);
  2550. /* 0x7F/0x7E/0x7D/0x7C */
  2551. type = read_uint8(p_code);
  2552. local_count += sub_local_count;
  2553. #else
  2554. if (!resolve_value_type(&p_code, buf_code_end, module,
  2555. &need_ref_type_map, &ref_type, false,
  2556. error_buf, error_buf_size)) {
  2557. return false;
  2558. }
  2559. local_count += sub_local_count;
  2560. if (need_ref_type_map)
  2561. ref_type_map_count += sub_local_count;
  2562. #endif
  2563. }
  2564. /* Alloc memory, layout: function structure + local types */
  2565. code_size = (uint32)(p_code_end - p_code);
  2566. total_size = sizeof(WASMFunction) + (uint64)local_count;
  2567. if (!(func = module->functions[i] =
  2568. loader_malloc(total_size, error_buf, error_buf_size))) {
  2569. return false;
  2570. }
  2571. #if WASM_ENABLE_GC != 0
  2572. if (ref_type_map_count > 0) {
  2573. total_size =
  2574. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  2575. if (!(func->local_ref_type_maps = loader_malloc(
  2576. total_size, error_buf, error_buf_size))) {
  2577. return false;
  2578. }
  2579. func->local_ref_type_map_count = ref_type_map_count;
  2580. }
  2581. #endif
  2582. /* Set function type, local count, code size and code body */
  2583. func->func_type = (WASMFuncType *)module->types[type_index];
  2584. func->local_count = local_count;
  2585. if (local_count > 0)
  2586. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  2587. func->code_size = code_size;
  2588. /*
  2589. * we shall make a copy of code body [p_code, p_code + code_size]
  2590. * when we are worrying about inappropriate releasing behaviour.
  2591. * all code bodies are actually in a buffer which user allocates in
  2592. * his embedding environment and we don't have power on them.
  2593. * it will be like:
  2594. * code_body_cp = malloc(code_size);
  2595. * memcpy(code_body_cp, p_code, code_size);
  2596. * func->code = code_body_cp;
  2597. */
  2598. func->code = (uint8 *)p_code;
  2599. #if WASM_ENABLE_GC != 0
  2600. func->type_idx = type_index_org;
  2601. #endif
  2602. #if WASM_ENABLE_GC != 0
  2603. t = 0;
  2604. #endif
  2605. /* Load each local type */
  2606. p_code = p_code_save;
  2607. local_type_index = 0;
  2608. for (j = 0; j < local_set_count; j++) {
  2609. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  2610. /* Note: sub_local_count is allowed to be 0 */
  2611. if (local_type_index > UINT32_MAX - sub_local_count
  2612. || local_type_index + sub_local_count > local_count) {
  2613. set_error_buf(error_buf, error_buf_size,
  2614. "invalid local count");
  2615. return false;
  2616. }
  2617. #if WASM_ENABLE_GC == 0
  2618. CHECK_BUF(p_code, buf_code_end, 1);
  2619. /* 0x7F/0x7E/0x7D/0x7C */
  2620. type = read_uint8(p_code);
  2621. if (!is_value_type(type)) {
  2622. if (type == VALUE_TYPE_V128)
  2623. set_error_buf(error_buf, error_buf_size,
  2624. "v128 value type requires simd feature");
  2625. else if (type == VALUE_TYPE_FUNCREF
  2626. || type == VALUE_TYPE_EXTERNREF)
  2627. set_error_buf(error_buf, error_buf_size,
  2628. "ref value type requires "
  2629. "reference types feature");
  2630. else
  2631. set_error_buf_v(error_buf, error_buf_size,
  2632. "invalid local type 0x%02X", type);
  2633. return false;
  2634. }
  2635. #else
  2636. if (!resolve_value_type(&p_code, buf_code_end, module,
  2637. &need_ref_type_map, &ref_type, false,
  2638. error_buf, error_buf_size)) {
  2639. return false;
  2640. }
  2641. if (need_ref_type_map) {
  2642. WASMRefType *ref_type_tmp;
  2643. if (!(ref_type_tmp = reftype_set_insert(
  2644. module->ref_type_set, &ref_type, error_buf,
  2645. error_buf_size))) {
  2646. return false;
  2647. }
  2648. for (k = 0; k < sub_local_count; k++) {
  2649. func->local_ref_type_maps[t + k].ref_type =
  2650. ref_type_tmp;
  2651. func->local_ref_type_maps[t + k].index =
  2652. local_type_index + k;
  2653. }
  2654. t += sub_local_count;
  2655. }
  2656. type = ref_type.ref_type;
  2657. #endif
  2658. for (k = 0; k < sub_local_count; k++) {
  2659. func->local_types[local_type_index++] = type;
  2660. }
  2661. }
  2662. bh_assert(local_type_index == func->local_count);
  2663. #if WASM_ENABLE_GC != 0
  2664. bh_assert(t == func->local_ref_type_map_count);
  2665. #if TRACE_WASM_LOADER != 0
  2666. os_printf("func %u, local types: [", i);
  2667. k = 0;
  2668. for (j = 0; j < func->local_count; j++) {
  2669. WASMRefType *ref_type_tmp = NULL;
  2670. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  2671. bh_assert(j == func->local_ref_type_maps[k].index);
  2672. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  2673. }
  2674. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  2675. if (j < func->local_count - 1)
  2676. os_printf(" ");
  2677. }
  2678. os_printf("]\n");
  2679. #endif
  2680. #endif
  2681. func->param_cell_num = func->func_type->param_cell_num;
  2682. func->ret_cell_num = func->func_type->ret_cell_num;
  2683. local_cell_num =
  2684. wasm_get_cell_num(func->local_types, func->local_count);
  2685. if (local_cell_num > UINT16_MAX) {
  2686. set_error_buf(error_buf, error_buf_size,
  2687. "local count too large");
  2688. return false;
  2689. }
  2690. func->local_cell_num = (uint16)local_cell_num;
  2691. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  2692. return false;
  2693. p_code = p_code_end;
  2694. }
  2695. }
  2696. if (p != p_end) {
  2697. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2698. return false;
  2699. }
  2700. LOG_VERBOSE("Load function section success.\n");
  2701. return true;
  2702. fail:
  2703. return false;
  2704. }
  2705. static bool
  2706. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2707. char *error_buf, uint32 error_buf_size)
  2708. {
  2709. const uint8 *p = buf, *p_end = buf_end;
  2710. uint32 table_count, i;
  2711. uint64 total_size;
  2712. WASMTable *table;
  2713. read_leb_uint32(p, p_end, table_count);
  2714. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2715. if (module->import_table_count + table_count > 1) {
  2716. /* a total of one table is allowed */
  2717. set_error_buf(error_buf, error_buf_size, "multiple tables");
  2718. return false;
  2719. }
  2720. #endif
  2721. if (table_count) {
  2722. module->table_count = table_count;
  2723. total_size = sizeof(WASMTable) * (uint64)table_count;
  2724. if (!(module->tables =
  2725. loader_malloc(total_size, error_buf, error_buf_size))) {
  2726. return false;
  2727. }
  2728. /* load each table */
  2729. table = module->tables;
  2730. for (i = 0; i < table_count; i++, table++)
  2731. if (!load_table(&p, p_end, module, table, error_buf,
  2732. error_buf_size))
  2733. return false;
  2734. }
  2735. if (p != p_end) {
  2736. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2737. return false;
  2738. }
  2739. LOG_VERBOSE("Load table section success.\n");
  2740. return true;
  2741. fail:
  2742. return false;
  2743. }
  2744. static bool
  2745. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2746. char *error_buf, uint32 error_buf_size)
  2747. {
  2748. const uint8 *p = buf, *p_end = buf_end;
  2749. uint32 memory_count, i;
  2750. uint64 total_size;
  2751. WASMMemory *memory;
  2752. read_leb_uint32(p, p_end, memory_count);
  2753. /* a total of one memory is allowed */
  2754. if (module->import_memory_count + memory_count > 1) {
  2755. set_error_buf(error_buf, error_buf_size, "multiple memories");
  2756. return false;
  2757. }
  2758. if (memory_count) {
  2759. module->memory_count = memory_count;
  2760. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  2761. if (!(module->memories =
  2762. loader_malloc(total_size, error_buf, error_buf_size))) {
  2763. return false;
  2764. }
  2765. /* load each memory */
  2766. memory = module->memories;
  2767. for (i = 0; i < memory_count; i++, memory++)
  2768. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  2769. return false;
  2770. }
  2771. if (p != p_end) {
  2772. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2773. return false;
  2774. }
  2775. LOG_VERBOSE("Load memory section success.\n");
  2776. return true;
  2777. fail:
  2778. return false;
  2779. }
  2780. static bool
  2781. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2782. char *error_buf, uint32 error_buf_size)
  2783. {
  2784. const uint8 *p = buf, *p_end = buf_end;
  2785. uint32 global_count, i;
  2786. uint64 total_size;
  2787. WASMGlobal *global;
  2788. uint8 mutable;
  2789. #if WASM_ENABLE_GC != 0
  2790. bool need_ref_type_map;
  2791. WASMRefType ref_type;
  2792. #endif
  2793. read_leb_uint32(p, p_end, global_count);
  2794. if (global_count) {
  2795. module->global_count = global_count;
  2796. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  2797. if (!(module->globals =
  2798. loader_malloc(total_size, error_buf, error_buf_size))) {
  2799. return false;
  2800. }
  2801. global = module->globals;
  2802. for (i = 0; i < global_count; i++, global++) {
  2803. #if WASM_ENABLE_GC == 0
  2804. CHECK_BUF(p, p_end, 2);
  2805. global->type = read_uint8(p);
  2806. mutable = read_uint8(p);
  2807. #else
  2808. if (!resolve_value_type(&p, p_end, module, &need_ref_type_map,
  2809. &ref_type, false, error_buf,
  2810. error_buf_size)) {
  2811. return false;
  2812. }
  2813. global->type = ref_type.ref_type;
  2814. CHECK_BUF(p, p_end, 1);
  2815. mutable = read_uint8(p);
  2816. #endif /* end of WASM_ENABLE_GC */
  2817. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  2818. return false;
  2819. }
  2820. global->is_mutable = mutable ? true : false;
  2821. /* initialize expression */
  2822. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  2823. global->type,
  2824. #if WASM_ENABLE_GC == 0
  2825. NULL,
  2826. #else
  2827. &ref_type,
  2828. #endif
  2829. error_buf, error_buf_size))
  2830. return false;
  2831. #if WASM_ENABLE_GC != 0
  2832. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  2833. uint8 global_type;
  2834. WASMRefType *global_ref_type;
  2835. uint32 global_idx = global->init_expr.u.global_index;
  2836. if (global->init_expr.u.global_index
  2837. >= module->import_global_count + i) {
  2838. set_error_buf(error_buf, error_buf_size, "unknown global");
  2839. return false;
  2840. }
  2841. if (global_idx < module->import_global_count) {
  2842. global_type =
  2843. module->import_globals[global_idx].u.global.type;
  2844. global_ref_type =
  2845. module->import_globals[global_idx].u.global.ref_type;
  2846. }
  2847. else {
  2848. global_type = module->globals[global_idx].type;
  2849. global_ref_type = module->globals[global_idx].ref_type;
  2850. }
  2851. if (!wasm_reftype_is_subtype_of(
  2852. global_type, global_ref_type, global->type,
  2853. global->ref_type, module->types, module->type_count)) {
  2854. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2855. return false;
  2856. }
  2857. }
  2858. if (need_ref_type_map) {
  2859. if (!(global->ref_type =
  2860. reftype_set_insert(module->ref_type_set, &ref_type,
  2861. error_buf, error_buf_size))) {
  2862. return false;
  2863. }
  2864. }
  2865. #if TRACE_WASM_LOADER != 0
  2866. os_printf("global type: ");
  2867. wasm_dump_value_type(global->type, global->ref_type);
  2868. os_printf("\n");
  2869. #endif
  2870. #endif
  2871. }
  2872. }
  2873. if (p != p_end) {
  2874. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2875. return false;
  2876. }
  2877. LOG_VERBOSE("Load global section success.\n");
  2878. return true;
  2879. fail:
  2880. return false;
  2881. }
  2882. static bool
  2883. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2884. bool is_load_from_file_buf, char *error_buf,
  2885. uint32 error_buf_size)
  2886. {
  2887. const uint8 *p = buf, *p_end = buf_end;
  2888. uint32 export_count, i, j, index;
  2889. uint64 total_size;
  2890. uint32 str_len;
  2891. WASMExport *export;
  2892. const char *name;
  2893. read_leb_uint32(p, p_end, export_count);
  2894. if (export_count) {
  2895. module->export_count = export_count;
  2896. total_size = sizeof(WASMExport) * (uint64)export_count;
  2897. if (!(module->exports =
  2898. loader_malloc(total_size, error_buf, error_buf_size))) {
  2899. return false;
  2900. }
  2901. export = module->exports;
  2902. for (i = 0; i < export_count; i++, export ++) {
  2903. #if WASM_ENABLE_THREAD_MGR == 0
  2904. if (p == p_end) {
  2905. /* export section with inconsistent count:
  2906. n export declared, but less than n given */
  2907. set_error_buf(error_buf, error_buf_size,
  2908. "length out of bounds");
  2909. return false;
  2910. }
  2911. #endif
  2912. read_leb_uint32(p, p_end, str_len);
  2913. CHECK_BUF(p, p_end, str_len);
  2914. for (j = 0; j < i; j++) {
  2915. name = module->exports[j].name;
  2916. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  2917. set_error_buf(error_buf, error_buf_size,
  2918. "duplicate export name");
  2919. return false;
  2920. }
  2921. }
  2922. if (!(export->name = const_str_list_insert(
  2923. p, str_len, module, is_load_from_file_buf, error_buf,
  2924. error_buf_size))) {
  2925. return false;
  2926. }
  2927. p += str_len;
  2928. CHECK_BUF(p, p_end, 1);
  2929. export->kind = read_uint8(p);
  2930. read_leb_uint32(p, p_end, index);
  2931. export->index = index;
  2932. switch (export->kind) {
  2933. /* function index */
  2934. case EXPORT_KIND_FUNC:
  2935. if (index >= module->function_count
  2936. + module->import_function_count) {
  2937. set_error_buf(error_buf, error_buf_size,
  2938. "unknown function");
  2939. return false;
  2940. }
  2941. #if WASM_ENABLE_SIMD != 0
  2942. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2943. /* TODO: check func type, if it has v128 param or result,
  2944. report error */
  2945. #endif
  2946. #endif
  2947. break;
  2948. /* table index */
  2949. case EXPORT_KIND_TABLE:
  2950. if (index
  2951. >= module->table_count + module->import_table_count) {
  2952. set_error_buf(error_buf, error_buf_size,
  2953. "unknown table");
  2954. return false;
  2955. }
  2956. break;
  2957. /* memory index */
  2958. case EXPORT_KIND_MEMORY:
  2959. if (index
  2960. >= module->memory_count + module->import_memory_count) {
  2961. set_error_buf(error_buf, error_buf_size,
  2962. "unknown memory");
  2963. return false;
  2964. }
  2965. break;
  2966. /* global index */
  2967. case EXPORT_KIND_GLOBAL:
  2968. if (index
  2969. >= module->global_count + module->import_global_count) {
  2970. set_error_buf(error_buf, error_buf_size,
  2971. "unknown global");
  2972. return false;
  2973. }
  2974. break;
  2975. default:
  2976. set_error_buf(error_buf, error_buf_size,
  2977. "invalid export kind");
  2978. return false;
  2979. }
  2980. }
  2981. }
  2982. if (p != p_end) {
  2983. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2984. return false;
  2985. }
  2986. LOG_VERBOSE("Load export section success.\n");
  2987. return true;
  2988. fail:
  2989. return false;
  2990. }
  2991. static bool
  2992. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  2993. uint32 error_buf_size)
  2994. {
  2995. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2996. if (table_index != 0) {
  2997. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  2998. return false;
  2999. }
  3000. #endif
  3001. if (table_index >= module->import_table_count + module->table_count) {
  3002. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3003. table_index);
  3004. return false;
  3005. }
  3006. return true;
  3007. }
  3008. static bool
  3009. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3010. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3011. {
  3012. const uint8 *p = *p_buf, *p_end = buf_end;
  3013. uint32 table_index;
  3014. read_leb_uint32(p, p_end, table_index);
  3015. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3016. return false;
  3017. }
  3018. *p_table_index = table_index;
  3019. *p_buf = p;
  3020. return true;
  3021. fail:
  3022. return false;
  3023. }
  3024. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3025. static bool
  3026. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3027. uint32 *p_elem_type,
  3028. #if WASM_ENABLE_GC != 0
  3029. WASMRefType **p_elem_ref_type,
  3030. #endif
  3031. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3032. {
  3033. const uint8 *p = *p_buf, *p_end = buf_end;
  3034. uint8 elem_type;
  3035. #if WASM_ENABLE_GC != 0
  3036. WASMRefType elem_ref_type;
  3037. bool need_ref_type_map;
  3038. #endif
  3039. CHECK_BUF(p, p_end, 1);
  3040. elem_type = read_uint8(p);
  3041. if (elemkind_zero) {
  3042. if (elem_type != 0) {
  3043. set_error_buf(error_buf, error_buf_size,
  3044. "invalid reference type or unknown type");
  3045. return false;
  3046. }
  3047. else {
  3048. *p_elem_type = VALUE_TYPE_FUNCREF;
  3049. *p_buf = p;
  3050. return true;
  3051. }
  3052. }
  3053. #if WASM_ENABLE_GC == 0
  3054. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3055. set_error_buf(error_buf, error_buf_size,
  3056. "invalid reference type or unknown type");
  3057. return false;
  3058. }
  3059. *p_elem_type = elem_type;
  3060. #else
  3061. p--;
  3062. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3063. &need_ref_type_map, &elem_ref_type, false,
  3064. error_buf, error_buf_size)) {
  3065. return false;
  3066. }
  3067. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3068. set_error_buf(error_buf, error_buf_size,
  3069. "invalid reference type or unknown type");
  3070. return false;
  3071. }
  3072. *p_elem_type = elem_ref_type.ref_type;
  3073. if (need_ref_type_map) {
  3074. if (!(*p_elem_ref_type =
  3075. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3076. error_buf, error_buf_size))) {
  3077. return false;
  3078. }
  3079. }
  3080. #endif
  3081. *p_buf = p;
  3082. return true;
  3083. fail:
  3084. return false;
  3085. }
  3086. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3087. static bool
  3088. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3089. WASMModule *module, WASMTableSeg *table_segment,
  3090. bool use_init_expr, char *error_buf, uint32 error_buf_size)
  3091. {
  3092. const uint8 *p = *p_buf, *p_end = buf_end;
  3093. uint32 function_count, function_index = 0, i;
  3094. uint64 total_size;
  3095. read_leb_uint32(p, p_end, function_count);
  3096. table_segment->function_count = function_count;
  3097. total_size = sizeof(uint32) * (uint64)function_count;
  3098. if (total_size > 0
  3099. && !(table_segment->func_indexes = (uint32 *)loader_malloc(
  3100. total_size, error_buf, error_buf_size))) {
  3101. return false;
  3102. }
  3103. for (i = 0; i < function_count; i++) {
  3104. InitializerExpression init_expr = { 0 };
  3105. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3106. if (!use_init_expr) {
  3107. read_leb_uint32(p, p_end, function_index);
  3108. }
  3109. else {
  3110. if (!load_init_expr(module, &p, p_end, &init_expr,
  3111. table_segment->elem_type,
  3112. #if WASM_ENABLE_GC == 0
  3113. NULL,
  3114. #else
  3115. table_segment->elem_ref_type,
  3116. #endif
  3117. error_buf, error_buf_size))
  3118. return false;
  3119. function_index = init_expr.u.ref_index;
  3120. }
  3121. #else
  3122. read_leb_uint32(p, p_end, function_index);
  3123. #endif
  3124. /* since we are using -1 to indicate ref.null */
  3125. if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
  3126. && !check_function_index(module, function_index, error_buf,
  3127. error_buf_size)) {
  3128. return false;
  3129. }
  3130. table_segment->func_indexes[i] = function_index;
  3131. }
  3132. *p_buf = p;
  3133. return true;
  3134. fail:
  3135. return false;
  3136. }
  3137. static bool
  3138. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3139. WASMModule *module, char *error_buf,
  3140. uint32 error_buf_size)
  3141. {
  3142. const uint8 *p = buf, *p_end = buf_end;
  3143. uint32 table_segment_count, i;
  3144. uint64 total_size;
  3145. WASMTableSeg *table_segment;
  3146. read_leb_uint32(p, p_end, table_segment_count);
  3147. if (table_segment_count) {
  3148. module->table_seg_count = table_segment_count;
  3149. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3150. if (!(module->table_segments =
  3151. loader_malloc(total_size, error_buf, error_buf_size))) {
  3152. return false;
  3153. }
  3154. table_segment = module->table_segments;
  3155. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3156. if (p >= p_end) {
  3157. set_error_buf(error_buf, error_buf_size,
  3158. "invalid value type or "
  3159. "invalid elements segment kind");
  3160. return false;
  3161. }
  3162. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3163. read_leb_uint32(p, p_end, table_segment->mode);
  3164. /* last three bits */
  3165. table_segment->mode = table_segment->mode & 0x07;
  3166. switch (table_segment->mode) {
  3167. /* elemkind/elemtype + active */
  3168. case 0:
  3169. case 4:
  3170. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  3171. table_segment->table_index = 0;
  3172. if (!check_table_index(module, table_segment->table_index,
  3173. error_buf, error_buf_size))
  3174. return false;
  3175. if (!load_init_expr(
  3176. module, &p, p_end, &table_segment->base_offset,
  3177. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3178. return false;
  3179. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3180. table_segment->mode == 0 ? false
  3181. : true,
  3182. error_buf, error_buf_size))
  3183. return false;
  3184. break;
  3185. /* elemkind + passive/declarative */
  3186. case 1:
  3187. case 3:
  3188. if (!load_elem_type(module, &p, p_end,
  3189. &table_segment->elem_type,
  3190. #if WASM_ENABLE_GC != 0
  3191. &table_segment->elem_ref_type,
  3192. #endif
  3193. true, error_buf, error_buf_size))
  3194. return false;
  3195. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3196. false, error_buf, error_buf_size))
  3197. return false;
  3198. break;
  3199. /* elemkind/elemtype + table_idx + active */
  3200. case 2:
  3201. case 6:
  3202. if (!load_table_index(&p, p_end, module,
  3203. &table_segment->table_index,
  3204. error_buf, error_buf_size))
  3205. return false;
  3206. if (!load_init_expr(
  3207. module, &p, p_end, &table_segment->base_offset,
  3208. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  3209. return false;
  3210. if (!load_elem_type(module, &p, p_end,
  3211. &table_segment->elem_type,
  3212. #if WASM_ENABLE_GC != 0
  3213. &table_segment->elem_ref_type,
  3214. #endif
  3215. table_segment->mode == 2 ? true : false,
  3216. error_buf, error_buf_size))
  3217. return false;
  3218. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3219. table_segment->mode == 2 ? false
  3220. : true,
  3221. error_buf, error_buf_size))
  3222. return false;
  3223. break;
  3224. case 5:
  3225. case 7:
  3226. if (!load_elem_type(module, &p, p_end,
  3227. &table_segment->elem_type,
  3228. #if WASM_ENABLE_GC != 0
  3229. &table_segment->elem_ref_type,
  3230. #endif
  3231. false, error_buf, error_buf_size))
  3232. return false;
  3233. if (!load_func_index_vec(&p, p_end, module, table_segment,
  3234. true, error_buf, error_buf_size))
  3235. return false;
  3236. break;
  3237. default:
  3238. set_error_buf(error_buf, error_buf_size,
  3239. "unknown element segment kind");
  3240. return false;
  3241. }
  3242. #else
  3243. /*
  3244. * like: 00 41 05 0b 04 00 01 00 01
  3245. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  3246. */
  3247. if (!load_table_index(&p, p_end, module,
  3248. &table_segment->table_index, error_buf,
  3249. error_buf_size))
  3250. return false;
  3251. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  3252. VALUE_TYPE_I32, NULL, error_buf,
  3253. error_buf_size))
  3254. return false;
  3255. if (!load_func_index_vec(&p, p_end, module, table_segment, false,
  3256. error_buf, error_buf_size))
  3257. return false;
  3258. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  3259. }
  3260. }
  3261. if (p != p_end) {
  3262. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3263. return false;
  3264. }
  3265. LOG_VERBOSE("Load table segment section success.\n");
  3266. return true;
  3267. fail:
  3268. return false;
  3269. }
  3270. static bool
  3271. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  3272. WASMModule *module, char *error_buf,
  3273. uint32 error_buf_size)
  3274. {
  3275. const uint8 *p = buf, *p_end = buf_end;
  3276. uint32 data_seg_count, i, mem_index, data_seg_len;
  3277. uint64 total_size;
  3278. WASMDataSeg *dataseg;
  3279. InitializerExpression init_expr;
  3280. #if WASM_ENABLE_BULK_MEMORY != 0
  3281. bool is_passive = false;
  3282. uint32 mem_flag;
  3283. #endif
  3284. read_leb_uint32(p, p_end, data_seg_count);
  3285. #if WASM_ENABLE_BULK_MEMORY != 0
  3286. if ((module->data_seg_count1 != 0)
  3287. && (data_seg_count != module->data_seg_count1)) {
  3288. set_error_buf(error_buf, error_buf_size,
  3289. "data count and data section have inconsistent lengths");
  3290. return false;
  3291. }
  3292. #endif
  3293. if (data_seg_count) {
  3294. module->data_seg_count = data_seg_count;
  3295. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  3296. if (!(module->data_segments =
  3297. loader_malloc(total_size, error_buf, error_buf_size))) {
  3298. return false;
  3299. }
  3300. for (i = 0; i < data_seg_count; i++) {
  3301. read_leb_uint32(p, p_end, mem_index);
  3302. #if WASM_ENABLE_BULK_MEMORY != 0
  3303. is_passive = false;
  3304. mem_flag = mem_index & 0x03;
  3305. switch (mem_flag) {
  3306. case 0x01:
  3307. is_passive = true;
  3308. break;
  3309. case 0x00:
  3310. /* no memory index, treat index as 0 */
  3311. mem_index = 0;
  3312. goto check_mem_index;
  3313. case 0x02:
  3314. /* read following memory index */
  3315. read_leb_uint32(p, p_end, mem_index);
  3316. check_mem_index:
  3317. if (mem_index
  3318. >= module->import_memory_count + module->memory_count) {
  3319. set_error_buf_v(error_buf, error_buf_size,
  3320. "unknown memory %d", mem_index);
  3321. return false;
  3322. }
  3323. break;
  3324. case 0x03:
  3325. default:
  3326. set_error_buf(error_buf, error_buf_size, "unknown memory");
  3327. return false;
  3328. break;
  3329. }
  3330. #else
  3331. if (mem_index
  3332. >= module->import_memory_count + module->memory_count) {
  3333. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  3334. mem_index);
  3335. return false;
  3336. }
  3337. #endif /* WASM_ENABLE_BULK_MEMORY */
  3338. #if WASM_ENABLE_BULK_MEMORY != 0
  3339. if (!is_passive)
  3340. #endif
  3341. if (!load_init_expr(module, &p, p_end, &init_expr,
  3342. VALUE_TYPE_I32, NULL, error_buf,
  3343. error_buf_size))
  3344. return false;
  3345. read_leb_uint32(p, p_end, data_seg_len);
  3346. if (!(dataseg = module->data_segments[i] = loader_malloc(
  3347. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  3348. return false;
  3349. }
  3350. #if WASM_ENABLE_BULK_MEMORY != 0
  3351. dataseg->is_passive = is_passive;
  3352. if (!is_passive)
  3353. #endif
  3354. {
  3355. bh_memcpy_s(&dataseg->base_offset,
  3356. sizeof(InitializerExpression), &init_expr,
  3357. sizeof(InitializerExpression));
  3358. dataseg->memory_index = mem_index;
  3359. }
  3360. dataseg->data_length = data_seg_len;
  3361. CHECK_BUF(p, p_end, data_seg_len);
  3362. dataseg->data = (uint8 *)p;
  3363. p += data_seg_len;
  3364. }
  3365. }
  3366. if (p != p_end) {
  3367. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3368. return false;
  3369. }
  3370. LOG_VERBOSE("Load data segment section success.\n");
  3371. return true;
  3372. fail:
  3373. return false;
  3374. }
  3375. #if WASM_ENABLE_BULK_MEMORY != 0
  3376. static bool
  3377. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  3378. WASMModule *module, char *error_buf,
  3379. uint32 error_buf_size)
  3380. {
  3381. const uint8 *p = buf, *p_end = buf_end;
  3382. uint32 data_seg_count1 = 0;
  3383. read_leb_uint32(p, p_end, data_seg_count1);
  3384. module->data_seg_count1 = data_seg_count1;
  3385. if (p != p_end) {
  3386. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3387. return false;
  3388. }
  3389. LOG_VERBOSE("Load datacount section success.\n");
  3390. return true;
  3391. fail:
  3392. return false;
  3393. }
  3394. #endif
  3395. static bool
  3396. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  3397. const uint8 *buf_func_end, WASMModule *module,
  3398. char *error_buf, uint32 error_buf_size)
  3399. {
  3400. const uint8 *p = buf, *p_end = buf_end;
  3401. const uint8 *p_func = buf_func;
  3402. uint32 func_count = 0, code_count;
  3403. /* code has been loaded in function section, so pass it here, just check
  3404. * whether function and code section have inconsistent lengths */
  3405. read_leb_uint32(p, p_end, code_count);
  3406. if (buf_func)
  3407. read_leb_uint32(p_func, buf_func_end, func_count);
  3408. if (func_count != code_count) {
  3409. set_error_buf(error_buf, error_buf_size,
  3410. "function and code section have inconsistent lengths");
  3411. return false;
  3412. }
  3413. LOG_VERBOSE("Load code segment section success.\n");
  3414. (void)module;
  3415. return true;
  3416. fail:
  3417. return false;
  3418. }
  3419. static bool
  3420. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3421. char *error_buf, uint32 error_buf_size)
  3422. {
  3423. const uint8 *p = buf, *p_end = buf_end;
  3424. WASMFuncType *type;
  3425. uint32 start_function;
  3426. read_leb_uint32(p, p_end, start_function);
  3427. if (start_function
  3428. >= module->function_count + module->import_function_count) {
  3429. set_error_buf(error_buf, error_buf_size, "unknown function");
  3430. return false;
  3431. }
  3432. if (start_function < module->import_function_count)
  3433. type = module->import_functions[start_function].u.function.func_type;
  3434. else
  3435. type = module->functions[start_function - module->import_function_count]
  3436. ->func_type;
  3437. if (type->param_count != 0 || type->result_count != 0) {
  3438. set_error_buf(error_buf, error_buf_size, "invalid start function");
  3439. return false;
  3440. }
  3441. module->start_function = start_function;
  3442. if (p != p_end) {
  3443. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3444. return false;
  3445. }
  3446. LOG_VERBOSE("Load start section success.\n");
  3447. return true;
  3448. fail:
  3449. return false;
  3450. }
  3451. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3452. static bool
  3453. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3454. bool is_load_from_file_buf, char *error_buf,
  3455. uint32 error_buf_size)
  3456. {
  3457. const uint8 *p = buf, *p_end = buf_end;
  3458. uint32 name_type, subsection_size;
  3459. uint32 previous_name_type = 0;
  3460. uint32 num_func_name;
  3461. uint32 func_index;
  3462. uint32 previous_func_index = ~0U;
  3463. uint32 func_name_len;
  3464. uint32 name_index;
  3465. int i = 0;
  3466. if (p >= p_end) {
  3467. set_error_buf(error_buf, error_buf_size, "unexpected end");
  3468. return false;
  3469. }
  3470. while (p < p_end) {
  3471. read_leb_uint32(p, p_end, name_type);
  3472. if (i != 0) {
  3473. if (name_type == previous_name_type) {
  3474. set_error_buf(error_buf, error_buf_size,
  3475. "duplicate sub-section");
  3476. return false;
  3477. }
  3478. if (name_type < previous_name_type) {
  3479. set_error_buf(error_buf, error_buf_size,
  3480. "out-of-order sub-section");
  3481. return false;
  3482. }
  3483. }
  3484. previous_name_type = name_type;
  3485. read_leb_uint32(p, p_end, subsection_size);
  3486. CHECK_BUF(p, p_end, subsection_size);
  3487. switch (name_type) {
  3488. case SUB_SECTION_TYPE_FUNC:
  3489. if (subsection_size) {
  3490. read_leb_uint32(p, p_end, num_func_name);
  3491. for (name_index = 0; name_index < num_func_name;
  3492. name_index++) {
  3493. read_leb_uint32(p, p_end, func_index);
  3494. if (func_index == previous_func_index) {
  3495. set_error_buf(error_buf, error_buf_size,
  3496. "duplicate function name");
  3497. return false;
  3498. }
  3499. if (func_index < previous_func_index
  3500. && previous_func_index != ~0U) {
  3501. set_error_buf(error_buf, error_buf_size,
  3502. "out-of-order function index ");
  3503. return false;
  3504. }
  3505. previous_func_index = func_index;
  3506. read_leb_uint32(p, p_end, func_name_len);
  3507. CHECK_BUF(p, p_end, func_name_len);
  3508. /* Skip the import functions */
  3509. if (func_index >= module->import_function_count) {
  3510. func_index -= module->import_function_count;
  3511. if (func_index >= module->function_count) {
  3512. set_error_buf(error_buf, error_buf_size,
  3513. "out-of-range function index");
  3514. return false;
  3515. }
  3516. if (!(module->functions[func_index]->field_name =
  3517. const_str_list_insert(
  3518. p, func_name_len, module,
  3519. is_load_from_file_buf, error_buf,
  3520. error_buf_size))) {
  3521. return false;
  3522. }
  3523. }
  3524. p += func_name_len;
  3525. }
  3526. }
  3527. break;
  3528. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  3529. */
  3530. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  3531. default:
  3532. p = p + subsection_size;
  3533. break;
  3534. }
  3535. i++;
  3536. }
  3537. return true;
  3538. fail:
  3539. return false;
  3540. }
  3541. #endif
  3542. static bool
  3543. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3544. bool is_load_from_file_buf, char *error_buf,
  3545. uint32 error_buf_size)
  3546. {
  3547. const uint8 *p = buf, *p_end = buf_end;
  3548. char section_name[32];
  3549. uint32 name_len, buffer_len;
  3550. if (p >= p_end) {
  3551. set_error_buf(error_buf, error_buf_size, "unexpected end");
  3552. return false;
  3553. }
  3554. read_leb_uint32(p, p_end, name_len);
  3555. if (name_len == 0 || p + name_len > p_end) {
  3556. set_error_buf(error_buf, error_buf_size, "unexpected end");
  3557. return false;
  3558. }
  3559. if (!check_utf8_str(p, name_len)) {
  3560. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  3561. return false;
  3562. }
  3563. buffer_len = sizeof(section_name);
  3564. memset(section_name, 0, buffer_len);
  3565. if (name_len < buffer_len) {
  3566. bh_memcpy_s(section_name, buffer_len, p, name_len);
  3567. }
  3568. else {
  3569. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  3570. memset(section_name + buffer_len - 4, '.', 3);
  3571. }
  3572. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  3573. if (memcmp(p, "name", 4) == 0) {
  3574. module->name_section_buf = buf;
  3575. module->name_section_buf_end = buf_end;
  3576. p += name_len;
  3577. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  3578. error_buf_size);
  3579. LOG_VERBOSE("Load custom name section success.");
  3580. return true;
  3581. }
  3582. #endif
  3583. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3584. {
  3585. WASMCustomSection *section =
  3586. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  3587. if (!section) {
  3588. return false;
  3589. }
  3590. section->name_addr = (char *)p;
  3591. section->name_len = name_len;
  3592. section->content_addr = (uint8 *)(p + name_len);
  3593. section->content_len = (uint32)(p_end - p - name_len);
  3594. section->next = module->custom_section_list;
  3595. module->custom_section_list = section;
  3596. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  3597. return true;
  3598. }
  3599. #endif
  3600. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  3601. (void)is_load_from_file_buf;
  3602. (void)module;
  3603. return true;
  3604. fail:
  3605. return false;
  3606. }
  3607. static void
  3608. calculate_global_data_offset(WASMModule *module)
  3609. {
  3610. uint32 i, data_offset;
  3611. data_offset = 0;
  3612. for (i = 0; i < module->import_global_count; i++) {
  3613. WASMGlobalImport *import_global =
  3614. &((module->import_globals + i)->u.global);
  3615. #if WASM_ENABLE_FAST_JIT != 0
  3616. import_global->data_offset = data_offset;
  3617. #endif
  3618. data_offset += wasm_value_type_size(import_global->type);
  3619. }
  3620. for (i = 0; i < module->global_count; i++) {
  3621. WASMGlobal *global = module->globals + i;
  3622. #if WASM_ENABLE_FAST_JIT != 0
  3623. global->data_offset = data_offset;
  3624. #endif
  3625. data_offset += wasm_value_type_size(global->type);
  3626. }
  3627. module->global_data_size = data_offset;
  3628. }
  3629. #if WASM_ENABLE_FAST_JIT != 0
  3630. static bool
  3631. init_fast_jit_functions(WASMModule *module, char *error_buf,
  3632. uint32 error_buf_size)
  3633. {
  3634. #if WASM_ENABLE_LAZY_JIT != 0
  3635. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  3636. #endif
  3637. uint32 i;
  3638. if (!module->function_count)
  3639. return true;
  3640. if (!(module->fast_jit_func_ptrs =
  3641. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  3642. error_buf_size))) {
  3643. return false;
  3644. }
  3645. #if WASM_ENABLE_LAZY_JIT != 0
  3646. for (i = 0; i < module->function_count; i++) {
  3647. module->fast_jit_func_ptrs[i] =
  3648. jit_globals->compile_fast_jit_and_then_call;
  3649. }
  3650. #endif
  3651. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  3652. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  3653. set_error_buf(error_buf, error_buf_size,
  3654. "init fast jit thread lock failed");
  3655. return false;
  3656. }
  3657. module->fast_jit_thread_locks_inited[i] = true;
  3658. }
  3659. return true;
  3660. }
  3661. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  3662. #if WASM_ENABLE_JIT != 0
  3663. static bool
  3664. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  3665. uint32 error_buf_size)
  3666. {
  3667. LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  3668. AOTCompOption option = { 0 };
  3669. char *aot_last_error;
  3670. uint64 size;
  3671. if (module->function_count == 0)
  3672. return true;
  3673. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LLVM_JIT != 0
  3674. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  3675. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  3676. return false;
  3677. }
  3678. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  3679. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  3680. os_mutex_destroy(&module->tierup_wait_lock);
  3681. return false;
  3682. }
  3683. module->tierup_wait_lock_inited = true;
  3684. #endif
  3685. size = sizeof(void *) * (uint64)module->function_count
  3686. + sizeof(bool) * (uint64)module->function_count;
  3687. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  3688. return false;
  3689. }
  3690. module->func_ptrs_compiled =
  3691. (bool *)((uint8 *)module->func_ptrs
  3692. + sizeof(void *) * module->function_count);
  3693. module->comp_data = aot_create_comp_data(module);
  3694. if (!module->comp_data) {
  3695. aot_last_error = aot_get_last_error();
  3696. bh_assert(aot_last_error != NULL);
  3697. set_error_buf(error_buf, error_buf_size, aot_last_error);
  3698. return false;
  3699. }
  3700. option.is_jit_mode = true;
  3701. llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  3702. option.opt_level = llvm_jit_options.opt_level;
  3703. option.size_level = llvm_jit_options.size_level;
  3704. #if WASM_ENABLE_BULK_MEMORY != 0
  3705. option.enable_bulk_memory = true;
  3706. #endif
  3707. #if WASM_ENABLE_THREAD_MGR != 0
  3708. option.enable_thread_mgr = true;
  3709. #endif
  3710. #if WASM_ENABLE_TAIL_CALL != 0
  3711. option.enable_tail_call = true;
  3712. #endif
  3713. #if WASM_ENABLE_SIMD != 0
  3714. option.enable_simd = true;
  3715. #endif
  3716. #if WASM_ENABLE_REF_TYPES != 0
  3717. option.enable_ref_types = true;
  3718. #endif
  3719. option.enable_aux_stack_check = true;
  3720. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
  3721. option.enable_aux_stack_frame = true;
  3722. #endif
  3723. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3724. option.enable_stack_estimation = true;
  3725. #endif
  3726. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  3727. if (!module->comp_ctx) {
  3728. aot_last_error = aot_get_last_error();
  3729. bh_assert(aot_last_error != NULL);
  3730. set_error_buf(error_buf, error_buf_size, aot_last_error);
  3731. return false;
  3732. }
  3733. return true;
  3734. }
  3735. static bool
  3736. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  3737. uint32 error_buf_size)
  3738. {
  3739. char *aot_last_error;
  3740. uint32 i;
  3741. if (module->function_count == 0)
  3742. return true;
  3743. if (!aot_compile_wasm(module->comp_ctx)) {
  3744. aot_last_error = aot_get_last_error();
  3745. bh_assert(aot_last_error != NULL);
  3746. set_error_buf(error_buf, error_buf_size, aot_last_error);
  3747. return false;
  3748. }
  3749. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3750. if (module->orcjit_stop_compiling)
  3751. return false;
  3752. #endif
  3753. bh_print_time("Begin to lookup llvm jit functions");
  3754. for (i = 0; i < module->function_count; i++) {
  3755. LLVMOrcJITTargetAddress func_addr = 0;
  3756. LLVMErrorRef error;
  3757. char func_name[48];
  3758. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  3759. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  3760. func_name);
  3761. if (error != LLVMErrorSuccess) {
  3762. char *err_msg = LLVMGetErrorMessage(error);
  3763. set_error_buf_v(error_buf, error_buf_size,
  3764. "failed to compile llvm jit function: %s", err_msg);
  3765. LLVMDisposeErrorMessage(err_msg);
  3766. return false;
  3767. }
  3768. /**
  3769. * No need to lock the func_ptr[func_idx] here as it is basic
  3770. * data type, the load/store for it can be finished by one cpu
  3771. * instruction, and there can be only one cpu instruction
  3772. * loading/storing at the same time.
  3773. */
  3774. module->func_ptrs[i] = (void *)func_addr;
  3775. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3776. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  3777. if (module->orcjit_stop_compiling)
  3778. return false;
  3779. #endif
  3780. }
  3781. bh_print_time("End lookup llvm jit functions");
  3782. return true;
  3783. }
  3784. #endif /* end of WASM_ENABLE_JIT != 0 */
  3785. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3786. && WASM_ENABLE_LAZY_JIT != 0
  3787. static void *
  3788. init_llvm_jit_functions_stage2_callback(void *arg)
  3789. {
  3790. WASMModule *module = (WASMModule *)arg;
  3791. char error_buf[128];
  3792. uint32 error_buf_size = (uint32)sizeof(error_buf);
  3793. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  3794. module->orcjit_stop_compiling = true;
  3795. return NULL;
  3796. }
  3797. os_mutex_lock(&module->tierup_wait_lock);
  3798. module->llvm_jit_inited = true;
  3799. os_cond_broadcast(&module->tierup_wait_cond);
  3800. os_mutex_unlock(&module->tierup_wait_lock);
  3801. return NULL;
  3802. }
  3803. #endif
  3804. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  3805. /* The callback function to compile jit functions */
  3806. static void *
  3807. orcjit_thread_callback(void *arg)
  3808. {
  3809. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  3810. #if WASM_ENABLE_JIT != 0
  3811. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  3812. #endif
  3813. WASMModule *module = thread_arg->module;
  3814. uint32 group_idx = thread_arg->group_idx;
  3815. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  3816. uint32 func_count = module->function_count;
  3817. uint32 i;
  3818. #if WASM_ENABLE_FAST_JIT != 0
  3819. /* Compile fast jit funcitons of this group */
  3820. for (i = group_idx; i < func_count; i += group_stride) {
  3821. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  3822. os_printf("failed to compile fast jit function %u\n", i);
  3823. break;
  3824. }
  3825. if (module->orcjit_stop_compiling) {
  3826. return NULL;
  3827. }
  3828. }
  3829. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3830. os_mutex_lock(&module->tierup_wait_lock);
  3831. module->fast_jit_ready_groups++;
  3832. os_mutex_unlock(&module->tierup_wait_lock);
  3833. #endif
  3834. #endif
  3835. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3836. && WASM_ENABLE_LAZY_JIT != 0
  3837. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  3838. for (i = group_idx; i < func_count;
  3839. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  3840. uint32 j;
  3841. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  3842. if (i + j * group_stride < func_count) {
  3843. if (!jit_compiler_set_call_to_fast_jit(
  3844. module,
  3845. i + j * group_stride + module->import_function_count)) {
  3846. os_printf(
  3847. "failed to compile call_to_fast_jit for func %u\n",
  3848. i + j * group_stride + module->import_function_count);
  3849. module->orcjit_stop_compiling = true;
  3850. return NULL;
  3851. }
  3852. }
  3853. if (module->orcjit_stop_compiling) {
  3854. return NULL;
  3855. }
  3856. }
  3857. }
  3858. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  3859. fast jit functions are compiled */
  3860. os_mutex_lock(&module->tierup_wait_lock);
  3861. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  3862. && module->fast_jit_ready_groups >= group_stride)) {
  3863. os_cond_reltimedwait(&module->tierup_wait_cond,
  3864. &module->tierup_wait_lock, 10000);
  3865. if (module->orcjit_stop_compiling) {
  3866. /* init_llvm_jit_functions_stage2 failed */
  3867. os_mutex_unlock(&module->tierup_wait_lock);
  3868. return NULL;
  3869. }
  3870. }
  3871. os_mutex_unlock(&module->tierup_wait_lock);
  3872. #endif
  3873. #if WASM_ENABLE_JIT != 0
  3874. /* Compile llvm jit functions of this group */
  3875. for (i = group_idx; i < func_count;
  3876. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  3877. LLVMOrcJITTargetAddress func_addr = 0;
  3878. LLVMErrorRef error;
  3879. char func_name[48];
  3880. typedef void (*F)(void);
  3881. union {
  3882. F f;
  3883. void *v;
  3884. } u;
  3885. uint32 j;
  3886. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  3887. "_wrapper");
  3888. LOG_DEBUG("compile llvm jit func %s", func_name);
  3889. error =
  3890. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  3891. if (error != LLVMErrorSuccess) {
  3892. char *err_msg = LLVMGetErrorMessage(error);
  3893. os_printf("failed to compile llvm jit function %u: %s", i, err_msg);
  3894. LLVMDisposeErrorMessage(err_msg);
  3895. break;
  3896. }
  3897. /* Call the jit wrapper function to trigger its compilation, so as
  3898. to compile the actual jit functions, since we add the latter to
  3899. function list in the PartitionFunction callback */
  3900. u.v = (void *)func_addr;
  3901. u.f();
  3902. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  3903. if (i + j * group_stride < func_count) {
  3904. module->func_ptrs_compiled[i + j * group_stride] = true;
  3905. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3906. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  3907. i + j * group_stride);
  3908. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  3909. func_name);
  3910. if (error != LLVMErrorSuccess) {
  3911. char *err_msg = LLVMGetErrorMessage(error);
  3912. os_printf("failed to compile llvm jit function %u: %s", i,
  3913. err_msg);
  3914. LLVMDisposeErrorMessage(err_msg);
  3915. /* Ignore current llvm jit func, as its func ptr is
  3916. previous set to call_to_fast_jit, which also works */
  3917. continue;
  3918. }
  3919. jit_compiler_set_llvm_jit_func_ptr(
  3920. module,
  3921. i + j * group_stride + module->import_function_count,
  3922. (void *)func_addr);
  3923. /* Try to switch to call this llvm jit funtion instead of
  3924. fast jit function from fast jit jitted code */
  3925. jit_compiler_set_call_to_llvm_jit(
  3926. module,
  3927. i + j * group_stride + module->import_function_count);
  3928. #endif
  3929. }
  3930. }
  3931. if (module->orcjit_stop_compiling) {
  3932. break;
  3933. }
  3934. }
  3935. #endif
  3936. return NULL;
  3937. }
  3938. static void
  3939. orcjit_stop_compile_threads(WASMModule *module)
  3940. {
  3941. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  3942. / sizeof(OrcJitThreadArg));
  3943. module->orcjit_stop_compiling = true;
  3944. for (i = 0; i < thread_num; i++) {
  3945. if (module->orcjit_threads[i])
  3946. os_thread_join(module->orcjit_threads[i], NULL);
  3947. }
  3948. }
  3949. static bool
  3950. compile_jit_functions(WASMModule *module, char *error_buf,
  3951. uint32 error_buf_size)
  3952. {
  3953. uint32 thread_num =
  3954. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  3955. uint32 i, j;
  3956. bh_print_time("Begin to compile jit functions");
  3957. /* Create threads to compile the jit functions */
  3958. for (i = 0; i < thread_num && i < module->function_count; i++) {
  3959. #if WASM_ENABLE_JIT != 0
  3960. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  3961. #endif
  3962. module->orcjit_thread_args[i].module = module;
  3963. module->orcjit_thread_args[i].group_idx = i;
  3964. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  3965. (void *)&module->orcjit_thread_args[i],
  3966. APP_THREAD_STACK_SIZE_DEFAULT)
  3967. != 0) {
  3968. set_error_buf(error_buf, error_buf_size,
  3969. "create orcjit compile thread failed");
  3970. /* Terminate the threads created */
  3971. module->orcjit_stop_compiling = true;
  3972. for (j = 0; j < i; j++) {
  3973. os_thread_join(module->orcjit_threads[j], NULL);
  3974. }
  3975. return false;
  3976. }
  3977. }
  3978. #if WASM_ENABLE_LAZY_JIT == 0
  3979. /* Wait until all jit functions are compiled for eager mode */
  3980. for (i = 0; i < thread_num; i++) {
  3981. if (module->orcjit_threads[i])
  3982. os_thread_join(module->orcjit_threads[i], NULL);
  3983. }
  3984. #if WASM_ENABLE_FAST_JIT != 0
  3985. /* Ensure all the fast-jit functions are compiled */
  3986. for (i = 0; i < module->function_count; i++) {
  3987. if (!jit_compiler_is_compiled(module,
  3988. i + module->import_function_count)) {
  3989. set_error_buf(error_buf, error_buf_size,
  3990. "failed to compile fast jit function");
  3991. return false;
  3992. }
  3993. }
  3994. #endif
  3995. #if WASM_ENABLE_JIT != 0
  3996. /* Ensure all the llvm-jit functions are compiled */
  3997. for (i = 0; i < module->function_count; i++) {
  3998. if (!module->func_ptrs_compiled[i]) {
  3999. set_error_buf(error_buf, error_buf_size,
  4000. "failed to compile llvm jit function");
  4001. return false;
  4002. }
  4003. }
  4004. #endif
  4005. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  4006. bh_print_time("End compile jit functions");
  4007. return true;
  4008. }
  4009. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  4010. static bool
  4011. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4012. uint32 cur_func_idx, char *error_buf,
  4013. uint32 error_buf_size);
  4014. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  4015. void **
  4016. wasm_interp_get_handle_table();
  4017. static void **handle_table;
  4018. #endif
  4019. static bool
  4020. load_from_sections(WASMModule *module, WASMSection *sections,
  4021. bool is_load_from_file_buf, char *error_buf,
  4022. uint32 error_buf_size)
  4023. {
  4024. WASMExport *export;
  4025. WASMSection *section = sections;
  4026. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  4027. *buf_func = NULL, *buf_func_end = NULL;
  4028. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  4029. WASMGlobal *aux_stack_top_global = NULL, *global;
  4030. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  4031. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  4032. uint32 aux_data_end_global_index = (uint32)-1;
  4033. uint32 aux_heap_base_global_index = (uint32)-1;
  4034. WASMFuncType *func_type;
  4035. /* Find code and function sections if have */
  4036. while (section) {
  4037. if (section->section_type == SECTION_TYPE_CODE) {
  4038. buf_code = section->section_body;
  4039. buf_code_end = buf_code + section->section_body_size;
  4040. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  4041. module->buf_code = (uint8 *)buf_code;
  4042. module->buf_code_size = section->section_body_size;
  4043. #endif
  4044. }
  4045. else if (section->section_type == SECTION_TYPE_FUNC) {
  4046. buf_func = section->section_body;
  4047. buf_func_end = buf_func + section->section_body_size;
  4048. }
  4049. section = section->next;
  4050. }
  4051. section = sections;
  4052. while (section) {
  4053. buf = section->section_body;
  4054. buf_end = buf + section->section_body_size;
  4055. switch (section->section_type) {
  4056. case SECTION_TYPE_USER:
  4057. /* unsupported user section, ignore it. */
  4058. if (!load_user_section(buf, buf_end, module,
  4059. is_load_from_file_buf, error_buf,
  4060. error_buf_size))
  4061. return false;
  4062. break;
  4063. case SECTION_TYPE_TYPE:
  4064. if (!load_type_section(buf, buf_end, module, error_buf,
  4065. error_buf_size))
  4066. return false;
  4067. break;
  4068. case SECTION_TYPE_IMPORT:
  4069. if (!load_import_section(buf, buf_end, module,
  4070. is_load_from_file_buf, error_buf,
  4071. error_buf_size))
  4072. return false;
  4073. break;
  4074. case SECTION_TYPE_FUNC:
  4075. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  4076. module, error_buf, error_buf_size))
  4077. return false;
  4078. break;
  4079. case SECTION_TYPE_TABLE:
  4080. if (!load_table_section(buf, buf_end, module, error_buf,
  4081. error_buf_size))
  4082. return false;
  4083. break;
  4084. case SECTION_TYPE_MEMORY:
  4085. if (!load_memory_section(buf, buf_end, module, error_buf,
  4086. error_buf_size))
  4087. return false;
  4088. break;
  4089. case SECTION_TYPE_GLOBAL:
  4090. if (!load_global_section(buf, buf_end, module, error_buf,
  4091. error_buf_size))
  4092. return false;
  4093. break;
  4094. case SECTION_TYPE_EXPORT:
  4095. if (!load_export_section(buf, buf_end, module,
  4096. is_load_from_file_buf, error_buf,
  4097. error_buf_size))
  4098. return false;
  4099. break;
  4100. case SECTION_TYPE_START:
  4101. if (!load_start_section(buf, buf_end, module, error_buf,
  4102. error_buf_size))
  4103. return false;
  4104. break;
  4105. case SECTION_TYPE_ELEM:
  4106. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  4107. error_buf_size))
  4108. return false;
  4109. break;
  4110. case SECTION_TYPE_CODE:
  4111. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  4112. module, error_buf, error_buf_size))
  4113. return false;
  4114. break;
  4115. case SECTION_TYPE_DATA:
  4116. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  4117. error_buf_size))
  4118. return false;
  4119. break;
  4120. #if WASM_ENABLE_BULK_MEMORY != 0
  4121. case SECTION_TYPE_DATACOUNT:
  4122. if (!load_datacount_section(buf, buf_end, module, error_buf,
  4123. error_buf_size))
  4124. return false;
  4125. break;
  4126. #endif
  4127. default:
  4128. set_error_buf(error_buf, error_buf_size, "invalid section id");
  4129. return false;
  4130. }
  4131. section = section->next;
  4132. }
  4133. module->aux_data_end_global_index = (uint32)-1;
  4134. module->aux_heap_base_global_index = (uint32)-1;
  4135. module->aux_stack_top_global_index = (uint32)-1;
  4136. /* Resolve auxiliary data/stack/heap info and reset memory info */
  4137. export = module->exports;
  4138. for (i = 0; i < module->export_count; i++, export ++) {
  4139. if (export->kind == EXPORT_KIND_GLOBAL) {
  4140. if (!strcmp(export->name, "__heap_base")) {
  4141. global_index = export->index - module->import_global_count;
  4142. global = module->globals + global_index;
  4143. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  4144. && global->init_expr.init_expr_type
  4145. == INIT_EXPR_TYPE_I32_CONST) {
  4146. aux_heap_base_global = global;
  4147. aux_heap_base = global->init_expr.u.i32;
  4148. aux_heap_base_global_index = export->index;
  4149. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  4150. aux_heap_base);
  4151. }
  4152. }
  4153. else if (!strcmp(export->name, "__data_end")) {
  4154. global_index = export->index - module->import_global_count;
  4155. global = module->globals + global_index;
  4156. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  4157. && global->init_expr.init_expr_type
  4158. == INIT_EXPR_TYPE_I32_CONST) {
  4159. aux_data_end_global = global;
  4160. aux_data_end = global->init_expr.u.i32;
  4161. aux_data_end_global_index = export->index;
  4162. LOG_VERBOSE("Found aux __data_end global, value: %d",
  4163. aux_data_end);
  4164. aux_data_end = align_uint(aux_data_end, 16);
  4165. }
  4166. }
  4167. /* For module compiled with -pthread option, the global is:
  4168. [0] stack_top <-- 0
  4169. [1] tls_pointer
  4170. [2] tls_size
  4171. [3] data_end <-- 3
  4172. [4] global_base
  4173. [5] heap_base <-- 5
  4174. [6] dso_handle
  4175. For module compiled without -pthread option:
  4176. [0] stack_top <-- 0
  4177. [1] data_end <-- 1
  4178. [2] global_base
  4179. [3] heap_base <-- 3
  4180. [4] dso_handle
  4181. */
  4182. if (aux_data_end_global && aux_heap_base_global
  4183. && aux_data_end <= aux_heap_base) {
  4184. module->aux_data_end_global_index = aux_data_end_global_index;
  4185. module->aux_data_end = aux_data_end;
  4186. module->aux_heap_base_global_index = aux_heap_base_global_index;
  4187. module->aux_heap_base = aux_heap_base;
  4188. /* Resolve aux stack top global */
  4189. for (global_index = 0; global_index < module->global_count;
  4190. global_index++) {
  4191. global = module->globals + global_index;
  4192. if (global->is_mutable /* heap_base and data_end is
  4193. not mutable */
  4194. && global->type == VALUE_TYPE_I32
  4195. && global->init_expr.init_expr_type
  4196. == INIT_EXPR_TYPE_I32_CONST
  4197. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  4198. aux_stack_top_global = global;
  4199. aux_stack_top = (uint32)global->init_expr.u.i32;
  4200. module->aux_stack_top_global_index =
  4201. module->import_global_count + global_index;
  4202. module->aux_stack_bottom = aux_stack_top;
  4203. module->aux_stack_size =
  4204. aux_stack_top > aux_data_end
  4205. ? aux_stack_top - aux_data_end
  4206. : aux_stack_top;
  4207. LOG_VERBOSE("Found aux stack top global, value: %d, "
  4208. "global index: %d, stack size: %d",
  4209. aux_stack_top, global_index,
  4210. module->aux_stack_size);
  4211. break;
  4212. }
  4213. }
  4214. if (!aux_stack_top_global) {
  4215. /* Auxiliary stack global isn't found, it must be unused
  4216. in the wasm app, as if it is used, the global must be
  4217. defined. Here we set it to __heap_base global and set
  4218. its size to 0. */
  4219. aux_stack_top_global = aux_heap_base_global;
  4220. aux_stack_top = aux_heap_base;
  4221. module->aux_stack_top_global_index =
  4222. module->aux_heap_base_global_index;
  4223. module->aux_stack_bottom = aux_stack_top;
  4224. module->aux_stack_size = 0;
  4225. }
  4226. break;
  4227. }
  4228. }
  4229. }
  4230. module->malloc_function = (uint32)-1;
  4231. module->free_function = (uint32)-1;
  4232. module->retain_function = (uint32)-1;
  4233. /* Resolve malloc/free function exported by wasm module */
  4234. export = module->exports;
  4235. for (i = 0; i < module->export_count; i++, export ++) {
  4236. if (export->kind == EXPORT_KIND_FUNC) {
  4237. if (!strcmp(export->name, "malloc")
  4238. && export->index >= module->import_function_count) {
  4239. func_index = export->index - module->import_function_count;
  4240. func_type = module->functions[func_index]->func_type;
  4241. if (func_type->param_count == 1 && func_type->result_count == 1
  4242. && func_type->types[0] == VALUE_TYPE_I32
  4243. && func_type->types[1] == VALUE_TYPE_I32) {
  4244. bh_assert(module->malloc_function == (uint32)-1);
  4245. module->malloc_function = export->index;
  4246. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  4247. export->name, export->index);
  4248. }
  4249. }
  4250. else if (!strcmp(export->name, "__new")
  4251. && export->index >= module->import_function_count) {
  4252. /* __new && __pin for AssemblyScript */
  4253. func_index = export->index - module->import_function_count;
  4254. func_type = module->functions[func_index]->func_type;
  4255. if (func_type->param_count == 2 && func_type->result_count == 1
  4256. && func_type->types[0] == VALUE_TYPE_I32
  4257. && func_type->types[1] == VALUE_TYPE_I32
  4258. && func_type->types[2] == VALUE_TYPE_I32) {
  4259. uint32 j;
  4260. WASMExport *export_tmp;
  4261. bh_assert(module->malloc_function == (uint32)-1);
  4262. module->malloc_function = export->index;
  4263. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  4264. export->name, export->index);
  4265. /* resolve retain function.
  4266. If not found, reset malloc function index */
  4267. export_tmp = module->exports;
  4268. for (j = 0; j < module->export_count; j++, export_tmp++) {
  4269. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  4270. && (!strcmp(export_tmp->name, "__retain")
  4271. || (!strcmp(export_tmp->name, "__pin")))
  4272. && (export_tmp->index
  4273. >= module->import_function_count)) {
  4274. func_index = export_tmp->index
  4275. - module->import_function_count;
  4276. func_type =
  4277. module->functions[func_index]->func_type;
  4278. if (func_type->param_count == 1
  4279. && func_type->result_count == 1
  4280. && func_type->types[0] == VALUE_TYPE_I32
  4281. && func_type->types[1] == VALUE_TYPE_I32) {
  4282. bh_assert(module->retain_function
  4283. == (uint32)-1);
  4284. module->retain_function = export_tmp->index;
  4285. LOG_VERBOSE("Found retain function, name: %s, "
  4286. "index: %u",
  4287. export_tmp->name,
  4288. export_tmp->index);
  4289. break;
  4290. }
  4291. }
  4292. }
  4293. if (j == module->export_count) {
  4294. module->malloc_function = (uint32)-1;
  4295. LOG_VERBOSE("Can't find retain function,"
  4296. "reset malloc function index to -1");
  4297. }
  4298. }
  4299. }
  4300. else if (((!strcmp(export->name, "free"))
  4301. || (!strcmp(export->name, "__release"))
  4302. || (!strcmp(export->name, "__unpin")))
  4303. && export->index >= module->import_function_count) {
  4304. func_index = export->index - module->import_function_count;
  4305. func_type = module->functions[func_index]->func_type;
  4306. if (func_type->param_count == 1 && func_type->result_count == 0
  4307. && func_type->types[0] == VALUE_TYPE_I32) {
  4308. bh_assert(module->free_function == (uint32)-1);
  4309. module->free_function = export->index;
  4310. LOG_VERBOSE("Found free function, name: %s, index: %u",
  4311. export->name, export->index);
  4312. }
  4313. }
  4314. }
  4315. }
  4316. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  4317. handle_table = wasm_interp_get_handle_table();
  4318. #endif
  4319. for (i = 0; i < module->function_count; i++) {
  4320. WASMFunction *func = module->functions[i];
  4321. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  4322. error_buf_size)) {
  4323. return false;
  4324. }
  4325. if (i == module->function_count - 1
  4326. && func->code + func->code_size != buf_code_end) {
  4327. set_error_buf(error_buf, error_buf_size,
  4328. "code section size mismatch");
  4329. return false;
  4330. }
  4331. }
  4332. if (!module->possible_memory_grow) {
  4333. WASMMemoryImport *memory_import;
  4334. WASMMemory *memory;
  4335. if (aux_data_end_global && aux_heap_base_global
  4336. && aux_stack_top_global) {
  4337. uint64 init_memory_size;
  4338. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  4339. if (module->import_memory_count) {
  4340. memory_import = &module->import_memories[0].u.memory;
  4341. init_memory_size = (uint64)memory_import->num_bytes_per_page
  4342. * memory_import->init_page_count;
  4343. if (shrunk_memory_size <= init_memory_size) {
  4344. /* Reset memory info to decrease memory usage */
  4345. memory_import->num_bytes_per_page = shrunk_memory_size;
  4346. memory_import->init_page_count = 1;
  4347. LOG_VERBOSE("Shrink import memory size to %d",
  4348. shrunk_memory_size);
  4349. }
  4350. }
  4351. if (module->memory_count) {
  4352. memory = &module->memories[0];
  4353. init_memory_size = (uint64)memory->num_bytes_per_page
  4354. * memory->init_page_count;
  4355. if (shrunk_memory_size <= init_memory_size) {
  4356. /* Reset memory info to decrease memory usage */
  4357. memory->num_bytes_per_page = shrunk_memory_size;
  4358. memory->init_page_count = 1;
  4359. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  4360. }
  4361. }
  4362. }
  4363. #if WASM_ENABLE_MULTI_MODULE == 0
  4364. if (module->import_memory_count) {
  4365. memory_import = &module->import_memories[0].u.memory;
  4366. if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
  4367. memory_import->num_bytes_per_page *=
  4368. memory_import->init_page_count;
  4369. else
  4370. memory_import->num_bytes_per_page = UINT32_MAX;
  4371. if (memory_import->init_page_count > 0)
  4372. memory_import->init_page_count = memory_import->max_page_count =
  4373. 1;
  4374. else
  4375. memory_import->init_page_count = memory_import->max_page_count =
  4376. 0;
  4377. }
  4378. if (module->memory_count) {
  4379. memory = &module->memories[0];
  4380. if (memory->init_page_count < DEFAULT_MAX_PAGES)
  4381. memory->num_bytes_per_page *= memory->init_page_count;
  4382. else
  4383. memory->num_bytes_per_page = UINT32_MAX;
  4384. if (memory->init_page_count > 0)
  4385. memory->init_page_count = memory->max_page_count = 1;
  4386. else
  4387. memory->init_page_count = memory->max_page_count = 0;
  4388. }
  4389. #endif
  4390. }
  4391. calculate_global_data_offset(module);
  4392. #if WASM_ENABLE_FAST_JIT != 0
  4393. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  4394. return false;
  4395. }
  4396. #endif
  4397. #if WASM_ENABLE_JIT != 0
  4398. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  4399. return false;
  4400. }
  4401. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  4402. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4403. return false;
  4404. }
  4405. #else
  4406. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  4407. thread fast jit execution, since applying llvm optimizations in
  4408. aot_compile_wasm may cost a lot of time.
  4409. Create thread with enough native stack to apply llvm optimizations */
  4410. if (os_thread_create(&module->llvm_jit_init_thread,
  4411. init_llvm_jit_functions_stage2_callback,
  4412. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  4413. != 0) {
  4414. set_error_buf(error_buf, error_buf_size,
  4415. "create orcjit compile thread failed");
  4416. return false;
  4417. }
  4418. #endif
  4419. #endif
  4420. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4421. /* Create threads to compile the jit functions */
  4422. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  4423. return false;
  4424. }
  4425. #endif
  4426. #if WASM_ENABLE_MEMORY_TRACING != 0
  4427. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  4428. #endif
  4429. return true;
  4430. }
  4431. static WASMModule *
  4432. create_module(char *error_buf, uint32 error_buf_size)
  4433. {
  4434. WASMModule *module =
  4435. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  4436. bh_list_status ret;
  4437. if (!module) {
  4438. return NULL;
  4439. }
  4440. module->module_type = Wasm_Module_Bytecode;
  4441. /* Set start_function to -1, means no start function */
  4442. module->start_function = (uint32)-1;
  4443. #if WASM_ENABLE_FAST_INTERP == 0
  4444. module->br_table_cache_list = &module->br_table_cache_list_head;
  4445. ret = bh_list_init(module->br_table_cache_list);
  4446. bh_assert(ret == BH_LIST_SUCCESS);
  4447. #endif
  4448. #if WASM_ENABLE_MULTI_MODULE != 0
  4449. module->import_module_list = &module->import_module_list_head;
  4450. ret = bh_list_init(module->import_module_list);
  4451. bh_assert(ret == BH_LIST_SUCCESS);
  4452. #endif
  4453. #if WASM_ENABLE_DEBUG_INTERP != 0
  4454. ret = bh_list_init(&module->fast_opcode_list);
  4455. bh_assert(ret == BH_LIST_SUCCESS);
  4456. #endif
  4457. #if WASM_ENABLE_GC != 0
  4458. if (!(module->ref_type_set =
  4459. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  4460. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  4461. goto fail1;
  4462. }
  4463. if (os_mutex_init(&module->rtt_type_lock)) {
  4464. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  4465. goto fail2;
  4466. }
  4467. #endif
  4468. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  4469. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4470. && WASM_ENABLE_LAZY_JIT != 0)
  4471. if (os_mutex_init(&module->instance_list_lock) != 0) {
  4472. set_error_buf(error_buf, error_buf_size,
  4473. "init instance list lock failed");
  4474. goto fail3;
  4475. }
  4476. #endif
  4477. (void)ret;
  4478. return module;
  4479. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  4480. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  4481. && WASM_ENABLE_LAZY_JIT != 0)
  4482. fail3:
  4483. #endif
  4484. #if WASM_ENABLE_GC != 0
  4485. os_mutex_destroy(&module->rtt_type_lock);
  4486. fail2:
  4487. bh_hash_map_destroy(module->ref_type_set);
  4488. fail1:
  4489. #endif
  4490. wasm_runtime_free(module);
  4491. return NULL;
  4492. }
  4493. #if WASM_ENABLE_DEBUG_INTERP != 0
  4494. static bool
  4495. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  4496. uint32 error_buf_size)
  4497. {
  4498. WASMFastOPCodeNode *fast_op =
  4499. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  4500. if (fast_op) {
  4501. fast_op->offset = pos - module->load_addr;
  4502. fast_op->orig_op = orig_op;
  4503. bh_list_insert(&module->fast_opcode_list, fast_op);
  4504. }
  4505. return fast_op ? true : false;
  4506. }
  4507. #endif
  4508. WASMModule *
  4509. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  4510. uint32 error_buf_size)
  4511. {
  4512. WASMModule *module = create_module(error_buf, error_buf_size);
  4513. if (!module)
  4514. return NULL;
  4515. if (!load_from_sections(module, section_list, false, error_buf,
  4516. error_buf_size)) {
  4517. wasm_loader_unload(module);
  4518. return NULL;
  4519. }
  4520. LOG_VERBOSE("Load module from sections success.\n");
  4521. return module;
  4522. }
  4523. static void
  4524. destroy_sections(WASMSection *section_list)
  4525. {
  4526. WASMSection *section = section_list, *next;
  4527. while (section) {
  4528. next = section->next;
  4529. wasm_runtime_free(section);
  4530. section = next;
  4531. }
  4532. }
  4533. /* clang-format off */
  4534. static uint8 section_ids[] = {
  4535. SECTION_TYPE_USER,
  4536. SECTION_TYPE_TYPE,
  4537. SECTION_TYPE_IMPORT,
  4538. SECTION_TYPE_FUNC,
  4539. SECTION_TYPE_TABLE,
  4540. SECTION_TYPE_MEMORY,
  4541. SECTION_TYPE_GLOBAL,
  4542. SECTION_TYPE_EXPORT,
  4543. SECTION_TYPE_START,
  4544. SECTION_TYPE_ELEM,
  4545. #if WASM_ENABLE_BULK_MEMORY != 0
  4546. SECTION_TYPE_DATACOUNT,
  4547. #endif
  4548. SECTION_TYPE_CODE,
  4549. SECTION_TYPE_DATA
  4550. };
  4551. /* clang-format on */
  4552. static uint8
  4553. get_section_index(uint8 section_type)
  4554. {
  4555. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  4556. for (uint8 i = 0; i < max_id; i++) {
  4557. if (section_type == section_ids[i])
  4558. return i;
  4559. }
  4560. return (uint8)-1;
  4561. }
  4562. static bool
  4563. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  4564. char *error_buf, uint32 error_buf_size)
  4565. {
  4566. WASMSection *section_list_end = NULL, *section;
  4567. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  4568. uint8 section_type, section_index, last_section_index = (uint8)-1;
  4569. uint32 section_size;
  4570. bh_assert(!*p_section_list);
  4571. p += 8;
  4572. while (p < p_end) {
  4573. CHECK_BUF(p, p_end, 1);
  4574. section_type = read_uint8(p);
  4575. section_index = get_section_index(section_type);
  4576. if (section_index != (uint8)-1) {
  4577. if (section_type != SECTION_TYPE_USER) {
  4578. /* Custom sections may be inserted at any place,
  4579. while other sections must occur at most once
  4580. and in prescribed order. */
  4581. if (last_section_index != (uint8)-1
  4582. && (section_index <= last_section_index)) {
  4583. set_error_buf(error_buf, error_buf_size,
  4584. "unexpected content after last section or "
  4585. "junk after last section");
  4586. return false;
  4587. }
  4588. last_section_index = section_index;
  4589. }
  4590. read_leb_uint32(p, p_end, section_size);
  4591. CHECK_BUF1(p, p_end, section_size);
  4592. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  4593. error_buf_size))) {
  4594. return false;
  4595. }
  4596. section->section_type = section_type;
  4597. section->section_body = (uint8 *)p;
  4598. section->section_body_size = section_size;
  4599. if (!section_list_end)
  4600. *p_section_list = section_list_end = section;
  4601. else {
  4602. section_list_end->next = section;
  4603. section_list_end = section;
  4604. }
  4605. p += section_size;
  4606. }
  4607. else {
  4608. set_error_buf(error_buf, error_buf_size, "invalid section id");
  4609. return false;
  4610. }
  4611. }
  4612. return true;
  4613. fail:
  4614. return false;
  4615. }
  4616. static void
  4617. exchange32(uint8 *p_data)
  4618. {
  4619. uint8 value = *p_data;
  4620. *p_data = *(p_data + 3);
  4621. *(p_data + 3) = value;
  4622. value = *(p_data + 1);
  4623. *(p_data + 1) = *(p_data + 2);
  4624. *(p_data + 2) = value;
  4625. }
  4626. static union {
  4627. int a;
  4628. char b;
  4629. } __ue = { .a = 1 };
  4630. #define is_little_endian() (__ue.b == 1)
  4631. static bool
  4632. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  4633. uint32 error_buf_size)
  4634. {
  4635. const uint8 *buf_end = buf + size;
  4636. const uint8 *p = buf, *p_end = buf_end;
  4637. uint32 magic_number, version;
  4638. WASMSection *section_list = NULL;
  4639. CHECK_BUF1(p, p_end, sizeof(uint32));
  4640. magic_number = read_uint32(p);
  4641. if (!is_little_endian())
  4642. exchange32((uint8 *)&magic_number);
  4643. if (magic_number != WASM_MAGIC_NUMBER) {
  4644. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  4645. return false;
  4646. }
  4647. CHECK_BUF1(p, p_end, sizeof(uint32));
  4648. version = read_uint32(p);
  4649. if (!is_little_endian())
  4650. exchange32((uint8 *)&version);
  4651. if (version != WASM_CURRENT_VERSION) {
  4652. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  4653. return false;
  4654. }
  4655. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  4656. || !load_from_sections(module, section_list, true, error_buf,
  4657. error_buf_size)) {
  4658. destroy_sections(section_list);
  4659. return false;
  4660. }
  4661. destroy_sections(section_list);
  4662. return true;
  4663. fail:
  4664. return false;
  4665. }
  4666. #if WASM_ENABLE_LIBC_WASI != 0
  4667. /**
  4668. * refer to
  4669. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  4670. */
  4671. static bool
  4672. check_wasi_abi_compatibility(const WASMModule *module,
  4673. #if WASM_ENABLE_MULTI_MODULE != 0
  4674. bool main_module,
  4675. #endif
  4676. char *error_buf, uint32 error_buf_size)
  4677. {
  4678. /**
  4679. * be careful with:
  4680. * wasi compatiable modules(command/reactor) which don't import any wasi
  4681. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  4682. * reactor can depend on nothing. At the same time, each has its own entry
  4683. * point.
  4684. *
  4685. * observations:
  4686. * - clang always injects `_start` into a command
  4687. * - clang always injects `_initialize` into a reactor
  4688. * - `iwasm -f` allows to run a function in the reactor
  4689. *
  4690. * strong assumptions:
  4691. * - no one will define either `_start` or `_initialize` on purpose
  4692. * - `_start` should always be `void _start(void)`
  4693. * - `_initialize` should always be `void _initialize(void)`
  4694. *
  4695. */
  4696. /* clang-format off */
  4697. /**
  4698. *
  4699. * | | import_wasi_api True | | import_wasi_api False | |
  4700. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  4701. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  4702. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  4703. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  4704. */
  4705. /* clang-format on */
  4706. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  4707. /* (func (export "_start") (...) */
  4708. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  4709. error_buf, error_buf_size);
  4710. if (start) {
  4711. WASMFuncType *func_type =
  4712. module->functions[start->index - module->import_function_count]
  4713. ->func_type;
  4714. if (func_type->param_count || func_type->result_count) {
  4715. set_error_buf(error_buf, error_buf_size,
  4716. "the signature of builtin _start function is wrong");
  4717. return false;
  4718. }
  4719. }
  4720. /* (func (export "_initialize") (...) */
  4721. initialize = wasm_loader_find_export(
  4722. module, "", "_initialize", EXPORT_KIND_FUNC, error_buf, error_buf_size);
  4723. if (initialize) {
  4724. WASMFuncType *func_type =
  4725. module->functions[initialize->index - module->import_function_count]
  4726. ->func_type;
  4727. if (func_type->param_count || func_type->result_count) {
  4728. set_error_buf(
  4729. error_buf, error_buf_size,
  4730. "the signature of builtin _initialize function is wrong");
  4731. return false;
  4732. }
  4733. }
  4734. /* filter out non-wasi compatiable modules */
  4735. if (!module->import_wasi_api && !start && !initialize) {
  4736. return true;
  4737. }
  4738. /* should have one at least */
  4739. if (module->import_wasi_api && !start && !initialize) {
  4740. LOG_WARNING("warning: a module with WASI apis should be either "
  4741. "a command or a reactor");
  4742. }
  4743. /*
  4744. * there is at least one of `_start` and `_initialize` in below cases.
  4745. * according to the assumption, they should be all wasi compatiable
  4746. */
  4747. /* always can not have both at the same time */
  4748. if (start && initialize) {
  4749. set_error_buf(
  4750. error_buf, error_buf_size,
  4751. "neither a command nor a reactor can both have _start function "
  4752. "and _initialize function at the same time");
  4753. return false;
  4754. }
  4755. #if WASM_ENABLE_MULTI_MODULE != 0
  4756. /* filter out commands (with `_start`) cases */
  4757. if (start && !main_module) {
  4758. set_error_buf(
  4759. error_buf, error_buf_size,
  4760. "a command (with _start function) can not be a sub-module");
  4761. return false;
  4762. }
  4763. #endif
  4764. /*
  4765. * it is ok a reactor acts as a main module,
  4766. * so skip the check about (with `_initialize`)
  4767. */
  4768. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  4769. error_buf, error_buf_size);
  4770. if (!memory
  4771. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  4772. /*
  4773. * with wasi-threads, it's still an open question if a memory
  4774. * should be exported.
  4775. *
  4776. * https://github.com/WebAssembly/wasi-threads/issues/22
  4777. * https://github.com/WebAssembly/WASI/issues/502
  4778. *
  4779. * Note: this code assumes the number of memories is at most 1.
  4780. */
  4781. && module->import_memory_count == 0
  4782. #endif
  4783. ) {
  4784. set_error_buf(error_buf, error_buf_size,
  4785. "a module with WASI apis must export memory by default");
  4786. return false;
  4787. }
  4788. return true;
  4789. }
  4790. #endif
  4791. WASMModule *
  4792. wasm_loader_load(uint8 *buf, uint32 size,
  4793. #if WASM_ENABLE_MULTI_MODULE != 0
  4794. bool main_module,
  4795. #endif
  4796. char *error_buf, uint32 error_buf_size)
  4797. {
  4798. WASMModule *module = create_module(error_buf, error_buf_size);
  4799. if (!module) {
  4800. return NULL;
  4801. }
  4802. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0
  4803. module->load_addr = (uint8 *)buf;
  4804. module->load_size = size;
  4805. #endif
  4806. if (!load(buf, size, module, error_buf, error_buf_size)) {
  4807. goto fail;
  4808. }
  4809. #if WASM_ENABLE_LIBC_WASI != 0
  4810. /* Check the WASI application ABI */
  4811. if (!check_wasi_abi_compatibility(module,
  4812. #if WASM_ENABLE_MULTI_MODULE != 0
  4813. main_module,
  4814. #endif
  4815. error_buf, error_buf_size)) {
  4816. goto fail;
  4817. }
  4818. #endif
  4819. LOG_VERBOSE("Load module success.\n");
  4820. return module;
  4821. fail:
  4822. wasm_loader_unload(module);
  4823. return NULL;
  4824. }
  4825. void
  4826. wasm_loader_unload(WASMModule *module)
  4827. {
  4828. uint32 i;
  4829. if (!module)
  4830. return;
  4831. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4832. && WASM_ENABLE_LAZY_JIT != 0
  4833. module->orcjit_stop_compiling = true;
  4834. if (module->llvm_jit_init_thread)
  4835. os_thread_join(module->llvm_jit_init_thread, NULL);
  4836. #endif
  4837. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4838. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  4839. module internal data after they were freed */
  4840. orcjit_stop_compile_threads(module);
  4841. #endif
  4842. #if WASM_ENABLE_JIT != 0
  4843. if (module->func_ptrs)
  4844. wasm_runtime_free(module->func_ptrs);
  4845. if (module->comp_ctx)
  4846. aot_destroy_comp_context(module->comp_ctx);
  4847. if (module->comp_data)
  4848. aot_destroy_comp_data(module->comp_data);
  4849. #endif
  4850. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4851. && WASM_ENABLE_LAZY_JIT != 0
  4852. if (module->tierup_wait_lock_inited) {
  4853. os_mutex_destroy(&module->tierup_wait_lock);
  4854. os_cond_destroy(&module->tierup_wait_cond);
  4855. }
  4856. #endif
  4857. if (module->types) {
  4858. for (i = 0; i < module->type_count; i++) {
  4859. if (module->types[i])
  4860. destroy_wasm_type(module->types[i]);
  4861. }
  4862. wasm_runtime_free(module->types);
  4863. }
  4864. if (module->imports)
  4865. wasm_runtime_free(module->imports);
  4866. if (module->functions) {
  4867. for (i = 0; i < module->function_count; i++) {
  4868. if (module->functions[i]) {
  4869. if (module->functions[i]->local_offsets)
  4870. wasm_runtime_free(module->functions[i]->local_offsets);
  4871. #if WASM_ENABLE_FAST_INTERP != 0
  4872. if (module->functions[i]->code_compiled)
  4873. wasm_runtime_free(module->functions[i]->code_compiled);
  4874. if (module->functions[i]->consts)
  4875. wasm_runtime_free(module->functions[i]->consts);
  4876. #endif
  4877. #if WASM_ENABLE_FAST_JIT != 0
  4878. if (module->functions[i]->fast_jit_jitted_code) {
  4879. jit_code_cache_free(
  4880. module->functions[i]->fast_jit_jitted_code);
  4881. }
  4882. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4883. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  4884. jit_code_cache_free(
  4885. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  4886. }
  4887. #endif
  4888. #endif
  4889. #if WASM_ENABLE_GC != 0
  4890. /* TODO
  4891. if (module->functions[i]->local_ref_type_maps)
  4892. wasm_runtime_free(
  4893. module->functions[i]->local_ref_type_maps);
  4894. */
  4895. #endif
  4896. wasm_runtime_free(module->functions[i]);
  4897. }
  4898. }
  4899. wasm_runtime_free(module->functions);
  4900. }
  4901. if (module->tables)
  4902. wasm_runtime_free(module->tables);
  4903. if (module->memories)
  4904. wasm_runtime_free(module->memories);
  4905. if (module->globals)
  4906. wasm_runtime_free(module->globals);
  4907. if (module->exports)
  4908. wasm_runtime_free(module->exports);
  4909. if (module->table_segments) {
  4910. for (i = 0; i < module->table_seg_count; i++) {
  4911. if (module->table_segments[i].func_indexes)
  4912. wasm_runtime_free(module->table_segments[i].func_indexes);
  4913. }
  4914. wasm_runtime_free(module->table_segments);
  4915. }
  4916. if (module->data_segments) {
  4917. for (i = 0; i < module->data_seg_count; i++) {
  4918. if (module->data_segments[i])
  4919. wasm_runtime_free(module->data_segments[i]);
  4920. }
  4921. wasm_runtime_free(module->data_segments);
  4922. }
  4923. if (module->const_str_list) {
  4924. StringNode *node = module->const_str_list, *node_next;
  4925. while (node) {
  4926. node_next = node->next;
  4927. wasm_runtime_free(node);
  4928. node = node_next;
  4929. }
  4930. }
  4931. #if WASM_ENABLE_FAST_INTERP == 0
  4932. if (module->br_table_cache_list) {
  4933. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  4934. BrTableCache *node_next;
  4935. while (node) {
  4936. node_next = bh_list_elem_next(node);
  4937. wasm_runtime_free(node);
  4938. node = node_next;
  4939. }
  4940. }
  4941. #endif
  4942. #if WASM_ENABLE_MULTI_MODULE != 0
  4943. /* just release the sub module list */
  4944. if (module->import_module_list) {
  4945. WASMRegisteredModule *node =
  4946. bh_list_first_elem(module->import_module_list);
  4947. while (node) {
  4948. WASMRegisteredModule *next = bh_list_elem_next(node);
  4949. bh_list_remove(module->import_module_list, node);
  4950. /*
  4951. * unload(sub_module) will be trigged during runtime_destroy().
  4952. * every module in the global module list will be unloaded one by
  4953. * one. so don't worry.
  4954. */
  4955. wasm_runtime_free(node);
  4956. /*
  4957. * the module file reading buffer will be released
  4958. * in runtime_destroy()
  4959. */
  4960. node = next;
  4961. }
  4962. }
  4963. #endif
  4964. #if WASM_ENABLE_DEBUG_INTERP != 0
  4965. WASMFastOPCodeNode *fast_opcode =
  4966. bh_list_first_elem(&module->fast_opcode_list);
  4967. while (fast_opcode) {
  4968. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  4969. wasm_runtime_free(fast_opcode);
  4970. fast_opcode = next;
  4971. }
  4972. #endif
  4973. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  4974. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4975. && WASM_ENABLE_LAZY_JIT != 0)
  4976. os_mutex_destroy(&module->instance_list_lock);
  4977. #endif
  4978. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4979. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  4980. #endif
  4981. #if WASM_ENABLE_FAST_JIT != 0
  4982. if (module->fast_jit_func_ptrs) {
  4983. wasm_runtime_free(module->fast_jit_func_ptrs);
  4984. }
  4985. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4986. if (module->fast_jit_thread_locks_inited[i]) {
  4987. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  4988. }
  4989. }
  4990. #endif
  4991. #if WASM_ENABLE_GC != 0
  4992. os_mutex_destroy(&module->rtt_type_lock);
  4993. bh_hash_map_destroy(module->ref_type_set);
  4994. if (module->rtt_types) {
  4995. for (i = 0; i < module->type_count; i++) {
  4996. if (module->rtt_types[i])
  4997. wasm_runtime_free(module->rtt_types[i]);
  4998. }
  4999. wasm_runtime_free(module->rtt_types);
  5000. }
  5001. #endif
  5002. wasm_runtime_free(module);
  5003. }
  5004. bool
  5005. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  5006. const uint8 *start_addr, const uint8 *code_end_addr,
  5007. uint8 label_type, uint8 **p_else_addr,
  5008. uint8 **p_end_addr)
  5009. {
  5010. const uint8 *p = start_addr, *p_end = code_end_addr;
  5011. uint8 *else_addr = NULL;
  5012. char error_buf[128];
  5013. uint32 block_nested_depth = 1, count, i, j, t;
  5014. uint32 error_buf_size = sizeof(error_buf);
  5015. uint8 opcode, u8;
  5016. BlockAddr block_stack[16] = { { 0 } }, *block;
  5017. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  5018. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  5019. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  5020. if (block[j].start_addr == start_addr) {
  5021. /* Cache hit */
  5022. *p_else_addr = block[j].else_addr;
  5023. *p_end_addr = block[j].end_addr;
  5024. return true;
  5025. }
  5026. }
  5027. /* Cache unhit */
  5028. block_stack[0].start_addr = start_addr;
  5029. while (p < code_end_addr) {
  5030. opcode = *p++;
  5031. #if WASM_ENABLE_DEBUG_INTERP != 0
  5032. op_break_retry:
  5033. #endif
  5034. switch (opcode) {
  5035. case WASM_OP_UNREACHABLE:
  5036. case WASM_OP_NOP:
  5037. break;
  5038. case WASM_OP_BLOCK:
  5039. case WASM_OP_LOOP:
  5040. case WASM_OP_IF:
  5041. {
  5042. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  5043. u8 = read_uint8(p);
  5044. if (is_byte_a_type(u8)) {
  5045. #if WASM_ENABLE_GC != 0
  5046. if (wasm_is_type_multi_byte_type(u8)) {
  5047. /* the possible extra bytes of GC ref type have been
  5048. modified to OP_NOP, no need to resolve them again */
  5049. }
  5050. #endif
  5051. }
  5052. else {
  5053. p--;
  5054. skip_leb_uint32(p, p_end);
  5055. }
  5056. if (block_nested_depth
  5057. < sizeof(block_stack) / sizeof(BlockAddr)) {
  5058. block_stack[block_nested_depth].start_addr = p;
  5059. block_stack[block_nested_depth].else_addr = NULL;
  5060. }
  5061. block_nested_depth++;
  5062. break;
  5063. }
  5064. case EXT_OP_BLOCK:
  5065. case EXT_OP_LOOP:
  5066. case EXT_OP_IF:
  5067. /* block type */
  5068. skip_leb_uint32(p, p_end);
  5069. if (block_nested_depth
  5070. < sizeof(block_stack) / sizeof(BlockAddr)) {
  5071. block_stack[block_nested_depth].start_addr = p;
  5072. block_stack[block_nested_depth].else_addr = NULL;
  5073. }
  5074. block_nested_depth++;
  5075. break;
  5076. case WASM_OP_ELSE:
  5077. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  5078. else_addr = (uint8 *)(p - 1);
  5079. if (block_nested_depth - 1
  5080. < sizeof(block_stack) / sizeof(BlockAddr))
  5081. block_stack[block_nested_depth - 1].else_addr =
  5082. (uint8 *)(p - 1);
  5083. break;
  5084. case WASM_OP_END:
  5085. if (block_nested_depth == 1) {
  5086. if (label_type == LABEL_TYPE_IF)
  5087. *p_else_addr = else_addr;
  5088. *p_end_addr = (uint8 *)(p - 1);
  5089. block_stack[0].end_addr = (uint8 *)(p - 1);
  5090. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  5091. t++) {
  5092. start_addr = block_stack[t].start_addr;
  5093. if (start_addr) {
  5094. i = ((uintptr_t)start_addr)
  5095. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  5096. block =
  5097. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  5098. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  5099. if (!block[j].start_addr)
  5100. break;
  5101. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  5102. memmove(block + 1, block,
  5103. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  5104. * sizeof(BlockAddr));
  5105. j = 0;
  5106. }
  5107. block[j].start_addr = block_stack[t].start_addr;
  5108. block[j].else_addr = block_stack[t].else_addr;
  5109. block[j].end_addr = block_stack[t].end_addr;
  5110. }
  5111. else
  5112. break;
  5113. }
  5114. return true;
  5115. }
  5116. else {
  5117. block_nested_depth--;
  5118. if (block_nested_depth
  5119. < sizeof(block_stack) / sizeof(BlockAddr))
  5120. block_stack[block_nested_depth].end_addr =
  5121. (uint8 *)(p - 1);
  5122. }
  5123. break;
  5124. case WASM_OP_BR:
  5125. case WASM_OP_BR_IF:
  5126. skip_leb_uint32(p, p_end); /* labelidx */
  5127. break;
  5128. case WASM_OP_BR_TABLE:
  5129. read_leb_uint32(p, p_end, count); /* lable num */
  5130. #if WASM_ENABLE_FAST_INTERP != 0
  5131. for (i = 0; i <= count; i++) /* lableidxs */
  5132. skip_leb_uint32(p, p_end);
  5133. #else
  5134. p += count + 1;
  5135. while (*p == WASM_OP_NOP)
  5136. p++;
  5137. #endif
  5138. break;
  5139. #if WASM_ENABLE_FAST_INTERP == 0
  5140. case EXT_OP_BR_TABLE_CACHE:
  5141. read_leb_uint32(p, p_end, count); /* lable num */
  5142. while (*p == WASM_OP_NOP)
  5143. p++;
  5144. break;
  5145. #endif
  5146. case WASM_OP_RETURN:
  5147. break;
  5148. case WASM_OP_CALL:
  5149. #if WASM_ENABLE_TAIL_CALL != 0
  5150. case WASM_OP_RETURN_CALL:
  5151. #endif
  5152. skip_leb_uint32(p, p_end); /* funcidx */
  5153. break;
  5154. case WASM_OP_CALL_INDIRECT:
  5155. #if WASM_ENABLE_TAIL_CALL != 0
  5156. case WASM_OP_RETURN_CALL_INDIRECT:
  5157. #endif
  5158. skip_leb_uint32(p, p_end); /* typeidx */
  5159. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  5160. u8 = read_uint8(p); /* 0x00 */
  5161. #else
  5162. skip_leb_uint32(p, p_end); /* talbeidx */
  5163. #endif
  5164. break;
  5165. #if WASM_ENABLE_GC != 0
  5166. case WASM_OP_CALL_REF:
  5167. case WASM_OP_RETURN_CALL_REF:
  5168. skip_leb_uint32(p, p_end); /* typeidx */
  5169. break;
  5170. #endif
  5171. case WASM_OP_DROP:
  5172. case WASM_OP_SELECT:
  5173. case WASM_OP_DROP_64:
  5174. case WASM_OP_SELECT_64:
  5175. break;
  5176. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  5177. case WASM_OP_SELECT_T:
  5178. {
  5179. skip_leb_uint32(p, p_end); /* vec length */
  5180. u8 = read_uint8(p); /* typeidx */
  5181. /* the possible extra bytes of GC ref type have been
  5182. modified to OP_NOP, no need to resolve them again */
  5183. break;
  5184. }
  5185. case WASM_OP_TABLE_GET:
  5186. case WASM_OP_TABLE_SET:
  5187. skip_leb_uint32(p, p_end); /* table index */
  5188. break;
  5189. case WASM_OP_REF_NULL:
  5190. {
  5191. u8 = read_uint8(p); /* type */
  5192. if (is_byte_a_type(u8)) {
  5193. #if WASM_ENABLE_GC != 0
  5194. if (wasm_is_type_multi_byte_type(u8)) {
  5195. /* the possible extra bytes of GC ref type have been
  5196. modified to OP_NOP, no need to resolve them again */
  5197. }
  5198. #endif
  5199. }
  5200. else {
  5201. p--;
  5202. skip_leb_uint32(p, p_end);
  5203. }
  5204. break;
  5205. }
  5206. case WASM_OP_REF_IS_NULL:
  5207. break;
  5208. case WASM_OP_REF_FUNC:
  5209. skip_leb_uint32(p, p_end); /* func index */
  5210. break;
  5211. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  5212. #if WASM_ENABLE_GC != 0
  5213. case WASM_OP_REF_AS_NON_NULL:
  5214. case WASM_OP_REF_EQ:
  5215. break;
  5216. case WASM_OP_BR_ON_NULL:
  5217. case WASM_OP_BR_ON_NON_NULL:
  5218. skip_leb_uint32(p, p_end); /* label index */
  5219. break;
  5220. #endif /* end of WASM_ENABLE_GC != 0 */
  5221. case WASM_OP_GET_LOCAL:
  5222. case WASM_OP_SET_LOCAL:
  5223. case WASM_OP_TEE_LOCAL:
  5224. case WASM_OP_GET_GLOBAL:
  5225. case WASM_OP_SET_GLOBAL:
  5226. case WASM_OP_GET_GLOBAL_64:
  5227. case WASM_OP_SET_GLOBAL_64:
  5228. case WASM_OP_SET_GLOBAL_AUX_STACK:
  5229. skip_leb_uint32(p, p_end); /* local index */
  5230. break;
  5231. case EXT_OP_GET_LOCAL_FAST:
  5232. case EXT_OP_SET_LOCAL_FAST:
  5233. case EXT_OP_TEE_LOCAL_FAST:
  5234. CHECK_BUF(p, p_end, 1);
  5235. p++;
  5236. break;
  5237. case WASM_OP_I32_LOAD:
  5238. case WASM_OP_I64_LOAD:
  5239. case WASM_OP_F32_LOAD:
  5240. case WASM_OP_F64_LOAD:
  5241. case WASM_OP_I32_LOAD8_S:
  5242. case WASM_OP_I32_LOAD8_U:
  5243. case WASM_OP_I32_LOAD16_S:
  5244. case WASM_OP_I32_LOAD16_U:
  5245. case WASM_OP_I64_LOAD8_S:
  5246. case WASM_OP_I64_LOAD8_U:
  5247. case WASM_OP_I64_LOAD16_S:
  5248. case WASM_OP_I64_LOAD16_U:
  5249. case WASM_OP_I64_LOAD32_S:
  5250. case WASM_OP_I64_LOAD32_U:
  5251. case WASM_OP_I32_STORE:
  5252. case WASM_OP_I64_STORE:
  5253. case WASM_OP_F32_STORE:
  5254. case WASM_OP_F64_STORE:
  5255. case WASM_OP_I32_STORE8:
  5256. case WASM_OP_I32_STORE16:
  5257. case WASM_OP_I64_STORE8:
  5258. case WASM_OP_I64_STORE16:
  5259. case WASM_OP_I64_STORE32:
  5260. skip_leb_uint32(p, p_end); /* align */
  5261. skip_leb_uint32(p, p_end); /* offset */
  5262. break;
  5263. case WASM_OP_MEMORY_SIZE:
  5264. case WASM_OP_MEMORY_GROW:
  5265. skip_leb_uint32(p, p_end); /* 0x00 */
  5266. break;
  5267. case WASM_OP_I32_CONST:
  5268. skip_leb_int32(p, p_end);
  5269. break;
  5270. case WASM_OP_I64_CONST:
  5271. skip_leb_int64(p, p_end);
  5272. break;
  5273. case WASM_OP_F32_CONST:
  5274. p += sizeof(float32);
  5275. break;
  5276. case WASM_OP_F64_CONST:
  5277. p += sizeof(float64);
  5278. break;
  5279. case WASM_OP_I32_EQZ:
  5280. case WASM_OP_I32_EQ:
  5281. case WASM_OP_I32_NE:
  5282. case WASM_OP_I32_LT_S:
  5283. case WASM_OP_I32_LT_U:
  5284. case WASM_OP_I32_GT_S:
  5285. case WASM_OP_I32_GT_U:
  5286. case WASM_OP_I32_LE_S:
  5287. case WASM_OP_I32_LE_U:
  5288. case WASM_OP_I32_GE_S:
  5289. case WASM_OP_I32_GE_U:
  5290. case WASM_OP_I64_EQZ:
  5291. case WASM_OP_I64_EQ:
  5292. case WASM_OP_I64_NE:
  5293. case WASM_OP_I64_LT_S:
  5294. case WASM_OP_I64_LT_U:
  5295. case WASM_OP_I64_GT_S:
  5296. case WASM_OP_I64_GT_U:
  5297. case WASM_OP_I64_LE_S:
  5298. case WASM_OP_I64_LE_U:
  5299. case WASM_OP_I64_GE_S:
  5300. case WASM_OP_I64_GE_U:
  5301. case WASM_OP_F32_EQ:
  5302. case WASM_OP_F32_NE:
  5303. case WASM_OP_F32_LT:
  5304. case WASM_OP_F32_GT:
  5305. case WASM_OP_F32_LE:
  5306. case WASM_OP_F32_GE:
  5307. case WASM_OP_F64_EQ:
  5308. case WASM_OP_F64_NE:
  5309. case WASM_OP_F64_LT:
  5310. case WASM_OP_F64_GT:
  5311. case WASM_OP_F64_LE:
  5312. case WASM_OP_F64_GE:
  5313. case WASM_OP_I32_CLZ:
  5314. case WASM_OP_I32_CTZ:
  5315. case WASM_OP_I32_POPCNT:
  5316. case WASM_OP_I32_ADD:
  5317. case WASM_OP_I32_SUB:
  5318. case WASM_OP_I32_MUL:
  5319. case WASM_OP_I32_DIV_S:
  5320. case WASM_OP_I32_DIV_U:
  5321. case WASM_OP_I32_REM_S:
  5322. case WASM_OP_I32_REM_U:
  5323. case WASM_OP_I32_AND:
  5324. case WASM_OP_I32_OR:
  5325. case WASM_OP_I32_XOR:
  5326. case WASM_OP_I32_SHL:
  5327. case WASM_OP_I32_SHR_S:
  5328. case WASM_OP_I32_SHR_U:
  5329. case WASM_OP_I32_ROTL:
  5330. case WASM_OP_I32_ROTR:
  5331. case WASM_OP_I64_CLZ:
  5332. case WASM_OP_I64_CTZ:
  5333. case WASM_OP_I64_POPCNT:
  5334. case WASM_OP_I64_ADD:
  5335. case WASM_OP_I64_SUB:
  5336. case WASM_OP_I64_MUL:
  5337. case WASM_OP_I64_DIV_S:
  5338. case WASM_OP_I64_DIV_U:
  5339. case WASM_OP_I64_REM_S:
  5340. case WASM_OP_I64_REM_U:
  5341. case WASM_OP_I64_AND:
  5342. case WASM_OP_I64_OR:
  5343. case WASM_OP_I64_XOR:
  5344. case WASM_OP_I64_SHL:
  5345. case WASM_OP_I64_SHR_S:
  5346. case WASM_OP_I64_SHR_U:
  5347. case WASM_OP_I64_ROTL:
  5348. case WASM_OP_I64_ROTR:
  5349. case WASM_OP_F32_ABS:
  5350. case WASM_OP_F32_NEG:
  5351. case WASM_OP_F32_CEIL:
  5352. case WASM_OP_F32_FLOOR:
  5353. case WASM_OP_F32_TRUNC:
  5354. case WASM_OP_F32_NEAREST:
  5355. case WASM_OP_F32_SQRT:
  5356. case WASM_OP_F32_ADD:
  5357. case WASM_OP_F32_SUB:
  5358. case WASM_OP_F32_MUL:
  5359. case WASM_OP_F32_DIV:
  5360. case WASM_OP_F32_MIN:
  5361. case WASM_OP_F32_MAX:
  5362. case WASM_OP_F32_COPYSIGN:
  5363. case WASM_OP_F64_ABS:
  5364. case WASM_OP_F64_NEG:
  5365. case WASM_OP_F64_CEIL:
  5366. case WASM_OP_F64_FLOOR:
  5367. case WASM_OP_F64_TRUNC:
  5368. case WASM_OP_F64_NEAREST:
  5369. case WASM_OP_F64_SQRT:
  5370. case WASM_OP_F64_ADD:
  5371. case WASM_OP_F64_SUB:
  5372. case WASM_OP_F64_MUL:
  5373. case WASM_OP_F64_DIV:
  5374. case WASM_OP_F64_MIN:
  5375. case WASM_OP_F64_MAX:
  5376. case WASM_OP_F64_COPYSIGN:
  5377. case WASM_OP_I32_WRAP_I64:
  5378. case WASM_OP_I32_TRUNC_S_F32:
  5379. case WASM_OP_I32_TRUNC_U_F32:
  5380. case WASM_OP_I32_TRUNC_S_F64:
  5381. case WASM_OP_I32_TRUNC_U_F64:
  5382. case WASM_OP_I64_EXTEND_S_I32:
  5383. case WASM_OP_I64_EXTEND_U_I32:
  5384. case WASM_OP_I64_TRUNC_S_F32:
  5385. case WASM_OP_I64_TRUNC_U_F32:
  5386. case WASM_OP_I64_TRUNC_S_F64:
  5387. case WASM_OP_I64_TRUNC_U_F64:
  5388. case WASM_OP_F32_CONVERT_S_I32:
  5389. case WASM_OP_F32_CONVERT_U_I32:
  5390. case WASM_OP_F32_CONVERT_S_I64:
  5391. case WASM_OP_F32_CONVERT_U_I64:
  5392. case WASM_OP_F32_DEMOTE_F64:
  5393. case WASM_OP_F64_CONVERT_S_I32:
  5394. case WASM_OP_F64_CONVERT_U_I32:
  5395. case WASM_OP_F64_CONVERT_S_I64:
  5396. case WASM_OP_F64_CONVERT_U_I64:
  5397. case WASM_OP_F64_PROMOTE_F32:
  5398. case WASM_OP_I32_REINTERPRET_F32:
  5399. case WASM_OP_I64_REINTERPRET_F64:
  5400. case WASM_OP_F32_REINTERPRET_I32:
  5401. case WASM_OP_F64_REINTERPRET_I64:
  5402. case WASM_OP_I32_EXTEND8_S:
  5403. case WASM_OP_I32_EXTEND16_S:
  5404. case WASM_OP_I64_EXTEND8_S:
  5405. case WASM_OP_I64_EXTEND16_S:
  5406. case WASM_OP_I64_EXTEND32_S:
  5407. break;
  5408. #if WASM_ENABLE_GC != 0
  5409. case WASM_OP_GC_PREFIX:
  5410. {
  5411. uint32 opcode1;
  5412. read_leb_uint32(p, p_end, opcode1);
  5413. switch (opcode1) {
  5414. case WASM_OP_STRUCT_NEW_CANON:
  5415. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  5416. skip_leb_uint32(p, p_end); /* typeidx */
  5417. break;
  5418. case WASM_OP_STRUCT_GET:
  5419. case WASM_OP_STRUCT_GET_S:
  5420. case WASM_OP_STRUCT_GET_U:
  5421. case WASM_OP_STRUCT_SET:
  5422. skip_leb_uint32(p, p_end); /* typeidx */
  5423. skip_leb_uint32(p, p_end); /* fieldidx */
  5424. break;
  5425. case WASM_OP_ARRAY_NEW_CANON:
  5426. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  5427. case WASM_OP_ARRAY_GET:
  5428. case WASM_OP_ARRAY_GET_S:
  5429. case WASM_OP_ARRAY_GET_U:
  5430. case WASM_OP_ARRAY_SET:
  5431. skip_leb_uint32(p, p_end); /* typeidx */
  5432. break;
  5433. #if WASM_ENABLE_GC_BINARYEN != 0
  5434. case WASM_OP_ARRAY_COPY:
  5435. skip_leb_uint32(p, p_end); /* typeidx1 */
  5436. skip_leb_uint32(p, p_end); /* typeidx2 */
  5437. break;
  5438. #endif
  5439. case WASM_OP_ARRAY_LEN:
  5440. break;
  5441. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  5442. case WASM_OP_ARRAY_NEW_CANON_DATA:
  5443. case WASM_OP_ARRAY_NEW_CANON_ELEM:
  5444. skip_leb_uint32(p, p_end); /* typeidx */
  5445. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  5446. break;
  5447. case WASM_OP_I31_NEW:
  5448. case WASM_OP_I31_GET_S:
  5449. case WASM_OP_I31_GET_U:
  5450. break;
  5451. case WASM_OP_REF_TEST:
  5452. case WASM_OP_REF_CAST:
  5453. case WASM_OP_REF_TEST_NULLABLE:
  5454. case WASM_OP_REF_CAST_NULLABLE:
  5455. skip_leb_int32(p, p_end); /* heaptype */
  5456. break;
  5457. case WASM_OP_BR_ON_CAST:
  5458. case WASM_OP_BR_ON_CAST_FAIL:
  5459. case WASM_OP_BR_ON_CAST_NULLABLE:
  5460. case WASM_OP_BR_ON_CAST_FAIL_NULLABLE:
  5461. skip_leb_uint32(p, p_end); /* labelidx */
  5462. skip_leb_int32(p, p_end); /* heaptype */
  5463. break;
  5464. case WASM_OP_EXTERN_INTERNALIZE:
  5465. case WASM_OP_EXTERN_EXTERNALIZE:
  5466. break;
  5467. default:
  5468. return false;
  5469. }
  5470. break;
  5471. }
  5472. #endif /* end of WASM_ENABLE_GC != 0 */
  5473. case WASM_OP_MISC_PREFIX:
  5474. {
  5475. uint32 opcode1;
  5476. read_leb_uint32(p, p_end, opcode1);
  5477. switch (opcode1) {
  5478. case WASM_OP_I32_TRUNC_SAT_S_F32:
  5479. case WASM_OP_I32_TRUNC_SAT_U_F32:
  5480. case WASM_OP_I32_TRUNC_SAT_S_F64:
  5481. case WASM_OP_I32_TRUNC_SAT_U_F64:
  5482. case WASM_OP_I64_TRUNC_SAT_S_F32:
  5483. case WASM_OP_I64_TRUNC_SAT_U_F32:
  5484. case WASM_OP_I64_TRUNC_SAT_S_F64:
  5485. case WASM_OP_I64_TRUNC_SAT_U_F64:
  5486. break;
  5487. #if WASM_ENABLE_BULK_MEMORY != 0
  5488. case WASM_OP_MEMORY_INIT:
  5489. skip_leb_uint32(p, p_end);
  5490. /* skip memory idx */
  5491. p++;
  5492. break;
  5493. case WASM_OP_DATA_DROP:
  5494. skip_leb_uint32(p, p_end);
  5495. break;
  5496. case WASM_OP_MEMORY_COPY:
  5497. /* skip two memory idx */
  5498. p += 2;
  5499. break;
  5500. case WASM_OP_MEMORY_FILL:
  5501. /* skip memory idx */
  5502. p++;
  5503. break;
  5504. #endif /* WASM_ENABLE_BULK_MEMORY */
  5505. #if WASM_ENABLE_REF_TYPES != 0
  5506. case WASM_OP_TABLE_INIT:
  5507. case WASM_OP_TABLE_COPY:
  5508. /* tableidx */
  5509. skip_leb_uint32(p, p_end);
  5510. /* elemidx */
  5511. skip_leb_uint32(p, p_end);
  5512. break;
  5513. case WASM_OP_ELEM_DROP:
  5514. /* elemidx */
  5515. skip_leb_uint32(p, p_end);
  5516. break;
  5517. case WASM_OP_TABLE_SIZE:
  5518. case WASM_OP_TABLE_GROW:
  5519. case WASM_OP_TABLE_FILL:
  5520. skip_leb_uint32(p, p_end); /* table idx */
  5521. break;
  5522. #endif /* WASM_ENABLE_REF_TYPES */
  5523. default:
  5524. return false;
  5525. }
  5526. break;
  5527. }
  5528. #if WASM_ENABLE_SIMD != 0
  5529. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  5530. case WASM_OP_SIMD_PREFIX:
  5531. {
  5532. /* TODO: shall we ceate a table to be friendly to branch
  5533. * prediction */
  5534. opcode = read_uint8(p);
  5535. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  5536. */
  5537. switch (opcode) {
  5538. case SIMD_v128_load:
  5539. case SIMD_v128_load8x8_s:
  5540. case SIMD_v128_load8x8_u:
  5541. case SIMD_v128_load16x4_s:
  5542. case SIMD_v128_load16x4_u:
  5543. case SIMD_v128_load32x2_s:
  5544. case SIMD_v128_load32x2_u:
  5545. case SIMD_v128_load8_splat:
  5546. case SIMD_v128_load16_splat:
  5547. case SIMD_v128_load32_splat:
  5548. case SIMD_v128_load64_splat:
  5549. case SIMD_v128_store:
  5550. /* memarg align */
  5551. skip_leb_uint32(p, p_end);
  5552. /* memarg offset*/
  5553. skip_leb_uint32(p, p_end);
  5554. break;
  5555. case SIMD_v128_const:
  5556. case SIMD_v8x16_shuffle:
  5557. /* immByte[16] immLaneId[16] */
  5558. CHECK_BUF1(p, p_end, 16);
  5559. p += 16;
  5560. break;
  5561. case SIMD_i8x16_extract_lane_s:
  5562. case SIMD_i8x16_extract_lane_u:
  5563. case SIMD_i8x16_replace_lane:
  5564. case SIMD_i16x8_extract_lane_s:
  5565. case SIMD_i16x8_extract_lane_u:
  5566. case SIMD_i16x8_replace_lane:
  5567. case SIMD_i32x4_extract_lane:
  5568. case SIMD_i32x4_replace_lane:
  5569. case SIMD_i64x2_extract_lane:
  5570. case SIMD_i64x2_replace_lane:
  5571. case SIMD_f32x4_extract_lane:
  5572. case SIMD_f32x4_replace_lane:
  5573. case SIMD_f64x2_extract_lane:
  5574. case SIMD_f64x2_replace_lane:
  5575. /* ImmLaneId */
  5576. CHECK_BUF(p, p_end, 1);
  5577. p++;
  5578. break;
  5579. case SIMD_v128_load8_lane:
  5580. case SIMD_v128_load16_lane:
  5581. case SIMD_v128_load32_lane:
  5582. case SIMD_v128_load64_lane:
  5583. case SIMD_v128_store8_lane:
  5584. case SIMD_v128_store16_lane:
  5585. case SIMD_v128_store32_lane:
  5586. case SIMD_v128_store64_lane:
  5587. /* memarg align */
  5588. skip_leb_uint32(p, p_end);
  5589. /* memarg offset*/
  5590. skip_leb_uint32(p, p_end);
  5591. /* ImmLaneId */
  5592. CHECK_BUF(p, p_end, 1);
  5593. p++;
  5594. break;
  5595. case SIMD_v128_load32_zero:
  5596. case SIMD_v128_load64_zero:
  5597. /* memarg align */
  5598. skip_leb_uint32(p, p_end);
  5599. /* memarg offset*/
  5600. skip_leb_uint32(p, p_end);
  5601. break;
  5602. default:
  5603. /*
  5604. * since latest SIMD specific used almost every value
  5605. * from 0x00 to 0xff, the default branch will present
  5606. * all opcodes without imm
  5607. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  5608. */
  5609. break;
  5610. }
  5611. break;
  5612. }
  5613. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  5614. #endif /* end of WASM_ENABLE_SIMD */
  5615. #if WASM_ENABLE_SHARED_MEMORY != 0
  5616. case WASM_OP_ATOMIC_PREFIX:
  5617. {
  5618. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  5619. opcode = read_uint8(p);
  5620. if (opcode != WASM_OP_ATOMIC_FENCE) {
  5621. skip_leb_uint32(p, p_end); /* align */
  5622. skip_leb_uint32(p, p_end); /* offset */
  5623. }
  5624. else {
  5625. /* atomic.fence doesn't have memarg */
  5626. p++;
  5627. }
  5628. break;
  5629. }
  5630. #endif
  5631. #if WASM_ENABLE_DEBUG_INTERP != 0
  5632. case DEBUG_OP_BREAK:
  5633. {
  5634. WASMDebugInstance *debug_instance =
  5635. wasm_exec_env_get_instance(exec_env);
  5636. char orignal_opcode[1];
  5637. uint64 size = 1;
  5638. WASMModuleInstance *module_inst =
  5639. (WASMModuleInstance *)exec_env->module_inst;
  5640. uint64 offset = (p - 1) >= module_inst->module->load_addr
  5641. ? (p - 1) - module_inst->module->load_addr
  5642. : ~0;
  5643. if (debug_instance) {
  5644. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  5645. orignal_opcode, &size)
  5646. && size == 1) {
  5647. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  5648. "with %02x: ",
  5649. orignal_opcode[0]);
  5650. opcode = orignal_opcode[0];
  5651. goto op_break_retry;
  5652. }
  5653. }
  5654. break;
  5655. }
  5656. #endif
  5657. default:
  5658. return false;
  5659. }
  5660. }
  5661. (void)u8;
  5662. (void)exec_env;
  5663. return false;
  5664. fail:
  5665. return false;
  5666. }
  5667. #if WASM_ENABLE_FAST_INTERP != 0
  5668. #if WASM_DEBUG_PREPROCESSOR != 0
  5669. #define LOG_OP(...) os_printf(__VA_ARGS__)
  5670. #else
  5671. #define LOG_OP(...) (void)0
  5672. #endif
  5673. #define PATCH_ELSE 0
  5674. #define PATCH_END 1
  5675. typedef struct BranchBlockPatch {
  5676. struct BranchBlockPatch *next;
  5677. uint8 patch_type;
  5678. uint8 *code_compiled;
  5679. } BranchBlockPatch;
  5680. #endif
  5681. typedef struct BranchBlock {
  5682. uint8 label_type;
  5683. BlockType block_type;
  5684. uint8 *start_addr;
  5685. uint8 *else_addr;
  5686. uint8 *end_addr;
  5687. uint32 stack_cell_num;
  5688. #if WASM_ENABLE_GC != 0
  5689. uint32 reftype_map_num;
  5690. #endif
  5691. #if WASM_ENABLE_FAST_INTERP != 0
  5692. uint16 dynamic_offset;
  5693. uint8 *code_compiled;
  5694. BranchBlockPatch *patch_list;
  5695. /* This is used to save params frame_offset of of if block */
  5696. int16 *param_frame_offsets;
  5697. #endif
  5698. /* Indicate the operand stack is in polymorphic state.
  5699. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  5700. * to polymorphic state until the block's 'end' opcode is processed.
  5701. * If stack is in polymorphic state and stack is empty, instruction can
  5702. * pop any type of value directly without decreasing stack top pointer
  5703. * and stack cell num. */
  5704. bool is_stack_polymorphic;
  5705. } BranchBlock;
  5706. typedef struct WASMLoaderContext {
  5707. /* frame ref stack */
  5708. uint8 *frame_ref;
  5709. uint8 *frame_ref_bottom;
  5710. uint8 *frame_ref_boundary;
  5711. uint32 frame_ref_size;
  5712. uint32 stack_cell_num;
  5713. uint32 max_stack_cell_num;
  5714. #if WASM_ENABLE_GC != 0
  5715. /* frame reftype map stack */
  5716. WASMRefTypeMap *frame_reftype_map;
  5717. WASMRefTypeMap *frame_reftype_map_bottom;
  5718. WASMRefTypeMap *frame_reftype_map_boundary;
  5719. uint32 frame_reftype_map_size;
  5720. uint32 reftype_map_num;
  5721. uint32 max_reftype_map_num;
  5722. /* Current module */
  5723. WASMModule *module;
  5724. /* Current module's ref_type_set */
  5725. HashMap *ref_type_set;
  5726. /* Always point to local variable ref_type of
  5727. wasm_loader_prepare_bytecode */
  5728. WASMRefType *ref_type_tmp;
  5729. #endif
  5730. /* frame csp stack */
  5731. BranchBlock *frame_csp;
  5732. BranchBlock *frame_csp_bottom;
  5733. BranchBlock *frame_csp_boundary;
  5734. uint32 frame_csp_size;
  5735. uint32 csp_num;
  5736. uint32 max_csp_num;
  5737. #if WASM_ENABLE_FAST_INTERP != 0
  5738. /* frame offset stack */
  5739. int16 *frame_offset;
  5740. int16 *frame_offset_bottom;
  5741. int16 *frame_offset_boundary;
  5742. uint32 frame_offset_size;
  5743. int16 dynamic_offset;
  5744. int16 start_dynamic_offset;
  5745. int16 max_dynamic_offset;
  5746. /* preserved local offset */
  5747. int16 preserved_local_offset;
  5748. /* const buffer */
  5749. uint8 *const_buf;
  5750. uint16 num_const;
  5751. uint16 const_cell_num;
  5752. uint32 const_buf_size;
  5753. /* processed code */
  5754. uint8 *p_code_compiled;
  5755. uint8 *p_code_compiled_end;
  5756. uint32 code_compiled_size;
  5757. /* If the last opcode will be dropped, the peak memory usage will be larger
  5758. * than the final code_compiled_size, we record the peak size to ensure
  5759. * there will not be invalid memory access during second traverse */
  5760. uint32 code_compiled_peak_size;
  5761. #endif
  5762. } WASMLoaderContext;
  5763. typedef struct Const {
  5764. WASMValue value;
  5765. uint16 slot_index;
  5766. uint8 value_type;
  5767. } Const;
  5768. static void *
  5769. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  5770. uint32 error_buf_size)
  5771. {
  5772. uint8 *mem_new;
  5773. bh_assert(size_new > size_old);
  5774. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  5775. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  5776. memset(mem_new + size_old, 0, size_new - size_old);
  5777. wasm_runtime_free(mem_old);
  5778. }
  5779. return mem_new;
  5780. }
  5781. #define MEM_REALLOC(mem, size_old, size_new) \
  5782. do { \
  5783. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  5784. error_buf_size); \
  5785. if (!mem_new) \
  5786. goto fail; \
  5787. mem = mem_new; \
  5788. } while (0)
  5789. #define CHECK_CSP_PUSH() \
  5790. do { \
  5791. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  5792. MEM_REALLOC( \
  5793. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  5794. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  5795. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  5796. ctx->frame_csp_boundary = \
  5797. ctx->frame_csp_bottom \
  5798. + ctx->frame_csp_size / sizeof(BranchBlock); \
  5799. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  5800. } \
  5801. } while (0)
  5802. #define CHECK_CSP_POP() \
  5803. do { \
  5804. if (ctx->csp_num < 1) { \
  5805. set_error_buf(error_buf, error_buf_size, \
  5806. "type mismatch: " \
  5807. "expect data but block stack was empty"); \
  5808. goto fail; \
  5809. } \
  5810. } while (0)
  5811. #if WASM_ENABLE_FAST_INTERP != 0
  5812. static bool
  5813. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  5814. uint32 error_buf_size)
  5815. {
  5816. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  5817. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  5818. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  5819. ctx->frame_offset_size + 16);
  5820. ctx->frame_offset_size += 16;
  5821. ctx->frame_offset_boundary =
  5822. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  5823. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  5824. }
  5825. return true;
  5826. fail:
  5827. return false;
  5828. }
  5829. static bool
  5830. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  5831. {
  5832. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  5833. return false;
  5834. return true;
  5835. }
  5836. static void
  5837. free_label_patch_list(BranchBlock *frame_csp)
  5838. {
  5839. BranchBlockPatch *label_patch = frame_csp->patch_list;
  5840. BranchBlockPatch *next;
  5841. while (label_patch != NULL) {
  5842. next = label_patch->next;
  5843. wasm_runtime_free(label_patch);
  5844. label_patch = next;
  5845. }
  5846. frame_csp->patch_list = NULL;
  5847. }
  5848. static void
  5849. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  5850. {
  5851. BranchBlock *tmp_csp = frame_csp;
  5852. for (uint32 i = 0; i < csp_num; i++) {
  5853. free_label_patch_list(tmp_csp);
  5854. tmp_csp++;
  5855. }
  5856. }
  5857. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5858. static void
  5859. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  5860. {
  5861. if (ctx) {
  5862. if (ctx->frame_ref_bottom)
  5863. wasm_runtime_free(ctx->frame_ref_bottom);
  5864. #if WASM_ENABLE_GC != 0
  5865. if (ctx->frame_reftype_map_bottom)
  5866. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  5867. #endif
  5868. if (ctx->frame_csp_bottom) {
  5869. #if WASM_ENABLE_FAST_INTERP != 0
  5870. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  5871. #endif
  5872. wasm_runtime_free(ctx->frame_csp_bottom);
  5873. }
  5874. #if WASM_ENABLE_FAST_INTERP != 0
  5875. if (ctx->frame_offset_bottom)
  5876. wasm_runtime_free(ctx->frame_offset_bottom);
  5877. if (ctx->const_buf)
  5878. wasm_runtime_free(ctx->const_buf);
  5879. #endif
  5880. wasm_runtime_free(ctx);
  5881. }
  5882. }
  5883. static WASMLoaderContext *
  5884. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  5885. {
  5886. WASMLoaderContext *loader_ctx =
  5887. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  5888. if (!loader_ctx)
  5889. return NULL;
  5890. loader_ctx->frame_ref_size = 32;
  5891. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  5892. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  5893. goto fail;
  5894. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  5895. #if WASM_ENABLE_GC != 0
  5896. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  5897. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  5898. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  5899. error_buf_size)))
  5900. goto fail;
  5901. loader_ctx->frame_reftype_map_boundary =
  5902. loader_ctx->frame_reftype_map_bottom + 16;
  5903. #endif
  5904. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  5905. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  5906. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  5907. goto fail;
  5908. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  5909. #if WASM_ENABLE_FAST_INTERP != 0
  5910. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  5911. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  5912. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  5913. error_buf_size)))
  5914. goto fail;
  5915. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  5916. loader_ctx->num_const = 0;
  5917. loader_ctx->const_buf_size = sizeof(Const) * 8;
  5918. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  5919. error_buf, error_buf_size)))
  5920. goto fail;
  5921. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  5922. set_error_buf(error_buf, error_buf_size,
  5923. "fast interpreter offset overflow");
  5924. goto fail;
  5925. }
  5926. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  5927. loader_ctx->max_dynamic_offset =
  5928. func->param_cell_num + func->local_cell_num;
  5929. #endif
  5930. return loader_ctx;
  5931. fail:
  5932. wasm_loader_ctx_destroy(loader_ctx);
  5933. return NULL;
  5934. }
  5935. static bool
  5936. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  5937. uint32 error_buf_size)
  5938. {
  5939. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  5940. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  5941. /* Increase the frame ref stack */
  5942. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  5943. ctx->frame_ref_size + 16);
  5944. ctx->frame_ref_size += 16;
  5945. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  5946. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  5947. }
  5948. #if WASM_ENABLE_GC != 0
  5949. if (wasm_is_type_multi_byte_type(type)
  5950. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  5951. /* Increase the frame reftype map stack */
  5952. bh_assert(
  5953. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  5954. * sizeof(WASMRefTypeMap))
  5955. == ctx->frame_reftype_map_size);
  5956. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  5957. ctx->frame_reftype_map_size
  5958. + (uint32)sizeof(WASMRefTypeMap) * 8);
  5959. ctx->frame_reftype_map =
  5960. ctx->frame_reftype_map_bottom
  5961. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  5962. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  5963. ctx->frame_reftype_map_boundary =
  5964. ctx->frame_reftype_map_bottom
  5965. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  5966. }
  5967. #endif
  5968. return true;
  5969. fail:
  5970. return false;
  5971. }
  5972. static bool
  5973. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  5974. uint32 error_buf_size)
  5975. {
  5976. uint32 type_cell_num = wasm_value_type_cell_num(type);
  5977. uint32 i;
  5978. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  5979. return false;
  5980. #if WASM_ENABLE_GC != 0
  5981. if (wasm_is_type_multi_byte_type(type)) {
  5982. WASMRefType *ref_type;
  5983. if (!(ref_type =
  5984. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  5985. error_buf, error_buf_size))) {
  5986. return false;
  5987. }
  5988. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  5989. /* Increase the frame reftype map stack */
  5990. bh_assert((uint32)((ctx->frame_reftype_map
  5991. - ctx->frame_reftype_map_bottom)
  5992. * sizeof(WASMRefTypeMap))
  5993. == ctx->frame_reftype_map_size);
  5994. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  5995. ctx->frame_reftype_map_size,
  5996. ctx->frame_reftype_map_size
  5997. + (uint32)sizeof(WASMRefTypeMap) * 8);
  5998. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  5999. + ctx->frame_reftype_map_size
  6000. / ((uint32)sizeof(WASMRefTypeMap));
  6001. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  6002. ctx->frame_reftype_map_boundary =
  6003. ctx->frame_reftype_map_bottom
  6004. + ctx->frame_reftype_map_size
  6005. / ((uint32)sizeof(WASMRefTypeMap));
  6006. }
  6007. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  6008. ctx->frame_reftype_map->ref_type = ref_type;
  6009. ctx->frame_reftype_map++;
  6010. ctx->reftype_map_num++;
  6011. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  6012. ctx->max_reftype_map_num = ctx->reftype_map_num;
  6013. }
  6014. #endif
  6015. for (i = 0; i < type_cell_num; i++)
  6016. *ctx->frame_ref++ = type;
  6017. ctx->stack_cell_num += type_cell_num;
  6018. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  6019. ctx->max_stack_cell_num = ctx->stack_cell_num;
  6020. if (ctx->max_stack_cell_num > UINT16_MAX) {
  6021. set_error_buf(error_buf, error_buf_size,
  6022. "operand stack depth limit exceeded");
  6023. return false;
  6024. }
  6025. }
  6026. return true;
  6027. #if WASM_ENABLE_GC != 0
  6028. fail:
  6029. return false;
  6030. #endif
  6031. }
  6032. static bool
  6033. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  6034. int32 stack_cell_num,
  6035. #if WASM_ENABLE_GC != 0
  6036. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  6037. #endif
  6038. uint8 type,
  6039. #if WASM_ENABLE_GC != 0
  6040. WASMRefType *ref_type,
  6041. #endif
  6042. char *error_buf, uint32 error_buf_size)
  6043. {
  6044. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  6045. #if WASM_ENABLE_GC != 0
  6046. WASMRefType *frame_reftype = NULL;
  6047. #endif
  6048. if (stack_cell_num < type_cell_num) {
  6049. set_error_buf(error_buf, error_buf_size,
  6050. "type mismatch: expect data but stack was empty");
  6051. return false;
  6052. }
  6053. #if WASM_ENABLE_GC == 0
  6054. for (i = 0; i < type_cell_num; i++) {
  6055. if (*(frame_ref - 1 - i) != type) {
  6056. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6057. "type mismatch: expect ", type2str(type),
  6058. " but got other");
  6059. return false;
  6060. }
  6061. }
  6062. #else
  6063. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  6064. bh_assert(reftype_map_num > 0);
  6065. frame_reftype = (frame_reftype_map - 1)->ref_type;
  6066. }
  6067. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  6068. ref_type, ctx->module->types,
  6069. ctx->module->type_count)) {
  6070. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6071. "type mismatch: expect ", type2str(type),
  6072. " but got other");
  6073. return false;
  6074. }
  6075. for (i = 0; i < type_cell_num - 1; i++) {
  6076. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  6077. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6078. "type mismatch: expect ", type2str(type),
  6079. " but got other");
  6080. return false;
  6081. }
  6082. }
  6083. #endif
  6084. return true;
  6085. }
  6086. static bool
  6087. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  6088. uint32 error_buf_size)
  6089. {
  6090. int32 block_stack_cell_num =
  6091. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  6092. #if WASM_ENABLE_GC != 0
  6093. int32 reftype_map_num =
  6094. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  6095. #endif
  6096. if (block_stack_cell_num > 0) {
  6097. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  6098. /* the stack top is a value of any type, return success */
  6099. return true;
  6100. }
  6101. #if WASM_ENABLE_GC != 0
  6102. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  6103. uint8 stack_top_type = *(ctx->frame_ref - 1);
  6104. WASMRefType *stack_top_ref_type = NULL;
  6105. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  6106. bh_assert(reftype_map_num > 0);
  6107. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  6108. }
  6109. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  6110. ctx->ref_type_tmp, ctx->module->types,
  6111. ctx->module->type_count)) {
  6112. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  6113. uint32 ref_type_struct_size =
  6114. wasm_reftype_struct_size(stack_top_ref_type);
  6115. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  6116. stack_top_ref_type, ref_type_struct_size);
  6117. }
  6118. return true;
  6119. }
  6120. }
  6121. #endif
  6122. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  6123. #if WASM_ENABLE_GC != 0
  6124. ctx->frame_reftype_map, reftype_map_num,
  6125. #endif
  6126. type,
  6127. #if WASM_ENABLE_GC != 0
  6128. ctx->ref_type_tmp,
  6129. #endif
  6130. error_buf, error_buf_size)) {
  6131. return false;
  6132. }
  6133. return true;
  6134. }
  6135. static bool
  6136. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  6137. uint32 error_buf_size)
  6138. {
  6139. BranchBlock *cur_block = ctx->frame_csp - 1;
  6140. int32 available_stack_cell =
  6141. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  6142. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  6143. /* Directly return success if current block is in stack
  6144. polymorphic state while stack is empty. */
  6145. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  6146. return true;
  6147. if (type == VALUE_TYPE_VOID)
  6148. return true;
  6149. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  6150. return false;
  6151. bh_assert(available_stack_cell > 0);
  6152. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  6153. type = VALUE_TYPE_ANY;
  6154. cell_num_to_pop = 1;
  6155. }
  6156. ctx->frame_ref -= cell_num_to_pop;
  6157. ctx->stack_cell_num -= cell_num_to_pop;
  6158. #if WASM_ENABLE_GC != 0
  6159. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  6160. ctx->frame_reftype_map--;
  6161. ctx->reftype_map_num--;
  6162. }
  6163. #endif
  6164. return true;
  6165. }
  6166. #if WASM_ENABLE_GC != 0
  6167. /* Get the stack top element of current block */
  6168. static bool
  6169. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  6170. WASMRefType **p_ref_type, char *error_buf,
  6171. uint32 error_buf_size)
  6172. {
  6173. BranchBlock *cur_block = ctx->frame_csp - 1;
  6174. int32 available_stack_cell =
  6175. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  6176. if (available_stack_cell <= 0) {
  6177. /* Directly return success if current block is in stack
  6178. polymorphic state while stack is empty. */
  6179. if (cur_block->is_stack_polymorphic) {
  6180. *p_type = VALUE_TYPE_ANY;
  6181. return true;
  6182. }
  6183. else {
  6184. set_error_buf(
  6185. error_buf, error_buf_size,
  6186. "type mismatch: expect data but block stack was empty");
  6187. return false;
  6188. }
  6189. }
  6190. *p_type = *(ctx->frame_ref - 1);
  6191. if (wasm_is_type_multi_byte_type(*p_type)) {
  6192. int32 available_reftype_map =
  6193. (int32)(ctx->reftype_map_num
  6194. - (ctx->frame_csp - 1)->reftype_map_num);
  6195. bh_assert(available_reftype_map > 0);
  6196. (void)available_reftype_map;
  6197. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  6198. }
  6199. return true;
  6200. }
  6201. #if WASM_ENABLE_FAST_INTERP != 0
  6202. static bool
  6203. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  6204. char *error_buf, uint32 error_buf_size);
  6205. #endif
  6206. /* Check whether the stack top elem is a heap object, and if yes,
  6207. pop and return it */
  6208. static bool
  6209. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  6210. WASMRefType *ref_ht_ret, char *error_buf,
  6211. uint32 error_buf_size)
  6212. {
  6213. uint8 type = 0;
  6214. WASMRefType *ref_type = NULL;
  6215. /* Get stack top element */
  6216. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  6217. error_buf_size)) {
  6218. return false;
  6219. }
  6220. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  6221. /* stack top isn't a ref type */
  6222. && !wasm_is_type_reftype(type)) {
  6223. set_error_buf(error_buf, error_buf_size,
  6224. "type mismatch: expect heap object but got others");
  6225. return false;
  6226. }
  6227. /* POP stack top */
  6228. if (wasm_is_type_multi_byte_type(type)) {
  6229. bh_assert(ref_type);
  6230. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  6231. wasm_reftype_struct_size(ref_type));
  6232. }
  6233. #if WASM_ENABLE_FAST_INTERP != 0
  6234. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  6235. error_buf_size)) {
  6236. return false;
  6237. }
  6238. #else
  6239. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  6240. return false;
  6241. }
  6242. #endif
  6243. if (p_type)
  6244. *p_type = type;
  6245. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  6246. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  6247. wasm_reftype_struct_size(ref_type));
  6248. }
  6249. return true;
  6250. }
  6251. /* Check whether the stack top elem is subtype of (ref null ht),
  6252. and if yes, pop it and return the converted (ref ht) */
  6253. static bool
  6254. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  6255. WASMRefType *ref_ht_ret, char *error_buf,
  6256. uint32 error_buf_size)
  6257. {
  6258. uint8 type = 0;
  6259. WASMRefType ref_type = { 0 };
  6260. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  6261. error_buf_size)) {
  6262. return false;
  6263. }
  6264. /* Convert to related (ref ht) and return */
  6265. if ((type >= REF_TYPE_EQREF && type <= REF_TYPE_FUNCREF)
  6266. || (type >= REF_TYPE_NULLREF && type <= REF_TYPE_I31REF)) {
  6267. /* Return (ref func/extern/any/eq/i31/nofunc/noextern/struct/array/none)
  6268. */
  6269. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  6270. HEAP_TYPE_FUNC + (type - REF_TYPE_FUNCREF));
  6271. type = ref_ht_ret->ref_type;
  6272. }
  6273. else if (wasm_is_reftype_htref_nullable(type)
  6274. || wasm_is_reftype_htref_non_nullable(type)) {
  6275. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  6276. wasm_reftype_struct_size(&ref_type));
  6277. /* Convert to (ref ht) */
  6278. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  6279. ref_ht_ret->ref_ht_common.nullable = false;
  6280. type = ref_ht_ret->ref_type;
  6281. }
  6282. *p_type = type;
  6283. return true;
  6284. }
  6285. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  6286. and if yes, pop it and return the type_idx */
  6287. static bool
  6288. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  6289. uint32 *p_type_idx, char *error_buf,
  6290. uint32 error_buf_size)
  6291. {
  6292. uint8 type = 0;
  6293. int32 type_idx = -1;
  6294. WASMRefType *ref_type = NULL;
  6295. /* Get stack top element */
  6296. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  6297. error_buf_size)) {
  6298. return false;
  6299. }
  6300. if (type != VALUE_TYPE_ANY) {
  6301. /* stack top isn't (ref null $t) */
  6302. if (!((wasm_is_reftype_htref_nullable(type)
  6303. || wasm_is_reftype_htref_non_nullable(type))
  6304. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  6305. set_error_buf(error_buf, error_buf_size,
  6306. "type mismatch: expect (ref null $t) but got others");
  6307. return false;
  6308. }
  6309. type_idx = ref_type->ref_ht_typeidx.type_idx;
  6310. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  6311. wasm_reftype_struct_size(ref_type));
  6312. }
  6313. /* POP stack top */
  6314. #if WASM_ENABLE_FAST_INTERP != 0
  6315. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  6316. error_buf_size)) {
  6317. return false;
  6318. }
  6319. #else
  6320. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  6321. return false;
  6322. }
  6323. #endif
  6324. /* Convert to type_idx and return */
  6325. *p_type = type;
  6326. if (type != VALUE_TYPE_ANY)
  6327. *p_type_idx = (uint32)type_idx;
  6328. return true;
  6329. }
  6330. #endif /* WASM_ENABLE_GC != 0 */
  6331. static bool
  6332. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  6333. uint8 type_push, uint8 type_pop, char *error_buf,
  6334. uint32 error_buf_size)
  6335. {
  6336. for (int i = 0; i < pop_cnt; i++) {
  6337. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  6338. error_buf_size))
  6339. return false;
  6340. }
  6341. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  6342. return false;
  6343. return true;
  6344. }
  6345. static bool
  6346. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  6347. BlockType block_type, uint8 *start_addr,
  6348. char *error_buf, uint32 error_buf_size)
  6349. {
  6350. CHECK_CSP_PUSH();
  6351. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  6352. ctx->frame_csp->label_type = label_type;
  6353. ctx->frame_csp->block_type = block_type;
  6354. ctx->frame_csp->start_addr = start_addr;
  6355. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  6356. #if WASM_ENABLE_GC != 0
  6357. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  6358. #endif
  6359. #if WASM_ENABLE_FAST_INTERP != 0
  6360. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  6361. ctx->frame_csp->patch_list = NULL;
  6362. #endif
  6363. ctx->frame_csp++;
  6364. ctx->csp_num++;
  6365. if (ctx->csp_num > ctx->max_csp_num) {
  6366. ctx->max_csp_num = ctx->csp_num;
  6367. if (ctx->max_csp_num > UINT16_MAX) {
  6368. set_error_buf(error_buf, error_buf_size,
  6369. "label stack depth limit exceeded");
  6370. return false;
  6371. }
  6372. }
  6373. return true;
  6374. fail:
  6375. return false;
  6376. }
  6377. static bool
  6378. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  6379. uint32 error_buf_size)
  6380. {
  6381. CHECK_CSP_POP();
  6382. #if WASM_ENABLE_FAST_INTERP != 0
  6383. if ((ctx->frame_csp - 1)->param_frame_offsets)
  6384. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  6385. #endif
  6386. ctx->frame_csp--;
  6387. ctx->csp_num--;
  6388. return true;
  6389. fail:
  6390. return false;
  6391. }
  6392. #if WASM_ENABLE_FAST_INTERP != 0
  6393. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6394. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6395. #define emit_label(opcode) \
  6396. do { \
  6397. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  6398. LOG_OP("\nemit_op [%02x]\t", opcode); \
  6399. } while (0)
  6400. #define skip_label() \
  6401. do { \
  6402. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  6403. LOG_OP("\ndelete last op\n"); \
  6404. } while (0)
  6405. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6406. #define emit_label(opcode) \
  6407. do { \
  6408. int32 offset = \
  6409. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  6410. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  6411. set_error_buf(error_buf, error_buf_size, \
  6412. "pre-compiled label offset out of range"); \
  6413. goto fail; \
  6414. } \
  6415. wasm_loader_emit_int16(loader_ctx, offset); \
  6416. LOG_OP("\nemit_op [%02x]\t", opcode); \
  6417. } while (0)
  6418. #define skip_label() \
  6419. do { \
  6420. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  6421. LOG_OP("\ndelete last op\n"); \
  6422. } while (0)
  6423. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6424. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6425. #define emit_label(opcode) \
  6426. do { \
  6427. wasm_loader_emit_uint8(loader_ctx, opcode); \
  6428. LOG_OP("\nemit_op [%02x]\t", opcode); \
  6429. } while (0)
  6430. #define skip_label() \
  6431. do { \
  6432. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  6433. LOG_OP("\ndelete last op\n"); \
  6434. } while (0)
  6435. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6436. #define emit_empty_label_addr_and_frame_ip(type) \
  6437. do { \
  6438. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  6439. loader_ctx->p_code_compiled, error_buf, \
  6440. error_buf_size)) \
  6441. goto fail; \
  6442. /* label address, to be patched */ \
  6443. wasm_loader_emit_ptr(loader_ctx, NULL); \
  6444. } while (0)
  6445. #define emit_br_info(frame_csp) \
  6446. do { \
  6447. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  6448. error_buf_size)) \
  6449. goto fail; \
  6450. } while (0)
  6451. #define LAST_OP_OUTPUT_I32() \
  6452. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  6453. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  6454. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  6455. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  6456. || (last_op >= WASM_OP_I32_WRAP_I64 \
  6457. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  6458. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  6459. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  6460. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  6461. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  6462. || (last_op == EXT_OP_COPY_STACK_TOP)
  6463. #define LAST_OP_OUTPUT_I64() \
  6464. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  6465. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  6466. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  6467. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  6468. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  6469. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  6470. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  6471. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  6472. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  6473. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  6474. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  6475. #define GET_CONST_OFFSET(type, val) \
  6476. do { \
  6477. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  6478. &operand_offset, error_buf, \
  6479. error_buf_size))) \
  6480. goto fail; \
  6481. } while (0)
  6482. #define GET_CONST_F32_OFFSET(type, fval) \
  6483. do { \
  6484. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  6485. &operand_offset, error_buf, \
  6486. error_buf_size))) \
  6487. goto fail; \
  6488. } while (0)
  6489. #define GET_CONST_F64_OFFSET(type, fval) \
  6490. do { \
  6491. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  6492. &operand_offset, error_buf, \
  6493. error_buf_size))) \
  6494. goto fail; \
  6495. } while (0)
  6496. #define emit_operand(ctx, offset) \
  6497. do { \
  6498. wasm_loader_emit_int16(ctx, offset); \
  6499. LOG_OP("%d\t", offset); \
  6500. } while (0)
  6501. #define emit_byte(ctx, byte) \
  6502. do { \
  6503. wasm_loader_emit_uint8(ctx, byte); \
  6504. LOG_OP("%d\t", byte); \
  6505. } while (0)
  6506. #define emit_uint32(ctx, value) \
  6507. do { \
  6508. wasm_loader_emit_uint32(ctx, value); \
  6509. LOG_OP("%d\t", value); \
  6510. } while (0)
  6511. #define emit_uint64(ctx, value) \
  6512. do { \
  6513. wasm_loader_emit_const(ctx, &value, false); \
  6514. LOG_OP("%lld\t", value); \
  6515. } while (0)
  6516. #define emit_float32(ctx, value) \
  6517. do { \
  6518. wasm_loader_emit_const(ctx, &value, true); \
  6519. LOG_OP("%f\t", value); \
  6520. } while (0)
  6521. #define emit_float64(ctx, value) \
  6522. do { \
  6523. wasm_loader_emit_const(ctx, &value, false); \
  6524. LOG_OP("%f\t", value); \
  6525. } while (0)
  6526. static bool
  6527. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  6528. {
  6529. if (!(ctx->p_code_compiled =
  6530. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  6531. return false;
  6532. ctx->p_code_compiled_end =
  6533. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  6534. /* clean up frame ref */
  6535. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  6536. ctx->frame_ref = ctx->frame_ref_bottom;
  6537. ctx->stack_cell_num = 0;
  6538. #if WASM_ENABLE_GC != 0
  6539. /* clean up reftype map */
  6540. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  6541. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  6542. ctx->reftype_map_num = 0;
  6543. #endif
  6544. /* clean up frame csp */
  6545. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  6546. ctx->frame_csp = ctx->frame_csp_bottom;
  6547. ctx->csp_num = 0;
  6548. ctx->max_csp_num = 0;
  6549. /* clean up frame offset */
  6550. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  6551. ctx->frame_offset = ctx->frame_offset_bottom;
  6552. ctx->dynamic_offset = ctx->start_dynamic_offset;
  6553. /* init preserved local offsets */
  6554. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  6555. /* const buf is reserved */
  6556. return true;
  6557. }
  6558. static void
  6559. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  6560. {
  6561. ctx->code_compiled_size += size;
  6562. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  6563. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  6564. }
  6565. }
  6566. static void
  6567. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  6568. {
  6569. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  6570. if (ctx->p_code_compiled) {
  6571. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6572. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6573. #endif
  6574. bh_memcpy_s(ctx->p_code_compiled,
  6575. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  6576. value, size);
  6577. ctx->p_code_compiled += size;
  6578. }
  6579. else {
  6580. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6581. bh_assert((ctx->code_compiled_size & 1) == 0);
  6582. #endif
  6583. increase_compiled_code_space(ctx, size);
  6584. }
  6585. }
  6586. static void
  6587. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  6588. {
  6589. if (ctx->p_code_compiled) {
  6590. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6591. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6592. #endif
  6593. STORE_U32(ctx->p_code_compiled, value);
  6594. ctx->p_code_compiled += sizeof(uint32);
  6595. }
  6596. else {
  6597. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6598. bh_assert((ctx->code_compiled_size & 1) == 0);
  6599. #endif
  6600. increase_compiled_code_space(ctx, sizeof(uint32));
  6601. }
  6602. }
  6603. static void
  6604. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  6605. {
  6606. if (ctx->p_code_compiled) {
  6607. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6608. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6609. #endif
  6610. STORE_U16(ctx->p_code_compiled, (uint16)value);
  6611. ctx->p_code_compiled += sizeof(int16);
  6612. }
  6613. else {
  6614. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6615. bh_assert((ctx->code_compiled_size & 1) == 0);
  6616. #endif
  6617. increase_compiled_code_space(ctx, sizeof(uint16));
  6618. }
  6619. }
  6620. static void
  6621. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  6622. {
  6623. if (ctx->p_code_compiled) {
  6624. *(ctx->p_code_compiled) = value;
  6625. ctx->p_code_compiled += sizeof(uint8);
  6626. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6627. ctx->p_code_compiled++;
  6628. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6629. #endif
  6630. }
  6631. else {
  6632. increase_compiled_code_space(ctx, sizeof(uint8));
  6633. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6634. increase_compiled_code_space(ctx, sizeof(uint8));
  6635. bh_assert((ctx->code_compiled_size & 1) == 0);
  6636. #endif
  6637. }
  6638. }
  6639. static void
  6640. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  6641. {
  6642. if (ctx->p_code_compiled) {
  6643. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6644. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6645. #endif
  6646. STORE_PTR(ctx->p_code_compiled, value);
  6647. ctx->p_code_compiled += sizeof(void *);
  6648. }
  6649. else {
  6650. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6651. bh_assert((ctx->code_compiled_size & 1) == 0);
  6652. #endif
  6653. increase_compiled_code_space(ctx, sizeof(void *));
  6654. }
  6655. }
  6656. static void
  6657. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  6658. {
  6659. if (ctx->p_code_compiled) {
  6660. ctx->p_code_compiled -= size;
  6661. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6662. if (size == sizeof(uint8)) {
  6663. ctx->p_code_compiled--;
  6664. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  6665. }
  6666. #endif
  6667. }
  6668. else {
  6669. ctx->code_compiled_size -= size;
  6670. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6671. if (size == sizeof(uint8)) {
  6672. ctx->code_compiled_size--;
  6673. bh_assert((ctx->code_compiled_size & 1) == 0);
  6674. }
  6675. #endif
  6676. }
  6677. }
  6678. static bool
  6679. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  6680. uint32 local_index, uint32 local_type,
  6681. bool *preserved, char *error_buf,
  6682. uint32 error_buf_size)
  6683. {
  6684. uint32 i = 0;
  6685. int16 preserved_offset = (int16)local_index;
  6686. *preserved = false;
  6687. while (i < loader_ctx->stack_cell_num) {
  6688. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  6689. /* move previous local into dynamic space before a set/tee_local opcode
  6690. */
  6691. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  6692. if (!(*preserved)) {
  6693. *preserved = true;
  6694. skip_label();
  6695. preserved_offset = loader_ctx->preserved_local_offset;
  6696. if (loader_ctx->p_code_compiled) {
  6697. bh_assert(preserved_offset != (int16)local_index);
  6698. }
  6699. if (is_32bit_type(local_type)) {
  6700. /* Only increase preserve offset in the second traversal */
  6701. if (loader_ctx->p_code_compiled)
  6702. loader_ctx->preserved_local_offset++;
  6703. emit_label(EXT_OP_COPY_STACK_TOP);
  6704. }
  6705. else {
  6706. if (loader_ctx->p_code_compiled)
  6707. loader_ctx->preserved_local_offset += 2;
  6708. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  6709. }
  6710. emit_operand(loader_ctx, local_index);
  6711. emit_operand(loader_ctx, preserved_offset);
  6712. emit_label(opcode);
  6713. }
  6714. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  6715. }
  6716. if (is_32bit_type(cur_type))
  6717. i++;
  6718. else
  6719. i += 2;
  6720. }
  6721. (void)error_buf;
  6722. (void)error_buf_size;
  6723. return true;
  6724. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6725. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  6726. fail:
  6727. return false;
  6728. #endif
  6729. #endif
  6730. }
  6731. static bool
  6732. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  6733. char *error_buf, uint32 error_buf_size)
  6734. {
  6735. uint32 i = 0;
  6736. bool preserve_local;
  6737. /* preserve locals before blocks to ensure that "tee/set_local" inside
  6738. blocks will not influence the value of these locals */
  6739. while (i < loader_ctx->stack_cell_num) {
  6740. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  6741. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  6742. if ((cur_offset < loader_ctx->start_dynamic_offset)
  6743. && (cur_offset >= 0)) {
  6744. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  6745. cur_type, &preserve_local,
  6746. error_buf, error_buf_size)))
  6747. return false;
  6748. }
  6749. if (is_32bit_type(cur_type)) {
  6750. i++;
  6751. }
  6752. else {
  6753. i += 2;
  6754. }
  6755. }
  6756. return true;
  6757. }
  6758. static bool
  6759. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  6760. uint8 *p_code_compiled, char *error_buf,
  6761. uint32 error_buf_size)
  6762. {
  6763. BranchBlockPatch *patch =
  6764. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  6765. if (!patch) {
  6766. return false;
  6767. }
  6768. patch->patch_type = patch_type;
  6769. patch->code_compiled = p_code_compiled;
  6770. if (!frame_csp->patch_list) {
  6771. frame_csp->patch_list = patch;
  6772. patch->next = NULL;
  6773. }
  6774. else {
  6775. patch->next = frame_csp->patch_list;
  6776. frame_csp->patch_list = patch;
  6777. }
  6778. return true;
  6779. }
  6780. static void
  6781. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  6782. {
  6783. BranchBlock *frame_csp = ctx->frame_csp - depth;
  6784. BranchBlockPatch *node = frame_csp->patch_list;
  6785. BranchBlockPatch *node_prev = NULL, *node_next;
  6786. if (!ctx->p_code_compiled)
  6787. return;
  6788. while (node) {
  6789. node_next = node->next;
  6790. if (node->patch_type == patch_type) {
  6791. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  6792. if (node_prev == NULL) {
  6793. frame_csp->patch_list = node_next;
  6794. }
  6795. else {
  6796. node_prev->next = node_next;
  6797. }
  6798. wasm_runtime_free(node);
  6799. }
  6800. else {
  6801. node_prev = node;
  6802. }
  6803. node = node_next;
  6804. }
  6805. }
  6806. static bool
  6807. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  6808. char *error_buf, uint32 error_buf_size)
  6809. {
  6810. /* br info layout:
  6811. * a) arity of target block
  6812. * b) total cell num of arity values
  6813. * c) each arity value's cell num
  6814. * d) each arity value's src frame offset
  6815. * e) each arity values's dst dynamic offset
  6816. * f) branch target address
  6817. *
  6818. * Note: b-e are omitted when arity is 0 so that
  6819. * interpreter can recover the br info quickly.
  6820. */
  6821. BlockType *block_type = &frame_csp->block_type;
  6822. uint8 *types = NULL, cell;
  6823. #if WASM_ENABLE_GC != 0
  6824. WASMRefTypeMap *reftype_maps;
  6825. uint32 reftype_map_count;
  6826. #endif
  6827. uint32 arity = 0;
  6828. int32 i;
  6829. int16 *frame_offset = ctx->frame_offset;
  6830. uint16 dynamic_offset;
  6831. /* Note: loop's arity is different from if and block. loop's arity is
  6832. * its parameter count while if and block arity is result count.
  6833. */
  6834. #if WASM_ENABLE_GC == 0
  6835. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  6836. arity = block_type_get_param_types(block_type, &types);
  6837. else
  6838. arity = block_type_get_result_types(block_type, &types);
  6839. #else
  6840. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  6841. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  6842. &reftype_map_count);
  6843. else
  6844. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  6845. &reftype_map_count);
  6846. #endif
  6847. /* Part a */
  6848. emit_uint32(ctx, arity);
  6849. if (arity) {
  6850. /* Part b */
  6851. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  6852. /* Part c */
  6853. for (i = (int32)arity - 1; i >= 0; i--) {
  6854. cell = (uint8)wasm_value_type_cell_num(types[i]);
  6855. emit_byte(ctx, cell);
  6856. }
  6857. /* Part d */
  6858. for (i = (int32)arity - 1; i >= 0; i--) {
  6859. cell = (uint8)wasm_value_type_cell_num(types[i]);
  6860. frame_offset -= cell;
  6861. emit_operand(ctx, *(int16 *)(frame_offset));
  6862. }
  6863. /* Part e */
  6864. dynamic_offset =
  6865. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  6866. for (i = (int32)arity - 1; i >= 0; i--) {
  6867. cell = (uint8)wasm_value_type_cell_num(types[i]);
  6868. dynamic_offset -= cell;
  6869. emit_operand(ctx, dynamic_offset);
  6870. }
  6871. }
  6872. /* Part f */
  6873. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  6874. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  6875. }
  6876. else {
  6877. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  6878. error_buf, error_buf_size))
  6879. return false;
  6880. /* label address, to be patched */
  6881. wasm_loader_emit_ptr(ctx, NULL);
  6882. }
  6883. return true;
  6884. }
  6885. static bool
  6886. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  6887. bool disable_emit, int16 operand_offset,
  6888. char *error_buf, uint32 error_buf_size)
  6889. {
  6890. if (type == VALUE_TYPE_VOID)
  6891. return true;
  6892. /* only check memory overflow in first traverse */
  6893. if (ctx->p_code_compiled == NULL) {
  6894. if (!check_offset_push(ctx, error_buf, error_buf_size))
  6895. return false;
  6896. }
  6897. if (disable_emit)
  6898. *(ctx->frame_offset)++ = operand_offset;
  6899. else {
  6900. emit_operand(ctx, ctx->dynamic_offset);
  6901. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  6902. ctx->dynamic_offset++;
  6903. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  6904. ctx->max_dynamic_offset = ctx->dynamic_offset;
  6905. if (ctx->max_dynamic_offset >= INT16_MAX) {
  6906. goto fail;
  6907. }
  6908. }
  6909. }
  6910. if (is_32bit_type(type))
  6911. return true;
  6912. if (ctx->p_code_compiled == NULL) {
  6913. if (!check_offset_push(ctx, error_buf, error_buf_size))
  6914. return false;
  6915. }
  6916. ctx->frame_offset++;
  6917. if (!disable_emit) {
  6918. ctx->dynamic_offset++;
  6919. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  6920. ctx->max_dynamic_offset = ctx->dynamic_offset;
  6921. if (ctx->max_dynamic_offset >= INT16_MAX) {
  6922. goto fail;
  6923. }
  6924. }
  6925. }
  6926. return true;
  6927. fail:
  6928. set_error_buf(error_buf, error_buf_size,
  6929. "fast interpreter offset overflow");
  6930. return false;
  6931. }
  6932. /* This function should be in front of wasm_loader_pop_frame_ref
  6933. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  6934. will be modified by wasm_loader_pop_frame_ref */
  6935. static bool
  6936. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  6937. char *error_buf, uint32 error_buf_size)
  6938. {
  6939. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  6940. then current block is the function block */
  6941. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  6942. BranchBlock *cur_block = ctx->frame_csp - depth;
  6943. int32 available_stack_cell =
  6944. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  6945. /* Directly return success if current block is in stack
  6946. * polymorphic state while stack is empty. */
  6947. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  6948. return true;
  6949. if (type == VALUE_TYPE_VOID)
  6950. return true;
  6951. if (is_32bit_type(type)) {
  6952. /* Check the offset stack bottom to ensure the frame offset
  6953. stack will not go underflow. But we don't thrown error
  6954. and return true here, because the error msg should be
  6955. given in wasm_loader_pop_frame_ref */
  6956. if (!check_offset_pop(ctx, 1))
  6957. return true;
  6958. ctx->frame_offset -= 1;
  6959. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  6960. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  6961. ctx->dynamic_offset -= 1;
  6962. }
  6963. else {
  6964. if (!check_offset_pop(ctx, 2))
  6965. return true;
  6966. ctx->frame_offset -= 2;
  6967. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  6968. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  6969. ctx->dynamic_offset -= 2;
  6970. }
  6971. emit_operand(ctx, *(ctx->frame_offset));
  6972. (void)error_buf;
  6973. (void)error_buf_size;
  6974. return true;
  6975. }
  6976. static bool
  6977. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  6978. uint8 type_push, uint8 type_pop,
  6979. bool disable_emit, int16 operand_offset,
  6980. char *error_buf, uint32 error_buf_size)
  6981. {
  6982. uint8 i;
  6983. for (i = 0; i < pop_cnt; i++) {
  6984. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  6985. error_buf_size))
  6986. return false;
  6987. }
  6988. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  6989. operand_offset, error_buf,
  6990. error_buf_size))
  6991. return false;
  6992. return true;
  6993. }
  6994. static bool
  6995. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  6996. bool disable_emit, int16 operand_offset,
  6997. char *error_buf, uint32 error_buf_size)
  6998. {
  6999. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  7000. error_buf, error_buf_size)))
  7001. return false;
  7002. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  7003. return false;
  7004. return true;
  7005. }
  7006. static bool
  7007. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7008. char *error_buf, uint32 error_buf_size)
  7009. {
  7010. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  7011. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  7012. return false;
  7013. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  7014. return false;
  7015. return true;
  7016. }
  7017. static bool
  7018. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  7019. uint8 type_push, uint8 type_pop,
  7020. bool disable_emit, int16 operand_offset,
  7021. char *error_buf, uint32 error_buf_size)
  7022. {
  7023. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  7024. disable_emit, operand_offset,
  7025. error_buf, error_buf_size))
  7026. return false;
  7027. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  7028. error_buf, error_buf_size))
  7029. return false;
  7030. return true;
  7031. }
  7032. static bool
  7033. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  7034. int16 *offset, char *error_buf,
  7035. uint32 error_buf_size)
  7036. {
  7037. int8 bytes_to_increase;
  7038. int16 operand_offset = 0;
  7039. Const *c;
  7040. /* Search existing constant */
  7041. for (c = (Const *)ctx->const_buf;
  7042. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  7043. /* TODO: handle v128 type? */
  7044. if ((type == c->value_type)
  7045. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  7046. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  7047. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  7048. || (type == VALUE_TYPE_FUNCREF
  7049. && *(int32 *)value == c->value.i32)
  7050. || (type == VALUE_TYPE_EXTERNREF
  7051. && *(int32 *)value == c->value.i32)
  7052. #endif
  7053. || (type == VALUE_TYPE_F64
  7054. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  7055. || (type == VALUE_TYPE_F32
  7056. && (0
  7057. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  7058. operand_offset = c->slot_index;
  7059. break;
  7060. }
  7061. if (is_32bit_type(c->value_type))
  7062. operand_offset += 1;
  7063. else
  7064. operand_offset += 2;
  7065. }
  7066. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  7067. /* New constant, append to the const buffer */
  7068. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  7069. bytes_to_increase = 2;
  7070. }
  7071. else {
  7072. bytes_to_increase = 1;
  7073. }
  7074. /* The max cell num of const buffer is 32768 since the valid index range
  7075. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  7076. * full */
  7077. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  7078. *offset = 0;
  7079. return true;
  7080. }
  7081. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  7082. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  7083. ctx->const_buf_size + 4 * sizeof(Const));
  7084. ctx->const_buf_size += 4 * sizeof(Const);
  7085. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  7086. }
  7087. c->value_type = type;
  7088. switch (type) {
  7089. case VALUE_TYPE_F64:
  7090. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  7091. sizeof(float64));
  7092. ctx->const_cell_num += 2;
  7093. /* The const buf will be reversed, we use the second cell */
  7094. /* of the i64/f64 const so the finnal offset is corrent */
  7095. operand_offset++;
  7096. break;
  7097. case VALUE_TYPE_I64:
  7098. c->value.i64 = *(int64 *)value;
  7099. ctx->const_cell_num += 2;
  7100. operand_offset++;
  7101. break;
  7102. case VALUE_TYPE_F32:
  7103. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  7104. sizeof(float32));
  7105. ctx->const_cell_num++;
  7106. break;
  7107. case VALUE_TYPE_I32:
  7108. c->value.i32 = *(int32 *)value;
  7109. ctx->const_cell_num++;
  7110. break;
  7111. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  7112. case VALUE_TYPE_EXTERNREF:
  7113. case VALUE_TYPE_FUNCREF:
  7114. c->value.i32 = *(int32 *)value;
  7115. ctx->const_cell_num++;
  7116. break;
  7117. #endif
  7118. default:
  7119. break;
  7120. }
  7121. c->slot_index = operand_offset;
  7122. ctx->num_const++;
  7123. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  7124. (int64)c->value.i64);
  7125. }
  7126. /* use negetive index for const */
  7127. operand_offset = -(operand_offset + 1);
  7128. *offset = operand_offset;
  7129. return true;
  7130. fail:
  7131. return false;
  7132. }
  7133. /*
  7134. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  7135. -- Mostly used for the binary / compare operation
  7136. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  7137. -- Mostly used in block / control instructions
  7138. The POP will always emit the offset on the top of the frame_offset stack
  7139. PUSH can be used in two ways:
  7140. 1. directly PUSH:
  7141. PUSH_XXX();
  7142. will allocate a dynamic space and emit
  7143. 2. silent PUSH:
  7144. operand_offset = xxx; disable_emit = true;
  7145. PUSH_XXX();
  7146. only push the frame_offset stack, no emit
  7147. */
  7148. #define TEMPLATE_PUSH(Type) \
  7149. do { \
  7150. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  7151. disable_emit, operand_offset, \
  7152. error_buf, error_buf_size)) \
  7153. goto fail; \
  7154. } while (0)
  7155. #define TEMPLATE_PUSH_REF(Type) \
  7156. do { \
  7157. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  7158. operand_offset, error_buf, \
  7159. error_buf_size)) \
  7160. goto fail; \
  7161. } while (0)
  7162. #define TEMPLATE_POP(Type) \
  7163. do { \
  7164. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  7165. error_buf, error_buf_size)) \
  7166. goto fail; \
  7167. } while (0)
  7168. #define TEMPLATE_POP_REF(Type) \
  7169. do { \
  7170. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  7171. error_buf_size)) \
  7172. goto fail; \
  7173. } while (0)
  7174. #define PUSH_OFFSET_TYPE(type) \
  7175. do { \
  7176. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  7177. operand_offset, error_buf, \
  7178. error_buf_size))) \
  7179. goto fail; \
  7180. } while (0)
  7181. #define POP_OFFSET_TYPE(type) \
  7182. do { \
  7183. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  7184. error_buf_size))) \
  7185. goto fail; \
  7186. } while (0)
  7187. #define POP_AND_PUSH(type_pop, type_push) \
  7188. do { \
  7189. if (!(wasm_loader_push_pop_frame_ref_offset( \
  7190. loader_ctx, 1, type_push, type_pop, disable_emit, \
  7191. operand_offset, error_buf, error_buf_size))) \
  7192. goto fail; \
  7193. } while (0)
  7194. /* type of POPs should be the same */
  7195. #define POP2_AND_PUSH(type_pop, type_push) \
  7196. do { \
  7197. if (!(wasm_loader_push_pop_frame_ref_offset( \
  7198. loader_ctx, 2, type_push, type_pop, disable_emit, \
  7199. operand_offset, error_buf, error_buf_size))) \
  7200. goto fail; \
  7201. } while (0)
  7202. #else /* WASM_ENABLE_FAST_INTERP */
  7203. #define TEMPLATE_PUSH(Type) \
  7204. do { \
  7205. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  7206. error_buf, error_buf_size))) \
  7207. goto fail; \
  7208. } while (0)
  7209. #define TEMPLATE_PUSH_REF(Type) \
  7210. do { \
  7211. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  7212. error_buf_size))) \
  7213. goto fail; \
  7214. } while (0)
  7215. #define TEMPLATE_POP(Type) \
  7216. do { \
  7217. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  7218. error_buf, error_buf_size))) \
  7219. goto fail; \
  7220. } while (0)
  7221. #define TEMPLATE_POP_REF(Type) \
  7222. do { \
  7223. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  7224. error_buf_size))) \
  7225. goto fail; \
  7226. } while (0)
  7227. #define POP_AND_PUSH(type_pop, type_push) \
  7228. do { \
  7229. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  7230. type_pop, error_buf, \
  7231. error_buf_size))) \
  7232. goto fail; \
  7233. } while (0)
  7234. /* type of POPs should be the same */
  7235. #define POP2_AND_PUSH(type_pop, type_push) \
  7236. do { \
  7237. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  7238. type_pop, error_buf, \
  7239. error_buf_size))) \
  7240. goto fail; \
  7241. } while (0)
  7242. #endif /* WASM_ENABLE_FAST_INTERP */
  7243. #define PUSH_I32() TEMPLATE_PUSH(I32)
  7244. #define PUSH_F32() TEMPLATE_PUSH(F32)
  7245. #define PUSH_I64() TEMPLATE_PUSH(I64)
  7246. #define PUSH_F64() TEMPLATE_PUSH(F64)
  7247. #define PUSH_V128() TEMPLATE_PUSH(V128)
  7248. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  7249. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  7250. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  7251. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  7252. #define POP_I32() TEMPLATE_POP(I32)
  7253. #define POP_F32() TEMPLATE_POP(F32)
  7254. #define POP_I64() TEMPLATE_POP(I64)
  7255. #define POP_F64() TEMPLATE_POP(F64)
  7256. #define POP_V128() TEMPLATE_POP(V128)
  7257. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  7258. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  7259. #if WASM_ENABLE_FAST_INTERP != 0
  7260. static bool
  7261. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  7262. bool disable_emit, char *error_buf, uint32 error_buf_size)
  7263. {
  7264. int16 operand_offset = 0;
  7265. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  7266. : loader_ctx->frame_csp;
  7267. BlockType *block_type = &block->block_type;
  7268. uint8 *return_types = NULL;
  7269. #if WASM_ENABLE_GC != 0
  7270. WASMRefTypeMap *reftype_maps = NULL;
  7271. uint32 reftype_map_count;
  7272. #endif
  7273. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  7274. int32 i = 0;
  7275. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  7276. *frame_offset_org = NULL;
  7277. #if WASM_ENABLE_GC == 0
  7278. return_count = block_type_get_result_types(block_type, &return_types);
  7279. #else
  7280. return_count = block_type_get_result_types(
  7281. block_type, &return_types, &reftype_maps, &reftype_map_count);
  7282. #endif
  7283. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  7284. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  7285. if (return_count == 1) {
  7286. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  7287. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  7288. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  7289. /* insert op_copy before else opcode */
  7290. if (opcode == WASM_OP_ELSE)
  7291. skip_label();
  7292. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  7293. : EXT_OP_COPY_STACK_TOP_I64);
  7294. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  7295. emit_operand(loader_ctx, block->dynamic_offset);
  7296. if (opcode == WASM_OP_ELSE) {
  7297. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  7298. }
  7299. else {
  7300. loader_ctx->frame_offset -= cell;
  7301. loader_ctx->dynamic_offset = block->dynamic_offset;
  7302. PUSH_OFFSET_TYPE(return_types[0]);
  7303. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  7304. }
  7305. if (opcode == WASM_OP_ELSE)
  7306. emit_label(opcode);
  7307. }
  7308. return true;
  7309. }
  7310. /* Copy stack top values to block's results which are in dynamic space.
  7311. * The instruction format:
  7312. * Part a: values count
  7313. * Part b: all values total cell num
  7314. * Part c: each value's cell_num, src offset and dst offset
  7315. * Part d: each value's src offset and dst offset
  7316. * Part e: each value's dst offset
  7317. */
  7318. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  7319. dynamic_offset = dynamic_offset_org =
  7320. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  7321. /* First traversal to get the count of values needed to be copied. */
  7322. for (i = (int32)return_count - 1; i >= 0; i--) {
  7323. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  7324. frame_offset -= cells;
  7325. dynamic_offset -= cells;
  7326. if (dynamic_offset != *frame_offset) {
  7327. value_count++;
  7328. total_cel_num += cells;
  7329. }
  7330. }
  7331. if (value_count) {
  7332. uint32 j = 0;
  7333. uint8 *emit_data = NULL, *cells = NULL;
  7334. int16 *src_offsets = NULL;
  7335. uint16 *dst_offsets = NULL;
  7336. uint64 size =
  7337. (uint64)value_count
  7338. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  7339. /* Allocate memory for the emit data */
  7340. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  7341. return false;
  7342. cells = emit_data;
  7343. src_offsets = (int16 *)(cells + value_count);
  7344. dst_offsets = (uint16 *)(src_offsets + value_count);
  7345. /* insert op_copy before else opcode */
  7346. if (opcode == WASM_OP_ELSE)
  7347. skip_label();
  7348. emit_label(EXT_OP_COPY_STACK_VALUES);
  7349. /* Part a) */
  7350. emit_uint32(loader_ctx, value_count);
  7351. /* Part b) */
  7352. emit_uint32(loader_ctx, total_cel_num);
  7353. /* Second traversal to get each value's cell num, src offset and dst
  7354. * offset. */
  7355. frame_offset = frame_offset_org;
  7356. dynamic_offset = dynamic_offset_org;
  7357. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  7358. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  7359. frame_offset -= cell;
  7360. dynamic_offset -= cell;
  7361. if (dynamic_offset != *frame_offset) {
  7362. /* cell num */
  7363. cells[j] = cell;
  7364. /* src offset */
  7365. src_offsets[j] = *frame_offset;
  7366. /* dst offset */
  7367. dst_offsets[j] = dynamic_offset;
  7368. j++;
  7369. }
  7370. if (opcode == WASM_OP_ELSE) {
  7371. *frame_offset = dynamic_offset;
  7372. }
  7373. else {
  7374. loader_ctx->frame_offset = frame_offset;
  7375. loader_ctx->dynamic_offset = dynamic_offset;
  7376. PUSH_OFFSET_TYPE(return_types[i]);
  7377. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  7378. loader_ctx->frame_offset = frame_offset_org;
  7379. loader_ctx->dynamic_offset = dynamic_offset_org;
  7380. }
  7381. }
  7382. bh_assert(j == value_count);
  7383. /* Emit the cells, src_offsets and dst_offsets */
  7384. for (j = 0; j < value_count; j++)
  7385. emit_byte(loader_ctx, cells[j]);
  7386. for (j = 0; j < value_count; j++)
  7387. emit_operand(loader_ctx, src_offsets[j]);
  7388. for (j = 0; j < value_count; j++)
  7389. emit_operand(loader_ctx, dst_offsets[j]);
  7390. if (opcode == WASM_OP_ELSE)
  7391. emit_label(opcode);
  7392. wasm_runtime_free(emit_data);
  7393. }
  7394. return true;
  7395. fail:
  7396. return false;
  7397. }
  7398. #endif /* WASM_ENABLE_FAST_INTERP */
  7399. #define RESERVE_BLOCK_RET() \
  7400. do { \
  7401. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  7402. error_buf_size)) \
  7403. goto fail; \
  7404. } while (0)
  7405. #define PUSH_TYPE(type) \
  7406. do { \
  7407. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  7408. error_buf_size))) \
  7409. goto fail; \
  7410. } while (0)
  7411. #define POP_TYPE(type) \
  7412. do { \
  7413. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  7414. error_buf_size))) \
  7415. goto fail; \
  7416. } while (0)
  7417. #define PUSH_CSP(label_type, block_type, _start_addr) \
  7418. do { \
  7419. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  7420. _start_addr, error_buf, \
  7421. error_buf_size)) \
  7422. goto fail; \
  7423. } while (0)
  7424. #define POP_CSP() \
  7425. do { \
  7426. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  7427. goto fail; \
  7428. } while (0)
  7429. #if WASM_ENABLE_GC == 0
  7430. #define GET_LOCAL_REFTYPE() (void)0
  7431. #else
  7432. #define GET_LOCAL_REFTYPE() \
  7433. do { \
  7434. if (wasm_is_type_multi_byte_type(local_type)) { \
  7435. WASMRefType *_ref_type; \
  7436. if (local_idx < param_count) \
  7437. _ref_type = wasm_reftype_map_find( \
  7438. param_reftype_maps, param_reftype_map_count, local_idx); \
  7439. else \
  7440. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  7441. local_reftype_map_count, \
  7442. local_idx - param_count); \
  7443. bh_assert(_ref_type); \
  7444. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  7445. wasm_reftype_struct_size(_ref_type)); \
  7446. } \
  7447. } while (0)
  7448. #endif
  7449. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  7450. do { \
  7451. read_leb_uint32(p, p_end, local_idx); \
  7452. if (local_idx >= param_count + local_count) { \
  7453. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  7454. goto fail; \
  7455. } \
  7456. local_type = local_idx < param_count \
  7457. ? param_types[local_idx] \
  7458. : local_types[local_idx - param_count]; \
  7459. local_offset = local_offsets[local_idx]; \
  7460. GET_LOCAL_REFTYPE(); \
  7461. } while (0)
  7462. #define CHECK_BR(depth) \
  7463. do { \
  7464. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  7465. error_buf_size)) \
  7466. goto fail; \
  7467. } while (0)
  7468. static bool
  7469. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  7470. {
  7471. if (module->memory_count == 0 && module->import_memory_count == 0) {
  7472. set_error_buf(error_buf, error_buf_size, "unknown memory");
  7473. return false;
  7474. }
  7475. return true;
  7476. }
  7477. #define CHECK_MEMORY() \
  7478. do { \
  7479. if (!check_memory(module, error_buf, error_buf_size)) \
  7480. goto fail; \
  7481. } while (0)
  7482. static bool
  7483. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  7484. uint32 error_buf_size)
  7485. {
  7486. uint8 mem_access_aligns[] = {
  7487. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  7488. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  7489. };
  7490. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  7491. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  7492. set_error_buf(error_buf, error_buf_size,
  7493. "alignment must not be larger than natural");
  7494. return false;
  7495. }
  7496. return true;
  7497. }
  7498. #if WASM_ENABLE_SIMD != 0
  7499. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  7500. static bool
  7501. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  7502. uint32 error_buf_size)
  7503. {
  7504. uint8 mem_access_aligns[] = {
  7505. 4, /* load */
  7506. 3, 3, 3, 3, 3, 3, /* load and extend */
  7507. 0, 1, 2, 3, /* load and splat */
  7508. 4, /* store */
  7509. };
  7510. uint8 mem_access_aligns_load_lane[] = {
  7511. 0, 1, 2, 3, /* load lane */
  7512. 0, 1, 2, 3, /* store lane */
  7513. 2, 3 /* store zero */
  7514. };
  7515. if (!((opcode <= SIMD_v128_store)
  7516. || (SIMD_v128_load8_lane <= opcode
  7517. && opcode <= SIMD_v128_load64_zero))) {
  7518. set_error_buf(error_buf, error_buf_size,
  7519. "the opcode doesn't include memarg");
  7520. return false;
  7521. }
  7522. if ((opcode <= SIMD_v128_store
  7523. && align > mem_access_aligns[opcode - SIMD_v128_load])
  7524. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  7525. && align > mem_access_aligns_load_lane[opcode
  7526. - SIMD_v128_load8_lane])) {
  7527. set_error_buf(error_buf, error_buf_size,
  7528. "alignment must not be larger than natural");
  7529. return false;
  7530. }
  7531. return true;
  7532. }
  7533. static bool
  7534. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  7535. uint32 error_buf_size)
  7536. {
  7537. switch (opcode) {
  7538. case SIMD_i8x16_extract_lane_s:
  7539. case SIMD_i8x16_extract_lane_u:
  7540. case SIMD_i8x16_replace_lane:
  7541. if (lane >= 16) {
  7542. goto fail;
  7543. }
  7544. break;
  7545. case SIMD_i16x8_extract_lane_s:
  7546. case SIMD_i16x8_extract_lane_u:
  7547. case SIMD_i16x8_replace_lane:
  7548. if (lane >= 8) {
  7549. goto fail;
  7550. }
  7551. break;
  7552. case SIMD_i32x4_extract_lane:
  7553. case SIMD_i32x4_replace_lane:
  7554. case SIMD_f32x4_extract_lane:
  7555. case SIMD_f32x4_replace_lane:
  7556. if (lane >= 4) {
  7557. goto fail;
  7558. }
  7559. break;
  7560. case SIMD_i64x2_extract_lane:
  7561. case SIMD_i64x2_replace_lane:
  7562. case SIMD_f64x2_extract_lane:
  7563. case SIMD_f64x2_replace_lane:
  7564. if (lane >= 2) {
  7565. goto fail;
  7566. }
  7567. break;
  7568. case SIMD_v128_load8_lane:
  7569. case SIMD_v128_load16_lane:
  7570. case SIMD_v128_load32_lane:
  7571. case SIMD_v128_load64_lane:
  7572. case SIMD_v128_store8_lane:
  7573. case SIMD_v128_store16_lane:
  7574. case SIMD_v128_store32_lane:
  7575. case SIMD_v128_store64_lane:
  7576. case SIMD_v128_load32_zero:
  7577. case SIMD_v128_load64_zero:
  7578. {
  7579. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  7580. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  7581. goto fail;
  7582. }
  7583. break;
  7584. }
  7585. default:
  7586. goto fail;
  7587. }
  7588. return true;
  7589. fail:
  7590. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  7591. return false;
  7592. }
  7593. static bool
  7594. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  7595. {
  7596. uint8 i;
  7597. for (i = 0; i != 16; ++i) {
  7598. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  7599. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  7600. return false;
  7601. }
  7602. }
  7603. return true;
  7604. }
  7605. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  7606. #endif /* end of WASM_ENABLE_SIMD */
  7607. #if WASM_ENABLE_SHARED_MEMORY != 0
  7608. static bool
  7609. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  7610. uint32 error_buf_size)
  7611. {
  7612. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  7613. uint8 mem_access_aligns[] = {
  7614. 2, 3, 0, 1, 0, 1, 2,
  7615. };
  7616. uint8 expect;
  7617. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  7618. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  7619. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  7620. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  7621. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  7622. }
  7623. else {
  7624. /* 7 opcodes in every group */
  7625. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  7626. }
  7627. if (align != expect) {
  7628. set_error_buf(error_buf, error_buf_size,
  7629. "alignment isn't equal to natural");
  7630. return false;
  7631. }
  7632. return true;
  7633. }
  7634. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  7635. static bool
  7636. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  7637. bool is_br_table, char *error_buf, uint32 error_buf_size)
  7638. {
  7639. BranchBlock *target_block, *cur_block;
  7640. BlockType *target_block_type;
  7641. uint8 type, *types = NULL, *frame_ref;
  7642. uint32 arity = 0;
  7643. int32 i, available_stack_cell;
  7644. uint16 cell_num;
  7645. #if WASM_ENABLE_GC != 0
  7646. WASMRefTypeMap *frame_reftype_map;
  7647. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  7648. WASMRefType *ref_type;
  7649. uint32 reftype_map_count = 0;
  7650. int32 available_reftype_map;
  7651. bool is_type_multi_byte;
  7652. #endif
  7653. if (loader_ctx->csp_num < depth + 1) {
  7654. set_error_buf(error_buf, error_buf_size,
  7655. "unknown label, "
  7656. "unexpected end of section or function");
  7657. return false;
  7658. }
  7659. cur_block = loader_ctx->frame_csp - 1;
  7660. target_block = loader_ctx->frame_csp - (depth + 1);
  7661. target_block_type = &target_block->block_type;
  7662. frame_ref = loader_ctx->frame_ref;
  7663. #if WASM_ENABLE_GC != 0
  7664. frame_reftype_map = loader_ctx->frame_reftype_map;
  7665. #endif
  7666. /* Note: loop's arity is different from if and block. loop's arity is
  7667. * its parameter count while if and block arity is result count.
  7668. */
  7669. #if WASM_ENABLE_GC == 0
  7670. if (target_block->label_type == LABEL_TYPE_LOOP)
  7671. arity = block_type_get_param_types(target_block_type, &types);
  7672. else
  7673. arity = block_type_get_result_types(target_block_type, &types);
  7674. #else
  7675. if (target_block->label_type == LABEL_TYPE_LOOP)
  7676. arity = block_type_get_param_types(target_block_type, &types,
  7677. &reftype_maps, &reftype_map_count);
  7678. else
  7679. arity = block_type_get_result_types(target_block_type, &types,
  7680. &reftype_maps, &reftype_map_count);
  7681. #endif
  7682. /* If the stack is in polymorphic state, just clear the stack
  7683. * and then re-push the values to make the stack top values
  7684. * match block type. */
  7685. if (cur_block->is_stack_polymorphic && !is_br_table) {
  7686. #if WASM_ENABLE_GC != 0
  7687. int32 j = reftype_map_count - 1;
  7688. #endif
  7689. for (i = (int32)arity - 1; i >= 0; i--) {
  7690. #if WASM_ENABLE_GC != 0
  7691. if (wasm_is_type_multi_byte_type(types[i])) {
  7692. bh_assert(reftype_maps[j].index == i);
  7693. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7694. reftype_maps[j].ref_type,
  7695. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  7696. j--;
  7697. }
  7698. #endif
  7699. #if WASM_ENABLE_FAST_INTERP != 0
  7700. POP_OFFSET_TYPE(types[i]);
  7701. #endif
  7702. POP_TYPE(types[i]);
  7703. }
  7704. #if WASM_ENABLE_GC != 0
  7705. j = 0;
  7706. #endif
  7707. for (i = 0; i < (int32)arity; i++) {
  7708. #if WASM_ENABLE_GC != 0
  7709. if (wasm_is_type_multi_byte_type(types[i])) {
  7710. bh_assert(reftype_maps[j].index == i);
  7711. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7712. reftype_maps[j].ref_type,
  7713. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  7714. j++;
  7715. }
  7716. #endif
  7717. #if WASM_ENABLE_FAST_INTERP != 0
  7718. bool disable_emit = true;
  7719. int16 operand_offset = 0;
  7720. PUSH_OFFSET_TYPE(types[i]);
  7721. #endif
  7722. PUSH_TYPE(types[i]);
  7723. }
  7724. return true;
  7725. }
  7726. available_stack_cell =
  7727. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  7728. #if WASM_ENABLE_GC != 0
  7729. available_reftype_map =
  7730. (int32)(loader_ctx->reftype_map_num
  7731. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  7732. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  7733. #endif
  7734. /* Check stack top values match target block type */
  7735. for (i = (int32)arity - 1; i >= 0; i--) {
  7736. type = types[i];
  7737. #if WASM_ENABLE_GC != 0
  7738. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  7739. ref_type = is_type_multi_byte ? reftype_map->ref_type : NULL;
  7740. #endif
  7741. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7742. break;
  7743. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  7744. #if WASM_ENABLE_GC != 0
  7745. frame_reftype_map, available_reftype_map,
  7746. #endif
  7747. type,
  7748. #if WASM_ENABLE_GC != 0
  7749. ref_type,
  7750. #endif
  7751. error_buf, error_buf_size)) {
  7752. return false;
  7753. }
  7754. cell_num = wasm_value_type_cell_num(types[i]);
  7755. frame_ref -= cell_num;
  7756. available_stack_cell -= cell_num;
  7757. #if WASM_ENABLE_GC != 0
  7758. if (is_type_multi_byte) {
  7759. frame_reftype_map--;
  7760. available_reftype_map--;
  7761. reftype_map--;
  7762. }
  7763. #endif
  7764. }
  7765. return true;
  7766. fail:
  7767. return false;
  7768. }
  7769. static BranchBlock *
  7770. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  7771. bool is_br_table, char *error_buf, uint32 error_buf_size)
  7772. {
  7773. uint8 *p = *p_buf, *p_end = buf_end;
  7774. BranchBlock *frame_csp_tmp;
  7775. uint32 depth;
  7776. read_leb_uint32(p, p_end, depth);
  7777. if (!wasm_loader_check_br(loader_ctx, depth, is_br_table, error_buf,
  7778. error_buf_size)) {
  7779. goto fail;
  7780. }
  7781. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  7782. #if WASM_ENABLE_FAST_INTERP != 0
  7783. emit_br_info(frame_csp_tmp);
  7784. #endif
  7785. *p_buf = p;
  7786. return frame_csp_tmp;
  7787. fail:
  7788. return NULL;
  7789. }
  7790. static bool
  7791. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  7792. char *error_buf, uint32 error_buf_size)
  7793. {
  7794. BlockType *block_type = &block->block_type;
  7795. uint8 *return_types = NULL;
  7796. uint32 return_count = 0;
  7797. int32 available_stack_cell, return_cell_num, i;
  7798. uint8 *frame_ref = NULL;
  7799. #if WASM_ENABLE_GC != 0
  7800. WASMRefTypeMap *frame_reftype_map;
  7801. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  7802. WASMRefType *ref_type;
  7803. uint32 param_count, return_reftype_map_count = 0;
  7804. int32 available_reftype_map =
  7805. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  7806. #endif
  7807. available_stack_cell =
  7808. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  7809. #if WASM_ENABLE_GC == 0
  7810. return_count = block_type_get_result_types(block_type, &return_types);
  7811. #else
  7812. return_count = block_type_get_result_types(block_type, &return_types,
  7813. &return_reftype_maps,
  7814. &return_reftype_map_count);
  7815. param_count =
  7816. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  7817. (void)param_count;
  7818. #endif
  7819. return_cell_num =
  7820. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  7821. /* If the stack is in polymorphic state, just clear the stack
  7822. * and then re-push the values to make the stack top values
  7823. * match block type. */
  7824. if (block->is_stack_polymorphic) {
  7825. #if WASM_ENABLE_GC != 0
  7826. int32 j = return_reftype_map_count - 1;
  7827. #endif
  7828. for (i = (int32)return_count - 1; i >= 0; i--) {
  7829. #if WASM_ENABLE_GC != 0
  7830. if (wasm_is_type_multi_byte_type(return_types[i])) {
  7831. bh_assert(return_reftype_maps[j].index == i + param_count);
  7832. bh_memcpy_s(
  7833. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7834. return_reftype_maps[j].ref_type,
  7835. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  7836. j--;
  7837. }
  7838. #endif
  7839. #if WASM_ENABLE_FAST_INTERP != 0
  7840. POP_OFFSET_TYPE(return_types[i]);
  7841. #endif
  7842. POP_TYPE(return_types[i]);
  7843. }
  7844. /* Check stack is empty */
  7845. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  7846. set_error_buf(
  7847. error_buf, error_buf_size,
  7848. "type mismatch: stack size does not match block type");
  7849. goto fail;
  7850. }
  7851. #if WASM_ENABLE_GC != 0
  7852. j = 0;
  7853. #endif
  7854. for (i = 0; i < (int32)return_count; i++) {
  7855. #if WASM_ENABLE_GC != 0
  7856. if (wasm_is_type_multi_byte_type(return_types[i])) {
  7857. bh_assert(return_reftype_maps[j].index == i + param_count);
  7858. bh_memcpy_s(
  7859. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  7860. return_reftype_maps[j].ref_type,
  7861. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  7862. j++;
  7863. }
  7864. #endif
  7865. #if WASM_ENABLE_FAST_INTERP != 0
  7866. bool disable_emit = true;
  7867. int16 operand_offset = 0;
  7868. PUSH_OFFSET_TYPE(return_types[i]);
  7869. #endif
  7870. PUSH_TYPE(return_types[i]);
  7871. }
  7872. return true;
  7873. }
  7874. /* Check stack cell num equals return cell num */
  7875. if (available_stack_cell != return_cell_num) {
  7876. set_error_buf(error_buf, error_buf_size,
  7877. "type mismatch: stack size does not match block type");
  7878. goto fail;
  7879. }
  7880. /* Check stack values match return types */
  7881. frame_ref = loader_ctx->frame_ref;
  7882. #if WASM_ENABLE_GC != 0
  7883. frame_reftype_map = loader_ctx->frame_reftype_map;
  7884. return_reftype_map =
  7885. return_reftype_map_count
  7886. ? return_reftype_maps + return_reftype_map_count - 1
  7887. : NULL;
  7888. #endif
  7889. for (i = (int32)return_count - 1; i >= 0; i--) {
  7890. uint8 type = return_types[i];
  7891. #if WASM_ENABLE_GC != 0
  7892. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  7893. ref_type = is_type_multi_byte ? return_reftype_map->ref_type : NULL;
  7894. #endif
  7895. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  7896. #if WASM_ENABLE_GC != 0
  7897. frame_reftype_map, available_reftype_map,
  7898. #endif
  7899. type,
  7900. #if WASM_ENABLE_GC != 0
  7901. ref_type,
  7902. #endif
  7903. error_buf, error_buf_size))
  7904. return false;
  7905. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  7906. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  7907. #if WASM_ENABLE_GC != 0
  7908. if (is_type_multi_byte) {
  7909. frame_reftype_map--;
  7910. available_reftype_map--;
  7911. return_reftype_map--;
  7912. }
  7913. #endif
  7914. }
  7915. return true;
  7916. fail:
  7917. return false;
  7918. }
  7919. #if WASM_ENABLE_FAST_INTERP != 0
  7920. /* Copy parameters to dynamic space.
  7921. * 1) POP original parameter out;
  7922. * 2) Push and copy original values to dynamic space.
  7923. * The copy instruction format:
  7924. * Part a: param count
  7925. * Part b: all param total cell num
  7926. * Part c: each param's cell_num, src offset and dst offset
  7927. * Part d: each param's src offset
  7928. * Part e: each param's dst offset
  7929. */
  7930. static bool
  7931. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  7932. char *error_buf, uint32 error_buf_size)
  7933. {
  7934. int16 *frame_offset = NULL;
  7935. uint8 *cells = NULL, cell;
  7936. int16 *src_offsets = NULL;
  7937. uint8 *emit_data = NULL;
  7938. uint32 i;
  7939. BranchBlock *block = loader_ctx->frame_csp - 1;
  7940. BlockType *block_type = &block->block_type;
  7941. WASMFuncType *wasm_type = block_type->u.type;
  7942. uint32 param_count = block_type->u.type->param_count;
  7943. int16 condition_offset = 0;
  7944. bool disable_emit = false;
  7945. int16 operand_offset = 0;
  7946. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  7947. /* For if block, we also need copy the condition operand offset. */
  7948. if (is_if_block)
  7949. size += sizeof(*cells) + sizeof(*src_offsets);
  7950. /* Allocate memory for the emit data */
  7951. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  7952. return false;
  7953. cells = emit_data;
  7954. src_offsets = (int16 *)(cells + param_count);
  7955. if (is_if_block)
  7956. condition_offset = *loader_ctx->frame_offset;
  7957. /* POP original parameter out */
  7958. for (i = 0; i < param_count; i++) {
  7959. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  7960. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  7961. }
  7962. frame_offset = loader_ctx->frame_offset;
  7963. /* Get each param's cell num and src offset */
  7964. for (i = 0; i < param_count; i++) {
  7965. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  7966. cells[i] = cell;
  7967. src_offsets[i] = *frame_offset;
  7968. frame_offset += cell;
  7969. }
  7970. /* emit copy instruction */
  7971. emit_label(EXT_OP_COPY_STACK_VALUES);
  7972. /* Part a) */
  7973. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  7974. /* Part b) */
  7975. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  7976. : wasm_type->param_cell_num);
  7977. /* Part c) */
  7978. for (i = 0; i < param_count; i++)
  7979. emit_byte(loader_ctx, cells[i]);
  7980. if (is_if_block)
  7981. emit_byte(loader_ctx, 1);
  7982. /* Part d) */
  7983. for (i = 0; i < param_count; i++)
  7984. emit_operand(loader_ctx, src_offsets[i]);
  7985. if (is_if_block)
  7986. emit_operand(loader_ctx, condition_offset);
  7987. /* Part e) */
  7988. /* Push to dynamic space. The push will emit the dst offset. */
  7989. for (i = 0; i < param_count; i++)
  7990. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  7991. if (is_if_block)
  7992. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  7993. /* Free the emit data */
  7994. wasm_runtime_free(emit_data);
  7995. return true;
  7996. fail:
  7997. return false;
  7998. }
  7999. #endif
  8000. #if WASM_ENABLE_GC == 0
  8001. #define RESET_REFTYPE_MAP_STACK() (void)0
  8002. #else
  8003. #define RESET_REFTYPE_MAP_STACK() \
  8004. do { \
  8005. loader_ctx->reftype_map_num = \
  8006. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  8007. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  8008. + loader_ctx->reftype_map_num; \
  8009. } while (0)
  8010. #endif
  8011. /* reset the stack to the state of before entering the last block */
  8012. #if WASM_ENABLE_FAST_INTERP != 0
  8013. #define RESET_STACK() \
  8014. do { \
  8015. loader_ctx->stack_cell_num = \
  8016. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  8017. loader_ctx->frame_ref = \
  8018. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  8019. loader_ctx->frame_offset = \
  8020. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  8021. RESET_REFTYPE_MAP_STACK(); \
  8022. } while (0)
  8023. #else
  8024. #define RESET_STACK() \
  8025. do { \
  8026. loader_ctx->stack_cell_num = \
  8027. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  8028. loader_ctx->frame_ref = \
  8029. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  8030. RESET_REFTYPE_MAP_STACK(); \
  8031. } while (0)
  8032. #endif
  8033. /* set current block's stack polymorphic state */
  8034. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  8035. do { \
  8036. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  8037. _cur_block->is_stack_polymorphic = flag; \
  8038. } while (0)
  8039. #define BLOCK_HAS_PARAM(block_type) \
  8040. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  8041. #define PRESERVE_LOCAL_FOR_BLOCK() \
  8042. do { \
  8043. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  8044. error_buf_size))) { \
  8045. goto fail; \
  8046. } \
  8047. } while (0)
  8048. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  8049. static bool
  8050. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  8051. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  8052. uint32 error_buf_size)
  8053. {
  8054. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  8055. return false;
  8056. }
  8057. if (table_idx < module->import_table_count) {
  8058. if (p_elem_type)
  8059. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  8060. #if WASM_ENABLE_GC != 0
  8061. if (p_ref_type)
  8062. *((WASMRefType **)p_ref_type) =
  8063. module->import_tables[table_idx].u.table.elem_ref_type;
  8064. #endif
  8065. }
  8066. else {
  8067. if (p_elem_type)
  8068. *p_elem_type =
  8069. module->tables[module->import_table_count + table_idx]
  8070. .elem_type;
  8071. #if WASM_ENABLE_GC != 0
  8072. if (p_ref_type)
  8073. *((WASMRefType **)p_ref_type) =
  8074. module->tables[module->import_table_count + table_idx]
  8075. .elem_ref_type;
  8076. #endif
  8077. }
  8078. return true;
  8079. }
  8080. static bool
  8081. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  8082. uint8 *p_elem_type, void **p_elem_ref_type,
  8083. char *error_buf, uint32 error_buf_size)
  8084. {
  8085. if (table_seg_idx >= module->table_seg_count) {
  8086. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  8087. table_seg_idx);
  8088. return false;
  8089. }
  8090. if (p_elem_type) {
  8091. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  8092. }
  8093. #if WASM_ENABLE_GC != 0
  8094. if (p_elem_ref_type)
  8095. *((WASMRefType **)p_elem_ref_type) =
  8096. module->table_segments[table_seg_idx].elem_ref_type;
  8097. #endif
  8098. return true;
  8099. }
  8100. #endif
  8101. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  8102. const uint8 *
  8103. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  8104. uint32 *len)
  8105. {
  8106. WASMCustomSection *section = module->custom_section_list;
  8107. while (section) {
  8108. if ((section->name_len == strlen(name))
  8109. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  8110. if (len) {
  8111. *len = section->content_len;
  8112. }
  8113. return section->content_addr;
  8114. }
  8115. section = section->next;
  8116. }
  8117. return false;
  8118. }
  8119. #endif
  8120. static bool
  8121. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  8122. uint32 cur_func_idx, char *error_buf,
  8123. uint32 error_buf_size)
  8124. {
  8125. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  8126. uint32 param_count, local_count, global_count;
  8127. uint8 *param_types, *local_types, local_type, global_type;
  8128. BlockType func_block_type;
  8129. uint16 *local_offsets, local_offset;
  8130. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  8131. uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
  8132. int32 i32_const = 0;
  8133. int64 i64_const;
  8134. uint8 opcode;
  8135. bool return_value = false;
  8136. WASMLoaderContext *loader_ctx;
  8137. BranchBlock *frame_csp_tmp;
  8138. #if WASM_ENABLE_GC != 0
  8139. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  8140. uint32 param_reftype_map_count, local_reftype_map_count;
  8141. int32 heap_type;
  8142. WASMRefType wasm_ref_type = { 0 };
  8143. bool need_ref_type_map;
  8144. #endif
  8145. #if WASM_ENABLE_FAST_INTERP != 0
  8146. uint8 *func_const_end, *func_const = NULL;
  8147. int16 operand_offset = 0;
  8148. uint8 last_op = 0;
  8149. bool disable_emit, preserve_local = false;
  8150. float32 f32_const;
  8151. float64 f64_const;
  8152. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  8153. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  8154. #endif
  8155. global_count = module->import_global_count + module->global_count;
  8156. param_count = func->func_type->param_count;
  8157. param_types = func->func_type->types;
  8158. func_block_type.is_value_type = false;
  8159. func_block_type.u.type = func->func_type;
  8160. local_count = func->local_count;
  8161. local_types = func->local_types;
  8162. local_offsets = func->local_offsets;
  8163. #if WASM_ENABLE_GC != 0
  8164. param_reftype_maps = func->func_type->ref_type_maps;
  8165. param_reftype_map_count = func->func_type->ref_type_map_count;
  8166. local_reftype_maps = func->local_ref_type_maps;
  8167. local_reftype_map_count = func->local_ref_type_map_count;
  8168. #endif
  8169. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  8170. goto fail;
  8171. }
  8172. #if WASM_ENABLE_GC != 0
  8173. loader_ctx->module = module;
  8174. loader_ctx->ref_type_set = module->ref_type_set;
  8175. loader_ctx->ref_type_tmp = &wasm_ref_type;
  8176. #endif
  8177. #if WASM_ENABLE_FAST_INTERP != 0
  8178. /* For the first traverse, the initial value of preserved_local_offset has
  8179. * not been determined, we use the INT16_MAX to represent that a slot has
  8180. * been copied to preserve space. For second traverse, this field will be
  8181. * set to the appropriate value in wasm_loader_ctx_reinit.
  8182. * This is for Issue #1230,
  8183. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  8184. * drop opcodes need to know which slots are preserved, so those slots will
  8185. * not be treated as dynamically allocated slots */
  8186. loader_ctx->preserved_local_offset = INT16_MAX;
  8187. re_scan:
  8188. if (loader_ctx->code_compiled_size > 0) {
  8189. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  8190. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  8191. goto fail;
  8192. }
  8193. p = func->code;
  8194. func->code_compiled = loader_ctx->p_code_compiled;
  8195. func->code_compiled_size = loader_ctx->code_compiled_size;
  8196. }
  8197. #endif
  8198. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  8199. while (p < p_end) {
  8200. opcode = *p++;
  8201. #if WASM_ENABLE_FAST_INTERP != 0
  8202. p_org = p;
  8203. disable_emit = false;
  8204. emit_label(opcode);
  8205. #endif
  8206. switch (opcode) {
  8207. case WASM_OP_UNREACHABLE:
  8208. RESET_STACK();
  8209. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8210. break;
  8211. case WASM_OP_NOP:
  8212. #if WASM_ENABLE_FAST_INTERP != 0
  8213. skip_label();
  8214. #endif
  8215. break;
  8216. case WASM_OP_IF:
  8217. #if WASM_ENABLE_FAST_INTERP != 0
  8218. PRESERVE_LOCAL_FOR_BLOCK();
  8219. #endif
  8220. POP_I32();
  8221. goto handle_op_block_and_loop;
  8222. case WASM_OP_BLOCK:
  8223. case WASM_OP_LOOP:
  8224. #if WASM_ENABLE_FAST_INTERP != 0
  8225. PRESERVE_LOCAL_FOR_BLOCK();
  8226. #endif
  8227. handle_op_block_and_loop:
  8228. {
  8229. uint8 value_type;
  8230. BlockType block_type;
  8231. CHECK_BUF(p, p_end, 1);
  8232. value_type = read_uint8(p);
  8233. if (is_byte_a_type(value_type)) {
  8234. /* If the first byte is one of these special values:
  8235. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  8236. * the single return value. */
  8237. block_type.is_value_type = true;
  8238. block_type.u.value_type.type = value_type;
  8239. #if WASM_ENABLE_GC != 0
  8240. if (value_type != VALUE_TYPE_VOID) {
  8241. p_org = p;
  8242. p--;
  8243. if (!resolve_value_type((const uint8 **)&p, p_end,
  8244. module, &need_ref_type_map,
  8245. &wasm_ref_type, false,
  8246. error_buf, error_buf_size)) {
  8247. goto fail;
  8248. }
  8249. if (need_ref_type_map) {
  8250. block_type.u.value_type.ref_type_map.index = 0;
  8251. if (!(block_type.u.value_type.ref_type_map
  8252. .ref_type = reftype_set_insert(
  8253. module->ref_type_set, &wasm_ref_type,
  8254. error_buf, error_buf_size))) {
  8255. goto fail;
  8256. }
  8257. }
  8258. /* Set again as the type might be changed, e.g.
  8259. (ref null any) to anyref */
  8260. block_type.u.value_type.type = wasm_ref_type.ref_type;
  8261. #if WASM_ENABLE_FAST_INTERP == 0
  8262. while (p_org < p) {
  8263. #if WASM_ENABLE_DEBUG_INTERP != 0
  8264. if (!record_fast_op(module, p_org, *p_org,
  8265. error_buf, error_buf_size)) {
  8266. goto fail;
  8267. }
  8268. #endif
  8269. /* Ignore extra bytes for interpreter */
  8270. *p_org++ = WASM_OP_NOP;
  8271. }
  8272. #endif
  8273. }
  8274. #endif /* end of WASM_ENABLE_GC != 0 */
  8275. }
  8276. else {
  8277. uint32 type_index;
  8278. /* Resolve the leb128 encoded type index as block type */
  8279. p--;
  8280. p_org = p - 1;
  8281. read_leb_uint32(p, p_end, type_index);
  8282. if (type_index >= module->type_count) {
  8283. set_error_buf(error_buf, error_buf_size,
  8284. "unknown type");
  8285. goto fail;
  8286. }
  8287. block_type.is_value_type = false;
  8288. block_type.u.type =
  8289. (WASMFuncType *)module->types[type_index];
  8290. #if WASM_ENABLE_FAST_INTERP == 0
  8291. /* If block use type index as block type, change the opcode
  8292. * to new extended opcode so that interpreter can resolve
  8293. * the block quickly.
  8294. */
  8295. #if WASM_ENABLE_DEBUG_INTERP != 0
  8296. if (!record_fast_op(module, p_org, *p_org, error_buf,
  8297. error_buf_size)) {
  8298. goto fail;
  8299. }
  8300. #endif
  8301. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  8302. #endif
  8303. }
  8304. /* Pop block parameters from stack */
  8305. if (BLOCK_HAS_PARAM(block_type)) {
  8306. WASMFuncType *func_type = block_type.u.type;
  8307. for (i = 0; i < block_type.u.type->param_count; i++)
  8308. POP_TYPE(
  8309. func_type->types[func_type->param_count - i - 1]);
  8310. }
  8311. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  8312. block_type, p);
  8313. /* Pass parameters to block */
  8314. if (BLOCK_HAS_PARAM(block_type)) {
  8315. for (i = 0; i < block_type.u.type->param_count; i++)
  8316. PUSH_TYPE(block_type.u.type->types[i]);
  8317. }
  8318. #if WASM_ENABLE_FAST_INTERP != 0
  8319. if (opcode == WASM_OP_BLOCK) {
  8320. skip_label();
  8321. }
  8322. else if (opcode == WASM_OP_LOOP) {
  8323. skip_label();
  8324. if (BLOCK_HAS_PARAM(block_type)) {
  8325. /* Make sure params are in dynamic space */
  8326. if (!copy_params_to_dynamic_space(
  8327. loader_ctx, false, error_buf, error_buf_size))
  8328. goto fail;
  8329. }
  8330. (loader_ctx->frame_csp - 1)->code_compiled =
  8331. loader_ctx->p_code_compiled;
  8332. }
  8333. else if (opcode == WASM_OP_IF) {
  8334. /* If block has parameters, we should make sure they are in
  8335. * dynamic space. Otherwise, when else branch is missing,
  8336. * the later opcode may consume incorrect operand offset.
  8337. * Spec case:
  8338. * (func (export "params-id") (param i32) (result i32)
  8339. * (i32.const 1)
  8340. * (i32.const 2)
  8341. * (if (param i32 i32) (result i32 i32) (local.get 0)
  8342. * (then)) (i32.add)
  8343. * )
  8344. *
  8345. * So we should emit a copy instruction before the if.
  8346. *
  8347. * And we also need to save the parameter offsets and
  8348. * recover them before entering else branch.
  8349. *
  8350. */
  8351. if (BLOCK_HAS_PARAM(block_type)) {
  8352. BranchBlock *block = loader_ctx->frame_csp - 1;
  8353. uint64 size;
  8354. /* skip the if condition operand offset */
  8355. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8356. /* skip the if label */
  8357. skip_label();
  8358. /* Emit a copy instruction */
  8359. if (!copy_params_to_dynamic_space(
  8360. loader_ctx, true, error_buf, error_buf_size))
  8361. goto fail;
  8362. /* Emit the if instruction */
  8363. emit_label(opcode);
  8364. /* Emit the new condition operand offset */
  8365. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  8366. /* Save top param_count values of frame_offset stack, so
  8367. * that we can recover it before executing else branch
  8368. */
  8369. size = sizeof(int16)
  8370. * (uint64)block_type.u.type->param_cell_num;
  8371. if (!(block->param_frame_offsets = loader_malloc(
  8372. size, error_buf, error_buf_size)))
  8373. goto fail;
  8374. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  8375. loader_ctx->frame_offset
  8376. - size / sizeof(int16),
  8377. (uint32)size);
  8378. }
  8379. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  8380. emit_empty_label_addr_and_frame_ip(PATCH_END);
  8381. }
  8382. #endif
  8383. break;
  8384. }
  8385. case WASM_OP_ELSE:
  8386. {
  8387. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  8388. if (loader_ctx->csp_num < 2
  8389. || (loader_ctx->frame_csp - 1)->label_type
  8390. != LABEL_TYPE_IF) {
  8391. set_error_buf(
  8392. error_buf, error_buf_size,
  8393. "opcode else found without matched opcode if");
  8394. goto fail;
  8395. }
  8396. /* check whether if branch's stack matches its result type */
  8397. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  8398. error_buf, error_buf_size))
  8399. goto fail;
  8400. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  8401. #if WASM_ENABLE_FAST_INTERP != 0
  8402. /* if the result of if branch is in local or const area, add a
  8403. * copy op */
  8404. RESERVE_BLOCK_RET();
  8405. emit_empty_label_addr_and_frame_ip(PATCH_END);
  8406. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  8407. #endif
  8408. RESET_STACK();
  8409. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  8410. /* Pass parameters to if-false branch */
  8411. if (BLOCK_HAS_PARAM(block_type)) {
  8412. for (i = 0; i < block_type.u.type->param_count; i++)
  8413. PUSH_TYPE(block_type.u.type->types[i]);
  8414. }
  8415. #if WASM_ENABLE_FAST_INTERP != 0
  8416. /* Recover top param_count values of frame_offset stack */
  8417. if (BLOCK_HAS_PARAM((block_type))) {
  8418. uint32 size;
  8419. BranchBlock *block = loader_ctx->frame_csp - 1;
  8420. size = sizeof(int16) * block_type.u.type->param_cell_num;
  8421. bh_memcpy_s(loader_ctx->frame_offset, size,
  8422. block->param_frame_offsets, size);
  8423. loader_ctx->frame_offset += (size / sizeof(int16));
  8424. }
  8425. #endif
  8426. break;
  8427. }
  8428. case WASM_OP_END:
  8429. {
  8430. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  8431. /* check whether block stack matches its result type */
  8432. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  8433. error_buf_size))
  8434. goto fail;
  8435. /* if no else branch, and return types do not match param types,
  8436. report failure */
  8437. if (cur_block->label_type == LABEL_TYPE_IF
  8438. && !cur_block->else_addr) {
  8439. uint32 block_param_count = 0, block_ret_count = 0;
  8440. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  8441. BlockType *cur_block_type = &cur_block->block_type;
  8442. #if WASM_ENABLE_GC != 0
  8443. uint32 block_param_reftype_map_count;
  8444. uint32 block_ret_reftype_map_count;
  8445. WASMRefTypeMap *block_param_reftype_maps;
  8446. WASMRefTypeMap *block_ret_reftype_maps;
  8447. #endif
  8448. block_param_count = block_type_get_param_types(
  8449. cur_block_type, &block_param_types
  8450. #if WASM_ENABLE_GC != 0
  8451. ,
  8452. &block_param_reftype_maps,
  8453. &block_param_reftype_map_count
  8454. #endif
  8455. );
  8456. block_ret_count = block_type_get_result_types(
  8457. cur_block_type, &block_ret_types
  8458. #if WASM_ENABLE_GC != 0
  8459. ,
  8460. &block_ret_reftype_maps, &block_ret_reftype_map_count
  8461. #endif
  8462. );
  8463. if (block_param_count != block_ret_count
  8464. || (block_param_count
  8465. && memcmp(block_param_types, block_ret_types,
  8466. block_param_count))) {
  8467. set_error_buf(error_buf, error_buf_size,
  8468. "type mismatch: else branch missing");
  8469. goto fail;
  8470. }
  8471. #if WASM_ENABLE_GC != 0
  8472. if (block_param_reftype_map_count
  8473. != block_ret_reftype_map_count
  8474. || (block_param_reftype_map_count
  8475. && memcmp(block_param_reftype_maps,
  8476. block_ret_reftype_maps,
  8477. sizeof(WASMRefTypeMap)
  8478. * block_param_reftype_map_count))) {
  8479. set_error_buf(error_buf, error_buf_size,
  8480. "type mismatch: else branch missing");
  8481. goto fail;
  8482. }
  8483. #endif
  8484. }
  8485. POP_CSP();
  8486. #if WASM_ENABLE_FAST_INTERP != 0
  8487. skip_label();
  8488. /* copy the result to the block return address */
  8489. RESERVE_BLOCK_RET();
  8490. apply_label_patch(loader_ctx, 0, PATCH_END);
  8491. free_label_patch_list(loader_ctx->frame_csp);
  8492. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  8493. int32 idx;
  8494. uint8 ret_type;
  8495. emit_label(WASM_OP_RETURN);
  8496. for (idx = (int32)func->func_type->result_count - 1;
  8497. idx >= 0; idx--) {
  8498. ret_type = *(func->func_type->types
  8499. + func->func_type->param_count + idx);
  8500. POP_OFFSET_TYPE(ret_type);
  8501. }
  8502. }
  8503. #endif
  8504. if (loader_ctx->csp_num > 0) {
  8505. loader_ctx->frame_csp->end_addr = p - 1;
  8506. }
  8507. else {
  8508. /* end of function block, function will return */
  8509. if (p < p_end) {
  8510. set_error_buf(error_buf, error_buf_size,
  8511. "section size mismatch");
  8512. goto fail;
  8513. }
  8514. }
  8515. break;
  8516. }
  8517. case WASM_OP_BR:
  8518. {
  8519. if (!(frame_csp_tmp =
  8520. check_branch_block(loader_ctx, &p, p_end, false,
  8521. error_buf, error_buf_size)))
  8522. goto fail;
  8523. RESET_STACK();
  8524. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8525. break;
  8526. }
  8527. case WASM_OP_BR_IF:
  8528. {
  8529. POP_I32();
  8530. if (!(frame_csp_tmp =
  8531. check_branch_block(loader_ctx, &p, p_end, false,
  8532. error_buf, error_buf_size)))
  8533. goto fail;
  8534. break;
  8535. }
  8536. case WASM_OP_BR_TABLE:
  8537. {
  8538. uint32 depth, default_arity, arity = 0;
  8539. BranchBlock *target_block;
  8540. BlockType *target_block_type;
  8541. #if WASM_ENABLE_FAST_INTERP == 0
  8542. BrTableCache *br_table_cache = NULL;
  8543. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  8544. uint32 j;
  8545. #endif
  8546. read_leb_uint32(p, p_end, count);
  8547. #if WASM_ENABLE_FAST_INTERP != 0
  8548. emit_uint32(loader_ctx, count);
  8549. #endif
  8550. POP_I32();
  8551. /* Get the default depth and check it */
  8552. p_org = p;
  8553. for (i = 0; i <= count; i++) {
  8554. read_leb_uint32(p, p_end, depth);
  8555. }
  8556. if (loader_ctx->csp_num < depth + 1) {
  8557. set_error_buf(error_buf, error_buf_size,
  8558. "unknown label, "
  8559. "unexpected end of section or function");
  8560. goto fail;
  8561. }
  8562. p = p_org;
  8563. /* Get the default block's arity */
  8564. target_block = loader_ctx->frame_csp - (depth + 1);
  8565. target_block_type = &target_block->block_type;
  8566. default_arity = block_type_get_arity(target_block_type,
  8567. target_block->label_type);
  8568. #if WASM_ENABLE_FAST_INTERP == 0
  8569. p_depth_begin = p_depth = p;
  8570. #endif
  8571. for (i = 0; i <= count; i++) {
  8572. p_org = p;
  8573. read_leb_uint32(p, p_end, depth);
  8574. if (loader_ctx->csp_num < depth + 1) {
  8575. set_error_buf(error_buf, error_buf_size,
  8576. "unknown label, "
  8577. "unexpected end of section or function");
  8578. goto fail;
  8579. }
  8580. p = p_org;
  8581. /* Get the target block's arity and check it */
  8582. target_block = loader_ctx->frame_csp - (depth + 1);
  8583. target_block_type = &target_block->block_type;
  8584. arity = block_type_get_arity(target_block_type,
  8585. target_block->label_type);
  8586. if (arity != default_arity) {
  8587. set_error_buf(error_buf, error_buf_size,
  8588. "type mismatch: br_table targets must "
  8589. "all use same result type");
  8590. goto fail;
  8591. }
  8592. if (!(frame_csp_tmp =
  8593. check_branch_block(loader_ctx, &p, p_end, true,
  8594. error_buf, error_buf_size))) {
  8595. goto fail;
  8596. }
  8597. #if WASM_ENABLE_FAST_INTERP == 0
  8598. if (br_table_cache) {
  8599. br_table_cache->br_depths[i] = depth;
  8600. }
  8601. else {
  8602. if (depth > 255) {
  8603. /* The depth cannot be stored in one byte,
  8604. create br_table cache to store each depth */
  8605. #if WASM_ENABLE_DEBUG_INTERP != 0
  8606. if (!record_fast_op(module, p_opcode, *p_opcode,
  8607. error_buf, error_buf_size)) {
  8608. goto fail;
  8609. }
  8610. #endif
  8611. if (!(br_table_cache = loader_malloc(
  8612. offsetof(BrTableCache, br_depths)
  8613. + sizeof(uint32)
  8614. * (uint64)(count + 1),
  8615. error_buf, error_buf_size))) {
  8616. goto fail;
  8617. }
  8618. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  8619. br_table_cache->br_table_op_addr = p_opcode;
  8620. br_table_cache->br_count = count;
  8621. /* Copy previous depths which are one byte */
  8622. for (j = 0; j < i; j++) {
  8623. br_table_cache->br_depths[j] = p_depth_begin[j];
  8624. }
  8625. br_table_cache->br_depths[i] = depth;
  8626. bh_list_insert(module->br_table_cache_list,
  8627. br_table_cache);
  8628. }
  8629. else {
  8630. /* The depth can be stored in one byte, use the
  8631. byte of the leb to store it */
  8632. *p_depth++ = (uint8)depth;
  8633. }
  8634. }
  8635. #endif
  8636. }
  8637. #if WASM_ENABLE_FAST_INTERP == 0
  8638. /* Set the tailing bytes to nop */
  8639. if (br_table_cache)
  8640. p_depth = p_depth_begin;
  8641. while (p_depth < p)
  8642. *p_depth++ = WASM_OP_NOP;
  8643. #endif
  8644. RESET_STACK();
  8645. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8646. break;
  8647. }
  8648. case WASM_OP_RETURN:
  8649. {
  8650. int32 idx;
  8651. uint8 ret_type;
  8652. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  8653. idx--) {
  8654. ret_type = *(func->func_type->types
  8655. + func->func_type->param_count + idx);
  8656. POP_TYPE(ret_type);
  8657. #if WASM_ENABLE_FAST_INTERP != 0
  8658. /* emit the offset after return opcode */
  8659. POP_OFFSET_TYPE(ret_type);
  8660. #endif
  8661. }
  8662. RESET_STACK();
  8663. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8664. break;
  8665. }
  8666. case WASM_OP_CALL:
  8667. #if WASM_ENABLE_TAIL_CALL != 0
  8668. case WASM_OP_RETURN_CALL:
  8669. #endif
  8670. #if WASM_ENABLE_GC != 0
  8671. case WASM_OP_CALL_REF:
  8672. case WASM_OP_RETURN_CALL_REF:
  8673. #endif
  8674. {
  8675. WASMFuncType *func_type;
  8676. uint8 type;
  8677. int32 idx;
  8678. #if WASM_ENABLE_GC != 0
  8679. WASMRefType *ref_type;
  8680. uint32 type_idx1;
  8681. int32 j;
  8682. #endif
  8683. #if WASM_ENABLE_GC != 0
  8684. if (opcode == WASM_OP_CALL_REF
  8685. || opcode == WASM_OP_RETURN_CALL_REF) {
  8686. read_leb_uint32(p, p_end, type_idx1);
  8687. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  8688. &type_idx, error_buf,
  8689. error_buf_size)) {
  8690. goto fail;
  8691. }
  8692. if (type == VALUE_TYPE_ANY) {
  8693. type_idx = type_idx1;
  8694. }
  8695. if (!check_type_index(module, type_idx, error_buf,
  8696. error_buf_size)) {
  8697. goto fail;
  8698. }
  8699. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  8700. set_error_buf(error_buf, error_buf_size,
  8701. "unkown function type");
  8702. goto fail;
  8703. }
  8704. if (type_idx != type_idx1) {
  8705. set_error_buf(error_buf, error_buf_size,
  8706. "function type mismatch");
  8707. goto fail;
  8708. }
  8709. func_type = (WASMFuncType *)module->types[type_idx];
  8710. }
  8711. else
  8712. #endif
  8713. {
  8714. read_leb_uint32(p, p_end, func_idx);
  8715. #if WASM_ENABLE_FAST_INTERP != 0
  8716. /* we need to emit func_idx before arguments */
  8717. emit_uint32(loader_ctx, func_idx);
  8718. #endif
  8719. if (!check_function_index(module, func_idx, error_buf,
  8720. error_buf_size)) {
  8721. goto fail;
  8722. }
  8723. if (func_idx < module->import_function_count)
  8724. func_type = module->import_functions[func_idx]
  8725. .u.function.func_type;
  8726. else
  8727. func_type =
  8728. module
  8729. ->functions[func_idx
  8730. - module->import_function_count]
  8731. ->func_type;
  8732. }
  8733. if (func_type->param_count > 0) {
  8734. #if WASM_ENABLE_GC != 0
  8735. j = (int32)(func_type->result_ref_type_maps
  8736. - func_type->ref_type_maps - 1);
  8737. #endif
  8738. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  8739. idx--) {
  8740. #if WASM_ENABLE_GC != 0
  8741. if (wasm_is_type_multi_byte_type(
  8742. func_type->types[idx])) {
  8743. ref_type = func_type->ref_type_maps[j].ref_type;
  8744. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  8745. ref_type,
  8746. wasm_reftype_struct_size(ref_type));
  8747. j--;
  8748. }
  8749. #endif
  8750. POP_TYPE(func_type->types[idx]);
  8751. #if WASM_ENABLE_FAST_INTERP != 0
  8752. POP_OFFSET_TYPE(func_type->types[idx]);
  8753. #endif
  8754. }
  8755. }
  8756. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  8757. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  8758. #endif
  8759. #if WASM_ENABLE_GC != 0
  8760. j = (int32)(func_type->result_ref_type_maps
  8761. - func_type->ref_type_maps);
  8762. #endif
  8763. for (i = 0; i < func_type->result_count; i++) {
  8764. #if WASM_ENABLE_GC != 0
  8765. if (wasm_is_type_multi_byte_type(
  8766. func_type->types[func_type->param_count + i])) {
  8767. ref_type = func_type->ref_type_maps[j].ref_type;
  8768. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  8769. ref_type,
  8770. wasm_reftype_struct_size(ref_type));
  8771. j++;
  8772. }
  8773. #endif
  8774. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  8775. #if WASM_ENABLE_FAST_INTERP != 0
  8776. /* Here we emit each return value's dynamic_offset. But
  8777. * in fact these offsets are continuous, so interpreter
  8778. * only need to get the first return value's offset.
  8779. */
  8780. PUSH_OFFSET_TYPE(
  8781. func_type->types[func_type->param_count + i]);
  8782. #endif
  8783. }
  8784. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  8785. }
  8786. else {
  8787. #if WASM_ENABLE_GC == 0
  8788. if (func_type->result_count
  8789. != func->func_type->result_count) {
  8790. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  8791. "type mismatch: expect ",
  8792. func->func_type->result_count,
  8793. " return values but got other");
  8794. goto fail;
  8795. }
  8796. for (i = 0; i < func_type->result_count; i++) {
  8797. type = func->func_type
  8798. ->types[func->func_type->param_count + i];
  8799. if (func_type->types[func_type->param_count + i]
  8800. != type) {
  8801. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8802. "type mismatch: expect ",
  8803. type2str(type), " but got other");
  8804. goto fail;
  8805. }
  8806. }
  8807. #else
  8808. if (!wasm_func_type_result_is_subtype_of(
  8809. func_type, func->func_type, module->types,
  8810. module->type_count)) {
  8811. set_error_buf(
  8812. error_buf, error_buf_size,
  8813. "type mismatch: invalid func result types");
  8814. goto fail;
  8815. }
  8816. #endif
  8817. RESET_STACK();
  8818. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8819. }
  8820. #endif
  8821. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  8822. || WASM_ENABLE_WAMR_COMPILER != 0
  8823. func->has_op_func_call = true;
  8824. #endif
  8825. (void)type;
  8826. break;
  8827. }
  8828. /*
  8829. * if disable reference type: call_indirect typeidx, 0x00
  8830. * if enable reference type: call_indirect typeidx, tableidx
  8831. */
  8832. case WASM_OP_CALL_INDIRECT:
  8833. #if WASM_ENABLE_TAIL_CALL != 0
  8834. case WASM_OP_RETURN_CALL_INDIRECT:
  8835. #endif
  8836. {
  8837. int32 idx;
  8838. WASMFuncType *func_type;
  8839. read_leb_uint32(p, p_end, type_idx);
  8840. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  8841. read_leb_uint32(p, p_end, table_idx);
  8842. #else
  8843. CHECK_BUF(p, p_end, 1);
  8844. table_idx = read_uint8(p);
  8845. #endif
  8846. if (!check_table_index(module, table_idx, error_buf,
  8847. error_buf_size)) {
  8848. goto fail;
  8849. }
  8850. #if WASM_ENABLE_FAST_INTERP != 0
  8851. /* we need to emit before arguments */
  8852. #if WASM_ENABLE_TAIL_CALL != 0
  8853. emit_byte(loader_ctx, opcode);
  8854. #endif
  8855. emit_uint32(loader_ctx, type_idx);
  8856. emit_uint32(loader_ctx, table_idx);
  8857. #endif
  8858. /* skip elem idx */
  8859. POP_I32();
  8860. if (type_idx >= module->type_count) {
  8861. set_error_buf(error_buf, error_buf_size, "unknown type");
  8862. goto fail;
  8863. }
  8864. func_type = (WASMFuncType *)module->types[type_idx];
  8865. if (func_type->param_count > 0) {
  8866. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  8867. idx--) {
  8868. POP_TYPE(func_type->types[idx]);
  8869. #if WASM_ENABLE_FAST_INTERP != 0
  8870. POP_OFFSET_TYPE(func_type->types[idx]);
  8871. #endif
  8872. }
  8873. }
  8874. #if WASM_ENABLE_TAIL_CALL != 0
  8875. if (opcode == WASM_OP_CALL_INDIRECT) {
  8876. #endif
  8877. for (i = 0; i < func_type->result_count; i++) {
  8878. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  8879. #if WASM_ENABLE_FAST_INTERP != 0
  8880. PUSH_OFFSET_TYPE(
  8881. func_type->types[func_type->param_count + i]);
  8882. #endif
  8883. }
  8884. #if WASM_ENABLE_TAIL_CALL != 0
  8885. }
  8886. else {
  8887. uint8 type;
  8888. if (func_type->result_count
  8889. != func->func_type->result_count) {
  8890. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  8891. "type mismatch: expect ",
  8892. func->func_type->result_count,
  8893. " return values but got other");
  8894. goto fail;
  8895. }
  8896. for (i = 0; i < func_type->result_count; i++) {
  8897. type = func->func_type
  8898. ->types[func->func_type->param_count + i];
  8899. if (func_type->types[func_type->param_count + i]
  8900. != type) {
  8901. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8902. "type mismatch: expect ",
  8903. type2str(type), " but got other");
  8904. goto fail;
  8905. }
  8906. }
  8907. RESET_STACK();
  8908. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  8909. }
  8910. #endif
  8911. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  8912. || WASM_ENABLE_WAMR_COMPILER != 0
  8913. func->has_op_func_call = true;
  8914. #endif
  8915. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8916. func->has_op_call_indirect = true;
  8917. #endif
  8918. break;
  8919. }
  8920. case WASM_OP_DROP:
  8921. {
  8922. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  8923. int32 available_stack_cell =
  8924. (int32)(loader_ctx->stack_cell_num
  8925. - cur_block->stack_cell_num);
  8926. if (available_stack_cell <= 0
  8927. && !cur_block->is_stack_polymorphic) {
  8928. set_error_buf(error_buf, error_buf_size,
  8929. "type mismatch, opcode drop was found "
  8930. "but stack was empty");
  8931. goto fail;
  8932. }
  8933. if (available_stack_cell > 0) {
  8934. #if WASM_ENABLE_GC != 0
  8935. if (wasm_is_type_multi_byte_type(
  8936. *(loader_ctx->frame_ref - 1))) {
  8937. bh_assert((int32)(loader_ctx->reftype_map_num
  8938. - cur_block->reftype_map_num)
  8939. > 0);
  8940. loader_ctx->frame_reftype_map--;
  8941. loader_ctx->reftype_map_num--;
  8942. }
  8943. #endif
  8944. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  8945. loader_ctx->frame_ref--;
  8946. loader_ctx->stack_cell_num--;
  8947. #if WASM_ENABLE_FAST_INTERP != 0
  8948. skip_label();
  8949. loader_ctx->frame_offset--;
  8950. if ((*(loader_ctx->frame_offset)
  8951. > loader_ctx->start_dynamic_offset)
  8952. && (*(loader_ctx->frame_offset)
  8953. < loader_ctx->max_dynamic_offset))
  8954. loader_ctx->dynamic_offset--;
  8955. #endif
  8956. }
  8957. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  8958. loader_ctx->frame_ref -= 2;
  8959. loader_ctx->stack_cell_num -= 2;
  8960. #if WASM_ENABLE_FAST_INTERP == 0
  8961. *(p - 1) = WASM_OP_DROP_64;
  8962. #endif
  8963. #if WASM_ENABLE_FAST_INTERP != 0
  8964. skip_label();
  8965. loader_ctx->frame_offset -= 2;
  8966. if ((*(loader_ctx->frame_offset)
  8967. > loader_ctx->start_dynamic_offset)
  8968. && (*(loader_ctx->frame_offset)
  8969. < loader_ctx->max_dynamic_offset))
  8970. loader_ctx->dynamic_offset -= 2;
  8971. #endif
  8972. }
  8973. #if WASM_ENABLE_SIMD != 0
  8974. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8975. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  8976. loader_ctx->frame_ref -= 4;
  8977. loader_ctx->stack_cell_num -= 4;
  8978. }
  8979. #endif
  8980. #endif
  8981. else {
  8982. set_error_buf(error_buf, error_buf_size,
  8983. "type mismatch");
  8984. goto fail;
  8985. }
  8986. }
  8987. else {
  8988. #if WASM_ENABLE_FAST_INTERP != 0
  8989. skip_label();
  8990. #endif
  8991. }
  8992. break;
  8993. }
  8994. case WASM_OP_SELECT:
  8995. {
  8996. uint8 ref_type;
  8997. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  8998. int32 available_stack_cell;
  8999. POP_I32();
  9000. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  9001. - cur_block->stack_cell_num);
  9002. if (available_stack_cell <= 0
  9003. && !cur_block->is_stack_polymorphic) {
  9004. set_error_buf(error_buf, error_buf_size,
  9005. "type mismatch or invalid result arity, "
  9006. "opcode select was found "
  9007. "but stack was empty");
  9008. goto fail;
  9009. }
  9010. if (available_stack_cell > 0) {
  9011. switch (*(loader_ctx->frame_ref - 1)) {
  9012. case VALUE_TYPE_I32:
  9013. case VALUE_TYPE_F32:
  9014. break;
  9015. case VALUE_TYPE_I64:
  9016. case VALUE_TYPE_F64:
  9017. #if WASM_ENABLE_FAST_INTERP == 0
  9018. *(p - 1) = WASM_OP_SELECT_64;
  9019. #endif
  9020. #if WASM_ENABLE_FAST_INTERP != 0
  9021. if (loader_ctx->p_code_compiled) {
  9022. uint8 opcode_tmp = WASM_OP_SELECT_64;
  9023. uint8 *p_code_compiled_tmp =
  9024. loader_ctx->p_code_compiled - 2;
  9025. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  9026. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9027. *(void **)(p_code_compiled_tmp
  9028. - sizeof(void *)) =
  9029. handle_table[opcode_tmp];
  9030. #else
  9031. int32 offset =
  9032. (int32)((uint8 *)handle_table[opcode_tmp]
  9033. - (uint8 *)handle_table[0]);
  9034. if (!(offset >= INT16_MIN
  9035. && offset < INT16_MAX)) {
  9036. set_error_buf(error_buf, error_buf_size,
  9037. "pre-compiled label offset "
  9038. "out of range");
  9039. goto fail;
  9040. }
  9041. *(int16 *)(p_code_compiled_tmp
  9042. - sizeof(int16)) = (int16)offset;
  9043. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9044. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  9045. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9046. *(p_code_compiled_tmp - 1) = opcode_tmp;
  9047. #else
  9048. *(p_code_compiled_tmp - 2) = opcode_tmp;
  9049. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9050. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  9051. }
  9052. #endif /* end of WASM_ENABLE_FAST_INTERP */
  9053. break;
  9054. #if WASM_ENABLE_SIMD != 0
  9055. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  9056. case VALUE_TYPE_V128:
  9057. break;
  9058. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9059. #endif /* WASM_ENABLE_SIMD != 0 */
  9060. default:
  9061. {
  9062. set_error_buf(error_buf, error_buf_size,
  9063. "type mismatch");
  9064. goto fail;
  9065. }
  9066. }
  9067. ref_type = *(loader_ctx->frame_ref - 1);
  9068. #if WASM_ENABLE_FAST_INTERP != 0
  9069. POP_OFFSET_TYPE(ref_type);
  9070. POP_TYPE(ref_type);
  9071. POP_OFFSET_TYPE(ref_type);
  9072. POP_TYPE(ref_type);
  9073. PUSH_OFFSET_TYPE(ref_type);
  9074. PUSH_TYPE(ref_type);
  9075. #else
  9076. POP2_AND_PUSH(ref_type, ref_type);
  9077. #endif
  9078. }
  9079. else {
  9080. #if WASM_ENABLE_FAST_INTERP != 0
  9081. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  9082. #endif
  9083. PUSH_TYPE(VALUE_TYPE_ANY);
  9084. }
  9085. break;
  9086. }
  9087. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9088. case WASM_OP_SELECT_T:
  9089. {
  9090. uint8 vec_len, type;
  9091. #if WASM_ENABLE_GC != 0
  9092. WASMRefType *ref_type = NULL;
  9093. #endif
  9094. read_leb_uint32(p, p_end, vec_len);
  9095. if (!vec_len) {
  9096. set_error_buf(error_buf, error_buf_size,
  9097. "invalid result arity");
  9098. goto fail;
  9099. }
  9100. #if WASM_ENABLE_GC == 0
  9101. CHECK_BUF(p, p_end, 1);
  9102. type = read_uint8(p);
  9103. if (!is_value_type(type)) {
  9104. set_error_buf(error_buf, error_buf_size,
  9105. "unknown value type");
  9106. goto fail;
  9107. }
  9108. #else
  9109. p_org = p + 1;
  9110. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  9111. &need_ref_type_map, &wasm_ref_type,
  9112. false, error_buf, error_buf_size)) {
  9113. goto fail;
  9114. }
  9115. type = wasm_ref_type.ref_type;
  9116. if (need_ref_type_map) {
  9117. if (!(ref_type = reftype_set_insert(
  9118. module->ref_type_set, &wasm_ref_type, error_buf,
  9119. error_buf_size))) {
  9120. goto fail;
  9121. }
  9122. }
  9123. #if WASM_ENABLE_FAST_INTERP == 0
  9124. while (p_org < p) {
  9125. #if WASM_ENABLE_DEBUG_INTERP != 0
  9126. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9127. error_buf_size)) {
  9128. goto fail;
  9129. }
  9130. #endif
  9131. /* Ignore extra bytes for interpreter */
  9132. *p_org++ = WASM_OP_NOP;
  9133. }
  9134. #endif
  9135. #endif /* end of WASM_ENABLE_GC == 0 */
  9136. POP_I32();
  9137. #if WASM_ENABLE_FAST_INTERP != 0
  9138. if (loader_ctx->p_code_compiled) {
  9139. uint8 opcode_tmp = WASM_OP_SELECT;
  9140. uint8 *p_code_compiled_tmp =
  9141. loader_ctx->p_code_compiled - 2;
  9142. if (type == VALUE_TYPE_V128) {
  9143. #if (WASM_ENABLE_SIMD == 0) \
  9144. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  9145. set_error_buf(error_buf, error_buf_size,
  9146. "SIMD v128 type isn't supported");
  9147. goto fail;
  9148. #endif
  9149. }
  9150. else {
  9151. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  9152. opcode_tmp = WASM_OP_SELECT_64;
  9153. #if WASM_ENABLE_GC != 0
  9154. if (wasm_is_type_reftype(type))
  9155. opcode_tmp = WASM_OP_SELECT_T;
  9156. #endif
  9157. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  9158. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9159. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  9160. handle_table[opcode_tmp];
  9161. #else
  9162. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  9163. - (uint8 *)handle_table[0]);
  9164. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  9165. set_error_buf(
  9166. error_buf, error_buf_size,
  9167. "pre-compiled label offset out of range");
  9168. goto fail;
  9169. }
  9170. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  9171. (int16)offset;
  9172. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9173. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  9174. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  9175. *(p_code_compiled_tmp - 1) = opcode_tmp;
  9176. #else
  9177. *(p_code_compiled_tmp - 2) = opcode_tmp;
  9178. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  9179. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  9180. }
  9181. }
  9182. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  9183. POP_REF(type);
  9184. #if WASM_ENABLE_GC != 0
  9185. if (need_ref_type_map) {
  9186. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9187. wasm_reftype_struct_size(ref_type));
  9188. }
  9189. #endif
  9190. POP_REF(type);
  9191. #if WASM_ENABLE_GC != 0
  9192. if (need_ref_type_map) {
  9193. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9194. wasm_reftype_struct_size(ref_type));
  9195. }
  9196. #endif
  9197. PUSH_REF(type);
  9198. (void)vec_len;
  9199. break;
  9200. }
  9201. /* table.get x. tables[x]. [i32] -> [t] */
  9202. /* table.set x. tables[x]. [i32 t] -> [] */
  9203. case WASM_OP_TABLE_GET:
  9204. case WASM_OP_TABLE_SET:
  9205. {
  9206. uint8 decl_ref_type;
  9207. #if WASM_ENABLE_GC != 0
  9208. WASMRefType *ref_type;
  9209. #endif
  9210. read_leb_uint32(p, p_end, table_idx);
  9211. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  9212. #if WASM_ENABLE_GC != 0
  9213. (void **)&ref_type,
  9214. #else
  9215. NULL,
  9216. #endif
  9217. error_buf, error_buf_size))
  9218. goto fail;
  9219. #if WASM_ENABLE_GC != 0
  9220. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  9221. bh_assert(ref_type);
  9222. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  9223. ref_type, wasm_reftype_struct_size(ref_type));
  9224. }
  9225. #endif
  9226. #if WASM_ENABLE_FAST_INTERP != 0
  9227. emit_uint32(loader_ctx, table_idx);
  9228. #endif
  9229. if (opcode == WASM_OP_TABLE_GET) {
  9230. POP_I32();
  9231. #if WASM_ENABLE_FAST_INTERP != 0
  9232. PUSH_OFFSET_TYPE(decl_ref_type);
  9233. #endif
  9234. PUSH_TYPE(decl_ref_type);
  9235. }
  9236. else {
  9237. #if WASM_ENABLE_FAST_INTERP != 0
  9238. POP_OFFSET_TYPE(decl_ref_type);
  9239. #endif
  9240. POP_TYPE(decl_ref_type);
  9241. POP_I32();
  9242. }
  9243. break;
  9244. }
  9245. case WASM_OP_REF_NULL:
  9246. {
  9247. uint8 ref_type;
  9248. CHECK_BUF(p, p_end, 1);
  9249. ref_type = read_uint8(p);
  9250. #if WASM_ENABLE_GC == 0
  9251. if (ref_type != VALUE_TYPE_FUNCREF
  9252. && ref_type != VALUE_TYPE_EXTERNREF) {
  9253. set_error_buf(error_buf, error_buf_size, "type mismatch");
  9254. goto fail;
  9255. }
  9256. #else
  9257. p--;
  9258. if (is_byte_a_type(ref_type)) {
  9259. p_org = p + 1;
  9260. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  9261. &need_ref_type_map, &wasm_ref_type,
  9262. false, error_buf, error_buf_size)) {
  9263. goto fail;
  9264. }
  9265. ref_type = wasm_ref_type.ref_type;
  9266. #if WASM_ENABLE_FAST_INTERP == 0
  9267. while (p_org < p) {
  9268. #if WASM_ENABLE_DEBUG_INTERP != 0
  9269. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9270. error_buf_size)) {
  9271. goto fail;
  9272. }
  9273. #endif
  9274. /* Ignore extra bytes for interpreter */
  9275. *p_org++ = WASM_OP_NOP;
  9276. }
  9277. #endif
  9278. }
  9279. else {
  9280. read_leb_uint32(p, p_end, type_idx);
  9281. if (!check_type_index(module, type_idx, error_buf,
  9282. error_buf_size)) {
  9283. goto fail;
  9284. }
  9285. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  9286. true, type_idx);
  9287. ref_type = wasm_ref_type.ref_type;
  9288. }
  9289. #endif /* end of WASM_ENABLE_GC == 0 */
  9290. #if WASM_ENABLE_FAST_INTERP != 0
  9291. PUSH_OFFSET_TYPE(ref_type);
  9292. #endif
  9293. PUSH_TYPE(ref_type);
  9294. break;
  9295. }
  9296. case WASM_OP_REF_IS_NULL:
  9297. {
  9298. #if WASM_ENABLE_GC == 0
  9299. #if WASM_ENABLE_FAST_INTERP != 0
  9300. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  9301. VALUE_TYPE_FUNCREF,
  9302. error_buf, error_buf_size)
  9303. && !wasm_loader_pop_frame_ref_offset(
  9304. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  9305. error_buf_size)) {
  9306. goto fail;
  9307. }
  9308. #else
  9309. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  9310. error_buf, error_buf_size)
  9311. && !wasm_loader_pop_frame_ref(loader_ctx,
  9312. VALUE_TYPE_EXTERNREF,
  9313. error_buf, error_buf_size)) {
  9314. goto fail;
  9315. }
  9316. #endif
  9317. #else /* else of WASM_ENABLE_GC == 0 */
  9318. uint8 type;
  9319. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  9320. error_buf, error_buf_size)) {
  9321. goto fail;
  9322. }
  9323. #endif
  9324. PUSH_I32();
  9325. break;
  9326. }
  9327. case WASM_OP_REF_FUNC:
  9328. {
  9329. read_leb_uint32(p, p_end, func_idx);
  9330. if (!check_function_index(module, func_idx, error_buf,
  9331. error_buf_size)) {
  9332. goto fail;
  9333. }
  9334. /* Refer to a forward-declared function */
  9335. if (func_idx >= cur_func_idx + module->import_function_count) {
  9336. WASMTableSeg *table_seg = module->table_segments;
  9337. bool func_declared = false;
  9338. uint32 j;
  9339. /* Check whether the function is declared in table segs */
  9340. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  9341. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  9342. && wasm_elem_is_declarative(table_seg->mode)) {
  9343. for (j = 0; j < table_seg->function_count; j++) {
  9344. if (table_seg->func_indexes[j] == func_idx) {
  9345. func_declared = true;
  9346. break;
  9347. }
  9348. }
  9349. }
  9350. }
  9351. if (!func_declared) {
  9352. /* Check whether the function is exported */
  9353. for (i = 0; i < module->export_count; i++) {
  9354. if (module->exports[i].kind == EXPORT_KIND_FUNC
  9355. && module->exports[i].index == func_idx) {
  9356. func_declared = true;
  9357. break;
  9358. }
  9359. }
  9360. }
  9361. if (!func_declared) {
  9362. set_error_buf(error_buf, error_buf_size,
  9363. "undeclared function reference");
  9364. goto fail;
  9365. }
  9366. }
  9367. #if WASM_ENABLE_FAST_INTERP != 0
  9368. emit_uint32(loader_ctx, func_idx);
  9369. #endif
  9370. #if WASM_ENABLE_GC == 0
  9371. PUSH_FUNCREF();
  9372. #else
  9373. if (func_idx < module->import_function_count)
  9374. type_idx =
  9375. module->import_functions[func_idx].u.function.type_idx;
  9376. else
  9377. type_idx = module
  9378. ->functions[func_idx
  9379. - module->import_function_count]
  9380. ->type_idx;
  9381. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  9382. false, type_idx);
  9383. PUSH_REF(wasm_ref_type.ref_type);
  9384. #endif
  9385. break;
  9386. }
  9387. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  9388. #if WASM_ENABLE_GC != 0
  9389. case WASM_OP_REF_AS_NON_NULL:
  9390. case WASM_OP_BR_ON_NULL:
  9391. {
  9392. uint8 type;
  9393. WASMRefType ref_type;
  9394. /* POP (ref null ht) and get the converted (ref ht) */
  9395. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  9396. error_buf, error_buf_size)) {
  9397. goto fail;
  9398. }
  9399. if (opcode == WASM_OP_BR_ON_NULL) {
  9400. if (!(frame_csp_tmp =
  9401. check_branch_block(loader_ctx, &p, p_end, false,
  9402. error_buf, error_buf_size))) {
  9403. goto fail;
  9404. }
  9405. }
  9406. #if WASM_ENABLE_FAST_INTERP != 0
  9407. disable_emit = true;
  9408. #endif
  9409. /* PUSH the converted (ref ht) */
  9410. if (type != VALUE_TYPE_ANY) {
  9411. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  9412. sizeof(WASMRefType));
  9413. }
  9414. PUSH_REF(type);
  9415. break;
  9416. }
  9417. case WASM_OP_BR_ON_NON_NULL:
  9418. {
  9419. uint8 type;
  9420. WASMRefType ref_type;
  9421. uint32 available_stack_cell =
  9422. loader_ctx->stack_cell_num
  9423. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  9424. /* POP (ref null ht) and get the converted (ref ht) */
  9425. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  9426. error_buf, error_buf_size)) {
  9427. goto fail;
  9428. }
  9429. #if WASM_ENABLE_FAST_INTERP != 0
  9430. disable_emit = true;
  9431. #endif
  9432. /* Temporarily PUSH back (ref ht), check brach block and
  9433. then POP it */
  9434. if (available_stack_cell
  9435. > 0) { /* stack isn't in polymorphic state */
  9436. if (type != VALUE_TYPE_ANY) {
  9437. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  9438. &ref_type, sizeof(WASMRefType));
  9439. }
  9440. PUSH_REF(type);
  9441. }
  9442. if (!(frame_csp_tmp =
  9443. check_branch_block(loader_ctx, &p, p_end, false,
  9444. error_buf, error_buf_size))) {
  9445. goto fail;
  9446. }
  9447. if (available_stack_cell
  9448. > 0) { /* stack isn't in polymorphic state */
  9449. POP_REF(type);
  9450. #if WASM_ENABLE_FAST_INTERP != 0
  9451. /* Erase the opnd offset emitted by POP_REF() */
  9452. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  9453. #endif
  9454. }
  9455. break;
  9456. }
  9457. case WASM_OP_REF_EQ:
  9458. POP_REF(REF_TYPE_EQREF);
  9459. POP_REF(REF_TYPE_EQREF);
  9460. PUSH_I32();
  9461. break;
  9462. #endif /* end of WASM_ENABLE_GC != 0 */
  9463. case WASM_OP_GET_LOCAL:
  9464. {
  9465. p_org = p - 1;
  9466. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  9467. PUSH_TYPE(local_type);
  9468. #if WASM_ENABLE_GC != 0
  9469. /* Cannot get a non-nullable and unset local */
  9470. if (local_idx >= param_count
  9471. && wasm_is_reftype_htref_non_nullable(local_type)) {
  9472. set_error_buf(error_buf, error_buf_size,
  9473. "uninitialized local");
  9474. return false;
  9475. }
  9476. #endif
  9477. #if WASM_ENABLE_FAST_INTERP != 0
  9478. /* Get Local is optimized out */
  9479. skip_label();
  9480. disable_emit = true;
  9481. operand_offset = local_offset;
  9482. PUSH_OFFSET_TYPE(local_type);
  9483. #else
  9484. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  9485. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  9486. if (local_offset < 0x80
  9487. #if WASM_ENABLE_GC != 0
  9488. && !wasm_is_type_reftype(local_type)
  9489. #endif
  9490. ) {
  9491. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  9492. if (is_32bit_type(local_type)) {
  9493. *p_org++ = (uint8)local_offset;
  9494. }
  9495. else {
  9496. *p_org++ = (uint8)(local_offset | 0x80);
  9497. }
  9498. while (p_org < p) {
  9499. *p_org++ = WASM_OP_NOP;
  9500. }
  9501. }
  9502. #endif
  9503. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9504. break;
  9505. }
  9506. case WASM_OP_SET_LOCAL:
  9507. {
  9508. p_org = p - 1;
  9509. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  9510. POP_TYPE(local_type);
  9511. #if WASM_ENABLE_FAST_INTERP != 0
  9512. if (!(preserve_referenced_local(
  9513. loader_ctx, opcode, local_offset, local_type,
  9514. &preserve_local, error_buf, error_buf_size)))
  9515. goto fail;
  9516. if (local_offset < 256
  9517. #if WASM_ENABLE_GC != 0
  9518. && !wasm_is_type_reftype(local_type)
  9519. #endif
  9520. ) {
  9521. skip_label();
  9522. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  9523. if (loader_ctx->p_code_compiled)
  9524. STORE_U16(loader_ctx->p_code_compiled - 2,
  9525. local_offset);
  9526. loader_ctx->frame_offset--;
  9527. loader_ctx->dynamic_offset--;
  9528. }
  9529. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  9530. if (loader_ctx->p_code_compiled)
  9531. STORE_U16(loader_ctx->p_code_compiled - 2,
  9532. local_offset);
  9533. loader_ctx->frame_offset -= 2;
  9534. loader_ctx->dynamic_offset -= 2;
  9535. }
  9536. else {
  9537. if (is_32bit_type(local_type)) {
  9538. emit_label(EXT_OP_SET_LOCAL_FAST);
  9539. emit_byte(loader_ctx, (uint8)local_offset);
  9540. }
  9541. else {
  9542. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  9543. emit_byte(loader_ctx, (uint8)local_offset);
  9544. }
  9545. POP_OFFSET_TYPE(local_type);
  9546. }
  9547. }
  9548. else { /* local index larger than 255, reserve leb */
  9549. emit_uint32(loader_ctx, local_idx);
  9550. POP_OFFSET_TYPE(local_type);
  9551. }
  9552. #else
  9553. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  9554. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  9555. if (local_offset < 0x80
  9556. #if WASM_ENABLE_GC != 0
  9557. && !wasm_is_type_reftype(local_type)
  9558. #endif
  9559. ) {
  9560. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  9561. if (is_32bit_type(local_type)) {
  9562. *p_org++ = (uint8)local_offset;
  9563. }
  9564. else {
  9565. *p_org++ = (uint8)(local_offset | 0x80);
  9566. }
  9567. while (p_org < p) {
  9568. *p_org++ = WASM_OP_NOP;
  9569. }
  9570. }
  9571. #endif
  9572. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9573. break;
  9574. }
  9575. case WASM_OP_TEE_LOCAL:
  9576. {
  9577. p_org = p - 1;
  9578. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  9579. #if WASM_ENABLE_FAST_INTERP != 0
  9580. /* If the stack is in polymorphic state, do fake pop and push on
  9581. offset stack to keep the depth of offset stack to be the
  9582. same with ref stack */
  9583. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9584. if (cur_block->is_stack_polymorphic) {
  9585. POP_OFFSET_TYPE(local_type);
  9586. PUSH_OFFSET_TYPE(local_type);
  9587. }
  9588. #endif
  9589. POP_TYPE(local_type);
  9590. PUSH_TYPE(local_type);
  9591. #if WASM_ENABLE_FAST_INTERP != 0
  9592. if (!(preserve_referenced_local(
  9593. loader_ctx, opcode, local_offset, local_type,
  9594. &preserve_local, error_buf, error_buf_size)))
  9595. goto fail;
  9596. if (local_offset < 256
  9597. #if WASM_ENABLE_GC != 0
  9598. && !wasm_is_type_reftype(local_type)
  9599. #endif
  9600. ) {
  9601. skip_label();
  9602. if (is_32bit_type(local_type)) {
  9603. emit_label(EXT_OP_TEE_LOCAL_FAST);
  9604. emit_byte(loader_ctx, (uint8)local_offset);
  9605. }
  9606. else {
  9607. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  9608. emit_byte(loader_ctx, (uint8)local_offset);
  9609. }
  9610. }
  9611. else { /* local index larger than 255, reserve leb */
  9612. emit_uint32(loader_ctx, local_idx);
  9613. }
  9614. emit_operand(loader_ctx,
  9615. *(loader_ctx->frame_offset
  9616. - wasm_value_type_cell_num(local_type)));
  9617. #else
  9618. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  9619. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  9620. if (local_offset < 0x80
  9621. #if WASM_ENABLE_GC != 0
  9622. && !wasm_is_type_reftype(local_type)
  9623. #endif
  9624. ) {
  9625. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  9626. if (is_32bit_type(local_type)) {
  9627. *p_org++ = (uint8)local_offset;
  9628. }
  9629. else {
  9630. *p_org++ = (uint8)(local_offset | 0x80);
  9631. }
  9632. while (p_org < p) {
  9633. *p_org++ = WASM_OP_NOP;
  9634. }
  9635. }
  9636. #endif
  9637. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  9638. break;
  9639. }
  9640. case WASM_OP_GET_GLOBAL:
  9641. {
  9642. #if WASM_ENABLE_GC != 0
  9643. WASMRefType *ref_type;
  9644. #endif
  9645. p_org = p - 1;
  9646. read_leb_uint32(p, p_end, global_idx);
  9647. if (global_idx >= global_count) {
  9648. set_error_buf(error_buf, error_buf_size, "unknown global");
  9649. goto fail;
  9650. }
  9651. global_type =
  9652. global_idx < module->import_global_count
  9653. ? module->import_globals[global_idx].u.global.type
  9654. : module
  9655. ->globals[global_idx
  9656. - module->import_global_count]
  9657. .type;
  9658. #if WASM_ENABLE_GC != 0
  9659. ref_type =
  9660. global_idx < module->import_global_count
  9661. ? module->import_globals[global_idx].u.global.ref_type
  9662. : module
  9663. ->globals[global_idx
  9664. - module->import_global_count]
  9665. .ref_type;
  9666. if (wasm_is_type_multi_byte_type(global_type)) {
  9667. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9668. wasm_reftype_struct_size(ref_type));
  9669. }
  9670. #endif
  9671. PUSH_TYPE(global_type);
  9672. #if WASM_ENABLE_FAST_INTERP == 0
  9673. if (global_type == VALUE_TYPE_I64
  9674. || global_type == VALUE_TYPE_F64) {
  9675. #if WASM_ENABLE_DEBUG_INTERP != 0
  9676. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9677. error_buf_size)) {
  9678. goto fail;
  9679. }
  9680. #endif
  9681. *p_org = WASM_OP_GET_GLOBAL_64;
  9682. }
  9683. #else /* else of WASM_ENABLE_FAST_INTERP */
  9684. if (global_type == VALUE_TYPE_I64
  9685. || global_type == VALUE_TYPE_F64) {
  9686. skip_label();
  9687. emit_label(WASM_OP_GET_GLOBAL_64);
  9688. }
  9689. emit_uint32(loader_ctx, global_idx);
  9690. PUSH_OFFSET_TYPE(global_type);
  9691. #endif /* end of WASM_ENABLE_FAST_INTERP */
  9692. break;
  9693. }
  9694. case WASM_OP_SET_GLOBAL:
  9695. {
  9696. bool is_mutable = false;
  9697. #if WASM_ENABLE_GC != 0
  9698. WASMRefType *ref_type;
  9699. #endif
  9700. p_org = p - 1;
  9701. read_leb_uint32(p, p_end, global_idx);
  9702. if (global_idx >= global_count) {
  9703. set_error_buf(error_buf, error_buf_size, "unknown global");
  9704. goto fail;
  9705. }
  9706. is_mutable =
  9707. global_idx < module->import_global_count
  9708. ? module->import_globals[global_idx].u.global.is_mutable
  9709. : module
  9710. ->globals[global_idx
  9711. - module->import_global_count]
  9712. .is_mutable;
  9713. if (!is_mutable) {
  9714. #if WASM_ENABLE_GC == 0
  9715. set_error_buf(error_buf, error_buf_size,
  9716. "global is immutable");
  9717. #else
  9718. set_error_buf(error_buf, error_buf_size,
  9719. "immutable global");
  9720. #endif
  9721. goto fail;
  9722. }
  9723. global_type =
  9724. global_idx < module->import_global_count
  9725. ? module->import_globals[global_idx].u.global.type
  9726. : module
  9727. ->globals[global_idx
  9728. - module->import_global_count]
  9729. .type;
  9730. #if WASM_ENABLE_GC != 0
  9731. ref_type =
  9732. global_idx < module->import_global_count
  9733. ? module->import_globals[global_idx].u.global.ref_type
  9734. : module
  9735. ->globals[global_idx
  9736. - module->import_global_count]
  9737. .ref_type;
  9738. if (wasm_is_type_multi_byte_type(global_type)) {
  9739. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  9740. wasm_reftype_struct_size(ref_type));
  9741. }
  9742. #endif
  9743. POP_TYPE(global_type);
  9744. #if WASM_ENABLE_FAST_INTERP == 0
  9745. if (global_type == VALUE_TYPE_I64
  9746. || global_type == VALUE_TYPE_F64) {
  9747. #if WASM_ENABLE_DEBUG_INTERP != 0
  9748. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9749. error_buf_size)) {
  9750. goto fail;
  9751. }
  9752. #endif
  9753. *p_org = WASM_OP_SET_GLOBAL_64;
  9754. }
  9755. else if (module->aux_stack_size > 0
  9756. && global_idx == module->aux_stack_top_global_index) {
  9757. #if WASM_ENABLE_DEBUG_INTERP != 0
  9758. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9759. error_buf_size)) {
  9760. goto fail;
  9761. }
  9762. #endif
  9763. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  9764. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9765. func->has_op_set_global_aux_stack = true;
  9766. #endif
  9767. }
  9768. #else /* else of WASM_ENABLE_FAST_INTERP */
  9769. if (global_type == VALUE_TYPE_I64
  9770. || global_type == VALUE_TYPE_F64) {
  9771. skip_label();
  9772. emit_label(WASM_OP_SET_GLOBAL_64);
  9773. }
  9774. else if (module->aux_stack_size > 0
  9775. && global_idx == module->aux_stack_top_global_index) {
  9776. skip_label();
  9777. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  9778. }
  9779. emit_uint32(loader_ctx, global_idx);
  9780. POP_OFFSET_TYPE(global_type);
  9781. #endif /* end of WASM_ENABLE_FAST_INTERP */
  9782. break;
  9783. }
  9784. /* load */
  9785. case WASM_OP_I32_LOAD:
  9786. case WASM_OP_I32_LOAD8_S:
  9787. case WASM_OP_I32_LOAD8_U:
  9788. case WASM_OP_I32_LOAD16_S:
  9789. case WASM_OP_I32_LOAD16_U:
  9790. case WASM_OP_I64_LOAD:
  9791. case WASM_OP_I64_LOAD8_S:
  9792. case WASM_OP_I64_LOAD8_U:
  9793. case WASM_OP_I64_LOAD16_S:
  9794. case WASM_OP_I64_LOAD16_U:
  9795. case WASM_OP_I64_LOAD32_S:
  9796. case WASM_OP_I64_LOAD32_U:
  9797. case WASM_OP_F32_LOAD:
  9798. case WASM_OP_F64_LOAD:
  9799. /* store */
  9800. case WASM_OP_I32_STORE:
  9801. case WASM_OP_I32_STORE8:
  9802. case WASM_OP_I32_STORE16:
  9803. case WASM_OP_I64_STORE:
  9804. case WASM_OP_I64_STORE8:
  9805. case WASM_OP_I64_STORE16:
  9806. case WASM_OP_I64_STORE32:
  9807. case WASM_OP_F32_STORE:
  9808. case WASM_OP_F64_STORE:
  9809. {
  9810. #if WASM_ENABLE_FAST_INTERP != 0
  9811. /* change F32/F64 into I32/I64 */
  9812. if (opcode == WASM_OP_F32_LOAD) {
  9813. skip_label();
  9814. emit_label(WASM_OP_I32_LOAD);
  9815. }
  9816. else if (opcode == WASM_OP_F64_LOAD) {
  9817. skip_label();
  9818. emit_label(WASM_OP_I64_LOAD);
  9819. }
  9820. else if (opcode == WASM_OP_F32_STORE) {
  9821. skip_label();
  9822. emit_label(WASM_OP_I32_STORE);
  9823. }
  9824. else if (opcode == WASM_OP_F64_STORE) {
  9825. skip_label();
  9826. emit_label(WASM_OP_I64_STORE);
  9827. }
  9828. #endif
  9829. CHECK_MEMORY();
  9830. read_leb_uint32(p, p_end, align); /* align */
  9831. read_leb_uint32(p, p_end, mem_offset); /* offset */
  9832. if (!check_memory_access_align(opcode, align, error_buf,
  9833. error_buf_size)) {
  9834. goto fail;
  9835. }
  9836. #if WASM_ENABLE_FAST_INTERP != 0
  9837. emit_uint32(loader_ctx, mem_offset);
  9838. #endif
  9839. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9840. func->has_memory_operations = true;
  9841. #endif
  9842. switch (opcode) {
  9843. /* load */
  9844. case WASM_OP_I32_LOAD:
  9845. case WASM_OP_I32_LOAD8_S:
  9846. case WASM_OP_I32_LOAD8_U:
  9847. case WASM_OP_I32_LOAD16_S:
  9848. case WASM_OP_I32_LOAD16_U:
  9849. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  9850. break;
  9851. case WASM_OP_I64_LOAD:
  9852. case WASM_OP_I64_LOAD8_S:
  9853. case WASM_OP_I64_LOAD8_U:
  9854. case WASM_OP_I64_LOAD16_S:
  9855. case WASM_OP_I64_LOAD16_U:
  9856. case WASM_OP_I64_LOAD32_S:
  9857. case WASM_OP_I64_LOAD32_U:
  9858. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  9859. break;
  9860. case WASM_OP_F32_LOAD:
  9861. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  9862. break;
  9863. case WASM_OP_F64_LOAD:
  9864. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  9865. break;
  9866. /* store */
  9867. case WASM_OP_I32_STORE:
  9868. case WASM_OP_I32_STORE8:
  9869. case WASM_OP_I32_STORE16:
  9870. POP_I32();
  9871. POP_I32();
  9872. break;
  9873. case WASM_OP_I64_STORE:
  9874. case WASM_OP_I64_STORE8:
  9875. case WASM_OP_I64_STORE16:
  9876. case WASM_OP_I64_STORE32:
  9877. POP_I64();
  9878. POP_I32();
  9879. break;
  9880. case WASM_OP_F32_STORE:
  9881. POP_F32();
  9882. POP_I32();
  9883. break;
  9884. case WASM_OP_F64_STORE:
  9885. POP_F64();
  9886. POP_I32();
  9887. break;
  9888. default:
  9889. break;
  9890. }
  9891. break;
  9892. }
  9893. case WASM_OP_MEMORY_SIZE:
  9894. CHECK_MEMORY();
  9895. /* reserved byte 0x00 */
  9896. if (*p++ != 0x00) {
  9897. set_error_buf(error_buf, error_buf_size,
  9898. "zero byte expected");
  9899. goto fail;
  9900. }
  9901. PUSH_I32();
  9902. module->possible_memory_grow = true;
  9903. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9904. func->has_memory_operations = true;
  9905. #endif
  9906. break;
  9907. case WASM_OP_MEMORY_GROW:
  9908. CHECK_MEMORY();
  9909. /* reserved byte 0x00 */
  9910. if (*p++ != 0x00) {
  9911. set_error_buf(error_buf, error_buf_size,
  9912. "zero byte expected");
  9913. goto fail;
  9914. }
  9915. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  9916. module->possible_memory_grow = true;
  9917. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  9918. || WASM_ENABLE_WAMR_COMPILER != 0
  9919. func->has_op_memory_grow = true;
  9920. #endif
  9921. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  9922. func->has_memory_operations = true;
  9923. #endif
  9924. break;
  9925. case WASM_OP_I32_CONST:
  9926. read_leb_int32(p, p_end, i32_const);
  9927. #if WASM_ENABLE_FAST_INTERP != 0
  9928. skip_label();
  9929. disable_emit = true;
  9930. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  9931. if (operand_offset == 0) {
  9932. disable_emit = false;
  9933. emit_label(WASM_OP_I32_CONST);
  9934. emit_uint32(loader_ctx, i32_const);
  9935. }
  9936. #else
  9937. (void)i32_const;
  9938. #endif
  9939. PUSH_I32();
  9940. break;
  9941. case WASM_OP_I64_CONST:
  9942. read_leb_int64(p, p_end, i64_const);
  9943. #if WASM_ENABLE_FAST_INTERP != 0
  9944. skip_label();
  9945. disable_emit = true;
  9946. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  9947. if (operand_offset == 0) {
  9948. disable_emit = false;
  9949. emit_label(WASM_OP_I64_CONST);
  9950. emit_uint64(loader_ctx, i64_const);
  9951. }
  9952. #endif
  9953. PUSH_I64();
  9954. break;
  9955. case WASM_OP_F32_CONST:
  9956. p += sizeof(float32);
  9957. #if WASM_ENABLE_FAST_INTERP != 0
  9958. skip_label();
  9959. disable_emit = true;
  9960. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  9961. sizeof(float32));
  9962. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  9963. if (operand_offset == 0) {
  9964. disable_emit = false;
  9965. emit_label(WASM_OP_F32_CONST);
  9966. emit_float32(loader_ctx, f32_const);
  9967. }
  9968. #endif
  9969. PUSH_F32();
  9970. break;
  9971. case WASM_OP_F64_CONST:
  9972. p += sizeof(float64);
  9973. #if WASM_ENABLE_FAST_INTERP != 0
  9974. skip_label();
  9975. disable_emit = true;
  9976. /* Some MCU may require 8-byte align */
  9977. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  9978. sizeof(float64));
  9979. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  9980. if (operand_offset == 0) {
  9981. disable_emit = false;
  9982. emit_label(WASM_OP_F64_CONST);
  9983. emit_float64(loader_ctx, f64_const);
  9984. }
  9985. #endif
  9986. PUSH_F64();
  9987. break;
  9988. case WASM_OP_I32_EQZ:
  9989. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  9990. break;
  9991. case WASM_OP_I32_EQ:
  9992. case WASM_OP_I32_NE:
  9993. case WASM_OP_I32_LT_S:
  9994. case WASM_OP_I32_LT_U:
  9995. case WASM_OP_I32_GT_S:
  9996. case WASM_OP_I32_GT_U:
  9997. case WASM_OP_I32_LE_S:
  9998. case WASM_OP_I32_LE_U:
  9999. case WASM_OP_I32_GE_S:
  10000. case WASM_OP_I32_GE_U:
  10001. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10002. break;
  10003. case WASM_OP_I64_EQZ:
  10004. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  10005. break;
  10006. case WASM_OP_I64_EQ:
  10007. case WASM_OP_I64_NE:
  10008. case WASM_OP_I64_LT_S:
  10009. case WASM_OP_I64_LT_U:
  10010. case WASM_OP_I64_GT_S:
  10011. case WASM_OP_I64_GT_U:
  10012. case WASM_OP_I64_LE_S:
  10013. case WASM_OP_I64_LE_U:
  10014. case WASM_OP_I64_GE_S:
  10015. case WASM_OP_I64_GE_U:
  10016. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  10017. break;
  10018. case WASM_OP_F32_EQ:
  10019. case WASM_OP_F32_NE:
  10020. case WASM_OP_F32_LT:
  10021. case WASM_OP_F32_GT:
  10022. case WASM_OP_F32_LE:
  10023. case WASM_OP_F32_GE:
  10024. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10025. break;
  10026. case WASM_OP_F64_EQ:
  10027. case WASM_OP_F64_NE:
  10028. case WASM_OP_F64_LT:
  10029. case WASM_OP_F64_GT:
  10030. case WASM_OP_F64_LE:
  10031. case WASM_OP_F64_GE:
  10032. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  10033. break;
  10034. case WASM_OP_I32_CLZ:
  10035. case WASM_OP_I32_CTZ:
  10036. case WASM_OP_I32_POPCNT:
  10037. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10038. break;
  10039. case WASM_OP_I32_ADD:
  10040. case WASM_OP_I32_SUB:
  10041. case WASM_OP_I32_MUL:
  10042. case WASM_OP_I32_DIV_S:
  10043. case WASM_OP_I32_DIV_U:
  10044. case WASM_OP_I32_REM_S:
  10045. case WASM_OP_I32_REM_U:
  10046. case WASM_OP_I32_AND:
  10047. case WASM_OP_I32_OR:
  10048. case WASM_OP_I32_XOR:
  10049. case WASM_OP_I32_SHL:
  10050. case WASM_OP_I32_SHR_S:
  10051. case WASM_OP_I32_SHR_U:
  10052. case WASM_OP_I32_ROTL:
  10053. case WASM_OP_I32_ROTR:
  10054. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10055. break;
  10056. case WASM_OP_I64_CLZ:
  10057. case WASM_OP_I64_CTZ:
  10058. case WASM_OP_I64_POPCNT:
  10059. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  10060. break;
  10061. case WASM_OP_I64_ADD:
  10062. case WASM_OP_I64_SUB:
  10063. case WASM_OP_I64_MUL:
  10064. case WASM_OP_I64_DIV_S:
  10065. case WASM_OP_I64_DIV_U:
  10066. case WASM_OP_I64_REM_S:
  10067. case WASM_OP_I64_REM_U:
  10068. case WASM_OP_I64_AND:
  10069. case WASM_OP_I64_OR:
  10070. case WASM_OP_I64_XOR:
  10071. case WASM_OP_I64_SHL:
  10072. case WASM_OP_I64_SHR_S:
  10073. case WASM_OP_I64_SHR_U:
  10074. case WASM_OP_I64_ROTL:
  10075. case WASM_OP_I64_ROTR:
  10076. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  10077. break;
  10078. case WASM_OP_F32_ABS:
  10079. case WASM_OP_F32_NEG:
  10080. case WASM_OP_F32_CEIL:
  10081. case WASM_OP_F32_FLOOR:
  10082. case WASM_OP_F32_TRUNC:
  10083. case WASM_OP_F32_NEAREST:
  10084. case WASM_OP_F32_SQRT:
  10085. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  10086. break;
  10087. case WASM_OP_F32_ADD:
  10088. case WASM_OP_F32_SUB:
  10089. case WASM_OP_F32_MUL:
  10090. case WASM_OP_F32_DIV:
  10091. case WASM_OP_F32_MIN:
  10092. case WASM_OP_F32_MAX:
  10093. case WASM_OP_F32_COPYSIGN:
  10094. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  10095. break;
  10096. case WASM_OP_F64_ABS:
  10097. case WASM_OP_F64_NEG:
  10098. case WASM_OP_F64_CEIL:
  10099. case WASM_OP_F64_FLOOR:
  10100. case WASM_OP_F64_TRUNC:
  10101. case WASM_OP_F64_NEAREST:
  10102. case WASM_OP_F64_SQRT:
  10103. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  10104. break;
  10105. case WASM_OP_F64_ADD:
  10106. case WASM_OP_F64_SUB:
  10107. case WASM_OP_F64_MUL:
  10108. case WASM_OP_F64_DIV:
  10109. case WASM_OP_F64_MIN:
  10110. case WASM_OP_F64_MAX:
  10111. case WASM_OP_F64_COPYSIGN:
  10112. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  10113. break;
  10114. case WASM_OP_I32_WRAP_I64:
  10115. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  10116. break;
  10117. case WASM_OP_I32_TRUNC_S_F32:
  10118. case WASM_OP_I32_TRUNC_U_F32:
  10119. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10120. break;
  10121. case WASM_OP_I32_TRUNC_S_F64:
  10122. case WASM_OP_I32_TRUNC_U_F64:
  10123. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  10124. break;
  10125. case WASM_OP_I64_EXTEND_S_I32:
  10126. case WASM_OP_I64_EXTEND_U_I32:
  10127. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  10128. break;
  10129. case WASM_OP_I64_TRUNC_S_F32:
  10130. case WASM_OP_I64_TRUNC_U_F32:
  10131. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  10132. break;
  10133. case WASM_OP_I64_TRUNC_S_F64:
  10134. case WASM_OP_I64_TRUNC_U_F64:
  10135. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  10136. break;
  10137. case WASM_OP_F32_CONVERT_S_I32:
  10138. case WASM_OP_F32_CONVERT_U_I32:
  10139. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  10140. break;
  10141. case WASM_OP_F32_CONVERT_S_I64:
  10142. case WASM_OP_F32_CONVERT_U_I64:
  10143. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  10144. break;
  10145. case WASM_OP_F32_DEMOTE_F64:
  10146. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  10147. break;
  10148. case WASM_OP_F64_CONVERT_S_I32:
  10149. case WASM_OP_F64_CONVERT_U_I32:
  10150. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  10151. break;
  10152. case WASM_OP_F64_CONVERT_S_I64:
  10153. case WASM_OP_F64_CONVERT_U_I64:
  10154. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  10155. break;
  10156. case WASM_OP_F64_PROMOTE_F32:
  10157. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  10158. break;
  10159. case WASM_OP_I32_REINTERPRET_F32:
  10160. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10161. break;
  10162. case WASM_OP_I64_REINTERPRET_F64:
  10163. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  10164. break;
  10165. case WASM_OP_F32_REINTERPRET_I32:
  10166. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  10167. break;
  10168. case WASM_OP_F64_REINTERPRET_I64:
  10169. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  10170. break;
  10171. case WASM_OP_I32_EXTEND8_S:
  10172. case WASM_OP_I32_EXTEND16_S:
  10173. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  10174. break;
  10175. case WASM_OP_I64_EXTEND8_S:
  10176. case WASM_OP_I64_EXTEND16_S:
  10177. case WASM_OP_I64_EXTEND32_S:
  10178. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  10179. break;
  10180. #if WASM_ENABLE_GC != 0
  10181. case WASM_OP_GC_PREFIX:
  10182. {
  10183. uint32 opcode1;
  10184. read_leb_uint32(p, p_end, opcode1);
  10185. #if WASM_ENABLE_FAST_INTERP != 0
  10186. emit_byte(loader_ctx, ((uint8)opcode1));
  10187. #endif
  10188. switch (opcode1) {
  10189. case WASM_OP_STRUCT_NEW_CANON:
  10190. case WASM_OP_STRUCT_NEW_CANON_DEFAULT:
  10191. {
  10192. read_leb_uint32(p, p_end, type_idx);
  10193. #if WASM_ENABLE_FAST_INTERP != 0
  10194. emit_uint32(loader_ctx, type_idx);
  10195. #endif
  10196. if (!check_type_index(module, type_idx, error_buf,
  10197. error_buf_size)) {
  10198. goto fail;
  10199. }
  10200. if (module->types[type_idx]->type_flag
  10201. != WASM_TYPE_STRUCT) {
  10202. set_error_buf(error_buf, error_buf_size,
  10203. "unkown struct type");
  10204. goto fail;
  10205. }
  10206. if (opcode1 == WASM_OP_STRUCT_NEW_CANON) {
  10207. int32 j, k;
  10208. uint8 value_type;
  10209. uint32 ref_type_struct_size;
  10210. WASMStructType *struct_type =
  10211. (WASMStructType *)module->types[type_idx];
  10212. k = struct_type->ref_type_map_count - 1;
  10213. for (j = struct_type->field_count - 1; j >= 0;
  10214. j--) {
  10215. value_type = struct_type->fields[j].field_type;
  10216. if (wasm_is_type_reftype(value_type)) {
  10217. if (wasm_is_type_multi_byte_type(
  10218. value_type)) {
  10219. ref_type_struct_size =
  10220. wasm_reftype_struct_size(
  10221. struct_type->ref_type_maps[k]
  10222. .ref_type);
  10223. bh_memcpy_s(
  10224. &wasm_ref_type,
  10225. (uint32)sizeof(WASMRefType),
  10226. struct_type->ref_type_maps[k]
  10227. .ref_type,
  10228. ref_type_struct_size);
  10229. k--;
  10230. }
  10231. POP_REF(value_type);
  10232. }
  10233. else {
  10234. switch (value_type) {
  10235. case VALUE_TYPE_I32:
  10236. case PACKED_TYPE_I8:
  10237. case PACKED_TYPE_I16:
  10238. POP_I32();
  10239. break;
  10240. case VALUE_TYPE_I64:
  10241. POP_I64();
  10242. break;
  10243. case VALUE_TYPE_F32:
  10244. POP_F32();
  10245. break;
  10246. case VALUE_TYPE_F64:
  10247. POP_F64();
  10248. break;
  10249. default:
  10250. set_error_buf(error_buf,
  10251. error_buf_size,
  10252. "unknown type");
  10253. goto fail;
  10254. }
  10255. }
  10256. }
  10257. }
  10258. /* PUSH struct obj, (ref $t) */
  10259. wasm_set_refheaptype_typeidx(
  10260. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  10261. PUSH_REF(wasm_ref_type.ref_type);
  10262. break;
  10263. }
  10264. case WASM_OP_STRUCT_GET:
  10265. case WASM_OP_STRUCT_GET_S:
  10266. case WASM_OP_STRUCT_GET_U:
  10267. case WASM_OP_STRUCT_SET:
  10268. {
  10269. WASMStructType *struct_type;
  10270. WASMRefType *ref_type = NULL;
  10271. uint32 field_idx;
  10272. uint8 field_type;
  10273. read_leb_uint32(p, p_end, type_idx);
  10274. #if WASM_ENABLE_FAST_INTERP != 0
  10275. emit_uint32(loader_ctx, type_idx);
  10276. #endif
  10277. if (!check_type_index(module, type_idx, error_buf,
  10278. error_buf_size)) {
  10279. goto fail;
  10280. }
  10281. if (module->types[type_idx]->type_flag
  10282. != WASM_TYPE_STRUCT) {
  10283. set_error_buf(error_buf, error_buf_size,
  10284. "unknown struct type");
  10285. goto fail;
  10286. }
  10287. struct_type = (WASMStructType *)module->types[type_idx];
  10288. read_leb_uint32(p, p_end, field_idx);
  10289. #if WASM_ENABLE_FAST_INTERP != 0
  10290. emit_uint32(loader_ctx, field_idx);
  10291. #endif
  10292. if (field_idx >= struct_type->field_count) {
  10293. set_error_buf(error_buf, error_buf_size,
  10294. "unknown struct field");
  10295. goto fail;
  10296. }
  10297. if (opcode1 == WASM_OP_STRUCT_SET
  10298. && !(struct_type->fields[field_idx].field_flags
  10299. & 1)) {
  10300. set_error_buf(error_buf, error_buf_size,
  10301. "field is immutable");
  10302. goto fail;
  10303. }
  10304. field_type = struct_type->fields[field_idx].field_type;
  10305. if (is_packed_type(field_type)) {
  10306. if (opcode1 != WASM_OP_STRUCT_GET_S
  10307. && opcode1 != WASM_OP_STRUCT_GET_U) {
  10308. set_error_buf(error_buf, error_buf_size,
  10309. "type mismatch");
  10310. goto fail;
  10311. }
  10312. else {
  10313. field_type = VALUE_TYPE_I32;
  10314. }
  10315. }
  10316. if (wasm_is_type_multi_byte_type(field_type)) {
  10317. ref_type = wasm_reftype_map_find(
  10318. struct_type->ref_type_maps,
  10319. struct_type->ref_type_map_count, field_idx);
  10320. bh_assert(ref_type);
  10321. }
  10322. if (opcode1 == WASM_OP_STRUCT_SET) {
  10323. /* POP field */
  10324. if (wasm_is_type_multi_byte_type(field_type)) {
  10325. bh_memcpy_s(&wasm_ref_type,
  10326. (uint32)sizeof(WASMRefType),
  10327. ref_type,
  10328. wasm_reftype_struct_size(ref_type));
  10329. }
  10330. POP_REF(field_type);
  10331. /* POP struct obj, (ref null $t) */
  10332. wasm_set_refheaptype_typeidx(
  10333. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10334. POP_REF(wasm_ref_type.ref_type);
  10335. }
  10336. else {
  10337. /* POP struct obj, (ref null $t) */
  10338. wasm_set_refheaptype_typeidx(
  10339. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10340. POP_REF(wasm_ref_type.ref_type);
  10341. /* PUSH field */
  10342. if (wasm_is_type_multi_byte_type(field_type)) {
  10343. bh_memcpy_s(&wasm_ref_type,
  10344. (uint32)sizeof(WASMRefType),
  10345. ref_type,
  10346. wasm_reftype_struct_size(ref_type));
  10347. }
  10348. PUSH_REF(field_type);
  10349. }
  10350. break;
  10351. }
  10352. case WASM_OP_ARRAY_NEW_CANON:
  10353. case WASM_OP_ARRAY_NEW_CANON_DEFAULT:
  10354. case WASM_OP_ARRAY_NEW_CANON_FIXED:
  10355. case WASM_OP_ARRAY_NEW_CANON_DATA:
  10356. case WASM_OP_ARRAY_NEW_CANON_ELEM:
  10357. {
  10358. WASMArrayType *array_type;
  10359. uint8 elem_type;
  10360. uint32 u32 = 0;
  10361. read_leb_uint32(p, p_end, type_idx);
  10362. #if WASM_ENABLE_FAST_INTERP != 0
  10363. emit_uint32(loader_ctx, type_idx);
  10364. #endif
  10365. if (opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED
  10366. || opcode1 == WASM_OP_ARRAY_NEW_CANON_DATA
  10367. || opcode1 == WASM_OP_ARRAY_NEW_CANON_ELEM) {
  10368. read_leb_uint32(p, p_end, u32);
  10369. #if WASM_ENABLE_FAST_INTERP != 0
  10370. emit_uint32(loader_ctx, u32);
  10371. #endif
  10372. }
  10373. if (!check_array_type(module, type_idx, error_buf,
  10374. error_buf_size)) {
  10375. goto fail;
  10376. }
  10377. if (opcode1 != WASM_OP_ARRAY_NEW_CANON_FIXED) {
  10378. /* length */
  10379. POP_I32();
  10380. }
  10381. array_type = (WASMArrayType *)module->types[type_idx];
  10382. elem_type = array_type->elem_type;
  10383. if (opcode1 == WASM_OP_ARRAY_NEW_CANON
  10384. || opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  10385. if (wasm_is_type_multi_byte_type(elem_type)) {
  10386. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10387. array_type->elem_ref_type,
  10388. wasm_reftype_struct_size(
  10389. array_type->elem_ref_type));
  10390. }
  10391. if (is_packed_type(elem_type)) {
  10392. elem_type = VALUE_TYPE_I32;
  10393. }
  10394. if (opcode1 == WASM_OP_ARRAY_NEW_CANON_FIXED) {
  10395. uint32 N = u32;
  10396. for (i = 0; i < N; i++)
  10397. POP_REF(elem_type);
  10398. }
  10399. else
  10400. POP_REF(elem_type);
  10401. }
  10402. else if (opcode1 == WASM_OP_ARRAY_NEW_CANON_DATA) {
  10403. /* offset of data segment */
  10404. POP_I32();
  10405. if (u32 >= module->data_seg_count) {
  10406. set_error_buf(error_buf, error_buf_size,
  10407. "unknown data segement");
  10408. goto fail;
  10409. }
  10410. if (wasm_is_type_reftype(elem_type)) {
  10411. set_error_buf(error_buf, error_buf_size,
  10412. "array elem type mismatch");
  10413. goto fail;
  10414. }
  10415. }
  10416. else if (opcode1 == WASM_OP_ARRAY_NEW_CANON_ELEM) {
  10417. WASMTableSeg *table_seg =
  10418. module->table_segments + u32;
  10419. /* offset of element segment */
  10420. POP_I32();
  10421. if (u32 >= module->table_seg_count) {
  10422. set_error_buf(error_buf, error_buf_size,
  10423. "unknown element segement");
  10424. goto fail;
  10425. }
  10426. if (!wasm_reftype_is_subtype_of(
  10427. table_seg->elem_type,
  10428. table_seg->elem_ref_type, elem_type,
  10429. array_type->elem_ref_type, module->types,
  10430. module->type_count)) {
  10431. set_error_buf(error_buf, error_buf_size,
  10432. "array elem type mismatch");
  10433. goto fail;
  10434. }
  10435. }
  10436. /* PUSH array obj, (ref $t) */
  10437. wasm_set_refheaptype_typeidx(
  10438. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  10439. PUSH_REF(wasm_ref_type.ref_type);
  10440. break;
  10441. }
  10442. case WASM_OP_ARRAY_GET:
  10443. case WASM_OP_ARRAY_GET_S:
  10444. case WASM_OP_ARRAY_GET_U:
  10445. case WASM_OP_ARRAY_SET:
  10446. {
  10447. uint8 elem_type;
  10448. WASMArrayType *array_type;
  10449. WASMRefType *ref_type = NULL;
  10450. read_leb_uint32(p, p_end, type_idx);
  10451. #if WASM_ENABLE_FAST_INTERP != 0
  10452. emit_uint32(loader_ctx, type_idx);
  10453. #endif
  10454. if (!check_array_type(module, type_idx, error_buf,
  10455. error_buf_size)) {
  10456. goto fail;
  10457. }
  10458. array_type = (WASMArrayType *)module->types[type_idx];
  10459. if (opcode1 == WASM_OP_ARRAY_SET
  10460. && !(array_type->elem_flags & 1)) {
  10461. set_error_buf(error_buf, error_buf_size,
  10462. "array is immutable");
  10463. goto fail;
  10464. }
  10465. elem_type = array_type->elem_type;
  10466. if (is_packed_type(elem_type)) {
  10467. if (opcode1 != WASM_OP_ARRAY_GET_S
  10468. && opcode1 != WASM_OP_ARRAY_GET_U
  10469. && opcode1 != WASM_OP_ARRAY_SET) {
  10470. set_error_buf(error_buf, error_buf_size,
  10471. "type mismatch");
  10472. goto fail;
  10473. }
  10474. else {
  10475. elem_type = VALUE_TYPE_I32;
  10476. }
  10477. }
  10478. ref_type = array_type->elem_ref_type;
  10479. if (opcode1 == WASM_OP_ARRAY_SET) {
  10480. /* POP elem to set */
  10481. if (wasm_is_type_multi_byte_type(elem_type)) {
  10482. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10483. ref_type,
  10484. wasm_reftype_struct_size(ref_type));
  10485. }
  10486. POP_REF(elem_type);
  10487. }
  10488. /* elem idx */
  10489. POP_I32();
  10490. /* POP array obj, (ref null $t) */
  10491. wasm_set_refheaptype_typeidx(
  10492. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10493. POP_REF(wasm_ref_type.ref_type);
  10494. if (opcode1 != WASM_OP_ARRAY_SET) {
  10495. /* PUSH elem */
  10496. if (wasm_is_type_multi_byte_type(elem_type)) {
  10497. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10498. ref_type,
  10499. wasm_reftype_struct_size(ref_type));
  10500. }
  10501. PUSH_REF(elem_type);
  10502. }
  10503. break;
  10504. }
  10505. #if WASM_ENABLE_GC_BINARYEN != 0
  10506. case WASM_OP_ARRAY_COPY:
  10507. {
  10508. uint32 src_type_idx;
  10509. WASMRefType src_ref_type = { 0 };
  10510. WASMRefType dst_ref_type = { 0 };
  10511. WASMArrayType *array_type;
  10512. /* typeidx1 */
  10513. read_leb_uint32(p, p_end, type_idx);
  10514. #if WASM_ENABLE_FAST_INTERP != 0
  10515. emit_uint32(loader_ctx, type_idx);
  10516. #endif
  10517. /* typeidx2 */
  10518. read_leb_uint32(p, p_end, src_type_idx);
  10519. #if WASM_ENABLE_FAST_INTERP != 0
  10520. emit_uint32(loader_ctx, src_type_idx);
  10521. #endif
  10522. if (!check_array_type(module, type_idx, error_buf,
  10523. error_buf_size)) {
  10524. goto fail;
  10525. }
  10526. if (!check_array_type(module, src_type_idx, error_buf,
  10527. error_buf_size)) {
  10528. goto fail;
  10529. }
  10530. POP_I32();
  10531. POP_I32();
  10532. /* POP array obj, (ref null $t) */
  10533. wasm_set_refheaptype_typeidx(
  10534. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  10535. POP_REF(wasm_ref_type.ref_type);
  10536. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  10537. &wasm_ref_type,
  10538. wasm_reftype_struct_size(&wasm_ref_type));
  10539. POP_I32();
  10540. /* POP array obj, (ref null $t) */
  10541. wasm_set_refheaptype_typeidx(
  10542. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  10543. POP_REF(wasm_ref_type.ref_type);
  10544. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  10545. &wasm_ref_type,
  10546. wasm_reftype_struct_size(&wasm_ref_type));
  10547. if (!wasm_reftype_is_subtype_of(
  10548. src_ref_type.ref_type, &src_ref_type,
  10549. dst_ref_type.ref_type, &dst_ref_type,
  10550. module->types, module->type_count)) {
  10551. set_error_buf(error_buf, error_buf_size,
  10552. "type mismatch");
  10553. goto fail;
  10554. }
  10555. array_type = (WASMArrayType *)module->types[type_idx];
  10556. if (!(array_type->elem_flags & 1)) {
  10557. set_error_buf(error_buf, error_buf_size,
  10558. "array is immutable");
  10559. goto fail;
  10560. }
  10561. break;
  10562. }
  10563. #endif
  10564. case WASM_OP_ARRAY_LEN:
  10565. {
  10566. POP_REF(REF_TYPE_ARRAYREF);
  10567. /* length */
  10568. PUSH_I32();
  10569. break;
  10570. }
  10571. case WASM_OP_I31_NEW:
  10572. {
  10573. POP_I32();
  10574. wasm_set_refheaptype_common(
  10575. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  10576. PUSH_REF(wasm_ref_type.ref_type);
  10577. break;
  10578. }
  10579. case WASM_OP_I31_GET_S:
  10580. case WASM_OP_I31_GET_U:
  10581. {
  10582. POP_REF(REF_TYPE_I31REF);
  10583. PUSH_I32();
  10584. break;
  10585. }
  10586. case WASM_OP_REF_TEST:
  10587. case WASM_OP_REF_CAST:
  10588. case WASM_OP_REF_TEST_NULLABLE:
  10589. case WASM_OP_REF_CAST_NULLABLE:
  10590. {
  10591. uint8 type;
  10592. read_leb_int32(p, p_end, heap_type);
  10593. #if WASM_ENABLE_FAST_INTERP != 0
  10594. emit_uint32(loader_ctx, (uint32)heap_type);
  10595. #endif
  10596. if (heap_type >= 0) {
  10597. if (!check_type_index(module, heap_type, error_buf,
  10598. error_buf_size)) {
  10599. goto fail;
  10600. }
  10601. }
  10602. else {
  10603. if (heap_type > HEAP_TYPE_FUNC
  10604. || heap_type < HEAP_TYPE_NONE) {
  10605. set_error_buf(error_buf, error_buf_size,
  10606. "unknown type");
  10607. goto fail;
  10608. }
  10609. }
  10610. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  10611. &wasm_ref_type, error_buf,
  10612. error_buf_size)) {
  10613. goto fail;
  10614. }
  10615. if (opcode1 == WASM_OP_REF_TEST
  10616. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  10617. PUSH_I32();
  10618. else {
  10619. bool nullable =
  10620. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  10621. : false;
  10622. wasm_set_refheaptype_typeidx(
  10623. &wasm_ref_type.ref_ht_typeidx, nullable,
  10624. heap_type);
  10625. PUSH_REF(wasm_ref_type.ref_type);
  10626. }
  10627. break;
  10628. }
  10629. case WASM_OP_BR_ON_CAST:
  10630. case WASM_OP_BR_ON_CAST_NULLABLE:
  10631. {
  10632. WASMRefType ref_type_tmp;
  10633. uint8 type_tmp;
  10634. uint32 depth;
  10635. bool nullable;
  10636. nullable = (opcode1 == WASM_OP_BR_ON_CAST_NULLABLE)
  10637. ? true
  10638. : false;
  10639. p_org = p;
  10640. read_leb_uint32(p, p_end, depth);
  10641. read_leb_int32(p, p_end, heap_type);
  10642. #if WASM_ENABLE_FAST_INTERP != 0
  10643. /* Emit heap_type firstly */
  10644. emit_uint32(loader_ctx, (uint32)heap_type);
  10645. #endif
  10646. (void)depth;
  10647. /* Pop and backup the stack top's ref type */
  10648. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  10649. &ref_type_tmp, error_buf,
  10650. error_buf_size)) {
  10651. goto fail;
  10652. }
  10653. if (heap_type >= 0) {
  10654. if (!check_type_index(module, heap_type, error_buf,
  10655. error_buf_size)) {
  10656. goto fail;
  10657. }
  10658. wasm_set_refheaptype_typeidx(
  10659. &wasm_ref_type.ref_ht_typeidx, nullable,
  10660. heap_type);
  10661. }
  10662. else {
  10663. if (heap_type > HEAP_TYPE_FUNC
  10664. || heap_type < HEAP_TYPE_NONE) {
  10665. set_error_buf(error_buf, error_buf_size,
  10666. "unknown type");
  10667. goto fail;
  10668. }
  10669. wasm_set_refheaptype_common(
  10670. &wasm_ref_type.ref_ht_common, nullable,
  10671. heap_type);
  10672. }
  10673. p = p_org;
  10674. /* Push ref type casted for branch block check */
  10675. PUSH_REF(wasm_ref_type.ref_type);
  10676. if (!(frame_csp_tmp = check_branch_block(
  10677. loader_ctx, &p, p_end, false, error_buf,
  10678. error_buf_size))) {
  10679. goto fail;
  10680. }
  10681. /* Ignore heap_type */
  10682. skip_leb_uint32(p, p_end);
  10683. /* Restore the original stack top's ref type */
  10684. POP_REF(wasm_ref_type.ref_type);
  10685. #if WASM_ENABLE_FAST_INTERP != 0
  10686. /* Erase the opnd offset emitted by POP_REF() */
  10687. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10688. #endif
  10689. if (wasm_is_type_multi_byte_type(type_tmp)) {
  10690. bh_memcpy_s(
  10691. &wasm_ref_type,
  10692. wasm_reftype_struct_size(&ref_type_tmp),
  10693. &ref_type_tmp,
  10694. wasm_reftype_struct_size(&ref_type_tmp));
  10695. }
  10696. PUSH_REF(type_tmp);
  10697. #if WASM_ENABLE_FAST_INTERP != 0
  10698. /* Erase the opnd offset emitted by PUSH_REF() */
  10699. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10700. #endif
  10701. break;
  10702. }
  10703. case WASM_OP_BR_ON_CAST_FAIL:
  10704. case WASM_OP_BR_ON_CAST_FAIL_NULLABLE:
  10705. {
  10706. uint8 type_tmp;
  10707. uint32 depth;
  10708. bool nullable;
  10709. nullable = (opcode1 == WASM_OP_BR_ON_CAST_FAIL_NULLABLE)
  10710. ? true
  10711. : false;
  10712. p_org = p;
  10713. read_leb_uint32(p, p_end, depth);
  10714. read_leb_int32(p, p_end, heap_type);
  10715. #if WASM_ENABLE_FAST_INTERP != 0
  10716. /* Emit heap_type firstly */
  10717. emit_uint32(loader_ctx, (uint32)heap_type);
  10718. #endif
  10719. (void)depth;
  10720. /* Pop and push the stack top's ref type so fast-interp
  10721. can get the opnd offset */
  10722. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  10723. &wasm_ref_type, error_buf,
  10724. error_buf_size)) {
  10725. goto fail;
  10726. }
  10727. p = p_org;
  10728. PUSH_REF(type_tmp);
  10729. if (!(frame_csp_tmp = check_branch_block(
  10730. loader_ctx, &p, p_end, false, error_buf,
  10731. error_buf_size))) {
  10732. goto fail;
  10733. }
  10734. /* Ignore heap_type */
  10735. skip_leb_uint32(p, p_end);
  10736. /* Pop the original ref type, and push the casted
  10737. ref type */
  10738. POP_REF(type_tmp);
  10739. #if WASM_ENABLE_FAST_INTERP != 0
  10740. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10741. #endif
  10742. if (heap_type >= 0) {
  10743. if (!check_type_index(module, heap_type, error_buf,
  10744. error_buf_size)) {
  10745. goto fail;
  10746. }
  10747. wasm_set_refheaptype_typeidx(
  10748. &wasm_ref_type.ref_ht_typeidx, nullable,
  10749. heap_type);
  10750. }
  10751. else {
  10752. if (heap_type > HEAP_TYPE_FUNC
  10753. || heap_type < HEAP_TYPE_NONE) {
  10754. set_error_buf(error_buf, error_buf_size,
  10755. "unknown type");
  10756. goto fail;
  10757. }
  10758. wasm_set_refheaptype_common(
  10759. &wasm_ref_type.ref_ht_common, nullable,
  10760. heap_type);
  10761. }
  10762. PUSH_REF(wasm_ref_type.ref_type);
  10763. #if WASM_ENABLE_FAST_INTERP != 0
  10764. /* Erase the opnd offset emitted by PUSH_REF() */
  10765. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  10766. #endif
  10767. break;
  10768. }
  10769. case WASM_OP_EXTERN_INTERNALIZE:
  10770. {
  10771. uint8 type;
  10772. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  10773. &wasm_ref_type, error_buf,
  10774. error_buf_size)) {
  10775. goto fail;
  10776. }
  10777. if (!(type == REF_TYPE_EXTERNREF
  10778. || (type == REF_TYPE_HT_NON_NULLABLE
  10779. && wasm_ref_type.ref_ht_common.heap_type
  10780. == HEAP_TYPE_EXTERN)
  10781. || type == VALUE_TYPE_ANY)) {
  10782. set_error_buf(error_buf, error_buf_size,
  10783. "type mismatch");
  10784. goto fail;
  10785. }
  10786. if (type == REF_TYPE_EXTERNREF)
  10787. type = REF_TYPE_ANYREF;
  10788. else {
  10789. wasm_ref_type.ref_ht_common.heap_type =
  10790. HEAP_TYPE_ANY;
  10791. }
  10792. PUSH_REF(type);
  10793. break;
  10794. }
  10795. case WASM_OP_EXTERN_EXTERNALIZE:
  10796. {
  10797. uint8 type;
  10798. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  10799. &wasm_ref_type, error_buf,
  10800. error_buf_size)) {
  10801. goto fail;
  10802. }
  10803. if (type == REF_TYPE_EXTERNREF
  10804. || ((type == REF_TYPE_HT_NULLABLE
  10805. || type == REF_TYPE_HT_NON_NULLABLE)
  10806. && wasm_ref_type.ref_ht_common.heap_type
  10807. == HEAP_TYPE_EXTERN)) {
  10808. set_error_buf(error_buf, error_buf_size,
  10809. "type mismatch");
  10810. goto fail;
  10811. }
  10812. if (type != REF_TYPE_HT_NON_NULLABLE) {
  10813. /* push (ref null extern) */
  10814. type = REF_TYPE_EXTERNREF;
  10815. }
  10816. else {
  10817. /* push (ref extern) */
  10818. type = REF_TYPE_HT_NON_NULLABLE;
  10819. wasm_set_refheaptype_common(
  10820. &wasm_ref_type.ref_ht_common, false,
  10821. HEAP_TYPE_EXTERN);
  10822. }
  10823. PUSH_REF(type);
  10824. break;
  10825. }
  10826. default:
  10827. set_error_buf_v(error_buf, error_buf_size,
  10828. "%s %02x %02x", "unsupported opcode",
  10829. 0xfb, opcode1);
  10830. goto fail;
  10831. }
  10832. break;
  10833. }
  10834. #endif /* end of WASM_ENABLE_GC != 0 */
  10835. case WASM_OP_MISC_PREFIX:
  10836. {
  10837. uint32 opcode1;
  10838. read_leb_uint32(p, p_end, opcode1);
  10839. #if WASM_ENABLE_FAST_INTERP != 0
  10840. emit_byte(loader_ctx, ((uint8)opcode1));
  10841. #endif
  10842. switch (opcode1) {
  10843. case WASM_OP_I32_TRUNC_SAT_S_F32:
  10844. case WASM_OP_I32_TRUNC_SAT_U_F32:
  10845. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  10846. break;
  10847. case WASM_OP_I32_TRUNC_SAT_S_F64:
  10848. case WASM_OP_I32_TRUNC_SAT_U_F64:
  10849. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  10850. break;
  10851. case WASM_OP_I64_TRUNC_SAT_S_F32:
  10852. case WASM_OP_I64_TRUNC_SAT_U_F32:
  10853. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  10854. break;
  10855. case WASM_OP_I64_TRUNC_SAT_S_F64:
  10856. case WASM_OP_I64_TRUNC_SAT_U_F64:
  10857. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  10858. break;
  10859. #if WASM_ENABLE_BULK_MEMORY != 0
  10860. case WASM_OP_MEMORY_INIT:
  10861. {
  10862. read_leb_uint32(p, p_end, data_seg_idx);
  10863. #if WASM_ENABLE_FAST_INTERP != 0
  10864. emit_uint32(loader_ctx, data_seg_idx);
  10865. #endif
  10866. if (module->import_memory_count == 0
  10867. && module->memory_count == 0)
  10868. goto fail_unknown_memory;
  10869. if (*p++ != 0x00)
  10870. goto fail_zero_byte_expected;
  10871. if (data_seg_idx >= module->data_seg_count) {
  10872. set_error_buf_v(error_buf, error_buf_size,
  10873. "unknown data segment %d",
  10874. data_seg_idx);
  10875. goto fail;
  10876. }
  10877. if (module->data_seg_count1 == 0)
  10878. goto fail_data_cnt_sec_require;
  10879. POP_I32();
  10880. POP_I32();
  10881. POP_I32();
  10882. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10883. func->has_memory_operations = true;
  10884. #endif
  10885. break;
  10886. }
  10887. case WASM_OP_DATA_DROP:
  10888. {
  10889. read_leb_uint32(p, p_end, data_seg_idx);
  10890. #if WASM_ENABLE_FAST_INTERP != 0
  10891. emit_uint32(loader_ctx, data_seg_idx);
  10892. #endif
  10893. if (data_seg_idx >= module->data_seg_count) {
  10894. set_error_buf(error_buf, error_buf_size,
  10895. "unknown data segment");
  10896. goto fail;
  10897. }
  10898. if (module->data_seg_count1 == 0)
  10899. goto fail_data_cnt_sec_require;
  10900. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10901. func->has_memory_operations = true;
  10902. #endif
  10903. break;
  10904. }
  10905. case WASM_OP_MEMORY_COPY:
  10906. {
  10907. /* both src and dst memory index should be 0 */
  10908. if (*(int16 *)p != 0x0000)
  10909. goto fail_zero_byte_expected;
  10910. p += 2;
  10911. if (module->import_memory_count == 0
  10912. && module->memory_count == 0)
  10913. goto fail_unknown_memory;
  10914. POP_I32();
  10915. POP_I32();
  10916. POP_I32();
  10917. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10918. func->has_memory_operations = true;
  10919. #endif
  10920. break;
  10921. }
  10922. case WASM_OP_MEMORY_FILL:
  10923. {
  10924. if (*p++ != 0x00) {
  10925. goto fail_zero_byte_expected;
  10926. }
  10927. if (module->import_memory_count == 0
  10928. && module->memory_count == 0) {
  10929. goto fail_unknown_memory;
  10930. }
  10931. POP_I32();
  10932. POP_I32();
  10933. POP_I32();
  10934. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10935. func->has_memory_operations = true;
  10936. #endif
  10937. break;
  10938. }
  10939. fail_zero_byte_expected:
  10940. set_error_buf(error_buf, error_buf_size,
  10941. "zero byte expected");
  10942. goto fail;
  10943. fail_unknown_memory:
  10944. set_error_buf(error_buf, error_buf_size,
  10945. "unknown memory 0");
  10946. goto fail;
  10947. fail_data_cnt_sec_require:
  10948. set_error_buf(error_buf, error_buf_size,
  10949. "data count section required");
  10950. goto fail;
  10951. #endif /* WASM_ENABLE_BULK_MEMORY */
  10952. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10953. case WASM_OP_TABLE_INIT:
  10954. {
  10955. uint8 seg_type = 0, tbl_type = 0;
  10956. #if WASM_ENABLE_GC != 0
  10957. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  10958. #endif
  10959. read_leb_uint32(p, p_end, table_seg_idx);
  10960. read_leb_uint32(p, p_end, table_idx);
  10961. if (!get_table_elem_type(module, table_idx, &tbl_type,
  10962. #if WASM_ENABLE_GC != 0
  10963. (void **)&tbl_ref_type,
  10964. #else
  10965. NULL,
  10966. #endif
  10967. error_buf, error_buf_size))
  10968. goto fail;
  10969. if (!get_table_seg_elem_type(module, table_seg_idx,
  10970. &seg_type,
  10971. #if WASM_ENABLE_GC != 0
  10972. (void **)&seg_ref_type,
  10973. #else
  10974. NULL,
  10975. #endif
  10976. error_buf, error_buf_size))
  10977. goto fail;
  10978. #if WASM_ENABLE_GC == 0
  10979. if (seg_type != tbl_type) {
  10980. set_error_buf(error_buf, error_buf_size,
  10981. "type mismatch");
  10982. goto fail;
  10983. }
  10984. #else
  10985. if (!wasm_reftype_is_subtype_of(
  10986. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  10987. module->types, module->type_count)) {
  10988. set_error_buf(error_buf, error_buf_size,
  10989. "type mismatch");
  10990. goto fail;
  10991. }
  10992. #endif
  10993. #if WASM_ENABLE_FAST_INTERP != 0
  10994. emit_uint32(loader_ctx, table_seg_idx);
  10995. emit_uint32(loader_ctx, table_idx);
  10996. #endif
  10997. POP_I32();
  10998. POP_I32();
  10999. POP_I32();
  11000. break;
  11001. }
  11002. case WASM_OP_ELEM_DROP:
  11003. {
  11004. read_leb_uint32(p, p_end, table_seg_idx);
  11005. if (!get_table_seg_elem_type(module, table_seg_idx,
  11006. NULL, NULL, error_buf,
  11007. error_buf_size))
  11008. goto fail;
  11009. #if WASM_ENABLE_FAST_INTERP != 0
  11010. emit_uint32(loader_ctx, table_seg_idx);
  11011. #endif
  11012. break;
  11013. }
  11014. case WASM_OP_TABLE_COPY:
  11015. {
  11016. uint8 src_type, dst_type;
  11017. #if WASM_ENABLE_GC != 0
  11018. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  11019. #endif
  11020. uint32 src_tbl_idx, dst_tbl_idx;
  11021. read_leb_uint32(p, p_end, dst_tbl_idx);
  11022. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  11023. #if WASM_ENABLE_GC != 0
  11024. (void **)&dst_ref_type,
  11025. #else
  11026. NULL,
  11027. #endif
  11028. error_buf, error_buf_size))
  11029. goto fail;
  11030. read_leb_uint32(p, p_end, src_tbl_idx);
  11031. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  11032. #if WASM_ENABLE_GC != 0
  11033. (void **)&src_ref_type,
  11034. #else
  11035. NULL,
  11036. #endif
  11037. error_buf, error_buf_size))
  11038. goto fail;
  11039. #if WASM_ENABLE_GC == 0
  11040. if (src_type != dst_type) {
  11041. set_error_buf(error_buf, error_buf_size,
  11042. "type mismatch");
  11043. goto fail;
  11044. }
  11045. #else
  11046. if (!wasm_reftype_is_subtype_of(
  11047. src_type, src_ref_type, dst_type, dst_ref_type,
  11048. module->types, module->type_count)) {
  11049. set_error_buf(error_buf, error_buf_size,
  11050. "type mismatch");
  11051. goto fail;
  11052. }
  11053. #endif
  11054. #if WASM_ENABLE_FAST_INTERP != 0
  11055. emit_uint32(loader_ctx, dst_tbl_idx);
  11056. emit_uint32(loader_ctx, src_tbl_idx);
  11057. #endif
  11058. POP_I32();
  11059. POP_I32();
  11060. POP_I32();
  11061. break;
  11062. }
  11063. case WASM_OP_TABLE_SIZE:
  11064. {
  11065. read_leb_uint32(p, p_end, table_idx);
  11066. /* TODO: shall we create a new function to check
  11067. table idx instead of using below function? */
  11068. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  11069. error_buf, error_buf_size))
  11070. goto fail;
  11071. #if WASM_ENABLE_FAST_INTERP != 0
  11072. emit_uint32(loader_ctx, table_idx);
  11073. #endif
  11074. PUSH_I32();
  11075. break;
  11076. }
  11077. case WASM_OP_TABLE_GROW:
  11078. case WASM_OP_TABLE_FILL:
  11079. {
  11080. uint8 decl_type;
  11081. #if WASM_ENABLE_GC != 0
  11082. WASMRefType *ref_type = NULL;
  11083. #endif
  11084. read_leb_uint32(p, p_end, table_idx);
  11085. if (!get_table_elem_type(module, table_idx, &decl_type,
  11086. #if WASM_ENABLE_GC != 0
  11087. (void **)&ref_type,
  11088. #else
  11089. NULL,
  11090. #endif
  11091. error_buf, error_buf_size))
  11092. goto fail;
  11093. #if WASM_ENABLE_GC != 0
  11094. if (wasm_is_type_multi_byte_type(decl_type)) {
  11095. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11096. ref_type,
  11097. wasm_reftype_struct_size(ref_type));
  11098. }
  11099. #endif
  11100. if (opcode1 == WASM_OP_TABLE_GROW) {
  11101. if (table_idx < module->import_table_count) {
  11102. module->import_tables[table_idx]
  11103. .u.table.possible_grow = true;
  11104. }
  11105. else {
  11106. module
  11107. ->tables[table_idx
  11108. - module->import_table_count]
  11109. .possible_grow = true;
  11110. }
  11111. }
  11112. #if WASM_ENABLE_FAST_INTERP != 0
  11113. emit_uint32(loader_ctx, table_idx);
  11114. #endif
  11115. POP_I32();
  11116. #if WASM_ENABLE_FAST_INTERP != 0
  11117. POP_OFFSET_TYPE(decl_type);
  11118. #endif
  11119. POP_TYPE(decl_type);
  11120. if (opcode1 == WASM_OP_TABLE_GROW)
  11121. PUSH_I32();
  11122. else
  11123. POP_I32();
  11124. break;
  11125. }
  11126. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11127. default:
  11128. set_error_buf_v(error_buf, error_buf_size,
  11129. "%s %02x %02x", "unsupported opcode",
  11130. 0xfc, opcode1);
  11131. goto fail;
  11132. }
  11133. break;
  11134. }
  11135. #if WASM_ENABLE_SIMD != 0
  11136. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11137. case WASM_OP_SIMD_PREFIX:
  11138. {
  11139. opcode = read_uint8(p);
  11140. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  11141. */
  11142. switch (opcode) {
  11143. /* memory instruction */
  11144. case SIMD_v128_load:
  11145. case SIMD_v128_load8x8_s:
  11146. case SIMD_v128_load8x8_u:
  11147. case SIMD_v128_load16x4_s:
  11148. case SIMD_v128_load16x4_u:
  11149. case SIMD_v128_load32x2_s:
  11150. case SIMD_v128_load32x2_u:
  11151. case SIMD_v128_load8_splat:
  11152. case SIMD_v128_load16_splat:
  11153. case SIMD_v128_load32_splat:
  11154. case SIMD_v128_load64_splat:
  11155. {
  11156. CHECK_MEMORY();
  11157. read_leb_uint32(p, p_end, align); /* align */
  11158. if (!check_simd_memory_access_align(
  11159. opcode, align, error_buf, error_buf_size)) {
  11160. goto fail;
  11161. }
  11162. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11163. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  11164. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11165. func->has_memory_operations = true;
  11166. #endif
  11167. break;
  11168. }
  11169. case SIMD_v128_store:
  11170. {
  11171. CHECK_MEMORY();
  11172. read_leb_uint32(p, p_end, align); /* align */
  11173. if (!check_simd_memory_access_align(
  11174. opcode, align, error_buf, error_buf_size)) {
  11175. goto fail;
  11176. }
  11177. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11178. POP_V128();
  11179. POP_I32();
  11180. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11181. func->has_memory_operations = true;
  11182. #endif
  11183. break;
  11184. }
  11185. /* basic operation */
  11186. case SIMD_v128_const:
  11187. {
  11188. CHECK_BUF1(p, p_end, 16);
  11189. p += 16;
  11190. PUSH_V128();
  11191. break;
  11192. }
  11193. case SIMD_v8x16_shuffle:
  11194. {
  11195. V128 mask;
  11196. CHECK_BUF1(p, p_end, 16);
  11197. mask = read_i8x16(p, error_buf, error_buf_size);
  11198. p += 16;
  11199. if (!check_simd_shuffle_mask(mask, error_buf,
  11200. error_buf_size)) {
  11201. goto fail;
  11202. }
  11203. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11204. break;
  11205. }
  11206. case SIMD_v8x16_swizzle:
  11207. {
  11208. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11209. break;
  11210. }
  11211. /* splat operation */
  11212. case SIMD_i8x16_splat:
  11213. case SIMD_i16x8_splat:
  11214. case SIMD_i32x4_splat:
  11215. case SIMD_i64x2_splat:
  11216. case SIMD_f32x4_splat:
  11217. case SIMD_f64x2_splat:
  11218. {
  11219. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  11220. VALUE_TYPE_I32, VALUE_TYPE_I64,
  11221. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  11222. POP_AND_PUSH(pop_type[opcode - SIMD_i8x16_splat],
  11223. VALUE_TYPE_V128);
  11224. break;
  11225. }
  11226. /* lane operation */
  11227. case SIMD_i8x16_extract_lane_s:
  11228. case SIMD_i8x16_extract_lane_u:
  11229. case SIMD_i8x16_replace_lane:
  11230. case SIMD_i16x8_extract_lane_s:
  11231. case SIMD_i16x8_extract_lane_u:
  11232. case SIMD_i16x8_replace_lane:
  11233. case SIMD_i32x4_extract_lane:
  11234. case SIMD_i32x4_replace_lane:
  11235. case SIMD_i64x2_extract_lane:
  11236. case SIMD_i64x2_replace_lane:
  11237. case SIMD_f32x4_extract_lane:
  11238. case SIMD_f32x4_replace_lane:
  11239. case SIMD_f64x2_extract_lane:
  11240. case SIMD_f64x2_replace_lane:
  11241. {
  11242. uint8 lane;
  11243. /* clang-format off */
  11244. uint8 replace[] = {
  11245. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  11246. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  11247. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  11248. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  11249. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  11250. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  11251. };
  11252. uint8 push_type[] = {
  11253. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  11254. VALUE_TYPE_V128,
  11255. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  11256. VALUE_TYPE_V128,
  11257. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  11258. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  11259. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  11260. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  11261. };
  11262. /* clang-format on */
  11263. CHECK_BUF(p, p_end, 1);
  11264. lane = read_uint8(p);
  11265. if (!check_simd_access_lane(opcode, lane, error_buf,
  11266. error_buf_size)) {
  11267. goto fail;
  11268. }
  11269. if (replace[opcode - SIMD_i8x16_extract_lane_s]) {
  11270. if (!(wasm_loader_pop_frame_ref(
  11271. loader_ctx,
  11272. replace[opcode - SIMD_i8x16_extract_lane_s],
  11273. error_buf, error_buf_size)))
  11274. goto fail;
  11275. }
  11276. POP_AND_PUSH(
  11277. VALUE_TYPE_V128,
  11278. push_type[opcode - SIMD_i8x16_extract_lane_s]);
  11279. break;
  11280. }
  11281. /* i8x16 compare operation */
  11282. case SIMD_i8x16_eq:
  11283. case SIMD_i8x16_ne:
  11284. case SIMD_i8x16_lt_s:
  11285. case SIMD_i8x16_lt_u:
  11286. case SIMD_i8x16_gt_s:
  11287. case SIMD_i8x16_gt_u:
  11288. case SIMD_i8x16_le_s:
  11289. case SIMD_i8x16_le_u:
  11290. case SIMD_i8x16_ge_s:
  11291. case SIMD_i8x16_ge_u:
  11292. /* i16x8 compare operation */
  11293. case SIMD_i16x8_eq:
  11294. case SIMD_i16x8_ne:
  11295. case SIMD_i16x8_lt_s:
  11296. case SIMD_i16x8_lt_u:
  11297. case SIMD_i16x8_gt_s:
  11298. case SIMD_i16x8_gt_u:
  11299. case SIMD_i16x8_le_s:
  11300. case SIMD_i16x8_le_u:
  11301. case SIMD_i16x8_ge_s:
  11302. case SIMD_i16x8_ge_u:
  11303. /* i32x4 compare operation */
  11304. case SIMD_i32x4_eq:
  11305. case SIMD_i32x4_ne:
  11306. case SIMD_i32x4_lt_s:
  11307. case SIMD_i32x4_lt_u:
  11308. case SIMD_i32x4_gt_s:
  11309. case SIMD_i32x4_gt_u:
  11310. case SIMD_i32x4_le_s:
  11311. case SIMD_i32x4_le_u:
  11312. case SIMD_i32x4_ge_s:
  11313. case SIMD_i32x4_ge_u:
  11314. /* f32x4 compare operation */
  11315. case SIMD_f32x4_eq:
  11316. case SIMD_f32x4_ne:
  11317. case SIMD_f32x4_lt:
  11318. case SIMD_f32x4_gt:
  11319. case SIMD_f32x4_le:
  11320. case SIMD_f32x4_ge:
  11321. /* f64x2 compare operation */
  11322. case SIMD_f64x2_eq:
  11323. case SIMD_f64x2_ne:
  11324. case SIMD_f64x2_lt:
  11325. case SIMD_f64x2_gt:
  11326. case SIMD_f64x2_le:
  11327. case SIMD_f64x2_ge:
  11328. {
  11329. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11330. break;
  11331. }
  11332. /* v128 operation */
  11333. case SIMD_v128_not:
  11334. {
  11335. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11336. break;
  11337. }
  11338. case SIMD_v128_and:
  11339. case SIMD_v128_andnot:
  11340. case SIMD_v128_or:
  11341. case SIMD_v128_xor:
  11342. {
  11343. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11344. break;
  11345. }
  11346. case SIMD_v128_bitselect:
  11347. {
  11348. POP_V128();
  11349. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11350. break;
  11351. }
  11352. case SIMD_v128_any_true:
  11353. {
  11354. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11355. break;
  11356. }
  11357. /* Load Lane Operation */
  11358. case SIMD_v128_load8_lane:
  11359. case SIMD_v128_load16_lane:
  11360. case SIMD_v128_load32_lane:
  11361. case SIMD_v128_load64_lane:
  11362. case SIMD_v128_store8_lane:
  11363. case SIMD_v128_store16_lane:
  11364. case SIMD_v128_store32_lane:
  11365. case SIMD_v128_store64_lane:
  11366. {
  11367. uint8 lane;
  11368. CHECK_MEMORY();
  11369. read_leb_uint32(p, p_end, align); /* align */
  11370. if (!check_simd_memory_access_align(
  11371. opcode, align, error_buf, error_buf_size)) {
  11372. goto fail;
  11373. }
  11374. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11375. CHECK_BUF(p, p_end, 1);
  11376. lane = read_uint8(p);
  11377. if (!check_simd_access_lane(opcode, lane, error_buf,
  11378. error_buf_size)) {
  11379. goto fail;
  11380. }
  11381. POP_V128();
  11382. POP_I32();
  11383. if (opcode < SIMD_v128_store8_lane) {
  11384. PUSH_V128();
  11385. }
  11386. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11387. func->has_memory_operations = true;
  11388. #endif
  11389. break;
  11390. }
  11391. case SIMD_v128_load32_zero:
  11392. case SIMD_v128_load64_zero:
  11393. {
  11394. CHECK_MEMORY();
  11395. read_leb_uint32(p, p_end, align); /* align */
  11396. if (!check_simd_memory_access_align(
  11397. opcode, align, error_buf, error_buf_size)) {
  11398. goto fail;
  11399. }
  11400. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11401. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  11402. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11403. func->has_memory_operations = true;
  11404. #endif
  11405. break;
  11406. }
  11407. /* Float conversion */
  11408. case SIMD_f32x4_demote_f64x2_zero:
  11409. case SIMD_f64x2_promote_low_f32x4_zero:
  11410. {
  11411. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11412. break;
  11413. }
  11414. /* i8x16 Operation */
  11415. case SIMD_i8x16_abs:
  11416. case SIMD_i8x16_neg:
  11417. case SIMD_i8x16_popcnt:
  11418. {
  11419. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11420. break;
  11421. }
  11422. case SIMD_i8x16_all_true:
  11423. case SIMD_i8x16_bitmask:
  11424. {
  11425. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11426. break;
  11427. }
  11428. case SIMD_i8x16_narrow_i16x8_s:
  11429. case SIMD_i8x16_narrow_i16x8_u:
  11430. {
  11431. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11432. break;
  11433. }
  11434. case SIMD_f32x4_ceil:
  11435. case SIMD_f32x4_floor:
  11436. case SIMD_f32x4_trunc:
  11437. case SIMD_f32x4_nearest:
  11438. {
  11439. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11440. break;
  11441. }
  11442. case SIMD_i8x16_shl:
  11443. case SIMD_i8x16_shr_s:
  11444. case SIMD_i8x16_shr_u:
  11445. {
  11446. POP_I32();
  11447. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11448. break;
  11449. }
  11450. case SIMD_i8x16_add:
  11451. case SIMD_i8x16_add_sat_s:
  11452. case SIMD_i8x16_add_sat_u:
  11453. case SIMD_i8x16_sub:
  11454. case SIMD_i8x16_sub_sat_s:
  11455. case SIMD_i8x16_sub_sat_u:
  11456. {
  11457. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11458. break;
  11459. }
  11460. case SIMD_f64x2_ceil:
  11461. case SIMD_f64x2_floor:
  11462. {
  11463. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11464. break;
  11465. }
  11466. case SIMD_i8x16_min_s:
  11467. case SIMD_i8x16_min_u:
  11468. case SIMD_i8x16_max_s:
  11469. case SIMD_i8x16_max_u:
  11470. {
  11471. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11472. break;
  11473. }
  11474. case SIMD_f64x2_trunc:
  11475. {
  11476. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11477. break;
  11478. }
  11479. case SIMD_i8x16_avgr_u:
  11480. {
  11481. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11482. break;
  11483. }
  11484. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  11485. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  11486. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  11487. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  11488. /* i16x8 operation */
  11489. case SIMD_i16x8_abs:
  11490. case SIMD_i16x8_neg:
  11491. {
  11492. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11493. break;
  11494. }
  11495. case SIMD_i16x8_q15mulr_sat_s:
  11496. {
  11497. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11498. break;
  11499. }
  11500. case SIMD_i16x8_all_true:
  11501. case SIMD_i16x8_bitmask:
  11502. {
  11503. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11504. break;
  11505. }
  11506. case SIMD_i16x8_narrow_i32x4_s:
  11507. case SIMD_i16x8_narrow_i32x4_u:
  11508. {
  11509. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11510. break;
  11511. }
  11512. case SIMD_i16x8_extend_low_i8x16_s:
  11513. case SIMD_i16x8_extend_high_i8x16_s:
  11514. case SIMD_i16x8_extend_low_i8x16_u:
  11515. case SIMD_i16x8_extend_high_i8x16_u:
  11516. {
  11517. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11518. break;
  11519. }
  11520. case SIMD_i16x8_shl:
  11521. case SIMD_i16x8_shr_s:
  11522. case SIMD_i16x8_shr_u:
  11523. {
  11524. POP_I32();
  11525. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11526. break;
  11527. }
  11528. case SIMD_i16x8_add:
  11529. case SIMD_i16x8_add_sat_s:
  11530. case SIMD_i16x8_add_sat_u:
  11531. case SIMD_i16x8_sub:
  11532. case SIMD_i16x8_sub_sat_s:
  11533. case SIMD_i16x8_sub_sat_u:
  11534. {
  11535. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11536. break;
  11537. }
  11538. case SIMD_f64x2_nearest:
  11539. {
  11540. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11541. break;
  11542. }
  11543. case SIMD_i16x8_mul:
  11544. case SIMD_i16x8_min_s:
  11545. case SIMD_i16x8_min_u:
  11546. case SIMD_i16x8_max_s:
  11547. case SIMD_i16x8_max_u:
  11548. case SIMD_i16x8_avgr_u:
  11549. case SIMD_i16x8_extmul_low_i8x16_s:
  11550. case SIMD_i16x8_extmul_high_i8x16_s:
  11551. case SIMD_i16x8_extmul_low_i8x16_u:
  11552. case SIMD_i16x8_extmul_high_i8x16_u:
  11553. {
  11554. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11555. break;
  11556. }
  11557. /* i32x4 operation */
  11558. case SIMD_i32x4_abs:
  11559. case SIMD_i32x4_neg:
  11560. {
  11561. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11562. break;
  11563. }
  11564. case SIMD_i32x4_all_true:
  11565. case SIMD_i32x4_bitmask:
  11566. {
  11567. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11568. break;
  11569. }
  11570. case SIMD_i32x4_narrow_i64x2_s:
  11571. case SIMD_i32x4_narrow_i64x2_u:
  11572. {
  11573. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11574. break;
  11575. }
  11576. case SIMD_i32x4_extend_low_i16x8_s:
  11577. case SIMD_i32x4_extend_high_i16x8_s:
  11578. case SIMD_i32x4_extend_low_i16x8_u:
  11579. case SIMD_i32x4_extend_high_i16x8_u:
  11580. {
  11581. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11582. break;
  11583. }
  11584. case SIMD_i32x4_shl:
  11585. case SIMD_i32x4_shr_s:
  11586. case SIMD_i32x4_shr_u:
  11587. {
  11588. POP_I32();
  11589. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11590. break;
  11591. }
  11592. case SIMD_i32x4_add:
  11593. case SIMD_i32x4_sub:
  11594. case SIMD_i32x4_mul:
  11595. case SIMD_i32x4_min_s:
  11596. case SIMD_i32x4_min_u:
  11597. case SIMD_i32x4_max_s:
  11598. case SIMD_i32x4_max_u:
  11599. case SIMD_i32x4_dot_i16x8_s:
  11600. case SIMD_i32x4_avgr_u:
  11601. case SIMD_i32x4_extmul_low_i16x8_s:
  11602. case SIMD_i32x4_extmul_high_i16x8_s:
  11603. case SIMD_i32x4_extmul_low_i16x8_u:
  11604. case SIMD_i32x4_extmul_high_i16x8_u:
  11605. {
  11606. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11607. break;
  11608. }
  11609. /* i64x2 operation */
  11610. case SIMD_i64x2_abs:
  11611. case SIMD_i64x2_neg:
  11612. {
  11613. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11614. break;
  11615. }
  11616. case SIMD_i64x2_all_true:
  11617. case SIMD_i64x2_bitmask:
  11618. {
  11619. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  11620. break;
  11621. }
  11622. case SIMD_i64x2_extend_low_i32x4_s:
  11623. case SIMD_i64x2_extend_high_i32x4_s:
  11624. case SIMD_i64x2_extend_low_i32x4_u:
  11625. case SIMD_i64x2_extend_high_i32x4_u:
  11626. {
  11627. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11628. break;
  11629. }
  11630. case SIMD_i64x2_shl:
  11631. case SIMD_i64x2_shr_s:
  11632. case SIMD_i64x2_shr_u:
  11633. {
  11634. POP_I32();
  11635. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11636. break;
  11637. }
  11638. case SIMD_i64x2_add:
  11639. case SIMD_i64x2_sub:
  11640. case SIMD_i64x2_mul:
  11641. case SIMD_i64x2_eq:
  11642. case SIMD_i64x2_ne:
  11643. case SIMD_i64x2_lt_s:
  11644. case SIMD_i64x2_gt_s:
  11645. case SIMD_i64x2_le_s:
  11646. case SIMD_i64x2_ge_s:
  11647. case SIMD_i64x2_extmul_low_i32x4_s:
  11648. case SIMD_i64x2_extmul_high_i32x4_s:
  11649. case SIMD_i64x2_extmul_low_i32x4_u:
  11650. case SIMD_i64x2_extmul_high_i32x4_u:
  11651. {
  11652. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11653. break;
  11654. }
  11655. /* f32x4 operation */
  11656. case SIMD_f32x4_abs:
  11657. case SIMD_f32x4_neg:
  11658. case SIMD_f32x4_round:
  11659. case SIMD_f32x4_sqrt:
  11660. {
  11661. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11662. break;
  11663. }
  11664. case SIMD_f32x4_add:
  11665. case SIMD_f32x4_sub:
  11666. case SIMD_f32x4_mul:
  11667. case SIMD_f32x4_div:
  11668. case SIMD_f32x4_min:
  11669. case SIMD_f32x4_max:
  11670. case SIMD_f32x4_pmin:
  11671. case SIMD_f32x4_pmax:
  11672. {
  11673. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11674. break;
  11675. }
  11676. /* f64x2 operation */
  11677. case SIMD_f64x2_abs:
  11678. case SIMD_f64x2_neg:
  11679. case SIMD_f64x2_round:
  11680. case SIMD_f64x2_sqrt:
  11681. {
  11682. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11683. break;
  11684. }
  11685. case SIMD_f64x2_add:
  11686. case SIMD_f64x2_sub:
  11687. case SIMD_f64x2_mul:
  11688. case SIMD_f64x2_div:
  11689. case SIMD_f64x2_min:
  11690. case SIMD_f64x2_max:
  11691. case SIMD_f64x2_pmin:
  11692. case SIMD_f64x2_pmax:
  11693. {
  11694. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11695. break;
  11696. }
  11697. case SIMD_i32x4_trunc_sat_f32x4_s:
  11698. case SIMD_i32x4_trunc_sat_f32x4_u:
  11699. case SIMD_f32x4_convert_i32x4_s:
  11700. case SIMD_f32x4_convert_i32x4_u:
  11701. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  11702. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  11703. case SIMD_f64x2_convert_low_i32x4_s:
  11704. case SIMD_f64x2_convert_low_i32x4_u:
  11705. {
  11706. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  11707. break;
  11708. }
  11709. default:
  11710. {
  11711. if (error_buf != NULL) {
  11712. snprintf(error_buf, error_buf_size,
  11713. "WASM module load failed: "
  11714. "invalid opcode 0xfd %02x.",
  11715. opcode);
  11716. }
  11717. goto fail;
  11718. }
  11719. }
  11720. break;
  11721. }
  11722. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11723. #endif /* end of WASM_ENABLE_SIMD */
  11724. #if WASM_ENABLE_SHARED_MEMORY != 0
  11725. case WASM_OP_ATOMIC_PREFIX:
  11726. {
  11727. opcode = read_uint8(p);
  11728. #if WASM_ENABLE_FAST_INTERP != 0
  11729. emit_byte(loader_ctx, opcode);
  11730. #endif
  11731. if (opcode != WASM_OP_ATOMIC_FENCE) {
  11732. CHECK_MEMORY();
  11733. read_leb_uint32(p, p_end, align); /* align */
  11734. read_leb_uint32(p, p_end, mem_offset); /* offset */
  11735. if (!check_memory_align_equal(opcode, align, error_buf,
  11736. error_buf_size)) {
  11737. goto fail;
  11738. }
  11739. #if WASM_ENABLE_FAST_INTERP != 0
  11740. emit_uint32(loader_ctx, mem_offset);
  11741. #endif
  11742. }
  11743. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11744. func->has_memory_operations = true;
  11745. #endif
  11746. switch (opcode) {
  11747. case WASM_OP_ATOMIC_NOTIFY:
  11748. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11749. break;
  11750. case WASM_OP_ATOMIC_WAIT32:
  11751. POP_I64();
  11752. POP_I32();
  11753. POP_I32();
  11754. PUSH_I32();
  11755. break;
  11756. case WASM_OP_ATOMIC_WAIT64:
  11757. POP_I64();
  11758. POP_I64();
  11759. POP_I32();
  11760. PUSH_I32();
  11761. break;
  11762. case WASM_OP_ATOMIC_FENCE:
  11763. /* reserved byte 0x00 */
  11764. if (*p++ != 0x00) {
  11765. set_error_buf(error_buf, error_buf_size,
  11766. "zero byte expected");
  11767. goto fail;
  11768. }
  11769. break;
  11770. case WASM_OP_ATOMIC_I32_LOAD:
  11771. case WASM_OP_ATOMIC_I32_LOAD8_U:
  11772. case WASM_OP_ATOMIC_I32_LOAD16_U:
  11773. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11774. break;
  11775. case WASM_OP_ATOMIC_I32_STORE:
  11776. case WASM_OP_ATOMIC_I32_STORE8:
  11777. case WASM_OP_ATOMIC_I32_STORE16:
  11778. POP_I32();
  11779. POP_I32();
  11780. break;
  11781. case WASM_OP_ATOMIC_I64_LOAD:
  11782. case WASM_OP_ATOMIC_I64_LOAD8_U:
  11783. case WASM_OP_ATOMIC_I64_LOAD16_U:
  11784. case WASM_OP_ATOMIC_I64_LOAD32_U:
  11785. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  11786. break;
  11787. case WASM_OP_ATOMIC_I64_STORE:
  11788. case WASM_OP_ATOMIC_I64_STORE8:
  11789. case WASM_OP_ATOMIC_I64_STORE16:
  11790. case WASM_OP_ATOMIC_I64_STORE32:
  11791. POP_I64();
  11792. POP_I32();
  11793. break;
  11794. case WASM_OP_ATOMIC_RMW_I32_ADD:
  11795. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  11796. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  11797. case WASM_OP_ATOMIC_RMW_I32_SUB:
  11798. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  11799. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  11800. case WASM_OP_ATOMIC_RMW_I32_AND:
  11801. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  11802. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  11803. case WASM_OP_ATOMIC_RMW_I32_OR:
  11804. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  11805. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  11806. case WASM_OP_ATOMIC_RMW_I32_XOR:
  11807. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  11808. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  11809. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  11810. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  11811. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  11812. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11813. break;
  11814. case WASM_OP_ATOMIC_RMW_I64_ADD:
  11815. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  11816. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  11817. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  11818. case WASM_OP_ATOMIC_RMW_I64_SUB:
  11819. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  11820. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  11821. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  11822. case WASM_OP_ATOMIC_RMW_I64_AND:
  11823. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  11824. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  11825. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  11826. case WASM_OP_ATOMIC_RMW_I64_OR:
  11827. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  11828. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  11829. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  11830. case WASM_OP_ATOMIC_RMW_I64_XOR:
  11831. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  11832. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  11833. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  11834. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  11835. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  11836. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  11837. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  11838. POP_I64();
  11839. POP_I32();
  11840. PUSH_I64();
  11841. break;
  11842. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  11843. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  11844. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  11845. POP_I32();
  11846. POP_I32();
  11847. POP_I32();
  11848. PUSH_I32();
  11849. break;
  11850. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  11851. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  11852. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  11853. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  11854. POP_I64();
  11855. POP_I64();
  11856. POP_I32();
  11857. PUSH_I64();
  11858. break;
  11859. default:
  11860. set_error_buf_v(error_buf, error_buf_size,
  11861. "%s %02x %02x", "unsupported opcode",
  11862. 0xfe, opcode);
  11863. goto fail;
  11864. }
  11865. break;
  11866. }
  11867. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  11868. default:
  11869. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  11870. "unsupported opcode", opcode);
  11871. goto fail;
  11872. }
  11873. #if WASM_ENABLE_FAST_INTERP != 0
  11874. last_op = opcode;
  11875. #endif
  11876. }
  11877. if (loader_ctx->csp_num > 0) {
  11878. if (cur_func_idx < module->function_count - 1)
  11879. /* Function with missing end marker (between two functions) */
  11880. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  11881. else
  11882. /* Function with missing end marker
  11883. (at EOF or end of code sections) */
  11884. set_error_buf(error_buf, error_buf_size,
  11885. "unexpected end of section or function, "
  11886. "or section size mismatch");
  11887. goto fail;
  11888. }
  11889. #if WASM_ENABLE_FAST_INTERP != 0
  11890. if (loader_ctx->p_code_compiled == NULL)
  11891. goto re_scan;
  11892. func->const_cell_num = loader_ctx->const_cell_num;
  11893. if (func->const_cell_num > 0) {
  11894. int32 j;
  11895. if (!(func->consts = func_const = loader_malloc(
  11896. func->const_cell_num * 4, error_buf, error_buf_size)))
  11897. goto fail;
  11898. func_const_end = func->consts + func->const_cell_num * 4;
  11899. /* reverse the const buf */
  11900. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  11901. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  11902. if (c->value_type == VALUE_TYPE_F64
  11903. || c->value_type == VALUE_TYPE_I64) {
  11904. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  11905. &(c->value.f64), (uint32)sizeof(int64));
  11906. func_const += sizeof(int64);
  11907. }
  11908. else {
  11909. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  11910. &(c->value.f32), (uint32)sizeof(int32));
  11911. func_const += sizeof(int32);
  11912. }
  11913. }
  11914. }
  11915. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  11916. - loader_ctx->start_dynamic_offset + 1;
  11917. #else
  11918. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  11919. #endif
  11920. func->max_block_num = loader_ctx->max_csp_num;
  11921. return_value = true;
  11922. fail:
  11923. wasm_loader_ctx_destroy(loader_ctx);
  11924. (void)table_idx;
  11925. (void)table_seg_idx;
  11926. (void)data_seg_idx;
  11927. (void)i64_const;
  11928. (void)local_offset;
  11929. (void)p_org;
  11930. (void)mem_offset;
  11931. (void)align;
  11932. return return_value;
  11933. }