wasm_mini_loader.c 285 KB

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