wasm_mini_loader.c 292 KB

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