wasm_mini_loader.c 296 KB

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