wasm_loader.c 339 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486
  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_DEBUG_INTERP != 0
  14. #include "../libraries/debug-engine/debug_engine.h"
  15. #endif
  16. #if WASM_ENABLE_FAST_JIT != 0
  17. #include "../fast-jit/jit_compiler.h"
  18. #include "../fast-jit/jit_codecache.h"
  19. #endif
  20. /* Read a value of given type from the address pointed to by the given
  21. pointer and increase the pointer to the position just after the
  22. value being read. */
  23. #define TEMPLATE_READ_VALUE(Type, p) \
  24. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  25. static void
  26. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  27. {
  28. if (error_buf != NULL) {
  29. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  30. string);
  31. }
  32. }
  33. static void
  34. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  35. {
  36. va_list args;
  37. char buf[128];
  38. if (error_buf != NULL) {
  39. va_start(args, format);
  40. vsnprintf(buf, sizeof(buf), format, args);
  41. va_end(args);
  42. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  43. }
  44. }
  45. static bool
  46. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  47. char *error_buf, uint32 error_buf_size)
  48. {
  49. if ((uintptr_t)buf + length < (uintptr_t)buf
  50. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  51. set_error_buf(error_buf, error_buf_size,
  52. "unexpected end of section or function");
  53. return false;
  54. }
  55. return true;
  56. }
  57. static bool
  58. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  59. char *error_buf, uint32 error_buf_size)
  60. {
  61. if ((uintptr_t)buf + length < (uintptr_t)buf
  62. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  63. set_error_buf(error_buf, error_buf_size, "unexpected end");
  64. return false;
  65. }
  66. return true;
  67. }
  68. #define CHECK_BUF(buf, buf_end, length) \
  69. do { \
  70. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  71. goto fail; \
  72. } \
  73. } while (0)
  74. #define CHECK_BUF1(buf, buf_end, length) \
  75. do { \
  76. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  77. goto fail; \
  78. } \
  79. } while (0)
  80. #define skip_leb(p) while (*p++ & 0x80)
  81. #define skip_leb_int64(p, p_end) skip_leb(p)
  82. #define skip_leb_uint32(p, p_end) skip_leb(p)
  83. #define skip_leb_int32(p, p_end) skip_leb(p)
  84. static bool
  85. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  86. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  87. {
  88. const uint8 *buf = *p_buf;
  89. uint64 result = 0;
  90. uint32 shift = 0;
  91. uint32 offset = 0, bcnt = 0;
  92. uint64 byte;
  93. while (true) {
  94. /* uN or SN must not exceed ceil(N/7) bytes */
  95. if (bcnt + 1 > (maxbits + 6) / 7) {
  96. set_error_buf(error_buf, error_buf_size,
  97. "integer representation too long");
  98. return false;
  99. }
  100. CHECK_BUF(buf, buf_end, offset + 1);
  101. byte = buf[offset];
  102. offset += 1;
  103. result |= ((byte & 0x7f) << shift);
  104. shift += 7;
  105. bcnt += 1;
  106. if ((byte & 0x80) == 0) {
  107. break;
  108. }
  109. }
  110. if (!sign && maxbits == 32 && shift >= maxbits) {
  111. /* The top bits set represent values > 32 bits */
  112. if (((uint8)byte) & 0xf0)
  113. goto fail_integer_too_large;
  114. }
  115. else if (sign && maxbits == 32) {
  116. if (shift < maxbits) {
  117. /* Sign extend, second highest bit is the sign bit */
  118. if ((uint8)byte & 0x40)
  119. result |= (~((uint64)0)) << shift;
  120. }
  121. else {
  122. /* The top bits should be a sign-extension of the sign bit */
  123. bool sign_bit_set = ((uint8)byte) & 0x8;
  124. int top_bits = ((uint8)byte) & 0xf0;
  125. if ((sign_bit_set && top_bits != 0x70)
  126. || (!sign_bit_set && top_bits != 0))
  127. goto fail_integer_too_large;
  128. }
  129. }
  130. else if (sign && maxbits == 64) {
  131. if (shift < maxbits) {
  132. /* Sign extend, second highest bit is the sign bit */
  133. if ((uint8)byte & 0x40)
  134. result |= (~((uint64)0)) << shift;
  135. }
  136. else {
  137. /* The top bits should be a sign-extension of the sign bit */
  138. bool sign_bit_set = ((uint8)byte) & 0x1;
  139. int top_bits = ((uint8)byte) & 0xfe;
  140. if ((sign_bit_set && top_bits != 0x7e)
  141. || (!sign_bit_set && top_bits != 0))
  142. goto fail_integer_too_large;
  143. }
  144. }
  145. *p_buf += offset;
  146. *p_result = result;
  147. return true;
  148. fail_integer_too_large:
  149. set_error_buf(error_buf, error_buf_size, "integer too large");
  150. fail:
  151. return false;
  152. }
  153. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  154. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  155. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  156. #define read_leb_int64(p, p_end, res) \
  157. do { \
  158. uint64 res64; \
  159. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  160. error_buf_size)) \
  161. goto fail; \
  162. res = (int64)res64; \
  163. } while (0)
  164. #define read_leb_uint32(p, p_end, res) \
  165. do { \
  166. uint64 res64; \
  167. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  168. error_buf_size)) \
  169. goto fail; \
  170. res = (uint32)res64; \
  171. } while (0)
  172. #define read_leb_int32(p, p_end, res) \
  173. do { \
  174. uint64 res64; \
  175. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  176. error_buf_size)) \
  177. goto fail; \
  178. res = (int32)res64; \
  179. } while (0)
  180. static char *
  181. type2str(uint8 type)
  182. {
  183. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  184. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  185. return type_str[type - VALUE_TYPE_V128];
  186. else if (type == VALUE_TYPE_FUNCREF)
  187. return "funcref";
  188. else if (type == VALUE_TYPE_EXTERNREF)
  189. return "externref";
  190. else
  191. return "unknown type";
  192. }
  193. static bool
  194. is_32bit_type(uint8 type)
  195. {
  196. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  197. #if WASM_ENABLE_REF_TYPES != 0
  198. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  199. #endif
  200. )
  201. return true;
  202. return false;
  203. }
  204. static bool
  205. is_64bit_type(uint8 type)
  206. {
  207. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  208. return true;
  209. return false;
  210. }
  211. static bool
  212. is_value_type(uint8 type)
  213. {
  214. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64
  215. || type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64
  216. #if WASM_ENABLE_REF_TYPES != 0
  217. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  218. #endif
  219. #if WASM_ENABLE_SIMD != 0
  220. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  221. || type == VALUE_TYPE_V128
  222. #endif
  223. #endif
  224. )
  225. return true;
  226. return false;
  227. }
  228. static bool
  229. is_byte_a_type(uint8 type)
  230. {
  231. return is_value_type(type) || (type == VALUE_TYPE_VOID);
  232. }
  233. #if WASM_ENABLE_SIMD != 0
  234. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  235. static V128
  236. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  237. {
  238. V128 result;
  239. uint8 i;
  240. for (i = 0; i != 16; ++i) {
  241. result.i8x16[i] = read_uint8(p_buf);
  242. }
  243. return result;
  244. }
  245. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  246. #endif /* end of WASM_ENABLE_SIMD */
  247. static void *
  248. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  249. {
  250. void *mem;
  251. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  252. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  253. return NULL;
  254. }
  255. memset(mem, 0, (uint32)size);
  256. return mem;
  257. }
  258. static bool
  259. check_utf8_str(const uint8 *str, uint32 len)
  260. {
  261. /* The valid ranges are taken from page 125, below link
  262. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  263. const uint8 *p = str, *p_end = str + len;
  264. uint8 chr;
  265. while (p < p_end) {
  266. chr = *p;
  267. if (chr < 0x80) {
  268. p++;
  269. }
  270. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  271. if (p[1] < 0x80 || p[1] > 0xBF) {
  272. return false;
  273. }
  274. p += 2;
  275. }
  276. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  277. if (chr == 0xE0) {
  278. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  279. return false;
  280. }
  281. }
  282. else if (chr == 0xED) {
  283. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  284. return false;
  285. }
  286. }
  287. else if (chr >= 0xE1 && chr <= 0xEF) {
  288. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  289. return false;
  290. }
  291. }
  292. p += 3;
  293. }
  294. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  295. if (chr == 0xF0) {
  296. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  297. || p[3] < 0x80 || p[3] > 0xBF) {
  298. return false;
  299. }
  300. }
  301. else if (chr >= 0xF1 && chr <= 0xF3) {
  302. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  303. || p[3] < 0x80 || p[3] > 0xBF) {
  304. return false;
  305. }
  306. }
  307. else if (chr == 0xF4) {
  308. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  309. || p[3] < 0x80 || p[3] > 0xBF) {
  310. return false;
  311. }
  312. }
  313. p += 4;
  314. }
  315. else {
  316. return false;
  317. }
  318. }
  319. return (p == p_end);
  320. }
  321. static char *
  322. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  323. bool is_load_from_file_buf, char *error_buf,
  324. uint32 error_buf_size)
  325. {
  326. StringNode *node, *node_next;
  327. if (!check_utf8_str(str, len)) {
  328. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  329. return NULL;
  330. }
  331. if (len == 0) {
  332. return "";
  333. }
  334. else if (is_load_from_file_buf) {
  335. /* As the file buffer can be referred to after loading, we use
  336. the previous byte of leb encoded size to adjust the string:
  337. move string 1 byte backward and then append '\0' */
  338. char *c_str = (char *)str - 1;
  339. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  340. c_str[len] = '\0';
  341. return c_str;
  342. }
  343. /* Search const str list */
  344. node = module->const_str_list;
  345. while (node) {
  346. node_next = node->next;
  347. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  348. break;
  349. node = node_next;
  350. }
  351. if (node) {
  352. return node->str;
  353. }
  354. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  355. error_buf_size))) {
  356. return NULL;
  357. }
  358. node->str = ((char *)node) + sizeof(StringNode);
  359. bh_memcpy_s(node->str, len + 1, str, len);
  360. node->str[len] = '\0';
  361. if (!module->const_str_list) {
  362. /* set as head */
  363. module->const_str_list = node;
  364. node->next = NULL;
  365. }
  366. else {
  367. /* insert it */
  368. node->next = module->const_str_list;
  369. module->const_str_list = node;
  370. }
  371. return node->str;
  372. }
  373. static void
  374. destroy_wasm_type(WASMType *type)
  375. {
  376. if (type->ref_count > 1) {
  377. /* The type is referenced by other types
  378. of current wasm module */
  379. type->ref_count--;
  380. return;
  381. }
  382. wasm_runtime_free(type);
  383. }
  384. static bool
  385. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  386. InitializerExpression *init_expr, uint8 type, char *error_buf,
  387. uint32 error_buf_size)
  388. {
  389. const uint8 *p = *p_buf, *p_end = buf_end;
  390. uint8 flag, end_byte, *p_float;
  391. uint32 i;
  392. CHECK_BUF(p, p_end, 1);
  393. init_expr->init_expr_type = read_uint8(p);
  394. flag = init_expr->init_expr_type;
  395. switch (flag) {
  396. /* i32.const */
  397. case INIT_EXPR_TYPE_I32_CONST:
  398. if (type != VALUE_TYPE_I32)
  399. goto fail_type_mismatch;
  400. read_leb_int32(p, p_end, init_expr->u.i32);
  401. break;
  402. /* i64.const */
  403. case INIT_EXPR_TYPE_I64_CONST:
  404. if (type != VALUE_TYPE_I64)
  405. goto fail_type_mismatch;
  406. read_leb_int64(p, p_end, init_expr->u.i64);
  407. break;
  408. /* f32.const */
  409. case INIT_EXPR_TYPE_F32_CONST:
  410. if (type != VALUE_TYPE_F32)
  411. goto fail_type_mismatch;
  412. CHECK_BUF(p, p_end, 4);
  413. p_float = (uint8 *)&init_expr->u.f32;
  414. for (i = 0; i < sizeof(float32); i++)
  415. *p_float++ = *p++;
  416. break;
  417. /* f64.const */
  418. case INIT_EXPR_TYPE_F64_CONST:
  419. if (type != VALUE_TYPE_F64)
  420. goto fail_type_mismatch;
  421. CHECK_BUF(p, p_end, 8);
  422. p_float = (uint8 *)&init_expr->u.f64;
  423. for (i = 0; i < sizeof(float64); i++)
  424. *p_float++ = *p++;
  425. break;
  426. #if WASM_ENABLE_SIMD != 0
  427. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  428. case INIT_EXPR_TYPE_V128_CONST:
  429. {
  430. uint64 high, low;
  431. if (type != VALUE_TYPE_V128)
  432. goto fail_type_mismatch;
  433. flag = read_uint8(p);
  434. (void)flag;
  435. CHECK_BUF(p, p_end, 16);
  436. wasm_runtime_read_v128(p, &high, &low);
  437. p += 16;
  438. init_expr->u.v128.i64x2[0] = high;
  439. init_expr->u.v128.i64x2[1] = low;
  440. break;
  441. }
  442. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  443. #endif /* end of WASM_ENABLE_SIMD */
  444. #if WASM_ENABLE_REF_TYPES != 0
  445. case INIT_EXPR_TYPE_FUNCREF_CONST:
  446. {
  447. if (type != VALUE_TYPE_FUNCREF)
  448. goto fail_type_mismatch;
  449. read_leb_uint32(p, p_end, init_expr->u.ref_index);
  450. break;
  451. }
  452. case INIT_EXPR_TYPE_REFNULL_CONST:
  453. {
  454. uint8 reftype;
  455. CHECK_BUF(p, p_end, 1);
  456. reftype = read_uint8(p);
  457. if (reftype != type)
  458. goto fail_type_mismatch;
  459. init_expr->u.ref_index = NULL_REF;
  460. break;
  461. }
  462. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  463. /* get_global */
  464. case INIT_EXPR_TYPE_GET_GLOBAL:
  465. read_leb_uint32(p, p_end, init_expr->u.global_index);
  466. break;
  467. default:
  468. {
  469. set_error_buf(error_buf, error_buf_size,
  470. "illegal opcode "
  471. "or constant expression required "
  472. "or type mismatch");
  473. goto fail;
  474. }
  475. }
  476. CHECK_BUF(p, p_end, 1);
  477. end_byte = read_uint8(p);
  478. if (end_byte != 0x0b)
  479. goto fail_type_mismatch;
  480. *p_buf = p;
  481. return true;
  482. fail_type_mismatch:
  483. set_error_buf(error_buf, error_buf_size,
  484. "type mismatch or constant expression required");
  485. fail:
  486. return false;
  487. }
  488. static bool
  489. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  490. char *error_buf, uint32 error_buf_size)
  491. {
  492. const uint8 *p = buf, *p_end = buf_end, *p_org;
  493. uint32 type_count, param_count, result_count, i, j;
  494. uint32 param_cell_num, ret_cell_num;
  495. uint64 total_size;
  496. uint8 flag;
  497. WASMType *type;
  498. read_leb_uint32(p, p_end, type_count);
  499. if (type_count) {
  500. module->type_count = type_count;
  501. total_size = sizeof(WASMType *) * (uint64)type_count;
  502. if (!(module->types =
  503. loader_malloc(total_size, error_buf, error_buf_size))) {
  504. return false;
  505. }
  506. for (i = 0; i < type_count; i++) {
  507. CHECK_BUF(p, p_end, 1);
  508. flag = read_uint8(p);
  509. if (flag != 0x60) {
  510. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  511. return false;
  512. }
  513. read_leb_uint32(p, p_end, param_count);
  514. /* Resolve param count and result count firstly */
  515. p_org = p;
  516. CHECK_BUF(p, p_end, param_count);
  517. p += param_count;
  518. read_leb_uint32(p, p_end, result_count);
  519. CHECK_BUF(p, p_end, result_count);
  520. p = p_org;
  521. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  522. set_error_buf(error_buf, error_buf_size,
  523. "param count or result count too large");
  524. return false;
  525. }
  526. total_size = offsetof(WASMType, types)
  527. + sizeof(uint8) * (uint64)(param_count + result_count);
  528. if (!(type = module->types[i] =
  529. loader_malloc(total_size, error_buf, error_buf_size))) {
  530. return false;
  531. }
  532. /* Resolve param types and result types */
  533. type->ref_count = 1;
  534. type->param_count = (uint16)param_count;
  535. type->result_count = (uint16)result_count;
  536. for (j = 0; j < param_count; j++) {
  537. CHECK_BUF(p, p_end, 1);
  538. type->types[j] = read_uint8(p);
  539. }
  540. read_leb_uint32(p, p_end, result_count);
  541. for (j = 0; j < result_count; j++) {
  542. CHECK_BUF(p, p_end, 1);
  543. type->types[param_count + j] = read_uint8(p);
  544. }
  545. for (j = 0; j < param_count + result_count; j++) {
  546. if (!is_value_type(type->types[j])) {
  547. set_error_buf(error_buf, error_buf_size,
  548. "unknown value type");
  549. return false;
  550. }
  551. }
  552. param_cell_num = wasm_get_cell_num(type->types, param_count);
  553. ret_cell_num =
  554. wasm_get_cell_num(type->types + param_count, result_count);
  555. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  556. set_error_buf(error_buf, error_buf_size,
  557. "param count or result count too large");
  558. return false;
  559. }
  560. type->param_cell_num = (uint16)param_cell_num;
  561. type->ret_cell_num = (uint16)ret_cell_num;
  562. /* If there is already a same type created, use it instead */
  563. for (j = 0; j < i; j++) {
  564. if (wasm_type_equal(type, module->types[j])) {
  565. if (module->types[j]->ref_count == UINT16_MAX) {
  566. set_error_buf(error_buf, error_buf_size,
  567. "wasm type's ref count too large");
  568. return false;
  569. }
  570. destroy_wasm_type(type);
  571. module->types[i] = module->types[j];
  572. module->types[j]->ref_count++;
  573. break;
  574. }
  575. }
  576. }
  577. }
  578. if (p != p_end) {
  579. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  580. return false;
  581. }
  582. LOG_VERBOSE("Load type section success.\n");
  583. return true;
  584. fail:
  585. return false;
  586. }
  587. static void
  588. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  589. {
  590. uint32 default_max_size =
  591. init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
  592. if (max_size_flag) {
  593. /* module defines the table limitation */
  594. bh_assert(init_size <= *max_size);
  595. if (init_size < *max_size) {
  596. *max_size =
  597. *max_size < default_max_size ? *max_size : default_max_size;
  598. }
  599. }
  600. else {
  601. /* partial defined table limitation, gives a default value */
  602. *max_size = default_max_size;
  603. }
  604. }
  605. #if WASM_ENABLE_MULTI_MODULE != 0
  606. /**
  607. * Find export item of a module with export info:
  608. * module name, field name and export kind
  609. */
  610. static WASMExport *
  611. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  612. const char *field_name, uint8 export_kind,
  613. char *error_buf, uint32 error_buf_size)
  614. {
  615. WASMExport *export;
  616. uint32 i;
  617. uint32 export_index_boundary = 0;
  618. for (i = 0, export = module->exports; i < module->export_count;
  619. ++i, ++export) {
  620. /**
  621. * need to consider a scenario that different kinds of exports
  622. * may have the same name, like
  623. * (table (export "m1" "exported") 10 funcref)
  624. * (memory (export "m1" "exported") 10)
  625. **/
  626. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  627. break;
  628. }
  629. }
  630. if (i == module->export_count) {
  631. LOG_DEBUG("can not find an export %d named %s in the module %s",
  632. export_kind, field_name, module_name);
  633. set_error_buf(error_buf, error_buf_size,
  634. "unknown import or incompatible import type");
  635. return NULL;
  636. }
  637. switch (export_kind) {
  638. case EXPORT_KIND_FUNC:
  639. export_index_boundary =
  640. module->import_function_count + module->function_count;
  641. break;
  642. case EXPORT_KIND_GLOBAL:
  643. export_index_boundary =
  644. module->import_global_count + module->global_count;
  645. break;
  646. case EXPORT_KIND_MEMORY:
  647. export_index_boundary =
  648. module->import_memory_count + module->memory_count;
  649. break;
  650. case EXPORT_KIND_TABLE:
  651. export_index_boundary =
  652. module->import_table_count + module->table_count;
  653. break;
  654. default:
  655. bh_assert(0);
  656. }
  657. if (export->index >= export_index_boundary) {
  658. LOG_DEBUG("%s in the module %s is out of index (%d >= %d )", field_name,
  659. module_name, export->index, export_index_boundary);
  660. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  661. return NULL;
  662. }
  663. return export;
  664. }
  665. static WASMFunction *
  666. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  667. const WASMType *expected_function_type,
  668. char *error_buf, uint32 error_buf_size)
  669. {
  670. WASMModuleCommon *module_reg;
  671. WASMFunction *function = NULL;
  672. WASMExport *export = NULL;
  673. WASMModule *module = NULL;
  674. WASMType *target_function_type = NULL;
  675. module_reg = wasm_runtime_find_module_registered(module_name);
  676. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  677. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  678. function_name);
  679. set_error_buf(error_buf, error_buf_size, "unknown import");
  680. return NULL;
  681. }
  682. module = (WASMModule *)module_reg;
  683. export =
  684. wasm_loader_find_export(module, module_name, function_name,
  685. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  686. if (!export) {
  687. return NULL;
  688. }
  689. /* resolve function type and function */
  690. if (export->index < module->import_function_count) {
  691. target_function_type =
  692. module->import_functions[export->index].u.function.func_type;
  693. function = module->import_functions[export->index]
  694. .u.function.import_func_linked;
  695. }
  696. else {
  697. target_function_type =
  698. module->functions[export->index - module->import_function_count]
  699. ->func_type;
  700. function =
  701. module->functions[export->index - module->import_function_count];
  702. }
  703. /* check function type */
  704. if (!wasm_type_equal(expected_function_type, target_function_type)) {
  705. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  706. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  707. return NULL;
  708. }
  709. return function;
  710. }
  711. static WASMTable *
  712. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  713. uint32 init_size, uint32 max_size, char *error_buf,
  714. uint32 error_buf_size)
  715. {
  716. WASMModuleCommon *module_reg;
  717. WASMTable *table = NULL;
  718. WASMExport *export = NULL;
  719. WASMModule *module = NULL;
  720. module_reg = wasm_runtime_find_module_registered(module_name);
  721. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  722. LOG_DEBUG("can not find a module named %s for table", module_name);
  723. set_error_buf(error_buf, error_buf_size, "unknown import");
  724. return NULL;
  725. }
  726. module = (WASMModule *)module_reg;
  727. export =
  728. wasm_loader_find_export(module, module_name, table_name,
  729. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  730. if (!export) {
  731. return NULL;
  732. }
  733. /* resolve table and check the init/max size */
  734. if (export->index < module->import_table_count) {
  735. table =
  736. module->import_tables[export->index].u.table.import_table_linked;
  737. }
  738. else {
  739. table = &(module->tables[export->index - module->import_table_count]);
  740. }
  741. if (table->init_size < init_size || table->max_size > max_size) {
  742. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  743. module_name, table_name, table->init_size, table->max_size,
  744. init_size, max_size);
  745. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  746. return NULL;
  747. }
  748. return table;
  749. }
  750. static WASMMemory *
  751. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  752. uint32 init_page_count, uint32 max_page_count,
  753. char *error_buf, uint32 error_buf_size)
  754. {
  755. WASMModuleCommon *module_reg;
  756. WASMMemory *memory = NULL;
  757. WASMExport *export = NULL;
  758. WASMModule *module = NULL;
  759. module_reg = wasm_runtime_find_module_registered(module_name);
  760. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  761. LOG_DEBUG("can not find a module named %s for memory", module_name);
  762. set_error_buf(error_buf, error_buf_size, "unknown import");
  763. return NULL;
  764. }
  765. module = (WASMModule *)module_reg;
  766. export =
  767. wasm_loader_find_export(module, module_name, memory_name,
  768. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  769. if (!export) {
  770. return NULL;
  771. }
  772. /* resolve memory and check the init/max page count */
  773. if (export->index < module->import_memory_count) {
  774. memory = module->import_memories[export->index]
  775. .u.memory.import_memory_linked;
  776. }
  777. else {
  778. memory =
  779. &(module->memories[export->index - module->import_memory_count]);
  780. }
  781. if (memory->init_page_count < init_page_count
  782. || memory->max_page_count > max_page_count) {
  783. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  784. module_name, memory_name, memory->init_page_count,
  785. memory->max_page_count, init_page_count, max_page_count);
  786. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  787. return NULL;
  788. }
  789. return memory;
  790. }
  791. static WASMGlobal *
  792. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  793. uint8 type, bool is_mutable, char *error_buf,
  794. uint32 error_buf_size)
  795. {
  796. WASMModuleCommon *module_reg;
  797. WASMGlobal *global = NULL;
  798. WASMExport *export = NULL;
  799. WASMModule *module = NULL;
  800. module_reg = wasm_runtime_find_module_registered(module_name);
  801. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  802. LOG_DEBUG("can not find a module named %s for global", module_name);
  803. set_error_buf(error_buf, error_buf_size, "unknown import");
  804. return NULL;
  805. }
  806. module = (WASMModule *)module_reg;
  807. export =
  808. wasm_loader_find_export(module, module_name, global_name,
  809. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  810. if (!export) {
  811. return NULL;
  812. }
  813. /* resolve and check the global */
  814. if (export->index < module->import_global_count) {
  815. global =
  816. module->import_globals[export->index].u.global.import_global_linked;
  817. }
  818. else {
  819. global =
  820. &(module->globals[export->index - module->import_global_count]);
  821. }
  822. if (global->type != type || global->is_mutable != is_mutable) {
  823. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  824. module_name, global_name, global->type, global->is_mutable,
  825. type, is_mutable);
  826. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  827. return NULL;
  828. }
  829. return global;
  830. }
  831. static WASMModule *
  832. search_sub_module(const WASMModule *parent_module, const char *sub_module_name)
  833. {
  834. WASMRegisteredModule *node =
  835. bh_list_first_elem(parent_module->import_module_list);
  836. while (node && strcmp(sub_module_name, node->module_name)) {
  837. node = bh_list_elem_next(node);
  838. }
  839. return node ? (WASMModule *)node->module : NULL;
  840. }
  841. static bool
  842. register_sub_module(const WASMModule *parent_module,
  843. const char *sub_module_name, WASMModule *sub_module)
  844. {
  845. /* register sub_module into its parent sub module list */
  846. WASMRegisteredModule *node = NULL;
  847. bh_list_status ret;
  848. if (search_sub_module(parent_module, sub_module_name)) {
  849. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  850. return true;
  851. }
  852. node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
  853. if (!node) {
  854. return false;
  855. }
  856. node->module_name = sub_module_name;
  857. node->module = (WASMModuleCommon *)sub_module;
  858. ret = bh_list_insert(parent_module->import_module_list, node);
  859. bh_assert(BH_LIST_SUCCESS == ret);
  860. (void)ret;
  861. return true;
  862. }
  863. static WASMModule *
  864. load_depended_module(const WASMModule *parent_module,
  865. const char *sub_module_name, char *error_buf,
  866. uint32 error_buf_size)
  867. {
  868. WASMModule *sub_module = NULL;
  869. bool ret = false;
  870. uint8 *buffer = NULL;
  871. uint32 buffer_size = 0;
  872. const module_reader reader = wasm_runtime_get_module_reader();
  873. const module_destroyer destroyer = wasm_runtime_get_module_destroyer();
  874. /* check the registered module list of the parent */
  875. sub_module = search_sub_module(parent_module, sub_module_name);
  876. if (sub_module) {
  877. LOG_DEBUG("%s has been loaded before", sub_module_name);
  878. return sub_module;
  879. }
  880. /* check the global registered module list */
  881. sub_module =
  882. (WASMModule *)wasm_runtime_find_module_registered(sub_module_name);
  883. if (sub_module) {
  884. LOG_DEBUG("%s has been loaded", sub_module_name);
  885. goto register_sub_module;
  886. }
  887. LOG_VERBOSE("loading %s", sub_module_name);
  888. if (!reader) {
  889. set_error_buf_v(error_buf, error_buf_size,
  890. "no sub module reader to load %s", sub_module_name);
  891. return NULL;
  892. }
  893. /* start to maintain a loading module list */
  894. ret = wasm_runtime_is_loading_module(sub_module_name);
  895. if (ret) {
  896. set_error_buf_v(error_buf, error_buf_size,
  897. "found circular dependency on %s", sub_module_name);
  898. return NULL;
  899. }
  900. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  901. error_buf_size);
  902. if (!ret) {
  903. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  904. return NULL;
  905. }
  906. ret = reader(sub_module_name, &buffer, &buffer_size);
  907. if (!ret) {
  908. LOG_DEBUG("read the file of %s failed", sub_module_name);
  909. set_error_buf_v(error_buf, error_buf_size, "unknown import",
  910. sub_module_name);
  911. goto delete_loading_module;
  912. }
  913. sub_module =
  914. wasm_loader_load(buffer, buffer_size, false, error_buf, error_buf_size);
  915. if (!sub_module) {
  916. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  917. /* others will be destroyed in runtime_destroy() */
  918. goto destroy_file_buffer;
  919. }
  920. wasm_runtime_delete_loading_module(sub_module_name);
  921. /* register on a global list */
  922. ret = wasm_runtime_register_module_internal(
  923. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  924. error_buf, error_buf_size);
  925. if (!ret) {
  926. LOG_DEBUG("error: can not register module %s globally\n",
  927. sub_module_name);
  928. /* others will be unloaded in runtime_destroy() */
  929. goto unload_module;
  930. }
  931. /* register into its parent list */
  932. register_sub_module:
  933. ret = register_sub_module(parent_module, sub_module_name, sub_module);
  934. if (!ret) {
  935. set_error_buf_v(error_buf, error_buf_size,
  936. "failed to register sub module %s", sub_module_name);
  937. /* since it is in the global module list, no need to
  938. * unload the module. the runtime_destroy() will do it
  939. */
  940. return NULL;
  941. }
  942. return sub_module;
  943. unload_module:
  944. wasm_loader_unload(sub_module);
  945. destroy_file_buffer:
  946. if (destroyer) {
  947. destroyer(buffer, buffer_size);
  948. }
  949. else {
  950. LOG_WARNING("need to release the reading buffer of %s manually",
  951. sub_module_name);
  952. }
  953. delete_loading_module:
  954. wasm_runtime_delete_loading_module(sub_module_name);
  955. return NULL;
  956. }
  957. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  958. static bool
  959. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  960. const WASMModule *parent_module,
  961. const char *sub_module_name, const char *function_name,
  962. WASMFunctionImport *function, char *error_buf,
  963. uint32 error_buf_size)
  964. {
  965. const uint8 *p = *p_buf, *p_end = buf_end;
  966. uint32 declare_type_index = 0;
  967. WASMType *declare_func_type = NULL;
  968. WASMFunction *linked_func = NULL;
  969. #if WASM_ENABLE_MULTI_MODULE != 0
  970. WASMModule *sub_module = NULL;
  971. #endif
  972. const char *linked_signature = NULL;
  973. void *linked_attachment = NULL;
  974. bool linked_call_conv_raw = false;
  975. bool is_native_symbol = false;
  976. read_leb_uint32(p, p_end, declare_type_index);
  977. *p_buf = p;
  978. if (declare_type_index >= parent_module->type_count) {
  979. set_error_buf(error_buf, error_buf_size, "unknown type");
  980. return false;
  981. }
  982. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  983. declare_type_index = wasm_get_smallest_type_idx(
  984. parent_module->types, parent_module->type_count, declare_type_index);
  985. #endif
  986. declare_func_type = parent_module->types[declare_type_index];
  987. /* lookup registered native symbols first */
  988. linked_func = wasm_native_resolve_symbol(
  989. sub_module_name, function_name, declare_func_type, &linked_signature,
  990. &linked_attachment, &linked_call_conv_raw);
  991. if (linked_func) {
  992. is_native_symbol = true;
  993. }
  994. #if WASM_ENABLE_MULTI_MODULE != 0
  995. else {
  996. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  997. sub_module = load_depended_module(parent_module, sub_module_name,
  998. error_buf, error_buf_size);
  999. if (!sub_module) {
  1000. return false;
  1001. }
  1002. }
  1003. linked_func = wasm_loader_resolve_function(
  1004. sub_module_name, function_name, declare_func_type, error_buf,
  1005. error_buf_size);
  1006. }
  1007. #endif
  1008. function->module_name = (char *)sub_module_name;
  1009. function->field_name = (char *)function_name;
  1010. function->func_type = declare_func_type;
  1011. /* func_ptr_linked is for native registered symbol */
  1012. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  1013. function->signature = linked_signature;
  1014. function->attachment = linked_attachment;
  1015. function->call_conv_raw = linked_call_conv_raw;
  1016. #if WASM_ENABLE_MULTI_MODULE != 0
  1017. function->import_module = is_native_symbol ? NULL : sub_module;
  1018. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  1019. #endif
  1020. return true;
  1021. fail:
  1022. return false;
  1023. }
  1024. static bool
  1025. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1026. uint32 error_buf_size)
  1027. {
  1028. if (max_size < init_size) {
  1029. set_error_buf(error_buf, error_buf_size,
  1030. "size minimum must not be greater than maximum");
  1031. return false;
  1032. }
  1033. return true;
  1034. }
  1035. static bool
  1036. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  1037. WASMModule *parent_module, const char *sub_module_name,
  1038. const char *table_name, WASMTableImport *table,
  1039. char *error_buf, uint32 error_buf_size)
  1040. {
  1041. const uint8 *p = *p_buf, *p_end = buf_end;
  1042. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  1043. declare_init_size = 0, declare_max_size = 0;
  1044. #if WASM_ENABLE_MULTI_MODULE != 0
  1045. WASMModule *sub_module = NULL;
  1046. WASMTable *linked_table = NULL;
  1047. #endif
  1048. CHECK_BUF(p, p_end, 1);
  1049. /* 0x70 or 0x6F */
  1050. declare_elem_type = read_uint8(p);
  1051. if (VALUE_TYPE_FUNCREF != declare_elem_type
  1052. #if WASM_ENABLE_REF_TYPES != 0
  1053. && VALUE_TYPE_EXTERNREF != declare_elem_type
  1054. #endif
  1055. ) {
  1056. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1057. return false;
  1058. }
  1059. read_leb_uint32(p, p_end, declare_max_size_flag);
  1060. if (declare_max_size_flag > 1) {
  1061. set_error_buf(error_buf, error_buf_size, "integer too large");
  1062. return false;
  1063. }
  1064. read_leb_uint32(p, p_end, declare_init_size);
  1065. if (declare_max_size_flag) {
  1066. read_leb_uint32(p, p_end, declare_max_size);
  1067. if (!check_table_max_size(declare_init_size, declare_max_size,
  1068. error_buf, error_buf_size))
  1069. return false;
  1070. }
  1071. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  1072. &declare_max_size);
  1073. *p_buf = p;
  1074. #if WASM_ENABLE_MULTI_MODULE != 0
  1075. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1076. sub_module = load_depended_module(parent_module, sub_module_name,
  1077. error_buf, error_buf_size);
  1078. if (!sub_module) {
  1079. return false;
  1080. }
  1081. linked_table = wasm_loader_resolve_table(
  1082. sub_module_name, table_name, declare_init_size, declare_max_size,
  1083. error_buf, error_buf_size);
  1084. if (!linked_table) {
  1085. return false;
  1086. }
  1087. /* reset with linked table limit */
  1088. declare_elem_type = linked_table->elem_type;
  1089. declare_init_size = linked_table->init_size;
  1090. declare_max_size = linked_table->max_size;
  1091. declare_max_size_flag = linked_table->flags;
  1092. table->import_table_linked = linked_table;
  1093. table->import_module = sub_module;
  1094. }
  1095. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  1096. /* (table (export "table") 10 20 funcref) */
  1097. /* we need this section working in wamrc */
  1098. if (!strcmp("spectest", sub_module_name)) {
  1099. const uint32 spectest_table_init_size = 10;
  1100. const uint32 spectest_table_max_size = 20;
  1101. if (strcmp("table", table_name)) {
  1102. set_error_buf(error_buf, error_buf_size,
  1103. "incompatible import type or unknown import");
  1104. return false;
  1105. }
  1106. if (declare_init_size > spectest_table_init_size
  1107. || declare_max_size < spectest_table_max_size) {
  1108. set_error_buf(error_buf, error_buf_size,
  1109. "incompatible import type");
  1110. return false;
  1111. }
  1112. declare_init_size = spectest_table_init_size;
  1113. declare_max_size = spectest_table_max_size;
  1114. }
  1115. /* now we believe all declaration are ok */
  1116. table->elem_type = declare_elem_type;
  1117. table->init_size = declare_init_size;
  1118. table->flags = declare_max_size_flag;
  1119. table->max_size = declare_max_size;
  1120. return true;
  1121. fail:
  1122. return false;
  1123. }
  1124. static bool
  1125. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  1126. {
  1127. if (init_size > 65536) {
  1128. set_error_buf(error_buf, error_buf_size,
  1129. "memory size must be at most 65536 pages (4GiB)");
  1130. return false;
  1131. }
  1132. return true;
  1133. }
  1134. static bool
  1135. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1136. uint32 error_buf_size)
  1137. {
  1138. if (max_size < init_size) {
  1139. set_error_buf(error_buf, error_buf_size,
  1140. "size minimum must not be greater than maximum");
  1141. return false;
  1142. }
  1143. if (max_size > 65536) {
  1144. set_error_buf(error_buf, error_buf_size,
  1145. "memory size must be at most 65536 pages (4GiB)");
  1146. return false;
  1147. }
  1148. return true;
  1149. }
  1150. static bool
  1151. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  1152. WASMModule *parent_module, const char *sub_module_name,
  1153. const char *memory_name, WASMMemoryImport *memory,
  1154. char *error_buf, uint32 error_buf_size)
  1155. {
  1156. const uint8 *p = *p_buf, *p_end = buf_end;
  1157. uint32 pool_size = wasm_runtime_memory_pool_size();
  1158. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1159. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1160. / DEFAULT_NUM_BYTES_PER_PAGE;
  1161. #else
  1162. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  1163. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  1164. uint32 declare_max_page_count_flag = 0;
  1165. uint32 declare_init_page_count = 0;
  1166. uint32 declare_max_page_count = 0;
  1167. #if WASM_ENABLE_MULTI_MODULE != 0
  1168. WASMModule *sub_module = NULL;
  1169. WASMMemory *linked_memory = NULL;
  1170. #endif
  1171. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  1172. read_leb_uint32(p, p_end, declare_init_page_count);
  1173. if (!check_memory_init_size(declare_init_page_count, error_buf,
  1174. error_buf_size)) {
  1175. return false;
  1176. }
  1177. if (declare_max_page_count_flag & 1) {
  1178. read_leb_uint32(p, p_end, declare_max_page_count);
  1179. if (!check_memory_max_size(declare_init_page_count,
  1180. declare_max_page_count, error_buf,
  1181. error_buf_size)) {
  1182. return false;
  1183. }
  1184. if (declare_max_page_count > max_page_count) {
  1185. declare_max_page_count = max_page_count;
  1186. }
  1187. }
  1188. else {
  1189. /* Limit the maximum memory size to max_page_count */
  1190. declare_max_page_count = max_page_count;
  1191. }
  1192. #if WASM_ENABLE_MULTI_MODULE != 0
  1193. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1194. sub_module = load_depended_module(parent_module, sub_module_name,
  1195. error_buf, error_buf_size);
  1196. if (!sub_module) {
  1197. return false;
  1198. }
  1199. linked_memory = wasm_loader_resolve_memory(
  1200. sub_module_name, memory_name, declare_init_page_count,
  1201. declare_max_page_count, error_buf, error_buf_size);
  1202. if (!linked_memory) {
  1203. return false;
  1204. }
  1205. /**
  1206. * reset with linked memory limit
  1207. */
  1208. memory->import_module = sub_module;
  1209. memory->import_memory_linked = linked_memory;
  1210. declare_init_page_count = linked_memory->init_page_count;
  1211. declare_max_page_count = linked_memory->max_page_count;
  1212. }
  1213. #endif
  1214. /* (memory (export "memory") 1 2) */
  1215. if (!strcmp("spectest", sub_module_name)) {
  1216. uint32 spectest_memory_init_page = 1;
  1217. uint32 spectest_memory_max_page = 2;
  1218. if (strcmp("memory", memory_name)) {
  1219. set_error_buf(error_buf, error_buf_size,
  1220. "incompatible import type or unknown import");
  1221. return false;
  1222. }
  1223. if (declare_init_page_count > spectest_memory_init_page
  1224. || declare_max_page_count < spectest_memory_max_page) {
  1225. set_error_buf(error_buf, error_buf_size,
  1226. "incompatible import type");
  1227. return false;
  1228. }
  1229. declare_init_page_count = spectest_memory_init_page;
  1230. declare_max_page_count = spectest_memory_max_page;
  1231. }
  1232. /* now we believe all declaration are ok */
  1233. memory->flags = declare_max_page_count_flag;
  1234. memory->init_page_count = declare_init_page_count;
  1235. memory->max_page_count = declare_max_page_count;
  1236. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1237. *p_buf = p;
  1238. return true;
  1239. fail:
  1240. return false;
  1241. }
  1242. static bool
  1243. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  1244. const WASMModule *parent_module, char *sub_module_name,
  1245. char *global_name, WASMGlobalImport *global, char *error_buf,
  1246. uint32 error_buf_size)
  1247. {
  1248. const uint8 *p = *p_buf, *p_end = buf_end;
  1249. uint8 declare_type = 0;
  1250. uint8 declare_mutable = 0;
  1251. #if WASM_ENABLE_MULTI_MODULE != 0
  1252. WASMModule *sub_module = NULL;
  1253. WASMGlobal *linked_global = NULL;
  1254. #endif
  1255. CHECK_BUF(p, p_end, 2);
  1256. declare_type = read_uint8(p);
  1257. declare_mutable = read_uint8(p);
  1258. *p_buf = p;
  1259. if (declare_mutable >= 2) {
  1260. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1261. return false;
  1262. }
  1263. #if WASM_ENABLE_LIBC_BUILTIN != 0
  1264. global->is_linked = wasm_native_lookup_libc_builtin_global(
  1265. sub_module_name, global_name, global);
  1266. if (global->is_linked) {
  1267. if (global->type != declare_type
  1268. || global->is_mutable != declare_mutable) {
  1269. set_error_buf(error_buf, error_buf_size,
  1270. "incompatible import type");
  1271. return false;
  1272. }
  1273. }
  1274. #endif
  1275. #if WASM_ENABLE_MULTI_MODULE != 0
  1276. if (!global->is_linked
  1277. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  1278. sub_module = load_depended_module(parent_module, sub_module_name,
  1279. error_buf, error_buf_size);
  1280. if (!sub_module) {
  1281. return false;
  1282. }
  1283. /* check sub modules */
  1284. linked_global = wasm_loader_resolve_global(
  1285. sub_module_name, global_name, declare_type, declare_mutable,
  1286. error_buf, error_buf_size);
  1287. if (linked_global) {
  1288. global->import_module = sub_module;
  1289. global->import_global_linked = linked_global;
  1290. global->is_linked = true;
  1291. }
  1292. }
  1293. #endif
  1294. global->module_name = sub_module_name;
  1295. global->field_name = global_name;
  1296. global->type = declare_type;
  1297. global->is_mutable = (declare_mutable == 1);
  1298. return true;
  1299. fail:
  1300. return false;
  1301. }
  1302. static bool
  1303. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  1304. char *error_buf, uint32 error_buf_size)
  1305. {
  1306. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1307. CHECK_BUF(p, p_end, 1);
  1308. /* 0x70 or 0x6F */
  1309. table->elem_type = read_uint8(p);
  1310. if (VALUE_TYPE_FUNCREF != table->elem_type
  1311. #if WASM_ENABLE_REF_TYPES != 0
  1312. && VALUE_TYPE_EXTERNREF != table->elem_type
  1313. #endif
  1314. ) {
  1315. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1316. return false;
  1317. }
  1318. p_org = p;
  1319. read_leb_uint32(p, p_end, table->flags);
  1320. #if WASM_ENABLE_SHARED_MEMORY == 0
  1321. if (p - p_org > 1) {
  1322. set_error_buf(error_buf, error_buf_size,
  1323. "integer representation too long");
  1324. return false;
  1325. }
  1326. if (table->flags > 1) {
  1327. set_error_buf(error_buf, error_buf_size, "integer too large");
  1328. return false;
  1329. }
  1330. #else
  1331. if (p - p_org > 1) {
  1332. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1333. return false;
  1334. }
  1335. if (table->flags == 2) {
  1336. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  1337. return false;
  1338. }
  1339. if (table->flags > 1) {
  1340. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1341. return false;
  1342. }
  1343. #endif
  1344. read_leb_uint32(p, p_end, table->init_size);
  1345. if (table->flags) {
  1346. read_leb_uint32(p, p_end, table->max_size);
  1347. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  1348. error_buf_size))
  1349. return false;
  1350. }
  1351. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  1352. *p_buf = p;
  1353. return true;
  1354. fail:
  1355. return false;
  1356. }
  1357. static bool
  1358. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  1359. char *error_buf, uint32 error_buf_size)
  1360. {
  1361. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1362. uint32 pool_size = wasm_runtime_memory_pool_size();
  1363. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1364. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1365. / DEFAULT_NUM_BYTES_PER_PAGE;
  1366. #else
  1367. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  1368. #endif
  1369. p_org = p;
  1370. read_leb_uint32(p, p_end, memory->flags);
  1371. #if WASM_ENABLE_SHARED_MEMORY == 0
  1372. if (p - p_org > 1) {
  1373. set_error_buf(error_buf, error_buf_size,
  1374. "integer representation too long");
  1375. return false;
  1376. }
  1377. if (memory->flags > 1) {
  1378. set_error_buf(error_buf, error_buf_size, "integer too large");
  1379. return false;
  1380. }
  1381. #else
  1382. if (p - p_org > 1) {
  1383. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1384. return false;
  1385. }
  1386. if (memory->flags > 3) {
  1387. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1388. return false;
  1389. }
  1390. else if (memory->flags == 2) {
  1391. set_error_buf(error_buf, error_buf_size,
  1392. "shared memory must have maximum");
  1393. return false;
  1394. }
  1395. #endif
  1396. read_leb_uint32(p, p_end, memory->init_page_count);
  1397. if (!check_memory_init_size(memory->init_page_count, error_buf,
  1398. error_buf_size))
  1399. return false;
  1400. if (memory->flags & 1) {
  1401. read_leb_uint32(p, p_end, memory->max_page_count);
  1402. if (!check_memory_max_size(memory->init_page_count,
  1403. memory->max_page_count, error_buf,
  1404. error_buf_size))
  1405. return false;
  1406. if (memory->max_page_count > max_page_count)
  1407. memory->max_page_count = max_page_count;
  1408. }
  1409. else {
  1410. /* Limit the maximum memory size to max_page_count */
  1411. memory->max_page_count = max_page_count;
  1412. }
  1413. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1414. *p_buf = p;
  1415. return true;
  1416. fail:
  1417. return false;
  1418. }
  1419. static bool
  1420. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1421. bool is_load_from_file_buf, char *error_buf,
  1422. uint32 error_buf_size)
  1423. {
  1424. const uint8 *p = buf, *p_end = buf_end, *p_old;
  1425. uint32 import_count, name_len, type_index, i, u32, flags;
  1426. uint64 total_size;
  1427. WASMImport *import;
  1428. WASMImport *import_functions = NULL, *import_tables = NULL;
  1429. WASMImport *import_memories = NULL, *import_globals = NULL;
  1430. char *sub_module_name, *field_name;
  1431. uint8 u8, kind;
  1432. read_leb_uint32(p, p_end, import_count);
  1433. if (import_count) {
  1434. module->import_count = import_count;
  1435. total_size = sizeof(WASMImport) * (uint64)import_count;
  1436. if (!(module->imports =
  1437. loader_malloc(total_size, error_buf, error_buf_size))) {
  1438. return false;
  1439. }
  1440. p_old = p;
  1441. /* Scan firstly to get import count of each type */
  1442. for (i = 0; i < import_count; i++) {
  1443. /* module name */
  1444. read_leb_uint32(p, p_end, name_len);
  1445. CHECK_BUF(p, p_end, name_len);
  1446. p += name_len;
  1447. /* field name */
  1448. read_leb_uint32(p, p_end, name_len);
  1449. CHECK_BUF(p, p_end, name_len);
  1450. p += name_len;
  1451. CHECK_BUF(p, p_end, 1);
  1452. /* 0x00/0x01/0x02/0x03 */
  1453. kind = read_uint8(p);
  1454. switch (kind) {
  1455. case IMPORT_KIND_FUNC: /* import function */
  1456. read_leb_uint32(p, p_end, type_index);
  1457. module->import_function_count++;
  1458. break;
  1459. case IMPORT_KIND_TABLE: /* import table */
  1460. CHECK_BUF(p, p_end, 1);
  1461. /* 0x70 */
  1462. u8 = read_uint8(p);
  1463. read_leb_uint32(p, p_end, flags);
  1464. read_leb_uint32(p, p_end, u32);
  1465. if (flags & 1)
  1466. read_leb_uint32(p, p_end, u32);
  1467. module->import_table_count++;
  1468. #if WASM_ENABLE_REF_TYPES == 0
  1469. if (module->import_table_count > 1) {
  1470. set_error_buf(error_buf, error_buf_size,
  1471. "multiple tables");
  1472. return false;
  1473. }
  1474. #endif
  1475. break;
  1476. case IMPORT_KIND_MEMORY: /* import memory */
  1477. read_leb_uint32(p, p_end, flags);
  1478. read_leb_uint32(p, p_end, u32);
  1479. if (flags & 1)
  1480. read_leb_uint32(p, p_end, u32);
  1481. module->import_memory_count++;
  1482. if (module->import_memory_count > 1) {
  1483. set_error_buf(error_buf, error_buf_size,
  1484. "multiple memories");
  1485. return false;
  1486. }
  1487. break;
  1488. case IMPORT_KIND_GLOBAL: /* import global */
  1489. CHECK_BUF(p, p_end, 2);
  1490. p += 2;
  1491. module->import_global_count++;
  1492. break;
  1493. default:
  1494. set_error_buf(error_buf, error_buf_size,
  1495. "invalid import kind");
  1496. return false;
  1497. }
  1498. }
  1499. if (module->import_function_count)
  1500. import_functions = module->import_functions = module->imports;
  1501. if (module->import_table_count)
  1502. import_tables = module->import_tables =
  1503. module->imports + module->import_function_count;
  1504. if (module->import_memory_count)
  1505. import_memories = module->import_memories =
  1506. module->imports + module->import_function_count
  1507. + module->import_table_count;
  1508. if (module->import_global_count)
  1509. import_globals = module->import_globals =
  1510. module->imports + module->import_function_count
  1511. + module->import_table_count + module->import_memory_count;
  1512. p = p_old;
  1513. /* Scan again to resolve the data */
  1514. for (i = 0; i < import_count; i++) {
  1515. /* load module name */
  1516. read_leb_uint32(p, p_end, name_len);
  1517. CHECK_BUF(p, p_end, name_len);
  1518. if (!(sub_module_name = const_str_list_insert(
  1519. p, name_len, module, is_load_from_file_buf, error_buf,
  1520. error_buf_size))) {
  1521. return false;
  1522. }
  1523. p += name_len;
  1524. /* load field name */
  1525. read_leb_uint32(p, p_end, name_len);
  1526. CHECK_BUF(p, p_end, name_len);
  1527. if (!(field_name = const_str_list_insert(
  1528. p, name_len, module, is_load_from_file_buf, error_buf,
  1529. error_buf_size))) {
  1530. return false;
  1531. }
  1532. p += name_len;
  1533. CHECK_BUF(p, p_end, 1);
  1534. /* 0x00/0x01/0x02/0x03 */
  1535. kind = read_uint8(p);
  1536. switch (kind) {
  1537. case IMPORT_KIND_FUNC: /* import function */
  1538. bh_assert(import_functions);
  1539. import = import_functions++;
  1540. if (!load_function_import(
  1541. &p, p_end, module, sub_module_name, field_name,
  1542. &import->u.function, error_buf, error_buf_size)) {
  1543. return false;
  1544. }
  1545. break;
  1546. case IMPORT_KIND_TABLE: /* import table */
  1547. bh_assert(import_tables);
  1548. import = import_tables++;
  1549. if (!load_table_import(&p, p_end, module, sub_module_name,
  1550. field_name, &import->u.table,
  1551. error_buf, error_buf_size)) {
  1552. LOG_DEBUG("can not import such a table (%s,%s)",
  1553. sub_module_name, field_name);
  1554. return false;
  1555. }
  1556. break;
  1557. case IMPORT_KIND_MEMORY: /* import memory */
  1558. bh_assert(import_memories);
  1559. import = import_memories++;
  1560. if (!load_memory_import(&p, p_end, module, sub_module_name,
  1561. field_name, &import->u.memory,
  1562. error_buf, error_buf_size)) {
  1563. return false;
  1564. }
  1565. break;
  1566. case IMPORT_KIND_GLOBAL: /* import global */
  1567. bh_assert(import_globals);
  1568. import = import_globals++;
  1569. if (!load_global_import(&p, p_end, module, sub_module_name,
  1570. field_name, &import->u.global,
  1571. error_buf, error_buf_size)) {
  1572. return false;
  1573. }
  1574. break;
  1575. default:
  1576. set_error_buf(error_buf, error_buf_size,
  1577. "invalid import kind");
  1578. return false;
  1579. }
  1580. import->kind = kind;
  1581. import->u.names.module_name = sub_module_name;
  1582. import->u.names.field_name = field_name;
  1583. }
  1584. #if WASM_ENABLE_LIBC_WASI != 0
  1585. import = module->import_functions;
  1586. for (i = 0; i < module->import_function_count; i++, import++) {
  1587. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  1588. || !strcmp(import->u.names.module_name,
  1589. "wasi_snapshot_preview1")) {
  1590. module->import_wasi_api = true;
  1591. break;
  1592. }
  1593. }
  1594. #endif
  1595. }
  1596. if (p != p_end) {
  1597. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1598. return false;
  1599. }
  1600. LOG_VERBOSE("Load import section success.\n");
  1601. (void)u8;
  1602. (void)u32;
  1603. (void)type_index;
  1604. return true;
  1605. fail:
  1606. return false;
  1607. }
  1608. static bool
  1609. init_function_local_offsets(WASMFunction *func, char *error_buf,
  1610. uint32 error_buf_size)
  1611. {
  1612. WASMType *param_type = func->func_type;
  1613. uint32 param_count = param_type->param_count;
  1614. uint8 *param_types = param_type->types;
  1615. uint32 local_count = func->local_count;
  1616. uint8 *local_types = func->local_types;
  1617. uint32 i, local_offset = 0;
  1618. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  1619. /*
  1620. * Only allocate memory when total_size is not 0,
  1621. * or the return value of malloc(0) might be NULL on some platforms,
  1622. * which causes wasm loader return false.
  1623. */
  1624. if (total_size > 0
  1625. && !(func->local_offsets =
  1626. loader_malloc(total_size, error_buf, error_buf_size))) {
  1627. return false;
  1628. }
  1629. for (i = 0; i < param_count; i++) {
  1630. func->local_offsets[i] = (uint16)local_offset;
  1631. local_offset += wasm_value_type_cell_num(param_types[i]);
  1632. }
  1633. for (i = 0; i < local_count; i++) {
  1634. func->local_offsets[param_count + i] = (uint16)local_offset;
  1635. local_offset += wasm_value_type_cell_num(local_types[i]);
  1636. }
  1637. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  1638. return true;
  1639. }
  1640. static bool
  1641. load_function_section(const uint8 *buf, const uint8 *buf_end,
  1642. const uint8 *buf_code, const uint8 *buf_code_end,
  1643. WASMModule *module, char *error_buf,
  1644. uint32 error_buf_size)
  1645. {
  1646. const uint8 *p = buf, *p_end = buf_end;
  1647. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  1648. uint32 func_count;
  1649. uint64 total_size;
  1650. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  1651. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  1652. uint8 type;
  1653. WASMFunction *func;
  1654. read_leb_uint32(p, p_end, func_count);
  1655. if (buf_code)
  1656. read_leb_uint32(p_code, buf_code_end, code_count);
  1657. if (func_count != code_count) {
  1658. set_error_buf(error_buf, error_buf_size,
  1659. "function and code section have inconsistent lengths or "
  1660. "unexpected end");
  1661. return false;
  1662. }
  1663. if (func_count) {
  1664. module->function_count = func_count;
  1665. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  1666. if (!(module->functions =
  1667. loader_malloc(total_size, error_buf, error_buf_size))) {
  1668. return false;
  1669. }
  1670. for (i = 0; i < func_count; i++) {
  1671. /* Resolve function type */
  1672. read_leb_uint32(p, p_end, type_index);
  1673. if (type_index >= module->type_count) {
  1674. set_error_buf(error_buf, error_buf_size, "unknown type");
  1675. return false;
  1676. }
  1677. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1678. type_index = wasm_get_smallest_type_idx(
  1679. module->types, module->type_count, type_index);
  1680. #endif
  1681. read_leb_uint32(p_code, buf_code_end, code_size);
  1682. if (code_size == 0 || p_code + code_size > buf_code_end) {
  1683. set_error_buf(error_buf, error_buf_size,
  1684. "invalid function code size");
  1685. return false;
  1686. }
  1687. /* Resolve local set count */
  1688. p_code_end = p_code + code_size;
  1689. local_count = 0;
  1690. read_leb_uint32(p_code, buf_code_end, local_set_count);
  1691. p_code_save = p_code;
  1692. /* Calculate total local count */
  1693. for (j = 0; j < local_set_count; j++) {
  1694. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1695. if (sub_local_count > UINT32_MAX - local_count) {
  1696. set_error_buf(error_buf, error_buf_size, "too many locals");
  1697. return false;
  1698. }
  1699. CHECK_BUF(p_code, buf_code_end, 1);
  1700. /* 0x7F/0x7E/0x7D/0x7C */
  1701. type = read_uint8(p_code);
  1702. local_count += sub_local_count;
  1703. }
  1704. /* Alloc memory, layout: function structure + local types */
  1705. code_size = (uint32)(p_code_end - p_code);
  1706. total_size = sizeof(WASMFunction) + (uint64)local_count;
  1707. if (!(func = module->functions[i] =
  1708. loader_malloc(total_size, error_buf, error_buf_size))) {
  1709. return false;
  1710. }
  1711. /* Set function type, local count, code size and code body */
  1712. func->func_type = module->types[type_index];
  1713. func->local_count = local_count;
  1714. if (local_count > 0)
  1715. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  1716. func->code_size = code_size;
  1717. /*
  1718. * we shall make a copy of code body [p_code, p_code + code_size]
  1719. * when we are worrying about inappropriate releasing behaviour.
  1720. * all code bodies are actually in a buffer which user allocates in
  1721. * his embedding environment and we don't have power on them.
  1722. * it will be like:
  1723. * code_body_cp = malloc(code_size);
  1724. * memcpy(code_body_cp, p_code, code_size);
  1725. * func->code = code_body_cp;
  1726. */
  1727. func->code = (uint8 *)p_code;
  1728. /* Load each local type */
  1729. p_code = p_code_save;
  1730. local_type_index = 0;
  1731. for (j = 0; j < local_set_count; j++) {
  1732. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1733. if (!sub_local_count
  1734. || local_type_index > UINT32_MAX - sub_local_count
  1735. || local_type_index + sub_local_count > local_count) {
  1736. set_error_buf(error_buf, error_buf_size,
  1737. "invalid local count");
  1738. return false;
  1739. }
  1740. CHECK_BUF(p_code, buf_code_end, 1);
  1741. /* 0x7F/0x7E/0x7D/0x7C */
  1742. type = read_uint8(p_code);
  1743. if (!is_value_type(type)) {
  1744. if (type == VALUE_TYPE_V128)
  1745. set_error_buf(error_buf, error_buf_size,
  1746. "v128 value type requires simd feature");
  1747. else if (type == VALUE_TYPE_FUNCREF
  1748. || type == VALUE_TYPE_EXTERNREF)
  1749. set_error_buf(error_buf, error_buf_size,
  1750. "ref value type requires "
  1751. "reference types feature");
  1752. else
  1753. set_error_buf_v(error_buf, error_buf_size,
  1754. "invalid local type 0x%02X", type);
  1755. return false;
  1756. }
  1757. for (k = 0; k < sub_local_count; k++) {
  1758. func->local_types[local_type_index++] = type;
  1759. }
  1760. }
  1761. func->param_cell_num = func->func_type->param_cell_num;
  1762. func->ret_cell_num = func->func_type->ret_cell_num;
  1763. local_cell_num =
  1764. wasm_get_cell_num(func->local_types, func->local_count);
  1765. if (local_cell_num > UINT16_MAX) {
  1766. set_error_buf(error_buf, error_buf_size,
  1767. "local count too large");
  1768. return false;
  1769. }
  1770. func->local_cell_num = (uint16)local_cell_num;
  1771. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  1772. return false;
  1773. p_code = p_code_end;
  1774. }
  1775. }
  1776. if (p != p_end) {
  1777. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1778. return false;
  1779. }
  1780. LOG_VERBOSE("Load function section success.\n");
  1781. return true;
  1782. fail:
  1783. return false;
  1784. }
  1785. static bool
  1786. check_function_index(const WASMModule *module, uint32 function_index,
  1787. char *error_buf, uint32 error_buf_size)
  1788. {
  1789. if (function_index
  1790. >= module->import_function_count + module->function_count) {
  1791. set_error_buf_v(error_buf, error_buf_size, "unknown function %d",
  1792. function_index);
  1793. return false;
  1794. }
  1795. return true;
  1796. }
  1797. static bool
  1798. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1799. char *error_buf, uint32 error_buf_size)
  1800. {
  1801. const uint8 *p = buf, *p_end = buf_end;
  1802. uint32 table_count, i;
  1803. uint64 total_size;
  1804. WASMTable *table;
  1805. read_leb_uint32(p, p_end, table_count);
  1806. #if WASM_ENABLE_REF_TYPES == 0
  1807. if (module->import_table_count + table_count > 1) {
  1808. /* a total of one table is allowed */
  1809. set_error_buf(error_buf, error_buf_size, "multiple tables");
  1810. return false;
  1811. }
  1812. #endif
  1813. if (table_count) {
  1814. module->table_count = table_count;
  1815. total_size = sizeof(WASMTable) * (uint64)table_count;
  1816. if (!(module->tables =
  1817. loader_malloc(total_size, error_buf, error_buf_size))) {
  1818. return false;
  1819. }
  1820. /* load each table */
  1821. table = module->tables;
  1822. for (i = 0; i < table_count; i++, table++)
  1823. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  1824. return false;
  1825. }
  1826. if (p != p_end) {
  1827. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1828. return false;
  1829. }
  1830. LOG_VERBOSE("Load table section success.\n");
  1831. return true;
  1832. fail:
  1833. return false;
  1834. }
  1835. static bool
  1836. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1837. char *error_buf, uint32 error_buf_size)
  1838. {
  1839. const uint8 *p = buf, *p_end = buf_end;
  1840. uint32 memory_count, i;
  1841. uint64 total_size;
  1842. WASMMemory *memory;
  1843. read_leb_uint32(p, p_end, memory_count);
  1844. /* a total of one memory is allowed */
  1845. if (module->import_memory_count + memory_count > 1) {
  1846. set_error_buf(error_buf, error_buf_size, "multiple memories");
  1847. return false;
  1848. }
  1849. if (memory_count) {
  1850. module->memory_count = memory_count;
  1851. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  1852. if (!(module->memories =
  1853. loader_malloc(total_size, error_buf, error_buf_size))) {
  1854. return false;
  1855. }
  1856. /* load each memory */
  1857. memory = module->memories;
  1858. for (i = 0; i < memory_count; i++, memory++)
  1859. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  1860. return false;
  1861. }
  1862. if (p != p_end) {
  1863. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1864. return false;
  1865. }
  1866. LOG_VERBOSE("Load memory section success.\n");
  1867. return true;
  1868. fail:
  1869. return false;
  1870. }
  1871. static bool
  1872. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1873. char *error_buf, uint32 error_buf_size)
  1874. {
  1875. const uint8 *p = buf, *p_end = buf_end;
  1876. uint32 global_count, i;
  1877. uint64 total_size;
  1878. WASMGlobal *global;
  1879. uint8 mutable;
  1880. read_leb_uint32(p, p_end, global_count);
  1881. if (global_count) {
  1882. module->global_count = global_count;
  1883. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  1884. if (!(module->globals =
  1885. loader_malloc(total_size, error_buf, error_buf_size))) {
  1886. return false;
  1887. }
  1888. global = module->globals;
  1889. for (i = 0; i < global_count; i++, global++) {
  1890. CHECK_BUF(p, p_end, 2);
  1891. global->type = read_uint8(p);
  1892. mutable = read_uint8(p);
  1893. if (mutable >= 2) {
  1894. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1895. return false;
  1896. }
  1897. global->is_mutable = mutable ? true : false;
  1898. /* initialize expression */
  1899. if (!load_init_expr(&p, p_end, &(global->init_expr), global->type,
  1900. error_buf, error_buf_size))
  1901. return false;
  1902. if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
  1903. /**
  1904. * Currently, constant expressions occurring as initializers
  1905. * of globals are further constrained in that contained
  1906. * global.get instructions are
  1907. * only allowed to refer to imported globals.
  1908. */
  1909. uint32 target_global_index = global->init_expr.u.global_index;
  1910. if (target_global_index >= module->import_global_count) {
  1911. set_error_buf(error_buf, error_buf_size, "unknown global");
  1912. return false;
  1913. }
  1914. }
  1915. else if (INIT_EXPR_TYPE_FUNCREF_CONST
  1916. == global->init_expr.init_expr_type) {
  1917. if (!check_function_index(module, global->init_expr.u.ref_index,
  1918. error_buf, error_buf_size)) {
  1919. return false;
  1920. }
  1921. }
  1922. }
  1923. }
  1924. if (p != p_end) {
  1925. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1926. return false;
  1927. }
  1928. LOG_VERBOSE("Load global section success.\n");
  1929. return true;
  1930. fail:
  1931. return false;
  1932. }
  1933. static bool
  1934. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1935. bool is_load_from_file_buf, char *error_buf,
  1936. uint32 error_buf_size)
  1937. {
  1938. const uint8 *p = buf, *p_end = buf_end;
  1939. uint32 export_count, i, j, index;
  1940. uint64 total_size;
  1941. uint32 str_len;
  1942. WASMExport *export;
  1943. const char *name;
  1944. read_leb_uint32(p, p_end, export_count);
  1945. if (export_count) {
  1946. module->export_count = export_count;
  1947. total_size = sizeof(WASMExport) * (uint64)export_count;
  1948. if (!(module->exports =
  1949. loader_malloc(total_size, error_buf, error_buf_size))) {
  1950. return false;
  1951. }
  1952. export = module->exports;
  1953. for (i = 0; i < export_count; i++, export ++) {
  1954. #if WASM_ENABLE_THREAD_MGR == 0
  1955. if (p == p_end) {
  1956. /* export section with inconsistent count:
  1957. n export declared, but less than n given */
  1958. set_error_buf(error_buf, error_buf_size,
  1959. "length out of bounds");
  1960. return false;
  1961. }
  1962. #endif
  1963. read_leb_uint32(p, p_end, str_len);
  1964. CHECK_BUF(p, p_end, str_len);
  1965. for (j = 0; j < i; j++) {
  1966. name = module->exports[j].name;
  1967. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  1968. set_error_buf(error_buf, error_buf_size,
  1969. "duplicate export name");
  1970. return false;
  1971. }
  1972. }
  1973. if (!(export->name = const_str_list_insert(
  1974. p, str_len, module, is_load_from_file_buf, error_buf,
  1975. error_buf_size))) {
  1976. return false;
  1977. }
  1978. p += str_len;
  1979. CHECK_BUF(p, p_end, 1);
  1980. export->kind = read_uint8(p);
  1981. read_leb_uint32(p, p_end, index);
  1982. export->index = index;
  1983. switch (export->kind) {
  1984. /* function index */
  1985. case EXPORT_KIND_FUNC:
  1986. if (index >= module->function_count
  1987. + module->import_function_count) {
  1988. set_error_buf(error_buf, error_buf_size,
  1989. "unknown function");
  1990. return false;
  1991. }
  1992. #if WASM_ENABLE_SIMD != 0
  1993. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1994. /* TODO: check func type, if it has v128 param or result,
  1995. report error */
  1996. #endif
  1997. #endif
  1998. break;
  1999. /* table index */
  2000. case EXPORT_KIND_TABLE:
  2001. if (index
  2002. >= module->table_count + module->import_table_count) {
  2003. set_error_buf(error_buf, error_buf_size,
  2004. "unknown table");
  2005. return false;
  2006. }
  2007. break;
  2008. /* memory index */
  2009. case EXPORT_KIND_MEMORY:
  2010. if (index
  2011. >= module->memory_count + module->import_memory_count) {
  2012. set_error_buf(error_buf, error_buf_size,
  2013. "unknown memory");
  2014. return false;
  2015. }
  2016. break;
  2017. /* global index */
  2018. case EXPORT_KIND_GLOBAL:
  2019. if (index
  2020. >= module->global_count + module->import_global_count) {
  2021. set_error_buf(error_buf, error_buf_size,
  2022. "unknown global");
  2023. return false;
  2024. }
  2025. break;
  2026. default:
  2027. set_error_buf(error_buf, error_buf_size,
  2028. "invalid export kind");
  2029. return false;
  2030. }
  2031. }
  2032. }
  2033. if (p != p_end) {
  2034. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2035. return false;
  2036. }
  2037. LOG_VERBOSE("Load export section success.\n");
  2038. return true;
  2039. fail:
  2040. return false;
  2041. }
  2042. static bool
  2043. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  2044. uint32 error_buf_size)
  2045. {
  2046. #if WASM_ENABLE_REF_TYPES == 0
  2047. if (table_index != 0) {
  2048. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  2049. return false;
  2050. }
  2051. #endif
  2052. if (table_index >= module->import_table_count + module->table_count) {
  2053. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  2054. table_index);
  2055. return false;
  2056. }
  2057. return true;
  2058. }
  2059. static bool
  2060. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2061. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  2062. {
  2063. const uint8 *p = *p_buf, *p_end = buf_end;
  2064. uint32 table_index;
  2065. read_leb_uint32(p, p_end, table_index);
  2066. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  2067. return false;
  2068. }
  2069. *p_table_index = table_index;
  2070. *p_buf = p;
  2071. return true;
  2072. fail:
  2073. return false;
  2074. }
  2075. #if WASM_ENABLE_REF_TYPES != 0
  2076. static bool
  2077. load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type,
  2078. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  2079. {
  2080. const uint8 *p = *p_buf, *p_end = buf_end;
  2081. uint8 elem_type;
  2082. CHECK_BUF(p, p_end, 1);
  2083. elem_type = read_uint8(p);
  2084. if ((elemkind_zero && elem_type != 0)
  2085. || (!elemkind_zero && elem_type != VALUE_TYPE_FUNCREF
  2086. && elem_type != VALUE_TYPE_EXTERNREF)) {
  2087. set_error_buf(error_buf, error_buf_size, "invalid reference type");
  2088. return false;
  2089. }
  2090. if (elemkind_zero)
  2091. *p_elem_type = VALUE_TYPE_FUNCREF;
  2092. else
  2093. *p_elem_type = elem_type;
  2094. *p_buf = p;
  2095. return true;
  2096. fail:
  2097. return false;
  2098. }
  2099. #endif /* WASM_ENABLE_REF_TYPES != 0*/
  2100. static bool
  2101. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  2102. WASMModule *module, WASMTableSeg *table_segment,
  2103. bool use_init_expr, char *error_buf, uint32 error_buf_size)
  2104. {
  2105. const uint8 *p = *p_buf, *p_end = buf_end;
  2106. uint32 function_count, function_index = 0, i;
  2107. uint64 total_size;
  2108. read_leb_uint32(p, p_end, function_count);
  2109. table_segment->function_count = function_count;
  2110. total_size = sizeof(uint32) * (uint64)function_count;
  2111. if (total_size > 0
  2112. && !(table_segment->func_indexes = (uint32 *)loader_malloc(
  2113. total_size, error_buf, error_buf_size))) {
  2114. return false;
  2115. }
  2116. for (i = 0; i < function_count; i++) {
  2117. InitializerExpression init_expr = { 0 };
  2118. #if WASM_ENABLE_REF_TYPES != 0
  2119. if (!use_init_expr) {
  2120. read_leb_uint32(p, p_end, function_index);
  2121. }
  2122. else {
  2123. if (!load_init_expr(&p, p_end, &init_expr, table_segment->elem_type,
  2124. error_buf, error_buf_size))
  2125. return false;
  2126. function_index = init_expr.u.ref_index;
  2127. }
  2128. #else
  2129. read_leb_uint32(p, p_end, function_index);
  2130. #endif
  2131. /* since we are using -1 to indicate ref.null */
  2132. if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
  2133. && !check_function_index(module, function_index, error_buf,
  2134. error_buf_size)) {
  2135. return false;
  2136. }
  2137. table_segment->func_indexes[i] = function_index;
  2138. }
  2139. *p_buf = p;
  2140. return true;
  2141. fail:
  2142. return false;
  2143. }
  2144. static bool
  2145. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  2146. WASMModule *module, char *error_buf,
  2147. uint32 error_buf_size)
  2148. {
  2149. const uint8 *p = buf, *p_end = buf_end;
  2150. uint32 table_segment_count, i;
  2151. uint64 total_size;
  2152. WASMTableSeg *table_segment;
  2153. read_leb_uint32(p, p_end, table_segment_count);
  2154. if (table_segment_count) {
  2155. module->table_seg_count = table_segment_count;
  2156. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  2157. if (!(module->table_segments =
  2158. loader_malloc(total_size, error_buf, error_buf_size))) {
  2159. return false;
  2160. }
  2161. table_segment = module->table_segments;
  2162. for (i = 0; i < table_segment_count; i++, table_segment++) {
  2163. if (p >= p_end) {
  2164. set_error_buf(error_buf, error_buf_size,
  2165. "invalid value type or "
  2166. "invalid elements segment kind");
  2167. return false;
  2168. }
  2169. #if WASM_ENABLE_REF_TYPES != 0
  2170. read_leb_uint32(p, p_end, table_segment->mode);
  2171. /* last three bits */
  2172. table_segment->mode = table_segment->mode & 0x07;
  2173. switch (table_segment->mode) {
  2174. /* elemkind/elemtype + active */
  2175. case 0:
  2176. case 4:
  2177. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  2178. table_segment->table_index = 0;
  2179. if (!check_table_index(module, table_segment->table_index,
  2180. error_buf, error_buf_size))
  2181. return false;
  2182. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2183. VALUE_TYPE_I32, error_buf,
  2184. error_buf_size))
  2185. return false;
  2186. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2187. table_segment->mode == 0 ? false
  2188. : true,
  2189. error_buf, error_buf_size))
  2190. return false;
  2191. break;
  2192. /* elemkind + passive/declarative */
  2193. case 1:
  2194. case 3:
  2195. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2196. true, error_buf, error_buf_size))
  2197. return false;
  2198. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2199. false, error_buf, error_buf_size))
  2200. return false;
  2201. break;
  2202. /* elemkind/elemtype + table_idx + active */
  2203. case 2:
  2204. case 6:
  2205. if (!load_table_index(&p, p_end, module,
  2206. &table_segment->table_index,
  2207. error_buf, error_buf_size))
  2208. return false;
  2209. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2210. VALUE_TYPE_I32, error_buf,
  2211. error_buf_size))
  2212. return false;
  2213. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2214. table_segment->mode == 2 ? true : false,
  2215. error_buf, error_buf_size))
  2216. return false;
  2217. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2218. table_segment->mode == 2 ? false
  2219. : true,
  2220. error_buf, error_buf_size))
  2221. return false;
  2222. break;
  2223. case 5:
  2224. case 7:
  2225. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2226. false, error_buf, error_buf_size))
  2227. return false;
  2228. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2229. true, error_buf, error_buf_size))
  2230. return false;
  2231. break;
  2232. default:
  2233. set_error_buf(error_buf, error_buf_size,
  2234. "unknown element segment kind");
  2235. return false;
  2236. }
  2237. #else
  2238. /*
  2239. * like: 00 41 05 0b 04 00 01 00 01
  2240. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  2241. */
  2242. if (!load_table_index(&p, p_end, module,
  2243. &table_segment->table_index, error_buf,
  2244. error_buf_size))
  2245. return false;
  2246. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2247. VALUE_TYPE_I32, error_buf, error_buf_size))
  2248. return false;
  2249. if (!load_func_index_vec(&p, p_end, module, table_segment, false,
  2250. error_buf, error_buf_size))
  2251. return false;
  2252. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  2253. }
  2254. }
  2255. if (p != p_end) {
  2256. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2257. return false;
  2258. }
  2259. LOG_VERBOSE("Load table segment section success.\n");
  2260. return true;
  2261. fail:
  2262. return false;
  2263. }
  2264. static bool
  2265. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  2266. WASMModule *module, char *error_buf,
  2267. uint32 error_buf_size)
  2268. {
  2269. const uint8 *p = buf, *p_end = buf_end;
  2270. uint32 data_seg_count, i, mem_index, data_seg_len;
  2271. uint64 total_size;
  2272. WASMDataSeg *dataseg;
  2273. InitializerExpression init_expr;
  2274. #if WASM_ENABLE_BULK_MEMORY != 0
  2275. bool is_passive = false;
  2276. uint32 mem_flag;
  2277. #endif
  2278. read_leb_uint32(p, p_end, data_seg_count);
  2279. #if WASM_ENABLE_BULK_MEMORY != 0
  2280. if ((module->data_seg_count1 != 0)
  2281. && (data_seg_count != module->data_seg_count1)) {
  2282. set_error_buf(error_buf, error_buf_size,
  2283. "data count and data section have inconsistent lengths");
  2284. return false;
  2285. }
  2286. #endif
  2287. if (data_seg_count) {
  2288. module->data_seg_count = data_seg_count;
  2289. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  2290. if (!(module->data_segments =
  2291. loader_malloc(total_size, error_buf, error_buf_size))) {
  2292. return false;
  2293. }
  2294. for (i = 0; i < data_seg_count; i++) {
  2295. read_leb_uint32(p, p_end, mem_index);
  2296. #if WASM_ENABLE_BULK_MEMORY != 0
  2297. is_passive = false;
  2298. mem_flag = mem_index & 0x03;
  2299. switch (mem_flag) {
  2300. case 0x01:
  2301. is_passive = true;
  2302. break;
  2303. case 0x00:
  2304. /* no memory index, treat index as 0 */
  2305. mem_index = 0;
  2306. goto check_mem_index;
  2307. case 0x02:
  2308. /* read following memory index */
  2309. read_leb_uint32(p, p_end, mem_index);
  2310. check_mem_index:
  2311. if (mem_index
  2312. >= module->import_memory_count + module->memory_count) {
  2313. set_error_buf_v(error_buf, error_buf_size,
  2314. "unknown memory %d", mem_index);
  2315. return false;
  2316. }
  2317. break;
  2318. case 0x03:
  2319. default:
  2320. set_error_buf(error_buf, error_buf_size, "unknown memory");
  2321. return false;
  2322. break;
  2323. }
  2324. #else
  2325. if (mem_index
  2326. >= module->import_memory_count + module->memory_count) {
  2327. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  2328. mem_index);
  2329. return false;
  2330. }
  2331. #endif /* WASM_ENABLE_BULK_MEMORY */
  2332. #if WASM_ENABLE_BULK_MEMORY != 0
  2333. if (!is_passive)
  2334. #endif
  2335. if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32,
  2336. error_buf, error_buf_size))
  2337. return false;
  2338. read_leb_uint32(p, p_end, data_seg_len);
  2339. if (!(dataseg = module->data_segments[i] = loader_malloc(
  2340. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  2341. return false;
  2342. }
  2343. #if WASM_ENABLE_BULK_MEMORY != 0
  2344. dataseg->is_passive = is_passive;
  2345. if (!is_passive)
  2346. #endif
  2347. {
  2348. bh_memcpy_s(&dataseg->base_offset,
  2349. sizeof(InitializerExpression), &init_expr,
  2350. sizeof(InitializerExpression));
  2351. dataseg->memory_index = mem_index;
  2352. }
  2353. dataseg->data_length = data_seg_len;
  2354. CHECK_BUF(p, p_end, data_seg_len);
  2355. dataseg->data = (uint8 *)p;
  2356. p += data_seg_len;
  2357. }
  2358. }
  2359. if (p != p_end) {
  2360. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2361. return false;
  2362. }
  2363. LOG_VERBOSE("Load data segment section success.\n");
  2364. return true;
  2365. fail:
  2366. return false;
  2367. }
  2368. #if WASM_ENABLE_BULK_MEMORY != 0
  2369. static bool
  2370. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  2371. WASMModule *module, char *error_buf,
  2372. uint32 error_buf_size)
  2373. {
  2374. const uint8 *p = buf, *p_end = buf_end;
  2375. uint32 data_seg_count1 = 0;
  2376. read_leb_uint32(p, p_end, data_seg_count1);
  2377. module->data_seg_count1 = data_seg_count1;
  2378. if (p != p_end) {
  2379. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2380. return false;
  2381. }
  2382. LOG_VERBOSE("Load datacount section success.\n");
  2383. return true;
  2384. fail:
  2385. return false;
  2386. }
  2387. #endif
  2388. static bool
  2389. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  2390. const uint8 *buf_func_end, WASMModule *module,
  2391. char *error_buf, uint32 error_buf_size)
  2392. {
  2393. const uint8 *p = buf, *p_end = buf_end;
  2394. const uint8 *p_func = buf_func;
  2395. uint32 func_count = 0, code_count;
  2396. /* code has been loaded in function section, so pass it here, just check
  2397. * whether function and code section have inconsistent lengths */
  2398. read_leb_uint32(p, p_end, code_count);
  2399. if (buf_func)
  2400. read_leb_uint32(p_func, buf_func_end, func_count);
  2401. if (func_count != code_count) {
  2402. set_error_buf(error_buf, error_buf_size,
  2403. "function and code section have inconsistent lengths");
  2404. return false;
  2405. }
  2406. LOG_VERBOSE("Load code segment section success.\n");
  2407. return true;
  2408. fail:
  2409. return false;
  2410. }
  2411. static bool
  2412. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2413. char *error_buf, uint32 error_buf_size)
  2414. {
  2415. const uint8 *p = buf, *p_end = buf_end;
  2416. WASMType *type;
  2417. uint32 start_function;
  2418. read_leb_uint32(p, p_end, start_function);
  2419. if (start_function
  2420. >= module->function_count + module->import_function_count) {
  2421. set_error_buf(error_buf, error_buf_size, "unknown function");
  2422. return false;
  2423. }
  2424. if (start_function < module->import_function_count)
  2425. type = module->import_functions[start_function].u.function.func_type;
  2426. else
  2427. type = module->functions[start_function - module->import_function_count]
  2428. ->func_type;
  2429. if (type->param_count != 0 || type->result_count != 0) {
  2430. set_error_buf(error_buf, error_buf_size, "invalid start function");
  2431. return false;
  2432. }
  2433. module->start_function = start_function;
  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 start section success.\n");
  2439. return true;
  2440. fail:
  2441. return false;
  2442. }
  2443. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2444. static bool
  2445. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2446. bool is_load_from_file_buf, char *error_buf,
  2447. uint32 error_buf_size)
  2448. {
  2449. const uint8 *p = buf, *p_end = buf_end;
  2450. uint32 name_type, subsection_size;
  2451. uint32 previous_name_type = 0;
  2452. uint32 num_func_name;
  2453. uint32 func_index;
  2454. uint32 previous_func_index = ~0U;
  2455. uint32 func_name_len;
  2456. uint32 name_index;
  2457. int i = 0;
  2458. if (p >= p_end) {
  2459. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2460. return false;
  2461. }
  2462. while (p < p_end) {
  2463. read_leb_uint32(p, p_end, name_type);
  2464. if (i != 0) {
  2465. if (name_type == previous_name_type) {
  2466. set_error_buf(error_buf, error_buf_size,
  2467. "duplicate sub-section");
  2468. return false;
  2469. }
  2470. if (name_type < previous_name_type) {
  2471. set_error_buf(error_buf, error_buf_size,
  2472. "out-of-order sub-section");
  2473. return false;
  2474. }
  2475. }
  2476. previous_name_type = name_type;
  2477. read_leb_uint32(p, p_end, subsection_size);
  2478. CHECK_BUF(p, p_end, subsection_size);
  2479. switch (name_type) {
  2480. case SUB_SECTION_TYPE_FUNC:
  2481. if (subsection_size) {
  2482. read_leb_uint32(p, p_end, num_func_name);
  2483. for (name_index = 0; name_index < num_func_name;
  2484. name_index++) {
  2485. read_leb_uint32(p, p_end, func_index);
  2486. if (func_index == previous_func_index) {
  2487. set_error_buf(error_buf, error_buf_size,
  2488. "duplicate function name");
  2489. return false;
  2490. }
  2491. if (func_index < previous_func_index
  2492. && previous_func_index != ~0U) {
  2493. set_error_buf(error_buf, error_buf_size,
  2494. "out-of-order function index ");
  2495. return false;
  2496. }
  2497. previous_func_index = func_index;
  2498. read_leb_uint32(p, p_end, func_name_len);
  2499. CHECK_BUF(p, p_end, func_name_len);
  2500. /* Skip the import functions */
  2501. if (func_index >= module->import_count) {
  2502. func_index -= module->import_count;
  2503. if (func_index >= module->function_count) {
  2504. set_error_buf(error_buf, error_buf_size,
  2505. "out-of-range function index");
  2506. return false;
  2507. }
  2508. if (!(module->functions[func_index]->field_name =
  2509. const_str_list_insert(
  2510. p, func_name_len, module,
  2511. is_load_from_file_buf, error_buf,
  2512. error_buf_size))) {
  2513. return false;
  2514. }
  2515. }
  2516. p += func_name_len;
  2517. }
  2518. }
  2519. break;
  2520. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  2521. */
  2522. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  2523. default:
  2524. p = p + subsection_size;
  2525. break;
  2526. }
  2527. i++;
  2528. }
  2529. return true;
  2530. fail:
  2531. return false;
  2532. }
  2533. #endif
  2534. static bool
  2535. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2536. bool is_load_from_file_buf, char *error_buf,
  2537. uint32 error_buf_size)
  2538. {
  2539. const uint8 *p = buf, *p_end = buf_end;
  2540. char section_name[32];
  2541. uint32 name_len, buffer_len;
  2542. if (p >= p_end) {
  2543. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2544. return false;
  2545. }
  2546. read_leb_uint32(p, p_end, name_len);
  2547. if (name_len == 0 || p + name_len > p_end) {
  2548. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2549. return false;
  2550. }
  2551. if (!check_utf8_str(p, name_len)) {
  2552. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  2553. return false;
  2554. }
  2555. buffer_len = sizeof(section_name);
  2556. memset(section_name, 0, buffer_len);
  2557. if (name_len < buffer_len) {
  2558. bh_memcpy_s(section_name, buffer_len, p, name_len);
  2559. }
  2560. else {
  2561. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  2562. memset(section_name + buffer_len - 4, '.', 3);
  2563. }
  2564. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2565. if (memcmp(p, "name", 4) == 0) {
  2566. module->name_section_buf = buf;
  2567. module->name_section_buf_end = buf_end;
  2568. p += name_len;
  2569. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  2570. error_buf_size);
  2571. LOG_VERBOSE("Load custom name section success.");
  2572. return true;
  2573. }
  2574. #endif
  2575. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  2576. {
  2577. WASMCustomSection *section =
  2578. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  2579. if (!section) {
  2580. return false;
  2581. }
  2582. section->name_addr = (char *)p;
  2583. section->name_len = name_len;
  2584. section->content_addr = (uint8 *)(p + name_len);
  2585. section->content_len = p_end - p - name_len;
  2586. section->next = module->custom_section_list;
  2587. module->custom_section_list = section;
  2588. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  2589. return true;
  2590. }
  2591. #endif
  2592. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  2593. return true;
  2594. fail:
  2595. return false;
  2596. }
  2597. #if WASM_ENABLE_FAST_JIT != 0
  2598. static void
  2599. calculate_global_data_offset(WASMModule *module)
  2600. {
  2601. uint32 i, data_offset;
  2602. data_offset = 0;
  2603. for (i = 0; i < module->import_global_count; i++) {
  2604. WASMGlobalImport *import_global =
  2605. &((module->import_globals + i)->u.global);
  2606. import_global->data_offset = data_offset;
  2607. data_offset += wasm_value_type_size(import_global->type);
  2608. }
  2609. for (i = 0; i < module->global_count; i++) {
  2610. WASMGlobal *global = module->globals + i;
  2611. global->data_offset = data_offset;
  2612. data_offset += wasm_value_type_size(global->type);
  2613. }
  2614. }
  2615. #endif
  2616. static bool
  2617. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  2618. uint32 cur_func_idx, char *error_buf,
  2619. uint32 error_buf_size);
  2620. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  2621. void **
  2622. wasm_interp_get_handle_table();
  2623. static void **handle_table;
  2624. #endif
  2625. static bool
  2626. load_from_sections(WASMModule *module, WASMSection *sections,
  2627. bool is_load_from_file_buf, char *error_buf,
  2628. uint32 error_buf_size)
  2629. {
  2630. WASMExport *export;
  2631. WASMSection *section = sections;
  2632. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  2633. *buf_func = NULL, *buf_func_end = NULL;
  2634. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  2635. WASMGlobal *aux_stack_top_global = NULL, *global;
  2636. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  2637. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  2638. uint32 aux_data_end_global_index = (uint32)-1;
  2639. uint32 aux_heap_base_global_index = (uint32)-1;
  2640. WASMType *func_type;
  2641. /* Find code and function sections if have */
  2642. while (section) {
  2643. if (section->section_type == SECTION_TYPE_CODE) {
  2644. buf_code = section->section_body;
  2645. buf_code_end = buf_code + section->section_body_size;
  2646. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  2647. module->buf_code = (uint8 *)buf_code;
  2648. module->buf_code_size = section->section_body_size;
  2649. #endif
  2650. }
  2651. else if (section->section_type == SECTION_TYPE_FUNC) {
  2652. buf_func = section->section_body;
  2653. buf_func_end = buf_func + section->section_body_size;
  2654. }
  2655. section = section->next;
  2656. }
  2657. section = sections;
  2658. while (section) {
  2659. buf = section->section_body;
  2660. buf_end = buf + section->section_body_size;
  2661. switch (section->section_type) {
  2662. case SECTION_TYPE_USER:
  2663. /* unsupported user section, ignore it. */
  2664. if (!load_user_section(buf, buf_end, module,
  2665. is_load_from_file_buf, error_buf,
  2666. error_buf_size))
  2667. return false;
  2668. break;
  2669. case SECTION_TYPE_TYPE:
  2670. if (!load_type_section(buf, buf_end, module, error_buf,
  2671. error_buf_size))
  2672. return false;
  2673. break;
  2674. case SECTION_TYPE_IMPORT:
  2675. if (!load_import_section(buf, buf_end, module,
  2676. is_load_from_file_buf, error_buf,
  2677. error_buf_size))
  2678. return false;
  2679. break;
  2680. case SECTION_TYPE_FUNC:
  2681. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  2682. module, error_buf, error_buf_size))
  2683. return false;
  2684. break;
  2685. case SECTION_TYPE_TABLE:
  2686. if (!load_table_section(buf, buf_end, module, error_buf,
  2687. error_buf_size))
  2688. return false;
  2689. break;
  2690. case SECTION_TYPE_MEMORY:
  2691. if (!load_memory_section(buf, buf_end, module, error_buf,
  2692. error_buf_size))
  2693. return false;
  2694. break;
  2695. case SECTION_TYPE_GLOBAL:
  2696. if (!load_global_section(buf, buf_end, module, error_buf,
  2697. error_buf_size))
  2698. return false;
  2699. break;
  2700. case SECTION_TYPE_EXPORT:
  2701. if (!load_export_section(buf, buf_end, module,
  2702. is_load_from_file_buf, error_buf,
  2703. error_buf_size))
  2704. return false;
  2705. break;
  2706. case SECTION_TYPE_START:
  2707. if (!load_start_section(buf, buf_end, module, error_buf,
  2708. error_buf_size))
  2709. return false;
  2710. break;
  2711. case SECTION_TYPE_ELEM:
  2712. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  2713. error_buf_size))
  2714. return false;
  2715. break;
  2716. case SECTION_TYPE_CODE:
  2717. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  2718. module, error_buf, error_buf_size))
  2719. return false;
  2720. break;
  2721. case SECTION_TYPE_DATA:
  2722. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  2723. error_buf_size))
  2724. return false;
  2725. break;
  2726. #if WASM_ENABLE_BULK_MEMORY != 0
  2727. case SECTION_TYPE_DATACOUNT:
  2728. if (!load_datacount_section(buf, buf_end, module, error_buf,
  2729. error_buf_size))
  2730. return false;
  2731. break;
  2732. #endif
  2733. default:
  2734. set_error_buf(error_buf, error_buf_size, "invalid section id");
  2735. return false;
  2736. }
  2737. section = section->next;
  2738. }
  2739. module->aux_data_end_global_index = (uint32)-1;
  2740. module->aux_heap_base_global_index = (uint32)-1;
  2741. module->aux_stack_top_global_index = (uint32)-1;
  2742. /* Resolve auxiliary data/stack/heap info and reset memory info */
  2743. export = module->exports;
  2744. for (i = 0; i < module->export_count; i++, export ++) {
  2745. if (export->kind == EXPORT_KIND_GLOBAL) {
  2746. if (!strcmp(export->name, "__heap_base")) {
  2747. global_index = export->index - module->import_global_count;
  2748. global = module->globals + global_index;
  2749. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  2750. && global->init_expr.init_expr_type
  2751. == INIT_EXPR_TYPE_I32_CONST) {
  2752. aux_heap_base_global = global;
  2753. aux_heap_base = global->init_expr.u.i32;
  2754. aux_heap_base_global_index = export->index;
  2755. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  2756. aux_heap_base);
  2757. }
  2758. }
  2759. else if (!strcmp(export->name, "__data_end")) {
  2760. global_index = export->index - module->import_global_count;
  2761. global = module->globals + global_index;
  2762. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  2763. && global->init_expr.init_expr_type
  2764. == INIT_EXPR_TYPE_I32_CONST) {
  2765. aux_data_end_global = global;
  2766. aux_data_end = global->init_expr.u.i32;
  2767. aux_data_end_global_index = export->index;
  2768. LOG_VERBOSE("Found aux __data_end global, value: %d",
  2769. aux_data_end);
  2770. aux_data_end = align_uint(aux_data_end, 16);
  2771. }
  2772. }
  2773. /* For module compiled with -pthread option, the global is:
  2774. [0] stack_top <-- 0
  2775. [1] tls_pointer
  2776. [2] tls_size
  2777. [3] data_end <-- 3
  2778. [4] global_base
  2779. [5] heap_base <-- 5
  2780. [6] dso_handle
  2781. For module compiled without -pthread option:
  2782. [0] stack_top <-- 0
  2783. [1] data_end <-- 1
  2784. [2] global_base
  2785. [3] heap_base <-- 3
  2786. [4] dso_handle
  2787. */
  2788. if (aux_data_end_global && aux_heap_base_global
  2789. && aux_data_end <= aux_heap_base) {
  2790. module->aux_data_end_global_index = aux_data_end_global_index;
  2791. module->aux_data_end = aux_data_end;
  2792. module->aux_heap_base_global_index = aux_heap_base_global_index;
  2793. module->aux_heap_base = aux_heap_base;
  2794. /* Resolve aux stack top global */
  2795. for (global_index = 0; global_index < module->global_count;
  2796. global_index++) {
  2797. global = module->globals + global_index;
  2798. if (global->is_mutable /* heap_base and data_end is
  2799. not mutable */
  2800. && global->type == VALUE_TYPE_I32
  2801. && global->init_expr.init_expr_type
  2802. == INIT_EXPR_TYPE_I32_CONST
  2803. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  2804. aux_stack_top_global = global;
  2805. aux_stack_top = (uint32)global->init_expr.u.i32;
  2806. module->aux_stack_top_global_index =
  2807. module->import_global_count + global_index;
  2808. module->aux_stack_bottom = aux_stack_top;
  2809. module->aux_stack_size =
  2810. aux_stack_top > aux_data_end
  2811. ? aux_stack_top - aux_data_end
  2812. : aux_stack_top;
  2813. LOG_VERBOSE("Found aux stack top global, value: %d, "
  2814. "global index: %d, stack size: %d",
  2815. aux_stack_top, global_index,
  2816. module->aux_stack_size);
  2817. break;
  2818. }
  2819. }
  2820. if (!aux_stack_top_global) {
  2821. /* Auxiliary stack global isn't found, it must be unused
  2822. in the wasm app, as if it is used, the global must be
  2823. defined. Here we set it to __heap_base global and set
  2824. its size to 0. */
  2825. aux_stack_top_global = aux_heap_base_global;
  2826. aux_stack_top = aux_heap_base;
  2827. module->aux_stack_top_global_index =
  2828. module->aux_heap_base_global_index;
  2829. module->aux_stack_bottom = aux_stack_top;
  2830. module->aux_stack_size = 0;
  2831. }
  2832. break;
  2833. }
  2834. }
  2835. }
  2836. module->malloc_function = (uint32)-1;
  2837. module->free_function = (uint32)-1;
  2838. module->retain_function = (uint32)-1;
  2839. /* Resolve malloc/free function exported by wasm module */
  2840. export = module->exports;
  2841. for (i = 0; i < module->export_count; i++, export ++) {
  2842. if (export->kind == EXPORT_KIND_FUNC) {
  2843. if (!strcmp(export->name, "malloc")
  2844. && export->index >= module->import_function_count) {
  2845. func_index = export->index - module->import_function_count;
  2846. func_type = module->functions[func_index]->func_type;
  2847. if (func_type->param_count == 1 && func_type->result_count == 1
  2848. && func_type->types[0] == VALUE_TYPE_I32
  2849. && func_type->types[1] == VALUE_TYPE_I32) {
  2850. bh_assert(module->malloc_function == (uint32)-1);
  2851. module->malloc_function = export->index;
  2852. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2853. export->name, export->index);
  2854. }
  2855. }
  2856. else if (!strcmp(export->name, "__new")
  2857. && export->index >= module->import_function_count) {
  2858. /* __new && __pin for AssemblyScript */
  2859. func_index = export->index - module->import_function_count;
  2860. func_type = module->functions[func_index]->func_type;
  2861. if (func_type->param_count == 2 && func_type->result_count == 1
  2862. && func_type->types[0] == VALUE_TYPE_I32
  2863. && func_type->types[1] == VALUE_TYPE_I32
  2864. && func_type->types[2] == VALUE_TYPE_I32) {
  2865. uint32 j;
  2866. WASMExport *export_tmp;
  2867. bh_assert(module->malloc_function == (uint32)-1);
  2868. module->malloc_function = export->index;
  2869. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2870. export->name, export->index);
  2871. /* resolve retain function.
  2872. If not found, reset malloc function index */
  2873. export_tmp = module->exports;
  2874. for (j = 0; j < module->export_count; j++, export_tmp++) {
  2875. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  2876. && (!strcmp(export_tmp->name, "__retain")
  2877. || (!strcmp(export_tmp->name, "__pin")))
  2878. && (export_tmp->index
  2879. >= module->import_function_count)) {
  2880. func_index = export_tmp->index
  2881. - module->import_function_count;
  2882. func_type =
  2883. module->functions[func_index]->func_type;
  2884. if (func_type->param_count == 1
  2885. && func_type->result_count == 1
  2886. && func_type->types[0] == VALUE_TYPE_I32
  2887. && func_type->types[1] == VALUE_TYPE_I32) {
  2888. bh_assert(module->retain_function
  2889. == (uint32)-1);
  2890. module->retain_function = export_tmp->index;
  2891. LOG_VERBOSE("Found retain function, name: %s, "
  2892. "index: %u",
  2893. export_tmp->name,
  2894. export_tmp->index);
  2895. break;
  2896. }
  2897. }
  2898. }
  2899. if (j == module->export_count) {
  2900. module->malloc_function = (uint32)-1;
  2901. LOG_VERBOSE("Can't find retain function,"
  2902. "reset malloc function index to -1");
  2903. }
  2904. }
  2905. }
  2906. else if (((!strcmp(export->name, "free"))
  2907. || (!strcmp(export->name, "__release"))
  2908. || (!strcmp(export->name, "__unpin")))
  2909. && export->index >= module->import_function_count) {
  2910. func_index = export->index - module->import_function_count;
  2911. func_type = module->functions[func_index]->func_type;
  2912. if (func_type->param_count == 1 && func_type->result_count == 0
  2913. && func_type->types[0] == VALUE_TYPE_I32) {
  2914. bh_assert(module->free_function == (uint32)-1);
  2915. module->free_function = export->index;
  2916. LOG_VERBOSE("Found free function, name: %s, index: %u",
  2917. export->name, export->index);
  2918. }
  2919. }
  2920. }
  2921. }
  2922. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  2923. handle_table = wasm_interp_get_handle_table();
  2924. #endif
  2925. for (i = 0; i < module->function_count; i++) {
  2926. WASMFunction *func = module->functions[i];
  2927. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  2928. error_buf_size)) {
  2929. return false;
  2930. }
  2931. }
  2932. if (!module->possible_memory_grow) {
  2933. WASMMemoryImport *memory_import;
  2934. WASMMemory *memory;
  2935. if (aux_data_end_global && aux_heap_base_global
  2936. && aux_stack_top_global) {
  2937. uint64 init_memory_size;
  2938. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  2939. if (module->import_memory_count) {
  2940. memory_import = &module->import_memories[0].u.memory;
  2941. init_memory_size = (uint64)memory_import->num_bytes_per_page
  2942. * memory_import->init_page_count;
  2943. if (shrunk_memory_size <= init_memory_size) {
  2944. /* Reset memory info to decrease memory usage */
  2945. memory_import->num_bytes_per_page = shrunk_memory_size;
  2946. memory_import->init_page_count = 1;
  2947. LOG_VERBOSE("Shrink import memory size to %d",
  2948. shrunk_memory_size);
  2949. }
  2950. }
  2951. if (module->memory_count) {
  2952. memory = &module->memories[0];
  2953. init_memory_size = (uint64)memory->num_bytes_per_page
  2954. * memory->init_page_count;
  2955. if (shrunk_memory_size <= init_memory_size) {
  2956. /* Reset memory info to decrease memory usage */
  2957. memory->num_bytes_per_page = shrunk_memory_size;
  2958. memory->init_page_count = 1;
  2959. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  2960. }
  2961. }
  2962. }
  2963. #if WASM_ENABLE_MULTI_MODULE == 0
  2964. if (module->import_memory_count) {
  2965. memory_import = &module->import_memories[0].u.memory;
  2966. /* Memory init page count cannot be larger than 65536, we don't
  2967. check integer overflow again. */
  2968. memory_import->num_bytes_per_page *= memory_import->init_page_count;
  2969. memory_import->init_page_count = memory_import->max_page_count = 1;
  2970. }
  2971. if (module->memory_count) {
  2972. /* Memory init page count cannot be larger than 65536, we don't
  2973. check integer overflow again. */
  2974. memory = &module->memories[0];
  2975. memory->num_bytes_per_page *= memory->init_page_count;
  2976. memory->init_page_count = memory->max_page_count = 1;
  2977. }
  2978. #endif
  2979. }
  2980. #if WASM_ENABLE_FAST_JIT != 0
  2981. calculate_global_data_offset(module);
  2982. if (module->function_count
  2983. && !(module->fast_jit_func_ptrs =
  2984. loader_malloc(sizeof(void *) * module->function_count,
  2985. error_buf, error_buf_size))) {
  2986. return false;
  2987. }
  2988. if (!jit_compiler_compile_all(module)) {
  2989. set_error_buf(error_buf, error_buf_size, "fast jit compilation failed");
  2990. return false;
  2991. }
  2992. #endif
  2993. #if WASM_ENABLE_MEMORY_TRACING != 0
  2994. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  2995. #endif
  2996. return true;
  2997. }
  2998. static WASMModule *
  2999. create_module(char *error_buf, uint32 error_buf_size)
  3000. {
  3001. WASMModule *module =
  3002. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  3003. #if WASM_ENABLE_FAST_INTERP == 0
  3004. bh_list_status ret;
  3005. #endif
  3006. if (!module) {
  3007. return NULL;
  3008. }
  3009. module->module_type = Wasm_Module_Bytecode;
  3010. /* Set start_function to -1, means no start function */
  3011. module->start_function = (uint32)-1;
  3012. #if WASM_ENABLE_FAST_INTERP == 0
  3013. module->br_table_cache_list = &module->br_table_cache_list_head;
  3014. ret = bh_list_init(module->br_table_cache_list);
  3015. bh_assert(ret == BH_LIST_SUCCESS);
  3016. (void)ret;
  3017. #endif
  3018. #if WASM_ENABLE_MULTI_MODULE != 0
  3019. module->import_module_list = &module->import_module_list_head;
  3020. #endif
  3021. #if WASM_ENABLE_DEBUG_INTERP != 0
  3022. bh_list_init(&module->fast_opcode_list);
  3023. if (os_mutex_init(&module->ref_count_lock) != 0) {
  3024. wasm_runtime_free(module);
  3025. return NULL;
  3026. }
  3027. #endif
  3028. return module;
  3029. }
  3030. #if WASM_ENABLE_DEBUG_INTERP != 0
  3031. static bool
  3032. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  3033. uint32 error_buf_size)
  3034. {
  3035. WASMFastOPCodeNode *fast_op =
  3036. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  3037. if (fast_op) {
  3038. fast_op->offset = pos - module->load_addr;
  3039. fast_op->orig_op = orig_op;
  3040. bh_list_insert(&module->fast_opcode_list, fast_op);
  3041. }
  3042. return fast_op ? true : false;
  3043. }
  3044. #endif
  3045. WASMModule *
  3046. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  3047. uint32 error_buf_size)
  3048. {
  3049. WASMModule *module = create_module(error_buf, error_buf_size);
  3050. if (!module)
  3051. return NULL;
  3052. if (!load_from_sections(module, section_list, false, error_buf,
  3053. error_buf_size)) {
  3054. wasm_loader_unload(module);
  3055. return NULL;
  3056. }
  3057. LOG_VERBOSE("Load module from sections success.\n");
  3058. return module;
  3059. }
  3060. static void
  3061. destroy_sections(WASMSection *section_list)
  3062. {
  3063. WASMSection *section = section_list, *next;
  3064. while (section) {
  3065. next = section->next;
  3066. wasm_runtime_free(section);
  3067. section = next;
  3068. }
  3069. }
  3070. /* clang-format off */
  3071. static uint8 section_ids[] = {
  3072. SECTION_TYPE_USER,
  3073. SECTION_TYPE_TYPE,
  3074. SECTION_TYPE_IMPORT,
  3075. SECTION_TYPE_FUNC,
  3076. SECTION_TYPE_TABLE,
  3077. SECTION_TYPE_MEMORY,
  3078. SECTION_TYPE_GLOBAL,
  3079. SECTION_TYPE_EXPORT,
  3080. SECTION_TYPE_START,
  3081. SECTION_TYPE_ELEM,
  3082. #if WASM_ENABLE_BULK_MEMORY != 0
  3083. SECTION_TYPE_DATACOUNT,
  3084. #endif
  3085. SECTION_TYPE_CODE,
  3086. SECTION_TYPE_DATA
  3087. };
  3088. /* clang-format on */
  3089. static uint8
  3090. get_section_index(uint8 section_type)
  3091. {
  3092. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  3093. for (uint8 i = 0; i < max_id; i++) {
  3094. if (section_type == section_ids[i])
  3095. return i;
  3096. }
  3097. return (uint8)-1;
  3098. }
  3099. static bool
  3100. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  3101. char *error_buf, uint32 error_buf_size)
  3102. {
  3103. WASMSection *section_list_end = NULL, *section;
  3104. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  3105. uint8 section_type, section_index, last_section_index = (uint8)-1;
  3106. uint32 section_size;
  3107. bh_assert(!*p_section_list);
  3108. p += 8;
  3109. while (p < p_end) {
  3110. CHECK_BUF(p, p_end, 1);
  3111. section_type = read_uint8(p);
  3112. section_index = get_section_index(section_type);
  3113. if (section_index != (uint8)-1) {
  3114. if (section_type != SECTION_TYPE_USER) {
  3115. /* Custom sections may be inserted at any place,
  3116. while other sections must occur at most once
  3117. and in prescribed order. */
  3118. if (last_section_index != (uint8)-1
  3119. && (section_index <= last_section_index)) {
  3120. set_error_buf(error_buf, error_buf_size,
  3121. "unexpected content after last section or "
  3122. "junk after last section");
  3123. return false;
  3124. }
  3125. last_section_index = section_index;
  3126. }
  3127. read_leb_uint32(p, p_end, section_size);
  3128. CHECK_BUF1(p, p_end, section_size);
  3129. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  3130. error_buf_size))) {
  3131. return false;
  3132. }
  3133. section->section_type = section_type;
  3134. section->section_body = (uint8 *)p;
  3135. section->section_body_size = section_size;
  3136. if (!section_list_end)
  3137. *p_section_list = section_list_end = section;
  3138. else {
  3139. section_list_end->next = section;
  3140. section_list_end = section;
  3141. }
  3142. p += section_size;
  3143. }
  3144. else {
  3145. set_error_buf(error_buf, error_buf_size, "invalid section id");
  3146. return false;
  3147. }
  3148. }
  3149. return true;
  3150. fail:
  3151. return false;
  3152. }
  3153. static void
  3154. exchange32(uint8 *p_data)
  3155. {
  3156. uint8 value = *p_data;
  3157. *p_data = *(p_data + 3);
  3158. *(p_data + 3) = value;
  3159. value = *(p_data + 1);
  3160. *(p_data + 1) = *(p_data + 2);
  3161. *(p_data + 2) = value;
  3162. }
  3163. static union {
  3164. int a;
  3165. char b;
  3166. } __ue = { .a = 1 };
  3167. #define is_little_endian() (__ue.b == 1)
  3168. static bool
  3169. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  3170. uint32 error_buf_size)
  3171. {
  3172. const uint8 *buf_end = buf + size;
  3173. const uint8 *p = buf, *p_end = buf_end;
  3174. uint32 magic_number, version;
  3175. WASMSection *section_list = NULL;
  3176. CHECK_BUF1(p, p_end, sizeof(uint32));
  3177. magic_number = read_uint32(p);
  3178. if (!is_little_endian())
  3179. exchange32((uint8 *)&magic_number);
  3180. if (magic_number != WASM_MAGIC_NUMBER) {
  3181. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  3182. return false;
  3183. }
  3184. CHECK_BUF1(p, p_end, sizeof(uint32));
  3185. version = read_uint32(p);
  3186. if (!is_little_endian())
  3187. exchange32((uint8 *)&version);
  3188. if (version != WASM_CURRENT_VERSION) {
  3189. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  3190. return false;
  3191. }
  3192. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  3193. || !load_from_sections(module, section_list, true, error_buf,
  3194. error_buf_size)) {
  3195. destroy_sections(section_list);
  3196. return false;
  3197. }
  3198. destroy_sections(section_list);
  3199. return true;
  3200. fail:
  3201. return false;
  3202. }
  3203. #if (WASM_ENABLE_MULTI_MODULE != 0) && (WASM_ENABLE_LIBC_WASI != 0)
  3204. /**
  3205. * refer to
  3206. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  3207. */
  3208. static bool
  3209. check_wasi_abi_compatibility(const WASMModule *module, bool main_module,
  3210. char *error_buf, uint32 error_buf_size)
  3211. {
  3212. /**
  3213. * need to handle:
  3214. * - non-wasi compatiable modules
  3215. * - a fake wasi compatiable module
  3216. * - a command acts as a main_module
  3217. * - a command acts as a sub_module
  3218. * - a reactor acts as a main_module
  3219. * - a reactor acts as a sub_module
  3220. *
  3221. * be careful with:
  3222. * wasi compatiable modules(command/reactor) which don't import any wasi
  3223. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  3224. * reactor can depend on nothing. At the same time, each has its own entry
  3225. * point.
  3226. *
  3227. * observations:
  3228. * - clang always injects `_start` into a command
  3229. * - clang always injects `_initialize` into a reactor
  3230. * - `iwasm -f` allows to run a function in the reactor
  3231. *
  3232. * strong assumptions:
  3233. * - no one will define either `_start` or `_initialize` on purpose
  3234. * - `_start` should always be `void _start(void)`
  3235. * - `_initialize` should always be `void _initialize(void)`
  3236. */
  3237. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  3238. /* (func (export "_start") (...) */
  3239. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  3240. error_buf, error_buf_size);
  3241. if (start) {
  3242. WASMType *func_type =
  3243. module->functions[start->index - module->import_function_count]
  3244. ->func_type;
  3245. if (func_type->param_count || func_type->result_count) {
  3246. set_error_buf(error_buf, error_buf_size,
  3247. "the signature of builtin _start function is wrong");
  3248. return false;
  3249. }
  3250. }
  3251. /* (func (export "_initialize") (...) */
  3252. initialize = wasm_loader_find_export(
  3253. module, "", "_initialize", EXPORT_KIND_FUNC, error_buf, error_buf_size);
  3254. if (initialize) {
  3255. WASMType *func_type =
  3256. module->functions[initialize->index - module->import_function_count]
  3257. ->func_type;
  3258. if (func_type->param_count || func_type->result_count) {
  3259. set_error_buf(
  3260. error_buf, error_buf_size,
  3261. "the signature of builtin _initialize function is wrong");
  3262. return false;
  3263. }
  3264. }
  3265. /* filter out non-wasi compatiable modules */
  3266. if (!module->import_wasi_api && !start && !initialize) {
  3267. return true;
  3268. }
  3269. /* should have one at least */
  3270. if (module->import_wasi_api && !start && !initialize) {
  3271. set_error_buf(
  3272. error_buf, error_buf_size,
  3273. "a module with WASI apis must be either a command or a reactor");
  3274. return false;
  3275. }
  3276. /*
  3277. * there is at least one of `_start` and `_initialize` in below cases.
  3278. * according to the assumption, they should be all wasi compatiable
  3279. */
  3280. /* always can not have both at the same time */
  3281. if (start && initialize) {
  3282. set_error_buf(
  3283. error_buf, error_buf_size,
  3284. "neither a command nor a reactor can both have _start function "
  3285. "and _initialize function at the same time");
  3286. return false;
  3287. }
  3288. /* filter out commands (with `_start`) cases */
  3289. if (start && !main_module) {
  3290. set_error_buf(
  3291. error_buf, error_buf_size,
  3292. "a command (with _start function) can not be a sub-module");
  3293. return false;
  3294. }
  3295. /*
  3296. * it is ok a reactor acts as a main module,
  3297. * so skip the check about (with `_initialize`)
  3298. */
  3299. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  3300. error_buf, error_buf_size);
  3301. if (!memory) {
  3302. set_error_buf(error_buf, error_buf_size,
  3303. "a module with WASI apis must export memory by default");
  3304. return false;
  3305. }
  3306. return true;
  3307. }
  3308. #endif
  3309. WASMModule *
  3310. wasm_loader_load(uint8 *buf, uint32 size,
  3311. #if WASM_ENABLE_MULTI_MODULE != 0
  3312. bool main_module,
  3313. #endif
  3314. char *error_buf, uint32 error_buf_size)
  3315. {
  3316. WASMModule *module = create_module(error_buf, error_buf_size);
  3317. if (!module) {
  3318. return NULL;
  3319. }
  3320. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0
  3321. module->load_addr = (uint8 *)buf;
  3322. module->load_size = size;
  3323. #endif
  3324. if (!load(buf, size, module, error_buf, error_buf_size)) {
  3325. goto fail;
  3326. }
  3327. #if (WASM_ENABLE_MULTI_MODULE != 0) && (WASM_ENABLE_LIBC_WASI != 0)
  3328. /* Check the WASI application ABI */
  3329. if (!check_wasi_abi_compatibility(module, main_module, error_buf,
  3330. error_buf_size)) {
  3331. goto fail;
  3332. }
  3333. #endif
  3334. LOG_VERBOSE("Load module success.\n");
  3335. return module;
  3336. fail:
  3337. wasm_loader_unload(module);
  3338. return NULL;
  3339. }
  3340. void
  3341. wasm_loader_unload(WASMModule *module)
  3342. {
  3343. uint32 i;
  3344. if (!module)
  3345. return;
  3346. if (module->types) {
  3347. for (i = 0; i < module->type_count; i++) {
  3348. if (module->types[i])
  3349. destroy_wasm_type(module->types[i]);
  3350. }
  3351. wasm_runtime_free(module->types);
  3352. }
  3353. if (module->imports)
  3354. wasm_runtime_free(module->imports);
  3355. if (module->functions) {
  3356. for (i = 0; i < module->function_count; i++) {
  3357. if (module->functions[i]) {
  3358. if (module->functions[i]->local_offsets)
  3359. wasm_runtime_free(module->functions[i]->local_offsets);
  3360. #if WASM_ENABLE_FAST_INTERP != 0
  3361. if (module->functions[i]->code_compiled)
  3362. wasm_runtime_free(module->functions[i]->code_compiled);
  3363. if (module->functions[i]->consts)
  3364. wasm_runtime_free(module->functions[i]->consts);
  3365. #endif
  3366. wasm_runtime_free(module->functions[i]);
  3367. }
  3368. }
  3369. wasm_runtime_free(module->functions);
  3370. }
  3371. if (module->tables)
  3372. wasm_runtime_free(module->tables);
  3373. if (module->memories)
  3374. wasm_runtime_free(module->memories);
  3375. if (module->globals)
  3376. wasm_runtime_free(module->globals);
  3377. if (module->exports)
  3378. wasm_runtime_free(module->exports);
  3379. if (module->table_segments) {
  3380. for (i = 0; i < module->table_seg_count; i++) {
  3381. if (module->table_segments[i].func_indexes)
  3382. wasm_runtime_free(module->table_segments[i].func_indexes);
  3383. }
  3384. wasm_runtime_free(module->table_segments);
  3385. }
  3386. if (module->data_segments) {
  3387. for (i = 0; i < module->data_seg_count; i++) {
  3388. if (module->data_segments[i])
  3389. wasm_runtime_free(module->data_segments[i]);
  3390. }
  3391. wasm_runtime_free(module->data_segments);
  3392. }
  3393. if (module->const_str_list) {
  3394. StringNode *node = module->const_str_list, *node_next;
  3395. while (node) {
  3396. node_next = node->next;
  3397. wasm_runtime_free(node);
  3398. node = node_next;
  3399. }
  3400. }
  3401. #if WASM_ENABLE_FAST_INTERP == 0
  3402. if (module->br_table_cache_list) {
  3403. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  3404. BrTableCache *node_next;
  3405. while (node) {
  3406. node_next = bh_list_elem_next(node);
  3407. wasm_runtime_free(node);
  3408. node = node_next;
  3409. }
  3410. }
  3411. #endif
  3412. #if WASM_ENABLE_MULTI_MODULE != 0
  3413. /* just release the sub module list */
  3414. if (module->import_module_list) {
  3415. WASMRegisteredModule *node =
  3416. bh_list_first_elem(module->import_module_list);
  3417. while (node) {
  3418. WASMRegisteredModule *next = bh_list_elem_next(node);
  3419. bh_list_remove(module->import_module_list, node);
  3420. /*
  3421. * unload(sub_module) will be trigged during runtime_destroy().
  3422. * every module in the global module list will be unloaded one by
  3423. * one. so don't worry.
  3424. */
  3425. wasm_runtime_free(node);
  3426. /*
  3427. * the module file reading buffer will be released
  3428. * in runtime_destroy()
  3429. */
  3430. node = next;
  3431. }
  3432. }
  3433. #endif
  3434. #if WASM_ENABLE_DEBUG_INTERP != 0
  3435. WASMFastOPCodeNode *fast_opcode =
  3436. bh_list_first_elem(&module->fast_opcode_list);
  3437. while (fast_opcode) {
  3438. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  3439. wasm_runtime_free(fast_opcode);
  3440. fast_opcode = next;
  3441. }
  3442. os_mutex_destroy(&module->ref_count_lock);
  3443. #endif
  3444. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3445. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  3446. #endif
  3447. #if WASM_ENABLE_FAST_JIT != 0
  3448. if (module->fast_jit_func_ptrs) {
  3449. for (i = 0; i < module->function_count; i++) {
  3450. if (module->fast_jit_func_ptrs[i])
  3451. jit_code_cache_free(module->fast_jit_func_ptrs[i]);
  3452. }
  3453. wasm_runtime_free(module->fast_jit_func_ptrs);
  3454. }
  3455. #endif
  3456. wasm_runtime_free(module);
  3457. }
  3458. bool
  3459. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  3460. const uint8 *start_addr, const uint8 *code_end_addr,
  3461. uint8 label_type, uint8 **p_else_addr,
  3462. uint8 **p_end_addr)
  3463. {
  3464. const uint8 *p = start_addr, *p_end = code_end_addr;
  3465. uint8 *else_addr = NULL;
  3466. char error_buf[128];
  3467. uint32 block_nested_depth = 1, count, i, j, t;
  3468. uint32 error_buf_size = sizeof(error_buf);
  3469. uint8 opcode, u8;
  3470. BlockAddr block_stack[16] = { { 0 } }, *block;
  3471. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3472. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  3473. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  3474. if (block[j].start_addr == start_addr) {
  3475. /* Cache hit */
  3476. *p_else_addr = block[j].else_addr;
  3477. *p_end_addr = block[j].end_addr;
  3478. return true;
  3479. }
  3480. }
  3481. /* Cache unhit */
  3482. block_stack[0].start_addr = start_addr;
  3483. while (p < code_end_addr) {
  3484. opcode = *p++;
  3485. #if WASM_ENABLE_DEBUG_INTERP != 0
  3486. op_break_retry:
  3487. #endif
  3488. switch (opcode) {
  3489. case WASM_OP_UNREACHABLE:
  3490. case WASM_OP_NOP:
  3491. break;
  3492. case WASM_OP_BLOCK:
  3493. case WASM_OP_LOOP:
  3494. case WASM_OP_IF:
  3495. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  3496. u8 = read_uint8(p);
  3497. if (block_nested_depth
  3498. < sizeof(block_stack) / sizeof(BlockAddr)) {
  3499. block_stack[block_nested_depth].start_addr = p;
  3500. block_stack[block_nested_depth].else_addr = NULL;
  3501. }
  3502. block_nested_depth++;
  3503. break;
  3504. case EXT_OP_BLOCK:
  3505. case EXT_OP_LOOP:
  3506. case EXT_OP_IF:
  3507. /* block type */
  3508. skip_leb_uint32(p, p_end);
  3509. if (block_nested_depth
  3510. < sizeof(block_stack) / sizeof(BlockAddr)) {
  3511. block_stack[block_nested_depth].start_addr = p;
  3512. block_stack[block_nested_depth].else_addr = NULL;
  3513. }
  3514. block_nested_depth++;
  3515. break;
  3516. case WASM_OP_ELSE:
  3517. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  3518. else_addr = (uint8 *)(p - 1);
  3519. if (block_nested_depth - 1
  3520. < sizeof(block_stack) / sizeof(BlockAddr))
  3521. block_stack[block_nested_depth - 1].else_addr =
  3522. (uint8 *)(p - 1);
  3523. break;
  3524. case WASM_OP_END:
  3525. if (block_nested_depth == 1) {
  3526. if (label_type == LABEL_TYPE_IF)
  3527. *p_else_addr = else_addr;
  3528. *p_end_addr = (uint8 *)(p - 1);
  3529. block_stack[0].end_addr = (uint8 *)(p - 1);
  3530. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  3531. t++) {
  3532. start_addr = block_stack[t].start_addr;
  3533. if (start_addr) {
  3534. i = ((uintptr_t)start_addr)
  3535. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3536. block =
  3537. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  3538. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  3539. if (!block[j].start_addr)
  3540. break;
  3541. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  3542. memmove(block + 1, block,
  3543. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  3544. * sizeof(BlockAddr));
  3545. j = 0;
  3546. }
  3547. block[j].start_addr = block_stack[t].start_addr;
  3548. block[j].else_addr = block_stack[t].else_addr;
  3549. block[j].end_addr = block_stack[t].end_addr;
  3550. }
  3551. else
  3552. break;
  3553. }
  3554. return true;
  3555. }
  3556. else {
  3557. block_nested_depth--;
  3558. if (block_nested_depth
  3559. < sizeof(block_stack) / sizeof(BlockAddr))
  3560. block_stack[block_nested_depth].end_addr =
  3561. (uint8 *)(p - 1);
  3562. }
  3563. break;
  3564. case WASM_OP_BR:
  3565. case WASM_OP_BR_IF:
  3566. skip_leb_uint32(p, p_end); /* labelidx */
  3567. break;
  3568. case WASM_OP_BR_TABLE:
  3569. read_leb_uint32(p, p_end, count); /* lable num */
  3570. #if WASM_ENABLE_FAST_INTERP != 0
  3571. for (i = 0; i <= count; i++) /* lableidxs */
  3572. skip_leb_uint32(p, p_end);
  3573. #else
  3574. p += count + 1;
  3575. while (*p == WASM_OP_NOP)
  3576. p++;
  3577. #endif
  3578. break;
  3579. #if WASM_ENABLE_FAST_INTERP == 0
  3580. case EXT_OP_BR_TABLE_CACHE:
  3581. read_leb_uint32(p, p_end, count); /* lable num */
  3582. while (*p == WASM_OP_NOP)
  3583. p++;
  3584. break;
  3585. #endif
  3586. case WASM_OP_RETURN:
  3587. break;
  3588. case WASM_OP_CALL:
  3589. #if WASM_ENABLE_TAIL_CALL != 0
  3590. case WASM_OP_RETURN_CALL:
  3591. #endif
  3592. skip_leb_uint32(p, p_end); /* funcidx */
  3593. break;
  3594. case WASM_OP_CALL_INDIRECT:
  3595. #if WASM_ENABLE_TAIL_CALL != 0
  3596. case WASM_OP_RETURN_CALL_INDIRECT:
  3597. #endif
  3598. skip_leb_uint32(p, p_end); /* typeidx */
  3599. CHECK_BUF(p, p_end, 1);
  3600. u8 = read_uint8(p); /* 0x00 */
  3601. break;
  3602. case WASM_OP_DROP:
  3603. case WASM_OP_SELECT:
  3604. case WASM_OP_DROP_64:
  3605. case WASM_OP_SELECT_64:
  3606. break;
  3607. #if WASM_ENABLE_REF_TYPES != 0
  3608. case WASM_OP_SELECT_T:
  3609. skip_leb_uint32(p, p_end); /* vec length */
  3610. CHECK_BUF(p, p_end, 1);
  3611. u8 = read_uint8(p); /* typeidx */
  3612. break;
  3613. case WASM_OP_TABLE_GET:
  3614. case WASM_OP_TABLE_SET:
  3615. skip_leb_uint32(p, p_end); /* table index */
  3616. break;
  3617. case WASM_OP_REF_NULL:
  3618. CHECK_BUF(p, p_end, 1);
  3619. u8 = read_uint8(p); /* type */
  3620. break;
  3621. case WASM_OP_REF_IS_NULL:
  3622. break;
  3623. case WASM_OP_REF_FUNC:
  3624. skip_leb_uint32(p, p_end); /* func index */
  3625. break;
  3626. #endif /* WASM_ENABLE_REF_TYPES */
  3627. case WASM_OP_GET_LOCAL:
  3628. case WASM_OP_SET_LOCAL:
  3629. case WASM_OP_TEE_LOCAL:
  3630. case WASM_OP_GET_GLOBAL:
  3631. case WASM_OP_SET_GLOBAL:
  3632. case WASM_OP_GET_GLOBAL_64:
  3633. case WASM_OP_SET_GLOBAL_64:
  3634. case WASM_OP_SET_GLOBAL_AUX_STACK:
  3635. skip_leb_uint32(p, p_end); /* local index */
  3636. break;
  3637. case EXT_OP_GET_LOCAL_FAST:
  3638. case EXT_OP_SET_LOCAL_FAST:
  3639. case EXT_OP_TEE_LOCAL_FAST:
  3640. CHECK_BUF(p, p_end, 1);
  3641. p++;
  3642. break;
  3643. case WASM_OP_I32_LOAD:
  3644. case WASM_OP_I64_LOAD:
  3645. case WASM_OP_F32_LOAD:
  3646. case WASM_OP_F64_LOAD:
  3647. case WASM_OP_I32_LOAD8_S:
  3648. case WASM_OP_I32_LOAD8_U:
  3649. case WASM_OP_I32_LOAD16_S:
  3650. case WASM_OP_I32_LOAD16_U:
  3651. case WASM_OP_I64_LOAD8_S:
  3652. case WASM_OP_I64_LOAD8_U:
  3653. case WASM_OP_I64_LOAD16_S:
  3654. case WASM_OP_I64_LOAD16_U:
  3655. case WASM_OP_I64_LOAD32_S:
  3656. case WASM_OP_I64_LOAD32_U:
  3657. case WASM_OP_I32_STORE:
  3658. case WASM_OP_I64_STORE:
  3659. case WASM_OP_F32_STORE:
  3660. case WASM_OP_F64_STORE:
  3661. case WASM_OP_I32_STORE8:
  3662. case WASM_OP_I32_STORE16:
  3663. case WASM_OP_I64_STORE8:
  3664. case WASM_OP_I64_STORE16:
  3665. case WASM_OP_I64_STORE32:
  3666. skip_leb_uint32(p, p_end); /* align */
  3667. skip_leb_uint32(p, p_end); /* offset */
  3668. break;
  3669. case WASM_OP_MEMORY_SIZE:
  3670. case WASM_OP_MEMORY_GROW:
  3671. skip_leb_uint32(p, p_end); /* 0x00 */
  3672. break;
  3673. case WASM_OP_I32_CONST:
  3674. skip_leb_int32(p, p_end);
  3675. break;
  3676. case WASM_OP_I64_CONST:
  3677. skip_leb_int64(p, p_end);
  3678. break;
  3679. case WASM_OP_F32_CONST:
  3680. p += sizeof(float32);
  3681. break;
  3682. case WASM_OP_F64_CONST:
  3683. p += sizeof(float64);
  3684. break;
  3685. case WASM_OP_I32_EQZ:
  3686. case WASM_OP_I32_EQ:
  3687. case WASM_OP_I32_NE:
  3688. case WASM_OP_I32_LT_S:
  3689. case WASM_OP_I32_LT_U:
  3690. case WASM_OP_I32_GT_S:
  3691. case WASM_OP_I32_GT_U:
  3692. case WASM_OP_I32_LE_S:
  3693. case WASM_OP_I32_LE_U:
  3694. case WASM_OP_I32_GE_S:
  3695. case WASM_OP_I32_GE_U:
  3696. case WASM_OP_I64_EQZ:
  3697. case WASM_OP_I64_EQ:
  3698. case WASM_OP_I64_NE:
  3699. case WASM_OP_I64_LT_S:
  3700. case WASM_OP_I64_LT_U:
  3701. case WASM_OP_I64_GT_S:
  3702. case WASM_OP_I64_GT_U:
  3703. case WASM_OP_I64_LE_S:
  3704. case WASM_OP_I64_LE_U:
  3705. case WASM_OP_I64_GE_S:
  3706. case WASM_OP_I64_GE_U:
  3707. case WASM_OP_F32_EQ:
  3708. case WASM_OP_F32_NE:
  3709. case WASM_OP_F32_LT:
  3710. case WASM_OP_F32_GT:
  3711. case WASM_OP_F32_LE:
  3712. case WASM_OP_F32_GE:
  3713. case WASM_OP_F64_EQ:
  3714. case WASM_OP_F64_NE:
  3715. case WASM_OP_F64_LT:
  3716. case WASM_OP_F64_GT:
  3717. case WASM_OP_F64_LE:
  3718. case WASM_OP_F64_GE:
  3719. case WASM_OP_I32_CLZ:
  3720. case WASM_OP_I32_CTZ:
  3721. case WASM_OP_I32_POPCNT:
  3722. case WASM_OP_I32_ADD:
  3723. case WASM_OP_I32_SUB:
  3724. case WASM_OP_I32_MUL:
  3725. case WASM_OP_I32_DIV_S:
  3726. case WASM_OP_I32_DIV_U:
  3727. case WASM_OP_I32_REM_S:
  3728. case WASM_OP_I32_REM_U:
  3729. case WASM_OP_I32_AND:
  3730. case WASM_OP_I32_OR:
  3731. case WASM_OP_I32_XOR:
  3732. case WASM_OP_I32_SHL:
  3733. case WASM_OP_I32_SHR_S:
  3734. case WASM_OP_I32_SHR_U:
  3735. case WASM_OP_I32_ROTL:
  3736. case WASM_OP_I32_ROTR:
  3737. case WASM_OP_I64_CLZ:
  3738. case WASM_OP_I64_CTZ:
  3739. case WASM_OP_I64_POPCNT:
  3740. case WASM_OP_I64_ADD:
  3741. case WASM_OP_I64_SUB:
  3742. case WASM_OP_I64_MUL:
  3743. case WASM_OP_I64_DIV_S:
  3744. case WASM_OP_I64_DIV_U:
  3745. case WASM_OP_I64_REM_S:
  3746. case WASM_OP_I64_REM_U:
  3747. case WASM_OP_I64_AND:
  3748. case WASM_OP_I64_OR:
  3749. case WASM_OP_I64_XOR:
  3750. case WASM_OP_I64_SHL:
  3751. case WASM_OP_I64_SHR_S:
  3752. case WASM_OP_I64_SHR_U:
  3753. case WASM_OP_I64_ROTL:
  3754. case WASM_OP_I64_ROTR:
  3755. case WASM_OP_F32_ABS:
  3756. case WASM_OP_F32_NEG:
  3757. case WASM_OP_F32_CEIL:
  3758. case WASM_OP_F32_FLOOR:
  3759. case WASM_OP_F32_TRUNC:
  3760. case WASM_OP_F32_NEAREST:
  3761. case WASM_OP_F32_SQRT:
  3762. case WASM_OP_F32_ADD:
  3763. case WASM_OP_F32_SUB:
  3764. case WASM_OP_F32_MUL:
  3765. case WASM_OP_F32_DIV:
  3766. case WASM_OP_F32_MIN:
  3767. case WASM_OP_F32_MAX:
  3768. case WASM_OP_F32_COPYSIGN:
  3769. case WASM_OP_F64_ABS:
  3770. case WASM_OP_F64_NEG:
  3771. case WASM_OP_F64_CEIL:
  3772. case WASM_OP_F64_FLOOR:
  3773. case WASM_OP_F64_TRUNC:
  3774. case WASM_OP_F64_NEAREST:
  3775. case WASM_OP_F64_SQRT:
  3776. case WASM_OP_F64_ADD:
  3777. case WASM_OP_F64_SUB:
  3778. case WASM_OP_F64_MUL:
  3779. case WASM_OP_F64_DIV:
  3780. case WASM_OP_F64_MIN:
  3781. case WASM_OP_F64_MAX:
  3782. case WASM_OP_F64_COPYSIGN:
  3783. case WASM_OP_I32_WRAP_I64:
  3784. case WASM_OP_I32_TRUNC_S_F32:
  3785. case WASM_OP_I32_TRUNC_U_F32:
  3786. case WASM_OP_I32_TRUNC_S_F64:
  3787. case WASM_OP_I32_TRUNC_U_F64:
  3788. case WASM_OP_I64_EXTEND_S_I32:
  3789. case WASM_OP_I64_EXTEND_U_I32:
  3790. case WASM_OP_I64_TRUNC_S_F32:
  3791. case WASM_OP_I64_TRUNC_U_F32:
  3792. case WASM_OP_I64_TRUNC_S_F64:
  3793. case WASM_OP_I64_TRUNC_U_F64:
  3794. case WASM_OP_F32_CONVERT_S_I32:
  3795. case WASM_OP_F32_CONVERT_U_I32:
  3796. case WASM_OP_F32_CONVERT_S_I64:
  3797. case WASM_OP_F32_CONVERT_U_I64:
  3798. case WASM_OP_F32_DEMOTE_F64:
  3799. case WASM_OP_F64_CONVERT_S_I32:
  3800. case WASM_OP_F64_CONVERT_U_I32:
  3801. case WASM_OP_F64_CONVERT_S_I64:
  3802. case WASM_OP_F64_CONVERT_U_I64:
  3803. case WASM_OP_F64_PROMOTE_F32:
  3804. case WASM_OP_I32_REINTERPRET_F32:
  3805. case WASM_OP_I64_REINTERPRET_F64:
  3806. case WASM_OP_F32_REINTERPRET_I32:
  3807. case WASM_OP_F64_REINTERPRET_I64:
  3808. case WASM_OP_I32_EXTEND8_S:
  3809. case WASM_OP_I32_EXTEND16_S:
  3810. case WASM_OP_I64_EXTEND8_S:
  3811. case WASM_OP_I64_EXTEND16_S:
  3812. case WASM_OP_I64_EXTEND32_S:
  3813. break;
  3814. case WASM_OP_MISC_PREFIX:
  3815. {
  3816. uint32 opcode1;
  3817. read_leb_uint32(p, p_end, opcode1);
  3818. switch (opcode1) {
  3819. case WASM_OP_I32_TRUNC_SAT_S_F32:
  3820. case WASM_OP_I32_TRUNC_SAT_U_F32:
  3821. case WASM_OP_I32_TRUNC_SAT_S_F64:
  3822. case WASM_OP_I32_TRUNC_SAT_U_F64:
  3823. case WASM_OP_I64_TRUNC_SAT_S_F32:
  3824. case WASM_OP_I64_TRUNC_SAT_U_F32:
  3825. case WASM_OP_I64_TRUNC_SAT_S_F64:
  3826. case WASM_OP_I64_TRUNC_SAT_U_F64:
  3827. break;
  3828. #if WASM_ENABLE_BULK_MEMORY != 0
  3829. case WASM_OP_MEMORY_INIT:
  3830. skip_leb_uint32(p, p_end);
  3831. /* skip memory idx */
  3832. p++;
  3833. break;
  3834. case WASM_OP_DATA_DROP:
  3835. skip_leb_uint32(p, p_end);
  3836. break;
  3837. case WASM_OP_MEMORY_COPY:
  3838. /* skip two memory idx */
  3839. p += 2;
  3840. break;
  3841. case WASM_OP_MEMORY_FILL:
  3842. /* skip memory idx */
  3843. p++;
  3844. break;
  3845. #endif /* WASM_ENABLE_BULK_MEMORY */
  3846. #if WASM_ENABLE_REF_TYPES != 0
  3847. case WASM_OP_TABLE_INIT:
  3848. case WASM_OP_TABLE_COPY:
  3849. /* tableidx */
  3850. skip_leb_uint32(p, p_end);
  3851. /* elemidx */
  3852. skip_leb_uint32(p, p_end);
  3853. break;
  3854. case WASM_OP_ELEM_DROP:
  3855. /* elemidx */
  3856. skip_leb_uint32(p, p_end);
  3857. break;
  3858. case WASM_OP_TABLE_SIZE:
  3859. case WASM_OP_TABLE_GROW:
  3860. case WASM_OP_TABLE_FILL:
  3861. skip_leb_uint32(p, p_end); /* table idx */
  3862. break;
  3863. #endif /* WASM_ENABLE_REF_TYPES */
  3864. default:
  3865. return false;
  3866. }
  3867. break;
  3868. }
  3869. #if WASM_ENABLE_SIMD != 0
  3870. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3871. case WASM_OP_SIMD_PREFIX:
  3872. {
  3873. /* TODO: shall we ceate a table to be friendly to branch
  3874. * prediction */
  3875. opcode = read_uint8(p);
  3876. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  3877. */
  3878. switch (opcode) {
  3879. case SIMD_v128_load:
  3880. case SIMD_v128_load8x8_s:
  3881. case SIMD_v128_load8x8_u:
  3882. case SIMD_v128_load16x4_s:
  3883. case SIMD_v128_load16x4_u:
  3884. case SIMD_v128_load32x2_s:
  3885. case SIMD_v128_load32x2_u:
  3886. case SIMD_v128_load8_splat:
  3887. case SIMD_v128_load16_splat:
  3888. case SIMD_v128_load32_splat:
  3889. case SIMD_v128_load64_splat:
  3890. case SIMD_v128_store:
  3891. /* memarg align */
  3892. skip_leb_uint32(p, p_end);
  3893. /* memarg offset*/
  3894. skip_leb_uint32(p, p_end);
  3895. break;
  3896. case SIMD_v128_const:
  3897. case SIMD_v8x16_shuffle:
  3898. /* immByte[16] immLaneId[16] */
  3899. CHECK_BUF1(p, p_end, 16);
  3900. p += 16;
  3901. break;
  3902. case SIMD_i8x16_extract_lane_s:
  3903. case SIMD_i8x16_extract_lane_u:
  3904. case SIMD_i8x16_replace_lane:
  3905. case SIMD_i16x8_extract_lane_s:
  3906. case SIMD_i16x8_extract_lane_u:
  3907. case SIMD_i16x8_replace_lane:
  3908. case SIMD_i32x4_extract_lane:
  3909. case SIMD_i32x4_replace_lane:
  3910. case SIMD_i64x2_extract_lane:
  3911. case SIMD_i64x2_replace_lane:
  3912. case SIMD_f32x4_extract_lane:
  3913. case SIMD_f32x4_replace_lane:
  3914. case SIMD_f64x2_extract_lane:
  3915. case SIMD_f64x2_replace_lane:
  3916. /* ImmLaneId */
  3917. CHECK_BUF(p, p_end, 1);
  3918. p++;
  3919. break;
  3920. case SIMD_v128_load8_lane:
  3921. case SIMD_v128_load16_lane:
  3922. case SIMD_v128_load32_lane:
  3923. case SIMD_v128_load64_lane:
  3924. case SIMD_v128_store8_lane:
  3925. case SIMD_v128_store16_lane:
  3926. case SIMD_v128_store32_lane:
  3927. case SIMD_v128_store64_lane:
  3928. /* memarg align */
  3929. skip_leb_uint32(p, p_end);
  3930. /* memarg offset*/
  3931. skip_leb_uint32(p, p_end);
  3932. /* ImmLaneId */
  3933. CHECK_BUF(p, p_end, 1);
  3934. p++;
  3935. break;
  3936. case SIMD_v128_load32_zero:
  3937. case SIMD_v128_load64_zero:
  3938. /* memarg align */
  3939. skip_leb_uint32(p, p_end);
  3940. /* memarg offset*/
  3941. skip_leb_uint32(p, p_end);
  3942. break;
  3943. default:
  3944. /*
  3945. * since latest SIMD specific used almost every value
  3946. * from 0x00 to 0xff, the default branch will present
  3947. * all opcodes without imm
  3948. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  3949. */
  3950. break;
  3951. }
  3952. break;
  3953. }
  3954. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  3955. #endif /* end of WASM_ENABLE_SIMD */
  3956. #if WASM_ENABLE_SHARED_MEMORY != 0
  3957. case WASM_OP_ATOMIC_PREFIX:
  3958. {
  3959. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  3960. opcode = read_uint8(p);
  3961. if (opcode != WASM_OP_ATOMIC_FENCE) {
  3962. skip_leb_uint32(p, p_end); /* align */
  3963. skip_leb_uint32(p, p_end); /* offset */
  3964. }
  3965. else {
  3966. /* atomic.fence doesn't have memarg */
  3967. p++;
  3968. }
  3969. break;
  3970. }
  3971. #endif
  3972. #if WASM_ENABLE_DEBUG_INTERP != 0
  3973. case DEBUG_OP_BREAK:
  3974. {
  3975. WASMDebugInstance *debug_instance =
  3976. wasm_exec_env_get_instance(exec_env);
  3977. char orignal_opcode[1];
  3978. uint64 size = 1;
  3979. WASMModuleInstance *module_inst =
  3980. (WASMModuleInstance *)exec_env->module_inst;
  3981. uint64 offset = (p - 1) >= module_inst->module->load_addr
  3982. ? (p - 1) - module_inst->module->load_addr
  3983. : ~0;
  3984. if (debug_instance) {
  3985. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  3986. orignal_opcode, &size)
  3987. && size == 1) {
  3988. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  3989. "with %02x: ",
  3990. orignal_opcode[0]);
  3991. opcode = orignal_opcode[0];
  3992. goto op_break_retry;
  3993. }
  3994. }
  3995. break;
  3996. }
  3997. #endif
  3998. default:
  3999. return false;
  4000. }
  4001. }
  4002. (void)u8;
  4003. return false;
  4004. fail:
  4005. return false;
  4006. }
  4007. #define REF_ANY VALUE_TYPE_ANY
  4008. #define REF_I32 VALUE_TYPE_I32
  4009. #define REF_F32 VALUE_TYPE_F32
  4010. #define REF_I64_1 VALUE_TYPE_I64
  4011. #define REF_I64_2 VALUE_TYPE_I64
  4012. #define REF_F64_1 VALUE_TYPE_F64
  4013. #define REF_F64_2 VALUE_TYPE_F64
  4014. #define REF_V128_1 VALUE_TYPE_V128
  4015. #define REF_V128_2 VALUE_TYPE_V128
  4016. #define REF_V128_3 VALUE_TYPE_V128
  4017. #define REF_V128_4 VALUE_TYPE_V128
  4018. #define REF_FUNCREF VALUE_TYPE_FUNCREF
  4019. #define REF_EXTERNREF VALUE_TYPE_EXTERNREF
  4020. #if WASM_ENABLE_FAST_INTERP != 0
  4021. #if WASM_DEBUG_PREPROCESSOR != 0
  4022. #define LOG_OP(...) os_printf(__VA_ARGS__)
  4023. #else
  4024. #define LOG_OP(...) (void)0
  4025. #endif
  4026. #define PATCH_ELSE 0
  4027. #define PATCH_END 1
  4028. typedef struct BranchBlockPatch {
  4029. struct BranchBlockPatch *next;
  4030. uint8 patch_type;
  4031. uint8 *code_compiled;
  4032. } BranchBlockPatch;
  4033. #endif
  4034. typedef struct BranchBlock {
  4035. uint8 label_type;
  4036. BlockType block_type;
  4037. uint8 *start_addr;
  4038. uint8 *else_addr;
  4039. uint8 *end_addr;
  4040. uint32 stack_cell_num;
  4041. #if WASM_ENABLE_FAST_INTERP != 0
  4042. uint16 dynamic_offset;
  4043. uint8 *code_compiled;
  4044. BranchBlockPatch *patch_list;
  4045. /* This is used to save params frame_offset of of if block */
  4046. int16 *param_frame_offsets;
  4047. #endif
  4048. /* Indicate the operand stack is in polymorphic state.
  4049. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  4050. * to polymorphic state until the block's 'end' opcode is processed.
  4051. * If stack is in polymorphic state and stack is empty, instruction can
  4052. * pop any type of value directly without decreasing stack top pointer
  4053. * and stack cell num. */
  4054. bool is_stack_polymorphic;
  4055. } BranchBlock;
  4056. typedef struct WASMLoaderContext {
  4057. /* frame ref stack */
  4058. uint8 *frame_ref;
  4059. uint8 *frame_ref_bottom;
  4060. uint8 *frame_ref_boundary;
  4061. uint32 frame_ref_size;
  4062. uint32 stack_cell_num;
  4063. uint32 max_stack_cell_num;
  4064. /* frame csp stack */
  4065. BranchBlock *frame_csp;
  4066. BranchBlock *frame_csp_bottom;
  4067. BranchBlock *frame_csp_boundary;
  4068. uint32 frame_csp_size;
  4069. uint32 csp_num;
  4070. uint32 max_csp_num;
  4071. #if WASM_ENABLE_FAST_INTERP != 0
  4072. /* frame offset stack */
  4073. int16 *frame_offset;
  4074. int16 *frame_offset_bottom;
  4075. int16 *frame_offset_boundary;
  4076. uint32 frame_offset_size;
  4077. int16 dynamic_offset;
  4078. int16 start_dynamic_offset;
  4079. int16 max_dynamic_offset;
  4080. /* preserved local offset */
  4081. int16 preserved_local_offset;
  4082. /* const buffer */
  4083. uint8 *const_buf;
  4084. uint16 num_const;
  4085. uint16 const_cell_num;
  4086. uint32 const_buf_size;
  4087. /* processed code */
  4088. uint8 *p_code_compiled;
  4089. uint8 *p_code_compiled_end;
  4090. uint32 code_compiled_size;
  4091. /* If the last opcode will be dropped, the peak memory usage will be larger
  4092. * than the final code_compiled_size, we record the peak size to ensure
  4093. * there will not be invalid memory access during second traverse */
  4094. uint32 code_compiled_peak_size;
  4095. #endif
  4096. } WASMLoaderContext;
  4097. typedef struct Const {
  4098. WASMValue value;
  4099. uint16 slot_index;
  4100. uint8 value_type;
  4101. } Const;
  4102. static void *
  4103. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  4104. uint32 error_buf_size)
  4105. {
  4106. uint8 *mem_new;
  4107. bh_assert(size_new > size_old);
  4108. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  4109. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  4110. memset(mem_new + size_old, 0, size_new - size_old);
  4111. wasm_runtime_free(mem_old);
  4112. }
  4113. return mem_new;
  4114. }
  4115. #define MEM_REALLOC(mem, size_old, size_new) \
  4116. do { \
  4117. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  4118. error_buf_size); \
  4119. if (!mem_new) \
  4120. goto fail; \
  4121. mem = mem_new; \
  4122. } while (0)
  4123. #define CHECK_CSP_PUSH() \
  4124. do { \
  4125. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  4126. MEM_REALLOC( \
  4127. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  4128. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  4129. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  4130. ctx->frame_csp_boundary = \
  4131. ctx->frame_csp_bottom \
  4132. + ctx->frame_csp_size / sizeof(BranchBlock); \
  4133. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  4134. } \
  4135. } while (0)
  4136. #define CHECK_CSP_POP() \
  4137. do { \
  4138. if (ctx->csp_num < 1) { \
  4139. set_error_buf(error_buf, error_buf_size, \
  4140. "type mismatch: " \
  4141. "expect data but block stack was empty"); \
  4142. goto fail; \
  4143. } \
  4144. } while (0)
  4145. #if WASM_ENABLE_FAST_INTERP != 0
  4146. static bool
  4147. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  4148. uint32 error_buf_size)
  4149. {
  4150. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  4151. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  4152. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  4153. ctx->frame_offset_size + 16);
  4154. ctx->frame_offset_size += 16;
  4155. ctx->frame_offset_boundary =
  4156. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  4157. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  4158. }
  4159. return true;
  4160. fail:
  4161. return false;
  4162. }
  4163. static bool
  4164. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  4165. {
  4166. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  4167. return false;
  4168. return true;
  4169. }
  4170. static void
  4171. free_label_patch_list(BranchBlock *frame_csp)
  4172. {
  4173. BranchBlockPatch *label_patch = frame_csp->patch_list;
  4174. BranchBlockPatch *next;
  4175. while (label_patch != NULL) {
  4176. next = label_patch->next;
  4177. wasm_runtime_free(label_patch);
  4178. label_patch = next;
  4179. }
  4180. frame_csp->patch_list = NULL;
  4181. }
  4182. static void
  4183. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  4184. {
  4185. BranchBlock *tmp_csp = frame_csp;
  4186. for (uint32 i = 0; i < csp_num; i++) {
  4187. free_label_patch_list(tmp_csp);
  4188. tmp_csp++;
  4189. }
  4190. }
  4191. #endif /* end of WASM_ENABLE_FAST_INTERP */
  4192. static bool
  4193. check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
  4194. {
  4195. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  4196. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  4197. ctx->frame_ref_size + 16);
  4198. ctx->frame_ref_size += 16;
  4199. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  4200. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  4201. }
  4202. return true;
  4203. fail:
  4204. return false;
  4205. }
  4206. static bool
  4207. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  4208. char *error_buf, uint32 error_buf_size)
  4209. {
  4210. if ((is_32bit_type(type) && stack_cell_num < 1)
  4211. || (is_64bit_type(type) && stack_cell_num < 2)
  4212. #if WASM_ENABLE_SIMD != 0
  4213. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4214. || (type == VALUE_TYPE_V128 && stack_cell_num < 4)
  4215. #endif
  4216. #endif
  4217. ) {
  4218. set_error_buf(error_buf, error_buf_size,
  4219. "type mismatch: expect data but stack was empty");
  4220. return false;
  4221. }
  4222. if ((is_32bit_type(type) && *(frame_ref - 1) != type)
  4223. || (is_64bit_type(type)
  4224. && (*(frame_ref - 2) != type || *(frame_ref - 1) != type))
  4225. #if WASM_ENABLE_SIMD != 0
  4226. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4227. || (type == VALUE_TYPE_V128
  4228. && (*(frame_ref - 4) != REF_V128_1 || *(frame_ref - 3) != REF_V128_2
  4229. || *(frame_ref - 2) != REF_V128_3
  4230. || *(frame_ref - 1) != REF_V128_4))
  4231. #endif
  4232. #endif
  4233. ) {
  4234. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  4235. "type mismatch: expect ", type2str(type),
  4236. " but got other");
  4237. return false;
  4238. }
  4239. return true;
  4240. }
  4241. static bool
  4242. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4243. uint32 error_buf_size)
  4244. {
  4245. int32 block_stack_cell_num =
  4246. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  4247. if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  4248. /* the stack top is a value of any type, return success */
  4249. return true;
  4250. }
  4251. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
  4252. error_buf, error_buf_size))
  4253. return false;
  4254. return true;
  4255. }
  4256. static void
  4257. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  4258. {
  4259. if (ctx) {
  4260. if (ctx->frame_ref_bottom)
  4261. wasm_runtime_free(ctx->frame_ref_bottom);
  4262. if (ctx->frame_csp_bottom) {
  4263. #if WASM_ENABLE_FAST_INTERP != 0
  4264. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  4265. #endif
  4266. wasm_runtime_free(ctx->frame_csp_bottom);
  4267. }
  4268. #if WASM_ENABLE_FAST_INTERP != 0
  4269. if (ctx->frame_offset_bottom)
  4270. wasm_runtime_free(ctx->frame_offset_bottom);
  4271. if (ctx->const_buf)
  4272. wasm_runtime_free(ctx->const_buf);
  4273. #endif
  4274. wasm_runtime_free(ctx);
  4275. }
  4276. }
  4277. static WASMLoaderContext *
  4278. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  4279. {
  4280. WASMLoaderContext *loader_ctx =
  4281. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  4282. if (!loader_ctx)
  4283. return NULL;
  4284. loader_ctx->frame_ref_size = 32;
  4285. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  4286. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  4287. goto fail;
  4288. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  4289. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  4290. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  4291. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  4292. goto fail;
  4293. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  4294. #if WASM_ENABLE_FAST_INTERP != 0
  4295. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  4296. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  4297. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  4298. error_buf_size)))
  4299. goto fail;
  4300. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  4301. loader_ctx->num_const = 0;
  4302. loader_ctx->const_buf_size = sizeof(Const) * 8;
  4303. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  4304. error_buf, error_buf_size)))
  4305. goto fail;
  4306. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  4307. set_error_buf(error_buf, error_buf_size,
  4308. "fast interpreter offset overflow");
  4309. goto fail;
  4310. }
  4311. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  4312. loader_ctx->max_dynamic_offset =
  4313. func->param_cell_num + func->local_cell_num;
  4314. #endif
  4315. return loader_ctx;
  4316. fail:
  4317. wasm_loader_ctx_destroy(loader_ctx);
  4318. return NULL;
  4319. }
  4320. static bool
  4321. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4322. uint32 error_buf_size)
  4323. {
  4324. if (type == VALUE_TYPE_VOID)
  4325. return true;
  4326. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4327. return false;
  4328. *ctx->frame_ref++ = type;
  4329. ctx->stack_cell_num++;
  4330. if (is_32bit_type(type) || type == VALUE_TYPE_ANY)
  4331. goto check_stack_and_return;
  4332. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4333. return false;
  4334. *ctx->frame_ref++ = type;
  4335. ctx->stack_cell_num++;
  4336. #if WASM_ENABLE_SIMD != 0
  4337. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4338. if (type == VALUE_TYPE_V128) {
  4339. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4340. return false;
  4341. *ctx->frame_ref++ = type;
  4342. ctx->stack_cell_num++;
  4343. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4344. return false;
  4345. *ctx->frame_ref++ = type;
  4346. ctx->stack_cell_num++;
  4347. }
  4348. #endif
  4349. #endif
  4350. check_stack_and_return:
  4351. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  4352. ctx->max_stack_cell_num = ctx->stack_cell_num;
  4353. return true;
  4354. }
  4355. static bool
  4356. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4357. uint32 error_buf_size)
  4358. {
  4359. BranchBlock *cur_block = ctx->frame_csp - 1;
  4360. int32 available_stack_cell =
  4361. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4362. /* Directly return success if current block is in stack
  4363. * polymorphic state while stack is empty. */
  4364. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4365. return true;
  4366. if (type == VALUE_TYPE_VOID)
  4367. return true;
  4368. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  4369. return false;
  4370. ctx->frame_ref--;
  4371. ctx->stack_cell_num--;
  4372. if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
  4373. return true;
  4374. ctx->frame_ref--;
  4375. ctx->stack_cell_num--;
  4376. #if WASM_ENABLE_SIMD != 0
  4377. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4378. if (type == VALUE_TYPE_V128) {
  4379. ctx->frame_ref -= 2;
  4380. ctx->stack_cell_num -= 2;
  4381. }
  4382. #endif
  4383. #endif
  4384. return true;
  4385. }
  4386. static bool
  4387. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  4388. uint8 type_push, uint8 type_pop, char *error_buf,
  4389. uint32 error_buf_size)
  4390. {
  4391. for (int i = 0; i < pop_cnt; i++) {
  4392. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  4393. error_buf_size))
  4394. return false;
  4395. }
  4396. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  4397. return false;
  4398. return true;
  4399. }
  4400. static bool
  4401. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  4402. BlockType block_type, uint8 *start_addr,
  4403. char *error_buf, uint32 error_buf_size)
  4404. {
  4405. CHECK_CSP_PUSH();
  4406. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  4407. ctx->frame_csp->label_type = label_type;
  4408. ctx->frame_csp->block_type = block_type;
  4409. ctx->frame_csp->start_addr = start_addr;
  4410. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  4411. #if WASM_ENABLE_FAST_INTERP != 0
  4412. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  4413. ctx->frame_csp->patch_list = NULL;
  4414. #endif
  4415. ctx->frame_csp++;
  4416. ctx->csp_num++;
  4417. if (ctx->csp_num > ctx->max_csp_num)
  4418. ctx->max_csp_num = ctx->csp_num;
  4419. return true;
  4420. fail:
  4421. return false;
  4422. }
  4423. static bool
  4424. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  4425. uint32 error_buf_size)
  4426. {
  4427. CHECK_CSP_POP();
  4428. #if WASM_ENABLE_FAST_INTERP != 0
  4429. if ((ctx->frame_csp - 1)->param_frame_offsets)
  4430. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  4431. #endif
  4432. ctx->frame_csp--;
  4433. ctx->csp_num--;
  4434. return true;
  4435. fail:
  4436. return false;
  4437. }
  4438. #if WASM_ENABLE_FAST_INTERP != 0
  4439. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4440. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  4441. #define emit_label(opcode) \
  4442. do { \
  4443. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  4444. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4445. } while (0)
  4446. #define skip_label() \
  4447. do { \
  4448. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  4449. LOG_OP("\ndelete last op\n"); \
  4450. } while (0)
  4451. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4452. #define emit_label(opcode) \
  4453. do { \
  4454. int32 offset = \
  4455. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  4456. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  4457. set_error_buf(error_buf, error_buf_size, \
  4458. "pre-compiled label offset out of range"); \
  4459. goto fail; \
  4460. } \
  4461. wasm_loader_emit_int16(loader_ctx, offset); \
  4462. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4463. } while (0)
  4464. #define skip_label() \
  4465. do { \
  4466. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  4467. LOG_OP("\ndelete last op\n"); \
  4468. } while (0)
  4469. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4470. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  4471. #define emit_label(opcode) \
  4472. do { \
  4473. wasm_loader_emit_uint8(loader_ctx, opcode); \
  4474. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4475. } while (0)
  4476. #define skip_label() \
  4477. do { \
  4478. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  4479. LOG_OP("\ndelete last op\n"); \
  4480. } while (0)
  4481. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  4482. #define emit_empty_label_addr_and_frame_ip(type) \
  4483. do { \
  4484. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  4485. loader_ctx->p_code_compiled, error_buf, \
  4486. error_buf_size)) \
  4487. goto fail; \
  4488. /* label address, to be patched */ \
  4489. wasm_loader_emit_ptr(loader_ctx, NULL); \
  4490. } while (0)
  4491. #define emit_br_info(frame_csp) \
  4492. do { \
  4493. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  4494. error_buf_size)) \
  4495. goto fail; \
  4496. } while (0)
  4497. #define LAST_OP_OUTPUT_I32() \
  4498. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  4499. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  4500. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  4501. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  4502. || (last_op >= WASM_OP_I32_WRAP_I64 \
  4503. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  4504. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  4505. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  4506. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  4507. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  4508. || (last_op == EXT_OP_COPY_STACK_TOP)
  4509. #define LAST_OP_OUTPUT_I64() \
  4510. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  4511. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  4512. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  4513. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  4514. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  4515. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  4516. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  4517. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  4518. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  4519. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  4520. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  4521. #define GET_CONST_OFFSET(type, val) \
  4522. do { \
  4523. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  4524. &operand_offset, error_buf, \
  4525. error_buf_size))) \
  4526. goto fail; \
  4527. } while (0)
  4528. #define GET_CONST_F32_OFFSET(type, fval) \
  4529. do { \
  4530. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  4531. &operand_offset, error_buf, \
  4532. error_buf_size))) \
  4533. goto fail; \
  4534. } while (0)
  4535. #define GET_CONST_F64_OFFSET(type, fval) \
  4536. do { \
  4537. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  4538. &operand_offset, error_buf, \
  4539. error_buf_size))) \
  4540. goto fail; \
  4541. } while (0)
  4542. #define emit_operand(ctx, offset) \
  4543. do { \
  4544. wasm_loader_emit_int16(ctx, offset); \
  4545. LOG_OP("%d\t", offset); \
  4546. } while (0)
  4547. #define emit_byte(ctx, byte) \
  4548. do { \
  4549. wasm_loader_emit_uint8(ctx, byte); \
  4550. LOG_OP("%d\t", byte); \
  4551. } while (0)
  4552. #define emit_uint32(ctx, value) \
  4553. do { \
  4554. wasm_loader_emit_uint32(ctx, value); \
  4555. LOG_OP("%d\t", value); \
  4556. } while (0)
  4557. #define emit_uint64(ctx, value) \
  4558. do { \
  4559. wasm_loader_emit_const(ctx, &value, false); \
  4560. LOG_OP("%lld\t", value); \
  4561. } while (0)
  4562. #define emit_float32(ctx, value) \
  4563. do { \
  4564. wasm_loader_emit_const(ctx, &value, true); \
  4565. LOG_OP("%f\t", value); \
  4566. } while (0)
  4567. #define emit_float64(ctx, value) \
  4568. do { \
  4569. wasm_loader_emit_const(ctx, &value, false); \
  4570. LOG_OP("%f\t", value); \
  4571. } while (0)
  4572. static bool
  4573. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  4574. {
  4575. if (!(ctx->p_code_compiled =
  4576. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  4577. return false;
  4578. ctx->p_code_compiled_end =
  4579. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  4580. /* clean up frame ref */
  4581. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  4582. ctx->frame_ref = ctx->frame_ref_bottom;
  4583. ctx->stack_cell_num = 0;
  4584. /* clean up frame csp */
  4585. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  4586. ctx->frame_csp = ctx->frame_csp_bottom;
  4587. ctx->csp_num = 0;
  4588. ctx->max_csp_num = 0;
  4589. /* clean up frame offset */
  4590. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  4591. ctx->frame_offset = ctx->frame_offset_bottom;
  4592. ctx->dynamic_offset = ctx->start_dynamic_offset;
  4593. /* init preserved local offsets */
  4594. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  4595. /* const buf is reserved */
  4596. return true;
  4597. }
  4598. static void
  4599. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  4600. {
  4601. ctx->code_compiled_size += size;
  4602. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  4603. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  4604. }
  4605. }
  4606. static void
  4607. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  4608. {
  4609. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  4610. if (ctx->p_code_compiled) {
  4611. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4612. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4613. #endif
  4614. bh_memcpy_s(ctx->p_code_compiled,
  4615. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  4616. value, size);
  4617. ctx->p_code_compiled += size;
  4618. }
  4619. else {
  4620. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4621. bh_assert((ctx->code_compiled_size & 1) == 0);
  4622. #endif
  4623. increase_compiled_code_space(ctx, size);
  4624. }
  4625. }
  4626. static void
  4627. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  4628. {
  4629. if (ctx->p_code_compiled) {
  4630. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4631. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4632. #endif
  4633. STORE_U32(ctx->p_code_compiled, value);
  4634. ctx->p_code_compiled += sizeof(uint32);
  4635. }
  4636. else {
  4637. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4638. bh_assert((ctx->code_compiled_size & 1) == 0);
  4639. #endif
  4640. increase_compiled_code_space(ctx, sizeof(uint32));
  4641. }
  4642. }
  4643. static void
  4644. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  4645. {
  4646. if (ctx->p_code_compiled) {
  4647. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4648. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4649. #endif
  4650. STORE_U16(ctx->p_code_compiled, (uint16)value);
  4651. ctx->p_code_compiled += sizeof(int16);
  4652. }
  4653. else {
  4654. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4655. bh_assert((ctx->code_compiled_size & 1) == 0);
  4656. #endif
  4657. increase_compiled_code_space(ctx, sizeof(uint16));
  4658. }
  4659. }
  4660. static void
  4661. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  4662. {
  4663. if (ctx->p_code_compiled) {
  4664. *(ctx->p_code_compiled) = value;
  4665. ctx->p_code_compiled += sizeof(uint8);
  4666. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4667. ctx->p_code_compiled++;
  4668. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4669. #endif
  4670. }
  4671. else {
  4672. increase_compiled_code_space(ctx, sizeof(uint8));
  4673. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4674. increase_compiled_code_space(ctx, sizeof(uint8));
  4675. bh_assert((ctx->code_compiled_size & 1) == 0);
  4676. #endif
  4677. }
  4678. }
  4679. static void
  4680. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  4681. {
  4682. if (ctx->p_code_compiled) {
  4683. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4684. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4685. #endif
  4686. STORE_PTR(ctx->p_code_compiled, value);
  4687. ctx->p_code_compiled += sizeof(void *);
  4688. }
  4689. else {
  4690. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4691. bh_assert((ctx->code_compiled_size & 1) == 0);
  4692. #endif
  4693. increase_compiled_code_space(ctx, sizeof(void *));
  4694. }
  4695. }
  4696. static void
  4697. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  4698. {
  4699. if (ctx->p_code_compiled) {
  4700. ctx->p_code_compiled -= size;
  4701. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4702. if (size == sizeof(uint8)) {
  4703. ctx->p_code_compiled--;
  4704. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  4705. }
  4706. #endif
  4707. }
  4708. else {
  4709. ctx->code_compiled_size -= size;
  4710. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4711. if (size == sizeof(uint8)) {
  4712. ctx->code_compiled_size--;
  4713. bh_assert((ctx->code_compiled_size & 1) == 0);
  4714. }
  4715. #endif
  4716. }
  4717. }
  4718. static bool
  4719. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  4720. uint32 local_index, uint32 local_type,
  4721. bool *preserved, char *error_buf,
  4722. uint32 error_buf_size)
  4723. {
  4724. uint32 i = 0;
  4725. int16 preserved_offset = (int16)local_index;
  4726. *preserved = false;
  4727. while (i < loader_ctx->stack_cell_num) {
  4728. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  4729. /* move previous local into dynamic space before a set/tee_local opcode
  4730. */
  4731. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  4732. if (!(*preserved)) {
  4733. *preserved = true;
  4734. skip_label();
  4735. preserved_offset = loader_ctx->preserved_local_offset;
  4736. if (loader_ctx->p_code_compiled) {
  4737. bh_assert(preserved_offset != (int16)local_index);
  4738. }
  4739. if (is_32bit_type(local_type)) {
  4740. /* Only increase preserve offset in the second traversal */
  4741. if (loader_ctx->p_code_compiled)
  4742. loader_ctx->preserved_local_offset++;
  4743. emit_label(EXT_OP_COPY_STACK_TOP);
  4744. }
  4745. else {
  4746. if (loader_ctx->p_code_compiled)
  4747. loader_ctx->preserved_local_offset += 2;
  4748. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  4749. }
  4750. emit_operand(loader_ctx, local_index);
  4751. emit_operand(loader_ctx, preserved_offset);
  4752. emit_label(opcode);
  4753. }
  4754. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  4755. }
  4756. if (is_32bit_type(cur_type))
  4757. i++;
  4758. else
  4759. i += 2;
  4760. }
  4761. return true;
  4762. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4763. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  4764. fail:
  4765. return false;
  4766. #endif
  4767. #endif
  4768. }
  4769. static bool
  4770. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  4771. char *error_buf, uint32 error_buf_size)
  4772. {
  4773. uint32 i = 0;
  4774. bool preserve_local;
  4775. /* preserve locals before blocks to ensure that "tee/set_local" inside
  4776. blocks will not influence the value of these locals */
  4777. while (i < loader_ctx->stack_cell_num) {
  4778. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  4779. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  4780. if ((cur_offset < loader_ctx->start_dynamic_offset)
  4781. && (cur_offset >= 0)) {
  4782. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  4783. cur_type, &preserve_local,
  4784. error_buf, error_buf_size)))
  4785. return false;
  4786. }
  4787. if (is_32bit_type(cur_type)) {
  4788. i++;
  4789. }
  4790. else {
  4791. i += 2;
  4792. }
  4793. }
  4794. return true;
  4795. }
  4796. static bool
  4797. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  4798. uint8 *p_code_compiled, char *error_buf,
  4799. uint32 error_buf_size)
  4800. {
  4801. BranchBlockPatch *patch =
  4802. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  4803. if (!patch) {
  4804. return false;
  4805. }
  4806. patch->patch_type = patch_type;
  4807. patch->code_compiled = p_code_compiled;
  4808. if (!frame_csp->patch_list) {
  4809. frame_csp->patch_list = patch;
  4810. patch->next = NULL;
  4811. }
  4812. else {
  4813. patch->next = frame_csp->patch_list;
  4814. frame_csp->patch_list = patch;
  4815. }
  4816. return true;
  4817. }
  4818. static void
  4819. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  4820. {
  4821. BranchBlock *frame_csp = ctx->frame_csp - depth;
  4822. BranchBlockPatch *node = frame_csp->patch_list;
  4823. BranchBlockPatch *node_prev = NULL, *node_next;
  4824. if (!ctx->p_code_compiled)
  4825. return;
  4826. while (node) {
  4827. node_next = node->next;
  4828. if (node->patch_type == patch_type) {
  4829. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  4830. if (node_prev == NULL) {
  4831. frame_csp->patch_list = node_next;
  4832. }
  4833. else {
  4834. node_prev->next = node_next;
  4835. }
  4836. wasm_runtime_free(node);
  4837. }
  4838. else {
  4839. node_prev = node;
  4840. }
  4841. node = node_next;
  4842. }
  4843. }
  4844. static bool
  4845. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  4846. char *error_buf, uint32 error_buf_size)
  4847. {
  4848. /* br info layout:
  4849. * a) arity of target block
  4850. * b) total cell num of arity values
  4851. * c) each arity value's cell num
  4852. * d) each arity value's src frame offset
  4853. * e) each arity values's dst dynamic offset
  4854. * f) branch target address
  4855. *
  4856. * Note: b-e are omitted when arity is 0 so that
  4857. * interpreter can recover the br info quickly.
  4858. */
  4859. BlockType *block_type = &frame_csp->block_type;
  4860. uint8 *types = NULL, cell;
  4861. uint32 arity = 0;
  4862. int32 i;
  4863. int16 *frame_offset = ctx->frame_offset;
  4864. uint16 dynamic_offset;
  4865. /* Note: loop's arity is different from if and block. loop's arity is
  4866. * its parameter count while if and block arity is result count.
  4867. */
  4868. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  4869. arity = block_type_get_param_types(block_type, &types);
  4870. else
  4871. arity = block_type_get_result_types(block_type, &types);
  4872. /* Part a */
  4873. emit_uint32(ctx, arity);
  4874. if (arity) {
  4875. /* Part b */
  4876. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  4877. /* Part c */
  4878. for (i = (int32)arity - 1; i >= 0; i--) {
  4879. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4880. emit_byte(ctx, cell);
  4881. }
  4882. /* Part d */
  4883. for (i = (int32)arity - 1; i >= 0; i--) {
  4884. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4885. frame_offset -= cell;
  4886. emit_operand(ctx, *(int16 *)(frame_offset));
  4887. }
  4888. /* Part e */
  4889. dynamic_offset =
  4890. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  4891. for (i = (int32)arity - 1; i >= 0; i--) {
  4892. cell = (uint8)wasm_value_type_cell_num(types[i]);
  4893. dynamic_offset -= cell;
  4894. emit_operand(ctx, dynamic_offset);
  4895. }
  4896. }
  4897. /* Part f */
  4898. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  4899. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  4900. }
  4901. else {
  4902. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  4903. error_buf, error_buf_size))
  4904. return false;
  4905. /* label address, to be patched */
  4906. wasm_loader_emit_ptr(ctx, NULL);
  4907. }
  4908. return true;
  4909. }
  4910. static bool
  4911. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4912. bool disable_emit, int16 operand_offset,
  4913. char *error_buf, uint32 error_buf_size)
  4914. {
  4915. if (type == VALUE_TYPE_VOID)
  4916. return true;
  4917. /* only check memory overflow in first traverse */
  4918. if (ctx->p_code_compiled == NULL) {
  4919. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4920. return false;
  4921. }
  4922. if (disable_emit)
  4923. *(ctx->frame_offset)++ = operand_offset;
  4924. else {
  4925. emit_operand(ctx, ctx->dynamic_offset);
  4926. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  4927. ctx->dynamic_offset++;
  4928. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4929. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4930. if (ctx->max_dynamic_offset >= INT16_MAX) {
  4931. goto fail;
  4932. }
  4933. }
  4934. }
  4935. if (is_32bit_type(type))
  4936. return true;
  4937. if (ctx->p_code_compiled == NULL) {
  4938. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4939. return false;
  4940. }
  4941. ctx->frame_offset++;
  4942. if (!disable_emit) {
  4943. ctx->dynamic_offset++;
  4944. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4945. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4946. if (ctx->max_dynamic_offset >= INT16_MAX) {
  4947. goto fail;
  4948. }
  4949. }
  4950. }
  4951. return true;
  4952. fail:
  4953. set_error_buf(error_buf, error_buf_size,
  4954. "fast interpreter offset overflow");
  4955. return false;
  4956. }
  4957. /* This function should be in front of wasm_loader_pop_frame_ref
  4958. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  4959. will be modified by wasm_loader_pop_frame_ref */
  4960. static bool
  4961. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4962. char *error_buf, uint32 error_buf_size)
  4963. {
  4964. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  4965. then current block is the function block */
  4966. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  4967. BranchBlock *cur_block = ctx->frame_csp - depth;
  4968. int32 available_stack_cell =
  4969. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4970. /* Directly return success if current block is in stack
  4971. * polymorphic state while stack is empty. */
  4972. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4973. return true;
  4974. if (type == VALUE_TYPE_VOID)
  4975. return true;
  4976. if (is_32bit_type(type)) {
  4977. /* Check the offset stack bottom to ensure the frame offset
  4978. stack will not go underflow. But we don't thrown error
  4979. and return true here, because the error msg should be
  4980. given in wasm_loader_pop_frame_ref */
  4981. if (!check_offset_pop(ctx, 1))
  4982. return true;
  4983. ctx->frame_offset -= 1;
  4984. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4985. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4986. ctx->dynamic_offset -= 1;
  4987. }
  4988. else {
  4989. if (!check_offset_pop(ctx, 2))
  4990. return true;
  4991. ctx->frame_offset -= 2;
  4992. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4993. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4994. ctx->dynamic_offset -= 2;
  4995. }
  4996. emit_operand(ctx, *(ctx->frame_offset));
  4997. return true;
  4998. }
  4999. static bool
  5000. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  5001. uint8 type_push, uint8 type_pop,
  5002. bool disable_emit, int16 operand_offset,
  5003. char *error_buf, uint32 error_buf_size)
  5004. {
  5005. uint8 i;
  5006. for (i = 0; i < pop_cnt; i++) {
  5007. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  5008. error_buf_size))
  5009. return false;
  5010. }
  5011. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  5012. operand_offset, error_buf,
  5013. error_buf_size))
  5014. return false;
  5015. return true;
  5016. }
  5017. static bool
  5018. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  5019. bool disable_emit, int16 operand_offset,
  5020. char *error_buf, uint32 error_buf_size)
  5021. {
  5022. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  5023. error_buf, error_buf_size)))
  5024. return false;
  5025. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  5026. return false;
  5027. return true;
  5028. }
  5029. static bool
  5030. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  5031. char *error_buf, uint32 error_buf_size)
  5032. {
  5033. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  5034. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  5035. return false;
  5036. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  5037. return false;
  5038. return true;
  5039. }
  5040. static bool
  5041. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  5042. uint8 type_push, uint8 type_pop,
  5043. bool disable_emit, int16 operand_offset,
  5044. char *error_buf, uint32 error_buf_size)
  5045. {
  5046. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  5047. disable_emit, operand_offset,
  5048. error_buf, error_buf_size))
  5049. return false;
  5050. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  5051. error_buf, error_buf_size))
  5052. return false;
  5053. return true;
  5054. }
  5055. static bool
  5056. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  5057. int16 *offset, char *error_buf,
  5058. uint32 error_buf_size)
  5059. {
  5060. int8 bytes_to_increase;
  5061. int16 operand_offset = 0;
  5062. Const *c;
  5063. /* Search existing constant */
  5064. for (c = (Const *)ctx->const_buf;
  5065. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  5066. /* TODO: handle v128 type? */
  5067. if ((type == c->value_type)
  5068. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  5069. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  5070. #if WASM_ENABLE_REF_TYPES != 0
  5071. || (type == VALUE_TYPE_FUNCREF
  5072. && *(int32 *)value == c->value.i32)
  5073. || (type == VALUE_TYPE_EXTERNREF
  5074. && *(int32 *)value == c->value.i32)
  5075. #endif
  5076. || (type == VALUE_TYPE_F64
  5077. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  5078. || (type == VALUE_TYPE_F32
  5079. && (0
  5080. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  5081. operand_offset = c->slot_index;
  5082. break;
  5083. }
  5084. if (is_32bit_type(c->value_type))
  5085. operand_offset += 1;
  5086. else
  5087. operand_offset += 2;
  5088. }
  5089. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  5090. /* New constant, append to the const buffer */
  5091. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  5092. bytes_to_increase = 2;
  5093. }
  5094. else {
  5095. bytes_to_increase = 1;
  5096. }
  5097. /* The max cell num of const buffer is 32768 since the valid index range
  5098. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  5099. * full */
  5100. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  5101. *offset = 0;
  5102. return true;
  5103. }
  5104. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  5105. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  5106. ctx->const_buf_size + 4 * sizeof(Const));
  5107. ctx->const_buf_size += 4 * sizeof(Const);
  5108. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  5109. }
  5110. c->value_type = type;
  5111. switch (type) {
  5112. case VALUE_TYPE_F64:
  5113. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  5114. sizeof(float64));
  5115. ctx->const_cell_num += 2;
  5116. /* The const buf will be reversed, we use the second cell */
  5117. /* of the i64/f64 const so the finnal offset is corrent */
  5118. operand_offset++;
  5119. break;
  5120. case VALUE_TYPE_I64:
  5121. c->value.i64 = *(int64 *)value;
  5122. ctx->const_cell_num += 2;
  5123. operand_offset++;
  5124. break;
  5125. case VALUE_TYPE_F32:
  5126. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  5127. sizeof(float32));
  5128. ctx->const_cell_num++;
  5129. break;
  5130. case VALUE_TYPE_I32:
  5131. c->value.i32 = *(int32 *)value;
  5132. ctx->const_cell_num++;
  5133. break;
  5134. #if WASM_ENABLE_REF_TYPES != 0
  5135. case VALUE_TYPE_EXTERNREF:
  5136. case VALUE_TYPE_FUNCREF:
  5137. c->value.i32 = *(int32 *)value;
  5138. ctx->const_cell_num++;
  5139. break;
  5140. #endif
  5141. default:
  5142. break;
  5143. }
  5144. c->slot_index = operand_offset;
  5145. ctx->num_const++;
  5146. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  5147. (int64)c->value.i64);
  5148. }
  5149. /* use negetive index for const */
  5150. operand_offset = -(operand_offset + 1);
  5151. *offset = operand_offset;
  5152. return true;
  5153. fail:
  5154. return false;
  5155. }
  5156. /*
  5157. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  5158. -- Mostly used for the binary / compare operation
  5159. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  5160. -- Mostly used in block / control instructions
  5161. The POP will always emit the offset on the top of the frame_offset stack
  5162. PUSH can be used in two ways:
  5163. 1. directly PUSH:
  5164. PUSH_XXX();
  5165. will allocate a dynamic space and emit
  5166. 2. silent PUSH:
  5167. operand_offset = xxx; disable_emit = true;
  5168. PUSH_XXX();
  5169. only push the frame_offset stack, no emit
  5170. */
  5171. #define TEMPLATE_PUSH(Type) \
  5172. do { \
  5173. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  5174. disable_emit, operand_offset, \
  5175. error_buf, error_buf_size)) \
  5176. goto fail; \
  5177. } while (0)
  5178. #define TEMPLATE_POP(Type) \
  5179. do { \
  5180. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  5181. error_buf, error_buf_size)) \
  5182. goto fail; \
  5183. } while (0)
  5184. #define PUSH_OFFSET_TYPE(type) \
  5185. do { \
  5186. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  5187. operand_offset, error_buf, \
  5188. error_buf_size))) \
  5189. goto fail; \
  5190. } while (0)
  5191. #define POP_OFFSET_TYPE(type) \
  5192. do { \
  5193. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  5194. error_buf_size))) \
  5195. goto fail; \
  5196. } while (0)
  5197. #define POP_AND_PUSH(type_pop, type_push) \
  5198. do { \
  5199. if (!(wasm_loader_push_pop_frame_ref_offset( \
  5200. loader_ctx, 1, type_push, type_pop, disable_emit, \
  5201. operand_offset, error_buf, error_buf_size))) \
  5202. goto fail; \
  5203. } while (0)
  5204. /* type of POPs should be the same */
  5205. #define POP2_AND_PUSH(type_pop, type_push) \
  5206. do { \
  5207. if (!(wasm_loader_push_pop_frame_ref_offset( \
  5208. loader_ctx, 2, type_push, type_pop, disable_emit, \
  5209. operand_offset, error_buf, error_buf_size))) \
  5210. goto fail; \
  5211. } while (0)
  5212. #else /* WASM_ENABLE_FAST_INTERP */
  5213. #define TEMPLATE_PUSH(Type) \
  5214. do { \
  5215. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  5216. error_buf, error_buf_size))) \
  5217. goto fail; \
  5218. } while (0)
  5219. #define TEMPLATE_POP(Type) \
  5220. do { \
  5221. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  5222. error_buf, error_buf_size))) \
  5223. goto fail; \
  5224. } while (0)
  5225. #define POP_AND_PUSH(type_pop, type_push) \
  5226. do { \
  5227. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  5228. type_pop, error_buf, \
  5229. error_buf_size))) \
  5230. goto fail; \
  5231. } while (0)
  5232. /* type of POPs should be the same */
  5233. #define POP2_AND_PUSH(type_pop, type_push) \
  5234. do { \
  5235. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  5236. type_pop, error_buf, \
  5237. error_buf_size))) \
  5238. goto fail; \
  5239. } while (0)
  5240. #endif /* WASM_ENABLE_FAST_INTERP */
  5241. #define PUSH_I32() TEMPLATE_PUSH(I32)
  5242. #define PUSH_F32() TEMPLATE_PUSH(F32)
  5243. #define PUSH_I64() TEMPLATE_PUSH(I64)
  5244. #define PUSH_F64() TEMPLATE_PUSH(F64)
  5245. #define PUSH_V128() TEMPLATE_PUSH(V128)
  5246. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  5247. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  5248. #define POP_I32() TEMPLATE_POP(I32)
  5249. #define POP_F32() TEMPLATE_POP(F32)
  5250. #define POP_I64() TEMPLATE_POP(I64)
  5251. #define POP_F64() TEMPLATE_POP(F64)
  5252. #define POP_V128() TEMPLATE_POP(V128)
  5253. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  5254. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  5255. #if WASM_ENABLE_FAST_INTERP != 0
  5256. static bool
  5257. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  5258. bool disable_emit, char *error_buf, uint32 error_buf_size)
  5259. {
  5260. int16 operand_offset = 0;
  5261. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  5262. : loader_ctx->frame_csp;
  5263. BlockType *block_type = &block->block_type;
  5264. uint8 *return_types = NULL;
  5265. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  5266. int32 i = 0;
  5267. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  5268. *frame_offset_org = NULL;
  5269. return_count = block_type_get_result_types(block_type, &return_types);
  5270. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  5271. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  5272. if (return_count == 1) {
  5273. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  5274. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  5275. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  5276. /* insert op_copy before else opcode */
  5277. if (opcode == WASM_OP_ELSE)
  5278. skip_label();
  5279. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  5280. : EXT_OP_COPY_STACK_TOP_I64);
  5281. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  5282. emit_operand(loader_ctx, block->dynamic_offset);
  5283. if (opcode == WASM_OP_ELSE) {
  5284. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  5285. }
  5286. else {
  5287. loader_ctx->frame_offset -= cell;
  5288. loader_ctx->dynamic_offset = block->dynamic_offset;
  5289. PUSH_OFFSET_TYPE(return_types[0]);
  5290. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5291. }
  5292. if (opcode == WASM_OP_ELSE)
  5293. emit_label(opcode);
  5294. }
  5295. return true;
  5296. }
  5297. /* Copy stack top values to block's results which are in dynamic space.
  5298. * The instruction format:
  5299. * Part a: values count
  5300. * Part b: all values total cell num
  5301. * Part c: each value's cell_num, src offset and dst offset
  5302. * Part d: each value's src offset and dst offset
  5303. * Part e: each value's dst offset
  5304. */
  5305. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  5306. dynamic_offset = dynamic_offset_org =
  5307. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  5308. /* First traversal to get the count of values needed to be copied. */
  5309. for (i = (int32)return_count - 1; i >= 0; i--) {
  5310. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  5311. frame_offset -= cells;
  5312. dynamic_offset -= cells;
  5313. if (dynamic_offset != *frame_offset) {
  5314. value_count++;
  5315. total_cel_num += cells;
  5316. }
  5317. }
  5318. if (value_count) {
  5319. uint32 j = 0;
  5320. uint8 *emit_data = NULL, *cells = NULL;
  5321. int16 *src_offsets = NULL;
  5322. uint16 *dst_offsets = NULL;
  5323. uint64 size =
  5324. (uint64)value_count
  5325. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  5326. /* Allocate memory for the emit data */
  5327. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  5328. return false;
  5329. cells = emit_data;
  5330. src_offsets = (int16 *)(cells + value_count);
  5331. dst_offsets = (uint16 *)(src_offsets + value_count);
  5332. /* insert op_copy before else opcode */
  5333. if (opcode == WASM_OP_ELSE)
  5334. skip_label();
  5335. emit_label(EXT_OP_COPY_STACK_VALUES);
  5336. /* Part a) */
  5337. emit_uint32(loader_ctx, value_count);
  5338. /* Part b) */
  5339. emit_uint32(loader_ctx, total_cel_num);
  5340. /* Second traversal to get each value's cell num, src offset and dst
  5341. * offset. */
  5342. frame_offset = frame_offset_org;
  5343. dynamic_offset = dynamic_offset_org;
  5344. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  5345. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  5346. frame_offset -= cell;
  5347. dynamic_offset -= cell;
  5348. if (dynamic_offset != *frame_offset) {
  5349. /* cell num */
  5350. cells[j] = cell;
  5351. /* src offset */
  5352. src_offsets[j] = *frame_offset;
  5353. /* dst offset */
  5354. dst_offsets[j] = dynamic_offset;
  5355. j++;
  5356. }
  5357. if (opcode == WASM_OP_ELSE) {
  5358. *frame_offset = dynamic_offset;
  5359. }
  5360. else {
  5361. loader_ctx->frame_offset = frame_offset;
  5362. loader_ctx->dynamic_offset = dynamic_offset;
  5363. PUSH_OFFSET_TYPE(return_types[i]);
  5364. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5365. loader_ctx->frame_offset = frame_offset_org;
  5366. loader_ctx->dynamic_offset = dynamic_offset_org;
  5367. }
  5368. }
  5369. bh_assert(j == value_count);
  5370. /* Emit the cells, src_offsets and dst_offsets */
  5371. for (j = 0; j < value_count; j++)
  5372. emit_byte(loader_ctx, cells[j]);
  5373. for (j = 0; j < value_count; j++)
  5374. emit_operand(loader_ctx, src_offsets[j]);
  5375. for (j = 0; j < value_count; j++)
  5376. emit_operand(loader_ctx, dst_offsets[j]);
  5377. if (opcode == WASM_OP_ELSE)
  5378. emit_label(opcode);
  5379. wasm_runtime_free(emit_data);
  5380. }
  5381. return true;
  5382. fail:
  5383. return false;
  5384. }
  5385. #endif /* WASM_ENABLE_FAST_INTERP */
  5386. #define RESERVE_BLOCK_RET() \
  5387. do { \
  5388. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  5389. error_buf_size)) \
  5390. goto fail; \
  5391. } while (0)
  5392. #define PUSH_TYPE(type) \
  5393. do { \
  5394. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  5395. error_buf_size))) \
  5396. goto fail; \
  5397. } while (0)
  5398. #define POP_TYPE(type) \
  5399. do { \
  5400. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  5401. error_buf_size))) \
  5402. goto fail; \
  5403. } while (0)
  5404. #define PUSH_CSP(label_type, block_type, _start_addr) \
  5405. do { \
  5406. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  5407. _start_addr, error_buf, \
  5408. error_buf_size)) \
  5409. goto fail; \
  5410. } while (0)
  5411. #define POP_CSP() \
  5412. do { \
  5413. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  5414. goto fail; \
  5415. } while (0)
  5416. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  5417. do { \
  5418. read_leb_uint32(p, p_end, local_idx); \
  5419. if (local_idx >= param_count + local_count) { \
  5420. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  5421. goto fail; \
  5422. } \
  5423. local_type = local_idx < param_count \
  5424. ? param_types[local_idx] \
  5425. : local_types[local_idx - param_count]; \
  5426. local_offset = local_offsets[local_idx]; \
  5427. } while (0)
  5428. #define CHECK_BR(depth) \
  5429. do { \
  5430. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  5431. error_buf_size)) \
  5432. goto fail; \
  5433. } while (0)
  5434. static bool
  5435. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  5436. {
  5437. if (module->memory_count == 0 && module->import_memory_count == 0) {
  5438. set_error_buf(error_buf, error_buf_size, "unknown memory");
  5439. return false;
  5440. }
  5441. return true;
  5442. }
  5443. #define CHECK_MEMORY() \
  5444. do { \
  5445. if (!check_memory(module, error_buf, error_buf_size)) \
  5446. goto fail; \
  5447. } while (0)
  5448. static bool
  5449. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  5450. uint32 error_buf_size)
  5451. {
  5452. uint8 mem_access_aligns[] = {
  5453. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  5454. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  5455. };
  5456. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  5457. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  5458. set_error_buf(error_buf, error_buf_size,
  5459. "alignment must not be larger than natural");
  5460. return false;
  5461. }
  5462. return true;
  5463. }
  5464. #if WASM_ENABLE_SIMD != 0
  5465. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  5466. static bool
  5467. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  5468. uint32 error_buf_size)
  5469. {
  5470. uint8 mem_access_aligns[] = {
  5471. 4, /* load */
  5472. 3, 3, 3, 3, 3, 3, /* load and extend */
  5473. 0, 1, 2, 3, /* load and splat */
  5474. 4, /* store */
  5475. };
  5476. uint8 mem_access_aligns_load_lane[] = {
  5477. 0, 1, 2, 3, /* load lane */
  5478. 0, 1, 2, 3, /* store lane */
  5479. 2, 3 /* store zero */
  5480. };
  5481. if (!((opcode <= SIMD_v128_store)
  5482. || (SIMD_v128_load8_lane <= opcode
  5483. && opcode <= SIMD_v128_load64_zero))) {
  5484. set_error_buf(error_buf, error_buf_size,
  5485. "the opcode doesn't include memarg");
  5486. return false;
  5487. }
  5488. if ((opcode <= SIMD_v128_store
  5489. && align > mem_access_aligns[opcode - SIMD_v128_load])
  5490. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  5491. && align > mem_access_aligns_load_lane[opcode
  5492. - SIMD_v128_load8_lane])) {
  5493. set_error_buf(error_buf, error_buf_size,
  5494. "alignment must not be larger than natural");
  5495. return false;
  5496. }
  5497. return true;
  5498. }
  5499. static bool
  5500. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  5501. uint32 error_buf_size)
  5502. {
  5503. switch (opcode) {
  5504. case SIMD_i8x16_extract_lane_s:
  5505. case SIMD_i8x16_extract_lane_u:
  5506. case SIMD_i8x16_replace_lane:
  5507. if (lane >= 16) {
  5508. goto fail;
  5509. }
  5510. break;
  5511. case SIMD_i16x8_extract_lane_s:
  5512. case SIMD_i16x8_extract_lane_u:
  5513. case SIMD_i16x8_replace_lane:
  5514. if (lane >= 8) {
  5515. goto fail;
  5516. }
  5517. break;
  5518. case SIMD_i32x4_extract_lane:
  5519. case SIMD_i32x4_replace_lane:
  5520. case SIMD_f32x4_extract_lane:
  5521. case SIMD_f32x4_replace_lane:
  5522. if (lane >= 4) {
  5523. goto fail;
  5524. }
  5525. break;
  5526. case SIMD_i64x2_extract_lane:
  5527. case SIMD_i64x2_replace_lane:
  5528. case SIMD_f64x2_extract_lane:
  5529. case SIMD_f64x2_replace_lane:
  5530. if (lane >= 2) {
  5531. goto fail;
  5532. }
  5533. break;
  5534. case SIMD_v128_load8_lane:
  5535. case SIMD_v128_load16_lane:
  5536. case SIMD_v128_load32_lane:
  5537. case SIMD_v128_load64_lane:
  5538. case SIMD_v128_store8_lane:
  5539. case SIMD_v128_store16_lane:
  5540. case SIMD_v128_store32_lane:
  5541. case SIMD_v128_store64_lane:
  5542. case SIMD_v128_load32_zero:
  5543. case SIMD_v128_load64_zero:
  5544. {
  5545. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  5546. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  5547. goto fail;
  5548. }
  5549. break;
  5550. }
  5551. default:
  5552. goto fail;
  5553. }
  5554. return true;
  5555. fail:
  5556. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  5557. return false;
  5558. }
  5559. static bool
  5560. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  5561. {
  5562. uint8 i;
  5563. for (i = 0; i != 16; ++i) {
  5564. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  5565. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  5566. return false;
  5567. }
  5568. }
  5569. return true;
  5570. }
  5571. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  5572. #endif /* end of WASM_ENABLE_SIMD */
  5573. #if WASM_ENABLE_SHARED_MEMORY != 0
  5574. static bool
  5575. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  5576. uint32 error_buf_size)
  5577. {
  5578. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  5579. uint8 mem_access_aligns[] = {
  5580. 2, 3, 0, 1, 0, 1, 2,
  5581. };
  5582. uint8 expect;
  5583. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  5584. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  5585. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  5586. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  5587. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  5588. }
  5589. else {
  5590. /* 7 opcodes in every group */
  5591. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  5592. }
  5593. if (align != expect) {
  5594. set_error_buf(error_buf, error_buf_size,
  5595. "alignment isn't equal to natural");
  5596. return false;
  5597. }
  5598. return true;
  5599. }
  5600. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  5601. static bool
  5602. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  5603. char *error_buf, uint32 error_buf_size)
  5604. {
  5605. BranchBlock *target_block, *cur_block;
  5606. BlockType *target_block_type;
  5607. uint8 *types = NULL, *frame_ref;
  5608. uint32 arity = 0;
  5609. int32 i, available_stack_cell;
  5610. uint16 cell_num;
  5611. if (loader_ctx->csp_num < depth + 1) {
  5612. set_error_buf(error_buf, error_buf_size,
  5613. "unknown label, "
  5614. "unexpected end of section or function");
  5615. return false;
  5616. }
  5617. cur_block = loader_ctx->frame_csp - 1;
  5618. target_block = loader_ctx->frame_csp - (depth + 1);
  5619. target_block_type = &target_block->block_type;
  5620. frame_ref = loader_ctx->frame_ref;
  5621. /* Note: loop's arity is different from if and block. loop's arity is
  5622. * its parameter count while if and block arity is result count.
  5623. */
  5624. if (target_block->label_type == LABEL_TYPE_LOOP)
  5625. arity = block_type_get_param_types(target_block_type, &types);
  5626. else
  5627. arity = block_type_get_result_types(target_block_type, &types);
  5628. /* If the stack is in polymorphic state, just clear the stack
  5629. * and then re-push the values to make the stack top values
  5630. * match block type. */
  5631. if (cur_block->is_stack_polymorphic) {
  5632. for (i = (int32)arity - 1; i >= 0; i--) {
  5633. #if WASM_ENABLE_FAST_INTERP != 0
  5634. POP_OFFSET_TYPE(types[i]);
  5635. #endif
  5636. POP_TYPE(types[i]);
  5637. }
  5638. for (i = 0; i < (int32)arity; i++) {
  5639. #if WASM_ENABLE_FAST_INTERP != 0
  5640. bool disable_emit = true;
  5641. int16 operand_offset = 0;
  5642. PUSH_OFFSET_TYPE(types[i]);
  5643. #endif
  5644. PUSH_TYPE(types[i]);
  5645. }
  5646. return true;
  5647. }
  5648. available_stack_cell =
  5649. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  5650. /* Check stack top values match target block type */
  5651. for (i = (int32)arity - 1; i >= 0; i--) {
  5652. if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
  5653. error_buf, error_buf_size))
  5654. return false;
  5655. cell_num = wasm_value_type_cell_num(types[i]);
  5656. frame_ref -= cell_num;
  5657. available_stack_cell -= cell_num;
  5658. }
  5659. return true;
  5660. fail:
  5661. return false;
  5662. }
  5663. static BranchBlock *
  5664. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  5665. char *error_buf, uint32 error_buf_size)
  5666. {
  5667. uint8 *p = *p_buf, *p_end = buf_end;
  5668. BranchBlock *frame_csp_tmp;
  5669. uint32 depth;
  5670. read_leb_uint32(p, p_end, depth);
  5671. CHECK_BR(depth);
  5672. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  5673. #if WASM_ENABLE_FAST_INTERP != 0
  5674. emit_br_info(frame_csp_tmp);
  5675. #endif
  5676. *p_buf = p;
  5677. return frame_csp_tmp;
  5678. fail:
  5679. return NULL;
  5680. }
  5681. static bool
  5682. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  5683. char *error_buf, uint32 error_buf_size)
  5684. {
  5685. BlockType *block_type = &block->block_type;
  5686. uint8 *return_types = NULL;
  5687. uint32 return_count = 0;
  5688. int32 available_stack_cell, return_cell_num, i;
  5689. uint8 *frame_ref = NULL;
  5690. available_stack_cell =
  5691. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  5692. return_count = block_type_get_result_types(block_type, &return_types);
  5693. return_cell_num =
  5694. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  5695. /* If the stack is in polymorphic state, just clear the stack
  5696. * and then re-push the values to make the stack top values
  5697. * match block type. */
  5698. if (block->is_stack_polymorphic) {
  5699. for (i = (int32)return_count - 1; i >= 0; i--) {
  5700. #if WASM_ENABLE_FAST_INTERP != 0
  5701. POP_OFFSET_TYPE(return_types[i]);
  5702. #endif
  5703. POP_TYPE(return_types[i]);
  5704. }
  5705. /* Check stack is empty */
  5706. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  5707. set_error_buf(
  5708. error_buf, error_buf_size,
  5709. "type mismatch: stack size does not match block type");
  5710. goto fail;
  5711. }
  5712. for (i = 0; i < (int32)return_count; i++) {
  5713. #if WASM_ENABLE_FAST_INTERP != 0
  5714. bool disable_emit = true;
  5715. int16 operand_offset = 0;
  5716. PUSH_OFFSET_TYPE(return_types[i]);
  5717. #endif
  5718. PUSH_TYPE(return_types[i]);
  5719. }
  5720. return true;
  5721. }
  5722. /* Check stack cell num equals return cell num */
  5723. if (available_stack_cell != return_cell_num) {
  5724. set_error_buf(error_buf, error_buf_size,
  5725. "type mismatch: stack size does not match block type");
  5726. goto fail;
  5727. }
  5728. /* Check stack values match return types */
  5729. frame_ref = loader_ctx->frame_ref;
  5730. for (i = (int32)return_count - 1; i >= 0; i--) {
  5731. if (!check_stack_top_values(frame_ref, available_stack_cell,
  5732. return_types[i], error_buf, error_buf_size))
  5733. return false;
  5734. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  5735. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  5736. }
  5737. return true;
  5738. fail:
  5739. return false;
  5740. }
  5741. #if WASM_ENABLE_FAST_INTERP != 0
  5742. /* Copy parameters to dynamic space.
  5743. * 1) POP original parameter out;
  5744. * 2) Push and copy original values to dynamic space.
  5745. * The copy instruction format:
  5746. * Part a: param count
  5747. * Part b: all param total cell num
  5748. * Part c: each param's cell_num, src offset and dst offset
  5749. * Part d: each param's src offset
  5750. * Part e: each param's dst offset
  5751. */
  5752. static bool
  5753. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  5754. char *error_buf, uint32 error_buf_size)
  5755. {
  5756. int16 *frame_offset = NULL;
  5757. uint8 *cells = NULL, cell;
  5758. int16 *src_offsets = NULL;
  5759. uint8 *emit_data = NULL;
  5760. uint32 i;
  5761. BranchBlock *block = loader_ctx->frame_csp - 1;
  5762. BlockType *block_type = &block->block_type;
  5763. WASMType *wasm_type = block_type->u.type;
  5764. uint32 param_count = block_type->u.type->param_count;
  5765. int16 condition_offset = 0;
  5766. bool disable_emit = false;
  5767. int16 operand_offset = 0;
  5768. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  5769. /* For if block, we also need copy the condition operand offset. */
  5770. if (is_if_block)
  5771. size += sizeof(*cells) + sizeof(*src_offsets);
  5772. /* Allocate memory for the emit data */
  5773. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  5774. return false;
  5775. cells = emit_data;
  5776. src_offsets = (int16 *)(cells + param_count);
  5777. if (is_if_block)
  5778. condition_offset = *loader_ctx->frame_offset;
  5779. /* POP original parameter out */
  5780. for (i = 0; i < param_count; i++) {
  5781. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  5782. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5783. }
  5784. frame_offset = loader_ctx->frame_offset;
  5785. /* Get each param's cell num and src offset */
  5786. for (i = 0; i < param_count; i++) {
  5787. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  5788. cells[i] = cell;
  5789. src_offsets[i] = *frame_offset;
  5790. frame_offset += cell;
  5791. }
  5792. /* emit copy instruction */
  5793. emit_label(EXT_OP_COPY_STACK_VALUES);
  5794. /* Part a) */
  5795. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  5796. /* Part b) */
  5797. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  5798. : wasm_type->param_cell_num);
  5799. /* Part c) */
  5800. for (i = 0; i < param_count; i++)
  5801. emit_byte(loader_ctx, cells[i]);
  5802. if (is_if_block)
  5803. emit_byte(loader_ctx, 1);
  5804. /* Part d) */
  5805. for (i = 0; i < param_count; i++)
  5806. emit_operand(loader_ctx, src_offsets[i]);
  5807. if (is_if_block)
  5808. emit_operand(loader_ctx, condition_offset);
  5809. /* Part e) */
  5810. /* Push to dynamic space. The push will emit the dst offset. */
  5811. for (i = 0; i < param_count; i++)
  5812. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  5813. if (is_if_block)
  5814. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  5815. /* Free the emit data */
  5816. wasm_runtime_free(emit_data);
  5817. return true;
  5818. fail:
  5819. return false;
  5820. }
  5821. #endif
  5822. /* reset the stack to the state of before entering the last block */
  5823. #if WASM_ENABLE_FAST_INTERP != 0
  5824. #define RESET_STACK() \
  5825. do { \
  5826. loader_ctx->stack_cell_num = \
  5827. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  5828. loader_ctx->frame_ref = \
  5829. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  5830. loader_ctx->frame_offset = \
  5831. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  5832. } while (0)
  5833. #else
  5834. #define RESET_STACK() \
  5835. do { \
  5836. loader_ctx->stack_cell_num = \
  5837. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  5838. loader_ctx->frame_ref = \
  5839. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  5840. } while (0)
  5841. #endif
  5842. /* set current block's stack polymorphic state */
  5843. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  5844. do { \
  5845. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  5846. _cur_block->is_stack_polymorphic = flag; \
  5847. } while (0)
  5848. #define BLOCK_HAS_PARAM(block_type) \
  5849. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  5850. #define PRESERVE_LOCAL_FOR_BLOCK() \
  5851. do { \
  5852. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  5853. error_buf_size))) { \
  5854. goto fail; \
  5855. } \
  5856. } while (0)
  5857. #if WASM_ENABLE_REF_TYPES != 0
  5858. static bool
  5859. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  5860. uint8 *p_elem_type, char *error_buf, uint32 error_buf_size)
  5861. {
  5862. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  5863. return false;
  5864. }
  5865. if (p_elem_type) {
  5866. if (table_idx < module->import_table_count)
  5867. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  5868. else
  5869. *p_elem_type =
  5870. module->tables[module->import_table_count + table_idx]
  5871. .elem_type;
  5872. }
  5873. return true;
  5874. }
  5875. static bool
  5876. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  5877. uint8 *p_elem_type, char *error_buf,
  5878. uint32 error_buf_size)
  5879. {
  5880. if (table_seg_idx >= module->table_seg_count) {
  5881. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  5882. table_seg_idx);
  5883. return false;
  5884. }
  5885. if (p_elem_type) {
  5886. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  5887. }
  5888. return true;
  5889. }
  5890. #endif
  5891. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  5892. const uint8 *
  5893. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  5894. uint32 *len)
  5895. {
  5896. WASMCustomSection *section = module->custom_section_list;
  5897. while (section) {
  5898. if ((section->name_len == strlen(name))
  5899. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  5900. if (len) {
  5901. *len = section->content_len;
  5902. }
  5903. return section->content_addr;
  5904. }
  5905. section = section->next;
  5906. }
  5907. return false;
  5908. }
  5909. #endif
  5910. static bool
  5911. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5912. uint32 cur_func_idx, char *error_buf,
  5913. uint32 error_buf_size)
  5914. {
  5915. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  5916. uint32 param_count, local_count, global_count;
  5917. uint8 *param_types, *local_types, local_type, global_type;
  5918. BlockType func_block_type;
  5919. uint16 *local_offsets, local_offset;
  5920. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  5921. uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
  5922. int32 i32_const = 0;
  5923. int64 i64_const;
  5924. uint8 opcode;
  5925. bool return_value = false;
  5926. WASMLoaderContext *loader_ctx;
  5927. BranchBlock *frame_csp_tmp;
  5928. #if WASM_ENABLE_FAST_INTERP != 0
  5929. uint8 *func_const_end, *func_const = NULL;
  5930. int16 operand_offset = 0;
  5931. uint8 last_op = 0;
  5932. bool disable_emit, preserve_local = false;
  5933. float32 f32_const;
  5934. float64 f64_const;
  5935. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  5936. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  5937. #endif
  5938. global_count = module->import_global_count + module->global_count;
  5939. param_count = func->func_type->param_count;
  5940. param_types = func->func_type->types;
  5941. func_block_type.is_value_type = false;
  5942. func_block_type.u.type = func->func_type;
  5943. local_count = func->local_count;
  5944. local_types = func->local_types;
  5945. local_offsets = func->local_offsets;
  5946. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  5947. goto fail;
  5948. }
  5949. #if WASM_ENABLE_FAST_INTERP != 0
  5950. /* For the first traverse, the initial value of preserved_local_offset has
  5951. * not been determined, we use the INT16_MAX to represent that a slot has
  5952. * been copied to preserve space. For second traverse, this field will be
  5953. * set to the appropriate value in wasm_loader_ctx_reinit.
  5954. * This is for Issue #1230,
  5955. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  5956. * drop opcodes need to know which slots are preserved, so those slots will
  5957. * not be treated as dynamically allocated slots */
  5958. loader_ctx->preserved_local_offset = INT16_MAX;
  5959. re_scan:
  5960. if (loader_ctx->code_compiled_size > 0) {
  5961. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  5962. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  5963. goto fail;
  5964. }
  5965. p = func->code;
  5966. func->code_compiled = loader_ctx->p_code_compiled;
  5967. func->code_compiled_size = loader_ctx->code_compiled_size;
  5968. }
  5969. #endif
  5970. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  5971. while (p < p_end) {
  5972. opcode = *p++;
  5973. #if WASM_ENABLE_FAST_INTERP != 0
  5974. p_org = p;
  5975. disable_emit = false;
  5976. emit_label(opcode);
  5977. #endif
  5978. switch (opcode) {
  5979. case WASM_OP_UNREACHABLE:
  5980. RESET_STACK();
  5981. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5982. break;
  5983. case WASM_OP_NOP:
  5984. #if WASM_ENABLE_FAST_INTERP != 0
  5985. skip_label();
  5986. #endif
  5987. break;
  5988. case WASM_OP_IF:
  5989. #if WASM_ENABLE_FAST_INTERP != 0
  5990. PRESERVE_LOCAL_FOR_BLOCK();
  5991. #endif
  5992. POP_I32();
  5993. goto handle_op_block_and_loop;
  5994. case WASM_OP_BLOCK:
  5995. case WASM_OP_LOOP:
  5996. #if WASM_ENABLE_FAST_INTERP != 0
  5997. PRESERVE_LOCAL_FOR_BLOCK();
  5998. #endif
  5999. handle_op_block_and_loop:
  6000. {
  6001. uint8 value_type;
  6002. BlockType block_type;
  6003. p_org = p - 1;
  6004. value_type = read_uint8(p);
  6005. if (is_byte_a_type(value_type)) {
  6006. /* If the first byte is one of these special values:
  6007. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  6008. * the single return value. */
  6009. block_type.is_value_type = true;
  6010. block_type.u.value_type = value_type;
  6011. }
  6012. else {
  6013. uint32 type_index;
  6014. /* Resolve the leb128 encoded type index as block type */
  6015. p--;
  6016. read_leb_uint32(p, p_end, type_index);
  6017. if (type_index >= module->type_count) {
  6018. set_error_buf(error_buf, error_buf_size,
  6019. "unknown type");
  6020. goto fail;
  6021. }
  6022. block_type.is_value_type = false;
  6023. block_type.u.type = module->types[type_index];
  6024. #if WASM_ENABLE_FAST_INTERP == 0 && WASM_ENABLE_WAMR_COMPILER == 0 \
  6025. && WASM_ENABLE_JIT == 0
  6026. /* If block use type index as block type, change the opcode
  6027. * to new extended opcode so that interpreter can resolve
  6028. * the block quickly.
  6029. */
  6030. #if WASM_ENABLE_DEBUG_INTERP != 0
  6031. if (!record_fast_op(module, p_org, *p_org, error_buf,
  6032. error_buf_size)) {
  6033. goto fail;
  6034. }
  6035. #endif
  6036. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  6037. #endif
  6038. }
  6039. /* Pop block parameters from stack */
  6040. if (BLOCK_HAS_PARAM(block_type)) {
  6041. WASMType *wasm_type = block_type.u.type;
  6042. for (i = 0; i < block_type.u.type->param_count; i++)
  6043. POP_TYPE(
  6044. wasm_type->types[wasm_type->param_count - i - 1]);
  6045. }
  6046. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  6047. block_type, p);
  6048. /* Pass parameters to block */
  6049. if (BLOCK_HAS_PARAM(block_type)) {
  6050. for (i = 0; i < block_type.u.type->param_count; i++)
  6051. PUSH_TYPE(block_type.u.type->types[i]);
  6052. }
  6053. #if WASM_ENABLE_FAST_INTERP != 0
  6054. if (opcode == WASM_OP_BLOCK) {
  6055. skip_label();
  6056. }
  6057. else if (opcode == WASM_OP_LOOP) {
  6058. skip_label();
  6059. if (BLOCK_HAS_PARAM(block_type)) {
  6060. /* Make sure params are in dynamic space */
  6061. if (!copy_params_to_dynamic_space(
  6062. loader_ctx, false, error_buf, error_buf_size))
  6063. goto fail;
  6064. }
  6065. (loader_ctx->frame_csp - 1)->code_compiled =
  6066. loader_ctx->p_code_compiled;
  6067. }
  6068. else if (opcode == WASM_OP_IF) {
  6069. /* If block has parameters, we should make sure they are in
  6070. * dynamic space. Otherwise, when else branch is missing,
  6071. * the later opcode may consume incorrect operand offset.
  6072. * Spec case:
  6073. * (func (export "params-id") (param i32) (result i32)
  6074. * (i32.const 1)
  6075. * (i32.const 2)
  6076. * (if (param i32 i32) (result i32 i32) (local.get 0)
  6077. * (then)) (i32.add)
  6078. * )
  6079. *
  6080. * So we should emit a copy instruction before the if.
  6081. *
  6082. * And we also need to save the parameter offsets and
  6083. * recover them before entering else branch.
  6084. *
  6085. */
  6086. if (BLOCK_HAS_PARAM(block_type)) {
  6087. BranchBlock *block = loader_ctx->frame_csp - 1;
  6088. uint64 size;
  6089. /* skip the if condition operand offset */
  6090. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  6091. /* skip the if label */
  6092. skip_label();
  6093. /* Emit a copy instruction */
  6094. if (!copy_params_to_dynamic_space(
  6095. loader_ctx, true, error_buf, error_buf_size))
  6096. goto fail;
  6097. /* Emit the if instruction */
  6098. emit_label(opcode);
  6099. /* Emit the new condition operand offset */
  6100. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  6101. /* Save top param_count values of frame_offset stack, so
  6102. * that we can recover it before executing else branch
  6103. */
  6104. size = sizeof(int16)
  6105. * (uint64)block_type.u.type->param_cell_num;
  6106. if (!(block->param_frame_offsets = loader_malloc(
  6107. size, error_buf, error_buf_size)))
  6108. goto fail;
  6109. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  6110. loader_ctx->frame_offset
  6111. - size / sizeof(int16),
  6112. (uint32)size);
  6113. }
  6114. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  6115. emit_empty_label_addr_and_frame_ip(PATCH_END);
  6116. }
  6117. #endif
  6118. break;
  6119. }
  6120. case WASM_OP_ELSE:
  6121. {
  6122. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  6123. if (loader_ctx->csp_num < 2
  6124. || (loader_ctx->frame_csp - 1)->label_type
  6125. != LABEL_TYPE_IF) {
  6126. set_error_buf(
  6127. error_buf, error_buf_size,
  6128. "opcode else found without matched opcode if");
  6129. goto fail;
  6130. }
  6131. /* check whether if branch's stack matches its result type */
  6132. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  6133. error_buf, error_buf_size))
  6134. goto fail;
  6135. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  6136. #if WASM_ENABLE_FAST_INTERP != 0
  6137. /* if the result of if branch is in local or const area, add a
  6138. * copy op */
  6139. RESERVE_BLOCK_RET();
  6140. emit_empty_label_addr_and_frame_ip(PATCH_END);
  6141. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  6142. #endif
  6143. RESET_STACK();
  6144. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  6145. /* Pass parameters to if-false branch */
  6146. if (BLOCK_HAS_PARAM(block_type)) {
  6147. for (i = 0; i < block_type.u.type->param_count; i++)
  6148. PUSH_TYPE(block_type.u.type->types[i]);
  6149. }
  6150. #if WASM_ENABLE_FAST_INTERP != 0
  6151. /* Recover top param_count values of frame_offset stack */
  6152. if (BLOCK_HAS_PARAM((block_type))) {
  6153. uint32 size;
  6154. BranchBlock *block = loader_ctx->frame_csp - 1;
  6155. size = sizeof(int16) * block_type.u.type->param_cell_num;
  6156. bh_memcpy_s(loader_ctx->frame_offset, size,
  6157. block->param_frame_offsets, size);
  6158. loader_ctx->frame_offset += (size / sizeof(int16));
  6159. }
  6160. #endif
  6161. break;
  6162. }
  6163. case WASM_OP_END:
  6164. {
  6165. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6166. /* check whether block stack matches its result type */
  6167. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  6168. error_buf_size))
  6169. goto fail;
  6170. /* if no else branch, and return types do not match param types,
  6171. * fail */
  6172. if (cur_block->label_type == LABEL_TYPE_IF
  6173. && !cur_block->else_addr) {
  6174. uint32 block_param_count = 0, block_ret_count = 0;
  6175. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  6176. BlockType *cur_block_type = &cur_block->block_type;
  6177. if (cur_block_type->is_value_type) {
  6178. if (cur_block_type->u.value_type != VALUE_TYPE_VOID) {
  6179. block_ret_count = 1;
  6180. block_ret_types = &cur_block_type->u.value_type;
  6181. }
  6182. }
  6183. else {
  6184. block_param_count = cur_block_type->u.type->param_count;
  6185. block_ret_count = cur_block_type->u.type->result_count;
  6186. block_param_types = cur_block_type->u.type->types;
  6187. block_ret_types =
  6188. cur_block_type->u.type->types + block_param_count;
  6189. }
  6190. if (block_param_count != block_ret_count
  6191. || (block_param_count
  6192. && memcmp(block_param_types, block_ret_types,
  6193. block_param_count))) {
  6194. set_error_buf(error_buf, error_buf_size,
  6195. "type mismatch: else branch missing");
  6196. goto fail;
  6197. }
  6198. }
  6199. POP_CSP();
  6200. #if WASM_ENABLE_FAST_INTERP != 0
  6201. skip_label();
  6202. /* copy the result to the block return address */
  6203. RESERVE_BLOCK_RET();
  6204. apply_label_patch(loader_ctx, 0, PATCH_END);
  6205. free_label_patch_list(loader_ctx->frame_csp);
  6206. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  6207. int32 idx;
  6208. uint8 ret_type;
  6209. emit_label(WASM_OP_RETURN);
  6210. for (idx = (int32)func->func_type->result_count - 1;
  6211. idx >= 0; idx--) {
  6212. ret_type = *(func->func_type->types
  6213. + func->func_type->param_count + idx);
  6214. POP_OFFSET_TYPE(ret_type);
  6215. }
  6216. }
  6217. #endif
  6218. if (loader_ctx->csp_num > 0) {
  6219. loader_ctx->frame_csp->end_addr = p - 1;
  6220. }
  6221. else {
  6222. /* end of function block, function will return */
  6223. if (p < p_end) {
  6224. set_error_buf(error_buf, error_buf_size,
  6225. "section size mismatch");
  6226. goto fail;
  6227. }
  6228. }
  6229. break;
  6230. }
  6231. case WASM_OP_BR:
  6232. {
  6233. if (!(frame_csp_tmp = check_branch_block(
  6234. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  6235. goto fail;
  6236. RESET_STACK();
  6237. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6238. break;
  6239. }
  6240. case WASM_OP_BR_IF:
  6241. {
  6242. POP_I32();
  6243. if (!(frame_csp_tmp = check_branch_block(
  6244. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  6245. goto fail;
  6246. break;
  6247. }
  6248. case WASM_OP_BR_TABLE:
  6249. {
  6250. uint8 *ret_types = NULL;
  6251. uint32 ret_count = 0;
  6252. #if WASM_ENABLE_FAST_INTERP == 0
  6253. uint8 *p_depth_begin, *p_depth;
  6254. uint32 depth, j;
  6255. BrTableCache *br_table_cache = NULL;
  6256. p_org = p - 1;
  6257. #endif
  6258. read_leb_uint32(p, p_end, count);
  6259. #if WASM_ENABLE_FAST_INTERP != 0
  6260. emit_uint32(loader_ctx, count);
  6261. #endif
  6262. POP_I32();
  6263. #if WASM_ENABLE_FAST_INTERP == 0
  6264. p_depth_begin = p_depth = p;
  6265. #endif
  6266. for (i = 0; i <= count; i++) {
  6267. if (!(frame_csp_tmp =
  6268. check_branch_block(loader_ctx, &p, p_end,
  6269. error_buf, error_buf_size)))
  6270. goto fail;
  6271. if (i == 0) {
  6272. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  6273. ret_count = block_type_get_result_types(
  6274. &frame_csp_tmp->block_type, &ret_types);
  6275. }
  6276. else {
  6277. uint8 *tmp_ret_types = NULL;
  6278. uint32 tmp_ret_count = 0;
  6279. /* Check whether all table items have the same return
  6280. * type */
  6281. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  6282. tmp_ret_count = block_type_get_result_types(
  6283. &frame_csp_tmp->block_type, &tmp_ret_types);
  6284. if (ret_count != tmp_ret_count
  6285. || (ret_count
  6286. && 0
  6287. != memcmp(ret_types, tmp_ret_types,
  6288. ret_count))) {
  6289. set_error_buf(
  6290. error_buf, error_buf_size,
  6291. "type mismatch: br_table targets must "
  6292. "all use same result type");
  6293. goto fail;
  6294. }
  6295. }
  6296. #if WASM_ENABLE_FAST_INTERP == 0
  6297. depth = (uint32)(loader_ctx->frame_csp - 1 - frame_csp_tmp);
  6298. if (br_table_cache) {
  6299. br_table_cache->br_depths[i] = depth;
  6300. }
  6301. else {
  6302. if (depth > 255) {
  6303. /* The depth cannot be stored in one byte,
  6304. create br_table cache to store each depth */
  6305. #if WASM_ENABLE_DEBUG_INTERP != 0
  6306. if (!record_fast_op(module, p_org, *p_org,
  6307. error_buf, error_buf_size)) {
  6308. goto fail;
  6309. }
  6310. #endif
  6311. if (!(br_table_cache = loader_malloc(
  6312. offsetof(BrTableCache, br_depths)
  6313. + sizeof(uint32)
  6314. * (uint64)(count + 1),
  6315. error_buf, error_buf_size))) {
  6316. goto fail;
  6317. }
  6318. *p_org = EXT_OP_BR_TABLE_CACHE;
  6319. br_table_cache->br_table_op_addr = p_org;
  6320. br_table_cache->br_count = count;
  6321. /* Copy previous depths which are one byte */
  6322. for (j = 0; j < i; j++) {
  6323. br_table_cache->br_depths[j] = p_depth_begin[j];
  6324. }
  6325. br_table_cache->br_depths[i] = depth;
  6326. bh_list_insert(module->br_table_cache_list,
  6327. br_table_cache);
  6328. }
  6329. else {
  6330. /* The depth can be stored in one byte, use the
  6331. byte of the leb to store it */
  6332. *p_depth++ = (uint8)depth;
  6333. }
  6334. }
  6335. #endif
  6336. }
  6337. #if WASM_ENABLE_FAST_INTERP == 0
  6338. /* Set the tailing bytes to nop */
  6339. if (br_table_cache)
  6340. p_depth = p_depth_begin;
  6341. while (p_depth < p)
  6342. *p_depth++ = WASM_OP_NOP;
  6343. #endif
  6344. RESET_STACK();
  6345. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6346. break;
  6347. }
  6348. case WASM_OP_RETURN:
  6349. {
  6350. int32 idx;
  6351. uint8 ret_type;
  6352. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  6353. idx--) {
  6354. ret_type = *(func->func_type->types
  6355. + func->func_type->param_count + idx);
  6356. POP_TYPE(ret_type);
  6357. #if WASM_ENABLE_FAST_INTERP != 0
  6358. /* emit the offset after return opcode */
  6359. POP_OFFSET_TYPE(ret_type);
  6360. #endif
  6361. }
  6362. RESET_STACK();
  6363. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6364. break;
  6365. }
  6366. case WASM_OP_CALL:
  6367. #if WASM_ENABLE_TAIL_CALL != 0
  6368. case WASM_OP_RETURN_CALL:
  6369. #endif
  6370. {
  6371. WASMType *func_type;
  6372. int32 idx;
  6373. read_leb_uint32(p, p_end, func_idx);
  6374. #if WASM_ENABLE_FAST_INTERP != 0
  6375. /* we need to emit func_idx before arguments */
  6376. emit_uint32(loader_ctx, func_idx);
  6377. #endif
  6378. if (!check_function_index(module, func_idx, error_buf,
  6379. error_buf_size)) {
  6380. goto fail;
  6381. }
  6382. if (func_idx < module->import_function_count)
  6383. func_type =
  6384. module->import_functions[func_idx].u.function.func_type;
  6385. else
  6386. func_type = module
  6387. ->functions[func_idx
  6388. - module->import_function_count]
  6389. ->func_type;
  6390. if (func_type->param_count > 0) {
  6391. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  6392. idx--) {
  6393. POP_TYPE(func_type->types[idx]);
  6394. #if WASM_ENABLE_FAST_INTERP != 0
  6395. POP_OFFSET_TYPE(func_type->types[idx]);
  6396. #endif
  6397. }
  6398. }
  6399. #if WASM_ENABLE_TAIL_CALL != 0
  6400. if (opcode == WASM_OP_CALL) {
  6401. #endif
  6402. for (i = 0; i < func_type->result_count; i++) {
  6403. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  6404. #if WASM_ENABLE_FAST_INTERP != 0
  6405. /* Here we emit each return value's dynamic_offset. But
  6406. * in fact these offsets are continuous, so interpreter
  6407. * only need to get the first return value's offset.
  6408. */
  6409. PUSH_OFFSET_TYPE(
  6410. func_type->types[func_type->param_count + i]);
  6411. #endif
  6412. }
  6413. #if WASM_ENABLE_TAIL_CALL != 0
  6414. }
  6415. else {
  6416. uint8 type;
  6417. if (func_type->result_count
  6418. != func->func_type->result_count) {
  6419. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  6420. "type mismatch: expect ",
  6421. func->func_type->result_count,
  6422. " return values but got other");
  6423. goto fail;
  6424. }
  6425. for (i = 0; i < func_type->result_count; i++) {
  6426. type = func->func_type
  6427. ->types[func->func_type->param_count + i];
  6428. if (func_type->types[func_type->param_count + i]
  6429. != type) {
  6430. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6431. "type mismatch: expect ",
  6432. type2str(type), " but got other");
  6433. goto fail;
  6434. }
  6435. }
  6436. RESET_STACK();
  6437. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6438. }
  6439. #endif
  6440. func->has_op_func_call = true;
  6441. break;
  6442. }
  6443. /*
  6444. * if disable reference type: call_indirect typeidx, 0x00
  6445. * if enable reference type: call_indirect typeidx, tableidx
  6446. */
  6447. case WASM_OP_CALL_INDIRECT:
  6448. #if WASM_ENABLE_TAIL_CALL != 0
  6449. case WASM_OP_RETURN_CALL_INDIRECT:
  6450. #endif
  6451. {
  6452. int32 idx;
  6453. WASMType *func_type;
  6454. read_leb_uint32(p, p_end, type_idx);
  6455. #if WASM_ENABLE_REF_TYPES != 0
  6456. read_leb_uint32(p, p_end, table_idx);
  6457. #else
  6458. CHECK_BUF(p, p_end, 1);
  6459. table_idx = read_uint8(p);
  6460. #endif
  6461. if (!check_table_index(module, table_idx, error_buf,
  6462. error_buf_size)) {
  6463. goto fail;
  6464. }
  6465. #if WASM_ENABLE_FAST_INTERP != 0
  6466. /* we need to emit before arguments */
  6467. #if WASM_ENABLE_TAIL_CALL != 0
  6468. emit_byte(loader_ctx, opcode);
  6469. #endif
  6470. emit_uint32(loader_ctx, type_idx);
  6471. emit_uint32(loader_ctx, table_idx);
  6472. #endif
  6473. /* skip elem idx */
  6474. POP_I32();
  6475. if (type_idx >= module->type_count) {
  6476. set_error_buf(error_buf, error_buf_size, "unknown type");
  6477. goto fail;
  6478. }
  6479. func_type = module->types[type_idx];
  6480. if (func_type->param_count > 0) {
  6481. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  6482. idx--) {
  6483. POP_TYPE(func_type->types[idx]);
  6484. #if WASM_ENABLE_FAST_INTERP != 0
  6485. POP_OFFSET_TYPE(func_type->types[idx]);
  6486. #endif
  6487. }
  6488. }
  6489. #if WASM_ENABLE_TAIL_CALL != 0
  6490. if (opcode == WASM_OP_CALL_INDIRECT) {
  6491. #endif
  6492. for (i = 0; i < func_type->result_count; i++) {
  6493. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  6494. #if WASM_ENABLE_FAST_INTERP != 0
  6495. PUSH_OFFSET_TYPE(
  6496. func_type->types[func_type->param_count + i]);
  6497. #endif
  6498. }
  6499. #if WASM_ENABLE_TAIL_CALL != 0
  6500. }
  6501. else {
  6502. uint8 type;
  6503. if (func_type->result_count
  6504. != func->func_type->result_count) {
  6505. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  6506. "type mismatch: expect ",
  6507. func->func_type->result_count,
  6508. " return values but got other");
  6509. goto fail;
  6510. }
  6511. for (i = 0; i < func_type->result_count; i++) {
  6512. type = func->func_type
  6513. ->types[func->func_type->param_count + i];
  6514. if (func_type->types[func_type->param_count + i]
  6515. != type) {
  6516. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6517. "type mismatch: expect ",
  6518. type2str(type), " but got other");
  6519. goto fail;
  6520. }
  6521. }
  6522. RESET_STACK();
  6523. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6524. }
  6525. #endif
  6526. func->has_op_func_call = true;
  6527. break;
  6528. }
  6529. case WASM_OP_DROP:
  6530. case WASM_OP_DROP_64:
  6531. {
  6532. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6533. int32 available_stack_cell =
  6534. (int32)(loader_ctx->stack_cell_num
  6535. - cur_block->stack_cell_num);
  6536. if (available_stack_cell <= 0
  6537. && !cur_block->is_stack_polymorphic) {
  6538. set_error_buf(error_buf, error_buf_size,
  6539. "type mismatch, opcode drop was found "
  6540. "but stack was empty");
  6541. goto fail;
  6542. }
  6543. if (available_stack_cell > 0) {
  6544. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  6545. loader_ctx->frame_ref--;
  6546. loader_ctx->stack_cell_num--;
  6547. #if WASM_ENABLE_FAST_INTERP != 0
  6548. skip_label();
  6549. loader_ctx->frame_offset--;
  6550. if ((*(loader_ctx->frame_offset)
  6551. > loader_ctx->start_dynamic_offset)
  6552. && (*(loader_ctx->frame_offset)
  6553. < loader_ctx->max_dynamic_offset))
  6554. loader_ctx->dynamic_offset--;
  6555. #endif
  6556. }
  6557. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  6558. loader_ctx->frame_ref -= 2;
  6559. loader_ctx->stack_cell_num -= 2;
  6560. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  6561. *(p - 1) = WASM_OP_DROP_64;
  6562. #endif
  6563. #if WASM_ENABLE_FAST_INTERP != 0
  6564. skip_label();
  6565. loader_ctx->frame_offset -= 2;
  6566. if ((*(loader_ctx->frame_offset)
  6567. > loader_ctx->start_dynamic_offset)
  6568. && (*(loader_ctx->frame_offset)
  6569. < loader_ctx->max_dynamic_offset))
  6570. loader_ctx->dynamic_offset -= 2;
  6571. #endif
  6572. }
  6573. #if WASM_ENABLE_SIMD != 0
  6574. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6575. else if (*(loader_ctx->frame_ref - 1) == REF_V128_1) {
  6576. loader_ctx->frame_ref -= 4;
  6577. loader_ctx->stack_cell_num -= 4;
  6578. }
  6579. #endif
  6580. #endif
  6581. else {
  6582. set_error_buf(error_buf, error_buf_size,
  6583. "type mismatch");
  6584. goto fail;
  6585. }
  6586. }
  6587. else {
  6588. #if WASM_ENABLE_FAST_INTERP != 0
  6589. skip_label();
  6590. #endif
  6591. }
  6592. break;
  6593. }
  6594. case WASM_OP_SELECT:
  6595. case WASM_OP_SELECT_64:
  6596. {
  6597. uint8 ref_type;
  6598. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6599. int32 available_stack_cell;
  6600. POP_I32();
  6601. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  6602. - cur_block->stack_cell_num);
  6603. if (available_stack_cell <= 0
  6604. && !cur_block->is_stack_polymorphic) {
  6605. set_error_buf(error_buf, error_buf_size,
  6606. "type mismatch or invalid result arity, "
  6607. "opcode select was found "
  6608. "but stack was empty");
  6609. goto fail;
  6610. }
  6611. if (available_stack_cell > 0) {
  6612. switch (*(loader_ctx->frame_ref - 1)) {
  6613. case REF_I32:
  6614. case REF_F32:
  6615. break;
  6616. case REF_I64_2:
  6617. case REF_F64_2:
  6618. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  6619. *(p - 1) = WASM_OP_SELECT_64;
  6620. #endif
  6621. #if WASM_ENABLE_FAST_INTERP != 0
  6622. if (loader_ctx->p_code_compiled) {
  6623. uint8 opcode_tmp = WASM_OP_SELECT_64;
  6624. uint8 *p_code_compiled_tmp =
  6625. loader_ctx->p_code_compiled - 2;
  6626. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6627. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6628. *(void **)(p_code_compiled_tmp
  6629. - sizeof(void *)) =
  6630. handle_table[opcode_tmp];
  6631. #else
  6632. int32 offset =
  6633. (int32)((uint8 *)handle_table[opcode_tmp]
  6634. - (uint8 *)handle_table[0]);
  6635. if (!(offset >= INT16_MIN
  6636. && offset < INT16_MAX)) {
  6637. set_error_buf(error_buf, error_buf_size,
  6638. "pre-compiled label offset "
  6639. "out of range");
  6640. goto fail;
  6641. }
  6642. *(int16 *)(p_code_compiled_tmp
  6643. - sizeof(int16)) = (int16)offset;
  6644. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6645. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6646. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6647. *(p_code_compiled_tmp - 1) = opcode_tmp;
  6648. #else
  6649. *(p_code_compiled_tmp - 2) = opcode_tmp;
  6650. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6651. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6652. }
  6653. #endif /* end of WASM_ENABLE_FAST_INTERP */
  6654. break;
  6655. #if WASM_ENABLE_SIMD != 0
  6656. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6657. case REF_V128_4:
  6658. break;
  6659. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6660. #endif /* WASM_ENABLE_SIMD != 0 */
  6661. default:
  6662. {
  6663. set_error_buf(error_buf, error_buf_size,
  6664. "type mismatch");
  6665. goto fail;
  6666. }
  6667. }
  6668. ref_type = *(loader_ctx->frame_ref - 1);
  6669. #if WASM_ENABLE_FAST_INTERP != 0
  6670. POP_OFFSET_TYPE(ref_type);
  6671. POP_TYPE(ref_type);
  6672. POP_OFFSET_TYPE(ref_type);
  6673. POP_TYPE(ref_type);
  6674. PUSH_OFFSET_TYPE(ref_type);
  6675. PUSH_TYPE(ref_type);
  6676. #else
  6677. POP2_AND_PUSH(ref_type, ref_type);
  6678. #endif
  6679. }
  6680. else {
  6681. #if WASM_ENABLE_FAST_INTERP != 0
  6682. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  6683. #endif
  6684. PUSH_TYPE(VALUE_TYPE_ANY);
  6685. }
  6686. break;
  6687. }
  6688. #if WASM_ENABLE_REF_TYPES != 0
  6689. case WASM_OP_SELECT_T:
  6690. {
  6691. uint8 vec_len, ref_type;
  6692. read_leb_uint32(p, p_end, vec_len);
  6693. if (!vec_len) {
  6694. set_error_buf(error_buf, error_buf_size,
  6695. "invalid result arity");
  6696. goto fail;
  6697. }
  6698. CHECK_BUF(p, p_end, 1);
  6699. ref_type = read_uint8(p);
  6700. if (!is_value_type(ref_type)) {
  6701. set_error_buf(error_buf, error_buf_size,
  6702. "unknown value type");
  6703. goto fail;
  6704. }
  6705. POP_I32();
  6706. #if WASM_ENABLE_FAST_INTERP != 0
  6707. if (loader_ctx->p_code_compiled) {
  6708. uint8 opcode_tmp = WASM_OP_SELECT;
  6709. uint8 *p_code_compiled_tmp =
  6710. loader_ctx->p_code_compiled - 2;
  6711. if (ref_type == VALUE_TYPE_V128) {
  6712. #if (WASM_ENABLE_SIMD == 0) \
  6713. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  6714. set_error_buf(error_buf, error_buf_size,
  6715. "SIMD v128 type isn't supported");
  6716. goto fail;
  6717. #endif
  6718. }
  6719. else {
  6720. if (ref_type == VALUE_TYPE_F64
  6721. || ref_type == VALUE_TYPE_I64)
  6722. opcode_tmp = WASM_OP_SELECT_64;
  6723. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6724. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6725. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  6726. handle_table[opcode_tmp];
  6727. #else
  6728. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  6729. - (uint8 *)handle_table[0]);
  6730. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  6731. set_error_buf(
  6732. error_buf, error_buf_size,
  6733. "pre-compiled label offset out of range");
  6734. goto fail;
  6735. }
  6736. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  6737. (int16)offset;
  6738. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6739. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  6740. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  6741. *(p_code_compiled_tmp - 1) = opcode_tmp;
  6742. #else
  6743. *(p_code_compiled_tmp - 2) = opcode_tmp;
  6744. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  6745. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  6746. }
  6747. }
  6748. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  6749. #if WASM_ENABLE_FAST_INTERP != 0
  6750. POP_OFFSET_TYPE(ref_type);
  6751. POP_TYPE(ref_type);
  6752. POP_OFFSET_TYPE(ref_type);
  6753. POP_TYPE(ref_type);
  6754. PUSH_OFFSET_TYPE(ref_type);
  6755. PUSH_TYPE(ref_type);
  6756. #else
  6757. POP2_AND_PUSH(ref_type, ref_type);
  6758. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  6759. (void)vec_len;
  6760. break;
  6761. }
  6762. /* table.get x. tables[x]. [i32] -> [t] */
  6763. /* table.set x. tables[x]. [i32 t] -> [] */
  6764. case WASM_OP_TABLE_GET:
  6765. case WASM_OP_TABLE_SET:
  6766. {
  6767. uint8 decl_ref_type;
  6768. read_leb_uint32(p, p_end, table_idx);
  6769. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  6770. error_buf, error_buf_size))
  6771. goto fail;
  6772. #if WASM_ENABLE_FAST_INTERP != 0
  6773. emit_uint32(loader_ctx, table_idx);
  6774. #endif
  6775. if (opcode == WASM_OP_TABLE_GET) {
  6776. POP_I32();
  6777. #if WASM_ENABLE_FAST_INTERP != 0
  6778. PUSH_OFFSET_TYPE(decl_ref_type);
  6779. #endif
  6780. PUSH_TYPE(decl_ref_type);
  6781. }
  6782. else {
  6783. #if WASM_ENABLE_FAST_INTERP != 0
  6784. POP_OFFSET_TYPE(decl_ref_type);
  6785. #endif
  6786. POP_TYPE(decl_ref_type);
  6787. POP_I32();
  6788. }
  6789. break;
  6790. }
  6791. case WASM_OP_REF_NULL:
  6792. {
  6793. uint8 ref_type;
  6794. CHECK_BUF(p, p_end, 1);
  6795. ref_type = read_uint8(p);
  6796. if (ref_type != VALUE_TYPE_FUNCREF
  6797. && ref_type != VALUE_TYPE_EXTERNREF) {
  6798. set_error_buf(error_buf, error_buf_size,
  6799. "unknown value type");
  6800. goto fail;
  6801. }
  6802. #if WASM_ENABLE_FAST_INTERP != 0
  6803. PUSH_OFFSET_TYPE(ref_type);
  6804. #endif
  6805. PUSH_TYPE(ref_type);
  6806. break;
  6807. }
  6808. case WASM_OP_REF_IS_NULL:
  6809. {
  6810. #if WASM_ENABLE_FAST_INTERP != 0
  6811. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  6812. VALUE_TYPE_FUNCREF,
  6813. error_buf, error_buf_size)
  6814. && !wasm_loader_pop_frame_ref_offset(
  6815. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  6816. error_buf_size)) {
  6817. goto fail;
  6818. }
  6819. #else
  6820. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  6821. error_buf, error_buf_size)
  6822. && !wasm_loader_pop_frame_ref(loader_ctx,
  6823. VALUE_TYPE_EXTERNREF,
  6824. error_buf, error_buf_size)) {
  6825. goto fail;
  6826. }
  6827. #endif
  6828. PUSH_I32();
  6829. break;
  6830. }
  6831. case WASM_OP_REF_FUNC:
  6832. {
  6833. read_leb_uint32(p, p_end, func_idx);
  6834. if (!check_function_index(module, func_idx, error_buf,
  6835. error_buf_size)) {
  6836. goto fail;
  6837. }
  6838. if (func_idx == cur_func_idx + module->import_function_count) {
  6839. WASMTableSeg *table_seg = module->table_segments;
  6840. bool func_declared = false;
  6841. uint32 j;
  6842. /* Check whether current function is declared */
  6843. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  6844. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  6845. && wasm_elem_is_declarative(table_seg->mode)) {
  6846. for (j = 0; j < table_seg->function_count; j++) {
  6847. if (table_seg->func_indexes[j] == func_idx) {
  6848. func_declared = true;
  6849. break;
  6850. }
  6851. }
  6852. }
  6853. }
  6854. if (!func_declared) {
  6855. set_error_buf(error_buf, error_buf_size,
  6856. "undeclared function reference");
  6857. goto fail;
  6858. }
  6859. }
  6860. #if WASM_ENABLE_FAST_INTERP != 0
  6861. emit_uint32(loader_ctx, func_idx);
  6862. #endif
  6863. PUSH_FUNCREF();
  6864. break;
  6865. }
  6866. #endif /* WASM_ENABLE_REF_TYPES */
  6867. case WASM_OP_GET_LOCAL:
  6868. {
  6869. p_org = p - 1;
  6870. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6871. PUSH_TYPE(local_type);
  6872. #if WASM_ENABLE_FAST_INTERP != 0
  6873. /* Get Local is optimized out */
  6874. skip_label();
  6875. disable_emit = true;
  6876. operand_offset = local_offset;
  6877. PUSH_OFFSET_TYPE(local_type);
  6878. #else
  6879. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  6880. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  6881. if (local_offset < 0x80) {
  6882. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  6883. if (is_32bit_type(local_type)) {
  6884. *p_org++ = (uint8)local_offset;
  6885. }
  6886. else {
  6887. *p_org++ = (uint8)(local_offset | 0x80);
  6888. }
  6889. while (p_org < p) {
  6890. *p_org++ = WASM_OP_NOP;
  6891. }
  6892. }
  6893. #endif
  6894. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  6895. break;
  6896. }
  6897. case WASM_OP_SET_LOCAL:
  6898. {
  6899. p_org = p - 1;
  6900. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6901. POP_TYPE(local_type);
  6902. #if WASM_ENABLE_FAST_INTERP != 0
  6903. if (!(preserve_referenced_local(
  6904. loader_ctx, opcode, local_offset, local_type,
  6905. &preserve_local, error_buf, error_buf_size)))
  6906. goto fail;
  6907. if (local_offset < 256) {
  6908. skip_label();
  6909. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  6910. if (loader_ctx->p_code_compiled)
  6911. STORE_U16(loader_ctx->p_code_compiled - 2,
  6912. local_offset);
  6913. loader_ctx->frame_offset--;
  6914. loader_ctx->dynamic_offset--;
  6915. }
  6916. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  6917. if (loader_ctx->p_code_compiled)
  6918. STORE_U16(loader_ctx->p_code_compiled - 2,
  6919. local_offset);
  6920. loader_ctx->frame_offset -= 2;
  6921. loader_ctx->dynamic_offset -= 2;
  6922. }
  6923. else {
  6924. if (is_32bit_type(local_type)) {
  6925. emit_label(EXT_OP_SET_LOCAL_FAST);
  6926. emit_byte(loader_ctx, (uint8)local_offset);
  6927. }
  6928. else {
  6929. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  6930. emit_byte(loader_ctx, (uint8)local_offset);
  6931. }
  6932. POP_OFFSET_TYPE(local_type);
  6933. }
  6934. }
  6935. else { /* local index larger than 255, reserve leb */
  6936. emit_uint32(loader_ctx, local_idx);
  6937. POP_OFFSET_TYPE(local_type);
  6938. }
  6939. #else
  6940. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  6941. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  6942. if (local_offset < 0x80) {
  6943. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  6944. if (is_32bit_type(local_type)) {
  6945. *p_org++ = (uint8)local_offset;
  6946. }
  6947. else {
  6948. *p_org++ = (uint8)(local_offset | 0x80);
  6949. }
  6950. while (p_org < p) {
  6951. *p_org++ = WASM_OP_NOP;
  6952. }
  6953. }
  6954. #endif
  6955. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  6956. break;
  6957. }
  6958. case WASM_OP_TEE_LOCAL:
  6959. {
  6960. p_org = p - 1;
  6961. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  6962. #if WASM_ENABLE_FAST_INTERP != 0
  6963. /* If the stack is in polymorphic state, do fake pop and push on
  6964. offset stack to keep the depth of offset stack to be the
  6965. same with ref stack */
  6966. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6967. if (cur_block->is_stack_polymorphic) {
  6968. POP_OFFSET_TYPE(local_type);
  6969. PUSH_OFFSET_TYPE(local_type);
  6970. }
  6971. #endif
  6972. POP_TYPE(local_type);
  6973. PUSH_TYPE(local_type);
  6974. #if WASM_ENABLE_FAST_INTERP != 0
  6975. if (!(preserve_referenced_local(
  6976. loader_ctx, opcode, local_offset, local_type,
  6977. &preserve_local, error_buf, error_buf_size)))
  6978. goto fail;
  6979. if (local_offset < 256) {
  6980. skip_label();
  6981. if (is_32bit_type(local_type)) {
  6982. emit_label(EXT_OP_TEE_LOCAL_FAST);
  6983. emit_byte(loader_ctx, (uint8)local_offset);
  6984. }
  6985. else {
  6986. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  6987. emit_byte(loader_ctx, (uint8)local_offset);
  6988. }
  6989. }
  6990. else { /* local index larger than 255, reserve leb */
  6991. emit_uint32(loader_ctx, local_idx);
  6992. }
  6993. emit_operand(loader_ctx,
  6994. *(loader_ctx->frame_offset
  6995. - wasm_value_type_cell_num(local_type)));
  6996. #else
  6997. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  6998. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  6999. if (local_offset < 0x80) {
  7000. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  7001. if (is_32bit_type(local_type)) {
  7002. *p_org++ = (uint8)local_offset;
  7003. }
  7004. else {
  7005. *p_org++ = (uint8)(local_offset | 0x80);
  7006. }
  7007. while (p_org < p) {
  7008. *p_org++ = WASM_OP_NOP;
  7009. }
  7010. }
  7011. #endif
  7012. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  7013. break;
  7014. }
  7015. case WASM_OP_GET_GLOBAL:
  7016. {
  7017. p_org = p - 1;
  7018. read_leb_uint32(p, p_end, global_idx);
  7019. if (global_idx >= global_count) {
  7020. set_error_buf(error_buf, error_buf_size, "unknown global");
  7021. goto fail;
  7022. }
  7023. global_type =
  7024. global_idx < module->import_global_count
  7025. ? module->import_globals[global_idx].u.global.type
  7026. : module
  7027. ->globals[global_idx
  7028. - module->import_global_count]
  7029. .type;
  7030. PUSH_TYPE(global_type);
  7031. #if WASM_ENABLE_FAST_INTERP == 0
  7032. if (global_type == VALUE_TYPE_I64
  7033. || global_type == VALUE_TYPE_F64) {
  7034. #if WASM_ENABLE_DEBUG_INTERP != 0
  7035. if (!record_fast_op(module, p_org, *p_org, error_buf,
  7036. error_buf_size)) {
  7037. goto fail;
  7038. }
  7039. #endif
  7040. *p_org = WASM_OP_GET_GLOBAL_64;
  7041. }
  7042. #else /* else of WASM_ENABLE_FAST_INTERP */
  7043. if (global_type == VALUE_TYPE_I64
  7044. || global_type == VALUE_TYPE_F64) {
  7045. skip_label();
  7046. emit_label(WASM_OP_GET_GLOBAL_64);
  7047. }
  7048. emit_uint32(loader_ctx, global_idx);
  7049. PUSH_OFFSET_TYPE(global_type);
  7050. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7051. break;
  7052. }
  7053. case WASM_OP_SET_GLOBAL:
  7054. {
  7055. bool is_mutable = false;
  7056. p_org = p - 1;
  7057. read_leb_uint32(p, p_end, global_idx);
  7058. if (global_idx >= global_count) {
  7059. set_error_buf(error_buf, error_buf_size, "unknown global");
  7060. goto fail;
  7061. }
  7062. is_mutable =
  7063. global_idx < module->import_global_count
  7064. ? module->import_globals[global_idx].u.global.is_mutable
  7065. : module
  7066. ->globals[global_idx
  7067. - module->import_global_count]
  7068. .is_mutable;
  7069. if (!is_mutable) {
  7070. set_error_buf(error_buf, error_buf_size,
  7071. "global is immutable");
  7072. goto fail;
  7073. }
  7074. global_type =
  7075. global_idx < module->import_global_count
  7076. ? module->import_globals[global_idx].u.global.type
  7077. : module
  7078. ->globals[global_idx
  7079. - module->import_global_count]
  7080. .type;
  7081. POP_TYPE(global_type);
  7082. #if WASM_ENABLE_FAST_INTERP == 0
  7083. if (global_type == VALUE_TYPE_I64
  7084. || global_type == VALUE_TYPE_F64) {
  7085. #if WASM_ENABLE_DEBUG_INTERP != 0
  7086. if (!record_fast_op(module, p_org, *p_org, error_buf,
  7087. error_buf_size)) {
  7088. goto fail;
  7089. }
  7090. #endif
  7091. *p_org = WASM_OP_SET_GLOBAL_64;
  7092. }
  7093. else if (module->aux_stack_size > 0
  7094. && global_idx == module->aux_stack_top_global_index) {
  7095. #if WASM_ENABLE_DEBUG_INTERP != 0
  7096. if (!record_fast_op(module, p_org, *p_org, error_buf,
  7097. error_buf_size)) {
  7098. goto fail;
  7099. }
  7100. #endif
  7101. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  7102. }
  7103. #else /* else of WASM_ENABLE_FAST_INTERP */
  7104. if (global_type == VALUE_TYPE_I64
  7105. || global_type == VALUE_TYPE_F64) {
  7106. skip_label();
  7107. emit_label(WASM_OP_SET_GLOBAL_64);
  7108. }
  7109. else if (module->aux_stack_size > 0
  7110. && global_idx == module->aux_stack_top_global_index) {
  7111. skip_label();
  7112. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  7113. }
  7114. emit_uint32(loader_ctx, global_idx);
  7115. POP_OFFSET_TYPE(global_type);
  7116. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7117. break;
  7118. }
  7119. /* load */
  7120. case WASM_OP_I32_LOAD:
  7121. case WASM_OP_I32_LOAD8_S:
  7122. case WASM_OP_I32_LOAD8_U:
  7123. case WASM_OP_I32_LOAD16_S:
  7124. case WASM_OP_I32_LOAD16_U:
  7125. case WASM_OP_I64_LOAD:
  7126. case WASM_OP_I64_LOAD8_S:
  7127. case WASM_OP_I64_LOAD8_U:
  7128. case WASM_OP_I64_LOAD16_S:
  7129. case WASM_OP_I64_LOAD16_U:
  7130. case WASM_OP_I64_LOAD32_S:
  7131. case WASM_OP_I64_LOAD32_U:
  7132. case WASM_OP_F32_LOAD:
  7133. case WASM_OP_F64_LOAD:
  7134. /* store */
  7135. case WASM_OP_I32_STORE:
  7136. case WASM_OP_I32_STORE8:
  7137. case WASM_OP_I32_STORE16:
  7138. case WASM_OP_I64_STORE:
  7139. case WASM_OP_I64_STORE8:
  7140. case WASM_OP_I64_STORE16:
  7141. case WASM_OP_I64_STORE32:
  7142. case WASM_OP_F32_STORE:
  7143. case WASM_OP_F64_STORE:
  7144. {
  7145. #if WASM_ENABLE_FAST_INTERP != 0
  7146. /* change F32/F64 into I32/I64 */
  7147. if (opcode == WASM_OP_F32_LOAD) {
  7148. skip_label();
  7149. emit_label(WASM_OP_I32_LOAD);
  7150. }
  7151. else if (opcode == WASM_OP_F64_LOAD) {
  7152. skip_label();
  7153. emit_label(WASM_OP_I64_LOAD);
  7154. }
  7155. else if (opcode == WASM_OP_F32_STORE) {
  7156. skip_label();
  7157. emit_label(WASM_OP_I32_STORE);
  7158. }
  7159. else if (opcode == WASM_OP_F64_STORE) {
  7160. skip_label();
  7161. emit_label(WASM_OP_I64_STORE);
  7162. }
  7163. #endif
  7164. CHECK_MEMORY();
  7165. read_leb_uint32(p, p_end, align); /* align */
  7166. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7167. if (!check_memory_access_align(opcode, align, error_buf,
  7168. error_buf_size)) {
  7169. goto fail;
  7170. }
  7171. #if WASM_ENABLE_FAST_INTERP != 0
  7172. emit_uint32(loader_ctx, mem_offset);
  7173. #endif
  7174. switch (opcode) {
  7175. /* load */
  7176. case WASM_OP_I32_LOAD:
  7177. case WASM_OP_I32_LOAD8_S:
  7178. case WASM_OP_I32_LOAD8_U:
  7179. case WASM_OP_I32_LOAD16_S:
  7180. case WASM_OP_I32_LOAD16_U:
  7181. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7182. break;
  7183. case WASM_OP_I64_LOAD:
  7184. case WASM_OP_I64_LOAD8_S:
  7185. case WASM_OP_I64_LOAD8_U:
  7186. case WASM_OP_I64_LOAD16_S:
  7187. case WASM_OP_I64_LOAD16_U:
  7188. case WASM_OP_I64_LOAD32_S:
  7189. case WASM_OP_I64_LOAD32_U:
  7190. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  7191. break;
  7192. case WASM_OP_F32_LOAD:
  7193. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7194. break;
  7195. case WASM_OP_F64_LOAD:
  7196. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  7197. break;
  7198. /* store */
  7199. case WASM_OP_I32_STORE:
  7200. case WASM_OP_I32_STORE8:
  7201. case WASM_OP_I32_STORE16:
  7202. POP_I32();
  7203. POP_I32();
  7204. break;
  7205. case WASM_OP_I64_STORE:
  7206. case WASM_OP_I64_STORE8:
  7207. case WASM_OP_I64_STORE16:
  7208. case WASM_OP_I64_STORE32:
  7209. POP_I64();
  7210. POP_I32();
  7211. break;
  7212. case WASM_OP_F32_STORE:
  7213. POP_F32();
  7214. POP_I32();
  7215. break;
  7216. case WASM_OP_F64_STORE:
  7217. POP_F64();
  7218. POP_I32();
  7219. break;
  7220. default:
  7221. break;
  7222. }
  7223. break;
  7224. }
  7225. case WASM_OP_MEMORY_SIZE:
  7226. CHECK_MEMORY();
  7227. /* reserved byte 0x00 */
  7228. if (*p++ != 0x00) {
  7229. set_error_buf(error_buf, error_buf_size,
  7230. "zero byte expected");
  7231. goto fail;
  7232. }
  7233. PUSH_I32();
  7234. module->possible_memory_grow = true;
  7235. break;
  7236. case WASM_OP_MEMORY_GROW:
  7237. CHECK_MEMORY();
  7238. /* reserved byte 0x00 */
  7239. if (*p++ != 0x00) {
  7240. set_error_buf(error_buf, error_buf_size,
  7241. "zero byte expected");
  7242. goto fail;
  7243. }
  7244. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7245. func->has_op_memory_grow = true;
  7246. module->possible_memory_grow = true;
  7247. break;
  7248. case WASM_OP_I32_CONST:
  7249. read_leb_int32(p, p_end, i32_const);
  7250. #if WASM_ENABLE_FAST_INTERP != 0
  7251. skip_label();
  7252. disable_emit = true;
  7253. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  7254. if (operand_offset == 0) {
  7255. disable_emit = false;
  7256. emit_label(WASM_OP_I32_CONST);
  7257. emit_uint32(loader_ctx, i32_const);
  7258. }
  7259. #else
  7260. (void)i32_const;
  7261. #endif
  7262. PUSH_I32();
  7263. break;
  7264. case WASM_OP_I64_CONST:
  7265. read_leb_int64(p, p_end, i64_const);
  7266. #if WASM_ENABLE_FAST_INTERP != 0
  7267. skip_label();
  7268. disable_emit = true;
  7269. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  7270. if (operand_offset == 0) {
  7271. disable_emit = false;
  7272. emit_label(WASM_OP_I64_CONST);
  7273. emit_uint64(loader_ctx, i64_const);
  7274. }
  7275. #endif
  7276. PUSH_I64();
  7277. break;
  7278. case WASM_OP_F32_CONST:
  7279. p += sizeof(float32);
  7280. #if WASM_ENABLE_FAST_INTERP != 0
  7281. skip_label();
  7282. disable_emit = true;
  7283. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  7284. sizeof(float32));
  7285. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  7286. if (operand_offset == 0) {
  7287. disable_emit = false;
  7288. emit_label(WASM_OP_F32_CONST);
  7289. emit_float32(loader_ctx, f32_const);
  7290. }
  7291. #endif
  7292. PUSH_F32();
  7293. break;
  7294. case WASM_OP_F64_CONST:
  7295. p += sizeof(float64);
  7296. #if WASM_ENABLE_FAST_INTERP != 0
  7297. skip_label();
  7298. disable_emit = true;
  7299. /* Some MCU may require 8-byte align */
  7300. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  7301. sizeof(float64));
  7302. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  7303. if (operand_offset == 0) {
  7304. disable_emit = false;
  7305. emit_label(WASM_OP_F64_CONST);
  7306. emit_float64(loader_ctx, f64_const);
  7307. }
  7308. #endif
  7309. PUSH_F64();
  7310. break;
  7311. case WASM_OP_I32_EQZ:
  7312. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7313. break;
  7314. case WASM_OP_I32_EQ:
  7315. case WASM_OP_I32_NE:
  7316. case WASM_OP_I32_LT_S:
  7317. case WASM_OP_I32_LT_U:
  7318. case WASM_OP_I32_GT_S:
  7319. case WASM_OP_I32_GT_U:
  7320. case WASM_OP_I32_LE_S:
  7321. case WASM_OP_I32_LE_U:
  7322. case WASM_OP_I32_GE_S:
  7323. case WASM_OP_I32_GE_U:
  7324. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7325. break;
  7326. case WASM_OP_I64_EQZ:
  7327. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7328. break;
  7329. case WASM_OP_I64_EQ:
  7330. case WASM_OP_I64_NE:
  7331. case WASM_OP_I64_LT_S:
  7332. case WASM_OP_I64_LT_U:
  7333. case WASM_OP_I64_GT_S:
  7334. case WASM_OP_I64_GT_U:
  7335. case WASM_OP_I64_LE_S:
  7336. case WASM_OP_I64_LE_U:
  7337. case WASM_OP_I64_GE_S:
  7338. case WASM_OP_I64_GE_U:
  7339. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7340. break;
  7341. case WASM_OP_F32_EQ:
  7342. case WASM_OP_F32_NE:
  7343. case WASM_OP_F32_LT:
  7344. case WASM_OP_F32_GT:
  7345. case WASM_OP_F32_LE:
  7346. case WASM_OP_F32_GE:
  7347. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7348. break;
  7349. case WASM_OP_F64_EQ:
  7350. case WASM_OP_F64_NE:
  7351. case WASM_OP_F64_LT:
  7352. case WASM_OP_F64_GT:
  7353. case WASM_OP_F64_LE:
  7354. case WASM_OP_F64_GE:
  7355. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7356. break;
  7357. case WASM_OP_I32_CLZ:
  7358. case WASM_OP_I32_CTZ:
  7359. case WASM_OP_I32_POPCNT:
  7360. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7361. break;
  7362. case WASM_OP_I32_ADD:
  7363. case WASM_OP_I32_SUB:
  7364. case WASM_OP_I32_MUL:
  7365. case WASM_OP_I32_DIV_S:
  7366. case WASM_OP_I32_DIV_U:
  7367. case WASM_OP_I32_REM_S:
  7368. case WASM_OP_I32_REM_U:
  7369. case WASM_OP_I32_AND:
  7370. case WASM_OP_I32_OR:
  7371. case WASM_OP_I32_XOR:
  7372. case WASM_OP_I32_SHL:
  7373. case WASM_OP_I32_SHR_S:
  7374. case WASM_OP_I32_SHR_U:
  7375. case WASM_OP_I32_ROTL:
  7376. case WASM_OP_I32_ROTR:
  7377. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7378. break;
  7379. case WASM_OP_I64_CLZ:
  7380. case WASM_OP_I64_CTZ:
  7381. case WASM_OP_I64_POPCNT:
  7382. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7383. break;
  7384. case WASM_OP_I64_ADD:
  7385. case WASM_OP_I64_SUB:
  7386. case WASM_OP_I64_MUL:
  7387. case WASM_OP_I64_DIV_S:
  7388. case WASM_OP_I64_DIV_U:
  7389. case WASM_OP_I64_REM_S:
  7390. case WASM_OP_I64_REM_U:
  7391. case WASM_OP_I64_AND:
  7392. case WASM_OP_I64_OR:
  7393. case WASM_OP_I64_XOR:
  7394. case WASM_OP_I64_SHL:
  7395. case WASM_OP_I64_SHR_S:
  7396. case WASM_OP_I64_SHR_U:
  7397. case WASM_OP_I64_ROTL:
  7398. case WASM_OP_I64_ROTR:
  7399. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7400. break;
  7401. case WASM_OP_F32_ABS:
  7402. case WASM_OP_F32_NEG:
  7403. case WASM_OP_F32_CEIL:
  7404. case WASM_OP_F32_FLOOR:
  7405. case WASM_OP_F32_TRUNC:
  7406. case WASM_OP_F32_NEAREST:
  7407. case WASM_OP_F32_SQRT:
  7408. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  7409. break;
  7410. case WASM_OP_F32_ADD:
  7411. case WASM_OP_F32_SUB:
  7412. case WASM_OP_F32_MUL:
  7413. case WASM_OP_F32_DIV:
  7414. case WASM_OP_F32_MIN:
  7415. case WASM_OP_F32_MAX:
  7416. case WASM_OP_F32_COPYSIGN:
  7417. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  7418. break;
  7419. case WASM_OP_F64_ABS:
  7420. case WASM_OP_F64_NEG:
  7421. case WASM_OP_F64_CEIL:
  7422. case WASM_OP_F64_FLOOR:
  7423. case WASM_OP_F64_TRUNC:
  7424. case WASM_OP_F64_NEAREST:
  7425. case WASM_OP_F64_SQRT:
  7426. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  7427. break;
  7428. case WASM_OP_F64_ADD:
  7429. case WASM_OP_F64_SUB:
  7430. case WASM_OP_F64_MUL:
  7431. case WASM_OP_F64_DIV:
  7432. case WASM_OP_F64_MIN:
  7433. case WASM_OP_F64_MAX:
  7434. case WASM_OP_F64_COPYSIGN:
  7435. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  7436. break;
  7437. case WASM_OP_I32_WRAP_I64:
  7438. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7439. break;
  7440. case WASM_OP_I32_TRUNC_S_F32:
  7441. case WASM_OP_I32_TRUNC_U_F32:
  7442. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7443. break;
  7444. case WASM_OP_I32_TRUNC_S_F64:
  7445. case WASM_OP_I32_TRUNC_U_F64:
  7446. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7447. break;
  7448. case WASM_OP_I64_EXTEND_S_I32:
  7449. case WASM_OP_I64_EXTEND_U_I32:
  7450. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  7451. break;
  7452. case WASM_OP_I64_TRUNC_S_F32:
  7453. case WASM_OP_I64_TRUNC_U_F32:
  7454. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  7455. break;
  7456. case WASM_OP_I64_TRUNC_S_F64:
  7457. case WASM_OP_I64_TRUNC_U_F64:
  7458. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7459. break;
  7460. case WASM_OP_F32_CONVERT_S_I32:
  7461. case WASM_OP_F32_CONVERT_U_I32:
  7462. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7463. break;
  7464. case WASM_OP_F32_CONVERT_S_I64:
  7465. case WASM_OP_F32_CONVERT_U_I64:
  7466. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  7467. break;
  7468. case WASM_OP_F32_DEMOTE_F64:
  7469. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  7470. break;
  7471. case WASM_OP_F64_CONVERT_S_I32:
  7472. case WASM_OP_F64_CONVERT_U_I32:
  7473. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  7474. break;
  7475. case WASM_OP_F64_CONVERT_S_I64:
  7476. case WASM_OP_F64_CONVERT_U_I64:
  7477. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  7478. break;
  7479. case WASM_OP_F64_PROMOTE_F32:
  7480. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  7481. break;
  7482. case WASM_OP_I32_REINTERPRET_F32:
  7483. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7484. break;
  7485. case WASM_OP_I64_REINTERPRET_F64:
  7486. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7487. break;
  7488. case WASM_OP_F32_REINTERPRET_I32:
  7489. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7490. break;
  7491. case WASM_OP_F64_REINTERPRET_I64:
  7492. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  7493. break;
  7494. case WASM_OP_I32_EXTEND8_S:
  7495. case WASM_OP_I32_EXTEND16_S:
  7496. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7497. break;
  7498. case WASM_OP_I64_EXTEND8_S:
  7499. case WASM_OP_I64_EXTEND16_S:
  7500. case WASM_OP_I64_EXTEND32_S:
  7501. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7502. break;
  7503. case WASM_OP_MISC_PREFIX:
  7504. {
  7505. uint32 opcode1;
  7506. read_leb_uint32(p, p_end, opcode1);
  7507. #if WASM_ENABLE_FAST_INTERP != 0
  7508. emit_byte(loader_ctx, ((uint8)opcode1));
  7509. #endif
  7510. switch (opcode1) {
  7511. case WASM_OP_I32_TRUNC_SAT_S_F32:
  7512. case WASM_OP_I32_TRUNC_SAT_U_F32:
  7513. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7514. break;
  7515. case WASM_OP_I32_TRUNC_SAT_S_F64:
  7516. case WASM_OP_I32_TRUNC_SAT_U_F64:
  7517. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7518. break;
  7519. case WASM_OP_I64_TRUNC_SAT_S_F32:
  7520. case WASM_OP_I64_TRUNC_SAT_U_F32:
  7521. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  7522. break;
  7523. case WASM_OP_I64_TRUNC_SAT_S_F64:
  7524. case WASM_OP_I64_TRUNC_SAT_U_F64:
  7525. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  7526. break;
  7527. #if WASM_ENABLE_BULK_MEMORY != 0
  7528. case WASM_OP_MEMORY_INIT:
  7529. {
  7530. read_leb_uint32(p, p_end, data_seg_idx);
  7531. #if WASM_ENABLE_FAST_INTERP != 0
  7532. emit_uint32(loader_ctx, data_seg_idx);
  7533. #endif
  7534. if (module->import_memory_count == 0
  7535. && module->memory_count == 0)
  7536. goto fail_unknown_memory;
  7537. if (*p++ != 0x00)
  7538. goto fail_zero_byte_expected;
  7539. if (data_seg_idx >= module->data_seg_count) {
  7540. set_error_buf_v(error_buf, error_buf_size,
  7541. "unknown data segment %d",
  7542. data_seg_idx);
  7543. goto fail;
  7544. }
  7545. if (module->data_seg_count1 == 0)
  7546. goto fail_data_cnt_sec_require;
  7547. POP_I32();
  7548. POP_I32();
  7549. POP_I32();
  7550. break;
  7551. }
  7552. case WASM_OP_DATA_DROP:
  7553. {
  7554. read_leb_uint32(p, p_end, data_seg_idx);
  7555. #if WASM_ENABLE_FAST_INTERP != 0
  7556. emit_uint32(loader_ctx, data_seg_idx);
  7557. #endif
  7558. if (data_seg_idx >= module->data_seg_count) {
  7559. set_error_buf(error_buf, error_buf_size,
  7560. "unknown data segment");
  7561. goto fail;
  7562. }
  7563. if (module->data_seg_count1 == 0)
  7564. goto fail_data_cnt_sec_require;
  7565. break;
  7566. }
  7567. case WASM_OP_MEMORY_COPY:
  7568. {
  7569. /* both src and dst memory index should be 0 */
  7570. if (*(int16 *)p != 0x0000)
  7571. goto fail_zero_byte_expected;
  7572. p += 2;
  7573. if (module->import_memory_count == 0
  7574. && module->memory_count == 0)
  7575. goto fail_unknown_memory;
  7576. POP_I32();
  7577. POP_I32();
  7578. POP_I32();
  7579. break;
  7580. }
  7581. case WASM_OP_MEMORY_FILL:
  7582. {
  7583. if (*p++ != 0x00) {
  7584. goto fail_zero_byte_expected;
  7585. }
  7586. if (module->import_memory_count == 0
  7587. && module->memory_count == 0) {
  7588. goto fail_unknown_memory;
  7589. }
  7590. POP_I32();
  7591. POP_I32();
  7592. POP_I32();
  7593. break;
  7594. fail_zero_byte_expected:
  7595. set_error_buf(error_buf, error_buf_size,
  7596. "zero byte expected");
  7597. goto fail;
  7598. fail_unknown_memory:
  7599. set_error_buf(error_buf, error_buf_size,
  7600. "unknown memory 0");
  7601. goto fail;
  7602. fail_data_cnt_sec_require:
  7603. set_error_buf(error_buf, error_buf_size,
  7604. "data count section required");
  7605. goto fail;
  7606. }
  7607. #endif /* WASM_ENABLE_BULK_MEMORY */
  7608. #if WASM_ENABLE_REF_TYPES != 0
  7609. case WASM_OP_TABLE_INIT:
  7610. {
  7611. uint8 seg_ref_type = 0, tbl_ref_type = 0;
  7612. read_leb_uint32(p, p_end, table_seg_idx);
  7613. read_leb_uint32(p, p_end, table_idx);
  7614. if (!get_table_elem_type(module, table_idx,
  7615. &tbl_ref_type, error_buf,
  7616. error_buf_size))
  7617. goto fail;
  7618. if (!get_table_seg_elem_type(module, table_seg_idx,
  7619. &seg_ref_type, error_buf,
  7620. error_buf_size))
  7621. goto fail;
  7622. if (seg_ref_type != tbl_ref_type) {
  7623. set_error_buf(error_buf, error_buf_size,
  7624. "type mismatch");
  7625. goto fail;
  7626. }
  7627. #if WASM_ENABLE_FAST_INTERP != 0
  7628. emit_uint32(loader_ctx, table_seg_idx);
  7629. emit_uint32(loader_ctx, table_idx);
  7630. #endif
  7631. POP_I32();
  7632. POP_I32();
  7633. POP_I32();
  7634. break;
  7635. }
  7636. case WASM_OP_ELEM_DROP:
  7637. {
  7638. read_leb_uint32(p, p_end, table_seg_idx);
  7639. if (!get_table_seg_elem_type(module, table_seg_idx,
  7640. NULL, error_buf,
  7641. error_buf_size))
  7642. goto fail;
  7643. #if WASM_ENABLE_FAST_INTERP != 0
  7644. emit_uint32(loader_ctx, table_seg_idx);
  7645. #endif
  7646. break;
  7647. }
  7648. case WASM_OP_TABLE_COPY:
  7649. {
  7650. uint8 src_ref_type, dst_ref_type;
  7651. uint32 src_tbl_idx, dst_tbl_idx;
  7652. read_leb_uint32(p, p_end, src_tbl_idx);
  7653. if (!get_table_elem_type(module, src_tbl_idx,
  7654. &src_ref_type, error_buf,
  7655. error_buf_size))
  7656. goto fail;
  7657. read_leb_uint32(p, p_end, dst_tbl_idx);
  7658. if (!get_table_elem_type(module, dst_tbl_idx,
  7659. &dst_ref_type, error_buf,
  7660. error_buf_size))
  7661. goto fail;
  7662. if (src_ref_type != dst_ref_type) {
  7663. set_error_buf(error_buf, error_buf_size,
  7664. "type mismatch");
  7665. goto fail;
  7666. }
  7667. #if WASM_ENABLE_FAST_INTERP != 0
  7668. emit_uint32(loader_ctx, src_tbl_idx);
  7669. emit_uint32(loader_ctx, dst_tbl_idx);
  7670. #endif
  7671. POP_I32();
  7672. POP_I32();
  7673. POP_I32();
  7674. break;
  7675. }
  7676. case WASM_OP_TABLE_SIZE:
  7677. {
  7678. read_leb_uint32(p, p_end, table_idx);
  7679. /* TODO: shall we create a new function to check
  7680. table idx instead of using below function? */
  7681. if (!get_table_elem_type(module, table_idx, NULL,
  7682. error_buf, error_buf_size))
  7683. goto fail;
  7684. #if WASM_ENABLE_FAST_INTERP != 0
  7685. emit_uint32(loader_ctx, table_idx);
  7686. #endif
  7687. PUSH_I32();
  7688. break;
  7689. }
  7690. case WASM_OP_TABLE_GROW:
  7691. case WASM_OP_TABLE_FILL:
  7692. {
  7693. uint8 decl_ref_type;
  7694. read_leb_uint32(p, p_end, table_idx);
  7695. if (!get_table_elem_type(module, table_idx,
  7696. &decl_ref_type, error_buf,
  7697. error_buf_size))
  7698. goto fail;
  7699. if (opcode1 == WASM_OP_TABLE_GROW) {
  7700. if (table_idx < module->import_table_count) {
  7701. module->import_tables[table_idx]
  7702. .u.table.possible_grow = true;
  7703. }
  7704. else {
  7705. module
  7706. ->tables[table_idx
  7707. - module->import_table_count]
  7708. .possible_grow = true;
  7709. }
  7710. }
  7711. #if WASM_ENABLE_FAST_INTERP != 0
  7712. emit_uint32(loader_ctx, table_idx);
  7713. #endif
  7714. POP_I32();
  7715. #if WASM_ENABLE_FAST_INTERP != 0
  7716. POP_OFFSET_TYPE(decl_ref_type);
  7717. #endif
  7718. POP_TYPE(decl_ref_type);
  7719. if (opcode1 == WASM_OP_TABLE_GROW)
  7720. PUSH_I32();
  7721. else
  7722. POP_I32();
  7723. break;
  7724. }
  7725. #endif /* WASM_ENABLE_REF_TYPES */
  7726. default:
  7727. set_error_buf_v(error_buf, error_buf_size,
  7728. "%s %02x %02x", "unsupported opcode",
  7729. 0xfc, opcode1);
  7730. goto fail;
  7731. }
  7732. break;
  7733. }
  7734. #if WASM_ENABLE_SIMD != 0
  7735. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  7736. case WASM_OP_SIMD_PREFIX:
  7737. {
  7738. opcode = read_uint8(p);
  7739. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  7740. */
  7741. switch (opcode) {
  7742. /* memory instruction */
  7743. case SIMD_v128_load:
  7744. case SIMD_v128_load8x8_s:
  7745. case SIMD_v128_load8x8_u:
  7746. case SIMD_v128_load16x4_s:
  7747. case SIMD_v128_load16x4_u:
  7748. case SIMD_v128_load32x2_s:
  7749. case SIMD_v128_load32x2_u:
  7750. case SIMD_v128_load8_splat:
  7751. case SIMD_v128_load16_splat:
  7752. case SIMD_v128_load32_splat:
  7753. case SIMD_v128_load64_splat:
  7754. {
  7755. CHECK_MEMORY();
  7756. read_leb_uint32(p, p_end, align); /* align */
  7757. if (!check_simd_memory_access_align(
  7758. opcode, align, error_buf, error_buf_size)) {
  7759. goto fail;
  7760. }
  7761. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7762. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  7763. break;
  7764. }
  7765. case SIMD_v128_store:
  7766. {
  7767. CHECK_MEMORY();
  7768. read_leb_uint32(p, p_end, align); /* align */
  7769. if (!check_simd_memory_access_align(
  7770. opcode, align, error_buf, error_buf_size)) {
  7771. goto fail;
  7772. }
  7773. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7774. POP_V128();
  7775. POP_I32();
  7776. break;
  7777. }
  7778. /* basic operation */
  7779. case SIMD_v128_const:
  7780. {
  7781. CHECK_BUF1(p, p_end, 16);
  7782. p += 16;
  7783. PUSH_V128();
  7784. break;
  7785. }
  7786. case SIMD_v8x16_shuffle:
  7787. {
  7788. V128 mask;
  7789. CHECK_BUF1(p, p_end, 16);
  7790. mask = read_i8x16(p, error_buf, error_buf_size);
  7791. p += 16;
  7792. if (!check_simd_shuffle_mask(mask, error_buf,
  7793. error_buf_size)) {
  7794. goto fail;
  7795. }
  7796. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7797. break;
  7798. }
  7799. case SIMD_v8x16_swizzle:
  7800. {
  7801. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7802. break;
  7803. }
  7804. /* splat operation */
  7805. case SIMD_i8x16_splat:
  7806. case SIMD_i16x8_splat:
  7807. case SIMD_i32x4_splat:
  7808. case SIMD_i64x2_splat:
  7809. case SIMD_f32x4_splat:
  7810. case SIMD_f64x2_splat:
  7811. {
  7812. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  7813. VALUE_TYPE_I32, VALUE_TYPE_I64,
  7814. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  7815. POP_AND_PUSH(pop_type[opcode - SIMD_i8x16_splat],
  7816. VALUE_TYPE_V128);
  7817. break;
  7818. }
  7819. /* lane operation */
  7820. case SIMD_i8x16_extract_lane_s:
  7821. case SIMD_i8x16_extract_lane_u:
  7822. case SIMD_i8x16_replace_lane:
  7823. case SIMD_i16x8_extract_lane_s:
  7824. case SIMD_i16x8_extract_lane_u:
  7825. case SIMD_i16x8_replace_lane:
  7826. case SIMD_i32x4_extract_lane:
  7827. case SIMD_i32x4_replace_lane:
  7828. case SIMD_i64x2_extract_lane:
  7829. case SIMD_i64x2_replace_lane:
  7830. case SIMD_f32x4_extract_lane:
  7831. case SIMD_f32x4_replace_lane:
  7832. case SIMD_f64x2_extract_lane:
  7833. case SIMD_f64x2_replace_lane:
  7834. {
  7835. uint8 lane;
  7836. /* clang-format off */
  7837. uint8 replace[] = {
  7838. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  7839. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  7840. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  7841. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  7842. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  7843. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  7844. };
  7845. uint8 push_type[] = {
  7846. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  7847. VALUE_TYPE_V128,
  7848. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  7849. VALUE_TYPE_V128,
  7850. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  7851. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  7852. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  7853. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  7854. };
  7855. /* clang-format on */
  7856. CHECK_BUF(p, p_end, 1);
  7857. lane = read_uint8(p);
  7858. if (!check_simd_access_lane(opcode, lane, error_buf,
  7859. error_buf_size)) {
  7860. goto fail;
  7861. }
  7862. if (replace[opcode - SIMD_i8x16_extract_lane_s]) {
  7863. if (!(wasm_loader_pop_frame_ref(
  7864. loader_ctx,
  7865. replace[opcode - SIMD_i8x16_extract_lane_s],
  7866. error_buf, error_buf_size)))
  7867. goto fail;
  7868. }
  7869. POP_AND_PUSH(
  7870. VALUE_TYPE_V128,
  7871. push_type[opcode - SIMD_i8x16_extract_lane_s]);
  7872. break;
  7873. }
  7874. /* i8x16 compare operation */
  7875. case SIMD_i8x16_eq:
  7876. case SIMD_i8x16_ne:
  7877. case SIMD_i8x16_lt_s:
  7878. case SIMD_i8x16_lt_u:
  7879. case SIMD_i8x16_gt_s:
  7880. case SIMD_i8x16_gt_u:
  7881. case SIMD_i8x16_le_s:
  7882. case SIMD_i8x16_le_u:
  7883. case SIMD_i8x16_ge_s:
  7884. case SIMD_i8x16_ge_u:
  7885. /* i16x8 compare operation */
  7886. case SIMD_i16x8_eq:
  7887. case SIMD_i16x8_ne:
  7888. case SIMD_i16x8_lt_s:
  7889. case SIMD_i16x8_lt_u:
  7890. case SIMD_i16x8_gt_s:
  7891. case SIMD_i16x8_gt_u:
  7892. case SIMD_i16x8_le_s:
  7893. case SIMD_i16x8_le_u:
  7894. case SIMD_i16x8_ge_s:
  7895. case SIMD_i16x8_ge_u:
  7896. /* i32x4 compare operation */
  7897. case SIMD_i32x4_eq:
  7898. case SIMD_i32x4_ne:
  7899. case SIMD_i32x4_lt_s:
  7900. case SIMD_i32x4_lt_u:
  7901. case SIMD_i32x4_gt_s:
  7902. case SIMD_i32x4_gt_u:
  7903. case SIMD_i32x4_le_s:
  7904. case SIMD_i32x4_le_u:
  7905. case SIMD_i32x4_ge_s:
  7906. case SIMD_i32x4_ge_u:
  7907. /* f32x4 compare operation */
  7908. case SIMD_f32x4_eq:
  7909. case SIMD_f32x4_ne:
  7910. case SIMD_f32x4_lt:
  7911. case SIMD_f32x4_gt:
  7912. case SIMD_f32x4_le:
  7913. case SIMD_f32x4_ge:
  7914. /* f64x2 compare operation */
  7915. case SIMD_f64x2_eq:
  7916. case SIMD_f64x2_ne:
  7917. case SIMD_f64x2_lt:
  7918. case SIMD_f64x2_gt:
  7919. case SIMD_f64x2_le:
  7920. case SIMD_f64x2_ge:
  7921. {
  7922. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7923. break;
  7924. }
  7925. /* v128 operation */
  7926. case SIMD_v128_not:
  7927. {
  7928. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7929. break;
  7930. }
  7931. case SIMD_v128_and:
  7932. case SIMD_v128_andnot:
  7933. case SIMD_v128_or:
  7934. case SIMD_v128_xor:
  7935. {
  7936. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7937. break;
  7938. }
  7939. case SIMD_v128_bitselect:
  7940. {
  7941. POP_V128();
  7942. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7943. break;
  7944. }
  7945. case SIMD_v128_any_true:
  7946. {
  7947. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  7948. break;
  7949. }
  7950. /* Load Lane Operation */
  7951. case SIMD_v128_load8_lane:
  7952. case SIMD_v128_load16_lane:
  7953. case SIMD_v128_load32_lane:
  7954. case SIMD_v128_load64_lane:
  7955. case SIMD_v128_store8_lane:
  7956. case SIMD_v128_store16_lane:
  7957. case SIMD_v128_store32_lane:
  7958. case SIMD_v128_store64_lane:
  7959. {
  7960. uint8 lane;
  7961. CHECK_MEMORY();
  7962. read_leb_uint32(p, p_end, align); /* align */
  7963. if (!check_simd_memory_access_align(
  7964. opcode, align, error_buf, error_buf_size)) {
  7965. goto fail;
  7966. }
  7967. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7968. CHECK_BUF(p, p_end, 1);
  7969. lane = read_uint8(p);
  7970. if (!check_simd_access_lane(opcode, lane, error_buf,
  7971. error_buf_size)) {
  7972. goto fail;
  7973. }
  7974. POP_V128();
  7975. POP_I32();
  7976. if (opcode < SIMD_v128_store8_lane) {
  7977. PUSH_V128();
  7978. }
  7979. break;
  7980. }
  7981. case SIMD_v128_load32_zero:
  7982. case SIMD_v128_load64_zero:
  7983. {
  7984. CHECK_MEMORY();
  7985. read_leb_uint32(p, p_end, align); /* align */
  7986. if (!check_simd_memory_access_align(
  7987. opcode, align, error_buf, error_buf_size)) {
  7988. goto fail;
  7989. }
  7990. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7991. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  7992. break;
  7993. }
  7994. /* Float conversion */
  7995. case SIMD_f32x4_demote_f64x2_zero:
  7996. case SIMD_f64x2_promote_low_f32x4_zero:
  7997. {
  7998. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  7999. break;
  8000. }
  8001. /* i8x16 Operation */
  8002. case SIMD_i8x16_abs:
  8003. case SIMD_i8x16_neg:
  8004. case SIMD_i8x16_popcnt:
  8005. {
  8006. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8007. break;
  8008. }
  8009. case SIMD_i8x16_all_true:
  8010. case SIMD_i8x16_bitmask:
  8011. {
  8012. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8013. break;
  8014. }
  8015. case SIMD_i8x16_narrow_i16x8_s:
  8016. case SIMD_i8x16_narrow_i16x8_u:
  8017. {
  8018. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8019. break;
  8020. }
  8021. case SIMD_f32x4_ceil:
  8022. case SIMD_f32x4_floor:
  8023. case SIMD_f32x4_trunc:
  8024. case SIMD_f32x4_nearest:
  8025. {
  8026. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8027. break;
  8028. }
  8029. case SIMD_i8x16_shl:
  8030. case SIMD_i8x16_shr_s:
  8031. case SIMD_i8x16_shr_u:
  8032. {
  8033. POP_I32();
  8034. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8035. break;
  8036. }
  8037. case SIMD_i8x16_add:
  8038. case SIMD_i8x16_add_sat_s:
  8039. case SIMD_i8x16_add_sat_u:
  8040. case SIMD_i8x16_sub:
  8041. case SIMD_i8x16_sub_sat_s:
  8042. case SIMD_i8x16_sub_sat_u:
  8043. {
  8044. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8045. break;
  8046. }
  8047. case SIMD_f64x2_ceil:
  8048. case SIMD_f64x2_floor:
  8049. {
  8050. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8051. break;
  8052. }
  8053. case SIMD_i8x16_min_s:
  8054. case SIMD_i8x16_min_u:
  8055. case SIMD_i8x16_max_s:
  8056. case SIMD_i8x16_max_u:
  8057. {
  8058. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8059. break;
  8060. }
  8061. case SIMD_f64x2_trunc:
  8062. {
  8063. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8064. break;
  8065. }
  8066. case SIMD_i8x16_avgr_u:
  8067. {
  8068. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8069. break;
  8070. }
  8071. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  8072. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  8073. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  8074. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  8075. /* i16x8 operation */
  8076. case SIMD_i16x8_abs:
  8077. case SIMD_i16x8_neg:
  8078. {
  8079. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8080. break;
  8081. }
  8082. case SIMD_i16x8_q15mulr_sat_s:
  8083. {
  8084. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8085. break;
  8086. }
  8087. case SIMD_i16x8_all_true:
  8088. case SIMD_i16x8_bitmask:
  8089. {
  8090. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8091. break;
  8092. }
  8093. case SIMD_i16x8_narrow_i32x4_s:
  8094. case SIMD_i16x8_narrow_i32x4_u:
  8095. {
  8096. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8097. break;
  8098. }
  8099. case SIMD_i16x8_extend_low_i8x16_s:
  8100. case SIMD_i16x8_extend_high_i8x16_s:
  8101. case SIMD_i16x8_extend_low_i8x16_u:
  8102. case SIMD_i16x8_extend_high_i8x16_u:
  8103. {
  8104. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8105. break;
  8106. }
  8107. case SIMD_i16x8_shl:
  8108. case SIMD_i16x8_shr_s:
  8109. case SIMD_i16x8_shr_u:
  8110. {
  8111. POP_I32();
  8112. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8113. break;
  8114. }
  8115. case SIMD_i16x8_add:
  8116. case SIMD_i16x8_add_sat_s:
  8117. case SIMD_i16x8_add_sat_u:
  8118. case SIMD_i16x8_sub:
  8119. case SIMD_i16x8_sub_sat_s:
  8120. case SIMD_i16x8_sub_sat_u:
  8121. {
  8122. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8123. break;
  8124. }
  8125. case SIMD_f64x2_nearest:
  8126. {
  8127. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8128. break;
  8129. }
  8130. case SIMD_i16x8_mul:
  8131. case SIMD_i16x8_min_s:
  8132. case SIMD_i16x8_min_u:
  8133. case SIMD_i16x8_max_s:
  8134. case SIMD_i16x8_max_u:
  8135. case SIMD_i16x8_avgr_u:
  8136. case SIMD_i16x8_extmul_low_i8x16_s:
  8137. case SIMD_i16x8_extmul_high_i8x16_s:
  8138. case SIMD_i16x8_extmul_low_i8x16_u:
  8139. case SIMD_i16x8_extmul_high_i8x16_u:
  8140. {
  8141. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8142. break;
  8143. }
  8144. /* i32x4 operation */
  8145. case SIMD_i32x4_abs:
  8146. case SIMD_i32x4_neg:
  8147. {
  8148. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8149. break;
  8150. }
  8151. case SIMD_i32x4_all_true:
  8152. case SIMD_i32x4_bitmask:
  8153. {
  8154. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8155. break;
  8156. }
  8157. case SIMD_i32x4_narrow_i64x2_s:
  8158. case SIMD_i32x4_narrow_i64x2_u:
  8159. {
  8160. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8161. break;
  8162. }
  8163. case SIMD_i32x4_extend_low_i16x8_s:
  8164. case SIMD_i32x4_extend_high_i16x8_s:
  8165. case SIMD_i32x4_extend_low_i16x8_u:
  8166. case SIMD_i32x4_extend_high_i16x8_u:
  8167. {
  8168. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8169. break;
  8170. }
  8171. case SIMD_i32x4_shl:
  8172. case SIMD_i32x4_shr_s:
  8173. case SIMD_i32x4_shr_u:
  8174. {
  8175. POP_I32();
  8176. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8177. break;
  8178. }
  8179. case SIMD_i32x4_add:
  8180. case SIMD_i32x4_sub:
  8181. case SIMD_i32x4_mul:
  8182. case SIMD_i32x4_min_s:
  8183. case SIMD_i32x4_min_u:
  8184. case SIMD_i32x4_max_s:
  8185. case SIMD_i32x4_max_u:
  8186. case SIMD_i32x4_dot_i16x8_s:
  8187. case SIMD_i32x4_avgr_u:
  8188. case SIMD_i32x4_extmul_low_i16x8_s:
  8189. case SIMD_i32x4_extmul_high_i16x8_s:
  8190. case SIMD_i32x4_extmul_low_i16x8_u:
  8191. case SIMD_i32x4_extmul_high_i16x8_u:
  8192. {
  8193. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8194. break;
  8195. }
  8196. /* i64x2 operation */
  8197. case SIMD_i64x2_abs:
  8198. case SIMD_i64x2_neg:
  8199. {
  8200. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8201. break;
  8202. }
  8203. case SIMD_i64x2_all_true:
  8204. case SIMD_i64x2_bitmask:
  8205. {
  8206. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8207. break;
  8208. }
  8209. case SIMD_i64x2_extend_low_i32x4_s:
  8210. case SIMD_i64x2_extend_high_i32x4_s:
  8211. case SIMD_i64x2_extend_low_i32x4_u:
  8212. case SIMD_i64x2_extend_high_i32x4_u:
  8213. {
  8214. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8215. break;
  8216. }
  8217. case SIMD_i64x2_shl:
  8218. case SIMD_i64x2_shr_s:
  8219. case SIMD_i64x2_shr_u:
  8220. {
  8221. POP_I32();
  8222. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8223. break;
  8224. }
  8225. case SIMD_i64x2_add:
  8226. case SIMD_i64x2_sub:
  8227. case SIMD_i64x2_mul:
  8228. case SIMD_i64x2_eq:
  8229. case SIMD_i64x2_ne:
  8230. case SIMD_i64x2_lt_s:
  8231. case SIMD_i64x2_gt_s:
  8232. case SIMD_i64x2_le_s:
  8233. case SIMD_i64x2_ge_s:
  8234. case SIMD_i64x2_extmul_low_i32x4_s:
  8235. case SIMD_i64x2_extmul_high_i32x4_s:
  8236. case SIMD_i64x2_extmul_low_i32x4_u:
  8237. case SIMD_i64x2_extmul_high_i32x4_u:
  8238. {
  8239. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8240. break;
  8241. }
  8242. /* f32x4 operation */
  8243. case SIMD_f32x4_abs:
  8244. case SIMD_f32x4_neg:
  8245. case SIMD_f32x4_round:
  8246. case SIMD_f32x4_sqrt:
  8247. {
  8248. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8249. break;
  8250. }
  8251. case SIMD_f32x4_add:
  8252. case SIMD_f32x4_sub:
  8253. case SIMD_f32x4_mul:
  8254. case SIMD_f32x4_div:
  8255. case SIMD_f32x4_min:
  8256. case SIMD_f32x4_max:
  8257. case SIMD_f32x4_pmin:
  8258. case SIMD_f32x4_pmax:
  8259. {
  8260. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8261. break;
  8262. }
  8263. /* f64x2 operation */
  8264. case SIMD_f64x2_abs:
  8265. case SIMD_f64x2_neg:
  8266. case SIMD_f64x2_round:
  8267. case SIMD_f64x2_sqrt:
  8268. {
  8269. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8270. break;
  8271. }
  8272. case SIMD_f64x2_add:
  8273. case SIMD_f64x2_sub:
  8274. case SIMD_f64x2_mul:
  8275. case SIMD_f64x2_div:
  8276. case SIMD_f64x2_min:
  8277. case SIMD_f64x2_max:
  8278. case SIMD_f64x2_pmin:
  8279. case SIMD_f64x2_pmax:
  8280. {
  8281. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8282. break;
  8283. }
  8284. case SIMD_i32x4_trunc_sat_f32x4_s:
  8285. case SIMD_i32x4_trunc_sat_f32x4_u:
  8286. case SIMD_f32x4_convert_i32x4_s:
  8287. case SIMD_f32x4_convert_i32x4_u:
  8288. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  8289. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  8290. case SIMD_f64x2_convert_low_i32x4_s:
  8291. case SIMD_f64x2_convert_low_i32x4_u:
  8292. {
  8293. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8294. break;
  8295. }
  8296. default:
  8297. {
  8298. if (error_buf != NULL) {
  8299. snprintf(error_buf, error_buf_size,
  8300. "WASM module load failed: "
  8301. "invalid opcode 0xfd %02x.",
  8302. opcode);
  8303. }
  8304. goto fail;
  8305. }
  8306. }
  8307. break;
  8308. }
  8309. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8310. #endif /* end of WASM_ENABLE_SIMD */
  8311. #if WASM_ENABLE_SHARED_MEMORY != 0
  8312. case WASM_OP_ATOMIC_PREFIX:
  8313. {
  8314. opcode = read_uint8(p);
  8315. #if WASM_ENABLE_FAST_INTERP != 0
  8316. emit_byte(loader_ctx, opcode);
  8317. #endif
  8318. if (opcode != WASM_OP_ATOMIC_FENCE) {
  8319. CHECK_MEMORY();
  8320. read_leb_uint32(p, p_end, align); /* align */
  8321. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8322. if (!check_memory_align_equal(opcode, align, error_buf,
  8323. error_buf_size)) {
  8324. goto fail;
  8325. }
  8326. #if WASM_ENABLE_FAST_INTERP != 0
  8327. emit_uint32(loader_ctx, mem_offset);
  8328. #endif
  8329. }
  8330. switch (opcode) {
  8331. case WASM_OP_ATOMIC_NOTIFY:
  8332. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8333. break;
  8334. case WASM_OP_ATOMIC_WAIT32:
  8335. POP_I64();
  8336. POP_I32();
  8337. POP_I32();
  8338. PUSH_I32();
  8339. break;
  8340. case WASM_OP_ATOMIC_WAIT64:
  8341. POP_I64();
  8342. POP_I64();
  8343. POP_I32();
  8344. PUSH_I32();
  8345. break;
  8346. case WASM_OP_ATOMIC_FENCE:
  8347. /* reserved byte 0x00 */
  8348. if (*p++ != 0x00) {
  8349. set_error_buf(error_buf, error_buf_size,
  8350. "zero byte expected");
  8351. goto fail;
  8352. }
  8353. break;
  8354. case WASM_OP_ATOMIC_I32_LOAD:
  8355. case WASM_OP_ATOMIC_I32_LOAD8_U:
  8356. case WASM_OP_ATOMIC_I32_LOAD16_U:
  8357. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8358. break;
  8359. case WASM_OP_ATOMIC_I32_STORE:
  8360. case WASM_OP_ATOMIC_I32_STORE8:
  8361. case WASM_OP_ATOMIC_I32_STORE16:
  8362. POP_I32();
  8363. POP_I32();
  8364. break;
  8365. case WASM_OP_ATOMIC_I64_LOAD:
  8366. case WASM_OP_ATOMIC_I64_LOAD8_U:
  8367. case WASM_OP_ATOMIC_I64_LOAD16_U:
  8368. case WASM_OP_ATOMIC_I64_LOAD32_U:
  8369. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  8370. break;
  8371. case WASM_OP_ATOMIC_I64_STORE:
  8372. case WASM_OP_ATOMIC_I64_STORE8:
  8373. case WASM_OP_ATOMIC_I64_STORE16:
  8374. case WASM_OP_ATOMIC_I64_STORE32:
  8375. POP_I64();
  8376. POP_I32();
  8377. break;
  8378. case WASM_OP_ATOMIC_RMW_I32_ADD:
  8379. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  8380. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  8381. case WASM_OP_ATOMIC_RMW_I32_SUB:
  8382. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  8383. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  8384. case WASM_OP_ATOMIC_RMW_I32_AND:
  8385. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  8386. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  8387. case WASM_OP_ATOMIC_RMW_I32_OR:
  8388. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  8389. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  8390. case WASM_OP_ATOMIC_RMW_I32_XOR:
  8391. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  8392. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  8393. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  8394. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  8395. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  8396. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8397. break;
  8398. case WASM_OP_ATOMIC_RMW_I64_ADD:
  8399. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  8400. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  8401. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  8402. case WASM_OP_ATOMIC_RMW_I64_SUB:
  8403. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  8404. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  8405. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  8406. case WASM_OP_ATOMIC_RMW_I64_AND:
  8407. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  8408. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  8409. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  8410. case WASM_OP_ATOMIC_RMW_I64_OR:
  8411. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  8412. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  8413. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  8414. case WASM_OP_ATOMIC_RMW_I64_XOR:
  8415. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  8416. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  8417. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  8418. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  8419. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  8420. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  8421. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  8422. POP_I64();
  8423. POP_I32();
  8424. PUSH_I64();
  8425. break;
  8426. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  8427. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  8428. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  8429. POP_I32();
  8430. POP_I32();
  8431. POP_I32();
  8432. PUSH_I32();
  8433. break;
  8434. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  8435. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  8436. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  8437. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  8438. POP_I64();
  8439. POP_I64();
  8440. POP_I32();
  8441. PUSH_I64();
  8442. break;
  8443. default:
  8444. set_error_buf_v(error_buf, error_buf_size,
  8445. "%s %02x %02x", "unsupported opcode",
  8446. 0xfe, opcode);
  8447. goto fail;
  8448. }
  8449. break;
  8450. }
  8451. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  8452. default:
  8453. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  8454. "unsupported opcode", opcode);
  8455. goto fail;
  8456. }
  8457. #if WASM_ENABLE_FAST_INTERP != 0
  8458. last_op = opcode;
  8459. #endif
  8460. }
  8461. if (loader_ctx->csp_num > 0) {
  8462. if (cur_func_idx < module->function_count - 1)
  8463. /* Function with missing end marker (between two functions) */
  8464. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  8465. else
  8466. /* Function with missing end marker
  8467. (at EOF or end of code sections) */
  8468. set_error_buf(error_buf, error_buf_size,
  8469. "unexpected end of section or function, "
  8470. "or section size mismatch");
  8471. goto fail;
  8472. }
  8473. #if WASM_ENABLE_FAST_INTERP != 0
  8474. if (loader_ctx->p_code_compiled == NULL)
  8475. goto re_scan;
  8476. func->const_cell_num = loader_ctx->const_cell_num;
  8477. if (func->const_cell_num > 0) {
  8478. int32 j;
  8479. if (!(func->consts = func_const = loader_malloc(
  8480. func->const_cell_num * 4, error_buf, error_buf_size)))
  8481. goto fail;
  8482. func_const_end = func->consts + func->const_cell_num * 4;
  8483. /* reverse the const buf */
  8484. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  8485. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  8486. if (c->value_type == VALUE_TYPE_F64
  8487. || c->value_type == VALUE_TYPE_I64) {
  8488. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  8489. &(c->value.f64), (uint32)sizeof(int64));
  8490. func_const += sizeof(int64);
  8491. }
  8492. else {
  8493. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  8494. &(c->value.f32), (uint32)sizeof(int32));
  8495. func_const += sizeof(int32);
  8496. }
  8497. }
  8498. }
  8499. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  8500. - loader_ctx->start_dynamic_offset + 1;
  8501. #else
  8502. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  8503. #endif
  8504. func->max_block_num = loader_ctx->max_csp_num;
  8505. return_value = true;
  8506. fail:
  8507. wasm_loader_ctx_destroy(loader_ctx);
  8508. (void)table_idx;
  8509. (void)table_seg_idx;
  8510. (void)data_seg_idx;
  8511. (void)i64_const;
  8512. (void)local_offset;
  8513. (void)p_org;
  8514. (void)mem_offset;
  8515. (void)align;
  8516. return return_value;
  8517. }