wasm_loader.c 305 KB

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