wasm_mini_loader.c 280 KB

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