wasm_loader.c 169 KB

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