wasm_loader.c 360 KB

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