wasm_mini_loader.c 269 KB

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