wasm_loader.c 241 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870
  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. static bool
  73. skip_leb(const uint8 **p_buf, const uint8 *buf_end, uint32 maxbits,
  74. char* error_buf, uint32 error_buf_size)
  75. {
  76. const uint8 *buf = *p_buf;
  77. uint32 offset = 0, bcnt = 0;
  78. uint64 byte;
  79. while (true) {
  80. if (bcnt + 1 > (maxbits + 6) / 7) {
  81. set_error_buf(error_buf, error_buf_size,
  82. "integer representation too long");
  83. return false;
  84. }
  85. CHECK_BUF(buf, buf_end, offset + 1);
  86. byte = buf[offset];
  87. offset += 1;
  88. bcnt += 1;
  89. if ((byte & 0x80) == 0) {
  90. break;
  91. }
  92. }
  93. *p_buf += offset;
  94. return true;
  95. fail:
  96. return false;
  97. }
  98. #define skip_leb_int64(p, p_end) do { \
  99. if (!skip_leb(&p, p_end, 64, \
  100. error_buf, error_buf_size)) \
  101. return false; \
  102. } while (0)
  103. #define skip_leb_uint32(p, p_end) do { \
  104. if (!skip_leb(&p, p_end, 32, \
  105. error_buf, error_buf_size)) \
  106. return false; \
  107. } while (0)
  108. #define skip_leb_int32(p, p_end) do { \
  109. if (!skip_leb(&p, p_end, 32, \
  110. error_buf, error_buf_size)) \
  111. return false; \
  112. } while (0)
  113. static bool
  114. read_leb(uint8 **p_buf, const uint8 *buf_end,
  115. uint32 maxbits, bool sign, uint64 *p_result,
  116. char* error_buf, uint32 error_buf_size)
  117. {
  118. const uint8 *buf = *p_buf;
  119. uint64 result = 0;
  120. uint32 shift = 0;
  121. uint32 offset = 0, bcnt = 0;
  122. uint64 byte;
  123. while (true) {
  124. if (bcnt + 1 > (maxbits + 6) / 7) {
  125. set_error_buf(error_buf, error_buf_size,
  126. "integer representation too long");
  127. return false;
  128. }
  129. CHECK_BUF(buf, buf_end, offset + 1);
  130. byte = buf[offset];
  131. offset += 1;
  132. result |= ((byte & 0x7f) << shift);
  133. shift += 7;
  134. bcnt += 1;
  135. if ((byte & 0x80) == 0) {
  136. break;
  137. }
  138. }
  139. if (!sign && maxbits == 32 && shift >= maxbits) {
  140. /* The top bits set represent values > 32 bits */
  141. if (((uint8)byte) & 0xf0)
  142. goto fail_integer_too_large;
  143. }
  144. else if (sign && maxbits == 32) {
  145. if (shift < maxbits) {
  146. /* Sign extend */
  147. result = (((int32)result) << (maxbits - shift))
  148. >> (maxbits - shift);
  149. }
  150. else {
  151. /* The top bits should be a sign-extension of the sign bit */
  152. bool sign_bit_set = ((uint8)byte) & 0x8;
  153. int top_bits = ((uint8)byte) & 0xf0;
  154. if ((sign_bit_set && top_bits != 0x70)
  155. || (!sign_bit_set && top_bits != 0))
  156. goto fail_integer_too_large;
  157. }
  158. }
  159. else if (sign && maxbits == 64) {
  160. if (shift < maxbits) {
  161. /* Sign extend */
  162. result = (((int64)result) << (maxbits - shift))
  163. >> (maxbits - shift);
  164. }
  165. else {
  166. /* The top bits should be a sign-extension of the sign bit */
  167. bool sign_bit_set = ((uint8)byte) & 0x1;
  168. int top_bits = ((uint8)byte) & 0xfe;
  169. if ((sign_bit_set && top_bits != 0x7e)
  170. || (!sign_bit_set && top_bits != 0))
  171. goto fail_integer_too_large;
  172. }
  173. }
  174. *p_buf += offset;
  175. *p_result = result;
  176. return true;
  177. fail_integer_too_large:
  178. set_error_buf(error_buf, error_buf_size, "integer too large");
  179. fail:
  180. return false;
  181. }
  182. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  183. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  184. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  185. #define read_leb_int64(p, p_end, res) do { \
  186. uint64 res64; \
  187. if (!read_leb((uint8**)&p, p_end, 64, true, &res64,\
  188. error_buf, error_buf_size)) \
  189. goto fail; \
  190. res = (int64)res64; \
  191. } while (0)
  192. #define read_leb_uint32(p, p_end, res) do { \
  193. uint64 res64; \
  194. if (!read_leb((uint8**)&p, p_end, 32, false, &res64,\
  195. error_buf, error_buf_size)) \
  196. goto fail; \
  197. res = (uint32)res64; \
  198. } while (0)
  199. #define read_leb_int32(p, p_end, res) do { \
  200. uint64 res64; \
  201. if (!read_leb((uint8**)&p, p_end, 32, true, &res64,\
  202. error_buf, error_buf_size)) \
  203. goto fail; \
  204. res = (int32)res64; \
  205. } while (0)
  206. static void *
  207. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  208. {
  209. void *mem;
  210. if (size >= UINT32_MAX
  211. || !(mem = wasm_runtime_malloc((uint32)size))) {
  212. set_error_buf(error_buf, error_buf_size,
  213. "allocate memory failed");
  214. return NULL;
  215. }
  216. memset(mem, 0, (uint32)size);
  217. return mem;
  218. }
  219. static bool
  220. check_utf8_str(const uint8* str, uint32 len)
  221. {
  222. const uint8 *p = str, *p_end = str + len, *p_end1;
  223. uint8 chr, n_bytes;
  224. while (p < p_end) {
  225. chr = *p++;
  226. if (chr >= 0x80) {
  227. /* Calculate the byte count: the first byte must be
  228. 110XXXXX, 1110XXXX, 11110XXX, 111110XX, or 1111110X,
  229. the count of leading '1' denotes the total byte count */
  230. n_bytes = 0;
  231. while ((chr & 0x80) != 0) {
  232. chr = (uint8)(chr << 1);
  233. n_bytes++;
  234. }
  235. /* Check byte count */
  236. if (n_bytes < 2 || n_bytes > 6
  237. || p + n_bytes - 1 > p_end)
  238. return false;
  239. /* Check the following bytes, which must be 10XXXXXX */
  240. p_end1 = p + n_bytes - 1;
  241. while (p < p_end1) {
  242. if (!(*p & 0x80) || (*p | 0x40))
  243. return false;
  244. p++;
  245. }
  246. }
  247. }
  248. return true;
  249. }
  250. static char*
  251. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  252. char* error_buf, uint32 error_buf_size)
  253. {
  254. StringNode *node, *node_next;
  255. if (!check_utf8_str(str, len)) {
  256. set_error_buf(error_buf, error_buf_size,
  257. "invalid UTF-8 encoding");
  258. return NULL;
  259. }
  260. /* Search const str list */
  261. node = module->const_str_list;
  262. while (node) {
  263. node_next = node->next;
  264. if (strlen(node->str) == len
  265. && !memcmp(node->str, str, len))
  266. break;
  267. node = node_next;
  268. }
  269. if (node) {
  270. LOG_DEBUG("reuse %s", node->str);
  271. return node->str;
  272. }
  273. if (!(node = loader_malloc(sizeof(StringNode) + len + 1,
  274. error_buf, error_buf_size))) {
  275. return NULL;
  276. }
  277. node->str = ((char*)node) + sizeof(StringNode);
  278. bh_memcpy_s(node->str, len + 1, str, len);
  279. node->str[len] = '\0';
  280. if (!module->const_str_list) {
  281. /* set as head */
  282. module->const_str_list = node;
  283. node->next = NULL;
  284. }
  285. else {
  286. /* insert it */
  287. node->next = module->const_str_list;
  288. module->const_str_list = node;
  289. }
  290. return node->str;
  291. }
  292. static bool
  293. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  294. InitializerExpression *init_expr, uint8 type,
  295. char *error_buf, uint32 error_buf_size)
  296. {
  297. const uint8 *p = *p_buf, *p_end = buf_end;
  298. uint8 flag, end_byte, *p_float;
  299. uint32 i;
  300. CHECK_BUF(p, p_end, 1);
  301. init_expr->init_expr_type = read_uint8(p);
  302. flag = init_expr->init_expr_type;
  303. switch (flag) {
  304. /* i32.const */
  305. case INIT_EXPR_TYPE_I32_CONST:
  306. if (type != VALUE_TYPE_I32)
  307. goto fail_type_mismatch;
  308. read_leb_int32(p, p_end, init_expr->u.i32);
  309. break;
  310. /* i64.const */
  311. case INIT_EXPR_TYPE_I64_CONST:
  312. if (type != VALUE_TYPE_I64)
  313. goto fail_type_mismatch;
  314. read_leb_int64(p, p_end, init_expr->u.i64);
  315. break;
  316. /* f32.const */
  317. case INIT_EXPR_TYPE_F32_CONST:
  318. if (type != VALUE_TYPE_F32)
  319. goto fail_type_mismatch;
  320. CHECK_BUF(p, p_end, 4);
  321. p_float = (uint8*)&init_expr->u.f32;
  322. for (i = 0; i < sizeof(float32); i++)
  323. *p_float++ = *p++;
  324. break;
  325. /* f64.const */
  326. case INIT_EXPR_TYPE_F64_CONST:
  327. if (type != VALUE_TYPE_F64)
  328. goto fail_type_mismatch;
  329. CHECK_BUF(p, p_end, 8);
  330. p_float = (uint8*)&init_expr->u.f64;
  331. for (i = 0; i < sizeof(float64); i++)
  332. *p_float++ = *p++;
  333. break;
  334. /* get_global */
  335. case INIT_EXPR_TYPE_GET_GLOBAL:
  336. read_leb_uint32(p, p_end, init_expr->u.global_index);
  337. break;
  338. default:
  339. goto fail_type_mismatch;
  340. }
  341. CHECK_BUF(p, p_end, 1);
  342. end_byte = read_uint8(p);
  343. if (end_byte != 0x0b)
  344. goto fail_type_mismatch;
  345. *p_buf = p;
  346. return true;
  347. fail_type_mismatch:
  348. set_error_buf(error_buf, error_buf_size,
  349. "type mismatch or constant expression required");
  350. fail:
  351. return false;
  352. }
  353. static bool
  354. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  355. char *error_buf, uint32 error_buf_size)
  356. {
  357. const uint8 *p = buf, *p_end = buf_end, *p_org;
  358. uint32 type_count, param_count, result_count, i, j;
  359. uint32 param_cell_num, ret_cell_num;
  360. uint64 total_size;
  361. uint8 flag;
  362. WASMType *type;
  363. read_leb_uint32(p, p_end, type_count);
  364. if (type_count) {
  365. module->type_count = type_count;
  366. total_size = sizeof(WASMType*) * (uint64)type_count;
  367. if (!(module->types = loader_malloc
  368. (total_size, error_buf, error_buf_size))) {
  369. return false;
  370. }
  371. for (i = 0; i < type_count; i++) {
  372. CHECK_BUF(p, p_end, 1);
  373. flag = read_uint8(p);
  374. if (flag != 0x60) {
  375. set_error_buf(error_buf, error_buf_size,
  376. "invalid type flag");
  377. return false;
  378. }
  379. read_leb_uint32(p, p_end, param_count);
  380. /* Resolve param count and result count firstly */
  381. p_org = p;
  382. CHECK_BUF(p, p_end, param_count);
  383. p += param_count;
  384. read_leb_uint32(p, p_end, result_count);
  385. CHECK_BUF(p, p_end, result_count);
  386. p = p_org;
  387. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  388. set_error_buf(error_buf, error_buf_size,
  389. "param count or result count too large");
  390. return false;
  391. }
  392. total_size = offsetof(WASMType, types) +
  393. sizeof(uint8) * (uint64)(param_count + result_count);
  394. if (!(type = module->types[i] =
  395. loader_malloc(total_size, error_buf, error_buf_size))) {
  396. return false;
  397. }
  398. /* Resolve param types and result types */
  399. type->param_count = (uint16)param_count;
  400. type->result_count = (uint16)result_count;
  401. for (j = 0; j < param_count; j++) {
  402. CHECK_BUF(p, p_end, 1);
  403. type->types[j] = read_uint8(p);
  404. }
  405. read_leb_uint32(p, p_end, result_count);
  406. for (j = 0; j < result_count; j++) {
  407. CHECK_BUF(p, p_end, 1);
  408. type->types[param_count + j] = read_uint8(p);
  409. }
  410. param_cell_num = wasm_get_cell_num(type->types, param_count);
  411. ret_cell_num = wasm_get_cell_num(type->types + param_count,
  412. result_count);
  413. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  414. set_error_buf(error_buf, error_buf_size,
  415. "param count or result count too large");
  416. return false;
  417. }
  418. type->param_cell_num = (uint16)param_cell_num;
  419. type->ret_cell_num = (uint16)ret_cell_num;
  420. }
  421. }
  422. if (p != p_end) {
  423. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  424. return false;
  425. }
  426. LOG_VERBOSE("Load type section success.\n");
  427. return true;
  428. fail:
  429. return false;
  430. }
  431. #if WASM_ENABLE_MULTI_MODULE != 0
  432. /**
  433. * Find export item of a module with export info:
  434. * module name, field name and export kind
  435. */
  436. static WASMExport *
  437. wasm_loader_find_export(const WASMModule *module,
  438. const char *module_name,
  439. const char *field_name,
  440. uint8 export_kind,
  441. uint32 export_index_boundary,
  442. char *error_buf, uint32 error_buf_size)
  443. {
  444. WASMExport *export;
  445. uint32 i;
  446. for (i = 0, export = module->exports; i < module->export_count;
  447. ++i, ++export) {
  448. /**
  449. * need to consider a scenario that different kinds of exports
  450. * may have the same name, like
  451. * (table (export "m1" "exported") 10 funcref)
  452. * (memory (export "m1" "exported") 10)
  453. **/
  454. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  455. break;
  456. }
  457. }
  458. if (i == module->export_count) {
  459. LOG_DEBUG("can not find an export %d named %s in the module %s",
  460. export_kind, field_name, module_name);
  461. set_error_buf(error_buf, error_buf_size,
  462. "unknown import or incompatible import type");
  463. return NULL;
  464. }
  465. if (export->index >= export_index_boundary) {
  466. LOG_DEBUG("%s in the module %s is out of index (%d >= %d )",
  467. field_name, module_name,
  468. export->index, export_index_boundary);
  469. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  470. return NULL;
  471. }
  472. return export;
  473. }
  474. static WASMFunction *
  475. wasm_loader_resolve_function(const char *module_name,
  476. const char *function_name,
  477. const WASMType *expected_function_type,
  478. char *error_buf, uint32 error_buf_size)
  479. {
  480. WASMModuleCommon *module_reg;
  481. WASMFunction *function = NULL;
  482. WASMExport *export = NULL;
  483. WASMModule *module = NULL;
  484. WASMType *target_function_type = NULL;
  485. module_reg = wasm_runtime_find_module_registered(module_name);
  486. if (!module_reg
  487. || module_reg->module_type != Wasm_Module_Bytecode) {
  488. LOG_DEBUG("can not find a module named %s for function", module_name);
  489. set_error_buf(error_buf, error_buf_size, "unknown import");
  490. return NULL;
  491. }
  492. module = (WASMModule *)module_reg;
  493. export = wasm_loader_find_export(module, module_name, function_name,
  494. EXPORT_KIND_FUNC,
  495. module->import_function_count
  496. + module->function_count,
  497. error_buf, error_buf_size);
  498. if (!export) {
  499. return NULL;
  500. }
  501. /* run a function type check */
  502. if (export->index < module->import_function_count) {
  503. target_function_type =
  504. module->import_functions[export->index].u.function.func_type;
  505. function = module->import_functions[export->index]
  506. .u.function.import_func_linked;
  507. }
  508. else {
  509. target_function_type =
  510. module->functions[export->index - module->import_function_count]
  511. ->func_type;
  512. function =
  513. module->functions[export->index - module->import_function_count];
  514. }
  515. if (!wasm_type_equal(expected_function_type, target_function_type)) {
  516. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  517. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  518. return NULL;
  519. }
  520. return function;
  521. }
  522. static WASMTable *
  523. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  524. uint32 init_size, uint32 max_size,
  525. char *error_buf, uint32 error_buf_size)
  526. {
  527. WASMModuleCommon *module_reg;
  528. WASMTable *table = NULL;
  529. WASMExport *export = NULL;
  530. WASMModule *module = NULL;
  531. module_reg = wasm_runtime_find_module_registered(module_name);
  532. if (!module_reg
  533. || module_reg->module_type != Wasm_Module_Bytecode) {
  534. LOG_DEBUG("can not find a module named %s for table", module_name);
  535. set_error_buf(error_buf, error_buf_size, "unknown import");
  536. return NULL;
  537. }
  538. module = (WASMModule *)module_reg;
  539. export = wasm_loader_find_export(module, module_name, table_name,
  540. EXPORT_KIND_TABLE,
  541. module->table_count
  542. + module->import_table_count,
  543. error_buf, error_buf_size);
  544. if (!export) {
  545. return NULL;
  546. }
  547. /* run a table type check */
  548. if (export->index < module->import_table_count) {
  549. table =
  550. module->import_tables[export->index].u.table.import_table_linked;
  551. }
  552. else {
  553. table = &(module->tables[export->index - module->import_table_count]);
  554. }
  555. if (table->init_size < init_size || table->max_size > max_size) {
  556. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  557. module_name, table_name, table->init_size, table->max_size,
  558. init_size, max_size);
  559. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  560. return NULL;
  561. }
  562. return table;
  563. }
  564. static WASMMemory *
  565. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  566. uint32 init_page_count, uint32 max_page_count,
  567. char *error_buf, uint32 error_buf_size)
  568. {
  569. WASMModuleCommon *module_reg;
  570. WASMMemory *memory = NULL;
  571. WASMExport *export = NULL;
  572. WASMModule *module = NULL;
  573. module_reg = wasm_runtime_find_module_registered(module_name);
  574. if (!module_reg
  575. || module_reg->module_type != Wasm_Module_Bytecode) {
  576. LOG_DEBUG("can not find a module named %s for memory", module_name);
  577. set_error_buf(error_buf, error_buf_size, "unknown import");
  578. return NULL;
  579. }
  580. module = (WASMModule *)module_reg;
  581. export = wasm_loader_find_export(module, module_name, memory_name,
  582. EXPORT_KIND_MEMORY,
  583. module->import_memory_count
  584. + module->memory_count,
  585. error_buf, error_buf_size);
  586. if (!export) {
  587. return NULL;
  588. }
  589. /* run a memory check */
  590. if (export->index < module->import_memory_count) {
  591. memory =
  592. module->import_memories[export->index].u.memory.import_memory_linked;
  593. }
  594. else {
  595. memory =
  596. &(module->memories[export->index - module->import_memory_count]);
  597. }
  598. if (memory->init_page_count < init_page_count ||
  599. memory->max_page_count > max_page_count) {
  600. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  601. module_name, memory_name, memory->init_page_count,
  602. memory->max_page_count, init_page_count, max_page_count);
  603. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  604. return NULL;
  605. }
  606. return memory;
  607. }
  608. static WASMGlobal *
  609. wasm_loader_resolve_global(const char *module_name,
  610. const char *global_name,
  611. uint8 type, bool is_mutable,
  612. char *error_buf, uint32 error_buf_size)
  613. {
  614. WASMModuleCommon *module_reg;
  615. WASMGlobal *global = NULL;
  616. WASMExport *export = NULL;
  617. WASMModule *module = NULL;
  618. module_reg = wasm_runtime_find_module_registered(module_name);
  619. if (!module_reg
  620. || module_reg->module_type != Wasm_Module_Bytecode) {
  621. LOG_DEBUG("can not find a module named %s for global", module_name);
  622. set_error_buf(error_buf, error_buf_size, "unknown import");
  623. return NULL;
  624. }
  625. module = (WASMModule *)module_reg;
  626. export = wasm_loader_find_export(module, module_name, global_name,
  627. EXPORT_KIND_GLOBAL,
  628. module->import_global_count
  629. + module->global_count,
  630. error_buf, error_buf_size);
  631. if (!export) {
  632. return NULL;
  633. }
  634. /* run a global check */
  635. if (export->index < module->import_global_count) {
  636. global =
  637. module->import_globals[export->index].u.global.import_global_linked;
  638. } else {
  639. global =
  640. &(module->globals[export->index - module->import_global_count]);
  641. }
  642. if (global->type != type || global->is_mutable != is_mutable) {
  643. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  644. module_name, global_name, global->type, global->is_mutable,
  645. type, is_mutable);
  646. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  647. return NULL;
  648. }
  649. return global;
  650. }
  651. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  652. static bool
  653. load_function_import(const WASMModule *parent_module, WASMModule *sub_module,
  654. char *sub_module_name, char *function_name,
  655. const uint8 **p_buf, const uint8 *buf_end,
  656. WASMFunctionImport *function,
  657. char *error_buf, uint32 error_buf_size)
  658. {
  659. const uint8 *p = *p_buf, *p_end = buf_end;
  660. uint32 declare_type_index = 0;
  661. WASMType *declare_func_type = NULL;
  662. WASMFunction *linked_func = NULL;
  663. const char *linked_signature = NULL;
  664. void *linked_attachment = NULL;
  665. bool linked_call_conv_raw = false;
  666. bool is_built_in_module = false;
  667. CHECK_BUF(p, p_end, 1);
  668. read_leb_uint32(p, p_end, declare_type_index);
  669. *p_buf = p;
  670. if (declare_type_index >= parent_module->type_count) {
  671. set_error_buf(error_buf, error_buf_size, "unknown type");
  672. return false;
  673. }
  674. declare_func_type = parent_module->types[declare_type_index];
  675. is_built_in_module = wasm_runtime_is_built_in_module(sub_module_name);
  676. if (is_built_in_module) {
  677. LOG_DEBUG("%s is a function of a built-in module %s",
  678. function_name, sub_module_name);
  679. /* check built-in modules */
  680. linked_func = wasm_native_resolve_symbol(sub_module_name,
  681. function_name,
  682. declare_func_type,
  683. &linked_signature,
  684. &linked_attachment,
  685. &linked_call_conv_raw);
  686. }
  687. #if WASM_ENABLE_MULTI_MODULE != 0
  688. else {
  689. LOG_DEBUG("%s is a function of a sub-module %s",
  690. function_name, sub_module_name);
  691. linked_func = wasm_loader_resolve_function(sub_module_name,
  692. function_name,
  693. declare_func_type,
  694. error_buf,
  695. error_buf_size);
  696. }
  697. #endif
  698. if (!linked_func) {
  699. #if WASM_ENABLE_SPEC_TEST != 0
  700. set_error_buf(error_buf, error_buf_size,
  701. "unknown import or incompatible import type");
  702. return false;
  703. #else
  704. #if WASM_ENABLE_WAMR_COMPILER == 0
  705. LOG_WARNING("warning: fail to link import function (%s, %s)",
  706. sub_module_name, function_name);
  707. #endif
  708. #endif
  709. }
  710. function->module_name = sub_module_name;
  711. function->field_name = function_name;
  712. function->func_type = declare_func_type;
  713. /* func_ptr_linked is for built-in functions */
  714. function->func_ptr_linked = is_built_in_module ? linked_func : NULL;
  715. function->signature = linked_signature;
  716. function->attachment = linked_attachment;
  717. function->call_conv_raw = linked_call_conv_raw;
  718. #if WASM_ENABLE_MULTI_MODULE != 0
  719. function->import_module = is_built_in_module ? NULL : sub_module;
  720. /* can not set both func_ptr_linked and import_func_linked not NULL */
  721. function->import_func_linked = is_built_in_module ? NULL : linked_func;
  722. #endif
  723. return true;
  724. fail:
  725. return false;
  726. }
  727. static bool
  728. check_table_max_size(uint32 init_size, uint32 max_size,
  729. char *error_buf, uint32 error_buf_size)
  730. {
  731. if (max_size < init_size) {
  732. set_error_buf(error_buf, error_buf_size,
  733. "size minimum must not be greater than maximum");
  734. return false;
  735. }
  736. return true;
  737. }
  738. static bool
  739. load_table_import(WASMModule *sub_module, const char *sub_module_name,
  740. const char *table_name, const uint8 **p_buf,
  741. const uint8 *buf_end, WASMTableImport *table,
  742. char *error_buf, uint32 error_buf_size)
  743. {
  744. const uint8 *p = *p_buf, *p_end = buf_end;
  745. uint32 declare_elem_type = 0;
  746. uint32 declare_max_size_flag = 0;
  747. uint32 declare_init_size = 0;
  748. uint32 declare_max_size = 0;
  749. #if WASM_ENABLE_MULTI_MODULE != 0
  750. WASMTable *linked_table = NULL;
  751. #endif
  752. CHECK_BUF(p, p_end, 1);
  753. /* 0x70 */
  754. declare_elem_type = read_uint8(p);
  755. if (TABLE_ELEM_TYPE_ANY_FUNC != declare_elem_type) {
  756. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  757. return false;
  758. }
  759. read_leb_uint32(p, p_end, declare_max_size_flag);
  760. read_leb_uint32(p, p_end, declare_init_size);
  761. if (declare_max_size_flag & 1) {
  762. read_leb_uint32(p, p_end, declare_max_size);
  763. if (!check_table_max_size(table->init_size, table->max_size,
  764. error_buf, error_buf_size))
  765. return false;
  766. } else {
  767. declare_max_size = 0x10000;
  768. }
  769. *p_buf = p;
  770. #if WASM_ENABLE_MULTI_MODULE != 0
  771. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  772. linked_table = wasm_loader_resolve_table(
  773. sub_module_name, table_name,
  774. declare_init_size, declare_max_size,
  775. error_buf, error_buf_size);
  776. if (!linked_table) {
  777. LOG_DEBUG("(%s, %s) is not an exported from one of modules",
  778. table_name, sub_module_name);
  779. return false;
  780. }
  781. /**
  782. * reset with linked table limit
  783. */
  784. declare_elem_type = linked_table->elem_type;
  785. declare_init_size = linked_table->init_size;
  786. declare_max_size = linked_table->max_size;
  787. declare_max_size_flag = linked_table->flags;
  788. table->import_table_linked = linked_table;
  789. table->import_module = sub_module;
  790. }
  791. #endif
  792. /* (table (export "table") 10 20 funcref) */
  793. if (!strcmp("spectest", sub_module_name)) {
  794. uint32 spectest_table_init_size = 10;
  795. uint32 spectest_table_max_size = 20;
  796. if (strcmp("table", table_name)) {
  797. set_error_buf(error_buf, error_buf_size,
  798. "incompatible import type or unknown import");
  799. return false;
  800. }
  801. if (declare_init_size > spectest_table_init_size
  802. || declare_max_size < spectest_table_max_size) {
  803. set_error_buf(error_buf, error_buf_size,
  804. "incompatible import type");
  805. return false;
  806. }
  807. declare_init_size = spectest_table_init_size;
  808. declare_max_size = spectest_table_max_size;
  809. }
  810. /* now we believe all declaration are ok */
  811. table->elem_type = declare_elem_type;
  812. table->init_size = declare_init_size;
  813. table->flags = declare_max_size_flag;
  814. table->max_size = declare_max_size;
  815. return true;
  816. fail:
  817. return false;
  818. }
  819. unsigned
  820. wasm_runtime_memory_pool_size();
  821. static bool
  822. check_memory_init_size(uint32 init_size,
  823. char *error_buf, uint32 error_buf_size)
  824. {
  825. if (init_size > 65536) {
  826. set_error_buf(error_buf, error_buf_size,
  827. "memory size must be at most 65536 pages (4GiB)");
  828. return false;
  829. }
  830. return true;
  831. }
  832. static bool
  833. check_memory_max_size(uint32 init_size, uint32 max_size,
  834. char *error_buf, uint32 error_buf_size)
  835. {
  836. if (max_size < init_size) {
  837. set_error_buf(error_buf, error_buf_size,
  838. "size minimum must not be greater than maximum");
  839. return false;
  840. }
  841. if (max_size > 65536) {
  842. set_error_buf(error_buf, error_buf_size,
  843. "memory size must be at most 65536 pages (4GiB)");
  844. return false;
  845. }
  846. return true;
  847. }
  848. static bool
  849. load_memory_import(WASMModule *sub_module, const char *sub_module_name,
  850. const char *memory_name, const uint8 **p_buf,
  851. const uint8 *buf_end, WASMMemoryImport *memory,
  852. char *error_buf, uint32 error_buf_size)
  853. {
  854. const uint8 *p = *p_buf, *p_end = buf_end;
  855. uint32 pool_size = wasm_runtime_memory_pool_size();
  856. #if WASM_ENABLE_APP_FRAMEWORK != 0
  857. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  858. / DEFAULT_NUM_BYTES_PER_PAGE;
  859. #else
  860. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  861. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  862. uint32 declare_max_page_count_flag = 0;
  863. uint32 declare_init_page_count = 0;
  864. uint32 declare_max_page_count = 0;
  865. #if WASM_ENABLE_MULTI_MODULE != 0
  866. WASMMemory *linked_memory = NULL;
  867. #endif
  868. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  869. read_leb_uint32(p, p_end, declare_init_page_count);
  870. if (!check_memory_init_size(declare_init_page_count, error_buf,
  871. error_buf_size)) {
  872. return false;
  873. }
  874. if (declare_max_page_count_flag & 1) {
  875. read_leb_uint32(p, p_end, declare_max_page_count);
  876. if (!check_memory_max_size(declare_init_page_count,
  877. declare_max_page_count, error_buf,
  878. error_buf_size)) {
  879. return false;
  880. }
  881. if (declare_max_page_count > max_page_count) {
  882. declare_max_page_count = max_page_count;
  883. }
  884. }
  885. else {
  886. /* Limit the maximum memory size to max_page_count */
  887. declare_max_page_count = max_page_count;
  888. }
  889. #if WASM_ENABLE_MULTI_MODULE != 0
  890. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  891. linked_memory = wasm_loader_resolve_memory(
  892. sub_module_name, memory_name,
  893. declare_init_page_count, declare_max_page_count,
  894. error_buf, error_buf_size);
  895. if (!linked_memory) {
  896. return false;
  897. }
  898. /**
  899. * reset with linked memory limit
  900. */
  901. memory->import_module = sub_module;
  902. memory->import_memory_linked = linked_memory;
  903. declare_init_page_count = linked_memory->init_page_count;
  904. declare_max_page_count = linked_memory->max_page_count;
  905. }
  906. #endif
  907. /* (memory (export "memory") 1 2) */
  908. if (!strcmp("spectest", sub_module_name)) {
  909. uint32 spectest_memory_init_page = 1;
  910. uint32 spectest_memory_max_page = 2;
  911. if (strcmp("memory", memory_name)) {
  912. set_error_buf(error_buf, error_buf_size,
  913. "incompatible import type or unknown import");
  914. return false;
  915. }
  916. if (declare_init_page_count > spectest_memory_init_page
  917. || declare_max_page_count < spectest_memory_max_page) {
  918. set_error_buf(error_buf, error_buf_size,
  919. "incompatible import type");
  920. return false;
  921. }
  922. declare_init_page_count = spectest_memory_init_page;
  923. declare_max_page_count = spectest_memory_max_page;
  924. }
  925. /* now we believe all declaration are ok */
  926. memory->flags = declare_max_page_count_flag;
  927. memory->init_page_count = declare_init_page_count;
  928. memory->max_page_count = declare_max_page_count;
  929. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  930. *p_buf = p;
  931. return true;
  932. fail:
  933. return false;
  934. }
  935. static bool
  936. load_global_import(const WASMModule *parent_module,
  937. WASMModule *sub_module,
  938. char *sub_module_name, char *global_name,
  939. const uint8 **p_buf, const uint8 *buf_end,
  940. WASMGlobalImport *global,
  941. char *error_buf, uint32 error_buf_size)
  942. {
  943. const uint8 *p = *p_buf, *p_end = buf_end;
  944. uint8 declare_type = 0;
  945. uint8 declare_mutable = 0;
  946. bool is_mutable = false;
  947. bool ret = false;
  948. CHECK_BUF(p, p_end, 2);
  949. declare_type = read_uint8(p);
  950. declare_mutable = read_uint8(p);
  951. *p_buf = p;
  952. if (declare_mutable >= 2) {
  953. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  954. return false;
  955. }
  956. is_mutable = declare_mutable & 1 ? true : false;
  957. #if WASM_ENABLE_LIBC_BUILTIN != 0
  958. ret = wasm_runtime_is_built_in_module(sub_module_name);
  959. if (ret) {
  960. /* check built-in modules */
  961. ret = wasm_native_lookup_libc_builtin_global(sub_module_name,
  962. global_name, global);
  963. if (ret) {
  964. LOG_DEBUG("(%s, %s) is a global of a built-in module",
  965. sub_module_name, global_name);
  966. }
  967. }
  968. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  969. #if WASM_ENABLE_MULTI_MODULE != 0
  970. if (!ret) {
  971. /* check sub modules */
  972. WASMGlobal *linked_global =
  973. wasm_loader_resolve_global(sub_module_name, global_name,
  974. declare_type, declare_mutable,
  975. error_buf, error_buf_size);
  976. if (linked_global) {
  977. LOG_DEBUG("(%s, %s) is a global of external module",
  978. sub_module_name, global_name);
  979. global->import_module = sub_module;
  980. global->import_global_linked = linked_global;
  981. ret = true;
  982. }
  983. }
  984. #endif
  985. if (!ret) {
  986. #if WASM_ENABLE_SPEC_TEST != 0
  987. set_error_buf(error_buf, error_buf_size,
  988. "unknown import or incompatible import type");
  989. return false;
  990. #endif
  991. }
  992. global->module_name = sub_module_name;
  993. global->field_name = global_name;
  994. global->type = declare_type;
  995. global->is_mutable = is_mutable;
  996. return true;
  997. fail:
  998. return false;
  999. }
  1000. static bool
  1001. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  1002. char *error_buf, uint32 error_buf_size)
  1003. {
  1004. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1005. CHECK_BUF(p, p_end, 1);
  1006. /* 0x70 */
  1007. table->elem_type = read_uint8(p);
  1008. if (TABLE_ELEM_TYPE_ANY_FUNC != table->elem_type) {
  1009. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1010. return false;
  1011. }
  1012. p_org = p;
  1013. read_leb_uint32(p, p_end, table->flags);
  1014. if (p - p_org > 1) {
  1015. set_error_buf(error_buf, error_buf_size,
  1016. "integer representation too long");
  1017. return false;
  1018. }
  1019. if (table->flags > 1) {
  1020. set_error_buf(error_buf, error_buf_size, "integer too large");
  1021. return false;
  1022. }
  1023. read_leb_uint32(p, p_end, table->init_size);
  1024. if (table->flags == 0) {
  1025. table->max_size = 0x10000;
  1026. }
  1027. else if (table->flags == 1) {
  1028. read_leb_uint32(p, p_end, table->max_size);
  1029. if (!check_table_max_size(table->init_size, table->max_size,
  1030. error_buf, error_buf_size))
  1031. return false;
  1032. }
  1033. *p_buf = p;
  1034. return true;
  1035. fail:
  1036. return false;
  1037. }
  1038. static bool
  1039. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  1040. char *error_buf, uint32 error_buf_size)
  1041. {
  1042. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1043. uint32 pool_size = wasm_runtime_memory_pool_size();
  1044. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1045. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1046. / DEFAULT_NUM_BYTES_PER_PAGE;
  1047. #else
  1048. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  1049. #endif
  1050. p_org = p;
  1051. read_leb_uint32(p, p_end, memory->flags);
  1052. if (p - p_org > 1) {
  1053. set_error_buf(error_buf, error_buf_size,
  1054. "integer representation too long");
  1055. return false;
  1056. }
  1057. #if WASM_ENABLE_SHARED_MEMORY == 0
  1058. if (memory->flags > 1) {
  1059. set_error_buf(error_buf, error_buf_size, "integer too large");
  1060. return false;
  1061. }
  1062. #else
  1063. if (memory->flags > 3) {
  1064. set_error_buf(error_buf, error_buf_size, "integer too large");
  1065. return false;
  1066. }
  1067. else if (memory->flags == 2) {
  1068. set_error_buf(error_buf, error_buf_size, "shared memory must have maximum");
  1069. return false;
  1070. }
  1071. #endif
  1072. read_leb_uint32(p, p_end, memory->init_page_count);
  1073. if (!check_memory_init_size(memory->init_page_count,
  1074. error_buf, error_buf_size))
  1075. return false;
  1076. if (memory->flags & 1) {
  1077. read_leb_uint32(p, p_end, memory->max_page_count);
  1078. if (!check_memory_max_size(memory->init_page_count,
  1079. memory->max_page_count,
  1080. error_buf, error_buf_size))
  1081. return false;
  1082. if (memory->max_page_count > max_page_count)
  1083. memory->max_page_count = max_page_count;
  1084. }
  1085. else {
  1086. /* Limit the maximum memory size to max_page_count */
  1087. memory->max_page_count = max_page_count;
  1088. }
  1089. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1090. *p_buf = p;
  1091. return true;
  1092. fail:
  1093. return false;
  1094. }
  1095. #if WASM_ENABLE_MULTI_MODULE != 0
  1096. static WASMModule *
  1097. search_sub_module(const WASMModule *parent_module, const char *sub_module_name)
  1098. {
  1099. WASMRegisteredModule *node =
  1100. bh_list_first_elem(parent_module->import_module_list);
  1101. while (node && strcmp(sub_module_name, node->module_name)) {
  1102. node = bh_list_elem_next(node);
  1103. }
  1104. return node ? (WASMModule*)node->module : NULL;
  1105. }
  1106. static bool
  1107. register_sub_module(const WASMModule *parent_module,
  1108. const char *sub_module_name, WASMModule *sub_module)
  1109. {
  1110. /* register a sub_module on its parent sub module list */
  1111. WASMRegisteredModule *node = NULL;
  1112. bh_list_status ret;
  1113. if (search_sub_module(parent_module, sub_module_name)) {
  1114. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  1115. return true;
  1116. }
  1117. node = wasm_runtime_malloc(sizeof(WASMRegisteredModule));
  1118. if (!node) {
  1119. LOG_DEBUG("malloc WASMRegisteredModule failed. SZ %d\n",
  1120. sizeof(WASMRegisteredModule));
  1121. return false;
  1122. }
  1123. node->module_name = sub_module_name;
  1124. node->module = (WASMModuleCommon*)sub_module;
  1125. ret = bh_list_insert(parent_module->import_module_list, node);
  1126. bh_assert(BH_LIST_SUCCESS == ret);
  1127. (void)ret;
  1128. return true;
  1129. }
  1130. static WASMModule *
  1131. load_depended_module(const WASMModule *parent_module,
  1132. const char *sub_module_name, char *error_buf,
  1133. uint32 error_buf_size)
  1134. {
  1135. WASMModule *sub_module = NULL;
  1136. bool ret = false;
  1137. uint8 *buffer = NULL;
  1138. uint32 buffer_size = 0;
  1139. const module_reader reader = wasm_runtime_get_module_reader();
  1140. const module_destroyer destroyer = wasm_runtime_get_module_destroyer();
  1141. /* check the registered module list of the parent */
  1142. sub_module = search_sub_module(parent_module, sub_module_name);
  1143. if (sub_module) {
  1144. LOG_DEBUG("%s has been loaded before", sub_module_name);
  1145. return sub_module;
  1146. }
  1147. /* check the global registered module list */
  1148. sub_module =
  1149. (WASMModule *)wasm_runtime_find_module_registered(sub_module_name);
  1150. if (sub_module) {
  1151. LOG_DEBUG("%s has been loaded", sub_module_name);
  1152. goto REGISTER_SUB_MODULE;
  1153. }
  1154. LOG_VERBOSE("to load %s", sub_module_name);
  1155. if (!reader) {
  1156. LOG_DEBUG("error: there is no sub_module reader to load %s",
  1157. sub_module_name);
  1158. set_error_buf_v(error_buf, error_buf_size,
  1159. "no sub module reader to load %s",
  1160. sub_module_name);
  1161. return NULL;
  1162. }
  1163. /* start to maintain a loading module list */
  1164. ret = wasm_runtime_is_loading_module(sub_module_name);
  1165. if (ret) {
  1166. LOG_DEBUG("find a circular dependency on %s", sub_module_name);
  1167. set_error_buf_v(error_buf, error_buf_size,
  1168. "found circular dependency on %s",
  1169. sub_module_name);
  1170. return NULL;
  1171. }
  1172. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  1173. error_buf_size);
  1174. if (!ret) {
  1175. LOG_DEBUG("can not add %s into loading module list\n",
  1176. sub_module_name);
  1177. return NULL;
  1178. }
  1179. ret = reader(sub_module_name, &buffer, &buffer_size);
  1180. if (!ret) {
  1181. LOG_DEBUG("read the file of %s failed", sub_module_name);
  1182. set_error_buf_v(error_buf, error_buf_size,
  1183. "failed to read module file of %s",
  1184. sub_module_name);
  1185. goto DELETE_FROM_LOADING_LIST;
  1186. }
  1187. sub_module =
  1188. wasm_loader_load(buffer, buffer_size, error_buf, error_buf_size);
  1189. if (!sub_module) {
  1190. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  1191. /*
  1192. * others will be destroyed in runtime_destroy()
  1193. */
  1194. goto DESTROY_FILE_BUFFER;
  1195. }
  1196. wasm_runtime_delete_loading_module(sub_module_name);
  1197. /* register on a global list */
  1198. ret = wasm_runtime_register_module_internal(sub_module_name,
  1199. (WASMModuleCommon*)sub_module,
  1200. buffer, buffer_size, error_buf,
  1201. error_buf_size);
  1202. if (!ret) {
  1203. LOG_DEBUG("error: can not register module %s globally\n",
  1204. sub_module_name);
  1205. /*
  1206. * others will be unload in runtime_destroy()
  1207. */
  1208. goto UNLOAD_MODULE;
  1209. }
  1210. /* register on its parent list */
  1211. REGISTER_SUB_MODULE:
  1212. ret = register_sub_module(parent_module, sub_module_name, sub_module);
  1213. if (!ret) {
  1214. set_error_buf_v(error_buf, error_buf_size,
  1215. "failed to register sub module %s",
  1216. sub_module_name);
  1217. /*
  1218. * since it is in the global module list, there is no need to
  1219. * unload the module. the runtime_destroy() will do it
  1220. */
  1221. return NULL;
  1222. }
  1223. return sub_module;
  1224. UNLOAD_MODULE:
  1225. wasm_loader_unload(sub_module);
  1226. DESTROY_FILE_BUFFER:
  1227. if (destroyer) {
  1228. destroyer(buffer, buffer_size);
  1229. }
  1230. else {
  1231. LOG_WARNING("need to release the reading buffer of %s manually",
  1232. sub_module_name);
  1233. }
  1234. DELETE_FROM_LOADING_LIST:
  1235. wasm_runtime_delete_loading_module(sub_module_name);
  1236. return NULL;
  1237. }
  1238. #endif /* WASM_ENABLE_MULTI_MODULE */
  1239. static bool
  1240. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1241. char *error_buf, uint32 error_buf_size)
  1242. {
  1243. const uint8 *p = buf, *p_end = buf_end, *p_old;
  1244. uint32 import_count, name_len, type_index, i, u32, flags;
  1245. uint64 total_size;
  1246. WASMImport *import;
  1247. WASMImport *import_functions = NULL, *import_tables = NULL;
  1248. WASMImport *import_memories = NULL, *import_globals = NULL;
  1249. char *sub_module_name, *field_name;
  1250. uint8 u8, kind;
  1251. read_leb_uint32(p, p_end, import_count);
  1252. if (import_count) {
  1253. module->import_count = import_count;
  1254. total_size = sizeof(WASMImport) * (uint64)import_count;
  1255. if (!(module->imports = loader_malloc
  1256. (total_size, error_buf, error_buf_size))) {
  1257. return false;
  1258. }
  1259. p_old = p;
  1260. /* Scan firstly to get import count of each type */
  1261. for (i = 0; i < import_count; i++) {
  1262. /* module name */
  1263. read_leb_uint32(p, p_end, name_len);
  1264. CHECK_BUF(p, p_end, name_len);
  1265. p += name_len;
  1266. /* field name */
  1267. read_leb_uint32(p, p_end, name_len);
  1268. CHECK_BUF(p, p_end, name_len);
  1269. p += name_len;
  1270. CHECK_BUF(p, p_end, 1);
  1271. /* 0x00/0x01/0x02/0x03 */
  1272. kind = read_uint8(p);
  1273. switch (kind) {
  1274. case IMPORT_KIND_FUNC: /* import function */
  1275. read_leb_uint32(p, p_end, type_index);
  1276. module->import_function_count++;
  1277. break;
  1278. case IMPORT_KIND_TABLE: /* import table */
  1279. CHECK_BUF(p, p_end, 1);
  1280. /* 0x70 */
  1281. u8 = read_uint8(p);
  1282. read_leb_uint32(p, p_end, flags);
  1283. read_leb_uint32(p, p_end, u32);
  1284. if (flags & 1)
  1285. read_leb_uint32(p, p_end, u32);
  1286. module->import_table_count++;
  1287. if (module->import_table_count > 1) {
  1288. set_error_buf(error_buf, error_buf_size,
  1289. "multiple tables");
  1290. return false;
  1291. }
  1292. break;
  1293. case IMPORT_KIND_MEMORY: /* import memory */
  1294. read_leb_uint32(p, p_end, flags);
  1295. read_leb_uint32(p, p_end, u32);
  1296. if (flags & 1)
  1297. read_leb_uint32(p, p_end, u32);
  1298. module->import_memory_count++;
  1299. if (module->import_memory_count > 1) {
  1300. set_error_buf(error_buf, error_buf_size,
  1301. "multiple memories");
  1302. return false;
  1303. }
  1304. break;
  1305. case IMPORT_KIND_GLOBAL: /* import global */
  1306. CHECK_BUF(p, p_end, 2);
  1307. p += 2;
  1308. module->import_global_count++;
  1309. break;
  1310. default:
  1311. set_error_buf(error_buf, error_buf_size,
  1312. "invalid import kind");
  1313. return false;
  1314. }
  1315. }
  1316. if (module->import_function_count)
  1317. import_functions = module->import_functions = module->imports;
  1318. if (module->import_table_count)
  1319. import_tables = module->import_tables =
  1320. module->imports + module->import_function_count;
  1321. if (module->import_memory_count)
  1322. import_memories = module->import_memories =
  1323. module->imports + module->import_function_count + module->import_table_count;
  1324. if (module->import_global_count)
  1325. import_globals = module->import_globals =
  1326. module->imports + module->import_function_count + module->import_table_count
  1327. + module->import_memory_count;
  1328. p = p_old;
  1329. /* TODO: move it out of the loop */
  1330. /* insert "env", "wasi_unstable" and "wasi_snapshot_preview1" to const str list */
  1331. if (!const_str_list_insert((uint8*)"env", 3, module, error_buf, error_buf_size)
  1332. || !const_str_list_insert((uint8*)"wasi_unstable", 13, module,
  1333. error_buf, error_buf_size)
  1334. || !const_str_list_insert((uint8*)"wasi_snapshot_preview1", 22, module,
  1335. error_buf, error_buf_size)) {
  1336. return false;
  1337. }
  1338. /* Scan again to read the data */
  1339. for (i = 0; i < import_count; i++) {
  1340. WASMModule *sub_module = NULL;
  1341. /* load module name */
  1342. read_leb_uint32(p, p_end, name_len);
  1343. CHECK_BUF(p, p_end, name_len);
  1344. if (!(sub_module_name = const_str_list_insert(
  1345. p, name_len, module, error_buf, error_buf_size))) {
  1346. return false;
  1347. }
  1348. p += name_len;
  1349. /* load field name */
  1350. read_leb_uint32(p, p_end, name_len);
  1351. CHECK_BUF(p, p_end, name_len);
  1352. if (!(field_name = const_str_list_insert(
  1353. p, name_len, module, error_buf, error_buf_size))) {
  1354. return false;
  1355. }
  1356. p += name_len;
  1357. LOG_DEBUG("import #%d: (%s, %s)", i, sub_module_name, field_name);
  1358. #if WASM_ENABLE_MULTI_MODULE != 0
  1359. /* assume built-in modules have been loaded */
  1360. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1361. LOG_DEBUG("%s is an exported field of a %s", field_name,
  1362. sub_module_name);
  1363. /*
  1364. * if it returns well, guarantee that
  1365. * the sub_module_name and its dependencies
  1366. * have been loaded well
  1367. */
  1368. sub_module = load_depended_module(module, sub_module_name,
  1369. error_buf, error_buf_size);
  1370. if (!sub_module) {
  1371. return false;
  1372. }
  1373. }
  1374. #endif
  1375. CHECK_BUF(p, p_end, 1);
  1376. /* 0x00/0x01/0x02/0x03 */
  1377. kind = read_uint8(p);
  1378. switch (kind) {
  1379. case IMPORT_KIND_FUNC: /* import function */
  1380. bh_assert(import_functions);
  1381. import = import_functions++;
  1382. if (!load_function_import(module, sub_module,
  1383. sub_module_name, field_name, &p,
  1384. p_end, &import->u.function,
  1385. error_buf, error_buf_size)) {
  1386. return false;
  1387. }
  1388. break;
  1389. case IMPORT_KIND_TABLE: /* import table */
  1390. bh_assert(import_tables);
  1391. import = import_tables++;
  1392. if (!load_table_import(sub_module,
  1393. sub_module_name,
  1394. field_name,
  1395. &p,
  1396. p_end,
  1397. &import->u.table,
  1398. error_buf,
  1399. error_buf_size)) {
  1400. LOG_DEBUG("can not import such a table (%s,%s)",
  1401. sub_module_name, field_name);
  1402. return false;
  1403. }
  1404. break;
  1405. case IMPORT_KIND_MEMORY: /* import memory */
  1406. bh_assert(import_memories);
  1407. import = import_memories++;
  1408. if (!load_memory_import(sub_module,
  1409. sub_module_name,
  1410. field_name,
  1411. &p,
  1412. p_end,
  1413. &import->u.memory,
  1414. error_buf,
  1415. error_buf_size)) {
  1416. return false;
  1417. }
  1418. break;
  1419. case IMPORT_KIND_GLOBAL: /* import global */
  1420. bh_assert(import_globals);
  1421. import = import_globals++;
  1422. if (!load_global_import(module, sub_module,
  1423. sub_module_name, field_name,
  1424. &p, p_end, &import->u.global,
  1425. error_buf, error_buf_size)) {
  1426. return false;
  1427. }
  1428. break;
  1429. default:
  1430. set_error_buf(error_buf, error_buf_size,
  1431. "invalid import kind");
  1432. return false;
  1433. }
  1434. import->kind = kind;
  1435. import->u.names.module_name = sub_module_name;
  1436. import->u.names.field_name = field_name;
  1437. (void)sub_module;
  1438. }
  1439. #if WASM_ENABLE_LIBC_WASI != 0
  1440. import = module->import_functions;
  1441. for (i = 0; i < module->import_function_count; i++, import++) {
  1442. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  1443. || !strcmp(import->u.names.module_name, "wasi_snapshot_preview1")) {
  1444. module->is_wasi_module = true;
  1445. break;
  1446. }
  1447. }
  1448. #endif
  1449. }
  1450. if (p != p_end) {
  1451. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1452. return false;
  1453. }
  1454. LOG_VERBOSE("Load import section success.\n");
  1455. (void)u8;
  1456. (void)u32;
  1457. (void)type_index;
  1458. return true;
  1459. fail:
  1460. return false;
  1461. }
  1462. static bool
  1463. init_function_local_offsets(WASMFunction *func,
  1464. char *error_buf, uint32 error_buf_size)
  1465. {
  1466. WASMType *param_type = func->func_type;
  1467. uint32 param_count = param_type->param_count;
  1468. uint8 *param_types = param_type->types;
  1469. uint32 local_count = func->local_count;
  1470. uint8 *local_types = func->local_types;
  1471. uint32 i, local_offset = 0;
  1472. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  1473. if (!(func->local_offsets =
  1474. loader_malloc(total_size, error_buf, error_buf_size))) {
  1475. return false;
  1476. }
  1477. for (i = 0; i < param_count; i++) {
  1478. func->local_offsets[i] = (uint16)local_offset;
  1479. local_offset += wasm_value_type_cell_num(param_types[i]);
  1480. }
  1481. for (i = 0; i < local_count; i++) {
  1482. func->local_offsets[param_count + i] = (uint16)local_offset;
  1483. local_offset += wasm_value_type_cell_num(local_types[i]);
  1484. }
  1485. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  1486. return true;
  1487. }
  1488. static bool
  1489. load_function_section(const uint8 *buf, const uint8 *buf_end,
  1490. const uint8 *buf_code, const uint8 *buf_code_end,
  1491. WASMModule *module,
  1492. char *error_buf, uint32 error_buf_size)
  1493. {
  1494. const uint8 *p = buf, *p_end = buf_end;
  1495. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  1496. uint32 func_count;
  1497. uint64 total_size;
  1498. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  1499. uint32 local_count, local_set_count, sub_local_count;
  1500. uint8 type;
  1501. WASMFunction *func;
  1502. read_leb_uint32(p, p_end, func_count);
  1503. if (buf_code)
  1504. read_leb_uint32(p_code, buf_code_end, code_count);
  1505. if (func_count != code_count) {
  1506. set_error_buf(error_buf, error_buf_size,
  1507. "function and code section have inconsistent lengths");
  1508. return false;
  1509. }
  1510. if (func_count) {
  1511. module->function_count = func_count;
  1512. total_size = sizeof(WASMFunction*) * (uint64)func_count;
  1513. if (!(module->functions =
  1514. loader_malloc(total_size, error_buf, error_buf_size))) {
  1515. return false;
  1516. }
  1517. for (i = 0; i < func_count; i++) {
  1518. /* Resolve function type */
  1519. read_leb_uint32(p, p_end, type_index);
  1520. if (type_index >= module->type_count) {
  1521. set_error_buf(error_buf, error_buf_size, "unknown type");
  1522. return false;
  1523. }
  1524. read_leb_uint32(p_code, buf_code_end, code_size);
  1525. if (code_size == 0
  1526. || p_code + code_size > buf_code_end) {
  1527. set_error_buf(error_buf, error_buf_size,
  1528. "invalid function code size");
  1529. return false;
  1530. }
  1531. /* Resolve local set count */
  1532. p_code_end = p_code + code_size;
  1533. local_count = 0;
  1534. read_leb_uint32(p_code, buf_code_end, local_set_count);
  1535. p_code_save = p_code;
  1536. /* Calculate total local count */
  1537. for (j = 0; j < local_set_count; j++) {
  1538. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1539. if (sub_local_count > UINT32_MAX - local_count) {
  1540. set_error_buf(error_buf, error_buf_size,
  1541. "too many locals");
  1542. return false;
  1543. }
  1544. CHECK_BUF(p_code, buf_code_end, 1);
  1545. /* 0x7F/0x7E/0x7D/0x7C */
  1546. type = read_uint8(p_code);
  1547. local_count += sub_local_count;
  1548. }
  1549. /* Alloc memory, layout: function structure + local types */
  1550. code_size = (uint32)(p_code_end - p_code);
  1551. total_size = sizeof(WASMFunction) + (uint64)local_count;
  1552. if (!(func = module->functions[i] =
  1553. loader_malloc(total_size, error_buf, error_buf_size))) {
  1554. return false;
  1555. }
  1556. /* Set function type, local count, code size and code body */
  1557. func->func_type = module->types[type_index];
  1558. func->local_count = local_count;
  1559. if (local_count > 0)
  1560. func->local_types = (uint8*)func + sizeof(WASMFunction);
  1561. func->code_size = code_size;
  1562. /*
  1563. * we shall make a copy of code body [p_code, p_code + code_size]
  1564. * when we are worrying about inappropriate releasing behaviour.
  1565. * all code bodies are actually in a buffer which user allocates in
  1566. * his embedding environment and we don't have power on them.
  1567. * it will be like:
  1568. * code_body_cp = malloc(code_size);
  1569. * memcpy(code_body_cp, p_code, code_size);
  1570. * func->code = code_body_cp;
  1571. */
  1572. func->code = (uint8*)p_code;
  1573. /* Load each local type */
  1574. p_code = p_code_save;
  1575. local_type_index = 0;
  1576. for (j = 0; j < local_set_count; j++) {
  1577. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1578. if (local_type_index + sub_local_count <= local_type_index
  1579. || local_type_index + sub_local_count > local_count) {
  1580. set_error_buf(error_buf, error_buf_size,
  1581. "invalid local count");
  1582. return false;
  1583. }
  1584. CHECK_BUF(p_code, buf_code_end, 1);
  1585. /* 0x7F/0x7E/0x7D/0x7C */
  1586. type = read_uint8(p_code);
  1587. if (type < VALUE_TYPE_F64 || type > VALUE_TYPE_I32) {
  1588. set_error_buf(error_buf, error_buf_size,
  1589. "invalid local type");
  1590. return false;
  1591. }
  1592. for (k = 0; k < sub_local_count; k++) {
  1593. func->local_types[local_type_index++] = type;
  1594. }
  1595. }
  1596. func->param_cell_num = func->func_type->param_cell_num;
  1597. func->ret_cell_num = func->func_type->ret_cell_num;
  1598. func->local_cell_num =
  1599. wasm_get_cell_num(func->local_types, func->local_count);
  1600. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  1601. return false;
  1602. p_code = p_code_end;
  1603. }
  1604. }
  1605. if (p != p_end) {
  1606. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1607. return false;
  1608. }
  1609. LOG_VERBOSE("Load function section success.\n");
  1610. return true;
  1611. fail:
  1612. return false;
  1613. }
  1614. static bool
  1615. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1616. char *error_buf, uint32 error_buf_size)
  1617. {
  1618. const uint8 *p = buf, *p_end = buf_end;
  1619. uint32 table_count, i;
  1620. uint64 total_size;
  1621. WASMTable *table;
  1622. read_leb_uint32(p, p_end, table_count);
  1623. /* a total of one table is allowed */
  1624. if (module->import_table_count + table_count > 1) {
  1625. set_error_buf(error_buf, error_buf_size, "multiple tables");
  1626. return false;
  1627. }
  1628. if (table_count) {
  1629. module->table_count = table_count;
  1630. total_size = sizeof(WASMTable) * (uint64)table_count;
  1631. if (!(module->tables = loader_malloc
  1632. (total_size, error_buf, error_buf_size))) {
  1633. return false;
  1634. }
  1635. /* load each table */
  1636. table = module->tables;
  1637. for (i = 0; i < table_count; i++, table++)
  1638. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  1639. return false;
  1640. }
  1641. if (p != p_end) {
  1642. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1643. return false;
  1644. }
  1645. LOG_VERBOSE("Load table section success.\n");
  1646. return true;
  1647. fail:
  1648. return false;
  1649. }
  1650. static bool
  1651. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1652. char *error_buf, uint32 error_buf_size)
  1653. {
  1654. const uint8 *p = buf, *p_end = buf_end;
  1655. uint32 memory_count, i;
  1656. uint64 total_size;
  1657. WASMMemory *memory;
  1658. read_leb_uint32(p, p_end, memory_count);
  1659. /* a total of one memory is allowed */
  1660. if (module->import_memory_count + memory_count > 1) {
  1661. set_error_buf(error_buf, error_buf_size, "multiple memories");
  1662. return false;
  1663. }
  1664. if (memory_count) {
  1665. module->memory_count = memory_count;
  1666. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  1667. if (!(module->memories = loader_malloc
  1668. (total_size, error_buf, error_buf_size))) {
  1669. return false;
  1670. }
  1671. /* load each memory */
  1672. memory = module->memories;
  1673. for (i = 0; i < memory_count; i++, memory++)
  1674. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  1675. return false;
  1676. }
  1677. if (p != p_end) {
  1678. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1679. return false;
  1680. }
  1681. LOG_VERBOSE("Load memory section success.\n");
  1682. return true;
  1683. fail:
  1684. return false;
  1685. }
  1686. static bool
  1687. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1688. char *error_buf, uint32 error_buf_size)
  1689. {
  1690. const uint8 *p = buf, *p_end = buf_end;
  1691. uint32 global_count, i;
  1692. uint64 total_size;
  1693. WASMGlobal *global;
  1694. uint8 mutable;
  1695. read_leb_uint32(p, p_end, global_count);
  1696. if (global_count) {
  1697. module->global_count = global_count;
  1698. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  1699. if (!(module->globals = loader_malloc
  1700. (total_size, error_buf, error_buf_size))) {
  1701. return false;
  1702. }
  1703. global = module->globals;
  1704. for(i = 0; i < global_count; i++, global++) {
  1705. CHECK_BUF(p, p_end, 2);
  1706. global->type = read_uint8(p);
  1707. mutable = read_uint8(p);
  1708. if (mutable >= 2) {
  1709. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1710. return false;
  1711. }
  1712. global->is_mutable = mutable ? true : false;
  1713. /* initialize expression */
  1714. if (!load_init_expr(&p, p_end, &(global->init_expr),
  1715. global->type, error_buf, error_buf_size))
  1716. return false;
  1717. if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
  1718. /**
  1719. * Currently, constant expressions occurring as initializers
  1720. * of globals are further constrained in that contained
  1721. * global.get instructions are
  1722. * only allowed to refer to imported globals.
  1723. */
  1724. uint32 target_global_index = global->init_expr.u.global_index;
  1725. if (target_global_index >= module->import_global_count) {
  1726. set_error_buf(error_buf, error_buf_size, "unknown global");
  1727. return false;
  1728. }
  1729. }
  1730. }
  1731. }
  1732. if (p != p_end) {
  1733. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1734. return false;
  1735. }
  1736. LOG_VERBOSE("Load global section success.\n");
  1737. return true;
  1738. fail:
  1739. return false;
  1740. }
  1741. static bool
  1742. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1743. char *error_buf, uint32 error_buf_size)
  1744. {
  1745. const uint8 *p = buf, *p_end = buf_end;
  1746. uint32 export_count, i, j, index;
  1747. uint64 total_size;
  1748. uint32 str_len;
  1749. WASMExport *export;
  1750. const char *name;
  1751. read_leb_uint32(p, p_end, export_count);
  1752. if (export_count) {
  1753. module->export_count = export_count;
  1754. total_size = sizeof(WASMExport) * (uint64)export_count;
  1755. if (!(module->exports = loader_malloc
  1756. (total_size, error_buf, error_buf_size))) {
  1757. return false;
  1758. }
  1759. export = module->exports;
  1760. for (i = 0; i < export_count; i++, export++) {
  1761. read_leb_uint32(p, p_end, str_len);
  1762. CHECK_BUF(p, p_end, str_len);
  1763. for (j = 0; j < i; j++) {
  1764. name = module->exports[j].name;
  1765. if (strlen(name) == str_len
  1766. && memcmp(name, p, str_len) == 0) {
  1767. set_error_buf(error_buf, error_buf_size,
  1768. "duplicate export name");
  1769. return false;
  1770. }
  1771. }
  1772. if (!(export->name = const_str_list_insert(p, str_len, module,
  1773. error_buf, error_buf_size))) {
  1774. return false;
  1775. }
  1776. p += str_len;
  1777. CHECK_BUF(p, p_end, 1);
  1778. export->kind = read_uint8(p);
  1779. read_leb_uint32(p, p_end, index);
  1780. export->index = index;
  1781. switch(export->kind) {
  1782. /*function index*/
  1783. case EXPORT_KIND_FUNC:
  1784. if (index >= module->function_count
  1785. + module->import_function_count) {
  1786. set_error_buf(error_buf, error_buf_size,
  1787. "unknown function");
  1788. return false;
  1789. }
  1790. break;
  1791. /*table index*/
  1792. case EXPORT_KIND_TABLE:
  1793. if (index >= module->table_count
  1794. + module->import_table_count) {
  1795. set_error_buf(error_buf, error_buf_size,
  1796. "unknown table");
  1797. return false;
  1798. }
  1799. break;
  1800. /*memory index*/
  1801. case EXPORT_KIND_MEMORY:
  1802. if (index >= module->memory_count
  1803. + module->import_memory_count) {
  1804. set_error_buf(error_buf, error_buf_size,
  1805. "unknown memory");
  1806. return false;
  1807. }
  1808. break;
  1809. /*global index*/
  1810. case EXPORT_KIND_GLOBAL:
  1811. if (index >= module->global_count
  1812. + module->import_global_count) {
  1813. set_error_buf(error_buf, error_buf_size,
  1814. "unknown global");
  1815. return false;
  1816. }
  1817. break;
  1818. default:
  1819. set_error_buf(error_buf, error_buf_size,
  1820. "invalid export kind");
  1821. return false;
  1822. }
  1823. }
  1824. }
  1825. if (p != p_end) {
  1826. set_error_buf(error_buf, error_buf_size,
  1827. "section size mismatch");
  1828. return false;
  1829. }
  1830. LOG_VERBOSE("Load export section success.\n");
  1831. return true;
  1832. fail:
  1833. return false;
  1834. }
  1835. static bool
  1836. load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1837. char *error_buf, uint32 error_buf_size)
  1838. {
  1839. const uint8 *p = buf, *p_end = buf_end;
  1840. uint32 table_segment_count, i, j, table_index, function_count, function_index;
  1841. uint64 total_size;
  1842. WASMTableSeg *table_segment;
  1843. read_leb_uint32(p, p_end, table_segment_count);
  1844. if (table_segment_count) {
  1845. module->table_seg_count = table_segment_count;
  1846. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  1847. if (!(module->table_segments = loader_malloc
  1848. (total_size, error_buf, error_buf_size))) {
  1849. return false;
  1850. }
  1851. table_segment = module->table_segments;
  1852. for (i = 0; i < table_segment_count; i++, table_segment++) {
  1853. if (p >= p_end) {
  1854. set_error_buf(error_buf, error_buf_size, "unexpected end");
  1855. return false;
  1856. }
  1857. read_leb_uint32(p, p_end, table_index);
  1858. if (table_index
  1859. >= module->import_table_count + module->table_count) {
  1860. LOG_DEBUG("table#%d does not exist", table_index);
  1861. set_error_buf(error_buf, error_buf_size, "unknown table");
  1862. return false;
  1863. }
  1864. table_segment->table_index = table_index;
  1865. /* initialize expression */
  1866. if (!load_init_expr(&p, p_end, &(table_segment->base_offset),
  1867. VALUE_TYPE_I32, error_buf, error_buf_size))
  1868. return false;
  1869. read_leb_uint32(p, p_end, function_count);
  1870. table_segment->function_count = function_count;
  1871. total_size = sizeof(uint32) * (uint64)function_count;
  1872. if (!(table_segment->func_indexes = (uint32 *)
  1873. loader_malloc(total_size, error_buf, error_buf_size))) {
  1874. return false;
  1875. }
  1876. for (j = 0; j < function_count; j++) {
  1877. read_leb_uint32(p, p_end, function_index);
  1878. if (function_index >= module->import_function_count
  1879. + module->function_count) {
  1880. set_error_buf(error_buf, error_buf_size,
  1881. "unknown function");
  1882. return false;
  1883. }
  1884. table_segment->func_indexes[j] = function_index;
  1885. }
  1886. }
  1887. }
  1888. if (p != p_end) {
  1889. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1890. return false;
  1891. }
  1892. LOG_VERBOSE("Load table segment section success.\n");
  1893. return true;
  1894. fail:
  1895. return false;
  1896. }
  1897. static bool
  1898. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  1899. WASMModule *module,
  1900. char *error_buf, uint32 error_buf_size)
  1901. {
  1902. const uint8 *p = buf, *p_end = buf_end;
  1903. uint32 data_seg_count, i, mem_index, data_seg_len;
  1904. uint64 total_size;
  1905. WASMDataSeg *dataseg;
  1906. InitializerExpression init_expr;
  1907. #if WASM_ENABLE_BULK_MEMORY != 0
  1908. bool is_passive = false;
  1909. uint32 mem_flag;
  1910. #endif
  1911. read_leb_uint32(p, p_end, data_seg_count);
  1912. #if WASM_ENABLE_BULK_MEMORY != 0
  1913. if ((module->data_seg_count1 != 0)
  1914. && (data_seg_count != module->data_seg_count1)) {
  1915. set_error_buf(error_buf, error_buf_size,
  1916. "data count and data section have inconsistent lengths");
  1917. return false;
  1918. }
  1919. #endif
  1920. if (data_seg_count) {
  1921. module->data_seg_count = data_seg_count;
  1922. total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count;
  1923. if (!(module->data_segments = loader_malloc
  1924. (total_size, error_buf, error_buf_size))) {
  1925. return false;
  1926. }
  1927. for (i = 0; i < data_seg_count; i++) {
  1928. read_leb_uint32(p, p_end, mem_index);
  1929. #if WASM_ENABLE_BULK_MEMORY != 0
  1930. is_passive = false;
  1931. mem_flag = mem_index & 0x03;
  1932. switch (mem_flag) {
  1933. case 0x01:
  1934. is_passive = true;
  1935. break;
  1936. case 0x00:
  1937. /* no memory index, treat index as 0 */
  1938. mem_index = 0;
  1939. goto check_mem_index;
  1940. case 0x02:
  1941. /* read following memory index */
  1942. read_leb_uint32(p, p_end, mem_index);
  1943. check_mem_index:
  1944. if (mem_index
  1945. >= module->import_memory_count + module->memory_count) {
  1946. LOG_DEBUG("memory#%d does not exist", mem_index);
  1947. set_error_buf(error_buf, error_buf_size, "unknown memory");
  1948. return false;
  1949. }
  1950. break;
  1951. case 0x03:
  1952. default:
  1953. set_error_buf(error_buf, error_buf_size, "unknown memory");
  1954. return false;
  1955. break;
  1956. }
  1957. #else
  1958. if (mem_index
  1959. >= module->import_memory_count + module->memory_count) {
  1960. LOG_DEBUG("memory#%d does not exist", mem_index);
  1961. set_error_buf(error_buf, error_buf_size, "unknown memory");
  1962. return false;
  1963. }
  1964. #endif /* WASM_ENABLE_BULK_MEMORY */
  1965. #if WASM_ENABLE_BULK_MEMORY != 0
  1966. if (!is_passive)
  1967. #endif
  1968. if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32,
  1969. error_buf, error_buf_size))
  1970. return false;
  1971. read_leb_uint32(p, p_end, data_seg_len);
  1972. if (!(dataseg = module->data_segments[i] = loader_malloc
  1973. (sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  1974. return false;
  1975. }
  1976. #if WASM_ENABLE_BULK_MEMORY != 0
  1977. dataseg->is_passive = is_passive;
  1978. if (!is_passive)
  1979. #endif
  1980. {
  1981. bh_memcpy_s(&dataseg->base_offset, sizeof(InitializerExpression),
  1982. &init_expr, sizeof(InitializerExpression));
  1983. dataseg->memory_index = mem_index;
  1984. }
  1985. dataseg->data_length = data_seg_len;
  1986. CHECK_BUF(p, p_end, data_seg_len);
  1987. dataseg->data = (uint8*)p;
  1988. p += data_seg_len;
  1989. }
  1990. }
  1991. if (p != p_end) {
  1992. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1993. return false;
  1994. }
  1995. LOG_VERBOSE("Load data segment section success.\n");
  1996. return true;
  1997. fail:
  1998. return false;
  1999. }
  2000. #if WASM_ENABLE_BULK_MEMORY != 0
  2001. static bool
  2002. load_datacount_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2003. char *error_buf, uint32 error_buf_size)
  2004. {
  2005. const uint8 *p = buf, *p_end = buf_end;
  2006. uint32 data_seg_count1 = 0;
  2007. read_leb_uint32(p, p_end, data_seg_count1);
  2008. module->data_seg_count1 = data_seg_count1;
  2009. if (p != p_end) {
  2010. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2011. return false;
  2012. }
  2013. LOG_VERBOSE("Load datacount section success.\n");
  2014. return true;
  2015. fail:
  2016. return false;
  2017. }
  2018. #endif
  2019. static bool
  2020. load_code_section(const uint8 *buf, const uint8 *buf_end,
  2021. const uint8 *buf_func,
  2022. const uint8 *buf_func_end,
  2023. WASMModule *module,
  2024. char *error_buf, uint32 error_buf_size)
  2025. {
  2026. const uint8 *p = buf, *p_end = buf_end;
  2027. const uint8 *p_func = buf_func;
  2028. uint32 func_count = 0, code_count;
  2029. /* code has been loaded in function section, so pass it here, just check
  2030. * whether function and code section have inconsistent lengths */
  2031. read_leb_uint32(p, p_end, code_count);
  2032. if (buf_func)
  2033. read_leb_uint32(p_func, buf_func_end, func_count);
  2034. if (func_count != code_count) {
  2035. set_error_buf(error_buf, error_buf_size,
  2036. "function and code section have inconsistent lengths");
  2037. return false;
  2038. }
  2039. LOG_VERBOSE("Load code segment section success.\n");
  2040. return true;
  2041. fail:
  2042. return false;
  2043. }
  2044. static bool
  2045. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2046. char *error_buf, uint32 error_buf_size)
  2047. {
  2048. const uint8 *p = buf, *p_end = buf_end;
  2049. WASMType *type;
  2050. uint32 start_function;
  2051. read_leb_uint32(p, p_end, start_function);
  2052. if (start_function
  2053. >= module->function_count + module->import_function_count) {
  2054. set_error_buf(error_buf, error_buf_size, "unknown function");
  2055. return false;
  2056. }
  2057. if (start_function < module->import_function_count)
  2058. type = module->import_functions[start_function].u.function.func_type;
  2059. else
  2060. type =
  2061. module->functions[start_function - module->import_function_count]
  2062. ->func_type;
  2063. if (type->param_count != 0 || type->result_count != 0) {
  2064. set_error_buf(error_buf, error_buf_size, "invalid start function");
  2065. return false;
  2066. }
  2067. module->start_function = start_function;
  2068. if (p != p_end) {
  2069. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2070. return false;
  2071. }
  2072. LOG_VERBOSE("Load start section success.\n");
  2073. return true;
  2074. fail:
  2075. return false;
  2076. }
  2077. static bool
  2078. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2079. char *error_buf, uint32 error_buf_size)
  2080. {
  2081. const uint8 *p = buf, *p_end = buf_end;
  2082. uint32 name_len;
  2083. if (p >= p_end) {
  2084. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2085. return false;
  2086. }
  2087. read_leb_uint32(p, p_end, name_len);
  2088. if (name_len == 0
  2089. || p + name_len > p_end) {
  2090. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2091. return false;
  2092. }
  2093. if (!check_utf8_str(p, name_len)) {
  2094. set_error_buf(error_buf, error_buf_size,
  2095. "invalid UTF-8 encoding");
  2096. return false;
  2097. }
  2098. LOG_VERBOSE("Load custom section success.\n");
  2099. return true;
  2100. fail:
  2101. return false;
  2102. }
  2103. static bool
  2104. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  2105. BlockAddr *block_addr_cache,
  2106. char *error_buf, uint32 error_buf_size);
  2107. #if WASM_ENABLE_FAST_INTERP != 0
  2108. void **
  2109. wasm_interp_get_handle_table();
  2110. static void **handle_table;
  2111. #endif
  2112. static bool
  2113. load_from_sections(WASMModule *module, WASMSection *sections,
  2114. char *error_buf, uint32 error_buf_size)
  2115. {
  2116. WASMExport *export;
  2117. WASMSection *section = sections;
  2118. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  2119. *buf_func = NULL, *buf_func_end = NULL;
  2120. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  2121. WASMGlobal *aux_stack_top_global = NULL, *global;
  2122. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  2123. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  2124. uint32 aux_data_end_global_index = (uint32)-1;
  2125. uint32 aux_heap_base_global_index = (uint32)-1;
  2126. BlockAddr *block_addr_cache;
  2127. WASMType *func_type;
  2128. uint64 total_size;
  2129. /* Find code and function sections if have */
  2130. while (section) {
  2131. if (section->section_type == SECTION_TYPE_CODE) {
  2132. buf_code = section->section_body;
  2133. buf_code_end = buf_code + section->section_body_size;
  2134. }
  2135. else if (section->section_type == SECTION_TYPE_FUNC) {
  2136. buf_func = section->section_body;
  2137. buf_func_end = buf_func + section->section_body_size;
  2138. }
  2139. section = section->next;
  2140. }
  2141. section = sections;
  2142. while (section) {
  2143. buf = section->section_body;
  2144. buf_end = buf + section->section_body_size;
  2145. LOG_DEBUG("to section %d", section->section_type);
  2146. switch (section->section_type) {
  2147. case SECTION_TYPE_USER:
  2148. /* unsupported user section, ignore it. */
  2149. if (!load_user_section(buf, buf_end, module,
  2150. error_buf, error_buf_size))
  2151. return false;
  2152. break;
  2153. case SECTION_TYPE_TYPE:
  2154. if (!load_type_section(buf, buf_end, module,
  2155. error_buf, error_buf_size))
  2156. return false;
  2157. break;
  2158. case SECTION_TYPE_IMPORT:
  2159. if (!load_import_section(buf, buf_end, module,
  2160. error_buf, error_buf_size))
  2161. return false;
  2162. break;
  2163. case SECTION_TYPE_FUNC:
  2164. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  2165. module, error_buf, error_buf_size))
  2166. return false;
  2167. break;
  2168. case SECTION_TYPE_TABLE:
  2169. if (!load_table_section(buf, buf_end, module,
  2170. error_buf, error_buf_size))
  2171. return false;
  2172. break;
  2173. case SECTION_TYPE_MEMORY:
  2174. if (!load_memory_section(buf, buf_end, module,
  2175. error_buf, error_buf_size))
  2176. return false;
  2177. break;
  2178. case SECTION_TYPE_GLOBAL:
  2179. if (!load_global_section(buf, buf_end, module,
  2180. error_buf, error_buf_size))
  2181. return false;
  2182. break;
  2183. case SECTION_TYPE_EXPORT:
  2184. if (!load_export_section(buf, buf_end, module,
  2185. error_buf, error_buf_size))
  2186. return false;
  2187. break;
  2188. case SECTION_TYPE_START:
  2189. if (!load_start_section(buf, buf_end, module,
  2190. error_buf, error_buf_size))
  2191. return false;
  2192. break;
  2193. case SECTION_TYPE_ELEM:
  2194. if (!load_table_segment_section(buf, buf_end, module,
  2195. error_buf, error_buf_size))
  2196. return false;
  2197. break;
  2198. case SECTION_TYPE_CODE:
  2199. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  2200. module, error_buf, error_buf_size))
  2201. return false;
  2202. break;
  2203. case SECTION_TYPE_DATA:
  2204. if (!load_data_segment_section(buf, buf_end, module,
  2205. error_buf, error_buf_size))
  2206. return false;
  2207. break;
  2208. #if WASM_ENABLE_BULK_MEMORY != 0
  2209. case SECTION_TYPE_DATACOUNT:
  2210. if (!load_datacount_section(buf, buf_end, module,
  2211. error_buf, error_buf_size))
  2212. return false;
  2213. break;
  2214. #endif
  2215. default:
  2216. set_error_buf(error_buf, error_buf_size,
  2217. "invalid section id");
  2218. return false;
  2219. }
  2220. section = section->next;
  2221. }
  2222. #if WASM_ENABLE_FAST_INTERP != 0
  2223. handle_table = wasm_interp_get_handle_table();
  2224. #endif
  2225. total_size = sizeof(BlockAddr) * (uint64)BLOCK_ADDR_CACHE_SIZE
  2226. * BLOCK_ADDR_CONFLICT_SIZE;
  2227. if (!(block_addr_cache = loader_malloc
  2228. (total_size, error_buf, error_buf_size))) {
  2229. return false;
  2230. }
  2231. for (i = 0; i < module->function_count; i++) {
  2232. WASMFunction *func = module->functions[i];
  2233. memset(block_addr_cache, 0, (uint32)total_size);
  2234. if (!wasm_loader_prepare_bytecode(module, func, block_addr_cache,
  2235. error_buf, error_buf_size)) {
  2236. wasm_runtime_free(block_addr_cache);
  2237. return false;
  2238. }
  2239. }
  2240. wasm_runtime_free(block_addr_cache);
  2241. module->aux_data_end_global_index = (uint32)-1;
  2242. module->aux_heap_base_global_index = (uint32)-1;
  2243. module->aux_stack_top_global_index = (uint32)-1;
  2244. /* Resolve auxiliary data/stack/heap info and reset memory info */
  2245. export = module->exports;
  2246. for (i = 0; i < module->export_count; i++, export++) {
  2247. if (export->kind == EXPORT_KIND_GLOBAL) {
  2248. if (!strcmp(export->name, "__heap_base")) {
  2249. global_index = export->index - module->import_global_count;
  2250. global = module->globals + global_index;
  2251. if (global->type == VALUE_TYPE_I32
  2252. && !global->is_mutable
  2253. && global->init_expr.init_expr_type ==
  2254. INIT_EXPR_TYPE_I32_CONST) {
  2255. aux_heap_base_global = global;
  2256. aux_heap_base = global->init_expr.u.i32;
  2257. aux_heap_base_global_index = export->index;
  2258. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  2259. aux_heap_base);
  2260. }
  2261. }
  2262. else if (!strcmp(export->name, "__data_end")) {
  2263. global_index = export->index - module->import_global_count;
  2264. global = module->globals + global_index;
  2265. if (global->type == VALUE_TYPE_I32
  2266. && !global->is_mutable
  2267. && global->init_expr.init_expr_type ==
  2268. INIT_EXPR_TYPE_I32_CONST) {
  2269. aux_data_end_global = global;
  2270. aux_data_end = global->init_expr.u.i32;
  2271. aux_data_end_global_index = export->index;
  2272. LOG_VERBOSE("Found aux __data_end global, value: %d",
  2273. aux_data_end);
  2274. aux_data_end = align_uint(aux_data_end, 16);
  2275. }
  2276. }
  2277. /* For module compiled with -pthread option, the global is:
  2278. [0] stack_top <-- 0
  2279. [1] tls_pointer
  2280. [2] tls_size
  2281. [3] data_end <-- 3
  2282. [4] global_base
  2283. [5] heap_base <-- 5
  2284. [6] dso_handle
  2285. For module compiled without -pthread option:
  2286. [0] stack_top <-- 0
  2287. [1] data_end <-- 1
  2288. [2] global_base
  2289. [3] heap_base <-- 3
  2290. [4] dso_handle
  2291. */
  2292. if (aux_data_end_global && aux_heap_base_global
  2293. && aux_data_end <= aux_heap_base) {
  2294. module->aux_data_end_global_index = aux_data_end_global_index;
  2295. module->aux_data_end = aux_data_end;
  2296. module->aux_heap_base_global_index = aux_heap_base_global_index;
  2297. module->aux_heap_base = aux_heap_base;
  2298. /* Resolve aux stack top global */
  2299. for (global_index = 0; global_index < module->global_count;
  2300. global_index++) {
  2301. global = module->globals + global_index;
  2302. if (global->is_mutable /* heap_base and data_end is
  2303. not mutable */
  2304. && global->type == VALUE_TYPE_I32
  2305. && global->init_expr.init_expr_type ==
  2306. INIT_EXPR_TYPE_I32_CONST
  2307. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  2308. aux_stack_top_global = global;
  2309. aux_stack_top = (uint32)global->init_expr.u.i32;
  2310. module->aux_stack_top_global_index =
  2311. module->import_global_count + global_index;
  2312. module->aux_stack_bottom = aux_stack_top;
  2313. module->aux_stack_size = aux_stack_top > aux_data_end
  2314. ? aux_stack_top - aux_data_end
  2315. : aux_stack_top;
  2316. LOG_VERBOSE("Found aux stack top global, value: %d, "
  2317. "global index: %d, stack size: %d",
  2318. aux_stack_top, global_index,
  2319. module->aux_stack_size);
  2320. break;
  2321. }
  2322. }
  2323. break;
  2324. }
  2325. }
  2326. }
  2327. module->malloc_function = (uint32)-1;
  2328. module->free_function = (uint32)-1;
  2329. /* Resolve auxiliary data/stack/heap info and reset memory info */
  2330. export = module->exports;
  2331. for (i = 0; i < module->export_count; i++, export++) {
  2332. if (export->kind == EXPORT_KIND_FUNC) {
  2333. if (!strcmp(export->name, "malloc")
  2334. && export->index >= module->import_function_count) {
  2335. func_index = export->index - module->import_function_count;
  2336. func_type = module->functions[func_index]->func_type;
  2337. if (func_type->param_count == 1
  2338. && func_type->result_count == 1
  2339. && func_type->types[0] == VALUE_TYPE_I32
  2340. && func_type->types[1] == VALUE_TYPE_I32) {
  2341. module->malloc_function = export->index;
  2342. LOG_VERBOSE("Found malloc function, index: %u",
  2343. export->index);
  2344. }
  2345. }
  2346. else if (!strcmp(export->name, "free")
  2347. && export->index >= module->import_function_count) {
  2348. func_index = export->index - module->import_function_count;
  2349. func_type = module->functions[func_index]->func_type;
  2350. if (func_type->param_count == 1
  2351. && func_type->result_count == 0
  2352. && func_type->types[0] == VALUE_TYPE_I32) {
  2353. module->free_function = export->index;
  2354. LOG_VERBOSE("Found free function, index: %u",
  2355. export->index);
  2356. }
  2357. }
  2358. }
  2359. }
  2360. if (!module->possible_memory_grow) {
  2361. WASMMemoryImport *memory_import;
  2362. WASMMemory *memory;
  2363. if (aux_data_end_global
  2364. && aux_heap_base_global
  2365. && aux_stack_top_global) {
  2366. uint64 init_memory_size;
  2367. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  2368. if (module->import_memory_count) {
  2369. memory_import = &module->import_memories[0].u.memory;
  2370. init_memory_size = (uint64)memory_import->num_bytes_per_page *
  2371. memory_import->init_page_count;
  2372. if (shrunk_memory_size <= init_memory_size) {
  2373. /* Reset memory info to decrease memory usage */
  2374. memory_import->num_bytes_per_page = shrunk_memory_size;
  2375. memory_import->init_page_count = 1;
  2376. LOG_VERBOSE("Shrink import memory size to %d",
  2377. shrunk_memory_size);
  2378. }
  2379. }
  2380. if (module->memory_count) {
  2381. memory = &module->memories[0];
  2382. init_memory_size = (uint64)memory->num_bytes_per_page *
  2383. memory->init_page_count;
  2384. if (shrunk_memory_size <= init_memory_size) {
  2385. /* Reset memory info to decrease memory usage */
  2386. memory->num_bytes_per_page = shrunk_memory_size;
  2387. memory->init_page_count = 1;
  2388. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  2389. }
  2390. }
  2391. }
  2392. #if WASM_ENABLE_MULTI_MODULE == 0
  2393. if (module->import_memory_count) {
  2394. memory_import = &module->import_memories[0].u.memory;
  2395. /* Memory init page count cannot be larger than 65536, we don't
  2396. check integer overflow again. */
  2397. memory_import->num_bytes_per_page *= memory_import->init_page_count;
  2398. memory_import->init_page_count = memory_import->max_page_count = 1;
  2399. }
  2400. if (module->memory_count) {
  2401. /* Memory init page count cannot be larger than 65536, we don't
  2402. check integer overflow again. */
  2403. memory = &module->memories[0];
  2404. memory->num_bytes_per_page *= memory->init_page_count;
  2405. memory->init_page_count = memory->max_page_count = 1;
  2406. }
  2407. #endif
  2408. }
  2409. return true;
  2410. }
  2411. #if BH_ENABLE_MEMORY_PROFILING != 0
  2412. static void wasm_loader_free(void *ptr)
  2413. {
  2414. wasm_runtime_free(ptr);
  2415. }
  2416. #else
  2417. #define wasm_loader_free wasm_free
  2418. #endif
  2419. static WASMModule*
  2420. create_module(char *error_buf, uint32 error_buf_size)
  2421. {
  2422. WASMModule *module = loader_malloc(sizeof(WASMModule),
  2423. error_buf, error_buf_size);
  2424. if (!module) {
  2425. return NULL;
  2426. }
  2427. module->module_type = Wasm_Module_Bytecode;
  2428. /* Set start_function to -1, means no start function */
  2429. module->start_function = (uint32)-1;
  2430. #if WASM_ENABLE_MULTI_MODULE != 0
  2431. module->import_module_list = &module->import_module_list_head;
  2432. #endif
  2433. return module;
  2434. }
  2435. WASMModule *
  2436. wasm_loader_load_from_sections(WASMSection *section_list,
  2437. char *error_buf, uint32 error_buf_size)
  2438. {
  2439. WASMModule *module = create_module(error_buf, error_buf_size);
  2440. if (!module)
  2441. return NULL;
  2442. if (!load_from_sections(module, section_list, error_buf, error_buf_size)) {
  2443. wasm_loader_unload(module);
  2444. return NULL;
  2445. }
  2446. LOG_VERBOSE("Load module from sections success.\n");
  2447. return module;
  2448. }
  2449. static void
  2450. destroy_sections(WASMSection *section_list)
  2451. {
  2452. WASMSection *section = section_list, *next;
  2453. while (section) {
  2454. next = section->next;
  2455. wasm_runtime_free(section);
  2456. section = next;
  2457. }
  2458. }
  2459. static uint8 section_ids[] = {
  2460. SECTION_TYPE_USER,
  2461. SECTION_TYPE_TYPE,
  2462. SECTION_TYPE_IMPORT,
  2463. SECTION_TYPE_FUNC,
  2464. SECTION_TYPE_TABLE,
  2465. SECTION_TYPE_MEMORY,
  2466. SECTION_TYPE_GLOBAL,
  2467. SECTION_TYPE_EXPORT,
  2468. SECTION_TYPE_START,
  2469. SECTION_TYPE_ELEM,
  2470. #if WASM_ENABLE_BULK_MEMORY != 0
  2471. SECTION_TYPE_DATACOUNT,
  2472. #endif
  2473. SECTION_TYPE_CODE,
  2474. SECTION_TYPE_DATA
  2475. };
  2476. static uint8
  2477. get_section_index(uint8 section_type)
  2478. {
  2479. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  2480. for (uint8 i = 0; i < max_id; i++) {
  2481. if (section_type == section_ids[i])
  2482. return i;
  2483. }
  2484. return (uint8)-1;
  2485. }
  2486. static bool
  2487. create_sections(const uint8 *buf, uint32 size,
  2488. WASMSection **p_section_list,
  2489. char *error_buf, uint32 error_buf_size)
  2490. {
  2491. WASMSection *section_list_end = NULL, *section;
  2492. const uint8 *p = buf, *p_end = buf + size/*, *section_body*/;
  2493. uint8 section_type, section_index, last_section_index = (uint8)-1;
  2494. uint32 section_size;
  2495. bh_assert(!*p_section_list);
  2496. p += 8;
  2497. while (p < p_end) {
  2498. CHECK_BUF(p, p_end, 1);
  2499. section_type = read_uint8(p);
  2500. section_index = get_section_index(section_type);
  2501. if (section_index != (uint8)-1) {
  2502. if (section_type != SECTION_TYPE_USER) {
  2503. /* Custom sections may be inserted at any place,
  2504. while other sections must occur at most once
  2505. and in prescribed order. */
  2506. if (last_section_index != (uint8)-1
  2507. && (section_index <= last_section_index)) {
  2508. set_error_buf(error_buf, error_buf_size,
  2509. "junk after last section");
  2510. return false;
  2511. }
  2512. last_section_index = section_index;
  2513. }
  2514. CHECK_BUF1(p, p_end, 1);
  2515. read_leb_uint32(p, p_end, section_size);
  2516. CHECK_BUF1(p, p_end, section_size);
  2517. if (!(section = loader_malloc(sizeof(WASMSection),
  2518. error_buf, error_buf_size))) {
  2519. return false;
  2520. }
  2521. section->section_type = section_type;
  2522. section->section_body = (uint8*)p;
  2523. section->section_body_size = section_size;
  2524. if (!*p_section_list)
  2525. *p_section_list = section_list_end = section;
  2526. else {
  2527. section_list_end->next = section;
  2528. section_list_end = section;
  2529. }
  2530. p += section_size;
  2531. }
  2532. else {
  2533. set_error_buf(error_buf, error_buf_size,
  2534. "invalid section id");
  2535. return false;
  2536. }
  2537. }
  2538. return true;
  2539. fail:
  2540. return false;
  2541. }
  2542. static void
  2543. exchange32(uint8* p_data)
  2544. {
  2545. uint8 value = *p_data;
  2546. *p_data = *(p_data + 3);
  2547. *(p_data + 3) = value;
  2548. value = *(p_data + 1);
  2549. *(p_data + 1) = *(p_data + 2);
  2550. *(p_data + 2) = value;
  2551. }
  2552. static union {
  2553. int a;
  2554. char b;
  2555. } __ue = { .a = 1 };
  2556. #define is_little_endian() (__ue.b == 1)
  2557. static bool
  2558. load(const uint8 *buf, uint32 size, WASMModule *module,
  2559. char *error_buf, uint32 error_buf_size)
  2560. {
  2561. const uint8 *buf_end = buf + size;
  2562. const uint8 *p = buf, *p_end = buf_end;
  2563. uint32 magic_number, version;
  2564. WASMSection *section_list = NULL;
  2565. CHECK_BUF1(p, p_end, sizeof(uint32));
  2566. magic_number = read_uint32(p);
  2567. if (!is_little_endian())
  2568. exchange32((uint8*)&magic_number);
  2569. if (magic_number != WASM_MAGIC_NUMBER) {
  2570. set_error_buf(error_buf, error_buf_size,
  2571. "magic header not detected");
  2572. return false;
  2573. }
  2574. CHECK_BUF1(p, p_end, sizeof(uint32));
  2575. version = read_uint32(p);
  2576. if (!is_little_endian())
  2577. exchange32((uint8*)&version);
  2578. if (version != WASM_CURRENT_VERSION) {
  2579. set_error_buf(error_buf, error_buf_size,
  2580. "unknown binary version");
  2581. return false;
  2582. }
  2583. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  2584. || !load_from_sections(module, section_list, error_buf, error_buf_size)) {
  2585. destroy_sections(section_list);
  2586. return false;
  2587. }
  2588. destroy_sections(section_list);
  2589. return true;
  2590. fail:
  2591. return false;
  2592. }
  2593. WASMModule*
  2594. wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
  2595. {
  2596. WASMModule *module = create_module(error_buf, error_buf_size);
  2597. if (!module) {
  2598. return NULL;
  2599. }
  2600. if (!load(buf, size, module, error_buf, error_buf_size)) {
  2601. goto fail;
  2602. }
  2603. LOG_VERBOSE("Load module success.\n");
  2604. return module;
  2605. fail:
  2606. wasm_loader_unload(module);
  2607. return NULL;
  2608. }
  2609. void
  2610. wasm_loader_unload(WASMModule *module)
  2611. {
  2612. uint32 i;
  2613. if (!module)
  2614. return;
  2615. if (module->types) {
  2616. for (i = 0; i < module->type_count; i++) {
  2617. if (module->types[i])
  2618. wasm_runtime_free(module->types[i]);
  2619. }
  2620. wasm_runtime_free(module->types);
  2621. }
  2622. if (module->imports)
  2623. wasm_runtime_free(module->imports);
  2624. if (module->functions) {
  2625. for (i = 0; i < module->function_count; i++) {
  2626. if (module->functions[i]) {
  2627. if (module->functions[i]->local_offsets)
  2628. wasm_runtime_free(module->functions[i]->local_offsets);
  2629. #if WASM_ENABLE_FAST_INTERP != 0
  2630. if (module->functions[i]->code_compiled)
  2631. wasm_runtime_free(module->functions[i]->code_compiled);
  2632. if (module->functions[i]->consts)
  2633. wasm_runtime_free(module->functions[i]->consts);
  2634. #endif
  2635. wasm_runtime_free(module->functions[i]);
  2636. }
  2637. }
  2638. wasm_runtime_free(module->functions);
  2639. }
  2640. if (module->tables)
  2641. wasm_runtime_free(module->tables);
  2642. if (module->memories)
  2643. wasm_runtime_free(module->memories);
  2644. if (module->globals)
  2645. wasm_runtime_free(module->globals);
  2646. if (module->exports)
  2647. wasm_runtime_free(module->exports);
  2648. if (module->table_segments) {
  2649. for (i = 0; i < module->table_seg_count; i++) {
  2650. if (module->table_segments[i].func_indexes)
  2651. wasm_runtime_free(module->table_segments[i].func_indexes);
  2652. }
  2653. wasm_runtime_free(module->table_segments);
  2654. }
  2655. if (module->data_segments) {
  2656. for (i = 0; i < module->data_seg_count; i++) {
  2657. if (module->data_segments[i])
  2658. wasm_runtime_free(module->data_segments[i]);
  2659. }
  2660. wasm_runtime_free(module->data_segments);
  2661. }
  2662. if (module->const_str_list) {
  2663. StringNode *node = module->const_str_list, *node_next;
  2664. while (node) {
  2665. node_next = node->next;
  2666. wasm_runtime_free(node);
  2667. node = node_next;
  2668. }
  2669. }
  2670. #if WASM_ENABLE_MULTI_MODULE != 0
  2671. /* just release the sub module list */
  2672. if (module->import_module_list) {
  2673. WASMRegisteredModule *node =
  2674. bh_list_first_elem(module->import_module_list);
  2675. while (node) {
  2676. WASMRegisteredModule *next = bh_list_elem_next(node);
  2677. bh_list_remove(module->import_module_list, node);
  2678. /*
  2679. * unload(sub_module) will be trigged during runtime_destroy().
  2680. * every module in the global module list will be unloaded one by
  2681. * one. so don't worry.
  2682. */
  2683. wasm_runtime_free(node);
  2684. /*
  2685. *
  2686. * the module file reading buffer will be released
  2687. * in runtime_destroy()
  2688. */
  2689. node = next;
  2690. }
  2691. }
  2692. #endif
  2693. wasm_runtime_free(module);
  2694. }
  2695. bool
  2696. wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
  2697. const uint8 *start_addr,
  2698. const uint8 *code_end_addr,
  2699. uint8 label_type,
  2700. uint8 **p_else_addr,
  2701. uint8 **p_end_addr,
  2702. char *error_buf,
  2703. uint32 error_buf_size)
  2704. {
  2705. const uint8 *p = start_addr, *p_end = code_end_addr;
  2706. uint8 *else_addr = NULL;
  2707. uint32 block_nested_depth = 1, count, i, j, t;
  2708. uint8 opcode, u8;
  2709. BlockAddr block_stack[16] = { 0 }, *block;
  2710. i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
  2711. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2712. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  2713. if (block[j].start_addr == start_addr) {
  2714. /* Cache hit */
  2715. *p_else_addr = block[j].else_addr;
  2716. *p_end_addr = block[j].end_addr;
  2717. return true;
  2718. }
  2719. }
  2720. /* Cache unhit */
  2721. block_stack[0].start_addr = start_addr;
  2722. while (p < code_end_addr) {
  2723. opcode = *p++;
  2724. switch (opcode) {
  2725. case WASM_OP_UNREACHABLE:
  2726. case WASM_OP_NOP:
  2727. break;
  2728. case WASM_OP_BLOCK:
  2729. case WASM_OP_LOOP:
  2730. case WASM_OP_IF:
  2731. CHECK_BUF(p, p_end, 1);
  2732. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  2733. u8 = read_uint8(p);
  2734. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
  2735. block_stack[block_nested_depth].start_addr = p;
  2736. block_stack[block_nested_depth].else_addr = NULL;
  2737. }
  2738. block_nested_depth++;
  2739. break;
  2740. case EXT_OP_BLOCK:
  2741. case EXT_OP_LOOP:
  2742. case EXT_OP_IF:
  2743. /* block type */
  2744. skip_leb_uint32(p, p_end);
  2745. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
  2746. block_stack[block_nested_depth].start_addr = p;
  2747. block_stack[block_nested_depth].else_addr = NULL;
  2748. }
  2749. block_nested_depth++;
  2750. break;
  2751. case WASM_OP_ELSE:
  2752. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  2753. else_addr = (uint8*)(p - 1);
  2754. if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr))
  2755. block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1);
  2756. break;
  2757. case WASM_OP_END:
  2758. if (block_nested_depth == 1) {
  2759. if (label_type == LABEL_TYPE_IF)
  2760. *p_else_addr = else_addr;
  2761. *p_end_addr = (uint8*)(p - 1);
  2762. block_stack[0].end_addr = (uint8*)(p - 1);
  2763. for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) {
  2764. start_addr = block_stack[t].start_addr;
  2765. if (start_addr) {
  2766. i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
  2767. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2768. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  2769. if (!block[j].start_addr)
  2770. break;
  2771. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  2772. memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) *
  2773. sizeof(BlockAddr));
  2774. j = 0;
  2775. }
  2776. block[j].start_addr = block_stack[t].start_addr;
  2777. block[j].else_addr = block_stack[t].else_addr;
  2778. block[j].end_addr = block_stack[t].end_addr;
  2779. }
  2780. else
  2781. break;
  2782. }
  2783. return true;
  2784. }
  2785. else {
  2786. block_nested_depth--;
  2787. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr))
  2788. block_stack[block_nested_depth].end_addr = (uint8*)(p - 1);
  2789. }
  2790. break;
  2791. case WASM_OP_BR:
  2792. case WASM_OP_BR_IF:
  2793. skip_leb_uint32(p, p_end); /* labelidx */
  2794. break;
  2795. case WASM_OP_BR_TABLE:
  2796. read_leb_uint32(p, p_end, count); /* lable num */
  2797. for (i = 0; i <= count; i++) /* lableidxs */
  2798. skip_leb_uint32(p, p_end);
  2799. break;
  2800. case WASM_OP_RETURN:
  2801. break;
  2802. case WASM_OP_CALL:
  2803. skip_leb_uint32(p, p_end); /* funcidx */
  2804. break;
  2805. case WASM_OP_CALL_INDIRECT:
  2806. skip_leb_uint32(p, p_end); /* typeidx */
  2807. CHECK_BUF(p, p_end, 1);
  2808. u8 = read_uint8(p); /* 0x00 */
  2809. break;
  2810. case WASM_OP_DROP:
  2811. case WASM_OP_SELECT:
  2812. case WASM_OP_DROP_64:
  2813. case WASM_OP_SELECT_64:
  2814. break;
  2815. case WASM_OP_GET_LOCAL:
  2816. case WASM_OP_SET_LOCAL:
  2817. case WASM_OP_TEE_LOCAL:
  2818. case WASM_OP_GET_GLOBAL:
  2819. case WASM_OP_SET_GLOBAL:
  2820. case WASM_OP_GET_GLOBAL_64:
  2821. case WASM_OP_SET_GLOBAL_64:
  2822. case WASM_OP_SET_GLOBAL_AUX_STACK:
  2823. skip_leb_uint32(p, p_end); /* localidx */
  2824. break;
  2825. case EXT_OP_GET_LOCAL_FAST:
  2826. case EXT_OP_SET_LOCAL_FAST:
  2827. case EXT_OP_TEE_LOCAL_FAST:
  2828. CHECK_BUF(p, p_end, 1);
  2829. p++;
  2830. break;
  2831. case WASM_OP_I32_LOAD:
  2832. case WASM_OP_I64_LOAD:
  2833. case WASM_OP_F32_LOAD:
  2834. case WASM_OP_F64_LOAD:
  2835. case WASM_OP_I32_LOAD8_S:
  2836. case WASM_OP_I32_LOAD8_U:
  2837. case WASM_OP_I32_LOAD16_S:
  2838. case WASM_OP_I32_LOAD16_U:
  2839. case WASM_OP_I64_LOAD8_S:
  2840. case WASM_OP_I64_LOAD8_U:
  2841. case WASM_OP_I64_LOAD16_S:
  2842. case WASM_OP_I64_LOAD16_U:
  2843. case WASM_OP_I64_LOAD32_S:
  2844. case WASM_OP_I64_LOAD32_U:
  2845. case WASM_OP_I32_STORE:
  2846. case WASM_OP_I64_STORE:
  2847. case WASM_OP_F32_STORE:
  2848. case WASM_OP_F64_STORE:
  2849. case WASM_OP_I32_STORE8:
  2850. case WASM_OP_I32_STORE16:
  2851. case WASM_OP_I64_STORE8:
  2852. case WASM_OP_I64_STORE16:
  2853. case WASM_OP_I64_STORE32:
  2854. skip_leb_uint32(p, p_end); /* align */
  2855. skip_leb_uint32(p, p_end); /* offset */
  2856. break;
  2857. case WASM_OP_MEMORY_SIZE:
  2858. case WASM_OP_MEMORY_GROW:
  2859. skip_leb_uint32(p, p_end); /* 0x00 */
  2860. break;
  2861. case WASM_OP_I32_CONST:
  2862. skip_leb_int32(p, p_end);
  2863. break;
  2864. case WASM_OP_I64_CONST:
  2865. skip_leb_int64(p, p_end);
  2866. break;
  2867. case WASM_OP_F32_CONST:
  2868. p += sizeof(float32);
  2869. break;
  2870. case WASM_OP_F64_CONST:
  2871. p += sizeof(float64);
  2872. break;
  2873. case WASM_OP_I32_EQZ:
  2874. case WASM_OP_I32_EQ:
  2875. case WASM_OP_I32_NE:
  2876. case WASM_OP_I32_LT_S:
  2877. case WASM_OP_I32_LT_U:
  2878. case WASM_OP_I32_GT_S:
  2879. case WASM_OP_I32_GT_U:
  2880. case WASM_OP_I32_LE_S:
  2881. case WASM_OP_I32_LE_U:
  2882. case WASM_OP_I32_GE_S:
  2883. case WASM_OP_I32_GE_U:
  2884. case WASM_OP_I64_EQZ:
  2885. case WASM_OP_I64_EQ:
  2886. case WASM_OP_I64_NE:
  2887. case WASM_OP_I64_LT_S:
  2888. case WASM_OP_I64_LT_U:
  2889. case WASM_OP_I64_GT_S:
  2890. case WASM_OP_I64_GT_U:
  2891. case WASM_OP_I64_LE_S:
  2892. case WASM_OP_I64_LE_U:
  2893. case WASM_OP_I64_GE_S:
  2894. case WASM_OP_I64_GE_U:
  2895. case WASM_OP_F32_EQ:
  2896. case WASM_OP_F32_NE:
  2897. case WASM_OP_F32_LT:
  2898. case WASM_OP_F32_GT:
  2899. case WASM_OP_F32_LE:
  2900. case WASM_OP_F32_GE:
  2901. case WASM_OP_F64_EQ:
  2902. case WASM_OP_F64_NE:
  2903. case WASM_OP_F64_LT:
  2904. case WASM_OP_F64_GT:
  2905. case WASM_OP_F64_LE:
  2906. case WASM_OP_F64_GE:
  2907. case WASM_OP_I32_CLZ:
  2908. case WASM_OP_I32_CTZ:
  2909. case WASM_OP_I32_POPCNT:
  2910. case WASM_OP_I32_ADD:
  2911. case WASM_OP_I32_SUB:
  2912. case WASM_OP_I32_MUL:
  2913. case WASM_OP_I32_DIV_S:
  2914. case WASM_OP_I32_DIV_U:
  2915. case WASM_OP_I32_REM_S:
  2916. case WASM_OP_I32_REM_U:
  2917. case WASM_OP_I32_AND:
  2918. case WASM_OP_I32_OR:
  2919. case WASM_OP_I32_XOR:
  2920. case WASM_OP_I32_SHL:
  2921. case WASM_OP_I32_SHR_S:
  2922. case WASM_OP_I32_SHR_U:
  2923. case WASM_OP_I32_ROTL:
  2924. case WASM_OP_I32_ROTR:
  2925. case WASM_OP_I64_CLZ:
  2926. case WASM_OP_I64_CTZ:
  2927. case WASM_OP_I64_POPCNT:
  2928. case WASM_OP_I64_ADD:
  2929. case WASM_OP_I64_SUB:
  2930. case WASM_OP_I64_MUL:
  2931. case WASM_OP_I64_DIV_S:
  2932. case WASM_OP_I64_DIV_U:
  2933. case WASM_OP_I64_REM_S:
  2934. case WASM_OP_I64_REM_U:
  2935. case WASM_OP_I64_AND:
  2936. case WASM_OP_I64_OR:
  2937. case WASM_OP_I64_XOR:
  2938. case WASM_OP_I64_SHL:
  2939. case WASM_OP_I64_SHR_S:
  2940. case WASM_OP_I64_SHR_U:
  2941. case WASM_OP_I64_ROTL:
  2942. case WASM_OP_I64_ROTR:
  2943. case WASM_OP_F32_ABS:
  2944. case WASM_OP_F32_NEG:
  2945. case WASM_OP_F32_CEIL:
  2946. case WASM_OP_F32_FLOOR:
  2947. case WASM_OP_F32_TRUNC:
  2948. case WASM_OP_F32_NEAREST:
  2949. case WASM_OP_F32_SQRT:
  2950. case WASM_OP_F32_ADD:
  2951. case WASM_OP_F32_SUB:
  2952. case WASM_OP_F32_MUL:
  2953. case WASM_OP_F32_DIV:
  2954. case WASM_OP_F32_MIN:
  2955. case WASM_OP_F32_MAX:
  2956. case WASM_OP_F32_COPYSIGN:
  2957. case WASM_OP_F64_ABS:
  2958. case WASM_OP_F64_NEG:
  2959. case WASM_OP_F64_CEIL:
  2960. case WASM_OP_F64_FLOOR:
  2961. case WASM_OP_F64_TRUNC:
  2962. case WASM_OP_F64_NEAREST:
  2963. case WASM_OP_F64_SQRT:
  2964. case WASM_OP_F64_ADD:
  2965. case WASM_OP_F64_SUB:
  2966. case WASM_OP_F64_MUL:
  2967. case WASM_OP_F64_DIV:
  2968. case WASM_OP_F64_MIN:
  2969. case WASM_OP_F64_MAX:
  2970. case WASM_OP_F64_COPYSIGN:
  2971. case WASM_OP_I32_WRAP_I64:
  2972. case WASM_OP_I32_TRUNC_S_F32:
  2973. case WASM_OP_I32_TRUNC_U_F32:
  2974. case WASM_OP_I32_TRUNC_S_F64:
  2975. case WASM_OP_I32_TRUNC_U_F64:
  2976. case WASM_OP_I64_EXTEND_S_I32:
  2977. case WASM_OP_I64_EXTEND_U_I32:
  2978. case WASM_OP_I64_TRUNC_S_F32:
  2979. case WASM_OP_I64_TRUNC_U_F32:
  2980. case WASM_OP_I64_TRUNC_S_F64:
  2981. case WASM_OP_I64_TRUNC_U_F64:
  2982. case WASM_OP_F32_CONVERT_S_I32:
  2983. case WASM_OP_F32_CONVERT_U_I32:
  2984. case WASM_OP_F32_CONVERT_S_I64:
  2985. case WASM_OP_F32_CONVERT_U_I64:
  2986. case WASM_OP_F32_DEMOTE_F64:
  2987. case WASM_OP_F64_CONVERT_S_I32:
  2988. case WASM_OP_F64_CONVERT_U_I32:
  2989. case WASM_OP_F64_CONVERT_S_I64:
  2990. case WASM_OP_F64_CONVERT_U_I64:
  2991. case WASM_OP_F64_PROMOTE_F32:
  2992. case WASM_OP_I32_REINTERPRET_F32:
  2993. case WASM_OP_I64_REINTERPRET_F64:
  2994. case WASM_OP_F32_REINTERPRET_I32:
  2995. case WASM_OP_F64_REINTERPRET_I64:
  2996. case WASM_OP_I32_EXTEND8_S:
  2997. case WASM_OP_I32_EXTEND16_S:
  2998. case WASM_OP_I64_EXTEND8_S:
  2999. case WASM_OP_I64_EXTEND16_S:
  3000. case WASM_OP_I64_EXTEND32_S:
  3001. break;
  3002. case WASM_OP_MISC_PREFIX:
  3003. {
  3004. uint32 opcode1;
  3005. read_leb_uint32(p, p_end, opcode1);
  3006. switch (opcode1) {
  3007. case WASM_OP_I32_TRUNC_SAT_S_F32:
  3008. case WASM_OP_I32_TRUNC_SAT_U_F32:
  3009. case WASM_OP_I32_TRUNC_SAT_S_F64:
  3010. case WASM_OP_I32_TRUNC_SAT_U_F64:
  3011. case WASM_OP_I64_TRUNC_SAT_S_F32:
  3012. case WASM_OP_I64_TRUNC_SAT_U_F32:
  3013. case WASM_OP_I64_TRUNC_SAT_S_F64:
  3014. case WASM_OP_I64_TRUNC_SAT_U_F64:
  3015. break;
  3016. #if WASM_ENABLE_BULK_MEMORY != 0
  3017. case WASM_OP_MEMORY_INIT:
  3018. skip_leb_uint32(p, p_end);
  3019. /* skip memory idx */
  3020. p++;
  3021. break;
  3022. case WASM_OP_DATA_DROP:
  3023. skip_leb_uint32(p, p_end);
  3024. break;
  3025. case WASM_OP_MEMORY_COPY:
  3026. /* skip two memory idx */
  3027. p += 2;
  3028. break;
  3029. case WASM_OP_MEMORY_FILL:
  3030. /* skip memory idx */
  3031. p++;
  3032. break;
  3033. #endif
  3034. default:
  3035. set_error_buf_v(error_buf, error_buf_size,
  3036. "%s %02x %02x",
  3037. "unsupported opcode", 0xfc, opcode);
  3038. return false;
  3039. }
  3040. break;
  3041. }
  3042. #if WASM_ENABLE_SHARED_MEMORY != 0
  3043. case WASM_OP_ATOMIC_PREFIX:
  3044. {
  3045. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  3046. opcode = read_uint8(p);
  3047. if (opcode != WASM_OP_ATOMIC_FENCE) {
  3048. skip_leb_uint32(p, p_end); /* align */
  3049. skip_leb_uint32(p, p_end); /* offset */
  3050. }
  3051. else {
  3052. /* atomic.fence doesn't have memarg */
  3053. p++;
  3054. }
  3055. break;
  3056. }
  3057. #endif
  3058. default:
  3059. set_error_buf_v(error_buf, error_buf_size,
  3060. "%s %02x",
  3061. "unsupported opcode", opcode);
  3062. return false;
  3063. }
  3064. }
  3065. (void)u8;
  3066. return false;
  3067. fail:
  3068. return false;
  3069. }
  3070. #define REF_I32 VALUE_TYPE_I32
  3071. #define REF_F32 VALUE_TYPE_F32
  3072. #define REF_I64_1 VALUE_TYPE_I64
  3073. #define REF_I64_2 VALUE_TYPE_I64
  3074. #define REF_F64_1 VALUE_TYPE_F64
  3075. #define REF_F64_2 VALUE_TYPE_F64
  3076. #define REF_ANY VALUE_TYPE_ANY
  3077. #if WASM_ENABLE_FAST_INTERP != 0
  3078. #if WASM_DEBUG_PREPROCESSOR != 0
  3079. #define LOG_OP(...) os_printf(__VA_ARGS__)
  3080. #else
  3081. #define LOG_OP(...) (void)0
  3082. #endif
  3083. #define PATCH_ELSE 0
  3084. #define PATCH_END 1
  3085. typedef struct BranchBlockPatch {
  3086. struct BranchBlockPatch *next;
  3087. uint8 patch_type;
  3088. uint8 *code_compiled;
  3089. } BranchBlockPatch;
  3090. #endif
  3091. typedef struct BranchBlock {
  3092. uint8 label_type;
  3093. BlockType block_type;
  3094. uint8 *start_addr;
  3095. uint8 *else_addr;
  3096. uint8 *end_addr;
  3097. uint32 stack_cell_num;
  3098. #if WASM_ENABLE_FAST_INTERP != 0
  3099. uint16 dynamic_offset;
  3100. uint8 *code_compiled;
  3101. BranchBlockPatch *patch_list;
  3102. /* This is used to save params frame_offset of of if block */
  3103. int16 *param_frame_offsets;
  3104. #endif
  3105. /* Indicate the operand stack is in polymorphic state.
  3106. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  3107. * to polymorphic state until the block's 'end' opcode is processed.
  3108. * If stack is in polymorphic state and stack is empty, instruction can
  3109. * pop any type of value directly without decreasing stack top pointer
  3110. * and stack cell num. */
  3111. bool is_stack_polymorphic;
  3112. } BranchBlock;
  3113. typedef struct WASMLoaderContext {
  3114. /* frame ref stack */
  3115. uint8 *frame_ref;
  3116. uint8 *frame_ref_bottom;
  3117. uint8 *frame_ref_boundary;
  3118. uint32 frame_ref_size;
  3119. uint32 stack_cell_num;
  3120. uint32 max_stack_cell_num;
  3121. /* frame csp stack */
  3122. BranchBlock *frame_csp;
  3123. BranchBlock *frame_csp_bottom;
  3124. BranchBlock *frame_csp_boundary;
  3125. uint32 frame_csp_size;
  3126. uint32 csp_num;
  3127. uint32 max_csp_num;
  3128. #if WASM_ENABLE_FAST_INTERP != 0
  3129. /* frame offset stack */
  3130. int16 *frame_offset;
  3131. int16 *frame_offset_bottom;
  3132. int16 *frame_offset_boundary;
  3133. uint32 frame_offset_size;
  3134. int16 dynamic_offset;
  3135. int16 start_dynamic_offset;
  3136. int16 max_dynamic_offset;
  3137. /* preserved local offset */
  3138. int16 preserved_local_offset;
  3139. /* const buffer */
  3140. uint8 *const_buf;
  3141. uint16 num_const;
  3142. uint16 const_buf_size;
  3143. uint16 const_cell_num;
  3144. /* processed code */
  3145. uint8 *p_code_compiled;
  3146. uint8 *p_code_compiled_end;
  3147. uint32 code_compiled_size;
  3148. #endif
  3149. } WASMLoaderContext;
  3150. typedef struct Const {
  3151. WASMValue value;
  3152. uint16 slot_index;
  3153. uint8 value_type;
  3154. } Const;
  3155. static void*
  3156. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new,
  3157. char *error_buf, uint32 error_buf_size)
  3158. {
  3159. uint8 *mem_new;
  3160. bh_assert(size_new > size_old);
  3161. if ((mem_new = loader_malloc
  3162. (size_new, error_buf, error_buf_size))) {
  3163. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  3164. memset(mem_new + size_old, 0, size_new - size_old);
  3165. wasm_runtime_free(mem_old);
  3166. }
  3167. return mem_new;
  3168. }
  3169. #define MEM_REALLOC(mem, size_old, size_new) do { \
  3170. void *mem_new = memory_realloc(mem, size_old, size_new, \
  3171. error_buf, error_buf_size); \
  3172. if (!mem_new) \
  3173. goto fail; \
  3174. mem = mem_new; \
  3175. } while (0)
  3176. #define CHECK_CSP_PUSH() do { \
  3177. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  3178. MEM_REALLOC(ctx->frame_csp_bottom, ctx->frame_csp_size, \
  3179. (uint32)(ctx->frame_csp_size \
  3180. + 8 * sizeof(BranchBlock))); \
  3181. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  3182. ctx->frame_csp_boundary = ctx->frame_csp_bottom + \
  3183. ctx->frame_csp_size / sizeof(BranchBlock); \
  3184. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  3185. } \
  3186. } while (0)
  3187. #define CHECK_CSP_POP() do { \
  3188. if (ctx->csp_num < 1) { \
  3189. set_error_buf(error_buf, error_buf_size, \
  3190. "type mismatch: " \
  3191. "expect data but block stack was empty"); \
  3192. goto fail; \
  3193. } \
  3194. } while (0)
  3195. #if WASM_ENABLE_FAST_INTERP != 0
  3196. static bool
  3197. check_offset_push(WASMLoaderContext *ctx,
  3198. char *error_buf, uint32 error_buf_size)
  3199. {
  3200. uint32 cell_num = (ctx->frame_offset - ctx->frame_offset_bottom);
  3201. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  3202. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  3203. ctx->frame_offset_size + 16);
  3204. ctx->frame_offset_size += 16;
  3205. ctx->frame_offset_boundary = ctx->frame_offset_bottom +
  3206. ctx->frame_offset_size / sizeof(int16);
  3207. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  3208. }
  3209. return true;
  3210. fail:
  3211. return false;
  3212. }
  3213. static bool
  3214. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  3215. {
  3216. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  3217. return false;
  3218. return true;
  3219. }
  3220. static void
  3221. free_label_patch_list(BranchBlock *frame_csp)
  3222. {
  3223. BranchBlockPatch *label_patch = frame_csp->patch_list;
  3224. BranchBlockPatch *next;
  3225. while (label_patch != NULL) {
  3226. next = label_patch->next;
  3227. wasm_runtime_free(label_patch);
  3228. label_patch = next;
  3229. }
  3230. frame_csp->patch_list = NULL;
  3231. }
  3232. static void
  3233. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  3234. {
  3235. BranchBlock *tmp_csp = frame_csp;
  3236. for (uint32 i = 0; i < csp_num; i++) {
  3237. free_label_patch_list(tmp_csp);
  3238. tmp_csp ++;
  3239. }
  3240. }
  3241. #endif
  3242. static bool
  3243. check_stack_push(WASMLoaderContext *ctx,
  3244. char *error_buf, uint32 error_buf_size)
  3245. {
  3246. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  3247. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  3248. ctx->frame_ref_size + 16);
  3249. ctx->frame_ref_size += 16;
  3250. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  3251. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  3252. }
  3253. return true;
  3254. fail:
  3255. return false;
  3256. }
  3257. static bool
  3258. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  3259. char *error_buf, uint32 error_buf_size)
  3260. {
  3261. char *type_str[] = { "f64", "f32", "i64", "i32" };
  3262. if (((type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  3263. && stack_cell_num < 1)
  3264. || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  3265. && stack_cell_num < 2)) {
  3266. set_error_buf(error_buf, error_buf_size,
  3267. "type mismatch: expect data but stack was empty");
  3268. return false;
  3269. }
  3270. if ((type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  3271. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  3272. || (type == VALUE_TYPE_I64
  3273. && (*(frame_ref - 2) != REF_I64_1
  3274. || *(frame_ref - 1) != REF_I64_2))
  3275. || (type == VALUE_TYPE_F64
  3276. && (*(frame_ref - 2) != REF_F64_1
  3277. || *(frame_ref - 1) != REF_F64_2))) {
  3278. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  3279. "type mismatch: expect ",
  3280. type_str[type - VALUE_TYPE_F64],
  3281. " but got other");
  3282. return false;
  3283. }
  3284. return true;
  3285. }
  3286. static bool
  3287. check_stack_pop(WASMLoaderContext *ctx, uint8 type,
  3288. char *error_buf, uint32 error_buf_size)
  3289. {
  3290. int32 block_stack_cell_num = (int32)
  3291. (ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  3292. if (block_stack_cell_num > 0
  3293. && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  3294. /* the stack top is a value of any type, return success */
  3295. return true;
  3296. }
  3297. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num,
  3298. type, error_buf, error_buf_size))
  3299. return false;
  3300. return true;
  3301. }
  3302. static void
  3303. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  3304. {
  3305. if (ctx) {
  3306. if (ctx->frame_ref_bottom)
  3307. wasm_runtime_free(ctx->frame_ref_bottom);
  3308. if (ctx->frame_csp_bottom) {
  3309. #if WASM_ENABLE_FAST_INTERP != 0
  3310. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  3311. #endif
  3312. wasm_runtime_free(ctx->frame_csp_bottom);
  3313. }
  3314. #if WASM_ENABLE_FAST_INTERP != 0
  3315. if (ctx->frame_offset_bottom)
  3316. wasm_runtime_free(ctx->frame_offset_bottom);
  3317. if (ctx->const_buf)
  3318. wasm_runtime_free(ctx->const_buf);
  3319. #endif
  3320. wasm_runtime_free(ctx);
  3321. }
  3322. }
  3323. static WASMLoaderContext*
  3324. wasm_loader_ctx_init(WASMFunction *func)
  3325. {
  3326. WASMLoaderContext *loader_ctx =
  3327. wasm_runtime_malloc(sizeof(WASMLoaderContext));
  3328. if (!loader_ctx)
  3329. return false;
  3330. memset(loader_ctx, 0, sizeof(WASMLoaderContext));
  3331. loader_ctx->frame_ref_size = 32;
  3332. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref =
  3333. wasm_runtime_malloc(loader_ctx->frame_ref_size)))
  3334. goto fail;
  3335. memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size);
  3336. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom +
  3337. loader_ctx->frame_ref_size;
  3338. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  3339. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp =
  3340. wasm_runtime_malloc(loader_ctx->frame_csp_size)))
  3341. goto fail;
  3342. memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size);
  3343. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  3344. #if WASM_ENABLE_FAST_INTERP != 0
  3345. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  3346. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  3347. wasm_runtime_malloc(loader_ctx->frame_offset_size)))
  3348. goto fail;
  3349. memset(loader_ctx->frame_offset_bottom, 0,
  3350. loader_ctx->frame_offset_size);
  3351. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  3352. loader_ctx->num_const = 0;
  3353. loader_ctx->const_buf_size = sizeof(Const) * 8;
  3354. if (!(loader_ctx->const_buf = wasm_runtime_malloc(loader_ctx->const_buf_size)))
  3355. goto fail;
  3356. memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size);
  3357. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  3358. loader_ctx->max_dynamic_offset = func->param_cell_num +
  3359. func->local_cell_num;
  3360. #endif
  3361. return loader_ctx;
  3362. fail:
  3363. wasm_loader_ctx_destroy(loader_ctx);
  3364. return NULL;
  3365. }
  3366. static bool
  3367. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type,
  3368. char *error_buf, uint32 error_buf_size)
  3369. {
  3370. if (type == VALUE_TYPE_VOID)
  3371. return true;
  3372. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3373. return false;
  3374. *ctx->frame_ref++ = type;
  3375. ctx->stack_cell_num++;
  3376. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  3377. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3378. if (type == VALUE_TYPE_I32
  3379. || type == VALUE_TYPE_F32
  3380. || type == VALUE_TYPE_ANY)
  3381. return true;
  3382. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3383. return false;
  3384. *ctx->frame_ref++ = type;
  3385. ctx->stack_cell_num++;
  3386. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  3387. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3388. return true;
  3389. }
  3390. static bool
  3391. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type,
  3392. char *error_buf, uint32 error_buf_size)
  3393. {
  3394. BranchBlock *cur_block = ctx->frame_csp - 1;
  3395. int32 available_stack_cell = (int32)
  3396. (ctx->stack_cell_num - cur_block->stack_cell_num);
  3397. /* Directly return success if current block is in stack
  3398. * polymorphic state while stack is empty. */
  3399. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  3400. return true;
  3401. if (type == VALUE_TYPE_VOID)
  3402. return true;
  3403. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  3404. return false;
  3405. ctx->frame_ref--;
  3406. ctx->stack_cell_num--;
  3407. if (type == VALUE_TYPE_I32
  3408. || type == VALUE_TYPE_F32
  3409. || *ctx->frame_ref == VALUE_TYPE_ANY)
  3410. return true;
  3411. ctx->frame_ref--;
  3412. ctx->stack_cell_num--;
  3413. return true;
  3414. }
  3415. static bool
  3416. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  3417. uint8 type_push, uint8 type_pop,
  3418. char *error_buf, uint32 error_buf_size)
  3419. {
  3420. for (int i = 0; i < pop_cnt; i++) {
  3421. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf, error_buf_size))
  3422. return false;
  3423. }
  3424. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  3425. return false;
  3426. return true;
  3427. }
  3428. static bool
  3429. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  3430. BlockType block_type, uint8* start_addr,
  3431. char *error_buf, uint32 error_buf_size)
  3432. {
  3433. CHECK_CSP_PUSH();
  3434. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  3435. ctx->frame_csp->label_type = label_type;
  3436. ctx->frame_csp->block_type = block_type;
  3437. ctx->frame_csp->start_addr = start_addr;
  3438. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  3439. #if WASM_ENABLE_FAST_INTERP != 0
  3440. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  3441. ctx->frame_csp->patch_list = NULL;
  3442. #endif
  3443. ctx->frame_csp++;
  3444. ctx->csp_num++;
  3445. if (ctx->csp_num > ctx->max_csp_num)
  3446. ctx->max_csp_num = ctx->csp_num;
  3447. return true;
  3448. fail:
  3449. return false;
  3450. }
  3451. static bool
  3452. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx,
  3453. char *error_buf, uint32 error_buf_size)
  3454. {
  3455. CHECK_CSP_POP();
  3456. #if WASM_ENABLE_FAST_INTERP != 0
  3457. if ((ctx->frame_csp - 1)->param_frame_offsets)
  3458. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  3459. #endif
  3460. ctx->frame_csp--;
  3461. ctx->csp_num--;
  3462. return true;
  3463. fail:
  3464. return false;
  3465. }
  3466. #if WASM_ENABLE_FAST_INTERP != 0
  3467. #if WASM_ENABLE_ABS_LABEL_ADDR != 0
  3468. #define emit_label(opcode) do { \
  3469. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  3470. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3471. } while (0)
  3472. #define skip_label() do { \
  3473. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  3474. LOG_OP("\ndelete last op\n"); \
  3475. } while (0)
  3476. #else
  3477. #define emit_label(opcode) do { \
  3478. int32 offset = (int32)(handle_table[opcode] - handle_table[0]); \
  3479. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  3480. set_error_buf(error_buf, error_buf_size, \
  3481. "pre-compiled label offset out of range"); \
  3482. goto fail; \
  3483. } \
  3484. wasm_loader_emit_int16(loader_ctx, offset); \
  3485. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3486. } while (0)
  3487. // drop local.get / const / block / loop / end
  3488. #define skip_label() do { \
  3489. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  3490. LOG_OP("\ndelete last op\n"); \
  3491. } while (0)
  3492. #endif /* WASM_ENABLE_ABS_LABEL_ADDR */
  3493. #define emit_empty_label_addr_and_frame_ip(type) do { \
  3494. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  3495. loader_ctx->p_code_compiled, \
  3496. error_buf, error_buf_size)) \
  3497. goto fail; \
  3498. /* label address, to be patched */ \
  3499. wasm_loader_emit_ptr(loader_ctx, NULL); \
  3500. } while (0)
  3501. #define emit_br_info(frame_csp) do { \
  3502. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, \
  3503. error_buf, error_buf_size)) \
  3504. goto fail; \
  3505. } while (0)
  3506. #define LAST_OP_OUTPUT_I32() (last_op >= WASM_OP_I32_EQZ \
  3507. && last_op <= WASM_OP_I32_ROTR) \
  3508. || (last_op == WASM_OP_I32_LOAD \
  3509. || last_op == WASM_OP_F32_LOAD) \
  3510. || (last_op >= WASM_OP_I32_LOAD8_S \
  3511. && last_op <= WASM_OP_I32_LOAD16_U) \
  3512. || (last_op >= WASM_OP_F32_ABS \
  3513. && last_op <= WASM_OP_F32_COPYSIGN) \
  3514. || (last_op >= WASM_OP_I32_WRAP_I64 \
  3515. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  3516. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  3517. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  3518. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  3519. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  3520. || (last_op == EXT_OP_COPY_STACK_TOP)
  3521. #define LAST_OP_OUTPUT_I64() (last_op >= WASM_OP_I64_CLZ \
  3522. && last_op <= WASM_OP_I64_ROTR) \
  3523. || (last_op >= WASM_OP_F64_ABS \
  3524. && last_op <= WASM_OP_F64_COPYSIGN) \
  3525. || (last_op == WASM_OP_I64_LOAD \
  3526. || last_op == WASM_OP_F64_LOAD) \
  3527. || (last_op >= WASM_OP_I64_LOAD8_S \
  3528. && last_op <= WASM_OP_I64_LOAD32_U) \
  3529. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  3530. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  3531. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  3532. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  3533. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  3534. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  3535. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  3536. #define GET_CONST_OFFSET(type, val) do { \
  3537. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  3538. &val, &operand_offset, \
  3539. error_buf, error_buf_size))) \
  3540. goto fail; \
  3541. } while (0)
  3542. #define GET_CONST_F32_OFFSET(type, fval) do { \
  3543. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  3544. &fval, &operand_offset, \
  3545. error_buf, error_buf_size))) \
  3546. goto fail; \
  3547. } while (0)
  3548. #define GET_CONST_F64_OFFSET(type, fval) do { \
  3549. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  3550. &fval, &operand_offset, \
  3551. error_buf, error_buf_size))) \
  3552. goto fail; \
  3553. } while (0)
  3554. #define emit_operand(ctx, offset) do { \
  3555. wasm_loader_emit_int16(ctx, offset); \
  3556. LOG_OP("%d\t", offset); \
  3557. } while (0)
  3558. #define emit_byte(ctx, byte) do { \
  3559. wasm_loader_emit_uint8(ctx, byte); \
  3560. LOG_OP("%d\t", byte); \
  3561. } while (0)
  3562. #define emit_uint32(ctx, value) do { \
  3563. wasm_loader_emit_uint32(ctx, value); \
  3564. LOG_OP("%d\t", value); \
  3565. } while (0)
  3566. #define emit_leb() do { \
  3567. wasm_loader_emit_leb(loader_ctx, p_org, p); \
  3568. } while (0)
  3569. static bool
  3570. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  3571. {
  3572. if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size)))
  3573. return false;
  3574. memset(ctx->p_code_compiled, 0, ctx->code_compiled_size);
  3575. ctx->p_code_compiled_end = ctx->p_code_compiled +
  3576. ctx->code_compiled_size;
  3577. /* clean up frame ref */
  3578. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  3579. ctx->frame_ref = ctx->frame_ref_bottom;
  3580. ctx->stack_cell_num = 0;
  3581. /* clean up frame csp */
  3582. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  3583. ctx->frame_csp = ctx->frame_csp_bottom;
  3584. ctx->csp_num = 0;
  3585. ctx->max_csp_num = 0;
  3586. /* clean up frame offset */
  3587. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  3588. ctx->frame_offset = ctx->frame_offset_bottom;
  3589. ctx->dynamic_offset = ctx->start_dynamic_offset;
  3590. /* init preserved local offsets */
  3591. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  3592. /* const buf is reserved */
  3593. return true;
  3594. }
  3595. static void
  3596. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  3597. {
  3598. if (ctx->p_code_compiled) {
  3599. *(uint32*)(ctx->p_code_compiled) = value;
  3600. ctx->p_code_compiled += sizeof(uint32);
  3601. }
  3602. else
  3603. ctx->code_compiled_size += sizeof(uint32);
  3604. }
  3605. static void
  3606. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  3607. {
  3608. if (ctx->p_code_compiled) {
  3609. *(int16*)(ctx->p_code_compiled) = value;
  3610. ctx->p_code_compiled += sizeof(int16);
  3611. }
  3612. else
  3613. ctx->code_compiled_size += sizeof(int16);
  3614. }
  3615. static void
  3616. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  3617. {
  3618. if (ctx->p_code_compiled) {
  3619. *(ctx->p_code_compiled) = value;
  3620. ctx->p_code_compiled += sizeof(uint8);
  3621. }
  3622. else
  3623. ctx->code_compiled_size += sizeof(uint8);
  3624. }
  3625. static void
  3626. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  3627. {
  3628. if (ctx->p_code_compiled) {
  3629. *(uint8**)(ctx->p_code_compiled) = value;
  3630. ctx->p_code_compiled += sizeof(void *);
  3631. }
  3632. else
  3633. ctx->code_compiled_size += sizeof(void *);
  3634. }
  3635. static void
  3636. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  3637. {
  3638. if (ctx->p_code_compiled) {
  3639. ctx->p_code_compiled -= size;
  3640. }
  3641. else
  3642. ctx->code_compiled_size -= size;
  3643. }
  3644. static void
  3645. wasm_loader_emit_leb(WASMLoaderContext *ctx, uint8* start, uint8* end)
  3646. {
  3647. if (ctx->p_code_compiled) {
  3648. bh_memcpy_s(ctx->p_code_compiled,
  3649. ctx->p_code_compiled_end - ctx->p_code_compiled,
  3650. start, end - start);
  3651. ctx->p_code_compiled += (end - start);
  3652. }
  3653. else {
  3654. ctx->code_compiled_size += (end - start);
  3655. }
  3656. }
  3657. static bool
  3658. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  3659. uint32 local_index, uint32 local_type, bool *preserved,
  3660. char *error_buf, uint32 error_buf_size)
  3661. {
  3662. int16 preserved_offset = (int16)local_index;
  3663. *preserved = false;
  3664. for (uint32 i = 0; i < loader_ctx->stack_cell_num; i++) {
  3665. /* move previous local into dynamic space before a set/tee_local opcode */
  3666. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  3667. if (preserved_offset == (int16)local_index) {
  3668. *preserved = true;
  3669. skip_label();
  3670. if (local_type == VALUE_TYPE_I32
  3671. || local_type == VALUE_TYPE_F32) {
  3672. preserved_offset = loader_ctx->preserved_local_offset;
  3673. /* Only increase preserve offset in the second traversal */
  3674. if (loader_ctx->p_code_compiled)
  3675. loader_ctx->preserved_local_offset++;
  3676. emit_label(EXT_OP_COPY_STACK_TOP);
  3677. }
  3678. else {
  3679. preserved_offset = loader_ctx->preserved_local_offset;
  3680. if (loader_ctx->p_code_compiled)
  3681. loader_ctx->preserved_local_offset += 2;
  3682. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  3683. }
  3684. emit_operand(loader_ctx, local_index);
  3685. emit_operand(loader_ctx, preserved_offset);
  3686. emit_label(opcode);
  3687. }
  3688. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  3689. }
  3690. }
  3691. return true;
  3692. #if WASM_ENABLE_ABS_LABEL_ADDR == 0
  3693. fail:
  3694. return false;
  3695. #endif
  3696. }
  3697. static bool
  3698. add_label_patch_to_list(BranchBlock *frame_csp,
  3699. uint8 patch_type, uint8 *p_code_compiled,
  3700. char *error_buf, uint32 error_buf_size)
  3701. {
  3702. BranchBlockPatch *patch = loader_malloc
  3703. (sizeof(BranchBlockPatch), error_buf, error_buf_size);
  3704. if (!patch) {
  3705. return false;
  3706. }
  3707. patch->patch_type = patch_type;
  3708. patch->code_compiled = p_code_compiled;
  3709. if (!frame_csp->patch_list) {
  3710. frame_csp->patch_list = patch;
  3711. patch->next = NULL;
  3712. }
  3713. else {
  3714. patch->next = frame_csp->patch_list;
  3715. frame_csp->patch_list = patch;
  3716. }
  3717. return true;
  3718. }
  3719. static void
  3720. apply_label_patch(WASMLoaderContext *ctx, uint8 depth,
  3721. uint8 patch_type)
  3722. {
  3723. BranchBlock *frame_csp = ctx->frame_csp - depth;
  3724. BranchBlockPatch *node = frame_csp->patch_list;
  3725. BranchBlockPatch *node_prev = NULL, *node_next;
  3726. if (!ctx->p_code_compiled)
  3727. return;
  3728. while (node) {
  3729. node_next = node->next;
  3730. if (node->patch_type == patch_type) {
  3731. *((uint8**)node->code_compiled) = ctx->p_code_compiled;
  3732. if (node_prev == NULL) {
  3733. frame_csp->patch_list = node_next;
  3734. }
  3735. else {
  3736. node_prev->next = node_next;
  3737. }
  3738. wasm_runtime_free(node);
  3739. }
  3740. else {
  3741. node_prev = node;
  3742. }
  3743. node = node_next;
  3744. }
  3745. }
  3746. static bool
  3747. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  3748. char *error_buf, uint32 error_buf_size)
  3749. {
  3750. /* br info layout:
  3751. * a) arity of target block
  3752. * b) total cell num of arity values
  3753. * c) each arity value's cell num
  3754. * d) each arity value's src frame offset
  3755. * e) each arity values's dst dynamic offset
  3756. * f) branch target address
  3757. *
  3758. * Note: b-e are omitted when arity is 0 so that
  3759. * interpreter can recover the br info quickly.
  3760. */
  3761. BlockType *block_type = &frame_csp->block_type;
  3762. uint8 *types = NULL, cell;
  3763. uint32 arity = 0;
  3764. int32 i;
  3765. int16 *frame_offset = ctx->frame_offset;
  3766. uint16 dynamic_offset;
  3767. /* Note: loop's arity is different from if and block. loop's arity is
  3768. * its parameter count while if and block arity is result count.
  3769. */
  3770. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  3771. arity = block_type_get_param_types(block_type, &types);
  3772. else
  3773. arity = block_type_get_result_types(block_type, &types);
  3774. /* Part a */
  3775. emit_uint32(ctx, arity);
  3776. if (arity) {
  3777. /* Part b */
  3778. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  3779. /* Part c */
  3780. for (i = (int32)arity - 1; i >= 0; i--) {
  3781. cell = wasm_value_type_cell_num(types[i]);
  3782. emit_byte(ctx, cell);
  3783. }
  3784. /* Part d */
  3785. for (i = (int32)arity - 1; i >= 0; i--) {
  3786. cell = wasm_value_type_cell_num(types[i]);
  3787. frame_offset -= cell;
  3788. emit_operand(ctx, *(int16*)(frame_offset));
  3789. }
  3790. /* Part e */
  3791. dynamic_offset = frame_csp->dynamic_offset
  3792. + wasm_get_cell_num(types, arity);
  3793. for (i = (int32)arity - 1; i >= 0; i--) {
  3794. cell = wasm_value_type_cell_num(types[i]);
  3795. dynamic_offset -= cell;
  3796. emit_operand(ctx, dynamic_offset);
  3797. }
  3798. }
  3799. /* Part f */
  3800. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  3801. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  3802. }
  3803. else {
  3804. if (!add_label_patch_to_list(frame_csp, PATCH_END,
  3805. ctx->p_code_compiled,
  3806. error_buf, error_buf_size))
  3807. return false;
  3808. /* label address, to be patched */
  3809. wasm_loader_emit_ptr(ctx, NULL);
  3810. }
  3811. return true;
  3812. }
  3813. static bool
  3814. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  3815. bool disable_emit, int16 operand_offset,
  3816. char *error_buf, uint32 error_buf_size)
  3817. {
  3818. if (type == VALUE_TYPE_VOID)
  3819. return true;
  3820. // only check memory overflow in first traverse
  3821. if (ctx->p_code_compiled == NULL) {
  3822. if (!check_offset_push(ctx, error_buf, error_buf_size))
  3823. return false;
  3824. }
  3825. if (disable_emit)
  3826. *(ctx->frame_offset)++ = operand_offset;
  3827. else {
  3828. emit_operand(ctx, ctx->dynamic_offset);
  3829. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  3830. ctx->dynamic_offset++;
  3831. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  3832. ctx->max_dynamic_offset = ctx->dynamic_offset;
  3833. }
  3834. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  3835. return true;
  3836. if (ctx->p_code_compiled == NULL) {
  3837. if (!check_offset_push(ctx, error_buf, error_buf_size))
  3838. return false;
  3839. }
  3840. ctx->frame_offset++;
  3841. if (!disable_emit) {
  3842. ctx->dynamic_offset++;
  3843. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  3844. ctx->max_dynamic_offset = ctx->dynamic_offset;
  3845. }
  3846. return true;
  3847. }
  3848. /* This function should be in front of wasm_loader_pop_frame_ref
  3849. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  3850. will be modified by wasm_loader_pop_frame_ref */
  3851. static bool
  3852. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  3853. char *error_buf, uint32 error_buf_size)
  3854. {
  3855. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  3856. then current block is the function block */
  3857. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  3858. BranchBlock *cur_block = ctx->frame_csp - depth;
  3859. int32 available_stack_cell = (int32)
  3860. (ctx->stack_cell_num - cur_block->stack_cell_num);
  3861. /* Directly return success if current block is in stack
  3862. * polymorphic state while stack is empty. */
  3863. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  3864. return true;
  3865. if (type == VALUE_TYPE_VOID)
  3866. return true;
  3867. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) {
  3868. /* Check the offset stack bottom to ensure the frame offset
  3869. stack will not go underflow. But we don't thrown error
  3870. and return true here, because the error msg should be
  3871. given in wasm_loader_pop_frame_ref */
  3872. if (!check_offset_pop(ctx, 1))
  3873. return true;
  3874. ctx->frame_offset -= 1;
  3875. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  3876. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  3877. ctx->dynamic_offset -= 1;
  3878. }
  3879. else {
  3880. if (!check_offset_pop(ctx, 2))
  3881. return true;
  3882. ctx->frame_offset -= 2;
  3883. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  3884. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  3885. ctx->dynamic_offset -= 2;
  3886. }
  3887. emit_operand(ctx, *(ctx->frame_offset));
  3888. return true;
  3889. }
  3890. static bool
  3891. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  3892. uint8 type_push, uint8 type_pop,
  3893. bool disable_emit, int16 operand_offset,
  3894. char *error_buf, uint32 error_buf_size)
  3895. {
  3896. for (int i = 0; i < pop_cnt; i++) {
  3897. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf, error_buf_size))
  3898. return false;
  3899. }
  3900. if (!wasm_loader_push_frame_offset(ctx, type_push,
  3901. disable_emit, operand_offset,
  3902. error_buf, error_buf_size))
  3903. return false;
  3904. return true;
  3905. }
  3906. static bool
  3907. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  3908. bool disable_emit, int16 operand_offset,
  3909. char *error_buf, uint32 error_buf_size)
  3910. {
  3911. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  3912. error_buf, error_buf_size)))
  3913. return false;
  3914. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  3915. return false;
  3916. return true;
  3917. }
  3918. static bool
  3919. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  3920. char *error_buf, uint32 error_buf_size)
  3921. {
  3922. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  3923. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  3924. return false;
  3925. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  3926. return false;
  3927. return true;
  3928. }
  3929. static bool
  3930. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  3931. uint8 type_push, uint8 type_pop,
  3932. bool disable_emit, int16 operand_offset,
  3933. char *error_buf, uint32 error_buf_size)
  3934. {
  3935. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  3936. disable_emit, operand_offset,
  3937. error_buf, error_buf_size))
  3938. return false;
  3939. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  3940. error_buf, error_buf_size))
  3941. return false;
  3942. return true;
  3943. }
  3944. static bool
  3945. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type,
  3946. void *value, int16 *offset,
  3947. char *error_buf, uint32 error_buf_size)
  3948. {
  3949. int16 operand_offset = 0;
  3950. Const *c;
  3951. for (c = (Const *)ctx->const_buf;
  3952. (uint8*)c < ctx->const_buf + ctx->num_const * sizeof(Const); c ++) {
  3953. if ((type == c->value_type)
  3954. && ((type == VALUE_TYPE_I64 && *(int64*)value == c->value.i64)
  3955. || (type == VALUE_TYPE_I32 && *(int32*)value == c->value.i32)
  3956. || (type == VALUE_TYPE_F64
  3957. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  3958. || (type == VALUE_TYPE_F32
  3959. && (0 == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  3960. operand_offset = c->slot_index;
  3961. break;
  3962. }
  3963. if (c->value_type == VALUE_TYPE_I64
  3964. || c->value_type == VALUE_TYPE_F64)
  3965. operand_offset += 2;
  3966. else
  3967. operand_offset += 1;
  3968. }
  3969. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  3970. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  3971. MEM_REALLOC(ctx->const_buf,
  3972. ctx->const_buf_size,
  3973. ctx->const_buf_size + 4 * sizeof(Const));
  3974. ctx->const_buf_size += 4 * sizeof(Const);
  3975. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  3976. }
  3977. c->value_type = type;
  3978. switch (type) {
  3979. case VALUE_TYPE_F64:
  3980. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value, sizeof(float64));
  3981. ctx->const_cell_num += 2;
  3982. /* The const buf will be reversed, we use the second cell */
  3983. /* of the i64/f64 const so the finnal offset is corrent */
  3984. operand_offset ++;
  3985. break;
  3986. case VALUE_TYPE_I64:
  3987. c->value.i64 = *(int64*)value;
  3988. ctx->const_cell_num += 2;
  3989. operand_offset ++;
  3990. break;
  3991. case VALUE_TYPE_F32:
  3992. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value, sizeof(float32));
  3993. ctx->const_cell_num ++;
  3994. break;
  3995. case VALUE_TYPE_I32:
  3996. c->value.i32 = *(int32*)value;
  3997. ctx->const_cell_num ++;
  3998. break;
  3999. default:
  4000. break;
  4001. }
  4002. c->slot_index = operand_offset;
  4003. ctx->num_const ++;
  4004. LOG_OP("#### new const [%d]: %ld\n",
  4005. ctx->num_const, (int64)c->value.i64);
  4006. }
  4007. /* use negetive index for const */
  4008. operand_offset = -(operand_offset + 1);
  4009. *offset = operand_offset;
  4010. return true;
  4011. fail:
  4012. return false;
  4013. }
  4014. /*
  4015. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  4016. -- Mostly used for the binary / compare operation
  4017. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  4018. -- Mostly used in block / control instructions
  4019. The POP will always emit the offset on the top of the frame_offset stack
  4020. PUSH can be used in two ways:
  4021. 1. directly PUSH:
  4022. PUSH_XXX();
  4023. will allocate a dynamic space and emit
  4024. 2. silent PUSH:
  4025. operand_offset = xxx; disable_emit = true;
  4026. PUSH_XXX();
  4027. only push the frame_offset stack, no emit
  4028. */
  4029. #define PUSH_I32() do { \
  4030. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4031. disable_emit, operand_offset,\
  4032. error_buf, error_buf_size)) \
  4033. goto fail; \
  4034. } while (0)
  4035. #define PUSH_F32() do { \
  4036. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4037. disable_emit, operand_offset,\
  4038. error_buf, error_buf_size)) \
  4039. goto fail; \
  4040. } while (0)
  4041. #define PUSH_I64() do { \
  4042. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4043. disable_emit, operand_offset,\
  4044. error_buf, error_buf_size)) \
  4045. goto fail; \
  4046. } while (0)
  4047. #define PUSH_F64() do { \
  4048. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4049. disable_emit, operand_offset,\
  4050. error_buf, error_buf_size)) \
  4051. goto fail; \
  4052. } while (0)
  4053. #define POP_I32() do { \
  4054. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4055. error_buf, error_buf_size)) \
  4056. goto fail; \
  4057. } while (0)
  4058. #define POP_F32() do { \
  4059. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4060. error_buf, error_buf_size)) \
  4061. goto fail; \
  4062. } while (0)
  4063. #define POP_I64() do { \
  4064. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4065. error_buf, error_buf_size)) \
  4066. goto fail; \
  4067. } while (0)
  4068. #define POP_F64() do { \
  4069. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4070. error_buf, error_buf_size)) \
  4071. goto fail; \
  4072. } while (0)
  4073. #define PUSH_OFFSET_TYPE(type) do { \
  4074. if (!(wasm_loader_push_frame_offset(loader_ctx, type, \
  4075. disable_emit, operand_offset, \
  4076. error_buf, error_buf_size))) \
  4077. goto fail; \
  4078. } while (0)
  4079. #define POP_OFFSET_TYPE(type) do { \
  4080. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, \
  4081. error_buf, error_buf_size))) \
  4082. goto fail; \
  4083. } while (0)
  4084. #define POP_AND_PUSH(type_pop, type_push) do { \
  4085. if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 1, \
  4086. type_push, type_pop, \
  4087. disable_emit, operand_offset, \
  4088. error_buf, error_buf_size))) \
  4089. goto fail; \
  4090. } while (0)
  4091. /* type of POPs should be the same */
  4092. #define POP2_AND_PUSH(type_pop, type_push) do { \
  4093. if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 2, \
  4094. type_push, type_pop, \
  4095. disable_emit, operand_offset, \
  4096. error_buf, error_buf_size))) \
  4097. goto fail; \
  4098. } while (0)
  4099. #else /* WASM_ENABLE_FAST_INTERP */
  4100. #define PUSH_I32() do { \
  4101. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  4102. error_buf, error_buf_size))) \
  4103. goto fail; \
  4104. } while (0)
  4105. #define PUSH_F32() do { \
  4106. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  4107. error_buf, error_buf_size))) \
  4108. goto fail; \
  4109. } while (0)
  4110. #define PUSH_I64() do { \
  4111. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  4112. error_buf, error_buf_size))) \
  4113. goto fail; \
  4114. } while (0)
  4115. #define PUSH_F64() do { \
  4116. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  4117. error_buf, error_buf_size))) \
  4118. goto fail; \
  4119. } while (0)
  4120. #define POP_I32() do { \
  4121. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  4122. error_buf, error_buf_size))) \
  4123. goto fail; \
  4124. } while (0)
  4125. #define POP_F32() do { \
  4126. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  4127. error_buf, error_buf_size))) \
  4128. goto fail; \
  4129. } while (0)
  4130. #define POP_I64() do { \
  4131. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  4132. error_buf, error_buf_size))) \
  4133. goto fail; \
  4134. } while (0)
  4135. #define POP_F64() do { \
  4136. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  4137. error_buf, error_buf_size))) \
  4138. goto fail; \
  4139. } while (0)
  4140. #define POP_AND_PUSH(type_pop, type_push) do { \
  4141. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, \
  4142. type_push, type_pop, \
  4143. error_buf, error_buf_size))) \
  4144. goto fail; \
  4145. } while (0)
  4146. /* type of POPs should be the same */
  4147. #define POP2_AND_PUSH(type_pop, type_push) do { \
  4148. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, \
  4149. type_push, type_pop, \
  4150. error_buf, error_buf_size))) \
  4151. goto fail; \
  4152. } while (0)
  4153. #endif /* WASM_ENABLE_FAST_INTERP */
  4154. #if WASM_ENABLE_FAST_INTERP != 0
  4155. static bool
  4156. reserve_block_ret(WASMLoaderContext *loader_ctx,
  4157. uint8 opcode, bool disable_emit,
  4158. char *error_buf, uint32 error_buf_size)
  4159. {
  4160. int16 operand_offset = 0;
  4161. BranchBlock *block = (opcode == WASM_OP_ELSE) ?
  4162. loader_ctx->frame_csp - 1 : loader_ctx->frame_csp;
  4163. BlockType *block_type = &block->block_type;
  4164. uint8 *return_types = NULL;
  4165. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  4166. int32 i = 0;
  4167. int16 dynamic_offset, dynamic_offset_org,
  4168. *frame_offset = NULL, *frame_offset_org = NULL;
  4169. return_count = block_type_get_result_types(block_type, &return_types);
  4170. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  4171. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  4172. if (return_count == 1) {
  4173. uint8 cell = wasm_value_type_cell_num(return_types[0]);
  4174. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  4175. /* insert op_copy before else opcode */
  4176. if (opcode == WASM_OP_ELSE)
  4177. skip_label();
  4178. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP : EXT_OP_COPY_STACK_TOP_I64);
  4179. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  4180. emit_operand(loader_ctx, block->dynamic_offset);
  4181. if (opcode == WASM_OP_ELSE) {
  4182. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  4183. }
  4184. else {
  4185. loader_ctx->frame_offset -= cell;
  4186. loader_ctx->dynamic_offset = block->dynamic_offset;
  4187. PUSH_OFFSET_TYPE(return_types[0]);
  4188. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4189. }
  4190. if (opcode == WASM_OP_ELSE)
  4191. emit_label(opcode);
  4192. }
  4193. return true;
  4194. }
  4195. /* Copy stack top values to block's results which are in dynamic space.
  4196. * The instruction format:
  4197. * Part a: values count
  4198. * Part b: all values total cell num
  4199. * Part c: each value's cell_num, src offset and dst offset
  4200. * Part d: each value's src offset and dst offset
  4201. * Part e: each value's dst offset
  4202. */
  4203. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  4204. dynamic_offset = dynamic_offset_org =
  4205. block->dynamic_offset
  4206. + wasm_get_cell_num(return_types, return_count);
  4207. /* First traversal to get the count of values needed to be copied. */
  4208. for (i = (int32)return_count - 1; i >= 0; i--) {
  4209. uint8 cells = wasm_value_type_cell_num(return_types[i]);
  4210. frame_offset -= cells;
  4211. dynamic_offset -= cells;
  4212. if (dynamic_offset != *frame_offset) {
  4213. value_count++;
  4214. total_cel_num += cells;
  4215. }
  4216. }
  4217. if (value_count) {
  4218. uint32 j = 0;
  4219. uint8 *emit_data = NULL, *cells = NULL;
  4220. int16 *src_offsets = NULL;
  4221. uint16 *dst_offsets = NULL;
  4222. uint64 size = (uint64)value_count * (sizeof(*cells)
  4223. + sizeof(*src_offsets)
  4224. + sizeof(*dst_offsets));
  4225. /* Allocate memory for the emit data */
  4226. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4227. return false;
  4228. cells = emit_data;
  4229. src_offsets = (int16 *)(cells + value_count);
  4230. dst_offsets = (uint16 *)(src_offsets + value_count);
  4231. /* insert op_copy before else opcode */
  4232. if (opcode == WASM_OP_ELSE)
  4233. skip_label();
  4234. emit_label(EXT_OP_COPY_STACK_VALUES);
  4235. /* Part a) */
  4236. emit_uint32(loader_ctx, value_count);
  4237. /* Part b) */
  4238. emit_uint32(loader_ctx, total_cel_num);
  4239. /* Second traversal to get each value's cell num, src offset and dst offset. */
  4240. frame_offset = frame_offset_org;
  4241. dynamic_offset = dynamic_offset_org;
  4242. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  4243. uint8 cell = wasm_value_type_cell_num(return_types[i]);
  4244. frame_offset -= cell;
  4245. dynamic_offset -= cell;
  4246. if (dynamic_offset != *frame_offset) {
  4247. /* cell num */
  4248. cells[j] = cell;
  4249. /* src offset */
  4250. src_offsets[j] = *frame_offset;
  4251. /* dst offset */
  4252. dst_offsets[j] = dynamic_offset;
  4253. j++;
  4254. }
  4255. if (opcode == WASM_OP_ELSE) {
  4256. *frame_offset = dynamic_offset;
  4257. }
  4258. else {
  4259. loader_ctx->frame_offset = frame_offset;
  4260. loader_ctx->dynamic_offset = dynamic_offset;
  4261. PUSH_OFFSET_TYPE(return_types[i]);
  4262. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4263. loader_ctx->frame_offset = frame_offset_org;
  4264. loader_ctx->dynamic_offset = dynamic_offset_org;
  4265. }
  4266. }
  4267. bh_assert(j == value_count);
  4268. /* Emit the cells, src_offsets and dst_offsets */
  4269. for (j = 0; j < value_count; j++)
  4270. emit_byte(loader_ctx, cells[j]);
  4271. for (j = 0; j < value_count; j++)
  4272. emit_operand(loader_ctx, src_offsets[j]);
  4273. for (j = 0; j < value_count; j++)
  4274. emit_operand(loader_ctx, dst_offsets[j]);
  4275. if (opcode == WASM_OP_ELSE)
  4276. emit_label(opcode);
  4277. wasm_runtime_free(emit_data);
  4278. }
  4279. return true;
  4280. fail:
  4281. return false;
  4282. }
  4283. #endif /* WASM_ENABLE_FAST_INTERP */
  4284. #define RESERVE_BLOCK_RET() do { \
  4285. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, \
  4286. error_buf, error_buf_size)) \
  4287. goto fail; \
  4288. } while (0)
  4289. #define PUSH_TYPE(type) do { \
  4290. if (!(wasm_loader_push_frame_ref(loader_ctx, type, \
  4291. error_buf, error_buf_size))) \
  4292. goto fail; \
  4293. } while (0)
  4294. #define POP_TYPE(type) do { \
  4295. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, \
  4296. error_buf, error_buf_size))) \
  4297. goto fail; \
  4298. } while (0)
  4299. #define PUSH_CSP(label_type, block_type, _start_addr) do { \
  4300. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  4301. _start_addr, error_buf, \
  4302. error_buf_size)) \
  4303. goto fail; \
  4304. } while (0)
  4305. #define POP_CSP() do { \
  4306. if (!wasm_loader_pop_frame_csp(loader_ctx, \
  4307. error_buf, error_buf_size)) \
  4308. goto fail; \
  4309. } while (0)
  4310. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \
  4311. read_leb_uint32(p, p_end, local_idx); \
  4312. if (local_idx >= param_count + local_count) { \
  4313. set_error_buf(error_buf, error_buf_size, \
  4314. "unknown local"); \
  4315. goto fail; \
  4316. } \
  4317. local_type = local_idx < param_count \
  4318. ? param_types[local_idx] \
  4319. : local_types[local_idx - param_count]; \
  4320. local_offset = local_offsets[local_idx]; \
  4321. } while (0)
  4322. #define CHECK_BR(depth) do { \
  4323. if (!wasm_loader_check_br(loader_ctx, depth, \
  4324. error_buf, error_buf_size)) \
  4325. goto fail; \
  4326. } while (0)
  4327. static bool
  4328. check_memory(WASMModule *module,
  4329. char *error_buf, uint32 error_buf_size)
  4330. {
  4331. if (module->memory_count == 0
  4332. && module->import_memory_count == 0) {
  4333. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4334. return false;
  4335. }
  4336. return true;
  4337. }
  4338. #define CHECK_MEMORY() do { \
  4339. if (!check_memory(module, error_buf, error_buf_size)) \
  4340. goto fail; \
  4341. } while (0)
  4342. static bool
  4343. check_memory_access_align(uint8 opcode, uint32 align,
  4344. char *error_buf, uint32 error_buf_size)
  4345. {
  4346. uint8 mem_access_aligns[] = {
  4347. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  4348. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  4349. };
  4350. bh_assert(opcode >= WASM_OP_I32_LOAD
  4351. && opcode <= WASM_OP_I64_STORE32);
  4352. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  4353. set_error_buf(error_buf, error_buf_size,
  4354. "alignment must not be larger than natural");
  4355. return false;
  4356. }
  4357. return true;
  4358. }
  4359. #if WASM_ENABLE_SHARED_MEMORY != 0
  4360. static bool
  4361. check_memory_align_equal(uint8 opcode, uint32 align,
  4362. char *error_buf, uint32 error_buf_size)
  4363. {
  4364. uint8 wait_notify_aligns[] = {2, 2, 3};
  4365. uint8 mem_access_aligns[] = {
  4366. 2, 3, 0, 1, 0, 1, 2,
  4367. };
  4368. uint8 expect;
  4369. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  4370. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  4371. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  4372. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  4373. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  4374. }
  4375. else {
  4376. /* 7 opcodes in every group */
  4377. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  4378. }
  4379. if (align != expect) {
  4380. set_error_buf(error_buf, error_buf_size,
  4381. "alignment isn't equal to natural");
  4382. return false;
  4383. }
  4384. return true;
  4385. }
  4386. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  4387. static bool
  4388. is_value_type(uint8 type)
  4389. {
  4390. return type == VALUE_TYPE_I32 ||
  4391. type == VALUE_TYPE_I64 ||
  4392. type == VALUE_TYPE_F32 ||
  4393. type == VALUE_TYPE_F64 ||
  4394. type == VALUE_TYPE_VOID;
  4395. }
  4396. static bool
  4397. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  4398. char *error_buf, uint32 error_buf_size)
  4399. {
  4400. BranchBlock *target_block, *cur_block;
  4401. BlockType *target_block_type;
  4402. uint8 *types = NULL, *frame_ref;
  4403. uint32 arity = 0;
  4404. int32 i, available_stack_cell;
  4405. uint16 cell_num;
  4406. if (loader_ctx->csp_num < depth + 1) {
  4407. set_error_buf(error_buf, error_buf_size,
  4408. "unknown label, "
  4409. "unexpected end of section or function");
  4410. return false;
  4411. }
  4412. cur_block = loader_ctx->frame_csp - 1;
  4413. target_block = loader_ctx->frame_csp - (depth + 1);
  4414. target_block_type = &target_block->block_type;
  4415. frame_ref = loader_ctx->frame_ref;
  4416. /* Note: loop's arity is different from if and block. loop's arity is
  4417. * its parameter count while if and block arity is result count.
  4418. */
  4419. if (target_block->label_type == LABEL_TYPE_LOOP)
  4420. arity = block_type_get_param_types(target_block_type, &types);
  4421. else
  4422. arity = block_type_get_result_types(target_block_type, &types);
  4423. /* If the stack is in polymorphic state, just clear the stack
  4424. * and then re-push the values to make the stack top values
  4425. * match block type. */
  4426. if (cur_block->is_stack_polymorphic) {
  4427. for (i = (int32)arity -1; i >= 0; i--) {
  4428. #if WASM_ENABLE_FAST_INTERP != 0
  4429. POP_OFFSET_TYPE(types[i]);
  4430. #endif
  4431. POP_TYPE(types[i]);
  4432. }
  4433. for (i = 0; i < (int32)arity; i++) {
  4434. #if WASM_ENABLE_FAST_INTERP != 0
  4435. bool disable_emit = true;
  4436. int16 operand_offset = 0;
  4437. PUSH_OFFSET_TYPE(types[i]);
  4438. #endif
  4439. PUSH_TYPE(types[i]);
  4440. }
  4441. return true;
  4442. }
  4443. available_stack_cell = (int32)
  4444. (loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  4445. /* Check stack top values match target block type */
  4446. for (i = (int32)arity -1; i >= 0; i--) {
  4447. if (!check_stack_top_values(frame_ref, available_stack_cell,
  4448. types[i],
  4449. error_buf, error_buf_size))
  4450. return false;
  4451. cell_num = wasm_value_type_cell_num(types[i]);
  4452. frame_ref -= cell_num;
  4453. available_stack_cell -= cell_num;
  4454. }
  4455. return true;
  4456. fail:
  4457. return false;
  4458. }
  4459. static BranchBlock *
  4460. check_branch_block(WASMLoaderContext *loader_ctx,
  4461. uint8 **p_buf, uint8 *buf_end,
  4462. char *error_buf, uint32 error_buf_size)
  4463. {
  4464. uint8 *p = *p_buf, *p_end = buf_end;
  4465. BranchBlock *frame_csp_tmp;
  4466. uint32 depth;
  4467. read_leb_uint32(p, p_end, depth);
  4468. CHECK_BR(depth);
  4469. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  4470. #if WASM_ENABLE_FAST_INTERP != 0
  4471. emit_br_info(frame_csp_tmp);
  4472. #endif
  4473. *p_buf = p;
  4474. return frame_csp_tmp;
  4475. fail:
  4476. return NULL;
  4477. }
  4478. static bool
  4479. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  4480. char *error_buf, uint32 error_buf_size)
  4481. {
  4482. BlockType *block_type = &block->block_type;
  4483. uint8 *return_types = NULL;
  4484. uint32 return_count = 0;
  4485. int32 available_stack_cell, return_cell_num, i;
  4486. uint8 *frame_ref = NULL;
  4487. available_stack_cell = (int32)
  4488. (loader_ctx->stack_cell_num
  4489. - block->stack_cell_num);
  4490. return_count = block_type_get_result_types(block_type, &return_types);
  4491. return_cell_num = return_count > 0 ?
  4492. wasm_get_cell_num(return_types, return_count) : 0;
  4493. /* If the stack is in polymorphic state, just clear the stack
  4494. * and then re-push the values to make the stack top values
  4495. * match block type. */
  4496. if (block->is_stack_polymorphic) {
  4497. for (i = (int32)return_count -1; i >= 0; i--) {
  4498. #if WASM_ENABLE_FAST_INTERP != 0
  4499. POP_OFFSET_TYPE(return_types[i]);
  4500. #endif
  4501. POP_TYPE(return_types[i]);
  4502. }
  4503. /* Check stack is empty */
  4504. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  4505. set_error_buf(error_buf, error_buf_size,
  4506. "type mismatch: stack size does not match block type");
  4507. goto fail;
  4508. }
  4509. for (i = 0; i < (int32)return_count; i++) {
  4510. #if WASM_ENABLE_FAST_INTERP != 0
  4511. bool disable_emit = true;
  4512. int16 operand_offset = 0;
  4513. PUSH_OFFSET_TYPE(return_types[i]);
  4514. #endif
  4515. PUSH_TYPE(return_types[i]);
  4516. }
  4517. return true;
  4518. }
  4519. /* Check stack cell num equals return cell num */
  4520. if (available_stack_cell != return_cell_num) {
  4521. set_error_buf(error_buf, error_buf_size,
  4522. "type mismatch: stack size does not match block type");
  4523. goto fail;
  4524. }
  4525. /* Check stack values match return types */
  4526. frame_ref = loader_ctx->frame_ref;
  4527. for (i = (int32)return_count -1; i >= 0; i--) {
  4528. if (!check_stack_top_values(frame_ref, available_stack_cell,
  4529. return_types[i],
  4530. error_buf, error_buf_size))
  4531. return false;
  4532. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  4533. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  4534. }
  4535. return true;
  4536. fail:
  4537. return false;
  4538. }
  4539. #if WASM_ENABLE_FAST_INTERP != 0
  4540. /* Copy parameters to dynamic space.
  4541. * 1) POP original parameter out;
  4542. * 2) Push and copy original values to dynamic space.
  4543. * The copy instruction format:
  4544. * Part a: param count
  4545. * Part b: all param total cell num
  4546. * Part c: each param's cell_num, src offset and dst offset
  4547. * Part d: each param's src offset
  4548. * Part e: each param's dst offset
  4549. */
  4550. static bool
  4551. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  4552. char* error_buf, uint32 error_buf_size)
  4553. {
  4554. int16 *frame_offset = NULL;
  4555. uint8 *cells = NULL, cell;
  4556. int16 *src_offsets = NULL;
  4557. uint8 *emit_data = NULL;
  4558. uint32 i;
  4559. BranchBlock *block = loader_ctx->frame_csp - 1;
  4560. BlockType *block_type = &block->block_type;
  4561. WASMType *wasm_type = block_type->u.type;
  4562. uint32 param_count = block_type->u.type->param_count;
  4563. int16 condition_offset = 0;
  4564. bool disable_emit = false;
  4565. int16 operand_offset = 0;
  4566. uint64 size = (uint64)param_count * (sizeof(*cells)
  4567. + sizeof(*src_offsets));
  4568. /* For if block, we also need copy the condition operand offset. */
  4569. if (is_if_block)
  4570. size += sizeof(*cells) + sizeof(*src_offsets);
  4571. /* Allocate memory for the emit data */
  4572. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4573. return false;
  4574. cells = emit_data;
  4575. src_offsets = (int16 *)(cells + param_count);
  4576. if (is_if_block)
  4577. condition_offset = *loader_ctx->frame_offset;
  4578. /* POP original parameter out */
  4579. for (i = 0; i < param_count; i++) {
  4580. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  4581. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4582. }
  4583. frame_offset = loader_ctx->frame_offset;
  4584. /* Get each param's cell num and src offset */
  4585. for (i = 0; i < param_count; i++) {
  4586. cell = wasm_value_type_cell_num(wasm_type->types[i]);
  4587. cells[i] = cell;
  4588. src_offsets[i] = *frame_offset;
  4589. frame_offset += cell;
  4590. }
  4591. /* emit copy instruction */
  4592. emit_label(EXT_OP_COPY_STACK_VALUES);
  4593. /* Part a) */
  4594. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  4595. /* Part b) */
  4596. emit_uint32(loader_ctx, is_if_block ?
  4597. wasm_type->param_cell_num + 1 :
  4598. wasm_type->param_cell_num);
  4599. /* Part c) */
  4600. for (i = 0; i < param_count; i++)
  4601. emit_byte(loader_ctx, cells[i]);
  4602. if (is_if_block)
  4603. emit_byte(loader_ctx, 1);
  4604. /* Part d) */
  4605. for (i = 0; i < param_count; i++)
  4606. emit_operand(loader_ctx, src_offsets[i]);
  4607. if (is_if_block)
  4608. emit_operand(loader_ctx, condition_offset);
  4609. /* Part e) */
  4610. /* Push to dynamic space. The push will emit the dst offset. */
  4611. for (i = 0; i < param_count; i++)
  4612. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  4613. if (is_if_block)
  4614. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  4615. /* Free the emit data */
  4616. wasm_runtime_free(emit_data);
  4617. return true;
  4618. fail:
  4619. return false;
  4620. }
  4621. #endif
  4622. /* reset the stack to the state of before entering the last block */
  4623. #if WASM_ENABLE_FAST_INTERP != 0
  4624. #define RESET_STACK() do { \
  4625. loader_ctx->stack_cell_num = \
  4626. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4627. loader_ctx->frame_ref = \
  4628. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4629. loader_ctx->frame_offset = \
  4630. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  4631. } while (0)
  4632. #else
  4633. #define RESET_STACK() do { \
  4634. loader_ctx->stack_cell_num = \
  4635. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4636. loader_ctx->frame_ref = \
  4637. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4638. } while (0)
  4639. #endif
  4640. /* set current block's stack polymorphic state */
  4641. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) do { \
  4642. BranchBlock *cur_block = loader_ctx->frame_csp - 1; \
  4643. cur_block->is_stack_polymorphic = flag; \
  4644. } while (0)
  4645. #define BLOCK_HAS_PARAM(block_type) \
  4646. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  4647. static bool
  4648. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4649. BlockAddr *block_addr_cache,
  4650. char *error_buf, uint32 error_buf_size)
  4651. {
  4652. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  4653. uint32 param_count, local_count, global_count;
  4654. uint8 *param_types, *local_types, local_type, global_type;
  4655. BlockType func_type;
  4656. uint16 *local_offsets, local_offset;
  4657. uint32 count, i, local_idx, global_idx, u32, align, mem_offset;
  4658. int32 i32, i32_const = 0;
  4659. int64 i64;
  4660. uint8 opcode, u8;
  4661. bool return_value = false;
  4662. WASMLoaderContext *loader_ctx;
  4663. BranchBlock *frame_csp_tmp;
  4664. #if WASM_ENABLE_BULK_MEMORY != 0
  4665. uint32 segment_index;
  4666. #endif
  4667. #if WASM_ENABLE_FAST_INTERP != 0
  4668. uint8 *func_const_end, *func_const;
  4669. int16 operand_offset;
  4670. uint8 last_op = 0;
  4671. bool disable_emit, preserve_local = false;
  4672. float32 f32;
  4673. float64 f64;
  4674. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  4675. func->param_cell_num,
  4676. func->local_cell_num,
  4677. func->ret_cell_num);
  4678. #endif
  4679. global_count = module->import_global_count + module->global_count;
  4680. param_count = func->func_type->param_count;
  4681. param_types = func->func_type->types;
  4682. func_type.is_value_type = false;
  4683. func_type.u.type = func->func_type;
  4684. local_count = func->local_count;
  4685. local_types = func->local_types;
  4686. local_offsets = func->local_offsets;
  4687. if (!(loader_ctx = wasm_loader_ctx_init(func))) {
  4688. set_error_buf(error_buf, error_buf_size,
  4689. "allocate memory failed");
  4690. goto fail;
  4691. }
  4692. #if WASM_ENABLE_FAST_INTERP != 0
  4693. re_scan:
  4694. if (loader_ctx->code_compiled_size > 0) {
  4695. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  4696. set_error_buf(error_buf, error_buf_size,
  4697. "allocate memory failed");
  4698. goto fail;
  4699. }
  4700. p = func->code;
  4701. func->code_compiled = loader_ctx->p_code_compiled;
  4702. }
  4703. #endif
  4704. PUSH_CSP(LABEL_TYPE_FUNCTION, func_type, p);
  4705. while (p < p_end) {
  4706. opcode = *p++;
  4707. #if WASM_ENABLE_FAST_INTERP != 0
  4708. p_org = p;
  4709. disable_emit = false;
  4710. emit_label(opcode);
  4711. #endif
  4712. switch (opcode) {
  4713. case WASM_OP_UNREACHABLE:
  4714. RESET_STACK();
  4715. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  4716. break;
  4717. case WASM_OP_NOP:
  4718. #if WASM_ENABLE_FAST_INTERP != 0
  4719. skip_label();
  4720. #endif
  4721. break;
  4722. case WASM_OP_IF:
  4723. POP_I32();
  4724. goto handle_op_block_and_loop;
  4725. case WASM_OP_BLOCK:
  4726. case WASM_OP_LOOP:
  4727. handle_op_block_and_loop:
  4728. {
  4729. uint8 value_type;
  4730. BlockType block_type;
  4731. value_type = read_uint8(p);
  4732. if (is_value_type(value_type)) {
  4733. /* If the first byte is one of these special values:
  4734. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  4735. * the single return value. */
  4736. block_type.is_value_type = true;
  4737. block_type.u.value_type = value_type;
  4738. }
  4739. else {
  4740. uint32 type_index;
  4741. /* Resolve the leb128 encoded type index as block type */
  4742. p--;
  4743. read_leb_uint32(p, p_end, type_index);
  4744. if (type_index >= module->type_count) {
  4745. set_error_buf(error_buf, error_buf_size,
  4746. "unknown type");
  4747. goto fail;
  4748. }
  4749. block_type.is_value_type = false;
  4750. block_type.u.type = module->types[type_index];
  4751. #if WASM_ENABLE_FAST_INTERP == 0 \
  4752. && WASM_ENABLE_WAMR_COMPILER == 0 \
  4753. && WASM_ENABLE_JIT == 0
  4754. /* If block use type index as block type, change the opcode
  4755. * to new extended opcode so that interpreter can resolve the
  4756. * block quickly.
  4757. */
  4758. *(p - 2) = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  4759. #endif
  4760. }
  4761. /* Pop block parameters from stack */
  4762. if (BLOCK_HAS_PARAM(block_type)) {
  4763. WASMType *wasm_type = block_type.u.type;
  4764. for (i = 0; i < block_type.u.type->param_count; i++)
  4765. POP_TYPE(wasm_type->types[wasm_type->param_count - i - 1]);
  4766. }
  4767. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK), block_type, p);
  4768. /* Pass parameters to block */
  4769. if (BLOCK_HAS_PARAM(block_type)) {
  4770. for (i = 0; i < block_type.u.type->param_count; i++)
  4771. PUSH_TYPE(block_type.u.type->types[i]);
  4772. }
  4773. #if WASM_ENABLE_FAST_INTERP != 0
  4774. if (opcode == WASM_OP_BLOCK) {
  4775. skip_label();
  4776. } else if (opcode == WASM_OP_LOOP) {
  4777. skip_label();
  4778. if (BLOCK_HAS_PARAM(block_type)) {
  4779. /* Make sure params are in dynamic space */
  4780. if (!copy_params_to_dynamic_space(loader_ctx,
  4781. false,
  4782. error_buf,
  4783. error_buf_size))
  4784. goto fail;
  4785. }
  4786. (loader_ctx->frame_csp - 1)->code_compiled =
  4787. loader_ctx->p_code_compiled;
  4788. } else if (opcode == WASM_OP_IF) {
  4789. /* If block has parameters, we should make sure they are in
  4790. * dynamic space. Otherwise, when else branch is missing,
  4791. * the later opcode may consume incorrect operand offset.
  4792. * Spec case:
  4793. * (func (export "params-id") (param i32) (result i32)
  4794. * (i32.const 1)
  4795. * (i32.const 2)
  4796. * (if (param i32 i32) (result i32 i32) (local.get 0) (then))
  4797. * (i32.add)
  4798. * )
  4799. *
  4800. * So we should emit a copy instruction before the if.
  4801. *
  4802. * And we also need to save the parameter offsets and
  4803. * recover them before entering else branch.
  4804. *
  4805. */
  4806. if (BLOCK_HAS_PARAM(block_type)) {
  4807. BranchBlock *block = loader_ctx->frame_csp - 1;
  4808. uint64 size;
  4809. /* skip the if condition operand offset */
  4810. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4811. /* skip the if label */
  4812. skip_label();
  4813. /* Emit a copy instruction */
  4814. if (!copy_params_to_dynamic_space(loader_ctx,
  4815. true,
  4816. error_buf,
  4817. error_buf_size))
  4818. goto fail;
  4819. /* Emit the if instruction */
  4820. emit_label(opcode);
  4821. /* Emit the new condition operand offset */
  4822. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  4823. /* Save top param_count values of frame_offset stack, so that
  4824. * we can recover it before executing else branch */
  4825. size = sizeof(int16) *
  4826. (uint64)block_type.u.type->param_cell_num;
  4827. if (!(block->param_frame_offsets =
  4828. loader_malloc(size, error_buf, error_buf_size)))
  4829. goto fail;
  4830. bh_memcpy_s(block->param_frame_offsets,
  4831. size,
  4832. loader_ctx->frame_offset - size/sizeof(int16),
  4833. size);
  4834. }
  4835. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  4836. emit_empty_label_addr_and_frame_ip(PATCH_END);
  4837. }
  4838. #endif
  4839. break;
  4840. }
  4841. case WASM_OP_ELSE:
  4842. {
  4843. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  4844. if (loader_ctx->csp_num < 2
  4845. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF) {
  4846. set_error_buf(error_buf, error_buf_size,
  4847. "opcode else found without matched opcode if");
  4848. goto fail;
  4849. }
  4850. /* check whether if branch's stack matches its result type */
  4851. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  4852. error_buf, error_buf_size))
  4853. goto fail;
  4854. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  4855. #if WASM_ENABLE_FAST_INTERP != 0
  4856. /* if the result of if branch is in local or const area, add a copy op */
  4857. RESERVE_BLOCK_RET();
  4858. emit_empty_label_addr_and_frame_ip(PATCH_END);
  4859. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  4860. #endif
  4861. RESET_STACK();
  4862. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  4863. /* Pass parameters to if-false branch */
  4864. if (BLOCK_HAS_PARAM(block_type)) {
  4865. for (i = 0; i < block_type.u.type->param_count; i++)
  4866. PUSH_TYPE(block_type.u.type->types[i]);
  4867. }
  4868. #if WASM_ENABLE_FAST_INTERP != 0
  4869. /* Recover top param_count values of frame_offset stack */
  4870. if (BLOCK_HAS_PARAM((block_type))) {
  4871. uint32 size;
  4872. BranchBlock *block = loader_ctx->frame_csp - 1;
  4873. size = sizeof(int16) *
  4874. block_type.u.type->param_cell_num;
  4875. bh_memcpy_s(loader_ctx->frame_offset, size,
  4876. block->param_frame_offsets, size);
  4877. loader_ctx->frame_offset += (size/sizeof(int16));
  4878. }
  4879. #endif
  4880. break;
  4881. }
  4882. case WASM_OP_END:
  4883. {
  4884. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  4885. /* check whether block stack matches its result type */
  4886. if (!check_block_stack(loader_ctx, cur_block,
  4887. error_buf, error_buf_size))
  4888. goto fail;
  4889. /* if no else branch, and return types do not match param types, fail */
  4890. if (cur_block->label_type == LABEL_TYPE_IF
  4891. && !cur_block->else_addr) {
  4892. uint32 param_count = 0, ret_count = 0;
  4893. uint8 *param_types = NULL, *ret_types = NULL;
  4894. BlockType *block_type = &cur_block->block_type;
  4895. if (block_type->is_value_type) {
  4896. if (block_type->u.value_type != VALUE_TYPE_VOID) {
  4897. ret_count = 1;
  4898. ret_types = &block_type->u.value_type;
  4899. }
  4900. }
  4901. else {
  4902. param_count = block_type->u.type->param_count;
  4903. ret_count = block_type->u.type->result_count;
  4904. param_types = block_type->u.type->types;
  4905. ret_types = block_type->u.type->types + param_count;
  4906. }
  4907. if (param_count != ret_count
  4908. || (param_count && memcmp(param_types, ret_types, param_count))) {
  4909. set_error_buf(error_buf, error_buf_size,
  4910. "type mismatch: else branch missing");
  4911. goto fail;
  4912. }
  4913. }
  4914. POP_CSP();
  4915. #if WASM_ENABLE_FAST_INTERP != 0
  4916. skip_label();
  4917. /* copy the result to the block return address */
  4918. RESERVE_BLOCK_RET();
  4919. apply_label_patch(loader_ctx, 0, PATCH_END);
  4920. free_label_patch_list(loader_ctx->frame_csp);
  4921. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  4922. int32 idx;
  4923. uint8 ret_type;
  4924. emit_label(WASM_OP_RETURN);
  4925. for (idx = (int32)func->func_type->result_count - 1;
  4926. idx >= 0; idx--) {
  4927. ret_type = *(func->func_type->types
  4928. + func->func_type->param_count + idx);
  4929. POP_OFFSET_TYPE(ret_type);
  4930. }
  4931. }
  4932. #endif
  4933. if (loader_ctx->csp_num > 0) {
  4934. loader_ctx->frame_csp->end_addr = p - 1;
  4935. }
  4936. else {
  4937. /* end of function block, function will return,
  4938. ignore the following bytecodes */
  4939. p = p_end;
  4940. continue;
  4941. }
  4942. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  4943. break;
  4944. }
  4945. case WASM_OP_BR:
  4946. {
  4947. if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end,
  4948. error_buf, error_buf_size)))
  4949. goto fail;
  4950. RESET_STACK();
  4951. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  4952. break;
  4953. }
  4954. case WASM_OP_BR_IF:
  4955. {
  4956. POP_I32();
  4957. if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end,
  4958. error_buf, error_buf_size)))
  4959. goto fail;
  4960. break;
  4961. }
  4962. case WASM_OP_BR_TABLE:
  4963. {
  4964. uint8 *ret_types = NULL;
  4965. uint32 ret_count = 0;
  4966. read_leb_uint32(p, p_end, count);
  4967. #if WASM_ENABLE_FAST_INTERP != 0
  4968. emit_uint32(loader_ctx, count);
  4969. #endif
  4970. POP_I32();
  4971. /* TODO: check the const */
  4972. for (i = 0; i <= count; i++) {
  4973. if (!(frame_csp_tmp =
  4974. check_branch_block(loader_ctx, &p, p_end,
  4975. error_buf, error_buf_size)))
  4976. goto fail;
  4977. if (i == 0) {
  4978. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  4979. ret_count =
  4980. block_type_get_result_types(&frame_csp_tmp->block_type,
  4981. &ret_types);
  4982. }
  4983. else {
  4984. uint8 *tmp_ret_types = NULL;
  4985. uint32 tmp_ret_count = 0;
  4986. /* Check whether all table items have the same return type */
  4987. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  4988. tmp_ret_count =
  4989. block_type_get_result_types(&frame_csp_tmp->block_type,
  4990. &tmp_ret_types);
  4991. if (ret_count != tmp_ret_count
  4992. || (ret_count
  4993. && 0 != memcmp(ret_types, tmp_ret_types, ret_count))) {
  4994. set_error_buf(error_buf, error_buf_size,
  4995. "type mismatch: br_table targets must "
  4996. "all use same result type");
  4997. goto fail;
  4998. }
  4999. }
  5000. }
  5001. RESET_STACK();
  5002. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5003. break;
  5004. }
  5005. case WASM_OP_RETURN:
  5006. {
  5007. int32 idx;
  5008. uint8 ret_type;
  5009. for (idx = (int32)func->func_type->result_count - 1; idx >= 0; idx--) {
  5010. ret_type = *(func->func_type->types
  5011. + func->func_type->param_count + idx);
  5012. POP_TYPE(ret_type);
  5013. #if WASM_ENABLE_FAST_INTERP != 0
  5014. // emit the offset after return opcode
  5015. POP_OFFSET_TYPE(ret_type);
  5016. #endif
  5017. }
  5018. RESET_STACK();
  5019. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5020. break;
  5021. }
  5022. case WASM_OP_CALL:
  5023. {
  5024. WASMType *func_type;
  5025. uint32 func_idx;
  5026. int32 idx;
  5027. read_leb_uint32(p, p_end, func_idx);
  5028. #if WASM_ENABLE_FAST_INTERP != 0
  5029. // we need to emit func_idx before arguments
  5030. emit_uint32(loader_ctx, func_idx);
  5031. #endif
  5032. if (func_idx >= module->import_function_count + module->function_count) {
  5033. set_error_buf(error_buf, error_buf_size,
  5034. "unknown function");
  5035. goto fail;
  5036. }
  5037. if (func_idx < module->import_function_count)
  5038. func_type = module->import_functions[func_idx].u.function.func_type;
  5039. else
  5040. func_type =
  5041. module->functions[func_idx - module->import_function_count]->func_type;
  5042. if (func_type->param_count > 0) {
  5043. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) {
  5044. POP_TYPE(func_type->types[idx]);
  5045. #if WASM_ENABLE_FAST_INTERP != 0
  5046. POP_OFFSET_TYPE(func_type->types[idx]);
  5047. #endif
  5048. }
  5049. }
  5050. for (i = 0; i < func_type->result_count; i++) {
  5051. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5052. #if WASM_ENABLE_FAST_INTERP != 0
  5053. /* Here we emit each return value's dynamic_offset. But in fact
  5054. * these offsets are continuous, so interpreter only need to get
  5055. * the first return value's offset.
  5056. */
  5057. PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]);
  5058. #endif
  5059. }
  5060. func->has_op_func_call = true;
  5061. break;
  5062. }
  5063. case WASM_OP_CALL_INDIRECT:
  5064. {
  5065. int32 idx;
  5066. WASMType *func_type;
  5067. uint32 type_idx;
  5068. if (module->table_count == 0
  5069. && module->import_table_count == 0) {
  5070. set_error_buf(error_buf, error_buf_size,
  5071. "call indirect with unknown table");
  5072. goto fail;
  5073. }
  5074. read_leb_uint32(p, p_end, type_idx);
  5075. #if WASM_ENABLE_FAST_INTERP != 0
  5076. // we need to emit func_idx before arguments
  5077. emit_uint32(loader_ctx, type_idx);
  5078. #endif
  5079. /* reserved byte 0x00 */
  5080. if (*p++ != 0x00) {
  5081. set_error_buf(error_buf, error_buf_size,
  5082. "zero flag expected");
  5083. goto fail;
  5084. }
  5085. POP_I32();
  5086. if (type_idx >= module->type_count) {
  5087. set_error_buf(error_buf, error_buf_size,
  5088. "unknown type");
  5089. goto fail;
  5090. }
  5091. func_type = module->types[type_idx];
  5092. if (func_type->param_count > 0) {
  5093. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) {
  5094. POP_TYPE(func_type->types[idx]);
  5095. #if WASM_ENABLE_FAST_INTERP != 0
  5096. POP_OFFSET_TYPE(func_type->types[idx]);
  5097. #endif
  5098. }
  5099. }
  5100. for (i = 0; i < func_type->result_count; i++) {
  5101. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5102. #if WASM_ENABLE_FAST_INTERP != 0
  5103. PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]);
  5104. #endif
  5105. }
  5106. func->has_op_func_call = true;
  5107. break;
  5108. }
  5109. case WASM_OP_DROP:
  5110. case WASM_OP_DROP_64:
  5111. {
  5112. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5113. int32 available_stack_cell = (int32)
  5114. (loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  5115. if (available_stack_cell <= 0
  5116. && !cur_block->is_stack_polymorphic) {
  5117. set_error_buf(error_buf, error_buf_size,
  5118. "type mismatch, opcode drop was found "
  5119. "but stack was empty");
  5120. goto fail;
  5121. }
  5122. if (available_stack_cell > 0) {
  5123. if (*(loader_ctx->frame_ref - 1) == REF_I32
  5124. || *(loader_ctx->frame_ref - 1) == REF_F32) {
  5125. loader_ctx->frame_ref--;
  5126. loader_ctx->stack_cell_num--;
  5127. #if WASM_ENABLE_FAST_INTERP != 0
  5128. skip_label();
  5129. loader_ctx->frame_offset--;
  5130. if (*(loader_ctx->frame_offset) >
  5131. loader_ctx->start_dynamic_offset)
  5132. loader_ctx->dynamic_offset --;
  5133. #endif
  5134. }
  5135. else {
  5136. loader_ctx->frame_ref -= 2;
  5137. loader_ctx->stack_cell_num -= 2;
  5138. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  5139. *(p - 1) = WASM_OP_DROP_64;
  5140. #endif
  5141. #if WASM_ENABLE_FAST_INTERP != 0
  5142. skip_label();
  5143. loader_ctx->frame_offset -= 2;
  5144. if (*(loader_ctx->frame_offset) >
  5145. loader_ctx->start_dynamic_offset)
  5146. loader_ctx->dynamic_offset -= 2;
  5147. #endif
  5148. }
  5149. }
  5150. else {
  5151. #if WASM_ENABLE_FAST_INTERP != 0
  5152. skip_label();
  5153. #endif
  5154. }
  5155. break;
  5156. }
  5157. case WASM_OP_SELECT:
  5158. case WASM_OP_SELECT_64:
  5159. {
  5160. uint8 ref_type;
  5161. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5162. int32 available_stack_cell;
  5163. POP_I32();
  5164. available_stack_cell = (int32)
  5165. (loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  5166. if (available_stack_cell <= 0
  5167. && !cur_block->is_stack_polymorphic) {
  5168. set_error_buf(error_buf, error_buf_size,
  5169. "type mismatch, opcode select was found "
  5170. "but stack was empty");
  5171. goto fail;
  5172. }
  5173. if (available_stack_cell > 0) {
  5174. switch (*(loader_ctx->frame_ref - 1)) {
  5175. case REF_I32:
  5176. case REF_F32:
  5177. break;
  5178. case REF_I64_2:
  5179. case REF_F64_2:
  5180. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  5181. *(p - 1) = WASM_OP_SELECT_64;
  5182. #endif
  5183. #if WASM_ENABLE_FAST_INTERP != 0
  5184. if (loader_ctx->p_code_compiled) {
  5185. #if WASM_ENABLE_ABS_LABEL_ADDR != 0
  5186. *(void**)(loader_ctx->p_code_compiled - 2 - sizeof(void*)) =
  5187. handle_table[WASM_OP_SELECT_64];
  5188. #else
  5189. *((int16*)loader_ctx->p_code_compiled - 2) = (int16)
  5190. (handle_table[WASM_OP_SELECT_64] - handle_table[0]);
  5191. #endif
  5192. }
  5193. #endif
  5194. break;
  5195. }
  5196. ref_type = *(loader_ctx->frame_ref - 1);
  5197. #if WASM_ENABLE_FAST_INTERP != 0
  5198. POP_OFFSET_TYPE(ref_type);
  5199. #endif
  5200. POP_TYPE(ref_type);
  5201. #if WASM_ENABLE_FAST_INTERP != 0
  5202. POP_OFFSET_TYPE(ref_type);
  5203. #endif
  5204. POP_TYPE(ref_type);
  5205. #if WASM_ENABLE_FAST_INTERP != 0
  5206. PUSH_OFFSET_TYPE(ref_type);
  5207. #endif
  5208. PUSH_TYPE(ref_type);
  5209. }
  5210. else {
  5211. #if WASM_ENABLE_FAST_INTERP != 0
  5212. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  5213. #endif
  5214. PUSH_TYPE(VALUE_TYPE_ANY);
  5215. }
  5216. break;
  5217. }
  5218. case WASM_OP_GET_LOCAL:
  5219. {
  5220. p_org = p - 1;
  5221. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5222. PUSH_TYPE(local_type);
  5223. #if WASM_ENABLE_FAST_INTERP != 0
  5224. /* Get Local is optimized out */
  5225. skip_label();
  5226. disable_emit = true;
  5227. operand_offset = local_offset;
  5228. PUSH_OFFSET_TYPE(local_type);
  5229. #else
  5230. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5231. if (local_offset < 0x80) {
  5232. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  5233. if (local_type == VALUE_TYPE_I32
  5234. || local_type == VALUE_TYPE_F32)
  5235. *p_org++ = (uint8)local_offset;
  5236. else
  5237. *p_org++ = (uint8)(local_offset | 0x80);
  5238. while (p_org < p)
  5239. *p_org++ = WASM_OP_NOP;
  5240. }
  5241. #endif
  5242. #endif
  5243. break;
  5244. }
  5245. case WASM_OP_SET_LOCAL:
  5246. {
  5247. p_org = p - 1;
  5248. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5249. POP_TYPE(local_type);
  5250. #if WASM_ENABLE_FAST_INTERP != 0
  5251. if (!(preserve_referenced_local(loader_ctx, opcode, local_offset,
  5252. local_type, &preserve_local,
  5253. error_buf, error_buf_size)))
  5254. goto fail;
  5255. if (local_offset < 256) {
  5256. skip_label();
  5257. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  5258. if (loader_ctx->p_code_compiled)
  5259. *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
  5260. loader_ctx->frame_offset --;
  5261. loader_ctx->dynamic_offset --;
  5262. }
  5263. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  5264. if (loader_ctx->p_code_compiled)
  5265. *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
  5266. loader_ctx->frame_offset -= 2;
  5267. loader_ctx->dynamic_offset -= 2;
  5268. }
  5269. else {
  5270. if (local_type == VALUE_TYPE_I32
  5271. || local_type == VALUE_TYPE_F32) {
  5272. emit_label(EXT_OP_SET_LOCAL_FAST);
  5273. emit_byte(loader_ctx, local_offset);
  5274. }
  5275. else {
  5276. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  5277. emit_byte(loader_ctx, local_offset);
  5278. }
  5279. POP_OFFSET_TYPE(local_type);
  5280. }
  5281. }
  5282. else { /* local index larger than 255, reserve leb */
  5283. p_org ++;
  5284. emit_leb();
  5285. POP_OFFSET_TYPE(local_type);
  5286. }
  5287. #else
  5288. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5289. if (local_offset < 0x80) {
  5290. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  5291. if (local_type == VALUE_TYPE_I32
  5292. || local_type == VALUE_TYPE_F32)
  5293. *p_org++ = (uint8)local_offset;
  5294. else
  5295. *p_org++ = (uint8)(local_offset | 0x80);
  5296. while (p_org < p)
  5297. *p_org++ = WASM_OP_NOP;
  5298. }
  5299. #endif
  5300. #endif
  5301. break;
  5302. }
  5303. case WASM_OP_TEE_LOCAL:
  5304. {
  5305. p_org = p - 1;
  5306. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5307. #if WASM_ENABLE_FAST_INTERP != 0
  5308. /* If the stack is in polymorphic state, do fake pop and push on
  5309. offset stack to keep the depth of offset stack to be the same
  5310. with ref stack */
  5311. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5312. if (cur_block->is_stack_polymorphic) {
  5313. POP_OFFSET_TYPE(local_type);
  5314. PUSH_OFFSET_TYPE(local_type);
  5315. }
  5316. #endif
  5317. POP_TYPE(local_type);
  5318. PUSH_TYPE(local_type);
  5319. #if WASM_ENABLE_FAST_INTERP != 0
  5320. if (!(preserve_referenced_local(loader_ctx, opcode, local_offset,
  5321. local_type, &preserve_local,
  5322. error_buf, error_buf_size)))
  5323. goto fail;
  5324. if (local_offset < 256) {
  5325. skip_label();
  5326. if (local_type == VALUE_TYPE_I32
  5327. || local_type == VALUE_TYPE_F32) {
  5328. emit_label(EXT_OP_TEE_LOCAL_FAST);
  5329. emit_byte(loader_ctx, local_offset);
  5330. }
  5331. else {
  5332. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  5333. emit_byte(loader_ctx, local_offset);
  5334. }
  5335. }
  5336. else { /* local index larger than 255, reserve leb */
  5337. p_org ++;
  5338. emit_leb();
  5339. }
  5340. emit_operand(loader_ctx, *(loader_ctx->frame_offset -
  5341. wasm_value_type_cell_num(local_type)));
  5342. #else
  5343. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5344. if (local_offset < 0x80) {
  5345. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  5346. if (local_type == VALUE_TYPE_I32
  5347. || local_type == VALUE_TYPE_F32)
  5348. *p_org++ = (uint8)local_offset;
  5349. else
  5350. *p_org++ = (uint8)(local_offset | 0x80);
  5351. while (p_org < p)
  5352. *p_org++ = WASM_OP_NOP;
  5353. }
  5354. #endif
  5355. #endif
  5356. break;
  5357. }
  5358. case WASM_OP_GET_GLOBAL:
  5359. {
  5360. p_org = p - 1;
  5361. read_leb_uint32(p, p_end, global_idx);
  5362. if (global_idx >= global_count) {
  5363. set_error_buf(error_buf, error_buf_size,
  5364. "unknown global");
  5365. goto fail;
  5366. }
  5367. global_type =
  5368. global_idx < module->import_global_count
  5369. ? module->import_globals[global_idx].u.global.type
  5370. : module->globals[global_idx - module->import_global_count]
  5371. .type;
  5372. PUSH_TYPE(global_type);
  5373. #if WASM_ENABLE_FAST_INTERP == 0
  5374. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5375. if (global_type == VALUE_TYPE_I64
  5376. || global_type == VALUE_TYPE_F64) {
  5377. *p_org = WASM_OP_GET_GLOBAL_64;
  5378. }
  5379. #endif
  5380. #else /* else of WASM_ENABLE_FAST_INTERP */
  5381. if (global_type == VALUE_TYPE_I64
  5382. || global_type == VALUE_TYPE_F64) {
  5383. skip_label();
  5384. emit_label(WASM_OP_GET_GLOBAL_64);
  5385. }
  5386. emit_uint32(loader_ctx, global_idx);
  5387. PUSH_OFFSET_TYPE(global_type);
  5388. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5389. break;
  5390. }
  5391. case WASM_OP_SET_GLOBAL:
  5392. {
  5393. bool is_mutable = false;
  5394. p_org = p - 1;
  5395. read_leb_uint32(p, p_end, global_idx);
  5396. if (global_idx >= global_count) {
  5397. set_error_buf(error_buf, error_buf_size,
  5398. "unknown global");
  5399. goto fail;
  5400. }
  5401. is_mutable =
  5402. global_idx < module->import_global_count
  5403. ? module->import_globals[global_idx].u.global.is_mutable
  5404. : module->globals[global_idx - module->import_global_count]
  5405. .is_mutable;
  5406. if (!is_mutable) {
  5407. set_error_buf(error_buf, error_buf_size,
  5408. "global is immutable");
  5409. goto fail;
  5410. }
  5411. global_type =
  5412. global_idx < module->import_global_count
  5413. ? module->import_globals[global_idx].u.global.type
  5414. : module->globals[global_idx - module->import_global_count]
  5415. .type;
  5416. POP_TYPE(global_type);
  5417. #if WASM_ENABLE_FAST_INTERP == 0
  5418. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  5419. if (global_type == VALUE_TYPE_I64
  5420. || global_type == VALUE_TYPE_F64) {
  5421. *p_org = WASM_OP_SET_GLOBAL_64;
  5422. }
  5423. else if (module->aux_stack_size > 0
  5424. && global_idx == module->aux_stack_top_global_index) {
  5425. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  5426. }
  5427. #endif
  5428. #else /* else of WASM_ENABLE_FAST_INTERP */
  5429. if (global_type == VALUE_TYPE_I64
  5430. || global_type == VALUE_TYPE_F64) {
  5431. skip_label();
  5432. emit_label(WASM_OP_SET_GLOBAL_64);
  5433. }
  5434. else if (module->aux_stack_size > 0
  5435. && global_idx == module->aux_stack_top_global_index) {
  5436. skip_label();
  5437. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  5438. }
  5439. emit_uint32(loader_ctx, global_idx);
  5440. POP_OFFSET_TYPE(global_type);
  5441. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5442. break;
  5443. }
  5444. /* load */
  5445. case WASM_OP_I32_LOAD:
  5446. case WASM_OP_I32_LOAD8_S:
  5447. case WASM_OP_I32_LOAD8_U:
  5448. case WASM_OP_I32_LOAD16_S:
  5449. case WASM_OP_I32_LOAD16_U:
  5450. case WASM_OP_I64_LOAD:
  5451. case WASM_OP_I64_LOAD8_S:
  5452. case WASM_OP_I64_LOAD8_U:
  5453. case WASM_OP_I64_LOAD16_S:
  5454. case WASM_OP_I64_LOAD16_U:
  5455. case WASM_OP_I64_LOAD32_S:
  5456. case WASM_OP_I64_LOAD32_U:
  5457. case WASM_OP_F32_LOAD:
  5458. case WASM_OP_F64_LOAD:
  5459. /* store */
  5460. case WASM_OP_I32_STORE:
  5461. case WASM_OP_I32_STORE8:
  5462. case WASM_OP_I32_STORE16:
  5463. case WASM_OP_I64_STORE:
  5464. case WASM_OP_I64_STORE8:
  5465. case WASM_OP_I64_STORE16:
  5466. case WASM_OP_I64_STORE32:
  5467. case WASM_OP_F32_STORE:
  5468. case WASM_OP_F64_STORE:
  5469. {
  5470. #if WASM_ENABLE_FAST_INTERP != 0
  5471. /* change F32/F64 into I32/I64 */
  5472. if (opcode == WASM_OP_F32_LOAD) {
  5473. skip_label();
  5474. emit_label(WASM_OP_I32_LOAD);
  5475. }
  5476. else if (opcode == WASM_OP_F64_LOAD) {
  5477. skip_label();
  5478. emit_label(WASM_OP_I64_LOAD);
  5479. }
  5480. else if (opcode == WASM_OP_F32_STORE) {
  5481. skip_label();
  5482. emit_label(WASM_OP_I32_STORE);
  5483. }
  5484. else if (opcode == WASM_OP_F64_STORE) {
  5485. skip_label();
  5486. emit_label(WASM_OP_I64_STORE);
  5487. }
  5488. #endif
  5489. CHECK_MEMORY();
  5490. read_leb_uint32(p, p_end, align); /* align */
  5491. read_leb_uint32(p, p_end, mem_offset); /* offset */
  5492. if (!check_memory_access_align(opcode, align,
  5493. error_buf, error_buf_size)) {
  5494. goto fail;
  5495. }
  5496. #if WASM_ENABLE_FAST_INTERP != 0
  5497. emit_uint32(loader_ctx, mem_offset);
  5498. #endif
  5499. switch (opcode)
  5500. {
  5501. /* load */
  5502. case WASM_OP_I32_LOAD:
  5503. case WASM_OP_I32_LOAD8_S:
  5504. case WASM_OP_I32_LOAD8_U:
  5505. case WASM_OP_I32_LOAD16_S:
  5506. case WASM_OP_I32_LOAD16_U:
  5507. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5508. break;
  5509. case WASM_OP_I64_LOAD:
  5510. case WASM_OP_I64_LOAD8_S:
  5511. case WASM_OP_I64_LOAD8_U:
  5512. case WASM_OP_I64_LOAD16_S:
  5513. case WASM_OP_I64_LOAD16_U:
  5514. case WASM_OP_I64_LOAD32_S:
  5515. case WASM_OP_I64_LOAD32_U:
  5516. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  5517. break;
  5518. case WASM_OP_F32_LOAD:
  5519. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  5520. break;
  5521. case WASM_OP_F64_LOAD:
  5522. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  5523. break;
  5524. /* store */
  5525. case WASM_OP_I32_STORE:
  5526. case WASM_OP_I32_STORE8:
  5527. case WASM_OP_I32_STORE16:
  5528. POP_I32();
  5529. POP_I32();
  5530. break;
  5531. case WASM_OP_I64_STORE:
  5532. case WASM_OP_I64_STORE8:
  5533. case WASM_OP_I64_STORE16:
  5534. case WASM_OP_I64_STORE32:
  5535. POP_I64();
  5536. POP_I32();
  5537. break;
  5538. case WASM_OP_F32_STORE:
  5539. POP_F32();
  5540. POP_I32();
  5541. break;
  5542. case WASM_OP_F64_STORE:
  5543. POP_F64();
  5544. POP_I32();
  5545. break;
  5546. default:
  5547. break;
  5548. }
  5549. break;
  5550. }
  5551. case WASM_OP_MEMORY_SIZE:
  5552. CHECK_MEMORY();
  5553. /* reserved byte 0x00 */
  5554. if (*p++ != 0x00) {
  5555. set_error_buf(error_buf, error_buf_size,
  5556. "zero flag expected");
  5557. goto fail;
  5558. }
  5559. PUSH_I32();
  5560. module->possible_memory_grow = true;
  5561. break;
  5562. case WASM_OP_MEMORY_GROW:
  5563. CHECK_MEMORY();
  5564. /* reserved byte 0x00 */
  5565. if (*p++ != 0x00) {
  5566. set_error_buf(error_buf, error_buf_size,
  5567. "zero flag expected");
  5568. goto fail;
  5569. }
  5570. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5571. func->has_op_memory_grow = true;
  5572. module->possible_memory_grow = true;
  5573. break;
  5574. case WASM_OP_I32_CONST:
  5575. read_leb_int32(p, p_end, i32_const);
  5576. #if WASM_ENABLE_FAST_INTERP != 0
  5577. skip_label();
  5578. disable_emit = true;
  5579. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  5580. #else
  5581. (void)i32_const;
  5582. #endif
  5583. PUSH_I32();
  5584. break;
  5585. case WASM_OP_I64_CONST:
  5586. read_leb_int64(p, p_end, i64);
  5587. #if WASM_ENABLE_FAST_INTERP != 0
  5588. skip_label();
  5589. disable_emit = true;
  5590. GET_CONST_OFFSET(VALUE_TYPE_I64, i64);
  5591. #endif
  5592. PUSH_I64();
  5593. break;
  5594. case WASM_OP_F32_CONST:
  5595. p += sizeof(float32);
  5596. #if WASM_ENABLE_FAST_INTERP != 0
  5597. skip_label();
  5598. disable_emit = true;
  5599. bh_memcpy_s((uint8*)&f32, sizeof(float32), p_org, sizeof(float32));
  5600. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32);
  5601. #endif
  5602. PUSH_F32();
  5603. break;
  5604. case WASM_OP_F64_CONST:
  5605. p += sizeof(float64);
  5606. #if WASM_ENABLE_FAST_INTERP != 0
  5607. skip_label();
  5608. disable_emit = true;
  5609. /* Some MCU may require 8-byte align */
  5610. bh_memcpy_s((uint8*)&f64, sizeof(float64), p_org, sizeof(float64));
  5611. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64);
  5612. #endif
  5613. PUSH_F64();
  5614. break;
  5615. case WASM_OP_I32_EQZ:
  5616. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5617. break;
  5618. case WASM_OP_I32_EQ:
  5619. case WASM_OP_I32_NE:
  5620. case WASM_OP_I32_LT_S:
  5621. case WASM_OP_I32_LT_U:
  5622. case WASM_OP_I32_GT_S:
  5623. case WASM_OP_I32_GT_U:
  5624. case WASM_OP_I32_LE_S:
  5625. case WASM_OP_I32_LE_U:
  5626. case WASM_OP_I32_GE_S:
  5627. case WASM_OP_I32_GE_U:
  5628. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5629. break;
  5630. case WASM_OP_I64_EQZ:
  5631. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  5632. break;
  5633. case WASM_OP_I64_EQ:
  5634. case WASM_OP_I64_NE:
  5635. case WASM_OP_I64_LT_S:
  5636. case WASM_OP_I64_LT_U:
  5637. case WASM_OP_I64_GT_S:
  5638. case WASM_OP_I64_GT_U:
  5639. case WASM_OP_I64_LE_S:
  5640. case WASM_OP_I64_LE_U:
  5641. case WASM_OP_I64_GE_S:
  5642. case WASM_OP_I64_GE_U:
  5643. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  5644. break;
  5645. case WASM_OP_F32_EQ:
  5646. case WASM_OP_F32_NE:
  5647. case WASM_OP_F32_LT:
  5648. case WASM_OP_F32_GT:
  5649. case WASM_OP_F32_LE:
  5650. case WASM_OP_F32_GE:
  5651. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5652. break;
  5653. case WASM_OP_F64_EQ:
  5654. case WASM_OP_F64_NE:
  5655. case WASM_OP_F64_LT:
  5656. case WASM_OP_F64_GT:
  5657. case WASM_OP_F64_LE:
  5658. case WASM_OP_F64_GE:
  5659. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  5660. break;
  5661. case WASM_OP_I32_CLZ:
  5662. case WASM_OP_I32_CTZ:
  5663. case WASM_OP_I32_POPCNT:
  5664. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5665. break;
  5666. case WASM_OP_I32_ADD:
  5667. case WASM_OP_I32_SUB:
  5668. case WASM_OP_I32_MUL:
  5669. case WASM_OP_I32_DIV_S:
  5670. case WASM_OP_I32_DIV_U:
  5671. case WASM_OP_I32_REM_S:
  5672. case WASM_OP_I32_REM_U:
  5673. case WASM_OP_I32_AND:
  5674. case WASM_OP_I32_OR:
  5675. case WASM_OP_I32_XOR:
  5676. case WASM_OP_I32_SHL:
  5677. case WASM_OP_I32_SHR_S:
  5678. case WASM_OP_I32_SHR_U:
  5679. case WASM_OP_I32_ROTL:
  5680. case WASM_OP_I32_ROTR:
  5681. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5682. break;
  5683. case WASM_OP_I64_CLZ:
  5684. case WASM_OP_I64_CTZ:
  5685. case WASM_OP_I64_POPCNT:
  5686. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  5687. break;
  5688. case WASM_OP_I64_ADD:
  5689. case WASM_OP_I64_SUB:
  5690. case WASM_OP_I64_MUL:
  5691. case WASM_OP_I64_DIV_S:
  5692. case WASM_OP_I64_DIV_U:
  5693. case WASM_OP_I64_REM_S:
  5694. case WASM_OP_I64_REM_U:
  5695. case WASM_OP_I64_AND:
  5696. case WASM_OP_I64_OR:
  5697. case WASM_OP_I64_XOR:
  5698. case WASM_OP_I64_SHL:
  5699. case WASM_OP_I64_SHR_S:
  5700. case WASM_OP_I64_SHR_U:
  5701. case WASM_OP_I64_ROTL:
  5702. case WASM_OP_I64_ROTR:
  5703. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  5704. break;
  5705. case WASM_OP_F32_ABS:
  5706. case WASM_OP_F32_NEG:
  5707. case WASM_OP_F32_CEIL:
  5708. case WASM_OP_F32_FLOOR:
  5709. case WASM_OP_F32_TRUNC:
  5710. case WASM_OP_F32_NEAREST:
  5711. case WASM_OP_F32_SQRT:
  5712. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  5713. break;
  5714. case WASM_OP_F32_ADD:
  5715. case WASM_OP_F32_SUB:
  5716. case WASM_OP_F32_MUL:
  5717. case WASM_OP_F32_DIV:
  5718. case WASM_OP_F32_MIN:
  5719. case WASM_OP_F32_MAX:
  5720. case WASM_OP_F32_COPYSIGN:
  5721. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  5722. break;
  5723. case WASM_OP_F64_ABS:
  5724. case WASM_OP_F64_NEG:
  5725. case WASM_OP_F64_CEIL:
  5726. case WASM_OP_F64_FLOOR:
  5727. case WASM_OP_F64_TRUNC:
  5728. case WASM_OP_F64_NEAREST:
  5729. case WASM_OP_F64_SQRT:
  5730. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  5731. break;
  5732. case WASM_OP_F64_ADD:
  5733. case WASM_OP_F64_SUB:
  5734. case WASM_OP_F64_MUL:
  5735. case WASM_OP_F64_DIV:
  5736. case WASM_OP_F64_MIN:
  5737. case WASM_OP_F64_MAX:
  5738. case WASM_OP_F64_COPYSIGN:
  5739. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  5740. break;
  5741. case WASM_OP_I32_WRAP_I64:
  5742. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  5743. break;
  5744. case WASM_OP_I32_TRUNC_S_F32:
  5745. case WASM_OP_I32_TRUNC_U_F32:
  5746. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5747. break;
  5748. case WASM_OP_I32_TRUNC_S_F64:
  5749. case WASM_OP_I32_TRUNC_U_F64:
  5750. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  5751. break;
  5752. case WASM_OP_I64_EXTEND_S_I32:
  5753. case WASM_OP_I64_EXTEND_U_I32:
  5754. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  5755. break;
  5756. case WASM_OP_I64_TRUNC_S_F32:
  5757. case WASM_OP_I64_TRUNC_U_F32:
  5758. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  5759. break;
  5760. case WASM_OP_I64_TRUNC_S_F64:
  5761. case WASM_OP_I64_TRUNC_U_F64:
  5762. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  5763. break;
  5764. case WASM_OP_F32_CONVERT_S_I32:
  5765. case WASM_OP_F32_CONVERT_U_I32:
  5766. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  5767. break;
  5768. case WASM_OP_F32_CONVERT_S_I64:
  5769. case WASM_OP_F32_CONVERT_U_I64:
  5770. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  5771. break;
  5772. case WASM_OP_F32_DEMOTE_F64:
  5773. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  5774. break;
  5775. case WASM_OP_F64_CONVERT_S_I32:
  5776. case WASM_OP_F64_CONVERT_U_I32:
  5777. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  5778. break;
  5779. case WASM_OP_F64_CONVERT_S_I64:
  5780. case WASM_OP_F64_CONVERT_U_I64:
  5781. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  5782. break;
  5783. case WASM_OP_F64_PROMOTE_F32:
  5784. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  5785. break;
  5786. case WASM_OP_I32_REINTERPRET_F32:
  5787. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5788. break;
  5789. case WASM_OP_I64_REINTERPRET_F64:
  5790. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  5791. break;
  5792. case WASM_OP_F32_REINTERPRET_I32:
  5793. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  5794. break;
  5795. case WASM_OP_F64_REINTERPRET_I64:
  5796. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  5797. break;
  5798. case WASM_OP_I32_EXTEND8_S:
  5799. case WASM_OP_I32_EXTEND16_S:
  5800. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5801. break;
  5802. case WASM_OP_I64_EXTEND8_S:
  5803. case WASM_OP_I64_EXTEND16_S:
  5804. case WASM_OP_I64_EXTEND32_S:
  5805. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  5806. break;
  5807. case WASM_OP_MISC_PREFIX:
  5808. {
  5809. uint32 opcode1;
  5810. read_leb_uint32(p, p_end, opcode1);
  5811. #if WASM_ENABLE_FAST_INTERP != 0
  5812. emit_byte(loader_ctx, ((uint8)opcode1));
  5813. #endif
  5814. switch (opcode1)
  5815. {
  5816. case WASM_OP_I32_TRUNC_SAT_S_F32:
  5817. case WASM_OP_I32_TRUNC_SAT_U_F32:
  5818. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  5819. break;
  5820. case WASM_OP_I32_TRUNC_SAT_S_F64:
  5821. case WASM_OP_I32_TRUNC_SAT_U_F64:
  5822. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  5823. break;
  5824. case WASM_OP_I64_TRUNC_SAT_S_F32:
  5825. case WASM_OP_I64_TRUNC_SAT_U_F32:
  5826. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  5827. break;
  5828. case WASM_OP_I64_TRUNC_SAT_S_F64:
  5829. case WASM_OP_I64_TRUNC_SAT_U_F64:
  5830. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  5831. break;
  5832. #if WASM_ENABLE_BULK_MEMORY != 0
  5833. case WASM_OP_MEMORY_INIT:
  5834. read_leb_uint32(p, p_end, segment_index);
  5835. #if WASM_ENABLE_FAST_INTERP != 0
  5836. emit_uint32(loader_ctx, segment_index);
  5837. #endif
  5838. if (module->import_memory_count == 0 && module->memory_count == 0)
  5839. goto fail_unknown_memory;
  5840. if (*p++ != 0x00)
  5841. goto fail_zero_flag_expected;
  5842. if (segment_index >= module->data_seg_count) {
  5843. set_error_buf(error_buf, error_buf_size,
  5844. "unknown data segment");
  5845. goto fail;
  5846. }
  5847. if (module->data_seg_count1 == 0)
  5848. goto fail_data_cnt_sec_require;
  5849. POP_I32();
  5850. POP_I32();
  5851. POP_I32();
  5852. break;
  5853. case WASM_OP_DATA_DROP:
  5854. read_leb_uint32(p, p_end, segment_index);
  5855. #if WASM_ENABLE_FAST_INTERP != 0
  5856. emit_uint32(loader_ctx, segment_index);
  5857. #endif
  5858. if (segment_index >= module->data_seg_count) {
  5859. set_error_buf(error_buf, error_buf_size,
  5860. "unknown data segment");
  5861. goto fail;
  5862. }
  5863. if (module->data_seg_count1 == 0)
  5864. goto fail_data_cnt_sec_require;
  5865. break;
  5866. case WASM_OP_MEMORY_COPY:
  5867. /* both src and dst memory index should be 0 */
  5868. if (*(int16*)p != 0x0000)
  5869. goto fail_zero_flag_expected;
  5870. p += 2;
  5871. if (module->import_memory_count == 0 && module->memory_count == 0)
  5872. goto fail_unknown_memory;
  5873. POP_I32();
  5874. POP_I32();
  5875. POP_I32();
  5876. break;
  5877. case WASM_OP_MEMORY_FILL:
  5878. if (*p++ != 0x00) {
  5879. goto fail_zero_flag_expected;
  5880. }
  5881. if (module->import_memory_count == 0 && module->memory_count == 0) {
  5882. goto fail_unknown_memory;
  5883. }
  5884. POP_I32();
  5885. POP_I32();
  5886. POP_I32();
  5887. break;
  5888. fail_zero_flag_expected:
  5889. set_error_buf(error_buf, error_buf_size,
  5890. "zero flag expected");
  5891. goto fail;
  5892. fail_unknown_memory:
  5893. set_error_buf(error_buf, error_buf_size,
  5894. "unknown memory 0");
  5895. goto fail;
  5896. fail_data_cnt_sec_require:
  5897. set_error_buf(error_buf, error_buf_size,
  5898. "data count section required");
  5899. goto fail;
  5900. /* TODO: to support bulk table operation */
  5901. #endif /* WASM_ENABLE_BULK_MEMORY */
  5902. default:
  5903. set_error_buf_v(error_buf, error_buf_size,
  5904. "%s %02x %02x",
  5905. "unsupported opcode", 0xfc, opcode1);
  5906. goto fail;
  5907. }
  5908. break;
  5909. }
  5910. #if WASM_ENABLE_SHARED_MEMORY != 0
  5911. case WASM_OP_ATOMIC_PREFIX:
  5912. {
  5913. opcode = read_uint8(p);
  5914. #if WASM_ENABLE_FAST_INTERP != 0
  5915. emit_byte(loader_ctx, opcode);
  5916. #endif
  5917. if (opcode != WASM_OP_ATOMIC_FENCE) {
  5918. CHECK_MEMORY();
  5919. read_leb_uint32(p, p_end, align); /* align */
  5920. read_leb_uint32(p, p_end, mem_offset); /* offset */
  5921. if (!check_memory_align_equal(opcode, align,
  5922. error_buf,
  5923. error_buf_size)) {
  5924. goto fail;
  5925. }
  5926. #if WASM_ENABLE_FAST_INTERP != 0
  5927. emit_uint32(loader_ctx, mem_offset);
  5928. #endif
  5929. }
  5930. switch (opcode) {
  5931. case WASM_OP_ATOMIC_NOTIFY:
  5932. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5933. break;
  5934. case WASM_OP_ATOMIC_WAIT32:
  5935. POP_I64();
  5936. POP_I32();
  5937. POP_I32();
  5938. PUSH_I32();
  5939. break;
  5940. case WASM_OP_ATOMIC_WAIT64:
  5941. POP_I64();
  5942. POP_I64();
  5943. POP_I32();
  5944. PUSH_I32();
  5945. break;
  5946. case WASM_OP_ATOMIC_FENCE:
  5947. /* reserved byte 0x00 */
  5948. if (*p++ != 0x00) {
  5949. set_error_buf(error_buf, error_buf_size,
  5950. "zero flag expected");
  5951. goto fail;
  5952. }
  5953. break;
  5954. case WASM_OP_ATOMIC_I32_LOAD:
  5955. case WASM_OP_ATOMIC_I32_LOAD8_U:
  5956. case WASM_OP_ATOMIC_I32_LOAD16_U:
  5957. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5958. break;
  5959. case WASM_OP_ATOMIC_I32_STORE:
  5960. case WASM_OP_ATOMIC_I32_STORE8:
  5961. case WASM_OP_ATOMIC_I32_STORE16:
  5962. POP_I32();
  5963. POP_I32();
  5964. break;
  5965. case WASM_OP_ATOMIC_I64_LOAD:
  5966. case WASM_OP_ATOMIC_I64_LOAD8_U:
  5967. case WASM_OP_ATOMIC_I64_LOAD16_U:
  5968. case WASM_OP_ATOMIC_I64_LOAD32_U:
  5969. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  5970. break;
  5971. case WASM_OP_ATOMIC_I64_STORE:
  5972. case WASM_OP_ATOMIC_I64_STORE8:
  5973. case WASM_OP_ATOMIC_I64_STORE16:
  5974. case WASM_OP_ATOMIC_I64_STORE32:
  5975. POP_I64();
  5976. POP_I32();
  5977. break;
  5978. case WASM_OP_ATOMIC_RMW_I32_ADD:
  5979. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  5980. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  5981. case WASM_OP_ATOMIC_RMW_I32_SUB:
  5982. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  5983. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  5984. case WASM_OP_ATOMIC_RMW_I32_AND:
  5985. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  5986. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  5987. case WASM_OP_ATOMIC_RMW_I32_OR:
  5988. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  5989. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  5990. case WASM_OP_ATOMIC_RMW_I32_XOR:
  5991. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  5992. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  5993. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  5994. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  5995. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  5996. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5997. break;
  5998. case WASM_OP_ATOMIC_RMW_I64_ADD:
  5999. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  6000. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  6001. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  6002. case WASM_OP_ATOMIC_RMW_I64_SUB:
  6003. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  6004. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  6005. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  6006. case WASM_OP_ATOMIC_RMW_I64_AND:
  6007. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  6008. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  6009. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  6010. case WASM_OP_ATOMIC_RMW_I64_OR:
  6011. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  6012. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  6013. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  6014. case WASM_OP_ATOMIC_RMW_I64_XOR:
  6015. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  6016. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  6017. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  6018. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  6019. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  6020. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  6021. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  6022. POP_I64();
  6023. POP_I32();
  6024. PUSH_I64();
  6025. break;
  6026. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  6027. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  6028. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  6029. POP_I32();
  6030. POP_I32();
  6031. POP_I32();
  6032. PUSH_I32();
  6033. break;
  6034. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  6035. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  6036. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  6037. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  6038. POP_I64();
  6039. POP_I64();
  6040. POP_I32();
  6041. PUSH_I64();
  6042. break;
  6043. default:
  6044. set_error_buf_v(error_buf, error_buf_size,
  6045. "%s %02x %02x",
  6046. "unsupported opcode", 0xfe, opcode);
  6047. goto fail;
  6048. }
  6049. break;
  6050. }
  6051. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  6052. default:
  6053. set_error_buf_v(error_buf, error_buf_size,
  6054. "%s %02x",
  6055. "unsupported opcode", opcode);
  6056. goto fail;
  6057. }
  6058. #if WASM_ENABLE_FAST_INTERP != 0
  6059. last_op = opcode;
  6060. #endif
  6061. }
  6062. if (loader_ctx->csp_num > 0) {
  6063. set_error_buf(error_buf, error_buf_size,
  6064. "function body must end with END opcode");
  6065. goto fail;
  6066. }
  6067. #if WASM_ENABLE_FAST_INTERP != 0
  6068. if (loader_ctx->p_code_compiled == NULL)
  6069. goto re_scan;
  6070. func->const_cell_num = loader_ctx->const_cell_num;
  6071. if (!(func->consts = func_const =
  6072. loader_malloc(func->const_cell_num * 4,
  6073. error_buf, error_buf_size))) {
  6074. goto fail;
  6075. }
  6076. func_const_end = func->consts + func->const_cell_num * 4;
  6077. /* reverse the const buf */
  6078. for (int i = loader_ctx->num_const - 1; i >= 0; i--) {
  6079. Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const));
  6080. if (c->value_type == VALUE_TYPE_F64
  6081. || c->value_type == VALUE_TYPE_I64) {
  6082. bh_memcpy_s(func_const, func_const_end - func_const,
  6083. &(c->value.f64), sizeof(int64));
  6084. func_const += sizeof(int64);
  6085. } else {
  6086. bh_memcpy_s(func_const, func_const_end - func_const,
  6087. &(c->value.f32), sizeof(int32));
  6088. func_const += sizeof(int32);
  6089. }
  6090. }
  6091. func->max_stack_cell_num = loader_ctx->preserved_local_offset -
  6092. loader_ctx->start_dynamic_offset + 1;
  6093. #else
  6094. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  6095. #endif
  6096. func->max_block_num = loader_ctx->max_csp_num;
  6097. return_value = true;
  6098. fail:
  6099. wasm_loader_ctx_destroy(loader_ctx);
  6100. (void)u8;
  6101. (void)u32;
  6102. (void)i32;
  6103. (void)i64;
  6104. (void)local_offset;
  6105. (void)p_org;
  6106. (void)mem_offset;
  6107. (void)align;
  6108. return return_value;
  6109. }