wasm_loader.c 388 KB

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