wasm_mini_loader.c 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538
  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. /* Read a value of given type from the address pointed to by the given
  13. pointer and increase the pointer to the position just after the
  14. value being read. */
  15. #define TEMPLATE_READ_VALUE(Type, p) \
  16. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  17. static void
  18. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  19. {
  20. if (error_buf != NULL)
  21. snprintf(error_buf, error_buf_size, "%s", string);
  22. }
  23. #define CHECK_BUF(buf, buf_end, length) do { \
  24. bh_assert(buf + length <= buf_end); \
  25. } while (0)
  26. #define CHECK_BUF1(buf, buf_end, length) do { \
  27. bh_assert(buf + length <= buf_end); \
  28. } while (0)
  29. static void
  30. skip_leb(const uint8 **p_buf, const uint8 *buf_end, uint32 maxbits,
  31. char* error_buf, uint32 error_buf_size)
  32. {
  33. const uint8 *buf = *p_buf;
  34. uint32 offset = 0, bcnt = 0;
  35. uint64 byte;
  36. while (true) {
  37. bh_assert(bcnt + 1 <= (maxbits + 6) / 7);
  38. CHECK_BUF(buf, buf_end, offset + 1);
  39. byte = buf[offset];
  40. offset += 1;
  41. bcnt += 1;
  42. if ((byte & 0x80) == 0) {
  43. break;
  44. }
  45. }
  46. *p_buf += offset;
  47. }
  48. #define skip_leb_int64(p, p_end) do { \
  49. skip_leb(&p, p_end, 64, \
  50. error_buf, error_buf_size); \
  51. } while (0)
  52. #define skip_leb_uint32(p, p_end) do { \
  53. skip_leb(&p, p_end, 32, \
  54. error_buf, error_buf_size); \
  55. } while (0)
  56. #define skip_leb_int32(p, p_end) do { \
  57. skip_leb(&p, p_end, 32, \
  58. error_buf, error_buf_size); \
  59. } while (0)
  60. static void
  61. read_leb(uint8 **p_buf, const uint8 *buf_end,
  62. uint32 maxbits, bool sign, uint64 *p_result,
  63. char* error_buf, uint32 error_buf_size)
  64. {
  65. const uint8 *buf = *p_buf;
  66. uint64 result = 0;
  67. uint32 shift = 0;
  68. uint32 offset = 0, bcnt = 0;
  69. uint64 byte;
  70. while (true) {
  71. bh_assert(bcnt + 1 <= (maxbits + 6) / 7);
  72. CHECK_BUF(buf, buf_end, offset + 1);
  73. byte = buf[offset];
  74. offset += 1;
  75. result |= ((byte & 0x7f) << shift);
  76. shift += 7;
  77. bcnt += 1;
  78. if ((byte & 0x80) == 0) {
  79. break;
  80. }
  81. }
  82. if (!sign && maxbits == 32 && shift >= maxbits) {
  83. /* The top bits set represent values > 32 bits */
  84. bh_assert(!(((uint8)byte) & 0xf0));
  85. }
  86. else if (sign && maxbits == 32) {
  87. if (shift < maxbits) {
  88. /* Sign extend */
  89. result = (((int32)result) << (maxbits - shift))
  90. >> (maxbits - shift);
  91. }
  92. else {
  93. /* The top bits should be a sign-extension of the sign bit */
  94. bool sign_bit_set = ((uint8)byte) & 0x8;
  95. int top_bits = ((uint8)byte) & 0xf0;
  96. bh_assert(!((sign_bit_set && top_bits != 0x70)
  97. || (!sign_bit_set && top_bits != 0)));
  98. (void)top_bits;
  99. (void)sign_bit_set;
  100. }
  101. }
  102. else if (sign && maxbits == 64) {
  103. if (shift < maxbits) {
  104. /* Sign extend */
  105. result = (((int64)result) << (maxbits - shift))
  106. >> (maxbits - shift);
  107. }
  108. else {
  109. /* The top bits should be a sign-extension of the sign bit */
  110. bool sign_bit_set = ((uint8)byte) & 0x1;
  111. int top_bits = ((uint8)byte) & 0xfe;
  112. bh_assert(!((sign_bit_set && top_bits != 0x7e)
  113. || (!sign_bit_set && top_bits != 0)));
  114. (void)top_bits;
  115. (void)sign_bit_set;
  116. }
  117. }
  118. *p_buf += offset;
  119. *p_result = result;
  120. }
  121. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  122. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  123. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  124. #define read_leb_int64(p, p_end, res) do { \
  125. uint64 res64; \
  126. read_leb((uint8**)&p, p_end, 64, true, &res64, \
  127. error_buf, error_buf_size); \
  128. res = (int64)res64; \
  129. } while (0)
  130. #define read_leb_uint32(p, p_end, res) do { \
  131. uint64 res64; \
  132. read_leb((uint8**)&p, p_end, 32, false, &res64, \
  133. error_buf, error_buf_size); \
  134. res = (uint32)res64; \
  135. } while (0)
  136. #define read_leb_int32(p, p_end, res) do { \
  137. uint64 res64; \
  138. read_leb((uint8**)&p, p_end, 32, true, &res64, \
  139. error_buf, error_buf_size); \
  140. res = (int32)res64; \
  141. } while (0)
  142. static void *
  143. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  144. {
  145. void *mem;
  146. if (size >= UINT32_MAX
  147. || !(mem = wasm_runtime_malloc((uint32)size))) {
  148. set_error_buf(error_buf, error_buf_size,
  149. "WASM module load failed: "
  150. "allocate memory failed.");
  151. return NULL;
  152. }
  153. memset(mem, 0, (uint32)size);
  154. return mem;
  155. }
  156. static char *
  157. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  158. char *error_buf, uint32 error_buf_size)
  159. {
  160. StringNode *node, *node_next;
  161. /* Search const str list */
  162. node = module->const_str_list;
  163. while (node) {
  164. node_next = node->next;
  165. if (strlen(node->str) == len
  166. && !memcmp(node->str, str, len))
  167. break;
  168. node = node_next;
  169. }
  170. if (node) {
  171. LOG_DEBUG("reuse %s", node->str);
  172. return node->str;
  173. }
  174. if (!(node = loader_malloc(sizeof(StringNode) + len + 1,
  175. error_buf, error_buf_size))) {
  176. return NULL;
  177. }
  178. node->str = ((char*)node) + sizeof(StringNode);
  179. bh_memcpy_s(node->str, len + 1, str, len);
  180. node->str[len] = '\0';
  181. if (!module->const_str_list) {
  182. /* set as head */
  183. module->const_str_list = node;
  184. node->next = NULL;
  185. }
  186. else {
  187. /* insert it */
  188. node->next = module->const_str_list;
  189. module->const_str_list = node;
  190. }
  191. return node->str;
  192. }
  193. static bool
  194. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  195. InitializerExpression *init_expr, uint8 type,
  196. char *error_buf, uint32 error_buf_size)
  197. {
  198. const uint8 *p = *p_buf, *p_end = buf_end;
  199. uint8 flag, end_byte, *p_float;
  200. uint32 i;
  201. CHECK_BUF(p, p_end, 1);
  202. init_expr->init_expr_type = read_uint8(p);
  203. flag = init_expr->init_expr_type;
  204. switch (flag) {
  205. /* i32.const */
  206. case INIT_EXPR_TYPE_I32_CONST:
  207. bh_assert(type == VALUE_TYPE_I32);
  208. read_leb_int32(p, p_end, init_expr->u.i32);
  209. break;
  210. /* i64.const */
  211. case INIT_EXPR_TYPE_I64_CONST:
  212. bh_assert(type == VALUE_TYPE_I64);
  213. read_leb_int64(p, p_end, init_expr->u.i64);
  214. break;
  215. /* f32.const */
  216. case INIT_EXPR_TYPE_F32_CONST:
  217. bh_assert(type == VALUE_TYPE_F32);
  218. CHECK_BUF(p, p_end, 4);
  219. p_float = (uint8*)&init_expr->u.f32;
  220. for (i = 0; i < sizeof(float32); i++)
  221. *p_float++ = *p++;
  222. break;
  223. /* f64.const */
  224. case INIT_EXPR_TYPE_F64_CONST:
  225. bh_assert(type == VALUE_TYPE_F64);
  226. CHECK_BUF(p, p_end, 8);
  227. p_float = (uint8*)&init_expr->u.f64;
  228. for (i = 0; i < sizeof(float64); i++)
  229. *p_float++ = *p++;
  230. break;
  231. /* get_global */
  232. case INIT_EXPR_TYPE_GET_GLOBAL:
  233. read_leb_uint32(p, p_end, init_expr->u.global_index);
  234. break;
  235. default:
  236. bh_assert(0);
  237. break;
  238. }
  239. CHECK_BUF(p, p_end, 1);
  240. end_byte = read_uint8(p);
  241. bh_assert(end_byte == 0x0b);
  242. *p_buf = p;
  243. (void)end_byte;
  244. return true;
  245. }
  246. static bool
  247. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  248. char *error_buf, uint32 error_buf_size)
  249. {
  250. const uint8 *p = buf, *p_end = buf_end, *p_org;
  251. uint32 type_count, param_count, result_count, i, j;
  252. uint32 param_cell_num, ret_cell_num;
  253. uint64 total_size;
  254. uint8 flag;
  255. WASMType *type;
  256. read_leb_uint32(p, p_end, type_count);
  257. if (type_count) {
  258. module->type_count = type_count;
  259. total_size = sizeof(WASMType*) * (uint64)type_count;
  260. if (!(module->types = loader_malloc
  261. (total_size, error_buf, error_buf_size))) {
  262. return false;
  263. }
  264. for (i = 0; i < type_count; i++) {
  265. CHECK_BUF(p, p_end, 1);
  266. flag = read_uint8(p);
  267. bh_assert(flag == 0x60);
  268. read_leb_uint32(p, p_end, param_count);
  269. /* Resolve param count and result count firstly */
  270. p_org = p;
  271. CHECK_BUF(p, p_end, param_count);
  272. p += param_count;
  273. read_leb_uint32(p, p_end, result_count);
  274. CHECK_BUF(p, p_end, result_count);
  275. p = p_org;
  276. bh_assert(param_count <= UINT16_MAX && result_count <= UINT16_MAX);
  277. total_size = offsetof(WASMType, types) +
  278. sizeof(uint8) * (uint64)(param_count + result_count);
  279. if (!(type = module->types[i] =
  280. loader_malloc(total_size, error_buf, error_buf_size))) {
  281. return false;
  282. }
  283. /* Resolve param types and result types */
  284. type->param_count = (uint16)param_count;
  285. type->result_count = (uint16)result_count;
  286. for (j = 0; j < param_count; j++) {
  287. CHECK_BUF(p, p_end, 1);
  288. type->types[j] = read_uint8(p);
  289. }
  290. read_leb_uint32(p, p_end, result_count);
  291. for (j = 0; j < result_count; j++) {
  292. CHECK_BUF(p, p_end, 1);
  293. type->types[param_count + j] = read_uint8(p);
  294. }
  295. param_cell_num = wasm_get_cell_num(type->types, param_count);
  296. ret_cell_num = wasm_get_cell_num(type->types + param_count,
  297. result_count);
  298. bh_assert(param_cell_num <= UINT16_MAX && ret_cell_num <= UINT16_MAX);
  299. type->param_cell_num = (uint16)param_cell_num;
  300. type->ret_cell_num = (uint16)ret_cell_num;
  301. }
  302. }
  303. bh_assert(p == p_end);
  304. LOG_VERBOSE("Load type section success.\n");
  305. (void)flag;
  306. return true;
  307. }
  308. static bool
  309. load_function_import(const WASMModule *parent_module, WASMModule *sub_module,
  310. char *sub_module_name, char *function_name,
  311. const uint8 **p_buf, const uint8 *buf_end,
  312. WASMFunctionImport *function,
  313. char *error_buf, uint32 error_buf_size)
  314. {
  315. const uint8 *p = *p_buf, *p_end = buf_end;
  316. uint32 declare_type_index = 0;
  317. WASMType *declare_func_type = NULL;
  318. WASMFunction *linked_func = NULL;
  319. const char *linked_signature = NULL;
  320. void *linked_attachment = NULL;
  321. bool linked_call_conv_raw = false;
  322. bool is_built_in_module = false;
  323. CHECK_BUF(p, p_end, 1);
  324. read_leb_uint32(p, p_end, declare_type_index);
  325. *p_buf = p;
  326. bh_assert(declare_type_index < parent_module->type_count);
  327. declare_func_type = parent_module->types[declare_type_index];
  328. is_built_in_module = wasm_runtime_is_built_in_module(sub_module_name);
  329. if (is_built_in_module) {
  330. LOG_DEBUG("%s is a function of a built-in module %s",
  331. function_name,
  332. sub_module_name);
  333. /* check built-in modules */
  334. linked_func = wasm_native_resolve_symbol(sub_module_name,
  335. function_name,
  336. declare_func_type,
  337. &linked_signature,
  338. &linked_attachment,
  339. &linked_call_conv_raw);
  340. }
  341. if (!linked_func) {
  342. #if WASM_ENABLE_SPEC_TEST != 0
  343. set_error_buf(error_buf,
  344. error_buf_size,
  345. "unknown import or incompatible import type");
  346. return false;
  347. #else
  348. #if WASM_ENABLE_WAMR_COMPILER == 0
  349. LOG_WARNING(
  350. "warning: fail to link import function (%s, %s)",
  351. sub_module_name, function_name);
  352. #endif
  353. #endif
  354. }
  355. function->module_name = sub_module_name;
  356. function->field_name = function_name;
  357. function->func_type = declare_func_type;
  358. /* func_ptr_linked is for built-in functions */
  359. function->func_ptr_linked = is_built_in_module ? linked_func : NULL;
  360. function->signature = linked_signature;
  361. function->attachment = linked_attachment;
  362. function->call_conv_raw = linked_call_conv_raw;
  363. return true;
  364. }
  365. static bool
  366. load_table_import(WASMModule *sub_module, const char *sub_module_name,
  367. const char *table_name, const uint8 **p_buf,
  368. const uint8 *buf_end, WASMTableImport *table,
  369. char *error_buf, uint32 error_buf_size)
  370. {
  371. const uint8 *p = *p_buf, *p_end = buf_end;
  372. uint32 declare_elem_type = 0;
  373. uint32 declare_max_size_flag = 0;
  374. uint32 declare_init_size = 0;
  375. uint32 declare_max_size = 0;
  376. CHECK_BUF(p, p_end, 1);
  377. /* 0x70 */
  378. declare_elem_type = read_uint8(p);
  379. bh_assert(TABLE_ELEM_TYPE_ANY_FUNC == declare_elem_type);
  380. read_leb_uint32(p, p_end, declare_max_size_flag);
  381. read_leb_uint32(p, p_end, declare_init_size);
  382. if (declare_max_size_flag & 1) {
  383. read_leb_uint32(p, p_end, declare_max_size);
  384. bh_assert(table->init_size <= table->max_size);
  385. } else {
  386. declare_max_size = 0x10000;
  387. }
  388. *p_buf = p;
  389. bh_assert(!((declare_max_size_flag & 1)
  390. && declare_init_size > declare_max_size));
  391. /* now we believe all declaration are ok */
  392. table->elem_type = declare_elem_type;
  393. table->init_size = declare_init_size;
  394. table->flags = declare_max_size_flag;
  395. table->max_size = declare_max_size;
  396. return true;
  397. }
  398. unsigned
  399. wasm_runtime_memory_pool_size();
  400. static bool
  401. load_memory_import(WASMModule *sub_module, const char *sub_module_name,
  402. const char *memory_name, const uint8 **p_buf,
  403. const uint8 *buf_end, WASMMemoryImport *memory,
  404. char *error_buf, uint32 error_buf_size)
  405. {
  406. const uint8 *p = *p_buf, *p_end = buf_end;
  407. uint32 pool_size = wasm_runtime_memory_pool_size();
  408. #if WASM_ENABLE_APP_FRAMEWORK != 0
  409. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  410. / DEFAULT_NUM_BYTES_PER_PAGE;
  411. #else
  412. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  413. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  414. uint32 declare_max_page_count_flag = 0;
  415. uint32 declare_init_page_count = 0;
  416. uint32 declare_max_page_count = 0;
  417. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  418. read_leb_uint32(p, p_end, declare_init_page_count);
  419. bh_assert(declare_init_page_count <= 65536);
  420. if (declare_max_page_count_flag & 1) {
  421. read_leb_uint32(p, p_end, declare_max_page_count);
  422. bh_assert(declare_init_page_count <= declare_max_page_count);
  423. bh_assert(declare_max_page_count <= 65536);
  424. if (declare_max_page_count > max_page_count) {
  425. declare_max_page_count = max_page_count;
  426. }
  427. }
  428. else {
  429. /* Limit the maximum memory size to max_page_count */
  430. declare_max_page_count = max_page_count;
  431. }
  432. /* now we believe all declaration are ok */
  433. memory->flags = declare_max_page_count_flag;
  434. memory->init_page_count = declare_init_page_count;
  435. memory->max_page_count = declare_max_page_count;
  436. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  437. *p_buf = p;
  438. return true;
  439. }
  440. static bool
  441. load_global_import(const WASMModule *parent_module,
  442. WASMModule *sub_module,
  443. char *sub_module_name, char *global_name,
  444. const uint8 **p_buf, const uint8 *buf_end,
  445. WASMGlobalImport *global,
  446. char *error_buf, uint32 error_buf_size)
  447. {
  448. const uint8 *p = *p_buf, *p_end = buf_end;
  449. uint8 declare_type = 0;
  450. uint8 declare_mutable = 0;
  451. bool is_mutable = false;
  452. bool ret = false;
  453. CHECK_BUF(p, p_end, 2);
  454. declare_type = read_uint8(p);
  455. declare_mutable = read_uint8(p);
  456. *p_buf = p;
  457. bh_assert(declare_mutable < 2);
  458. is_mutable = declare_mutable & 1 ? true : false;
  459. #if WASM_ENABLE_LIBC_BUILTIN != 0
  460. ret = wasm_runtime_is_built_in_module(sub_module_name);
  461. if (ret) {
  462. /* check built-in modules */
  463. ret = wasm_native_lookup_libc_builtin_global(sub_module_name,
  464. global_name, global);
  465. if (ret) {
  466. LOG_DEBUG("(%s, %s) is a global of a built-in module",
  467. sub_module_name, global_name);
  468. }
  469. }
  470. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  471. if (!ret) {
  472. set_error_buf_v(error_buf, error_buf_size,
  473. "unknown import or incompatible import type");
  474. return false;
  475. }
  476. global->module_name = sub_module_name;
  477. global->field_name = global_name;
  478. global->type = declare_type;
  479. global->is_mutable = is_mutable;
  480. (void)p_end;
  481. return true;
  482. }
  483. static bool
  484. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  485. char *error_buf, uint32 error_buf_size)
  486. {
  487. const uint8 *p = *p_buf, *p_end = buf_end;
  488. CHECK_BUF(p, p_end, 1);
  489. /* 0x70 */
  490. table->elem_type = read_uint8(p);
  491. bh_assert(TABLE_ELEM_TYPE_ANY_FUNC == table->elem_type);
  492. read_leb_uint32(p, p_end, table->flags);
  493. read_leb_uint32(p, p_end, table->init_size);
  494. if (table->flags & 1) {
  495. read_leb_uint32(p, p_end, table->max_size);
  496. bh_assert(table->init_size <= table->max_size);
  497. }
  498. else
  499. table->max_size = 0x10000;
  500. bh_assert(!((table->flags & 1)
  501. && table->init_size > table->max_size));
  502. *p_buf = p;
  503. return true;
  504. }
  505. static bool
  506. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  507. char *error_buf, uint32 error_buf_size)
  508. {
  509. const uint8 *p = *p_buf, *p_end = buf_end;
  510. uint32 pool_size = wasm_runtime_memory_pool_size();
  511. #if WASM_ENABLE_APP_FRAMEWORK != 0
  512. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  513. / DEFAULT_NUM_BYTES_PER_PAGE;
  514. #else
  515. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  516. #endif
  517. read_leb_uint32(p, p_end, memory->flags);
  518. read_leb_uint32(p, p_end, memory->init_page_count);
  519. bh_assert(memory->init_page_count <= 65536);
  520. if (memory->flags & 1) {
  521. read_leb_uint32(p, p_end, memory->max_page_count);
  522. bh_assert(memory->init_page_count <= memory->max_page_count);
  523. bh_assert(memory->max_page_count <= 65536);
  524. if (memory->max_page_count > max_page_count)
  525. memory->max_page_count = max_page_count;
  526. }
  527. else
  528. /* Limit the maximum memory size to max_page_count */
  529. memory->max_page_count = max_page_count;
  530. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  531. *p_buf = p;
  532. return true;
  533. }
  534. static bool
  535. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  536. char *error_buf, uint32 error_buf_size)
  537. {
  538. const uint8 *p = buf, *p_end = buf_end, *p_old;
  539. uint32 import_count, name_len, type_index, i, u32, flags;
  540. uint64 total_size;
  541. WASMImport *import;
  542. WASMImport *import_functions = NULL, *import_tables = NULL;
  543. WASMImport *import_memories = NULL, *import_globals = NULL;
  544. char *sub_module_name, *field_name;
  545. uint8 u8, kind;
  546. read_leb_uint32(p, p_end, import_count);
  547. if (import_count) {
  548. module->import_count = import_count;
  549. total_size = sizeof(WASMImport) * (uint64)import_count;
  550. if (!(module->imports = loader_malloc
  551. (total_size, error_buf, error_buf_size))) {
  552. return false;
  553. }
  554. p_old = p;
  555. /* Scan firstly to get import count of each type */
  556. for (i = 0; i < import_count; i++) {
  557. /* module name */
  558. read_leb_uint32(p, p_end, name_len);
  559. CHECK_BUF(p, p_end, name_len);
  560. p += name_len;
  561. /* field name */
  562. read_leb_uint32(p, p_end, name_len);
  563. CHECK_BUF(p, p_end, name_len);
  564. p += name_len;
  565. CHECK_BUF(p, p_end, 1);
  566. /* 0x00/0x01/0x02/0x03 */
  567. kind = read_uint8(p);
  568. switch (kind) {
  569. case IMPORT_KIND_FUNC: /* import function */
  570. read_leb_uint32(p, p_end, type_index);
  571. module->import_function_count++;
  572. break;
  573. case IMPORT_KIND_TABLE: /* import table */
  574. CHECK_BUF(p, p_end, 1);
  575. /* 0x70 */
  576. u8 = read_uint8(p);
  577. read_leb_uint32(p, p_end, flags);
  578. read_leb_uint32(p, p_end, u32);
  579. if (flags & 1)
  580. read_leb_uint32(p, p_end, u32);
  581. module->import_table_count++;
  582. bh_assert(module->import_table_count <= 1);
  583. break;
  584. case IMPORT_KIND_MEMORY: /* import memory */
  585. read_leb_uint32(p, p_end, flags);
  586. read_leb_uint32(p, p_end, u32);
  587. if (flags & 1)
  588. read_leb_uint32(p, p_end, u32);
  589. module->import_memory_count++;
  590. bh_assert(module->import_memory_count <= 1);
  591. break;
  592. case IMPORT_KIND_GLOBAL: /* import global */
  593. CHECK_BUF(p, p_end, 2);
  594. p += 2;
  595. module->import_global_count++;
  596. break;
  597. default:
  598. bh_assert(0);
  599. break;
  600. }
  601. }
  602. if (module->import_function_count)
  603. import_functions = module->import_functions = module->imports;
  604. if (module->import_table_count)
  605. import_tables = module->import_tables =
  606. module->imports + module->import_function_count;
  607. if (module->import_memory_count)
  608. import_memories = module->import_memories =
  609. module->imports + module->import_function_count + module->import_table_count;
  610. if (module->import_global_count)
  611. import_globals = module->import_globals =
  612. module->imports + module->import_function_count + module->import_table_count
  613. + module->import_memory_count;
  614. p = p_old;
  615. // TODO: move it out of the loop
  616. /* insert "env", "wasi_unstable" and "wasi_snapshot_preview1" to const str list */
  617. if (!const_str_list_insert((uint8*)"env", 3, module, error_buf, error_buf_size)
  618. || !const_str_list_insert((uint8*)"wasi_unstable", 13, module,
  619. error_buf, error_buf_size)
  620. || !const_str_list_insert((uint8*)"wasi_snapshot_preview1", 22, module,
  621. error_buf, error_buf_size)) {
  622. return false;
  623. }
  624. /* Scan again to read the data */
  625. for (i = 0; i < import_count; i++) {
  626. WASMModule *sub_module = NULL;
  627. /* load module name */
  628. read_leb_uint32(p, p_end, name_len);
  629. CHECK_BUF(p, p_end, name_len);
  630. if (!(sub_module_name = const_str_list_insert(
  631. p, name_len, module, error_buf, error_buf_size))) {
  632. return false;
  633. }
  634. p += name_len;
  635. /* load field name */
  636. read_leb_uint32(p, p_end, name_len);
  637. CHECK_BUF(p, p_end, name_len);
  638. if (!(field_name = const_str_list_insert(
  639. p, name_len, module, error_buf, error_buf_size))) {
  640. return false;
  641. }
  642. p += name_len;
  643. LOG_DEBUG("import #%d: (%s, %s)", i, sub_module_name, field_name);
  644. CHECK_BUF(p, p_end, 1);
  645. /* 0x00/0x01/0x02/0x03 */
  646. kind = read_uint8(p);
  647. switch (kind) {
  648. case IMPORT_KIND_FUNC: /* import function */
  649. bh_assert(import_functions);
  650. import = import_functions++;
  651. if (!load_function_import(module, sub_module,
  652. sub_module_name, field_name, &p,
  653. p_end, &import->u.function,
  654. error_buf, error_buf_size)) {
  655. return false;
  656. }
  657. break;
  658. case IMPORT_KIND_TABLE: /* import table */
  659. bh_assert(import_tables);
  660. import = import_tables++;
  661. if (!load_table_import(sub_module,
  662. sub_module_name,
  663. field_name,
  664. &p,
  665. p_end,
  666. &import->u.table,
  667. error_buf,
  668. error_buf_size)) {
  669. LOG_DEBUG("can not import such a table (%s,%s)",
  670. sub_module_name, field_name);
  671. return false;
  672. }
  673. break;
  674. case IMPORT_KIND_MEMORY: /* import memory */
  675. bh_assert(import_memories);
  676. import = import_memories++;
  677. if (!load_memory_import(sub_module,
  678. sub_module_name,
  679. field_name,
  680. &p,
  681. p_end,
  682. &import->u.memory,
  683. error_buf,
  684. error_buf_size)) {
  685. return false;
  686. }
  687. break;
  688. case IMPORT_KIND_GLOBAL: /* import global */
  689. bh_assert(import_globals);
  690. import = import_globals++;
  691. if (!load_global_import(module, sub_module,
  692. sub_module_name, field_name,
  693. &p, p_end, &import->u.global,
  694. error_buf, error_buf_size)) {
  695. return false;
  696. }
  697. break;
  698. default:
  699. bh_assert(0);
  700. import = NULL;
  701. break;
  702. }
  703. import->kind = kind;
  704. import->u.names.module_name = sub_module_name;
  705. import->u.names.field_name = field_name;
  706. (void)sub_module;
  707. }
  708. #if WASM_ENABLE_LIBC_WASI != 0
  709. import = module->import_functions;
  710. for (i = 0; i < module->import_function_count; i++, import++) {
  711. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  712. || !strcmp(import->u.names.module_name, "wasi_snapshot_preview1")) {
  713. module->is_wasi_module = true;
  714. break;
  715. }
  716. }
  717. #endif
  718. }
  719. bh_assert(p == p_end);
  720. LOG_VERBOSE("Load import section success.\n");
  721. (void)u8;
  722. (void)u32;
  723. (void)type_index;
  724. return true;
  725. }
  726. static bool
  727. init_function_local_offsets(WASMFunction *func,
  728. char *error_buf, uint32 error_buf_size)
  729. {
  730. WASMType *param_type = func->func_type;
  731. uint32 param_count = param_type->param_count;
  732. uint8 *param_types = param_type->types;
  733. uint32 local_count = func->local_count;
  734. uint8 *local_types = func->local_types;
  735. uint32 i, local_offset = 0;
  736. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  737. if (!(func->local_offsets =
  738. loader_malloc(total_size, error_buf, error_buf_size))) {
  739. return false;
  740. }
  741. for (i = 0; i < param_count; i++) {
  742. func->local_offsets[i] = (uint16)local_offset;
  743. local_offset += wasm_value_type_cell_num(param_types[i]);
  744. }
  745. for (i = 0; i < local_count; i++) {
  746. func->local_offsets[param_count + i] = (uint16)local_offset;
  747. local_offset += wasm_value_type_cell_num(local_types[i]);
  748. }
  749. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  750. return true;
  751. }
  752. static bool
  753. load_function_section(const uint8 *buf, const uint8 *buf_end,
  754. const uint8 *buf_code, const uint8 *buf_code_end,
  755. WASMModule *module,
  756. char *error_buf, uint32 error_buf_size)
  757. {
  758. const uint8 *p = buf, *p_end = buf_end;
  759. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  760. uint32 func_count;
  761. uint64 total_size;
  762. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  763. uint32 local_count, local_set_count, sub_local_count;
  764. uint8 type;
  765. WASMFunction *func;
  766. read_leb_uint32(p, p_end, func_count);
  767. if (buf_code)
  768. read_leb_uint32(p_code, buf_code_end, code_count);
  769. bh_assert(func_count == code_count);
  770. if (func_count) {
  771. module->function_count = func_count;
  772. total_size = sizeof(WASMFunction*) * (uint64)func_count;
  773. if (!(module->functions =
  774. loader_malloc(total_size, error_buf, error_buf_size))) {
  775. return false;
  776. }
  777. for (i = 0; i < func_count; i++) {
  778. /* Resolve function type */
  779. read_leb_uint32(p, p_end, type_index);
  780. bh_assert(type_index < module->type_count);
  781. read_leb_uint32(p_code, buf_code_end, code_size);
  782. bh_assert(code_size > 0
  783. && p_code + code_size <= buf_code_end);
  784. /* Resolve local set count */
  785. p_code_end = p_code + code_size;
  786. local_count = 0;
  787. read_leb_uint32(p_code, buf_code_end, local_set_count);
  788. p_code_save = p_code;
  789. /* Calculate total local count */
  790. for (j = 0; j < local_set_count; j++) {
  791. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  792. bh_assert(sub_local_count <= UINT32_MAX - local_count);
  793. CHECK_BUF(p_code, buf_code_end, 1);
  794. /* 0x7F/0x7E/0x7D/0x7C */
  795. type = read_uint8(p_code);
  796. local_count += sub_local_count;
  797. }
  798. /* Alloc memory, layout: function structure + local types */
  799. code_size = (uint32)(p_code_end - p_code);
  800. total_size = sizeof(WASMFunction) + (uint64)local_count;
  801. if (!(func = module->functions[i] =
  802. loader_malloc(total_size, error_buf, error_buf_size))) {
  803. return false;
  804. }
  805. /* Set function type, local count, code size and code body */
  806. func->func_type = module->types[type_index];
  807. func->local_count = local_count;
  808. if (local_count > 0)
  809. func->local_types = (uint8*)func + sizeof(WASMFunction);
  810. func->code_size = code_size;
  811. /*
  812. * we shall make a copy of code body [p_code, p_code + code_size]
  813. * when we are worrying about inappropriate releasing behaviour.
  814. * all code bodies are actually in a buffer which user allocates in
  815. * his embedding environment and we don't have power on them.
  816. * it will be like:
  817. * code_body_cp = malloc(code_size);
  818. * memcpy(code_body_cp, p_code, code_size);
  819. * func->code = code_body_cp;
  820. */
  821. func->code = (uint8*)p_code;
  822. /* Load each local type */
  823. p_code = p_code_save;
  824. local_type_index = 0;
  825. for (j = 0; j < local_set_count; j++) {
  826. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  827. bh_assert(!(local_type_index + sub_local_count <= local_type_index
  828. || local_type_index + sub_local_count > local_count));
  829. CHECK_BUF(p_code, buf_code_end, 1);
  830. /* 0x7F/0x7E/0x7D/0x7C */
  831. type = read_uint8(p_code);
  832. bh_assert(type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32);
  833. for (k = 0; k < sub_local_count; k++) {
  834. func->local_types[local_type_index++] = type;
  835. }
  836. }
  837. func->param_cell_num = func->func_type->param_cell_num;
  838. func->ret_cell_num = func->func_type->ret_cell_num;
  839. func->local_cell_num =
  840. wasm_get_cell_num(func->local_types, func->local_count);
  841. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  842. return false;
  843. p_code = p_code_end;
  844. }
  845. }
  846. bh_assert(p == p_end);
  847. LOG_VERBOSE("Load function section success.\n");
  848. (void)code_count;
  849. return true;
  850. }
  851. static bool
  852. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  853. char *error_buf, uint32 error_buf_size)
  854. {
  855. const uint8 *p = buf, *p_end = buf_end;
  856. uint32 table_count, i;
  857. uint64 total_size;
  858. WASMTable *table;
  859. read_leb_uint32(p, p_end, table_count);
  860. bh_assert(module->import_table_count + table_count <= 1);
  861. if (table_count) {
  862. module->table_count = table_count;
  863. total_size = sizeof(WASMTable) * (uint64)table_count;
  864. if (!(module->tables = loader_malloc
  865. (total_size, error_buf, error_buf_size))) {
  866. return false;
  867. }
  868. /* load each table */
  869. table = module->tables;
  870. for (i = 0; i < table_count; i++, table++)
  871. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  872. return false;
  873. }
  874. bh_assert(p == p_end);
  875. LOG_VERBOSE("Load table section success.\n");
  876. return true;
  877. }
  878. static bool
  879. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  880. char *error_buf, uint32 error_buf_size)
  881. {
  882. const uint8 *p = buf, *p_end = buf_end;
  883. uint32 memory_count, i;
  884. uint64 total_size;
  885. WASMMemory *memory;
  886. read_leb_uint32(p, p_end, memory_count);
  887. bh_assert(module->import_memory_count + memory_count <= 1);
  888. if (memory_count) {
  889. module->memory_count = memory_count;
  890. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  891. if (!(module->memories = loader_malloc
  892. (total_size, error_buf, error_buf_size))) {
  893. return false;
  894. }
  895. /* load each memory */
  896. memory = module->memories;
  897. for (i = 0; i < memory_count; i++, memory++)
  898. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  899. return false;
  900. }
  901. bh_assert(p == p_end);
  902. LOG_VERBOSE("Load memory section success.\n");
  903. return true;
  904. }
  905. static bool
  906. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  907. char *error_buf, uint32 error_buf_size)
  908. {
  909. const uint8 *p = buf, *p_end = buf_end;
  910. uint32 global_count, i;
  911. uint64 total_size;
  912. WASMGlobal *global;
  913. uint8 mutable;
  914. read_leb_uint32(p, p_end, global_count);
  915. if (global_count) {
  916. module->global_count = global_count;
  917. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  918. if (!(module->globals = loader_malloc
  919. (total_size, error_buf, error_buf_size))) {
  920. return false;
  921. }
  922. global = module->globals;
  923. for(i = 0; i < global_count; i++, global++) {
  924. CHECK_BUF(p, p_end, 2);
  925. global->type = read_uint8(p);
  926. mutable = read_uint8(p);
  927. bh_assert(mutable < 2);
  928. global->is_mutable = mutable ? true : false;
  929. /* initialize expression */
  930. if (!load_init_expr(&p, p_end, &(global->init_expr),
  931. global->type, error_buf, error_buf_size))
  932. return false;
  933. if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
  934. /**
  935. * Currently, constant expressions occurring as initializers
  936. * of globals are further constrained in that contained
  937. * global.get instructions are
  938. * only allowed to refer to imported globals.
  939. */
  940. uint32 target_global_index = global->init_expr.u.global_index;
  941. bh_assert(target_global_index < module->import_global_count);
  942. (void)target_global_index;
  943. }
  944. }
  945. }
  946. bh_assert(p == p_end);
  947. LOG_VERBOSE("Load global section success.\n");
  948. return true;
  949. }
  950. static bool
  951. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  952. char *error_buf, uint32 error_buf_size)
  953. {
  954. const uint8 *p = buf, *p_end = buf_end;
  955. uint32 export_count, i, j, index;
  956. uint64 total_size;
  957. uint32 str_len;
  958. WASMExport *export;
  959. const char *name;
  960. read_leb_uint32(p, p_end, export_count);
  961. if (export_count) {
  962. module->export_count = export_count;
  963. total_size = sizeof(WASMExport) * (uint64)export_count;
  964. if (!(module->exports = loader_malloc
  965. (total_size, error_buf, error_buf_size))) {
  966. return false;
  967. }
  968. export = module->exports;
  969. for (i = 0; i < export_count; i++, export++) {
  970. read_leb_uint32(p, p_end, str_len);
  971. CHECK_BUF(p, p_end, str_len);
  972. for (j = 0; j < i; j++) {
  973. name = module->exports[j].name;
  974. bh_assert(!(strlen(name) == str_len
  975. && memcmp(name, p, str_len) == 0));
  976. }
  977. if (!(export->name = const_str_list_insert(p, str_len, module,
  978. error_buf, error_buf_size))) {
  979. return false;
  980. }
  981. p += str_len;
  982. CHECK_BUF(p, p_end, 1);
  983. export->kind = read_uint8(p);
  984. read_leb_uint32(p, p_end, index);
  985. export->index = index;
  986. switch(export->kind) {
  987. /*function index*/
  988. case EXPORT_KIND_FUNC:
  989. bh_assert(index < module->function_count
  990. + module->import_function_count);
  991. break;
  992. /*table index*/
  993. case EXPORT_KIND_TABLE:
  994. bh_assert(index < module->table_count
  995. + module->import_table_count);
  996. break;
  997. /*memory index*/
  998. case EXPORT_KIND_MEMORY:
  999. bh_assert(index < module->memory_count
  1000. + module->import_memory_count);
  1001. break;
  1002. /*global index*/
  1003. case EXPORT_KIND_GLOBAL:
  1004. bh_assert(index < module->global_count
  1005. + module->import_global_count);
  1006. break;
  1007. default:
  1008. bh_assert(0);
  1009. break;
  1010. }
  1011. }
  1012. }
  1013. bh_assert(p == p_end);
  1014. LOG_VERBOSE("Load export section success.\n");
  1015. (void)name;
  1016. return true;
  1017. }
  1018. static bool
  1019. load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1020. char *error_buf, uint32 error_buf_size)
  1021. {
  1022. const uint8 *p = buf, *p_end = buf_end;
  1023. uint32 table_segment_count, i, j, table_index, function_count, function_index;
  1024. uint64 total_size;
  1025. WASMTableSeg *table_segment;
  1026. read_leb_uint32(p, p_end, table_segment_count);
  1027. if (table_segment_count) {
  1028. module->table_seg_count = table_segment_count;
  1029. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  1030. if (!(module->table_segments = loader_malloc
  1031. (total_size, error_buf, error_buf_size))) {
  1032. return false;
  1033. }
  1034. table_segment = module->table_segments;
  1035. for (i = 0; i < table_segment_count; i++, table_segment++) {
  1036. bh_assert(p < p_end);
  1037. read_leb_uint32(p, p_end, table_index);
  1038. bh_assert(table_index < module->import_table_count
  1039. + module->table_count);
  1040. table_segment->table_index = table_index;
  1041. /* initialize expression */
  1042. if (!load_init_expr(&p, p_end, &(table_segment->base_offset),
  1043. VALUE_TYPE_I32, error_buf, error_buf_size))
  1044. return false;
  1045. read_leb_uint32(p, p_end, function_count);
  1046. table_segment->function_count = function_count;
  1047. total_size = sizeof(uint32) * (uint64)function_count;
  1048. if (!(table_segment->func_indexes = (uint32 *)
  1049. loader_malloc(total_size, error_buf, error_buf_size))) {
  1050. return false;
  1051. }
  1052. for (j = 0; j < function_count; j++) {
  1053. read_leb_uint32(p, p_end, function_index);
  1054. bh_assert(function_index < module->import_function_count
  1055. + module->function_count);
  1056. table_segment->func_indexes[j] = function_index;
  1057. }
  1058. }
  1059. }
  1060. bh_assert(p == p_end);
  1061. LOG_VERBOSE("Load table segment section success.\n");
  1062. return true;
  1063. }
  1064. static bool
  1065. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  1066. WASMModule *module,
  1067. char *error_buf, uint32 error_buf_size)
  1068. {
  1069. const uint8 *p = buf, *p_end = buf_end;
  1070. uint32 data_seg_count, i, mem_index, data_seg_len;
  1071. uint64 total_size;
  1072. WASMDataSeg *dataseg;
  1073. InitializerExpression init_expr;
  1074. #if WASM_ENABLE_BULK_MEMORY != 0
  1075. bool is_passive = false;
  1076. uint32 mem_flag;
  1077. #endif
  1078. read_leb_uint32(p, p_end, data_seg_count);
  1079. #if WASM_ENABLE_BULK_MEMORY != 0
  1080. bh_assert(module->data_seg_count1 == 0
  1081. || data_seg_count == module->data_seg_count1);
  1082. #endif
  1083. if (data_seg_count) {
  1084. module->data_seg_count = data_seg_count;
  1085. total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count;
  1086. if (!(module->data_segments = loader_malloc
  1087. (total_size, error_buf, error_buf_size))) {
  1088. return false;
  1089. }
  1090. for (i = 0; i < data_seg_count; i++) {
  1091. read_leb_uint32(p, p_end, mem_index);
  1092. #if WASM_ENABLE_BULK_MEMORY != 0
  1093. is_passive = false;
  1094. mem_flag = mem_index & 0x03;
  1095. switch (mem_flag) {
  1096. case 0x01:
  1097. is_passive = true;
  1098. break;
  1099. case 0x00:
  1100. /* no memory index, treat index as 0 */
  1101. mem_index = 0;
  1102. goto check_mem_index;
  1103. case 0x02:
  1104. /* read following memory index */
  1105. read_leb_uint32(p, p_end, mem_index);
  1106. check_mem_index:
  1107. bh_assert(mem_index < module->import_memory_count
  1108. + module->memory_count);
  1109. break;
  1110. case 0x03:
  1111. default:
  1112. bh_assert(0);
  1113. break;
  1114. }
  1115. #else
  1116. bh_assert(mem_index < module->import_memory_count
  1117. + module->memory_count);
  1118. #endif /* WASM_ENABLE_BULK_MEMORY */
  1119. #if WASM_ENABLE_BULK_MEMORY != 0
  1120. if (!is_passive)
  1121. #endif
  1122. if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32,
  1123. error_buf, error_buf_size))
  1124. return false;
  1125. read_leb_uint32(p, p_end, data_seg_len);
  1126. if (!(dataseg = module->data_segments[i] = loader_malloc
  1127. (sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  1128. return false;
  1129. }
  1130. #if WASM_ENABLE_BULK_MEMORY != 0
  1131. dataseg->is_passive = is_passive;
  1132. if (!is_passive)
  1133. #endif
  1134. {
  1135. bh_memcpy_s(&dataseg->base_offset, sizeof(InitializerExpression),
  1136. &init_expr, sizeof(InitializerExpression));
  1137. dataseg->memory_index = mem_index;
  1138. }
  1139. dataseg->data_length = data_seg_len;
  1140. CHECK_BUF(p, p_end, data_seg_len);
  1141. dataseg->data = (uint8*)p;
  1142. p += data_seg_len;
  1143. }
  1144. }
  1145. bh_assert(p == p_end);
  1146. LOG_VERBOSE("Load data segment section success.\n");
  1147. return true;
  1148. }
  1149. #if WASM_ENABLE_BULK_MEMORY != 0
  1150. static bool
  1151. load_datacount_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1152. char *error_buf, uint32 error_buf_size)
  1153. {
  1154. const uint8 *p = buf, *p_end = buf_end;
  1155. uint32 data_seg_count1 = 0;
  1156. read_leb_uint32(p, p_end, data_seg_count1);
  1157. module->data_seg_count1 = data_seg_count1;
  1158. bh_assert(p == p_end);
  1159. LOG_VERBOSE("Load datacount section success.\n");
  1160. return true;
  1161. }
  1162. #endif
  1163. static bool
  1164. load_code_section(const uint8 *buf, const uint8 *buf_end,
  1165. const uint8 *buf_func,
  1166. const uint8 *buf_func_end,
  1167. WASMModule *module,
  1168. char *error_buf, uint32 error_buf_size)
  1169. {
  1170. const uint8 *p = buf, *p_end = buf_end;
  1171. const uint8 *p_func = buf_func;
  1172. uint32 func_count = 0, code_count;
  1173. /* code has been loaded in function section, so pass it here, just check
  1174. * whether function and code section have inconsistent lengths */
  1175. read_leb_uint32(p, p_end, code_count);
  1176. if (buf_func)
  1177. read_leb_uint32(p_func, buf_func_end, func_count);
  1178. bh_assert(func_count == code_count);
  1179. LOG_VERBOSE("Load code segment section success.\n");
  1180. (void)code_count;
  1181. (void)func_count;
  1182. return true;
  1183. }
  1184. static bool
  1185. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1186. char *error_buf, uint32 error_buf_size)
  1187. {
  1188. const uint8 *p = buf, *p_end = buf_end;
  1189. WASMType *type;
  1190. uint32 start_function;
  1191. read_leb_uint32(p, p_end, start_function);
  1192. bh_assert(start_function < module->function_count
  1193. + module->import_function_count);
  1194. if (start_function < module->import_function_count)
  1195. type = module->import_functions[start_function].u.function.func_type;
  1196. else
  1197. type = module->functions[start_function - module->import_function_count]
  1198. ->func_type;
  1199. bh_assert(type->param_count == 0 && type->result_count == 0);
  1200. module->start_function = start_function;
  1201. bh_assert(p == p_end);
  1202. LOG_VERBOSE("Load start section success.\n");
  1203. (void)type;
  1204. return true;
  1205. }
  1206. static bool
  1207. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1208. char *error_buf, uint32 error_buf_size)
  1209. {
  1210. const uint8 *p = buf, *p_end = buf_end;
  1211. uint32 name_len;
  1212. bh_assert(p < p_end);
  1213. read_leb_uint32(p, p_end, name_len);
  1214. bh_assert(name_len > 0
  1215. && p + name_len <= p_end);
  1216. LOG_VERBOSE("Load custom section success.\n");
  1217. (void)name_len;
  1218. return true;
  1219. }
  1220. static bool
  1221. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  1222. BlockAddr *block_addr_cache,
  1223. char *error_buf, uint32 error_buf_size);
  1224. #if WASM_ENABLE_FAST_INTERP != 0
  1225. void **
  1226. wasm_interp_get_handle_table();
  1227. static void **handle_table;
  1228. #endif
  1229. static bool
  1230. load_from_sections(WASMModule *module, WASMSection *sections,
  1231. char *error_buf, uint32 error_buf_size)
  1232. {
  1233. WASMExport *export;
  1234. WASMSection *section = sections;
  1235. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  1236. *buf_func = NULL, *buf_func_end = NULL;
  1237. WASMGlobal *llvm_data_end_global = NULL, *llvm_heap_base_global = NULL;
  1238. WASMGlobal *llvm_stack_top_global = NULL, *global;
  1239. uint32 llvm_data_end = UINT32_MAX, llvm_heap_base = UINT32_MAX;
  1240. uint32 llvm_stack_top = UINT32_MAX, global_index, i;
  1241. uint32 stack_top_global_index = UINT32_MAX;
  1242. BlockAddr *block_addr_cache;
  1243. uint64 total_size;
  1244. /* Find code and function sections if have */
  1245. while (section) {
  1246. if (section->section_type == SECTION_TYPE_CODE) {
  1247. buf_code = section->section_body;
  1248. buf_code_end = buf_code + section->section_body_size;
  1249. }
  1250. else if (section->section_type == SECTION_TYPE_FUNC) {
  1251. buf_func = section->section_body;
  1252. buf_func_end = buf_func + section->section_body_size;
  1253. }
  1254. section = section->next;
  1255. }
  1256. section = sections;
  1257. while (section) {
  1258. buf = section->section_body;
  1259. buf_end = buf + section->section_body_size;
  1260. LOG_DEBUG("to section %d", section->section_type);
  1261. switch (section->section_type) {
  1262. case SECTION_TYPE_USER:
  1263. /* unsupported user section, ignore it. */
  1264. if (!load_user_section(buf, buf_end, module, error_buf, error_buf_size))
  1265. return false;
  1266. break;
  1267. case SECTION_TYPE_TYPE:
  1268. if (!load_type_section(buf, buf_end, module, error_buf, error_buf_size))
  1269. return false;
  1270. break;
  1271. case SECTION_TYPE_IMPORT:
  1272. if (!load_import_section(buf, buf_end, module, error_buf, error_buf_size))
  1273. return false;
  1274. break;
  1275. case SECTION_TYPE_FUNC:
  1276. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  1277. module, error_buf, error_buf_size))
  1278. return false;
  1279. break;
  1280. case SECTION_TYPE_TABLE:
  1281. if (!load_table_section(buf, buf_end, module, error_buf, error_buf_size))
  1282. return false;
  1283. break;
  1284. case SECTION_TYPE_MEMORY:
  1285. if (!load_memory_section(buf, buf_end, module, error_buf, error_buf_size))
  1286. return false;
  1287. break;
  1288. case SECTION_TYPE_GLOBAL:
  1289. if (!load_global_section(buf, buf_end, module, error_buf, error_buf_size))
  1290. return false;
  1291. break;
  1292. case SECTION_TYPE_EXPORT:
  1293. if (!load_export_section(buf, buf_end, module, error_buf, error_buf_size))
  1294. return false;
  1295. break;
  1296. case SECTION_TYPE_START:
  1297. if (!load_start_section(buf, buf_end, module, error_buf, error_buf_size))
  1298. return false;
  1299. break;
  1300. case SECTION_TYPE_ELEM:
  1301. if (!load_table_segment_section(buf, buf_end, module, error_buf, error_buf_size))
  1302. return false;
  1303. break;
  1304. case SECTION_TYPE_CODE:
  1305. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  1306. module, error_buf, error_buf_size))
  1307. return false;
  1308. break;
  1309. case SECTION_TYPE_DATA:
  1310. if (!load_data_segment_section(buf, buf_end, module, error_buf, error_buf_size))
  1311. return false;
  1312. break;
  1313. #if WASM_ENABLE_BULK_MEMORY != 0
  1314. case SECTION_TYPE_DATACOUNT:
  1315. if (!load_datacount_section(buf, buf_end, module, error_buf, error_buf_size))
  1316. return false;
  1317. break;
  1318. #endif
  1319. default:
  1320. set_error_buf(error_buf, error_buf_size,
  1321. "WASM module load failed: invalid section id");
  1322. return false;
  1323. }
  1324. section = section->next;
  1325. }
  1326. #if WASM_ENABLE_FAST_INTERP != 0
  1327. handle_table = wasm_interp_get_handle_table();
  1328. #endif
  1329. total_size = sizeof(BlockAddr) * (uint64)BLOCK_ADDR_CACHE_SIZE * BLOCK_ADDR_CONFLICT_SIZE;
  1330. if (!(block_addr_cache = loader_malloc
  1331. (total_size, error_buf, error_buf_size))) {
  1332. return false;
  1333. }
  1334. for (i = 0; i < module->function_count; i++) {
  1335. WASMFunction *func = module->functions[i];
  1336. memset(block_addr_cache, 0, (uint32)total_size);
  1337. if (!wasm_loader_prepare_bytecode(module, func, block_addr_cache,
  1338. error_buf, error_buf_size)) {
  1339. wasm_runtime_free(block_addr_cache);
  1340. return false;
  1341. }
  1342. }
  1343. wasm_runtime_free(block_addr_cache);
  1344. /* Resolve llvm auxiliary data/stack/heap info and reset memory info */
  1345. export = module->exports;
  1346. for (i = 0; i < module->export_count; i++, export++) {
  1347. if (export->kind == EXPORT_KIND_GLOBAL) {
  1348. if (!strcmp(export->name, "__heap_base")) {
  1349. global_index = export->index - module->import_global_count;
  1350. global = module->globals + global_index;
  1351. if (global->type == VALUE_TYPE_I32
  1352. && !global->is_mutable
  1353. && global->init_expr.init_expr_type ==
  1354. INIT_EXPR_TYPE_I32_CONST) {
  1355. llvm_heap_base_global = global;
  1356. llvm_heap_base = global->init_expr.u.i32;
  1357. LOG_VERBOSE("found llvm __heap_base global, value: %d\n",
  1358. llvm_heap_base);
  1359. }
  1360. }
  1361. else if (!strcmp(export->name, "__data_end")) {
  1362. global_index = export->index - module->import_global_count;
  1363. global = module->globals + global_index;
  1364. if (global->type == VALUE_TYPE_I32
  1365. && !global->is_mutable
  1366. && global->init_expr.init_expr_type ==
  1367. INIT_EXPR_TYPE_I32_CONST) {
  1368. llvm_data_end_global = global;
  1369. llvm_data_end = global->init_expr.u.i32;
  1370. LOG_VERBOSE("found llvm __data_end global, value: %d\n",
  1371. llvm_data_end);
  1372. llvm_data_end = align_uint(llvm_data_end, 16);
  1373. }
  1374. }
  1375. if (llvm_data_end_global && llvm_heap_base_global) {
  1376. /* Resolve aux stack top global */
  1377. for (global_index = 0; global_index < module->global_count; global_index++) {
  1378. global = module->globals + global_index;
  1379. if (global != llvm_data_end_global
  1380. && global != llvm_heap_base_global
  1381. && global->type == VALUE_TYPE_I32
  1382. && global->is_mutable
  1383. && global->init_expr.init_expr_type ==
  1384. INIT_EXPR_TYPE_I32_CONST
  1385. && (global->init_expr.u.i32 <=
  1386. llvm_heap_base_global->init_expr.u.i32
  1387. && llvm_data_end_global->init_expr.u.i32 <=
  1388. llvm_heap_base_global->init_expr.u.i32)) {
  1389. llvm_stack_top_global = global;
  1390. llvm_stack_top = global->init_expr.u.i32;
  1391. stack_top_global_index = global_index;
  1392. LOG_VERBOSE("found llvm stack top global, "
  1393. "value: %d, global index: %d\n",
  1394. llvm_stack_top, global_index);
  1395. break;
  1396. }
  1397. }
  1398. module->llvm_aux_data_end = llvm_data_end;
  1399. module->llvm_aux_stack_bottom = llvm_stack_top;
  1400. module->llvm_aux_stack_size = llvm_stack_top > llvm_data_end
  1401. ? llvm_stack_top - llvm_data_end
  1402. : llvm_stack_top;
  1403. module->llvm_aux_stack_global_index = stack_top_global_index;
  1404. LOG_VERBOSE("aux stack bottom: %d, size: %d\n",
  1405. module->llvm_aux_stack_bottom,
  1406. module->llvm_aux_stack_size);
  1407. break;
  1408. }
  1409. }
  1410. }
  1411. if (!module->possible_memory_grow) {
  1412. if (llvm_data_end_global
  1413. && llvm_heap_base_global
  1414. && llvm_stack_top_global
  1415. && llvm_stack_top <= llvm_heap_base) {
  1416. WASMMemoryImport *memory_import;
  1417. WASMMemory *memory;
  1418. uint64 init_memory_size;
  1419. uint32 shrunk_memory_size = llvm_heap_base > llvm_data_end
  1420. ? llvm_heap_base : llvm_data_end;
  1421. if (module->import_memory_count) {
  1422. memory_import = &module->import_memories[0].u.memory;
  1423. init_memory_size = (uint64)memory_import->num_bytes_per_page *
  1424. memory_import->init_page_count;
  1425. if (llvm_heap_base <= init_memory_size
  1426. && llvm_data_end <= init_memory_size) {
  1427. /* Reset memory info to decrease memory usage */
  1428. memory_import->num_bytes_per_page = shrunk_memory_size;
  1429. memory_import->init_page_count = 1;
  1430. LOG_VERBOSE("reset import memory size to %d\n",
  1431. shrunk_memory_size);
  1432. }
  1433. }
  1434. if (module->memory_count) {
  1435. memory = &module->memories[0];
  1436. init_memory_size = (uint64)memory->num_bytes_per_page *
  1437. memory->init_page_count;
  1438. if (llvm_heap_base <= init_memory_size
  1439. && llvm_data_end <= init_memory_size) {
  1440. /* Reset memory info to decrease memory usage */
  1441. memory->num_bytes_per_page = shrunk_memory_size;
  1442. memory->init_page_count = 1;
  1443. LOG_VERBOSE("reset memory size to %d\n", shrunk_memory_size);
  1444. }
  1445. }
  1446. }
  1447. }
  1448. return true;
  1449. }
  1450. #if BH_ENABLE_MEMORY_PROFILING != 0
  1451. static void wasm_loader_free(void *ptr)
  1452. {
  1453. wasm_runtime_free(ptr);
  1454. }
  1455. #else
  1456. #define wasm_loader_free wasm_free
  1457. #endif
  1458. static WASMModule*
  1459. create_module(char *error_buf, uint32 error_buf_size)
  1460. {
  1461. WASMModule *module = loader_malloc(sizeof(WASMModule),
  1462. error_buf, error_buf_size);
  1463. if (!module) {
  1464. return NULL;
  1465. }
  1466. module->module_type = Wasm_Module_Bytecode;
  1467. /* Set start_function to -1, means no start function */
  1468. module->start_function = (uint32)-1;
  1469. #if WASM_ENABLE_MULTI_MODULE != 0
  1470. module->import_module_list = &module->import_module_list_head;
  1471. #endif
  1472. return module;
  1473. }
  1474. WASMModule *
  1475. wasm_loader_load_from_sections(WASMSection *section_list,
  1476. char *error_buf, uint32 error_buf_size)
  1477. {
  1478. WASMModule *module = create_module(error_buf, error_buf_size);
  1479. if (!module)
  1480. return NULL;
  1481. if (!load_from_sections(module, section_list, error_buf, error_buf_size)) {
  1482. wasm_loader_unload(module);
  1483. return NULL;
  1484. }
  1485. LOG_VERBOSE("Load module from sections success.\n");
  1486. return module;
  1487. }
  1488. static void
  1489. destroy_sections(WASMSection *section_list)
  1490. {
  1491. WASMSection *section = section_list, *next;
  1492. while (section) {
  1493. next = section->next;
  1494. wasm_runtime_free(section);
  1495. section = next;
  1496. }
  1497. }
  1498. static uint8 section_ids[] = {
  1499. SECTION_TYPE_USER,
  1500. SECTION_TYPE_TYPE,
  1501. SECTION_TYPE_IMPORT,
  1502. SECTION_TYPE_FUNC,
  1503. SECTION_TYPE_TABLE,
  1504. SECTION_TYPE_MEMORY,
  1505. SECTION_TYPE_GLOBAL,
  1506. SECTION_TYPE_EXPORT,
  1507. SECTION_TYPE_START,
  1508. SECTION_TYPE_ELEM,
  1509. #if WASM_ENABLE_BULK_MEMORY != 0
  1510. SECTION_TYPE_DATACOUNT,
  1511. #endif
  1512. SECTION_TYPE_CODE,
  1513. SECTION_TYPE_DATA
  1514. };
  1515. static uint8
  1516. get_section_index(uint8 section_type)
  1517. {
  1518. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  1519. for (uint8 i = 0; i < max_id; i++) {
  1520. if (section_type == section_ids[i])
  1521. return i;
  1522. }
  1523. return (uint8)-1;
  1524. }
  1525. static bool
  1526. create_sections(const uint8 *buf, uint32 size,
  1527. WASMSection **p_section_list,
  1528. char *error_buf, uint32 error_buf_size)
  1529. {
  1530. WASMSection *section_list_end = NULL, *section;
  1531. const uint8 *p = buf, *p_end = buf + size/*, *section_body*/;
  1532. uint8 section_type, section_index, last_section_index = (uint8)-1;
  1533. uint32 section_size;
  1534. bh_assert(!*p_section_list);
  1535. p += 8;
  1536. while (p < p_end) {
  1537. CHECK_BUF(p, p_end, 1);
  1538. section_type = read_uint8(p);
  1539. section_index = get_section_index(section_type);
  1540. if (section_index != (uint8)-1) {
  1541. if (section_type != SECTION_TYPE_USER) {
  1542. /* Custom sections may be inserted at any place,
  1543. while other sections must occur at most once
  1544. and in prescribed order. */
  1545. bh_assert(last_section_index == (uint8)-1
  1546. || last_section_index < section_index);
  1547. last_section_index = section_index;
  1548. }
  1549. CHECK_BUF1(p, p_end, 1);
  1550. read_leb_uint32(p, p_end, section_size);
  1551. CHECK_BUF1(p, p_end, section_size);
  1552. if (!(section = loader_malloc(sizeof(WASMSection),
  1553. error_buf, error_buf_size))) {
  1554. return false;
  1555. }
  1556. section->section_type = section_type;
  1557. section->section_body = (uint8*)p;
  1558. section->section_body_size = section_size;
  1559. if (!*p_section_list)
  1560. *p_section_list = section_list_end = section;
  1561. else {
  1562. section_list_end->next = section;
  1563. section_list_end = section;
  1564. }
  1565. p += section_size;
  1566. }
  1567. else {
  1568. bh_assert(0);
  1569. }
  1570. }
  1571. (void)last_section_index;
  1572. return true;
  1573. }
  1574. static void
  1575. exchange32(uint8* p_data)
  1576. {
  1577. uint8 value = *p_data;
  1578. *p_data = *(p_data + 3);
  1579. *(p_data + 3) = value;
  1580. value = *(p_data + 1);
  1581. *(p_data + 1) = *(p_data + 2);
  1582. *(p_data + 2) = value;
  1583. }
  1584. static union {
  1585. int a;
  1586. char b;
  1587. } __ue = { .a = 1 };
  1588. #define is_little_endian() (__ue.b == 1)
  1589. static bool
  1590. load(const uint8 *buf, uint32 size, WASMModule *module,
  1591. char *error_buf, uint32 error_buf_size)
  1592. {
  1593. const uint8 *buf_end = buf + size;
  1594. const uint8 *p = buf, *p_end = buf_end;
  1595. uint32 magic_number, version;
  1596. WASMSection *section_list = NULL;
  1597. CHECK_BUF1(p, p_end, sizeof(uint32));
  1598. magic_number = read_uint32(p);
  1599. if (!is_little_endian())
  1600. exchange32((uint8*)&magic_number);
  1601. bh_assert(magic_number == WASM_MAGIC_NUMBER);
  1602. CHECK_BUF1(p, p_end, sizeof(uint32));
  1603. version = read_uint32(p);
  1604. if (!is_little_endian())
  1605. exchange32((uint8*)&version);
  1606. if (version != WASM_CURRENT_VERSION) {
  1607. set_error_buf(error_buf, error_buf_size,
  1608. "WASM module load failed: unknown binary version");
  1609. return false;
  1610. }
  1611. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  1612. || !load_from_sections(module, section_list, error_buf, error_buf_size)) {
  1613. destroy_sections(section_list);
  1614. return false;
  1615. }
  1616. destroy_sections(section_list);
  1617. (void)p_end;
  1618. return true;
  1619. }
  1620. WASMModule*
  1621. wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
  1622. {
  1623. WASMModule *module = create_module(error_buf, error_buf_size);
  1624. if (!module) {
  1625. return NULL;
  1626. }
  1627. if (!load(buf, size, module, error_buf, error_buf_size)) {
  1628. LOG_VERBOSE("Load module failed, %s", error_buf);
  1629. goto fail;
  1630. }
  1631. LOG_VERBOSE("Load module success");
  1632. return module;
  1633. fail:
  1634. wasm_loader_unload(module);
  1635. return NULL;
  1636. }
  1637. void
  1638. wasm_loader_unload(WASMModule *module)
  1639. {
  1640. uint32 i;
  1641. if (!module)
  1642. return;
  1643. if (module->types) {
  1644. for (i = 0; i < module->type_count; i++) {
  1645. if (module->types[i])
  1646. wasm_runtime_free(module->types[i]);
  1647. }
  1648. wasm_runtime_free(module->types);
  1649. }
  1650. if (module->imports)
  1651. wasm_runtime_free(module->imports);
  1652. if (module->functions) {
  1653. for (i = 0; i < module->function_count; i++) {
  1654. if (module->functions[i]) {
  1655. if (module->functions[i]->local_offsets)
  1656. wasm_runtime_free(module->functions[i]->local_offsets);
  1657. #if WASM_ENABLE_FAST_INTERP != 0
  1658. if (module->functions[i]->code_compiled)
  1659. wasm_runtime_free(module->functions[i]->code_compiled);
  1660. if (module->functions[i]->consts)
  1661. wasm_runtime_free(module->functions[i]->consts);
  1662. #endif
  1663. wasm_runtime_free(module->functions[i]);
  1664. }
  1665. }
  1666. wasm_runtime_free(module->functions);
  1667. }
  1668. if (module->tables)
  1669. wasm_runtime_free(module->tables);
  1670. if (module->memories)
  1671. wasm_runtime_free(module->memories);
  1672. if (module->globals)
  1673. wasm_runtime_free(module->globals);
  1674. if (module->exports)
  1675. wasm_runtime_free(module->exports);
  1676. if (module->table_segments) {
  1677. for (i = 0; i < module->table_seg_count; i++) {
  1678. if (module->table_segments[i].func_indexes)
  1679. wasm_runtime_free(module->table_segments[i].func_indexes);
  1680. }
  1681. wasm_runtime_free(module->table_segments);
  1682. }
  1683. if (module->data_segments) {
  1684. for (i = 0; i < module->data_seg_count; i++) {
  1685. if (module->data_segments[i])
  1686. wasm_runtime_free(module->data_segments[i]);
  1687. }
  1688. wasm_runtime_free(module->data_segments);
  1689. }
  1690. if (module->const_str_list) {
  1691. StringNode *node = module->const_str_list, *node_next;
  1692. while (node) {
  1693. node_next = node->next;
  1694. wasm_runtime_free(node);
  1695. node = node_next;
  1696. }
  1697. }
  1698. wasm_runtime_free(module);
  1699. }
  1700. bool
  1701. wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
  1702. const uint8 *start_addr,
  1703. const uint8 *code_end_addr,
  1704. uint8 label_type,
  1705. uint8 **p_else_addr,
  1706. uint8 **p_end_addr,
  1707. char *error_buf,
  1708. uint32 error_buf_size)
  1709. {
  1710. const uint8 *p = start_addr, *p_end = code_end_addr;
  1711. uint8 *else_addr = NULL;
  1712. uint32 block_nested_depth = 1, count, i, j, t;
  1713. uint8 opcode, u8;
  1714. BlockAddr block_stack[16] = { 0 }, *block;
  1715. i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
  1716. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  1717. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  1718. if (block[j].start_addr == start_addr) {
  1719. /* Cache hit */
  1720. *p_else_addr = block[j].else_addr;
  1721. *p_end_addr = block[j].end_addr;
  1722. return true;
  1723. }
  1724. }
  1725. /* Cache unhit */
  1726. block_stack[0].start_addr = start_addr;
  1727. while (p < code_end_addr) {
  1728. opcode = *p++;
  1729. switch (opcode) {
  1730. case WASM_OP_UNREACHABLE:
  1731. case WASM_OP_NOP:
  1732. break;
  1733. case WASM_OP_BLOCK:
  1734. case WASM_OP_LOOP:
  1735. case WASM_OP_IF:
  1736. CHECK_BUF(p, p_end, 1);
  1737. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  1738. u8 = read_uint8(p);
  1739. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
  1740. block_stack[block_nested_depth].start_addr = p;
  1741. block_stack[block_nested_depth].else_addr = NULL;
  1742. }
  1743. block_nested_depth++;
  1744. break;
  1745. case EXT_OP_BLOCK:
  1746. case EXT_OP_LOOP:
  1747. case EXT_OP_IF:
  1748. /* block type */
  1749. skip_leb_uint32(p, p_end);
  1750. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
  1751. block_stack[block_nested_depth].start_addr = p;
  1752. block_stack[block_nested_depth].else_addr = NULL;
  1753. }
  1754. block_nested_depth++;
  1755. break;
  1756. case WASM_OP_ELSE:
  1757. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  1758. else_addr = (uint8*)(p - 1);
  1759. if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr))
  1760. block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1);
  1761. break;
  1762. case WASM_OP_END:
  1763. if (block_nested_depth == 1) {
  1764. if (label_type == LABEL_TYPE_IF)
  1765. *p_else_addr = else_addr;
  1766. *p_end_addr = (uint8*)(p - 1);
  1767. block_stack[0].end_addr = (uint8*)(p - 1);
  1768. for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) {
  1769. start_addr = block_stack[t].start_addr;
  1770. if (start_addr) {
  1771. i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
  1772. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  1773. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  1774. if (!block[j].start_addr)
  1775. break;
  1776. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  1777. memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) *
  1778. sizeof(BlockAddr));
  1779. j = 0;
  1780. }
  1781. block[j].start_addr = block_stack[t].start_addr;
  1782. block[j].else_addr = block_stack[t].else_addr;
  1783. block[j].end_addr = block_stack[t].end_addr;
  1784. }
  1785. else
  1786. break;
  1787. }
  1788. return true;
  1789. }
  1790. else {
  1791. block_nested_depth--;
  1792. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr))
  1793. block_stack[block_nested_depth].end_addr = (uint8*)(p - 1);
  1794. }
  1795. break;
  1796. case WASM_OP_BR:
  1797. case WASM_OP_BR_IF:
  1798. skip_leb_uint32(p, p_end); /* labelidx */
  1799. break;
  1800. case WASM_OP_BR_TABLE:
  1801. read_leb_uint32(p, p_end, count); /* lable num */
  1802. for (i = 0; i <= count; i++) /* lableidxs */
  1803. skip_leb_uint32(p, p_end);
  1804. break;
  1805. case WASM_OP_RETURN:
  1806. break;
  1807. case WASM_OP_CALL:
  1808. skip_leb_uint32(p, p_end); /* funcidx */
  1809. break;
  1810. case WASM_OP_CALL_INDIRECT:
  1811. skip_leb_uint32(p, p_end); /* typeidx */
  1812. CHECK_BUF(p, p_end, 1);
  1813. u8 = read_uint8(p); /* 0x00 */
  1814. break;
  1815. case WASM_OP_DROP:
  1816. case WASM_OP_SELECT:
  1817. case WASM_OP_DROP_64:
  1818. case WASM_OP_SELECT_64:
  1819. break;
  1820. case WASM_OP_GET_LOCAL:
  1821. case WASM_OP_SET_LOCAL:
  1822. case WASM_OP_TEE_LOCAL:
  1823. case WASM_OP_GET_GLOBAL:
  1824. case WASM_OP_SET_GLOBAL:
  1825. case WASM_OP_GET_GLOBAL_64:
  1826. case WASM_OP_SET_GLOBAL_64:
  1827. case WASM_OP_SET_GLOBAL_AUX_STACK:
  1828. skip_leb_uint32(p, p_end); /* localidx */
  1829. break;
  1830. case EXT_OP_GET_LOCAL_FAST:
  1831. case EXT_OP_SET_LOCAL_FAST:
  1832. case EXT_OP_TEE_LOCAL_FAST:
  1833. CHECK_BUF(p, p_end, 1);
  1834. p++;
  1835. break;
  1836. case WASM_OP_I32_LOAD:
  1837. case WASM_OP_I64_LOAD:
  1838. case WASM_OP_F32_LOAD:
  1839. case WASM_OP_F64_LOAD:
  1840. case WASM_OP_I32_LOAD8_S:
  1841. case WASM_OP_I32_LOAD8_U:
  1842. case WASM_OP_I32_LOAD16_S:
  1843. case WASM_OP_I32_LOAD16_U:
  1844. case WASM_OP_I64_LOAD8_S:
  1845. case WASM_OP_I64_LOAD8_U:
  1846. case WASM_OP_I64_LOAD16_S:
  1847. case WASM_OP_I64_LOAD16_U:
  1848. case WASM_OP_I64_LOAD32_S:
  1849. case WASM_OP_I64_LOAD32_U:
  1850. case WASM_OP_I32_STORE:
  1851. case WASM_OP_I64_STORE:
  1852. case WASM_OP_F32_STORE:
  1853. case WASM_OP_F64_STORE:
  1854. case WASM_OP_I32_STORE8:
  1855. case WASM_OP_I32_STORE16:
  1856. case WASM_OP_I64_STORE8:
  1857. case WASM_OP_I64_STORE16:
  1858. case WASM_OP_I64_STORE32:
  1859. skip_leb_uint32(p, p_end); /* align */
  1860. skip_leb_uint32(p, p_end); /* offset */
  1861. break;
  1862. case WASM_OP_MEMORY_SIZE:
  1863. case WASM_OP_MEMORY_GROW:
  1864. skip_leb_uint32(p, p_end); /* 0x00 */
  1865. break;
  1866. case WASM_OP_I32_CONST:
  1867. skip_leb_int32(p, p_end);
  1868. break;
  1869. case WASM_OP_I64_CONST:
  1870. skip_leb_int64(p, p_end);
  1871. break;
  1872. case WASM_OP_F32_CONST:
  1873. p += sizeof(float32);
  1874. break;
  1875. case WASM_OP_F64_CONST:
  1876. p += sizeof(float64);
  1877. break;
  1878. case WASM_OP_I32_EQZ:
  1879. case WASM_OP_I32_EQ:
  1880. case WASM_OP_I32_NE:
  1881. case WASM_OP_I32_LT_S:
  1882. case WASM_OP_I32_LT_U:
  1883. case WASM_OP_I32_GT_S:
  1884. case WASM_OP_I32_GT_U:
  1885. case WASM_OP_I32_LE_S:
  1886. case WASM_OP_I32_LE_U:
  1887. case WASM_OP_I32_GE_S:
  1888. case WASM_OP_I32_GE_U:
  1889. case WASM_OP_I64_EQZ:
  1890. case WASM_OP_I64_EQ:
  1891. case WASM_OP_I64_NE:
  1892. case WASM_OP_I64_LT_S:
  1893. case WASM_OP_I64_LT_U:
  1894. case WASM_OP_I64_GT_S:
  1895. case WASM_OP_I64_GT_U:
  1896. case WASM_OP_I64_LE_S:
  1897. case WASM_OP_I64_LE_U:
  1898. case WASM_OP_I64_GE_S:
  1899. case WASM_OP_I64_GE_U:
  1900. case WASM_OP_F32_EQ:
  1901. case WASM_OP_F32_NE:
  1902. case WASM_OP_F32_LT:
  1903. case WASM_OP_F32_GT:
  1904. case WASM_OP_F32_LE:
  1905. case WASM_OP_F32_GE:
  1906. case WASM_OP_F64_EQ:
  1907. case WASM_OP_F64_NE:
  1908. case WASM_OP_F64_LT:
  1909. case WASM_OP_F64_GT:
  1910. case WASM_OP_F64_LE:
  1911. case WASM_OP_F64_GE:
  1912. case WASM_OP_I32_CLZ:
  1913. case WASM_OP_I32_CTZ:
  1914. case WASM_OP_I32_POPCNT:
  1915. case WASM_OP_I32_ADD:
  1916. case WASM_OP_I32_SUB:
  1917. case WASM_OP_I32_MUL:
  1918. case WASM_OP_I32_DIV_S:
  1919. case WASM_OP_I32_DIV_U:
  1920. case WASM_OP_I32_REM_S:
  1921. case WASM_OP_I32_REM_U:
  1922. case WASM_OP_I32_AND:
  1923. case WASM_OP_I32_OR:
  1924. case WASM_OP_I32_XOR:
  1925. case WASM_OP_I32_SHL:
  1926. case WASM_OP_I32_SHR_S:
  1927. case WASM_OP_I32_SHR_U:
  1928. case WASM_OP_I32_ROTL:
  1929. case WASM_OP_I32_ROTR:
  1930. case WASM_OP_I64_CLZ:
  1931. case WASM_OP_I64_CTZ:
  1932. case WASM_OP_I64_POPCNT:
  1933. case WASM_OP_I64_ADD:
  1934. case WASM_OP_I64_SUB:
  1935. case WASM_OP_I64_MUL:
  1936. case WASM_OP_I64_DIV_S:
  1937. case WASM_OP_I64_DIV_U:
  1938. case WASM_OP_I64_REM_S:
  1939. case WASM_OP_I64_REM_U:
  1940. case WASM_OP_I64_AND:
  1941. case WASM_OP_I64_OR:
  1942. case WASM_OP_I64_XOR:
  1943. case WASM_OP_I64_SHL:
  1944. case WASM_OP_I64_SHR_S:
  1945. case WASM_OP_I64_SHR_U:
  1946. case WASM_OP_I64_ROTL:
  1947. case WASM_OP_I64_ROTR:
  1948. case WASM_OP_F32_ABS:
  1949. case WASM_OP_F32_NEG:
  1950. case WASM_OP_F32_CEIL:
  1951. case WASM_OP_F32_FLOOR:
  1952. case WASM_OP_F32_TRUNC:
  1953. case WASM_OP_F32_NEAREST:
  1954. case WASM_OP_F32_SQRT:
  1955. case WASM_OP_F32_ADD:
  1956. case WASM_OP_F32_SUB:
  1957. case WASM_OP_F32_MUL:
  1958. case WASM_OP_F32_DIV:
  1959. case WASM_OP_F32_MIN:
  1960. case WASM_OP_F32_MAX:
  1961. case WASM_OP_F32_COPYSIGN:
  1962. case WASM_OP_F64_ABS:
  1963. case WASM_OP_F64_NEG:
  1964. case WASM_OP_F64_CEIL:
  1965. case WASM_OP_F64_FLOOR:
  1966. case WASM_OP_F64_TRUNC:
  1967. case WASM_OP_F64_NEAREST:
  1968. case WASM_OP_F64_SQRT:
  1969. case WASM_OP_F64_ADD:
  1970. case WASM_OP_F64_SUB:
  1971. case WASM_OP_F64_MUL:
  1972. case WASM_OP_F64_DIV:
  1973. case WASM_OP_F64_MIN:
  1974. case WASM_OP_F64_MAX:
  1975. case WASM_OP_F64_COPYSIGN:
  1976. case WASM_OP_I32_WRAP_I64:
  1977. case WASM_OP_I32_TRUNC_S_F32:
  1978. case WASM_OP_I32_TRUNC_U_F32:
  1979. case WASM_OP_I32_TRUNC_S_F64:
  1980. case WASM_OP_I32_TRUNC_U_F64:
  1981. case WASM_OP_I64_EXTEND_S_I32:
  1982. case WASM_OP_I64_EXTEND_U_I32:
  1983. case WASM_OP_I64_TRUNC_S_F32:
  1984. case WASM_OP_I64_TRUNC_U_F32:
  1985. case WASM_OP_I64_TRUNC_S_F64:
  1986. case WASM_OP_I64_TRUNC_U_F64:
  1987. case WASM_OP_F32_CONVERT_S_I32:
  1988. case WASM_OP_F32_CONVERT_U_I32:
  1989. case WASM_OP_F32_CONVERT_S_I64:
  1990. case WASM_OP_F32_CONVERT_U_I64:
  1991. case WASM_OP_F32_DEMOTE_F64:
  1992. case WASM_OP_F64_CONVERT_S_I32:
  1993. case WASM_OP_F64_CONVERT_U_I32:
  1994. case WASM_OP_F64_CONVERT_S_I64:
  1995. case WASM_OP_F64_CONVERT_U_I64:
  1996. case WASM_OP_F64_PROMOTE_F32:
  1997. case WASM_OP_I32_REINTERPRET_F32:
  1998. case WASM_OP_I64_REINTERPRET_F64:
  1999. case WASM_OP_F32_REINTERPRET_I32:
  2000. case WASM_OP_F64_REINTERPRET_I64:
  2001. case WASM_OP_I32_EXTEND8_S:
  2002. case WASM_OP_I32_EXTEND16_S:
  2003. case WASM_OP_I64_EXTEND8_S:
  2004. case WASM_OP_I64_EXTEND16_S:
  2005. case WASM_OP_I64_EXTEND32_S:
  2006. break;
  2007. case WASM_OP_MISC_PREFIX:
  2008. {
  2009. opcode = read_uint8(p);
  2010. switch (opcode) {
  2011. case WASM_OP_I32_TRUNC_SAT_S_F32:
  2012. case WASM_OP_I32_TRUNC_SAT_U_F32:
  2013. case WASM_OP_I32_TRUNC_SAT_S_F64:
  2014. case WASM_OP_I32_TRUNC_SAT_U_F64:
  2015. case WASM_OP_I64_TRUNC_SAT_S_F32:
  2016. case WASM_OP_I64_TRUNC_SAT_U_F32:
  2017. case WASM_OP_I64_TRUNC_SAT_S_F64:
  2018. case WASM_OP_I64_TRUNC_SAT_U_F64:
  2019. break;
  2020. #if WASM_ENABLE_BULK_MEMORY != 0
  2021. case WASM_OP_MEMORY_INIT:
  2022. skip_leb_uint32(p, p_end);
  2023. /* skip memory idx */
  2024. p++;
  2025. break;
  2026. case WASM_OP_DATA_DROP:
  2027. skip_leb_uint32(p, p_end);
  2028. break;
  2029. case WASM_OP_MEMORY_COPY:
  2030. /* skip two memory idx */
  2031. p += 2;
  2032. break;
  2033. case WASM_OP_MEMORY_FILL:
  2034. /* skip memory idx */
  2035. p++;
  2036. break;
  2037. #endif
  2038. default:
  2039. bh_assert(0);
  2040. break;
  2041. }
  2042. break;
  2043. }
  2044. #if WASM_ENABLE_SHARED_MEMORY != 0
  2045. case WASM_OP_ATOMIC_PREFIX:
  2046. {
  2047. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  2048. opcode = read_uint8(p);
  2049. if (opcode != WASM_OP_ATOMIC_FENCE) {
  2050. skip_leb_uint32(p, p_end); /* align */
  2051. skip_leb_uint32(p, p_end); /* offset */
  2052. }
  2053. else {
  2054. /* atomic.fence doesn't have memarg */
  2055. p++;
  2056. }
  2057. break;
  2058. }
  2059. #endif
  2060. default:
  2061. bh_assert(0);
  2062. break;
  2063. }
  2064. }
  2065. (void)u8;
  2066. return false;
  2067. }
  2068. #define REF_I32 VALUE_TYPE_I32
  2069. #define REF_F32 VALUE_TYPE_F32
  2070. #define REF_I64_1 VALUE_TYPE_I64
  2071. #define REF_I64_2 VALUE_TYPE_I64
  2072. #define REF_F64_1 VALUE_TYPE_F64
  2073. #define REF_F64_2 VALUE_TYPE_F64
  2074. #define REF_ANY VALUE_TYPE_ANY
  2075. #if WASM_ENABLE_FAST_INTERP != 0
  2076. #if WASM_DEBUG_PREPROCESSOR != 0
  2077. #define LOG_OP(...) os_printf(__VA_ARGS__)
  2078. #else
  2079. #define LOG_OP(...)
  2080. #endif
  2081. #define PATCH_ELSE 0
  2082. #define PATCH_END 1
  2083. typedef struct BranchBlockPatch {
  2084. struct BranchBlockPatch *next;
  2085. uint8 patch_type;
  2086. uint8 *code_compiled;
  2087. } BranchBlockPatch;
  2088. #endif
  2089. typedef struct BranchBlock {
  2090. uint8 label_type;
  2091. BlockType block_type;
  2092. uint8 *start_addr;
  2093. uint8 *else_addr;
  2094. uint8 *end_addr;
  2095. uint32 stack_cell_num;
  2096. #if WASM_ENABLE_FAST_INTERP != 0
  2097. uint16 dynamic_offset;
  2098. uint8 *code_compiled;
  2099. BranchBlockPatch *patch_list;
  2100. /* This is used to save params frame_offset of of if block */
  2101. int16 *param_frame_offsets;
  2102. #endif
  2103. /* Indicate the operand stack is in polymorphic state.
  2104. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  2105. * to polymorphic state until the block's 'end' opcode is processed.
  2106. * If stack is in polymorphic state and stack is empty, instruction can
  2107. * pop any type of value directly without decreasing stack top pointer
  2108. * and stack cell num. */
  2109. bool is_stack_polymorphic;
  2110. } BranchBlock;
  2111. typedef struct WASMLoaderContext {
  2112. /* frame ref stack */
  2113. uint8 *frame_ref;
  2114. uint8 *frame_ref_bottom;
  2115. uint8 *frame_ref_boundary;
  2116. uint32 frame_ref_size;
  2117. uint32 stack_cell_num;
  2118. uint32 max_stack_cell_num;
  2119. /* frame csp stack */
  2120. BranchBlock *frame_csp;
  2121. BranchBlock *frame_csp_bottom;
  2122. BranchBlock *frame_csp_boundary;
  2123. uint32 frame_csp_size;
  2124. uint32 csp_num;
  2125. uint32 max_csp_num;
  2126. #if WASM_ENABLE_FAST_INTERP != 0
  2127. /* frame offset stack */
  2128. int16 *frame_offset;
  2129. int16 *frame_offset_bottom;
  2130. int16 *frame_offset_boundary;
  2131. uint32 frame_offset_size;
  2132. int16 dynamic_offset;
  2133. int16 start_dynamic_offset;
  2134. int16 max_dynamic_offset;
  2135. /* preserved local offset */
  2136. int16 preserved_local_offset;
  2137. /* const buffer */
  2138. uint8 *const_buf;
  2139. uint16 num_const;
  2140. uint16 const_buf_size;
  2141. uint16 const_cell_num;
  2142. /* processed code */
  2143. uint8 *p_code_compiled;
  2144. uint8 *p_code_compiled_end;
  2145. uint32 code_compiled_size;
  2146. #endif
  2147. } WASMLoaderContext;
  2148. typedef struct Const {
  2149. WASMValue value;
  2150. uint16 slot_index;
  2151. uint8 value_type;
  2152. } Const;
  2153. static void*
  2154. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new,
  2155. char *error_buf, uint32 error_buf_size)
  2156. {
  2157. uint8 *mem_new;
  2158. bh_assert(size_new > size_old);
  2159. if ((mem_new = loader_malloc
  2160. (size_new, error_buf, error_buf_size))) {
  2161. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  2162. memset(mem_new + size_old, 0, size_new - size_old);
  2163. wasm_runtime_free(mem_old);
  2164. }
  2165. return mem_new;
  2166. }
  2167. #define MEM_REALLOC(mem, size_old, size_new) do { \
  2168. void *mem_new = memory_realloc(mem, size_old, size_new, \
  2169. error_buf, error_buf_size); \
  2170. if (!mem_new) \
  2171. goto fail; \
  2172. mem = mem_new; \
  2173. } while (0)
  2174. #define CHECK_CSP_PUSH() do { \
  2175. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  2176. MEM_REALLOC(ctx->frame_csp_bottom, ctx->frame_csp_size, \
  2177. (uint32)(ctx->frame_csp_size \
  2178. + 8 * sizeof(BranchBlock))); \
  2179. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  2180. ctx->frame_csp_boundary = ctx->frame_csp_bottom + \
  2181. ctx->frame_csp_size / sizeof(BranchBlock); \
  2182. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  2183. } \
  2184. } while (0)
  2185. #define CHECK_CSP_POP() do { \
  2186. bh_assert(ctx->csp_num >= 1); \
  2187. } while (0)
  2188. #if WASM_ENABLE_FAST_INTERP != 0
  2189. static bool
  2190. check_offset_push(WASMLoaderContext *ctx,
  2191. char *error_buf, uint32 error_buf_size)
  2192. {
  2193. uint32 cell_num = (ctx->frame_offset - ctx->frame_offset_bottom);
  2194. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  2195. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  2196. ctx->frame_offset_size + 16);
  2197. ctx->frame_offset_size += 16;
  2198. ctx->frame_offset_boundary = ctx->frame_offset_bottom +
  2199. ctx->frame_offset_size / sizeof(int16);
  2200. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  2201. }
  2202. return true;
  2203. fail:
  2204. return false;
  2205. }
  2206. static bool
  2207. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  2208. {
  2209. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  2210. return false;
  2211. return true;
  2212. }
  2213. static void
  2214. free_label_patch_list(BranchBlock *frame_csp)
  2215. {
  2216. BranchBlockPatch *label_patch = frame_csp->patch_list;
  2217. BranchBlockPatch *next;
  2218. while (label_patch != NULL) {
  2219. next = label_patch->next;
  2220. wasm_runtime_free(label_patch);
  2221. label_patch = next;
  2222. }
  2223. frame_csp->patch_list = NULL;
  2224. }
  2225. static void
  2226. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  2227. {
  2228. BranchBlock *tmp_csp = frame_csp;
  2229. for (uint32 i = 0; i < csp_num; i++) {
  2230. free_label_patch_list(tmp_csp);
  2231. tmp_csp ++;
  2232. }
  2233. }
  2234. #endif
  2235. static bool
  2236. check_stack_push(WASMLoaderContext *ctx,
  2237. char *error_buf, uint32 error_buf_size)
  2238. {
  2239. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  2240. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  2241. ctx->frame_ref_size + 16);
  2242. ctx->frame_ref_size += 16;
  2243. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  2244. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  2245. }
  2246. return true;
  2247. fail:
  2248. return false;
  2249. }
  2250. static bool
  2251. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  2252. char *error_buf, uint32 error_buf_size)
  2253. {
  2254. bh_assert(!(((type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  2255. && stack_cell_num < 1)
  2256. || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  2257. && stack_cell_num < 2)));
  2258. bh_assert(!((type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  2259. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  2260. || (type == VALUE_TYPE_I64
  2261. && (*(frame_ref - 2) != REF_I64_1
  2262. || *(frame_ref - 1) != REF_I64_2))
  2263. || (type == VALUE_TYPE_F64
  2264. && (*(frame_ref - 2) != REF_F64_1
  2265. || *(frame_ref - 1) != REF_F64_2))));
  2266. return true;
  2267. }
  2268. static bool
  2269. check_stack_pop(WASMLoaderContext *ctx, uint8 type,
  2270. char *error_buf, uint32 error_buf_size)
  2271. {
  2272. int32 block_stack_cell_num = (int32)
  2273. (ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  2274. if (block_stack_cell_num > 0
  2275. && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  2276. /* the stack top is a value of any type, return success */
  2277. return true;
  2278. }
  2279. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num,
  2280. type, error_buf, error_buf_size))
  2281. return false;
  2282. return true;
  2283. }
  2284. static void
  2285. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  2286. {
  2287. if (ctx) {
  2288. if (ctx->frame_ref_bottom)
  2289. wasm_runtime_free(ctx->frame_ref_bottom);
  2290. if (ctx->frame_csp_bottom) {
  2291. #if WASM_ENABLE_FAST_INTERP != 0
  2292. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  2293. #endif
  2294. wasm_runtime_free(ctx->frame_csp_bottom);
  2295. }
  2296. #if WASM_ENABLE_FAST_INTERP != 0
  2297. if (ctx->frame_offset_bottom)
  2298. wasm_runtime_free(ctx->frame_offset_bottom);
  2299. if (ctx->const_buf)
  2300. wasm_runtime_free(ctx->const_buf);
  2301. #endif
  2302. wasm_runtime_free(ctx);
  2303. }
  2304. }
  2305. static WASMLoaderContext*
  2306. wasm_loader_ctx_init(WASMFunction *func)
  2307. {
  2308. WASMLoaderContext *loader_ctx =
  2309. wasm_runtime_malloc(sizeof(WASMLoaderContext));
  2310. if (!loader_ctx)
  2311. return false;
  2312. memset(loader_ctx, 0, sizeof(WASMLoaderContext));
  2313. loader_ctx->frame_ref_size = 32;
  2314. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref =
  2315. wasm_runtime_malloc(loader_ctx->frame_ref_size)))
  2316. goto fail;
  2317. memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size);
  2318. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom +
  2319. loader_ctx->frame_ref_size;
  2320. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  2321. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp =
  2322. wasm_runtime_malloc(loader_ctx->frame_csp_size)))
  2323. goto fail;
  2324. memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size);
  2325. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  2326. #if WASM_ENABLE_FAST_INTERP != 0
  2327. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  2328. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  2329. wasm_runtime_malloc(loader_ctx->frame_offset_size)))
  2330. goto fail;
  2331. memset(loader_ctx->frame_offset_bottom, 0,
  2332. loader_ctx->frame_offset_size);
  2333. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  2334. loader_ctx->num_const = 0;
  2335. loader_ctx->const_buf_size = sizeof(Const) * 8;
  2336. if (!(loader_ctx->const_buf = wasm_runtime_malloc(loader_ctx->const_buf_size)))
  2337. goto fail;
  2338. memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size);
  2339. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  2340. loader_ctx->max_dynamic_offset = func->param_cell_num +
  2341. func->local_cell_num;
  2342. #endif
  2343. return loader_ctx;
  2344. fail:
  2345. wasm_loader_ctx_destroy(loader_ctx);
  2346. return NULL;
  2347. }
  2348. static bool
  2349. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type,
  2350. char *error_buf, uint32 error_buf_size)
  2351. {
  2352. if (type == VALUE_TYPE_VOID)
  2353. return true;
  2354. if (!check_stack_push(ctx, error_buf, error_buf_size))
  2355. return false;
  2356. *ctx->frame_ref++ = type;
  2357. ctx->stack_cell_num++;
  2358. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  2359. ctx->max_stack_cell_num = ctx->stack_cell_num;
  2360. if (type == VALUE_TYPE_I32
  2361. || type == VALUE_TYPE_F32
  2362. || type == VALUE_TYPE_ANY)
  2363. return true;
  2364. if (!check_stack_push(ctx, error_buf, error_buf_size))
  2365. return false;
  2366. *ctx->frame_ref++ = type;
  2367. ctx->stack_cell_num++;
  2368. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  2369. ctx->max_stack_cell_num = ctx->stack_cell_num;
  2370. return true;
  2371. }
  2372. static bool
  2373. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type,
  2374. char *error_buf, uint32 error_buf_size)
  2375. {
  2376. BranchBlock *cur_block = ctx->frame_csp - 1;
  2377. int32 available_stack_cell = (int32)
  2378. (ctx->stack_cell_num - cur_block->stack_cell_num);
  2379. /* Directly return success if current block is in stack
  2380. * polymorphic state while stack is empty. */
  2381. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  2382. return true;
  2383. if (type == VALUE_TYPE_VOID)
  2384. return true;
  2385. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  2386. return false;
  2387. ctx->frame_ref--;
  2388. ctx->stack_cell_num--;
  2389. if (type == VALUE_TYPE_I32
  2390. || type == VALUE_TYPE_F32
  2391. || *ctx->frame_ref == VALUE_TYPE_ANY)
  2392. return true;
  2393. ctx->frame_ref--;
  2394. ctx->stack_cell_num--;
  2395. return true;
  2396. }
  2397. static bool
  2398. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  2399. uint8 type_push, uint8 type_pop,
  2400. char *error_buf, uint32 error_buf_size)
  2401. {
  2402. for (int i = 0; i < pop_cnt; i++) {
  2403. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf, error_buf_size))
  2404. return false;
  2405. }
  2406. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  2407. return false;
  2408. return true;
  2409. }
  2410. static bool
  2411. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  2412. BlockType block_type, uint8* start_addr,
  2413. char *error_buf, uint32 error_buf_size)
  2414. {
  2415. CHECK_CSP_PUSH();
  2416. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  2417. ctx->frame_csp->label_type = label_type;
  2418. ctx->frame_csp->block_type = block_type;
  2419. ctx->frame_csp->start_addr = start_addr;
  2420. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  2421. #if WASM_ENABLE_FAST_INTERP != 0
  2422. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  2423. ctx->frame_csp->patch_list = NULL;
  2424. #endif
  2425. ctx->frame_csp++;
  2426. ctx->csp_num++;
  2427. if (ctx->csp_num > ctx->max_csp_num)
  2428. ctx->max_csp_num = ctx->csp_num;
  2429. return true;
  2430. fail:
  2431. return false;
  2432. }
  2433. static bool
  2434. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx,
  2435. char *error_buf, uint32 error_buf_size)
  2436. {
  2437. CHECK_CSP_POP();
  2438. #if WASM_ENABLE_FAST_INTERP != 0
  2439. if ((ctx->frame_csp - 1)->param_frame_offsets)
  2440. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  2441. #endif
  2442. ctx->frame_csp--;
  2443. ctx->csp_num--;
  2444. return true;
  2445. }
  2446. #if WASM_ENABLE_FAST_INTERP != 0
  2447. #if WASM_ENABLE_ABS_LABEL_ADDR != 0
  2448. #define emit_label(opcode) do { \
  2449. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  2450. LOG_OP("\nemit_op [%02x]\t", opcode); \
  2451. } while (0)
  2452. #define skip_label() do { \
  2453. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  2454. LOG_OP("\ndelete last op\n"); \
  2455. } while (0)
  2456. #else
  2457. #define emit_label(opcode) do { \
  2458. int32 offset = (int32)(handle_table[opcode] - handle_table[0]); \
  2459. bh_assert(offset >= INT16_MIN && offset < INT16_MAX); \
  2460. wasm_loader_emit_int16(loader_ctx, offset); \
  2461. LOG_OP("\nemit_op [%02x]\t", opcode); \
  2462. } while (0)
  2463. // drop local.get / const / block / loop / end
  2464. #define skip_label() do { \
  2465. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  2466. LOG_OP("\ndelete last op\n"); \
  2467. } while (0)
  2468. #endif /* WASM_ENABLE_ABS_LABEL_ADDR */
  2469. #define emit_empty_label_addr_and_frame_ip(type) do { \
  2470. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  2471. loader_ctx->p_code_compiled, \
  2472. error_buf, error_buf_size)) \
  2473. goto fail; \
  2474. /* label address, to be patched */ \
  2475. wasm_loader_emit_ptr(loader_ctx, NULL); \
  2476. } while (0)
  2477. #define emit_br_info(frame_csp) do { \
  2478. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, \
  2479. error_buf, error_buf_size)) \
  2480. goto fail; \
  2481. } while (0)
  2482. #define LAST_OP_OUTPUT_I32() (last_op >= WASM_OP_I32_EQZ \
  2483. && last_op <= WASM_OP_I32_ROTR) \
  2484. || (last_op == WASM_OP_I32_LOAD \
  2485. || last_op == WASM_OP_F32_LOAD) \
  2486. || (last_op >= WASM_OP_I32_LOAD8_S \
  2487. && last_op <= WASM_OP_I32_LOAD16_U) \
  2488. || (last_op >= WASM_OP_F32_ABS \
  2489. && last_op <= WASM_OP_F32_COPYSIGN) \
  2490. || (last_op >= WASM_OP_I32_WRAP_I64 \
  2491. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  2492. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  2493. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  2494. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  2495. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  2496. || (last_op == EXT_OP_COPY_STACK_TOP)
  2497. #define LAST_OP_OUTPUT_I64() (last_op >= WASM_OP_I64_CLZ \
  2498. && last_op <= WASM_OP_I64_ROTR) \
  2499. || (last_op >= WASM_OP_F64_ABS \
  2500. && last_op <= WASM_OP_F64_COPYSIGN) \
  2501. || (last_op == WASM_OP_I64_LOAD \
  2502. || last_op == WASM_OP_F64_LOAD) \
  2503. || (last_op >= WASM_OP_I64_LOAD8_S \
  2504. && last_op <= WASM_OP_I64_LOAD32_U) \
  2505. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  2506. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  2507. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  2508. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  2509. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  2510. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  2511. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  2512. #define GET_CONST_OFFSET(type, val) do { \
  2513. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  2514. &val, &operand_offset, \
  2515. error_buf, error_buf_size))) \
  2516. goto fail; \
  2517. } while (0)
  2518. #define GET_CONST_F32_OFFSET(type, fval) do { \
  2519. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  2520. &fval, &operand_offset, \
  2521. error_buf, error_buf_size))) \
  2522. goto fail; \
  2523. } while (0)
  2524. #define GET_CONST_F64_OFFSET(type, fval) do { \
  2525. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  2526. &fval, &operand_offset, \
  2527. error_buf, error_buf_size))) \
  2528. goto fail; \
  2529. } while (0)
  2530. #define emit_operand(ctx, offset) do { \
  2531. wasm_loader_emit_int16(ctx, offset); \
  2532. LOG_OP("%d\t", offset); \
  2533. } while (0)
  2534. #define emit_byte(ctx, byte) do { \
  2535. wasm_loader_emit_uint8(ctx, byte); \
  2536. LOG_OP("%d\t", byte); \
  2537. } while (0)
  2538. #define emit_uint32(ctx, value) do { \
  2539. wasm_loader_emit_uint32(ctx, value); \
  2540. LOG_OP("%d\t", value); \
  2541. } while (0)
  2542. #define emit_leb() do { \
  2543. wasm_loader_emit_leb(loader_ctx, p_org, p); \
  2544. } while (0)
  2545. static bool
  2546. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  2547. {
  2548. if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size)))
  2549. return false;
  2550. memset(ctx->p_code_compiled, 0, ctx->code_compiled_size);
  2551. ctx->p_code_compiled_end = ctx->p_code_compiled +
  2552. ctx->code_compiled_size;
  2553. /* clean up frame ref */
  2554. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  2555. ctx->frame_ref = ctx->frame_ref_bottom;
  2556. ctx->stack_cell_num = 0;
  2557. /* clean up frame csp */
  2558. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  2559. ctx->frame_csp = ctx->frame_csp_bottom;
  2560. ctx->csp_num = 0;
  2561. ctx->max_csp_num = 0;
  2562. /* clean up frame offset */
  2563. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  2564. ctx->frame_offset = ctx->frame_offset_bottom;
  2565. ctx->dynamic_offset = ctx->start_dynamic_offset;
  2566. /* init preserved local offsets */
  2567. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  2568. /* const buf is reserved */
  2569. return true;
  2570. }
  2571. static void
  2572. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  2573. {
  2574. if (ctx->p_code_compiled) {
  2575. *(uint32*)(ctx->p_code_compiled) = value;
  2576. ctx->p_code_compiled += sizeof(uint32);
  2577. }
  2578. else
  2579. ctx->code_compiled_size += sizeof(uint32);
  2580. }
  2581. static void
  2582. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  2583. {
  2584. if (ctx->p_code_compiled) {
  2585. *(int16*)(ctx->p_code_compiled) = value;
  2586. ctx->p_code_compiled += sizeof(int16);
  2587. }
  2588. else
  2589. ctx->code_compiled_size += sizeof(int16);
  2590. }
  2591. static void
  2592. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  2593. {
  2594. if (ctx->p_code_compiled) {
  2595. *(ctx->p_code_compiled) = value;
  2596. ctx->p_code_compiled += sizeof(uint8);
  2597. }
  2598. else
  2599. ctx->code_compiled_size += sizeof(uint8);
  2600. }
  2601. static void
  2602. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  2603. {
  2604. if (ctx->p_code_compiled) {
  2605. *(uint8**)(ctx->p_code_compiled) = value;
  2606. ctx->p_code_compiled += sizeof(void *);
  2607. }
  2608. else
  2609. ctx->code_compiled_size += sizeof(void *);
  2610. }
  2611. static void
  2612. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  2613. {
  2614. if (ctx->p_code_compiled) {
  2615. ctx->p_code_compiled -= size;
  2616. }
  2617. else
  2618. ctx->code_compiled_size -= size;
  2619. }
  2620. static void
  2621. wasm_loader_emit_leb(WASMLoaderContext *ctx, uint8* start, uint8* end)
  2622. {
  2623. if (ctx->p_code_compiled) {
  2624. bh_memcpy_s(ctx->p_code_compiled,
  2625. ctx->p_code_compiled_end - ctx->p_code_compiled,
  2626. start, end - start);
  2627. ctx->p_code_compiled += (end - start);
  2628. }
  2629. else {
  2630. ctx->code_compiled_size += (end - start);
  2631. }
  2632. }
  2633. static bool
  2634. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  2635. uint32 local_index, uint32 local_type, bool *preserved,
  2636. char *error_buf, uint32 error_buf_size)
  2637. {
  2638. int16 preserved_offset = (int16)local_index;
  2639. *preserved = false;
  2640. for (uint32 i = 0; i < loader_ctx->stack_cell_num; i++) {
  2641. /* move previous local into dynamic space before a set/tee_local opcode */
  2642. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  2643. if (preserved_offset == (int16)local_index) {
  2644. *preserved = true;
  2645. skip_label();
  2646. if (local_type == VALUE_TYPE_I32
  2647. || local_type == VALUE_TYPE_F32) {
  2648. preserved_offset = loader_ctx->preserved_local_offset;
  2649. /* Only increase preserve offset in the second traversal */
  2650. if (loader_ctx->p_code_compiled)
  2651. loader_ctx->preserved_local_offset++;
  2652. emit_label(EXT_OP_COPY_STACK_TOP);
  2653. }
  2654. else {
  2655. preserved_offset = loader_ctx->preserved_local_offset;
  2656. if (loader_ctx->p_code_compiled)
  2657. loader_ctx->preserved_local_offset += 2;
  2658. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  2659. }
  2660. emit_operand(loader_ctx, local_index);
  2661. emit_operand(loader_ctx, preserved_offset);
  2662. emit_label(opcode);
  2663. }
  2664. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  2665. }
  2666. }
  2667. return true;
  2668. #if WASM_ENABLE_ABS_LABEL_ADDR == 0
  2669. fail:
  2670. return false;
  2671. #endif
  2672. }
  2673. static bool
  2674. add_label_patch_to_list(BranchBlock *frame_csp,
  2675. uint8 patch_type, uint8 *p_code_compiled,
  2676. char *error_buf, uint32 error_buf_size)
  2677. {
  2678. BranchBlockPatch *patch = loader_malloc
  2679. (sizeof(BranchBlockPatch), error_buf, error_buf_size);
  2680. if (!patch) {
  2681. return false;
  2682. }
  2683. patch->patch_type = patch_type;
  2684. patch->code_compiled = p_code_compiled;
  2685. if (!frame_csp->patch_list) {
  2686. frame_csp->patch_list = patch;
  2687. patch->next = NULL;
  2688. }
  2689. else {
  2690. patch->next = frame_csp->patch_list;
  2691. frame_csp->patch_list = patch;
  2692. }
  2693. return true;
  2694. }
  2695. static void
  2696. apply_label_patch(WASMLoaderContext *ctx, uint8 depth,
  2697. uint8 patch_type)
  2698. {
  2699. BranchBlock *frame_csp = ctx->frame_csp - depth;
  2700. BranchBlockPatch *node = frame_csp->patch_list;
  2701. BranchBlockPatch *node_prev = NULL, *node_next;
  2702. if (!ctx->p_code_compiled)
  2703. return;
  2704. while (node) {
  2705. node_next = node->next;
  2706. if (node->patch_type == patch_type) {
  2707. *((uint8**)node->code_compiled) = ctx->p_code_compiled;
  2708. if (node_prev == NULL) {
  2709. frame_csp->patch_list = node_next;
  2710. }
  2711. else {
  2712. node_prev->next = node_next;
  2713. }
  2714. wasm_runtime_free(node);
  2715. }
  2716. else {
  2717. node_prev = node;
  2718. }
  2719. node = node_next;
  2720. }
  2721. }
  2722. static bool
  2723. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  2724. char *error_buf, uint32 error_buf_size)
  2725. {
  2726. /* br info layout:
  2727. * a) arity of target block
  2728. * b) total cell num of arity values
  2729. * c) each arity value's cell num
  2730. * d) each arity value's src frame offset
  2731. * e) each arity values's dst dynamic offset
  2732. * f) branch target address
  2733. */
  2734. BlockType *block_type = &frame_csp->block_type;
  2735. uint8 *types = NULL, cell;
  2736. uint32 arity = 0;
  2737. int32 i;
  2738. int16 *frame_offset = ctx->frame_offset;
  2739. uint16 dynamic_offset;
  2740. /* Note: loop's arity is different from if and block. loop's arity is
  2741. * its parameter count while if and block arity is result count.
  2742. */
  2743. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  2744. arity = block_type_get_param_types(block_type, &types);
  2745. else
  2746. arity = block_type_get_result_types(block_type, &types);
  2747. /* Part a */
  2748. emit_uint32(ctx, arity);
  2749. /* Part b */
  2750. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  2751. /* Part c */
  2752. for (i = (int32)arity - 1; i >= 0; i--) {
  2753. cell = wasm_value_type_cell_num(types[i]);
  2754. emit_byte(ctx, cell);
  2755. }
  2756. /* Part d */
  2757. for (i = (int32)arity - 1; i >= 0; i--) {
  2758. cell = wasm_value_type_cell_num(types[i]);
  2759. frame_offset -= cell;
  2760. emit_operand(ctx, *(int16*)(frame_offset));
  2761. }
  2762. /* Part e */
  2763. dynamic_offset = frame_csp->dynamic_offset
  2764. + wasm_get_cell_num(types, arity);
  2765. for (i = (int32)arity - 1; i >= 0; i--) {
  2766. cell = wasm_value_type_cell_num(types[i]);
  2767. dynamic_offset -= cell;
  2768. emit_operand(ctx, dynamic_offset);
  2769. }
  2770. /* Part f */
  2771. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  2772. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  2773. }
  2774. else {
  2775. if (!add_label_patch_to_list(frame_csp, PATCH_END,
  2776. ctx->p_code_compiled,
  2777. error_buf, error_buf_size))
  2778. return false;
  2779. /* label address, to be patched */
  2780. wasm_loader_emit_ptr(ctx, NULL);
  2781. }
  2782. return true;
  2783. }
  2784. static bool
  2785. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  2786. bool disable_emit, int16 operand_offset,
  2787. char *error_buf, uint32 error_buf_size)
  2788. {
  2789. if (type == VALUE_TYPE_VOID)
  2790. return true;
  2791. // only check memory overflow in first traverse
  2792. if (ctx->p_code_compiled == NULL) {
  2793. if (!check_offset_push(ctx, error_buf, error_buf_size))
  2794. return false;
  2795. }
  2796. if (disable_emit)
  2797. *(ctx->frame_offset)++ = operand_offset;
  2798. else {
  2799. emit_operand(ctx, ctx->dynamic_offset);
  2800. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  2801. ctx->dynamic_offset++;
  2802. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  2803. ctx->max_dynamic_offset = ctx->dynamic_offset;
  2804. }
  2805. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  2806. return true;
  2807. if (ctx->p_code_compiled == NULL) {
  2808. if (!check_offset_push(ctx, error_buf, error_buf_size))
  2809. return false;
  2810. }
  2811. ctx->frame_offset++;
  2812. if (!disable_emit) {
  2813. ctx->dynamic_offset++;
  2814. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  2815. ctx->max_dynamic_offset = ctx->dynamic_offset;
  2816. }
  2817. return true;
  2818. }
  2819. /* This function should be in front of wasm_loader_pop_frame_ref
  2820. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  2821. will be modified by wasm_loader_pop_frame_ref */
  2822. static bool
  2823. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  2824. char *error_buf, uint32 error_buf_size)
  2825. {
  2826. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  2827. then current block is the function block */
  2828. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  2829. BranchBlock *cur_block = ctx->frame_csp - depth;
  2830. int32 available_stack_cell = (int32)
  2831. (ctx->stack_cell_num - cur_block->stack_cell_num);
  2832. /* Directly return success if current block is in stack
  2833. * polymorphic state while stack is empty. */
  2834. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  2835. return true;
  2836. if (type == VALUE_TYPE_VOID)
  2837. return true;
  2838. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) {
  2839. /* Check the offset stack bottom to ensure the frame offset
  2840. stack will not go underflow. But we don't thrown error
  2841. and return true here, because the error msg should be
  2842. given in wasm_loader_pop_frame_ref */
  2843. if (!check_offset_pop(ctx, 1))
  2844. return true;
  2845. ctx->frame_offset -= 1;
  2846. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  2847. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  2848. ctx->dynamic_offset -= 1;
  2849. }
  2850. else {
  2851. if (!check_offset_pop(ctx, 2))
  2852. return true;
  2853. ctx->frame_offset -= 2;
  2854. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  2855. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  2856. ctx->dynamic_offset -= 2;
  2857. }
  2858. emit_operand(ctx, *(ctx->frame_offset));
  2859. return true;
  2860. }
  2861. static bool
  2862. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  2863. uint8 type_push, uint8 type_pop,
  2864. bool disable_emit, int16 operand_offset,
  2865. char *error_buf, uint32 error_buf_size)
  2866. {
  2867. for (int i = 0; i < pop_cnt; i++) {
  2868. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf, error_buf_size))
  2869. return false;
  2870. }
  2871. if (!wasm_loader_push_frame_offset(ctx, type_push,
  2872. disable_emit, operand_offset,
  2873. error_buf, error_buf_size))
  2874. return false;
  2875. return true;
  2876. }
  2877. static bool
  2878. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  2879. bool disable_emit, int16 operand_offset,
  2880. char *error_buf, uint32 error_buf_size)
  2881. {
  2882. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  2883. error_buf, error_buf_size)))
  2884. return false;
  2885. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  2886. return false;
  2887. return true;
  2888. }
  2889. static bool
  2890. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  2891. char *error_buf, uint32 error_buf_size)
  2892. {
  2893. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  2894. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  2895. return false;
  2896. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  2897. return false;
  2898. return true;
  2899. }
  2900. static bool
  2901. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  2902. uint8 type_push, uint8 type_pop,
  2903. bool disable_emit, int16 operand_offset,
  2904. char *error_buf, uint32 error_buf_size)
  2905. {
  2906. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  2907. disable_emit, operand_offset,
  2908. error_buf, error_buf_size))
  2909. return false;
  2910. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  2911. error_buf, error_buf_size))
  2912. return false;
  2913. return true;
  2914. }
  2915. static bool
  2916. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type,
  2917. void *value, int16 *offset,
  2918. char *error_buf, uint32 error_buf_size)
  2919. {
  2920. int16 operand_offset = 0;
  2921. Const *c;
  2922. for (c = (Const *)ctx->const_buf;
  2923. (uint8*)c < ctx->const_buf + ctx->num_const * sizeof(Const); c ++) {
  2924. if ((type == c->value_type)
  2925. && ((type == VALUE_TYPE_I64 && *(int64*)value == c->value.i64)
  2926. || (type == VALUE_TYPE_I32 && *(int32*)value == c->value.i32)
  2927. || (type == VALUE_TYPE_F64
  2928. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  2929. || (type == VALUE_TYPE_F32
  2930. && (0 == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  2931. operand_offset = c->slot_index;
  2932. break;
  2933. }
  2934. if (c->value_type == VALUE_TYPE_I64
  2935. || c->value_type == VALUE_TYPE_F64)
  2936. operand_offset += 2;
  2937. else
  2938. operand_offset += 1;
  2939. }
  2940. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  2941. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  2942. MEM_REALLOC(ctx->const_buf,
  2943. ctx->const_buf_size,
  2944. ctx->const_buf_size + 4 * sizeof(Const));
  2945. ctx->const_buf_size += 4 * sizeof(Const);
  2946. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  2947. }
  2948. c->value_type = type;
  2949. switch (type) {
  2950. case VALUE_TYPE_F64:
  2951. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value, sizeof(float64));
  2952. ctx->const_cell_num += 2;
  2953. /* The const buf will be reversed, we use the second cell */
  2954. /* of the i64/f64 const so the finnal offset is corrent */
  2955. operand_offset ++;
  2956. break;
  2957. case VALUE_TYPE_I64:
  2958. c->value.i64 = *(int64*)value;
  2959. ctx->const_cell_num += 2;
  2960. operand_offset ++;
  2961. break;
  2962. case VALUE_TYPE_F32:
  2963. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value, sizeof(float32));
  2964. ctx->const_cell_num ++;
  2965. break;
  2966. case VALUE_TYPE_I32:
  2967. c->value.i32 = *(int32*)value;
  2968. ctx->const_cell_num ++;
  2969. break;
  2970. default:
  2971. break;
  2972. }
  2973. c->slot_index = operand_offset;
  2974. ctx->num_const ++;
  2975. LOG_OP("#### new const [%d]: %ld\n",
  2976. ctx->num_const, (int64)c->value.i64);
  2977. }
  2978. /* use negetive index for const */
  2979. operand_offset = -(operand_offset + 1);
  2980. *offset = operand_offset;
  2981. return true;
  2982. fail:
  2983. return false;
  2984. }
  2985. /*
  2986. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  2987. -- Mostly used for the binary / compare operation
  2988. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  2989. -- Mostly used in block / control instructions
  2990. The POP will always emit the offset on the top of the frame_offset stack
  2991. PUSH can be used in two ways:
  2992. 1. directly PUSH:
  2993. PUSH_XXX();
  2994. will allocate a dynamic space and emit
  2995. 2. silent PUSH:
  2996. operand_offset = xxx; disable_emit = true;
  2997. PUSH_XXX();
  2998. only push the frame_offset stack, no emit
  2999. */
  3000. #define PUSH_I32() do { \
  3001. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  3002. disable_emit, operand_offset,\
  3003. error_buf, error_buf_size)) \
  3004. goto fail; \
  3005. } while (0)
  3006. #define PUSH_F32() do { \
  3007. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  3008. disable_emit, operand_offset,\
  3009. error_buf, error_buf_size)) \
  3010. goto fail; \
  3011. } while (0)
  3012. #define PUSH_I64() do { \
  3013. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  3014. disable_emit, operand_offset,\
  3015. error_buf, error_buf_size)) \
  3016. goto fail; \
  3017. } while (0)
  3018. #define PUSH_F64() do { \
  3019. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  3020. disable_emit, operand_offset,\
  3021. error_buf, error_buf_size)) \
  3022. goto fail; \
  3023. } while (0)
  3024. #define POP_I32() do { \
  3025. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  3026. error_buf, error_buf_size)) \
  3027. goto fail; \
  3028. } while (0)
  3029. #define POP_F32() do { \
  3030. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  3031. error_buf, error_buf_size)) \
  3032. goto fail; \
  3033. } while (0)
  3034. #define POP_I64() do { \
  3035. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  3036. error_buf, error_buf_size)) \
  3037. goto fail; \
  3038. } while (0)
  3039. #define POP_F64() do { \
  3040. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  3041. error_buf, error_buf_size)) \
  3042. goto fail; \
  3043. } while (0)
  3044. #define PUSH_OFFSET_TYPE(type) do { \
  3045. if (!(wasm_loader_push_frame_offset(loader_ctx, type, \
  3046. disable_emit, operand_offset, \
  3047. error_buf, error_buf_size))) \
  3048. goto fail; \
  3049. } while (0)
  3050. #define POP_OFFSET_TYPE(type) do { \
  3051. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, \
  3052. error_buf, error_buf_size))) \
  3053. goto fail; \
  3054. } while (0)
  3055. #define POP_AND_PUSH(type_pop, type_push) do { \
  3056. if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 1, \
  3057. type_push, type_pop, \
  3058. disable_emit, operand_offset, \
  3059. error_buf, error_buf_size))) \
  3060. goto fail; \
  3061. } while (0)
  3062. /* type of POPs should be the same */
  3063. #define POP2_AND_PUSH(type_pop, type_push) do { \
  3064. if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 2, \
  3065. type_push, type_pop, \
  3066. disable_emit, operand_offset, \
  3067. error_buf, error_buf_size))) \
  3068. goto fail; \
  3069. } while (0)
  3070. #else /* WASM_ENABLE_FAST_INTERP */
  3071. #define PUSH_I32() do { \
  3072. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  3073. error_buf, error_buf_size))) \
  3074. goto fail; \
  3075. } while (0)
  3076. #define PUSH_F32() do { \
  3077. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  3078. error_buf, error_buf_size))) \
  3079. goto fail; \
  3080. } while (0)
  3081. #define PUSH_I64() do { \
  3082. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  3083. error_buf, error_buf_size))) \
  3084. goto fail; \
  3085. } while (0)
  3086. #define PUSH_F64() do { \
  3087. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  3088. error_buf, error_buf_size))) \
  3089. goto fail; \
  3090. } while (0)
  3091. #define POP_I32() do { \
  3092. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  3093. error_buf, error_buf_size))) \
  3094. goto fail; \
  3095. } while (0)
  3096. #define POP_F32() do { \
  3097. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  3098. error_buf, error_buf_size))) \
  3099. goto fail; \
  3100. } while (0)
  3101. #define POP_I64() do { \
  3102. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  3103. error_buf, error_buf_size))) \
  3104. goto fail; \
  3105. } while (0)
  3106. #define POP_F64() do { \
  3107. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  3108. error_buf, error_buf_size))) \
  3109. goto fail; \
  3110. } while (0)
  3111. #define POP_AND_PUSH(type_pop, type_push) do { \
  3112. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, \
  3113. type_push, type_pop, \
  3114. error_buf, error_buf_size))) \
  3115. goto fail; \
  3116. } while (0)
  3117. /* type of POPs should be the same */
  3118. #define POP2_AND_PUSH(type_pop, type_push) do { \
  3119. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, \
  3120. type_push, type_pop, \
  3121. error_buf, error_buf_size))) \
  3122. goto fail; \
  3123. } while (0)
  3124. #endif /* WASM_ENABLE_FAST_INTERP */
  3125. #if WASM_ENABLE_FAST_INTERP != 0
  3126. static bool
  3127. reserve_block_ret(WASMLoaderContext *loader_ctx,
  3128. uint8 opcode, bool disable_emit,
  3129. char *error_buf, uint32 error_buf_size)
  3130. {
  3131. int16 operand_offset = 0;
  3132. BranchBlock *block = (opcode == WASM_OP_ELSE) ?
  3133. loader_ctx->frame_csp - 1 : loader_ctx->frame_csp;
  3134. BlockType *block_type = &block->block_type;
  3135. uint8 *return_types = NULL;
  3136. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  3137. int32 i = 0;
  3138. int16 dynamic_offset, dynamic_offset_org,
  3139. *frame_offset = NULL, *frame_offset_org = NULL;
  3140. return_count = block_type_get_result_types(block_type, &return_types);
  3141. /* Copy stack top values to block's results which are in dynamic space.
  3142. * The instruction format:
  3143. * Part a: values count
  3144. * Part b: all values total cell num
  3145. * Part c: each value's cell_num, src offset and dst offset
  3146. * Part d: each value's src offset and dst offset
  3147. * Part e: each value's dst offset
  3148. */
  3149. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  3150. dynamic_offset = dynamic_offset_org =
  3151. block->dynamic_offset
  3152. + wasm_get_cell_num(return_types, return_count);
  3153. /* First traversal to get the count of values needed to be copied. */
  3154. for (i = (int32)return_count - 1; i >= 0; i--) {
  3155. uint8 cells = wasm_value_type_cell_num(return_types[i]);
  3156. frame_offset -= cells;
  3157. dynamic_offset -= cells;
  3158. if (dynamic_offset != *frame_offset) {
  3159. value_count++;
  3160. total_cel_num += cells;
  3161. }
  3162. }
  3163. if (value_count) {
  3164. uint32 j = 0;
  3165. uint8 *emit_data = NULL, *cells = NULL;
  3166. int16 *src_offsets = NULL;
  3167. uint16 *dst_offsets = NULL;
  3168. uint64 size = (uint64)value_count * (sizeof(*cells)
  3169. + sizeof(*src_offsets)
  3170. + sizeof(*dst_offsets));
  3171. /* Allocate memory for the emit data */
  3172. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  3173. return false;
  3174. cells = emit_data;
  3175. src_offsets = (int16 *)(cells + value_count);
  3176. dst_offsets = (uint16 *)(src_offsets + value_count);
  3177. /* insert op_copy before else opcode */
  3178. if (opcode == WASM_OP_ELSE)
  3179. skip_label();
  3180. emit_label(EXT_OP_COPY_STACK_VALUES);
  3181. /* Part a) */
  3182. emit_uint32(loader_ctx, value_count);
  3183. /* Part b) */
  3184. emit_uint32(loader_ctx, total_cel_num);
  3185. /* Second traversal to get each value's cell num, src offset and dst offset. */
  3186. frame_offset = frame_offset_org;
  3187. dynamic_offset = dynamic_offset_org;
  3188. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  3189. uint8 cell = wasm_value_type_cell_num(return_types[i]);
  3190. frame_offset -= cell;
  3191. dynamic_offset -= cell;
  3192. if (dynamic_offset != *frame_offset) {
  3193. /* cell num */
  3194. cells[j] = cell;
  3195. /* src offset */
  3196. src_offsets[j] = *frame_offset;
  3197. /* dst offset */
  3198. dst_offsets[j] = dynamic_offset;
  3199. j++;
  3200. }
  3201. if (opcode == WASM_OP_ELSE) {
  3202. *frame_offset = dynamic_offset;
  3203. }
  3204. else {
  3205. loader_ctx->frame_offset = frame_offset;
  3206. loader_ctx->dynamic_offset = dynamic_offset;
  3207. PUSH_OFFSET_TYPE(return_types[i]);
  3208. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  3209. loader_ctx->frame_offset = frame_offset_org;
  3210. loader_ctx->dynamic_offset = dynamic_offset_org;
  3211. }
  3212. }
  3213. bh_assert(j == value_count);
  3214. /* Emit the cells, src_offsets and dst_offsets */
  3215. for (j = 0; j < value_count; j++)
  3216. emit_byte(loader_ctx, cells[j]);
  3217. for (j = 0; j < value_count; j++)
  3218. emit_operand(loader_ctx, src_offsets[j]);
  3219. for (j = 0; j < value_count; j++)
  3220. emit_operand(loader_ctx, dst_offsets[j]);
  3221. if (opcode == WASM_OP_ELSE)
  3222. emit_label(opcode);
  3223. wasm_runtime_free(emit_data);
  3224. }
  3225. return true;
  3226. fail:
  3227. return false;
  3228. }
  3229. #endif /* WASM_ENABLE_FAST_INTERP */
  3230. #define RESERVE_BLOCK_RET() do { \
  3231. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, \
  3232. error_buf, error_buf_size)) \
  3233. goto fail; \
  3234. } while (0)
  3235. #define PUSH_TYPE(type) do { \
  3236. if (!(wasm_loader_push_frame_ref(loader_ctx, type, \
  3237. error_buf, error_buf_size))) \
  3238. goto fail; \
  3239. } while (0)
  3240. #define POP_TYPE(type) do { \
  3241. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, \
  3242. error_buf, error_buf_size))) \
  3243. goto fail; \
  3244. } while (0)
  3245. #define PUSH_CSP(label_type, block_type, _start_addr) do { \
  3246. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  3247. _start_addr, error_buf, \
  3248. error_buf_size)) \
  3249. goto fail; \
  3250. } while (0)
  3251. #define POP_CSP() do { \
  3252. if (!wasm_loader_pop_frame_csp(loader_ctx, \
  3253. error_buf, error_buf_size)) \
  3254. goto fail; \
  3255. } while (0)
  3256. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \
  3257. read_leb_uint32(p, p_end, local_idx); \
  3258. bh_assert(local_idx < param_count + local_count);\
  3259. local_type = local_idx < param_count \
  3260. ? param_types[local_idx] \
  3261. : local_types[local_idx - param_count]; \
  3262. local_offset = local_offsets[local_idx]; \
  3263. } while (0)
  3264. #define CHECK_BR(depth) do { \
  3265. if (!wasm_loader_check_br(loader_ctx, depth, \
  3266. error_buf, error_buf_size)) \
  3267. goto fail; \
  3268. } while (0)
  3269. #define CHECK_MEMORY() do { \
  3270. bh_assert(module->import_memory_count \
  3271. + module->memory_count > 0); \
  3272. } while (0)
  3273. static bool
  3274. is_value_type(uint8 type)
  3275. {
  3276. return type == VALUE_TYPE_I32 ||
  3277. type == VALUE_TYPE_I64 ||
  3278. type == VALUE_TYPE_F32 ||
  3279. type == VALUE_TYPE_F64 ||
  3280. type == VALUE_TYPE_VOID;
  3281. }
  3282. static bool
  3283. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  3284. char *error_buf, uint32 error_buf_size)
  3285. {
  3286. BranchBlock *target_block, *cur_block;
  3287. BlockType *target_block_type;
  3288. uint8 *types = NULL, *frame_ref;
  3289. uint32 arity = 0;
  3290. int32 i, available_stack_cell;
  3291. uint16 cell_num;
  3292. if (loader_ctx->csp_num < depth + 1) {
  3293. set_error_buf(error_buf, error_buf_size,
  3294. "WASM module load failed: unknown label, "
  3295. "unexpected end of section or function");
  3296. return false;
  3297. }
  3298. cur_block = loader_ctx->frame_csp - 1;
  3299. target_block = loader_ctx->frame_csp - (depth + 1);
  3300. target_block_type = &target_block->block_type;
  3301. frame_ref = loader_ctx->frame_ref;
  3302. /* Note: loop's arity is different from if and block. loop's arity is
  3303. * its parameter count while if and block arity is result count.
  3304. */
  3305. if (target_block->label_type == LABEL_TYPE_LOOP)
  3306. arity = block_type_get_param_types(target_block_type, &types);
  3307. else
  3308. arity = block_type_get_result_types(target_block_type, &types);
  3309. /* If the stack is in polymorphic state, just clear the stack
  3310. * and then re-push the values to make the stack top values
  3311. * match block type. */
  3312. if (cur_block->is_stack_polymorphic) {
  3313. for (i = (int32)arity -1; i >= 0; i--) {
  3314. #if WASM_ENABLE_FAST_INTERP != 0
  3315. POP_OFFSET_TYPE(types[i]);
  3316. #endif
  3317. POP_TYPE(types[i]);
  3318. }
  3319. for (i = 0; i < (int32)arity; i++) {
  3320. #if WASM_ENABLE_FAST_INTERP != 0
  3321. bool disable_emit = true;
  3322. int16 operand_offset = 0;
  3323. PUSH_OFFSET_TYPE(types[i]);
  3324. #endif
  3325. PUSH_TYPE(types[i]);
  3326. }
  3327. return true;
  3328. }
  3329. available_stack_cell = (int32)
  3330. (loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  3331. /* Check stack top values match target block type */
  3332. for (i = (int32)arity -1; i >= 0; i--) {
  3333. if (!check_stack_top_values(frame_ref, available_stack_cell,
  3334. types[i],
  3335. error_buf, error_buf_size))
  3336. return false;
  3337. cell_num = wasm_value_type_cell_num(types[i]);
  3338. frame_ref -= cell_num;
  3339. available_stack_cell -= cell_num;
  3340. }
  3341. return true;
  3342. fail:
  3343. return false;
  3344. }
  3345. static BranchBlock *
  3346. check_branch_block(WASMLoaderContext *loader_ctx,
  3347. uint8 **p_buf, uint8 *buf_end,
  3348. char *error_buf, uint32 error_buf_size)
  3349. {
  3350. uint8 *p = *p_buf, *p_end = buf_end;
  3351. BranchBlock *frame_csp_tmp;
  3352. uint32 depth;
  3353. read_leb_uint32(p, p_end, depth);
  3354. CHECK_BR(depth);
  3355. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  3356. #if WASM_ENABLE_FAST_INTERP != 0
  3357. emit_br_info(frame_csp_tmp);
  3358. #endif
  3359. *p_buf = p;
  3360. return frame_csp_tmp;
  3361. fail:
  3362. return NULL;
  3363. }
  3364. static bool
  3365. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  3366. char *error_buf, uint32 error_buf_size)
  3367. {
  3368. BlockType *block_type = &block->block_type;
  3369. uint8 *return_types = NULL;
  3370. uint32 return_count = 0;
  3371. int32 available_stack_cell, return_cell_num, i;
  3372. uint8 *frame_ref = NULL;
  3373. available_stack_cell = (int32)
  3374. (loader_ctx->stack_cell_num
  3375. - block->stack_cell_num);
  3376. return_count = block_type_get_result_types(block_type, &return_types);
  3377. return_cell_num = return_count > 0 ?
  3378. wasm_get_cell_num(return_types, return_count) : 0;
  3379. /* If the stack is in polymorphic state, just clear the stack
  3380. * and then re-push the values to make the stack top values
  3381. * match block type. */
  3382. if (block->is_stack_polymorphic) {
  3383. for (i = (int32)return_count -1; i >= 0; i--) {
  3384. #if WASM_ENABLE_FAST_INTERP != 0
  3385. POP_OFFSET_TYPE(return_types[i]);
  3386. #endif
  3387. POP_TYPE(return_types[i]);
  3388. }
  3389. /* Check stack is empty */
  3390. bh_assert(loader_ctx->stack_cell_num == block->stack_cell_num);
  3391. for (i = 0; i < (int32)return_count; i++) {
  3392. #if WASM_ENABLE_FAST_INTERP != 0
  3393. bool disable_emit = true;
  3394. int16 operand_offset = 0;
  3395. PUSH_OFFSET_TYPE(return_types[i]);
  3396. #endif
  3397. PUSH_TYPE(return_types[i]);
  3398. }
  3399. return true;
  3400. }
  3401. /* Check stack cell num equals return cell num */
  3402. bh_assert(available_stack_cell == return_cell_num);
  3403. /* Check stack values match return types */
  3404. frame_ref = loader_ctx->frame_ref;
  3405. for (i = (int32)return_count -1; i >= 0; i--) {
  3406. if (!check_stack_top_values(frame_ref, available_stack_cell,
  3407. return_types[i],
  3408. error_buf, error_buf_size))
  3409. return false;
  3410. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  3411. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  3412. }
  3413. (void)return_cell_num;
  3414. return true;
  3415. fail:
  3416. return false;
  3417. }
  3418. #if WASM_ENABLE_FAST_INTERP != 0
  3419. /* Copy parameters to dynamic space.
  3420. * 1) POP original parameter out;
  3421. * 2) Push and copy original values to dynamic space.
  3422. * The copy instruction format:
  3423. * Part a: param count
  3424. * Part b: all param total cell num
  3425. * Part c: each param's cell_num, src offset and dst offset
  3426. * Part d: each param's src offset
  3427. * Part e: each param's dst offset
  3428. */
  3429. static bool
  3430. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  3431. char* error_buf, uint32 error_buf_size)
  3432. {
  3433. int16 *frame_offset = NULL;
  3434. uint8 *cells = NULL, cell;
  3435. int16 *src_offsets = NULL;
  3436. uint8 *emit_data = NULL;
  3437. uint32 i;
  3438. BranchBlock *block = loader_ctx->frame_csp - 1;
  3439. BlockType *block_type = &block->block_type;
  3440. WASMType *wasm_type = block_type->u.type;
  3441. uint32 param_count = block_type->u.type->param_count;
  3442. int16 condition_offset = 0;
  3443. bool disable_emit = false;
  3444. int16 operand_offset = 0;
  3445. uint64 size = (uint64)param_count * (sizeof(*cells)
  3446. + sizeof(*src_offsets));
  3447. /* For if block, we also need copy the condition operand offset. */
  3448. if (is_if_block)
  3449. size += sizeof(*cells) + sizeof(*src_offsets);
  3450. /* Allocate memory for the emit data */
  3451. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  3452. return false;
  3453. cells = emit_data;
  3454. src_offsets = (int16 *)(cells + param_count);
  3455. if (is_if_block)
  3456. condition_offset = *loader_ctx->frame_offset;
  3457. /* POP original parameter out */
  3458. for (i = 0; i < param_count; i++) {
  3459. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  3460. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  3461. }
  3462. frame_offset = loader_ctx->frame_offset;
  3463. /* Get each param's cell num and src offset */
  3464. for (i = 0; i < param_count; i++) {
  3465. cell = wasm_value_type_cell_num(wasm_type->types[i]);
  3466. cells[i] = cell;
  3467. src_offsets[i] = *frame_offset;
  3468. frame_offset += cell;
  3469. }
  3470. /* emit copy instruction */
  3471. emit_label(EXT_OP_COPY_STACK_VALUES);
  3472. /* Part a) */
  3473. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  3474. /* Part b) */
  3475. emit_uint32(loader_ctx, is_if_block ?
  3476. wasm_type->param_cell_num + 1 :
  3477. wasm_type->param_cell_num);
  3478. /* Part c) */
  3479. for (i = 0; i < param_count; i++)
  3480. emit_byte(loader_ctx, cells[i]);
  3481. if (is_if_block)
  3482. emit_byte(loader_ctx, 1);
  3483. /* Part d) */
  3484. for (i = 0; i < param_count; i++)
  3485. emit_operand(loader_ctx, src_offsets[i]);
  3486. if (is_if_block)
  3487. emit_operand(loader_ctx, condition_offset);
  3488. /* Part e) */
  3489. /* Push to dynamic space. The push will emit the dst offset. */
  3490. for (i = 0; i < param_count; i++)
  3491. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  3492. if (is_if_block)
  3493. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  3494. /* Free the emit data */
  3495. wasm_runtime_free(emit_data);
  3496. return true;
  3497. fail:
  3498. return false;
  3499. }
  3500. #endif
  3501. /* reset the stack to the state of before entering the last block */
  3502. #if WASM_ENABLE_FAST_INTERP != 0
  3503. #define RESET_STACK() do { \
  3504. loader_ctx->stack_cell_num = \
  3505. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  3506. loader_ctx->frame_ref = \
  3507. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  3508. loader_ctx->frame_offset = \
  3509. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  3510. } while (0)
  3511. #else
  3512. #define RESET_STACK() do { \
  3513. loader_ctx->stack_cell_num = \
  3514. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  3515. loader_ctx->frame_ref = \
  3516. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  3517. } while (0)
  3518. #endif
  3519. /* set current block's stack polymorphic state */
  3520. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) do { \
  3521. BranchBlock *cur_block = loader_ctx->frame_csp - 1; \
  3522. cur_block->is_stack_polymorphic = flag; \
  3523. } while (0)
  3524. #define BLOCK_HAS_PARAM(block_type) \
  3525. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  3526. static bool
  3527. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  3528. BlockAddr *block_addr_cache,
  3529. char *error_buf, uint32 error_buf_size)
  3530. {
  3531. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  3532. uint32 param_count, local_count, global_count;
  3533. uint8 *param_types, *local_types, local_type, global_type;
  3534. BlockType func_type;
  3535. uint16 *local_offsets, local_offset;
  3536. uint32 count, i, local_idx, global_idx, u32, align, mem_offset;
  3537. int32 i32, i32_const = 0;
  3538. int64 i64;
  3539. uint8 opcode, u8;
  3540. bool return_value = false;
  3541. WASMLoaderContext *loader_ctx;
  3542. BranchBlock *frame_csp_tmp;
  3543. #if WASM_ENABLE_BULK_MEMORY != 0
  3544. uint32 segment_index;
  3545. #endif
  3546. #if WASM_ENABLE_FAST_INTERP != 0
  3547. uint8 *func_const_end, *func_const;
  3548. int16 operand_offset;
  3549. uint8 last_op = 0;
  3550. bool disable_emit, preserve_local = false;
  3551. float32 f32;
  3552. float64 f64;
  3553. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  3554. func->param_cell_num,
  3555. func->local_cell_num,
  3556. func->ret_cell_num);
  3557. #endif
  3558. global_count = module->import_global_count + module->global_count;
  3559. param_count = func->func_type->param_count;
  3560. param_types = func->func_type->types;
  3561. func_type.is_value_type = false;
  3562. func_type.u.type = func->func_type;
  3563. local_count = func->local_count;
  3564. local_types = func->local_types;
  3565. local_offsets = func->local_offsets;
  3566. if (!(loader_ctx = wasm_loader_ctx_init(func))) {
  3567. set_error_buf(error_buf, error_buf_size,
  3568. "WASM loader prepare bytecode failed: "
  3569. "allocate memory failed");
  3570. goto fail;
  3571. }
  3572. #if WASM_ENABLE_FAST_INTERP != 0
  3573. re_scan:
  3574. if (loader_ctx->code_compiled_size > 0) {
  3575. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  3576. set_error_buf(error_buf, error_buf_size,
  3577. "WASM loader prepare bytecode failed: "
  3578. "allocate memory failed");
  3579. goto fail;
  3580. }
  3581. p = func->code;
  3582. func->code_compiled = loader_ctx->p_code_compiled;
  3583. }
  3584. #endif
  3585. PUSH_CSP(LABEL_TYPE_FUNCTION, func_type, p);
  3586. while (p < p_end) {
  3587. opcode = *p++;
  3588. #if WASM_ENABLE_FAST_INTERP != 0
  3589. p_org = p;
  3590. disable_emit = false;
  3591. emit_label(opcode);
  3592. #endif
  3593. switch (opcode) {
  3594. case WASM_OP_UNREACHABLE:
  3595. RESET_STACK();
  3596. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  3597. break;
  3598. case WASM_OP_NOP:
  3599. #if WASM_ENABLE_FAST_INTERP != 0
  3600. skip_label();
  3601. #endif
  3602. break;
  3603. case WASM_OP_IF:
  3604. POP_I32();
  3605. goto handle_op_block_and_loop;
  3606. case WASM_OP_BLOCK:
  3607. case WASM_OP_LOOP:
  3608. handle_op_block_and_loop:
  3609. {
  3610. uint8 value_type;
  3611. BlockType block_type;
  3612. value_type = read_uint8(p);
  3613. if (is_value_type(value_type)) {
  3614. /* If the first byte is one of these special values:
  3615. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  3616. * the single return value. */
  3617. block_type.is_value_type = true;
  3618. block_type.u.value_type = value_type;
  3619. }
  3620. else {
  3621. uint32 type_index;
  3622. /* Resolve the leb128 encoded type index as block type */
  3623. p--;
  3624. read_leb_uint32(p, p_end, type_index);
  3625. bh_assert(type_index < module->type_count);
  3626. block_type.is_value_type = false;
  3627. block_type.u.type = module->types[type_index];
  3628. #if WASM_ENABLE_FAST_INTERP == 0 \
  3629. && WASM_ENABLE_WAMR_COMPILER == 0 \
  3630. && WASM_ENABLE_JIT == 0
  3631. /* If block use type index as block type, change the opcode
  3632. * to new extended opcode so that interpreter can resolve the
  3633. * block quickly.
  3634. */
  3635. *(p - 2) = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  3636. #endif
  3637. }
  3638. /* Pop block parameters from stack */
  3639. if (BLOCK_HAS_PARAM(block_type)) {
  3640. WASMType *wasm_type = block_type.u.type;
  3641. for (i = 0; i < block_type.u.type->param_count; i++)
  3642. POP_TYPE(wasm_type->types[wasm_type->param_count - i - 1]);
  3643. }
  3644. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK), block_type, p);
  3645. /* Pass parameters to block */
  3646. if (BLOCK_HAS_PARAM(block_type)) {
  3647. for (i = 0; i < block_type.u.type->param_count; i++)
  3648. PUSH_TYPE(block_type.u.type->types[i]);
  3649. }
  3650. #if WASM_ENABLE_FAST_INTERP != 0
  3651. if (opcode == WASM_OP_BLOCK) {
  3652. skip_label();
  3653. } else if (opcode == WASM_OP_LOOP) {
  3654. skip_label();
  3655. if (BLOCK_HAS_PARAM(block_type)) {
  3656. /* Make sure params are in dynamic space */
  3657. if (!copy_params_to_dynamic_space(loader_ctx,
  3658. false,
  3659. error_buf,
  3660. error_buf_size))
  3661. goto fail;
  3662. }
  3663. (loader_ctx->frame_csp - 1)->code_compiled =
  3664. loader_ctx->p_code_compiled;
  3665. } else if (opcode == WASM_OP_IF) {
  3666. /* If block has parameters, we should make sure they are in
  3667. * dynamic space. Otherwise, when else branch is missing,
  3668. * the later opcode may consume incorrect operand offset.
  3669. * Spec case:
  3670. * (func (export "params-id") (param i32) (result i32)
  3671. * (i32.const 1)
  3672. * (i32.const 2)
  3673. * (if (param i32 i32) (result i32 i32) (local.get 0) (then))
  3674. * (i32.add)
  3675. * )
  3676. *
  3677. * So we should emit a copy instruction before the if.
  3678. *
  3679. * And we also need to save the parameter offsets and
  3680. * recover them before entering else branch.
  3681. *
  3682. */
  3683. if (BLOCK_HAS_PARAM(block_type)) {
  3684. BranchBlock *block = loader_ctx->frame_csp - 1;
  3685. uint64 size;
  3686. /* skip the if condition operand offset */
  3687. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  3688. /* skip the if label */
  3689. skip_label();
  3690. /* Emit a copy instruction */
  3691. if (!copy_params_to_dynamic_space(loader_ctx,
  3692. true,
  3693. error_buf,
  3694. error_buf_size))
  3695. goto fail;
  3696. /* Emit the if instruction */
  3697. emit_label(opcode);
  3698. /* Emit the new condition operand offset */
  3699. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  3700. /* Save top param_count values of frame_offset stack, so that
  3701. * we can recover it before executing else branch */
  3702. size = sizeof(int16) *
  3703. (uint64)block_type.u.type->param_cell_num;
  3704. if (!(block->param_frame_offsets =
  3705. loader_malloc(size, error_buf, error_buf_size)))
  3706. goto fail;
  3707. bh_memcpy_s(block->param_frame_offsets,
  3708. size,
  3709. loader_ctx->frame_offset - size/sizeof(int16),
  3710. size);
  3711. }
  3712. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  3713. emit_empty_label_addr_and_frame_ip(PATCH_END);
  3714. }
  3715. #endif
  3716. break;
  3717. }
  3718. case WASM_OP_ELSE:
  3719. {
  3720. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  3721. bh_assert(loader_ctx->csp_num >= 2
  3722. && (loader_ctx->frame_csp - 1)->label_type
  3723. == LABEL_TYPE_IF);
  3724. /* check whether if branch's stack matches its result type */
  3725. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  3726. error_buf, error_buf_size))
  3727. goto fail;
  3728. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  3729. #if WASM_ENABLE_FAST_INTERP != 0
  3730. /* if the result of if branch is in local or const area, add a copy op */
  3731. RESERVE_BLOCK_RET();
  3732. emit_empty_label_addr_and_frame_ip(PATCH_END);
  3733. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  3734. #endif
  3735. RESET_STACK();
  3736. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  3737. /* Pass parameters to if-false branch */
  3738. if (BLOCK_HAS_PARAM(block_type)) {
  3739. for (i = 0; i < block_type.u.type->param_count; i++)
  3740. PUSH_TYPE(block_type.u.type->types[i]);
  3741. }
  3742. #if WASM_ENABLE_FAST_INTERP != 0
  3743. /* Recover top param_count values of frame_offset stack */
  3744. if (BLOCK_HAS_PARAM((block_type))) {
  3745. uint32 size;
  3746. BranchBlock *block = loader_ctx->frame_csp - 1;
  3747. size = sizeof(int16) *
  3748. block_type.u.type->param_cell_num;
  3749. bh_memcpy_s(loader_ctx->frame_offset, size,
  3750. block->param_frame_offsets, size);
  3751. loader_ctx->frame_offset += (size/sizeof(int16));
  3752. }
  3753. #endif
  3754. break;
  3755. }
  3756. case WASM_OP_END:
  3757. {
  3758. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  3759. /* check whether block stack matches its result type */
  3760. if (!check_block_stack(loader_ctx, cur_block,
  3761. error_buf, error_buf_size))
  3762. goto fail;
  3763. /* if no else branch, and return types do not match param types, fail */
  3764. if (cur_block->label_type == LABEL_TYPE_IF
  3765. && !cur_block->else_addr) {
  3766. uint32 param_count = 0, ret_count = 0;
  3767. uint8 *param_types = NULL, *ret_types = NULL;
  3768. BlockType *block_type = &cur_block->block_type;
  3769. if (block_type->is_value_type) {
  3770. if (block_type->u.value_type != VALUE_TYPE_VOID) {
  3771. ret_count = 1;
  3772. ret_types = &block_type->u.value_type;
  3773. }
  3774. }
  3775. else {
  3776. param_count = block_type->u.type->param_count;
  3777. ret_count = block_type->u.type->result_count;
  3778. param_types = block_type->u.type->types;
  3779. ret_types = block_type->u.type->types + param_count;
  3780. }
  3781. bh_assert(param_count == ret_count
  3782. && (!param_count
  3783. || !memcmp(param_types, ret_types, param_count)));
  3784. (void)ret_types;
  3785. (void)ret_count;
  3786. (void)param_types;
  3787. }
  3788. POP_CSP();
  3789. #if WASM_ENABLE_FAST_INTERP != 0
  3790. skip_label();
  3791. /* copy the result to the block return address */
  3792. RESERVE_BLOCK_RET();
  3793. apply_label_patch(loader_ctx, 0, PATCH_END);
  3794. free_label_patch_list(loader_ctx->frame_csp);
  3795. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  3796. int32 idx;
  3797. uint8 ret_type;
  3798. emit_label(WASM_OP_RETURN);
  3799. for (idx = (int32)func->func_type->result_count - 1;
  3800. idx >= 0; idx--) {
  3801. ret_type = *(func->func_type->types
  3802. + func->func_type->param_count + idx);
  3803. POP_OFFSET_TYPE(ret_type);
  3804. }
  3805. }
  3806. #endif
  3807. if (loader_ctx->csp_num > 0) {
  3808. loader_ctx->frame_csp->end_addr = p - 1;
  3809. }
  3810. else {
  3811. /* end of function block, function will return,
  3812. ignore the following bytecodes */
  3813. p = p_end;
  3814. continue;
  3815. }
  3816. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  3817. break;
  3818. }
  3819. case WASM_OP_BR:
  3820. {
  3821. if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end,
  3822. error_buf, error_buf_size)))
  3823. goto fail;
  3824. RESET_STACK();
  3825. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  3826. break;
  3827. }
  3828. case WASM_OP_BR_IF:
  3829. {
  3830. POP_I32();
  3831. if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end,
  3832. error_buf, error_buf_size)))
  3833. goto fail;
  3834. break;
  3835. }
  3836. case WASM_OP_BR_TABLE:
  3837. {
  3838. uint8 *ret_types = NULL;
  3839. uint32 ret_count = 0;
  3840. read_leb_uint32(p, p_end, count);
  3841. #if WASM_ENABLE_FAST_INTERP != 0
  3842. emit_uint32(loader_ctx, count);
  3843. #endif
  3844. POP_I32();
  3845. /* TODO: check the const */
  3846. for (i = 0; i <= count; i++) {
  3847. if (!(frame_csp_tmp =
  3848. check_branch_block(loader_ctx, &p, p_end,
  3849. error_buf, error_buf_size)))
  3850. goto fail;
  3851. }
  3852. RESET_STACK();
  3853. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  3854. (void)ret_count;
  3855. (void)ret_types;
  3856. break;
  3857. }
  3858. case WASM_OP_RETURN:
  3859. {
  3860. int32 idx;
  3861. uint8 ret_type;
  3862. for (idx = (int32)func->func_type->result_count - 1; idx >= 0; idx--) {
  3863. ret_type = *(func->func_type->types
  3864. + func->func_type->param_count + idx);
  3865. POP_TYPE(ret_type);
  3866. #if WASM_ENABLE_FAST_INTERP != 0
  3867. // emit the offset after return opcode
  3868. POP_OFFSET_TYPE(ret_type);
  3869. #endif
  3870. }
  3871. RESET_STACK();
  3872. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  3873. break;
  3874. }
  3875. case WASM_OP_CALL:
  3876. {
  3877. WASMType *func_type;
  3878. uint32 func_idx;
  3879. int32 idx;
  3880. read_leb_uint32(p, p_end, func_idx);
  3881. #if WASM_ENABLE_FAST_INTERP != 0
  3882. // we need to emit func_idx before arguments
  3883. emit_uint32(loader_ctx, func_idx);
  3884. #endif
  3885. bh_assert(func_idx < module->import_function_count
  3886. + module->function_count);
  3887. if (func_idx < module->import_function_count)
  3888. func_type = module->import_functions[func_idx].u.function.func_type;
  3889. else
  3890. func_type =
  3891. module->functions[func_idx - module->import_function_count]->func_type;
  3892. if (func_type->param_count > 0) {
  3893. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) {
  3894. POP_TYPE(func_type->types[idx]);
  3895. #if WASM_ENABLE_FAST_INTERP != 0
  3896. POP_OFFSET_TYPE(func_type->types[idx]);
  3897. #endif
  3898. }
  3899. }
  3900. for (i = 0; i < func_type->result_count; i++) {
  3901. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  3902. #if WASM_ENABLE_FAST_INTERP != 0
  3903. /* Here we emit each return value's dynamic_offset. But in fact
  3904. * these offsets are continuous, so interpreter only need to get
  3905. * the first return value's offset.
  3906. */
  3907. PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]);
  3908. #endif
  3909. }
  3910. func->has_op_func_call = true;
  3911. break;
  3912. }
  3913. case WASM_OP_CALL_INDIRECT:
  3914. {
  3915. int32 idx;
  3916. WASMType *func_type;
  3917. uint32 type_idx;
  3918. bh_assert(module->import_table_count
  3919. + module->table_count > 0);
  3920. read_leb_uint32(p, p_end, type_idx);
  3921. #if WASM_ENABLE_FAST_INTERP != 0
  3922. // we need to emit func_idx before arguments
  3923. emit_uint32(loader_ctx, type_idx);
  3924. #endif
  3925. /* reserved byte 0x00 */
  3926. bh_assert(*p == 0x00);
  3927. p++;
  3928. POP_I32();
  3929. bh_assert(type_idx < module->type_count);
  3930. func_type = module->types[type_idx];
  3931. if (func_type->param_count > 0) {
  3932. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) {
  3933. POP_TYPE(func_type->types[idx]);
  3934. #if WASM_ENABLE_FAST_INTERP != 0
  3935. POP_OFFSET_TYPE(func_type->types[idx]);
  3936. #endif
  3937. }
  3938. }
  3939. for (i = 0; i < func_type->result_count; i++) {
  3940. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  3941. #if WASM_ENABLE_FAST_INTERP != 0
  3942. PUSH_OFFSET_TYPE(func_type->types[func_type->param_count + i]);
  3943. #endif
  3944. }
  3945. func->has_op_func_call = true;
  3946. break;
  3947. }
  3948. case WASM_OP_DROP:
  3949. case WASM_OP_DROP_64:
  3950. {
  3951. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  3952. int32 available_stack_cell = (int32)
  3953. (loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  3954. bh_assert(!(available_stack_cell <= 0
  3955. && !cur_block->is_stack_polymorphic));
  3956. if (available_stack_cell > 0) {
  3957. if (*(loader_ctx->frame_ref - 1) == REF_I32
  3958. || *(loader_ctx->frame_ref - 1) == REF_F32) {
  3959. loader_ctx->frame_ref--;
  3960. loader_ctx->stack_cell_num--;
  3961. #if WASM_ENABLE_FAST_INTERP != 0
  3962. skip_label();
  3963. loader_ctx->frame_offset--;
  3964. if (*(loader_ctx->frame_offset) >
  3965. loader_ctx->start_dynamic_offset)
  3966. loader_ctx->dynamic_offset --;
  3967. #endif
  3968. }
  3969. else {
  3970. loader_ctx->frame_ref -= 2;
  3971. loader_ctx->stack_cell_num -= 2;
  3972. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  3973. *(p - 1) = WASM_OP_DROP_64;
  3974. #endif
  3975. #if WASM_ENABLE_FAST_INTERP != 0
  3976. skip_label();
  3977. loader_ctx->frame_offset -= 2;
  3978. if (*(loader_ctx->frame_offset) >
  3979. loader_ctx->start_dynamic_offset)
  3980. loader_ctx->dynamic_offset -= 2;
  3981. #endif
  3982. }
  3983. }
  3984. else {
  3985. #if WASM_ENABLE_FAST_INTERP != 0
  3986. skip_label();
  3987. #endif
  3988. }
  3989. break;
  3990. }
  3991. case WASM_OP_SELECT:
  3992. case WASM_OP_SELECT_64:
  3993. {
  3994. uint8 ref_type;
  3995. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  3996. int32 available_stack_cell;
  3997. POP_I32();
  3998. available_stack_cell = (int32)
  3999. (loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  4000. bh_assert(!(available_stack_cell <= 0
  4001. && !cur_block->is_stack_polymorphic));
  4002. if (available_stack_cell > 0) {
  4003. switch (*(loader_ctx->frame_ref - 1)) {
  4004. case REF_I32:
  4005. case REF_F32:
  4006. break;
  4007. case REF_I64_2:
  4008. case REF_F64_2:
  4009. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  4010. *(p - 1) = WASM_OP_SELECT_64;
  4011. #endif
  4012. #if WASM_ENABLE_FAST_INTERP != 0
  4013. if (loader_ctx->p_code_compiled) {
  4014. #if WASM_ENABLE_ABS_LABEL_ADDR != 0
  4015. *(void**)(loader_ctx->p_code_compiled - 2 - sizeof(void*)) =
  4016. handle_table[WASM_OP_SELECT_64];
  4017. #else
  4018. *((int16*)loader_ctx->p_code_compiled - 2) = (int16)
  4019. (handle_table[WASM_OP_SELECT_64] - handle_table[0]);
  4020. #endif
  4021. }
  4022. #endif
  4023. break;
  4024. }
  4025. ref_type = *(loader_ctx->frame_ref - 1);
  4026. #if WASM_ENABLE_FAST_INTERP != 0
  4027. POP_OFFSET_TYPE(ref_type);
  4028. #endif
  4029. POP_TYPE(ref_type);
  4030. #if WASM_ENABLE_FAST_INTERP != 0
  4031. POP_OFFSET_TYPE(ref_type);
  4032. #endif
  4033. POP_TYPE(ref_type);
  4034. #if WASM_ENABLE_FAST_INTERP != 0
  4035. PUSH_OFFSET_TYPE(ref_type);
  4036. #endif
  4037. PUSH_TYPE(ref_type);
  4038. }
  4039. else {
  4040. #if WASM_ENABLE_FAST_INTERP != 0
  4041. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  4042. #endif
  4043. PUSH_TYPE(VALUE_TYPE_ANY);
  4044. }
  4045. break;
  4046. }
  4047. case WASM_OP_GET_LOCAL:
  4048. {
  4049. p_org = p - 1;
  4050. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  4051. PUSH_TYPE(local_type);
  4052. #if WASM_ENABLE_FAST_INTERP != 0
  4053. /* Get Local is optimized out */
  4054. skip_label();
  4055. disable_emit = true;
  4056. operand_offset = local_offset;
  4057. PUSH_OFFSET_TYPE(local_type);
  4058. #else
  4059. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  4060. if (local_offset < 0x80) {
  4061. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  4062. if (local_type == VALUE_TYPE_I32
  4063. || local_type == VALUE_TYPE_F32)
  4064. *p_org++ = (uint8)local_offset;
  4065. else
  4066. *p_org++ = (uint8)(local_offset | 0x80);
  4067. while (p_org < p)
  4068. *p_org++ = WASM_OP_NOP;
  4069. }
  4070. #endif
  4071. #endif
  4072. break;
  4073. }
  4074. case WASM_OP_SET_LOCAL:
  4075. {
  4076. p_org = p - 1;
  4077. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  4078. POP_TYPE(local_type);
  4079. #if WASM_ENABLE_FAST_INTERP != 0
  4080. if (!(preserve_referenced_local(loader_ctx, opcode, local_offset,
  4081. local_type, &preserve_local,
  4082. error_buf, error_buf_size)))
  4083. goto fail;
  4084. if (local_offset < 256) {
  4085. skip_label();
  4086. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  4087. if (loader_ctx->p_code_compiled)
  4088. *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
  4089. loader_ctx->frame_offset --;
  4090. loader_ctx->dynamic_offset --;
  4091. }
  4092. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  4093. if (loader_ctx->p_code_compiled)
  4094. *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
  4095. loader_ctx->frame_offset -= 2;
  4096. loader_ctx->dynamic_offset -= 2;
  4097. }
  4098. else {
  4099. if (local_type == VALUE_TYPE_I32
  4100. || local_type == VALUE_TYPE_F32) {
  4101. emit_label(EXT_OP_SET_LOCAL_FAST);
  4102. emit_byte(loader_ctx, local_offset);
  4103. }
  4104. else {
  4105. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  4106. emit_byte(loader_ctx, local_offset);
  4107. }
  4108. POP_OFFSET_TYPE(local_type);
  4109. }
  4110. }
  4111. else { /* local index larger than 255, reserve leb */
  4112. p_org ++;
  4113. emit_leb();
  4114. POP_OFFSET_TYPE(local_type);
  4115. }
  4116. #else
  4117. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  4118. if (local_offset < 0x80) {
  4119. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  4120. if (local_type == VALUE_TYPE_I32
  4121. || local_type == VALUE_TYPE_F32)
  4122. *p_org++ = (uint8)local_offset;
  4123. else
  4124. *p_org++ = (uint8)(local_offset | 0x80);
  4125. while (p_org < p)
  4126. *p_org++ = WASM_OP_NOP;
  4127. }
  4128. #endif
  4129. #endif
  4130. break;
  4131. }
  4132. case WASM_OP_TEE_LOCAL:
  4133. {
  4134. p_org = p - 1;
  4135. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  4136. #if WASM_ENABLE_FAST_INTERP != 0
  4137. /* If the stack is in polymorphic state, do fake pop and push on
  4138. offset stack to keep the depth of offset stack to be the same
  4139. with ref stack */
  4140. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  4141. if (cur_block->is_stack_polymorphic) {
  4142. POP_OFFSET_TYPE(local_type);
  4143. PUSH_OFFSET_TYPE(local_type);
  4144. }
  4145. #endif
  4146. POP_TYPE(local_type);
  4147. PUSH_TYPE(local_type);
  4148. #if WASM_ENABLE_FAST_INTERP != 0
  4149. if (!(preserve_referenced_local(loader_ctx, opcode, local_offset,
  4150. local_type, &preserve_local,
  4151. error_buf, error_buf_size)))
  4152. goto fail;
  4153. if (local_offset < 256) {
  4154. skip_label();
  4155. if (local_type == VALUE_TYPE_I32
  4156. || local_type == VALUE_TYPE_F32) {
  4157. emit_label(EXT_OP_TEE_LOCAL_FAST);
  4158. emit_byte(loader_ctx, local_offset);
  4159. }
  4160. else {
  4161. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  4162. emit_byte(loader_ctx, local_offset);
  4163. }
  4164. }
  4165. else { /* local index larger than 255, reserve leb */
  4166. p_org ++;
  4167. emit_leb();
  4168. }
  4169. emit_operand(loader_ctx, *(loader_ctx->frame_offset -
  4170. wasm_value_type_cell_num(local_type)));
  4171. #else
  4172. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  4173. if (local_offset < 0x80) {
  4174. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  4175. if (local_type == VALUE_TYPE_I32
  4176. || local_type == VALUE_TYPE_F32)
  4177. *p_org++ = (uint8)local_offset;
  4178. else
  4179. *p_org++ = (uint8)(local_offset | 0x80);
  4180. while (p_org < p)
  4181. *p_org++ = WASM_OP_NOP;
  4182. }
  4183. #endif
  4184. #endif
  4185. break;
  4186. }
  4187. case WASM_OP_GET_GLOBAL:
  4188. {
  4189. p_org = p - 1;
  4190. read_leb_uint32(p, p_end, global_idx);
  4191. bh_assert(global_idx < global_count);
  4192. global_type =
  4193. global_idx < module->import_global_count
  4194. ? module->import_globals[global_idx].u.global.type
  4195. : module->globals[global_idx - module->import_global_count]
  4196. .type;
  4197. PUSH_TYPE(global_type);
  4198. #if WASM_ENABLE_FAST_INTERP == 0
  4199. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  4200. if (global_type == VALUE_TYPE_I64
  4201. || global_type == VALUE_TYPE_F64) {
  4202. *p_org = WASM_OP_GET_GLOBAL_64;
  4203. }
  4204. #endif
  4205. #else /* else of WASM_ENABLE_FAST_INTERP */
  4206. if (global_type == VALUE_TYPE_I64
  4207. || global_type == VALUE_TYPE_F64) {
  4208. skip_label();
  4209. emit_label(WASM_OP_GET_GLOBAL_64);
  4210. }
  4211. emit_uint32(loader_ctx, global_idx);
  4212. PUSH_OFFSET_TYPE(global_type);
  4213. #endif /* end of WASM_ENABLE_FAST_INTERP */
  4214. break;
  4215. }
  4216. case WASM_OP_SET_GLOBAL:
  4217. {
  4218. bool is_mutable = false;
  4219. p_org = p - 1;
  4220. read_leb_uint32(p, p_end, global_idx);
  4221. bh_assert(global_idx < global_count);
  4222. is_mutable =
  4223. global_idx < module->import_global_count
  4224. ? module->import_globals[global_idx].u.global.is_mutable
  4225. : module->globals[global_idx - module->import_global_count]
  4226. .is_mutable;
  4227. bh_assert(is_mutable);
  4228. global_type =
  4229. global_idx < module->import_global_count
  4230. ? module->import_globals[global_idx].u.global.type
  4231. : module->globals[global_idx - module->import_global_count]
  4232. .type;
  4233. POP_TYPE(global_type);
  4234. #if WASM_ENABLE_FAST_INTERP == 0
  4235. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  4236. if (global_type == VALUE_TYPE_I64
  4237. || global_type == VALUE_TYPE_F64) {
  4238. *p_org = WASM_OP_SET_GLOBAL_64;
  4239. }
  4240. else if (module->llvm_aux_stack_size > 0
  4241. && global_idx == module->llvm_aux_stack_global_index) {
  4242. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  4243. }
  4244. #endif
  4245. #else /* else of WASM_ENABLE_FAST_INTERP */
  4246. if (global_type == VALUE_TYPE_I64
  4247. || global_type == VALUE_TYPE_F64) {
  4248. skip_label();
  4249. emit_label(WASM_OP_SET_GLOBAL_64);
  4250. }
  4251. else if (module->llvm_aux_stack_size > 0
  4252. && global_idx == module->llvm_aux_stack_global_index) {
  4253. skip_label();
  4254. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  4255. }
  4256. emit_uint32(loader_ctx, global_idx);
  4257. POP_OFFSET_TYPE(global_type);
  4258. #endif /* end of WASM_ENABLE_FAST_INTERP */
  4259. (void)is_mutable;
  4260. break;
  4261. }
  4262. /* load */
  4263. case WASM_OP_I32_LOAD:
  4264. case WASM_OP_I32_LOAD8_S:
  4265. case WASM_OP_I32_LOAD8_U:
  4266. case WASM_OP_I32_LOAD16_S:
  4267. case WASM_OP_I32_LOAD16_U:
  4268. case WASM_OP_I64_LOAD:
  4269. case WASM_OP_I64_LOAD8_S:
  4270. case WASM_OP_I64_LOAD8_U:
  4271. case WASM_OP_I64_LOAD16_S:
  4272. case WASM_OP_I64_LOAD16_U:
  4273. case WASM_OP_I64_LOAD32_S:
  4274. case WASM_OP_I64_LOAD32_U:
  4275. case WASM_OP_F32_LOAD:
  4276. case WASM_OP_F64_LOAD:
  4277. /* store */
  4278. case WASM_OP_I32_STORE:
  4279. case WASM_OP_I32_STORE8:
  4280. case WASM_OP_I32_STORE16:
  4281. case WASM_OP_I64_STORE:
  4282. case WASM_OP_I64_STORE8:
  4283. case WASM_OP_I64_STORE16:
  4284. case WASM_OP_I64_STORE32:
  4285. case WASM_OP_F32_STORE:
  4286. case WASM_OP_F64_STORE:
  4287. {
  4288. #if WASM_ENABLE_FAST_INTERP != 0
  4289. /* change F32/F64 into I32/I64 */
  4290. if (opcode == WASM_OP_F32_LOAD) {
  4291. skip_label();
  4292. emit_label(WASM_OP_I32_LOAD);
  4293. }
  4294. else if (opcode == WASM_OP_F64_LOAD) {
  4295. skip_label();
  4296. emit_label(WASM_OP_I64_LOAD);
  4297. }
  4298. else if (opcode == WASM_OP_F32_STORE) {
  4299. skip_label();
  4300. emit_label(WASM_OP_I32_STORE);
  4301. }
  4302. else if (opcode == WASM_OP_F64_STORE) {
  4303. skip_label();
  4304. emit_label(WASM_OP_I64_STORE);
  4305. }
  4306. #endif
  4307. CHECK_MEMORY();
  4308. read_leb_uint32(p, p_end, align); /* align */
  4309. read_leb_uint32(p, p_end, mem_offset); /* offset */
  4310. #if WASM_ENABLE_FAST_INTERP != 0
  4311. emit_uint32(loader_ctx, mem_offset);
  4312. #endif
  4313. switch (opcode)
  4314. {
  4315. /* load */
  4316. case WASM_OP_I32_LOAD:
  4317. case WASM_OP_I32_LOAD8_S:
  4318. case WASM_OP_I32_LOAD8_U:
  4319. case WASM_OP_I32_LOAD16_S:
  4320. case WASM_OP_I32_LOAD16_U:
  4321. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4322. break;
  4323. case WASM_OP_I64_LOAD:
  4324. case WASM_OP_I64_LOAD8_S:
  4325. case WASM_OP_I64_LOAD8_U:
  4326. case WASM_OP_I64_LOAD16_S:
  4327. case WASM_OP_I64_LOAD16_U:
  4328. case WASM_OP_I64_LOAD32_S:
  4329. case WASM_OP_I64_LOAD32_U:
  4330. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  4331. break;
  4332. case WASM_OP_F32_LOAD:
  4333. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  4334. break;
  4335. case WASM_OP_F64_LOAD:
  4336. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  4337. break;
  4338. /* store */
  4339. case WASM_OP_I32_STORE:
  4340. case WASM_OP_I32_STORE8:
  4341. case WASM_OP_I32_STORE16:
  4342. POP_I32();
  4343. POP_I32();
  4344. break;
  4345. case WASM_OP_I64_STORE:
  4346. case WASM_OP_I64_STORE8:
  4347. case WASM_OP_I64_STORE16:
  4348. case WASM_OP_I64_STORE32:
  4349. POP_I64();
  4350. POP_I32();
  4351. break;
  4352. case WASM_OP_F32_STORE:
  4353. POP_F32();
  4354. POP_I32();
  4355. break;
  4356. case WASM_OP_F64_STORE:
  4357. POP_F64();
  4358. POP_I32();
  4359. break;
  4360. default:
  4361. break;
  4362. }
  4363. break;
  4364. }
  4365. case WASM_OP_MEMORY_SIZE:
  4366. CHECK_MEMORY();
  4367. /* reserved byte 0x00 */
  4368. bh_assert(*p == 0x00);
  4369. p++;
  4370. PUSH_I32();
  4371. module->possible_memory_grow = true;
  4372. break;
  4373. case WASM_OP_MEMORY_GROW:
  4374. CHECK_MEMORY();
  4375. /* reserved byte 0x00 */
  4376. bh_assert(*p == 0x00);
  4377. p++;
  4378. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4379. func->has_op_memory_grow = true;
  4380. module->possible_memory_grow = true;
  4381. break;
  4382. case WASM_OP_I32_CONST:
  4383. read_leb_int32(p, p_end, i32_const);
  4384. #if WASM_ENABLE_FAST_INTERP != 0
  4385. skip_label();
  4386. disable_emit = true;
  4387. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  4388. #else
  4389. (void)i32_const;
  4390. #endif
  4391. PUSH_I32();
  4392. break;
  4393. case WASM_OP_I64_CONST:
  4394. read_leb_int64(p, p_end, i64);
  4395. #if WASM_ENABLE_FAST_INTERP != 0
  4396. skip_label();
  4397. disable_emit = true;
  4398. GET_CONST_OFFSET(VALUE_TYPE_I64, i64);
  4399. #endif
  4400. PUSH_I64();
  4401. break;
  4402. case WASM_OP_F32_CONST:
  4403. p += sizeof(float32);
  4404. #if WASM_ENABLE_FAST_INTERP != 0
  4405. skip_label();
  4406. disable_emit = true;
  4407. bh_memcpy_s((uint8*)&f32, sizeof(float32), p_org, sizeof(float32));
  4408. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32);
  4409. #endif
  4410. PUSH_F32();
  4411. break;
  4412. case WASM_OP_F64_CONST:
  4413. p += sizeof(float64);
  4414. #if WASM_ENABLE_FAST_INTERP != 0
  4415. skip_label();
  4416. disable_emit = true;
  4417. /* Some MCU may require 8-byte align */
  4418. bh_memcpy_s((uint8*)&f64, sizeof(float64), p_org, sizeof(float64));
  4419. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64);
  4420. #endif
  4421. PUSH_F64();
  4422. break;
  4423. case WASM_OP_I32_EQZ:
  4424. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4425. break;
  4426. case WASM_OP_I32_EQ:
  4427. case WASM_OP_I32_NE:
  4428. case WASM_OP_I32_LT_S:
  4429. case WASM_OP_I32_LT_U:
  4430. case WASM_OP_I32_GT_S:
  4431. case WASM_OP_I32_GT_U:
  4432. case WASM_OP_I32_LE_S:
  4433. case WASM_OP_I32_LE_U:
  4434. case WASM_OP_I32_GE_S:
  4435. case WASM_OP_I32_GE_U:
  4436. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4437. break;
  4438. case WASM_OP_I64_EQZ:
  4439. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  4440. break;
  4441. case WASM_OP_I64_EQ:
  4442. case WASM_OP_I64_NE:
  4443. case WASM_OP_I64_LT_S:
  4444. case WASM_OP_I64_LT_U:
  4445. case WASM_OP_I64_GT_S:
  4446. case WASM_OP_I64_GT_U:
  4447. case WASM_OP_I64_LE_S:
  4448. case WASM_OP_I64_LE_U:
  4449. case WASM_OP_I64_GE_S:
  4450. case WASM_OP_I64_GE_U:
  4451. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  4452. break;
  4453. case WASM_OP_F32_EQ:
  4454. case WASM_OP_F32_NE:
  4455. case WASM_OP_F32_LT:
  4456. case WASM_OP_F32_GT:
  4457. case WASM_OP_F32_LE:
  4458. case WASM_OP_F32_GE:
  4459. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  4460. break;
  4461. case WASM_OP_F64_EQ:
  4462. case WASM_OP_F64_NE:
  4463. case WASM_OP_F64_LT:
  4464. case WASM_OP_F64_GT:
  4465. case WASM_OP_F64_LE:
  4466. case WASM_OP_F64_GE:
  4467. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  4468. break;
  4469. case WASM_OP_I32_CLZ:
  4470. case WASM_OP_I32_CTZ:
  4471. case WASM_OP_I32_POPCNT:
  4472. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4473. break;
  4474. case WASM_OP_I32_ADD:
  4475. case WASM_OP_I32_SUB:
  4476. case WASM_OP_I32_MUL:
  4477. case WASM_OP_I32_DIV_S:
  4478. case WASM_OP_I32_DIV_U:
  4479. case WASM_OP_I32_REM_S:
  4480. case WASM_OP_I32_REM_U:
  4481. case WASM_OP_I32_AND:
  4482. case WASM_OP_I32_OR:
  4483. case WASM_OP_I32_XOR:
  4484. case WASM_OP_I32_SHL:
  4485. case WASM_OP_I32_SHR_S:
  4486. case WASM_OP_I32_SHR_U:
  4487. case WASM_OP_I32_ROTL:
  4488. case WASM_OP_I32_ROTR:
  4489. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4490. break;
  4491. case WASM_OP_I64_CLZ:
  4492. case WASM_OP_I64_CTZ:
  4493. case WASM_OP_I64_POPCNT:
  4494. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  4495. break;
  4496. case WASM_OP_I64_ADD:
  4497. case WASM_OP_I64_SUB:
  4498. case WASM_OP_I64_MUL:
  4499. case WASM_OP_I64_DIV_S:
  4500. case WASM_OP_I64_DIV_U:
  4501. case WASM_OP_I64_REM_S:
  4502. case WASM_OP_I64_REM_U:
  4503. case WASM_OP_I64_AND:
  4504. case WASM_OP_I64_OR:
  4505. case WASM_OP_I64_XOR:
  4506. case WASM_OP_I64_SHL:
  4507. case WASM_OP_I64_SHR_S:
  4508. case WASM_OP_I64_SHR_U:
  4509. case WASM_OP_I64_ROTL:
  4510. case WASM_OP_I64_ROTR:
  4511. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  4512. break;
  4513. case WASM_OP_F32_ABS:
  4514. case WASM_OP_F32_NEG:
  4515. case WASM_OP_F32_CEIL:
  4516. case WASM_OP_F32_FLOOR:
  4517. case WASM_OP_F32_TRUNC:
  4518. case WASM_OP_F32_NEAREST:
  4519. case WASM_OP_F32_SQRT:
  4520. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  4521. break;
  4522. case WASM_OP_F32_ADD:
  4523. case WASM_OP_F32_SUB:
  4524. case WASM_OP_F32_MUL:
  4525. case WASM_OP_F32_DIV:
  4526. case WASM_OP_F32_MIN:
  4527. case WASM_OP_F32_MAX:
  4528. case WASM_OP_F32_COPYSIGN:
  4529. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  4530. break;
  4531. case WASM_OP_F64_ABS:
  4532. case WASM_OP_F64_NEG:
  4533. case WASM_OP_F64_CEIL:
  4534. case WASM_OP_F64_FLOOR:
  4535. case WASM_OP_F64_TRUNC:
  4536. case WASM_OP_F64_NEAREST:
  4537. case WASM_OP_F64_SQRT:
  4538. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  4539. break;
  4540. case WASM_OP_F64_ADD:
  4541. case WASM_OP_F64_SUB:
  4542. case WASM_OP_F64_MUL:
  4543. case WASM_OP_F64_DIV:
  4544. case WASM_OP_F64_MIN:
  4545. case WASM_OP_F64_MAX:
  4546. case WASM_OP_F64_COPYSIGN:
  4547. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  4548. break;
  4549. case WASM_OP_I32_WRAP_I64:
  4550. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  4551. break;
  4552. case WASM_OP_I32_TRUNC_S_F32:
  4553. case WASM_OP_I32_TRUNC_U_F32:
  4554. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  4555. break;
  4556. case WASM_OP_I32_TRUNC_S_F64:
  4557. case WASM_OP_I32_TRUNC_U_F64:
  4558. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  4559. break;
  4560. case WASM_OP_I64_EXTEND_S_I32:
  4561. case WASM_OP_I64_EXTEND_U_I32:
  4562. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  4563. break;
  4564. case WASM_OP_I64_TRUNC_S_F32:
  4565. case WASM_OP_I64_TRUNC_U_F32:
  4566. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  4567. break;
  4568. case WASM_OP_I64_TRUNC_S_F64:
  4569. case WASM_OP_I64_TRUNC_U_F64:
  4570. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  4571. break;
  4572. case WASM_OP_F32_CONVERT_S_I32:
  4573. case WASM_OP_F32_CONVERT_U_I32:
  4574. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  4575. break;
  4576. case WASM_OP_F32_CONVERT_S_I64:
  4577. case WASM_OP_F32_CONVERT_U_I64:
  4578. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  4579. break;
  4580. case WASM_OP_F32_DEMOTE_F64:
  4581. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  4582. break;
  4583. case WASM_OP_F64_CONVERT_S_I32:
  4584. case WASM_OP_F64_CONVERT_U_I32:
  4585. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  4586. break;
  4587. case WASM_OP_F64_CONVERT_S_I64:
  4588. case WASM_OP_F64_CONVERT_U_I64:
  4589. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  4590. break;
  4591. case WASM_OP_F64_PROMOTE_F32:
  4592. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  4593. break;
  4594. case WASM_OP_I32_REINTERPRET_F32:
  4595. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  4596. break;
  4597. case WASM_OP_I64_REINTERPRET_F64:
  4598. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  4599. break;
  4600. case WASM_OP_F32_REINTERPRET_I32:
  4601. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  4602. break;
  4603. case WASM_OP_F64_REINTERPRET_I64:
  4604. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  4605. break;
  4606. case WASM_OP_I32_EXTEND8_S:
  4607. case WASM_OP_I32_EXTEND16_S:
  4608. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4609. break;
  4610. case WASM_OP_I64_EXTEND8_S:
  4611. case WASM_OP_I64_EXTEND16_S:
  4612. case WASM_OP_I64_EXTEND32_S:
  4613. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  4614. break;
  4615. case WASM_OP_MISC_PREFIX:
  4616. {
  4617. opcode = read_uint8(p);
  4618. #if WASM_ENABLE_FAST_INTERP != 0
  4619. emit_byte(loader_ctx, opcode);
  4620. #endif
  4621. switch (opcode)
  4622. {
  4623. case WASM_OP_I32_TRUNC_SAT_S_F32:
  4624. case WASM_OP_I32_TRUNC_SAT_U_F32:
  4625. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  4626. break;
  4627. case WASM_OP_I32_TRUNC_SAT_S_F64:
  4628. case WASM_OP_I32_TRUNC_SAT_U_F64:
  4629. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  4630. break;
  4631. case WASM_OP_I64_TRUNC_SAT_S_F32:
  4632. case WASM_OP_I64_TRUNC_SAT_U_F32:
  4633. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  4634. break;
  4635. case WASM_OP_I64_TRUNC_SAT_S_F64:
  4636. case WASM_OP_I64_TRUNC_SAT_U_F64:
  4637. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  4638. break;
  4639. #if WASM_ENABLE_BULK_MEMORY != 0
  4640. case WASM_OP_MEMORY_INIT:
  4641. read_leb_uint32(p, p_end, segment_index);
  4642. #if WASM_ENABLE_FAST_INTERP != 0
  4643. emit_uint32(loader_ctx, segment_index);
  4644. #endif
  4645. bh_assert(module->import_memory_count
  4646. + module->memory_count > 0);
  4647. bh_assert(*p == 0x00);
  4648. p++;
  4649. bh_assert(segment_index < module->data_seg_count);
  4650. bh_assert(module->data_seg_count1 > 0);
  4651. POP_I32();
  4652. POP_I32();
  4653. POP_I32();
  4654. break;
  4655. case WASM_OP_DATA_DROP:
  4656. read_leb_uint32(p, p_end, segment_index);
  4657. #if WASM_ENABLE_FAST_INTERP != 0
  4658. emit_uint32(loader_ctx, segment_index);
  4659. #endif
  4660. bh_assert(segment_index < module->data_seg_count);
  4661. bh_assert(module->data_seg_count1 > 0);
  4662. break;
  4663. case WASM_OP_MEMORY_COPY:
  4664. /* both src and dst memory index should be 0 */
  4665. bh_assert(*(int16*)p != 0x0000);
  4666. p += 2;
  4667. bh_assert(module->import_memory_count
  4668. + module->memory_count > 0);
  4669. POP_I32();
  4670. POP_I32();
  4671. POP_I32();
  4672. break;
  4673. case WASM_OP_MEMORY_FILL:
  4674. bh_assert(*p == 0);
  4675. p++;
  4676. bh_assert(module->import_memory_count
  4677. + module->memory_count > 0);
  4678. POP_I32();
  4679. POP_I32();
  4680. POP_I32();
  4681. break;
  4682. /* TODO: to support bulk table operation */
  4683. #endif /* WASM_ENABLE_BULK_MEMORY */
  4684. default:
  4685. bh_assert(0);
  4686. break;
  4687. }
  4688. break;
  4689. }
  4690. #if WASM_ENABLE_SHARED_MEMORY != 0
  4691. case WASM_OP_ATOMIC_PREFIX:
  4692. {
  4693. opcode = read_uint8(p);
  4694. #if WASM_ENABLE_FAST_INTERP != 0
  4695. emit_byte(loader_ctx, opcode);
  4696. #endif
  4697. if (opcode != WASM_OP_ATOMIC_FENCE) {
  4698. CHECK_MEMORY();
  4699. read_leb_uint32(p, p_end, align); /* align */
  4700. read_leb_uint32(p, p_end, mem_offset); /* offset */
  4701. #if WASM_ENABLE_FAST_INTERP != 0
  4702. emit_uint32(loader_ctx, mem_offset);
  4703. #endif
  4704. }
  4705. switch (opcode) {
  4706. case WASM_OP_ATOMIC_NOTIFY:
  4707. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4708. break;
  4709. case WASM_OP_ATOMIC_WAIT32:
  4710. POP_I64();
  4711. POP_I32();
  4712. POP_I32();
  4713. PUSH_I32();
  4714. break;
  4715. case WASM_OP_ATOMIC_WAIT64:
  4716. POP_I64();
  4717. POP_I64();
  4718. POP_I32();
  4719. PUSH_I32();
  4720. break;
  4721. case WASM_OP_ATOMIC_FENCE:
  4722. /* reserved byte 0x00 */
  4723. bh_assert(*p == 0x00);
  4724. p++;
  4725. break;
  4726. case WASM_OP_ATOMIC_I32_LOAD:
  4727. case WASM_OP_ATOMIC_I32_LOAD8_U:
  4728. case WASM_OP_ATOMIC_I32_LOAD16_U:
  4729. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4730. break;
  4731. case WASM_OP_ATOMIC_I32_STORE:
  4732. case WASM_OP_ATOMIC_I32_STORE8:
  4733. case WASM_OP_ATOMIC_I32_STORE16:
  4734. POP_I32();
  4735. POP_I32();
  4736. break;
  4737. case WASM_OP_ATOMIC_I64_LOAD:
  4738. case WASM_OP_ATOMIC_I64_LOAD8_U:
  4739. case WASM_OP_ATOMIC_I64_LOAD16_U:
  4740. case WASM_OP_ATOMIC_I64_LOAD32_U:
  4741. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  4742. break;
  4743. case WASM_OP_ATOMIC_I64_STORE:
  4744. case WASM_OP_ATOMIC_I64_STORE8:
  4745. case WASM_OP_ATOMIC_I64_STORE16:
  4746. case WASM_OP_ATOMIC_I64_STORE32:
  4747. POP_I64();
  4748. POP_I32();
  4749. break;
  4750. case WASM_OP_ATOMIC_RMW_I32_ADD:
  4751. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  4752. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  4753. case WASM_OP_ATOMIC_RMW_I32_SUB:
  4754. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  4755. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  4756. case WASM_OP_ATOMIC_RMW_I32_AND:
  4757. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  4758. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  4759. case WASM_OP_ATOMIC_RMW_I32_OR:
  4760. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  4761. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  4762. case WASM_OP_ATOMIC_RMW_I32_XOR:
  4763. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  4764. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  4765. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  4766. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  4767. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  4768. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  4769. break;
  4770. case WASM_OP_ATOMIC_RMW_I64_ADD:
  4771. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  4772. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  4773. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  4774. case WASM_OP_ATOMIC_RMW_I64_SUB:
  4775. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  4776. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  4777. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  4778. case WASM_OP_ATOMIC_RMW_I64_AND:
  4779. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  4780. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  4781. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  4782. case WASM_OP_ATOMIC_RMW_I64_OR:
  4783. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  4784. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  4785. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  4786. case WASM_OP_ATOMIC_RMW_I64_XOR:
  4787. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  4788. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  4789. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  4790. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  4791. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  4792. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  4793. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  4794. POP_I64();
  4795. POP_I32();
  4796. PUSH_I64();
  4797. break;
  4798. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  4799. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  4800. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  4801. POP_I32();
  4802. POP_I32();
  4803. POP_I32();
  4804. PUSH_I32();
  4805. break;
  4806. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  4807. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  4808. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  4809. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  4810. POP_I64();
  4811. POP_I64();
  4812. POP_I32();
  4813. PUSH_I64();
  4814. break;
  4815. default:
  4816. bh_assert(0);
  4817. break;
  4818. }
  4819. break;
  4820. }
  4821. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  4822. default:
  4823. bh_assert(0);
  4824. break;
  4825. }
  4826. #if WASM_ENABLE_FAST_INTERP != 0
  4827. last_op = opcode;
  4828. #endif
  4829. }
  4830. if (loader_ctx->csp_num > 0) {
  4831. set_error_buf(error_buf, error_buf_size,
  4832. "WASM module load failed: "
  4833. "function body must end with END opcode.");
  4834. goto fail;
  4835. }
  4836. #if WASM_ENABLE_FAST_INTERP != 0
  4837. if (loader_ctx->p_code_compiled == NULL)
  4838. goto re_scan;
  4839. func->const_cell_num = loader_ctx->const_cell_num;
  4840. if (!(func->consts = func_const =
  4841. loader_malloc(func->const_cell_num * 4,
  4842. error_buf, error_buf_size))) {
  4843. goto fail;
  4844. }
  4845. func_const_end = func->consts + func->const_cell_num * 4;
  4846. // reverse the const buf
  4847. for (int i = loader_ctx->num_const - 1; i >= 0; i--) {
  4848. Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const));
  4849. if (c->value_type == VALUE_TYPE_F64
  4850. || c->value_type == VALUE_TYPE_I64) {
  4851. bh_memcpy_s(func_const, func_const_end - func_const,
  4852. &(c->value.f64), sizeof(int64));
  4853. func_const += sizeof(int64);
  4854. } else {
  4855. bh_memcpy_s(func_const, func_const_end - func_const,
  4856. &(c->value.f32), sizeof(int32));
  4857. func_const += sizeof(int32);
  4858. }
  4859. }
  4860. func->max_stack_cell_num = loader_ctx->preserved_local_offset -
  4861. loader_ctx->start_dynamic_offset + 1;
  4862. #else
  4863. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  4864. #endif
  4865. func->max_block_num = loader_ctx->max_csp_num;
  4866. return_value = true;
  4867. fail:
  4868. wasm_loader_ctx_destroy(loader_ctx);
  4869. (void)u8;
  4870. (void)u32;
  4871. (void)i32;
  4872. (void)i64;
  4873. (void)global_count;
  4874. (void)local_count;
  4875. (void)local_offset;
  4876. (void)p_org;
  4877. (void)mem_offset;
  4878. (void)align;
  4879. return return_value;
  4880. }