wasm_loader.c 166 KB

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