wasm_mini_loader.c 302 KB

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