wasm_mini_loader.c 270 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545
  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. LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  1598. AOTCompOption option = { 0 };
  1599. char *aot_last_error;
  1600. uint64 size;
  1601. if (module->function_count == 0)
  1602. return true;
  1603. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1604. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  1605. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  1606. return false;
  1607. }
  1608. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  1609. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  1610. os_mutex_destroy(&module->tierup_wait_lock);
  1611. return false;
  1612. }
  1613. module->tierup_wait_lock_inited = true;
  1614. #endif
  1615. size = sizeof(void *) * (uint64)module->function_count
  1616. + sizeof(bool) * (uint64)module->function_count;
  1617. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  1618. return false;
  1619. }
  1620. module->func_ptrs_compiled =
  1621. (bool *)((uint8 *)module->func_ptrs
  1622. + sizeof(void *) * module->function_count);
  1623. module->comp_data = aot_create_comp_data(module);
  1624. if (!module->comp_data) {
  1625. aot_last_error = aot_get_last_error();
  1626. bh_assert(aot_last_error != NULL);
  1627. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1628. return false;
  1629. }
  1630. option.is_jit_mode = true;
  1631. option.opt_level = llvm_jit_options.opt_level;
  1632. option.size_level = llvm_jit_options.size_level;
  1633. option.segue_flags = llvm_jit_options.segue_flags;
  1634. #if WASM_ENABLE_BULK_MEMORY != 0
  1635. option.enable_bulk_memory = true;
  1636. #endif
  1637. #if WASM_ENABLE_THREAD_MGR != 0
  1638. option.enable_thread_mgr = true;
  1639. #endif
  1640. #if WASM_ENABLE_TAIL_CALL != 0
  1641. option.enable_tail_call = true;
  1642. #endif
  1643. #if WASM_ENABLE_SIMD != 0
  1644. option.enable_simd = true;
  1645. #endif
  1646. #if WASM_ENABLE_REF_TYPES != 0
  1647. option.enable_ref_types = true;
  1648. #endif
  1649. option.enable_aux_stack_check = true;
  1650. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
  1651. option.enable_aux_stack_frame = true;
  1652. #endif
  1653. #if WASM_ENABLE_MEMORY_PROFILING != 0
  1654. option.enable_stack_estimation = true;
  1655. #endif
  1656. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  1657. if (!module->comp_ctx) {
  1658. aot_last_error = aot_get_last_error();
  1659. bh_assert(aot_last_error != NULL);
  1660. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1661. return false;
  1662. }
  1663. return true;
  1664. }
  1665. static bool
  1666. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  1667. uint32 error_buf_size)
  1668. {
  1669. char *aot_last_error;
  1670. uint32 i;
  1671. if (module->function_count == 0)
  1672. return true;
  1673. if (!aot_compile_wasm(module->comp_ctx)) {
  1674. aot_last_error = aot_get_last_error();
  1675. bh_assert(aot_last_error != NULL);
  1676. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1677. return false;
  1678. }
  1679. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1680. if (module->orcjit_stop_compiling)
  1681. return false;
  1682. #endif
  1683. bh_print_time("Begin to lookup llvm jit functions");
  1684. for (i = 0; i < module->function_count; i++) {
  1685. LLVMOrcJITTargetAddress func_addr = 0;
  1686. LLVMErrorRef error;
  1687. char func_name[48];
  1688. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  1689. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  1690. func_name);
  1691. if (error != LLVMErrorSuccess) {
  1692. char *err_msg = LLVMGetErrorMessage(error);
  1693. char buf[96];
  1694. snprintf(buf, sizeof(buf),
  1695. "failed to compile llvm jit function: %s", err_msg);
  1696. set_error_buf(error_buf, error_buf_size, buf);
  1697. LLVMDisposeErrorMessage(err_msg);
  1698. return false;
  1699. }
  1700. /**
  1701. * No need to lock the func_ptr[func_idx] here as it is basic
  1702. * data type, the load/store for it can be finished by one cpu
  1703. * instruction, and there can be only one cpu instruction
  1704. * loading/storing at the same time.
  1705. */
  1706. module->func_ptrs[i] = (void *)func_addr;
  1707. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1708. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  1709. if (module->orcjit_stop_compiling)
  1710. return false;
  1711. #endif
  1712. }
  1713. bh_print_time("End lookup llvm jit functions");
  1714. return true;
  1715. }
  1716. #endif /* end of WASM_ENABLE_JIT != 0 */
  1717. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1718. && WASM_ENABLE_LAZY_JIT != 0
  1719. static void *
  1720. init_llvm_jit_functions_stage2_callback(void *arg)
  1721. {
  1722. WASMModule *module = (WASMModule *)arg;
  1723. char error_buf[128];
  1724. uint32 error_buf_size = (uint32)sizeof(error_buf);
  1725. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  1726. module->orcjit_stop_compiling = true;
  1727. return NULL;
  1728. }
  1729. os_mutex_lock(&module->tierup_wait_lock);
  1730. module->llvm_jit_inited = true;
  1731. os_cond_broadcast(&module->tierup_wait_cond);
  1732. os_mutex_unlock(&module->tierup_wait_lock);
  1733. return NULL;
  1734. }
  1735. #endif
  1736. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1737. /* The callback function to compile jit functions */
  1738. static void *
  1739. orcjit_thread_callback(void *arg)
  1740. {
  1741. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  1742. #if WASM_ENABLE_JIT != 0
  1743. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  1744. #endif
  1745. WASMModule *module = thread_arg->module;
  1746. uint32 group_idx = thread_arg->group_idx;
  1747. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  1748. uint32 func_count = module->function_count;
  1749. uint32 i;
  1750. #if WASM_ENABLE_FAST_JIT != 0
  1751. /* Compile fast jit funcitons of this group */
  1752. for (i = group_idx; i < func_count; i += group_stride) {
  1753. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  1754. os_printf("failed to compile fast jit function %u\n", i);
  1755. break;
  1756. }
  1757. if (module->orcjit_stop_compiling) {
  1758. return NULL;
  1759. }
  1760. }
  1761. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1762. os_mutex_lock(&module->tierup_wait_lock);
  1763. module->fast_jit_ready_groups++;
  1764. os_mutex_unlock(&module->tierup_wait_lock);
  1765. #endif
  1766. #endif
  1767. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1768. && WASM_ENABLE_LAZY_JIT != 0
  1769. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  1770. for (i = group_idx; i < func_count;
  1771. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  1772. uint32 j;
  1773. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  1774. if (i + j * group_stride < func_count) {
  1775. if (!jit_compiler_set_call_to_fast_jit(
  1776. module,
  1777. i + j * group_stride + module->import_function_count)) {
  1778. os_printf(
  1779. "failed to compile call_to_fast_jit for func %u\n",
  1780. i + j * group_stride + module->import_function_count);
  1781. module->orcjit_stop_compiling = true;
  1782. return NULL;
  1783. }
  1784. }
  1785. if (module->orcjit_stop_compiling) {
  1786. return NULL;
  1787. }
  1788. }
  1789. }
  1790. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  1791. fast jit functions are compiled */
  1792. os_mutex_lock(&module->tierup_wait_lock);
  1793. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  1794. && module->fast_jit_ready_groups >= group_stride)) {
  1795. os_cond_reltimedwait(&module->tierup_wait_cond,
  1796. &module->tierup_wait_lock, 10000);
  1797. if (module->orcjit_stop_compiling) {
  1798. /* init_llvm_jit_functions_stage2 failed */
  1799. os_mutex_unlock(&module->tierup_wait_lock);
  1800. return NULL;
  1801. }
  1802. }
  1803. os_mutex_unlock(&module->tierup_wait_lock);
  1804. #endif
  1805. #if WASM_ENABLE_JIT != 0
  1806. /* Compile llvm jit functions of this group */
  1807. for (i = group_idx; i < func_count;
  1808. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  1809. LLVMOrcJITTargetAddress func_addr = 0;
  1810. LLVMErrorRef error;
  1811. char func_name[48];
  1812. typedef void (*F)(void);
  1813. union {
  1814. F f;
  1815. void *v;
  1816. } u;
  1817. uint32 j;
  1818. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  1819. "_wrapper");
  1820. LOG_DEBUG("compile llvm jit func %s", func_name);
  1821. error =
  1822. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  1823. if (error != LLVMErrorSuccess) {
  1824. char *err_msg = LLVMGetErrorMessage(error);
  1825. os_printf("failed to compile llvm jit function %u: %s", i, err_msg);
  1826. LLVMDisposeErrorMessage(err_msg);
  1827. break;
  1828. }
  1829. /* Call the jit wrapper function to trigger its compilation, so as
  1830. to compile the actual jit functions, since we add the latter to
  1831. function list in the PartitionFunction callback */
  1832. u.v = (void *)func_addr;
  1833. u.f();
  1834. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  1835. if (i + j * group_stride < func_count) {
  1836. module->func_ptrs_compiled[i + j * group_stride] = true;
  1837. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1838. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  1839. i + j * group_stride);
  1840. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  1841. func_name);
  1842. if (error != LLVMErrorSuccess) {
  1843. char *err_msg = LLVMGetErrorMessage(error);
  1844. os_printf("failed to compile llvm jit function %u: %s", i,
  1845. err_msg);
  1846. LLVMDisposeErrorMessage(err_msg);
  1847. /* Ignore current llvm jit func, as its func ptr is
  1848. previous set to call_to_fast_jit, which also works */
  1849. continue;
  1850. }
  1851. jit_compiler_set_llvm_jit_func_ptr(
  1852. module,
  1853. i + j * group_stride + module->import_function_count,
  1854. (void *)func_addr);
  1855. /* Try to switch to call this llvm jit funtion instead of
  1856. fast jit function from fast jit jitted code */
  1857. jit_compiler_set_call_to_llvm_jit(
  1858. module,
  1859. i + j * group_stride + module->import_function_count);
  1860. #endif
  1861. }
  1862. }
  1863. if (module->orcjit_stop_compiling) {
  1864. break;
  1865. }
  1866. }
  1867. #endif
  1868. return NULL;
  1869. }
  1870. static void
  1871. orcjit_stop_compile_threads(WASMModule *module)
  1872. {
  1873. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  1874. / sizeof(OrcJitThreadArg));
  1875. module->orcjit_stop_compiling = true;
  1876. for (i = 0; i < thread_num; i++) {
  1877. if (module->orcjit_threads[i])
  1878. os_thread_join(module->orcjit_threads[i], NULL);
  1879. }
  1880. }
  1881. static bool
  1882. compile_jit_functions(WASMModule *module, char *error_buf,
  1883. uint32 error_buf_size)
  1884. {
  1885. uint32 thread_num =
  1886. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  1887. uint32 i, j;
  1888. bh_print_time("Begin to compile jit functions");
  1889. /* Create threads to compile the jit functions */
  1890. for (i = 0; i < thread_num && i < module->function_count; i++) {
  1891. #if WASM_ENABLE_JIT != 0
  1892. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  1893. #endif
  1894. module->orcjit_thread_args[i].module = module;
  1895. module->orcjit_thread_args[i].group_idx = i;
  1896. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  1897. (void *)&module->orcjit_thread_args[i],
  1898. APP_THREAD_STACK_SIZE_DEFAULT)
  1899. != 0) {
  1900. set_error_buf(error_buf, error_buf_size,
  1901. "create orcjit compile thread failed");
  1902. /* Terminate the threads created */
  1903. module->orcjit_stop_compiling = true;
  1904. for (j = 0; j < i; j++) {
  1905. os_thread_join(module->orcjit_threads[j], NULL);
  1906. }
  1907. return false;
  1908. }
  1909. }
  1910. #if WASM_ENABLE_LAZY_JIT == 0
  1911. /* Wait until all jit functions are compiled for eager mode */
  1912. for (i = 0; i < thread_num; i++) {
  1913. if (module->orcjit_threads[i])
  1914. os_thread_join(module->orcjit_threads[i], NULL);
  1915. }
  1916. #if WASM_ENABLE_FAST_JIT != 0
  1917. /* Ensure all the fast-jit functions are compiled */
  1918. for (i = 0; i < module->function_count; i++) {
  1919. if (!jit_compiler_is_compiled(module,
  1920. i + module->import_function_count)) {
  1921. set_error_buf(error_buf, error_buf_size,
  1922. "failed to compile fast jit function");
  1923. return false;
  1924. }
  1925. }
  1926. #endif
  1927. #if WASM_ENABLE_JIT != 0
  1928. /* Ensure all the llvm-jit functions are compiled */
  1929. for (i = 0; i < module->function_count; i++) {
  1930. if (!module->func_ptrs_compiled[i]) {
  1931. set_error_buf(error_buf, error_buf_size,
  1932. "failed to compile llvm jit function");
  1933. return false;
  1934. }
  1935. }
  1936. #endif
  1937. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  1938. bh_print_time("End compile jit functions");
  1939. return true;
  1940. }
  1941. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  1942. #if WASM_ENABLE_REF_TYPES != 0
  1943. static bool
  1944. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  1945. uint8 *p_elem_type, char *error_buf, uint32 error_buf_size)
  1946. {
  1947. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  1948. return false;
  1949. }
  1950. if (p_elem_type) {
  1951. if (table_idx < module->import_table_count)
  1952. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  1953. else
  1954. *p_elem_type =
  1955. module->tables[module->import_table_count + table_idx]
  1956. .elem_type;
  1957. }
  1958. return true;
  1959. }
  1960. static bool
  1961. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  1962. uint8 *p_elem_type, char *error_buf,
  1963. uint32 error_buf_size)
  1964. {
  1965. if (table_seg_idx >= module->table_seg_count) {
  1966. return false;
  1967. }
  1968. if (p_elem_type) {
  1969. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  1970. }
  1971. return true;
  1972. }
  1973. #endif
  1974. static bool
  1975. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  1976. uint32 cur_func_idx, char *error_buf,
  1977. uint32 error_buf_size);
  1978. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  1979. void **
  1980. wasm_interp_get_handle_table();
  1981. static void **handle_table;
  1982. #endif
  1983. static bool
  1984. load_from_sections(WASMModule *module, WASMSection *sections,
  1985. bool is_load_from_file_buf, char *error_buf,
  1986. uint32 error_buf_size)
  1987. {
  1988. WASMExport *export;
  1989. WASMSection *section = sections;
  1990. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  1991. *buf_func = NULL, *buf_func_end = NULL;
  1992. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  1993. WASMGlobal *aux_stack_top_global = NULL, *global;
  1994. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  1995. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  1996. uint32 aux_data_end_global_index = (uint32)-1;
  1997. uint32 aux_heap_base_global_index = (uint32)-1;
  1998. WASMType *func_type;
  1999. /* Find code and function sections if have */
  2000. while (section) {
  2001. if (section->section_type == SECTION_TYPE_CODE) {
  2002. buf_code = section->section_body;
  2003. buf_code_end = buf_code + section->section_body_size;
  2004. }
  2005. else if (section->section_type == SECTION_TYPE_FUNC) {
  2006. buf_func = section->section_body;
  2007. buf_func_end = buf_func + section->section_body_size;
  2008. }
  2009. section = section->next;
  2010. }
  2011. section = sections;
  2012. while (section) {
  2013. buf = section->section_body;
  2014. buf_end = buf + section->section_body_size;
  2015. LOG_DEBUG("load section, type: %d", section->section_type);
  2016. switch (section->section_type) {
  2017. case SECTION_TYPE_USER:
  2018. /* unsupported user section, ignore it. */
  2019. if (!load_user_section(buf, buf_end, module,
  2020. is_load_from_file_buf, error_buf,
  2021. error_buf_size))
  2022. return false;
  2023. break;
  2024. case SECTION_TYPE_TYPE:
  2025. if (!load_type_section(buf, buf_end, module, error_buf,
  2026. error_buf_size))
  2027. return false;
  2028. break;
  2029. case SECTION_TYPE_IMPORT:
  2030. if (!load_import_section(buf, buf_end, module,
  2031. is_load_from_file_buf, error_buf,
  2032. error_buf_size))
  2033. return false;
  2034. break;
  2035. case SECTION_TYPE_FUNC:
  2036. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  2037. module, error_buf, error_buf_size))
  2038. return false;
  2039. break;
  2040. case SECTION_TYPE_TABLE:
  2041. if (!load_table_section(buf, buf_end, module, error_buf,
  2042. error_buf_size))
  2043. return false;
  2044. break;
  2045. case SECTION_TYPE_MEMORY:
  2046. if (!load_memory_section(buf, buf_end, module, error_buf,
  2047. error_buf_size))
  2048. return false;
  2049. break;
  2050. case SECTION_TYPE_GLOBAL:
  2051. if (!load_global_section(buf, buf_end, module, error_buf,
  2052. error_buf_size))
  2053. return false;
  2054. break;
  2055. case SECTION_TYPE_EXPORT:
  2056. if (!load_export_section(buf, buf_end, module,
  2057. is_load_from_file_buf, error_buf,
  2058. error_buf_size))
  2059. return false;
  2060. break;
  2061. case SECTION_TYPE_START:
  2062. if (!load_start_section(buf, buf_end, module, error_buf,
  2063. error_buf_size))
  2064. return false;
  2065. break;
  2066. case SECTION_TYPE_ELEM:
  2067. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  2068. error_buf_size))
  2069. return false;
  2070. break;
  2071. case SECTION_TYPE_CODE:
  2072. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  2073. module, error_buf, error_buf_size))
  2074. return false;
  2075. break;
  2076. case SECTION_TYPE_DATA:
  2077. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  2078. error_buf_size))
  2079. return false;
  2080. break;
  2081. #if WASM_ENABLE_BULK_MEMORY != 0
  2082. case SECTION_TYPE_DATACOUNT:
  2083. if (!load_datacount_section(buf, buf_end, module, error_buf,
  2084. error_buf_size))
  2085. return false;
  2086. break;
  2087. #endif
  2088. default:
  2089. set_error_buf(error_buf, error_buf_size, "invalid section id");
  2090. return false;
  2091. }
  2092. section = section->next;
  2093. }
  2094. module->aux_data_end_global_index = (uint32)-1;
  2095. module->aux_heap_base_global_index = (uint32)-1;
  2096. module->aux_stack_top_global_index = (uint32)-1;
  2097. /* Resolve auxiliary data/stack/heap info and reset memory info */
  2098. export = module->exports;
  2099. for (i = 0; i < module->export_count; i++, export ++) {
  2100. if (export->kind == EXPORT_KIND_GLOBAL) {
  2101. if (!strcmp(export->name, "__heap_base")) {
  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_heap_base_global = global;
  2108. aux_heap_base = global->init_expr.u.i32;
  2109. aux_heap_base_global_index = export->index;
  2110. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  2111. aux_heap_base);
  2112. }
  2113. }
  2114. else if (!strcmp(export->name, "__data_end")) {
  2115. global_index = export->index - module->import_global_count;
  2116. global = module->globals + global_index;
  2117. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  2118. && global->init_expr.init_expr_type
  2119. == INIT_EXPR_TYPE_I32_CONST) {
  2120. aux_data_end_global = global;
  2121. aux_data_end = global->init_expr.u.i32;
  2122. aux_data_end_global_index = export->index;
  2123. LOG_VERBOSE("Found aux __data_end global, value: %d",
  2124. aux_data_end);
  2125. aux_data_end = align_uint(aux_data_end, 16);
  2126. }
  2127. }
  2128. /* For module compiled with -pthread option, the global is:
  2129. [0] stack_top <-- 0
  2130. [1] tls_pointer
  2131. [2] tls_size
  2132. [3] data_end <-- 3
  2133. [4] global_base
  2134. [5] heap_base <-- 5
  2135. [6] dso_handle
  2136. For module compiled without -pthread option:
  2137. [0] stack_top <-- 0
  2138. [1] data_end <-- 1
  2139. [2] global_base
  2140. [3] heap_base <-- 3
  2141. [4] dso_handle
  2142. */
  2143. if (aux_data_end_global && aux_heap_base_global
  2144. && aux_data_end <= aux_heap_base) {
  2145. module->aux_data_end_global_index = aux_data_end_global_index;
  2146. module->aux_data_end = aux_data_end;
  2147. module->aux_heap_base_global_index = aux_heap_base_global_index;
  2148. module->aux_heap_base = aux_heap_base;
  2149. /* Resolve aux stack top global */
  2150. for (global_index = 0; global_index < module->global_count;
  2151. global_index++) {
  2152. global = module->globals + global_index;
  2153. if (global->is_mutable /* heap_base and data_end is
  2154. not mutable */
  2155. && global->type == VALUE_TYPE_I32
  2156. && global->init_expr.init_expr_type
  2157. == INIT_EXPR_TYPE_I32_CONST
  2158. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  2159. aux_stack_top_global = global;
  2160. aux_stack_top = (uint32)global->init_expr.u.i32;
  2161. module->aux_stack_top_global_index =
  2162. module->import_global_count + global_index;
  2163. module->aux_stack_bottom = aux_stack_top;
  2164. module->aux_stack_size =
  2165. aux_stack_top > aux_data_end
  2166. ? aux_stack_top - aux_data_end
  2167. : aux_stack_top;
  2168. LOG_VERBOSE("Found aux stack top global, value: %d, "
  2169. "global index: %d, stack size: %d",
  2170. aux_stack_top, global_index,
  2171. module->aux_stack_size);
  2172. break;
  2173. }
  2174. }
  2175. if (!aux_stack_top_global) {
  2176. /* Auxiliary stack global isn't found, it must be unused
  2177. in the wasm app, as if it is used, the global must be
  2178. defined. Here we set it to __heap_base global and set
  2179. its size to 0. */
  2180. aux_stack_top_global = aux_heap_base_global;
  2181. aux_stack_top = aux_heap_base;
  2182. module->aux_stack_top_global_index =
  2183. module->aux_heap_base_global_index;
  2184. module->aux_stack_bottom = aux_stack_top;
  2185. module->aux_stack_size = 0;
  2186. }
  2187. break;
  2188. }
  2189. }
  2190. }
  2191. module->malloc_function = (uint32)-1;
  2192. module->free_function = (uint32)-1;
  2193. module->retain_function = (uint32)-1;
  2194. /* Resolve malloc/free function exported by wasm module */
  2195. export = module->exports;
  2196. for (i = 0; i < module->export_count; i++, export ++) {
  2197. if (export->kind == EXPORT_KIND_FUNC) {
  2198. if (!strcmp(export->name, "malloc")
  2199. && export->index >= module->import_function_count) {
  2200. func_index = export->index - module->import_function_count;
  2201. func_type = module->functions[func_index]->func_type;
  2202. if (func_type->param_count == 1 && func_type->result_count == 1
  2203. && func_type->types[0] == VALUE_TYPE_I32
  2204. && func_type->types[1] == VALUE_TYPE_I32) {
  2205. bh_assert(module->malloc_function == (uint32)-1);
  2206. module->malloc_function = export->index;
  2207. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2208. export->name, export->index);
  2209. }
  2210. }
  2211. else if (!strcmp(export->name, "__new")
  2212. && export->index >= module->import_function_count) {
  2213. /* __new && __pin for AssemblyScript */
  2214. func_index = export->index - module->import_function_count;
  2215. func_type = module->functions[func_index]->func_type;
  2216. if (func_type->param_count == 2 && func_type->result_count == 1
  2217. && func_type->types[0] == VALUE_TYPE_I32
  2218. && func_type->types[1] == VALUE_TYPE_I32
  2219. && func_type->types[2] == VALUE_TYPE_I32) {
  2220. uint32 j;
  2221. WASMExport *export_tmp;
  2222. bh_assert(module->malloc_function == (uint32)-1);
  2223. module->malloc_function = export->index;
  2224. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2225. export->name, export->index);
  2226. /* resolve retain function.
  2227. If not found, reset malloc function index */
  2228. export_tmp = module->exports;
  2229. for (j = 0; j < module->export_count; j++, export_tmp++) {
  2230. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  2231. && (!strcmp(export_tmp->name, "__retain")
  2232. || !strcmp(export_tmp->name, "__pin"))
  2233. && (export_tmp->index
  2234. >= module->import_function_count)) {
  2235. func_index = export_tmp->index
  2236. - module->import_function_count;
  2237. func_type =
  2238. module->functions[func_index]->func_type;
  2239. if (func_type->param_count == 1
  2240. && func_type->result_count == 1
  2241. && func_type->types[0] == VALUE_TYPE_I32
  2242. && func_type->types[1] == VALUE_TYPE_I32) {
  2243. bh_assert(module->retain_function
  2244. == (uint32)-1);
  2245. module->retain_function = export_tmp->index;
  2246. LOG_VERBOSE("Found retain function, name: %s, "
  2247. "index: %u",
  2248. export_tmp->name,
  2249. export_tmp->index);
  2250. break;
  2251. }
  2252. }
  2253. }
  2254. if (j == module->export_count) {
  2255. module->malloc_function = (uint32)-1;
  2256. LOG_VERBOSE("Can't find retain function,"
  2257. "reset malloc function index to -1");
  2258. }
  2259. }
  2260. }
  2261. else if (((!strcmp(export->name, "free"))
  2262. || (!strcmp(export->name, "__release"))
  2263. || (!strcmp(export->name, "__unpin")))
  2264. && export->index >= module->import_function_count) {
  2265. func_index = export->index - module->import_function_count;
  2266. func_type = module->functions[func_index]->func_type;
  2267. if (func_type->param_count == 1 && func_type->result_count == 0
  2268. && func_type->types[0] == VALUE_TYPE_I32) {
  2269. bh_assert(module->free_function == (uint32)-1);
  2270. module->free_function = export->index;
  2271. LOG_VERBOSE("Found free function, name: %s, index: %u",
  2272. export->name, export->index);
  2273. }
  2274. }
  2275. }
  2276. }
  2277. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  2278. handle_table = wasm_interp_get_handle_table();
  2279. #endif
  2280. for (i = 0; i < module->function_count; i++) {
  2281. WASMFunction *func = module->functions[i];
  2282. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  2283. error_buf_size)) {
  2284. return false;
  2285. }
  2286. if (i == module->function_count - 1) {
  2287. bh_assert(func->code + func->code_size == buf_code_end);
  2288. }
  2289. }
  2290. if (!module->possible_memory_grow) {
  2291. WASMMemoryImport *memory_import;
  2292. WASMMemory *memory;
  2293. if (aux_data_end_global && aux_heap_base_global
  2294. && aux_stack_top_global) {
  2295. uint64 init_memory_size;
  2296. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  2297. if (module->import_memory_count) {
  2298. memory_import = &module->import_memories[0].u.memory;
  2299. init_memory_size = (uint64)memory_import->num_bytes_per_page
  2300. * memory_import->init_page_count;
  2301. if (shrunk_memory_size <= init_memory_size) {
  2302. /* Reset memory info to decrease memory usage */
  2303. memory_import->num_bytes_per_page = shrunk_memory_size;
  2304. memory_import->init_page_count = 1;
  2305. LOG_VERBOSE("Shrink import memory size to %d",
  2306. shrunk_memory_size);
  2307. }
  2308. }
  2309. if (module->memory_count) {
  2310. memory = &module->memories[0];
  2311. init_memory_size = (uint64)memory->num_bytes_per_page
  2312. * memory->init_page_count;
  2313. if (shrunk_memory_size <= init_memory_size) {
  2314. /* Reset memory info to decrease memory usage */
  2315. memory->num_bytes_per_page = shrunk_memory_size;
  2316. memory->init_page_count = 1;
  2317. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  2318. }
  2319. }
  2320. }
  2321. if (module->import_memory_count) {
  2322. memory_import = &module->import_memories[0].u.memory;
  2323. if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
  2324. memory_import->num_bytes_per_page *=
  2325. memory_import->init_page_count;
  2326. else
  2327. memory_import->num_bytes_per_page = UINT32_MAX;
  2328. if (memory_import->init_page_count > 0)
  2329. memory_import->init_page_count = memory_import->max_page_count =
  2330. 1;
  2331. else
  2332. memory_import->init_page_count = memory_import->max_page_count =
  2333. 0;
  2334. }
  2335. if (module->memory_count) {
  2336. memory = &module->memories[0];
  2337. if (memory->init_page_count < DEFAULT_MAX_PAGES)
  2338. memory->num_bytes_per_page *= memory->init_page_count;
  2339. else
  2340. memory->num_bytes_per_page = UINT32_MAX;
  2341. if (memory->init_page_count > 0)
  2342. memory->init_page_count = memory->max_page_count = 1;
  2343. else
  2344. memory->init_page_count = memory->max_page_count = 0;
  2345. }
  2346. }
  2347. calculate_global_data_offset(module);
  2348. #if WASM_ENABLE_FAST_JIT != 0
  2349. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  2350. return false;
  2351. }
  2352. #endif
  2353. #if WASM_ENABLE_JIT != 0
  2354. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  2355. return false;
  2356. }
  2357. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  2358. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  2359. return false;
  2360. }
  2361. #else
  2362. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  2363. thread fast jit execution, since applying llvm optimizations in
  2364. aot_compile_wasm may cost a lot of time.
  2365. Create thread with enough native stack to apply llvm optimizations */
  2366. if (os_thread_create(&module->llvm_jit_init_thread,
  2367. init_llvm_jit_functions_stage2_callback,
  2368. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  2369. != 0) {
  2370. set_error_buf(error_buf, error_buf_size,
  2371. "create orcjit compile thread failed");
  2372. return false;
  2373. }
  2374. #endif
  2375. #endif
  2376. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2377. /* Create threads to compile the jit functions */
  2378. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  2379. return false;
  2380. }
  2381. #endif
  2382. #if WASM_ENABLE_MEMORY_TRACING != 0
  2383. wasm_runtime_dump_module_mem_consumption(module);
  2384. #endif
  2385. return true;
  2386. }
  2387. static WASMModule *
  2388. create_module(char *error_buf, uint32 error_buf_size)
  2389. {
  2390. WASMModule *module =
  2391. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  2392. bh_list_status ret;
  2393. if (!module) {
  2394. return NULL;
  2395. }
  2396. module->module_type = Wasm_Module_Bytecode;
  2397. /* Set start_function to -1, means no start function */
  2398. module->start_function = (uint32)-1;
  2399. #if WASM_ENABLE_FAST_INTERP == 0
  2400. module->br_table_cache_list = &module->br_table_cache_list_head;
  2401. ret = bh_list_init(module->br_table_cache_list);
  2402. bh_assert(ret == BH_LIST_SUCCESS);
  2403. #endif
  2404. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2405. && WASM_ENABLE_LAZY_JIT != 0
  2406. if (os_mutex_init(&module->instance_list_lock) != 0) {
  2407. set_error_buf(error_buf, error_buf_size,
  2408. "init instance list lock failed");
  2409. wasm_runtime_free(module);
  2410. return NULL;
  2411. }
  2412. #endif
  2413. (void)ret;
  2414. return module;
  2415. }
  2416. WASMModule *
  2417. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  2418. uint32 error_buf_size)
  2419. {
  2420. WASMModule *module = create_module(error_buf, error_buf_size);
  2421. if (!module)
  2422. return NULL;
  2423. if (!load_from_sections(module, section_list, false, error_buf,
  2424. error_buf_size)) {
  2425. wasm_loader_unload(module);
  2426. return NULL;
  2427. }
  2428. LOG_VERBOSE("Load module from sections success.\n");
  2429. return module;
  2430. }
  2431. static void
  2432. destroy_sections(WASMSection *section_list)
  2433. {
  2434. WASMSection *section = section_list, *next;
  2435. while (section) {
  2436. next = section->next;
  2437. wasm_runtime_free(section);
  2438. section = next;
  2439. }
  2440. }
  2441. /* clang-format off */
  2442. static uint8 section_ids[] = {
  2443. SECTION_TYPE_USER,
  2444. SECTION_TYPE_TYPE,
  2445. SECTION_TYPE_IMPORT,
  2446. SECTION_TYPE_FUNC,
  2447. SECTION_TYPE_TABLE,
  2448. SECTION_TYPE_MEMORY,
  2449. SECTION_TYPE_GLOBAL,
  2450. SECTION_TYPE_EXPORT,
  2451. SECTION_TYPE_START,
  2452. SECTION_TYPE_ELEM,
  2453. #if WASM_ENABLE_BULK_MEMORY != 0
  2454. SECTION_TYPE_DATACOUNT,
  2455. #endif
  2456. SECTION_TYPE_CODE,
  2457. SECTION_TYPE_DATA
  2458. };
  2459. /* clang-format on */
  2460. static uint8
  2461. get_section_index(uint8 section_type)
  2462. {
  2463. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  2464. for (uint8 i = 0; i < max_id; i++) {
  2465. if (section_type == section_ids[i])
  2466. return i;
  2467. }
  2468. return (uint8)-1;
  2469. }
  2470. static bool
  2471. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  2472. char *error_buf, uint32 error_buf_size)
  2473. {
  2474. WASMSection *section_list_end = NULL, *section;
  2475. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  2476. uint8 section_type, section_index, last_section_index = (uint8)-1;
  2477. uint32 section_size;
  2478. bh_assert(!*p_section_list);
  2479. p += 8;
  2480. while (p < p_end) {
  2481. CHECK_BUF(p, p_end, 1);
  2482. section_type = read_uint8(p);
  2483. section_index = get_section_index(section_type);
  2484. if (section_index != (uint8)-1) {
  2485. if (section_type != SECTION_TYPE_USER) {
  2486. /* Custom sections may be inserted at any place,
  2487. while other sections must occur at most once
  2488. and in prescribed order. */
  2489. bh_assert(last_section_index == (uint8)-1
  2490. || last_section_index < section_index);
  2491. last_section_index = section_index;
  2492. }
  2493. read_leb_uint32(p, p_end, section_size);
  2494. CHECK_BUF1(p, p_end, section_size);
  2495. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  2496. error_buf_size))) {
  2497. return false;
  2498. }
  2499. section->section_type = section_type;
  2500. section->section_body = (uint8 *)p;
  2501. section->section_body_size = section_size;
  2502. if (!*p_section_list)
  2503. *p_section_list = section_list_end = section;
  2504. else {
  2505. section_list_end->next = section;
  2506. section_list_end = section;
  2507. }
  2508. p += section_size;
  2509. }
  2510. else {
  2511. bh_assert(0);
  2512. }
  2513. }
  2514. (void)last_section_index;
  2515. return true;
  2516. }
  2517. static void
  2518. exchange32(uint8 *p_data)
  2519. {
  2520. uint8 value = *p_data;
  2521. *p_data = *(p_data + 3);
  2522. *(p_data + 3) = value;
  2523. value = *(p_data + 1);
  2524. *(p_data + 1) = *(p_data + 2);
  2525. *(p_data + 2) = value;
  2526. }
  2527. static union {
  2528. int a;
  2529. char b;
  2530. } __ue = { .a = 1 };
  2531. #define is_little_endian() (__ue.b == 1)
  2532. static bool
  2533. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  2534. uint32 error_buf_size)
  2535. {
  2536. const uint8 *buf_end = buf + size;
  2537. const uint8 *p = buf, *p_end = buf_end;
  2538. uint32 magic_number, version;
  2539. WASMSection *section_list = NULL;
  2540. CHECK_BUF1(p, p_end, sizeof(uint32));
  2541. magic_number = read_uint32(p);
  2542. if (!is_little_endian())
  2543. exchange32((uint8 *)&magic_number);
  2544. bh_assert(magic_number == WASM_MAGIC_NUMBER);
  2545. CHECK_BUF1(p, p_end, sizeof(uint32));
  2546. version = read_uint32(p);
  2547. if (!is_little_endian())
  2548. exchange32((uint8 *)&version);
  2549. if (version != WASM_CURRENT_VERSION) {
  2550. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  2551. return false;
  2552. }
  2553. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  2554. || !load_from_sections(module, section_list, true, error_buf,
  2555. error_buf_size)) {
  2556. destroy_sections(section_list);
  2557. return false;
  2558. }
  2559. destroy_sections(section_list);
  2560. (void)p_end;
  2561. return true;
  2562. }
  2563. WASMModule *
  2564. wasm_loader_load(uint8 *buf, uint32 size, char *error_buf,
  2565. uint32 error_buf_size)
  2566. {
  2567. WASMModule *module = create_module(error_buf, error_buf_size);
  2568. if (!module) {
  2569. return NULL;
  2570. }
  2571. #if WASM_ENABLE_FAST_JIT != 0
  2572. module->load_addr = (uint8 *)buf;
  2573. module->load_size = size;
  2574. #endif
  2575. if (!load(buf, size, module, error_buf, error_buf_size)) {
  2576. goto fail;
  2577. }
  2578. LOG_VERBOSE("Load module success.\n");
  2579. return module;
  2580. fail:
  2581. wasm_loader_unload(module);
  2582. return NULL;
  2583. }
  2584. void
  2585. wasm_loader_unload(WASMModule *module)
  2586. {
  2587. uint32 i;
  2588. if (!module)
  2589. return;
  2590. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2591. && WASM_ENABLE_LAZY_JIT != 0
  2592. module->orcjit_stop_compiling = true;
  2593. if (module->llvm_jit_init_thread)
  2594. os_thread_join(module->llvm_jit_init_thread, NULL);
  2595. #endif
  2596. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2597. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  2598. module internal data after they were freed */
  2599. orcjit_stop_compile_threads(module);
  2600. #endif
  2601. #if WASM_ENABLE_JIT != 0
  2602. if (module->func_ptrs)
  2603. wasm_runtime_free(module->func_ptrs);
  2604. if (module->comp_ctx)
  2605. aot_destroy_comp_context(module->comp_ctx);
  2606. if (module->comp_data)
  2607. aot_destroy_comp_data(module->comp_data);
  2608. #endif
  2609. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2610. && WASM_ENABLE_LAZY_JIT != 0
  2611. if (module->tierup_wait_lock_inited) {
  2612. os_mutex_destroy(&module->tierup_wait_lock);
  2613. os_cond_destroy(&module->tierup_wait_cond);
  2614. }
  2615. #endif
  2616. if (module->types) {
  2617. for (i = 0; i < module->type_count; i++) {
  2618. if (module->types[i])
  2619. destroy_wasm_type(module->types[i]);
  2620. }
  2621. wasm_runtime_free(module->types);
  2622. }
  2623. if (module->imports)
  2624. wasm_runtime_free(module->imports);
  2625. if (module->functions) {
  2626. for (i = 0; i < module->function_count; i++) {
  2627. if (module->functions[i]) {
  2628. if (module->functions[i]->local_offsets)
  2629. wasm_runtime_free(module->functions[i]->local_offsets);
  2630. #if WASM_ENABLE_FAST_INTERP != 0
  2631. if (module->functions[i]->code_compiled)
  2632. wasm_runtime_free(module->functions[i]->code_compiled);
  2633. if (module->functions[i]->consts)
  2634. wasm_runtime_free(module->functions[i]->consts);
  2635. #endif
  2636. #if WASM_ENABLE_FAST_JIT != 0
  2637. if (module->functions[i]->fast_jit_jitted_code) {
  2638. jit_code_cache_free(
  2639. module->functions[i]->fast_jit_jitted_code);
  2640. }
  2641. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2642. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  2643. jit_code_cache_free(
  2644. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  2645. }
  2646. #endif
  2647. #endif
  2648. wasm_runtime_free(module->functions[i]);
  2649. }
  2650. }
  2651. wasm_runtime_free(module->functions);
  2652. }
  2653. if (module->tables)
  2654. wasm_runtime_free(module->tables);
  2655. if (module->memories)
  2656. wasm_runtime_free(module->memories);
  2657. if (module->globals)
  2658. wasm_runtime_free(module->globals);
  2659. if (module->exports)
  2660. wasm_runtime_free(module->exports);
  2661. if (module->table_segments) {
  2662. for (i = 0; i < module->table_seg_count; i++) {
  2663. if (module->table_segments[i].func_indexes)
  2664. wasm_runtime_free(module->table_segments[i].func_indexes);
  2665. }
  2666. wasm_runtime_free(module->table_segments);
  2667. }
  2668. if (module->data_segments) {
  2669. for (i = 0; i < module->data_seg_count; i++) {
  2670. if (module->data_segments[i])
  2671. wasm_runtime_free(module->data_segments[i]);
  2672. }
  2673. wasm_runtime_free(module->data_segments);
  2674. }
  2675. if (module->const_str_list) {
  2676. StringNode *node = module->const_str_list, *node_next;
  2677. while (node) {
  2678. node_next = node->next;
  2679. wasm_runtime_free(node);
  2680. node = node_next;
  2681. }
  2682. }
  2683. #if WASM_ENABLE_FAST_INTERP == 0
  2684. if (module->br_table_cache_list) {
  2685. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  2686. BrTableCache *node_next;
  2687. while (node) {
  2688. node_next = bh_list_elem_next(node);
  2689. wasm_runtime_free(node);
  2690. node = node_next;
  2691. }
  2692. }
  2693. #endif
  2694. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2695. && WASM_ENABLE_LAZY_JIT != 0
  2696. os_mutex_destroy(&module->instance_list_lock);
  2697. #endif
  2698. #if WASM_ENABLE_FAST_JIT != 0
  2699. if (module->fast_jit_func_ptrs) {
  2700. wasm_runtime_free(module->fast_jit_func_ptrs);
  2701. }
  2702. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  2703. if (module->fast_jit_thread_locks_inited[i]) {
  2704. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  2705. }
  2706. }
  2707. #endif
  2708. wasm_runtime_free(module);
  2709. }
  2710. bool
  2711. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  2712. const uint8 *start_addr, const uint8 *code_end_addr,
  2713. uint8 label_type, uint8 **p_else_addr,
  2714. uint8 **p_end_addr)
  2715. {
  2716. const uint8 *p = start_addr, *p_end = code_end_addr;
  2717. uint8 *else_addr = NULL;
  2718. char error_buf[128];
  2719. uint32 block_nested_depth = 1, count, i, j, t;
  2720. uint32 error_buf_size = sizeof(error_buf);
  2721. uint8 opcode, u8;
  2722. BlockAddr block_stack[16] = { 0 }, *block;
  2723. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  2724. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2725. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  2726. if (block[j].start_addr == start_addr) {
  2727. /* Cache hit */
  2728. *p_else_addr = block[j].else_addr;
  2729. *p_end_addr = block[j].end_addr;
  2730. return true;
  2731. }
  2732. }
  2733. /* Cache unhit */
  2734. block_stack[0].start_addr = start_addr;
  2735. while (p < code_end_addr) {
  2736. opcode = *p++;
  2737. switch (opcode) {
  2738. case WASM_OP_UNREACHABLE:
  2739. case WASM_OP_NOP:
  2740. break;
  2741. case WASM_OP_BLOCK:
  2742. case WASM_OP_LOOP:
  2743. case WASM_OP_IF:
  2744. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  2745. u8 = read_uint8(p);
  2746. if (block_nested_depth
  2747. < sizeof(block_stack) / sizeof(BlockAddr)) {
  2748. block_stack[block_nested_depth].start_addr = p;
  2749. block_stack[block_nested_depth].else_addr = NULL;
  2750. }
  2751. block_nested_depth++;
  2752. break;
  2753. case EXT_OP_BLOCK:
  2754. case EXT_OP_LOOP:
  2755. case EXT_OP_IF:
  2756. /* block type */
  2757. skip_leb_uint32(p, p_end);
  2758. if (block_nested_depth
  2759. < sizeof(block_stack) / sizeof(BlockAddr)) {
  2760. block_stack[block_nested_depth].start_addr = p;
  2761. block_stack[block_nested_depth].else_addr = NULL;
  2762. }
  2763. block_nested_depth++;
  2764. break;
  2765. case WASM_OP_ELSE:
  2766. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  2767. else_addr = (uint8 *)(p - 1);
  2768. if (block_nested_depth - 1
  2769. < sizeof(block_stack) / sizeof(BlockAddr))
  2770. block_stack[block_nested_depth - 1].else_addr =
  2771. (uint8 *)(p - 1);
  2772. break;
  2773. case WASM_OP_END:
  2774. if (block_nested_depth == 1) {
  2775. if (label_type == LABEL_TYPE_IF)
  2776. *p_else_addr = else_addr;
  2777. *p_end_addr = (uint8 *)(p - 1);
  2778. block_stack[0].end_addr = (uint8 *)(p - 1);
  2779. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  2780. t++) {
  2781. start_addr = block_stack[t].start_addr;
  2782. if (start_addr) {
  2783. i = ((uintptr_t)start_addr)
  2784. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  2785. block =
  2786. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2787. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  2788. if (!block[j].start_addr)
  2789. break;
  2790. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  2791. memmove(block + 1, block,
  2792. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  2793. * sizeof(BlockAddr));
  2794. j = 0;
  2795. }
  2796. block[j].start_addr = block_stack[t].start_addr;
  2797. block[j].else_addr = block_stack[t].else_addr;
  2798. block[j].end_addr = block_stack[t].end_addr;
  2799. }
  2800. else
  2801. break;
  2802. }
  2803. return true;
  2804. }
  2805. else {
  2806. block_nested_depth--;
  2807. if (block_nested_depth
  2808. < sizeof(block_stack) / sizeof(BlockAddr))
  2809. block_stack[block_nested_depth].end_addr =
  2810. (uint8 *)(p - 1);
  2811. }
  2812. break;
  2813. case WASM_OP_BR:
  2814. case WASM_OP_BR_IF:
  2815. skip_leb_uint32(p, p_end); /* labelidx */
  2816. break;
  2817. case WASM_OP_BR_TABLE:
  2818. read_leb_uint32(p, p_end, count); /* lable num */
  2819. #if WASM_ENABLE_FAST_INTERP != 0
  2820. for (i = 0; i <= count; i++) /* lableidxs */
  2821. skip_leb_uint32(p, p_end);
  2822. #else
  2823. p += count + 1;
  2824. while (*p == WASM_OP_NOP)
  2825. p++;
  2826. #endif
  2827. break;
  2828. #if WASM_ENABLE_FAST_INTERP == 0
  2829. case EXT_OP_BR_TABLE_CACHE:
  2830. read_leb_uint32(p, p_end, count); /* lable num */
  2831. while (*p == WASM_OP_NOP)
  2832. p++;
  2833. break;
  2834. #endif
  2835. case WASM_OP_RETURN:
  2836. break;
  2837. case WASM_OP_CALL:
  2838. #if WASM_ENABLE_TAIL_CALL != 0
  2839. case WASM_OP_RETURN_CALL:
  2840. #endif
  2841. skip_leb_uint32(p, p_end); /* funcidx */
  2842. break;
  2843. case WASM_OP_CALL_INDIRECT:
  2844. #if WASM_ENABLE_TAIL_CALL != 0
  2845. case WASM_OP_RETURN_CALL_INDIRECT:
  2846. #endif
  2847. skip_leb_uint32(p, p_end); /* typeidx */
  2848. CHECK_BUF(p, p_end, 1);
  2849. u8 = read_uint8(p); /* 0x00 */
  2850. break;
  2851. case WASM_OP_DROP:
  2852. case WASM_OP_SELECT:
  2853. case WASM_OP_DROP_64:
  2854. case WASM_OP_SELECT_64:
  2855. break;
  2856. #if WASM_ENABLE_REF_TYPES != 0
  2857. case WASM_OP_SELECT_T:
  2858. skip_leb_uint32(p, p_end); /* vec length */
  2859. CHECK_BUF(p, p_end, 1);
  2860. u8 = read_uint8(p); /* typeidx */
  2861. break;
  2862. case WASM_OP_TABLE_GET:
  2863. case WASM_OP_TABLE_SET:
  2864. skip_leb_uint32(p, p_end); /* table index */
  2865. break;
  2866. case WASM_OP_REF_NULL:
  2867. CHECK_BUF(p, p_end, 1);
  2868. u8 = read_uint8(p); /* type */
  2869. break;
  2870. case WASM_OP_REF_IS_NULL:
  2871. break;
  2872. case WASM_OP_REF_FUNC:
  2873. skip_leb_uint32(p, p_end); /* func index */
  2874. break;
  2875. #endif /* WASM_ENABLE_REF_TYPES */
  2876. case WASM_OP_GET_LOCAL:
  2877. case WASM_OP_SET_LOCAL:
  2878. case WASM_OP_TEE_LOCAL:
  2879. case WASM_OP_GET_GLOBAL:
  2880. case WASM_OP_SET_GLOBAL:
  2881. case WASM_OP_GET_GLOBAL_64:
  2882. case WASM_OP_SET_GLOBAL_64:
  2883. case WASM_OP_SET_GLOBAL_AUX_STACK:
  2884. skip_leb_uint32(p, p_end); /* localidx */
  2885. break;
  2886. case EXT_OP_GET_LOCAL_FAST:
  2887. case EXT_OP_SET_LOCAL_FAST:
  2888. case EXT_OP_TEE_LOCAL_FAST:
  2889. CHECK_BUF(p, p_end, 1);
  2890. p++;
  2891. break;
  2892. case WASM_OP_I32_LOAD:
  2893. case WASM_OP_I64_LOAD:
  2894. case WASM_OP_F32_LOAD:
  2895. case WASM_OP_F64_LOAD:
  2896. case WASM_OP_I32_LOAD8_S:
  2897. case WASM_OP_I32_LOAD8_U:
  2898. case WASM_OP_I32_LOAD16_S:
  2899. case WASM_OP_I32_LOAD16_U:
  2900. case WASM_OP_I64_LOAD8_S:
  2901. case WASM_OP_I64_LOAD8_U:
  2902. case WASM_OP_I64_LOAD16_S:
  2903. case WASM_OP_I64_LOAD16_U:
  2904. case WASM_OP_I64_LOAD32_S:
  2905. case WASM_OP_I64_LOAD32_U:
  2906. case WASM_OP_I32_STORE:
  2907. case WASM_OP_I64_STORE:
  2908. case WASM_OP_F32_STORE:
  2909. case WASM_OP_F64_STORE:
  2910. case WASM_OP_I32_STORE8:
  2911. case WASM_OP_I32_STORE16:
  2912. case WASM_OP_I64_STORE8:
  2913. case WASM_OP_I64_STORE16:
  2914. case WASM_OP_I64_STORE32:
  2915. skip_leb_uint32(p, p_end); /* align */
  2916. skip_leb_uint32(p, p_end); /* offset */
  2917. break;
  2918. case WASM_OP_MEMORY_SIZE:
  2919. case WASM_OP_MEMORY_GROW:
  2920. skip_leb_uint32(p, p_end); /* 0x00 */
  2921. break;
  2922. case WASM_OP_I32_CONST:
  2923. skip_leb_int32(p, p_end);
  2924. break;
  2925. case WASM_OP_I64_CONST:
  2926. skip_leb_int64(p, p_end);
  2927. break;
  2928. case WASM_OP_F32_CONST:
  2929. p += sizeof(float32);
  2930. break;
  2931. case WASM_OP_F64_CONST:
  2932. p += sizeof(float64);
  2933. break;
  2934. case WASM_OP_I32_EQZ:
  2935. case WASM_OP_I32_EQ:
  2936. case WASM_OP_I32_NE:
  2937. case WASM_OP_I32_LT_S:
  2938. case WASM_OP_I32_LT_U:
  2939. case WASM_OP_I32_GT_S:
  2940. case WASM_OP_I32_GT_U:
  2941. case WASM_OP_I32_LE_S:
  2942. case WASM_OP_I32_LE_U:
  2943. case WASM_OP_I32_GE_S:
  2944. case WASM_OP_I32_GE_U:
  2945. case WASM_OP_I64_EQZ:
  2946. case WASM_OP_I64_EQ:
  2947. case WASM_OP_I64_NE:
  2948. case WASM_OP_I64_LT_S:
  2949. case WASM_OP_I64_LT_U:
  2950. case WASM_OP_I64_GT_S:
  2951. case WASM_OP_I64_GT_U:
  2952. case WASM_OP_I64_LE_S:
  2953. case WASM_OP_I64_LE_U:
  2954. case WASM_OP_I64_GE_S:
  2955. case WASM_OP_I64_GE_U:
  2956. case WASM_OP_F32_EQ:
  2957. case WASM_OP_F32_NE:
  2958. case WASM_OP_F32_LT:
  2959. case WASM_OP_F32_GT:
  2960. case WASM_OP_F32_LE:
  2961. case WASM_OP_F32_GE:
  2962. case WASM_OP_F64_EQ:
  2963. case WASM_OP_F64_NE:
  2964. case WASM_OP_F64_LT:
  2965. case WASM_OP_F64_GT:
  2966. case WASM_OP_F64_LE:
  2967. case WASM_OP_F64_GE:
  2968. case WASM_OP_I32_CLZ:
  2969. case WASM_OP_I32_CTZ:
  2970. case WASM_OP_I32_POPCNT:
  2971. case WASM_OP_I32_ADD:
  2972. case WASM_OP_I32_SUB:
  2973. case WASM_OP_I32_MUL:
  2974. case WASM_OP_I32_DIV_S:
  2975. case WASM_OP_I32_DIV_U:
  2976. case WASM_OP_I32_REM_S:
  2977. case WASM_OP_I32_REM_U:
  2978. case WASM_OP_I32_AND:
  2979. case WASM_OP_I32_OR:
  2980. case WASM_OP_I32_XOR:
  2981. case WASM_OP_I32_SHL:
  2982. case WASM_OP_I32_SHR_S:
  2983. case WASM_OP_I32_SHR_U:
  2984. case WASM_OP_I32_ROTL:
  2985. case WASM_OP_I32_ROTR:
  2986. case WASM_OP_I64_CLZ:
  2987. case WASM_OP_I64_CTZ:
  2988. case WASM_OP_I64_POPCNT:
  2989. case WASM_OP_I64_ADD:
  2990. case WASM_OP_I64_SUB:
  2991. case WASM_OP_I64_MUL:
  2992. case WASM_OP_I64_DIV_S:
  2993. case WASM_OP_I64_DIV_U:
  2994. case WASM_OP_I64_REM_S:
  2995. case WASM_OP_I64_REM_U:
  2996. case WASM_OP_I64_AND:
  2997. case WASM_OP_I64_OR:
  2998. case WASM_OP_I64_XOR:
  2999. case WASM_OP_I64_SHL:
  3000. case WASM_OP_I64_SHR_S:
  3001. case WASM_OP_I64_SHR_U:
  3002. case WASM_OP_I64_ROTL:
  3003. case WASM_OP_I64_ROTR:
  3004. case WASM_OP_F32_ABS:
  3005. case WASM_OP_F32_NEG:
  3006. case WASM_OP_F32_CEIL:
  3007. case WASM_OP_F32_FLOOR:
  3008. case WASM_OP_F32_TRUNC:
  3009. case WASM_OP_F32_NEAREST:
  3010. case WASM_OP_F32_SQRT:
  3011. case WASM_OP_F32_ADD:
  3012. case WASM_OP_F32_SUB:
  3013. case WASM_OP_F32_MUL:
  3014. case WASM_OP_F32_DIV:
  3015. case WASM_OP_F32_MIN:
  3016. case WASM_OP_F32_MAX:
  3017. case WASM_OP_F32_COPYSIGN:
  3018. case WASM_OP_F64_ABS:
  3019. case WASM_OP_F64_NEG:
  3020. case WASM_OP_F64_CEIL:
  3021. case WASM_OP_F64_FLOOR:
  3022. case WASM_OP_F64_TRUNC:
  3023. case WASM_OP_F64_NEAREST:
  3024. case WASM_OP_F64_SQRT:
  3025. case WASM_OP_F64_ADD:
  3026. case WASM_OP_F64_SUB:
  3027. case WASM_OP_F64_MUL:
  3028. case WASM_OP_F64_DIV:
  3029. case WASM_OP_F64_MIN:
  3030. case WASM_OP_F64_MAX:
  3031. case WASM_OP_F64_COPYSIGN:
  3032. case WASM_OP_I32_WRAP_I64:
  3033. case WASM_OP_I32_TRUNC_S_F32:
  3034. case WASM_OP_I32_TRUNC_U_F32:
  3035. case WASM_OP_I32_TRUNC_S_F64:
  3036. case WASM_OP_I32_TRUNC_U_F64:
  3037. case WASM_OP_I64_EXTEND_S_I32:
  3038. case WASM_OP_I64_EXTEND_U_I32:
  3039. case WASM_OP_I64_TRUNC_S_F32:
  3040. case WASM_OP_I64_TRUNC_U_F32:
  3041. case WASM_OP_I64_TRUNC_S_F64:
  3042. case WASM_OP_I64_TRUNC_U_F64:
  3043. case WASM_OP_F32_CONVERT_S_I32:
  3044. case WASM_OP_F32_CONVERT_U_I32:
  3045. case WASM_OP_F32_CONVERT_S_I64:
  3046. case WASM_OP_F32_CONVERT_U_I64:
  3047. case WASM_OP_F32_DEMOTE_F64:
  3048. case WASM_OP_F64_CONVERT_S_I32:
  3049. case WASM_OP_F64_CONVERT_U_I32:
  3050. case WASM_OP_F64_CONVERT_S_I64:
  3051. case WASM_OP_F64_CONVERT_U_I64:
  3052. case WASM_OP_F64_PROMOTE_F32:
  3053. case WASM_OP_I32_REINTERPRET_F32:
  3054. case WASM_OP_I64_REINTERPRET_F64:
  3055. case WASM_OP_F32_REINTERPRET_I32:
  3056. case WASM_OP_F64_REINTERPRET_I64:
  3057. case WASM_OP_I32_EXTEND8_S:
  3058. case WASM_OP_I32_EXTEND16_S:
  3059. case WASM_OP_I64_EXTEND8_S:
  3060. case WASM_OP_I64_EXTEND16_S:
  3061. case WASM_OP_I64_EXTEND32_S:
  3062. break;
  3063. case WASM_OP_MISC_PREFIX:
  3064. {
  3065. uint32 opcode1;
  3066. read_leb_uint32(p, p_end, opcode1);
  3067. switch (opcode1) {
  3068. case WASM_OP_I32_TRUNC_SAT_S_F32:
  3069. case WASM_OP_I32_TRUNC_SAT_U_F32:
  3070. case WASM_OP_I32_TRUNC_SAT_S_F64:
  3071. case WASM_OP_I32_TRUNC_SAT_U_F64:
  3072. case WASM_OP_I64_TRUNC_SAT_S_F32:
  3073. case WASM_OP_I64_TRUNC_SAT_U_F32:
  3074. case WASM_OP_I64_TRUNC_SAT_S_F64:
  3075. case WASM_OP_I64_TRUNC_SAT_U_F64:
  3076. break;
  3077. #if WASM_ENABLE_BULK_MEMORY != 0
  3078. case WASM_OP_MEMORY_INIT:
  3079. skip_leb_uint32(p, p_end);
  3080. /* skip memory idx */
  3081. p++;
  3082. break;
  3083. case WASM_OP_DATA_DROP:
  3084. skip_leb_uint32(p, p_end);
  3085. break;
  3086. case WASM_OP_MEMORY_COPY:
  3087. /* skip two memory idx */
  3088. p += 2;
  3089. break;
  3090. case WASM_OP_MEMORY_FILL:
  3091. /* skip memory idx */
  3092. p++;
  3093. break;
  3094. #endif
  3095. #if WASM_ENABLE_REF_TYPES != 0
  3096. case WASM_OP_TABLE_INIT:
  3097. case WASM_OP_TABLE_COPY:
  3098. /* tableidx */
  3099. skip_leb_uint32(p, p_end);
  3100. /* elemidx */
  3101. skip_leb_uint32(p, p_end);
  3102. break;
  3103. case WASM_OP_ELEM_DROP:
  3104. /* elemidx */
  3105. skip_leb_uint32(p, p_end);
  3106. break;
  3107. case WASM_OP_TABLE_SIZE:
  3108. case WASM_OP_TABLE_GROW:
  3109. case WASM_OP_TABLE_FILL:
  3110. skip_leb_uint32(p, p_end); /* table idx */
  3111. break;
  3112. #endif /* WASM_ENABLE_REF_TYPES */
  3113. default:
  3114. bh_assert(0);
  3115. break;
  3116. }
  3117. break;
  3118. }
  3119. #if WASM_ENABLE_SHARED_MEMORY != 0
  3120. case WASM_OP_ATOMIC_PREFIX:
  3121. {
  3122. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  3123. opcode = read_uint8(p);
  3124. if (opcode != WASM_OP_ATOMIC_FENCE) {
  3125. skip_leb_uint32(p, p_end); /* align */
  3126. skip_leb_uint32(p, p_end); /* offset */
  3127. }
  3128. else {
  3129. /* atomic.fence doesn't have memarg */
  3130. p++;
  3131. }
  3132. break;
  3133. }
  3134. #endif
  3135. default:
  3136. bh_assert(0);
  3137. break;
  3138. }
  3139. }
  3140. (void)u8;
  3141. return false;
  3142. }
  3143. #define REF_I32 VALUE_TYPE_I32
  3144. #define REF_F32 VALUE_TYPE_F32
  3145. #define REF_I64_1 VALUE_TYPE_I64
  3146. #define REF_I64_2 VALUE_TYPE_I64
  3147. #define REF_F64_1 VALUE_TYPE_F64
  3148. #define REF_F64_2 VALUE_TYPE_F64
  3149. #define REF_ANY VALUE_TYPE_ANY
  3150. #if WASM_ENABLE_FAST_INTERP != 0
  3151. #if WASM_DEBUG_PREPROCESSOR != 0
  3152. #define LOG_OP(...) os_printf(__VA_ARGS__)
  3153. #else
  3154. #define LOG_OP(...) (void)0
  3155. #endif
  3156. #define PATCH_ELSE 0
  3157. #define PATCH_END 1
  3158. typedef struct BranchBlockPatch {
  3159. struct BranchBlockPatch *next;
  3160. uint8 patch_type;
  3161. uint8 *code_compiled;
  3162. } BranchBlockPatch;
  3163. #endif
  3164. typedef struct BranchBlock {
  3165. uint8 label_type;
  3166. BlockType block_type;
  3167. uint8 *start_addr;
  3168. uint8 *else_addr;
  3169. uint8 *end_addr;
  3170. uint32 stack_cell_num;
  3171. #if WASM_ENABLE_FAST_INTERP != 0
  3172. uint16 dynamic_offset;
  3173. uint8 *code_compiled;
  3174. BranchBlockPatch *patch_list;
  3175. /* This is used to save params frame_offset of of if block */
  3176. int16 *param_frame_offsets;
  3177. #endif
  3178. /* Indicate the operand stack is in polymorphic state.
  3179. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  3180. * to polymorphic state until the block's 'end' opcode is processed.
  3181. * If stack is in polymorphic state and stack is empty, instruction can
  3182. * pop any type of value directly without decreasing stack top pointer
  3183. * and stack cell num. */
  3184. bool is_stack_polymorphic;
  3185. } BranchBlock;
  3186. typedef struct WASMLoaderContext {
  3187. /* frame ref stack */
  3188. uint8 *frame_ref;
  3189. uint8 *frame_ref_bottom;
  3190. uint8 *frame_ref_boundary;
  3191. uint32 frame_ref_size;
  3192. uint32 stack_cell_num;
  3193. uint32 max_stack_cell_num;
  3194. /* frame csp stack */
  3195. BranchBlock *frame_csp;
  3196. BranchBlock *frame_csp_bottom;
  3197. BranchBlock *frame_csp_boundary;
  3198. uint32 frame_csp_size;
  3199. uint32 csp_num;
  3200. uint32 max_csp_num;
  3201. #if WASM_ENABLE_FAST_INTERP != 0
  3202. /* frame offset stack */
  3203. int16 *frame_offset;
  3204. int16 *frame_offset_bottom;
  3205. int16 *frame_offset_boundary;
  3206. uint32 frame_offset_size;
  3207. int16 dynamic_offset;
  3208. int16 start_dynamic_offset;
  3209. int16 max_dynamic_offset;
  3210. /* preserved local offset */
  3211. int16 preserved_local_offset;
  3212. /* const buffer */
  3213. uint8 *const_buf;
  3214. uint16 num_const;
  3215. uint16 const_cell_num;
  3216. uint32 const_buf_size;
  3217. /* processed code */
  3218. uint8 *p_code_compiled;
  3219. uint8 *p_code_compiled_end;
  3220. uint32 code_compiled_size;
  3221. /* If the last opcode will be dropped, the peak memory usage will be larger
  3222. * than the final code_compiled_size, we record the peak size to ensure
  3223. * there will not be invalid memory access during second traverse */
  3224. uint32 code_compiled_peak_size;
  3225. #endif
  3226. } WASMLoaderContext;
  3227. typedef struct Const {
  3228. WASMValue value;
  3229. uint16 slot_index;
  3230. uint8 value_type;
  3231. } Const;
  3232. static void *
  3233. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  3234. uint32 error_buf_size)
  3235. {
  3236. uint8 *mem_new;
  3237. bh_assert(size_new > size_old);
  3238. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  3239. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  3240. memset(mem_new + size_old, 0, size_new - size_old);
  3241. wasm_runtime_free(mem_old);
  3242. }
  3243. return mem_new;
  3244. }
  3245. #define MEM_REALLOC(mem, size_old, size_new) \
  3246. do { \
  3247. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  3248. error_buf_size); \
  3249. if (!mem_new) \
  3250. goto fail; \
  3251. mem = mem_new; \
  3252. } while (0)
  3253. #define CHECK_CSP_PUSH() \
  3254. do { \
  3255. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  3256. MEM_REALLOC( \
  3257. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  3258. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  3259. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  3260. ctx->frame_csp_boundary = \
  3261. ctx->frame_csp_bottom \
  3262. + ctx->frame_csp_size / sizeof(BranchBlock); \
  3263. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  3264. } \
  3265. } while (0)
  3266. #define CHECK_CSP_POP() \
  3267. do { \
  3268. bh_assert(ctx->csp_num >= 1); \
  3269. } while (0)
  3270. #if WASM_ENABLE_FAST_INTERP != 0
  3271. static bool
  3272. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  3273. uint32 error_buf_size)
  3274. {
  3275. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  3276. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  3277. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  3278. ctx->frame_offset_size + 16);
  3279. ctx->frame_offset_size += 16;
  3280. ctx->frame_offset_boundary =
  3281. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  3282. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  3283. }
  3284. return true;
  3285. fail:
  3286. return false;
  3287. }
  3288. static bool
  3289. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  3290. {
  3291. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  3292. return false;
  3293. return true;
  3294. }
  3295. static void
  3296. free_label_patch_list(BranchBlock *frame_csp)
  3297. {
  3298. BranchBlockPatch *label_patch = frame_csp->patch_list;
  3299. BranchBlockPatch *next;
  3300. while (label_patch != NULL) {
  3301. next = label_patch->next;
  3302. wasm_runtime_free(label_patch);
  3303. label_patch = next;
  3304. }
  3305. frame_csp->patch_list = NULL;
  3306. }
  3307. static void
  3308. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  3309. {
  3310. BranchBlock *tmp_csp = frame_csp;
  3311. for (uint32 i = 0; i < csp_num; i++) {
  3312. free_label_patch_list(tmp_csp);
  3313. tmp_csp++;
  3314. }
  3315. }
  3316. #endif
  3317. static bool
  3318. check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
  3319. {
  3320. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  3321. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  3322. ctx->frame_ref_size + 16);
  3323. ctx->frame_ref_size += 16;
  3324. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  3325. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  3326. }
  3327. return true;
  3328. fail:
  3329. return false;
  3330. }
  3331. static bool
  3332. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  3333. char *error_buf, uint32 error_buf_size)
  3334. {
  3335. bh_assert(!((is_32bit_type(type) && stack_cell_num < 1)
  3336. || (is_64bit_type(type) && stack_cell_num < 2)));
  3337. bh_assert(!(
  3338. (type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  3339. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  3340. || (type == VALUE_TYPE_I64
  3341. && (*(frame_ref - 2) != REF_I64_1 || *(frame_ref - 1) != REF_I64_2))
  3342. || (type == VALUE_TYPE_F64
  3343. && (*(frame_ref - 2) != REF_F64_1
  3344. || *(frame_ref - 1) != REF_F64_2))));
  3345. return true;
  3346. }
  3347. static bool
  3348. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3349. uint32 error_buf_size)
  3350. {
  3351. int32 block_stack_cell_num =
  3352. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  3353. if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  3354. /* the stack top is a value of any type, return success */
  3355. return true;
  3356. }
  3357. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
  3358. error_buf, error_buf_size))
  3359. return false;
  3360. return true;
  3361. }
  3362. static void
  3363. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  3364. {
  3365. if (ctx) {
  3366. if (ctx->frame_ref_bottom)
  3367. wasm_runtime_free(ctx->frame_ref_bottom);
  3368. if (ctx->frame_csp_bottom) {
  3369. #if WASM_ENABLE_FAST_INTERP != 0
  3370. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  3371. #endif
  3372. wasm_runtime_free(ctx->frame_csp_bottom);
  3373. }
  3374. #if WASM_ENABLE_FAST_INTERP != 0
  3375. if (ctx->frame_offset_bottom)
  3376. wasm_runtime_free(ctx->frame_offset_bottom);
  3377. if (ctx->const_buf)
  3378. wasm_runtime_free(ctx->const_buf);
  3379. #endif
  3380. wasm_runtime_free(ctx);
  3381. }
  3382. }
  3383. static WASMLoaderContext *
  3384. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  3385. {
  3386. WASMLoaderContext *loader_ctx =
  3387. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  3388. if (!loader_ctx)
  3389. return NULL;
  3390. loader_ctx->frame_ref_size = 32;
  3391. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  3392. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  3393. goto fail;
  3394. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  3395. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  3396. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  3397. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  3398. goto fail;
  3399. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  3400. #if WASM_ENABLE_FAST_INTERP != 0
  3401. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  3402. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  3403. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  3404. error_buf_size)))
  3405. goto fail;
  3406. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  3407. loader_ctx->num_const = 0;
  3408. loader_ctx->const_buf_size = sizeof(Const) * 8;
  3409. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  3410. error_buf, error_buf_size)))
  3411. goto fail;
  3412. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  3413. set_error_buf(error_buf, error_buf_size,
  3414. "fast interpreter offset overflow");
  3415. goto fail;
  3416. }
  3417. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  3418. loader_ctx->max_dynamic_offset =
  3419. func->param_cell_num + func->local_cell_num;
  3420. #endif
  3421. return loader_ctx;
  3422. fail:
  3423. wasm_loader_ctx_destroy(loader_ctx);
  3424. return NULL;
  3425. }
  3426. static bool
  3427. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3428. uint32 error_buf_size)
  3429. {
  3430. if (type == VALUE_TYPE_VOID)
  3431. return true;
  3432. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3433. return false;
  3434. *ctx->frame_ref++ = type;
  3435. ctx->stack_cell_num++;
  3436. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  3437. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3438. if (is_32bit_type(type))
  3439. return true;
  3440. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3441. return false;
  3442. *ctx->frame_ref++ = type;
  3443. ctx->stack_cell_num++;
  3444. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  3445. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3446. bh_assert(ctx->max_stack_cell_num <= UINT16_MAX);
  3447. }
  3448. return true;
  3449. }
  3450. static bool
  3451. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3452. uint32 error_buf_size)
  3453. {
  3454. BranchBlock *cur_block = ctx->frame_csp - 1;
  3455. int32 available_stack_cell =
  3456. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  3457. /* Directly return success if current block is in stack
  3458. * polymorphic state while stack is empty. */
  3459. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  3460. return true;
  3461. if (type == VALUE_TYPE_VOID)
  3462. return true;
  3463. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  3464. return false;
  3465. ctx->frame_ref--;
  3466. ctx->stack_cell_num--;
  3467. if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
  3468. return true;
  3469. ctx->frame_ref--;
  3470. ctx->stack_cell_num--;
  3471. return true;
  3472. }
  3473. static bool
  3474. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  3475. uint8 type_push, uint8 type_pop, char *error_buf,
  3476. uint32 error_buf_size)
  3477. {
  3478. for (int i = 0; i < pop_cnt; i++) {
  3479. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  3480. error_buf_size))
  3481. return false;
  3482. }
  3483. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  3484. return false;
  3485. return true;
  3486. }
  3487. static bool
  3488. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  3489. BlockType block_type, uint8 *start_addr,
  3490. char *error_buf, uint32 error_buf_size)
  3491. {
  3492. CHECK_CSP_PUSH();
  3493. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  3494. ctx->frame_csp->label_type = label_type;
  3495. ctx->frame_csp->block_type = block_type;
  3496. ctx->frame_csp->start_addr = start_addr;
  3497. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  3498. #if WASM_ENABLE_FAST_INTERP != 0
  3499. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  3500. ctx->frame_csp->patch_list = NULL;
  3501. #endif
  3502. ctx->frame_csp++;
  3503. ctx->csp_num++;
  3504. if (ctx->csp_num > ctx->max_csp_num) {
  3505. ctx->max_csp_num = ctx->csp_num;
  3506. bh_assert(ctx->max_csp_num <= UINT16_MAX);
  3507. }
  3508. return true;
  3509. fail:
  3510. return false;
  3511. }
  3512. static bool
  3513. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  3514. uint32 error_buf_size)
  3515. {
  3516. CHECK_CSP_POP();
  3517. #if WASM_ENABLE_FAST_INTERP != 0
  3518. if ((ctx->frame_csp - 1)->param_frame_offsets)
  3519. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  3520. #endif
  3521. ctx->frame_csp--;
  3522. ctx->csp_num--;
  3523. return true;
  3524. }
  3525. #if WASM_ENABLE_FAST_INTERP != 0
  3526. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3527. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  3528. #define emit_label(opcode) \
  3529. do { \
  3530. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  3531. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3532. } while (0)
  3533. #define skip_label() \
  3534. do { \
  3535. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  3536. LOG_OP("\ndelete last op\n"); \
  3537. } while (0)
  3538. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  3539. #define emit_label(opcode) \
  3540. do { \
  3541. int32 offset = \
  3542. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  3543. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  3544. set_error_buf(error_buf, error_buf_size, \
  3545. "pre-compiled label offset out of range"); \
  3546. goto fail; \
  3547. } \
  3548. wasm_loader_emit_int16(loader_ctx, offset); \
  3549. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3550. } while (0)
  3551. #define skip_label() \
  3552. do { \
  3553. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  3554. LOG_OP("\ndelete last op\n"); \
  3555. } while (0)
  3556. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  3557. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  3558. #define emit_label(opcode) \
  3559. do { \
  3560. wasm_loader_emit_uint8(loader_ctx, opcode); \
  3561. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3562. } while (0)
  3563. #define skip_label() \
  3564. do { \
  3565. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  3566. LOG_OP("\ndelete last op\n"); \
  3567. } while (0)
  3568. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  3569. #define emit_empty_label_addr_and_frame_ip(type) \
  3570. do { \
  3571. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  3572. loader_ctx->p_code_compiled, error_buf, \
  3573. error_buf_size)) \
  3574. goto fail; \
  3575. /* label address, to be patched */ \
  3576. wasm_loader_emit_ptr(loader_ctx, NULL); \
  3577. } while (0)
  3578. #define emit_br_info(frame_csp) \
  3579. do { \
  3580. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  3581. error_buf_size)) \
  3582. goto fail; \
  3583. } while (0)
  3584. #define LAST_OP_OUTPUT_I32() \
  3585. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  3586. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  3587. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  3588. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  3589. || (last_op >= WASM_OP_I32_WRAP_I64 \
  3590. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  3591. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  3592. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  3593. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  3594. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  3595. || (last_op == EXT_OP_COPY_STACK_TOP)
  3596. #define LAST_OP_OUTPUT_I64() \
  3597. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  3598. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  3599. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  3600. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  3601. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  3602. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  3603. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  3604. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  3605. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  3606. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  3607. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  3608. #define GET_CONST_OFFSET(type, val) \
  3609. do { \
  3610. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  3611. &operand_offset, error_buf, \
  3612. error_buf_size))) \
  3613. goto fail; \
  3614. } while (0)
  3615. #define GET_CONST_F32_OFFSET(type, fval) \
  3616. do { \
  3617. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  3618. &operand_offset, error_buf, \
  3619. error_buf_size))) \
  3620. goto fail; \
  3621. } while (0)
  3622. #define GET_CONST_F64_OFFSET(type, fval) \
  3623. do { \
  3624. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  3625. &operand_offset, error_buf, \
  3626. error_buf_size))) \
  3627. goto fail; \
  3628. } while (0)
  3629. #define emit_operand(ctx, offset) \
  3630. do { \
  3631. wasm_loader_emit_int16(ctx, offset); \
  3632. LOG_OP("%d\t", offset); \
  3633. } while (0)
  3634. #define emit_byte(ctx, byte) \
  3635. do { \
  3636. wasm_loader_emit_uint8(ctx, byte); \
  3637. LOG_OP("%d\t", byte); \
  3638. } while (0)
  3639. #define emit_uint32(ctx, value) \
  3640. do { \
  3641. wasm_loader_emit_uint32(ctx, value); \
  3642. LOG_OP("%d\t", value); \
  3643. } while (0)
  3644. #define emit_uint64(ctx, value) \
  3645. do { \
  3646. wasm_loader_emit_const(ctx, &value, false); \
  3647. LOG_OP("%lld\t", value); \
  3648. } while (0)
  3649. #define emit_float32(ctx, value) \
  3650. do { \
  3651. wasm_loader_emit_const(ctx, &value, true); \
  3652. LOG_OP("%f\t", value); \
  3653. } while (0)
  3654. #define emit_float64(ctx, value) \
  3655. do { \
  3656. wasm_loader_emit_const(ctx, &value, false); \
  3657. LOG_OP("%f\t", value); \
  3658. } while (0)
  3659. static bool
  3660. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  3661. {
  3662. if (!(ctx->p_code_compiled =
  3663. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  3664. return false;
  3665. ctx->p_code_compiled_end =
  3666. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  3667. /* clean up frame ref */
  3668. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  3669. ctx->frame_ref = ctx->frame_ref_bottom;
  3670. ctx->stack_cell_num = 0;
  3671. /* clean up frame csp */
  3672. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  3673. ctx->frame_csp = ctx->frame_csp_bottom;
  3674. ctx->csp_num = 0;
  3675. ctx->max_csp_num = 0;
  3676. /* clean up frame offset */
  3677. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  3678. ctx->frame_offset = ctx->frame_offset_bottom;
  3679. ctx->dynamic_offset = ctx->start_dynamic_offset;
  3680. /* init preserved local offsets */
  3681. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  3682. /* const buf is reserved */
  3683. return true;
  3684. }
  3685. static void
  3686. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  3687. {
  3688. ctx->code_compiled_size += size;
  3689. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  3690. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  3691. }
  3692. }
  3693. static void
  3694. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  3695. {
  3696. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  3697. if (ctx->p_code_compiled) {
  3698. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3699. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3700. #endif
  3701. bh_memcpy_s(ctx->p_code_compiled,
  3702. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  3703. value, size);
  3704. ctx->p_code_compiled += size;
  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, size);
  3711. }
  3712. }
  3713. static void
  3714. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 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_U32(ctx->p_code_compiled, value);
  3721. ctx->p_code_compiled += sizeof(uint32);
  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(uint32));
  3728. }
  3729. }
  3730. static void
  3731. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  3732. {
  3733. if (ctx->p_code_compiled) {
  3734. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3735. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3736. #endif
  3737. STORE_U16(ctx->p_code_compiled, (uint16)value);
  3738. ctx->p_code_compiled += sizeof(int16);
  3739. }
  3740. else {
  3741. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3742. bh_assert((ctx->code_compiled_size & 1) == 0);
  3743. #endif
  3744. increase_compiled_code_space(ctx, sizeof(uint16));
  3745. }
  3746. }
  3747. static void
  3748. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  3749. {
  3750. if (ctx->p_code_compiled) {
  3751. *(ctx->p_code_compiled) = value;
  3752. ctx->p_code_compiled += sizeof(uint8);
  3753. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3754. ctx->p_code_compiled++;
  3755. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3756. #endif
  3757. }
  3758. else {
  3759. increase_compiled_code_space(ctx, sizeof(uint8));
  3760. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3761. increase_compiled_code_space(ctx, sizeof(uint8));
  3762. bh_assert((ctx->code_compiled_size & 1) == 0);
  3763. #endif
  3764. }
  3765. }
  3766. static void
  3767. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  3768. {
  3769. if (ctx->p_code_compiled) {
  3770. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3771. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3772. #endif
  3773. STORE_PTR(ctx->p_code_compiled, value);
  3774. ctx->p_code_compiled += sizeof(void *);
  3775. }
  3776. else {
  3777. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3778. bh_assert((ctx->code_compiled_size & 1) == 0);
  3779. #endif
  3780. increase_compiled_code_space(ctx, sizeof(void *));
  3781. }
  3782. }
  3783. static void
  3784. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  3785. {
  3786. if (ctx->p_code_compiled) {
  3787. ctx->p_code_compiled -= size;
  3788. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3789. if (size == sizeof(uint8)) {
  3790. ctx->p_code_compiled--;
  3791. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3792. }
  3793. #endif
  3794. }
  3795. else {
  3796. ctx->code_compiled_size -= size;
  3797. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3798. if (size == sizeof(uint8)) {
  3799. ctx->code_compiled_size--;
  3800. bh_assert((ctx->code_compiled_size & 1) == 0);
  3801. }
  3802. #endif
  3803. }
  3804. }
  3805. static bool
  3806. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  3807. uint32 local_index, uint32 local_type,
  3808. bool *preserved, char *error_buf,
  3809. uint32 error_buf_size)
  3810. {
  3811. uint32 i = 0;
  3812. int16 preserved_offset = (int16)local_index;
  3813. *preserved = false;
  3814. while (i < loader_ctx->stack_cell_num) {
  3815. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  3816. /* move previous local into dynamic space before a set/tee_local opcode
  3817. */
  3818. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  3819. if (!(*preserved)) {
  3820. *preserved = true;
  3821. skip_label();
  3822. preserved_offset = loader_ctx->preserved_local_offset;
  3823. if (loader_ctx->p_code_compiled) {
  3824. bh_assert(preserved_offset != (int16)local_index);
  3825. }
  3826. if (is_32bit_type(local_type)) {
  3827. /* Only increase preserve offset in the second traversal */
  3828. if (loader_ctx->p_code_compiled)
  3829. loader_ctx->preserved_local_offset++;
  3830. emit_label(EXT_OP_COPY_STACK_TOP);
  3831. }
  3832. else {
  3833. if (loader_ctx->p_code_compiled)
  3834. loader_ctx->preserved_local_offset += 2;
  3835. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  3836. }
  3837. emit_operand(loader_ctx, local_index);
  3838. emit_operand(loader_ctx, preserved_offset);
  3839. emit_label(opcode);
  3840. }
  3841. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  3842. }
  3843. if (is_32bit_type(cur_type))
  3844. i++;
  3845. else
  3846. i += 2;
  3847. }
  3848. return true;
  3849. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3850. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3851. fail:
  3852. return false;
  3853. #endif
  3854. #endif
  3855. }
  3856. static bool
  3857. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  3858. char *error_buf, uint32 error_buf_size)
  3859. {
  3860. uint32 i = 0;
  3861. bool preserve_local;
  3862. /* preserve locals before blocks to ensure that "tee/set_local" inside
  3863. blocks will not influence the value of these locals */
  3864. while (i < loader_ctx->stack_cell_num) {
  3865. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  3866. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  3867. if ((cur_offset < loader_ctx->start_dynamic_offset)
  3868. && (cur_offset >= 0)) {
  3869. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  3870. cur_type, &preserve_local,
  3871. error_buf, error_buf_size)))
  3872. return false;
  3873. }
  3874. if (is_32bit_type(cur_type == VALUE_TYPE_I32)) {
  3875. i++;
  3876. }
  3877. else {
  3878. i += 2;
  3879. }
  3880. }
  3881. return true;
  3882. }
  3883. static bool
  3884. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  3885. uint8 *p_code_compiled, char *error_buf,
  3886. uint32 error_buf_size)
  3887. {
  3888. BranchBlockPatch *patch =
  3889. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  3890. if (!patch) {
  3891. return false;
  3892. }
  3893. patch->patch_type = patch_type;
  3894. patch->code_compiled = p_code_compiled;
  3895. if (!frame_csp->patch_list) {
  3896. frame_csp->patch_list = patch;
  3897. patch->next = NULL;
  3898. }
  3899. else {
  3900. patch->next = frame_csp->patch_list;
  3901. frame_csp->patch_list = patch;
  3902. }
  3903. return true;
  3904. }
  3905. static void
  3906. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  3907. {
  3908. BranchBlock *frame_csp = ctx->frame_csp - depth;
  3909. BranchBlockPatch *node = frame_csp->patch_list;
  3910. BranchBlockPatch *node_prev = NULL, *node_next;
  3911. if (!ctx->p_code_compiled)
  3912. return;
  3913. while (node) {
  3914. node_next = node->next;
  3915. if (node->patch_type == patch_type) {
  3916. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  3917. if (node_prev == NULL) {
  3918. frame_csp->patch_list = node_next;
  3919. }
  3920. else {
  3921. node_prev->next = node_next;
  3922. }
  3923. wasm_runtime_free(node);
  3924. }
  3925. else {
  3926. node_prev = node;
  3927. }
  3928. node = node_next;
  3929. }
  3930. }
  3931. static bool
  3932. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  3933. char *error_buf, uint32 error_buf_size)
  3934. {
  3935. /* br info layout:
  3936. * a) arity of target block
  3937. * b) total cell num of arity values
  3938. * c) each arity value's cell num
  3939. * d) each arity value's src frame offset
  3940. * e) each arity values's dst dynamic offset
  3941. * f) branch target address
  3942. *
  3943. * Note: b-e are omitted when arity is 0 so that
  3944. * interpreter can recover the br info quickly.
  3945. */
  3946. BlockType *block_type = &frame_csp->block_type;
  3947. uint8 *types = NULL, cell;
  3948. uint32 arity = 0;
  3949. int32 i;
  3950. int16 *frame_offset = ctx->frame_offset;
  3951. uint16 dynamic_offset;
  3952. /* Note: loop's arity is different from if and block. loop's arity is
  3953. * its parameter count while if and block arity is result count.
  3954. */
  3955. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  3956. arity = block_type_get_param_types(block_type, &types);
  3957. else
  3958. arity = block_type_get_result_types(block_type, &types);
  3959. /* Part a */
  3960. emit_uint32(ctx, arity);
  3961. if (arity) {
  3962. /* Part b */
  3963. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  3964. /* Part c */
  3965. for (i = (int32)arity - 1; i >= 0; i--) {
  3966. cell = (uint8)wasm_value_type_cell_num(types[i]);
  3967. emit_byte(ctx, cell);
  3968. }
  3969. /* Part d */
  3970. for (i = (int32)arity - 1; i >= 0; i--) {
  3971. cell = (uint8)wasm_value_type_cell_num(types[i]);
  3972. frame_offset -= cell;
  3973. emit_operand(ctx, *(int16 *)(frame_offset));
  3974. }
  3975. /* Part e */
  3976. dynamic_offset =
  3977. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  3978. for (i = (int32)arity - 1; i >= 0; i--) {
  3979. cell = (uint8)wasm_value_type_cell_num(types[i]);
  3980. dynamic_offset -= cell;
  3981. emit_operand(ctx, dynamic_offset);
  3982. }
  3983. }
  3984. /* Part f */
  3985. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  3986. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  3987. }
  3988. else {
  3989. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  3990. error_buf, error_buf_size))
  3991. return false;
  3992. /* label address, to be patched */
  3993. wasm_loader_emit_ptr(ctx, NULL);
  3994. }
  3995. return true;
  3996. }
  3997. static bool
  3998. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  3999. bool disable_emit, int16 operand_offset,
  4000. char *error_buf, uint32 error_buf_size)
  4001. {
  4002. if (type == VALUE_TYPE_VOID)
  4003. return true;
  4004. /* only check memory overflow in first traverse */
  4005. if (ctx->p_code_compiled == NULL) {
  4006. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4007. return false;
  4008. }
  4009. if (disable_emit)
  4010. *(ctx->frame_offset)++ = operand_offset;
  4011. else {
  4012. emit_operand(ctx, ctx->dynamic_offset);
  4013. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  4014. ctx->dynamic_offset++;
  4015. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4016. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4017. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  4018. }
  4019. }
  4020. if (is_32bit_type(type))
  4021. return true;
  4022. if (ctx->p_code_compiled == NULL) {
  4023. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4024. return false;
  4025. }
  4026. ctx->frame_offset++;
  4027. if (!disable_emit) {
  4028. ctx->dynamic_offset++;
  4029. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4030. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4031. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  4032. }
  4033. }
  4034. return true;
  4035. }
  4036. /* This function should be in front of wasm_loader_pop_frame_ref
  4037. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  4038. will be modified by wasm_loader_pop_frame_ref */
  4039. static bool
  4040. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4041. char *error_buf, uint32 error_buf_size)
  4042. {
  4043. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  4044. then current block is the function block */
  4045. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  4046. BranchBlock *cur_block = ctx->frame_csp - depth;
  4047. int32 available_stack_cell =
  4048. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4049. /* Directly return success if current block is in stack
  4050. * polymorphic state while stack is empty. */
  4051. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4052. return true;
  4053. if (type == VALUE_TYPE_VOID)
  4054. return true;
  4055. if (is_32bit_type(type)) {
  4056. /* Check the offset stack bottom to ensure the frame offset
  4057. stack will not go underflow. But we don't thrown error
  4058. and return true here, because the error msg should be
  4059. given in wasm_loader_pop_frame_ref */
  4060. if (!check_offset_pop(ctx, 1))
  4061. return true;
  4062. ctx->frame_offset -= 1;
  4063. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4064. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4065. ctx->dynamic_offset -= 1;
  4066. }
  4067. else {
  4068. if (!check_offset_pop(ctx, 2))
  4069. return true;
  4070. ctx->frame_offset -= 2;
  4071. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4072. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4073. ctx->dynamic_offset -= 2;
  4074. }
  4075. emit_operand(ctx, *(ctx->frame_offset));
  4076. return true;
  4077. }
  4078. static bool
  4079. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  4080. uint8 type_push, uint8 type_pop,
  4081. bool disable_emit, int16 operand_offset,
  4082. char *error_buf, uint32 error_buf_size)
  4083. {
  4084. for (int i = 0; i < pop_cnt; i++) {
  4085. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  4086. error_buf_size))
  4087. return false;
  4088. }
  4089. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  4090. operand_offset, error_buf,
  4091. error_buf_size))
  4092. return false;
  4093. return true;
  4094. }
  4095. static bool
  4096. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4097. bool disable_emit, int16 operand_offset,
  4098. char *error_buf, uint32 error_buf_size)
  4099. {
  4100. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  4101. error_buf, error_buf_size)))
  4102. return false;
  4103. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  4104. return false;
  4105. return true;
  4106. }
  4107. static bool
  4108. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4109. char *error_buf, uint32 error_buf_size)
  4110. {
  4111. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  4112. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  4113. return false;
  4114. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  4115. return false;
  4116. return true;
  4117. }
  4118. static bool
  4119. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  4120. uint8 type_push, uint8 type_pop,
  4121. bool disable_emit, int16 operand_offset,
  4122. char *error_buf, uint32 error_buf_size)
  4123. {
  4124. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  4125. disable_emit, operand_offset,
  4126. error_buf, error_buf_size))
  4127. return false;
  4128. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  4129. error_buf, error_buf_size))
  4130. return false;
  4131. return true;
  4132. }
  4133. static bool
  4134. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  4135. int16 *offset, char *error_buf,
  4136. uint32 error_buf_size)
  4137. {
  4138. int8 bytes_to_increase;
  4139. int16 operand_offset = 0;
  4140. Const *c;
  4141. /* Search existing constant */
  4142. for (c = (Const *)ctx->const_buf;
  4143. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  4144. if ((type == c->value_type)
  4145. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  4146. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  4147. #if WASM_ENABLE_REF_TYPES != 0
  4148. || (type == VALUE_TYPE_FUNCREF
  4149. && *(int32 *)value == c->value.i32)
  4150. || (type == VALUE_TYPE_EXTERNREF
  4151. && *(int32 *)value == c->value.i32)
  4152. #endif
  4153. || (type == VALUE_TYPE_F64
  4154. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  4155. || (type == VALUE_TYPE_F32
  4156. && (0
  4157. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  4158. operand_offset = c->slot_index;
  4159. break;
  4160. }
  4161. if (c->value_type == VALUE_TYPE_I64 || c->value_type == VALUE_TYPE_F64)
  4162. operand_offset += 2;
  4163. else
  4164. operand_offset += 1;
  4165. }
  4166. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  4167. /* New constant, append to the const buffer */
  4168. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  4169. bytes_to_increase = 2;
  4170. }
  4171. else {
  4172. bytes_to_increase = 1;
  4173. }
  4174. /* The max cell num of const buffer is 32768 since the valid index range
  4175. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  4176. * full */
  4177. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  4178. *offset = 0;
  4179. return true;
  4180. }
  4181. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  4182. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  4183. ctx->const_buf_size + 4 * sizeof(Const));
  4184. ctx->const_buf_size += 4 * sizeof(Const);
  4185. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  4186. }
  4187. c->value_type = type;
  4188. switch (type) {
  4189. case VALUE_TYPE_F64:
  4190. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  4191. sizeof(float64));
  4192. ctx->const_cell_num += 2;
  4193. /* The const buf will be reversed, we use the second cell */
  4194. /* of the i64/f64 const so the finnal offset is corrent */
  4195. operand_offset++;
  4196. break;
  4197. case VALUE_TYPE_I64:
  4198. c->value.i64 = *(int64 *)value;
  4199. ctx->const_cell_num += 2;
  4200. operand_offset++;
  4201. break;
  4202. case VALUE_TYPE_F32:
  4203. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  4204. sizeof(float32));
  4205. ctx->const_cell_num++;
  4206. break;
  4207. case VALUE_TYPE_I32:
  4208. c->value.i32 = *(int32 *)value;
  4209. ctx->const_cell_num++;
  4210. break;
  4211. #if WASM_ENABLE_REF_TYPES != 0
  4212. case VALUE_TYPE_EXTERNREF:
  4213. case VALUE_TYPE_FUNCREF:
  4214. c->value.i32 = *(int32 *)value;
  4215. ctx->const_cell_num++;
  4216. break;
  4217. #endif
  4218. default:
  4219. break;
  4220. }
  4221. c->slot_index = operand_offset;
  4222. ctx->num_const++;
  4223. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  4224. (int64)c->value.i64);
  4225. }
  4226. /* use negetive index for const */
  4227. operand_offset = -(operand_offset + 1);
  4228. *offset = operand_offset;
  4229. return true;
  4230. fail:
  4231. return false;
  4232. }
  4233. /*
  4234. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  4235. -- Mostly used for the binary / compare operation
  4236. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  4237. -- Mostly used in block / control instructions
  4238. The POP will always emit the offset on the top of the frame_offset stack
  4239. PUSH can be used in two ways:
  4240. 1. directly PUSH:
  4241. PUSH_XXX();
  4242. will allocate a dynamic space and emit
  4243. 2. silent PUSH:
  4244. operand_offset = xxx; disable_emit = true;
  4245. PUSH_XXX();
  4246. only push the frame_offset stack, no emit
  4247. */
  4248. #define PUSH_I32() \
  4249. do { \
  4250. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4251. disable_emit, operand_offset, \
  4252. error_buf, error_buf_size)) \
  4253. goto fail; \
  4254. } while (0)
  4255. #define PUSH_F32() \
  4256. do { \
  4257. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4258. disable_emit, operand_offset, \
  4259. error_buf, error_buf_size)) \
  4260. goto fail; \
  4261. } while (0)
  4262. #define PUSH_I64() \
  4263. do { \
  4264. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4265. disable_emit, operand_offset, \
  4266. error_buf, error_buf_size)) \
  4267. goto fail; \
  4268. } while (0)
  4269. #define PUSH_F64() \
  4270. do { \
  4271. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4272. disable_emit, operand_offset, \
  4273. error_buf, error_buf_size)) \
  4274. goto fail; \
  4275. } while (0)
  4276. #define PUSH_FUNCREF() \
  4277. do { \
  4278. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_FUNCREF, \
  4279. disable_emit, operand_offset, \
  4280. error_buf, error_buf_size)) \
  4281. goto fail; \
  4282. } while (0)
  4283. #define POP_I32() \
  4284. do { \
  4285. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4286. error_buf, error_buf_size)) \
  4287. goto fail; \
  4288. } while (0)
  4289. #define POP_F32() \
  4290. do { \
  4291. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4292. error_buf, error_buf_size)) \
  4293. goto fail; \
  4294. } while (0)
  4295. #define POP_I64() \
  4296. do { \
  4297. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4298. error_buf, error_buf_size)) \
  4299. goto fail; \
  4300. } while (0)
  4301. #define POP_F64() \
  4302. do { \
  4303. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4304. error_buf, error_buf_size)) \
  4305. goto fail; \
  4306. } while (0)
  4307. #define PUSH_OFFSET_TYPE(type) \
  4308. do { \
  4309. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  4310. operand_offset, error_buf, \
  4311. error_buf_size))) \
  4312. goto fail; \
  4313. } while (0)
  4314. #define POP_OFFSET_TYPE(type) \
  4315. do { \
  4316. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  4317. error_buf_size))) \
  4318. goto fail; \
  4319. } while (0)
  4320. #define POP_AND_PUSH(type_pop, type_push) \
  4321. do { \
  4322. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4323. loader_ctx, 1, type_push, type_pop, disable_emit, \
  4324. operand_offset, error_buf, error_buf_size))) \
  4325. goto fail; \
  4326. } while (0)
  4327. /* type of POPs should be the same */
  4328. #define POP2_AND_PUSH(type_pop, type_push) \
  4329. do { \
  4330. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4331. loader_ctx, 2, type_push, type_pop, disable_emit, \
  4332. operand_offset, error_buf, error_buf_size))) \
  4333. goto fail; \
  4334. } while (0)
  4335. #else /* WASM_ENABLE_FAST_INTERP */
  4336. #define PUSH_I32() \
  4337. do { \
  4338. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  4339. error_buf, error_buf_size))) \
  4340. goto fail; \
  4341. } while (0)
  4342. #define PUSH_F32() \
  4343. do { \
  4344. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  4345. error_buf, error_buf_size))) \
  4346. goto fail; \
  4347. } while (0)
  4348. #define PUSH_I64() \
  4349. do { \
  4350. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  4351. error_buf, error_buf_size))) \
  4352. goto fail; \
  4353. } while (0)
  4354. #define PUSH_F64() \
  4355. do { \
  4356. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  4357. error_buf, error_buf_size))) \
  4358. goto fail; \
  4359. } while (0)
  4360. #define PUSH_FUNCREF() \
  4361. do { \
  4362. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  4363. error_buf, error_buf_size))) \
  4364. goto fail; \
  4365. } while (0)
  4366. #define POP_I32() \
  4367. do { \
  4368. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, error_buf, \
  4369. error_buf_size))) \
  4370. goto fail; \
  4371. } while (0)
  4372. #define POP_F32() \
  4373. do { \
  4374. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, error_buf, \
  4375. error_buf_size))) \
  4376. goto fail; \
  4377. } while (0)
  4378. #define POP_I64() \
  4379. do { \
  4380. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, error_buf, \
  4381. error_buf_size))) \
  4382. goto fail; \
  4383. } while (0)
  4384. #define POP_F64() \
  4385. do { \
  4386. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, error_buf, \
  4387. error_buf_size))) \
  4388. goto fail; \
  4389. } while (0)
  4390. #define POP_FUNCREF() \
  4391. do { \
  4392. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  4393. error_buf, error_buf_size))) \
  4394. goto fail; \
  4395. } while (0)
  4396. #define POP_AND_PUSH(type_pop, type_push) \
  4397. do { \
  4398. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  4399. type_pop, error_buf, \
  4400. error_buf_size))) \
  4401. goto fail; \
  4402. } while (0)
  4403. /* type of POPs should be the same */
  4404. #define POP2_AND_PUSH(type_pop, type_push) \
  4405. do { \
  4406. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  4407. type_pop, error_buf, \
  4408. error_buf_size))) \
  4409. goto fail; \
  4410. } while (0)
  4411. #endif /* WASM_ENABLE_FAST_INTERP */
  4412. #if WASM_ENABLE_FAST_INTERP != 0
  4413. static bool
  4414. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  4415. bool disable_emit, char *error_buf, uint32 error_buf_size)
  4416. {
  4417. int16 operand_offset = 0;
  4418. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  4419. : loader_ctx->frame_csp;
  4420. BlockType *block_type = &block->block_type;
  4421. uint8 *return_types = NULL;
  4422. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  4423. int32 i = 0;
  4424. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  4425. *frame_offset_org = NULL;
  4426. return_count = block_type_get_result_types(block_type, &return_types);
  4427. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  4428. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  4429. if (return_count == 1) {
  4430. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  4431. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  4432. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  4433. /* insert op_copy before else opcode */
  4434. if (opcode == WASM_OP_ELSE)
  4435. skip_label();
  4436. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  4437. : EXT_OP_COPY_STACK_TOP_I64);
  4438. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  4439. emit_operand(loader_ctx, block->dynamic_offset);
  4440. if (opcode == WASM_OP_ELSE) {
  4441. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  4442. }
  4443. else {
  4444. loader_ctx->frame_offset -= cell;
  4445. loader_ctx->dynamic_offset = block->dynamic_offset;
  4446. PUSH_OFFSET_TYPE(return_types[0]);
  4447. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4448. }
  4449. if (opcode == WASM_OP_ELSE)
  4450. emit_label(opcode);
  4451. }
  4452. return true;
  4453. }
  4454. /* Copy stack top values to block's results which are in dynamic space.
  4455. * The instruction format:
  4456. * Part a: values count
  4457. * Part b: all values total cell num
  4458. * Part c: each value's cell_num, src offset and dst offset
  4459. * Part d: each value's src offset and dst offset
  4460. * Part e: each value's dst offset
  4461. */
  4462. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  4463. dynamic_offset = dynamic_offset_org =
  4464. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  4465. /* First traversal to get the count of values needed to be copied. */
  4466. for (i = (int32)return_count - 1; i >= 0; i--) {
  4467. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  4468. frame_offset -= cells;
  4469. dynamic_offset -= cells;
  4470. if (dynamic_offset != *frame_offset) {
  4471. value_count++;
  4472. total_cel_num += cells;
  4473. }
  4474. }
  4475. if (value_count) {
  4476. uint32 j = 0;
  4477. uint8 *emit_data = NULL, *cells = NULL;
  4478. int16 *src_offsets = NULL;
  4479. uint16 *dst_offsets = NULL;
  4480. uint64 size =
  4481. (uint64)value_count
  4482. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  4483. /* Allocate memory for the emit data */
  4484. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4485. return false;
  4486. cells = emit_data;
  4487. src_offsets = (int16 *)(cells + value_count);
  4488. dst_offsets = (uint16 *)(src_offsets + value_count);
  4489. /* insert op_copy before else opcode */
  4490. if (opcode == WASM_OP_ELSE)
  4491. skip_label();
  4492. emit_label(EXT_OP_COPY_STACK_VALUES);
  4493. /* Part a) */
  4494. emit_uint32(loader_ctx, value_count);
  4495. /* Part b) */
  4496. emit_uint32(loader_ctx, total_cel_num);
  4497. /* Second traversal to get each value's cell num, src offset and dst
  4498. * offset. */
  4499. frame_offset = frame_offset_org;
  4500. dynamic_offset = dynamic_offset_org;
  4501. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  4502. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  4503. frame_offset -= cell;
  4504. dynamic_offset -= cell;
  4505. if (dynamic_offset != *frame_offset) {
  4506. /* cell num */
  4507. cells[j] = cell;
  4508. /* src offset */
  4509. src_offsets[j] = *frame_offset;
  4510. /* dst offset */
  4511. dst_offsets[j] = dynamic_offset;
  4512. j++;
  4513. }
  4514. if (opcode == WASM_OP_ELSE) {
  4515. *frame_offset = dynamic_offset;
  4516. }
  4517. else {
  4518. loader_ctx->frame_offset = frame_offset;
  4519. loader_ctx->dynamic_offset = dynamic_offset;
  4520. PUSH_OFFSET_TYPE(return_types[i]);
  4521. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4522. loader_ctx->frame_offset = frame_offset_org;
  4523. loader_ctx->dynamic_offset = dynamic_offset_org;
  4524. }
  4525. }
  4526. bh_assert(j == value_count);
  4527. /* Emit the cells, src_offsets and dst_offsets */
  4528. for (j = 0; j < value_count; j++)
  4529. emit_byte(loader_ctx, cells[j]);
  4530. for (j = 0; j < value_count; j++)
  4531. emit_operand(loader_ctx, src_offsets[j]);
  4532. for (j = 0; j < value_count; j++)
  4533. emit_operand(loader_ctx, dst_offsets[j]);
  4534. if (opcode == WASM_OP_ELSE)
  4535. emit_label(opcode);
  4536. wasm_runtime_free(emit_data);
  4537. }
  4538. return true;
  4539. fail:
  4540. return false;
  4541. }
  4542. #endif /* WASM_ENABLE_FAST_INTERP */
  4543. #define RESERVE_BLOCK_RET() \
  4544. do { \
  4545. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  4546. error_buf_size)) \
  4547. goto fail; \
  4548. } while (0)
  4549. #define PUSH_TYPE(type) \
  4550. do { \
  4551. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  4552. error_buf_size))) \
  4553. goto fail; \
  4554. } while (0)
  4555. #define POP_TYPE(type) \
  4556. do { \
  4557. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  4558. error_buf_size))) \
  4559. goto fail; \
  4560. } while (0)
  4561. #define PUSH_CSP(label_type, block_type, _start_addr) \
  4562. do { \
  4563. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  4564. _start_addr, error_buf, \
  4565. error_buf_size)) \
  4566. goto fail; \
  4567. } while (0)
  4568. #define POP_CSP() \
  4569. do { \
  4570. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  4571. goto fail; \
  4572. } while (0)
  4573. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  4574. do { \
  4575. read_leb_uint32(p, p_end, local_idx); \
  4576. bh_assert(local_idx < param_count + local_count); \
  4577. local_type = local_idx < param_count \
  4578. ? param_types[local_idx] \
  4579. : local_types[local_idx - param_count]; \
  4580. local_offset = local_offsets[local_idx]; \
  4581. } while (0)
  4582. #define CHECK_BR(depth) \
  4583. do { \
  4584. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  4585. error_buf_size)) \
  4586. goto fail; \
  4587. } while (0)
  4588. #define CHECK_MEMORY() \
  4589. do { \
  4590. bh_assert(module->import_memory_count + module->memory_count > 0); \
  4591. } while (0)
  4592. static bool
  4593. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  4594. char *error_buf, uint32 error_buf_size)
  4595. {
  4596. BranchBlock *target_block, *cur_block;
  4597. BlockType *target_block_type;
  4598. uint8 *types = NULL, *frame_ref;
  4599. uint32 arity = 0;
  4600. int32 i, available_stack_cell;
  4601. uint16 cell_num;
  4602. if (loader_ctx->csp_num < depth + 1) {
  4603. set_error_buf(error_buf, error_buf_size,
  4604. "unknown label, "
  4605. "unexpected end of section or function");
  4606. return false;
  4607. }
  4608. cur_block = loader_ctx->frame_csp - 1;
  4609. target_block = loader_ctx->frame_csp - (depth + 1);
  4610. target_block_type = &target_block->block_type;
  4611. frame_ref = loader_ctx->frame_ref;
  4612. /* Note: loop's arity is different from if and block. loop's arity is
  4613. * its parameter count while if and block arity is result count.
  4614. */
  4615. if (target_block->label_type == LABEL_TYPE_LOOP)
  4616. arity = block_type_get_param_types(target_block_type, &types);
  4617. else
  4618. arity = block_type_get_result_types(target_block_type, &types);
  4619. /* If the stack is in polymorphic state, just clear the stack
  4620. * and then re-push the values to make the stack top values
  4621. * match block type. */
  4622. if (cur_block->is_stack_polymorphic) {
  4623. for (i = (int32)arity - 1; i >= 0; i--) {
  4624. #if WASM_ENABLE_FAST_INTERP != 0
  4625. POP_OFFSET_TYPE(types[i]);
  4626. #endif
  4627. POP_TYPE(types[i]);
  4628. }
  4629. for (i = 0; i < (int32)arity; i++) {
  4630. #if WASM_ENABLE_FAST_INTERP != 0
  4631. bool disable_emit = true;
  4632. int16 operand_offset = 0;
  4633. PUSH_OFFSET_TYPE(types[i]);
  4634. #endif
  4635. PUSH_TYPE(types[i]);
  4636. }
  4637. return true;
  4638. }
  4639. available_stack_cell =
  4640. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  4641. /* Check stack top values match target block type */
  4642. for (i = (int32)arity - 1; i >= 0; i--) {
  4643. if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
  4644. error_buf, error_buf_size))
  4645. return false;
  4646. cell_num = wasm_value_type_cell_num(types[i]);
  4647. frame_ref -= cell_num;
  4648. available_stack_cell -= cell_num;
  4649. }
  4650. return true;
  4651. fail:
  4652. return false;
  4653. }
  4654. static BranchBlock *
  4655. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  4656. char *error_buf, uint32 error_buf_size)
  4657. {
  4658. uint8 *p = *p_buf, *p_end = buf_end;
  4659. BranchBlock *frame_csp_tmp;
  4660. uint32 depth;
  4661. read_leb_uint32(p, p_end, depth);
  4662. CHECK_BR(depth);
  4663. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  4664. #if WASM_ENABLE_FAST_INTERP != 0
  4665. emit_br_info(frame_csp_tmp);
  4666. #endif
  4667. *p_buf = p;
  4668. return frame_csp_tmp;
  4669. fail:
  4670. return NULL;
  4671. }
  4672. static bool
  4673. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  4674. char *error_buf, uint32 error_buf_size)
  4675. {
  4676. BlockType *block_type = &block->block_type;
  4677. uint8 *return_types = NULL;
  4678. uint32 return_count = 0;
  4679. int32 available_stack_cell, return_cell_num, i;
  4680. uint8 *frame_ref = NULL;
  4681. available_stack_cell =
  4682. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  4683. return_count = block_type_get_result_types(block_type, &return_types);
  4684. return_cell_num =
  4685. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  4686. /* If the stack is in polymorphic state, just clear the stack
  4687. * and then re-push the values to make the stack top values
  4688. * match block type. */
  4689. if (block->is_stack_polymorphic) {
  4690. for (i = (int32)return_count - 1; i >= 0; i--) {
  4691. #if WASM_ENABLE_FAST_INTERP != 0
  4692. POP_OFFSET_TYPE(return_types[i]);
  4693. #endif
  4694. POP_TYPE(return_types[i]);
  4695. }
  4696. /* Check stack is empty */
  4697. bh_assert(loader_ctx->stack_cell_num == block->stack_cell_num);
  4698. for (i = 0; i < (int32)return_count; i++) {
  4699. #if WASM_ENABLE_FAST_INTERP != 0
  4700. bool disable_emit = true;
  4701. int16 operand_offset = 0;
  4702. PUSH_OFFSET_TYPE(return_types[i]);
  4703. #endif
  4704. PUSH_TYPE(return_types[i]);
  4705. }
  4706. return true;
  4707. }
  4708. /* Check stack cell num equals return cell num */
  4709. bh_assert(available_stack_cell == return_cell_num);
  4710. /* Check stack values match return types */
  4711. frame_ref = loader_ctx->frame_ref;
  4712. for (i = (int32)return_count - 1; i >= 0; i--) {
  4713. if (!check_stack_top_values(frame_ref, available_stack_cell,
  4714. return_types[i], error_buf, error_buf_size))
  4715. return false;
  4716. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  4717. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  4718. }
  4719. (void)return_cell_num;
  4720. return true;
  4721. fail:
  4722. return false;
  4723. }
  4724. #if WASM_ENABLE_FAST_INTERP != 0
  4725. /* Copy parameters to dynamic space.
  4726. * 1) POP original parameter out;
  4727. * 2) Push and copy original values to dynamic space.
  4728. * The copy instruction format:
  4729. * Part a: param count
  4730. * Part b: all param total cell num
  4731. * Part c: each param's cell_num, src offset and dst offset
  4732. * Part d: each param's src offset
  4733. * Part e: each param's dst offset
  4734. */
  4735. static bool
  4736. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  4737. char *error_buf, uint32 error_buf_size)
  4738. {
  4739. int16 *frame_offset = NULL;
  4740. uint8 *cells = NULL, cell;
  4741. int16 *src_offsets = NULL;
  4742. uint8 *emit_data = NULL;
  4743. uint32 i;
  4744. BranchBlock *block = loader_ctx->frame_csp - 1;
  4745. BlockType *block_type = &block->block_type;
  4746. WASMType *wasm_type = block_type->u.type;
  4747. uint32 param_count = block_type->u.type->param_count;
  4748. int16 condition_offset = 0;
  4749. bool disable_emit = false;
  4750. int16 operand_offset = 0;
  4751. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  4752. /* For if block, we also need copy the condition operand offset. */
  4753. if (is_if_block)
  4754. size += sizeof(*cells) + sizeof(*src_offsets);
  4755. /* Allocate memory for the emit data */
  4756. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4757. return false;
  4758. cells = emit_data;
  4759. src_offsets = (int16 *)(cells + param_count);
  4760. if (is_if_block)
  4761. condition_offset = *loader_ctx->frame_offset;
  4762. /* POP original parameter out */
  4763. for (i = 0; i < param_count; i++) {
  4764. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  4765. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4766. }
  4767. frame_offset = loader_ctx->frame_offset;
  4768. /* Get each param's cell num and src offset */
  4769. for (i = 0; i < param_count; i++) {
  4770. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  4771. cells[i] = cell;
  4772. src_offsets[i] = *frame_offset;
  4773. frame_offset += cell;
  4774. }
  4775. /* emit copy instruction */
  4776. emit_label(EXT_OP_COPY_STACK_VALUES);
  4777. /* Part a) */
  4778. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  4779. /* Part b) */
  4780. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  4781. : wasm_type->param_cell_num);
  4782. /* Part c) */
  4783. for (i = 0; i < param_count; i++)
  4784. emit_byte(loader_ctx, cells[i]);
  4785. if (is_if_block)
  4786. emit_byte(loader_ctx, 1);
  4787. /* Part d) */
  4788. for (i = 0; i < param_count; i++)
  4789. emit_operand(loader_ctx, src_offsets[i]);
  4790. if (is_if_block)
  4791. emit_operand(loader_ctx, condition_offset);
  4792. /* Part e) */
  4793. /* Push to dynamic space. The push will emit the dst offset. */
  4794. for (i = 0; i < param_count; i++)
  4795. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  4796. if (is_if_block)
  4797. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  4798. /* Free the emit data */
  4799. wasm_runtime_free(emit_data);
  4800. return true;
  4801. fail:
  4802. /* Free the emit data */
  4803. wasm_runtime_free(emit_data);
  4804. return false;
  4805. }
  4806. #endif
  4807. /* reset the stack to the state of before entering the last block */
  4808. #if WASM_ENABLE_FAST_INTERP != 0
  4809. #define RESET_STACK() \
  4810. do { \
  4811. loader_ctx->stack_cell_num = \
  4812. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4813. loader_ctx->frame_ref = \
  4814. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4815. loader_ctx->frame_offset = \
  4816. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  4817. } while (0)
  4818. #else
  4819. #define RESET_STACK() \
  4820. do { \
  4821. loader_ctx->stack_cell_num = \
  4822. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4823. loader_ctx->frame_ref = \
  4824. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4825. } while (0)
  4826. #endif
  4827. /* set current block's stack polymorphic state */
  4828. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  4829. do { \
  4830. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  4831. _cur_block->is_stack_polymorphic = flag; \
  4832. } while (0)
  4833. #define BLOCK_HAS_PARAM(block_type) \
  4834. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  4835. #define PRESERVE_LOCAL_FOR_BLOCK() \
  4836. do { \
  4837. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  4838. error_buf_size))) { \
  4839. goto fail; \
  4840. } \
  4841. } while (0)
  4842. static bool
  4843. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4844. uint32 cur_func_idx, char *error_buf,
  4845. uint32 error_buf_size)
  4846. {
  4847. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  4848. uint32 param_count, local_count, global_count;
  4849. uint8 *param_types, *local_types, local_type, global_type;
  4850. BlockType func_block_type;
  4851. uint16 *local_offsets, local_offset;
  4852. uint32 count, local_idx, global_idx, u32, align, mem_offset, i;
  4853. int32 i32, i32_const = 0;
  4854. int64 i64_const;
  4855. uint8 opcode, u8;
  4856. bool return_value = false;
  4857. WASMLoaderContext *loader_ctx;
  4858. BranchBlock *frame_csp_tmp;
  4859. #if WASM_ENABLE_BULK_MEMORY != 0
  4860. uint32 segment_index;
  4861. #endif
  4862. #if WASM_ENABLE_FAST_INTERP != 0
  4863. uint8 *func_const_end, *func_const = NULL;
  4864. int16 operand_offset = 0;
  4865. uint8 last_op = 0;
  4866. bool disable_emit, preserve_local = false;
  4867. float32 f32_const;
  4868. float64 f64_const;
  4869. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  4870. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  4871. #endif
  4872. global_count = module->import_global_count + module->global_count;
  4873. param_count = func->func_type->param_count;
  4874. param_types = func->func_type->types;
  4875. func_block_type.is_value_type = false;
  4876. func_block_type.u.type = func->func_type;
  4877. local_count = func->local_count;
  4878. local_types = func->local_types;
  4879. local_offsets = func->local_offsets;
  4880. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  4881. goto fail;
  4882. }
  4883. #if WASM_ENABLE_FAST_INTERP != 0
  4884. /* For the first traverse, the initial value of preserved_local_offset has
  4885. * not been determined, we use the INT16_MAX to represent that a slot has
  4886. * been copied to preserve space. For second traverse, this field will be
  4887. * set to the appropriate value in wasm_loader_ctx_reinit.
  4888. * This is for Issue #1230,
  4889. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  4890. * drop opcodes need to know which slots are preserved, so those slots will
  4891. * not be treated as dynamically allocated slots */
  4892. loader_ctx->preserved_local_offset = INT16_MAX;
  4893. re_scan:
  4894. if (loader_ctx->code_compiled_size > 0) {
  4895. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  4896. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  4897. goto fail;
  4898. }
  4899. p = func->code;
  4900. func->code_compiled = loader_ctx->p_code_compiled;
  4901. func->code_compiled_size = loader_ctx->code_compiled_size;
  4902. }
  4903. #endif
  4904. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  4905. while (p < p_end) {
  4906. opcode = *p++;
  4907. #if WASM_ENABLE_FAST_INTERP != 0
  4908. p_org = p;
  4909. disable_emit = false;
  4910. emit_label(opcode);
  4911. #endif
  4912. switch (opcode) {
  4913. case WASM_OP_UNREACHABLE:
  4914. RESET_STACK();
  4915. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  4916. break;
  4917. case WASM_OP_NOP:
  4918. #if WASM_ENABLE_FAST_INTERP != 0
  4919. skip_label();
  4920. #endif
  4921. break;
  4922. case WASM_OP_IF:
  4923. #if WASM_ENABLE_FAST_INTERP != 0
  4924. PRESERVE_LOCAL_FOR_BLOCK();
  4925. #endif
  4926. POP_I32();
  4927. goto handle_op_block_and_loop;
  4928. case WASM_OP_BLOCK:
  4929. case WASM_OP_LOOP:
  4930. #if WASM_ENABLE_FAST_INTERP != 0
  4931. PRESERVE_LOCAL_FOR_BLOCK();
  4932. #endif
  4933. handle_op_block_and_loop:
  4934. {
  4935. uint8 value_type;
  4936. BlockType block_type;
  4937. p_org = p - 1;
  4938. value_type = read_uint8(p);
  4939. if (is_byte_a_type(value_type)) {
  4940. /* If the first byte is one of these special values:
  4941. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  4942. * the single return value. */
  4943. block_type.is_value_type = true;
  4944. block_type.u.value_type = value_type;
  4945. }
  4946. else {
  4947. uint32 type_index;
  4948. /* Resolve the leb128 encoded type index as block type */
  4949. p--;
  4950. read_leb_uint32(p, p_end, type_index);
  4951. bh_assert(type_index < module->type_count);
  4952. block_type.is_value_type = false;
  4953. block_type.u.type = module->types[type_index];
  4954. #if WASM_ENABLE_FAST_INTERP == 0
  4955. /* If block use type index as block type, change the opcode
  4956. * to new extended opcode so that interpreter can resolve
  4957. * the block quickly.
  4958. */
  4959. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  4960. #endif
  4961. }
  4962. /* Pop block parameters from stack */
  4963. if (BLOCK_HAS_PARAM(block_type)) {
  4964. WASMType *wasm_type = block_type.u.type;
  4965. for (i = 0; i < block_type.u.type->param_count; i++)
  4966. POP_TYPE(
  4967. wasm_type->types[wasm_type->param_count - i - 1]);
  4968. }
  4969. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  4970. block_type, p);
  4971. /* Pass parameters to block */
  4972. if (BLOCK_HAS_PARAM(block_type)) {
  4973. for (i = 0; i < block_type.u.type->param_count; i++)
  4974. PUSH_TYPE(block_type.u.type->types[i]);
  4975. }
  4976. #if WASM_ENABLE_FAST_INTERP != 0
  4977. if (opcode == WASM_OP_BLOCK) {
  4978. skip_label();
  4979. }
  4980. else if (opcode == WASM_OP_LOOP) {
  4981. skip_label();
  4982. if (BLOCK_HAS_PARAM(block_type)) {
  4983. /* Make sure params are in dynamic space */
  4984. if (!copy_params_to_dynamic_space(
  4985. loader_ctx, false, error_buf, error_buf_size))
  4986. goto fail;
  4987. }
  4988. (loader_ctx->frame_csp - 1)->code_compiled =
  4989. loader_ctx->p_code_compiled;
  4990. }
  4991. else if (opcode == WASM_OP_IF) {
  4992. /* If block has parameters, we should make sure they are in
  4993. * dynamic space. Otherwise, when else branch is missing,
  4994. * the later opcode may consume incorrect operand offset.
  4995. * Spec case:
  4996. * (func (export "params-id") (param i32) (result i32)
  4997. * (i32.const 1)
  4998. * (i32.const 2)
  4999. * (if (param i32 i32) (result i32 i32) (local.get 0)
  5000. * (then)) (i32.add)
  5001. * )
  5002. *
  5003. * So we should emit a copy instruction before the if.
  5004. *
  5005. * And we also need to save the parameter offsets and
  5006. * recover them before entering else branch.
  5007. *
  5008. */
  5009. if (BLOCK_HAS_PARAM(block_type)) {
  5010. BranchBlock *block = loader_ctx->frame_csp - 1;
  5011. uint64 size;
  5012. /* skip the if condition operand offset */
  5013. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5014. /* skip the if label */
  5015. skip_label();
  5016. /* Emit a copy instruction */
  5017. if (!copy_params_to_dynamic_space(
  5018. loader_ctx, true, error_buf, error_buf_size))
  5019. goto fail;
  5020. /* Emit the if instruction */
  5021. emit_label(opcode);
  5022. /* Emit the new condition operand offset */
  5023. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  5024. /* Save top param_count values of frame_offset stack, so
  5025. * that we can recover it before executing else branch
  5026. */
  5027. size = sizeof(int16)
  5028. * (uint64)block_type.u.type->param_cell_num;
  5029. if (!(block->param_frame_offsets = loader_malloc(
  5030. size, error_buf, error_buf_size)))
  5031. goto fail;
  5032. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  5033. loader_ctx->frame_offset
  5034. - size / sizeof(int16),
  5035. (uint32)size);
  5036. }
  5037. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  5038. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5039. }
  5040. #endif
  5041. break;
  5042. }
  5043. case WASM_OP_ELSE:
  5044. {
  5045. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  5046. bh_assert(loader_ctx->csp_num >= 2
  5047. && (loader_ctx->frame_csp - 1)->label_type
  5048. == LABEL_TYPE_IF);
  5049. /* check whether if branch's stack matches its result type */
  5050. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  5051. error_buf, error_buf_size))
  5052. goto fail;
  5053. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  5054. #if WASM_ENABLE_FAST_INTERP != 0
  5055. /* if the result of if branch is in local or const area, add a
  5056. * copy op */
  5057. RESERVE_BLOCK_RET();
  5058. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5059. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  5060. #endif
  5061. RESET_STACK();
  5062. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  5063. /* Pass parameters to if-false branch */
  5064. if (BLOCK_HAS_PARAM(block_type)) {
  5065. for (i = 0; i < block_type.u.type->param_count; i++)
  5066. PUSH_TYPE(block_type.u.type->types[i]);
  5067. }
  5068. #if WASM_ENABLE_FAST_INTERP != 0
  5069. /* Recover top param_count values of frame_offset stack */
  5070. if (BLOCK_HAS_PARAM((block_type))) {
  5071. uint32 size;
  5072. BranchBlock *block = loader_ctx->frame_csp - 1;
  5073. size = sizeof(int16) * block_type.u.type->param_cell_num;
  5074. bh_memcpy_s(loader_ctx->frame_offset, size,
  5075. block->param_frame_offsets, size);
  5076. loader_ctx->frame_offset += (size / sizeof(int16));
  5077. }
  5078. #endif
  5079. break;
  5080. }
  5081. case WASM_OP_END:
  5082. {
  5083. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5084. /* check whether block stack matches its result type */
  5085. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  5086. error_buf_size))
  5087. goto fail;
  5088. /* if no else branch, and return types do not match param types,
  5089. * fail */
  5090. if (cur_block->label_type == LABEL_TYPE_IF
  5091. && !cur_block->else_addr) {
  5092. uint32 block_param_count = 0, block_ret_count = 0;
  5093. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  5094. BlockType *cur_block_type = &cur_block->block_type;
  5095. if (cur_block_type->is_value_type) {
  5096. if (cur_block_type->u.value_type != VALUE_TYPE_VOID) {
  5097. block_ret_count = 1;
  5098. block_ret_types = &cur_block_type->u.value_type;
  5099. }
  5100. }
  5101. else {
  5102. block_param_count = cur_block_type->u.type->param_count;
  5103. block_ret_count = cur_block_type->u.type->result_count;
  5104. block_param_types = cur_block_type->u.type->types;
  5105. block_ret_types =
  5106. cur_block_type->u.type->types + block_param_count;
  5107. }
  5108. bh_assert(block_param_count == block_ret_count
  5109. && (!block_param_count
  5110. || !memcmp(block_param_types, block_ret_types,
  5111. block_param_count)));
  5112. (void)block_ret_types;
  5113. (void)block_ret_count;
  5114. (void)block_param_types;
  5115. }
  5116. POP_CSP();
  5117. #if WASM_ENABLE_FAST_INTERP != 0
  5118. skip_label();
  5119. /* copy the result to the block return address */
  5120. RESERVE_BLOCK_RET();
  5121. apply_label_patch(loader_ctx, 0, PATCH_END);
  5122. free_label_patch_list(loader_ctx->frame_csp);
  5123. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  5124. int32 idx;
  5125. uint8 ret_type;
  5126. emit_label(WASM_OP_RETURN);
  5127. for (idx = (int32)func->func_type->result_count - 1;
  5128. idx >= 0; idx--) {
  5129. ret_type = *(func->func_type->types
  5130. + func->func_type->param_count + idx);
  5131. POP_OFFSET_TYPE(ret_type);
  5132. }
  5133. }
  5134. #endif
  5135. if (loader_ctx->csp_num > 0) {
  5136. loader_ctx->frame_csp->end_addr = p - 1;
  5137. }
  5138. else {
  5139. /* end of function block, function will return */
  5140. bh_assert(p == p_end);
  5141. }
  5142. break;
  5143. }
  5144. case WASM_OP_BR:
  5145. {
  5146. if (!(frame_csp_tmp = check_branch_block(
  5147. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  5148. goto fail;
  5149. RESET_STACK();
  5150. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5151. break;
  5152. }
  5153. case WASM_OP_BR_IF:
  5154. {
  5155. POP_I32();
  5156. if (!(frame_csp_tmp = check_branch_block(
  5157. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  5158. goto fail;
  5159. break;
  5160. }
  5161. case WASM_OP_BR_TABLE:
  5162. {
  5163. uint8 *ret_types = NULL;
  5164. uint32 ret_count = 0;
  5165. #if WASM_ENABLE_FAST_INTERP == 0
  5166. uint8 *p_depth_begin, *p_depth;
  5167. uint32 depth, j;
  5168. BrTableCache *br_table_cache = NULL;
  5169. p_org = p - 1;
  5170. #endif
  5171. read_leb_uint32(p, p_end, count);
  5172. #if WASM_ENABLE_FAST_INTERP != 0
  5173. emit_uint32(loader_ctx, count);
  5174. #endif
  5175. POP_I32();
  5176. #if WASM_ENABLE_FAST_INTERP == 0
  5177. p_depth_begin = p_depth = p;
  5178. #endif
  5179. for (i = 0; i <= count; i++) {
  5180. if (!(frame_csp_tmp =
  5181. check_branch_block(loader_ctx, &p, p_end,
  5182. error_buf, error_buf_size)))
  5183. goto fail;
  5184. #if WASM_ENABLE_FAST_INTERP == 0
  5185. depth = (uint32)(loader_ctx->frame_csp - 1 - frame_csp_tmp);
  5186. if (br_table_cache) {
  5187. br_table_cache->br_depths[i] = depth;
  5188. }
  5189. else {
  5190. if (depth > 255) {
  5191. /* The depth cannot be stored in one byte,
  5192. create br_table cache to store each depth */
  5193. if (!(br_table_cache = loader_malloc(
  5194. offsetof(BrTableCache, br_depths)
  5195. + sizeof(uint32)
  5196. * (uint64)(count + 1),
  5197. error_buf, error_buf_size))) {
  5198. goto fail;
  5199. }
  5200. *p_org = EXT_OP_BR_TABLE_CACHE;
  5201. br_table_cache->br_table_op_addr = p_org;
  5202. br_table_cache->br_count = count;
  5203. /* Copy previous depths which are one byte */
  5204. for (j = 0; j < i; j++) {
  5205. br_table_cache->br_depths[j] = p_depth_begin[j];
  5206. }
  5207. br_table_cache->br_depths[i] = depth;
  5208. bh_list_insert(module->br_table_cache_list,
  5209. br_table_cache);
  5210. }
  5211. else {
  5212. /* The depth can be stored in one byte, use the
  5213. byte of the leb to store it */
  5214. *p_depth++ = (uint8)depth;
  5215. }
  5216. }
  5217. #endif
  5218. }
  5219. #if WASM_ENABLE_FAST_INTERP == 0
  5220. /* Set the tailing bytes to nop */
  5221. if (br_table_cache)
  5222. p_depth = p_depth_begin;
  5223. while (p_depth < p)
  5224. *p_depth++ = WASM_OP_NOP;
  5225. #endif
  5226. RESET_STACK();
  5227. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5228. (void)ret_count;
  5229. (void)ret_types;
  5230. break;
  5231. }
  5232. case WASM_OP_RETURN:
  5233. {
  5234. int32 idx;
  5235. uint8 ret_type;
  5236. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  5237. idx--) {
  5238. ret_type = *(func->func_type->types
  5239. + func->func_type->param_count + idx);
  5240. POP_TYPE(ret_type);
  5241. #if WASM_ENABLE_FAST_INTERP != 0
  5242. /* emit the offset after return opcode */
  5243. POP_OFFSET_TYPE(ret_type);
  5244. #endif
  5245. }
  5246. RESET_STACK();
  5247. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5248. break;
  5249. }
  5250. case WASM_OP_CALL:
  5251. #if WASM_ENABLE_TAIL_CALL != 0
  5252. case WASM_OP_RETURN_CALL:
  5253. #endif
  5254. {
  5255. WASMType *func_type;
  5256. uint32 func_idx;
  5257. int32 idx;
  5258. read_leb_uint32(p, p_end, func_idx);
  5259. #if WASM_ENABLE_FAST_INTERP != 0
  5260. /* we need to emit func_idx before arguments */
  5261. emit_uint32(loader_ctx, func_idx);
  5262. #endif
  5263. bh_assert(func_idx < module->import_function_count
  5264. + module->function_count);
  5265. if (func_idx < module->import_function_count)
  5266. func_type =
  5267. module->import_functions[func_idx].u.function.func_type;
  5268. else
  5269. func_type = module
  5270. ->functions[func_idx
  5271. - module->import_function_count]
  5272. ->func_type;
  5273. if (func_type->param_count > 0) {
  5274. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  5275. idx--) {
  5276. POP_TYPE(func_type->types[idx]);
  5277. #if WASM_ENABLE_FAST_INTERP != 0
  5278. POP_OFFSET_TYPE(func_type->types[idx]);
  5279. #endif
  5280. }
  5281. }
  5282. #if WASM_ENABLE_TAIL_CALL != 0
  5283. if (opcode == WASM_OP_CALL) {
  5284. #endif
  5285. for (i = 0; i < func_type->result_count; i++) {
  5286. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5287. #if WASM_ENABLE_FAST_INTERP != 0
  5288. /* Here we emit each return value's dynamic_offset. But
  5289. * in fact these offsets are continuous, so interpreter
  5290. * only need to get the first return value's offset.
  5291. */
  5292. PUSH_OFFSET_TYPE(
  5293. func_type->types[func_type->param_count + i]);
  5294. #endif
  5295. }
  5296. #if WASM_ENABLE_TAIL_CALL != 0
  5297. }
  5298. else {
  5299. bh_assert(func_type->result_count
  5300. == func->func_type->result_count);
  5301. for (i = 0; i < func_type->result_count; i++) {
  5302. bh_assert(
  5303. func_type->types[func_type->param_count + i]
  5304. == func->func_type
  5305. ->types[func->func_type->param_count + i]);
  5306. }
  5307. }
  5308. #endif
  5309. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  5310. || WASM_ENABLE_WAMR_COMPILER != 0
  5311. func->has_op_func_call = true;
  5312. #endif
  5313. break;
  5314. }
  5315. case WASM_OP_CALL_INDIRECT:
  5316. #if WASM_ENABLE_TAIL_CALL != 0
  5317. case WASM_OP_RETURN_CALL_INDIRECT:
  5318. #endif
  5319. {
  5320. int32 idx;
  5321. WASMType *func_type;
  5322. uint32 type_idx, table_idx;
  5323. bh_assert(module->import_table_count + module->table_count > 0);
  5324. read_leb_uint32(p, p_end, type_idx);
  5325. #if WASM_ENABLE_REF_TYPES != 0
  5326. read_leb_uint32(p, p_end, table_idx);
  5327. #else
  5328. CHECK_BUF(p, p_end, 1);
  5329. table_idx = read_uint8(p);
  5330. #endif
  5331. if (!check_table_index(module, table_idx, error_buf,
  5332. error_buf_size)) {
  5333. goto fail;
  5334. }
  5335. #if WASM_ENABLE_FAST_INTERP != 0
  5336. /* we need to emit before arguments */
  5337. emit_uint32(loader_ctx, type_idx);
  5338. emit_uint32(loader_ctx, table_idx);
  5339. #endif
  5340. /* skip elem idx */
  5341. POP_I32();
  5342. bh_assert(type_idx < module->type_count);
  5343. func_type = module->types[type_idx];
  5344. if (func_type->param_count > 0) {
  5345. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  5346. idx--) {
  5347. POP_TYPE(func_type->types[idx]);
  5348. #if WASM_ENABLE_FAST_INTERP != 0
  5349. POP_OFFSET_TYPE(func_type->types[idx]);
  5350. #endif
  5351. }
  5352. }
  5353. #if WASM_ENABLE_TAIL_CALL != 0
  5354. if (opcode == WASM_OP_CALL) {
  5355. #endif
  5356. for (i = 0; i < func_type->result_count; i++) {
  5357. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5358. #if WASM_ENABLE_FAST_INTERP != 0
  5359. PUSH_OFFSET_TYPE(
  5360. func_type->types[func_type->param_count + i]);
  5361. #endif
  5362. }
  5363. #if WASM_ENABLE_TAIL_CALL != 0
  5364. }
  5365. else {
  5366. bh_assert(func_type->result_count
  5367. == func->func_type->result_count);
  5368. for (i = 0; i < func_type->result_count; i++) {
  5369. bh_assert(
  5370. func_type->types[func_type->param_count + i]
  5371. == func->func_type
  5372. ->types[func->func_type->param_count + i]);
  5373. }
  5374. }
  5375. #endif
  5376. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  5377. || WASM_ENABLE_WAMR_COMPILER != 0
  5378. func->has_op_func_call = true;
  5379. #endif
  5380. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  5381. func->has_op_call_indirect = true;
  5382. #endif
  5383. break;
  5384. }
  5385. case WASM_OP_DROP:
  5386. {
  5387. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5388. int32 available_stack_cell =
  5389. (int32)(loader_ctx->stack_cell_num
  5390. - cur_block->stack_cell_num);
  5391. bh_assert(!(available_stack_cell <= 0
  5392. && !cur_block->is_stack_polymorphic));
  5393. if (available_stack_cell > 0) {
  5394. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  5395. loader_ctx->frame_ref--;
  5396. loader_ctx->stack_cell_num--;
  5397. #if WASM_ENABLE_FAST_INTERP != 0
  5398. skip_label();
  5399. loader_ctx->frame_offset--;
  5400. if ((*(loader_ctx->frame_offset)
  5401. > loader_ctx->start_dynamic_offset)
  5402. && (*(loader_ctx->frame_offset)
  5403. < loader_ctx->max_dynamic_offset))
  5404. loader_ctx->dynamic_offset--;
  5405. #endif
  5406. }
  5407. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  5408. loader_ctx->frame_ref -= 2;
  5409. loader_ctx->stack_cell_num -= 2;
  5410. #if WASM_ENABLE_FAST_INTERP == 0
  5411. *(p - 1) = WASM_OP_DROP_64;
  5412. #endif
  5413. #if WASM_ENABLE_FAST_INTERP != 0
  5414. skip_label();
  5415. loader_ctx->frame_offset -= 2;
  5416. if ((*(loader_ctx->frame_offset)
  5417. > loader_ctx->start_dynamic_offset)
  5418. && (*(loader_ctx->frame_offset)
  5419. < loader_ctx->max_dynamic_offset))
  5420. loader_ctx->dynamic_offset -= 2;
  5421. #endif
  5422. }
  5423. else {
  5424. bh_assert(0);
  5425. }
  5426. }
  5427. else {
  5428. #if WASM_ENABLE_FAST_INTERP != 0
  5429. skip_label();
  5430. #endif
  5431. }
  5432. break;
  5433. }
  5434. case WASM_OP_SELECT:
  5435. {
  5436. uint8 ref_type;
  5437. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5438. int32 available_stack_cell;
  5439. POP_I32();
  5440. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  5441. - cur_block->stack_cell_num);
  5442. bh_assert(!(available_stack_cell <= 0
  5443. && !cur_block->is_stack_polymorphic));
  5444. if (available_stack_cell > 0) {
  5445. switch (*(loader_ctx->frame_ref - 1)) {
  5446. case REF_I32:
  5447. case REF_F32:
  5448. break;
  5449. case REF_I64_2:
  5450. case REF_F64_2:
  5451. #if WASM_ENABLE_FAST_INTERP == 0
  5452. *(p - 1) = WASM_OP_SELECT_64;
  5453. #endif
  5454. #if WASM_ENABLE_FAST_INTERP != 0
  5455. if (loader_ctx->p_code_compiled) {
  5456. uint8 opcode_tmp = WASM_OP_SELECT_64;
  5457. uint8 *p_code_compiled_tmp =
  5458. loader_ctx->p_code_compiled - 2;
  5459. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5460. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5461. *(void **)(p_code_compiled_tmp
  5462. - sizeof(void *)) =
  5463. handle_table[opcode_tmp];
  5464. #else
  5465. int32 offset =
  5466. (int32)((uint8 *)handle_table[opcode_tmp]
  5467. - (uint8 *)handle_table[0]);
  5468. if (!(offset >= INT16_MIN
  5469. && offset < INT16_MAX)) {
  5470. set_error_buf(error_buf, error_buf_size,
  5471. "pre-compiled label offset "
  5472. "out of range");
  5473. goto fail;
  5474. }
  5475. *(int16 *)(p_code_compiled_tmp
  5476. - sizeof(int16)) = (int16)offset;
  5477. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5478. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  5479. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5480. *(p_code_compiled_tmp - 1) = opcode_tmp;
  5481. #else
  5482. *(p_code_compiled_tmp - 2) = opcode_tmp;
  5483. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5484. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  5485. }
  5486. #endif
  5487. break;
  5488. }
  5489. ref_type = *(loader_ctx->frame_ref - 1);
  5490. #if WASM_ENABLE_FAST_INTERP != 0
  5491. POP_OFFSET_TYPE(ref_type);
  5492. #endif
  5493. POP_TYPE(ref_type);
  5494. #if WASM_ENABLE_FAST_INTERP != 0
  5495. POP_OFFSET_TYPE(ref_type);
  5496. #endif
  5497. POP_TYPE(ref_type);
  5498. #if WASM_ENABLE_FAST_INTERP != 0
  5499. PUSH_OFFSET_TYPE(ref_type);
  5500. #endif
  5501. PUSH_TYPE(ref_type);
  5502. }
  5503. else {
  5504. #if WASM_ENABLE_FAST_INTERP != 0
  5505. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  5506. #endif
  5507. PUSH_TYPE(VALUE_TYPE_ANY);
  5508. }
  5509. break;
  5510. }
  5511. #if WASM_ENABLE_REF_TYPES != 0
  5512. case WASM_OP_SELECT_T:
  5513. {
  5514. uint8 vec_len, ref_type;
  5515. read_leb_uint32(p, p_end, vec_len);
  5516. if (!vec_len) {
  5517. set_error_buf(error_buf, error_buf_size,
  5518. "invalid result arity");
  5519. goto fail;
  5520. }
  5521. CHECK_BUF(p, p_end, 1);
  5522. ref_type = read_uint8(p);
  5523. if (!is_value_type(ref_type)) {
  5524. set_error_buf(error_buf, error_buf_size,
  5525. "unknown value type");
  5526. goto fail;
  5527. }
  5528. POP_I32();
  5529. #if WASM_ENABLE_FAST_INTERP != 0
  5530. if (loader_ctx->p_code_compiled) {
  5531. uint8 opcode_tmp = WASM_OP_SELECT;
  5532. uint8 *p_code_compiled_tmp =
  5533. loader_ctx->p_code_compiled - 2;
  5534. if (ref_type == VALUE_TYPE_F64
  5535. || ref_type == VALUE_TYPE_I64)
  5536. opcode_tmp = WASM_OP_SELECT_64;
  5537. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5538. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5539. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  5540. handle_table[opcode_tmp];
  5541. #else
  5542. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  5543. - (uint8 *)handle_table[0]);
  5544. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  5545. set_error_buf(error_buf, error_buf_size,
  5546. "pre-compiled label offset out of range");
  5547. goto fail;
  5548. }
  5549. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  5550. (int16)offset;
  5551. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5552. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  5553. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5554. *(p_code_compiled_tmp - 1) = opcode_tmp;
  5555. #else
  5556. *(p_code_compiled_tmp - 2) = opcode_tmp;
  5557. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5558. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  5559. }
  5560. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  5561. #if WASM_ENABLE_FAST_INTERP != 0
  5562. POP_OFFSET_TYPE(ref_type);
  5563. POP_TYPE(ref_type);
  5564. POP_OFFSET_TYPE(ref_type);
  5565. POP_TYPE(ref_type);
  5566. PUSH_OFFSET_TYPE(ref_type);
  5567. PUSH_TYPE(ref_type);
  5568. #else
  5569. POP2_AND_PUSH(ref_type, ref_type);
  5570. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  5571. (void)vec_len;
  5572. break;
  5573. }
  5574. /* table.get x. tables[x]. [i32] -> [t] */
  5575. /* table.set x. tables[x]. [i32 t] -> [] */
  5576. case WASM_OP_TABLE_GET:
  5577. case WASM_OP_TABLE_SET:
  5578. {
  5579. uint8 decl_ref_type;
  5580. uint32 table_idx;
  5581. read_leb_uint32(p, p_end, table_idx);
  5582. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  5583. error_buf, error_buf_size))
  5584. goto fail;
  5585. #if WASM_ENABLE_FAST_INTERP != 0
  5586. emit_uint32(loader_ctx, table_idx);
  5587. #endif
  5588. if (opcode == WASM_OP_TABLE_GET) {
  5589. POP_I32();
  5590. #if WASM_ENABLE_FAST_INTERP != 0
  5591. PUSH_OFFSET_TYPE(decl_ref_type);
  5592. #endif
  5593. PUSH_TYPE(decl_ref_type);
  5594. }
  5595. else {
  5596. #if WASM_ENABLE_FAST_INTERP != 0
  5597. POP_OFFSET_TYPE(decl_ref_type);
  5598. #endif
  5599. POP_TYPE(decl_ref_type);
  5600. POP_I32();
  5601. }
  5602. break;
  5603. }
  5604. case WASM_OP_REF_NULL:
  5605. {
  5606. uint8 ref_type;
  5607. CHECK_BUF(p, p_end, 1);
  5608. ref_type = read_uint8(p);
  5609. if (ref_type != VALUE_TYPE_FUNCREF
  5610. && ref_type != VALUE_TYPE_EXTERNREF) {
  5611. set_error_buf(error_buf, error_buf_size,
  5612. "unknown value type");
  5613. goto fail;
  5614. }
  5615. #if WASM_ENABLE_FAST_INTERP != 0
  5616. PUSH_OFFSET_TYPE(ref_type);
  5617. #endif
  5618. PUSH_TYPE(ref_type);
  5619. break;
  5620. }
  5621. case WASM_OP_REF_IS_NULL:
  5622. {
  5623. #if WASM_ENABLE_FAST_INTERP != 0
  5624. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  5625. VALUE_TYPE_FUNCREF,
  5626. error_buf, error_buf_size)
  5627. && !wasm_loader_pop_frame_ref_offset(
  5628. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  5629. error_buf_size)) {
  5630. goto fail;
  5631. }
  5632. #else
  5633. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  5634. error_buf, error_buf_size)
  5635. && !wasm_loader_pop_frame_ref(loader_ctx,
  5636. VALUE_TYPE_EXTERNREF,
  5637. error_buf, error_buf_size)) {
  5638. goto fail;
  5639. }
  5640. #endif
  5641. PUSH_I32();
  5642. break;
  5643. }
  5644. case WASM_OP_REF_FUNC:
  5645. {
  5646. uint32 func_idx = 0;
  5647. read_leb_uint32(p, p_end, func_idx);
  5648. if (!check_function_index(module, func_idx, error_buf,
  5649. error_buf_size)) {
  5650. goto fail;
  5651. }
  5652. /* Refer to a forward-declared function */
  5653. if (func_idx >= cur_func_idx + module->import_function_count) {
  5654. WASMTableSeg *table_seg = module->table_segments;
  5655. bool func_declared = false;
  5656. uint32 j;
  5657. /* Check whether the function is declared in table segs */
  5658. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  5659. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  5660. && wasm_elem_is_declarative(table_seg->mode)) {
  5661. for (j = 0; j < table_seg->function_count; j++) {
  5662. if (table_seg->func_indexes[j] == func_idx) {
  5663. func_declared = true;
  5664. break;
  5665. }
  5666. }
  5667. }
  5668. }
  5669. if (!func_declared) {
  5670. /* Check whether the function is exported */
  5671. for (i = 0; i < module->export_count; i++) {
  5672. if (module->exports[i].kind == EXPORT_KIND_FUNC
  5673. && module->exports[i].index == func_idx) {
  5674. func_declared = true;
  5675. break;
  5676. }
  5677. }
  5678. }
  5679. bh_assert(func_declared);
  5680. (void)func_declared;
  5681. }
  5682. #if WASM_ENABLE_FAST_INTERP != 0
  5683. emit_uint32(loader_ctx, func_idx);
  5684. #endif
  5685. PUSH_FUNCREF();
  5686. break;
  5687. }
  5688. #endif /* WASM_ENABLE_REF_TYPES */
  5689. case WASM_OP_GET_LOCAL:
  5690. {
  5691. p_org = p - 1;
  5692. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5693. PUSH_TYPE(local_type);
  5694. #if WASM_ENABLE_FAST_INTERP != 0
  5695. /* Get Local is optimized out */
  5696. skip_label();
  5697. disable_emit = true;
  5698. operand_offset = local_offset;
  5699. PUSH_OFFSET_TYPE(local_type);
  5700. #else
  5701. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  5702. && (WASM_ENABLE_FAST_JIT == 0)
  5703. if (local_offset < 0x80) {
  5704. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  5705. if (is_32bit_type(local_type))
  5706. *p_org++ = (uint8)local_offset;
  5707. else
  5708. *p_org++ = (uint8)(local_offset | 0x80);
  5709. while (p_org < p)
  5710. *p_org++ = WASM_OP_NOP;
  5711. }
  5712. #endif
  5713. #endif
  5714. break;
  5715. }
  5716. case WASM_OP_SET_LOCAL:
  5717. {
  5718. p_org = p - 1;
  5719. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5720. POP_TYPE(local_type);
  5721. #if WASM_ENABLE_FAST_INTERP != 0
  5722. if (!(preserve_referenced_local(
  5723. loader_ctx, opcode, local_offset, local_type,
  5724. &preserve_local, error_buf, error_buf_size)))
  5725. goto fail;
  5726. if (local_offset < 256) {
  5727. skip_label();
  5728. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  5729. if (loader_ctx->p_code_compiled)
  5730. STORE_U16(loader_ctx->p_code_compiled - 2,
  5731. local_offset);
  5732. loader_ctx->frame_offset--;
  5733. loader_ctx->dynamic_offset--;
  5734. }
  5735. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  5736. if (loader_ctx->p_code_compiled)
  5737. STORE_U16(loader_ctx->p_code_compiled - 2,
  5738. local_offset);
  5739. loader_ctx->frame_offset -= 2;
  5740. loader_ctx->dynamic_offset -= 2;
  5741. }
  5742. else {
  5743. if (is_32bit_type(local_type)) {
  5744. emit_label(EXT_OP_SET_LOCAL_FAST);
  5745. emit_byte(loader_ctx, (uint8)local_offset);
  5746. }
  5747. else {
  5748. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  5749. emit_byte(loader_ctx, (uint8)local_offset);
  5750. }
  5751. POP_OFFSET_TYPE(local_type);
  5752. }
  5753. }
  5754. else { /* local index larger than 255, reserve leb */
  5755. emit_uint32(loader_ctx, local_idx);
  5756. POP_OFFSET_TYPE(local_type);
  5757. }
  5758. #else
  5759. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  5760. && (WASM_ENABLE_FAST_JIT == 0)
  5761. if (local_offset < 0x80) {
  5762. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  5763. if (is_32bit_type(local_type))
  5764. *p_org++ = (uint8)local_offset;
  5765. else
  5766. *p_org++ = (uint8)(local_offset | 0x80);
  5767. while (p_org < p)
  5768. *p_org++ = WASM_OP_NOP;
  5769. }
  5770. #endif
  5771. #endif
  5772. break;
  5773. }
  5774. case WASM_OP_TEE_LOCAL:
  5775. {
  5776. p_org = p - 1;
  5777. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5778. #if WASM_ENABLE_FAST_INTERP != 0
  5779. /* If the stack is in polymorphic state, do fake pop and push on
  5780. offset stack to keep the depth of offset stack to be the
  5781. same with ref stack */
  5782. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5783. if (cur_block->is_stack_polymorphic) {
  5784. POP_OFFSET_TYPE(local_type);
  5785. PUSH_OFFSET_TYPE(local_type);
  5786. }
  5787. #endif
  5788. POP_TYPE(local_type);
  5789. PUSH_TYPE(local_type);
  5790. #if WASM_ENABLE_FAST_INTERP != 0
  5791. if (!(preserve_referenced_local(
  5792. loader_ctx, opcode, local_offset, local_type,
  5793. &preserve_local, error_buf, error_buf_size)))
  5794. goto fail;
  5795. if (local_offset < 256) {
  5796. skip_label();
  5797. if (is_32bit_type(local_type)) {
  5798. emit_label(EXT_OP_TEE_LOCAL_FAST);
  5799. emit_byte(loader_ctx, (uint8)local_offset);
  5800. }
  5801. else {
  5802. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  5803. emit_byte(loader_ctx, (uint8)local_offset);
  5804. }
  5805. }
  5806. else { /* local index larger than 255, reserve leb */
  5807. emit_uint32(loader_ctx, local_idx);
  5808. }
  5809. emit_operand(loader_ctx,
  5810. *(loader_ctx->frame_offset
  5811. - wasm_value_type_cell_num(local_type)));
  5812. #else
  5813. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  5814. && (WASM_ENABLE_FAST_JIT == 0)
  5815. if (local_offset < 0x80) {
  5816. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  5817. if (is_32bit_type(local_type))
  5818. *p_org++ = (uint8)local_offset;
  5819. else
  5820. *p_org++ = (uint8)(local_offset | 0x80);
  5821. while (p_org < p)
  5822. *p_org++ = WASM_OP_NOP;
  5823. }
  5824. #endif
  5825. #endif
  5826. break;
  5827. }
  5828. case WASM_OP_GET_GLOBAL:
  5829. {
  5830. p_org = p - 1;
  5831. read_leb_uint32(p, p_end, global_idx);
  5832. bh_assert(global_idx < global_count);
  5833. global_type =
  5834. global_idx < module->import_global_count
  5835. ? module->import_globals[global_idx].u.global.type
  5836. : module
  5837. ->globals[global_idx
  5838. - module->import_global_count]
  5839. .type;
  5840. PUSH_TYPE(global_type);
  5841. #if WASM_ENABLE_FAST_INTERP == 0
  5842. if (global_type == VALUE_TYPE_I64
  5843. || global_type == VALUE_TYPE_F64) {
  5844. *p_org = WASM_OP_GET_GLOBAL_64;
  5845. }
  5846. #else /* else of WASM_ENABLE_FAST_INTERP */
  5847. if (is_64bit_type(global_type)) {
  5848. skip_label();
  5849. emit_label(WASM_OP_GET_GLOBAL_64);
  5850. }
  5851. emit_uint32(loader_ctx, global_idx);
  5852. PUSH_OFFSET_TYPE(global_type);
  5853. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5854. break;
  5855. }
  5856. case WASM_OP_SET_GLOBAL:
  5857. {
  5858. bool is_mutable = false;
  5859. p_org = p - 1;
  5860. read_leb_uint32(p, p_end, global_idx);
  5861. bh_assert(global_idx < global_count);
  5862. is_mutable =
  5863. global_idx < module->import_global_count
  5864. ? module->import_globals[global_idx].u.global.is_mutable
  5865. : module
  5866. ->globals[global_idx
  5867. - module->import_global_count]
  5868. .is_mutable;
  5869. bh_assert(is_mutable);
  5870. global_type =
  5871. global_idx < module->import_global_count
  5872. ? module->import_globals[global_idx].u.global.type
  5873. : module
  5874. ->globals[global_idx
  5875. - module->import_global_count]
  5876. .type;
  5877. POP_TYPE(global_type);
  5878. #if WASM_ENABLE_FAST_INTERP == 0
  5879. if (is_64bit_type(global_type)) {
  5880. *p_org = WASM_OP_SET_GLOBAL_64;
  5881. }
  5882. else if (module->aux_stack_size > 0
  5883. && global_idx == module->aux_stack_top_global_index) {
  5884. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  5885. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  5886. func->has_op_set_global_aux_stack = true;
  5887. #endif
  5888. }
  5889. #else /* else of WASM_ENABLE_FAST_INTERP */
  5890. if (is_64bit_type(global_type)) {
  5891. skip_label();
  5892. emit_label(WASM_OP_SET_GLOBAL_64);
  5893. }
  5894. else if (module->aux_stack_size > 0
  5895. && global_idx == module->aux_stack_top_global_index) {
  5896. skip_label();
  5897. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  5898. }
  5899. emit_uint32(loader_ctx, global_idx);
  5900. POP_OFFSET_TYPE(global_type);
  5901. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5902. (void)is_mutable;
  5903. break;
  5904. }
  5905. /* load */
  5906. case WASM_OP_I32_LOAD:
  5907. case WASM_OP_I32_LOAD8_S:
  5908. case WASM_OP_I32_LOAD8_U:
  5909. case WASM_OP_I32_LOAD16_S:
  5910. case WASM_OP_I32_LOAD16_U:
  5911. case WASM_OP_I64_LOAD:
  5912. case WASM_OP_I64_LOAD8_S:
  5913. case WASM_OP_I64_LOAD8_U:
  5914. case WASM_OP_I64_LOAD16_S:
  5915. case WASM_OP_I64_LOAD16_U:
  5916. case WASM_OP_I64_LOAD32_S:
  5917. case WASM_OP_I64_LOAD32_U:
  5918. case WASM_OP_F32_LOAD:
  5919. case WASM_OP_F64_LOAD:
  5920. /* store */
  5921. case WASM_OP_I32_STORE:
  5922. case WASM_OP_I32_STORE8:
  5923. case WASM_OP_I32_STORE16:
  5924. case WASM_OP_I64_STORE:
  5925. case WASM_OP_I64_STORE8:
  5926. case WASM_OP_I64_STORE16:
  5927. case WASM_OP_I64_STORE32:
  5928. case WASM_OP_F32_STORE:
  5929. case WASM_OP_F64_STORE:
  5930. {
  5931. #if WASM_ENABLE_FAST_INTERP != 0
  5932. /* change F32/F64 into I32/I64 */
  5933. if (opcode == WASM_OP_F32_LOAD) {
  5934. skip_label();
  5935. emit_label(WASM_OP_I32_LOAD);
  5936. }
  5937. else if (opcode == WASM_OP_F64_LOAD) {
  5938. skip_label();
  5939. emit_label(WASM_OP_I64_LOAD);
  5940. }
  5941. else if (opcode == WASM_OP_F32_STORE) {
  5942. skip_label();
  5943. emit_label(WASM_OP_I32_STORE);
  5944. }
  5945. else if (opcode == WASM_OP_F64_STORE) {
  5946. skip_label();
  5947. emit_label(WASM_OP_I64_STORE);
  5948. }
  5949. #endif
  5950. CHECK_MEMORY();
  5951. read_leb_uint32(p, p_end, align); /* align */
  5952. read_leb_uint32(p, p_end, mem_offset); /* offset */
  5953. #if WASM_ENABLE_FAST_INTERP != 0
  5954. emit_uint32(loader_ctx, mem_offset);
  5955. #endif
  5956. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  5957. func->has_memory_operations = true;
  5958. #endif
  5959. switch (opcode) {
  5960. /* load */
  5961. case WASM_OP_I32_LOAD:
  5962. case WASM_OP_I32_LOAD8_S:
  5963. case WASM_OP_I32_LOAD8_U:
  5964. case WASM_OP_I32_LOAD16_S:
  5965. case WASM_OP_I32_LOAD16_U:
  5966. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5967. break;
  5968. case WASM_OP_I64_LOAD:
  5969. case WASM_OP_I64_LOAD8_S:
  5970. case WASM_OP_I64_LOAD8_U:
  5971. case WASM_OP_I64_LOAD16_S:
  5972. case WASM_OP_I64_LOAD16_U:
  5973. case WASM_OP_I64_LOAD32_S:
  5974. case WASM_OP_I64_LOAD32_U:
  5975. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  5976. break;
  5977. case WASM_OP_F32_LOAD:
  5978. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  5979. break;
  5980. case WASM_OP_F64_LOAD:
  5981. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  5982. break;
  5983. /* store */
  5984. case WASM_OP_I32_STORE:
  5985. case WASM_OP_I32_STORE8:
  5986. case WASM_OP_I32_STORE16:
  5987. POP_I32();
  5988. POP_I32();
  5989. break;
  5990. case WASM_OP_I64_STORE:
  5991. case WASM_OP_I64_STORE8:
  5992. case WASM_OP_I64_STORE16:
  5993. case WASM_OP_I64_STORE32:
  5994. POP_I64();
  5995. POP_I32();
  5996. break;
  5997. case WASM_OP_F32_STORE:
  5998. POP_F32();
  5999. POP_I32();
  6000. break;
  6001. case WASM_OP_F64_STORE:
  6002. POP_F64();
  6003. POP_I32();
  6004. break;
  6005. default:
  6006. break;
  6007. }
  6008. break;
  6009. }
  6010. case WASM_OP_MEMORY_SIZE:
  6011. CHECK_MEMORY();
  6012. /* reserved byte 0x00 */
  6013. bh_assert(*p == 0x00);
  6014. p++;
  6015. PUSH_I32();
  6016. module->possible_memory_grow = true;
  6017. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6018. func->has_memory_operations = true;
  6019. #endif
  6020. break;
  6021. case WASM_OP_MEMORY_GROW:
  6022. CHECK_MEMORY();
  6023. /* reserved byte 0x00 */
  6024. bh_assert(*p == 0x00);
  6025. p++;
  6026. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6027. module->possible_memory_grow = true;
  6028. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  6029. || WASM_ENABLE_WAMR_COMPILER != 0
  6030. func->has_op_memory_grow = true;
  6031. #endif
  6032. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6033. func->has_memory_operations = true;
  6034. #endif
  6035. break;
  6036. case WASM_OP_I32_CONST:
  6037. read_leb_int32(p, p_end, i32_const);
  6038. #if WASM_ENABLE_FAST_INTERP != 0
  6039. skip_label();
  6040. disable_emit = true;
  6041. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  6042. if (operand_offset == 0) {
  6043. disable_emit = false;
  6044. emit_label(WASM_OP_I32_CONST);
  6045. emit_uint32(loader_ctx, i32_const);
  6046. }
  6047. #else
  6048. (void)i32_const;
  6049. #endif
  6050. PUSH_I32();
  6051. break;
  6052. case WASM_OP_I64_CONST:
  6053. read_leb_int64(p, p_end, i64_const);
  6054. #if WASM_ENABLE_FAST_INTERP != 0
  6055. skip_label();
  6056. disable_emit = true;
  6057. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  6058. if (operand_offset == 0) {
  6059. disable_emit = false;
  6060. emit_label(WASM_OP_I64_CONST);
  6061. emit_uint64(loader_ctx, i64_const);
  6062. }
  6063. #endif
  6064. PUSH_I64();
  6065. break;
  6066. case WASM_OP_F32_CONST:
  6067. p += sizeof(float32);
  6068. #if WASM_ENABLE_FAST_INTERP != 0
  6069. skip_label();
  6070. disable_emit = true;
  6071. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  6072. sizeof(float32));
  6073. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  6074. if (operand_offset == 0) {
  6075. disable_emit = false;
  6076. emit_label(WASM_OP_F32_CONST);
  6077. emit_float32(loader_ctx, f32_const);
  6078. }
  6079. #endif
  6080. PUSH_F32();
  6081. break;
  6082. case WASM_OP_F64_CONST:
  6083. p += sizeof(float64);
  6084. #if WASM_ENABLE_FAST_INTERP != 0
  6085. skip_label();
  6086. disable_emit = true;
  6087. /* Some MCU may require 8-byte align */
  6088. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  6089. sizeof(float64));
  6090. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  6091. if (operand_offset == 0) {
  6092. disable_emit = false;
  6093. emit_label(WASM_OP_F64_CONST);
  6094. emit_float64(loader_ctx, f64_const);
  6095. }
  6096. #endif
  6097. PUSH_F64();
  6098. break;
  6099. case WASM_OP_I32_EQZ:
  6100. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6101. break;
  6102. case WASM_OP_I32_EQ:
  6103. case WASM_OP_I32_NE:
  6104. case WASM_OP_I32_LT_S:
  6105. case WASM_OP_I32_LT_U:
  6106. case WASM_OP_I32_GT_S:
  6107. case WASM_OP_I32_GT_U:
  6108. case WASM_OP_I32_LE_S:
  6109. case WASM_OP_I32_LE_U:
  6110. case WASM_OP_I32_GE_S:
  6111. case WASM_OP_I32_GE_U:
  6112. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6113. break;
  6114. case WASM_OP_I64_EQZ:
  6115. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6116. break;
  6117. case WASM_OP_I64_EQ:
  6118. case WASM_OP_I64_NE:
  6119. case WASM_OP_I64_LT_S:
  6120. case WASM_OP_I64_LT_U:
  6121. case WASM_OP_I64_GT_S:
  6122. case WASM_OP_I64_GT_U:
  6123. case WASM_OP_I64_LE_S:
  6124. case WASM_OP_I64_LE_U:
  6125. case WASM_OP_I64_GE_S:
  6126. case WASM_OP_I64_GE_U:
  6127. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6128. break;
  6129. case WASM_OP_F32_EQ:
  6130. case WASM_OP_F32_NE:
  6131. case WASM_OP_F32_LT:
  6132. case WASM_OP_F32_GT:
  6133. case WASM_OP_F32_LE:
  6134. case WASM_OP_F32_GE:
  6135. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6136. break;
  6137. case WASM_OP_F64_EQ:
  6138. case WASM_OP_F64_NE:
  6139. case WASM_OP_F64_LT:
  6140. case WASM_OP_F64_GT:
  6141. case WASM_OP_F64_LE:
  6142. case WASM_OP_F64_GE:
  6143. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6144. break;
  6145. case WASM_OP_I32_CLZ:
  6146. case WASM_OP_I32_CTZ:
  6147. case WASM_OP_I32_POPCNT:
  6148. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6149. break;
  6150. case WASM_OP_I32_ADD:
  6151. case WASM_OP_I32_SUB:
  6152. case WASM_OP_I32_MUL:
  6153. case WASM_OP_I32_DIV_S:
  6154. case WASM_OP_I32_DIV_U:
  6155. case WASM_OP_I32_REM_S:
  6156. case WASM_OP_I32_REM_U:
  6157. case WASM_OP_I32_AND:
  6158. case WASM_OP_I32_OR:
  6159. case WASM_OP_I32_XOR:
  6160. case WASM_OP_I32_SHL:
  6161. case WASM_OP_I32_SHR_S:
  6162. case WASM_OP_I32_SHR_U:
  6163. case WASM_OP_I32_ROTL:
  6164. case WASM_OP_I32_ROTR:
  6165. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6166. break;
  6167. case WASM_OP_I64_CLZ:
  6168. case WASM_OP_I64_CTZ:
  6169. case WASM_OP_I64_POPCNT:
  6170. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6171. break;
  6172. case WASM_OP_I64_ADD:
  6173. case WASM_OP_I64_SUB:
  6174. case WASM_OP_I64_MUL:
  6175. case WASM_OP_I64_DIV_S:
  6176. case WASM_OP_I64_DIV_U:
  6177. case WASM_OP_I64_REM_S:
  6178. case WASM_OP_I64_REM_U:
  6179. case WASM_OP_I64_AND:
  6180. case WASM_OP_I64_OR:
  6181. case WASM_OP_I64_XOR:
  6182. case WASM_OP_I64_SHL:
  6183. case WASM_OP_I64_SHR_S:
  6184. case WASM_OP_I64_SHR_U:
  6185. case WASM_OP_I64_ROTL:
  6186. case WASM_OP_I64_ROTR:
  6187. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6188. break;
  6189. case WASM_OP_F32_ABS:
  6190. case WASM_OP_F32_NEG:
  6191. case WASM_OP_F32_CEIL:
  6192. case WASM_OP_F32_FLOOR:
  6193. case WASM_OP_F32_TRUNC:
  6194. case WASM_OP_F32_NEAREST:
  6195. case WASM_OP_F32_SQRT:
  6196. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  6197. break;
  6198. case WASM_OP_F32_ADD:
  6199. case WASM_OP_F32_SUB:
  6200. case WASM_OP_F32_MUL:
  6201. case WASM_OP_F32_DIV:
  6202. case WASM_OP_F32_MIN:
  6203. case WASM_OP_F32_MAX:
  6204. case WASM_OP_F32_COPYSIGN:
  6205. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  6206. break;
  6207. case WASM_OP_F64_ABS:
  6208. case WASM_OP_F64_NEG:
  6209. case WASM_OP_F64_CEIL:
  6210. case WASM_OP_F64_FLOOR:
  6211. case WASM_OP_F64_TRUNC:
  6212. case WASM_OP_F64_NEAREST:
  6213. case WASM_OP_F64_SQRT:
  6214. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  6215. break;
  6216. case WASM_OP_F64_ADD:
  6217. case WASM_OP_F64_SUB:
  6218. case WASM_OP_F64_MUL:
  6219. case WASM_OP_F64_DIV:
  6220. case WASM_OP_F64_MIN:
  6221. case WASM_OP_F64_MAX:
  6222. case WASM_OP_F64_COPYSIGN:
  6223. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  6224. break;
  6225. case WASM_OP_I32_WRAP_I64:
  6226. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6227. break;
  6228. case WASM_OP_I32_TRUNC_S_F32:
  6229. case WASM_OP_I32_TRUNC_U_F32:
  6230. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6231. break;
  6232. case WASM_OP_I32_TRUNC_S_F64:
  6233. case WASM_OP_I32_TRUNC_U_F64:
  6234. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6235. break;
  6236. case WASM_OP_I64_EXTEND_S_I32:
  6237. case WASM_OP_I64_EXTEND_U_I32:
  6238. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  6239. break;
  6240. case WASM_OP_I64_TRUNC_S_F32:
  6241. case WASM_OP_I64_TRUNC_U_F32:
  6242. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  6243. break;
  6244. case WASM_OP_I64_TRUNC_S_F64:
  6245. case WASM_OP_I64_TRUNC_U_F64:
  6246. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6247. break;
  6248. case WASM_OP_F32_CONVERT_S_I32:
  6249. case WASM_OP_F32_CONVERT_U_I32:
  6250. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  6251. break;
  6252. case WASM_OP_F32_CONVERT_S_I64:
  6253. case WASM_OP_F32_CONVERT_U_I64:
  6254. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  6255. break;
  6256. case WASM_OP_F32_DEMOTE_F64:
  6257. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  6258. break;
  6259. case WASM_OP_F64_CONVERT_S_I32:
  6260. case WASM_OP_F64_CONVERT_U_I32:
  6261. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  6262. break;
  6263. case WASM_OP_F64_CONVERT_S_I64:
  6264. case WASM_OP_F64_CONVERT_U_I64:
  6265. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  6266. break;
  6267. case WASM_OP_F64_PROMOTE_F32:
  6268. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  6269. break;
  6270. case WASM_OP_I32_REINTERPRET_F32:
  6271. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6272. break;
  6273. case WASM_OP_I64_REINTERPRET_F64:
  6274. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6275. break;
  6276. case WASM_OP_F32_REINTERPRET_I32:
  6277. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  6278. break;
  6279. case WASM_OP_F64_REINTERPRET_I64:
  6280. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  6281. break;
  6282. case WASM_OP_I32_EXTEND8_S:
  6283. case WASM_OP_I32_EXTEND16_S:
  6284. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6285. break;
  6286. case WASM_OP_I64_EXTEND8_S:
  6287. case WASM_OP_I64_EXTEND16_S:
  6288. case WASM_OP_I64_EXTEND32_S:
  6289. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6290. break;
  6291. case WASM_OP_MISC_PREFIX:
  6292. {
  6293. uint32 opcode1;
  6294. read_leb_uint32(p, p_end, opcode1);
  6295. #if WASM_ENABLE_FAST_INTERP != 0
  6296. emit_byte(loader_ctx, ((uint8)opcode1));
  6297. #endif
  6298. switch (opcode1) {
  6299. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6300. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6301. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6302. break;
  6303. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6304. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6305. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6306. break;
  6307. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6308. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6309. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  6310. break;
  6311. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6312. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6313. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6314. break;
  6315. #if WASM_ENABLE_BULK_MEMORY != 0
  6316. case WASM_OP_MEMORY_INIT:
  6317. {
  6318. read_leb_uint32(p, p_end, segment_index);
  6319. #if WASM_ENABLE_FAST_INTERP != 0
  6320. emit_uint32(loader_ctx, segment_index);
  6321. #endif
  6322. bh_assert(module->import_memory_count
  6323. + module->memory_count
  6324. > 0);
  6325. bh_assert(*p == 0x00);
  6326. p++;
  6327. bh_assert(segment_index < module->data_seg_count);
  6328. bh_assert(module->data_seg_count1 > 0);
  6329. POP_I32();
  6330. POP_I32();
  6331. POP_I32();
  6332. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6333. func->has_memory_operations = true;
  6334. #endif
  6335. break;
  6336. }
  6337. case WASM_OP_DATA_DROP:
  6338. {
  6339. read_leb_uint32(p, p_end, segment_index);
  6340. #if WASM_ENABLE_FAST_INTERP != 0
  6341. emit_uint32(loader_ctx, segment_index);
  6342. #endif
  6343. bh_assert(segment_index < module->data_seg_count);
  6344. bh_assert(module->data_seg_count1 > 0);
  6345. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6346. func->has_memory_operations = true;
  6347. #endif
  6348. break;
  6349. }
  6350. case WASM_OP_MEMORY_COPY:
  6351. {
  6352. /* both src and dst memory index should be 0 */
  6353. bh_assert(*(int16 *)p == 0x0000);
  6354. p += 2;
  6355. bh_assert(module->import_memory_count
  6356. + module->memory_count
  6357. > 0);
  6358. POP_I32();
  6359. POP_I32();
  6360. POP_I32();
  6361. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6362. func->has_memory_operations = true;
  6363. #endif
  6364. break;
  6365. }
  6366. case WASM_OP_MEMORY_FILL:
  6367. {
  6368. bh_assert(*p == 0);
  6369. p++;
  6370. bh_assert(module->import_memory_count
  6371. + module->memory_count
  6372. > 0);
  6373. POP_I32();
  6374. POP_I32();
  6375. POP_I32();
  6376. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6377. func->has_memory_operations = true;
  6378. #endif
  6379. break;
  6380. }
  6381. #endif /* WASM_ENABLE_BULK_MEMORY */
  6382. #if WASM_ENABLE_REF_TYPES != 0
  6383. case WASM_OP_TABLE_INIT:
  6384. {
  6385. uint8 seg_ref_type, tbl_ref_type;
  6386. uint32 table_seg_idx, table_idx;
  6387. read_leb_uint32(p, p_end, table_seg_idx);
  6388. read_leb_uint32(p, p_end, table_idx);
  6389. if (!get_table_elem_type(module, table_idx,
  6390. &tbl_ref_type, error_buf,
  6391. error_buf_size))
  6392. goto fail;
  6393. if (!get_table_seg_elem_type(module, table_seg_idx,
  6394. &seg_ref_type, error_buf,
  6395. error_buf_size))
  6396. goto fail;
  6397. if (seg_ref_type != tbl_ref_type) {
  6398. set_error_buf(error_buf, error_buf_size,
  6399. "type mismatch");
  6400. goto fail;
  6401. }
  6402. #if WASM_ENABLE_FAST_INTERP != 0
  6403. emit_uint32(loader_ctx, table_seg_idx);
  6404. emit_uint32(loader_ctx, table_idx);
  6405. #endif
  6406. POP_I32();
  6407. POP_I32();
  6408. POP_I32();
  6409. break;
  6410. }
  6411. case WASM_OP_ELEM_DROP:
  6412. {
  6413. uint32 table_seg_idx;
  6414. read_leb_uint32(p, p_end, table_seg_idx);
  6415. if (!get_table_seg_elem_type(module, table_seg_idx,
  6416. NULL, error_buf,
  6417. error_buf_size))
  6418. goto fail;
  6419. #if WASM_ENABLE_FAST_INTERP != 0
  6420. emit_uint32(loader_ctx, table_seg_idx);
  6421. #endif
  6422. break;
  6423. }
  6424. case WASM_OP_TABLE_COPY:
  6425. {
  6426. uint8 src_ref_type, dst_ref_type;
  6427. uint32 src_tbl_idx, dst_tbl_idx;
  6428. read_leb_uint32(p, p_end, src_tbl_idx);
  6429. if (!get_table_elem_type(module, src_tbl_idx,
  6430. &src_ref_type, error_buf,
  6431. error_buf_size))
  6432. goto fail;
  6433. read_leb_uint32(p, p_end, dst_tbl_idx);
  6434. if (!get_table_elem_type(module, dst_tbl_idx,
  6435. &dst_ref_type, error_buf,
  6436. error_buf_size))
  6437. goto fail;
  6438. if (src_ref_type != dst_ref_type) {
  6439. set_error_buf(error_buf, error_buf_size,
  6440. "type mismatch");
  6441. goto fail;
  6442. }
  6443. #if WASM_ENABLE_FAST_INTERP != 0
  6444. emit_uint32(loader_ctx, src_tbl_idx);
  6445. emit_uint32(loader_ctx, dst_tbl_idx);
  6446. #endif
  6447. POP_I32();
  6448. POP_I32();
  6449. POP_I32();
  6450. break;
  6451. }
  6452. case WASM_OP_TABLE_SIZE:
  6453. {
  6454. uint32 table_idx;
  6455. read_leb_uint32(p, p_end, table_idx);
  6456. /* TODO: shall we create a new function to check
  6457. table idx instead of using below function? */
  6458. if (!get_table_elem_type(module, table_idx, NULL,
  6459. error_buf, error_buf_size))
  6460. goto fail;
  6461. #if WASM_ENABLE_FAST_INTERP != 0
  6462. emit_uint32(loader_ctx, table_idx);
  6463. #endif
  6464. PUSH_I32();
  6465. break;
  6466. }
  6467. case WASM_OP_TABLE_GROW:
  6468. case WASM_OP_TABLE_FILL:
  6469. {
  6470. uint8 decl_ref_type;
  6471. uint32 table_idx;
  6472. read_leb_uint32(p, p_end, table_idx);
  6473. if (!get_table_elem_type(module, table_idx,
  6474. &decl_ref_type, error_buf,
  6475. error_buf_size))
  6476. goto fail;
  6477. if (opcode1 == WASM_OP_TABLE_GROW) {
  6478. if (table_idx < module->import_table_count) {
  6479. module->import_tables[table_idx]
  6480. .u.table.possible_grow = true;
  6481. }
  6482. else {
  6483. module
  6484. ->tables[table_idx
  6485. - module->import_table_count]
  6486. .possible_grow = true;
  6487. }
  6488. }
  6489. #if WASM_ENABLE_FAST_INTERP != 0
  6490. emit_uint32(loader_ctx, table_idx);
  6491. #endif
  6492. POP_I32();
  6493. #if WASM_ENABLE_FAST_INTERP != 0
  6494. POP_OFFSET_TYPE(decl_ref_type);
  6495. #endif
  6496. POP_TYPE(decl_ref_type);
  6497. if (opcode1 == WASM_OP_TABLE_GROW)
  6498. PUSH_I32();
  6499. else
  6500. POP_I32();
  6501. break;
  6502. }
  6503. #endif /* WASM_ENABLE_REF_TYPES */
  6504. default:
  6505. bh_assert(0);
  6506. break;
  6507. }
  6508. break;
  6509. }
  6510. #if WASM_ENABLE_SHARED_MEMORY != 0
  6511. case WASM_OP_ATOMIC_PREFIX:
  6512. {
  6513. opcode = read_uint8(p);
  6514. #if WASM_ENABLE_FAST_INTERP != 0
  6515. emit_byte(loader_ctx, opcode);
  6516. #endif
  6517. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6518. CHECK_MEMORY();
  6519. read_leb_uint32(p, p_end, align); /* align */
  6520. read_leb_uint32(p, p_end, mem_offset); /* offset */
  6521. #if WASM_ENABLE_FAST_INTERP != 0
  6522. emit_uint32(loader_ctx, mem_offset);
  6523. #endif
  6524. }
  6525. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6526. func->has_memory_operations = true;
  6527. #endif
  6528. switch (opcode) {
  6529. case WASM_OP_ATOMIC_NOTIFY:
  6530. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6531. break;
  6532. case WASM_OP_ATOMIC_WAIT32:
  6533. POP_I64();
  6534. POP_I32();
  6535. POP_I32();
  6536. PUSH_I32();
  6537. break;
  6538. case WASM_OP_ATOMIC_WAIT64:
  6539. POP_I64();
  6540. POP_I64();
  6541. POP_I32();
  6542. PUSH_I32();
  6543. break;
  6544. case WASM_OP_ATOMIC_FENCE:
  6545. /* reserved byte 0x00 */
  6546. bh_assert(*p == 0x00);
  6547. p++;
  6548. break;
  6549. case WASM_OP_ATOMIC_I32_LOAD:
  6550. case WASM_OP_ATOMIC_I32_LOAD8_U:
  6551. case WASM_OP_ATOMIC_I32_LOAD16_U:
  6552. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6553. break;
  6554. case WASM_OP_ATOMIC_I32_STORE:
  6555. case WASM_OP_ATOMIC_I32_STORE8:
  6556. case WASM_OP_ATOMIC_I32_STORE16:
  6557. POP_I32();
  6558. POP_I32();
  6559. break;
  6560. case WASM_OP_ATOMIC_I64_LOAD:
  6561. case WASM_OP_ATOMIC_I64_LOAD8_U:
  6562. case WASM_OP_ATOMIC_I64_LOAD16_U:
  6563. case WASM_OP_ATOMIC_I64_LOAD32_U:
  6564. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  6565. break;
  6566. case WASM_OP_ATOMIC_I64_STORE:
  6567. case WASM_OP_ATOMIC_I64_STORE8:
  6568. case WASM_OP_ATOMIC_I64_STORE16:
  6569. case WASM_OP_ATOMIC_I64_STORE32:
  6570. POP_I64();
  6571. POP_I32();
  6572. break;
  6573. case WASM_OP_ATOMIC_RMW_I32_ADD:
  6574. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  6575. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  6576. case WASM_OP_ATOMIC_RMW_I32_SUB:
  6577. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  6578. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  6579. case WASM_OP_ATOMIC_RMW_I32_AND:
  6580. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  6581. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  6582. case WASM_OP_ATOMIC_RMW_I32_OR:
  6583. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  6584. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  6585. case WASM_OP_ATOMIC_RMW_I32_XOR:
  6586. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  6587. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  6588. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  6589. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  6590. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  6591. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6592. break;
  6593. case WASM_OP_ATOMIC_RMW_I64_ADD:
  6594. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  6595. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  6596. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  6597. case WASM_OP_ATOMIC_RMW_I64_SUB:
  6598. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  6599. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  6600. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  6601. case WASM_OP_ATOMIC_RMW_I64_AND:
  6602. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  6603. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  6604. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  6605. case WASM_OP_ATOMIC_RMW_I64_OR:
  6606. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  6607. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  6608. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  6609. case WASM_OP_ATOMIC_RMW_I64_XOR:
  6610. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  6611. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  6612. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  6613. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  6614. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  6615. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  6616. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  6617. POP_I64();
  6618. POP_I32();
  6619. PUSH_I64();
  6620. break;
  6621. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  6622. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  6623. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  6624. POP_I32();
  6625. POP_I32();
  6626. POP_I32();
  6627. PUSH_I32();
  6628. break;
  6629. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  6630. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  6631. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  6632. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  6633. POP_I64();
  6634. POP_I64();
  6635. POP_I32();
  6636. PUSH_I64();
  6637. break;
  6638. default:
  6639. bh_assert(0);
  6640. break;
  6641. }
  6642. break;
  6643. }
  6644. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  6645. default:
  6646. bh_assert(0);
  6647. break;
  6648. }
  6649. #if WASM_ENABLE_FAST_INTERP != 0
  6650. last_op = opcode;
  6651. #endif
  6652. }
  6653. if (loader_ctx->csp_num > 0) {
  6654. set_error_buf(error_buf, error_buf_size,
  6655. "function body must end with END opcode");
  6656. goto fail;
  6657. }
  6658. #if WASM_ENABLE_FAST_INTERP != 0
  6659. if (loader_ctx->p_code_compiled == NULL)
  6660. goto re_scan;
  6661. func->const_cell_num = loader_ctx->const_cell_num;
  6662. if (func->const_cell_num > 0) {
  6663. int32 j;
  6664. if (!(func->consts = func_const = loader_malloc(
  6665. func->const_cell_num * 4, error_buf, error_buf_size)))
  6666. goto fail;
  6667. func_const_end = func->consts + func->const_cell_num * 4;
  6668. /* reverse the const buf */
  6669. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  6670. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  6671. if (c->value_type == VALUE_TYPE_F64
  6672. || c->value_type == VALUE_TYPE_I64) {
  6673. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  6674. &(c->value.f64), (uint32)sizeof(int64));
  6675. func_const += sizeof(int64);
  6676. }
  6677. else {
  6678. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  6679. &(c->value.f32), (uint32)sizeof(int32));
  6680. func_const += sizeof(int32);
  6681. }
  6682. }
  6683. }
  6684. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  6685. - loader_ctx->start_dynamic_offset + 1;
  6686. #else
  6687. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  6688. #endif
  6689. func->max_block_num = loader_ctx->max_csp_num;
  6690. return_value = true;
  6691. fail:
  6692. wasm_loader_ctx_destroy(loader_ctx);
  6693. (void)u8;
  6694. (void)u32;
  6695. (void)i32;
  6696. (void)i64_const;
  6697. (void)global_count;
  6698. (void)local_count;
  6699. (void)local_offset;
  6700. (void)p_org;
  6701. (void)mem_offset;
  6702. (void)align;
  6703. #if WASM_ENABLE_BULK_MEMORY != 0
  6704. (void)segment_index;
  6705. #endif
  6706. return return_value;
  6707. }