wasm_loader.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562
  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_memory.h"
  8. #include "bh_log.h"
  9. #include "wasm.h"
  10. #include "wasm_opcode.h"
  11. #include "wasm_runtime.h"
  12. #include "../common/wasm_native.h"
  13. /* Read a value of given type from the address pointed to by the given
  14. pointer and increase the pointer to the position just after the
  15. value being read. */
  16. #define TEMPLATE_READ_VALUE(Type, p) \
  17. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  18. static void
  19. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  20. {
  21. if (error_buf != NULL)
  22. snprintf(error_buf, error_buf_size, "%s", string);
  23. }
  24. #define CHECK_BUF(buf, buf_end, length) do { \
  25. if (buf + length > buf_end) { \
  26. set_error_buf(error_buf, error_buf_size, \
  27. "WASM module load failed: " \
  28. "unexpected end of section or function"); \
  29. return false; \
  30. } \
  31. } while (0)
  32. #define CHECK_BUF1(buf, buf_end, length) do { \
  33. if (buf + length > buf_end) { \
  34. set_error_buf(error_buf, error_buf_size, \
  35. "WASM module load failed: unexpected end");\
  36. return false; \
  37. } \
  38. } while (0)
  39. static bool
  40. skip_leb(const uint8 *buf, const uint8 *buf_end,
  41. uint32 *p_offset, uint32 maxbits,
  42. char* error_buf, uint32 error_buf_size)
  43. {
  44. uint32 bcnt = 0;
  45. uint64 byte;
  46. while (true) {
  47. if (bcnt + 1 > (maxbits + 6) / 7) {
  48. set_error_buf(error_buf, error_buf_size,
  49. "WASM module load failed: "
  50. "integer representation too long");
  51. return false;
  52. }
  53. CHECK_BUF(buf, buf_end, *p_offset + 1);
  54. byte = buf[*p_offset];
  55. *p_offset += 1;
  56. bcnt += 1;
  57. if ((byte & 0x80) == 0) {
  58. break;
  59. }
  60. }
  61. return true;
  62. }
  63. #define skip_leb_int64(p, p_end) do { \
  64. uint32 off = 0; \
  65. if (!skip_leb(p, p_end, &off, 64, \
  66. error_buf, error_buf_size)) \
  67. return false; \
  68. p += off; \
  69. } while (0)
  70. #define skip_leb_uint32(p, p_end) do { \
  71. uint32 off = 0; \
  72. if (!skip_leb(p, p_end, &off, 32, \
  73. error_buf, error_buf_size)) \
  74. return false; \
  75. p += off; \
  76. } while (0)
  77. #define skip_leb_int32(p, p_end) do { \
  78. uint32 off = 0; \
  79. if (!skip_leb(p, p_end, &off, 32, \
  80. error_buf, error_buf_size)) \
  81. return false; \
  82. p += off; \
  83. } while (0)
  84. static bool
  85. read_leb(const uint8 *buf, const uint8 *buf_end,
  86. uint32 *p_offset, uint32 maxbits,
  87. bool sign, uint64 *p_result,
  88. char* error_buf, uint32 error_buf_size)
  89. {
  90. uint64 result = 0;
  91. uint32 shift = 0;
  92. uint32 bcnt = 0;
  93. uint64 byte;
  94. while (true) {
  95. if (bcnt + 1 > (maxbits + 6) / 7) {
  96. set_error_buf(error_buf, error_buf_size,
  97. "WASM module load failed: "
  98. "integer representation too long");
  99. return false;
  100. }
  101. CHECK_BUF(buf, buf_end, *p_offset + 1);
  102. byte = buf[*p_offset];
  103. *p_offset += 1;
  104. result |= ((byte & 0x7f) << shift);
  105. shift += 7;
  106. bcnt += 1;
  107. if ((byte & 0x80) == 0) {
  108. break;
  109. }
  110. }
  111. if (!sign && maxbits == 32 && shift >= maxbits) {
  112. /* The top bits set represent values > 32 bits */
  113. if (((uint8)byte) & 0xf0)
  114. goto fail_integer_too_large;
  115. }
  116. else if (sign && maxbits == 32) {
  117. if (shift < maxbits) {
  118. /* Sign extend */
  119. result = (((int32)result) << (maxbits - shift))
  120. >> (maxbits - shift);
  121. }
  122. else {
  123. /* The top bits should be a sign-extension of the sign bit */
  124. bool sign_bit_set = ((uint8)byte) & 0x8;
  125. int top_bits = ((uint8)byte) & 0xf0;
  126. if ((sign_bit_set && top_bits != 0x70)
  127. || (!sign_bit_set && top_bits != 0))
  128. goto fail_integer_too_large;
  129. }
  130. }
  131. else if (sign && maxbits == 64) {
  132. if (shift < maxbits) {
  133. /* Sign extend */
  134. result = (((int64)result) << (maxbits - shift))
  135. >> (maxbits - shift);
  136. }
  137. else {
  138. /* The top bits should be a sign-extension of the sign bit */
  139. bool sign_bit_set = ((uint8)byte) & 0x1;
  140. int top_bits = ((uint8)byte) & 0xfe;
  141. if ((sign_bit_set && top_bits != 0x7e)
  142. || (!sign_bit_set && top_bits != 0))
  143. goto fail_integer_too_large;
  144. }
  145. }
  146. *p_result = result;
  147. return true;
  148. fail_integer_too_large:
  149. set_error_buf(error_buf, error_buf_size,
  150. "WASM module load failed: integer too large");
  151. return false;
  152. }
  153. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  154. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  155. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  156. #define read_leb_int64(p, p_end, res) do { \
  157. if (p < p_end) { \
  158. uint8 _val = *p; \
  159. if (!(_val & 0x80)) { \
  160. res = (int64)_val; \
  161. if (_val & 0x40) \
  162. /* sign extend */ \
  163. res |= 0xFFFFFFFFFFFFFF80LL; \
  164. p++; \
  165. break; \
  166. } \
  167. } \
  168. uint32 off = 0; \
  169. uint64 res64; \
  170. if (!read_leb(p, p_end, &off, 64, true, &res64, \
  171. error_buf, error_buf_size)) \
  172. return false; \
  173. p += off; \
  174. res = (int64)res64; \
  175. } while (0)
  176. #define read_leb_uint32(p, p_end, res) do { \
  177. if (p < p_end) { \
  178. uint8 _val = *p; \
  179. if (!(_val & 0x80)) { \
  180. res = _val; \
  181. p++; \
  182. break; \
  183. } \
  184. } \
  185. uint32 off = 0; \
  186. uint64 res64; \
  187. if (!read_leb(p, p_end, &off, 32, false, &res64, \
  188. error_buf, error_buf_size)) \
  189. return false; \
  190. p += off; \
  191. res = (uint32)res64; \
  192. } while (0)
  193. #define read_leb_int32(p, p_end, res) do { \
  194. if (p < p_end) { \
  195. uint8 _val = *p; \
  196. if (!(_val & 0x80)) { \
  197. res = (int32)_val; \
  198. if (_val & 0x40) \
  199. /* sign extend */ \
  200. res |= 0xFFFFFF80; \
  201. p++; \
  202. break; \
  203. } \
  204. } \
  205. uint32 off = 0; \
  206. uint64 res64; \
  207. if (!read_leb(p, p_end, &off, 32, true, &res64, \
  208. error_buf, error_buf_size)) \
  209. return false; \
  210. p += off; \
  211. res = (int32)res64; \
  212. } while (0)
  213. static bool
  214. check_utf8_str(const uint8* str, uint32 len)
  215. {
  216. const uint8 *p = str, *p_end = str + len, *p_end1;
  217. uint8 chr, n_bytes;
  218. while (p < p_end) {
  219. chr = *p++;
  220. if (chr >= 0x80) {
  221. /* Calculate the byte count: the first byte must be
  222. 110XXXXX, 1110XXXX, 11110XXX, 111110XX, or 1111110X,
  223. the count of leading '1' denotes the total byte count */
  224. n_bytes = 0;
  225. while ((chr & 0x80) != 0) {
  226. chr = (uint8)(chr << 1);
  227. n_bytes++;
  228. }
  229. /* Check byte count */
  230. if (n_bytes < 2 || n_bytes > 6
  231. || p + n_bytes - 1 > p_end)
  232. return false;
  233. /* Check the following bytes, which must be 10XXXXXX */
  234. p_end1 = p + n_bytes - 1;
  235. while (p < p_end1) {
  236. if (!(*p & 0x80) || (*p | 0x40))
  237. return false;
  238. p++;
  239. }
  240. }
  241. }
  242. return true;
  243. }
  244. static char*
  245. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  246. char* error_buf, uint32 error_buf_size)
  247. {
  248. StringNode *node, *node_next;
  249. if (!check_utf8_str(str, len)) {
  250. set_error_buf(error_buf, error_buf_size,
  251. "WASM module load failed: "
  252. "invalid UTF-8 encoding");
  253. return NULL;
  254. }
  255. /* Search const str list */
  256. node = module->const_str_list;
  257. while (node) {
  258. node_next = node->next;
  259. if (strlen(node->str) == len
  260. && !memcmp(node->str, str, len))
  261. break;
  262. node = node_next;
  263. }
  264. if (node)
  265. return node->str;
  266. if (!(node = wasm_malloc(sizeof(StringNode) + len + 1))) {
  267. set_error_buf(error_buf, error_buf_size,
  268. "WASM module load failed: "
  269. "allocate memory failed.");
  270. return NULL;
  271. }
  272. node->str = ((char*)node) + sizeof(StringNode);
  273. bh_memcpy_s(node->str, len + 1, str, len);
  274. node->str[len] = '\0';
  275. if (!module->const_str_list) {
  276. /* set as head */
  277. module->const_str_list = node;
  278. node->next = NULL;
  279. }
  280. else {
  281. /* insert it */
  282. node->next = module->const_str_list;
  283. module->const_str_list = node;
  284. }
  285. return node->str;
  286. }
  287. static bool
  288. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  289. InitializerExpression *init_expr,
  290. char *error_buf, uint32 error_buf_size)
  291. {
  292. const uint8 *p = *p_buf, *p_end = buf_end;
  293. uint8 flag, end_byte, *p_float;
  294. uint32 i;
  295. CHECK_BUF(p, p_end, 1);
  296. init_expr->init_expr_type = read_uint8(p);
  297. flag = init_expr->init_expr_type;
  298. switch (flag) {
  299. /* i32.const */
  300. case INIT_EXPR_TYPE_I32_CONST:
  301. read_leb_int32(p, p_end, init_expr->u.i32);
  302. break;
  303. /* i64.const */
  304. case INIT_EXPR_TYPE_I64_CONST:
  305. read_leb_int64(p, p_end, init_expr->u.i64);
  306. break;
  307. /* f32.const */
  308. case INIT_EXPR_TYPE_F32_CONST:
  309. CHECK_BUF(p, p_end, 4);
  310. p_float = (uint8*)&init_expr->u.f32;
  311. for (i = 0; i < sizeof(float32); i++)
  312. *p_float++ = *p++;
  313. break;
  314. /* f64.const */
  315. case INIT_EXPR_TYPE_F64_CONST:
  316. CHECK_BUF(p, p_end, 8);
  317. p_float = (uint8*)&init_expr->u.f64;
  318. for (i = 0; i < sizeof(float64); i++)
  319. *p_float++ = *p++;
  320. break;
  321. /* get_global */
  322. case INIT_EXPR_TYPE_GET_GLOBAL:
  323. read_leb_uint32(p, p_end, init_expr->u.global_index);
  324. break;
  325. default:
  326. set_error_buf(error_buf, error_buf_size,
  327. "WASM module load failed: type mismatch");
  328. return false;
  329. }
  330. CHECK_BUF(p, p_end, 1);
  331. end_byte = read_uint8(p);
  332. if (end_byte != 0x0b) {
  333. set_error_buf(error_buf, error_buf_size,
  334. "WASM module load failed: "
  335. "unexpected end of section or function");
  336. return false;
  337. }
  338. *p_buf = p;
  339. return true;
  340. }
  341. static bool
  342. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  343. char *error_buf, uint32 error_buf_size)
  344. {
  345. const uint8 *p = buf, *p_end = buf_end, *p_org;
  346. uint32 type_count, param_count, result_count, i, j;
  347. uint64 total_size;
  348. uint8 flag;
  349. WASMType *type;
  350. read_leb_uint32(p, p_end, type_count);
  351. if (type_count) {
  352. module->type_count = type_count;
  353. total_size = sizeof(WASMType*) * (uint64)type_count;
  354. if (total_size >= UINT32_MAX
  355. || !(module->types = wasm_malloc((uint32)total_size))) {
  356. set_error_buf(error_buf, error_buf_size,
  357. "Load type section failed: allocate memory failed.");
  358. return false;
  359. }
  360. memset(module->types, 0, (uint32)total_size);
  361. for (i = 0; i < type_count; i++) {
  362. CHECK_BUF(p, p_end, 1);
  363. flag = read_uint8(p);
  364. if (flag != 0x60) {
  365. set_error_buf(error_buf, error_buf_size,
  366. "Load type section failed: invalid type flag.");
  367. return false;
  368. }
  369. read_leb_uint32(p, p_end, param_count);
  370. /* Resolve param count and result count firstly */
  371. p_org = p;
  372. CHECK_BUF(p, p_end, param_count);
  373. p += param_count;
  374. read_leb_uint32(p, p_end, result_count);
  375. if (result_count > 1) {
  376. set_error_buf(error_buf, error_buf_size,
  377. "Load type section failed: invalid result count.");
  378. return false;
  379. }
  380. CHECK_BUF(p, p_end, result_count);
  381. p = p_org;
  382. total_size = offsetof(WASMType, types) +
  383. sizeof(uint8) * (uint64)(param_count + result_count);
  384. if (total_size >= UINT32_MAX
  385. || !(type = module->types[i] = wasm_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. static bool
  432. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  433. WASMMemoryImport *memory,
  434. char *error_buf, uint32 error_buf_size)
  435. {
  436. const uint8 *p = *p_buf, *p_end = buf_end;
  437. uint32 pool_size = bh_memory_pool_size();
  438. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  439. / DEFAULT_NUM_BYTES_PER_PAGE;
  440. read_leb_uint32(p, p_end, memory->flags);
  441. read_leb_uint32(p, p_end, memory->init_page_count);
  442. if (memory->flags & 1) {
  443. read_leb_uint32(p, p_end, memory->max_page_count);
  444. if (memory->max_page_count > max_page_count)
  445. memory->max_page_count = max_page_count;
  446. }
  447. else
  448. /* Limit the maximum memory size to max_page_count */
  449. memory->max_page_count = max_page_count;
  450. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  451. *p_buf = p;
  452. return true;
  453. }
  454. static bool
  455. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  456. char *error_buf, uint32 error_buf_size)
  457. {
  458. const uint8 *p = *p_buf, *p_end = buf_end;
  459. CHECK_BUF(p, p_end, 1);
  460. /* 0x70 */
  461. table->elem_type = read_uint8(p);
  462. bh_assert(table->elem_type == TABLE_ELEM_TYPE_ANY_FUNC);
  463. read_leb_uint32(p, p_end, table->flags);
  464. read_leb_uint32(p, p_end, table->init_size);
  465. if (table->flags & 1)
  466. read_leb_uint32(p, p_end, table->max_size);
  467. else
  468. table->max_size = 0x10000;
  469. *p_buf = p;
  470. return true;
  471. }
  472. static bool
  473. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  474. char *error_buf, uint32 error_buf_size)
  475. {
  476. const uint8 *p = *p_buf, *p_end = buf_end;
  477. uint32 pool_size = bh_memory_pool_size();
  478. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  479. / DEFAULT_NUM_BYTES_PER_PAGE;
  480. read_leb_uint32(p, p_end, memory->flags);
  481. read_leb_uint32(p, p_end, memory->init_page_count);
  482. if (memory->flags & 1) {
  483. read_leb_uint32(p, p_end, memory->max_page_count);
  484. if (memory->max_page_count > max_page_count)
  485. memory->max_page_count = max_page_count;
  486. }
  487. else
  488. /* Limit the maximum memory size to max_page_count */
  489. memory->max_page_count = max_page_count;
  490. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  491. *p_buf = p;
  492. return true;
  493. }
  494. static void*
  495. resolve_sym(const char *module_name, const char *field_name)
  496. {
  497. void *sym;
  498. #if WASM_ENABLE_LIBC_BUILTIN != 0
  499. if ((sym = wasm_native_lookup_libc_builtin_func(module_name,
  500. field_name)))
  501. return sym;
  502. #endif
  503. #if WASM_ENABLE_LIBC_WASI != 0
  504. if ((sym = wasm_native_lookup_libc_wasi_func(module_name,
  505. field_name)))
  506. return sym;
  507. #endif
  508. #if WASM_ENABLE_BASE_LIB != 0
  509. if ((sym = wasm_native_lookup_base_lib_func(module_name,
  510. field_name)))
  511. return sym;
  512. #endif
  513. if ((sym = wasm_native_lookup_extension_lib_func(module_name,
  514. field_name)))
  515. return sym;
  516. return NULL;
  517. }
  518. static bool
  519. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  520. char *error_buf, uint32 error_buf_size)
  521. {
  522. const uint8 *p = buf, *p_end = buf_end, *p_old;
  523. uint32 import_count, name_len, type_index, i, u32, flags;
  524. uint64 total_size;
  525. WASMImport *import;
  526. WASMImport *import_functions = NULL, *import_tables = NULL;
  527. WASMImport *import_memories = NULL, *import_globals = NULL;
  528. char *module_name, *field_name;
  529. uint8 mutable, u8, kind;
  530. read_leb_uint32(p, p_end, import_count);
  531. if (import_count) {
  532. module->import_count = import_count;
  533. total_size = sizeof(WASMImport) * (uint64)import_count;
  534. if (total_size >= UINT32_MAX
  535. || !(module->imports = wasm_malloc((uint32)total_size))) {
  536. set_error_buf(error_buf, error_buf_size,
  537. "Load import section failed: allocate memory failed.");
  538. return false;
  539. }
  540. memset(module->imports, 0, (uint32)total_size);
  541. p_old = p;
  542. /* Scan firstly to get import count of each type */
  543. for (i = 0; i < import_count; i++) {
  544. /* module name */
  545. read_leb_uint32(p, p_end, name_len);
  546. CHECK_BUF(p, p_end, name_len);
  547. p += name_len;
  548. /* field name */
  549. read_leb_uint32(p, p_end, name_len);
  550. CHECK_BUF(p, p_end, name_len);
  551. p += name_len;
  552. CHECK_BUF(p, p_end, 1);
  553. /* 0x00/0x01/0x02/0x03 */
  554. kind = read_uint8(p);
  555. switch (kind) {
  556. case IMPORT_KIND_FUNC: /* import function */
  557. read_leb_uint32(p, p_end, type_index);
  558. module->import_function_count++;
  559. break;
  560. case IMPORT_KIND_TABLE: /* import table */
  561. CHECK_BUF(p, p_end, 1);
  562. /* 0x70 */
  563. u8 = read_uint8(p);
  564. read_leb_uint32(p, p_end, flags);
  565. read_leb_uint32(p, p_end, u32);
  566. if (flags & 1)
  567. read_leb_uint32(p, p_end, u32);
  568. module->import_table_count++;
  569. if (module->import_table_count > 1) {
  570. set_error_buf(error_buf, error_buf_size,
  571. "Load import section failed: multiple tables");
  572. return false;
  573. }
  574. break;
  575. case IMPORT_KIND_MEMORY: /* import memory */
  576. read_leb_uint32(p, p_end, flags);
  577. read_leb_uint32(p, p_end, u32);
  578. if (flags & 1)
  579. read_leb_uint32(p, p_end, u32);
  580. module->import_memory_count++;
  581. if (module->import_memory_count > 1) {
  582. set_error_buf(error_buf, error_buf_size,
  583. "Load import section failed: multiple memories");
  584. return false;
  585. }
  586. break;
  587. case IMPORT_KIND_GLOBAL: /* import global */
  588. CHECK_BUF(p, p_end, 2);
  589. p += 2;
  590. module->import_global_count++;
  591. break;
  592. default:
  593. set_error_buf(error_buf, error_buf_size,
  594. "Load import section failed: invalid import type.");
  595. return false;
  596. }
  597. }
  598. if (module->import_function_count)
  599. import_functions = module->import_functions = module->imports;
  600. if (module->import_table_count)
  601. import_tables = module->import_tables =
  602. module->imports + module->import_function_count;
  603. if (module->import_memory_count)
  604. import_memories = module->import_memories =
  605. module->imports + module->import_function_count + module->import_table_count;
  606. if (module->import_global_count)
  607. import_globals = module->import_globals =
  608. module->imports + module->import_function_count + module->import_table_count
  609. + module->import_memory_count;
  610. p = p_old;
  611. /* insert "env" and "wasi_unstable" to const str list */
  612. if (!const_str_list_insert((uint8*)"env", 3, module, error_buf, error_buf_size)
  613. || !const_str_list_insert((uint8*)"wasi_unstable", 13, module,
  614. error_buf, error_buf_size)) {
  615. return false;
  616. }
  617. /* Scan again to read the data */
  618. for (i = 0; i < import_count; i++) {
  619. /* load module name */
  620. read_leb_uint32(p, p_end, name_len);
  621. CHECK_BUF(p, p_end, name_len);
  622. if (!(module_name = const_str_list_insert
  623. (p, name_len, module, error_buf, error_buf_size))) {
  624. return false;
  625. }
  626. p += name_len;
  627. /* load field name */
  628. read_leb_uint32(p, p_end, name_len);
  629. CHECK_BUF(p, p_end, name_len);
  630. if (!(field_name = const_str_list_insert
  631. (p, name_len, module, error_buf, error_buf_size))) {
  632. return false;
  633. }
  634. p += name_len;
  635. CHECK_BUF(p, p_end, 1);
  636. /* 0x00/0x01/0x02/0x03 */
  637. kind = read_uint8(p);
  638. switch (kind) {
  639. case IMPORT_KIND_FUNC: /* import function */
  640. bh_assert(import_functions);
  641. import = import_functions++;
  642. read_leb_uint32(p, p_end, type_index);
  643. if (type_index >= module->type_count) {
  644. set_error_buf(error_buf, error_buf_size,
  645. "Load import section failed: "
  646. "function type index out of range.");
  647. return false;
  648. }
  649. import->u.function.func_type = module->types[type_index];
  650. if (!module->possible_memory_grow
  651. && !strcmp(module_name, "env")
  652. && !(strcmp(field_name, "enlargeMemory")))
  653. module->possible_memory_grow = true;
  654. if (!(import->u.function.func_ptr_linked =
  655. resolve_sym(module_name, field_name))) {
  656. #if WASM_ENABLE_WAMR_COMPILER == 0 /* Output warning except running aot compiler */
  657. LOG_WARNING("warning: fail to link import function (%s, %s)\n",
  658. module_name, field_name);
  659. #endif
  660. }
  661. break;
  662. case IMPORT_KIND_TABLE: /* import table */
  663. bh_assert(import_tables);
  664. import = import_tables++;
  665. if (!load_table_import(&p, p_end, &import->u.table,
  666. error_buf, error_buf_size))
  667. return false;
  668. if (module->import_table_count > 1) {
  669. set_error_buf(error_buf, error_buf_size, "multiple tables");
  670. return false;
  671. }
  672. break;
  673. case IMPORT_KIND_MEMORY: /* import memory */
  674. bh_assert(import_memories);
  675. import = import_memories++;
  676. if (!load_memory_import(&p, p_end, &import->u.memory,
  677. error_buf, error_buf_size))
  678. return false;
  679. if (module->import_memory_count > 1) {
  680. set_error_buf(error_buf, error_buf_size,
  681. "Load import section failed: multiple memories");
  682. return false;
  683. }
  684. break;
  685. case IMPORT_KIND_GLOBAL: /* import global */
  686. bh_assert(import_globals);
  687. import = import_globals++;
  688. CHECK_BUF(p, p_end, 2);
  689. import->u.global.type = read_uint8(p);
  690. mutable = read_uint8(p);
  691. if (mutable >= 2) {
  692. set_error_buf(error_buf, error_buf_size,
  693. "Load import section failed: "
  694. "invalid mutability");
  695. return false;
  696. }
  697. import->u.global.is_mutable = mutable & 1 ? true : false;
  698. #if WASM_ENABLE_LIBC_BUILTIN != 0
  699. if (!(wasm_native_lookup_libc_builtin_global(
  700. module_name, field_name,
  701. &import->u.global))) {
  702. if (error_buf != NULL)
  703. snprintf(error_buf, error_buf_size,
  704. "Load import section failed: "
  705. "resolve import global (%s, %s) failed.",
  706. module_name, field_name);
  707. return false;
  708. }
  709. #endif
  710. break;
  711. default:
  712. set_error_buf(error_buf, error_buf_size,
  713. "Load import section failed: "
  714. "invalid import type.");
  715. return false;
  716. }
  717. import->kind = kind;
  718. import->u.names.module_name = module_name;
  719. import->u.names.field_name = field_name;
  720. }
  721. #if WASM_ENABLE_LIBC_WASI != 0
  722. import = module->import_functions;
  723. for (i = 0; i < module->import_function_count; i++, import++) {
  724. if (!strcmp(import->u.names.module_name, "wasi_unstable")) {
  725. module->is_wasi_module = true;
  726. break;
  727. }
  728. }
  729. #endif
  730. }
  731. if (p != p_end) {
  732. set_error_buf(error_buf, error_buf_size,
  733. "Load import section failed: section size mismatch");
  734. return false;
  735. }
  736. LOG_VERBOSE("Load import section success.\n");
  737. (void)u8;
  738. (void)u32;
  739. return true;
  740. }
  741. static bool
  742. init_function_local_offsets(WASMFunction *func,
  743. char *error_buf, uint32 error_buf_size)
  744. {
  745. WASMType *param_type = func->func_type;
  746. uint32 param_count = param_type->param_count;
  747. uint8 *param_types = param_type->types;
  748. uint32 local_count = func->local_count;
  749. uint8 *local_types = func->local_types;
  750. uint32 i, local_offset = 0;
  751. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  752. if (total_size >= UINT32_MAX
  753. || !(func->local_offsets = wasm_malloc((uint32)total_size))) {
  754. set_error_buf(error_buf, error_buf_size,
  755. "Load function section failed: allocate memory failed.");
  756. return false;
  757. }
  758. for (i = 0; i < param_count; i++) {
  759. func->local_offsets[i] = (uint16)local_offset;
  760. local_offset += wasm_value_type_cell_num(param_types[i]);
  761. }
  762. for (i = 0; i < local_count; i++) {
  763. func->local_offsets[param_count + i] = (uint16)local_offset;
  764. local_offset += wasm_value_type_cell_num(local_types[i]);
  765. }
  766. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  767. return true;
  768. }
  769. static bool
  770. load_function_section(const uint8 *buf, const uint8 *buf_end,
  771. const uint8 *buf_code, const uint8 *buf_code_end,
  772. WASMModule *module,
  773. char *error_buf, uint32 error_buf_size)
  774. {
  775. const uint8 *p = buf, *p_end = buf_end;
  776. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  777. uint32 func_count;
  778. uint64 total_size;
  779. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  780. uint32 local_count, local_set_count, sub_local_count;
  781. uint8 type;
  782. WASMFunction *func;
  783. read_leb_uint32(p, p_end, func_count);
  784. if (buf_code)
  785. read_leb_uint32(p_code, buf_code_end, code_count);
  786. if (func_count != code_count) {
  787. set_error_buf(error_buf, error_buf_size,
  788. "Load function section failed: "
  789. "function and code section have inconsistent lengths");
  790. return false;
  791. }
  792. if (func_count) {
  793. module->function_count = func_count;
  794. total_size = sizeof(WASMFunction*) * (uint64)func_count;
  795. if (total_size >= UINT32_MAX
  796. || !(module->functions = wasm_malloc((uint32)total_size))) {
  797. set_error_buf(error_buf, error_buf_size,
  798. "Load function section failed: allocate memory failed.");
  799. return false;
  800. }
  801. memset(module->functions, 0, (uint32)total_size);
  802. for (i = 0; i < func_count; i++) {
  803. /* Resolve function type */
  804. read_leb_uint32(p, p_end, type_index);
  805. if (type_index >= module->type_count) {
  806. set_error_buf(error_buf, error_buf_size,
  807. "Load function section failed: "
  808. "function type index out of range.");
  809. return false;
  810. }
  811. read_leb_uint32(p_code, buf_code_end, code_size);
  812. if (code_size == 0
  813. || p_code + code_size > buf_code_end) {
  814. set_error_buf(error_buf, error_buf_size,
  815. "Load function section failed: "
  816. "invalid function code size.");
  817. return false;
  818. }
  819. /* Resolve local set count */
  820. p_code_end = p_code + code_size;
  821. local_count = 0;
  822. read_leb_uint32(p_code, buf_code_end, local_set_count);
  823. p_code_save = p_code;
  824. /* Calculate total local count */
  825. for (j = 0; j < local_set_count; j++) {
  826. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  827. if (sub_local_count > UINT32_MAX - local_count) {
  828. set_error_buf(error_buf, error_buf_size,
  829. "Load function section failed: "
  830. "too many locals");
  831. return false;
  832. }
  833. CHECK_BUF(p_code, buf_code_end, 1);
  834. /* 0x7F/0x7E/0x7D/0x7C */
  835. type = read_uint8(p_code);
  836. local_count += sub_local_count;
  837. }
  838. /* Alloc memory, layout: function structure + local types */
  839. code_size = (uint32)(p_code_end - p_code);
  840. total_size = sizeof(WASMFunction) + (uint64)local_count;
  841. if (total_size >= UINT32_MAX
  842. || !(func = module->functions[i] = wasm_malloc((uint32)total_size))) {
  843. set_error_buf(error_buf, error_buf_size,
  844. "Load function section failed: "
  845. "allocate memory failed.");
  846. return false;
  847. }
  848. /* Set function type, local count, code size and code body */
  849. memset(func, 0, (uint32)total_size);
  850. func->func_type = module->types[type_index];
  851. func->local_count = local_count;
  852. if (local_count > 0)
  853. func->local_types = (uint8*)func + sizeof(WASMFunction);
  854. func->code_size = code_size;
  855. func->code = (uint8*)p_code;
  856. /* Load each local type */
  857. p_code = p_code_save;
  858. local_type_index = 0;
  859. for (j = 0; j < local_set_count; j++) {
  860. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  861. if (local_type_index + sub_local_count <= local_type_index
  862. || local_type_index + sub_local_count > local_count) {
  863. set_error_buf(error_buf, error_buf_size,
  864. "Load function section failed: "
  865. "invalid local count.");
  866. return false;
  867. }
  868. CHECK_BUF(p_code, buf_code_end, 1);
  869. /* 0x7F/0x7E/0x7D/0x7C */
  870. type = read_uint8(p_code);
  871. if (type < VALUE_TYPE_F64 || type > VALUE_TYPE_I32) {
  872. set_error_buf(error_buf, error_buf_size,
  873. "Load function section failed: "
  874. "invalid local type.");
  875. return false;
  876. }
  877. for (k = 0; k < sub_local_count; k++) {
  878. func->local_types[local_type_index++] = type;
  879. }
  880. }
  881. func->param_cell_num = wasm_type_param_cell_num(func->func_type);
  882. func->ret_cell_num = wasm_type_return_cell_num(func->func_type);
  883. func->local_cell_num =
  884. wasm_get_cell_num(func->local_types, func->local_count);
  885. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  886. return false;
  887. p_code = p_code_end;
  888. }
  889. }
  890. if (p != p_end) {
  891. set_error_buf(error_buf, error_buf_size,
  892. "Load function section failed: section size mismatch");
  893. return false;
  894. }
  895. LOG_VERBOSE("Load function section success.\n");
  896. return true;
  897. }
  898. static bool
  899. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  900. char *error_buf, uint32 error_buf_size)
  901. {
  902. const uint8 *p = buf, *p_end = buf_end;
  903. uint32 table_count, i;
  904. uint64 total_size;
  905. WASMTable *table;
  906. read_leb_uint32(p, p_end, table_count);
  907. bh_assert(table_count == 1);
  908. if (table_count) {
  909. if (table_count > 1) {
  910. set_error_buf(error_buf, error_buf_size,
  911. "Load table section failed: multiple memories");
  912. return false;
  913. }
  914. module->table_count = table_count;
  915. total_size = sizeof(WASMTable) * (uint64)table_count;
  916. if (total_size >= UINT32_MAX
  917. || !(module->tables = wasm_malloc((uint32)total_size))) {
  918. set_error_buf(error_buf, error_buf_size,
  919. "Load table section failed: allocate memory failed.");
  920. return false;
  921. }
  922. memset(module->tables, 0, (uint32)total_size);
  923. /* load each table */
  924. table = module->tables;
  925. for (i = 0; i < table_count; i++, table++)
  926. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  927. return false;
  928. }
  929. if (p != p_end) {
  930. set_error_buf(error_buf, error_buf_size,
  931. "Load table section failed: section size mismatch");
  932. return false;
  933. }
  934. LOG_VERBOSE("Load table section success.\n");
  935. return true;
  936. }
  937. static bool
  938. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  939. char *error_buf, uint32 error_buf_size)
  940. {
  941. const uint8 *p = buf, *p_end = buf_end;
  942. uint32 memory_count, i;
  943. uint64 total_size;
  944. WASMMemory *memory;
  945. read_leb_uint32(p, p_end, memory_count);
  946. bh_assert(memory_count == 1);
  947. if (memory_count) {
  948. if (memory_count > 1) {
  949. set_error_buf(error_buf, error_buf_size,
  950. "Load memory section failed: multiple memories");
  951. return false;
  952. }
  953. module->memory_count = memory_count;
  954. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  955. if (total_size >= UINT32_MAX
  956. || !(module->memories = wasm_malloc((uint32)total_size))) {
  957. set_error_buf(error_buf, error_buf_size,
  958. "Load memory section failed: allocate memory failed.");
  959. return false;
  960. }
  961. memset(module->memories, 0, (uint32)total_size);
  962. /* load each memory */
  963. memory = module->memories;
  964. for (i = 0; i < memory_count; i++, memory++)
  965. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  966. return false;
  967. }
  968. if (p != p_end) {
  969. set_error_buf(error_buf, error_buf_size,
  970. "Load memory section failed: section size mismatch");
  971. return false;
  972. }
  973. LOG_VERBOSE("Load memory section success.\n");
  974. return true;
  975. }
  976. static bool
  977. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  978. char *error_buf, uint32 error_buf_size)
  979. {
  980. const uint8 *p = buf, *p_end = buf_end;
  981. uint32 global_count, i;
  982. uint64 total_size;
  983. WASMGlobal *global;
  984. uint8 mutable;
  985. read_leb_uint32(p, p_end, global_count);
  986. if (global_count) {
  987. module->global_count = global_count;
  988. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  989. if (total_size >= UINT32_MAX
  990. || !(module->globals = wasm_malloc((uint32)total_size))) {
  991. set_error_buf(error_buf, error_buf_size,
  992. "Load global section failed: "
  993. "allocate memory failed.");
  994. return false;
  995. }
  996. memset(module->globals, 0, (uint32)total_size);
  997. global = module->globals;
  998. for(i = 0; i < global_count; i++, global++) {
  999. CHECK_BUF(p, p_end, 2);
  1000. global->type = read_uint8(p);
  1001. mutable = read_uint8(p);
  1002. if (mutable >= 2) {
  1003. set_error_buf(error_buf, error_buf_size,
  1004. "Load import section failed: "
  1005. "invalid mutability");
  1006. return false;
  1007. }
  1008. global->is_mutable = mutable ? true : false;
  1009. /* initialize expression */
  1010. if (!load_init_expr(&p, p_end, &(global->init_expr), error_buf, error_buf_size))
  1011. return false;
  1012. }
  1013. }
  1014. if (p != p_end) {
  1015. set_error_buf(error_buf, error_buf_size,
  1016. "Load global section failed: section size mismatch");
  1017. return false;
  1018. }
  1019. LOG_VERBOSE("Load global section success.\n");
  1020. return true;
  1021. }
  1022. static bool
  1023. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1024. char *error_buf, uint32 error_buf_size)
  1025. {
  1026. const uint8 *p = buf, *p_end = buf_end;
  1027. uint32 export_count, i, index;
  1028. uint64 total_size;
  1029. uint32 str_len;
  1030. WASMExport *export;
  1031. read_leb_uint32(p, p_end, export_count);
  1032. if (export_count) {
  1033. module->export_count = export_count;
  1034. total_size = sizeof(WASMExport) * (uint64)export_count;
  1035. if (total_size >= UINT32_MAX
  1036. || !(module->exports = wasm_malloc((uint32)total_size))) {
  1037. set_error_buf(error_buf, error_buf_size,
  1038. "Load export section failed: "
  1039. "allocate memory failed.");
  1040. return false;
  1041. }
  1042. memset(module->exports, 0, (uint32)total_size);
  1043. export = module->exports;
  1044. for (i = 0; i < export_count; i++, export++) {
  1045. read_leb_uint32(p, p_end, str_len);
  1046. CHECK_BUF(p, p_end, str_len);
  1047. if (!(export->name = const_str_list_insert(p, str_len, module,
  1048. error_buf, error_buf_size))) {
  1049. return false;
  1050. }
  1051. p += str_len;
  1052. CHECK_BUF(p, p_end, 1);
  1053. export->kind = read_uint8(p);
  1054. read_leb_uint32(p, p_end, index);
  1055. export->index = index;
  1056. switch(export->kind) {
  1057. /*function index*/
  1058. case EXPORT_KIND_FUNC:
  1059. if (index >= module->function_count + module->import_function_count) {
  1060. set_error_buf(error_buf, error_buf_size,
  1061. "Load export section failed: "
  1062. "function index out of range.");
  1063. return false;
  1064. }
  1065. break;
  1066. /*table index*/
  1067. case EXPORT_KIND_TABLE:
  1068. if (index >= module->table_count + module->import_table_count) {
  1069. set_error_buf(error_buf, error_buf_size,
  1070. "Load export section failed: "
  1071. "table index out of range.");
  1072. return false;
  1073. }
  1074. break;
  1075. /*memory index*/
  1076. case EXPORT_KIND_MEMORY:
  1077. if (index >= module->memory_count + module->import_memory_count) {
  1078. set_error_buf(error_buf, error_buf_size,
  1079. "Load export section failed: "
  1080. "memory index out of range.");
  1081. return false;
  1082. }
  1083. break;
  1084. /*global index*/
  1085. case EXPORT_KIND_GLOBAL:
  1086. if (index >= module->global_count + module->import_global_count) {
  1087. set_error_buf(error_buf, error_buf_size,
  1088. "Load export section failed: "
  1089. "global index out of range.");
  1090. return false;
  1091. }
  1092. break;
  1093. default:
  1094. set_error_buf(error_buf, error_buf_size,
  1095. "Load export section failed: "
  1096. "invalid export kind.");
  1097. return false;
  1098. }
  1099. }
  1100. }
  1101. if (p != p_end) {
  1102. set_error_buf(error_buf, error_buf_size,
  1103. "Load export section failed: section size mismatch");
  1104. return false;
  1105. }
  1106. LOG_VERBOSE("Load export section success.\n");
  1107. return true;
  1108. }
  1109. static bool
  1110. load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1111. char *error_buf, uint32 error_buf_size)
  1112. {
  1113. const uint8 *p = buf, *p_end = buf_end;
  1114. uint32 table_segment_count, i, j, table_index, function_count, function_index;
  1115. uint64 total_size;
  1116. WASMTableSeg *table_segment;
  1117. read_leb_uint32(p, p_end, table_segment_count);
  1118. if (table_segment_count) {
  1119. module->table_seg_count = table_segment_count;
  1120. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  1121. if (total_size >= UINT32_MAX
  1122. || !(module->table_segments = wasm_malloc((uint32)total_size))) {
  1123. set_error_buf(error_buf, error_buf_size,
  1124. "Load table segment section failed: "
  1125. "allocate memory failed.");
  1126. return false;
  1127. }
  1128. memset(module->table_segments, 0, (uint32)total_size);
  1129. table_segment = module->table_segments;
  1130. for (i = 0; i < table_segment_count; i++, table_segment++) {
  1131. if (p >= p_end) {
  1132. set_error_buf(error_buf, error_buf_size,
  1133. "Load table segment section failed: "
  1134. "invalid value type");
  1135. return false;
  1136. }
  1137. read_leb_uint32(p, p_end, table_index);
  1138. table_segment->table_index = table_index;
  1139. /* initialize expression */
  1140. if (!load_init_expr(&p, p_end, &(table_segment->base_offset),
  1141. error_buf, error_buf_size))
  1142. return false;
  1143. read_leb_uint32(p, p_end, function_count);
  1144. table_segment->function_count = function_count;
  1145. total_size = sizeof(uint32) * (uint64)function_count;
  1146. if (total_size >= UINT32_MAX
  1147. || !(table_segment->func_indexes = (uint32 *)
  1148. wasm_malloc((uint32)total_size))) {
  1149. set_error_buf(error_buf, error_buf_size,
  1150. "Load table segment section failed: "
  1151. "allocate memory failed.");
  1152. return false;
  1153. }
  1154. for (j = 0; j < function_count; j++) {
  1155. read_leb_uint32(p, p_end, function_index);
  1156. table_segment->func_indexes[j] = function_index;
  1157. }
  1158. }
  1159. }
  1160. if (p != p_end) {
  1161. set_error_buf(error_buf, error_buf_size,
  1162. "Load table segment section failed: section size mismatch");
  1163. return false;
  1164. }
  1165. LOG_VERBOSE("Load table segment section success.\n");
  1166. return true;
  1167. }
  1168. static bool
  1169. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  1170. WASMModule *module,
  1171. char *error_buf, uint32 error_buf_size)
  1172. {
  1173. const uint8 *p = buf, *p_end = buf_end;
  1174. uint32 data_seg_count, i, mem_index, data_seg_len;
  1175. uint64 total_size;
  1176. WASMDataSeg *dataseg;
  1177. InitializerExpression init_expr;
  1178. read_leb_uint32(p, p_end, data_seg_count);
  1179. if (data_seg_count) {
  1180. module->data_seg_count = data_seg_count;
  1181. total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count;
  1182. if (total_size >= UINT32_MAX
  1183. || !(module->data_segments = wasm_malloc((uint32)total_size))) {
  1184. set_error_buf(error_buf, error_buf_size,
  1185. "Load data segment section failed: "
  1186. "allocate memory failed.");
  1187. return false;
  1188. }
  1189. memset(module->data_segments, 0, (uint32)total_size);
  1190. for (i = 0; i < data_seg_count; i++) {
  1191. read_leb_uint32(p, p_end, mem_index);
  1192. if (!load_init_expr(&p, p_end, &init_expr, error_buf, error_buf_size))
  1193. return false;
  1194. read_leb_uint32(p, p_end, data_seg_len);
  1195. if (!(dataseg = module->data_segments[i] =
  1196. wasm_malloc((uint32)sizeof(WASMDataSeg)))) {
  1197. set_error_buf(error_buf, error_buf_size,
  1198. "Load data segment section failed: "
  1199. "allocate memory failed.");
  1200. return false;
  1201. }
  1202. bh_memcpy_s(&dataseg->base_offset, sizeof(InitializerExpression),
  1203. &init_expr, sizeof(InitializerExpression));
  1204. dataseg->memory_index = mem_index;
  1205. dataseg->data_length = data_seg_len;
  1206. CHECK_BUF(p, p_end, data_seg_len);
  1207. dataseg->data = (uint8*)p;
  1208. p += data_seg_len;
  1209. }
  1210. }
  1211. if (p != p_end) {
  1212. set_error_buf(error_buf, error_buf_size,
  1213. "Load data segment section failed: section size mismatch");
  1214. return false;
  1215. }
  1216. LOG_VERBOSE("Load data segment section success.\n");
  1217. return true;
  1218. }
  1219. static bool
  1220. load_code_section(const uint8 *buf, const uint8 *buf_end,
  1221. const uint8 *buf_func,
  1222. const uint8 *buf_func_end,
  1223. WASMModule *module,
  1224. char *error_buf, uint32 error_buf_size)
  1225. {
  1226. const uint8 *p = buf, *p_end = buf_end;
  1227. const uint8 *p_func = buf_func;
  1228. uint32 func_count = 0, code_count;
  1229. /* code has been loaded in function section, so pass it here, just check
  1230. * whether function and code section have inconsistent lengths */
  1231. read_leb_uint32(p, p_end, code_count);
  1232. if (buf_func)
  1233. read_leb_uint32(p_func, buf_func_end, func_count);
  1234. if (func_count != code_count) {
  1235. set_error_buf(error_buf, error_buf_size,
  1236. "Load code section failed: "
  1237. "function and code section have inconsistent lengths");
  1238. return false;
  1239. }
  1240. LOG_VERBOSE("Load code segment section success.\n");
  1241. return true;
  1242. }
  1243. static bool
  1244. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1245. char *error_buf, uint32 error_buf_size)
  1246. {
  1247. const uint8 *p = buf, *p_end = buf_end;
  1248. uint32 start_function;
  1249. read_leb_uint32(p, p_end, start_function);
  1250. if (start_function) {
  1251. if (start_function >= module->function_count + module->import_function_count) {
  1252. set_error_buf(error_buf, error_buf_size,
  1253. "Load start section failed: "
  1254. "function index out of range.");
  1255. return false;
  1256. }
  1257. module->start_function = start_function;
  1258. }
  1259. if (p != p_end) {
  1260. set_error_buf(error_buf, error_buf_size,
  1261. "Load start section failed: section size mismatch");
  1262. return false;
  1263. }
  1264. LOG_VERBOSE("Load start section success.\n");
  1265. return true;
  1266. }
  1267. static bool
  1268. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1269. char *error_buf, uint32 error_buf_size)
  1270. {
  1271. const uint8 *p = buf, *p_end = buf_end;
  1272. uint32 name_len;
  1273. if (p >= p_end) {
  1274. set_error_buf(error_buf, error_buf_size,
  1275. "Load custom section failed: unexpected end");
  1276. return false;
  1277. }
  1278. read_leb_uint32(p, p_end, name_len);
  1279. if (name_len == 0
  1280. || p + name_len > p_end) {
  1281. set_error_buf(error_buf, error_buf_size,
  1282. "Load custom section failed: unexpected end");
  1283. return false;
  1284. }
  1285. if (!check_utf8_str(p, name_len)) {
  1286. set_error_buf(error_buf, error_buf_size,
  1287. "WASM module load failed: "
  1288. "invalid UTF-8 encoding");
  1289. return false;
  1290. }
  1291. LOG_VERBOSE("Load custom section success.\n");
  1292. return true;
  1293. }
  1294. static bool
  1295. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  1296. char *error_buf, uint32 error_buf_size);
  1297. static bool
  1298. load_from_sections(WASMModule *module, WASMSection *sections,
  1299. char *error_buf, uint32 error_buf_size)
  1300. {
  1301. WASMExport *export;
  1302. WASMSection *section = sections;
  1303. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  1304. *buf_func = NULL, *buf_func_end = NULL;
  1305. WASMGlobal *llvm_data_end_global = NULL, *llvm_heap_base_global = NULL;
  1306. WASMGlobal *llvm_stack_top_global = NULL, *global;
  1307. uint32 llvm_data_end = UINT32_MAX, llvm_heap_base = UINT32_MAX;
  1308. uint32 llvm_stack_top = UINT32_MAX, global_index, i;
  1309. uint32 data_end_global_index = UINT32_MAX;
  1310. uint32 heap_base_global_index = UINT32_MAX;
  1311. uint32 stack_top_global_index = UINT32_MAX;
  1312. /* Find code and function sections if have */
  1313. while (section) {
  1314. if (section->section_type == SECTION_TYPE_CODE) {
  1315. buf_code = section->section_body;
  1316. buf_code_end = buf_code + section->section_body_size;
  1317. }
  1318. else if (section->section_type == SECTION_TYPE_FUNC) {
  1319. buf_func = section->section_body;
  1320. buf_func_end = buf_func + section->section_body_size;
  1321. }
  1322. section = section->next;
  1323. }
  1324. section = sections;
  1325. while (section) {
  1326. buf = section->section_body;
  1327. buf_end = buf + section->section_body_size;
  1328. switch (section->section_type) {
  1329. case SECTION_TYPE_USER:
  1330. /* unsupported user section, ignore it. */
  1331. if (!load_user_section(buf, buf_end, module, error_buf, error_buf_size))
  1332. return false;
  1333. break;
  1334. case SECTION_TYPE_TYPE:
  1335. if (!load_type_section(buf, buf_end, module, error_buf, error_buf_size))
  1336. return false;
  1337. break;
  1338. case SECTION_TYPE_IMPORT:
  1339. if (!load_import_section(buf, buf_end, module, error_buf, error_buf_size))
  1340. return false;
  1341. break;
  1342. case SECTION_TYPE_FUNC:
  1343. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  1344. module, error_buf, error_buf_size))
  1345. return false;
  1346. break;
  1347. case SECTION_TYPE_TABLE:
  1348. if (!load_table_section(buf, buf_end, module, error_buf, error_buf_size))
  1349. return false;
  1350. break;
  1351. case SECTION_TYPE_MEMORY:
  1352. if (!load_memory_section(buf, buf_end, module, error_buf, error_buf_size))
  1353. return false;
  1354. break;
  1355. case SECTION_TYPE_GLOBAL:
  1356. if (!load_global_section(buf, buf_end, module, error_buf, error_buf_size))
  1357. return false;
  1358. break;
  1359. case SECTION_TYPE_EXPORT:
  1360. if (!load_export_section(buf, buf_end, module, error_buf, error_buf_size))
  1361. return false;
  1362. break;
  1363. case SECTION_TYPE_START:
  1364. if (!load_start_section(buf, buf_end, module, error_buf, error_buf_size))
  1365. return false;
  1366. break;
  1367. case SECTION_TYPE_ELEM:
  1368. if (!load_table_segment_section(buf, buf_end, module, error_buf, error_buf_size))
  1369. return false;
  1370. break;
  1371. case SECTION_TYPE_CODE:
  1372. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  1373. module, error_buf, error_buf_size))
  1374. return false;
  1375. break;
  1376. case SECTION_TYPE_DATA:
  1377. if (!load_data_segment_section(buf, buf_end, module, error_buf, error_buf_size))
  1378. return false;
  1379. break;
  1380. default:
  1381. set_error_buf(error_buf, error_buf_size,
  1382. "WASM module load failed: invalid section id");
  1383. return false;
  1384. }
  1385. section = section->next;
  1386. }
  1387. for (i = 0; i < module->function_count; i++) {
  1388. WASMFunction *func = module->functions[i];
  1389. if (!wasm_loader_prepare_bytecode(module, func, error_buf, error_buf_size))
  1390. return false;
  1391. }
  1392. /* Resolve llvm auxiliary data/stack/heap info and reset memory info */
  1393. if (!module->possible_memory_grow) {
  1394. export = module->exports;
  1395. for (i = 0; i < module->export_count; i++, export++) {
  1396. if (export->kind == EXPORT_KIND_GLOBAL) {
  1397. if (!strcmp(export->name, "__heap_base")) {
  1398. global_index = export->index - module->import_global_count;
  1399. global = module->globals + global_index;
  1400. if (global->type == VALUE_TYPE_I32
  1401. && !global->is_mutable
  1402. && global->init_expr.init_expr_type ==
  1403. INIT_EXPR_TYPE_I32_CONST) {
  1404. heap_base_global_index = global_index;
  1405. llvm_heap_base_global = global;
  1406. llvm_heap_base = global->init_expr.u.i32;
  1407. LOG_VERBOSE("found llvm __heap_base global, value: %d\n",
  1408. llvm_heap_base);
  1409. }
  1410. }
  1411. else if (!strcmp(export->name, "__data_end")) {
  1412. global_index = export->index - module->import_global_count;
  1413. global = module->globals + global_index;
  1414. if (global->type == VALUE_TYPE_I32
  1415. && !global->is_mutable
  1416. && global->init_expr.init_expr_type ==
  1417. INIT_EXPR_TYPE_I32_CONST) {
  1418. data_end_global_index = global_index;
  1419. llvm_data_end_global = global;
  1420. llvm_data_end = global->init_expr.u.i32;
  1421. LOG_VERBOSE("found llvm __data_end global, value: %d\n",
  1422. llvm_data_end);
  1423. llvm_data_end = align_uint(llvm_data_end, 16);
  1424. }
  1425. }
  1426. if (llvm_data_end_global && llvm_heap_base_global) {
  1427. if ((data_end_global_index == heap_base_global_index + 1
  1428. && data_end_global_index > 0)
  1429. || (heap_base_global_index == data_end_global_index + 1
  1430. && heap_base_global_index > 0)) {
  1431. global_index =
  1432. data_end_global_index < heap_base_global_index
  1433. ? data_end_global_index - 1 : heap_base_global_index - 1;
  1434. global = module->globals + global_index;
  1435. if (global->type == VALUE_TYPE_I32
  1436. && global->is_mutable
  1437. && global->init_expr.init_expr_type ==
  1438. INIT_EXPR_TYPE_I32_CONST) {
  1439. llvm_stack_top_global = global;
  1440. llvm_stack_top = global->init_expr.u.i32;
  1441. stack_top_global_index = global_index;
  1442. LOG_VERBOSE("found llvm stack top global, "
  1443. "value: %d, global index: %d\n",
  1444. llvm_stack_top, global_index);
  1445. }
  1446. }
  1447. break;
  1448. }
  1449. }
  1450. }
  1451. if (llvm_data_end_global
  1452. && llvm_heap_base_global
  1453. && llvm_stack_top_global
  1454. && llvm_stack_top <= llvm_heap_base) {
  1455. WASMMemoryImport *memory_import;
  1456. WASMMemory *memory;
  1457. uint64 init_memory_size;
  1458. uint32 shrunk_memory_size = llvm_heap_base > llvm_data_end
  1459. ? llvm_heap_base : llvm_data_end;
  1460. if (module->import_memory_count) {
  1461. memory_import = &module->import_memories[0].u.memory;
  1462. init_memory_size = (uint64)memory_import->num_bytes_per_page *
  1463. memory_import->init_page_count;
  1464. if (llvm_heap_base <= init_memory_size
  1465. && llvm_data_end <= init_memory_size) {
  1466. /* Reset memory info to decrease memory usage */
  1467. memory_import->num_bytes_per_page = shrunk_memory_size;
  1468. memory_import->init_page_count = 1;
  1469. LOG_VERBOSE("reset import memory size to %d\n",
  1470. shrunk_memory_size);
  1471. }
  1472. }
  1473. if (module->memory_count) {
  1474. memory = &module->memories[0];
  1475. init_memory_size = (uint64)memory->num_bytes_per_page *
  1476. memory->init_page_count;
  1477. if (llvm_heap_base <= init_memory_size
  1478. && llvm_data_end <= init_memory_size) {
  1479. /* Reset memory info to decrease memory usage */
  1480. memory->num_bytes_per_page = shrunk_memory_size;
  1481. memory->init_page_count = 1;
  1482. LOG_VERBOSE("reset memory size to %d\n", shrunk_memory_size);
  1483. }
  1484. }
  1485. module->llvm_aux_data_end = llvm_data_end;
  1486. module->llvm_aux_stack_bottom = llvm_stack_top;
  1487. module->llvm_aux_stack_size = llvm_stack_top > llvm_data_end
  1488. ? llvm_stack_top - llvm_data_end
  1489. : llvm_stack_top;
  1490. module->llvm_aux_stack_global_index = stack_top_global_index;
  1491. LOG_VERBOSE("aux stack bottom: %d, size: %d\n",
  1492. module->llvm_aux_stack_bottom,
  1493. module->llvm_aux_stack_size);
  1494. }
  1495. }
  1496. return true;
  1497. }
  1498. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  1499. static void wasm_loader_free(void *ptr)
  1500. {
  1501. wasm_free(ptr);
  1502. }
  1503. #else
  1504. #define wasm_loader_free wasm_free
  1505. #endif
  1506. static WASMModule*
  1507. create_module(char *error_buf, uint32 error_buf_size)
  1508. {
  1509. WASMModule *module = wasm_malloc(sizeof(WASMModule));
  1510. if (!module) {
  1511. set_error_buf(error_buf, error_buf_size,
  1512. "WASM module load failed: "
  1513. "allocate memory failed.");
  1514. return NULL;
  1515. }
  1516. memset(module, 0, sizeof(WASMModule));
  1517. module->module_type = Wasm_Module_Bytecode;
  1518. /* Set start_function to -1, means no start function */
  1519. module->start_function = (uint32)-1;
  1520. return module;
  1521. }
  1522. WASMModule *
  1523. wasm_loader_load_from_sections(WASMSection *section_list,
  1524. char *error_buf, uint32 error_buf_size)
  1525. {
  1526. WASMModule *module = create_module(error_buf, error_buf_size);
  1527. if (!module)
  1528. return NULL;
  1529. if (!load_from_sections(module, section_list, error_buf, error_buf_size)) {
  1530. wasm_loader_unload(module);
  1531. return NULL;
  1532. }
  1533. LOG_VERBOSE("Load module from sections success.\n");
  1534. return module;
  1535. }
  1536. static void
  1537. destroy_sections(WASMSection *section_list)
  1538. {
  1539. WASMSection *section = section_list, *next;
  1540. while (section) {
  1541. next = section->next;
  1542. wasm_free(section);
  1543. section = next;
  1544. }
  1545. }
  1546. static bool
  1547. create_sections(const uint8 *buf, uint32 size,
  1548. WASMSection **p_section_list,
  1549. char *error_buf, uint32 error_buf_size)
  1550. {
  1551. WASMSection *section_list_end = NULL, *section;
  1552. const uint8 *p = buf, *p_end = buf + size/*, *section_body*/;
  1553. uint8 section_type, last_section_type = (uint8)-1;
  1554. uint32 section_size;
  1555. bh_assert(!*p_section_list);
  1556. p += 8;
  1557. while (p < p_end) {
  1558. CHECK_BUF(p, p_end, 1);
  1559. section_type = read_uint8(p);
  1560. if (section_type <= SECTION_TYPE_DATA) {
  1561. if (section_type != SECTION_TYPE_USER) {
  1562. /* Custom sections may be inserted at any place,
  1563. while other sections must occur at most once
  1564. and in prescribed order. */
  1565. if (last_section_type != (uint8)-1
  1566. && section_type <= last_section_type) {
  1567. set_error_buf(error_buf, error_buf_size,
  1568. "WASM module load failed: "
  1569. "junk after last section");
  1570. return false;
  1571. }
  1572. last_section_type = section_type;
  1573. }
  1574. CHECK_BUF1(p, p_end, 1);
  1575. read_leb_uint32(p, p_end, section_size);
  1576. CHECK_BUF1(p, p_end, section_size);
  1577. if (!(section = wasm_malloc(sizeof(WASMSection)))) {
  1578. set_error_buf(error_buf, error_buf_size,
  1579. "WASM module load failed: "
  1580. "allocate memory failed.");
  1581. return false;
  1582. }
  1583. memset(section, 0, sizeof(WASMSection));
  1584. section->section_type = section_type;
  1585. section->section_body = p;
  1586. section->section_body_size = section_size;
  1587. if (!*p_section_list)
  1588. *p_section_list = section_list_end = section;
  1589. else {
  1590. section_list_end->next = section;
  1591. section_list_end = section;
  1592. }
  1593. p += section_size;
  1594. }
  1595. else {
  1596. set_error_buf(error_buf, error_buf_size,
  1597. "WASM module load failed: invalid section id");
  1598. return false;
  1599. }
  1600. }
  1601. return true;
  1602. }
  1603. static void
  1604. exchange32(uint8* p_data)
  1605. {
  1606. uint8 value = *p_data;
  1607. *p_data = *(p_data + 3);
  1608. *(p_data + 3) = value;
  1609. value = *(p_data + 1);
  1610. *(p_data + 1) = *(p_data + 2);
  1611. *(p_data + 2) = value;
  1612. }
  1613. static union {
  1614. int a;
  1615. char b;
  1616. } __ue = { .a = 1 };
  1617. #define is_little_endian() (__ue.b == 1)
  1618. static bool
  1619. load(const uint8 *buf, uint32 size, WASMModule *module,
  1620. char *error_buf, uint32 error_buf_size)
  1621. {
  1622. const uint8 *buf_end = buf + size;
  1623. const uint8 *p = buf, *p_end = buf_end;
  1624. uint32 magic_number, version;
  1625. WASMSection *section_list = NULL;
  1626. CHECK_BUF1(p, p_end, sizeof(uint32));
  1627. magic_number = read_uint32(p);
  1628. if (!is_little_endian())
  1629. exchange32((uint8*)&magic_number);
  1630. if (magic_number != WASM_MAGIC_NUMBER) {
  1631. set_error_buf(error_buf, error_buf_size,
  1632. "WASM module load failed: magic header not detected");
  1633. return false;
  1634. }
  1635. CHECK_BUF1(p, p_end, sizeof(uint32));
  1636. version = read_uint32(p);
  1637. if (!is_little_endian())
  1638. exchange32((uint8*)&version);
  1639. if (version != WASM_CURRENT_VERSION) {
  1640. set_error_buf(error_buf, error_buf_size,
  1641. "WASM module load failed: unknown binary version");
  1642. return false;
  1643. }
  1644. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  1645. || !load_from_sections(module, section_list, error_buf, error_buf_size)) {
  1646. destroy_sections(section_list);
  1647. return false;
  1648. }
  1649. destroy_sections(section_list);
  1650. return true;
  1651. }
  1652. const uint8* wasm_file;
  1653. WASMModule*
  1654. wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
  1655. {
  1656. wasm_file = buf;
  1657. WASMModule *module = wasm_malloc(sizeof(WASMModule));
  1658. if (!module) {
  1659. set_error_buf(error_buf, error_buf_size,
  1660. "WASM module load failed: allocate memory failed.");
  1661. return NULL;
  1662. }
  1663. memset(module, 0, sizeof(WASMModule));
  1664. module->module_type = Wasm_Module_Bytecode;
  1665. /* Set start_function to -1, means no start function */
  1666. module->start_function = (uint32)-1;
  1667. if (!load(buf, size, module, error_buf, error_buf_size))
  1668. goto fail;
  1669. LOG_VERBOSE("Load module success.\n");
  1670. return module;
  1671. fail:
  1672. wasm_loader_unload(module);
  1673. return NULL;
  1674. }
  1675. void
  1676. wasm_loader_unload(WASMModule *module)
  1677. {
  1678. uint32 i;
  1679. if (!module)
  1680. return;
  1681. if (module->types) {
  1682. for (i = 0; i < module->type_count; i++) {
  1683. if (module->types[i])
  1684. wasm_free(module->types[i]);
  1685. }
  1686. wasm_free(module->types);
  1687. }
  1688. if (module->imports)
  1689. wasm_free(module->imports);
  1690. if (module->functions) {
  1691. for (i = 0; i < module->function_count; i++) {
  1692. if (module->functions[i]) {
  1693. if (module->functions[i]->local_offsets)
  1694. wasm_free(module->functions[i]->local_offsets);
  1695. wasm_free(module->functions[i]);
  1696. }
  1697. }
  1698. wasm_free(module->functions);
  1699. }
  1700. if (module->tables)
  1701. wasm_free(module->tables);
  1702. if (module->memories)
  1703. wasm_free(module->memories);
  1704. if (module->globals)
  1705. wasm_free(module->globals);
  1706. if (module->exports)
  1707. wasm_free(module->exports);
  1708. if (module->table_segments) {
  1709. for (i = 0; i < module->table_seg_count; i++) {
  1710. if (module->table_segments[i].func_indexes)
  1711. wasm_free(module->table_segments[i].func_indexes);
  1712. }
  1713. wasm_free(module->table_segments);
  1714. }
  1715. if (module->data_segments) {
  1716. for (i = 0; i < module->data_seg_count; i++) {
  1717. if (module->data_segments[i])
  1718. wasm_free(module->data_segments[i]);
  1719. }
  1720. wasm_free(module->data_segments);
  1721. }
  1722. if (module->const_str_list) {
  1723. StringNode *node = module->const_str_list, *node_next;
  1724. while (node) {
  1725. node_next = node->next;
  1726. wasm_free(node);
  1727. node = node_next;
  1728. }
  1729. }
  1730. wasm_free(module);
  1731. }
  1732. bool
  1733. wasm_loader_find_block_addr(WASMModule *module,
  1734. const uint8 *start_addr,
  1735. const uint8 *code_end_addr,
  1736. uint8 block_type,
  1737. uint8 **p_else_addr,
  1738. uint8 **p_end_addr,
  1739. char *error_buf,
  1740. uint32 error_buf_size)
  1741. {
  1742. const uint8 *p = start_addr, *p_end = code_end_addr;
  1743. uint8 *else_addr = NULL;
  1744. uint32 block_nested_depth = 1, count, i;
  1745. uint8 opcode, u8;
  1746. BlockAddr block_stack[16] = { 0 }, *block;
  1747. uint32 j, t;
  1748. i = (uint32)(((uintptr_t)start_addr) ^ ((uintptr_t)start_addr >> 16));
  1749. i = i % BLOCK_ADDR_CACHE_SIZE;
  1750. block = module->block_addr_cache[i];
  1751. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  1752. if (block[j].start_addr == start_addr) {
  1753. /* Cache hit */
  1754. *p_else_addr = block[j].else_addr;
  1755. *p_end_addr = block[j].end_addr;
  1756. return true;
  1757. }
  1758. }
  1759. /* Cache unhit */
  1760. block_stack[0].start_addr = start_addr;
  1761. while (p < code_end_addr) {
  1762. opcode = *p++;
  1763. switch (opcode) {
  1764. case WASM_OP_UNREACHABLE:
  1765. case WASM_OP_NOP:
  1766. break;
  1767. case WASM_OP_BLOCK:
  1768. case WASM_OP_LOOP:
  1769. case WASM_OP_IF:
  1770. CHECK_BUF(p, p_end, 1);
  1771. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  1772. u8 = read_uint8(p);
  1773. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
  1774. block_stack[block_nested_depth].start_addr = p;
  1775. block_stack[block_nested_depth].else_addr = NULL;
  1776. }
  1777. block_nested_depth++;
  1778. break;
  1779. case WASM_OP_ELSE:
  1780. if (block_type == BLOCK_TYPE_IF && block_nested_depth == 1)
  1781. else_addr = (uint8*)(p - 1);
  1782. if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr))
  1783. block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1);
  1784. break;
  1785. case WASM_OP_END:
  1786. if (block_nested_depth == 1) {
  1787. if (block_type == BLOCK_TYPE_IF)
  1788. *p_else_addr = else_addr;
  1789. *p_end_addr = (uint8*)(p - 1);
  1790. block_stack[0].end_addr = (uint8*)(p - 1);
  1791. for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) {
  1792. start_addr = block_stack[t].start_addr;
  1793. if (start_addr) {
  1794. i = (uint32)(((uintptr_t)start_addr) ^ ((uintptr_t)start_addr >> 16));
  1795. i = i % BLOCK_ADDR_CACHE_SIZE;
  1796. block = module->block_addr_cache[i];
  1797. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  1798. if (!block[j].start_addr)
  1799. break;
  1800. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  1801. memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) *
  1802. sizeof(BlockAddr));
  1803. j = 0;
  1804. }
  1805. block[j].start_addr = block_stack[t].start_addr;
  1806. block[j].else_addr = block_stack[t].else_addr;
  1807. block[j].end_addr = block_stack[t].end_addr;
  1808. }
  1809. else
  1810. break;
  1811. }
  1812. return true;
  1813. }
  1814. else {
  1815. block_nested_depth--;
  1816. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr))
  1817. block_stack[block_nested_depth].end_addr = (uint8*)(p - 1);
  1818. }
  1819. break;
  1820. case WASM_OP_BR:
  1821. case WASM_OP_BR_IF:
  1822. skip_leb_uint32(p, p_end); /* labelidx */
  1823. break;
  1824. case WASM_OP_BR_TABLE:
  1825. read_leb_uint32(p, p_end, count); /* lable num */
  1826. for (i = 0; i <= count; i++) /* lableidxs */
  1827. skip_leb_uint32(p, p_end);
  1828. break;
  1829. case WASM_OP_RETURN:
  1830. break;
  1831. case WASM_OP_CALL:
  1832. skip_leb_uint32(p, p_end); /* funcidx */
  1833. break;
  1834. case WASM_OP_CALL_INDIRECT:
  1835. skip_leb_uint32(p, p_end); /* typeidx */
  1836. CHECK_BUF(p, p_end, 1);
  1837. u8 = read_uint8(p); /* 0x00 */
  1838. break;
  1839. case WASM_OP_DROP:
  1840. case WASM_OP_SELECT:
  1841. case WASM_OP_DROP_64:
  1842. case WASM_OP_SELECT_64:
  1843. break;
  1844. case WASM_OP_GET_LOCAL:
  1845. case WASM_OP_SET_LOCAL:
  1846. case WASM_OP_TEE_LOCAL:
  1847. case WASM_OP_GET_GLOBAL:
  1848. case WASM_OP_SET_GLOBAL:
  1849. skip_leb_uint32(p, p_end); /* localidx */
  1850. break;
  1851. case WASM_OP_GET_LOCAL_FAST:
  1852. case WASM_OP_SET_LOCAL_FAST:
  1853. case WASM_OP_TEE_LOCAL_FAST:
  1854. CHECK_BUF(p, p_end, 1);
  1855. p++;
  1856. break;
  1857. case WASM_OP_I32_LOAD:
  1858. case WASM_OP_I64_LOAD:
  1859. case WASM_OP_F32_LOAD:
  1860. case WASM_OP_F64_LOAD:
  1861. case WASM_OP_I32_LOAD8_S:
  1862. case WASM_OP_I32_LOAD8_U:
  1863. case WASM_OP_I32_LOAD16_S:
  1864. case WASM_OP_I32_LOAD16_U:
  1865. case WASM_OP_I64_LOAD8_S:
  1866. case WASM_OP_I64_LOAD8_U:
  1867. case WASM_OP_I64_LOAD16_S:
  1868. case WASM_OP_I64_LOAD16_U:
  1869. case WASM_OP_I64_LOAD32_S:
  1870. case WASM_OP_I64_LOAD32_U:
  1871. case WASM_OP_I32_STORE:
  1872. case WASM_OP_I64_STORE:
  1873. case WASM_OP_F32_STORE:
  1874. case WASM_OP_F64_STORE:
  1875. case WASM_OP_I32_STORE8:
  1876. case WASM_OP_I32_STORE16:
  1877. case WASM_OP_I64_STORE8:
  1878. case WASM_OP_I64_STORE16:
  1879. case WASM_OP_I64_STORE32:
  1880. skip_leb_uint32(p, p_end); /* align */
  1881. skip_leb_uint32(p, p_end); /* offset */
  1882. break;
  1883. case WASM_OP_MEMORY_SIZE:
  1884. case WASM_OP_MEMORY_GROW:
  1885. skip_leb_uint32(p, p_end); /* 0x00 */
  1886. break;
  1887. case WASM_OP_I32_CONST:
  1888. skip_leb_int32(p, p_end);
  1889. break;
  1890. case WASM_OP_I64_CONST:
  1891. skip_leb_int64(p, p_end);
  1892. break;
  1893. case WASM_OP_F32_CONST:
  1894. p += sizeof(float32);
  1895. break;
  1896. case WASM_OP_F64_CONST:
  1897. p += sizeof(float64);
  1898. break;
  1899. case WASM_OP_I32_EQZ:
  1900. case WASM_OP_I32_EQ:
  1901. case WASM_OP_I32_NE:
  1902. case WASM_OP_I32_LT_S:
  1903. case WASM_OP_I32_LT_U:
  1904. case WASM_OP_I32_GT_S:
  1905. case WASM_OP_I32_GT_U:
  1906. case WASM_OP_I32_LE_S:
  1907. case WASM_OP_I32_LE_U:
  1908. case WASM_OP_I32_GE_S:
  1909. case WASM_OP_I32_GE_U:
  1910. case WASM_OP_I64_EQZ:
  1911. case WASM_OP_I64_EQ:
  1912. case WASM_OP_I64_NE:
  1913. case WASM_OP_I64_LT_S:
  1914. case WASM_OP_I64_LT_U:
  1915. case WASM_OP_I64_GT_S:
  1916. case WASM_OP_I64_GT_U:
  1917. case WASM_OP_I64_LE_S:
  1918. case WASM_OP_I64_LE_U:
  1919. case WASM_OP_I64_GE_S:
  1920. case WASM_OP_I64_GE_U:
  1921. case WASM_OP_F32_EQ:
  1922. case WASM_OP_F32_NE:
  1923. case WASM_OP_F32_LT:
  1924. case WASM_OP_F32_GT:
  1925. case WASM_OP_F32_LE:
  1926. case WASM_OP_F32_GE:
  1927. case WASM_OP_F64_EQ:
  1928. case WASM_OP_F64_NE:
  1929. case WASM_OP_F64_LT:
  1930. case WASM_OP_F64_GT:
  1931. case WASM_OP_F64_LE:
  1932. case WASM_OP_F64_GE:
  1933. case WASM_OP_I32_CLZ:
  1934. case WASM_OP_I32_CTZ:
  1935. case WASM_OP_I32_POPCNT:
  1936. case WASM_OP_I32_ADD:
  1937. case WASM_OP_I32_SUB:
  1938. case WASM_OP_I32_MUL:
  1939. case WASM_OP_I32_DIV_S:
  1940. case WASM_OP_I32_DIV_U:
  1941. case WASM_OP_I32_REM_S:
  1942. case WASM_OP_I32_REM_U:
  1943. case WASM_OP_I32_AND:
  1944. case WASM_OP_I32_OR:
  1945. case WASM_OP_I32_XOR:
  1946. case WASM_OP_I32_SHL:
  1947. case WASM_OP_I32_SHR_S:
  1948. case WASM_OP_I32_SHR_U:
  1949. case WASM_OP_I32_ROTL:
  1950. case WASM_OP_I32_ROTR:
  1951. case WASM_OP_I64_CLZ:
  1952. case WASM_OP_I64_CTZ:
  1953. case WASM_OP_I64_POPCNT:
  1954. case WASM_OP_I64_ADD:
  1955. case WASM_OP_I64_SUB:
  1956. case WASM_OP_I64_MUL:
  1957. case WASM_OP_I64_DIV_S:
  1958. case WASM_OP_I64_DIV_U:
  1959. case WASM_OP_I64_REM_S:
  1960. case WASM_OP_I64_REM_U:
  1961. case WASM_OP_I64_AND:
  1962. case WASM_OP_I64_OR:
  1963. case WASM_OP_I64_XOR:
  1964. case WASM_OP_I64_SHL:
  1965. case WASM_OP_I64_SHR_S:
  1966. case WASM_OP_I64_SHR_U:
  1967. case WASM_OP_I64_ROTL:
  1968. case WASM_OP_I64_ROTR:
  1969. case WASM_OP_F32_ABS:
  1970. case WASM_OP_F32_NEG:
  1971. case WASM_OP_F32_CEIL:
  1972. case WASM_OP_F32_FLOOR:
  1973. case WASM_OP_F32_TRUNC:
  1974. case WASM_OP_F32_NEAREST:
  1975. case WASM_OP_F32_SQRT:
  1976. case WASM_OP_F32_ADD:
  1977. case WASM_OP_F32_SUB:
  1978. case WASM_OP_F32_MUL:
  1979. case WASM_OP_F32_DIV:
  1980. case WASM_OP_F32_MIN:
  1981. case WASM_OP_F32_MAX:
  1982. case WASM_OP_F32_COPYSIGN:
  1983. case WASM_OP_F64_ABS:
  1984. case WASM_OP_F64_NEG:
  1985. case WASM_OP_F64_CEIL:
  1986. case WASM_OP_F64_FLOOR:
  1987. case WASM_OP_F64_TRUNC:
  1988. case WASM_OP_F64_NEAREST:
  1989. case WASM_OP_F64_SQRT:
  1990. case WASM_OP_F64_ADD:
  1991. case WASM_OP_F64_SUB:
  1992. case WASM_OP_F64_MUL:
  1993. case WASM_OP_F64_DIV:
  1994. case WASM_OP_F64_MIN:
  1995. case WASM_OP_F64_MAX:
  1996. case WASM_OP_F64_COPYSIGN:
  1997. case WASM_OP_I32_WRAP_I64:
  1998. case WASM_OP_I32_TRUNC_S_F32:
  1999. case WASM_OP_I32_TRUNC_U_F32:
  2000. case WASM_OP_I32_TRUNC_S_F64:
  2001. case WASM_OP_I32_TRUNC_U_F64:
  2002. case WASM_OP_I64_EXTEND_S_I32:
  2003. case WASM_OP_I64_EXTEND_U_I32:
  2004. case WASM_OP_I64_TRUNC_S_F32:
  2005. case WASM_OP_I64_TRUNC_U_F32:
  2006. case WASM_OP_I64_TRUNC_S_F64:
  2007. case WASM_OP_I64_TRUNC_U_F64:
  2008. case WASM_OP_F32_CONVERT_S_I32:
  2009. case WASM_OP_F32_CONVERT_U_I32:
  2010. case WASM_OP_F32_CONVERT_S_I64:
  2011. case WASM_OP_F32_CONVERT_U_I64:
  2012. case WASM_OP_F32_DEMOTE_F64:
  2013. case WASM_OP_F64_CONVERT_S_I32:
  2014. case WASM_OP_F64_CONVERT_U_I32:
  2015. case WASM_OP_F64_CONVERT_S_I64:
  2016. case WASM_OP_F64_CONVERT_U_I64:
  2017. case WASM_OP_F64_PROMOTE_F32:
  2018. case WASM_OP_I32_REINTERPRET_F32:
  2019. case WASM_OP_I64_REINTERPRET_F64:
  2020. case WASM_OP_F32_REINTERPRET_I32:
  2021. case WASM_OP_F64_REINTERPRET_I64:
  2022. break;
  2023. default:
  2024. if (error_buf)
  2025. snprintf(error_buf, error_buf_size,
  2026. "WASM loader find block addr failed: "
  2027. "invalid opcode %02x.", opcode);
  2028. return false;
  2029. }
  2030. }
  2031. (void)u8;
  2032. return false;
  2033. }
  2034. #define REF_I32 VALUE_TYPE_I32
  2035. #define REF_F32 VALUE_TYPE_F32
  2036. #define REF_I64_1 VALUE_TYPE_I64
  2037. #define REF_I64_2 VALUE_TYPE_I64
  2038. #define REF_F64_1 VALUE_TYPE_F64
  2039. #define REF_F64_2 VALUE_TYPE_F64
  2040. typedef struct BranchBlock {
  2041. uint8 block_type;
  2042. uint8 return_type;
  2043. bool is_block_reachable;
  2044. uint8 *start_addr;
  2045. uint8 *else_addr;
  2046. uint8 *end_addr;
  2047. uint32 stack_cell_num;
  2048. } BranchBlock;
  2049. static void*
  2050. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new)
  2051. {
  2052. uint8 *mem_new;
  2053. bh_assert(size_new > size_old);
  2054. if ((mem_new = wasm_malloc(size_new))) {
  2055. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  2056. memset(mem_new + size_old, 0, size_new - size_old);
  2057. wasm_free(mem_old);
  2058. }
  2059. return mem_new;
  2060. }
  2061. #define MEM_REALLOC(mem, size_old, size_new) do { \
  2062. void *mem_new = memory_realloc(mem, size_old, size_new);\
  2063. if (!mem_new) { \
  2064. set_error_buf(error_buf, error_buf_size, \
  2065. "WASM loader prepare bytecode failed: " \
  2066. "allocate memory failed."); \
  2067. goto fail; \
  2068. } \
  2069. mem = mem_new; \
  2070. } while (0)
  2071. static bool
  2072. check_stack_push(uint8 **p_frame_ref_bottom, uint8 **p_frame_ref_boundary,
  2073. uint8 **p_frame_ref, uint32 *p_frame_ref_size,
  2074. uint32 stack_cell_num,
  2075. char *error_buf, uint32 error_buf_size)
  2076. {
  2077. if (*p_frame_ref >= *p_frame_ref_boundary) {
  2078. MEM_REALLOC(*p_frame_ref_bottom, *p_frame_ref_size,
  2079. *p_frame_ref_size + 16);
  2080. *p_frame_ref_size += 16;
  2081. *p_frame_ref_boundary = *p_frame_ref_bottom + *p_frame_ref_size;
  2082. *p_frame_ref = *p_frame_ref_bottom + stack_cell_num;
  2083. }
  2084. return true;
  2085. fail:
  2086. return false;
  2087. }
  2088. #define CHECK_STACK_PUSH() do { \
  2089. if (!check_stack_push(&frame_ref_bottom, &frame_ref_boundary,\
  2090. &frame_ref, &frame_ref_size, \
  2091. stack_cell_num, \
  2092. error_buf, error_buf_size)) \
  2093. goto fail; \
  2094. } while (0)
  2095. static bool
  2096. check_stack_pop(uint8 type, uint8 *frame_ref, uint32 stack_cell_num,
  2097. char *error_buf, uint32 error_buf_size,
  2098. const char *type_str)
  2099. {
  2100. if (((type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  2101. && stack_cell_num < 1)
  2102. || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  2103. && stack_cell_num < 2)) {
  2104. set_error_buf(error_buf, error_buf_size,
  2105. "WASM module load failed: "
  2106. "type mismatch: expect data but stack was empty");
  2107. return false;
  2108. }
  2109. if ((type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  2110. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  2111. || (type == VALUE_TYPE_I64
  2112. && (*(frame_ref - 2) != REF_I64_1 || *(frame_ref - 1) != REF_I64_2))
  2113. || (type == VALUE_TYPE_F64
  2114. && (*(frame_ref - 2) != REF_F64_1 || *(frame_ref - 1) != REF_F64_2))) {
  2115. if (error_buf != NULL)
  2116. snprintf(error_buf, error_buf_size, "%s%s%s",
  2117. "WASM module load failed: type mismatch: expect ",
  2118. type_str, " but got other");
  2119. return false;
  2120. }
  2121. return true;
  2122. }
  2123. #define CHECK_STACK_POP(TYPE, type) do { \
  2124. if (!check_stack_pop(VALUE_TYPE_##TYPE, \
  2125. frame_ref, stack_cell_num, \
  2126. error_buf, error_buf_size, #type)) \
  2127. goto fail; \
  2128. } while (0)
  2129. #define PUSH_I32() do { \
  2130. CHECK_STACK_PUSH(); \
  2131. *frame_ref++ = REF_I32; \
  2132. stack_cell_num++; \
  2133. if (stack_cell_num > max_stack_cell_num) \
  2134. max_stack_cell_num = stack_cell_num; \
  2135. } while (0)
  2136. #define PUSH_F32() do { \
  2137. CHECK_STACK_PUSH(); \
  2138. *frame_ref++ = REF_F32; \
  2139. stack_cell_num++; \
  2140. if (stack_cell_num > max_stack_cell_num) \
  2141. max_stack_cell_num = stack_cell_num; \
  2142. } while (0)
  2143. #define PUSH_I64() do { \
  2144. CHECK_STACK_PUSH(); \
  2145. *frame_ref++ = REF_I64_1; \
  2146. stack_cell_num++; \
  2147. CHECK_STACK_PUSH(); \
  2148. *frame_ref++ = REF_I64_2; \
  2149. stack_cell_num++; \
  2150. if (stack_cell_num > max_stack_cell_num) \
  2151. max_stack_cell_num = stack_cell_num; \
  2152. } while (0)
  2153. #define PUSH_F64() do { \
  2154. CHECK_STACK_PUSH(); \
  2155. *frame_ref++ = REF_F64_1; \
  2156. stack_cell_num++; \
  2157. CHECK_STACK_PUSH(); \
  2158. *frame_ref++ = REF_F64_2; \
  2159. stack_cell_num++; \
  2160. if (stack_cell_num > max_stack_cell_num) \
  2161. max_stack_cell_num = stack_cell_num; \
  2162. } while (0)
  2163. #define POP_I32() do { \
  2164. CHECK_STACK_POP(I32, i32); \
  2165. stack_cell_num--; \
  2166. frame_ref--; \
  2167. } while (0)
  2168. #define POP_I64() do { \
  2169. CHECK_STACK_POP(I64, i64); \
  2170. stack_cell_num -= 2; \
  2171. frame_ref -= 2; \
  2172. } while (0)
  2173. #define POP_F32() do { \
  2174. CHECK_STACK_POP(F32, f32); \
  2175. stack_cell_num--; \
  2176. frame_ref--; \
  2177. } while (0)
  2178. #define POP_F64() do { \
  2179. CHECK_STACK_POP(F64, f64); \
  2180. stack_cell_num -= 2; \
  2181. frame_ref -= 2; \
  2182. } while (0)
  2183. static bool
  2184. push_type(uint8 type, uint8 **p_frame_ref_bottom,
  2185. uint8 **p_frame_ref_boundary,
  2186. uint8 **p_frame_ref, uint32 *p_frame_ref_size,
  2187. uint32 *p_stack_cell_num, uint32 *p_max_stack_cell_num,
  2188. char *error_buf, uint32 error_buf_size)
  2189. {
  2190. uint8 *frame_ref = *p_frame_ref;
  2191. uint32 frame_ref_size = *p_frame_ref_size;
  2192. uint32 max_stack_cell_num = *p_max_stack_cell_num;
  2193. uint32 stack_cell_num = *p_stack_cell_num;
  2194. switch (type) {
  2195. case VALUE_TYPE_I64:
  2196. case VALUE_TYPE_F64:
  2197. if (!check_stack_push(p_frame_ref_bottom, p_frame_ref_boundary,
  2198. &frame_ref, &frame_ref_size,
  2199. stack_cell_num,
  2200. error_buf, error_buf_size))
  2201. goto fail;
  2202. *frame_ref++ = type;
  2203. stack_cell_num++;
  2204. if (stack_cell_num > max_stack_cell_num)
  2205. max_stack_cell_num = stack_cell_num;
  2206. goto handle_i32_f32;
  2207. handle_i32_f32:
  2208. case VALUE_TYPE_I32:
  2209. case VALUE_TYPE_F32:
  2210. if (!check_stack_push(p_frame_ref_bottom, p_frame_ref_boundary,
  2211. &frame_ref, &frame_ref_size,
  2212. stack_cell_num,
  2213. error_buf, error_buf_size))
  2214. goto fail;
  2215. *frame_ref++ = type;
  2216. stack_cell_num++;
  2217. if (stack_cell_num > max_stack_cell_num)
  2218. max_stack_cell_num = stack_cell_num;
  2219. break;
  2220. }
  2221. *p_frame_ref = frame_ref;
  2222. *p_frame_ref_size = frame_ref_size;
  2223. *p_max_stack_cell_num = max_stack_cell_num;
  2224. *p_stack_cell_num = stack_cell_num;
  2225. return true;
  2226. fail:
  2227. return false;
  2228. }
  2229. #define PUSH_TYPE(type) do { \
  2230. if (!push_type(type, &frame_ref_bottom, \
  2231. &frame_ref_boundary, \
  2232. &frame_ref, &frame_ref_size, \
  2233. &stack_cell_num, &max_stack_cell_num, \
  2234. error_buf, error_buf_size)) \
  2235. goto fail; \
  2236. } while (0)
  2237. static bool
  2238. pop_type(uint8 type, uint8 **p_frame_ref, uint32 *p_stack_cell_num,
  2239. char *error_buf, uint32 error_buf_size)
  2240. {
  2241. char *type_str[] = { "f64", "f32", "i64", "i32" };
  2242. switch (type) {
  2243. case VALUE_TYPE_I64:
  2244. case VALUE_TYPE_F64:
  2245. if (!check_stack_pop(type, *p_frame_ref, *p_stack_cell_num,
  2246. error_buf, error_buf_size,
  2247. type_str[type - VALUE_TYPE_F64]))
  2248. return false;
  2249. *p_frame_ref -= 2;
  2250. *p_stack_cell_num -= 2;
  2251. break;
  2252. case VALUE_TYPE_I32:
  2253. case VALUE_TYPE_F32:
  2254. if (!check_stack_pop(type, *p_frame_ref, *p_stack_cell_num,
  2255. error_buf, error_buf_size,
  2256. type_str[type - VALUE_TYPE_F64]))
  2257. return false;
  2258. *p_frame_ref -= 1;
  2259. *p_stack_cell_num -= 1;
  2260. break;
  2261. }
  2262. return true;
  2263. }
  2264. #define POP_TYPE(type) do { \
  2265. if (!pop_type(type, &frame_ref, &stack_cell_num,\
  2266. error_buf, error_buf_size)) \
  2267. goto fail; \
  2268. } while (0)
  2269. #define CHECK_CSP_PUSH() do { \
  2270. if (frame_csp >= frame_csp_boundary) { \
  2271. MEM_REALLOC(frame_csp_bottom, frame_csp_size, \
  2272. (uint32)(frame_csp_size \
  2273. + 8 * sizeof(BranchBlock))); \
  2274. frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  2275. frame_csp_boundary = frame_csp_bottom + \
  2276. frame_csp_size / sizeof(BranchBlock); \
  2277. frame_csp = frame_csp_bottom + csp_num; \
  2278. } \
  2279. } while (0)
  2280. #define CHECK_CSP_POP() do { \
  2281. if (csp_num < 1) { \
  2282. set_error_buf(error_buf, error_buf_size, \
  2283. "WASM module load failed: type mismatch: "\
  2284. "expect data but block stack was empty"); \
  2285. goto fail; \
  2286. } \
  2287. } while (0)
  2288. #define PUSH_CSP(type, ret_type, _start_addr) do { \
  2289. CHECK_CSP_PUSH(); \
  2290. frame_csp->block_type = type; \
  2291. frame_csp->is_block_reachable = false; \
  2292. frame_csp->return_type = ret_type; \
  2293. frame_csp->start_addr = _start_addr; \
  2294. frame_csp->else_addr = NULL; \
  2295. frame_csp->end_addr = NULL; \
  2296. frame_csp->stack_cell_num = stack_cell_num; \
  2297. frame_csp++; \
  2298. csp_num++; \
  2299. if (csp_num > max_csp_num) \
  2300. max_csp_num = csp_num; \
  2301. } while (0)
  2302. #define POP_CSP() do { \
  2303. CHECK_CSP_POP(); \
  2304. frame_csp--; \
  2305. csp_num--; \
  2306. } while (0)
  2307. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \
  2308. read_leb_uint32(p, p_end, local_idx); \
  2309. if (local_idx >= param_count + local_count) { \
  2310. set_error_buf(error_buf, error_buf_size, \
  2311. "WASM module load failed: " \
  2312. "local index out of range"); \
  2313. goto fail; \
  2314. } \
  2315. local_type = local_idx < param_count \
  2316. ? param_types[local_idx] \
  2317. : local_types[local_idx - param_count]; \
  2318. local_offset = local_offsets[local_idx]; \
  2319. } while (0)
  2320. #define CHECK_BR(depth) do { \
  2321. if (csp_num < depth + 1) { \
  2322. set_error_buf(error_buf, error_buf_size, \
  2323. "WASM module load failed: type mismatch: " \
  2324. "unexpected end of section or function"); \
  2325. goto fail; \
  2326. } \
  2327. if ((frame_csp - (depth + 1))->block_type != BLOCK_TYPE_LOOP) { \
  2328. uint8 tmp_ret_type = (frame_csp - (depth + 1))->return_type; \
  2329. if ((tmp_ret_type == VALUE_TYPE_I32 \
  2330. && (stack_cell_num < 1 || *(frame_ref - 1) != REF_I32)) \
  2331. || (tmp_ret_type == VALUE_TYPE_F32 \
  2332. && (stack_cell_num < 1 || *(frame_ref - 1) != REF_F32))\
  2333. || (tmp_ret_type == VALUE_TYPE_I64 \
  2334. && (stack_cell_num < 2 \
  2335. || *(frame_ref - 2) != REF_I64_1 \
  2336. || *(frame_ref - 1) != REF_I64_2)) \
  2337. || (tmp_ret_type == VALUE_TYPE_F64 \
  2338. && (stack_cell_num < 2 \
  2339. || *(frame_ref - 2) != REF_F64_1 \
  2340. || *(frame_ref - 1) != REF_F64_2))) { \
  2341. set_error_buf(error_buf, error_buf_size, \
  2342. "WASM module load failed: type mismatch: " \
  2343. "expect data but stack was empty or other type"); \
  2344. goto fail; \
  2345. } \
  2346. (frame_csp - (depth + 1))->is_block_reachable = true; \
  2347. } \
  2348. } while (0)
  2349. static bool
  2350. check_memory(WASMModule *module,
  2351. char *error_buf, uint32 error_buf_size)
  2352. {
  2353. if (module->memory_count == 0
  2354. && module->import_memory_count == 0) {
  2355. set_error_buf(error_buf, error_buf_size,
  2356. "WASM module load failed: "
  2357. "load or store in module without default memory");
  2358. return false;
  2359. }
  2360. return true;
  2361. }
  2362. #define CHECK_MEMORY() do { \
  2363. if (!check_memory(module, error_buf, error_buf_size)) \
  2364. goto fail; \
  2365. } while (0)
  2366. static bool
  2367. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  2368. char *error_buf, uint32 error_buf_size)
  2369. {
  2370. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  2371. uint8 *frame_ref_bottom = NULL, *frame_ref_boundary, *frame_ref;
  2372. BranchBlock *frame_csp_bottom = NULL, *frame_csp_boundary, *frame_csp;
  2373. uint32 param_count, local_count, global_count;
  2374. uint32 max_stack_cell_num = 0, max_csp_num = 0;
  2375. uint32 stack_cell_num = 0, csp_num = 0;
  2376. uint32 frame_ref_size, frame_csp_size;
  2377. uint8 *param_types, ret_type, *local_types, local_type, global_type;
  2378. uint16 *local_offsets, local_offset;
  2379. uint32 count, i, local_idx, global_idx, depth, u32;
  2380. int32 i32, i32_const = 0;
  2381. int64 i64;
  2382. uint8 opcode, u8, block_return_type;
  2383. bool return_value = false, is_i32_const = false;
  2384. global_count = module->import_global_count + module->global_count;
  2385. param_count = func->func_type->param_count;
  2386. param_types = func->func_type->types;
  2387. ret_type = func->func_type->result_count
  2388. ? param_types[param_count] : VALUE_TYPE_VOID;
  2389. local_count = func->local_count;
  2390. local_types = func->local_types;
  2391. local_offsets = func->local_offsets;
  2392. frame_ref_size = 32;
  2393. if (!(frame_ref_bottom = frame_ref = wasm_malloc(frame_ref_size))) {
  2394. set_error_buf(error_buf, error_buf_size,
  2395. "WASM loader prepare bytecode failed: "
  2396. "allocate memory failed");
  2397. goto fail;
  2398. }
  2399. memset(frame_ref_bottom, 0, frame_ref_size);
  2400. frame_ref_boundary = frame_ref_bottom + frame_ref_size;
  2401. frame_csp_size = sizeof(BranchBlock) * 8;
  2402. if (!(frame_csp_bottom = frame_csp = wasm_malloc(frame_csp_size))) {
  2403. set_error_buf(error_buf, error_buf_size,
  2404. "WASM loader prepare bytecode failed: "
  2405. "allocate memory failed");
  2406. goto fail;
  2407. }
  2408. memset(frame_csp_bottom, 0, frame_csp_size);
  2409. frame_csp_boundary = frame_csp_bottom + 8;
  2410. PUSH_CSP(BLOCK_TYPE_FUNCTION, ret_type, p);
  2411. (frame_csp - 1)->is_block_reachable = true;
  2412. while (p < p_end) {
  2413. opcode = *p++;
  2414. switch (opcode) {
  2415. case WASM_OP_UNREACHABLE:
  2416. goto handle_next_reachable_block;
  2417. case WASM_OP_NOP:
  2418. break;
  2419. case WASM_OP_BLOCK:
  2420. /* 0x40/0x7F/0x7E/0x7D/0x7C */
  2421. block_return_type = read_uint8(p);
  2422. PUSH_CSP(BLOCK_TYPE_BLOCK, block_return_type, p);
  2423. break;
  2424. case WASM_OP_LOOP:
  2425. /* 0x40/0x7F/0x7E/0x7D/0x7C */
  2426. block_return_type = read_uint8(p);
  2427. PUSH_CSP(BLOCK_TYPE_LOOP, block_return_type, p);
  2428. break;
  2429. case WASM_OP_IF:
  2430. POP_I32();
  2431. /* 0x40/0x7F/0x7E/0x7D/0x7C */
  2432. block_return_type = read_uint8(p);
  2433. PUSH_CSP(BLOCK_TYPE_IF, block_return_type, p);
  2434. if (!is_i32_const)
  2435. (frame_csp - 1)->is_block_reachable = true;
  2436. else {
  2437. if (!i32_const) {
  2438. if(!wasm_loader_find_block_addr(module,
  2439. (frame_csp - 1)->start_addr,
  2440. p_end,
  2441. (frame_csp - 1)->block_type,
  2442. &(frame_csp - 1)->else_addr,
  2443. &(frame_csp - 1)->end_addr,
  2444. error_buf, error_buf_size))
  2445. goto fail;
  2446. if ((frame_csp - 1)->else_addr)
  2447. p = (frame_csp - 1)->else_addr;
  2448. else
  2449. p = (frame_csp - 1)->end_addr;
  2450. }
  2451. }
  2452. break;
  2453. case WASM_OP_ELSE:
  2454. if (csp_num < 2
  2455. || (frame_csp - 1)->block_type != BLOCK_TYPE_IF) {
  2456. set_error_buf(error_buf, error_buf_size,
  2457. "WASM loader prepare bytecode failed: "
  2458. "opcode else found without matched opcode if");
  2459. goto fail;
  2460. }
  2461. (frame_csp - 1)->else_addr = p - 1;
  2462. stack_cell_num = (frame_csp - 1)->stack_cell_num;
  2463. frame_ref = frame_ref_bottom + stack_cell_num;
  2464. break;
  2465. case WASM_OP_END:
  2466. {
  2467. POP_CSP();
  2468. POP_TYPE(frame_csp->return_type);
  2469. PUSH_TYPE(frame_csp->return_type);
  2470. if (csp_num > 0) {
  2471. frame_csp->end_addr = p - 1;
  2472. }
  2473. else {
  2474. /* end of function block, function will return,
  2475. ignore the following bytecodes */
  2476. p = p_end;
  2477. }
  2478. break;
  2479. }
  2480. case WASM_OP_BR:
  2481. {
  2482. read_leb_uint32(p, p_end, depth);
  2483. CHECK_BR(depth);
  2484. handle_next_reachable_block:
  2485. for (i = 1; i <= csp_num; i++)
  2486. if ((frame_csp - i)->is_block_reachable)
  2487. break;
  2488. block_return_type = (frame_csp - i)->return_type;
  2489. if(!wasm_loader_find_block_addr(module,
  2490. (frame_csp - i)->start_addr,
  2491. p_end,
  2492. (frame_csp - i)->block_type,
  2493. &(frame_csp - i)->else_addr,
  2494. &(frame_csp - i)->end_addr,
  2495. error_buf, error_buf_size))
  2496. goto fail;
  2497. stack_cell_num = (frame_csp - i)->stack_cell_num;
  2498. frame_ref = frame_ref_bottom + stack_cell_num;
  2499. csp_num -= i - 1;
  2500. frame_csp -= i - 1;
  2501. if ((frame_csp - 1)->block_type == BLOCK_TYPE_IF
  2502. && (frame_csp - 1)->else_addr != NULL
  2503. && p <= (frame_csp - 1)->else_addr)
  2504. p = (frame_csp - 1)->else_addr;
  2505. else {
  2506. p = (frame_csp - 1)->end_addr;
  2507. PUSH_TYPE(block_return_type);
  2508. }
  2509. break;
  2510. }
  2511. case WASM_OP_BR_IF:
  2512. read_leb_uint32(p, p_end, depth);
  2513. POP_I32();
  2514. CHECK_BR(depth);
  2515. if (!is_i32_const)
  2516. (frame_csp - (depth + 1))->is_block_reachable = true;
  2517. else {
  2518. if (i32_const)
  2519. goto handle_next_reachable_block;
  2520. }
  2521. break;
  2522. case WASM_OP_BR_TABLE:
  2523. {
  2524. read_leb_uint32(p, p_end, count);
  2525. POP_I32();
  2526. /* TODO: check the const */
  2527. for (i = 0; i <= count; i++) {
  2528. read_leb_uint32(p, p_end, depth);
  2529. CHECK_BR(depth);
  2530. }
  2531. goto handle_next_reachable_block;
  2532. }
  2533. case WASM_OP_RETURN:
  2534. {
  2535. POP_TYPE(ret_type);
  2536. PUSH_TYPE(ret_type);
  2537. if(!wasm_loader_find_block_addr(module,
  2538. (frame_csp - 1)->start_addr,
  2539. p_end,
  2540. (frame_csp - 1)->block_type,
  2541. &(frame_csp - 1)->else_addr,
  2542. &(frame_csp - 1)->end_addr,
  2543. error_buf, error_buf_size))
  2544. goto fail;
  2545. stack_cell_num = (frame_csp - 1)->stack_cell_num;
  2546. frame_ref = frame_ref_bottom + stack_cell_num;
  2547. if ((frame_csp - 1)->block_type == BLOCK_TYPE_IF
  2548. && p <= (frame_csp - 1)->else_addr) {
  2549. p = (frame_csp - 1)->else_addr;
  2550. }
  2551. else {
  2552. p = (frame_csp - 1)->end_addr;
  2553. PUSH_TYPE((frame_csp - 1)->return_type);
  2554. }
  2555. break;
  2556. }
  2557. case WASM_OP_CALL:
  2558. {
  2559. WASMType *func_type;
  2560. uint32 func_idx;
  2561. int32 idx;
  2562. read_leb_uint32(p, p_end, func_idx);
  2563. if (func_idx >= module->import_function_count + module->function_count) {
  2564. set_error_buf(error_buf, error_buf_size,
  2565. "WASM loader prepare bytecode failed: "
  2566. "function index out of range");
  2567. goto fail;
  2568. }
  2569. if (func_idx < module->import_function_count)
  2570. func_type = module->import_functions[func_idx].u.function.func_type;
  2571. else
  2572. func_type =
  2573. module->functions[func_idx - module->import_function_count]->func_type;
  2574. if (func_type->param_count > 0) {
  2575. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--)
  2576. POP_TYPE(func_type->types[idx]);
  2577. }
  2578. if (func_type->result_count)
  2579. PUSH_TYPE(func_type->types[func_type->param_count]);
  2580. func->has_op_func_call = true;
  2581. break;
  2582. }
  2583. case WASM_OP_CALL_INDIRECT:
  2584. {
  2585. int32 idx;
  2586. WASMType *func_type;
  2587. uint32 type_idx;
  2588. if (module->table_count == 0
  2589. && module->import_table_count == 0) {
  2590. set_error_buf(error_buf, error_buf_size,
  2591. "WASM loader prepare bytecode failed: "
  2592. "call indirect without default table");
  2593. goto fail;
  2594. }
  2595. read_leb_uint32(p, p_end, type_idx);
  2596. /* reserved byte 0x00 */
  2597. if (*p++ != 0x00) {
  2598. set_error_buf(error_buf, error_buf_size,
  2599. "WASM loader prepare bytecode failed: "
  2600. "zero flag expected");
  2601. goto fail;
  2602. }
  2603. POP_I32();
  2604. if (type_idx >= module->type_count) {
  2605. set_error_buf(error_buf, error_buf_size,
  2606. "WASM loader prepare bytecode failed: "
  2607. "function index out of range");
  2608. goto fail;
  2609. }
  2610. func_type = module->types[type_idx];
  2611. if (func_type->param_count > 0) {
  2612. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--)
  2613. POP_TYPE(func_type->types[idx]);
  2614. }
  2615. PUSH_TYPE(func_type->types[func_type->param_count]);
  2616. func->has_op_func_call = true;
  2617. break;
  2618. }
  2619. case WASM_OP_DROP:
  2620. {
  2621. if (stack_cell_num <= 0) {
  2622. set_error_buf(error_buf, error_buf_size,
  2623. "WASM loader prepare bytecode failed: "
  2624. "opcode drop was found but stack was empty");
  2625. goto fail;
  2626. }
  2627. if (*(frame_ref - 1) == REF_I32
  2628. || *(frame_ref - 1) == REF_F32) {
  2629. frame_ref--;
  2630. stack_cell_num--;
  2631. }
  2632. else {
  2633. if (stack_cell_num <= 1) {
  2634. set_error_buf(error_buf, error_buf_size,
  2635. "WASM loader prepare bytecode failed: "
  2636. "opcode drop was found but stack was empty");
  2637. goto fail;
  2638. }
  2639. frame_ref -= 2;
  2640. stack_cell_num -= 2;
  2641. *(p - 1) = WASM_OP_DROP_64;
  2642. }
  2643. break;
  2644. }
  2645. case WASM_OP_SELECT:
  2646. {
  2647. uint8 ref_type;
  2648. POP_I32();
  2649. if (stack_cell_num <= 0) {
  2650. set_error_buf(error_buf, error_buf_size,
  2651. "WASM loader prepare bytecode failed: "
  2652. "opcode select was found but stack was empty");
  2653. goto fail;
  2654. }
  2655. switch (*(frame_ref - 1)) {
  2656. case REF_I32:
  2657. case REF_F32:
  2658. break;
  2659. case REF_I64_2:
  2660. case REF_F64_2:
  2661. *(p - 1) = WASM_OP_SELECT_64;
  2662. break;
  2663. }
  2664. ref_type = *(frame_ref - 1);
  2665. POP_TYPE(ref_type);
  2666. POP_TYPE(ref_type);
  2667. PUSH_TYPE(ref_type);
  2668. break;
  2669. }
  2670. case WASM_OP_GET_LOCAL:
  2671. {
  2672. p_org = p - 1;
  2673. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  2674. PUSH_TYPE(local_type);
  2675. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  2676. if (local_offset < 0x80) {
  2677. *p_org++ = WASM_OP_GET_LOCAL_FAST;
  2678. if (local_type == VALUE_TYPE_I32
  2679. || local_type == VALUE_TYPE_F32)
  2680. *p_org++ = (uint8)local_offset;
  2681. else
  2682. *p_org++ = (uint8)(local_offset | 0x80);
  2683. while (p_org < p)
  2684. *p_org++ = WASM_OP_NOP;
  2685. }
  2686. #endif
  2687. break;
  2688. }
  2689. case WASM_OP_SET_LOCAL:
  2690. {
  2691. p_org = p - 1;
  2692. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  2693. POP_TYPE(local_type);
  2694. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  2695. if (local_offset < 0x80) {
  2696. *p_org++ = WASM_OP_SET_LOCAL_FAST;
  2697. if (local_type == VALUE_TYPE_I32
  2698. || local_type == VALUE_TYPE_F32)
  2699. *p_org++ = (uint8)local_offset;
  2700. else
  2701. *p_org++ = (uint8)(local_offset | 0x80);
  2702. while (p_org < p)
  2703. *p_org++ = WASM_OP_NOP;
  2704. }
  2705. #endif
  2706. break;
  2707. }
  2708. case WASM_OP_TEE_LOCAL:
  2709. {
  2710. p_org = p - 1;
  2711. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  2712. POP_TYPE(local_type);
  2713. PUSH_TYPE(local_type);
  2714. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  2715. if (local_offset < 0x80) {
  2716. *p_org++ = WASM_OP_TEE_LOCAL_FAST;
  2717. if (local_type == VALUE_TYPE_I32
  2718. || local_type == VALUE_TYPE_F32)
  2719. *p_org++ = (uint8)local_offset;
  2720. else
  2721. *p_org++ = (uint8)(local_offset | 0x80);
  2722. while (p_org < p)
  2723. *p_org++ = WASM_OP_NOP;
  2724. }
  2725. #endif
  2726. break;
  2727. }
  2728. case WASM_OP_GET_GLOBAL:
  2729. {
  2730. read_leb_uint32(p, p_end, global_idx);
  2731. if (global_idx >= global_count) {
  2732. set_error_buf(error_buf, error_buf_size,
  2733. "WASM loader prepare bytecode failed: "
  2734. "global index out of range");
  2735. goto fail;
  2736. }
  2737. global_type = global_idx < module->import_global_count
  2738. ? module->import_globals[global_idx].u.global.type
  2739. :module->globals[global_idx - module->import_global_count].type;
  2740. PUSH_TYPE(global_type);
  2741. break;
  2742. }
  2743. case WASM_OP_SET_GLOBAL:
  2744. {
  2745. read_leb_uint32(p, p_end, global_idx);
  2746. if (global_idx >= global_count) {
  2747. set_error_buf(error_buf, error_buf_size,
  2748. "WASM loader prepare bytecode failed: "
  2749. "global index out of range");
  2750. goto fail;
  2751. }
  2752. global_type = global_idx < module->import_global_count
  2753. ? module->import_globals[global_idx].u.global.type
  2754. : module->globals[global_idx - module->import_global_count].type;
  2755. POP_TYPE(global_type);
  2756. break;
  2757. }
  2758. case WASM_OP_I32_LOAD:
  2759. case WASM_OP_I32_LOAD8_S:
  2760. case WASM_OP_I32_LOAD8_U:
  2761. case WASM_OP_I32_LOAD16_S:
  2762. case WASM_OP_I32_LOAD16_U:
  2763. CHECK_MEMORY();
  2764. read_leb_uint32(p, p_end, u32); /* align */
  2765. read_leb_uint32(p, p_end, u32); /* offset */
  2766. POP_I32();
  2767. PUSH_I32();
  2768. break;
  2769. case WASM_OP_I64_LOAD:
  2770. case WASM_OP_I64_LOAD8_S:
  2771. case WASM_OP_I64_LOAD8_U:
  2772. case WASM_OP_I64_LOAD16_S:
  2773. case WASM_OP_I64_LOAD16_U:
  2774. case WASM_OP_I64_LOAD32_S:
  2775. case WASM_OP_I64_LOAD32_U:
  2776. CHECK_MEMORY();
  2777. read_leb_uint32(p, p_end, u32); /* align */
  2778. read_leb_uint32(p, p_end, u32); /* offset */
  2779. POP_I32();
  2780. PUSH_I64();
  2781. break;
  2782. case WASM_OP_F32_LOAD:
  2783. CHECK_MEMORY();
  2784. read_leb_uint32(p, p_end, u32); /* align */
  2785. read_leb_uint32(p, p_end, u32); /* offset */
  2786. POP_I32();
  2787. PUSH_F32();
  2788. break;
  2789. case WASM_OP_F64_LOAD:
  2790. CHECK_MEMORY();
  2791. read_leb_uint32(p, p_end, u32); /* align */
  2792. read_leb_uint32(p, p_end, u32); /* offset */
  2793. POP_I32();
  2794. PUSH_F64();
  2795. break;
  2796. case WASM_OP_I32_STORE:
  2797. case WASM_OP_I32_STORE8:
  2798. case WASM_OP_I32_STORE16:
  2799. CHECK_MEMORY();
  2800. read_leb_uint32(p, p_end, u32); /* align */
  2801. read_leb_uint32(p, p_end, u32); /* offset */
  2802. POP_I32();
  2803. POP_I32();
  2804. break;
  2805. case WASM_OP_I64_STORE:
  2806. case WASM_OP_I64_STORE8:
  2807. case WASM_OP_I64_STORE16:
  2808. case WASM_OP_I64_STORE32:
  2809. CHECK_MEMORY();
  2810. read_leb_uint32(p, p_end, u32); /* align */
  2811. read_leb_uint32(p, p_end, u32); /* offset */
  2812. POP_I64();
  2813. POP_I32();
  2814. break;
  2815. case WASM_OP_F32_STORE:
  2816. CHECK_MEMORY();
  2817. read_leb_uint32(p, p_end, u32); /* align */
  2818. read_leb_uint32(p, p_end, u32); /* offset */
  2819. POP_F32();
  2820. POP_I32();
  2821. break;
  2822. case WASM_OP_F64_STORE:
  2823. CHECK_MEMORY();
  2824. read_leb_uint32(p, p_end, u32); /* align */
  2825. read_leb_uint32(p, p_end, u32); /* offset */
  2826. POP_F64();
  2827. POP_I32();
  2828. break;
  2829. case WASM_OP_MEMORY_SIZE:
  2830. CHECK_MEMORY();
  2831. /* reserved byte 0x00 */
  2832. if (*p++ != 0x00) {
  2833. set_error_buf(error_buf, error_buf_size,
  2834. "WASM loader prepare bytecode failed: "
  2835. "zero flag expected");
  2836. goto fail;
  2837. }
  2838. PUSH_I32();
  2839. break;
  2840. case WASM_OP_MEMORY_GROW:
  2841. CHECK_MEMORY();
  2842. /* reserved byte 0x00 */
  2843. if (*p++ != 0x00) {
  2844. set_error_buf(error_buf, error_buf_size,
  2845. "WASM loader prepare bytecode failed: "
  2846. "zero flag expected");
  2847. goto fail;
  2848. }
  2849. POP_I32();
  2850. PUSH_I32();
  2851. func->has_op_memory_grow = true;
  2852. module->possible_memory_grow = true;
  2853. break;
  2854. case WASM_OP_I32_CONST:
  2855. read_leb_int32(p, p_end, i32_const);
  2856. /* Currently we only track simple I32_CONST opcode. */
  2857. is_i32_const = true;
  2858. PUSH_I32();
  2859. break;
  2860. case WASM_OP_I64_CONST:
  2861. read_leb_int64(p, p_end, i64);
  2862. PUSH_I64();
  2863. break;
  2864. case WASM_OP_F32_CONST:
  2865. p += sizeof(float32);
  2866. PUSH_F32();
  2867. break;
  2868. case WASM_OP_F64_CONST:
  2869. p += sizeof(float64);
  2870. PUSH_F64();
  2871. break;
  2872. case WASM_OP_I32_EQZ:
  2873. POP_I32();
  2874. PUSH_I32();
  2875. break;
  2876. case WASM_OP_I32_EQ:
  2877. case WASM_OP_I32_NE:
  2878. case WASM_OP_I32_LT_S:
  2879. case WASM_OP_I32_LT_U:
  2880. case WASM_OP_I32_GT_S:
  2881. case WASM_OP_I32_GT_U:
  2882. case WASM_OP_I32_LE_S:
  2883. case WASM_OP_I32_LE_U:
  2884. case WASM_OP_I32_GE_S:
  2885. case WASM_OP_I32_GE_U:
  2886. POP_I32();
  2887. POP_I32();
  2888. PUSH_I32();
  2889. break;
  2890. case WASM_OP_I64_EQZ:
  2891. POP_I64();
  2892. PUSH_I32();
  2893. break;
  2894. case WASM_OP_I64_EQ:
  2895. case WASM_OP_I64_NE:
  2896. case WASM_OP_I64_LT_S:
  2897. case WASM_OP_I64_LT_U:
  2898. case WASM_OP_I64_GT_S:
  2899. case WASM_OP_I64_GT_U:
  2900. case WASM_OP_I64_LE_S:
  2901. case WASM_OP_I64_LE_U:
  2902. case WASM_OP_I64_GE_S:
  2903. case WASM_OP_I64_GE_U:
  2904. POP_I64();
  2905. POP_I64();
  2906. PUSH_I32();
  2907. break;
  2908. case WASM_OP_F32_EQ:
  2909. case WASM_OP_F32_NE:
  2910. case WASM_OP_F32_LT:
  2911. case WASM_OP_F32_GT:
  2912. case WASM_OP_F32_LE:
  2913. case WASM_OP_F32_GE:
  2914. POP_F32();
  2915. POP_F32();
  2916. PUSH_I32();
  2917. break;
  2918. case WASM_OP_F64_EQ:
  2919. case WASM_OP_F64_NE:
  2920. case WASM_OP_F64_LT:
  2921. case WASM_OP_F64_GT:
  2922. case WASM_OP_F64_LE:
  2923. case WASM_OP_F64_GE:
  2924. POP_F64();
  2925. POP_F64();
  2926. PUSH_I32();
  2927. break;
  2928. break;
  2929. case WASM_OP_I32_CLZ:
  2930. case WASM_OP_I32_CTZ:
  2931. case WASM_OP_I32_POPCNT:
  2932. POP_I32();
  2933. PUSH_I32();
  2934. break;
  2935. case WASM_OP_I32_ADD:
  2936. case WASM_OP_I32_SUB:
  2937. case WASM_OP_I32_MUL:
  2938. case WASM_OP_I32_DIV_S:
  2939. case WASM_OP_I32_DIV_U:
  2940. case WASM_OP_I32_REM_S:
  2941. case WASM_OP_I32_REM_U:
  2942. case WASM_OP_I32_AND:
  2943. case WASM_OP_I32_OR:
  2944. case WASM_OP_I32_XOR:
  2945. case WASM_OP_I32_SHL:
  2946. case WASM_OP_I32_SHR_S:
  2947. case WASM_OP_I32_SHR_U:
  2948. case WASM_OP_I32_ROTL:
  2949. case WASM_OP_I32_ROTR:
  2950. POP_I32();
  2951. POP_I32();
  2952. PUSH_I32();
  2953. break;
  2954. case WASM_OP_I64_CLZ:
  2955. case WASM_OP_I64_CTZ:
  2956. case WASM_OP_I64_POPCNT:
  2957. POP_I64();
  2958. PUSH_I64();
  2959. break;
  2960. case WASM_OP_I64_ADD:
  2961. case WASM_OP_I64_SUB:
  2962. case WASM_OP_I64_MUL:
  2963. case WASM_OP_I64_DIV_S:
  2964. case WASM_OP_I64_DIV_U:
  2965. case WASM_OP_I64_REM_S:
  2966. case WASM_OP_I64_REM_U:
  2967. case WASM_OP_I64_AND:
  2968. case WASM_OP_I64_OR:
  2969. case WASM_OP_I64_XOR:
  2970. case WASM_OP_I64_SHL:
  2971. case WASM_OP_I64_SHR_S:
  2972. case WASM_OP_I64_SHR_U:
  2973. case WASM_OP_I64_ROTL:
  2974. case WASM_OP_I64_ROTR:
  2975. POP_I64();
  2976. POP_I64();
  2977. PUSH_I64();
  2978. break;
  2979. case WASM_OP_F32_ABS:
  2980. case WASM_OP_F32_NEG:
  2981. case WASM_OP_F32_CEIL:
  2982. case WASM_OP_F32_FLOOR:
  2983. case WASM_OP_F32_TRUNC:
  2984. case WASM_OP_F32_NEAREST:
  2985. case WASM_OP_F32_SQRT:
  2986. POP_F32();
  2987. PUSH_F32();
  2988. break;
  2989. case WASM_OP_F32_ADD:
  2990. case WASM_OP_F32_SUB:
  2991. case WASM_OP_F32_MUL:
  2992. case WASM_OP_F32_DIV:
  2993. case WASM_OP_F32_MIN:
  2994. case WASM_OP_F32_MAX:
  2995. case WASM_OP_F32_COPYSIGN:
  2996. POP_F32();
  2997. POP_F32();
  2998. PUSH_F32();
  2999. break;
  3000. case WASM_OP_F64_ABS:
  3001. case WASM_OP_F64_NEG:
  3002. case WASM_OP_F64_CEIL:
  3003. case WASM_OP_F64_FLOOR:
  3004. case WASM_OP_F64_TRUNC:
  3005. case WASM_OP_F64_NEAREST:
  3006. case WASM_OP_F64_SQRT:
  3007. POP_F64();
  3008. PUSH_F64();
  3009. break;
  3010. case WASM_OP_F64_ADD:
  3011. case WASM_OP_F64_SUB:
  3012. case WASM_OP_F64_MUL:
  3013. case WASM_OP_F64_DIV:
  3014. case WASM_OP_F64_MIN:
  3015. case WASM_OP_F64_MAX:
  3016. case WASM_OP_F64_COPYSIGN:
  3017. POP_F64();
  3018. POP_F64();
  3019. PUSH_F64();
  3020. break;
  3021. case WASM_OP_I32_WRAP_I64:
  3022. POP_I64();
  3023. PUSH_I32();
  3024. break;
  3025. case WASM_OP_I32_TRUNC_S_F32:
  3026. case WASM_OP_I32_TRUNC_U_F32:
  3027. POP_F32();
  3028. PUSH_I32();
  3029. break;
  3030. case WASM_OP_I32_TRUNC_S_F64:
  3031. case WASM_OP_I32_TRUNC_U_F64:
  3032. POP_F64();
  3033. PUSH_I32();
  3034. break;
  3035. case WASM_OP_I64_EXTEND_S_I32:
  3036. case WASM_OP_I64_EXTEND_U_I32:
  3037. POP_I32();
  3038. PUSH_I64();
  3039. break;
  3040. case WASM_OP_I64_TRUNC_S_F32:
  3041. case WASM_OP_I64_TRUNC_U_F32:
  3042. POP_F32();
  3043. PUSH_I64();
  3044. break;
  3045. case WASM_OP_I64_TRUNC_S_F64:
  3046. case WASM_OP_I64_TRUNC_U_F64:
  3047. POP_F64();
  3048. PUSH_I64();
  3049. break;
  3050. case WASM_OP_F32_CONVERT_S_I32:
  3051. case WASM_OP_F32_CONVERT_U_I32:
  3052. POP_I32();
  3053. PUSH_F32();
  3054. break;
  3055. case WASM_OP_F32_CONVERT_S_I64:
  3056. case WASM_OP_F32_CONVERT_U_I64:
  3057. POP_I64();
  3058. PUSH_F32();
  3059. break;
  3060. case WASM_OP_F32_DEMOTE_F64:
  3061. POP_F64();
  3062. PUSH_F32();
  3063. break;
  3064. case WASM_OP_F64_CONVERT_S_I32:
  3065. case WASM_OP_F64_CONVERT_U_I32:
  3066. POP_I32();
  3067. PUSH_F64();
  3068. break;
  3069. case WASM_OP_F64_CONVERT_S_I64:
  3070. case WASM_OP_F64_CONVERT_U_I64:
  3071. POP_I64();
  3072. PUSH_F64();
  3073. break;
  3074. case WASM_OP_F64_PROMOTE_F32:
  3075. POP_F32();
  3076. PUSH_F64();
  3077. break;
  3078. case WASM_OP_I32_REINTERPRET_F32:
  3079. POP_F32();
  3080. PUSH_I32();
  3081. break;
  3082. case WASM_OP_I64_REINTERPRET_F64:
  3083. POP_F64();
  3084. PUSH_I64();
  3085. break;
  3086. case WASM_OP_F32_REINTERPRET_I32:
  3087. POP_I32();
  3088. PUSH_F32();
  3089. break;
  3090. case WASM_OP_F64_REINTERPRET_I64:
  3091. POP_I64();
  3092. PUSH_F64();
  3093. break;
  3094. default:
  3095. if (error_buf != NULL)
  3096. snprintf(error_buf, error_buf_size,
  3097. "WASM module load failed: "
  3098. "invalid opcode %02x.", opcode);
  3099. goto fail;
  3100. }
  3101. if (opcode != WASM_OP_I32_CONST)
  3102. is_i32_const = false;
  3103. }
  3104. if (csp_num > 0) {
  3105. set_error_buf(error_buf, error_buf_size,
  3106. "WASM module load failed: "
  3107. "function body must end with END opcode.");
  3108. goto fail;
  3109. }
  3110. func->max_stack_cell_num = max_stack_cell_num;
  3111. func->max_block_num = max_csp_num;
  3112. return_value = true;
  3113. fail:
  3114. if (frame_ref_bottom)
  3115. wasm_free(frame_ref_bottom);
  3116. if (frame_csp_bottom)
  3117. wasm_free(frame_csp_bottom);
  3118. (void)u8;
  3119. (void)u32;
  3120. (void)i32;
  3121. (void)i64;
  3122. (void)local_offset;
  3123. (void)p_org;
  3124. return return_value;
  3125. }